From 915846b9898f74308509346ba94db0dd2ba1de45 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 13 Apr 2024 22:46:53 +0300 Subject: Add memcopy, add test --- atk16_asm/builtin_asm/std_mem.atk16 | 23 +++++++++++++++++++ test/e2e/std_mem/run_memcopy.atk16 | 23 +++++++++++++++++++ test/e2e/std_mem/test_memset.py | 22 ------------------ test/e2e/std_mem/test_std_mem.py | 45 +++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 test/e2e/std_mem/run_memcopy.atk16 delete mode 100644 test/e2e/std_mem/test_memset.py create mode 100644 test/e2e/std_mem/test_std_mem.py diff --git a/atk16_asm/builtin_asm/std_mem.atk16 b/atk16_asm/builtin_asm/std_mem.atk16 index 7a7efab..43d00ea 100644 --- a/atk16_asm/builtin_asm/std_mem.atk16 +++ b/atk16_asm/builtin_asm/std_mem.atk16 @@ -18,3 +18,26 @@ @label memset_done stack_restore RD RE return + +@label memcopy + ; copies a specified number of words from one location in memory to another + ; parameters: RA = src (source address) + ; RB = dest (destination address) + ; RC = n (number of words to copy) + ; returns: void + stack_stash RD RE RF + ldi 0 RD ; RD := i (counter), initially 0 +@label memcopy_loop + sub RC RD RE ; RE := n – i (unused value) + bri zero memcopy_done ; exit the loop + + add RA RD RE ; RE := src + i (address of the word to copy) + ldr RE RE ; RE := the word at address RE + add RB RD RF ; RF := dest + i (address of the word to copy to) + str RE RF ; set the word at address RF to the word in RE + + inc RD ; i := i + 1 + jpi memcopy_loop ; repeat the loop +@label memcopy_done + stack_restore RD RE RF + return diff --git a/test/e2e/std_mem/run_memcopy.atk16 b/test/e2e/std_mem/run_memcopy.atk16 new file mode 100644 index 0000000..66f3c1d --- /dev/null +++ b/test/e2e/std_mem/run_memcopy.atk16 @@ -0,0 +1,23 @@ +@include %bootstrap +@include %std_mem + +@label mem_offset_from + 0x9000 +@label mem_offset_to + 0x9100 + +@label main + ; set values 0x0000..0x0004 to 3 + ldi mem_offset_from RA + ldr RA RA + ldi 5 RB + ldi 3 RC + calli memset + hlt + + ldi mem_offset_to RB + ldr RB RB + ldi 5 RC + calli memcopy + + hlt diff --git a/test/e2e/std_mem/test_memset.py b/test/e2e/std_mem/test_memset.py deleted file mode 100644 index 0052ffe..0000000 --- a/test/e2e/std_mem/test_memset.py +++ /dev/null @@ -1,22 +0,0 @@ -from atk16_asm import assemble -from atk16_emu import Machine -from test.utils import pad_bytearray - -def test_memset(): - filename = "test/e2e/std_mem/run_memset.atk16" - with open(filename, "r") as f: - source = f.read() - - obj = assemble(source, filename) - rom_image = pad_bytearray(obj.program) - - machine = Machine() - machine.load_rom_image(rom_image) - machine.reset() - machine.run_until_halted() - machine.print_state_summary() - - addr = 0x9000 - 0x8000 - expected_n = 5 - expected_value = 3 - assert all(map(lambda x: x == expected_value, machine.ram.memory[addr:addr+expected_n])) diff --git a/test/e2e/std_mem/test_std_mem.py b/test/e2e/std_mem/test_std_mem.py new file mode 100644 index 0000000..f45ca5b --- /dev/null +++ b/test/e2e/std_mem/test_std_mem.py @@ -0,0 +1,45 @@ +from atk16_asm import assemble +from atk16_emu import Machine +from test.utils import pad_bytearray + +def test_memset(): + filename = "test/e2e/std_mem/run_memset.atk16" + with open(filename, "r") as f: + source = f.read() + + obj = assemble(source, filename) + rom_image = pad_bytearray(obj.program) + + machine = Machine() + machine.load_rom_image(rom_image) + machine.reset() + machine.run_until_halted() + machine.print_state_summary() + + addr = 0x9000 - 0x8000 + n = 5 + value = 3 + assert all([x == value for x in machine.ram.memory[addr:addr+n]]) + +def test_memcopy(): + filename = "test/e2e/std_mem/run_memcopy.atk16" + with open(filename, "r") as f: + source = f.read() + + obj = assemble(source, filename) + rom_image = pad_bytearray(obj.program) + + machine = Machine() + machine.load_rom_image(rom_image) + machine.reset() + machine.run_until_halted() + machine.print_state_summary() + + addr_from = 0x9000 - 0x8000 + addr_to = 0x9100 - 0x8000 + n = 5 + + mem_range_from = machine.ram.memory[addr_from:addr_from+n] + mem_range_to = machine.ram.memory[addr_to:addr_to+n] + + assert [mem_range_from[i] == mem_range_to[i] for i in range(n)] -- cgit v1.3