cpu: cleanup to use named registers

Since we have direct access to registers via name by "cpu->a" and the
like. Use that notation in a few more places since it is far more concise.

Signed-off-by: Max Regan <mgregan2@gmail.com>
This commit is contained in:
2017-04-07 16:35:21 -07:00
parent 5bbc6097b5
commit b6f9557d11

View File

@@ -492,11 +492,7 @@ static void _push_stack_16(struct lr35902_state *cpu, uint16_t val)
static uint8_t _pop_stack_8(struct lr35902_state *cpu) static uint8_t _pop_stack_8(struct lr35902_state *cpu)
{ {
uint8_t ret; return cpu->mem_read(cpu, cpu->sp++);
cpu->sp++;
ret = cpu->mem_read(cpu, cpu->sp);
lr35902_set_reg_16(cpu, LR35902_REG_SP, cpu->sp);
return ret;
} }
static uint16_t _pop_stack_16(struct lr35902_state *cpu) static uint16_t _pop_stack_16(struct lr35902_state *cpu)
@@ -597,9 +593,8 @@ static void ld(struct lr35902_state *cpu, uint8_t instr)
static void ld_deref(struct lr35902_state *cpu, uint8_t instr) static void ld_deref(struct lr35902_state *cpu, uint8_t instr)
{ {
uint8_t val;
int incr = 0; int incr = 0;
uint16_t hl_val; uint8_t val;
switch(instr) { switch(instr) {
/** Copy to Registers **/ /** Copy to Registers **/
@@ -613,17 +608,16 @@ static void ld_deref(struct lr35902_state *cpu, uint8_t instr)
incr = 1; incr = 1;
/* Intentional fall-through */ /* Intentional fall-through */
case 0x3A: case 0x3A:
hl_val = lr35902_get_reg_16(cpu, LR35902_REG_HL); val = cpu->mem_read(cpu, cpu->hl);
val = cpu->mem_read(cpu, hl_val); cpu->a = val;
lr35902_set_reg_8(cpu, LR35902_REG_A, val);
if (incr) { if (incr) {
hl_val++; cpu->hl++;
} else { } else {
hl_val--; cpu->hl--;
} }
lr35902_set_reg_16(cpu, LR35902_REG_HL, hl_val); lr35902_set_reg_16(cpu, LR35902_REG_HL, cpu->hl);
break; break;
/** Copy to memory **/ /** Copy to memory **/
@@ -631,17 +625,14 @@ static void ld_deref(struct lr35902_state *cpu, uint8_t instr)
incr = 1; incr = 1;
/* Intentional fall-through */ /* Intentional fall-through */
case 0x32: case 0x32:
hl_val = cpu->hl; cpu->mem_write(cpu, cpu->hl, cpu->a);
val = cpu->a;
cpu->mem_write(cpu, hl_val, val);
if (incr) { if (incr) {
hl_val++; cpu->hl++;
} else { } else {
hl_val--; cpu->hl--;
} }
cpu->hl = hl_val;
break; break;
case 0x12: case 0x12:
cpu->mem_write(cpu, cpu->de, cpu->a); cpu->mem_write(cpu, cpu->de, cpu->a);
@@ -657,7 +648,7 @@ static void ld_deref(struct lr35902_state *cpu, uint8_t instr)
static void ld_into_deref(struct lr35902_state *cpu, uint8_t instr) static void ld_into_deref(struct lr35902_state *cpu, uint8_t instr)
{ {
uint8_t reg, a; uint8_t reg;
uint16_t addr; uint16_t addr;
switch (instr) { switch (instr) {
@@ -669,18 +660,15 @@ static void ld_into_deref(struct lr35902_state *cpu, uint8_t instr)
return; return;
} }
a = lr35902_get_reg_8(cpu, LR35902_REG_A);
addr = lr35902_get_reg_16(cpu, reg); addr = lr35902_get_reg_16(cpu, reg);
cpu->mem_write(cpu, addr, a); cpu->mem_write(cpu, addr, cpu->a);
} }
static void ld_d8(struct lr35902_state *cpu, uint8_t instr) static void ld_d8(struct lr35902_state *cpu, uint8_t instr)
{ {
uint8_t reg = (instr >> 3) & 0x7; uint8_t reg = (instr >> 3) & 0x7;
uint8_t val; ++cpu->pc;
_incr_pc(cpu); _set_reg_8(cpu, reg, cpu->mem_read(cpu, cpu->pc));
val = cpu->mem_read(cpu, lr35902_get_reg_16(cpu, LR35902_REG_PC));
_set_reg_8(cpu, reg, val);
} }
static void ld_d16(struct lr35902_state *cpu, uint8_t instr) static void ld_d16(struct lr35902_state *cpu, uint8_t instr)
@@ -723,22 +711,19 @@ static void ld_a16(struct lr35902_state *cpu, uint8_t instr)
static void ld_c_offset(struct lr35902_state *cpu, uint8_t instr) static void ld_c_offset(struct lr35902_state *cpu, uint8_t instr)
{ {
uint8_t val = 0;
uint16_t addr = 0; uint16_t addr = 0;
addr = lr35902_get_reg_8(cpu, LR35902_REG_C); addr = cpu->c;
addr |= 0xff00; addr |= 0xff00;
switch (instr) { switch (instr) {
case 0xf2: case 0xf2:
//LD A, ($FF00 + C) //LD A, ($FF00 + C)
val = cpu->mem_read(cpu, addr); cpu->a = cpu->mem_read(cpu, addr);
lr35902_set_reg_8(cpu, LR35902_REG_A, val);
break; break;
case 0xe2: case 0xe2:
//LD ($FF00 + C), A //LD ($FF00 + C), A
val = lr35902_get_reg_8(cpu, LR35902_REG_A); cpu->mem_write(cpu, addr, cpu->a);
cpu->mem_write(cpu, addr, val);
break; break;
default: default:
ASSERT(0); ASSERT(0);
@@ -749,12 +734,7 @@ static void ld_c_offset(struct lr35902_state *cpu, uint8_t instr)
static void ld_hl_d16(struct lr35902_state *cpu, uint8_t inst) static void ld_hl_d16(struct lr35902_state *cpu, uint8_t inst)
{ {
uint16_t addr = lr35902_get_reg_16(cpu, LR35902_REG_HL); cpu->a = cpu->mem_read(cpu, cpu->hl++);
uint8_t val = cpu->mem_read(cpu, addr);
_set_reg_8(cpu, LR35902_REG_A, val);
addr++;
lr35902_set_reg_16(cpu, LR35902_REG_HL, addr);
} }