diff --git a/ops/acc.c b/ops/acc.c index d26198b..795691b 100644 --- a/ops/acc.c +++ b/ops/acc.c @@ -1,7 +1,6 @@ // Other Accumulator Instructions --------------------------------------------- // ---------------------------------------------------------------------------- #include "../cpu.h" -extern CPU_8080 *CPU; // Load BYTE (WORD) into A diff --git a/ops/base.c b/ops/base.c index 93b108f..8246a14 100644 --- a/ops/base.c +++ b/ops/base.c @@ -1,7 +1,6 @@ // Basic Instructions --------------------------------------------------------- // ---------------------------------------------------------------------------- #include "../cpu.h" -extern CPU_8080 *CPU; // Mo Operation diff --git a/ops/bit.c b/ops/bit.c index e85f426..b826714 100644 --- a/ops/bit.c +++ b/ops/bit.c @@ -1,7 +1,6 @@ // Bitwise Instructions ------------------------------------------------------- // ---------------------------------------------------------------------------- #include "../cpu.h" -extern CPU_8080 *CPU; // Rotate left ignoring F carry @@ -52,6 +51,12 @@ static void ANA_M() { cpu_flag_szp(CPU->A); \ } +// AND A with next BYTE +static void ANI() { + *CPU->A &= read8(*CPU->PC); \ + cpu_flag_szp(CPU->A); \ +} + // XOR A with register REG #define XRA(REG) static void XRA_##REG() { \ @@ -66,6 +71,12 @@ static void XRA_M() { cpu_flag_szp(CPU->A); \ } +// XOR A with next BYTE +static void XRI() { + *CPU->A ^= read8(*CPU->PC); \ + cpu_flag_szp(CPU->A); \ +} + // OR A with register REG #define ORA(REG) static void ORA_##REG() { \ @@ -80,3 +91,9 @@ static void ORA_M() { cpu_flag_szp(CPU->A); \ } +// OR A with next BYTE +static void ORI() { + *CPU->A |= read8(*CPU->PC); \ + cpu_flag_szp(CPU->A); \ +} + diff --git a/ops/codes.h b/ops/codes.h index f0bc5db..b31dbcf 100644 --- a/ops/codes.h +++ b/ops/codes.h @@ -269,6 +269,94 @@ enum OP_CODE_MNEMONICS { CMP_M, CMP_A, + // C0 - C7 + RNZ, + POP_B, + JNZ, + JMP, + + CNZ, + PUSH_B, + ADI, + RST_0, + + // C8 - Cf + RZ, + RET, + JZ, + JMP_1, // remapped + + CZ, + CALL, + ACI, + RST_1, + + // D0 - D7 + RNC, + POP_D, + JNC, + OUT, + + CNC, + PUSH_D, + SUI, + RST_2, + + // D8 - Df + RC, + RET_1, // remapped + JC, + IN, + + CC, + CALL_1, // remapped + SBI, + RST_3, + + // E0 - E7 + RPO, + POP_H, + JPO, + XTHL, + + CPO, + PUSH_H, + ANI, + RST_4, + + // E8 - Ef + RPE, + PCHL, + JPE, + XCHG, + + CPE, + CALL_2, // remapped + XRI, + RST_5, + + // F0 - F7 + RP, + POP_PSW, + JP, + DI, + + CP, + PUSH_PSW, + ORI, + RST_6, + + // F8 - Ff + RM, + SPHL, + JM, + EI, + + CM, + CALL_3, // remapped + CPI, + RST_7 + }; #endif diff --git a/ops/ctrl.c b/ops/ctrl.c index bdb8c6b..6aa1a33 100644 --- a/ops/ctrl.c +++ b/ops/ctrl.c @@ -1,7 +1,7 @@ // Basic Instructions --------------------------------------------------------- // ---------------------------------------------------------------------------- #include "../cpu.h" -extern CPU_8080 *CPU; + // Move HL to PC static void PCHL() { @@ -21,65 +21,74 @@ static inline void jmp() { static inline void call() { // Push PC onto stack - write8((*CPU->SP) - 1, (*CPU->PC) >> 8); // high byte - write8((*CPU->SP) - 2, (*CPU->PC) & 0xff); // low byte + uint8_t h = (*CPU->PC + 2) >> 8; + uint8_t l = (*CPU->PC + 2) & 0xff; + write8((*CPU->SP) - 1, &h); // high byte + write8((*CPU->SP) - 2, &l); // low byte *CPU->SP -= 2; - - // Adjust cycle count to account for the memory operations - CPU->cycles += 6; - jmp(); } // Return helper static inline void ret() { - *CPU->PC = read8((*CPU->SP)) | (read8((*CPU->SP) + 1) << 8); *CPU->SP += 2; - - // Adjust cycle count to account for the memory operations - CPU->cycles += 6; - } // Control Macro -#define CONTROL(PREFIX, DIRECT, METHOD) \ - static void ##DIRECT() {\ +#define CONTROL(PREFIX, DIRECT, METHOD, CYCLES) \ + static void DIRECT() {\ METHOD();\ }\ - static void ##PREFIX##NZ() {\ - if (!(*CPU->F & 64)) METHOD();\ + static void PREFIX##NZ() {\ + if (!(*CPU->F & 64)) {\ + METHOD(); CPU->cycles += CYCLES;\ + }\ }\ - static void ##PREFIX##Z() {\ - if (*CPU->F & 64) METHOD();\ + static void PREFIX##Z() {\ + if (*CPU->F & 64) {\ + METHOD(); CPU->cycles += CYCLES;\ + }\ }\ - static void ##PREFIX##NC() {\ - if (!(*CPU->F & 1)) METHOD();\ + static void PREFIX##NC() {\ + if (!(*CPU->F & 1)) {\ + METHOD(); CPU->cycles += CYCLES;\ + }\ }\ - static void ##PREFIX##C() {\ - if (*CPU->F & 1) METHOD();\ + static void PREFIX##C() {\ + if (*CPU->F & 1) {\ + METHOD(); CPU->cycles += CYCLES;\ + }\ }\ - static void ##PREFIX##PO() {\ - if (!(*CPU->F & 4)) METHOD();\ + static void PREFIX##PO() {\ + if (!(*CPU->F & 4)) {\ + METHOD(); CPU->cycles += CYCLES;\ + }\ }\ - static void ##PREFIX##PE() {\ - if (*CPU->F & 4) METHOD();\ + static void PREFIX##PE() {\ + if (*CPU->F & 4) {\ + METHOD(); CPU->cycles += CYCLES;\ + }\ }\ - static void ##PREFIX##P() {\ - if (!(*CPU->F & 128)) METHOD();\ + static void PREFIX##P() {\ + if (!(*CPU->F & 128)) {\ + METHOD(); CPU->cycles += CYCLES;\ + }\ }\ - static void ##PREFIX##M() {\ - if (*CPU->F & 128) METHOD();\ + static void PREFIX##M() {\ + if (*CPU->F & 128) {\ + METHOD(); CPU->cycles += CYCLES;\ + }\ }\ // Define a ton of stuff... -CONTROL(C, CALL, call); -CONTROL(R, RET, ret); -CONTROL(J, JMP, jmp); +CONTROL(C, CALL, call, 6); +CONTROL(R, RET, ret, 6); +CONTROL(J, JMP, jmp, 0); -// Reset, jump to a fixed address where special stuff may reside +// Restart, jump to a fixed address where special stuff may reside #define RST(ID) static void RST_##ID() { \ write8((*CPU->SP) - 1, (*CPU->PC) >> 8); \ write8((*CPU->SP) - 2, (*CPU->PC) & 0xff); \ diff --git a/ops/data.h b/ops/data.h index bea1de0..e94c340 100644 --- a/ops/data.h +++ b/ops/data.h @@ -9,6 +9,8 @@ #include "math.c" #include "mov.c" #include "reg.c" +#include "stack.c" +#include "ctrl.c" typedef void (*OP_CODE_POINTER)(); @@ -279,6 +281,94 @@ unsigned int OP_CODE_DATA[768] = { 1, 7, (unsigned int)& CMP_M, 1, 4, (unsigned int)& CMP_A, + // C0 - C7 + 1, 5, (unsigned int)& RNZ, + 1, 10, (unsigned int)& POP_B, + 3, 10, (unsigned int)& JNZ, + 3, 10, (unsigned int)& JMP, + + 3, 11, (unsigned int)& CNZ, + 1, 11, (unsigned int)& PUSH_B, + 2, 7, (unsigned int)& ADI, + 1, 11, (unsigned int)& RST_0, + + // C8 - Cf + 1, 5, (unsigned int)& RZ, + 1, 10, (unsigned int)& RET, + 3, 10, (unsigned int)& JZ, + 3, 10, (unsigned int)& JMP, + + 3, 11, (unsigned int)& CZ, + 3, 17, (unsigned int)& CALL, + 2, 7, (unsigned int)& ACI, + 1, 11, (unsigned int)& RST_1, + + // D0 - D7 + 1, 5, (unsigned int)& RNC, + 1, 10, (unsigned int)& POP_D, + 3, 10, (unsigned int)& JNC, + 2, 10, (unsigned int)& OUT, + + 3, 11, (unsigned int)& CNC, + 1, 11, (unsigned int)& PUSH_D, + 2, 7, (unsigned int)& SUI, + 1, 11, (unsigned int)& RST_2, + + // D8 - Df + 1, 5, (unsigned int)& RC, + 1, 10, (unsigned int)& RET, + 3, 10, (unsigned int)& JC, + 2, 10, (unsigned int)& IN, + + 3, 11, (unsigned int)& CC, + 3, 17, (unsigned int)& CALL, + 2, 7, (unsigned int)& SBI, + 1, 11, (unsigned int)& RST_3, + + // E0 - E7 + 1, 5, (unsigned int)& RPO, + 1, 10, (unsigned int)& POP_H, + 3, 10, (unsigned int)& JPO, + 1, 18, (unsigned int)& XTHL, + + 3, 11, (unsigned int)& CPO, + 1, 11, (unsigned int)& PUSH_H, + 2, 7, (unsigned int)& ANI, + 1, 11, (unsigned int)& RST_4, + + // E8 - Ef + 1, 5, (unsigned int)& RPE, + 1, 5, (unsigned int)& PCHL, + 3, 10, (unsigned int)& JPE, + 1, 5, (unsigned int)& XCHG, + + 3, 11, (unsigned int)& CPE, + 3, 17, (unsigned int)& CALL, + 2, 7, (unsigned int)& XRI, + 1, 11, (unsigned int)& RST_5, + + // F0 - F7 + 1, 5, (unsigned int)& RP, + 1, 10, (unsigned int)& POP_PSW, + 3, 10, (unsigned int)& JP, + 1, 4, (unsigned int)& DI, + + 3, 11, (unsigned int)& CP, + 1, 11, (unsigned int)& PUSH_PSW, + 2, 7, (unsigned int)& ORI, + 1, 11, (unsigned int)& RST_6, + + // F8 - Ff + 1, 5, (unsigned int)& RM, + 1, 5, (unsigned int)& SPHL, + 3, 10, (unsigned int)& JM, + 1, 4, (unsigned int)& EI, + + 3, 11, (unsigned int)& CM, + 3, 17, (unsigned int)& CALL, + 2, 7, (unsigned int)& CPI, + 1, 11, (unsigned int)& RST_7 + }; #endif diff --git a/ops/math.c b/ops/math.c index 6dc38f2..b6ca9ba 100644 --- a/ops/math.c +++ b/ops/math.c @@ -1,7 +1,6 @@ // Math Instructions ---------------------------------------------------------- // ---------------------------------------------------------------------------- #include "../cpu.h" -extern CPU_8080 *CPU; // Compare A with REG diff --git a/ops/mov.c b/ops/mov.c index dbe9d23..bf3707d 100644 --- a/ops/mov.c +++ b/ops/mov.c @@ -1,7 +1,6 @@ // Transfer Instructions ------------------------------------------------------ // ---------------------------------------------------------------------------- #include "../cpu.h" -extern CPU_8080 *CPU; // Move FROM to TO diff --git a/ops/names.h b/ops/names.h index 03afff1..56691f5 100644 --- a/ops/names.h +++ b/ops/names.h @@ -269,6 +269,93 @@ const char *OP_CODE_NAMES[256] = { "CMP_M", "CMP_A", + // C0 - C7 + "RNZ", + "POP_B", + "JNZ", + "JMP", + + "CNZ", + "PUSH_B", + "ADI", + "RST_0", + + // C8 - Cf + "RZ", + "RET", + "JZ", + "JMP", + + "CZ", + "CALL", + "ACI", + "RST_1", + + // D0 - D7 + "RNC", + "POP_D", + "JNC", + "OUT", + + "CNC", + "PUSH_D", + "SUI", + "RST_2", + + // D8 - Df + "RC", + "RET", + "JC", + "IN", + + "CC", + "CALL", + "SBI", + "RST_3", + + // E0 - E7 + "RPO", + "POP_H", + "JPO", + "XTHL", + + "CPO", + "PUSH_H", + "ANI", + "RST_4", + + // E8 - Ef + "RPE", + "PCHL", + "JPE", + "XCHG", + + "CPE", + "CALL", + "XRI", + "RST_5", + + // F0 - F7 + "RP", + "POP_PSW", + "JP", + "DI", + + "CP", + "PUSH_PSW", + "ORI", + "RST_6", + + // F8 - Ff + "RM", + "SPHL", + "JM", + "EI", + + "CM", + "CALL", + "CPI", + "RST_7" }; diff --git a/ops/reg.c b/ops/reg.c index 05d48bf..f0cad39 100644 --- a/ops/reg.c +++ b/ops/reg.c @@ -1,7 +1,6 @@ // Other Register Instructions ------------------------------------------------ // ---------------------------------------------------------------------------- #include "../cpu.h" -extern CPU_8080 *CPU; // Increment REG diff --git a/ops/stack.c b/ops/stack.c index b23283e..fb7b6bc 100644 --- a/ops/stack.c +++ b/ops/stack.c @@ -1,7 +1,6 @@ // Stack Instructions --------------------------------------------------------- // ---------------------------------------------------------------------------- #include "../cpu.h" -extern CPU_8080 *CPU; // Push REG onto the Stack