From 3963742d5cc195bbbcc8081ec261b0d3e0c1d83d Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 11 Apr 2024 17:50:10 +0100 Subject: Add instr/s counter --- atk16_emu/emu.py | 3 + resources/asm/aoc23_1a.atk16 | 554 ------------------------------------------- resources/asm/aoc23_1a.py | 48 ---- test/e2e/fibo/fibo.atk16 | 8 +- test/e2e/fibo/test_fibo.py | 17 +- 5 files changed, 25 insertions(+), 605 deletions(-) delete mode 100644 resources/asm/aoc23_1a.atk16 delete mode 100644 resources/asm/aoc23_1a.py diff --git a/atk16_emu/emu.py b/atk16_emu/emu.py index 1d8fd1e..4257636 100644 --- a/atk16_emu/emu.py +++ b/atk16_emu/emu.py @@ -177,6 +177,7 @@ class Machine: sign = False, ) + self.steps_taken = 0 self.running = False def make_copy(self): @@ -265,6 +266,7 @@ class Machine: def reset(self): self.pc.reset() self.running = False + self.steps_taken = 0 def run(self): "Set running = True." @@ -289,6 +291,7 @@ class Machine: if not self.running: raise RuntimeError("Machine is not running") + self.steps_taken += 1 pc_addr = self.pc.value self.pc.step() diff --git a/resources/asm/aoc23_1a.atk16 b/resources/asm/aoc23_1a.atk16 deleted file mode 100644 index 78c4622..0000000 --- a/resources/asm/aoc23_1a.atk16 +++ /dev/null @@ -1,554 +0,0 @@ -;; BEGIN BOOTSTRAP - -@use ext_std:* - -@let sp RH ; stack pointer (points to empty slot at top of stack) -@let fp RG ; frame pointer (points to base of currently active frame) - -@let vector_table 0x10 -@let vt_ISR0 0x10 ; ISR0 -@let vt_ISR1 0x11 ; ISR1 -@let vt_ISR2 0x12 ; ISR2 -@let vt_ISR3 0x13 ; ISR3 -@let vt_stack_addr 0x14 ; Stack address -@let vt_term_pp_addr 0x15 ; Terminal peripheral address -@let vt_kb_pp_addr 0x16 ; Keyboard peripheral address -@let vt_gr_mode_addr 0x17 ; Graphics mode setting address -@let vt_sprite_mem 0x18 ; Sprite memory address -@let vt_text_mem 0x19 ; Text memory buffer address - -@let stack_segment 0x8000 -@let mmio_segment 0xE000 -@let terminal_addr 0xE000 -@let keyboard_addr 0xE001 -@let gr_mode_addr 0xE002 -@let sprite_mem 0xE800 -@let text_mem 0xF800 - -@let gr_disabled_mode 0b00 -@let gr_text_mode 0b01 -@let gr_sprite_mode 0b10 - -@address 0x0 - ldi vt_stack_addr RA - ldr RA SP - mov SP FP - jpi program_segment - -@address vector_table - keyboard_isr ; 0x10 - hlt_isr ; 0x11 - hlt_isr ; 0x12 - hlt_isr ; 0x13 - stack_segment ; 0x14 - terminal_addr ; 0x15 - keyboard_addr ; 0x16 - gr_mode_addr ; 0x17 - sprite_mem ; 0x18 - text_mem ; 0x19 - -@label hlt_isr - hlt - -@label keyboard_isr - spu RA - spu RB - ldi vt_kb_pp_addr RA - ldr RA RA - ldr RA RA - ldi vt_term_pp_addr RB - ldr RB RB - str RA RB - spo RB - spo RA - rti - -@label program_segment - ldi vt_gr_mode_addr RA - ldr RA RA - ldi gr_disabled_mode RB - str RB RA - - jpi main - -;; END BOOTSTRAP - -@label int_0 - 8 -@label graphics_no_mode - 0 -@label graphics_text_mode - 1 -@label graphics_sprite_mode - 2 -@label graphics_mode_pp - 23 -@label text_mem_pp - 25 -@label input_p - 36864 -@label input_elem_size - 256 -@label int_1 - 49 -@label int_2 - 97 -@label int_3 - 98 -@label int_4 - 99 -@label int_5 - 50 - - - -@label main -; stack frame with offsets -; graphics_mode_p 0 -; text_mem_p 1 -; row_p 2 -; i 3 -; first 4 -; last 5 -; char_p 6 -; char 7 - ldi int_0 RA - ldr RA RA - add SP RA SP -; -; assigning graphics_mode_p at FP + 0 -; evaluating value to be assigned -; -; Builtin call load [] -; (GRAPHICS_MODE_PP) - ldi graphics_mode_pp RC - ldr RC RC - spu RC -; (load) dereferencing top of stack - spo RC - ldr RC RC - spu RC -; assigning stack address (FP + 0) := top of stack - spo RA - ldi 0 RB - add RB FP RB - str RA RB -; -; assigning text_mem_p at FP + 1 -; evaluating value to be assigned -; -; Builtin call load [] -; (TEXT_MEM_PP) - ldi text_mem_pp RC - ldr RC RC - spu RC -; (load) dereferencing top of stack - spo RC - ldr RC RC - spu RC -; assigning stack address (FP + 1) := top of stack - spo RA - ldi 1 RB - add RB FP RB - str RA RB -; -; Builtin call store [, ] -; (graphics_mode_p) - ldi 0 RA - add FP RA RA - ldr RA RA - spu RA -; (GRAPHICS_TEXT_MODE) - ldi graphics_text_mode RA - ldr RA RA - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA -; -; Builtin call store [, ] -; -; BinOp lhs -; (INPUT_P) - ldi input_p RA - ldr RA RA - spu RA -; BinOp rhs -; - ldi 0 RA - spu RA - spo RB - spo RA - add RA RB RA - spu RA -; - ldi int_1 RA - ldr RA RA ; int_1 = 49 - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA -; -; Builtin call store [, ] -; -; BinOp lhs -; (INPUT_P) - ldi input_p RA - ldr RA RA - spu RA -; BinOp rhs -; - ldi 1 RA - spu RA - spo RB - spo RA - add RA RB RA - spu RA -; - ldi int_2 RA - ldr RA RA ; int_2 = 97 - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA -; -; Builtin call store [, ] -; -; BinOp lhs -; (INPUT_P) - ldi input_p RA - ldr RA RA - spu RA -; BinOp rhs -; - ldi 2 RA - spu RA - spo RB - spo RA - add RA RB RA - spu RA -; - ldi int_3 RA - ldr RA RA ; int_3 = 98 - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA -; -; Builtin call store [, ] -; -; BinOp lhs -; (INPUT_P) - ldi input_p RA - ldr RA RA - spu RA -; BinOp rhs -; - ldi 3 RA - spu RA - spo RB - spo RA - add RA RB RA - spu RA -; - ldi int_4 RA - ldr RA RA ; int_4 = 99 - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA -; -; Builtin call store [, ] -; -; BinOp lhs -; (INPUT_P) - ldi input_p RA - ldr RA RA - spu RA -; BinOp rhs -; - ldi 4 RA - spu RA - spo RB - spo RA - add RA RB RA - spu RA -; - ldi int_5 RA - ldr RA RA ; int_5 = 50 - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA -; -; Builtin call store [, ] -; -; BinOp lhs -; (INPUT_P) - ldi input_p RA - ldr RA RA - spu RA -; BinOp rhs -; - ldi 5 RA - spu RA - spo RB - spo RA - add RA RB RA - spu RA -; - ldi 0 RA - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA -; -; assigning row_p at FP + 2 -; evaluating value to be assigned -; (INPUT_P) - ldi input_p RC - ldr RC RC - spu RC -; assigning stack address (FP + 2) := top of stack - spo RA - ldi 2 RB - add RB FP RB - str RA RB -; -; assigning i at FP + 3 -; evaluating value to be assigned -; - ldi 0 RC - spu RC -; assigning stack address (FP + 3) := top of stack - spo RA - ldi 3 RB - add RB FP RB - str RA RB -; -; assigning first at FP + 4 -; evaluating value to be assigned -; -; Builtin call load [] -; (row_p) - ldi 2 RC - add FP RC RC - ldr RC RC - spu RC -; (load) dereferencing top of stack - spo RC - ldr RC RC - spu RC -; assigning stack address (FP + 4) := top of stack - spo RA - ldi 4 RB - add RB FP RB - str RA RB -; -; assigning last at FP + 5 -; evaluating value to be assigned -; - ldi 0 RC - spu RC -; assigning stack address (FP + 5) := top of stack - spo RA - ldi 5 RB - add RB FP RB - str RA RB -; -@label while_test_6 -; - ldi 1 RB - spu RB - spo RA - addi RA 0 RA - bri zero While_else_7 -; -; assigning char_p at FP + 6 -; evaluating value to be assigned -; -; BinOp lhs -; (INPUT_P) - ldi input_p RC - ldr RC RC - spu RC -; BinOp rhs -; (i) - ldi 3 RC - add FP RC RC - ldr RC RC - spu RC - spo RD - spo RC - add RC RD RC - spu RC -; assigning stack address (FP + 6) := top of stack - spo RA - ldi 6 RB - add RB FP RB - str RA RB -; -; assigning char at FP + 7 -; evaluating value to be assigned -; -; Builtin call load [] -; (char_p) - ldi 6 RC - add FP RC RC - ldr RC RC - spu RC -; (load) dereferencing top of stack - spo RC - ldr RC RC - spu RC -; assigning stack address (FP + 7) := top of stack - spo RA - ldi 7 RB - add RB FP RB - str RA RB -; -; -; (char) - ldi 7 RA - add FP RA RA - ldr RA RA - spu RA -; - ldi 0 RA - spu RA -; Comparing - spo RA - addi RA 0 RA - bri zero If_false_branch_11 -; - jpi While_end_8 - jpi If_end_branch_12 -@label if_false_branch_11 -@label if_end_branch_12 -; -; assigning last at FP + 5 -; evaluating value to be assigned -; (char) - ldi 7 RC - add FP RC RC - ldr RC RC - spu RC -; assigning stack address (FP + 5) := top of stack - spo RA - ldi 5 RB - add RB FP RB - str RA RB -; -; assigning i at FP + 3 -; evaluating value to be assigned -; -; BinOp lhs -; (i) - ldi 3 RC - add FP RC RC - ldr RC RC - spu RC -; BinOp rhs -; - ldi 1 RC - spu RC - spo RD - spo RC - add RC RD RC - spu RC -; assigning stack address (FP + 3) := top of stack - spo RA - ldi 3 RB - add RB FP RB - str RA RB - jpi While_test_6 -@label while_else_7 -@label while_end_8 -; -; Builtin call store [, ] -; -; BinOp lhs -; (text_mem_p) - ldi 1 RA - add FP RA RA - ldr RA RA - spu RA -; BinOp rhs -; - ldi 0 RA - spu RA - spo RB - spo RA - add RA RB RA - spu RA -; (first) - ldi 4 RA - add FP RA RA - ldr RA RA - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA -; -; Builtin call store [, ] -; -; BinOp lhs -; (text_mem_p) - ldi 1 RA - add FP RA RA - ldr RA RA - spu RA -; BinOp rhs -; - ldi 1 RA - spu RA - spo RB - spo RA - add RA RB RA - spu RA -; (last) - ldi 5 RA - add FP RA RA - ldr RA RA - spu RA - spo RB - spo RA - str RB RA - ldi 0 RA - spu RA -; popping free-standing Expr result from stack - spo RA - mov FP SP - hlt diff --git a/resources/asm/aoc23_1a.py b/resources/asm/aoc23_1a.py deleted file mode 100644 index 12e3953..0000000 --- a/resources/asm/aoc23_1a.py +++ /dev/null @@ -1,48 +0,0 @@ -import atk16 - -GRAPHICS_NO_MODE: atk16.ConstWord16 = 0 -GRAPHICS_TEXT_MODE: atk16.ConstWord16 = 1 -GRAPHICS_SPRITE_MODE: atk16.ConstWord16 = 2 - -GRAPHICS_MODE_PP: atk16.ConstWord16 = 0x17 -TEXT_MEM_PP: atk16.ConstWord16 = 0x19 - -graphics_mode_p = atk16.load(GRAPHICS_MODE_PP) -text_mem_p = atk16.load(TEXT_MEM_PP) - -atk16.store(graphics_mode_p, GRAPHICS_TEXT_MODE) - -INPUT_P: atk16.ConstWord16 = 0x9000 -INPUT_ELEM_SIZE: atk16.ConstWord16 = 256 - -# 1a example - -atk16.store(INPUT_P + 0, "1") -atk16.store(INPUT_P + 1, "a") -atk16.store(INPUT_P + 2, "b") -atk16.store(INPUT_P + 3, "c") -atk16.store(INPUT_P + 4, "2") -atk16.store(INPUT_P + 5, "\0") -# atk16.store_const_vec(INPUT_P + 0, "1abc2\0") -# atk16.store_const_vec(INPUT_P + 256, "pqr3stu8vwx\0") -# atk16.store_const_vec(INPUT_P + 512, "a1b2c3d4e5f\0") -# atk16.store_const_vec(INPUT_P + 768, "treb7uchet\0") - -row_p = INPUT_P -i = 0 - -first = atk16.load(row_p) -last = 0 - -while True: - char_p = INPUT_P + i - char = atk16.load(char_p) - - if char == "\0": - break - - last = char - i += 1 - -atk16.store(text_mem_p + 0, first) -atk16.store(text_mem_p + 1, last) \ No newline at end of file diff --git a/test/e2e/fibo/fibo.atk16 b/test/e2e/fibo/fibo.atk16 index fe38fe5..2cf3d20 100644 --- a/test/e2e/fibo/fibo.atk16 +++ b/test/e2e/fibo/fibo.atk16 @@ -2,9 +2,13 @@ ; will jump to label main @include bootstrap +@label iters + 51 + @label main - ldi 10 RA - calli fibo ; call fibo subroutine with parameter 10 + ldi iters RA + ldr RA RA + calli fibo ; call fibo subroutine with parameter 1000 hlt @label fibo ; fibo(n) subroutine diff --git a/test/e2e/fibo/test_fibo.py b/test/e2e/fibo/test_fibo.py index a7146fc..65af07a 100644 --- a/test/e2e/fibo/test_fibo.py +++ b/test/e2e/fibo/test_fibo.py @@ -1,3 +1,4 @@ +import time from atk16_asm import assemble from atk16_emu import Machine from test.utils import pad_bytearray @@ -13,7 +14,21 @@ def test_fibo(): machine = Machine() machine.load_rom_image(rom_image) machine.reset() + start = time.perf_counter_ns() machine.run_until_halted() + end = time.perf_counter_ns() machine.print_state_summary() - assert machine.ra.value == 89 + elapsed = end - start + print("Elapsed time:", elapsed, "ns") + steps_taken = machine.steps_taken + + # VGA frequency is 25.175 MHz + # Divided by 16, that's 1.5734375 MHz (main clock) + # Instructions take 3–5 cycles, the average instruction takes around 4 cycles + # 1.5734375 MHz / 4 = 393359.375 Hz + # That is the goal instruction frequency + # TODO limit emulation speed to 393359.375 Hz + print("Instructions / second:", steps_taken / (elapsed / 1e9)) + + assert machine.ra.value == 41443 -- cgit v1.3