From b0ae43067c665e055d3398346de960a66e2c0abc Mon Sep 17 00:00:00 2001 From: edmccard Date: Sun, 8 Apr 2012 20:17:42 -0400 Subject: [PATCH] Undo overeager removal of readFinal/writeFinal (without the checks for final cycle, they are still needed to know when to call tick in cumulative mode) --- src/d6502/cmos.d | 8 ++++---- src/d6502/cpu.d | 30 +++++++++++++++++++++++++----- src/d6502/nmosbase.d | 2 +- src/d6502/nmosundoc.d | 30 +++++++++++++++--------------- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/d6502/cmos.d b/src/d6502/cmos.d index f277ac0..f736676 100644 --- a/src/d6502/cmos.d +++ b/src/d6502/cmos.d @@ -104,20 +104,20 @@ class Cmos(bool strict, bool cumulative) : Cpu!(strict, cumulative) static string RMW(string action) { return "peek(primaryAddress);\n" ~ - "write(primaryAddress, (flag.zero_ = flag.negative_ = " ~ + "writeFinal(primaryAddress, (flag.zero_ = flag.negative_ = " ~ action ~ "(readVal = read(primaryAddress))));\n"; } static string TestModify(string action) { return "peek(primaryAddress);\n" ~ - "write(primaryAddress, " ~ + "writeFinal(primaryAddress, " ~ action ~ "(readVal = read(primaryAddress)));\n"; } static string ReadNOP() { - return "readVal = read(primaryAddress);\n"; + return "readVal = readFinal(primaryAddress);\n"; } static string ManualAddress(string name, int[] opcodes, @@ -260,7 +260,7 @@ class Cmos(bool strict, bool cumulative) : Cpu!(strict, cumulative) /* BIT #$$ */ void opcode89() { - readVal = operand1 = read(programCounter++); + readVal = operand1 = readFinal(programCounter++); flag.zero_ = accumulator & readVal; } } diff --git a/src/d6502/cpu.d b/src/d6502/cpu.d index 8fc084b..97895e5 100644 --- a/src/d6502/cpu.d +++ b/src/d6502/cpu.d @@ -196,6 +196,26 @@ class Cpu(bool strict, bool cumulative) : CpuBase!(strict, cumulative) static if (strict) memoryWrite(addr, val); } + final ubyte readFinal(ushort addr) + { + static if (cumulative) tick(++totalCycles); + else + { + tick(); + } + return memoryRead(addr); + } + + final void writeFinal(ushort addr, ubyte val) + { + static if (cumulative) tick(++totalCycles); + else + { + tick(); + } + memoryWrite(addr, val); + } + final ushort readWord(ushort addrLo, ushort addrHi) { ushort word = read(addrLo); @@ -564,12 +584,12 @@ class Cpu(bool strict, bool cumulative) : CpuBase!(strict, cumulative) static string Read(string action) { - return UpdateNZ(action ~ " (readVal = read(primaryAddress))"); + return UpdateNZ(action ~ " (readVal = readFinal(primaryAddress))"); } static string Decimal(string action) { - string code = action ~ "(readVal = read(primaryAddress));\n"; + string code = action ~ "(readVal = readFinal(primaryAddress));\n"; return "if (flag.decimal) dec_" ~ code ~ "else hex_" ~ code; } @@ -577,17 +597,17 @@ class Cpu(bool strict, bool cumulative) : CpuBase!(strict, cumulative) static string Compare(string action) { return UpdateNZ("compare(" ~ action ~ - ", (readVal = read(primaryAddress)))"); + ", (readVal = readFinal(primaryAddress)))"); } static string Write(string action) { - return "write(primaryAddress, " ~ action ~ ");\n"; + return "writeFinal(primaryAddress, " ~ action ~ ");\n"; } static string BitTest() { - return "bitTest(readVal = read(primaryAddress));\n"; + return "bitTest(readVal = readFinal(primaryAddress));\n"; } mixin(SimpleOpcode("CLC", "18", "flag.carry = false")); diff --git a/src/d6502/nmosbase.d b/src/d6502/nmosbase.d index 21e9cd3..2a2e2b9 100644 --- a/src/d6502/nmosbase.d +++ b/src/d6502/nmosbase.d @@ -37,7 +37,7 @@ class NmosBase(bool strict, bool cumulative) : Cpu!(strict, cumulative) static string RMW(string action) { return "poke(primaryAddress, (readVal = read(primaryAddress)));\n" ~ - "write(primaryAddress, flag.zero_ = flag.negative_ = " ~ + "writeFinal(primaryAddress, flag.zero_ = flag.negative_ = " ~ action ~ "(readVal));\n"; } diff --git a/src/d6502/nmosundoc.d b/src/d6502/nmosundoc.d index cc85c4d..abea573 100644 --- a/src/d6502/nmosundoc.d +++ b/src/d6502/nmosundoc.d @@ -54,12 +54,12 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) val = val & hiAddr; ushort addr = (badAddress == primaryAddress) ? primaryAddress : ((val << 8) | (primaryAddress & 0xFF)); - write(addr, val); + writeFinal(addr, val); } else { ubyte hiAddr = cast(ubyte)((baseAddress >> 8) + 1); - write(primaryAddress, val & hiAddr); + writeFinal(primaryAddress, val & hiAddr); } } @@ -122,13 +122,13 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) static string ReadNOP() { - return "readVal = read(primaryAddress);\n"; + return "readVal = readFinal(primaryAddress);\n"; } static string RMW_Read(string action1, string action2) { return "poke(primaryAddress, (readVal = read(primaryAddress)));\n" ~ - "write(primaryAddress, flag.zero_ = flag.negative_ = " ~ + "writeFinal(primaryAddress, flag.zero_ = flag.negative_ = " ~ "(writeVal = " ~ action1 ~ "(readVal)));\n" ~ action2 ~ " writeVal;\n"; } @@ -136,7 +136,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) static string RMW_Compare(string action1, string action2) { return "poke(primaryAddress, (readVal = read(primaryAddress)));\n" ~ - "write(primaryAddress, " ~ + "writeFinal(primaryAddress, " ~ "(writeVal = " ~ action1 ~ "(readVal)));\n" ~ "flag.zero_ = flag.negative_ = " ~ "compare(" ~ action2 ~ ", writeVal);\n"; @@ -145,7 +145,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) static string RMW_Decimal(string action1, string action2) { return "poke(primaryAddress, (readVal = read(primaryAddress)));\n" ~ - "write(primaryAddress, flag.zero_ = flag.negative_ = " ~ + "writeFinal(primaryAddress, flag.zero_ = flag.negative_ = " ~ "(writeVal = " ~ action1 ~ "(readVal)));\n" ~ "if (flag.decimal) dec_" ~ action2 ~ "(writeVal);\n" ~ "else hex_" ~ action2 ~ "(writeVal);\n"; @@ -209,7 +209,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) /* ANC #$$ */ override void opcode0B() { - readVal = operand1 = read(programCounter); + readVal = operand1 = readFinal(programCounter); flag.zero_ = flag.negative_ = (accumulator = readVal); flag.carry = (flag.negative_ > 0x7F); } @@ -217,7 +217,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) /* ANC #$$ */ override void opcode2B() { - readVal = operand1 = read(programCounter); + readVal = operand1 = readFinal(programCounter); flag.zero_ = flag.negative_ = (accumulator = readVal); flag.carry = (flag.negative_ > 0x7F); } @@ -225,7 +225,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) /* ALR #$$ */ override void opcode4B() { - readVal = operand1 = read(programCounter); + readVal = operand1 = readFinal(programCounter); flag.zero_ = flag.negative_ = (accumulator = shiftRight(accumulator & readVal)); } @@ -233,7 +233,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) /* ARR #$$ */ override void opcode6B() { - readVal = operand1 = read(programCounter); + readVal = operand1 = readFinal(programCounter); ubyte val = readVal & accumulator; if (flag.decimal) { ubyte temp = cast(ubyte)((val >> 1) + (flag.carry ? 0x80 : 0)); @@ -263,7 +263,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) override void opcode8B() { // unstable - readVal = operand1 = read(programCounter++); + readVal = operand1 = readFinal(programCounter++); version(Atari8Bit) { @@ -316,7 +316,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) /* LAX #$$ */ override void opcodeAB() { - readVal = operand1 = read(programCounter); + readVal = operand1 = readFinal(programCounter); version(Commodore128) { @@ -341,7 +341,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) override void opcodeBB() { addrAbsoluteY(false); - readVal = read(primaryAddress); + readVal = readFinal(primaryAddress); flag.zero_ = flag.negative_ = (xIndex = accumulator = (stackPointer & readVal)); @@ -350,7 +350,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) /* SBX #$$ */ override void opcodeCB() { - readVal = operand1 = read(programCounter++); + readVal = operand1 = readFinal(programCounter++); xIndex &= accumulator; flag.zero_ = flag.negative_ = compare(xIndex, readVal); } @@ -358,7 +358,7 @@ class NmosUndoc(bool strict, bool cumulative) : NmosBase!(strict, cumulative) /* SBC #$$ */ override void opcodeEB() { - readVal = operand1 = read(programCounter++); + readVal = operand1 = readFinal(programCounter++); if (flag.decimal) dec_subWithCarry(readVal); else hex_subWithCarry(readVal); }