A few small consistency updates:

1) Drop Get/SetPagedByte in favour of normal BusRead/Write
2) Tidy some "using" statements
3) More "expression body" usage, if possible
4) Use field initialisation, rather than construction, if possible
5) Correct IntelProcessor register set/get methods (there were remnants of "copy pasta" code)

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon
2019-02-03 20:29:52 +00:00
parent 0ca57d8641
commit d4a35c402c
10 changed files with 50 additions and 92 deletions

View File

@@ -37,8 +37,8 @@
protected override ushort GetWordPaged(byte page, byte offset) protected override ushort GetWordPaged(byte page, byte offset)
{ {
var high = GetBytePaged(page, offset); var high = BusRead(offset, page);
var low = GetBytePaged(page, (byte)(offset + 1)); var low = BusRead((byte)(offset + 1), page);
return MakeWord(low, high); return MakeWord(low, high);
} }
@@ -64,8 +64,8 @@
protected override void SetWordPaged(byte page, byte offset, ushort value) protected override void SetWordPaged(byte page, byte offset, ushort value)
{ {
SetBytePaged(page, offset, HighByte(value)); BusWrite(offset, page, HighByte(value));
SetBytePaged(page, (byte)(offset + 1), LowByte(value)); BusWrite((byte)(offset + 1), page, LowByte(value));
} }
} }
} }

View File

