]> Shamusworld >> Repos - apple2/blobdiff - src/v65c02.cpp
Added known issues file, small fix for booting from running Apple.
[apple2] / src / v65c02.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 6f2c969..fbc5018
@@ -1,21 +1,24 @@
 //
-// Virtual 65C02 Emulator v1.0
+// Virtual 65C02 Emulator v1.1
 //
-// by James L. Hammons
-// (c) 2005 Underground Software
+// by James Hammons
+// (c) 2005-2018 Underground Software
 //
-// JLH = James L. Hammons <jlhamm@acm.org>
+// JLH = James Hammons <jlhamm@acm.org>
 //
 // WHO  WHEN        WHAT
-// ---  ----------  ------------------------------------------------------------
+// ---  ----------  -----------------------------------------------------------
 // JLH  01/04/2006  Added changelog ;-)
 // JLH  01/18/2009  Fixed EA_ABS_* macros
 //
 
-//OK, the wraparound bug exists in both the Apple and Atari versions of Ultima II.
-//However, the Atari version *does* occassionally pick strength while the Apple
-//versions do not--which would seem to indicate a bug either in the RNG algorithm,
-//the 65C02 core, or the Apple hardware. Need to investigate all three!
+//OK, the wraparound bug exists in both the Apple and Atari versions of Ultima
+//II. However, the Atari version *does* occassionally pick strength while the
+//Apple versions do not--which would seem to indicate a bug either in the RNG
+//algorithm, the 65C02 core, or the Apple hardware. Need to investigate all
+//three!
+//N.B.: There were some lingering bugs in the BCD portions of the ADC and SBC
+//      opcodes; need to test to see if that clears up the problem.
 
 #define __DEBUG__
 //#define __DEBUGMON__
 #include "v65c02.h"
 
 #ifdef __DEBUG__
+#include <string.h>
 #include "dis65c02.h"
+#include "floppydrive.h"
 #include "log.h"
 #endif
 
-// Various macros
 
-#define CLR_Z                          (regs.cc &= ~FLAG_Z)
-#define CLR_ZN                         (regs.cc &= ~(FLAG_Z | FLAG_N))
-#define CLR_ZNC                                (regs.cc &= ~(FLAG_Z | FLAG_N | FLAG_C))
-#define CLR_V                          (regs.cc &= ~FLAG_V)
-#define CLR_N                          (regs.cc &= ~FLAG_N)
-#define SET_Z(r)                       (regs.cc = ((r) == 0 ? regs.cc | FLAG_Z : regs.cc & ~FLAG_Z))
-#define SET_N(r)                       (regs.cc = ((r) & 0x80 ? regs.cc | FLAG_N : regs.cc & ~FLAG_N))
+// Various helper macros
+
+#define CLR_Z                          (regs->cc &= ~FLAG_Z)
+#define CLR_ZN                         (regs->cc &= ~(FLAG_Z | FLAG_N))
+#define CLR_ZNC                                (regs->cc &= ~(FLAG_Z | FLAG_N | FLAG_C))
+#define CLR_V                          (regs->cc &= ~FLAG_V)
+#define CLR_N                          (regs->cc &= ~FLAG_N)
+#define CLR_D                          (regs->cc &= ~FLAG_D)
+#define SET_Z(r)                       (regs->cc = ((r) == 0 ? regs->cc | FLAG_Z : regs->cc & ~FLAG_Z))
+#define SET_N(r)                       (regs->cc = ((r) & 0x80 ? regs->cc | FLAG_N : regs->cc & ~FLAG_N))
+#define SET_I                          (regs->cc |= FLAG_I)
 
 //Not sure that this code is computing the carry correctly... Investigate! [Seems to be]
-#define SET_C_ADD(a,b)         (regs.cc = ((uint8)(b) > (uint8)(~(a)) ? regs.cc | FLAG_C : regs.cc & ~FLAG_C))
-//#define SET_C_SUB(a,b)               (regs.cc = ((uint8)(b) >= (uint8)(a) ? regs.cc | FLAG_C : regs.cc & ~FLAG_C))
-#define SET_C_CMP(a,b)         (regs.cc = ((uint8)(b) >= (uint8)(a) ? regs.cc | FLAG_C : regs.cc & ~FLAG_C))
+/*
+Not 100% sure (for SET_C_CMP), when we have things like this:
+D0BE: AC 6F D3  LDY  $D36F     [SP=01EC, CC=--.--IZ-, A=AA, X=60, Y=00]
+D0C1: CC 5A D3  CPY  $D35A     [SP=01EC, CC=--.--IZC, A=AA, X=60, Y=00]
+D0C4: F0 0F     BEQ  $D0D5     [SP=01EC, CC=--.--IZC, A=AA, X=60, Y=00]
+D0D5: AD 6E D3  LDA  $D36E     [SP=01EC, CC=--.--I-C, A=0A, X=60, Y=00]
+
+Which shows that $D35A has to be 0 since the Z flag is set.  Why would the carry flag be set on a comparison where the compared items are equal?
+*/
+#define SET_C_ADD(a,b)         (regs->cc = ((uint8_t)(b) > (uint8_t)(~(a)) ? regs->cc | FLAG_C : regs->cc & ~FLAG_C))
+#define SET_C_CMP(a,b)         (regs->cc = ((uint8_t)(b) >= (uint8_t)(a) ? regs->cc | FLAG_C : regs->cc & ~FLAG_C))
 #define SET_ZN(r)                      SET_N(r); SET_Z(r)
 #define SET_ZNC_ADD(a,b,r)     SET_N(r); SET_Z(r); SET_C_ADD(a,b)
-//#define SET_ZNC_SUB(a,b,r)   SET_N(r); SET_Z(r); SET_C_SUB(a,b)
 #define SET_ZNC_CMP(a,b,r)     SET_N(r); SET_Z(r); SET_C_CMP(a,b)
 
-//Small problem with the EA_ macros: ABS macros don't increment the PC!!! !!! FIX !!!
-//NB: It's properly handled by everything that uses it, so it works, even if it's klunky
-//Small problem with fixing it is that you can't do it in a single instruction, i.e.,
-//you have to read the value THEN you have to increment the PC. Unless there's another
-//way to do that
-//[DONE]
-#define EA_IMM                         regs.pc++
-#define EA_ZP                          regs.RdMem(regs.pc++)
-#define EA_ZP_X                                (regs.RdMem(regs.pc++) + regs.x) & 0xFF
-#define EA_ZP_Y                                (regs.RdMem(regs.pc++) + regs.y) & 0xFF
-#define EA_ABS                         FetchMemW(regs.pc)
-#define EA_ABS_X                       FetchMemW(regs.pc) + regs.x
-#define EA_ABS_Y                       FetchMemW(regs.pc) + regs.y
-#define EA_IND_ZP_X                    RdMemW((regs.RdMem(regs.pc++) + regs.x) & 0xFF)
-#define EA_IND_ZP_Y                    RdMemW(regs.RdMem(regs.pc++)) + regs.y
-#define EA_IND_ZP                      RdMemW(regs.RdMem(regs.pc++))
-
-#define READ_IMM                       regs.RdMem(EA_IMM)
-#define READ_ZP                                regs.RdMem(EA_ZP)
-#define READ_ZP_X                      regs.RdMem(EA_ZP_X)
-#define READ_ZP_Y                      regs.RdMem(EA_ZP_Y)
-#define READ_ABS                       regs.RdMem(EA_ABS)
-#define READ_ABS_X                     regs.RdMem(EA_ABS_X)
-#define READ_ABS_Y                     regs.RdMem(EA_ABS_Y)
-#define READ_IND_ZP_X          regs.RdMem(EA_IND_ZP_X)
-#define READ_IND_ZP_Y          regs.RdMem(EA_IND_ZP_Y)
-#define READ_IND_ZP                    regs.RdMem(EA_IND_ZP)
-
-#define READ_IMM_WB(v)         uint16 addr = EA_IMM;      v = regs.RdMem(addr)
-#define READ_ZP_WB(v)          uint16 addr = EA_ZP;       v = regs.RdMem(addr)
-#define READ_ZP_X_WB(v)                uint16 addr = EA_ZP_X;     v = regs.RdMem(addr)
-#define READ_ABS_WB(v)         uint16 addr = EA_ABS;      v = regs.RdMem(addr)
-#define READ_ABS_X_WB(v)       uint16 addr = EA_ABS_X;    v = regs.RdMem(addr)
-#define READ_ABS_Y_WB(v)       uint16 addr = EA_ABS_Y;    v = regs.RdMem(addr)
-#define READ_IND_ZP_X_WB(v)    uint16 addr = EA_IND_ZP_X; v = regs.RdMem(addr)
-#define READ_IND_ZP_Y_WB(v)    uint16 addr = EA_IND_ZP_Y; v = regs.RdMem(addr)
-#define READ_IND_ZP_WB(v)      uint16 addr = EA_IND_ZP;   v = regs.RdMem(addr)
-
-#define WRITE_BACK(d)          regs.WrMem(addr, (d))
+#define EA_IMM                         regs->pc++
+#define EA_ZP                          regs->RdMem(regs->pc++)
+#define EA_ZP_X                                (regs->RdMem(regs->pc++) + regs->x) & 0xFF
+#define EA_ZP_Y                                (regs->RdMem(regs->pc++) + regs->y) & 0xFF
+#define EA_ABS                         FetchMemW(regs->pc)
+#define EA_ABS_X                       FetchMemW(regs->pc) + regs->x
+#define EA_ABS_Y                       FetchMemW(regs->pc) + regs->y
+#define EA_IND_ZP_X                    RdMemWZP((regs->RdMem(regs->pc++) + regs->x) & 0xFF)
+#define EA_IND_ZP_Y                    RdMemWZP(regs->RdMem(regs->pc++)) + regs->y
+#define EA_IND_ZP                      RdMemWZP(regs->RdMem(regs->pc++))
+
+#define READ_IMM                       regs->RdMem(EA_IMM)
+#define READ_ZP                                regs->RdMem(EA_ZP)
+#define READ_ZP_X                      regs->RdMem(EA_ZP_X)
+#define READ_ZP_Y                      regs->RdMem(EA_ZP_Y)
+#define READ_ABS                       regs->RdMem(EA_ABS)
+#define READ_ABS_X                     regs->RdMem(EA_ABS_X)
+#define READ_ABS_Y                     regs->RdMem(EA_ABS_Y)
+#define READ_IND_ZP_X          regs->RdMem(EA_IND_ZP_X)
+#define READ_IND_ZP_Y          regs->RdMem(EA_IND_ZP_Y)
+#define READ_IND_ZP                    regs->RdMem(EA_IND_ZP)
+
+#define READ_IMM_WB(v)         uint16_t addr = EA_IMM;      v = regs->RdMem(addr)
+#define READ_ZP_WB(v)          uint16_t addr = EA_ZP;       v = regs->RdMem(addr)
+#define READ_ZP_X_WB(v)                uint16_t addr = EA_ZP_X;     v = regs->RdMem(addr)
+#define READ_ABS_WB(v)         uint16_t addr = EA_ABS;      v = regs->RdMem(addr)
+#define READ_ABS_X_WB(v)       uint16_t addr = EA_ABS_X;    v = regs->RdMem(addr)
+#define READ_ABS_Y_WB(v)       uint16_t addr = EA_ABS_Y;    v = regs->RdMem(addr)
+#define READ_IND_ZP_X_WB(v)    uint16_t addr = EA_IND_ZP_X; v = regs->RdMem(addr)
+#define READ_IND_ZP_Y_WB(v)    uint16_t addr = EA_IND_ZP_Y; v = regs->RdMem(addr)
+#define READ_IND_ZP_WB(v)      uint16_t addr = EA_IND_ZP;   v = regs->RdMem(addr)
+
+#define WRITE_BACK(d)          regs->WrMem(addr, (d))
+
 
 // Private global variables
 
-static V65C02REGS regs;
-
-//This is probably incorrect, at least WRT to the $x7 and $xF opcodes... !!! FIX !!!
-//Also this doesn't take into account the extra cycle it takes when an indirect fetch
-//(ABS, ABS X/Y, ZP) crosses a page boundary, or extra cycle for BCD add/subtract...
-#warning "Cycle counts are not accurate--!!! FIX !!!"
-static uint8 CPUCycles[256] = {
-       7, 6, 1, 1, 5, 3, 5, 1, 3, 2, 2, 1, 6, 4, 6, 1,
-       2, 5, 5, 1, 5, 4, 6, 1, 2, 4, 2, 1, 6, 4, 6, 1,
-       6, 6, 1, 1, 3, 3, 5, 1, 4, 2, 2, 1, 4, 4, 6, 1,
-       2, 5, 5, 1, 4, 4, 6, 1, 2, 4, 2, 1, 4, 4, 6, 1,
-       6, 6, 1, 1, 1, 3, 5, 1, 3, 2, 2, 1, 3, 4, 6, 1,
-       2, 5, 5, 1, 1, 4, 6, 1, 2, 4, 3, 1, 1, 4, 6, 1,
-       6, 6, 1, 1, 3, 3, 5, 1, 4, 2, 2, 1, 6, 4, 6, 1,
-       2, 5, 5, 1, 4, 4, 6, 1, 2, 4, 4, 1, 6, 4, 6, 1,
-       2, 6, 1, 1, 3, 3, 3, 1, 2, 2, 2, 1, 4, 4, 4, 1,
-       2, 6, 5, 1, 4, 4, 4, 1, 2, 5, 2, 1, 4, 5, 5, 1,
-       2, 6, 2, 1, 3, 3, 3, 1, 2, 2, 2, 1, 4, 4, 4, 1,
-       2, 5, 5, 1, 4, 4, 4, 1, 2, 4, 2, 1, 4, 4, 4, 1,
-       2, 6, 1, 1, 3, 3, 5, 1, 2, 2, 2, 1, 4, 4, 6, 1,
-       2, 5, 5, 1, 1, 4, 6, 1, 2, 4, 3, 1, 1, 4, 6, 1,
-       2, 6, 1, 1, 3, 3, 5, 1, 2, 2, 2, 1, 4, 4, 6, 1,
-       2, 5, 5, 1, 1, 4, 6, 1, 2, 4, 4, 1, 1, 4, 6, 1 };
-
-// Private function prototypes
-
-static uint16 RdMemW(uint16);
-static uint16 FetchMemW(uint16 addr);
+static V65C02REGS * regs;
+
+// Cycle counts should be correct for the the Rockwell version of the 65C02.
+// Extra cycles for page crossing or BCD mode are accounted for in their
+// respective opcode handlers.
+static uint8_t CPUCycles[256] = {
+       7, 6, 2, 2, 5, 3, 5, 5, 3, 2, 2, 2, 6, 4, 6, 5,
+       2, 5, 5, 2, 5, 4, 6, 5, 2, 4, 2, 2, 6, 4, 6, 5,
+       6, 6, 2, 2, 3, 3, 5, 5, 4, 2, 2, 2, 4, 2, 6, 5,
+       2, 5, 5, 2, 4, 4, 6, 5, 2, 4, 2, 2, 4, 4, 6, 5,
+       6, 6, 2, 2, 3, 3, 5, 5, 3, 2, 2, 2, 3, 4, 6, 5,
+       2, 5, 5, 2, 4, 4, 6, 5, 2, 4, 3, 2, 8, 4, 6, 5,
+       6, 6, 2, 2, 3, 3, 5, 5, 4, 2, 2, 2, 6, 4, 6, 5,
+       2, 5, 5, 2, 4, 4, 6, 5, 2, 4, 4, 2, 6, 4, 6, 5,
+       2, 6, 2, 2, 3, 3, 3, 5, 2, 2, 2, 2, 4, 4, 4, 5,
+       2, 6, 5, 2, 4, 4, 4, 5, 2, 5, 2, 2, 4, 5, 5, 5,
+       2, 6, 2, 2, 3, 3, 3, 5, 2, 2, 2, 2, 4, 4, 4, 5,
+       2, 5, 5, 2, 4, 4, 4, 5, 2, 4, 2, 2, 4, 4, 4, 5,
+       2, 6, 2, 2, 3, 3, 5, 5, 2, 2, 2, 2, 4, 4, 5, 5,
+       2, 5, 5, 2, 4, 4, 6, 5, 2, 4, 3, 2, 4, 4, 6, 5,
+       2, 6, 2, 2, 3, 3, 5, 5, 2, 2, 2, 2, 4, 4, 6, 5,
+       2, 5, 5, 2, 4, 4, 6, 5, 2, 4, 4, 2, 4, 4, 6, 5 };
+
+
+//
+// Read a uint16_t out of 65C02 memory (big endian format)
+//
+static inline uint16_t RdMemW(uint16_t address)
+{
+       return (uint16_t)(regs->RdMem(address + 1) << 8)
+               | regs->RdMem(address + 0);
+}
+
 
 //
