diff --git a/EightBit/BigEndianProcessor.cs b/EightBit/BigEndianProcessor.cs index 0257dd8..96b48c3 100644 --- a/EightBit/BigEndianProcessor.cs +++ b/EightBit/BigEndianProcessor.cs @@ -37,8 +37,8 @@ protected override ushort GetWordPaged(byte page, byte offset) { - var high = GetBytePaged(page, offset); - var low = GetBytePaged(page, (byte)(offset + 1)); + var high = BusRead(offset, page); + var low = BusRead((byte)(offset + 1), page); return MakeWord(low, high); } @@ -64,8 +64,8 @@ protected override void SetWordPaged(byte page, byte offset, ushort value) { - SetBytePaged(page, offset, HighByte(value)); - SetBytePaged(page, (byte)(offset + 1), LowByte(value)); + BusWrite(offset, page, HighByte(value)); + BusWrite((byte)(offset + 1), page, LowByte(value)); } } } diff --git a/EightBit/Bits.cs b/EightBit/Bits.cs index 84e0e82..1150b2d 100644 --- a/EightBit/Bits.cs +++ b/EightBit/Bits.cs @@ -1,7 +1,7 @@ -using System; - -namespace EightBit +namespace EightBit { + using System; + [Flags] public enum Bits { diff --git a/EightBit/Bus.cs b/EightBit/Bus.cs index bc5564e..9484ecd 100644 --- a/EightBit/Bus.cs +++ b/EightBit/Bus.cs @@ -13,17 +13,8 @@ public event EventHandler ReadingByte; public event EventHandler ReadByte; - public byte Data - { - get { return data; } - set { data = value; } - } - - public ushort Address - { - get { return address; } - set { address = value; } - } + public byte Data { get => data; set => data = value; } + public ushort Address { get => address; set => address = value; } public abstract MemoryMapping Mapping(ushort absolute); @@ -49,10 +40,7 @@ return Read(); } - public byte Read(byte low, byte high) - { - return Read(Chip.MakeWord(low, high)); - } + public byte Read(byte low, byte high) => Read(Chip.MakeWord(low, high)); public void Write() { @@ -73,10 +61,7 @@ Write(value); } - public void Write(byte low, byte high, byte value) - { - Write(Chip.MakeWord(low, high), value); - } + public void Write(byte low, byte high, byte value) => Write(Chip.MakeWord(low, high), value); public virtual void RaisePOWER() {} public virtual void LowerPOWER() {} @@ -103,10 +88,7 @@ protected ref byte Reference() => ref Reference(Address); - protected ref byte Reference(byte low, byte high) - { - return ref Reference(Chip.MakeWord(low, high)); - } + protected ref byte Reference(byte low, byte high) => ref Reference(Chip.MakeWord(low, high)); //[[nodiscard]] static std::map> parseHexFile(std::string path); //void loadHexFile(std::string path); diff --git a/EightBit/Device.cs b/EightBit/Device.cs index ac6ca2e..0e5c7dc 100644 --- a/EightBit/Device.cs +++ b/EightBit/Device.cs @@ -13,7 +13,7 @@ public event EventHandler LoweringPOWER; public event EventHandler LoweredPOWER; - public ref PinLevel POWER() { return ref powerLine; } + public ref PinLevel POWER() => ref powerLine; public bool Powered => POWER().Raised(); diff --git a/EightBit/InputOutput.cs b/EightBit/InputOutput.cs index 7afde5c..05bed00 100644 --- a/EightBit/InputOutput.cs +++ b/EightBit/InputOutput.cs @@ -4,13 +4,11 @@ public sealed class InputOutput { - private byte[] input; - private byte[] output; + private byte[] input = new byte[0x100]; + private byte[] output = new byte[0x100]; public InputOutput() { - input = new byte[0x100]; - output = new byte[0x100]; } public event EventHandler ReadingPort; @@ -19,8 +17,8 @@ public event EventHandler WritingPort; public event EventHandler WrittenPort; - byte Read(byte port) { return ReadInputPort(port); } - void Write(byte port, byte value) { WriteOutputPort(port, value); } + byte Read(byte port) => ReadInputPort(port); + void Write(byte port, byte value) => WriteOutputPort(port, value); byte ReadInputPort(byte port) { diff --git a/EightBit/IntelProcessor.cs b/EightBit/IntelProcessor.cs index 3f7dc6b..e3fca90 100644 --- a/EightBit/IntelProcessor.cs +++ b/EightBit/IntelProcessor.cs @@ -4,7 +4,7 @@ public abstract class IntelProcessor : LittleEndianProcessor { - private readonly IntelOpCodeDecoded[] decodedOpCodes; + private readonly IntelOpCodeDecoded[] decodedOpCodes = new IntelOpCodeDecoded[0x100]; private ushort sp; private ushort memptr; @@ -13,7 +13,6 @@ protected IntelProcessor(Bus bus) : base(bus) { - decodedOpCodes = new IntelOpCodeDecoded[0x100]; sp = (ushort)Mask.Mask16; memptr = (ushort)Mask.Mask16; @@ -26,7 +25,7 @@ public event EventHandler LoweringHALT; public event EventHandler LoweredHALT; - public ref PinLevel HALT() { return ref haltLine; } + public ref PinLevel HALT() => ref haltLine; protected bool Halted => HALT().Lowered(); @@ -34,20 +33,20 @@ public ushort MEMPTR { get => memptr; set => memptr = value; } public abstract ushort AF { get; set; } - public byte A { get { return HighByte(AF); } set { AF = (ushort)(LowerPart(AF) | PromoteByte(value)); } } - public byte F { get { return LowByte(AF); } set { AF = (ushort)(HigherPart(AF) | value); } } + public byte A { get => HighByte(AF); set => AF = (ushort)(LowerPart(AF) | PromoteByte(value)); } + public byte F { get => LowByte(AF); set => AF = (ushort)(HigherPart(AF) | value); } public abstract ushort BC { get; set; } - public byte B { get { return HighByte(AF); } set { BC = (ushort)(LowerPart(BC) | PromoteByte(value)); } } - public byte C { get { return LowByte(AF); } set { BC = (ushort)(HigherPart(BC) | value); } } + public byte B { get => HighByte(AF); set => BC = (ushort)(LowerPart(BC) | PromoteByte(value)); } + public byte C { get => LowByte(AF); set => BC = (ushort)(HigherPart(BC) | value); } public abstract ushort DE { get; set; } - public byte D { get { return HighByte(AF); } set { DE = (ushort)(LowerPart(DE) | PromoteByte(value)); } } - public byte E { get { return LowByte(AF); } set { DE = (ushort)(HigherPart(DE) | value); } } + public byte D { get => HighByte(DE); set => DE = (ushort)(LowerPart(DE) | PromoteByte(value)); } + public byte E { get => LowByte(DE); set => DE = (ushort)(HigherPart(DE) | value); } public abstract ushort HL { get; set; } - public byte H { get { return HighByte(AF); } set { HL = (ushort)(LowerPart(AF) | PromoteByte(value)); } } - public byte L { get { return LowByte(AF); } set { HL = (ushort)(HigherPart(AF) | value); } } + public byte H { get => HighByte(HL); set => HL = (ushort)(LowerPart(HL) | PromoteByte(value)); } + public byte L { get => LowByte(HL); set { HL = (ushort)(HigherPart(HL) | value); } } public override void RaisePOWER() { diff --git a/EightBit/LittleEndianProcessor.cs b/EightBit/LittleEndianProcessor.cs index d04747a..eaf0a0e 100644 --- a/EightBit/LittleEndianProcessor.cs +++ b/EightBit/LittleEndianProcessor.cs @@ -37,8 +37,8 @@ protected override ushort GetWordPaged(byte page, byte offset) { - var low = GetBytePaged(page, offset); - var high = GetBytePaged(page, (byte)(offset + 1)); + var low = BusRead(offset, page); + var high = BusRead((byte)(offset + 1), page); return MakeWord(low, high); } @@ -64,8 +64,8 @@ protected override void SetWordPaged(byte page, byte offset, ushort value) { - SetBytePaged(page, offset, LowByte(value)); - SetBytePaged(page, (byte)(offset + 1), HighByte(value)); + BusWrite(offset, page, LowByte(value)); + BusWrite((byte)(offset + 1), page, HighByte(value)); } } } diff --git a/EightBit/Processor.cs b/EightBit/Processor.cs index ae990e3..048fd48 100644 --- a/EightBit/Processor.cs +++ b/EightBit/Processor.cs @@ -31,8 +31,8 @@ protected byte OpCode { get => opcode; set => opcode = value; } public Bus Bus { get => bus; set => bus = value; } - public ref PinLevel RESET() { return ref resetLine; } - public ref PinLevel INT() { return ref intLine; } + public ref PinLevel RESET() => ref resetLine; + public ref PinLevel INT() => ref intLine; public abstract int Step(); public abstract int Execute(); @@ -101,10 +101,7 @@ #region BusWrite - protected void BusWrite(byte low, byte high, byte data) - { - BusWrite(MakeWord(low, high), data); - } + protected void BusWrite(byte low, byte high, byte data) => BusWrite(MakeWord(low, high), data); protected void BusWrite(ushort address, byte data) { @@ -124,10 +121,7 @@ #region BusRead - protected byte BusRead(byte low, byte high) - { - return BusRead(MakeWord(low, high)); - } + protected byte BusRead(byte low, byte high) => BusRead(MakeWord(low, high)); protected byte BusRead(ushort address) { @@ -139,14 +133,6 @@ #endregion - #region Paged reader/writer wrappers - - protected byte GetBytePaged(byte page, byte offset) => BusRead(offset, page); - - protected void SetBytePaged(byte page, byte offset, byte value) => BusWrite(offset, page, value); - - #endregion - protected byte FetchByte() => BusRead(PC++); protected abstract ushort GetWord(); @@ -175,10 +161,7 @@ SetWord(value); } - protected void Jump(ushort destination) - { - PC = destination; - } + protected void Jump(ushort destination) => PC = destination; protected void Call(ushort destination) { @@ -186,9 +169,6 @@ Jump(destination); } - protected virtual void Return() - { - Jump(PopWord()); - } + protected virtual void Return() => Jump(PopWord()); } } diff --git a/M6502/M6502.cs b/M6502/M6502.cs index d8ca2bd..67198c0 100644 --- a/M6502/M6502.cs +++ b/M6502/M6502.cs @@ -213,7 +213,7 @@ case 0x25: A = AndR(A, AM_ZeroPage()); break; // AND (zero page) case 0x26: BusReadModifyWrite(ROL(AM_ZeroPage())); break; // ROL (zero page) case 0x27: RLA(AM_ZeroPage()); break; // *RLA (zero page) - case 0x28: BusRead(); GetBytePaged(1, S); PLP(); break; // PLP (implied) + case 0x28: BusRead(); BusRead(S, 1); PLP(); break; // PLP (implied) case 0x29: A = AndR(A, AM_Immediate()); break; // AND (immediate) case 0x2a: BusRead(); A = ROL(A); break; // ROL A (implied) case 0x2b: ANC(AM_Immediate()); break; // *ANC (immediate) @@ -281,7 +281,7 @@ case 0x65: A = ADC(A, AM_ZeroPage()); break; // ADC (zero page) case 0x66: BusReadModifyWrite(ROR(AM_ZeroPage())); break; // ROR (zero page) case 0x67: RRA(AM_ZeroPage()); break; // *RRA (zero page) - case 0x68: BusRead(); GetBytePaged(1, S); A = Through(Pop()); break; // PLA (implied) + case 0x68: BusRead(); BusRead(S, 1); A = Through(Pop()); break; // PLA (implied) case 0x69: A = ADC(A, AM_Immediate()); break; // ADC (immediate) case 0x6a: BusRead(); A = ROR(A); break; // ROR A (implied) case 0x6b: ARR(AM_Immediate()); break; // *ARR (immediate) @@ -472,9 +472,9 @@ return Cycles; } - protected override byte Pop() => GetBytePaged(1, ++S); + protected override byte Pop() => BusRead(++S, 1); - protected override void Push(byte value) => SetBytePaged(1, S--, value); + protected override void Push(byte value) => BusWrite(S--, 1, value); protected override sealed void HandleRESET() { @@ -608,7 +608,7 @@ var crossed = Address_AbsoluteX(); var address = crossed.Item1; var page = crossed.Item2; - var possible = GetBytePaged(page, LowByte(address)); + var possible = BusRead(LowByte(address), page); if ((behaviour == PageCrossingBehavior.AlwaysReadTwice) || (page != HighByte(address))) possible = BusRead(address); return possible; @@ -619,7 +619,7 @@ var crossed = Address_AbsoluteY(); var address = crossed.Item1; var page = crossed.Item2; - var possible = GetBytePaged(page, LowByte(address)); + var possible = BusRead(LowByte(address), page); if (page != HighByte(address)) possible = BusRead(address); return possible; @@ -636,7 +636,7 @@ var crossed = Address_IndirectIndexedY(); var address = crossed.Item1; var page = crossed.Item2; - var possible = GetBytePaged(page, LowByte(address)); + var possible = BusRead(LowByte(address), page); if (page != HighByte(address)) possible = BusRead(address); return possible; @@ -803,7 +803,7 @@ private void JSR() { var low = FetchByte(); - GetBytePaged(1, S); // dummy read + BusRead(S, 1); // dummy read PushWord(PC); PC = MakeWord(low, FetchByte()); } @@ -838,14 +838,14 @@ private void RTI() { - GetBytePaged(1, S); // dummy read + BusRead(S, 1); // dummy read PLP(); Return(); } private void RTS() { - GetBytePaged(1, S); // dummy read + BusRead(S, 1); // dummy read Return(); FetchByte(); } @@ -921,7 +921,7 @@ var crossed = Address_AbsoluteX(); var address = crossed.Item1; var page = crossed.Item2; - GetBytePaged(page, LowByte(address)); + BusRead(LowByte(address), page); BusWrite(address, A); } @@ -930,7 +930,7 @@ var crossed = Address_AbsoluteY(); var address = crossed.Item1; var page = crossed.Item2; - GetBytePaged(page, LowByte(address)); + BusRead(LowByte(address), page); BusWrite(address, A); } } diff --git a/Test_M6502/Board.cs b/Test_M6502/Board.cs index 7ef3613..566ce01 100644 --- a/Test_M6502/Board.cs +++ b/Test_M6502/Board.cs @@ -56,8 +56,7 @@ CPU.ExecutedInstruction += CPU_ExecutedInstruction; Poke(0x00, 0x4c); - Poke(0x01, Chip.LowByte(configuration.StartAddress)); - Poke(0x02, Chip.HighByte(configuration.StartAddress)); + cpu.PokeWord(0x01, configuration.StartAddress); } private void CPU_ExecutedInstruction(object sender, System.EventArgs e)