summaryrefslogtreecommitdiff
path: root/Chip
diff options
context:
space:
mode:
Diffstat (limited to 'Chip')
-rw-r--r--Chip/Bus.cs2
-rw-r--r--Chip/DisplayDevice.cs15
-rw-r--r--Chip/Q1Cpu.cs3
-rw-r--r--Chip/Q1CpuAddressingModes.cs130
4 files changed, 119 insertions, 31 deletions
diff --git a/Chip/Bus.cs b/Chip/Bus.cs
index e8cf5ad..951d23f 100644
--- a/Chip/Bus.cs
+++ b/Chip/Bus.cs
@@ -39,7 +39,7 @@ public class Bus : BusDevice
{
BusDevice? device = this.GetDevice(address);
if (device == null)
- return [];
+ return new u8[length];
return device.ReadSeq(address, length);
}
diff --git a/Chip/DisplayDevice.cs b/Chip/DisplayDevice.cs
index ea34cfb..28bf4e0 100644
--- a/Chip/DisplayDevice.cs
+++ b/Chip/DisplayDevice.cs
@@ -2,6 +2,7 @@ namespace Q1Emu.Chip;
using System;
using System.Collections.Generic;
+using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@@ -11,6 +12,7 @@ public class DisplayDevice : RamDevice
private readonly u16 _paletteSize;
private readonly u16 _displayWidth;
private readonly u16 _displayHeight;
+ private Color[] textureData;
public DisplayDevice(u16 paletteSize, u16 displayWidth, u16 displayHeight, u16 addressableStart)
: base(addressableStart, DisplayDevice.DetermineSize(paletteSize, displayWidth, displayHeight))
@@ -18,6 +20,7 @@ public class DisplayDevice : RamDevice
this._paletteSize = paletteSize;
this._displayWidth = displayWidth;
this._displayHeight = displayHeight;
+ this.textureData = new Color[this._displayWidth * this._displayHeight];
}
private static u16 DetermineSize(u16 paletteSize, u16 displayWidth, u16 displayHeight)
@@ -30,7 +33,6 @@ public class DisplayDevice : RamDevice
Color[] palette = this.ReadPalette();
ref8 pixelData = this.ReadSeq((u16) (this.AddressableStart + this._paletteSize * 3), (u16) (this._displayWidth * this._displayHeight));
- Color[] textureData = new Color[this._displayWidth * this._displayHeight];
for (u16 i = 0; i < textureData.Length; i++)
{
u8 pixelValue = pixelData[i];
@@ -73,4 +75,15 @@ public class DisplayDevice : RamDevice
paletteData[offset + 2] = palette[i].B;
}
}
+
+ public override void Write(u16 address, u8 value)
+ {
+ Console.WriteLine($"DisplayDevice.Write: {address:X4} = {value:X2}");
+ base.Write(address, value);
+ }
+ public override void WriteSeq(u16 address, u16 length, ref8 value)
+ {
+ Console.WriteLine($"DisplayDevice.WriteSeq: {address:X4} length={length} value={Make.u16(value)}");
+ base.WriteSeq(address, length, value);
+ }
} \ No newline at end of file
diff --git a/Chip/Q1Cpu.cs b/Chip/Q1Cpu.cs
index 1124578..910ad64 100644
--- a/Chip/Q1Cpu.cs
+++ b/Chip/Q1Cpu.cs
@@ -3,6 +3,7 @@
namespace Q1Emu.Chip;
using System.Collections.Generic;
+using Assembler;
public partial class Q1Cpu
{
@@ -42,7 +43,9 @@ public partial class Q1Cpu
public void Clock()
{
+ var pc = this.PC;
u16 instruction = this.FetchInstruction(out u8 op, out u8 m1, out u8 m2, out _);
+ Console.WriteLine($"{pc:X4}: {Disassembler.ParseInstruction(instruction)}");
bool ex = m2 > 0;
switch (m1, ex)
diff --git a/Chip/Q1CpuAddressingModes.cs b/Chip/Q1CpuAddressingModes.cs
index 0d664f7..f809ff3 100644
--- a/Chip/Q1CpuAddressingModes.cs
+++ b/Chip/Q1CpuAddressingModes.cs
@@ -23,40 +23,55 @@ public partial class Q1Cpu
public void StoreData(u8 mode, u16 value)
{
- Console.WriteLine($"StoreData: mode={mode:X8}, value={value:X16}");
- if (mode != 0x05)
+ switch (mode)
{
- u16 address = this.FetchData(mode);
- Console.WriteLine($"StoreData: address={address:X16}");
- this.Bus.WriteWord(address, value);
- }
- else
- {
- u8 regIndex = this.Bus.Read(this.PC);
- this.PC += 1;
-
- this.StoreRegister(regIndex, value);
+ case 0x0:
+ throw new InvalidOperationException("Cannot store data in implicit mode.");
+ case 0x1:
+ throw new InvalidOperationException("Cannot store data in immediate mode.");
+ case 0x2:
+ this.AddressingMode_Direct_Store(value);
+ break;
+ case 0x3:
+ this.AddressingMode_Indirect_Store(value);
+ break;
+ case 0x4:
+ this.AddressingMode_Relative_Store(value);
+ break;
+ case 0x5:
+ this.AddressingMode_Register_Store(value);
+ break;
+ case 0x6:
+ this.AddressingMode_RegisterIndirect_Store(value);
+ break;
+ case 0x7:
+ this.AddressingMode_RegisterIndirectOffset_Store(value);
+ break;
+ default:
+ throw new InvalidOperationException($"Unknown addressing mode: {mode:X2}");
}
}
public void TransformData(u8 mode, Func<u16, u16> transform)
{
- if (mode != 0x05)
- {
- u16 address = this.FetchData(mode);
- u16 value = this.Bus.ReadWord(address);
- u16 transformedValue = transform(value);
- this.Bus.WriteWord(address, transformedValue);
- }
- else
- {
- u8 regIndex = this.Bus.Read(this.PC);
- this.PC += 1;
-
- u16 value = this.FetchRegister(regIndex);
- u16 transformedValue = transform(value);
- this.StoreRegister(regIndex, transformedValue);
- }
+ // if (mode != 0x05)
+ // {
+ // u16 address = this.FetchData(mode);
+ // u16 value = this.Bus.ReadWord(address);
+ // u16 transformedValue = transform(value);
+ // this.Bus.WriteWord(address, transformedValue);
+ // }
+ // else
+ // {
+ // u8 regIndex = this.Bus.Read(this.PC);
+ // this.PC += 1;
+ //
+ // u16 value = this.FetchRegister(regIndex);
+ // u16 transformedValue = transform(value);
+ // this.StoreRegister(regIndex, transformedValue);
+ // }
+
+ throw new NotImplementedException("TransformData is not implemented yet.");
}
public u8 AddressingModeLength(u8 mode)
@@ -132,6 +147,7 @@ public partial class Q1Cpu
u16 address = this.FetchRegister(regIndex);
u16 value = this.Bus.ReadWord(address);
+ Console.WriteLine($"Value at {address:X4}: {value:X4}");
return value;
}
@@ -140,11 +156,67 @@ public partial class Q1Cpu
u8 regIndex = this.Bus.Read(this.PC);
this.PC += 1;
- u16 offset = this.Bus.ReadWord(this.PC);
+ i16 offset = Make.i16(this.Bus.ReadSeq(this.PC, 2));
+ Console.WriteLine("Offset: " + offset);
this.PC += 2;
u16 address = (u16) (this.FetchRegister(regIndex) + offset);
u16 value = this.Bus.ReadWord(address);
return value;
}
+
+ public void AddressingMode_Direct_Store(u16 data)
+ {
+ u16 address = this.Bus.ReadWord(this.PC);
+ this.PC += 2;
+
+ this.Bus.WriteWord(address, data);
+ }
+
+ public void AddressingMode_Indirect_Store(u16 data)
+ {
+ u16 address = this.Bus.ReadWord(this.PC);
+ this.PC += 2;
+
+ u16 indirectAddress = this.Bus.ReadWord(address);
+ this.Bus.WriteWord(indirectAddress, data);
+ }
+
+ public void AddressingMode_Relative_Store(u16 data)
+ {
+ u16 offset = this.Bus.ReadWord(this.PC);
+ this.PC += 2;
+
+ u16 address = (u16) (this.PC + offset);
+ this.Bus.WriteWord(address, data);
+ }
+
+ public void AddressingMode_Register_Store(u16 data)
+ {
+ u8 regIndex = this.Bus.Read(this.PC);
+ this.PC += 1;
+
+ this.StoreRegister(regIndex, data);
+ }
+
+ public void AddressingMode_RegisterIndirect_Store(u16 data)
+ {
+ u8 regIndex = this.Bus.Read(this.PC);
+ this.PC += 1;
+
+ u16 address = this.FetchRegister(regIndex);
+ this.Bus.WriteWord(address, data);
+ }
+
+ public void AddressingMode_RegisterIndirectOffset_Store(u16 data)
+ {
+ u8 regIndex = this.Bus.Read(this.PC);
+ this.PC += 1;
+
+ i16 offset = Make.i16(this.Bus.ReadSeq(this.PC, 2));
+ this.PC += 2;
+
+ u16 address = (u16) (this.FetchRegister(regIndex) + offset);
+ this.Bus.WriteWord(address, data);
+ }
} \ No newline at end of file