From 1efe2b579d1c78a5b8050feca39d7d12d69731eb Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 24 Apr 2016 01:57:09 -0500 Subject: [PATCH] Add POP; fix bugs involving relative jumps and PUSH. --- src/z80.c | 10 ++++++++++ src/z80_ops.inc.c | 18 ++++++++++++++---- src/z80_tables.inc.c | 8 ++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/z80.c b/src/z80.c index 88e159f..5f76e7b 100644 --- a/src/z80.c +++ b/src/z80.c @@ -170,6 +170,16 @@ static inline void stack_push(Z80 *z80, uint16_t value) } /* + Pop a two-byte value from the stack. +*/ +static inline uint16_t stack_pop(Z80 *z80) +{ + uint16_t value = mmu_read_double(z80->mmu, z80->regfile.sp); + z80->regfile.sp += 2; + return value; +} + +/* Read and return a byte from the given port. */ static uint8_t read_port(Z80 *z80, uint8_t port) diff --git a/src/z80_ops.inc.c b/src/z80_ops.inc.c index 17aa15c..bebcc1d 100644 --- a/src/z80_ops.inc.c +++ b/src/z80_ops.inc.c @@ -225,7 +225,7 @@ static uint8_t z80_inst_ld_dd_nn(Z80 *z80, uint8_t opcode) static uint8_t z80_inst_push_qq(Z80 *z80, uint8_t opcode) { uint8_t pair = extract_pair(opcode); - stack_push(z80, pair); + stack_push(z80, get_pair(z80, pair)); z80->regfile.pc++; return 11; } @@ -234,7 +234,17 @@ static uint8_t z80_inst_push_qq(Z80 *z80, uint8_t opcode) // PUSH IY -// POP qq +/* + POP qq (0xC1, 0xD1, 0xE1, 0xF1): + Pop qq from the stack, and increment SP by two. +*/ +static uint8_t z80_inst_pop_qq(Z80 *z80, uint8_t opcode) +{ + uint8_t pair = extract_pair(opcode); + set_pair(z80, pair, stack_pop(z80)); + z80->regfile.pc++; + return 10; +} // POP IX @@ -594,7 +604,7 @@ static uint8_t z80_inst_jp_cc_nn(Z80 *z80, uint8_t opcode) static uint8_t z80_inst_jr_e(Z80 *z80, uint8_t opcode) { (void) opcode; - int8_t jump = mmu_read_byte(z80->mmu, ++z80->regfile.pc); + int8_t jump = mmu_read_byte(z80->mmu, z80->regfile.pc + 1); z80->regfile.pc += jump + 2; return 12; } @@ -607,7 +617,7 @@ static uint8_t z80_inst_jr_e(Z80 *z80, uint8_t opcode) static uint8_t z80_inst_jr_cc_e(Z80 *z80, uint8_t opcode) { if (extract_cond(z80, opcode - 0x20)) { - int8_t jump = mmu_read_byte(z80->mmu, ++z80->regfile.pc); + int8_t jump = mmu_read_byte(z80->mmu, z80->regfile.pc + 1); z80->regfile.pc += jump + 2; return 12; } else { diff --git a/src/z80_tables.inc.c b/src/z80_tables.inc.c index 5425a81..1798387 100644 --- a/src/z80_tables.inc.c +++ b/src/z80_tables.inc.c @@ -195,7 +195,7 @@ static DispatchTable instruction_table = { [0xBE] = z80_inst_unimplemented, // TODO [0xBF] = z80_inst_cp_r, [0xC0] = z80_inst_unimplemented, // TODO - [0xC1] = z80_inst_unimplemented, // TODO + [0xC1] = z80_inst_pop_qq, [0xC2] = z80_inst_jp_cc_nn, [0xC3] = z80_inst_jp_nn, [0xC4] = z80_inst_call_cc_nn, @@ -211,7 +211,7 @@ static DispatchTable instruction_table = { [0xCE] = z80_inst_unimplemented, // TODO [0xCF] = z80_inst_unimplemented, // TODO [0xD0] = z80_inst_unimplemented, // TODO - [0xD1] = z80_inst_unimplemented, // TODO + [0xD1] = z80_inst_pop_qq, [0xD2] = z80_inst_jp_cc_nn, [0xD3] = z80_inst_out_n_a, [0xD4] = z80_inst_call_cc_nn, @@ -227,7 +227,7 @@ static DispatchTable instruction_table = { [0xDE] = z80_inst_unimplemented, // TODO [0xDF] = z80_inst_unimplemented, // TODO [0xE0] = z80_inst_unimplemented, // TODO - [0xE1] = z80_inst_unimplemented, // TODO + [0xE1] = z80_inst_pop_qq, [0xE2] = z80_inst_jp_cc_nn, [0xE3] = z80_inst_unimplemented, // TODO [0xE4] = z80_inst_call_cc_nn, @@ -243,7 +243,7 @@ static DispatchTable instruction_table = { [0xEE] = z80_inst_unimplemented, // TODO [0xEF] = z80_inst_unimplemented, // TODO [0xF0] = z80_inst_unimplemented, // TODO - [0xF1] = z80_inst_unimplemented, // TODO + [0xF1] = z80_inst_pop_qq, [0xF2] = z80_inst_jp_cc_nn, [0xF3] = z80_inst_di, [0xF4] = z80_inst_call_cc_nn,