-// Read a uint16 out of 65C02 memory (big endian format)
+// Read a uint16_t out of 65C02 memory (big endian format), wrapping on page 0
 //
-static inline uint16 RdMemW(uint16 address)
+static inline uint16_t RdMemWZP(uint16_t address)
 {
-       return (uint16)(regs.RdMem(address + 1) << 8) | regs.RdMem(address + 0);
+       return (uint16_t)(regs->RdMem((address + 1) & 0xFF) << 8)
+               | regs->RdMem(address + 0);
 }
 
+
 //
-// Read a uint16 out of 65C02 memory (big endian format) and increment PC
+// Read a uint16_t out of 65C02 memory (big endian format) and increment PC
 //
-static inline uint16 FetchMemW(uint16 address)
+static inline uint16_t FetchMemW(uint16_t address)
 {
-       regs.pc += 2;
-       return (uint16)(regs.RdMem(address + 1) << 8) | regs.RdMem(address + 0);
+       regs->pc += 2;
+       return (uint16_t)(regs->RdMem(address + 1) << 8)
+               | regs->RdMem(address + 0);
 }
 
 
 //
 // 65C02 OPCODE IMPLEMENTATION
 //
-// NOTE: Lots of macros are used here to save a LOT of typing. Also
-//       helps speed the debugging process. :-) Because of this, combining
+// NOTE: Lots of macros are used here to save a LOT of typing.  Also
+//       helps speed the debugging process.  :-)  Because of this, combining
 //       certain lines may look like a good idea but would end in disaster.
-//       You have been warned! ;-)
+//       You have been warned!  ;-)
 //
 
+// Page crossing macros.  These catch the cases where access of a certain type
+// will incur a one cycle penalty when crossing a page boundary.
+
+#define HANDLE_PAGE_CROSSING_IND_Y \
+       uint16_t addressLo = regs->RdMem(regs->RdMem(regs->pc)); \
+\
+       if ((addressLo + regs->y) > 0xFF) \
+               regs->clock++;
+
+#define HANDLE_PAGE_CROSSING_ABS_X \
+       uint16_t addressLo = regs->RdMem(regs->pc); \
+\
+       if ((addressLo + regs->x) > 0xFF) \
+               regs->clock++;
+
+#define HANDLE_PAGE_CROSSING_ABS_Y \
+       uint16_t addressLo = regs->RdMem(regs->pc); \
+\
+       if ((addressLo + regs->y) > 0xFF) \
+               regs->clock++;
+
+// Branch taken adds a cycle, crossing page adds one more
+
+#define HANDLE_BRANCH_TAKEN(m)       \
+{                                    \
+       uint16_t oldpc = regs->pc;       \
+       regs->pc += m;                   \
+       regs->clock++;                   \
+                                     \
+       if ((oldpc ^ regs->pc) & 0xFF00) \
+               regs->clock++;               \
+}
+
 /*
 Mnemonic       Addressing mode Form            Opcode  Size    Timing
 
@@ -161,76 +212,85 @@ ADC                       Immediate               ADC #Oper       69              2               2
 // ADC opcodes
 
 //This is non-optimal, but it works--optimize later. :-)
+//N.B.: We have to pull the low nybble from each part of the sum in order to
+//      check BCD addition of the low nybble correctly.  It doesn't work to
+//      look at the sum after summing the bytes.  Also, Decimal mode incurs a
+//      one cycle penalty (for the decimal correction).
 #define OP_ADC_HANDLER(m) \
-       uint16 sum = (uint16)regs.a + (m) + (uint16)(regs.cc & FLAG_C); \
+       uint16_t sum = (uint16_t)regs->a + (m) + (uint16_t)(regs->cc & FLAG_C); \
 \
-       if (regs.cc & FLAG_D) \
+       if (regs->cc & FLAG_D) \
        { \
-               if ((sum & 0x0F) > 0x09) \
+               uint8_t an = regs->a & 0x0F, mn = (m) & 0x0F, cn = (uint8_t)(regs->cc & FLAG_C); \
+\
+               if ((an + mn + cn) > 9) \
                        sum += 0x06; \
 \
-               if ((sum & 0xF0) > 0x90) \
+               if ((sum & 0x1F0) > 0x90) \
                        sum += 0x60; \
+\
+               regs->clock++;\
        } \
 \
-       regs.cc = (regs.cc & ~FLAG_C) | (sum >> 8); \
-       regs.cc = (~(regs.a ^ (m)) & (regs.a ^ sum) & 0x80 ? regs.cc | FLAG_V : regs.cc & ~FLAG_V); \
-       regs.a = sum & 0xFF; \
-       SET_ZN(regs.a)
-
-//OLD V detection: regs.cc = ((regs.a ^ (m) ^ sum ^ (regs.cc << 7)) & 0x80 ? regs.cc | FLAG_V : regs.cc & ~FLAG_V);
+       regs->cc = (regs->cc & ~FLAG_C) | (sum >> 8); \
+       regs->cc = (~(regs->a ^ (m)) & (regs->a ^ sum) & 0x80 ? regs->cc | FLAG_V : regs->cc & ~FLAG_V); \
+       regs->a = sum & 0xFF; \
+       SET_ZN(regs->a)
 
 static void Op69(void)                                                 // ADC #
 {
-       uint16 m = READ_IMM;
+       uint16_t m = READ_IMM;
        OP_ADC_HANDLER(m);
 }
 
 static void Op65(void)                                                 // ADC ZP
 {
-       uint16 m = READ_ZP;
+       uint16_t m = READ_ZP;
        OP_ADC_HANDLER(m);
 }
 
 static void Op75(void)                                                 // ADC ZP, X
 {
-       uint16 m = READ_ZP_X;
+       uint16_t m = READ_ZP_X;
        OP_ADC_HANDLER(m);
 }
 
 static void Op6D(void)                                                 // ADC ABS
 {
-       uint16 m = READ_ABS;
+       uint16_t m = READ_ABS;
        OP_ADC_HANDLER(m);
 }
 
 static void Op7D(void)                                                 // ADC ABS, X
 {
-       uint16 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint16_t m = READ_ABS_X;
        OP_ADC_HANDLER(m);
 }
 
 static void Op79(void)                                                 // ADC ABS, Y
 {
-       uint16 m = READ_ABS_Y;
+       HANDLE_PAGE_CROSSING_ABS_Y;
+       uint16_t m = READ_ABS_Y;
        OP_ADC_HANDLER(m);
 }
 
 static void Op61(void)                                                 // ADC (ZP, X)
 {
-       uint16 m = READ_IND_ZP_X;
+       uint16_t m = READ_IND_ZP_X;
        OP_ADC_HANDLER(m);
 }
 
 static void Op71(void)                                                 // ADC (ZP), Y
 {
-       uint16 m = READ_IND_ZP_Y;
+       HANDLE_PAGE_CROSSING_IND_Y;
+       uint16_t m = READ_IND_ZP_Y;
        OP_ADC_HANDLER(m);
 }
 
 static void Op72(void)                                                 // ADC (ZP)
 {
-       uint16 m = READ_IND_ZP;
+       uint16_t m = READ_IND_ZP;
        OP_ADC_HANDLER(m);
 }
 
@@ -249,60 +309,63 @@ Absolute,Y                AND Abs,Y       39      3       4
 // AND opcodes
 
 #define OP_AND_HANDLER(m) \
-       regs.a &= m; \
-       SET_ZN(regs.a)
+       regs->a &= m; \
+       SET_ZN(regs->a)
 
 static void Op29(void)                                                 // AND #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_AND_HANDLER(m);
 }
 
 static void Op25(void)                                                 // AND ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_AND_HANDLER(m);
 }
 
 static void Op35(void)                                                 // AND ZP, X
 {
-       uint8 m = READ_ZP_X;
+       uint8_t m = READ_ZP_X;
        OP_AND_HANDLER(m);
 }
 
 static void Op2D(void)                                                 // AND ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_AND_HANDLER(m);
 }
 
 static void Op3D(void)                                                 // AND ABS, X
 {
-       uint8 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m = READ_ABS_X;
        OP_AND_HANDLER(m);
 }
 
 static void Op39(void)                                                 // AND ABS, Y
 {
-       uint8 m = READ_ABS_Y;
+       HANDLE_PAGE_CROSSING_ABS_Y;
+       uint8_t m = READ_ABS_Y;
        OP_AND_HANDLER(m);
 }
 
 static void Op21(void)                                                 // AND (ZP, X)
 {
-       uint8 m = READ_IND_ZP_X;
+       uint8_t m = READ_IND_ZP_X;
        OP_AND_HANDLER(m);
 }
 
 static void Op31(void)                                                 // AND (ZP), Y
 {
-       uint8 m = READ_IND_ZP_Y;
+       HANDLE_PAGE_CROSSING_IND_Y;
+       uint8_t m = READ_IND_ZP_Y;
        OP_AND_HANDLER(m);
 }
 
 static void Op32(void)                                                 // AND (ZP)
 {
-       uint8 m = READ_IND_ZP;
+       uint8_t m = READ_IND_ZP;
        OP_AND_HANDLER(m);
 }
 
@@ -314,33 +377,21 @@ Absolute          ASL Abs         0E      3       6
 Absolute,X             ASL Abs,X       1E      3       7
 */
 
-/*static void Op78(void)  // LSL ABS
-{
-       uint8 tmp;  uint16 addr;
-       addr = FetchW();
-       tmp = regs.RdMem(addr);
-       (tmp&0x80 ? regs.cc |= 0x01 : regs.cc &= 0xFE);  // Shift hi bit into Carry
-       tmp <<= 1;
-       regs.WrMem(addr, tmp);
-       (tmp == 0 ? regs.cc |= 0x04 : regs.cc &= 0xFB);  // Adjust Zero flag
-       (tmp&0x80 ? regs.cc |= 0x08 : regs.cc &= 0xF7);  // Adjust Negative flag
-}*/
-
 // ASL opcodes
 
 #define OP_ASL_HANDLER(m) \
-       regs.cc = ((m) & 0x80 ? regs.cc | FLAG_C : regs.cc & ~FLAG_C); \
+       regs->cc = ((m) & 0x80 ? regs->cc | FLAG_C : regs->cc & ~FLAG_C); \
        (m) <<= 1; \
        SET_ZN((m))
 
 static void Op0A(void)                                                 // ASL A
 {
-       OP_ASL_HANDLER(regs.a);
+       OP_ASL_HANDLER(regs->a);
 }
 
 static void Op06(void)                                                 // ASL ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        OP_ASL_HANDLER(m);
        WRITE_BACK(m);
@@ -348,7 +399,7 @@ static void Op06(void)                                                      // ASL ZP
 
 static void Op16(void)                                                 // ASL ZP, X
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_X_WB(m);
        OP_ASL_HANDLER(m);
        WRITE_BACK(m);
