From abf6cbdecc8eb006a859205dee7500c2b70c0998 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Fri, 3 Nov 2023 19:55:52 +0200 Subject: Fix macros, add string I/O helpers --- asm/ext_string_io.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 asm/ext_string_io.py (limited to 'asm/ext_string_io.py') diff --git a/asm/ext_string_io.py b/asm/ext_string_io.py new file mode 100644 index 0000000..cf7d598 --- /dev/null +++ b/asm/ext_string_io.py @@ -0,0 +1,47 @@ +from asm_ops import * +from ext_std import expand_addi + +def to_char_code(c: str): + if c == " " or c == "%SPACE": return 0 + else: return ord(c) - 64 + +_counter = 0 +def get_unique_name(prefix: str): + global _counter + ret = f"{prefix}_{_counter}" + _counter += 1 + return ret + +def expand_put_char(cursor_reg: str, scratch_reg: str, c: str) -> ExpandResult: + return [ + ["ldi", str(to_char_code(c)), scratch_reg], + ["str", scratch_reg, cursor_reg], + *expand_addi("RA", "1", "RA"), + ] + +def expand_put_string(cursor_reg: str, scratch_reg: str, *rest: str) -> ExpandResult: + string = " ".join(rest) + result: ExpandResult = [] + for c in string: + result += expand_put_char(cursor_reg, scratch_reg, c) + + return result + +def expand_cursor_to_line(cursor_reg: str, line: int) -> ExpandResult: + lbl_data = get_unique_name("cursor_to_line_data") + lbl_jmpover = get_unique_name("cursor_to_line_jmpover") + line_width = 64 + return [ + ["jpi", lbl_jmpover], + ["@label", lbl_data], + [f"text_mem + {line} * {line_width}"], + ["@label", lbl_jmpover], + ["ldi", lbl_data, cursor_reg], + ["ldr", cursor_reg, cursor_reg], + ] + +expansions: OpExpansionDict = { + "put_char": expand_put_char, + "cursor_to_line": expand_cursor_to_line, + "put_string": expand_put_string, +} \ No newline at end of file -- cgit v1.3