Skip to content

Commit

Permalink
Merge pull request #9 from koron-go/push-pop-optimization
Browse files Browse the repository at this point in the history
unified push/pop operation functions and optimized
  • Loading branch information
koron authored Apr 16, 2024
2 parents fd6a300 + c405a16 commit 5a96805
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 54 deletions.
66 changes: 20 additions & 46 deletions op_load16.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,52 +76,26 @@ func oopPOPIY(cpu *CPU) {
//////////////////////////////////////////////////////////////////////////////
// eXpanded OPration codes

func xopPUSHbc(cpu *CPU) {
cpu.SP -= 2
cpu.Memory.Set(cpu.SP, cpu.BC.Lo)
cpu.Memory.Set(cpu.SP+1, cpu.BC.Hi)
}

func xopPUSHde(cpu *CPU) {
cpu.SP -= 2
cpu.Memory.Set(cpu.SP, cpu.DE.Lo)
cpu.Memory.Set(cpu.SP+1, cpu.DE.Hi)
}

func xopPUSHhl(cpu *CPU) {
cpu.SP -= 2
cpu.Memory.Set(cpu.SP, cpu.HL.Lo)
cpu.Memory.Set(cpu.SP+1, cpu.HL.Hi)
}

func xopPUSHaf(cpu *CPU) {
cpu.SP -= 2
cpu.Memory.Set(cpu.SP, cpu.AF.Lo)
cpu.Memory.Set(cpu.SP+1, cpu.AF.Hi)
}

func xopPOPbc(cpu *CPU) {
cpu.BC.Lo = cpu.Memory.Get(cpu.SP)
cpu.BC.Hi = cpu.Memory.Get(cpu.SP + 1)
cpu.SP += 2
}

func xopPOPde(cpu *CPU) {
cpu.DE.Lo = cpu.Memory.Get(cpu.SP)
cpu.DE.Hi = cpu.Memory.Get(cpu.SP + 1)
cpu.SP += 2
}

func xopPOPhl(cpu *CPU) {
cpu.HL.Lo = cpu.Memory.Get(cpu.SP)
cpu.HL.Hi = cpu.Memory.Get(cpu.SP + 1)
cpu.SP += 2
}

func xopPOPaf(cpu *CPU) {
cpu.AF.Lo = cpu.Memory.Get(cpu.SP)
cpu.AF.Hi = cpu.Memory.Get(cpu.SP + 1)
cpu.SP += 2
func xopPUSHreg(cpu *CPU, reg Register) {
// Code before optimization
//cpu.SP -= 2
//cpu.Memory.Set(cpu.SP, reg.Lo)
//cpu.Memory.Set(cpu.SP+1, reg.Hi)
cpu.SP--
cpu.Memory.Set(cpu.SP, reg.Hi)
cpu.SP--
cpu.Memory.Set(cpu.SP, reg.Lo)
}

func xopPOPreg(cpu *CPU, reg *Register) {
// Code before optimization
//reg.Lo = cpu.Memory.Get(cpu.SP)
//reg.Hi = cpu.Memory.Get(cpu.SP + 1)
//cpu.SP += 2
reg.Lo = cpu.Memory.Get(cpu.SP)
cpu.SP++
reg.Hi = cpu.Memory.Get(cpu.SP)
cpu.SP++
}

func xopLDbcnn(cpu *CPU, l, h uint8) {
Expand Down
16 changes: 8 additions & 8 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,13 @@ func (cpu *CPU) executeOne(f fetcher) {
xopRETfS(cpu)

case 0xc1:
xopPOPbc(cpu)
xopPOPreg(cpu, &cpu.BC)
case 0xd1:
xopPOPde(cpu)
xopPOPreg(cpu, &cpu.DE)
case 0xe1:
xopPOPhl(cpu)
xopPOPreg(cpu, &cpu.HL)
case 0xf1:
xopPOPaf(cpu)
xopPOPreg(cpu, &cpu.AF)

case 0xc2:
l := f.fetch()
Expand Down Expand Up @@ -588,13 +588,13 @@ func (cpu *CPU) executeOne(f fetcher) {
xopCALLfSnn(cpu, l, h)

case 0xc5:
xopPUSHbc(cpu)
xopPUSHreg(cpu, cpu.BC)
case 0xd5:
xopPUSHde(cpu)
xopPUSHreg(cpu, cpu.DE)
case 0xe5:
xopPUSHhl(cpu)
xopPUSHreg(cpu, cpu.HL)
case 0xf5:
xopPUSHaf(cpu)
xopPUSHreg(cpu, cpu.AF)

case 0xc6:
n := f.fetch()
Expand Down

0 comments on commit 5a96805

Please sign in to comment.