@@ -356,7 +407,7 @@ static void Op16(void)                                                      // ASL ZP, X
 
 static void Op0E(void)                                                 // ASL ABS
 {
-       uint8 m;
+       uint8_t m;
        READ_ABS_WB(m);
        OP_ASL_HANDLER(m);
        WRITE_BACK(m);
@@ -364,159 +415,176 @@ static void Op0E(void)                                                  // ASL ABS
 
 static void Op1E(void)                                                 // ASL ABS, X
 {
-       uint8 m;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m;
        READ_ABS_X_WB(m);
        OP_ASL_HANDLER(m);
        WRITE_BACK(m);
 }
 
 /*
-BBR0   Relative        BBR0 Oper       0F      2       2
-BBR1   Relative        BBR1 Oper       1F      2       2
-BBR2   Relative        BBR2 Oper       2F      2       2
-BBR3   Relative        BBR3 Oper       3F      2       2
-BBR4   Relative        BBR4 Oper       4F      2       2
-BBR5   Relative        BBR5 Oper       5F      2       2
-BBR6   Relative        BBR6 Oper       6F      2       2
-BBR7   Relative        BBR7 Oper       7F      2       2
-BBS0   Relative        BBS0 Oper       8F      2       2
-BBS1   Relative        BBS1 Oper       9F      2       2
-BBS2   Relative        BBS2 Oper       AF      2       2
-BBS3   Relative        BBS3 Oper       BF      2       2
-BBS4   Relative        BBS4 Oper       CF      2       2
-BBS5   Relative        BBS5 Oper       DF      2       2
-BBS6   Relative        BBS6 Oper       EF      2       2
-BBS7   Relative        BBS7 Oper       FF      2       2
+BBR0   ZP, Relative    BBR0 Oper       0F      3       5
+BBR1   ZP, Relative    BBR1 Oper       1F      3       5
+BBR2   ZP, Relative    BBR2 Oper       2F      3       5
+BBR3   ZP, Relative    BBR3 Oper       3F      3       5
+BBR4   ZP, Relative    BBR4 Oper       4F      3       5
+BBR5   ZP, Relative    BBR5 Oper       5F      3       5
+BBR6   ZP, Relative    BBR6 Oper       6F      3       5
+BBR7   ZP, Relative    BBR7 Oper       7F      3       5
+BBS0   ZP, Relative    BBS0 Oper       8F      3       5
+BBS1   ZP, Relative    BBS1 Oper       9F      3       5
+BBS2   ZP, Relative    BBS2 Oper       AF      3       5
+BBS3   ZP, Relative    BBS3 Oper       BF      3       5
+BBS4   ZP, Relative    BBS4 Oper       CF      3       5
+BBS5   ZP, Relative    BBS5 Oper       DF      3       5
+BBS6   ZP, Relative    BBS6 Oper       EF      3       5
+BBS7   ZP, Relative    BBS7 Oper       FF      3       5
 */
 
 // BBR/Sn opcodes
 
 static void Op0F(void)                                                 // BBR0
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.a & 0x01))
-               regs.pc += m;
+       if (!(b & 0x01))
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op1F(void)                                                 // BBR1
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.a & 0x02))
-               regs.pc += m;
+       if (!(b & 0x02))
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op2F(void)                                                 // BBR2
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.a & 0x04))
-               regs.pc += m;
+       if (!(b & 0x04))
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op3F(void)                                                 // BBR3
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.a & 0x08))
-               regs.pc += m;
+       if (!(b & 0x08))
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op4F(void)                                                 // BBR4
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.a & 0x10))
-               regs.pc += m;
+       if (!(b & 0x10))
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op5F(void)                                                 // BBR5
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.a & 0x20))
-               regs.pc += m;
+       if (!(b & 0x20))
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op6F(void)                                                 // BBR6
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.a & 0x40))
-               regs.pc += m;
+       if (!(b & 0x40))
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op7F(void)                                                 // BBR7
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.a & 0x80))
-               regs.pc += m;
+       if (!(b & 0x80))
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op8F(void)                                                 // BBS0
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.a & 0x01)
-               regs.pc += m;
+       if (b & 0x01)
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void Op9F(void)                                                 // BBS1
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.a & 0x02)
-               regs.pc += m;
+       if (b & 0x02)
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void OpAF(void)                                                 // BBS2
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.a & 0x04)
-               regs.pc += m;
+       if (b & 0x04)
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void OpBF(void)                                                 // BBS3
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.a & 0x08)
-               regs.pc += m;
+       if (b & 0x08)
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void OpCF(void)                                                 // BBS4
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.a & 0x10)
-               regs.pc += m;
+       if (b & 0x10)
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void OpDF(void)                                                 // BBS5
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.a & 0x20)
-               regs.pc += m;
+       if (b & 0x20)
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void OpEF(void)                                                 // BBS6
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.a & 0x40)
-               regs.pc += m;
+       if (b & 0x40)
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 static void OpFF(void)                                                 // BBS7
 {
-       int16 m = (int16)(int8)READ_IMM;
+       uint8_t b = READ_ZP;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.a & 0x80)
-               regs.pc += m;
+       if (b & 0x80)
+               HANDLE_BRANCH_TAKEN(m);
 }
 
 /*
@@ -529,26 +597,26 @@ BEQ       Relative        BEQ Oper        F0      2       2
 
 static void Op90(void)                                                 // BCC
 {
-       int16 m = (int16)(int8)READ_IMM;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.cc & FLAG_C))
-               regs.pc += m;
+       if (!(regs->cc & FLAG_C))
+               HANDLE_BRANCH_TAKEN(m)
 }
 
 static void OpB0(void)                                                 // BCS
 {
-       int16 m = (int16)(int8)READ_IMM;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.cc & FLAG_C)
-               regs.pc += m;
+       if (regs->cc & FLAG_C)
+               HANDLE_BRANCH_TAKEN(m)
 }
 
 static void OpF0(void)                                                 // BEQ
 {
-       int16 m = (int16)(int8)READ_IMM;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.cc & FLAG_Z)
-               regs.pc += m;
+       if (regs->cc & FLAG_Z)
+               HANDLE_BRANCH_TAKEN(m)
 }
 
 /*
@@ -561,44 +629,46 @@ Absolute,X                BIT Abs,X       3C      3       4
 
 // BIT opcodes
 
-/* 1. The BIT instruction copies bit 6 to the V flag, and bit 7 to the N flag (except in immediate
-      addressing mode where V & N are untouched.) The accumulator and the operand are ANDed and the
-      Z flag is set appropriately. */
+/* 1. The BIT instruction copies bit 6 to the V flag, and bit 7 to the N flag
+      (except in immediate addressing mode where V & N are untouched.) The
+      accumulator and the operand are ANDed and the Z flag is set
+      appropriately. */
 
 #define OP_BIT_HANDLER(m) \
-       int8 result = regs.a & (m); \
-       regs.cc &= ~(FLAG_N | FLAG_V); \
-       regs.cc |= ((m) & 0xC0); \
+       int8_t result = regs->a & (m); \
+       regs->cc &= ~(FLAG_N | FLAG_V); \
+       regs->cc |= ((m) & 0xC0); \
        SET_Z(result)
 
 static void Op89(void)                                                 // BIT #
 {
-       int8 m = READ_IMM;
-       int8 result = regs.a & m;
+       int8_t m = READ_IMM;
+       int8_t result = regs->a & m;
        SET_Z(result);
 }
 
 static void Op24(void)                                                 // BIT ZP
 {
-       int8 m = READ_ZP;
+       int8_t m = READ_ZP;
        OP_BIT_HANDLER(m);
 }
 
 static void Op34(void)                                                 // BIT ZP, X
 {
-       uint8 m = READ_ZP_X;
+       uint8_t m = READ_ZP_X;
        OP_BIT_HANDLER(m);
 }
 
 static void Op2C(void)                                                 // BIT ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_BIT_HANDLER(m);
 }
 
 static void Op3C(void)                                                 // BIT ABS, X
 {
-       uint8 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m = READ_ABS_X;
        OP_BIT_HANDLER(m);
 }
 
@@ -613,32 +683,32 @@ BRA       Relative        BRA Oper        80      2       3
 
 static void Op30(void)                                                 // BMI
 {
-       int16 m = (int16)(int8)READ_IMM;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.cc & FLAG_N)
-               regs.pc += m;
+       if (regs->cc & FLAG_N)
+               HANDLE_BRANCH_TAKEN(m)
 }
 
 static void OpD0(void)                                                 // BNE
 {
-       int16 m = (int16)(int8)READ_IMM;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.cc & FLAG_Z))
-               regs.pc += m;
+       if (!(regs->cc & FLAG_Z))
+               HANDLE_BRANCH_TAKEN(m)
 }
 
 static void Op10(void)                                                 // BPL
 {
-       int16 m = (int16)(int8)READ_IMM;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.cc & FLAG_N))
-               regs.pc += m;
+       if (!(regs->cc & FLAG_N))
+               HANDLE_BRANCH_TAKEN(m)
 }
 
 static void Op80(void)                                                 // BRA
 {
-       int16 m = (int16)(int8)READ_IMM;
-       regs.pc += m;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
+       HANDLE_BRANCH_TAKEN(m)
 }
 
 /*
@@ -647,14 +717,24 @@ BRK       Implied         BRK                     00      1       7
 
 static void Op00(void)                                                 // BRK
 {
-       regs.cc |= FLAG_B;                                                      // Set B
-       regs.pc++;                                                                      // RTI comes back to the instruction one byte after the BRK
-       regs.WrMem(0x0100 + regs.sp--, regs.pc >> 8);   // Save PC and CC
-       regs.WrMem(0x0100 + regs.sp--, regs.pc & 0xFF);
-       regs.WrMem(0x0100 + regs.sp--, regs.cc);
-       regs.cc |= FLAG_I;                                                      // Set I
-       regs.cc &= ~FLAG_D;                                                     // & clear D
-       regs.pc = RdMemW(0xFFFE);                                       // Grab the IRQ vector & go...
+//#ifdef __DEBUG__
+#if 1
+WriteLog("\n*** BRK ***\n\n");
+WriteLog(" [PC=%04X, SP=%04X, CC=%s%s.%s%s%s%s%s, A=%02X, X=%02X, Y=%02X]\n",
+       regs->pc, 0x0100 + regs->sp,
+       (regs->cc & FLAG_N ? "N" : "-"), (regs->cc & FLAG_V ? "V" : "-"),
+       (regs->cc & FLAG_B ? "B" : "-"), (regs->cc & FLAG_D ? "D" : "-"),
+       (regs->cc & FLAG_I ? "I" : "-"), (regs->cc & FLAG_Z ? "Z" : "-"),
+       (regs->cc & FLAG_C ? "C" : "-"), regs->a, regs->x, regs->y);
+#endif
+       regs->cc |= FLAG_B;                                                     // Set B
+       regs->pc++;                                                                     // RTI comes back to the instruction one byte after the BRK
+       regs->WrMem(0x0100 + regs->sp--, regs->pc >> 8);        // Save PC and CC
+       regs->WrMem(0x0100 + regs->sp--, regs->pc & 0xFF);
+       regs->WrMem(0x0100 + regs->sp--, regs->cc);
+       regs->cc |= FLAG_I;                                                     // Set I
+       regs->cc &= ~FLAG_D;                                                    // & clear D
+       regs->pc = RdMemW(0xFFFE);                                      // Grab the IRQ vector & go...
 }
 
 /*
@@ -666,18 +746,18 @@ BVS       Relative        BVS Oper        70      2       2
 
 static void Op50(void)                                                 // BVC
 {
-       int16 m = (int16)(int8)READ_IMM;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (!(regs.cc & FLAG_V))
-               regs.pc += m;
+       if (!(regs->cc & FLAG_V))
+               HANDLE_BRANCH_TAKEN(m)
 }
 
 static void Op70(void)                                                 // BVS
 {
-       int16 m = (int16)(int8)READ_IMM;
+       int16_t m = (int16_t)(int8_t)READ_IMM;
 
-       if (regs.cc & FLAG_V)
-               regs.pc += m;
+       if (regs->cc & FLAG_V)
+               HANDLE_BRANCH_TAKEN(m)
 }
 
 /*
@@ -686,7 +766,7 @@ CLC Implied         CLC                     18      1       2
 
 static void Op18(void)                                                 // CLC
 {
-       regs.cc &= ~FLAG_C;
+       regs->cc &= ~FLAG_C;
 }
 
 /*
@@ -695,7 +775,7 @@ CLD Implied         CLD                     D8      1       2
 
 static void OpD8(void)                                                 // CLD
 {
-       regs.cc &= ~FLAG_D;
+       CLR_D;
 }
 
 /*
@@ -704,7 +784,7 @@ CLI Implied         CLI                     58      1       2
 
 static void Op58(void)                                                 // CLI
 {
-       regs.cc &= ~FLAG_I;
+       regs->cc &= ~FLAG_I;
 }
 
 /*
@@ -713,7 +793,7 @@ CLV Implied         CLV                     B8      1       2
 
 static void OpB8(void)                                                 // CLV
 {
-       regs.cc &= ~FLAG_V;
+       regs->cc &= ~FLAG_V;
 }
 
 /*
@@ -730,87 +810,64 @@ Absolute,Y                CMP Abs,Y       D9      3       4
 
 // CMP opcodes
 
-/*
-Here's the latest: The CMP is NOT generating the Z flag when A=$C0!
-
-FABA: A0 07          LDY   #$07                [PC=FABC, SP=01FF, CC=---B-IZ-, A=00, X=00, Y=07]
-FABC: C6 01          DEC   $01                 [PC=FABE, SP=01FF, CC=N--B-I--, A=00, X=00, Y=07]
-FABE: A5 01          LDA   $01                 [PC=FAC0, SP=01FF, CC=N--B-I--, A=C0, X=00, Y=07]
-FAC0: C9 C0          CMP   #$C0                [PC=FAC2, SP=01FF, CC=N--B-I--, A=C0, X=00, Y=07]
-FAC2: F0 D7          BEQ   $FA9B               [PC=FAC4, SP=01FF, CC=N--B-I--, A=C0, X=00, Y=07]
-FAC4: 8D F8 07       STA   $07F8               [PC=FAC7, SP=01FF, CC=N--B-I--, A=C0, X=00, Y=07]
-FAC7: B1 00          LDA   ($00),Y
-*** Read at I/O address C007
-               [PC=FAC9, SP=01FF, CC=---B-IZ-, A=00, X=00, Y=07]
-FAC9: D9 01 FB       CMP   $FB01,Y             [PC=FACC, SP=01FF, CC=---B-I--, A=00, X=00, Y=07]
-FACC: D0 EC          BNE   $FABA               [PC=FABA, SP=01FF, CC=---B-I--, A=00, X=00, Y=07]
-
-Should be fixed now... (was adding instead of subtracting!)
-
-Small problem here... First two should set the carry while the last one should clear it. !!! FIX !!! [DONE]
-
-FDF0: C9 A0          CMP   #$A0                [PC=FDF2, SP=01F1, CC=---B-IZ-, A=A0, X=02, Y=03]
-FD7E: C9 E0          CMP   #$E0                [PC=FD80, SP=01F4, CC=N--B-I--, A=A0, X=02, Y=03]
-FD38: C9 9B          CMP   #$9B                [PC=FD3A, SP=01F2, CC=---B-I-C, A=A0, X=02, Y=03]
-
-Compare sets flags as if a subtraction had been carried out. If the value in the accumulator is equal or greater than the compared value, the Carry will be set. The equal (Z) and sign (S) flags will be set based on equality or lack thereof and the sign (i.e. A>=$80) of the accumulator.
-*/
-
 #define OP_CMP_HANDLER(m) \
-       uint8 result = regs.a - (m); \
-       SET_ZNC_CMP(m, regs.a, result)
+       uint8_t result = regs->a - (m); \
+       SET_ZNC_CMP(m, regs->a, result)
 
 static void OpC9(void)                                                 // CMP #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_CMP_HANDLER(m);
 }
 
 static void OpC5(void)                                                 // CMP ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_CMP_HANDLER(m);
 }
 
 static void OpD5(void)                                                 // CMP ZP, X
 {
-       uint8 m = READ_ZP_X;
+       uint8_t m = READ_ZP_X;
        OP_CMP_HANDLER(m);
 }
 
 static void OpCD(void)                                                 // CMP ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_CMP_HANDLER(m);
 }
 
 static void OpDD(void)                                                 // CMP ABS, X
 {
-       uint8 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m = READ_ABS_X;
        OP_CMP_HANDLER(m);
 }
 
 static void OpD9(void)                                                 // CMP ABS, Y
 {
-       uint8 m = READ_ABS_Y;
+       HANDLE_PAGE_CROSSING_ABS_Y;
+       uint8_t m = READ_ABS_Y;
        OP_CMP_HANDLER(m);
 }
 
 static void OpC1(void)                                                 // CMP (ZP, X)
 {
-       uint8 m = READ_IND_ZP_X;
+       uint8_t m = READ_IND_ZP_X;
        OP_CMP_HANDLER(m);
 }
 
 static void OpD1(void)                                                 // CMP (ZP), Y
 {
-       uint8 m = READ_IND_ZP_Y;
+       HANDLE_PAGE_CROSSING_IND_Y;
+       uint8_t m = READ_IND_ZP_Y;
        OP_CMP_HANDLER(m);
 }
 
 static void OpD2(void)                                                 // CMP (ZP)
 {
-       uint8 m = READ_IND_ZP;
+       uint8_t m = READ_IND_ZP;
        OP_CMP_HANDLER(m);
 }
 
@@ -823,24 +880,24 @@ Absolute          CPX Abs         EC      3       4
 // CPX opcodes
 
 #define OP_CPX_HANDLER(m) \
