diff --git a/execute.go b/execute.go index ae9e300..6f44e79 100644 --- a/execute.go +++ b/execute.go @@ -274,18 +274,20 @@ func pullWord(s *state) uint16 { } -func buildOpPush(reg int) opFunc { - return func(s *state, line []uint8, opcode opcode) { - pushByte(s, s.registers.getRegister(reg)) - } -} - func buildOpPull(reg int) opFunc { return func(s *state, line []uint8, opcode opcode) { s.registers.setRegister(reg, pullByte(s)) } } +func opPHA(s *state, line []uint8, opcode opcode) { + pushByte(s, s.registers.getA()) +} + +func opPHP(s *state, line []uint8, opcode opcode) { + pushByte(s, s.registers.getP()|(flagB+flag5)) +} + func buildOpJump(addressMode int) opFunc { return func(s *state, line []uint8, opcode opcode) { _, address, _ := resolveWithAddressMode(s, line, addressMode) @@ -312,7 +314,7 @@ func opRTS(s *state, line []uint8, opcode opcode) { func opBRK(s *state, line []uint8, opcode opcode) { s.registers.setFlag(flagI) pushWord(s, s.registers.getPC()+1) // TODO: De we have to add 1 or 2? - pushByte(s, s.registers.getP()) + pushByte(s, s.registers.getP()|(flagB+flag5)) s.registers.setPC(s.memory.getWord(0xFFFE)) } @@ -324,8 +326,8 @@ var opcodes = [256]opcode{ 0x40: opcode{"RTI", 1, 6, opRTI}, 0x60: opcode{"RTS", 1, 6, opRTS}, - 0x48: opcode{"PHA", 1, 3, buildOpPush(regA)}, - 0x08: opcode{"PHP", 1, 3, buildOpPush(regP)}, + 0x48: opcode{"PHA", 1, 3, opPHA}, + 0x08: opcode{"PHP", 1, 3, opPHP}, 0x68: opcode{"PLA", 1, 4, buildOpPull(regA)}, 0x28: opcode{"PLP", 1, 4, buildOpPull(regP)}, diff --git a/execute_test.go b/execute_test.go index fcce26d..03fe4ea 100644 --- a/execute_test.go +++ b/execute_test.go @@ -427,7 +427,7 @@ func TestStack(t *testing.T) { if s.registers.getSP() != 0xEE { t.Errorf("Error in PHP stack pointer, %v", s.registers) } - if s.memory[0x01EF] != 0x0A { + if s.memory[0x01EF] != 0x3A { t.Errorf("Error in PHP, %v", s.registers) } @@ -435,7 +435,7 @@ func TestStack(t *testing.T) { if s.registers.getSP() != 0xEF { t.Errorf("Error in PLA stack pointer, %v", s.registers) } - if s.registers.getA() != 0x0A { + if s.registers.getA() != 0x3A { t.Errorf("Error in PLA, %v", s.registers) } @@ -447,3 +447,5 @@ func TestStack(t *testing.T) { t.Errorf("Error in PLP, %v", s.registers) } } + +// TODO: Tests for BRK, JMP, JSR, RTI, RTS diff --git a/registers.go b/registers.go index f0bd4f6..7c0587c 100644 --- a/registers.go +++ b/registers.go @@ -15,6 +15,7 @@ const ( const ( flagN uint8 = 1 << 7 flagV uint8 = 1 << 6 + flag5 uint8 = 1 << 5 flagB uint8 = 1 << 4 flagD uint8 = 1 << 3 flagI uint8 = 1 << 2