Skip to content

Commit

Permalink
fix some issues and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BonsaiDen committed May 22, 2011
1 parent b7c1c9b commit 8a85929
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 41 deletions.
1 change: 0 additions & 1 deletion ops/acc.c
@@ -1,7 +1,6 @@
// Other Accumulator Instructions ---------------------------------------------
// ----------------------------------------------------------------------------
#include "../cpu.h"
extern CPU_8080 *CPU;


// Load BYTE (WORD) into A
Expand Down
1 change: 0 additions & 1 deletion ops/base.c
@@ -1,7 +1,6 @@
// Basic Instructions ---------------------------------------------------------
// ----------------------------------------------------------------------------
#include "../cpu.h"
extern CPU_8080 *CPU;


// Mo Operation
Expand Down
19 changes: 18 additions & 1 deletion ops/bit.c
@@ -1,7 +1,6 @@
// Bitwise Instructions -------------------------------------------------------
// ----------------------------------------------------------------------------
#include "../cpu.h"
extern CPU_8080 *CPU;


// Rotate left ignoring F carry
Expand Down Expand Up @@ -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() { \
Expand All @@ -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() { \
Expand All @@ -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); \
}

88 changes: 88 additions & 0 deletions ops/codes.h
Expand Up @@ -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
Expand Down
77 changes: 43 additions & 34 deletions ops/ctrl.c
@@ -1,7 +1,7 @@
// Basic Instructions ---------------------------------------------------------
// ----------------------------------------------------------------------------
#include "../cpu.h"
extern CPU_8080 *CPU;


// Move HL to PC
static void PCHL() {
Expand All @@ -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); \
Expand Down
90 changes: 90 additions & 0 deletions ops/data.h
Expand Up @@ -9,6 +9,8 @@
#include "math.c"
#include "mov.c"
#include "reg.c"
#include "stack.c"
#include "ctrl.c"

typedef void (*OP_CODE_POINTER)();

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion ops/math.c
@@ -1,7 +1,6 @@
// Math Instructions ----------------------------------------------------------
// ----------------------------------------------------------------------------
#include "../cpu.h"
extern CPU_8080 *CPU;


// Compare A with REG
Expand Down
1 change: 0 additions & 1 deletion ops/mov.c
@@ -1,7 +1,6 @@
// Transfer Instructions ------------------------------------------------------
// ----------------------------------------------------------------------------
#include "../cpu.h"
extern CPU_8080 *CPU;


// Move FROM to TO
Expand Down

0 comments on commit 8a85929

Please sign in to comment.