-       uint8 result = regs.x - (m); \
-       SET_ZNC_CMP(m, regs.x, result)
+       uint8_t result = regs->x - (m); \
+       SET_ZNC_CMP(m, regs->x, result)
 
 static void OpE0(void)                                                 // CPX #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_CPX_HANDLER(m);
 }
 
 static void OpE4(void)                                                 // CPX ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_CPX_HANDLER(m);
 }
 
 static void OpEC(void)                                                 // CPX ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_CPX_HANDLER(m);
 }
 
@@ -853,24 +910,24 @@ Absolute          CPY Abs         CC      3       4
 // CPY opcodes
 
 #define OP_CPY_HANDLER(m) \
-       uint8 result = regs.y - (m); \
-       SET_ZNC_CMP(m, regs.y, result)
+       uint8_t result = regs->y - (m); \
+       SET_ZNC_CMP(m, regs->y, result)
 
 static void OpC0(void)                                                 // CPY #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_CPY_HANDLER(m);
 }
 
 static void OpC4(void)                                                 // CPY ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_CPY_HANDLER(m);
 }
 
 static void OpCC(void)                                                 // CPY ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_CPY_HANDLER(m);
 }
 
@@ -880,8 +937,8 @@ DEA Accumulator     DEA                     3A      1       2
 
 static void Op3A(void)                                                 // DEA
 {
-       regs.a--;
-       SET_ZN(regs.a);
+       regs->a--;
+       SET_ZN(regs->a);
 }
 
 /*
@@ -899,7 +956,7 @@ Absolute,X          DEC Abs,X       DE      3       7
 
 static void OpC6(void)                                                 // DEC ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        OP_DEC_HANDLER(m);
        WRITE_BACK(m);
@@ -907,7 +964,7 @@ static void OpC6(void)                                                      // DEC ZP
 
 static void OpD6(void)                                                 // DEC ZP, X
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_X_WB(m);
        OP_DEC_HANDLER(m);
        WRITE_BACK(m);
@@ -915,7 +972,7 @@ static void OpD6(void)                                                      // DEC ZP, X
 
 static void OpCE(void)                                                 // DEC ABS
 {
-       uint8 m;
+       uint8_t m;
        READ_ABS_WB(m);
        OP_DEC_HANDLER(m);
        WRITE_BACK(m);
@@ -923,34 +980,21 @@ static void OpCE(void)                                                    // DEC ABS
 
 static void OpDE(void)                                                 // DEC ABS, X
 {
-       uint8 m;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m;
        READ_ABS_X_WB(m);
        OP_DEC_HANDLER(m);
        WRITE_BACK(m);
 }
 
-/*
-Here's one problem: DEX is setting the N flag!
-
-D3EE: A2 09          LDX   #$09                [PC=D3F0, SP=01F7, CC=---B-I-C, A=01, X=09, Y=08]
-D3F0: 98             TYA               [PC=D3F1, SP=01F7, CC=N--B-I-C, A=08, X=09, Y=08]
-D3F1: 48             PHA               [PC=D3F2, SP=01F6, CC=N--B-I-C, A=08, X=09, Y=08]
-D3F2: B5 93          LDA   $93,X               [PC=D3F4, SP=01F6, CC=---B-IZC, A=00, X=09, Y=08]
-D3F4: CA             DEX               [PC=D3F5, SP=01F6, CC=N--B-I-C, A=00, X=08, Y=08]
-D3F5: 10 FA          BPL   $D3F1               [PC=D3F7, SP=01F6, CC=N--B-I-C, A=00, X=08, Y=08]
-D3F7: 20 84 E4       JSR   $E484               [PC=E484, SP=01F4, CC=N--B-I-C, A=00, X=08, Y=08]
-
-should be fixed now...
-*/
-
 /*
 DEX    Implied         DEX                     CA      1       2
 */
 
 static void OpCA(void)                                                 // DEX
 {
-       regs.x--;
-       SET_ZN(regs.x);
+       regs->x--;
+       SET_ZN(regs->x);
 }
 
 /*
@@ -959,8 +1003,8 @@ DEY        Implied         DEY                     88      1       2
 
 static void Op88(void)                                                 // DEY
 {
-       regs.y--;
-       SET_ZN(regs.y);
+       regs->y--;
+       SET_ZN(regs->y);
 }
 
 /*
@@ -978,60 +1022,63 @@ Absolute,Y               EOR Abs,Y       59      3       4
 // EOR opcodes
 
 #define OP_EOR_HANDLER(m) \
-       regs.a ^= m; \
-       SET_ZN(regs.a)
+       regs->a ^= m; \
+       SET_ZN(regs->a)
 
 static void Op49(void)                                                 // EOR #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_EOR_HANDLER(m);
 }
 
 static void Op45(void)                                                 // EOR ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_EOR_HANDLER(m);
 }
 
 static void Op55(void)                                                 // EOR ZP, X
 {
-       uint8 m = READ_ZP_X;
+       uint8_t m = READ_ZP_X;
        OP_EOR_HANDLER(m);
 }
 
 static void Op4D(void)                                                 // EOR ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_EOR_HANDLER(m);
 }
 
 static void Op5D(void)                                                 // EOR ABS, X
 {
-       uint8 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m = READ_ABS_X;
        OP_EOR_HANDLER(m);
 }
 
 static void Op59(void)                                                 // EOR ABS, Y
 {
-       uint8 m = READ_ABS_Y;
+       HANDLE_PAGE_CROSSING_ABS_Y;
+       uint8_t m = READ_ABS_Y;
        OP_EOR_HANDLER(m);
 }
 
 static void Op41(void)                                                 // EOR (ZP, X)
 {
-       uint8 m = READ_IND_ZP_X;
+       uint8_t m = READ_IND_ZP_X;
        OP_EOR_HANDLER(m);
 }
 
 static void Op51(void)                                                 // EOR (ZP), Y
 {
-       uint8 m = READ_IND_ZP_Y;
+       HANDLE_PAGE_CROSSING_IND_Y;
+       uint8_t m = READ_IND_ZP_Y;
        OP_EOR_HANDLER(m);
 }
 
 static void Op52(void)                                                 // EOR (ZP)
 {
-       uint8 m = READ_IND_ZP;
+       uint8_t m = READ_IND_ZP;
        OP_EOR_HANDLER(m);
 }
 
@@ -1041,8 +1088,8 @@ INA       Accumulator     INA                     1A      1       2
 
 static void Op1A(void)                                                 // INA
 {
-       regs.a++;
-       SET_ZN(regs.a);
+       regs->a++;
+       SET_ZN(regs->a);
 }
 
 /*
@@ -1060,7 +1107,7 @@ Absolute,X                INC Abs,X       FE      3       7
 
 static void OpE6(void)                                                 // INC ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        OP_INC_HANDLER(m);
        WRITE_BACK(m);
@@ -1068,7 +1115,7 @@ static void OpE6(void)                                                    // INC ZP
 
 static void OpF6(void)                                                 // INC ZP, X
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_X_WB(m);
        OP_INC_HANDLER(m);
        WRITE_BACK(m);
@@ -1076,7 +1123,7 @@ static void OpF6(void)                                                    // INC ZP, X
 
 static void OpEE(void)                                                 // INC ABS
 {
-       uint8 m;
+       uint8_t m;
        READ_ABS_WB(m);
        OP_INC_HANDLER(m);
        WRITE_BACK(m);
@@ -1084,7 +1131,8 @@ static void OpEE(void)                                                    // INC ABS
 
 static void OpFE(void)                                                 // INC ABS, X
 {
-       uint8 m;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m;
        READ_ABS_X_WB(m);
        OP_INC_HANDLER(m);
        WRITE_BACK(m);
@@ -1096,8 +1144,8 @@ INX       Implied         INX                     E8      1       2
 
 static void OpE8(void)                                                 // INX
 {
-       regs.x++;
-       SET_ZN(regs.x);
+       regs->x++;
+       SET_ZN(regs->x);
 }
 
 /*
@@ -1106,8 +1154,8 @@ INY       Implied         INY                     C8      1       2
 
 static void OpC8(void)                                                 // INY
 {
-       regs.y++;
-       SET_ZN(regs.y);
+       regs->y++;
+       SET_ZN(regs->y);
 }
 
 /*
@@ -1120,37 +1168,36 @@ JMP     Absolute        JMP Abs         4C      3       3
 
 static void Op4C(void)                                                 // JMP ABS
 {
-       regs.pc = RdMemW(regs.pc);
+       regs->pc = RdMemW(regs->pc);
 }
 
 static void Op6C(void)                                                 // JMP (ABS)
 {
-//     uint16 addr = RdMemW(regs.pc);
-//#ifdef __DEBUG__
-//WriteLog("\n[JMP ABS]: addr fetched = %04X, bytes at %04X = %02X %02X (RdMemw=%04X)\n",
-//     addr, addr, regs.RdMem(addr), regs.RdMem(addr+1), RdMemW(addr));
-//#endif
-//     addr = RdMemW(addr);
-       regs.pc = RdMemW(RdMemW(regs.pc));
+       // Check for page crossing
+       uint16_t addressLo = regs->RdMem(regs->pc);
+
+       if (addressLo == 0xFF)
+               regs->clock++;
+
+       regs->pc = RdMemW(RdMemW(regs->pc));
 }
 
 static void Op7C(void)                                                 // JMP (ABS, X)
 {
-       regs.pc = RdMemW(RdMemW(regs.pc) + regs.x);
+       regs->pc = RdMemW(RdMemW(regs->pc) + regs->x);
 }
 
 /*
 JSR    Absolute        JSR Abs         20      3       6
 */
 
-//This is not jumping to the correct address... !!! FIX !!! [DONE]
 static void Op20(void)                                                 // JSR
 {
-       uint16 addr = RdMemW(regs.pc);
-       regs.pc++;                                                                      // Since it pushes return address - 1...
-       regs.WrMem(0x0100 + regs.sp--, regs.pc >> 8);
-       regs.WrMem(0x0100 + regs.sp--, regs.pc & 0xFF);
-       regs.pc = addr;
+       uint16_t addr = RdMemW(regs->pc);
+       regs->pc++;                                                                     // Since it pushes return address - 1...
+       regs->WrMem(0x0100 + regs->sp--, regs->pc >> 8);
+       regs->WrMem(0x0100 + regs->sp--, regs->pc & 0xFF);
+       regs->pc = addr;
 }
 
 /*
@@ -1168,60 +1215,63 @@ Absolute,Y              LDA Abs,Y       B9      3       4
 // LDA opcodes
 
 #define OP_LDA_HANDLER(m) \
-       regs.a = m; \
-       SET_ZN(regs.a)
+       regs->a = m; \
+       SET_ZN(regs->a)
 
 static void OpA9(void)                                                 // LDA #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_LDA_HANDLER(m);
 }
 
 static void OpA5(void)                                                 // LDA ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_LDA_HANDLER(m);
 }
 
 static void OpB5(void)                                                 // LDA ZP, X
 {
-       uint8 m = READ_ZP_X;
+       uint8_t m = READ_ZP_X;
        OP_LDA_HANDLER(m);
 }
 
 static void OpAD(void)                                                 // LDA ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_LDA_HANDLER(m);
 }
 
 static void OpBD(void)                                                 // LDA ABS, X
 {
-       uint8 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m = READ_ABS_X;
        OP_LDA_HANDLER(m);
 }
 
 static void OpB9(void)                                                 // LDA ABS, Y
 {
-       uint8 m = READ_ABS_Y;
+       HANDLE_PAGE_CROSSING_ABS_Y;
+       uint8_t m = READ_ABS_Y;
        OP_LDA_HANDLER(m);
 }
 
 static void OpA1(void)                                                 // LDA (ZP, X)
 {
-       uint8 m = READ_IND_ZP_X;
+       uint8_t m = READ_IND_ZP_X;
        OP_LDA_HANDLER(m);
 }
 
 static void OpB1(void)                                                 // LDA (ZP), Y
 {
-       uint8 m = READ_IND_ZP_Y;
+       HANDLE_PAGE_CROSSING_IND_Y;
+       uint8_t m = READ_IND_ZP_Y;
        OP_LDA_HANDLER(m);
 }
 
 static void OpB2(void)                                                 // LDA (ZP)
 {
-       uint8 m = READ_IND_ZP;
+       uint8_t m = READ_IND_ZP;
        OP_LDA_HANDLER(m);
 }
 
@@ -1236,80 +1286,82 @@ Absolute,Y              LDX Abs,Y       BE      3       4
 // LDX opcodes
 
 #define OP_LDX_HANDLER(m) \
-       regs.x = m; \
-       SET_ZN(regs.x)
+       regs->x = m; \
+       SET_ZN(regs->x)
 
 static void OpA2(void)                                                 // LDX #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_LDX_HANDLER(m);
 }
 
 static void OpA6(void)                                                 // LDX ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_LDX_HANDLER(m);
 }
 
 static void OpB6(void)                                                 // LDX ZP, Y
 {
-       uint8 m = READ_ZP_Y;
+       uint8_t m = READ_ZP_Y;
        OP_LDX_HANDLER(m);
 }
 
 static void OpAE(void)                                                 // LDX ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_LDX_HANDLER(m);
 }
 
 static void OpBE(void)                                                 // LDX ABS, Y
 {
-       uint8 m = READ_ABS_Y;
+       HANDLE_PAGE_CROSSING_ABS_Y;
+       uint8_t m = READ_ABS_Y;
        OP_LDX_HANDLER(m);
 }
 
 /*
 LDY    Immediate       LDY #Oper       A0      2       2
 Zero Page              LDY Zpg         A4      2       3
-Zero Page,Y            LDY Zpg,X       B4      2       4
+Zero Page,X            LDY Zpg,X       B4      2       4
 Absolute               LDY Abs         AC      3       4
-Absolute,Y             LDY Abs,X       BC      3       4
+Absolute,X             LDY Abs,X       BC      3       4
 */
 
 // LDY opcodes
 
 #define OP_LDY_HANDLER(m) \
-       regs.y = m; \
-       SET_ZN(regs.y)
+       regs->y = m; \
+       SET_ZN(regs->y)
 
 static void OpA0(void)                                                 // LDY #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_LDY_HANDLER(m);
 }
 
 static void OpA4(void)                                                 // LDY ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_LDY_HANDLER(m);
 }
 
 static void OpB4(void)                                                 // LDY ZP, X
 {
-       uint8 m = READ_ZP_X;
+       uint8_t m = READ_ZP_X;
        OP_LDY_HANDLER(m);
 }
 
 static void OpAC(void)                                                 // LDY ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_LDY_HANDLER(m);
 }
 
 static void OpBC(void)                                                 // LDY ABS, X
 {
-       uint8 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m = READ_ABS_X;
        OP_LDY_HANDLER(m);
 }
 
@@ -1324,18 +1376,18 @@ Absolute,X              LSR Abs,X       5E      3       7
 // LSR opcodes
 
 #define OP_LSR_HANDLER(m) \
