From 7b89ebcecb428e1a036c08b5c7f04f3fb5ecac1e Mon Sep 17 00:00:00 2001 From: cuz Date: Sun, 31 Mar 2002 20:46:53 +0000 Subject: [PATCH] Working git-svn-id: svn://svn.cc65.org/cc65/trunk@1211 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/sim65/cpucore.c | 1685 +++++++++++++------------------------------ src/sim65/memory.c | 16 +- src/sim65/memory.h | 10 +- 3 files changed, 513 insertions(+), 1198 deletions(-) diff --git a/src/sim65/cpucore.c b/src/sim65/cpucore.c index c88269b7d..b469b95c9 100644 --- a/src/sim65/cpucore.c +++ b/src/sim65/cpucore.c @@ -37,6 +37,7 @@ /* common */ #include "abend.h" +#include "attrib.h" #include "print.h" /* sim65 */ @@ -88,7 +89,7 @@ int CPUHalted; -/* Return the flags as a boolean value */ +/* Return the flags as a boolean value (0/1) */ #define GET_CF() ((SR & CF) != 0) #define GET_ZF() ((SR & ZF) != 0) #define GET_IF() ((SR & IF) != 0) @@ -111,17 +112,17 @@ int CPUHalted; /* Special test and set macros. The meaning of the parameter depends on the * actual flag that should be set or reset. */ -#define TEST_ZF(v) SET_ZF (((v) & 0xFF) == 0) -#define TEST_SF(v) SET_SF (((v) & 0x80) != 0) -#define TEST_CF(v) SET_CF (((v) & 0xFF00) != 0) +#define TEST_ZF(v) SET_ZF (((v) & 0xFF) == 0) +#define TEST_SF(v) SET_SF (((v) & 0x80) != 0) +#define TEST_CF(v) SET_CF (((v) & 0xFF00) != 0) /* Program counter halves */ #define PCL (PC & 0xFF) #define PCH ((PC >> 8) & 0xFF) /* Stack operations */ -#define PUSH(Val) WriteMem (StackPage + SP--, Val) -#define POP() ReadMem (StackPage + ++SP) +#define PUSH(Val) MemWriteByte (StackPage + SP--, Val) +#define POP() MemReadByte (StackPage + ++SP) /* Test for page cross */ #define PAGE_CROSS(addr,offs) ((((addr) & 0xFF) + offs) >= 0x100) @@ -129,7 +130,7 @@ int CPUHalted; /* #imm */ #define AC_OP_IMM(op) \ Cycles = 2; \ - AC = AC op ReadMem (PC+1); \ + AC = AC op MemReadByte (PC+1); \ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 2 @@ -137,7 +138,7 @@ int CPUHalted; /* zp */ #define AC_OP_ZP(op) \ Cycles = 3; \ - AC = AC op ReadMem (ReadMem (PC+1)); \ + AC = AC op MemReadByte (MemReadByte (PC+1));\ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 2 @@ -146,8 +147,8 @@ int CPUHalted; #define AC_OP_ZPX(op) \ unsigned char ZPAddr; \ Cycles = 4; \ - ZPAddr = ReadMem (PC+1) + XR; \ - AC = AC op ReadMem (ZPAddr); \ + ZPAddr = MemReadByte (PC+1) + XR; \ + AC = AC op MemReadByte (ZPAddr); \ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 2 @@ -156,8 +157,8 @@ int CPUHalted; #define AC_OP_ZPY(op) \ unsigned char ZPAddr; \ Cycles = 4; \ - ZPAddr = ReadMem (PC+1) + YR; \ - AC = AC op ReadMem (ZPAddr); \ + ZPAddr = MemReadByte (PC+1) + YR; \ + AC = AC op MemReadByte (ZPAddr); \ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 2 @@ -166,8 +167,8 @@ int CPUHalted; #define AC_OP_ABS(op) \ unsigned Addr; \ Cycles = 4; \ - Addr = ReadMemW (PC+1); \ - AC = AC op ReadMem (Addr); \ + Addr = MemReadWord (PC+1); \ + AC = AC op MemReadByte (Addr); \ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 3 @@ -176,11 +177,11 @@ int CPUHalted; #define AC_OP_ABSX(op) \ unsigned Addr; \ Cycles = 4; \ - Addr = ReadMemW (PC+1); \ + Addr = MemReadWord (PC+1); \ if (PAGE_CROSS (Addr, XR)) { \ ++Cycles; \ } \ - AC = AC | ReadMem (Addr + XR); \ + AC = AC | MemReadByte (Addr + XR); \ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 3 @@ -189,11 +190,11 @@ int CPUHalted; #define AC_OP_ABSY(op) \ unsigned Addr; \ Cycles = 4; \ - Addr = ReadMemW (PC+1); \ + Addr = MemReadWord (PC+1); \ if (PAGE_CROSS (Addr, YR)) { \ ++Cycles; \ } \ - AC = AC | ReadMem (Addr + YR); \ + AC = AC | MemReadByte (Addr + YR); \ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 3 @@ -203,9 +204,9 @@ int CPUHalted; unsigned char ZPAddr; \ unsigned Addr; \ Cycles = 6; \ - ZPAddr = ReadMem (PC+1) + XR; \ - Addr = ReadZeroPageW (ZPAddr); \ - AC = AC op ReadMem (Addr); \ + ZPAddr = MemReadByte (PC+1) + XR; \ + Addr = MemReadZPWord (ZPAddr); \ + AC = AC op MemReadByte (Addr); \ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 2 @@ -215,13 +216,29 @@ int CPUHalted; unsigned char ZPAddr; \ unsigned Addr; \ Cycles = 5; \ - ZPAddr = ReadMem (PC+1); \ - Addr = ReadZeroPageW (ZPAddr) + YR; \ - AC = AC op ReadMem (Addr); \ + ZPAddr = MemReadByte (PC+1); \ + Addr = MemReadZPWord (ZPAddr) + YR; \ + AC = AC op MemReadByte (Addr); \ TEST_ZF (AC); \ TEST_SF (AC); \ PC += 2 +/* ADC */ +#define ADC(v) \ + if (GET_DF ()) { \ + NotImplemented (); \ + } else { \ + unsigned Val; \ + unsigned char rhs = v; \ + Val = AC + rhs + GET_CF (); \ + AC = (unsigned char) Val; \ + TEST_ZF (AC); \ + TEST_SF (AC); \ + TEST_CF (Val); \ + SET_OF (!((AC ^ rhs) & 0x80) && \ + ((AC ^ Val) & 0x80)); \ + } + /* branches */ #define BRANCH(cond) \ Cycles = 2; \ @@ -229,9 +246,9 @@ int CPUHalted; signed char Offs; \ unsigned char OldPCH; \ ++Cycles; \ - Offs = (signed char) ReadMem (PC+1); \ + Offs = (signed char) MemReadByte (PC+1);\ OldPCH = PCH; \ - PC += (int) Offs; \ + PC += 2 + (int) Offs; \ if (PCH != OldPCH) { \ ++Cycles; \ } \ @@ -249,6 +266,42 @@ int CPUHalted; } +/* ROL */ +#define ROL(Val) \ + Val <<= 1; \ + if (GET_CF ()) { \ + Val |= 0x01; \ + } \ + TEST_ZF (Val); \ + TEST_SF (Val); \ + TEST_CF (Val) + +/* ROR */ +#define ROR(Val) \ + if (GET_CF ()) { \ + Val |= 0x100; \ + } \ + SET_CF (Val & 0x01); \ + Val >>= 1; \ + TEST_ZF (Val); \ + TEST_SF (Val) + +/* SBC */ +#define SBC(v) \ + if (GET_DF ()) { \ + NotImplemented (); \ + } else { \ + unsigned Val; \ + unsigned char rhs = v; \ + Val = AC - rhs - (!GET_CF ()); \ + AC = (unsigned char) Val; \ + TEST_ZF (AC); \ + TEST_SF (AC); \ + SET_CF (Val <= 0xFF); \ + SET_OF (((AC^rhs) & (AC^Val) & 0x80)); \ + } + + /*****************************************************************************/ /* Helper functions */ @@ -256,17 +309,19 @@ int CPUHalted; -static void Illegal (void) +static void OPC_Illegal (void) attribute ((noreturn)); +static void OPC_Illegal (void) { - fprintf (stderr, "Illegal: $%02X\n", ReadMem (PC)); + fprintf (stderr, "Illegal: $%02X\n", MemReadByte (PC)); exit (EXIT_FAILURE); } +static void NotImplemented (void) attribute ((noreturn)); static void NotImplemented (void) { - fprintf (stderr, "Not implemented: $%02X\n", ReadMem (PC)); + fprintf (stderr, "Not implemented: $%02X\n", MemReadByte (PC)); exit (EXIT_FAILURE); } @@ -288,7 +343,7 @@ static void OPC_6502_00 (void) PUSH (PCL); PUSH (SR); SET_IF (1); - PC = ReadMemW (0xFFFE); + PC = MemReadWord (0xFFFE); } @@ -301,30 +356,6 @@ static void OPC_6502_01 (void) -static void OPC_6502_02 (void) -/* Opcode $02 */ -{ - Illegal (); -} - - - -static void OPC_6502_03 (void) -/* Opcode $03 */ -{ - Illegal (); -} - - - -static void OPC_6502_04 (void) -/* Opcode $04 */ -{ - Illegal (); -} - - - static void OPC_6502_05 (void) /* Opcode $05: ORA zp */ { @@ -339,9 +370,9 @@ static void OPC_6502_06 (void) unsigned char ZPAddr; unsigned Val; Cycles = 5; - ZPAddr = ReadMem (PC+1); - Val = ReadMem (ZPAddr) << 1; - WriteMem (ZPAddr, (unsigned char) Val); + ZPAddr = MemReadByte (PC+1); + Val = MemReadByte (ZPAddr) << 1; + MemWriteByte (ZPAddr, (unsigned char) Val); TEST_ZF (Val & 0xFF); TEST_SF (Val); SET_CF (Val & 0x100); @@ -350,14 +381,6 @@ static void OPC_6502_06 (void) -static void OPC_6502_07 (void) -/* Opcode $07 */ -{ - Illegal (); -} - - - static void OPC_6502_08 (void) /* Opcode $08: PHP */ { @@ -391,22 +414,6 @@ static void OPC_6502_0A (void) -static void OPC_6502_0B (void) -/* Opcode $0B */ -{ - Illegal (); -} - - - -static void OPC_6502_0C (void) -/* Opcode $0C */ -{ - Illegal (); -} - - - static void OPC_6502_0D (void) /* Opcode $0D: ORA abs */ { @@ -421,9 +428,9 @@ static void OPC_6502_0E (void) unsigned Addr; unsigned Val; Cycles = 6; - Addr = ReadMemW (PC+1); - Val = ReadMem (Addr) << 1; - WriteMem (Addr, (unsigned char) Val); + Addr = MemReadWord (PC+1); + Val = MemReadByte (Addr) << 1; + MemWriteByte (Addr, (unsigned char) Val); TEST_ZF (Val & 0xFF); TEST_SF (Val); SET_CF (Val & 0x100); @@ -432,14 +439,6 @@ static void OPC_6502_0E (void) -static void OPC_6502_0F (void) -/* Opcode $0F */ -{ - Illegal (); -} - - - static void OPC_6502_10 (void) /* Opcode $10: BPL */ { @@ -456,30 +455,6 @@ static void OPC_6502_11 (void) -static void OPC_6502_12 (void) -/* Opcode $12 */ -{ - Illegal (); -} - - - -static void OPC_6502_13 (void) -/* Opcode $13 */ -{ - Illegal (); -} - - - -static void OPC_6502_14 (void) -/* Opcode $14 */ -{ - Illegal (); -} - - - static void OPC_6502_15 (void) /* Opcode $15: ORA zp,x */ { @@ -494,9 +469,9 @@ static void OPC_6502_16 (void) unsigned char ZPAddr; unsigned Val; Cycles = 6; - ZPAddr = ReadMem (PC+1) + XR; - Val = ReadMem (ZPAddr) << 1; - WriteMem (ZPAddr, (unsigned char) Val); + ZPAddr = MemReadByte (PC+1) + XR; + Val = MemReadByte (ZPAddr) << 1; + MemWriteByte (ZPAddr, (unsigned char) Val); TEST_ZF (Val & 0xFF); TEST_SF (Val); SET_CF (Val & 0x100); @@ -505,14 +480,6 @@ static void OPC_6502_16 (void) -static void OPC_6502_17 (void) -/* Opcode $17 */ -{ - Illegal (); -} - - - static void OPC_6502_18 (void) /* Opcode $18: CLC */ { @@ -531,30 +498,6 @@ static void OPC_6502_19 (void) -static void OPC_6502_1A (void) -/* Opcode $1A */ -{ - Illegal (); -} - - - -static void OPC_6502_1B (void) -/* Opcode $1B */ -{ - Illegal (); -} - - - -static void OPC_6502_1C (void) -/* Opcode $1C */ -{ - Illegal (); -} - - - static void OPC_6502_1D (void) /* Opcode $1D: ORA abs,x */ { @@ -569,9 +512,9 @@ static void OPC_6502_1E (void) unsigned Addr; unsigned Val; Cycles = 7; - Addr = ReadMemW (PC+1) + XR; - Val = ReadMem (Addr) << 1; - WriteMem (Addr, (unsigned char) Val); + Addr = MemReadWord (PC+1) + XR; + Val = MemReadByte (Addr) << 1; + MemWriteByte (Addr, (unsigned char) Val); TEST_ZF (Val & 0xFF); TEST_SF (Val); SET_CF (Val & 0x100); @@ -580,20 +523,12 @@ static void OPC_6502_1E (void) -static void OPC_6502_1F (void) -/* Opcode $1F */ -{ - Illegal (); -} - - - static void OPC_6502_20 (void) /* Opcode $20: JSR */ { unsigned Addr; Cycles = 6; - Addr = ReadMemW (PC+1); + Addr = MemReadWord (PC+1); PC += 2; PUSH (PCH); PUSH (PCL); @@ -610,30 +545,14 @@ static void OPC_6502_21 (void) -static void OPC_6502_22 (void) -/* Opcode $22 */ -{ - Illegal (); -} - - - -static void OPC_6502_23 (void) -/* Opcode $23 */ -{ - Illegal (); -} - - - static void OPC_6502_24 (void) /* Opcode $24: BIT zp */ { unsigned char ZPAddr; unsigned char Val; Cycles = 3; - ZPAddr = ReadMem (PC+1); - Val = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1); + Val = MemReadByte (ZPAddr); SET_SF (Val & 0x80); SET_OF (Val & 0x40); SET_ZF ((Val & AC) == 0); @@ -656,28 +575,15 @@ static void OPC_6502_26 (void) unsigned char ZPAddr; unsigned Val; Cycles = 5; - ZPAddr = ReadMem (PC+1); - Val = ReadMem (ZPAddr) << 1; - if (GET_CF ()) { - Val |= 0x01; - } - AC = (unsigned char) Val; - TEST_ZF (AC); - TEST_SF (AC); - TEST_CF (Val); + ZPAddr = MemReadByte (PC+1); + Val = MemReadByte (ZPAddr); + ROL (Val); + MemWriteByte (ZPAddr, Val); PC += 2; } -static void OPC_6502_27 (void) -/* Opcode $27 */ -{ - Illegal (); -} - - - static void OPC_6502_28 (void) /* Opcode $28: PLP */ { @@ -701,35 +607,22 @@ static void OPC_6502_2A (void) { unsigned Val; Cycles = 2; - Val = AC << 1; - if (GET_CF ()) { - Val |= 0x01; - } + Val = AC; + ROL (Val); AC = (unsigned char) Val; - TEST_ZF (AC); - TEST_SF (AC); - TEST_CF (Val); PC += 1; } -static void OPC_6502_2B (void) -/* Opcode $2B */ -{ - Illegal (); -} - - - static void OPC_6502_2C (void) /* Opcode $2C: BIT abs */ { unsigned Addr; unsigned char Val; Cycles = 4; - Addr = ReadMem (PC+1); - Val = ReadMem (Addr); + Addr = MemReadByte (PC+1); + Val = MemReadByte (Addr); SET_SF (Val & 0x80); SET_OF (Val & 0x40); SET_ZF ((Val & AC) == 0); @@ -752,28 +645,15 @@ static void OPC_6502_2E (void) unsigned Addr; unsigned Val; Cycles = 6; - Addr = ReadMemW (PC+1); - Val = ReadMem (Addr) << 1; - if (GET_CF ()) { - Val |= 0x01; - } - AC = (unsigned char) Val; - TEST_ZF (AC); - TEST_SF (AC); - TEST_CF (Val); + Addr = MemReadWord (PC+1); + Val = MemReadByte (Addr); + ROL (Val); + MemWriteByte (Addr, Val); PC += 3; } -static void OPC_6502_2F (void) -/* Opcode $2F */ -{ - Illegal (); -} - - - static void OPC_6502_30 (void) /* Opcode $30: BMI */ { @@ -790,30 +670,6 @@ static void OPC_6502_31 (void) -static void OPC_6502_32 (void) -/* Opcode $32 */ -{ - Illegal (); -} - - - -static void OPC_6502_33 (void) -/* Opcode $33 */ -{ - Illegal (); -} - - - -static void OPC_6502_34 (void) -/* Opcode $34 */ -{ - Illegal (); -} - - - static void OPC_6502_35 (void) /* Opcode $35: AND zp,x */ { @@ -828,28 +684,15 @@ static void OPC_6502_36 (void) unsigned char ZPAddr; unsigned Val; Cycles = 6; - ZPAddr = ReadMem (PC+1) + XR; - Val = ReadMem (ZPAddr) << 1; - if (GET_CF ()) { - Val |= 0x01; - } - AC = (unsigned char) Val; - TEST_ZF (AC); - TEST_SF (AC); - TEST_CF (Val); + ZPAddr = MemReadByte (PC+1) + XR; + Val = MemReadByte (ZPAddr); + ROL (Val); + MemWriteByte (ZPAddr, Val); PC += 2; } -static void OPC_6502_37 (void) -/* Opcode $37 */ -{ - Illegal (); -} - - - static void OPC_6502_38 (void) /* Opcode $38: SEC */ { @@ -868,30 +711,6 @@ static void OPC_6502_39 (void) -static void OPC_6502_3A (void) -/* Opcode $3A */ -{ - Illegal (); -} - - - -static void OPC_6502_3B (void) -/* Opcode $3B */ -{ - Illegal (); -} - - - -static void OPC_6502_3C (void) -/* Opcode $3C */ -{ - Illegal (); -} - - - static void OPC_6502_3D (void) /* Opcode $3D: AND abs,x */ { @@ -906,28 +725,15 @@ static void OPC_6502_3E (void) unsigned Addr; unsigned Val; Cycles = 7; - Addr = ReadMemW (PC+1) + XR; - Val = ReadMem (Addr) << 1; - if (GET_CF ()) { - Val |= 0x01; - } - AC = (unsigned char) Val; - TEST_ZF (AC); - TEST_SF (AC); - TEST_CF (Val); + Addr = MemReadWord (PC+1) + XR; + Val = MemReadByte (Addr); + ROL (Val); + MemWriteByte (Addr, Val); PC += 2; } -static void OPC_6502_3F (void) -/* Opcode $3F */ -{ - Illegal (); -} - - - static void OPC_6502_40 (void) /* Opcode $40: RTI */ { @@ -947,30 +753,6 @@ static void OPC_6502_41 (void) -static void OPC_6502_42 (void) -/* Opcode $42 */ -{ - Illegal (); -} - - - -static void OPC_6502_43 (void) -/* Opcode $43 */ -{ - Illegal (); -} - - - -static void OPC_6502_44 (void) -/* Opcode $44 */ -{ - Illegal (); -} - - - static void OPC_6502_45 (void) /* Opcode $45: EOR zp */ { @@ -985,11 +767,11 @@ static void OPC_6502_46 (void) unsigned char ZPAddr; unsigned char Val; Cycles = 5; - ZPAddr = ReadMem (PC+1); - Val = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1); + Val = MemReadByte (ZPAddr); SET_CF (Val & 0x01); Val >>= 1; - WriteMem (ZPAddr, Val); + MemWriteByte (ZPAddr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 2; @@ -997,14 +779,6 @@ static void OPC_6502_46 (void) -static void OPC_6502_47 (void) -/* Opcode $47 */ -{ - Illegal (); -} - - - static void OPC_6502_48 (void) /* Opcode $48: PHA */ { @@ -1036,19 +810,11 @@ static void OPC_6502_4A (void) -static void OPC_6502_4B (void) -/* Opcode $4B */ -{ - Illegal (); -} - - - static void OPC_6502_4C (void) /* Opcode $4C: JMP abs */ { Cycles = 3; - PC = ReadMemW (PC+1); + PC = MemReadWord (PC+1); } @@ -1067,11 +833,11 @@ static void OPC_6502_4E (void) unsigned Addr; unsigned char Val; Cycles = 6; - Addr = ReadMemW (PC+1); - Val = ReadMem (Addr); + Addr = MemReadWord (PC+1); + Val = MemReadByte (Addr); SET_CF (Val & 0x01); Val >>= 1; - WriteMem (Addr, Val); + MemWriteByte (Addr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 3; @@ -1079,14 +845,6 @@ static void OPC_6502_4E (void) -static void OPC_6502_4F (void) -/* Opcode $4F */ -{ - Illegal (); -} - - - static void OPC_6502_50 (void) /* Opcode $50: BVC */ { @@ -1103,30 +861,6 @@ static void OPC_6502_51 (void) -static void OPC_6502_52 (void) -/* Opcode $52 */ -{ - Illegal (); -} - - - -static void OPC_6502_53 (void) -/* Opcode $53 */ -{ - Illegal (); -} - - - -static void OPC_6502_54 (void) -/* Opcode $54 */ -{ - Illegal (); -} - - - static void OPC_6502_55 (void) /* Opcode $55: EOR zp,x */ { @@ -1141,11 +875,11 @@ static void OPC_6502_56 (void) unsigned char ZPAddr; unsigned char Val; Cycles = 6; - ZPAddr = ReadMem (PC+1) + XR; - Val = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1) + XR; + Val = MemReadByte (ZPAddr); SET_CF (Val & 0x01); Val >>= 1; - WriteMem (ZPAddr, Val); + MemWriteByte (ZPAddr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 2; @@ -1153,14 +887,6 @@ static void OPC_6502_56 (void) -static void OPC_6502_57 (void) -/* Opcode $57 */ -{ - Illegal (); -} - - - static void OPC_6502_58 (void) /* Opcode $58: CLI */ { @@ -1179,30 +905,6 @@ static void OPC_6502_59 (void) -static void OPC_6502_5A (void) -/* Opcode $5A */ -{ - Illegal (); -} - - - -static void OPC_6502_5B (void) -/* Opcode $5B */ -{ - Illegal (); -} - - - -static void OPC_6502_5C (void) -/* Opcode $5C */ -{ - Illegal (); -} - - - static void OPC_6502_5D (void) /* Opcode $5D: EOR abs,x */ { @@ -1217,11 +919,11 @@ static void OPC_6502_5E (void) unsigned Addr; unsigned char Val; Cycles = 7; - Addr = ReadMemW (PC+1) + XR; - Val = ReadMem (Addr); + Addr = MemReadWord (PC+1) + XR; + Val = MemReadByte (Addr); SET_CF (Val & 0x01); Val >>= 1; - WriteMem (Addr, Val); + MemWriteByte (Addr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 3; @@ -1229,14 +931,6 @@ static void OPC_6502_5E (void) -static void OPC_6502_5F (void) -/* Opcode $5F */ -{ - Illegal (); -} - - - static void OPC_6502_60 (void) /* Opcode $60: RTS */ { @@ -1251,31 +945,13 @@ static void OPC_6502_60 (void) static void OPC_6502_61 (void) /* Opcode $61: ADC (zp,x) */ { - NotImplemented (); -} - - - -static void OPC_6502_62 (void) -/* Opcode $62 */ -{ - Illegal (); -} - - - -static void OPC_6502_63 (void) -/* Opcode $63 */ -{ - Illegal (); -} - - - -static void OPC_6502_64 (void) -/* Opcode $64 */ -{ - Illegal (); + unsigned char ZPAddr; + unsigned Addr; + Cycles = 6; + ZPAddr = MemReadByte (PC+1) + XR; + Addr = MemReadZPWord (ZPAddr); + ADC (MemReadByte (Addr)); + PC += 2; } @@ -1283,7 +959,11 @@ static void OPC_6502_64 (void) static void OPC_6502_65 (void) /* Opcode $65: ADC zp */ { - NotImplemented (); + unsigned char ZPAddr; + Cycles = 3; + ZPAddr = MemReadByte (PC+1); + ADC (MemReadByte (ZPAddr)); + PC += 2; } @@ -1291,15 +971,14 @@ static void OPC_6502_65 (void) static void OPC_6502_66 (void) /* Opcode $66: ROR zp */ { - NotImplemented (); -} - - - -static void OPC_6502_67 (void) -/* Opcode $67 */ -{ - Illegal (); + unsigned char ZPAddr; + unsigned Val; + Cycles = 5; + ZPAddr = MemReadByte (PC+1); + Val = MemReadByte (ZPAddr); + ROR (Val); + MemWriteByte (ZPAddr, Val); + PC += 2; } @@ -1319,7 +998,9 @@ static void OPC_6502_68 (void) static void OPC_6502_69 (void) /* Opcode $69: ADC #imm */ { - NotImplemented (); + Cycles = 2; + ADC (MemReadByte (PC+1)); + PC += 2; } @@ -1327,15 +1008,12 @@ static void OPC_6502_69 (void) static void OPC_6502_6A (void) /* Opcode $6A: ROR a */ { - NotImplemented (); -} - - - -static void OPC_6502_6B (void) -/* Opcode $6B */ -{ - Illegal (); + unsigned Val; + Cycles = 2; + Val = AC; + ROR (Val); + AC = (unsigned char) Val; + PC += 1; } @@ -1345,15 +1023,15 @@ static void OPC_6502_6C (void) { unsigned Addr; Cycles = 5; - Addr = ReadMemW (PC+1); + Addr = MemReadWord (PC+1); if (CPU == CPU_6502) { /* Emulate the 6502 bug */ - PC = ReadMem (Addr); + PC = MemReadByte (Addr); Addr = (Addr & 0xFF00) | ((Addr + 1) & 0xFF); - PC |= (ReadMem (Addr) << 8); + PC |= (MemReadByte (Addr) << 8); } else { /* 65C02 and above have this bug fixed */ - PC = ReadMemW (Addr); + PC = MemReadWord (Addr); } PC += 3; } @@ -1363,7 +1041,11 @@ static void OPC_6502_6C (void) static void OPC_6502_6D (void) /* Opcode $6D: ADC abs */ { - NotImplemented (); + unsigned Addr; + Cycles = 4; + Addr = MemReadWord (PC+1); + ADC (MemReadByte (Addr)); + PC += 3; } @@ -1371,15 +1053,14 @@ static void OPC_6502_6D (void) static void OPC_6502_6E (void) /* Opcode $6E: ROR abs */ { - NotImplemented (); -} - - - -static void OPC_6502_6F (void) -/* Opcode $6F */ -{ - Illegal (); + unsigned Addr; + unsigned Val; + Cycles = 6; + Addr = MemReadWord (PC+1); + Val = MemReadByte (Addr); + ROR (Val); + MemWriteByte (Addr, Val); + PC += 3; } @@ -1395,31 +1076,16 @@ static void OPC_6502_70 (void) static void OPC_6502_71 (void) /* Opcode $71: ADC (zp),y */ { - NotImplemented (); -} - - - -static void OPC_6502_72 (void) -/* Opcode $72 */ -{ - Illegal (); -} - - - -static void OPC_6502_73 (void) -/* Opcode $73 */ -{ - Illegal (); -} - - - -static void OPC_6502_74 (void) -/* Opcode $74 */ -{ - Illegal (); + unsigned char ZPAddr; + unsigned Addr; + Cycles = 5; + ZPAddr = MemReadByte (PC+1); + Addr = MemReadZPWord (ZPAddr); + if (PAGE_CROSS (Addr, YR)) { + ++Cycles; + } + ADC (MemReadByte (Addr + YR)); + PC += 2; } @@ -1427,7 +1093,11 @@ static void OPC_6502_74 (void) static void OPC_6502_75 (void) /* Opcode $75: ADC zp,x */ { - NotImplemented (); + unsigned char ZPAddr; + Cycles = 4; + ZPAddr = MemReadByte (PC+1) + XR; + ADC (MemReadByte (ZPAddr)); + PC += 2; } @@ -1435,15 +1105,14 @@ static void OPC_6502_75 (void) static void OPC_6502_76 (void) /* Opcode $76: ROR zp,x */ { - NotImplemented (); -} - - - -static void OPC_6502_77 (void) -/* Opcode $77 */ -{ - Illegal (); + unsigned char ZPAddr; + unsigned Val; + Cycles = 6; + ZPAddr = MemReadByte (PC+1) + XR; + Val = MemReadByte (ZPAddr); + ROR (Val); + MemWriteByte (ZPAddr, Val); + PC += 2; } @@ -1461,31 +1130,14 @@ static void OPC_6502_78 (void) static void OPC_6502_79 (void) /* Opcode $79: ADC abs,y */ { - NotImplemented (); -} - - - -static void OPC_6502_7A (void) -/* Opcode $7A */ -{ - Illegal (); -} - - - -static void OPC_6502_7B (void) -/* Opcode $7B */ -{ - Illegal (); -} - - - -static void OPC_6502_7C (void) -/* Opcode $7C */ -{ - Illegal (); + unsigned Addr; + Cycles = 4; + Addr = MemReadWord (PC+1); + if (PAGE_CROSS (Addr, YR)) { + ++Cycles; + } + ADC (MemReadByte (Addr + YR)); + PC += 3; } @@ -1493,7 +1145,14 @@ static void OPC_6502_7C (void) static void OPC_6502_7D (void) /* Opcode $7D: ADC abs,x */ { - NotImplemented (); + unsigned Addr; + Cycles = 4; + Addr = MemReadWord (PC+1); + if (PAGE_CROSS (Addr, XR)) { + ++Cycles; + } + ADC (MemReadByte (Addr + XR)); + PC += 3; } @@ -1501,23 +1160,14 @@ static void OPC_6502_7D (void) static void OPC_6502_7E (void) /* Opcode $7E: ROR abs,x */ { - NotImplemented (); -} - - - -static void OPC_6502_7F (void) -/* Opcode $7F */ -{ - Illegal (); -} - - - -static void OPC_6502_80 (void) -/* Opcode $80 */ -{ - Illegal (); + unsigned Addr; + unsigned Val; + Cycles = 7; + Addr = MemReadByte (PC+1) + XR; + Val = MemReadByte (Addr); + ROR (Val); + MemWriteByte (Addr, Val); + PC += 3; } @@ -1525,23 +1175,13 @@ static void OPC_6502_80 (void) static void OPC_6502_81 (void) /* Opcode $81: STA (zp,x) */ { - NotImplemented (); -} - - - -static void OPC_6502_82 (void) -/* Opcode $82 */ -{ - Illegal (); -} - - - -static void OPC_6502_83 (void) -/* Opcode $83 */ -{ - Illegal (); + unsigned char ZPAddr; + unsigned Addr; + Cycles = 6; + ZPAddr = MemReadByte (PC+1) + XR; + Addr = MemReadZPWord (ZPAddr); + MemWriteByte (Addr, AC); + PC += 2; } @@ -1551,8 +1191,8 @@ static void OPC_6502_84 (void) { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - WriteMem (ZPAddr, YR); + ZPAddr = MemReadByte (PC+1); + MemWriteByte (ZPAddr, YR); PC += 2; } @@ -1563,8 +1203,8 @@ static void OPC_6502_85 (void) { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - WriteMem (ZPAddr, AC); + ZPAddr = MemReadByte (PC+1); + MemWriteByte (ZPAddr, AC); PC += 2; } @@ -1575,21 +1215,13 @@ static void OPC_6502_86 (void) { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - WriteMem (ZPAddr, XR); + ZPAddr = MemReadByte (PC+1); + MemWriteByte (ZPAddr, XR); PC += 2; } -static void OPC_6502_87 (void) -/* Opcode $87 */ -{ - Illegal (); -} - - - static void OPC_6502_88 (void) /* Opcode $88: DEY */ { @@ -1602,14 +1234,6 @@ static void OPC_6502_88 (void) -static void OPC_6502_89 (void) -/* Opcode $89 */ -{ - Illegal (); -} - - - static void OPC_6502_8A (void) /* Opcode $8A: TXA */ { @@ -1622,21 +1246,13 @@ static void OPC_6502_8A (void) -static void OPC_6502_8B (void) -/* Opcode $8B */ -{ - Illegal (); -} - - - static void OPC_6502_8C (void) /* Opcode $8C: STY abs */ { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - WriteMem (Addr, YR); + Addr = MemReadWord (PC+1); + MemWriteByte (Addr, YR); PC += 3; } @@ -1647,8 +1263,8 @@ static void OPC_6502_8D (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - WriteMem (Addr, AC); + Addr = MemReadWord (PC+1); + MemWriteByte (Addr, AC); PC += 3; } @@ -1659,21 +1275,13 @@ static void OPC_6502_8E (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - WriteMem (Addr, XR); + Addr = MemReadWord (PC+1); + MemWriteByte (Addr, XR); PC += 3; } -static void OPC_6502_8F (void) -/* Opcode $8F */ -{ - Illegal (); -} - - - static void OPC_6502_90 (void) /* Opcode $90: BCC */ { @@ -1688,37 +1296,21 @@ static void OPC_6502_91 (void) unsigned char ZPAddr; unsigned Addr; Cycles = 6; - ZPAddr = ReadMem (PC+1); - Addr = ReadZeroPageW (ZPAddr) + YR; - WriteMem (Addr, AC); + ZPAddr = MemReadByte (PC+1); + Addr = MemReadZPWord (ZPAddr) + YR; + MemWriteByte (Addr, AC); PC += 2; } -static void OPC_6502_92 (void) -/* Opcode $92 */ -{ - Illegal (); -} - - - -static void OPC_6502_93 (void) -/* Opcode $93 */ -{ - Illegal (); -} - - - static void OPC_6502_94 (void) /* Opcode $94: STY zp,x */ { unsigned char ZPAddr; Cycles = 4; - ZPAddr = ReadMem (PC+1) + XR; - WriteMem (ZPAddr, YR); + ZPAddr = MemReadByte (PC+1) + XR; + MemWriteByte (ZPAddr, YR); PC += 2; } @@ -1729,8 +1321,8 @@ static void OPC_6502_95 (void) { unsigned char ZPAddr; Cycles = 4; - ZPAddr = ReadMem (PC+1) + XR; - WriteMem (ZPAddr, AC); + ZPAddr = MemReadByte (PC+1) + XR; + MemWriteByte (ZPAddr, AC); PC += 2; } @@ -1741,21 +1333,13 @@ static void OPC_6502_96 (void) { unsigned char ZPAddr; Cycles = 4; - ZPAddr = ReadMem (PC+1) + YR; - WriteMem (ZPAddr, XR); + ZPAddr = MemReadByte (PC+1) + YR; + MemWriteByte (ZPAddr, XR); PC += 2; } -static void OPC_6502_97 (void) -/* Opcode $97 */ -{ - Illegal (); -} - - - static void OPC_6502_98 (void) /* Opcode $98: TYA */ { @@ -1773,8 +1357,8 @@ static void OPC_6502_99 (void) { unsigned Addr; Cycles = 5; - Addr = ReadMemW (PC+1) + YR; - WriteMem (Addr, AC); + Addr = MemReadWord (PC+1) + YR; + MemWriteByte (Addr, AC); PC += 3; } @@ -1790,55 +1374,23 @@ static void OPC_6502_9A (void) -static void OPC_6502_9B (void) -/* Opcode $9B */ -{ - Illegal (); -} - - - -static void OPC_6502_9C (void) -/* Opcode $9C */ -{ - Illegal (); -} - - - static void OPC_6502_9D (void) /* Opcode $9D: STA abs,x */ { unsigned Addr; Cycles = 5; - Addr = ReadMemW (PC+1) + XR; - WriteMem (Addr, AC); + Addr = MemReadWord (PC+1) + XR; + MemWriteByte (Addr, AC); PC += 3; } -static void OPC_6502_9E (void) -/* Opcode $9E */ -{ - Illegal (); -} - - - -static void OPC_6502_9F (void) -/* Opcode $9F */ -{ - Illegal (); -} - - - static void OPC_6502_A0 (void) /* Opcode $A0: LDY #imm */ { Cycles = 2; - YR = ReadMem (PC+1); + YR = MemReadByte (PC+1); TEST_ZF (YR); TEST_SF (YR); PC += 2; @@ -1852,9 +1404,9 @@ static void OPC_6502_A1 (void) unsigned char ZPAddr; unsigned Addr; Cycles = 6; - ZPAddr = ReadMem (PC+1) + XR; - Addr = ReadZeroPageW (ZPAddr); - AC = ReadMem (Addr); + ZPAddr = MemReadByte (PC+1) + XR; + Addr = MemReadZPWord (ZPAddr); + AC = MemReadByte (Addr); TEST_ZF (AC); TEST_SF (AC); PC += 2; @@ -1866,7 +1418,7 @@ static void OPC_6502_A2 (void) /* Opcode $A2: LDX #imm */ { Cycles = 2; - XR = ReadMem (PC+1); + XR = MemReadByte (PC+1); TEST_ZF (XR); TEST_SF (XR); PC += 2; @@ -1874,21 +1426,13 @@ static void OPC_6502_A2 (void) -static void OPC_6502_A3 (void) -/* Opcode $A3 */ -{ - Illegal (); -} - - - static void OPC_6502_A4 (void) /* Opcode $A4: LDY zp */ { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - YR = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1); + YR = MemReadByte (ZPAddr); TEST_ZF (YR); TEST_SF (YR); PC += 2; @@ -1901,8 +1445,8 @@ static void OPC_6502_A5 (void) { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - AC = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1); + AC = MemReadByte (ZPAddr); TEST_ZF (AC); TEST_SF (AC); PC += 2; @@ -1915,8 +1459,8 @@ static void OPC_6502_A6 (void) { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - XR = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1); + XR = MemReadByte (ZPAddr); TEST_ZF (XR); TEST_SF (XR); PC += 2; @@ -1924,14 +1468,6 @@ static void OPC_6502_A6 (void) -static void OPC_6502_A7 (void) -/* Opcode $A7 */ -{ - Illegal (); -} - - - static void OPC_6502_A8 (void) /* Opcode $A8: TAY */ { @@ -1948,7 +1484,7 @@ static void OPC_6502_A9 (void) /* Opcode $A9: LDA #imm */ { Cycles = 2; - AC = ReadMem (PC+1); + AC = MemReadByte (PC+1); TEST_ZF (AC); TEST_SF (AC); PC += 2; @@ -1968,21 +1504,13 @@ static void OPC_6502_AA (void) -static void OPC_6502_AB (void) -/* Opcode $AB */ -{ - Illegal (); -} - - - static void OPC_6502_AC (void) /* Opcode $AC: LDY abs */ { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - YR = ReadMem (Addr); + Addr = MemReadWord (PC+1); + YR = MemReadByte (Addr); TEST_ZF (YR); TEST_SF (YR); PC += 3; @@ -1995,8 +1523,8 @@ static void OPC_6502_AD (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - AC = ReadMem (Addr); + Addr = MemReadWord (PC+1); + AC = MemReadByte (Addr); TEST_ZF (AC); TEST_SF (AC); PC += 3; @@ -2009,8 +1537,8 @@ static void OPC_6502_AE (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - XR = ReadMem (Addr); + Addr = MemReadWord (PC+1); + XR = MemReadByte (Addr); TEST_ZF (XR); TEST_SF (XR); PC += 3; @@ -2018,14 +1546,6 @@ static void OPC_6502_AE (void) -static void OPC_6502_AF (void) -/* Opcode $AF */ -{ - Illegal (); -} - - - static void OPC_6502_B0 (void) /* Opcode $B0: BCS */ { @@ -2040,12 +1560,12 @@ static void OPC_6502_B1 (void) unsigned char ZPAddr; unsigned Addr; Cycles = 5; - ZPAddr = ReadMem (PC+1); - Addr = ReadZeroPageW (ZPAddr); + ZPAddr = MemReadByte (PC+1); + Addr = MemReadZPWord (ZPAddr); if (PAGE_CROSS (Addr, YR)) { ++Cycles; } - AC = ReadMem (Addr + YR); + AC = MemReadByte (Addr + YR); TEST_ZF (AC); TEST_SF (AC); PC += 2; @@ -2053,29 +1573,13 @@ static void OPC_6502_B1 (void) -static void OPC_6502_B2 (void) -/* Opcode $B2 */ -{ - Illegal (); -} - - - -static void OPC_6502_B3 (void) -/* Opcode $B3 */ -{ - Illegal (); -} - - - static void OPC_6502_B4 (void) /* Opcode $B4: LDY zp,x */ { unsigned char ZPAddr; Cycles = 4; - ZPAddr = ReadMem (PC+1) + XR; - YR = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1) + XR; + YR = MemReadByte (ZPAddr); TEST_ZF (YR); TEST_SF (YR); PC += 2; @@ -2088,8 +1592,8 @@ static void OPC_6502_B5 (void) { unsigned char ZPAddr; Cycles = 4; - ZPAddr = ReadMem (PC+1) + XR; - AC = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1) + XR; + AC = MemReadByte (ZPAddr); TEST_ZF (AC); TEST_SF (AC); PC += 2; @@ -2102,8 +1606,8 @@ static void OPC_6502_B6 (void) { unsigned char ZPAddr; Cycles = 4; - ZPAddr = ReadMem (PC+1) + YR; - XR = ReadMem (ZPAddr); + ZPAddr = MemReadByte (PC+1) + YR; + XR = MemReadByte (ZPAddr); TEST_ZF (XR); TEST_SF (XR); PC += 2; @@ -2111,14 +1615,6 @@ static void OPC_6502_B6 (void) -static void OPC_6502_B7 (void) -/* Opcode $B7 */ -{ - Illegal (); -} - - - static void OPC_6502_B8 (void) /* Opcode $B8: CLV */ { @@ -2134,11 +1630,11 @@ static void OPC_6502_B9 (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); + Addr = MemReadWord (PC+1); if (PAGE_CROSS (Addr, YR)) { ++Cycles; } - AC = ReadMem (Addr + YR); + AC = MemReadByte (Addr + YR); TEST_ZF (AC); TEST_SF (AC); PC += 3; @@ -2158,24 +1654,16 @@ static void OPC_6502_BA (void) -static void OPC_6502_BB (void) -/* Opcode $BB */ -{ - Illegal (); -} - - - static void OPC_6502_BC (void) /* Opcode $BC: LDY abs,x */ { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); + Addr = MemReadWord (PC+1); if (PAGE_CROSS (Addr, XR)) { ++Cycles; } - YR = ReadMem (Addr + XR); + YR = MemReadByte (Addr + XR); TEST_ZF (YR); TEST_SF (YR); PC += 3; @@ -2188,11 +1676,11 @@ static void OPC_6502_BD (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); + Addr = MemReadWord (PC+1); if (PAGE_CROSS (Addr, XR)) { ++Cycles; } - AC = ReadMem (Addr + XR); + AC = MemReadByte (Addr + XR); TEST_ZF (AC); TEST_SF (AC); PC += 3; @@ -2205,11 +1693,11 @@ static void OPC_6502_BE (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); + Addr = MemReadWord (PC+1); if (PAGE_CROSS (Addr, YR)) { ++Cycles; } - XR = ReadMem (Addr + YR); + XR = MemReadByte (Addr + YR); TEST_ZF (XR); TEST_SF (XR); PC += 3; @@ -2217,19 +1705,11 @@ static void OPC_6502_BE (void) -static void OPC_6502_BF (void) -/* Opcode $BF */ -{ - Illegal (); -} - - - static void OPC_6502_C0 (void) /* Opcode $C0: CPY #imm */ { Cycles = 2; - CMP (YR, ReadMem (PC+1)); + CMP (YR, MemReadByte (PC+1)); PC += 2; } @@ -2241,37 +1721,21 @@ static void OPC_6502_C1 (void) unsigned char ZPAddr; unsigned Addr; Cycles = 6; - ZPAddr = ReadMem (PC+1) + XR; - Addr = ReadZeroPageW (ZPAddr); - CMP (AC, ReadMem (Addr)); + ZPAddr = MemReadByte (PC+1) + XR; + Addr = MemReadZPWord (ZPAddr); + CMP (AC, MemReadByte (Addr)); PC += 2; } -static void OPC_6502_C2 (void) -/* Opcode $C2 */ -{ - Illegal (); -} - - - -static void OPC_6502_C3 (void) -/* Opcode $C3 */ -{ - Illegal (); -} - - - static void OPC_6502_C4 (void) /* Opcode $C4: CPY zp */ { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - CMP (YR, ReadMem (ZPAddr)); + ZPAddr = MemReadByte (PC+1); + CMP (YR, MemReadByte (ZPAddr)); PC += 2; } @@ -2282,8 +1746,8 @@ static void OPC_6502_C5 (void) { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - CMP (AC, ReadMem (ZPAddr)); + ZPAddr = MemReadByte (PC+1); + CMP (AC, MemReadByte (ZPAddr)); PC += 2; } @@ -2295,9 +1759,9 @@ static void OPC_6502_C6 (void) unsigned char ZPAddr; unsigned char Val; Cycles = 5; - ZPAddr = ReadMem (PC+1); - Val = ReadMem (ZPAddr) - 1; - WriteMem (ZPAddr, Val); + ZPAddr = MemReadByte (PC+1); + Val = MemReadByte (ZPAddr) - 1; + MemWriteByte (ZPAddr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 2; @@ -2305,14 +1769,6 @@ static void OPC_6502_C6 (void) -static void OPC_6502_C7 (void) -/* Opcode $C7 */ -{ - Illegal (); -} - - - static void OPC_6502_C8 (void) /* Opcode $C8: INY */ { @@ -2329,7 +1785,7 @@ static void OPC_6502_C9 (void) /* Opcode $C9: CMP #imm */ { Cycles = 2; - CMP (AC, ReadMem (PC+1)); + CMP (AC, MemReadByte (PC+1)); PC += 2; } @@ -2347,21 +1803,13 @@ static void OPC_6502_CA (void) -static void OPC_6502_CB (void) -/* Opcode $CB */ -{ - Illegal (); -} - - - static void OPC_6502_CC (void) /* Opcode $CC: CPY abs */ { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - CMP (YR, ReadMem (Addr)); + Addr = MemReadWord (PC+1); + CMP (YR, MemReadByte (Addr)); PC += 3; } @@ -2372,8 +1820,8 @@ static void OPC_6502_CD (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - CMP (AC, ReadMem (Addr)); + Addr = MemReadWord (PC+1); + CMP (AC, MemReadByte (Addr)); PC += 3; } @@ -2385,9 +1833,9 @@ static void OPC_6502_CE (void) unsigned Addr; unsigned char Val; Cycles = 6; - Addr = ReadMemW (PC+1); - Val = ReadMem (Addr) - 1; - WriteMem (Addr, Val); + Addr = MemReadWord (PC+1); + Val = MemReadByte (Addr) - 1; + MemWriteByte (Addr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 3; @@ -2395,14 +1843,6 @@ static void OPC_6502_CE (void) -static void OPC_6502_CF (void) -/* Opcode $CF */ -{ - Illegal (); -} - - - static void OPC_6502_D0 (void) /* Opcode $D0: BNE */ { @@ -2417,48 +1857,24 @@ static void OPC_6502_D1 (void) unsigned ZPAddr; unsigned Addr; Cycles = 5; - ZPAddr = ReadMem (PC+1); - Addr = ReadMemW (ZPAddr); + ZPAddr = MemReadByte (PC+1); + Addr = MemReadWord (ZPAddr); if (PAGE_CROSS (Addr, YR)) { ++Cycles; } - CMP (AC, ReadMem (Addr + YR)); + CMP (AC, MemReadByte (Addr + YR)); PC += 2; } -static void OPC_6502_D2 (void) -/* Opcode $D2 */ -{ - Illegal (); -} - - - -static void OPC_6502_D3 (void) -/* Opcode $D3 */ -{ - Illegal (); -} - - - -static void OPC_6502_D4 (void) -/* Opcode $D4 */ -{ - Illegal (); -} - - - static void OPC_6502_D5 (void) /* Opcode $D5: CMP zp,x */ { unsigned char ZPAddr; Cycles = 4; - ZPAddr = ReadMem (PC+1) + XR; - CMP (AC, ReadMem (ZPAddr)); + ZPAddr = MemReadByte (PC+1) + XR; + CMP (AC, MemReadByte (ZPAddr)); PC += 2; } @@ -2470,9 +1886,9 @@ static void OPC_6502_D6 (void) unsigned char ZPAddr; unsigned char Val; Cycles = 6; - ZPAddr = ReadMem (PC+1) + XR; - Val = ReadMem (ZPAddr) - 1; - WriteMem (ZPAddr, Val); + ZPAddr = MemReadByte (PC+1) + XR; + Val = MemReadByte (ZPAddr) - 1; + MemWriteByte (ZPAddr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 2; @@ -2480,14 +1896,6 @@ static void OPC_6502_D6 (void) -static void OPC_6502_D7 (void) -/* Opcode $D7 */ -{ - Illegal (); -} - - - static void OPC_6502_D8 (void) /* Opcode $D8: CLD */ { @@ -2503,50 +1911,26 @@ static void OPC_6502_D9 (void) { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); + Addr = MemReadWord (PC+1); if (PAGE_CROSS (Addr, YR)) { ++Cycles; } - CMP (AC, ReadMem (Addr + YR)); + CMP (AC, MemReadByte (Addr + YR)); PC += 3; } -static void OPC_6502_DA (void) -/* Opcode $DA */ -{ - Illegal (); -} - - - -static void OPC_6502_DB (void) -/* Opcode $DB */ -{ - Illegal (); -} - - - -static void OPC_6502_DC (void) -/* Opcode $DC */ -{ - Illegal (); -} - - - static void OPC_6502_DD (void) /* Opcode $DD: CMP abs,x */ { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); + Addr = MemReadWord (PC+1); if (PAGE_CROSS (Addr, XR)) { ++Cycles; } - CMP (AC, ReadMem (Addr + XR)); + CMP (AC, MemReadByte (Addr + XR)); PC += 3; } @@ -2558,9 +1942,9 @@ static void OPC_6502_DE (void) unsigned Addr; unsigned char Val; Cycles = 7; - Addr = ReadMemW (PC+1) + XR; - Val = ReadMem (Addr) - 1; - WriteMem (Addr, Val); + Addr = MemReadWord (PC+1) + XR; + Val = MemReadByte (Addr) - 1; + MemWriteByte (Addr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 3; @@ -2568,19 +1952,11 @@ static void OPC_6502_DE (void) -static void OPC_6502_DF (void) -/* Opcode $DF */ -{ - Illegal (); -} - - - static void OPC_6502_E0 (void) /* Opcode $E0: CPX #imm */ { Cycles = 2; - CMP (XR, ReadMem (PC+1)); + CMP (XR, MemReadByte (PC+1)); PC += 2; } @@ -2589,23 +1965,13 @@ static void OPC_6502_E0 (void) static void OPC_6502_E1 (void) /* Opcode $E1: SBC (zp,x) */ { - NotImplemented (); -} - - - -static void OPC_6502_E2 (void) -/* Opcode $E2 */ -{ - Illegal (); -} - - - -static void OPC_6502_E3 (void) -/* Opcode $E3 */ -{ - Illegal (); + unsigned char ZPAddr; + unsigned Addr; + Cycles = 6; + ZPAddr = MemReadByte (PC+1) + XR; + Addr = MemReadZPWord (ZPAddr); + SBC (MemReadByte (Addr)); + PC += 2; } @@ -2615,8 +1981,8 @@ static void OPC_6502_E4 (void) { unsigned char ZPAddr; Cycles = 3; - ZPAddr = ReadMem (PC+1); - CMP (XR, ReadMem (ZPAddr)); + ZPAddr = MemReadByte (PC+1); + CMP (XR, MemReadByte (ZPAddr)); PC += 2; } @@ -2625,7 +1991,11 @@ static void OPC_6502_E4 (void) static void OPC_6502_E5 (void) /* Opcode $E5: SBC zp */ { - NotImplemented (); + unsigned char ZPAddr; + Cycles = 3; + ZPAddr = MemReadByte (PC+1); + SBC (MemReadByte (ZPAddr)); + PC += 2; } @@ -2636,9 +2006,9 @@ static void OPC_6502_E6 (void) unsigned char ZPAddr; unsigned char Val; Cycles = 5; - ZPAddr = ReadMem (PC+1); - Val = ReadMem (ZPAddr) + 1; - WriteMem (ZPAddr, Val); + ZPAddr = MemReadByte (PC+1); + Val = MemReadByte (ZPAddr) + 1; + MemWriteByte (ZPAddr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 2; @@ -2646,14 +2016,6 @@ static void OPC_6502_E6 (void) -static void OPC_6502_E7 (void) -/* Opcode $E7 */ -{ - Illegal (); -} - - - static void OPC_6502_E8 (void) /* Opcode $E8: INX */ { @@ -2669,7 +2031,9 @@ static void OPC_6502_E8 (void) static void OPC_6502_E9 (void) /* Opcode $E9: SBC #imm */ { - NotImplemented (); + Cycles = 2; + SBC (MemReadByte (PC+1)); + PC += 2; } @@ -2684,21 +2048,13 @@ static void OPC_6502_EA (void) -static void OPC_6502_EB (void) -/* Opcode $EB */ -{ - Illegal (); -} - - - static void OPC_6502_EC (void) /* Opcode $EC: CPX abs */ { unsigned Addr; Cycles = 4; - Addr = ReadMemW (PC+1); - CMP (XR, ReadMem (Addr)); + Addr = MemReadWord (PC+1); + CMP (XR, MemReadByte (Addr)); PC += 3; } @@ -2707,7 +2063,11 @@ static void OPC_6502_EC (void) static void OPC_6502_ED (void) /* Opcode $ED: SBC abs */ { - NotImplemented (); + unsigned Addr; + Cycles = 4; + Addr = MemReadWord (PC+1); + SBC (MemReadByte (Addr)); + PC += 3; } @@ -2718,9 +2078,9 @@ static void OPC_6502_EE (void) unsigned Addr; unsigned char Val; Cycles = 6; - Addr = ReadMemW (PC+1); - Val = ReadMem (Addr) + 1; - WriteMem (Addr, Val); + Addr = MemReadWord (PC+1); + Val = MemReadByte (Addr) + 1; + MemWriteByte (Addr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 3; @@ -2728,14 +2088,6 @@ static void OPC_6502_EE (void) -static void OPC_6502_EF (void) -/* Opcode $EF */ -{ - Illegal (); -} - - - static void OPC_6502_F0 (void) /* Opcode $F0: BEQ */ { @@ -2747,31 +2099,16 @@ static void OPC_6502_F0 (void) static void OPC_6502_F1 (void) /* Opcode $F1: SBC (zp),y */ { - NotImplemented (); -} - - - -static void OPC_6502_F2 (void) -/* Opcode $F2 */ -{ - Illegal (); -} - - - -static void OPC_6502_F3 (void) -/* Opcode $F3 */ -{ - Illegal (); -} - - - -static void OPC_6502_F4 (void) -/* Opcode $F4 */ -{ - Illegal (); + unsigned char ZPAddr; + unsigned Addr; + Cycles = 5; + ZPAddr = MemReadByte (PC+1); + Addr = MemReadZPWord (ZPAddr); + if (PAGE_CROSS (Addr, YR)) { + ++Cycles; + } + SBC (MemReadByte (Addr + YR)); + PC += 2; } @@ -2779,7 +2116,11 @@ static void OPC_6502_F4 (void) static void OPC_6502_F5 (void) /* Opcode $F5: SBC zp,x */ { - NotImplemented (); + unsigned char ZPAddr; + Cycles = 4; + ZPAddr = MemReadByte (PC+1) + XR; + SBC (MemReadByte (ZPAddr)); + PC += 2; } @@ -2790,9 +2131,9 @@ static void OPC_6502_F6 (void) unsigned char ZPAddr; unsigned char Val; Cycles = 6; - ZPAddr = ReadMem (PC+1) + XR; - Val = ReadMem (ZPAddr) + 1; - WriteMem (ZPAddr, Val); + ZPAddr = MemReadByte (PC+1) + XR; + Val = MemReadByte (ZPAddr) + 1; + MemWriteByte (ZPAddr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 2; @@ -2800,14 +2141,6 @@ static void OPC_6502_F6 (void) -static void OPC_6502_F7 (void) -/* Opcode $F7 */ -{ - Illegal (); -} - - - static void OPC_6502_F8 (void) /* Opcode $F8: SED */ { @@ -2819,31 +2152,14 @@ static void OPC_6502_F8 (void) static void OPC_6502_F9 (void) /* Opcode $F9: SBC abs,y */ { - NotImplemented (); -} - - - -static void OPC_6502_FA (void) -/* Opcode $FA */ -{ - Illegal (); -} - - - -static void OPC_6502_FB (void) -/* Opcode $FB */ -{ - Illegal (); -} - - - -static void OPC_6502_FC (void) -/* Opcode $FC */ -{ - Illegal (); + unsigned Addr; + Cycles = 4; + Addr = MemReadWord (PC+1); + if (PAGE_CROSS (Addr, YR)) { + ++Cycles; + } + SBC (MemReadByte (Addr + YR)); + PC += 3; } @@ -2851,7 +2167,14 @@ static void OPC_6502_FC (void) static void OPC_6502_FD (void) /* Opcode $FD: SBC abs,x */ { - NotImplemented (); + unsigned Addr; + Cycles = 4; + Addr = MemReadWord (PC+1); + if (PAGE_CROSS (Addr, XR)) { + ++Cycles; + } + SBC (MemReadByte (Addr + XR)); + PC += 3; } @@ -2862,9 +2185,9 @@ static void OPC_6502_FE (void) unsigned Addr; unsigned char Val; Cycles = 7; - Addr = ReadMemW (PC+1) + XR; - Val = ReadMem (Addr) + 1; - WriteMem (Addr, Val); + Addr = MemReadWord (PC+1) + XR; + Val = MemReadByte (Addr) + 1; + MemWriteByte (Addr, Val); TEST_ZF (Val); TEST_SF (Val); PC += 3; @@ -2872,14 +2195,6 @@ static void OPC_6502_FE (void) -static void OPC_6502_FF (void) -/* Opcode $FF */ -{ - Illegal (); -} - - - /*****************************************************************************/ /* Data */ /*****************************************************************************/ @@ -2891,260 +2206,260 @@ typedef void (*OPCFunc) (void); static OPCFunc OPCTable[256] = { OPC_6502_00, OPC_6502_01, - OPC_6502_02, - OPC_6502_03, - OPC_6502_04, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_05, OPC_6502_06, - OPC_6502_07, + OPC_Illegal, OPC_6502_08, OPC_6502_09, OPC_6502_0A, - OPC_6502_0B, - OPC_6502_0C, + OPC_Illegal, + OPC_Illegal, OPC_6502_0D, OPC_6502_0E, - OPC_6502_0F, + OPC_Illegal, OPC_6502_10, OPC_6502_11, - OPC_6502_12, - OPC_6502_13, - OPC_6502_14, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_15, OPC_6502_16, - OPC_6502_17, + OPC_Illegal, OPC_6502_18, OPC_6502_19, - OPC_6502_1A, - OPC_6502_1B, - OPC_6502_1C, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_1D, OPC_6502_1E, - OPC_6502_1F, + OPC_Illegal, OPC_6502_20, OPC_6502_21, - OPC_6502_22, - OPC_6502_23, + OPC_Illegal, + OPC_Illegal, OPC_6502_24, OPC_6502_25, OPC_6502_26, - OPC_6502_27, + OPC_Illegal, OPC_6502_28, OPC_6502_29, OPC_6502_2A, - OPC_6502_2B, + OPC_Illegal, OPC_6502_2C, OPC_6502_2D, OPC_6502_2E, - OPC_6502_2F, + OPC_Illegal, OPC_6502_30, OPC_6502_31, - OPC_6502_32, - OPC_6502_33, - OPC_6502_34, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_35, OPC_6502_36, - OPC_6502_37, + OPC_Illegal, OPC_6502_38, OPC_6502_39, - OPC_6502_3A, - OPC_6502_3B, - OPC_6502_3C, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_3D, OPC_6502_3E, - OPC_6502_3F, + OPC_Illegal, OPC_6502_40, OPC_6502_41, - OPC_6502_42, - OPC_6502_43, - OPC_6502_44, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_45, OPC_6502_46, - OPC_6502_47, + OPC_Illegal, OPC_6502_48, OPC_6502_49, OPC_6502_4A, - OPC_6502_4B, + OPC_Illegal, OPC_6502_4C, OPC_6502_4D, OPC_6502_4E, - OPC_6502_4F, + OPC_Illegal, OPC_6502_50, OPC_6502_51, - OPC_6502_52, - OPC_6502_53, - OPC_6502_54, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_55, OPC_6502_56, - OPC_6502_57, + OPC_Illegal, OPC_6502_58, OPC_6502_59, - OPC_6502_5A, - OPC_6502_5B, - OPC_6502_5C, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_5D, OPC_6502_5E, - OPC_6502_5F, + OPC_Illegal, OPC_6502_60, OPC_6502_61, - OPC_6502_62, - OPC_6502_63, - OPC_6502_64, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_65, OPC_6502_66, - OPC_6502_67, + OPC_Illegal, OPC_6502_68, OPC_6502_69, OPC_6502_6A, - OPC_6502_6B, + OPC_Illegal, OPC_6502_6C, OPC_6502_6D, OPC_6502_6E, - OPC_6502_6F, + OPC_Illegal, OPC_6502_70, OPC_6502_71, - OPC_6502_72, - OPC_6502_73, - OPC_6502_74, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_75, OPC_6502_76, - OPC_6502_77, + OPC_Illegal, OPC_6502_78, OPC_6502_79, - OPC_6502_7A, - OPC_6502_7B, - OPC_6502_7C, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_7D, OPC_6502_7E, - OPC_6502_7F, - OPC_6502_80, + OPC_Illegal, + OPC_Illegal, OPC_6502_81, - OPC_6502_82, - OPC_6502_83, + OPC_Illegal, + OPC_Illegal, OPC_6502_84, OPC_6502_85, OPC_6502_86, - OPC_6502_87, + OPC_Illegal, OPC_6502_88, - OPC_6502_89, + OPC_Illegal, OPC_6502_8A, - OPC_6502_8B, + OPC_Illegal, OPC_6502_8C, OPC_6502_8D, OPC_6502_8E, - OPC_6502_8F, + OPC_Illegal, OPC_6502_90, OPC_6502_91, - OPC_6502_92, - OPC_6502_93, + OPC_Illegal, + OPC_Illegal, OPC_6502_94, OPC_6502_95, OPC_6502_96, - OPC_6502_97, + OPC_Illegal, OPC_6502_98, OPC_6502_99, OPC_6502_9A, - OPC_6502_9B, - OPC_6502_9C, + OPC_Illegal, + OPC_Illegal, OPC_6502_9D, - OPC_6502_9E, - OPC_6502_9F, + OPC_Illegal, + OPC_Illegal, OPC_6502_A0, OPC_6502_A1, OPC_6502_A2, - OPC_6502_A3, + OPC_Illegal, OPC_6502_A4, OPC_6502_A5, OPC_6502_A6, - OPC_6502_A7, + OPC_Illegal, OPC_6502_A8, OPC_6502_A9, OPC_6502_AA, - OPC_6502_AB, + OPC_Illegal, OPC_6502_AC, OPC_6502_AD, OPC_6502_AE, - OPC_6502_AF, + OPC_Illegal, OPC_6502_B0, OPC_6502_B1, - OPC_6502_B2, - OPC_6502_B3, + OPC_Illegal, + OPC_Illegal, OPC_6502_B4, OPC_6502_B5, OPC_6502_B6, - OPC_6502_B7, + OPC_Illegal, OPC_6502_B8, OPC_6502_B9, OPC_6502_BA, - OPC_6502_BB, + OPC_Illegal, OPC_6502_BC, OPC_6502_BD, OPC_6502_BE, - OPC_6502_BF, + OPC_Illegal, OPC_6502_C0, OPC_6502_C1, - OPC_6502_C2, - OPC_6502_C3, + OPC_Illegal, + OPC_Illegal, OPC_6502_C4, OPC_6502_C5, OPC_6502_C6, - OPC_6502_C7, + OPC_Illegal, OPC_6502_C8, OPC_6502_C9, OPC_6502_CA, - OPC_6502_CB, + OPC_Illegal, OPC_6502_CC, OPC_6502_CD, OPC_6502_CE, - OPC_6502_CF, + OPC_Illegal, OPC_6502_D0, OPC_6502_D1, - OPC_6502_D2, - OPC_6502_D3, - OPC_6502_D4, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_D5, OPC_6502_D6, - OPC_6502_D7, + OPC_Illegal, OPC_6502_D8, OPC_6502_D9, - OPC_6502_DA, - OPC_6502_DB, - OPC_6502_DC, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_DD, OPC_6502_DE, - OPC_6502_DF, + OPC_Illegal, OPC_6502_E0, OPC_6502_E1, - OPC_6502_E2, - OPC_6502_E3, + OPC_Illegal, + OPC_Illegal, OPC_6502_E4, OPC_6502_E5, OPC_6502_E6, - OPC_6502_E7, + OPC_Illegal, OPC_6502_E8, OPC_6502_E9, OPC_6502_EA, - OPC_6502_EB, + OPC_Illegal, OPC_6502_EC, OPC_6502_ED, OPC_6502_EE, - OPC_6502_EF, + OPC_Illegal, OPC_6502_F0, OPC_6502_F1, - OPC_6502_F2, - OPC_6502_F3, - OPC_6502_F4, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_F5, OPC_6502_F6, - OPC_6502_F7, + OPC_Illegal, OPC_6502_F8, OPC_6502_F9, - OPC_6502_FA, - OPC_6502_FB, - OPC_6502_FC, + OPC_Illegal, + OPC_Illegal, + OPC_Illegal, OPC_6502_FD, OPC_6502_FE, - OPC_6502_FF, + OPC_Illegal, }; diff --git a/src/sim65/memory.c b/src/sim65/memory.c index 482733fa1..c433d41f3 100644 --- a/src/sim65/memory.c +++ b/src/sim65/memory.c @@ -54,7 +54,7 @@ static unsigned char RAM[0x10000]; -void WriteMem (unsigned Addr, unsigned char Val) +void MemWriteByte (unsigned Addr, unsigned char Val) /* Write a byte to a memory location */ { RAM[Addr] = Val; @@ -62,7 +62,7 @@ void WriteMem (unsigned Addr, unsigned char Val) -unsigned char ReadMem (unsigned Addr) +unsigned char MemReadByte (unsigned Addr) /* Read a byte from a memory location */ { return RAM[Addr]; @@ -70,23 +70,23 @@ unsigned char ReadMem (unsigned Addr) -unsigned ReadMemW (unsigned Addr) +unsigned MemReadWord (unsigned Addr) /* Read a word from a memory location */ { - unsigned W = ReadMem (Addr++); - return (W | (ReadMem (Addr) << 8)); + unsigned W = MemReadByte (Addr++); + return (W | (MemReadByte (Addr) << 8)); } -unsigned ReadZeroPageW (unsigned char Addr) +unsigned MemReadZPWord (unsigned char Addr) /* Read a word from the zero page. This function differs from ReadMemW in that * the read will always be in the zero page, even in case of an address * overflow. */ { - unsigned W = ReadMem (Addr++); - return (W | (ReadMem (Addr) << 8)); + unsigned W = MemReadByte (Addr++); + return (W | (MemReadByte (Addr) << 8)); } diff --git a/src/sim65/memory.h b/src/sim65/memory.h index cce83fc8d..201a27237 100644 --- a/src/sim65/memory.h +++ b/src/sim65/memory.h @@ -50,18 +50,18 @@ -void WriteMem (unsigned Addr, unsigned char Val); +void MemWriteByte (unsigned Addr, unsigned char Val); /* Write a byte to a memory location */ -unsigned char ReadMem (unsigned Addr); +unsigned char MemReadByte (unsigned Addr); /* Read a byte from a memory location */ -unsigned ReadMemW (unsigned Addr); +unsigned MemReadWord (unsigned Addr); /* Read a word from a memory location */ -unsigned ReadZeroPageW (unsigned char Addr); +unsigned MemReadZPWord (unsigned char Addr); /* Read a word from the zero page. This function differs from ReadMemW in that - * the read will always be in the zero page, even in case of an address + * the read will always be in the zero page, even in case of an address * overflow. */