Skip to content

Commit

Permalink
Add RTS instruction and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Eggbertx committed Apr 5, 2024
1 parent 33013f6 commit 1e8f007
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion addons/GD6502
Submodule GD6502 updated 2 files
+24 −17 CPU.gd
+1 −1 README.md
62 changes: 56 additions & 6 deletions test/unit/opcodes/test_stack.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,50 @@ rts
ldy #$1
"""

const rts_stack_empty_str := """
jsr lbl
rts
lbl:
rts
"""

var jsr_assembled := PackedByteArray([
0x20, 0x04, 0x06, 0x00, 00
0x20, 0x04, 0x06, 0x00, 0x00
])

var jsr_stack_filled_assembled := PackedByteArray([
0x20, 0x00, 0x06
])

var rts_assembled := PackedByteArray([
0x20, 0x05, 0x06, 0xa9, 0x01, 0xa2, 0x01, 0x60, 0xa0, 0x01
])

var rts_stack_empty_assembled := PackedByteArray([
0x20, 0x04, 0x06, 0x60, 0x60
])

var asm := Assembler.new()
var cpu := CPU.new()
var filled := false
var emptied := false

func on_filled():
filled = true

func on_emptied():
emptied = true

func before():
auto_free(asm)
auto_free(cpu)
cpu.stack_filled.connect(on_filled)
cpu.stack_emptied.connect(on_emptied)

func before_test():
filled = false
emptied = false
cpu.unload_rom()
cpu.reset()

Expand All @@ -56,20 +77,49 @@ func test_jsr():
assert_int(asm.assemble()).is_equal(OK)
assert_int(asm.assembled.size()).is_equal(jsr_assembled.size())
assert_array(asm.assembled).is_equal(jsr_assembled)
cpu.load_rom(jsr_assembled)
cpu.load_rom(asm.assembled)
cpu.step()
assert_int(cpu.memory[0x1ff]).is_equal(0x06)
assert_int(cpu.memory[0x1fe]).is_equal(0x05)
assert_int(cpu.memory[0x1fe]).is_equal(0x02)
assert_int(cpu.PC).is_equal(0x604)

func test_jsr_wrap():
asm.asm_str = jsr_stack_filled_str
assert_int(asm.assemble()).is_equal(OK)
assert_int(asm.assembled.size()).is_equal(jsr_stack_filled_assembled.size())
assert_array(asm.assembled).is_equal(jsr_stack_filled_assembled)
cpu.load_rom(jsr_stack_filled_assembled)
cpu.load_rom(asm.assembled)

cpu.step(128)
cpu.step(127)
assert_bool(filled).is_false()
cpu.step(1)
cpu.step()
assert_bool(filled).is_true()

func test_rts():
asm.asm_str = rts_str
assert_int(asm.assemble()).is_equal(OK)
assert_int(asm.assembled.size()).is_equal(rts_assembled.size())
assert_array(asm.assembled).is_equal(rts_assembled)
cpu.load_rom(asm.assembled)
cpu.step()
assert_int(cpu.PC).is_equal(0x605)
cpu.step()
assert_int(cpu.A).is_equal(0)
assert_int(cpu.X).is_equal(1)
cpu.step()
assert_int(cpu.PC).is_equal(0x603)
assert_int(cpu.memory[0x1ff]).is_equal(0x06)
assert_int(cpu.memory[0x1fe]).is_equal(0x02)

func test_rts_empty():
asm.asm_str = rts_stack_empty_str
assert_int(asm.assemble()).is_equal(OK)
assert_int(asm.assembled.size()).is_equal(rts_stack_empty_assembled.size())
assert_array(asm.assembled).is_equal(rts_stack_empty_assembled)
cpu.load_rom(asm.assembled)
cpu.step()
assert_int(cpu.PC).is_equal(0x604)
cpu.step()
assert_bool(emptied).is_false()
cpu.step()
assert_bool(emptied).is_true()

0 comments on commit 1e8f007

Please sign in to comment.