diff options
| -rw-r--r-- | atk16_asm/asm_eval.py | 13 | ||||
| -rw-r--r-- | atk16_asm/asm_ops.py | 8 | ||||
| -rw-r--r-- | atk16_projects/monitor/monitor.atk16 | 138 |
3 files changed, 96 insertions, 63 deletions
diff --git a/atk16_asm/asm_eval.py b/atk16_asm/asm_eval.py index 09c3a75..f9a4e3d 100644 --- a/atk16_asm/asm_eval.py +++ b/atk16_asm/asm_eval.py @@ -27,10 +27,6 @@ constants: dict[str, str] = { Symbols = dict[str, int] -def check_size(bits: int, val: int) -> None: - if val >= 2 ** bits: - raise Exception(f"Value does not fit in {bits} bits: 0x{val:>04x}") - def eval_symbol(symbols: Symbols, c: str) -> str: if c in symbols: return str(symbols[c]) @@ -42,6 +38,9 @@ def eval_symbol(symbols: Symbols, c: str) -> str: def eval_expr(symbols: Symbols, expr: str, bits: int = 16) -> int: expr = eval_symbol(symbols, expr) - ret = eval(expr, symbols.copy()) # eval as Python expr - check_size(bits, ret) - return ret + val = eval(expr, symbols.copy()) # eval as Python expr + + if val >= 2 ** bits: + raise Exception(f"When evaluating expression \"{expr}\":\nValue does not fit in {bits} bits: 0x{val:>04x}") + + return val diff --git a/atk16_asm/asm_ops.py b/atk16_asm/asm_ops.py index 3198d4c..65cf0da 100644 --- a/atk16_asm/asm_ops.py +++ b/atk16_asm/asm_ops.py @@ -124,7 +124,7 @@ def expand_jpr(addr_reg: str) -> ExpandResult: @register_primitive def make_jpi(meta: Meta, symbols: Symbols, imm: int | str) -> int: """JPI 0110 XXXI IIII IIII""" - imm_e = eval_expr(symbols, str(imm), bits=9) + imm_e = eval_expr(symbols, str(imm), bits=16) imm_e = imm_e - meta.address - 1 if imm_e < -(2 ** 8) or imm_e >= 2 ** 8: @@ -157,8 +157,12 @@ def expand_brr(flag_s: str, addr_reg: str) -> ExpandResult: def make_bri(meta: Meta, symbols: Symbols, flag_s: str, imm: int | str) -> int: """BRI 1000 XFFI IIII IIII""" flag_s_e = eval_expr(symbols, flag_s, bits=2) - imm_e = eval_expr(symbols, str(imm), bits=9) + imm_e = eval_expr(symbols, str(imm), bits=16) imm_e = imm_e - meta.address - 1 + + if imm_e < -(2 ** 8) or imm_e >= 2 ** 8: + raise Exception(f"jpi imm value is not representable in 9 bits: {imm_e}") + imm_e = imm_e & (0b111111111) word = (0b1000 << 12) + \ (flag_s_e << 9) + \ diff --git a/atk16_projects/monitor/monitor.atk16 b/atk16_projects/monitor/monitor.atk16 index 4580d5f..a0d4e9d 100644 --- a/atk16_projects/monitor/monitor.atk16 +++ b/atk16_projects/monitor/monitor.atk16 @@ -14,6 +14,7 @@ @data text_cursor_p 0xE800 @data input_buffer_p 0xE801 @data input_cursor_p 0xE802 +@data input_entered_bool 0xE803 @data title_string " »» ATK16 monitor v0.1 »»" @data subtitle_string " Run [help] for a list of commands" @@ -46,6 +47,13 @@ str RG RA ; END + ; BEGIN input_entered_bool = 0 + ldi input_entered_bool RA + ldr RA RA + ldi 0 RB + str RB RA + ; END + ; use memset to clear the text buffer ; BEGIN memset(text_mem, 0, text_buffer_size) ldi vt_text_mem RA ; RA := text buffer address pointer @@ -83,64 +91,34 @@ ; BEGIN critical section m_set_critical RA RB - ; TODO evaluate input_buffer if newline was just pressed - ;calli is_newline_pressed - ;subi RG 1 RG ; if is_newline_pressed() == 1, then loop_newline_pressed - ;bri zero loop_newline_pressed - jpi loop_finally + ldi input_entered_bool RA + ldr RA RA + ldr RA RB + subi RB 0 RB + bri zero loop_finally ; if input_entered_bool == 0 (false), skip to end of loop -@label loop_newline_pressed - ; TODO - jpi loop_finally +@label loop_input_was_entered + ldi 0 RB ; else, run this branch and set input_entered_bool = 0 + str RB RA -@label loop_finally - m_clear_critical RA RB - ; END critical section - jpi loop +@data eval_output_string "command received!" + ldi eval_output_string RA + calli put_string -;;; Subroutines + ; ldi input_buffer_p RA ; TODO! + ; ldr RA RA + ; ldr RA RA + ; calli put_string -@label newline - ; move cursor to the beginning of the next line - ; parameters: none - ; returns: none - - stack_stash RA RB m_newline RA RB - stack_restore RA RB - return -@label put_char - ; put a character at the current cursor position - ; parameters: RA = character to put - ; returns: none - - stack_stash RB RC - m_put_char RA RB RC - stack_restore RB RC - return - -@label put_string - ; put a string at the current cursor position - ; parameters: RA = string_p - ; returns: none - - stack_stash RA RB RC RD RE - ldr RA RB ; RB := string length - addi RA 1 RC ; RC := pointer to first character -@label put_string_loop - subi RB 0 RB ; exit if RB has been decremented to 0 - bri zero put_string_done ; i.e. no more characters to put - - ldr RC RA ; RA := character to put - m_put_char RA RD RE ; inline the put_char subroutine to avoid function call overhead + ldi prompt_string RA + calli put_string - inc RC - subi RB 1 RB - jpi put_string_loop -@label put_string_done - stack_restore RA RB RC RD RE - return +@label loop_finally + m_clear_critical RA RB + ; END critical section + jpi loop @data row_index_mask ${64 - 1} @@ -163,7 +141,8 @@ subi RB 5 RB subi RB 3 RB - bri zero keyboard_isr_backspace + ldi keyboard_isr_backspace RD + brr zero RD ldi row_index_mask RD ; else code is not special; write it to text buffer ldr RD RD ; if cursor is not at the end of the line @@ -201,8 +180,15 @@ callr RD ; END - ldi prompt_string RA - calli put_string + ; BEGIN input_entered_bool = 1 + ldi input_entered_bool RA + ldr RA RA + ldi 1 RB + str RB RA + ; END + + ldi ${ord(" ")} RA + m_put_char RA RB RD jpi keyboard_isr_end @@ -228,6 +214,50 @@ stack_restore RA RB RC RD rti +;;; Subroutines + +@label newline + ; move cursor to the beginning of the next line + ; parameters: none + ; returns: none + + stack_stash RA RB + m_newline RA RB + stack_restore RA RB + return + +@label put_char + ; put a character at the current cursor position + ; parameters: RA = character to put + ; returns: none + + stack_stash RB RC + m_put_char RA RB RC + stack_restore RB RC + return + +@label put_string + ; put a string at the current cursor position + ; parameters: RA = string_p + ; returns: none + + stack_stash RA RB RC RD RE + ldr RA RB ; RB := string length + addi RA 1 RC ; RC := pointer to first character +@label put_string_loop + subi RB 0 RB ; exit if RB has been decremented to 0 + bri zero put_string_done ; i.e. no more characters to put + + ldr RC RA ; RA := character to put + m_put_char RA RD RE ; inline the put_char subroutine to avoid function call overhead + + inc RC + subi RB 1 RB + jpi put_string_loop +@label put_string_done + stack_restore RA RB RC RD RE + return + @address vt_ISR0 @begin_override keyboard_isr |
