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)
{
uint8_t ret;
cpu->sp++;
ret = cpu->mem_read(cpu, cpu->sp);
lr35902_set_reg_16(cpu, LR35902_REG_SP, cpu->sp);
return ret;
return cpu->mem_read(cpu, cpu->sp++);
}
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)
{
uint8_t val;
int incr = 0;
uint16_t hl_val;
uint8_t val;
switch(instr) {
/** Copy to Registers **/
@@ -613,17 +608,16 @@ static void ld_deref(struct lr35902_state *cpu, uint8_t instr)
incr = 1;
/* Intentional fall-through */
case 0x3A:
hl_val = lr35902_get_reg_16(cpu, LR35902_REG_HL);
val = cpu->mem_read(cpu, hl_val);
lr35902_set_reg_8(cpu, LR35902_REG_A, val);
val = cpu->mem_read(cpu, cpu->hl);
cpu->a = val;
if (incr) {
hl_val++;
cpu->hl++;
} 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;
/** Copy to memory **/
@@ -631,17 +625,14 @@ static void ld_deref(struct lr35902_state *cpu, uint8_t instr)
incr = 1;
/* Intentional fall-through */
case 0x32:
hl_val = cpu->hl;
val = cpu->a;
cpu->mem_write(cpu, hl_val, val);
cpu->mem_write(cpu, cpu->hl, cpu->a);
if (incr) {
hl_val++;
cpu->hl++;
} else {
hl_val--;
cpu->hl--;
}
cpu->hl = hl_val;
break;
case 0x12:
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)
{
uint8_t reg, a;
uint8_t reg;
uint16_t addr;
switch (instr) {
@@ -669,18 +660,15 @@ static void ld_into_deref(struct lr35902_state *cpu, uint8_t instr)
return;
}
a = lr35902_get_reg_8(cpu, LR35902_REG_A);
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)
{
uint8_t reg = (instr >> 3) & 0x7;
uint8_t val;
_incr_pc(cpu);
val = cpu->mem_read(cpu, lr35902_get_reg_16(cpu, LR35902_REG_PC));
_set_reg_8(cpu, reg, val);
++cpu->pc;
_set_reg_8(cpu, reg, cpu->mem_read(cpu, cpu->pc));
}
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)
{
uint8_t val = 0;
uint16_t addr = 0;
addr = lr35902_get_reg_8(cpu, LR35902_REG_C);
addr = cpu->c;
addr |= 0xff00;
switch (instr) {
case 0xf2:
//LD A, ($FF00 + C)
val = cpu->mem_read(cpu, addr);
lr35902_set_reg_8(cpu, LR35902_REG_A, val);
cpu->a = cpu->mem_read(cpu, addr);
break;
case 0xe2:
//LD ($FF00 + C), A
val = lr35902_get_reg_8(cpu, LR35902_REG_A);
cpu->mem_write(cpu, addr, val);
cpu->mem_write(cpu, addr, cpu->a);
break;
default:
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)
{
uint16_t addr = lr35902_get_reg_16(cpu, LR35902_REG_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);
cpu->a = cpu->mem_read(cpu, cpu->hl++);
}