@@ -1,7 +1,7 @@
using System; namespace EightBit
namespace EightBit
{ {
using System;
[Flags] [Flags]
public enum Bits public enum Bits
{ {

View File

@@ -13,17 +13,8 @@
public event EventHandler<EventArgs> ReadingByte; public event EventHandler<EventArgs> ReadingByte;
public event EventHandler<EventArgs> ReadByte; public event EventHandler<EventArgs> ReadByte;
public byte Data public byte Data { get => data; set => data = value; }
{ public ushort Address { get => address; set => address = value; }
get { return data; }
set { data = value; }
}
public ushort Address
{
get { return address; }
set { address = value; }
}
public abstract MemoryMapping Mapping(ushort absolute); public abstract MemoryMapping Mapping(ushort absolute);
@@ -49,10 +40,7 @@
return Read(); return Read();
} }
public byte Read(byte low, byte high) public byte Read(byte low, byte high) => Read(Chip.MakeWord(low, high));
{
return Read(Chip.MakeWord(low, high));
}
public void Write() public void Write()
{ {
@@ -73,10 +61,7 @@
Write(value); Write(value);
} }
public void Write(byte low, byte high, byte value) public void Write(byte low, byte high, byte value) => Write(Chip.MakeWord(low, high), value);
{
Write(Chip.MakeWord(low, high), value);
}
public virtual void RaisePOWER() {} public virtual void RaisePOWER() {}
public virtual void LowerPOWER() {} public virtual void LowerPOWER() {}
@@ -103,10 +88,7 @@
protected ref byte Reference() => ref Reference(Address); protected ref byte Reference() => ref Reference(Address);
protected ref byte Reference(byte low, byte high) protected ref byte Reference(byte low, byte high) => ref Reference(Chip.MakeWord(low, high));
{
return ref Reference(Chip.MakeWord(low, high));
}
//[[nodiscard]] static std::map<uint16_t, std::vector<uint8_t>> parseHexFile(std::string path); //[[nodiscard]] static std::map<uint16_t, std::vector<uint8_t>> parseHexFile(std::string path);
//void loadHexFile(std::string path); //void loadHexFile(std::string path);

View File

@@ -13,7 +13,7 @@
public event EventHandler<EventArgs> LoweringPOWER; public event EventHandler<EventArgs> LoweringPOWER;
public event EventHandler<EventArgs> LoweredPOWER; public event EventHandler<EventArgs> LoweredPOWER;
public ref PinLevel POWER() { return ref powerLine; } public ref PinLevel POWER() => ref powerLine;
public bool Powered => POWER().Raised(); public bool Powered => POWER().Raised();

View File

@@ -4,13 +4,11 @@
public sealed class InputOutput public sealed class InputOutput
{ {
private byte[] input; private byte[] input = new byte[0x100];
private byte[] output; private byte[] output = new byte[0x100];
public InputOutput() public InputOutput()
{ {
input = new byte[0x100];
output = new byte[0x100];
} }
public event EventHandler<PortEventArgs> ReadingPort; public event EventHandler<PortEventArgs> ReadingPort;
@@ -19,8 +17,8 @@
public event EventHandler<PortEventArgs> WritingPort; public event EventHandler<PortEventArgs> WritingPort;
public event EventHandler<PortEventArgs> WrittenPort; public event EventHandler<PortEventArgs> WrittenPort;
byte Read(byte port) { return ReadInputPort(port); } byte Read(byte port) => ReadInputPort(port);
void Write(byte port, byte value) { WriteOutputPort(port, value); } void Write(byte port, byte value) => WriteOutputPort(port, value);
byte ReadInputPort(byte port) byte ReadInputPort(byte port)
{ {

View File

@@ -4,7 +4,7 @@
public abstract class IntelProcessor : LittleEndianProcessor public abstract class IntelProcessor : LittleEndianProcessor
{ {
private readonly IntelOpCodeDecoded[] decodedOpCodes; private readonly IntelOpCodeDecoded[] decodedOpCodes = new IntelOpCodeDecoded[0x100];
private ushort sp; private ushort sp;
private ushort memptr; private ushort memptr;
@@ -13,7 +13,6 @@
protected IntelProcessor(Bus bus) protected IntelProcessor(Bus bus)
: base(bus) : base(bus)
{ {
decodedOpCodes = new IntelOpCodeDecoded[0x100];
sp = (ushort)Mask.Mask16; sp = (ushort)Mask.Mask16;
memptr = (ushort)Mask.Mask16; memptr = (ushort)Mask.Mask16;
@@ -26,7 +25,7 @@
public event EventHandler<EventArgs> LoweringHALT; public event EventHandler<EventArgs> LoweringHALT;
public event EventHandler<EventArgs> LoweredHALT; public event EventHandler<EventArgs> LoweredHALT;
public ref PinLevel HALT() { return ref haltLine; } public ref PinLevel HALT() => ref haltLine;
protected bool Halted => HALT().Lowered(); protected bool Halted => HALT().Lowered();
@@ -34,20 +33,20 @@
public ushort MEMPTR { get => memptr; set => memptr = value; } public ushort MEMPTR { get => memptr; set => memptr = value; }
public abstract ushort AF { get; set; } public abstract ushort AF { get; set; }
public byte A { get { return HighByte(AF); } set { AF = (ushort)(LowerPart(AF) | PromoteByte(value)); } } public byte A { get => HighByte(AF); set => AF = (ushort)(LowerPart(AF) | PromoteByte(value)); }
public byte F { get { return LowByte(AF); } set { AF = (ushort)(HigherPart(AF) | value); } } public byte F { get => LowByte(AF); set => AF = (ushort)(HigherPart(AF) | value); }
public abstract ushort BC { get; set; } public abstract ushort BC { get; set; }
public byte B { get { return HighByte(AF); } set { BC = (ushort)(LowerPart(BC) | PromoteByte(value)); } } public byte B { get => HighByte(AF); set => BC = (ushort)(LowerPart(BC) | PromoteByte(value)); }
public byte C { get { return LowByte(AF); } set { BC = (ushort)(HigherPart(BC) | value); } } public byte C { get => LowByte(AF); set => BC = (ushort)(HigherPart(BC) | value); }
public abstract ushort DE { get; set; } public abstract ushort DE { get; set; }
public byte D { get { return HighByte(AF); } set { DE = (ushort)(LowerPart(DE) | PromoteByte(value)); } } public byte D { get => HighByte(DE); set => DE = (ushort)(LowerPart(DE) | PromoteByte(value)); }
public byte E { get { return LowByte(AF); } set { DE = (ushort)(HigherPart(DE) | value); } } public byte E { get => LowByte(DE); set => DE = (ushort)(HigherPart(DE) | value); }
public abstract ushort HL { get; set; } public abstract ushort HL { get; set; }
public byte H { get { return HighByte(AF); } set { HL = (ushort)(LowerPart(AF) | PromoteByte(value)); } } public byte H { get => HighByte(HL); set => HL = (ushort)(LowerPart(HL) | PromoteByte(value)); }
public byte L { get { return LowByte(AF); } set { HL = (ushort)(HigherPart(AF) | value); } } public byte L { get => LowByte(HL); set { HL = (ushort)(HigherPart(HL) | value); } }
public override void RaisePOWER() public override void RaisePOWER()
{ {

View File

@@ -37,8 +37,8 @@
protected override ushort GetWordPaged(byte page, byte offset) protected override ushort GetWordPaged(byte page, byte offset)
{ {
var low = GetBytePaged(page, offset); var low = BusRead(offset, page);
var high = GetBytePaged(page, (byte)(offset + 1)); var high = BusRead((byte)(offset + 1), page);
return MakeWord(low, high); return MakeWord(low, high);
} }
@@ -64,8 +64,8 @@
protected override void SetWordPaged(byte page, byte offset, ushort value) protected override void SetWordPaged(byte page, byte offset, ushort value)
{ {
SetBytePaged(page, offset, LowByte(value)); BusWrite(offset, page, LowByte(value));
SetBytePaged(page, (byte)(offset + 1), HighByte(value)); BusWrite((byte)(offset + 1), page, HighByte(value));
} }
} }
} }

View File

@@ -31,8 +31,8 @@
protected byte OpCode { get => opcode; set => opcode = value; } protected byte OpCode { get => opcode; set => opcode = value; }
public Bus Bus { get => bus; set => bus = value; } public Bus Bus { get => bus; set => bus = value; }
public ref PinLevel RESET() { return ref resetLine; } public ref PinLevel RESET() => ref resetLine;
public ref PinLevel INT() { return ref intLine; } public ref PinLevel INT() => ref intLine;
public abstract int Step(); public abstract int Step();
public abstract int Execute(); public abstract int Execute();
@@ -101,10 +101,7 @@
#region BusWrite #region BusWrite
protected void BusWrite(byte low, byte high, byte data) protected void BusWrite(byte low, byte high, byte data) => BusWrite(MakeWord(low, high), data);
{
BusWrite(MakeWord(low, high), data);
}
protected void BusWrite(ushort address, byte data) protected void BusWrite(ushort address, byte data)
{ {
@@ -124,10 +121,7 @@
#region BusRead #region BusRead
protected byte BusRead(byte low, byte high) protected byte BusRead(byte low, byte high) => BusRead(MakeWord(low, high));
{
return BusRead(MakeWord(low, high));
}
protected byte BusRead(ushort address) protected byte BusRead(ushort address)
{ {
@@ -139,14 +133,6 @@
#endregion #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 byte FetchByte() => BusRead(PC++);
protected abstract ushort GetWord(); protected abstract ushort GetWord();
@@ -175,10 +161,7 @@
SetWord(value); SetWord(value);
} }
protected void Jump(ushort destination) protected void Jump(ushort destination) => PC = destination;
{
PC = destination;
}
protected void Call(ushort destination) protected void Call(ushort destination)
{ {
@@ -186,9 +169,6 @@
Jump(destination); Jump(destination);
} }
protected virtual void Return() protected virtual void Return() => Jump(PopWord());
{
Jump(PopWord());
}
} }
} }

View File

@@ -213,7 +213,7 @@
case 0x25: A = AndR(A, AM_ZeroPage()); break; // AND (zero page) case 0x25: A = AndR(A, AM_ZeroPage()); break; // AND (zero page)
case 0x26: BusReadModifyWrite(ROL(AM_ZeroPage())); break; // ROL (zero page) case 0x26: BusReadModifyWrite(ROL(AM_ZeroPage())); break; // ROL (zero page)
case 0x27: RLA(AM_ZeroPage()); break; // *RLA (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 0x29: A = AndR(A, AM_Immediate()); break; // AND (immediate)
case 0x2a: BusRead(); A = ROL(A); break; // ROL A (implied) case 0x2a: BusRead(); A = ROL(A); break; // ROL A (implied)
case 0x2b: ANC(AM_Immediate()); break; // *ANC (immediate) case 0x2b: ANC(AM_Immediate()); break; // *ANC (immediate)
@@ -281,7 +281,7 @@
case 0x65: A = ADC(A, AM_ZeroPage()); break; // ADC (zero page) case 0x65: A = ADC(A, AM_ZeroPage()); break; // ADC (zero page)
case 0x66: BusReadModifyWrite(ROR(AM_ZeroPage())); break; // ROR (zero page) case 0x66: BusReadModifyWrite(ROR(AM_ZeroPage())); break; // ROR (zero page)
case 0x67: RRA(AM_ZeroPage()); break; // *RRA (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 0x69: A = ADC(A, AM_Immediate()); break; // ADC (immediate)
case 0x6a: BusRead(); A = ROR(A); break; // ROR A (implied) case 0x6a: BusRead(); A = ROR(A); break; // ROR A (implied)
case 0x6b: ARR(AM_Immediate()); break; // *ARR (immediate) case 0x6b: ARR(AM_Immediate()); break; // *ARR (immediate)
@@ -472,9 +472,9 @@
return Cycles; 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() protected override sealed void HandleRESET()
{ {
@@ -608,7 +608,7 @@
var crossed = Address_AbsoluteX(); var crossed = Address_AbsoluteX();
var address = crossed.Item1; var address = crossed.Item1;
var page = crossed.Item2; var page = crossed.Item2;
var possible = GetBytePaged(page, LowByte(address)); var possible = BusRead(LowByte(address), page);
if ((behaviour == PageCrossingBehavior.AlwaysReadTwice) || (page != HighByte(address))) if ((behaviour == PageCrossingBehavior.AlwaysReadTwice) || (page != HighByte(address)))
possible = BusRead(address); possible = BusRead(address);
return possible; return possible;
@@ -619,7 +619,7 @@
var crossed = Address_AbsoluteY(); var crossed = Address_AbsoluteY();
var address = crossed.Item1; var address = crossed.Item1;
var page = crossed.Item2; var page = crossed.Item2;
var possible = GetBytePaged(page, LowByte(address)); var possible = BusRead(LowByte(address), page);
if (page != HighByte(address)) if (page != HighByte(address))
possible = BusRead(address); possible = BusRead(address);
return possible; return possible;
@@ -636,7 +636,7 @@
var crossed = Address_IndirectIndexedY(); var crossed = Address_IndirectIndexedY();
var address = crossed.Item1; var address = crossed.Item1;
var page = crossed.Item2; var page = crossed.Item2;
var possible = GetBytePaged(page, LowByte(address)); var possible = BusRead(LowByte(address), page);
if (page != HighByte(address)) if (page != HighByte(address))
possible = BusRead(address); possible = BusRead(address);
return possible; return possible;
@@ -803,7 +803,7 @@
private void JSR() private void JSR()
{ {
var low = FetchByte(); var low = FetchByte();
GetBytePaged(1, S); // dummy read BusRead(S, 1); // dummy read
PushWord(PC); PushWord(PC);
PC = MakeWord(low, FetchByte()); PC = MakeWord(low, FetchByte());
} }
@@ -838,14 +838,14 @@
private void RTI() private void RTI()
{ {
GetBytePaged(1, S); // dummy read BusRead(S, 1); // dummy read
PLP(); PLP();
Return(); Return();
} }
private void RTS() private void RTS()
{ {
GetBytePaged(1, S); // dummy read BusRead(S, 1); // dummy read
Return(); Return();
FetchByte(); FetchByte();
} }
@@ -921,7 +921,7 @@
var crossed = Address_AbsoluteX(); var crossed = Address_AbsoluteX();
var address = crossed.Item1; var address = crossed.Item1;
var page = crossed.Item2; var page = crossed.Item2;
GetBytePaged(page, LowByte(address)); BusRead(LowByte(address), page);
BusWrite(address, A); BusWrite(address, A);
} }
@@ -930,7 +930,7 @@
var crossed = Address_AbsoluteY(); var crossed = Address_AbsoluteY();
var address = crossed.Item1; var address = crossed.Item1;
var page = crossed.Item2; var page = crossed.Item2;
GetBytePaged(page, LowByte(address)); BusRead(LowByte(address), page);
BusWrite(address, A); BusWrite(address, A);
} }
} }

View File

@@ -56,8 +56,7 @@
CPU.ExecutedInstruction += CPU_ExecutedInstruction; CPU.ExecutedInstruction += CPU_ExecutedInstruction;
Poke(0x00, 0x4c); Poke(0x00, 0x4c);
Poke(0x01, Chip.LowByte(configuration.StartAddress)); cpu.PokeWord(0x01, configuration.StartAddress);
Poke(0x02, Chip.HighByte(configuration.StartAddress));
} }
private void CPU_ExecutedInstruction(object sender, System.EventArgs e) private void CPU_ExecutedInstruction(object sender, System.EventArgs e)