From e62b66dd08ca445d1f7e5b802a503ad3ec83b2df Mon Sep 17 00:00:00 2001 From: Melodi <62381002+Melodi17@users.noreply.github.com> Date: Fri, 8 Aug 2025 23:57:26 +1000 Subject: added branching to compiler and comment generation --- Emulator/Chip/Q1Cpu.cs | 12 ++++-------- Emulator/Chip/Q1CpuInstructions.cs | 40 +++++++++++++++++--------------------- Emulator/Chip/RamDevice.cs | 6 +++--- Emulator/Program.cs | 14 ++++++------- Emulator/Q1Layout.cs | 15 ++++++++++++++ 5 files changed, 47 insertions(+), 40 deletions(-) create mode 100644 Emulator/Q1Layout.cs (limited to 'Emulator') diff --git a/Emulator/Chip/Q1Cpu.cs b/Emulator/Chip/Q1Cpu.cs index d1fcf7a..16489e4 100644 --- a/Emulator/Chip/Q1Cpu.cs +++ b/Emulator/Chip/Q1Cpu.cs @@ -5,10 +5,6 @@ using Assembler; public partial class Q1Cpu { - public const u16 StackStart = 0x2FFF; - public const u16 StackLength = 16 * 2; // 16 16-bit values - public const u16 ProgramStart = 0x1FFF; - public Random Random; public u16 AX = 0; // Accumulator register @@ -35,10 +31,10 @@ public partial class Q1Cpu this.V = new u16[16]; - this.SP = Q1Cpu.StackStart; + this.SP = Q1Layout.StackStart; this.PushStack(0xFFFF); - this.PC = Q1Cpu.ProgramStart; + this.PC = Q1Layout.ProgramStart; } public void Clock() @@ -88,7 +84,7 @@ public partial class Q1Cpu private void PushStack(u16 value) { - if (this.SP + 2 >= Q1Cpu.StackStart + Q1Cpu.StackLength) + if (this.SP + 2 >= Q1Layout.StackStart + Q1Layout.StackLength) throw new InvalidOperationException("Stack overflow"); this.Bus.WriteWord(this.SP, value); @@ -97,7 +93,7 @@ public partial class Q1Cpu private u16 PopStack() { - if (this.SP - 2 < Q1Cpu.StackStart) + if (this.SP - 2 < Q1Layout.StackStart) throw new InvalidOperationException("Stack underflow"); this.SP -= 2; diff --git a/Emulator/Chip/Q1CpuInstructions.cs b/Emulator/Chip/Q1CpuInstructions.cs index 2da8969..72d78e5 100644 --- a/Emulator/Chip/Q1CpuInstructions.cs +++ b/Emulator/Chip/Q1CpuInstructions.cs @@ -14,8 +14,10 @@ public partial class Q1Cpu { 0x04, ["NOP", "POP", "NOP", "NOP"] }, { 0x10, ["INV", "INV", "NOP", "AND"] }, - { 0x11, ["SHL", "SHL", "NOP", "OR"] }, - { 0x12, ["SHR", "SHR", "NOP", "XOR"] }, + { 0x11, ["NOP", "NOP", "NOP", "OR"] }, + { 0x12, ["NOP", "NOP", "NOP", "XOR"] }, + { 0x13, ["NOP", "NOP", "NOP", "SHL"] }, + { 0x14, ["NOP", "NOP", "NOP", "SHR"] }, { 0x20, ["NOT", "NOT", "NOP", "CMP"] }, { 0x21, ["NOP", "NOP", "NOP", "LT"] }, @@ -40,8 +42,10 @@ public partial class Q1Cpu { 0x04, (this.Instruction_NOP, this.Instruction_POP, this.Instruction_NOP, this.Instruction_NOP) }, { 0x10, (this.Instruction_INV_DX, this.Instruction_INV, this.Instruction_NOP, this.Instruction_AND) }, - { 0x11, (this.Instruction_SHL_DX, this.Instruction_SHL, this.Instruction_NOP, this.Instruction_OR) }, - { 0x12, (this.Instruction_SHR_DX, this.Instruction_SHR, this.Instruction_NOP, this.Instruction_XOR) }, + { 0x11, (this.Instruction_NOP, this.Instruction_NOP, this.Instruction_NOP, this.Instruction_OR) }, + { 0x12, (this.Instruction_NOP, this.Instruction_NOP, this.Instruction_NOP, this.Instruction_XOR) }, + { 0x13, (this.Instruction_NOP, this.Instruction_NOP, this.Instruction_NOP, this.Instruction_SHL) }, + { 0x14, (this.Instruction_NOP, this.Instruction_NOP, this.Instruction_NOP, this.Instruction_SHR) }, { 0x20, (this.Instruction_NOT_LX, this.Instruction_NOT, this.Instruction_NOP, this.Instruction_CMP) }, { 0x21, (this.Instruction_NOP, this.Instruction_NOP, this.Instruction_NOP, this.Instruction_LT) }, @@ -118,27 +122,19 @@ public partial class Q1Cpu { this.AX = (u16) (this.AX - 1); } - private void Instruction_SHL(u8 obj) + private void Instruction_SHL(u8 m1, u8 m2) { - var address = this.Address(obj); - u16 value = address.read(); - u16 result = (u16) (value << 1); - address.write(result); - } - private void Instruction_SHR(u8 obj) - { - var address = this.Address(obj); - u16 value = address.read(); - u16 result = (u16) (value >> 1); - address.write(result); - } - private void Instruction_SHL_DX() - { - this.DX = (u16) (this.DX << 1); + u16 v1 = this.Address(m1).read(); + u16 v2 = this.Address(m1).read(); + u16 shifted = (u16) (v1 << v2); + this.DX = shifted; } - private void Instruction_SHR_DX() + private void Instruction_SHR(u8 m1, u8 m2) { - this.DX = (u16) (this.DX >> 1); + u16 v1 = this.Address(m1).read(); + u16 v2 = this.Address(m1).read(); + u16 shifted = (u16) (v1 >> v2); + this.DX = shifted; } private void Instruction_XOR(u8 m1, u8 m2) { diff --git a/Emulator/Chip/RamDevice.cs b/Emulator/Chip/RamDevice.cs index 8d5b051..a40713a 100644 --- a/Emulator/Chip/RamDevice.cs +++ b/Emulator/Chip/RamDevice.cs @@ -6,14 +6,14 @@ public class RamDevice : BusDevice public RamDevice(u16 start, u16 size) : base(start, (u16) (start + size)) { - this.Memory = new u8[size]; + this.Memory = new u8[size]; } - + public override void Reset() { this.Memory = new u8[this.AddressableEnd - this.AddressableStart]; } - + public override void Clock() { // No clocking needed for RAM diff --git a/Emulator/Program.cs b/Emulator/Program.cs index 3104fa9..c5ae852 100644 --- a/Emulator/Program.cs +++ b/Emulator/Program.cs @@ -18,10 +18,10 @@ internal class Program { Q1Cpu cpu = new(); Bus bus = new(u16.MinValue, u16.MaxValue); - RamDevice ram = new(0x1FFF, 1024 * 8); - File.ReadAllBytes(options.InputFile).CopyTo(ram.Memory, 0); + RamDevice rom = new(Q1Layout.Rom.start, Q1Layout.Rom.size); + File.ReadAllBytes(options.InputFile).CopyTo(rom.Memory, 0); - DisplayDevice display = new(16, 64, 32, 0x3FFF); + DisplayDevice display = new(16, 64, 32, Q1Layout.Display); display.SetPalette([ Color.Red, Color.Green, @@ -41,12 +41,12 @@ internal class Program Color.LightGreen ]); - DebugDevice debug = new(0xEFFF); - + RamDevice memory = new(Q1Layout.Memory.start, Q1Layout.Memory.size); + cpu.Bus = bus; - ram.AttachToBus(bus); + rom.AttachToBus(bus); display.AttachToBus(bus); - debug.AttachToBus(bus); + memory.AttachToBus(bus); Thread t = new(() => { diff --git a/Emulator/Q1Layout.cs b/Emulator/Q1Layout.cs new file mode 100644 index 0000000..c3ccd08 --- /dev/null +++ b/Emulator/Q1Layout.cs @@ -0,0 +1,15 @@ +namespace Q1.Emulator; + +public static class Q1Layout +{ + // Bus locations + public static readonly (u16 start, u16 size) Rom = (0x1FFF, 1024 * 8); + public static readonly (u16 start, u16 size) Memory = (0x4FFF, 1024 * 16); + public static readonly u16 Display = 0x3FFF; + + + public static readonly u16 StackStart = Q1Layout.Memory.start; + public static readonly u16 StackLength = 256 * 2; // 256 16-bit values + public static readonly u16 FreeMemoryStart = (u16) (Q1Layout.StackStart + Q1Layout.StackLength); + public static readonly u16 ProgramStart = Q1Layout.Rom.start; +} \ No newline at end of file -- cgit v1.3.1