From 7b1cb65eefcd2ce1a734d33356a7c0683fa96dde Mon Sep 17 00:00:00 2001 From: Ivan Izaguirre Date: Wed, 30 Jan 2019 20:10:39 +0100 Subject: [PATCH] Support for STA, STX, STY --- execute.go | 28 ++++++++++++++++++++++++---- execute_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/execute.go b/execute.go index de56ede..fce6e89 100644 --- a/execute.go +++ b/execute.go @@ -152,6 +152,14 @@ func buildOpLoad(addressMode int, regDst int) opFunc { } } +func buildOpStore(addressMode int, regSrc int) opFunc { + return func(s *state, line []uint8, opcode opcode) { + _, setValue := resolveWithAddressMode(s, line, addressMode) + value := s.registers.getRegister(regSrc) + setValue(value) + } +} + func buildOpUpdateFlag(flag uint8, value bool) opFunc { return func(s *state, line []uint8, opcode opcode) { s.registers.updateFlag(flag, value) @@ -199,10 +207,6 @@ PHP PLA PLP -STA -STX -STY - */ var opcodes = [256]opcode{ @@ -270,6 +274,22 @@ var opcodes = [256]opcode{ 0xAC: opcode{"LDY", 3, 4, buildOpLoad(modeAbsolute, regY)}, 0xBC: opcode{"LDY", 3, 4, buildOpLoad(modeAbsoluteX, regY)}, // Extra cycles + 0x85: opcode{"STA", 2, 3, buildOpStore(modeZeroPage, regA)}, + 0x95: opcode{"STA", 2, 4, buildOpStore(modeZeroPageX, regA)}, + 0x8D: opcode{"STA", 3, 4, buildOpStore(modeAbsolute, regA)}, + 0x9D: opcode{"STA", 3, 5, buildOpStore(modeAbsoluteX, regA)}, + 0x99: opcode{"STA", 3, 5, buildOpStore(modeAbsoluteY, regA)}, + 0x81: opcode{"STA", 2, 6, buildOpStore(modeIndexedIndirectX, regA)}, + 0x91: opcode{"STA", 2, 6, buildOpStore(modeIndirectIndexedY, regA)}, + + 0x86: opcode{"STX", 2, 3, buildOpStore(modeZeroPage, regX)}, + 0x96: opcode{"STX", 2, 4, buildOpStore(modeZeroPageY, regX)}, + 0x8E: opcode{"STX", 3, 4, buildOpStore(modeAbsolute, regX)}, + + 0x84: opcode{"STY", 2, 3, buildOpStore(modeZeroPage, regY)}, + 0x94: opcode{"STY", 2, 4, buildOpStore(modeZeroPageX, regY)}, + 0x8C: opcode{"STY", 3, 4, buildOpStore(modeAbsolute, regY)}, + 0x90: opcode{"BCC", 2, 2, buildOpBranch(flagC, false)}, // Extra cycles 0xB0: opcode{"BCS", 2, 2, buildOpBranch(flagC, true)}, // Extra cycles 0xD0: opcode{"BNE", 2, 2, buildOpBranch(flagZ, false)}, // Extra cycles diff --git a/execute_test.go b/execute_test.go index ac44d95..7134d67 100644 --- a/execute_test.go +++ b/execute_test.go @@ -86,6 +86,38 @@ func TestLoad(t *testing.T) { } } +func TestStore(t *testing.T) { + var s state + s.registers.setA(0x10) + s.registers.setX(0x40) + s.registers.setY(0x80) + + executeLine(&s, []uint8{0x85, 0x50}) + if s.memory[0x0050] != 0x10 { + t.Error("Error in STA zpg") + } + + executeLine(&s, []uint8{0x86, 0x51}) + if s.memory[0x0051] != 0x40 { + t.Error("Error in STX zpg") + } + + executeLine(&s, []uint8{0x84, 0x52}) + if s.memory[0x0052] != 0x80 { + t.Error("Error in STY zpg") + } + + executeLine(&s, []uint8{0x8D, 0x20, 0xC0}) + if s.memory[0xC020] != 0x10 { + t.Error("Error in STA abs") + } + + executeLine(&s, []uint8{0x9D, 0x08, 0x10}) + if s.memory[0x1048] != 0x10 { + t.Error("Error in STA abs, X") + } +} + func TestTransfer(t *testing.T) { var s state