-       regs.cc = ((m) & 0x01 ? regs.cc | FLAG_C : regs.cc & ~FLAG_C); \
+       regs->cc = ((m) & 0x01 ? regs->cc | FLAG_C : regs->cc & ~FLAG_C); \
        (m) >>= 1; \
        CLR_N; SET_Z((m))
 
 static void Op4A(void)                                                 // LSR A
 {
-       OP_LSR_HANDLER(regs.a);
+       OP_LSR_HANDLER(regs->a);
 }
 
 static void Op46(void)                                                 // LSR ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        OP_LSR_HANDLER(m);
        WRITE_BACK(m);
@@ -1343,7 +1395,7 @@ static void Op46(void)                                                    // LSR ZP
 
 static void Op56(void)                                                 // LSR ZP, X
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_X_WB(m);
        OP_LSR_HANDLER(m);
        WRITE_BACK(m);
@@ -1351,7 +1403,7 @@ static void Op56(void)                                                    // LSR ZP, X
 
 static void Op4E(void)                                                 // LSR ABS
 {
-       uint8 m;
+       uint8_t m;
        READ_ABS_WB(m);
        OP_LSR_HANDLER(m);
        WRITE_BACK(m);
@@ -1359,7 +1411,8 @@ static void Op4E(void)                                                    // LSR ABS
 
 static void Op5E(void)                                                 // LSR ABS, X
 {
-       uint8 m;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m;
        READ_ABS_X_WB(m);
        OP_LSR_HANDLER(m);
        WRITE_BACK(m);
@@ -1388,60 +1441,63 @@ Absolute,Y              ORA Abs,Y       19      3       4
 // ORA opcodes
 
 #define OP_ORA_HANDLER(m) \
-       regs.a |= m; \
-       SET_ZN(regs.a)
+       regs->a |= m; \
+       SET_ZN(regs->a)
 
 static void Op09(void)                                                 // ORA #
 {
-       uint8 m = READ_IMM;
+       uint8_t m = READ_IMM;
        OP_ORA_HANDLER(m);
 }
 
 static void Op05(void)                                                 // ORA ZP
 {
-       uint8 m = READ_ZP;
+       uint8_t m = READ_ZP;
        OP_ORA_HANDLER(m);
 }
 
 static void Op15(void)                                                 // ORA ZP, X
 {
-       uint8 m = READ_ZP_X;
+       uint8_t m = READ_ZP_X;
        OP_ORA_HANDLER(m);
 }
 
 static void Op0D(void)                                                 // ORA ABS
 {
-       uint8 m = READ_ABS;
+       uint8_t m = READ_ABS;
        OP_ORA_HANDLER(m);
 }
 
 static void Op1D(void)                                                 // ORA ABS, X
 {
-       uint8 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m = READ_ABS_X;
        OP_ORA_HANDLER(m);
 }
 
 static void Op19(void)                                                 // ORA ABS, Y
 {
-       uint8 m = READ_ABS_Y;
+       HANDLE_PAGE_CROSSING_ABS_Y;
+       uint8_t m = READ_ABS_Y;
        OP_ORA_HANDLER(m);
 }
 
 static void Op01(void)                                                 // ORA (ZP, X)
 {
-       uint8 m = READ_IND_ZP_X;
+       uint8_t m = READ_IND_ZP_X;
        OP_ORA_HANDLER(m);
 }
 
 static void Op11(void)                                                 // ORA (ZP), Y
 {
-       uint8 m = READ_IND_ZP_Y;
+       HANDLE_PAGE_CROSSING_IND_Y;
+       uint8_t m = READ_IND_ZP_Y;
        OP_ORA_HANDLER(m);
 }
 
 static void Op12(void)                                                 // ORA (ZP)
 {
-       uint8 m = READ_IND_ZP;
+       uint8_t m = READ_IND_ZP;
        OP_ORA_HANDLER(m);
 }
 
@@ -1451,13 +1507,13 @@ PHA     Implied         PHA                     48      1       3
 
 static void Op48(void)                                                 // PHA
 {
-       regs.WrMem(0x0100 + regs.sp--, regs.a);
+       regs->WrMem(0x0100 + regs->sp--, regs->a);
 }
 
 static void Op08(void)                                                 // PHP
 {
-       regs.cc |= FLAG_UNK;                                            // Make sure that the unused bit is always set
-       regs.WrMem(0x0100 + regs.sp--, regs.cc);
+       regs->cc |= FLAG_UNK;                                           // Make sure that the unused bit is always set
+       regs->WrMem(0x0100 + regs->sp--, regs->cc);
 }
 
 /*
@@ -1466,7 +1522,7 @@ PHX       Implied         PHX                     DA      1       3
 
 static void OpDA(void)                                                 // PHX
 {
-       regs.WrMem(0x0100 + regs.sp--, regs.x);
+       regs->WrMem(0x0100 + regs->sp--, regs->x);
 }
 
 /*
@@ -1475,7 +1531,7 @@ PHY       Implied         PHY                     5A      1       3
 
 static void Op5A(void)                                                 // PHY
 {
-       regs.WrMem(0x0100 + regs.sp--, regs.y);
+       regs->WrMem(0x0100 + regs->sp--, regs->y);
 }
 
 /*
@@ -1484,13 +1540,13 @@ PLA     Implied         PLA                     68      1       4
 
 static void Op68(void)                                                 // PLA
 {
-       regs.a = regs.RdMem(0x0100 + ++regs.sp);
-       SET_ZN(regs.a);
+       regs->a = regs->RdMem(0x0100 + ++regs->sp);
+       SET_ZN(regs->a);
 }
 
 static void Op28(void)                                                 // PLP
 {
-       regs.cc = regs.RdMem(0x0100 + ++regs.sp);
+       regs->cc = regs->RdMem(0x0100 + ++regs->sp);
 }
 
 /*
@@ -1499,8 +1555,8 @@ PLX       Implied         PLX                     FA      1       4
 
 static void OpFA(void)                                                 // PLX
 {
-       regs.x = regs.RdMem(0x0100 + ++regs.sp);
-       SET_ZN(regs.x);
+       regs->x = regs->RdMem(0x0100 + ++regs->sp);
+       SET_ZN(regs->x);
 }
 
 /*
@@ -1509,8 +1565,8 @@ PLY       Implied         PLY                     7A      1       4
 
 static void Op7A(void)                                                 // PLY
 {
-       regs.y = regs.RdMem(0x0100 + ++regs.sp);
-       SET_ZN(regs.y);
+       regs->y = regs->RdMem(0x0100 + ++regs->sp);
+       SET_ZN(regs->y);
 }
 
 /*
@@ -1525,7 +1581,7 @@ The bit set and clear instructions have the form xyyy0111, where x is 0 to clear
 
 static void Op07(void)                                                 // RMB0 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m &= 0xFE;
        WRITE_BACK(m);
@@ -1533,7 +1589,7 @@ static void Op07(void)                                                    // RMB0 ZP
 
 static void Op17(void)                                                 // RMB1 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m &= 0xFD;
        WRITE_BACK(m);
@@ -1541,7 +1597,7 @@ static void Op17(void)                                                    // RMB1 ZP
 
 static void Op27(void)                                                 // RMB2 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m &= 0xFB;
        WRITE_BACK(m);
@@ -1549,7 +1605,7 @@ static void Op27(void)                                                    // RMB2 ZP
 
 static void Op37(void)                                                 // RMB3 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m &= 0xF7;
        WRITE_BACK(m);
@@ -1557,7 +1613,7 @@ static void Op37(void)                                                    // RMB3 ZP
 
 static void Op47(void)                                                 // RMB4 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m &= 0xEF;
        WRITE_BACK(m);
@@ -1565,7 +1621,7 @@ static void Op47(void)                                                    // RMB4 ZP
 
 static void Op57(void)                                                 // RMB5 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m &= 0xDF;
        WRITE_BACK(m);
@@ -1573,7 +1629,7 @@ static void Op57(void)                                                    // RMB5 ZP
 
 static void Op67(void)                                                 // RMB6 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m &= 0xBF;
        WRITE_BACK(m);
@@ -1581,7 +1637,7 @@ static void Op67(void)                                                    // RMB6 ZP
 
 static void Op77(void)                                                 // RMB7 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m &= 0x7F;
        WRITE_BACK(m);
@@ -1598,19 +1654,19 @@ Absolute,X              ROL Abs,X       3E      3       7
 // ROL opcodes
 
 #define OP_ROL_HANDLER(m) \
-       uint8 tmp = regs.cc & 0x01; \
-       regs.cc = ((m) & 0x80 ? regs.cc | FLAG_C : regs.cc & ~FLAG_C); \
+       uint8_t tmp = regs->cc & 0x01; \
+       regs->cc = ((m) & 0x80 ? regs->cc | FLAG_C : regs->cc & ~FLAG_C); \
        (m) = ((m) << 1) | tmp; \
        SET_ZN((m))
 
 static void Op2A(void)                                                 // ROL A
 {
-       OP_ROL_HANDLER(regs.a);
+       OP_ROL_HANDLER(regs->a);
 }
 
 static void Op26(void)                                                 // ROL ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        OP_ROL_HANDLER(m);
        WRITE_BACK(m);
@@ -1618,7 +1674,7 @@ static void Op26(void)                                                    // ROL ZP
 
 static void Op36(void)                                                 // ROL ZP, X
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_X_WB(m);
        OP_ROL_HANDLER(m);
        WRITE_BACK(m);
@@ -1626,7 +1682,7 @@ static void Op36(void)                                                    // ROL ZP, X
 
 static void Op2E(void)                                                 // ROL ABS
 {
-       uint8 m;
+       uint8_t m;
        READ_ABS_WB(m);
        OP_ROL_HANDLER(m);
        WRITE_BACK(m);
@@ -1634,7 +1690,8 @@ static void Op2E(void)                                                    // ROL ABS
 
 static void Op3E(void)                                                 // ROL ABS, X
 {
-       uint8 m;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m;
        READ_ABS_X_WB(m);
        OP_ROL_HANDLER(m);
        WRITE_BACK(m);
@@ -1651,19 +1708,19 @@ Absolute,X              ROR Abs,X       7E      3       7
 // ROR opcodes
 
 #define OP_ROR_HANDLER(m) \
-       uint8 tmp = (regs.cc & 0x01) << 7; \
-       regs.cc = ((m) & 0x01 ? regs.cc | FLAG_C : regs.cc & ~FLAG_C); \
+       uint8_t tmp = (regs->cc & 0x01) << 7; \
+       regs->cc = ((m) & 0x01 ? regs->cc | FLAG_C : regs->cc & ~FLAG_C); \
        (m) = ((m) >> 1) | tmp; \
        SET_ZN((m))
 
 static void Op6A(void)                                                 // ROR A
 {
-       OP_ROR_HANDLER(regs.a);
+       OP_ROR_HANDLER(regs->a);
 }
 
 static void Op66(void)                                                 // ROR ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        OP_ROR_HANDLER(m);
        WRITE_BACK(m);
@@ -1671,7 +1728,7 @@ static void Op66(void)                                                    // ROR ZP
 
 static void Op76(void)                                                 // ROR ZP, X
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_X_WB(m);
        OP_ROR_HANDLER(m);
        WRITE_BACK(m);
@@ -1679,7 +1736,7 @@ static void Op76(void)                                                    // ROR ZP, X
 
 static void Op6E(void)                                                 // ROR ABS
 {
-       uint8 m;
+       uint8_t m;
        READ_ABS_WB(m);
        OP_ROR_HANDLER(m);
        WRITE_BACK(m);
@@ -1687,7 +1744,8 @@ static void Op6E(void)                                                    // ROR ABS
 
 static void Op7E(void)                                                 // ROR ABS, X
 {
-       uint8 m;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint8_t m;
        READ_ABS_X_WB(m);
        OP_ROR_HANDLER(m);
        WRITE_BACK(m);
@@ -1699,12 +1757,9 @@ RTI      Implied         RTI                     40      1       6
 
 static void Op40(void)                                                 // RTI
 {
-       regs.cc = regs.RdMem(0x0100 + ++regs.sp);
-//clear I (seems to be the case, either that or clear it in the IRQ setup...)
-//I can't find *any* verification that this is the case.
-//     regs.cc &= ~FLAG_I;
-       regs.pc = regs.RdMem(0x0100 + ++regs.sp);
-       regs.pc |= (uint16)(regs.RdMem(0x0100 + ++regs.sp)) << 8;
+       regs->cc = regs->RdMem(0x0100 + ++regs->sp);
+       regs->pc = regs->RdMem(0x0100 + ++regs->sp);
+       regs->pc |= (uint16_t)(regs->RdMem(0x0100 + ++regs->sp)) << 8;
 }
 
 /*
@@ -1713,11 +1768,9 @@ RTS      Implied         RTS                     60      1       6
 
 static void Op60(void)                                                 // RTS
 {
-       regs.pc = regs.RdMem(0x0100 + ++regs.sp);
-       regs.pc |= (uint16)(regs.RdMem(0x0100 + ++regs.sp)) << 8;
-       regs.pc++;                                                                      // Since it pushes return address - 1...
-//printf("*** RTS: PC = $%04X, SP= $1%02X\n", regs.pc, regs.sp);
-//fflush(stdout);
+       regs->pc = regs->RdMem(0x0100 + ++regs->sp);
+       regs->pc |= (uint16_t)(regs->RdMem(0x0100 + ++regs->sp)) << 8;
+       regs->pc++;                                                                     // Since it pushes return address - 1...
 }
 
 /*
@@ -1735,87 +1788,88 @@ Absolute,Y              SBC Abs,Y       F9      3       4
 // SBC opcodes
 
 //This is non-optimal, but it works--optimize later. :-)
-//This is correct except for the BCD handling... !!! FIX !!! [Possibly DONE]
+// We do the BCD subtraction one nybble at a time to ensure a correct result.
+// 9 - m is a "Nine's Complement".  We do the BCD subtraction as a 9s
+// complement addition because it's easier and it works.  :-)  Also, Decimal
+// mode incurs a once cycle penalty (for the decimal correction).
 #define OP_SBC_HANDLER(m) \
-       uint16 sum = (uint16)regs.a - (m) - (uint16)((regs.cc & FLAG_C) ^ 0x01); \
+       uint16_t sum = (uint16_t)regs->a - (m) - (uint16_t)((regs->cc & FLAG_C) ^ 0x01); \
 \
-       if (regs.cc & FLAG_D) \
+       if (regs->cc & FLAG_D) \
        { \
-               if ((sum & 0x0F) > 0x09) \
-                       sum -= 0x06; \
+               sum = (regs->a & 0x0F) + (9 - ((m) & 0x0F)) + (uint16_t)(regs->cc & FLAG_C); \
+\
+               if (sum > 0x09) \
+                       sum += 0x06; \
+\
+               sum += (regs->a & 0xF0) + (0x90 - ((m) & 0xF0)); \
 \
-               if ((sum & 0xF0) > 0x90) \
-                       sum -= 0x60; \
+               if (sum > 0x99) \
+                       sum += 0x60; \
+\
+               sum ^= 0x100; /* Invert carry, for active low borrow */ \
+               regs->clock++;\
        } \
 \
-       regs.cc = (regs.cc & ~FLAG_C) | (((sum >> 8) ^ 0x01) & FLAG_C); \
-       regs.cc = ((regs.a ^ (m)) & (regs.a ^ sum) & 0x80 ? regs.cc | FLAG_V : regs.cc & ~FLAG_V); \
-       regs.a = sum & 0xFF; \
-       SET_ZN(regs.a)
-
-/*
-D5AF: 38             SEC               [PC=D5B0, SP=01F6, CC=---B-I-C, A=4C, X=00, Y=06]
-
-*** HERE'S where it sets the D flag on a subtract... Arg!
-
-D5B0: F1 9D          SBC   ($9D),Y     [PC=D5B2, SP=01F6, CC=N--BDI--, A=FE, X=00, Y=06]
-
-Fixed. :-)
-*/
-
-//OLD V detection: regs.cc = ((regs.a ^ (m) ^ sum ^ (regs.cc << 7)) & 0x80 ? regs.cc | FLAG_V : regs.cc & ~FLAG_V);
+       regs->cc = (regs->cc & ~FLAG_C) | (((sum >> 8) ^ 0x01) & FLAG_C); \
+       regs->cc = ((regs->a ^ (m)) & (regs->a ^ sum) & 0x80 ? regs->cc | FLAG_V : regs->cc & ~FLAG_V); \
+       regs->a = sum & 0xFF; \
+       SET_ZN(regs->a)
 
 static void OpE9(void)                                                 // SBC #
 {
-       uint16 m = READ_IMM;
+       uint16_t m = READ_IMM;
        OP_SBC_HANDLER(m);
 }
 
 static void OpE5(void)                                                 // SBC ZP
 {
-       uint16 m = READ_ZP;
+       uint16_t m = READ_ZP;
        OP_SBC_HANDLER(m);
 }
 
 static void OpF5(void)                                                 // SBC ZP, X
 {
-       uint16 m = READ_ZP_X;
+       uint16_t m = READ_ZP_X;
        OP_SBC_HANDLER(m);
 }
 
 static void OpED(void)                                                 // SBC ABS
 {
-       uint16 m = READ_ABS;
+       uint16_t m = READ_ABS;
        OP_SBC_HANDLER(m);
 }
 
 static void OpFD(void)                                                 // SBC ABS, X
 {
-       uint16 m = READ_ABS_X;
+       HANDLE_PAGE_CROSSING_ABS_X;
+       uint16_t m = READ_ABS_X;
        OP_SBC_HANDLER(m);
 }
 
 static void OpF9(void)                                                 // SBC ABS, Y
 {
-       uint16 m = READ_ABS_Y;
+       HANDLE_PAGE_CROSSING_ABS_Y;
+       uint16_t m = READ_ABS_Y;
        OP_SBC_HANDLER(m);
 }
 
 static void OpE1(void)                                                 // SBC (ZP, X)
 {
-       uint16 m = READ_IND_ZP_X;
+       uint16_t m = READ_IND_ZP_X;
        OP_SBC_HANDLER(m);
 }
 
 static void OpF1(void)                                                 // SBC (ZP), Y
 {
-       uint16 m = READ_IND_ZP_Y;
+       HANDLE_PAGE_CROSSING_IND_Y;
+       uint16_t m = READ_IND_ZP_Y;
        OP_SBC_HANDLER(m);
 }
 
 static void OpF2(void)                                                 // SBC (ZP)
 {
-       uint16 m = READ_IND_ZP;
+       uint16_t m = READ_IND_ZP;
        OP_SBC_HANDLER(m);
 }
 
@@ -1825,7 +1879,7 @@ SEC       Implied         SEC                     38      1       2
 
 static void Op38(void)                                                 // SEC
 {
-       regs.cc |= FLAG_C;
+       regs->cc |= FLAG_C;
 }
 
 /*
@@ -1834,7 +1888,7 @@ SED       Implied         SED                     F8      1       2
 
 static void OpF8(void)                                                 // SED
 {
-       regs.cc |= FLAG_D;
+       regs->cc |= FLAG_D;
 }
 
 /*
@@ -1843,7 +1897,7 @@ SEI       Implied         SEI                     78      1       2
 
 static void Op78(void)                                                 // SEI
 {
-       regs.cc |= FLAG_I;
+       SET_I;
 }
 
 /*
@@ -1858,7 +1912,7 @@ The bit set and clear instructions have the form xyyy0111, where x is 0 to clear
 
 static void Op87(void)                                                 // SMB0 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m |= 0x01;
        WRITE_BACK(m);
@@ -1866,7 +1920,7 @@ static void Op87(void)                                                    // SMB0 ZP
 
 static void Op97(void)                                                 // SMB1 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m |= 0x02;
        WRITE_BACK(m);
@@ -1874,7 +1928,7 @@ static void Op97(void)                                                    // SMB1 ZP
 
 static void OpA7(void)                                                 // SMB2 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m |= 0x04;
        WRITE_BACK(m);
@@ -1882,7 +1936,7 @@ static void OpA7(void)                                                    // SMB2 ZP
 
 static void OpB7(void)                                                 // SMB3 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m |= 0x08;
        WRITE_BACK(m);
@@ -1890,7 +1944,7 @@ static void OpB7(void)                                                    // SMB3 ZP
 
 static void OpC7(void)                                                 // SMB4 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m |= 0x10;
        WRITE_BACK(m);
@@ -1898,7 +1952,7 @@ static void OpC7(void)                                                    // SMB4 ZP
 
 static void OpD7(void)                                                 // SMB5 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m |= 0x20;
        WRITE_BACK(m);
@@ -1906,7 +1960,7 @@ static void OpD7(void)                                                    // SMB5 ZP
 
 static void OpE7(void)                                                 // SMB6 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m |= 0x40;
        WRITE_BACK(m);
@@ -1914,7 +1968,7 @@ static void OpE7(void)                                                    // SMB6 ZP
 
 static void OpF7(void)                                                 // SMB7 ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        m |= 0x80;
        WRITE_BACK(m);
@@ -1935,42 +1989,42 @@ Absolute,Y              STA Abs,Y       99      3       5
 
 static void Op85(void)
 {
-       regs.WrMem(EA_ZP, regs.a);
+       regs->WrMem(EA_ZP, regs->a);
 }
 
 static void Op95(void)
 {
-       regs.WrMem(EA_ZP_X, regs.a);
+       regs->WrMem(EA_ZP_X, regs->a);
 }
 
 static void Op8D(void)
 {
-       regs.WrMem(EA_ABS, regs.a);
+       regs->WrMem(EA_ABS, regs->a);
 }
 
 static void Op9D(void)
 {
-       regs.WrMem(EA_ABS_X, regs.a);
+       regs->WrMem(EA_ABS_X, regs->a);
 }
 
 static void Op99(void)
 {
-       regs.WrMem(EA_ABS_Y, regs.a);
+       regs->WrMem(EA_ABS_Y, regs->a);
 }
 
 static void Op81(void)
 {
-       regs.WrMem(EA_IND_ZP_X, regs.a);
+       regs->WrMem(EA_IND_ZP_X, regs->a);
 }
 
 static void Op91(void)
 {
-       regs.WrMem(EA_IND_ZP_Y, regs.a);
+       regs->WrMem(EA_IND_ZP_Y, regs->a);
 }
 
 static void Op92(void)
 {
-       regs.WrMem(EA_IND_ZP, regs.a);
+       regs->WrMem(EA_IND_ZP, regs->a);
 }
 
 /*
@@ -1983,17 +2037,17 @@ Absolute                STX Abs         8E      3       4
 
 static void Op86(void)
 {
-       regs.WrMem(EA_ZP, regs.x);
+       regs->WrMem(EA_ZP, regs->x);
 }
 
 static void Op96(void)
 {
-       regs.WrMem(EA_ZP_X, regs.x);
+       regs->WrMem(EA_ZP_Y, regs->x);
 }
 
 static void Op8E(void)
 {
-       regs.WrMem(EA_ABS, regs.x);
+       regs->WrMem(EA_ABS, regs->x);
 }
 
 /*
@@ -2006,17 +2060,17 @@ Absolute                STY Abs         8C      3       4
 
 static void Op84(void)
 {
-       regs.WrMem(EA_ZP, regs.y);
+       regs->WrMem(EA_ZP, regs->y);
 }
 
 static void Op94(void)
 {
-       regs.WrMem(EA_ZP_X, regs.y);
+       regs->WrMem(EA_ZP_X, regs->y);
 }
 
 static void Op8C(void)
 {
-       regs.WrMem(EA_ABS, regs.y);
+       regs->WrMem(EA_ABS, regs->y);
 }
 
 /*
@@ -2030,22 +2084,22 @@ Absolute,X              STZ Abs,X       9E      3       5
 
 static void Op64(void)
 {
-       regs.WrMem(EA_ZP, 0x00);
+       regs->WrMem(EA_ZP, 0x00);
 }
 
 static void Op74(void)
 {
-       regs.WrMem(EA_ZP_X, 0x00);
+       regs->WrMem(EA_ZP_X, 0x00);
 }
 
 static void Op9C(void)
 {
-       regs.WrMem(EA_ABS, 0x00);
+       regs->WrMem(EA_ABS, 0x00);
 }
 
 static void Op9E(void)
 {
-       regs.WrMem(EA_ABS_X, 0x00);
+       regs->WrMem(EA_ABS_X, 0x00);
 }
 
 /*
@@ -2054,8 +2108,8 @@ TAX       Implied         TAX                     AA      1       2
 
 static void OpAA(void)                                                 // TAX
 {
-       regs.x = regs.a;
-       SET_ZN(regs.x);
+       regs->x = regs->a;
+       SET_ZN(regs->x);
 }
 
 /*
@@ -2064,8 +2118,8 @@ TAY       Implied         TAY                     A8      1       2
 
 static void OpA8(void)                                                 // TAY
 {
-       regs.y = regs.a;
-       SET_ZN(regs.y);
+       regs->y = regs->a;
+       SET_ZN(regs->y);
 }
 
 /*
@@ -2076,12 +2130,12 @@ Absolute                TRB Abs         1C      3       6
 // TRB opcodes
 
 #define OP_TRB_HANDLER(m) \
-       SET_Z(m & regs.a); \
-       m &= ~regs.a
+       SET_Z(m & regs->a); \
+       m &= ~regs->a
 
 static void Op14(void)                                                 // TRB ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        OP_TRB_HANDLER(m);
        WRITE_BACK(m);
@@ -2089,7 +2143,7 @@ static void Op14(void)                                                    // TRB ZP
 
 static void Op1C(void)                                                 // TRB ABS
 {
-       uint8 m;
+       uint8_t m;
        READ_ABS_WB(m);
        OP_TRB_HANDLER(m);
        WRITE_BACK(m);
@@ -2103,12 +2157,12 @@ Absolute                TSB Abs         0C      3       6
 // TSB opcodes
 
 #define OP_TSB_HANDLER(m) \
-       SET_Z(m & regs.a); \
-       m |= regs.a
+       SET_Z(m & regs->a); \
+       m |= regs->a
 
 static void Op04(void)                                                 // TSB ZP
 {
-       uint8 m;
+       uint8_t m;
        READ_ZP_WB(m);
        OP_TSB_HANDLER(m);
        WRITE_BACK(m);
@@ -2116,7 +2170,7 @@ static void Op04(void)                                                    // TSB ZP
 
 static void Op0C(void)                                                 // TSB ABS
 {
-       uint8 m;
+       uint8_t m;
        READ_ABS_WB(m);
        OP_TSB_HANDLER(m);
        WRITE_BACK(m);
@@ -2128,8 +2182,8 @@ TSX       Implied         TSX                     BA      1       2
 
 static void OpBA(void)                                                 // TSX
 {
-       regs.x = regs.sp;
-       SET_ZN(regs.x);
+       regs->x = regs->sp;
+       SET_ZN(regs->x);
 }
 
 /*
@@ -2138,8 +2192,8 @@ TXA       Implied         TXA                     8A      1       2
 
 static void Op8A(void)                                                 // TXA
 {
-       regs.a = regs.x;
-       SET_ZN(regs.a);
+       regs->a = regs->x;
+       SET_ZN(regs->a);
 }
 
 /*
@@ -2148,7 +2202,7 @@ TXS       Implied         TXS                     9A      1       2
 
 static void Op9A(void)                                                 // TXS
 {
-       regs.sp = regs.x;
+       regs->sp = regs->x;
 }
 
 /*
@@ -2156,21 +2210,20 @@ TYA     Implied         TYA                     98      1       2
 */
 static void Op98(void)                                                 // TYA
 {
-       regs.a = regs.y;
-       SET_ZN(regs.a);
+       regs->a = regs->y;
+       SET_ZN(regs->a);
 }
 
 static void Op__(void)
 {
-       regs.cpuFlags |= V65C02_STATE_ILLEGAL_INST;
+       regs->cpuFlags |= V65C02_STATE_ILLEGAL_INST;
 }
 
-
 //
 // Ok, the exec_op[] array is globally defined here basically to save
 // a LOT of unnecessary typing.  Sure it's ugly, but hey, it works!
 //
-void (* exec_op[256])() = {
+static void (* exec_op[256])() = {
        Op00, Op01, Op__, Op__, Op04, Op05, Op06, Op07, Op08, Op09, Op0A, Op__, Op0C, Op0D, Op0E, Op0F,
        Op10, Op11, Op12, Op__, Op14, Op15, Op16, Op17, Op18, Op19, Op1A, Op__, Op1C, Op1D, Op1E, Op1F,
        Op20, Op21, Op__, Op__, Op24, Op25, Op26, Op27, Op28, Op29, Op2A, Op__, Op2C, Op2D, Op2E, Op2F,
@@ -2189,16 +2242,6 @@ void (* exec_op[256])() = {
        OpF0, OpF1, OpF2, Op__, Op__, OpF5, OpF6, OpF7, OpF8, OpF9, OpFA, Op__, Op__, OpFD, OpFE, OpFF
 };
 
-//
-// Internal "memcpy" (so we don't have to link with any external libraries!)
-//
-static void myMemcpy(void * dst, void * src, uint32 size)
-{
-       uint8 * d = (uint8 *)dst, * s = (uint8 *)src;
-
-       for(uint32 i=0; i<size; i++)
-               d[i] = s[i];
-}
 
 /*
 FCA8: 38        698  WAIT     SEC
@@ -2225,76 +2268,558 @@ FBEF: 60        602  RTS2B    RTS
 //int instCount[256];
 #ifdef __DEBUG__
 bool dumpDis = false;
+//bool dumpDis = true;
 #endif
 
-//Note: could enforce regs.clock to zero on starting the CPU with an Init() function...
-//bleh.
-//static uint32 limit = 0;
+/*
+On //e, $FCAA is the delay routine. (seems to not have changed from ][+)
+*/
+
+#define DO_BACKTRACE
+#ifdef DO_BACKTRACE
+#define BACKTRACE_SIZE 16384
+uint32_t btQueuePtr = 0;
+V65C02REGS btQueue[BACKTRACE_SIZE];
+uint8_t btQueueInst[BACKTRACE_SIZE][4];
+#endif
 
 //
 // Function to execute 65C02 for "cycles" cycles
 //
-void Execute65C02(V65C02REGS * context, uint32 cycles)
+//static bool first = true;
+void Execute65C02(V65C02REGS * context, uint32_t cycles)
 {
-       myMemcpy(&regs, context, sizeof(V65C02REGS));
+       regs = context;
 
-       // Execute here...
-// NOTE: There *must* be some way of doing this without requiring the caller to subtract out
-//       the previous run's cycles. !!! FIX !!!
-// Could try:
-//     while (regs.clock < regs.clock + cycles) <-- won't work
-/*
-       // This isn't as accurate as subtracting out cycles from regs.clock...
-       // Unless limit is a static variable, adding cycles to it each time through...
-       uint32 limit = regs.clock + cycles;
-       while (regs.clock < limit)
-*/
-// but have wraparound to deal with. :-/
-/*
-Let's see...
+       // Calculate number of clock cycles to run for
+       uint64_t endCycles = regs->clock + (uint64_t)cycles - regs->overflow;
 
-       if (regs.clock + cycles > 0xFFFFFFFF)
-               wraparound = true;
-*/
-       uint64 endCycles = regs.clock + (uint64)cycles;
+       while (regs->clock < endCycles)
+       {
+// Ultima I (WTF? This *used* to work! >:-U) [Now it does...  :-P]
+// Turns out it was a problem with PAGE2 changing too much (it ignored the HIRES switch when switching memory, causing code at $2141 to be swapped out with zeroes).
+/*if (regs->pc == 0xC311)
+       dumpDis = true;
+else if (regs->pc == 0x2141)
+       dumpDis = false;*/
+
+#if 0
+// Bard's Tale II
+// Turns out a bug in floppydrive.cpp prevented this from working.  :-P
+static bool hitGo = false;
+
+if (regs->pc == 0xA000)
+{
+       dumpDis = true;
+       hitGo = true;
+}
+
+// $FCA8 also needs to be silenced too ($FCB3 is exit point)
+if (regs->pc == 0xA181 && hitGo)
+{
+       dumpDis = false;
+       WriteLog("*** BT2 DELAY\n");
+}
+else if (regs->pc == 0xA18B && hitGo)
+{
+       dumpDis = true;
+}
+else if (regs->pc == 0xFCA8 && hitGo)
+{
+       dumpDis = false;
+       WriteLog("*** MONITOR DELAY ($FCA8)\n");
+}
+else if (regs->pc == 0xFCB3 && hitGo)
+{
+       dumpDis = true;
+}
+else if (regs->pc == 0xBD11 && hitGo)
+{
+       dumpDis = false;
+       WriteLog("*** BT2 DELAY $BD11\n");
+}
+else if (regs->pc == 0xDB1E && hitGo)
+{
+       dumpDis = true;
+}
+else if (regs->pc == 0xA003 && hitGo)
+{
+       dumpDis = false;
+       WriteLog("*** BT2 CHECK FOR $B7s\n");
+}
+else if (regs->pc == 0xA063 && hitGo)
+{
+       dumpDis = true;
+}
+else if (regs->pc == 0xA0FE && hitGo)
+{
+       dumpDis = false;
+       WriteLog("*** BT2 CHECK FOR $D5 $AA $96 HEADER\n");
+}
+else if (regs->pc == 0xA112 && hitGo)
+{
+       dumpDis = true;
+       WriteLog("*** BT2 LOOK FOR HEADER FAILED\n");
+}
+else if (regs->pc == 0xA14B && hitGo)
+{
+       dumpDis = true;
+}
+else if (regs->pc == 0xA254 && hitGo)
+{
+       // This is where it hits a BRK and goes BOOM
+       dumpDis = false;
+       hitGo = false;
+}
+else if (regs->pc == 0xA155)
+{
+       WriteLog("*** $A2E2 is %02X...\n", regs->RdMem(0xA2E2));
+}
+else if (regs->pc == 0xA1C2)
+{
+       static char bcName[13][5] = { "JMP", "JMPA", "BNE", "LDI", "JSR", "LDA", "SUB", "STA", "RTS", "JMPA", "INC", "CRSH", "ILDA" };
+       static int bcLen[13] = { 2, 2, 2, 1, 2, 2, 1, 2, 0, 2, 2, 0, 2 };
+
+       uint16_t addr = RdMemWZP(0x52) + regs->y;
+       uint8_t bytecode = regs->RdMem(addr);
+       uint16_t bcAddr = ((regs->RdMem(addr + 2) ^ 0xD9) << 8) | (regs->RdMem(addr + 1) ^ 0x03);
+       uint8_t bcVal = regs->RdMem(addr + 1) ^ 0x4C;
+
+       WriteLog("\n*** bc %04X: %s ", addr, bcName[bytecode]);
+
+       if (bcLen[bytecode] == 1)
+               WriteLog("$%02X", bcVal);
+       else if (bcLen[bytecode] == 2)
+               WriteLog("$%04X", bcAddr);
+
+       WriteLog("\n\n");
+}
+#endif
+
+#if 0
+// Border Zone timing...
+static bool inDelay1 = false;
+static bool inDelay2 = false;
+static bool inRead1 = false;
+static bool hitGo = false;
+if (regs->pc == 0xF0B1)
+       WriteLog("*** $F09C ($6F,70) -> $%02X%02X\n", regs->RdMem(0x70), regs->RdMem(0x6F));
+
+if (regs->pc == 0xC8F2)
+       hitGo = true;
+
+// Delay is $D20D to $D21D...
+if (regs->pc == 0xD20D && hitGo && !inDelay1)
+{
+       dumpDis = false;
+       inDelay1 = true;
+       WriteLog("*** DELAY\n");
+}
+else if (regs->pc == 0xD21D && inDelay1)
+{
+       dumpDis = true;
+       inDelay1 = false;
+}
 
-       while (regs.clock < endCycles)
+// Next delay starts @ $D356 - $D36A
+else if (regs->pc == 0xD356 && hitGo && !inDelay2)
+{
+       dumpDis = false;
+       inDelay2 = true;
+       WriteLog("*** DELAY #2\n");
+}
+else if (regs->pc == 0xD36A && inDelay2)
+{
+       dumpDis = true;
+       inDelay2 = false;
+}
+else if (regs->pc == 0xD486 && hitGo && !inRead1)
+{
+       dumpDis = false;
+       inRead1 = true;
+       WriteLog("\n*** FAST READ ROUTINE (!!!)\n\n");
+}
+else if (regs->pc == 0xD4B1 && inRead1)
+{
+       dumpDis = true;
+       inRead1 = false;
+}
+#endif
+#if 0
+// 13-sector disk debugging
+// start with the slot ROM
+static bool inDelay = false;
+static bool inBell = false;
+static bool inReadSector = false;
+static bool inSlotROM = false;
+if (regs->pc == 0xFCA8)// && !inSlotROM)//!inBell && !inReadSector)
+{
+       dumpDis = false;
+       inDelay = true;
+       WriteLog("*** DELAY\n");
+}
+else if (regs->pc == 0xFCB3 && inDelay && inSlotROM)//&& !inBell && !inReadSector)
+{
+       dumpDis = true;
+       inDelay = false;
+}
+if (regs->pc == 0xFBD9)
+{
+       dumpDis = false;
+       inBell = true;
+       WriteLog("*** BELL1\n");
+}
+else if (regs->pc == 0xFBEF && inBell)
+{
+//     dumpDis = true;
+       inBell = false;
+}
+//else if (regs->pc == 0xC664)
+else if (regs->pc == 0xC663)
+{
+       dumpDis = true;
+       inSlotROM = true;
+       WriteLog("*** DISK @ $C600\n");
+}
+else if (regs->pc == 0x801)
+{
+       WriteLog("*** DISK @ $801\n");
+       dumpDis = true;
+}
+#endif
+// Hard disk debugging
+#if 0
+if (first && (regs->pc == 0x801))
+{
+//     regs->WrMem(0x42, 1); // v3.0 does this now...
+       regs->WrMem(0x44, 0); // who writes non-zero to here??? (AHSSC does)
+       first = false;
+//     dumpDis = true;
+//WriteLog("V65C02: Executing $801...\n");
+}
+else if (regs->pc == 0x869)
+{
+/*     regs->WrMem(0x42, 1);
+       first = false;//*/
+/*     static char disbuf[80];
+       uint16_t pc=0x801;
+       while (pc < 0xA00)
        {
+               pc += Decode65C02(regs, disbuf, pc);
+               WriteLog("%s\n", disbuf);
+       }*/
+/*     dumpDis = true;
+       WriteLog("\n>>> $42-7: %02X %02X %02X %02X %02X %02X\n\n", regs->RdMem(0x42), regs->RdMem(0x43), regs->RdMem(0x44), regs->RdMem(0x45), regs->RdMem(0x46), regs->RdMem(0x47));//*/
+}
+#endif
+#if 0
+//Epoch
+if (regs->pc == 0x0518)
+{
+       dumpDis = true;
+}
+else if (regs->pc == 0x051E)
+{
+       uint16_t c1 = regs->RdMem(0xFF);
+       uint16_t c2 = regs->RdMem(0x00);
+       WriteLog("$FF/$00 = $%02X $%02X\n", c1, c2);
+       WriteLog("--> $%02X\n", regs->RdMem((c2 << 8) | c1));
+}
+else if (regs->pc == 0x0522)
+{
+       uint16_t c1 = regs->RdMem(0xFF);
+       uint16_t c2 = regs->RdMem(0x00);
+       WriteLog("$FF/$00 = $%02X $%02X\n", c1, c2);
+       WriteLog("--> $%02X\n", regs->RdMem(((c2 << 8) | c1) + 1));
+}
+#endif
+#if 0
+// Up N Down testing
+// Now Ankh testing...
+static bool inDelay = false;
+static bool inBell = false;
+static bool inReadSector = false;
+if (regs->pc == 0xFCA8 && !inBell && !inReadSector)
+{
+       dumpDis = false;
+       inDelay = true;
+       WriteLog("*** DELAY\n");
+}
+else if (regs->pc == 0xFCB3 && inDelay && !inBell && !inReadSector)
+{
+       dumpDis = true;
+       inDelay = false;
+}
+if (regs->pc == 0xFBD9)
+{
+       dumpDis = false;
+       inBell = true;
+       WriteLog("*** BELL1\n");
+}
+else if (regs->pc == 0xFBEF && inBell)
+{
+       dumpDis = true;
+       inBell = false;
+}
+else if (regs->pc == 0xC600)
+{
+       dumpDis = false;
+       WriteLog("*** DISK @ $C600\n");
+}
+else if (regs->pc == 0x801)
+{
+       WriteLog("*** DISK @ $801\n");
+       dumpDis = true;
+}
+else if (regs->pc == 0xC119)
+{
+       dumpDis = false;
+       WriteLog("*** BIOS @ $C119\n");
+}
+else if (regs->pc == 0xC117)
+{
+       dumpDis = true;
+}
+else if (regs->pc == 0x843)
+{
+       dumpDis = false;
+       inReadSector = true;
+       uint16_t lo = regs->RdMem(0x26);
+       uint16_t hi = regs->RdMem(0x27);
+       WriteLog("\n*** DISK Read sector ($26=$%04X)...\n\n", (hi << 8) | lo);
+}
+else if (regs->pc == 0x8FC)
+{
+       dumpDis = true;
+       inReadSector = false;
+}
+else if (regs->pc == 0xA8A8 || regs->pc == 0xC100)
+{
+       dumpDis = false;
+}
+else if (regs->pc == 0x8FD)
+{
+//     regs->WrMem(0x827, 3);
+//     regs->WrMem(0x82A, 0);
+//1 doesn't work, but 2 does (only with WOZ, not with DSK; DSK needs 4)...
+//     regs->WrMem(0x0D, 4);
+}
+
+#endif
+#if 0
+static bool inDelay = false;
+static bool inMLI = false;
+static uint16_t mliReturnAddr = 0;
+static uint8_t mliCmd = 0;
+if (regs->pc == 0x160B && dumpDis)
+{
+       inDelay = true;
+       dumpDis = false;
+       WriteLog("*** DELAY\n");
+}
+else if (regs->pc == 0x1616 && inDelay)
+{
+       inDelay = false;
+       dumpDis = true;
+}
+else if (regs->pc == 0xD385 && dumpDis)
+{
+       inDelay = true;
+       dumpDis = false;
+       WriteLog("*** DELAY\n");
+}
+else if (regs->pc == 0xD397 && inDelay)
+{
+       inDelay = false;
+       dumpDis = true;
+}
+else if (regs->pc == 0xBF00 && dumpDis)
+{
+       uint16_t lo = regs->RdMem(regs->sp + 0x101);
+       uint16_t hi = regs->RdMem(regs->sp + 0x102);
+       mliReturnAddr = ((hi << 8) | lo) + 1;
+       mliCmd = regs->RdMem(mliReturnAddr);
+       WriteLog("*** Calling ProDOS MLI with params: %02X %04X\n", mliCmd, RdMemW(mliReturnAddr + 1));
+       mliReturnAddr += 3;
+       inMLI = true;
+
+       // We want to see what's going on in the WRITE BLOCK command... :-P
+//     if (mliCmd != 0x81)
+//             dumpDis = false;
+}
+else if (regs->pc == mliReturnAddr && inMLI)
+{
+//extern bool stopWriting;
+//Stop writing to disk after the first block is done
+//     if (mliCmd == 0x81)
+//             stopWriting = true;
+
+       inMLI = false;
+       dumpDis = true;
+}
+else if (regs->pc == 0xAB3A && dumpDis && !inDelay)
+{
+       dumpDis = false;
+       inDelay = true;
+       WriteLog("\n*** DELAY (A=$%02X)\n\n", regs->a);
+}
+else if (regs->pc == 0xAB4A && inDelay)
+{
+       dumpDis = true;
+       inDelay = false;
+}
+
+if (regs->pc == 0xA80B)
+       dumpDis = true;
+
+#endif
+#if 0
+static bool weGo = false;
+static bool inDelay = false;
+if (regs->pc == 0x92BA)
+{
+       dumpDis = true;
+       weGo = true;
+}
+else if (regs->pc == 0xAB3A && weGo && !inDelay)
+{
+       dumpDis = false;
+       inDelay = true;
+       WriteLog("\n*** DELAY (A=$%02X)\n\n", regs->a);
+}
+else if (regs->pc == 0xAB4A && weGo)
+{
+       dumpDis = true;
+       inDelay = false;
+}
+else if (regs->pc == 0xA8B5 && weGo)
+{
+       WriteLog("\n$D4=%02X, $AC1F=%02X, $AC20=%02X\n\n", regs->RdMem(0xD4), regs->RdMem(0xAC1F), regs->RdMem(0xAC20));
+}
+/*else if (regs->pc == 0xA8C4 && weGo)
+{
+       WriteLog("Cheating... (clearing Carry flag)\n");
+       regs->cc &= ~FLAG_C;
+}*/
+#endif
+#if 0
+static bool weGo = false;
+if (regs->pc == 0x80AE)
+{
+       dumpDis = true;
+       weGo = true;
+}
+else if (regs->pc == 0xFCA8 && weGo)
+{
+       dumpDis = false;
+       WriteLog("\n*** DELAY (A=$%02X)\n\n", regs->a);
+}
+else if (regs->pc == 0xFCB3 && weGo)
+{
+       dumpDis = true;
+}
+#endif
 #if 0
-/*if (regs.pc == 0x4007)
+/*if (regs->pc == 0x4007)
 {
        dumpDis = true;
 }//*/
-if (regs.pc == 0x444B)
+if (regs->pc == 0x444B)
 {
        WriteLog("\n*** End of wait...\n\n");
        dumpDis = true;
 }//*/
-if (regs.pc == 0x444E)
+if (regs->pc == 0x444E)
 {
        WriteLog("\n*** Start of wait...\n\n");
        dumpDis = false;
 }//*/
 #endif
+/*if (regs->pc >= 0xC600 && regs->pc <=0xC6FF)
+{
+       dumpDis = true;
+}
+else
+       dumpDis = false;//*/
+/*if (regs->pc == 0xE039)
+{
+       dumpDis = true;
+}//*/
+
+#if 0
+/*if (regs->pc == 0x0801)
+{
+       WriteLog("\n*** DISK BOOT subroutine...\n\n");
+       dumpDis = true;
+}//*/
+if (regs->pc == 0xE000)
+{
+#if 0
+       WriteLog("\n*** Dump of $E000 routine ***\n\n");
 
+       for(uint32_t addr=0xE000; addr<0xF000;)
+       {
+               addr += Decode65C02(addr);
+               WriteLog("\n");
+       }
+#endif
+       WriteLog("\n*** DISK part II subroutine...\n\n");
+       dumpDis = true;
+}//*/
+if (regs->pc == 0xD000)
+{
+       WriteLog("\n*** CUSTOM DISK READ subroutine...\n\n");
+       dumpDis = false;
+}//*/
+if (regs->pc == 0xD1BE)
+{
+//     WriteLog("\n*** DISK part II subroutine...\n\n");
+       dumpDis = true;
+}//*/
+if (regs->pc == 0xD200)
+{
+       WriteLog("\n*** CUSTOM SCREEN subroutine...\n\n");
+       dumpDis = false;
+}//*/
+if (regs->pc == 0xD269)
+{
+//     WriteLog("\n*** DISK part II subroutine...\n\n");
+       dumpDis = true;
+}//*/
+#endif
+//if (regs->pc == 0xE08E)
+/*if (regs->pc == 0xAD33)
+{
+       WriteLog("\n*** After loader ***\n\n");
+       dumpDis = true;
+}//*/
+/*if (regs->pc == 0x0418)
+{
+       WriteLog("\n*** CUSTOM DISK READ subroutine...\n\n");
+       dumpDis = false;
+}
+if (regs->pc == 0x0)
+{
+       dumpDis = true;
+}//*/
 #ifdef __DEBUGMON__
 //WAIT is commented out here because it's called by BELL1...
-if (regs.pc == 0xFCA8)
+if (regs->pc == 0xFCA8)
 {
        WriteLog("\n*** WAIT subroutine...\n\n");
        dumpDis = false;
 }//*/
-if (regs.pc == 0xFBD9)
+if (regs->pc == 0xFBD9)
 {
        WriteLog("\n*** BELL1 subroutine...\n\n");
 //     dumpDis = false;
 }//*/
-if (regs.pc == 0xFC58)
+if (regs->pc == 0xFC58)
 {
        WriteLog("\n*** HOME subroutine...\n\n");
 //     dumpDis = false;
 }//*/
-if (regs.pc == 0xFDED)
+if (regs->pc == 0xFDED)
 {
        WriteLog("\n*** COUT subroutine...\n\n");
        dumpDis = false;
@@ -2302,117 +2827,178 @@ if (regs.pc == 0xFDED)
 #endif
 #if 0
 // ProDOS debugging
-if (regs.pc == 0x2000)
+if (regs->pc == 0x2000)
        dumpDis = true;
 #endif
 
 #ifdef __DEBUG__
+#ifdef DO_BACKTRACE
+//uint32_t btQueuePtr = 0;
+//V65C02REGS btQueue[BACKTRACE_SIZE];
+//uint8_t btQueueInst[BACKTRACE_SIZE][4];
+memcpy(&btQueue[btQueuePtr], regs, sizeof(V65C02REGS));
+btQueuePtr = (btQueuePtr + 1) % BACKTRACE_SIZE;
+#endif
+#endif
+#ifdef __DEBUG__
+static uint16_t spc, ppc = 0;
+static bool seenHi = false;
+static uint64_t oldClock = 0;
+spc = regs->pc;
+static char disbuf[80];
 if (dumpDis)
-       Decode65C02(regs.pc);
+{
+       Decode65C02(regs, disbuf, regs->pc);
+       WriteLog("%s", disbuf);
+}
 #endif
-               uint8 opcode = regs.RdMem(regs.pc++);
+               uint8_t opcode = regs->RdMem(regs->pc++);
 
-//if (!(regs.cpuFlags & V65C02_STATE_ILLEGAL_INST))
+#if 0
+if (opcode == 0)
+//if (regs->pc == 0xA255)
+//static bool seenBT = false;
+//if (hitGo && !seenBT)
+//if (dobacktrace)
+{
+//seenBT = true;
+       static char disbuf[80];
+//     uint32_t btStart = btQueuePtr - 12 + (btQueuePtr < 12 ? BACKTRACE_SIZE : 0);
+       uint32_t btStart = 0;
+
+       for(uint32_t i=btStart; i<btQueuePtr; i++)
+       {
+               Decode65C02(regs, disbuf, btQueue[i].pc);
+               WriteLog("%s\n", disbuf);
+       }
+}
+#endif
+//if (!(regs->cpuFlags & V65C02_STATE_ILLEGAL_INST))
 //instCount[opcode]++;
 
-               exec_op[opcode]();                                                              // Execute that opcode...
-               regs.clock += CPUCycles[opcode];
+               if (regs->Timer)
+                       regs->Timer(CPUCycles[opcode]);
+
+               uint64_t clockSave = regs->clock + CPUCycles[opcode];
+
+               // We need this because the opcode function could add 1 or 2 cycles
+               // to regs->clock which aren't accounted for in CPUCycles[].
+//             uint64_t clockSave = regs->clock;
+
+               // Execute that opcode...
+               exec_op[opcode]();
+               regs->clock += CPUCycles[opcode];
+
+               // Tell the timer function (if any) how many PHI2s have elapsed...
+//             if (regs->Timer)
+               if (regs->Timer && (regs->clock - clockSave) > 0)
+                       regs->Timer(regs->clock - clockSave);
+
 #ifdef __DEBUG__
 if (dumpDis)
-       WriteLog(" [PC=%04X, SP=%04X, CC=%s%s.%s%s%s%s%s, A=%02X, X=%02X, Y=%02X]\n",
-               regs.pc, 0x0100 + regs.sp,
-               (regs.cc & FLAG_N ? "N" : "-"), (regs.cc & FLAG_V ? "V" : "-"),
-               (regs.cc & FLAG_B ? "B" : "-"), (regs.cc & FLAG_D ? "D" : "-"),
-               (regs.cc & FLAG_I ? "I" : "-"), (regs.cc & FLAG_Z ? "Z" : "-"),
-               (regs.cc & FLAG_C ? "C" : "-"), regs.a, regs.x, regs.y);
+{
+       WriteLog(" [SP=01%02X, CC=%s%s.%s%s%s%s%s, A=%02X, X=%02X, Y=%02X](%d)[%02X]\n",//<%s>\n",
+               regs->sp,
+               (regs->cc & FLAG_N ? "N" : "-"), (regs->cc & FLAG_V ? "V" : "-"),
+               (regs->cc & FLAG_B ? "B" : "-"), (regs->cc & FLAG_D ? "D" : "-"),
+               (regs->cc & FLAG_I ? "I" : "-"), (regs->cc & FLAG_Z ? "Z" : "-"),
+               (regs->cc & FLAG_C ? "C" : "-"), regs->a, regs->x, regs->y, regs->clock - clockSave + CPUCycles[opcode], floppyDrive[0].dataRegister);//, sequence);
+       sequence[0] = 0;
+
+       if (((spc == 0xD4D1) || (spc == 0xD4E2)) && (floppyDrive[0].dataRegister & 0x80))
+               seenHi = true;
+
+       if ((spc == 0xD4CE) || (spc == 0xD4DF))
+       {
+               WriteLog(" (%d)\n", regs->clock - oldClock);
+
+               if ((regs->y & 0x80) == 0 && seenHi && ((ppc == 0xD4D1) || (ppc == 0xD4E2)))
+                       WriteLog("\n***** MISS! *****\n\n");
+
+               seenHi = false;
+               oldClock = regs->clock;
+       }
+
+       ppc = spc;
+}
 #endif
 
 #ifdef __DEBUGMON__
-if (regs.pc == 0xFCB3) // WAIT exit point
+if (regs->pc == 0xFCB3)        // WAIT exit point
 {
        dumpDis = true;
 }//*/
-/*if (regs.pc == 0xFBEF)       // BELL1 exit point
+/*if (regs->pc == 0xFBEF)      // BELL1 exit point
 {
        dumpDis = true;
 }//*/
-/*if (regs.pc == 0xFC22)       // HOME exit point
+/*if (regs->pc == 0xFC22)      // HOME exit point
 {
        dumpDis = true;
 }//*/
-if (regs.pc == 0xFDFF) // COUT exit point
+if (regs->pc == 0xFDFF)        // COUT exit point
 {
        dumpDis = true;
 }
-if (regs.pc == 0xFBD8)
+if (regs->pc == 0xFBD8)
 {
        WriteLog("\n*** BASCALC set BASL/H = $%04X\n\n", RdMemW(0x0028));
 }//*/
 #endif
 
 //These should be correct now...
-               if (regs.cpuFlags & V65C02_ASSERT_LINE_RESET)
+               if (regs->cpuFlags & V65C02_ASSERT_LINE_RESET)
                {
-#ifdef __DEBUG__
-WriteLog("\n*** RESET ***\n\n");
-#endif
                        // Not sure about this...
-                       regs.sp = 0xFF;
-                       regs.cc = FLAG_B | FLAG_I;                                      // Reset the CC register
-                       regs.pc = RdMemW(0xFFFC);                                       // And load PC with the RESET vector
+                       regs->sp = 0xFF;
+                       regs->cc = FLAG_I;                              // Reset the CC register
+                       regs->pc = RdMemW(0xFFFC);              // And load PC with RESET vector
 
-                       context->cpuFlags &= ~V65C02_ASSERT_LINE_RESET;
-                       regs.cpuFlags &= ~V65C02_ASSERT_LINE_RESET;
+                       regs->cpuFlags = 0;                             // Clear CPU flags...
+#ifdef __DEBUG__
+WriteLog("\n*** RESET *** (PC = $%04X)\n\n", regs->pc);
+#endif
                }
-               else if (regs.cpuFlags & V65C02_ASSERT_LINE_NMI)
+               else if (regs->cpuFlags & V65C02_ASSERT_LINE_NMI)
                {
 #ifdef __DEBUG__
 WriteLog("\n*** NMI ***\n\n");
 #endif
-                       regs.WrMem(0x0100 + regs.sp--, regs.pc >> 8);   // Save PC and CC
-                       regs.WrMem(0x0100 + regs.sp--, regs.pc & 0xFF);
-                       regs.WrMem(0x0100 + regs.sp--, regs.cc);
-                       regs.cc |= FLAG_I;                                                      // Set I
-                       regs.cc &= ~FLAG_D;                                                     // & clear D
-                       regs.pc = RdMemW(0xFFFA);                                       // And do it!
-
-                       regs.clock += 7;
-                       context->cpuFlags &= ~V65C02_ASSERT_LINE_NMI;// Reset the asserted line (NMI)...
-                       regs.cpuFlags &= ~V65C02_ASSERT_LINE_NMI;       // Reset the asserted line (NMI)...
+                       regs->WrMem(0x0100 + regs->sp--, regs->pc >> 8);        // Save PC & CC
+                       regs->WrMem(0x0100 + regs->sp--, regs->pc & 0xFF);
+                       regs->WrMem(0x0100 + regs->sp--, regs->cc);
+                       SET_I;
+                       CLR_D;
+                       regs->pc = RdMemW(0xFFFA);              // Jump to NMI vector
+
+                       regs->clock += 7;
+                       regs->cpuFlags &= ~V65C02_ASSERT_LINE_NMI;      // Reset NMI line
                }
-               else if (regs.cpuFlags & V65C02_ASSERT_LINE_IRQ)
+               else if ((regs->cpuFlags & V65C02_ASSERT_LINE_IRQ)
+                       // IRQs are maskable, so check if the I flag is clear
+                       && (!(regs->cc & FLAG_I)))
                {
-                       if (!(regs.cc & FLAG_I))                                        // Process an interrupt (I=0)?
-                       {
 #ifdef __DEBUG__
 WriteLog("\n*** IRQ ***\n\n");
+WriteLog("Clock=$%X\n", regs->clock);
+//dumpDis = true;
 #endif
-                               regs.WrMem(0x0100 + regs.sp--, regs.pc >> 8);   // Save PC and CC
-                               regs.WrMem(0x0100 + regs.sp--, regs.pc & 0xFF);
-                               regs.WrMem(0x0100 + regs.sp--, regs.cc);
-                               regs.cc |= FLAG_I;                                              // Set I
-                               regs.cc &= ~FLAG_D;                                             // & clear D
-                               regs.pc = RdMemW(0xFFFE);                               // And do it!
-
-                               regs.clock += 7;
-                               context->cpuFlags &= ~V65C02_ASSERT_LINE_IRQ;   // Reset the asserted line (IRQ)...
-                               regs.cpuFlags &= ~V65C02_ASSERT_LINE_IRQ;       // Reset the asserted line (IRQ)...
-                       }
+                       regs->WrMem(0x0100 + regs->sp--, regs->pc >> 8);        // Save PC & CC
+                       regs->WrMem(0x0100 + regs->sp--, regs->pc & 0xFF);
+                       regs->WrMem(0x0100 + regs->sp--, regs->cc);
+                       SET_I;
+                       CLR_D;
+                       regs->pc = RdMemW(0xFFFE);              // Jump to IRQ vector
+
+                       regs->clock += 7;
+                       regs->cpuFlags &= ~V65C02_ASSERT_LINE_IRQ;      // Reset IRQ line
                }
        }
 
-//This is a lame way of doing it, but in the end the simplest--however, it destroys any
-//record of elasped CPU time. Not sure that it's important to keep track, but there it is.
-// Now we use a 64-bit integer, so it won't wrap for about 500 millenia. ;-)
-//     regs.clock -= cycles;
-
-       myMemcpy(context, &regs, sizeof(V65C02REGS));
-}
-
-//
-// Get the clock of the currently executing CPU
-//
-uint64 GetCurrentV65C02Clock(void)
-{
-       return regs.clock;
+       // If we went longer than the passed in cycles, make a note of it so we can
+       // subtract it out from a subsequent run.  It's guaranteed to be non-
+       // negative, because the condition that exits the main loop above is
+       // written such that regs->clock has to be equal or larger than endCycles
+       // to exit from it.
+       regs->overflow = regs->clock - endCycles;
 }