diff options
Diffstat (limited to 'atk16_ast_walking_compiler')
| -rw-r--r-- | atk16_ast_walking_compiler/ast_compiler_bootstrap.atk16 | 74 | ||||
| -rw-r--r-- | atk16_ast_walking_compiler/sample_py/aoc23_1a.py | 48 | ||||
| -rw-r--r-- | atk16_ast_walking_compiler/sample_py/atk16.py | 39 | ||||
| -rw-r--r-- | atk16_ast_walking_compiler/sample_py/test_py_src.py | 36 |
4 files changed, 197 insertions, 0 deletions
diff --git a/atk16_ast_walking_compiler/ast_compiler_bootstrap.atk16 b/atk16_ast_walking_compiler/ast_compiler_bootstrap.atk16 new file mode 100644 index 0000000..b38e5f5 --- /dev/null +++ b/atk16_ast_walking_compiler/ast_compiler_bootstrap.atk16 @@ -0,0 +1,74 @@ +;; 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 0xE7F0 +@let terminal_addr 0xE7F0 +@let keyboard_addr 0xE7F1 +@let gr_mode_addr 0xE7F2 +@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
\ No newline at end of file diff --git a/atk16_ast_walking_compiler/sample_py/aoc23_1a.py b/atk16_ast_walking_compiler/sample_py/aoc23_1a.py new file mode 100644 index 0000000..12e3953 --- /dev/null +++ b/atk16_ast_walking_compiler/sample_py/aoc23_1a.py @@ -0,0 +1,48 @@ +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/atk16_ast_walking_compiler/sample_py/atk16.py b/atk16_ast_walking_compiler/sample_py/atk16.py new file mode 100644 index 0000000..9aa3c55 --- /dev/null +++ b/atk16_ast_walking_compiler/sample_py/atk16.py @@ -0,0 +1,39 @@ +from typing import Literal, Any, TypeVar, NewType, Never, Callable, Generic, overload, cast +Char = Literal['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\t', '\n', '\x0b', '\x0c', '\r', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\x7f', '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87', '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f', '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97', '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f', '\xa0', '¡', '¢', '£', '¤', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '\xad', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ'] +Void = Literal[0] +Word16 = int +ConstWord16 = int + +def store(p: Word16, value: Word16 | Char) -> Void: + raise NotImplementedError + +@overload +def store_const_vec(p: Word16, value: list[Word16]) -> Void: ... +@overload +def store_const_vec(p: Word16, value: list[Char]) -> Void: ... +@overload +def store_const_vec(p: Word16, value: str) -> Void: ... + +def store_const_vec(p, value) -> Void: + raise NotImplementedError + +def load(p: Word16) -> Word16: + raise NotImplementedError + +T = TypeVar("T") +def call_inline(expr: T) -> T: + """Inline the function call `expr` at the callsite. + This can lead to a larger output size but avoids having to do a subroutine call.""" + raise NotImplementedError + +def asm(asm: str): + """Inject ATK16 assembly `asm` into the compiled output.""" + raise NotImplementedError + +def ord(char: Char) -> int: + """Convert char to int""" + raise NotImplementedError + +def const(word: Word16) -> ConstWord16: + """When used in an assignment such as `A = const(0xFF)`, stores the value as a globally accessible constant.""" + raise NotImplementedError
\ No newline at end of file diff --git a/atk16_ast_walking_compiler/sample_py/test_py_src.py b/atk16_ast_walking_compiler/sample_py/test_py_src.py new file mode 100644 index 0000000..619d22e --- /dev/null +++ b/atk16_ast_walking_compiler/sample_py/test_py_src.py @@ -0,0 +1,36 @@ +# TODO: actual imports, not just `import atk16` +from atk16 import * + +GRAPHICS_NO_MODE: ConstWord16 = 0 +GRAPHICS_TEXT_MODE: ConstWord16 = 1 +GRAPHICS_SPRITE_MODE: ConstWord16 = 2 + +GRAPHICS_MODE_PP: ConstWord16 = 0x17 +TEXT_MEM_PP: ConstWord16 = 0x19 + +TEXT_MEM_PP: ConstWord16 = 0x19 + +# asm( +# "@label keyboard_isr" +# " spu RA" + +# " spo RA" +# ) + +# set_isr(0, "keyboard_isr") + +graphics_mode_p = load(GRAPHICS_MODE_PP) +text_mem_p = load(TEXT_MEM_PP) + +store(graphics_mode_p, GRAPHICS_TEXT_MODE) + +def nth_letter(n: int) -> int: + return ord('A') + n + +i = 0 +while i < 26: + store(text_mem_p + i, nth_letter(i)) + i += 1 + +while True: + pass |
