From 76cea79a46d42f8f59503d3bb421f0d298da66cb Mon Sep 17 00:00:00 2001 From: Melodi <62381002+Melodi17@users.noreply.github.com> Date: Thu, 28 Aug 2025 21:57:33 +1000 Subject: implemented standard library --- Orchestrator/Program.cs | 64 ++++-- ROMs/lib.c | 52 ++++- ROMs/paint.asm | 185 ++++++++++++----- ROMs/paint.c | 52 +---- ROMs/paint.q1 | Bin 676 -> 872 bytes ROMs/test_lib.c | 7 - ROMs/typewriter.asm | 542 +++++++++++++++++++++++++++--------------------- ROMs/typewriter.c | 84 +++----- ROMs/typewriter.q1 | Bin 3412 -> 3627 bytes 9 files changed, 562 insertions(+), 424 deletions(-) delete mode 100644 ROMs/test_lib.c diff --git a/Orchestrator/Program.cs b/Orchestrator/Program.cs index a59127b..213f5ee 100644 --- a/Orchestrator/Program.cs +++ b/Orchestrator/Program.cs @@ -13,32 +13,38 @@ class Program { with.CaseInsensitiveEnumValues = true; }); - + p.ParseArguments(args).WithParsed(Main); } - + private static void Main(Options options) { string compiledFile = Path.ChangeExtension(options.InputFile, "asm"); string assembledFile = Path.ChangeExtension(options.InputFile, "q1"); - - Q1.Compiler.Program.Main(new CompilerOptions - { - CommentCompilationMode = options.CommentCompilationMode, - InputFile = options.InputFile, - OutputFile = compiledFile - }); - - Q1.Assembler.Program.Main(new AssemblerOptions - { - InputFile = compiledFile, - OutputFile = assembledFile - }); - - Q1.Emulator.Program.Main(new EmulatorOptions - { - InputFile = assembledFile, - }); + + if (!options.NoCompile) + Q1.Compiler.Program.Main( + new CompilerOptions + { + CommentCompilationMode = options.CommentCompilationMode, + InputFile = options.InputFile, + OutputFile = compiledFile + }); + + if (!options.NoAssemble) + Q1.Assembler.Program.Main( + new AssemblerOptions + { + InputFile = compiledFile, + OutputFile = assembledFile + }); + + if (!options.NoEmulate) + Q1.Emulator.Program.Main( + new EmulatorOptions + { + InputFile = assembledFile, + }); } } @@ -46,7 +52,19 @@ class Options { [Option('i', "input", Required = true, HelpText = "Input file to process.")] public string InputFile { get; set; } - - [Option('c', "comment-compilation", Required = false, HelpText = "Comments to preserve in the compiled result")] - public CommentCompilationMode CommentCompilationMode { get; set; } = CommentCompilationMode.UserDefined; + + [Option( + 'c', "comment-compilation", Required = false, + HelpText = "Comments to preserve in the compiled result")] + public CommentCompilationMode CommentCompilationMode { get; set; } = + CommentCompilationMode.UserDefined; + + [Option("no-compile", HelpText = "Skip compilation")] + public bool NoCompile { get; set; } = false; + + [Option("no-assemble", HelpText = "Skip assembling")] + public bool NoAssemble { get; set; } = false; + + [Option("no-emulate", HelpText = "Skip emulation")] + public bool NoEmulate { get; set; } = false; } \ No newline at end of file diff --git a/ROMs/lib.c b/ROMs/lib.c index 32378c0..cd666b9 100644 --- a/ROMs/lib.c +++ b/ROMs/lib.c @@ -1,7 +1,34 @@ +#define SK_MAX 256 +#define SK_NL 13 +#define SK_BS 8 +#define SK_SHIFT 160 +#define SK_A 65 +#define SK_B 66 +#define SK_C 67 +#define SK_D 68 +#define SK_E 69 + +#define MS_LEFT 1 +#define MS_RIGHT 2 +#define MS_MIDDLE 4 + +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +#define SCREEN_MAX 128 * 64 + +#define true 1 +#define false 0 + +#define COLOR_BLACK 8 +#define COLOR_RED 1 +#define COLOR_GREEN 2 +#define COLOR_BLUE 3 +#define COLOR_TRANSPARENT 0 + int setPixel(int x, int y, int c) { char* screenStart = 0x402F; - int i = (y * 128) + x; + int i = (y * SCREEN_WIDTH) + x; screenStart[i] = c; } @@ -14,8 +41,7 @@ int setPixel(int i, int c) int clear(int c) { char* screenStart = 0x402F; - char screenSize = 64 * 128; - for (int i = 0; i < screenSize; i++) + for (int i = 0; i < SCREEN_MAX; i++) { screenStart[i] = c; } @@ -31,6 +57,26 @@ int getKeyState(int scanCode) return (val & (1 << bit)) != 0; } +int getPressedKey() +{ + for (int sk = 0; sk < SK_MAX; sk++) + { + if (getKeyState(sk) && sk != SK_SHIFT) + return sk; + } + return 0; +} + +int readKey() +{ + while (true) + { + int sk = getPressedKey(); + if (sk != 0) + return sk; + } +} + int getMouseIndex() { return *(0x602F); diff --git a/ROMs/paint.asm b/ROMs/paint.asm index 2f023b7..b4d454f 100644 --- a/ROMs/paint.asm +++ b/ROMs/paint.asm @@ -25,7 +25,7 @@ _setPixel_int_x__int_y__int_c:; int setPixel(int x, int y, int c) mov 0, V0 ; fallback return 0 ret -_setPixelIndex_int_i__int_c: ; int setPixelIndex(int i, int c) +_setPixel_int_i__int_c: ; int setPixel(int i, int c) pop V0 ; preserve function return address pop [$7209] ; param int i pop [$720B] ; param int c @@ -46,29 +46,28 @@ _clear_int_c: ; int clear(int c) pop [$720F] ; param int c push V0 ; restore function return address mov $402F, [$7211] ; int screenStart = $402F - push 64 ; left operand - mov 128, V1 ; right operand + mov 0, [$7213] ; int i = 0 +_body_1: + push [$7213] ; left operand + push 128 ; left operand + mov 64, V1 ; right operand pop V0 ; get left operand back mul V0, V1 ; compute multiplication - mov AX, [$7213] ; int screenSize = AX - mov 0, [$7215] ; int i = 0 -_body_1: - push [$7215] ; left operand - mov [$7213], V1 ; right operand + mov AX, V1 ; right operand pop V0 ; get left operand back lt V0, V1 ; compute less than check not LX bz jmp _end_2 ; if for condition fails, jump to end push [$7211] ; store index obj - mov [$7215], V1 ; store indexer + mov [$7213], V1 ; store indexer pop V0 ; restore index obj mul V1, 1 ; multiply indexer by 1 add AX, V0 ; add indexer and index obj mov AX, V0 ; move to general purpose register movb [$720F], [V0] ; [V0] = [$720F] - push [$7215] ; i++ - inc [$7215] + push [$7213] ; i++ + inc [$7213] pop V0 jmp _body_1 _end_2: @@ -77,21 +76,21 @@ _end_2: _getKeyState_int_scanCode: ; int getKeyState(int scanCode) pop V0 ; preserve function return address - pop [$7217] ; param int scanCode + pop [$7215] ; param int scanCode push V0 ; restore function return address - mov $6033, [$7219] ; int hid = $6033 - push [$7217] ; left operand + mov $6033, [$7217] ; int hid = $6033 + push [$7215] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back div V0, V1 ; compute division - mov AX, [$721B] ; int index = AX - push [$7217] ; left operand + mov AX, [$7219] ; int index = AX + push [$7215] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back mod V0, V1 ; compute modulus - mov AX, [$721D] ; int bit = AX - push [$7219] ; store index obj - push [$721B] ; left operand + mov AX, [$721B] ; int bit = AX + push [$7217] ; store index obj + push [$7219] ; left operand mov 1, V1 ; right operand pop V0 ; get left operand back sub V0, V1 ; compute subtraction @@ -100,10 +99,10 @@ _getKeyState_int_scanCode: ; int getKeyState(int scanCode) mul V1, 1 ; multiply indexer by 1 add AX, V0 ; add indexer and index obj mov AX, V0 ; move to general purpose register - mov [V0], [$721F] ; int val = [V0] - push [$721F] ; left operand + mov [V0], [$721D] ; int val = [V0] + push [$721D] ; left operand push 1 ; left operand - mov [$721D], V1 ; right operand + mov [$721B], V1 ; right operand pop V0 ; get left operand back shl V0, V1 ; compute left shift mov DX, V1 ; right operand @@ -119,35 +118,109 @@ _getKeyState_int_scanCode: ; int getKeyState(int scanCode) mov 0, V0 ; fallback return 0 ret -_main: ; int main() - mov 0, [$7221] ; int color = 0 +_getPressedKey: ; int getPressedKey() + mov 0, [$721F] ; int sk = 0 _body_3: + push [$721F] ; left operand + mov 256, V1 ; right operand + pop V0 ; get left operand back + lt V0, V1 ; compute less than check + not LX + bz + jmp _end_4 ; if for condition fails, jump to end + push [$721F] ; arg int scanCode + call _getKeyState_int_scanCode; int getKeyState(int scanCode) + cmp V0, 0 ; logical and + not + bz + jmp _clause_5 + mov 0, V0 ; first operand failed, store 0 and skip second + jmp _end_6 +_clause_5: + push [$721F] ; left operand + mov 160, V1 ; right operand + pop V0 ; get left operand back + cmp V0, V1 ; compute inequality check + not + cmp LX, 0 ; compare result from second operand to 0 + not + mov LX, V0 ; store second operand result +_end_6: + not V0 + bz ; check if condition + jmp _end_7 ; if succeeds, continue, else jump to end + mov [$721F], V0 ; return value + ret +_end_7: + push [$721F] ; i++ + inc [$721F] + pop V0 + jmp _body_3 +_end_4: + mov 0, V0 ; return value + ret + mov 0, V0 ; fallback return 0 + ret + +_readKey: ; int readKey() +_body_8: not 1 bz - jmp _end_4 ; if while condition fails, jump to end - mov $602F, [$7223] ; int hid = $602F - push [$7223] ; store index obj - mov 0, V1 ; store indexer - pop V0 ; restore index obj - mul V1, 2 ; multiply indexer by 2 - add AX, V0 ; add indexer and index obj - mov AX, V0 ; move to general purpose register - mov [V0], [$7225] ; int mouseIndex = [V0] - push [$7223] ; store index obj - mov 1, V1 ; store indexer - pop V0 ; restore index obj - mul V1, 2 ; multiply indexer by 2 - add AX, V0 ; add indexer and index obj - mov AX, V0 ; move to general purpose register - mov [V0], [$7227] ; int mouseState = [V0] + jmp _end_9 ; if while condition fails, jump to end + call _getPressedKey ; int getPressedKey() + mov V0, [$7221] ; int sk = V0 + push [$7221] ; left operand + mov 0, V1 ; right operand + pop V0 ; get left operand back + cmp V0, V1 ; compute inequality check + not + not LX + bz ; check if condition + jmp _end_10 ; if succeeds, continue, else jump to end + mov [$7221], V0 ; return value + ret +_end_10: + jmp _body_8 +_end_9: + mov 0, V0 ; fallback return 0 + ret + +_getMouseIndex: ; int getMouseIndex() + mov $602F, V0 ; move pointer into register for dereference + mov [V0], V0 ; return value + ret + mov 0, V0 ; fallback return 0 + ret + +_getMouseState: ; int getMouseState() + push $602F ; left operand + mov 2, V1 ; right operand + pop V0 ; get left operand back + add V0, V1 ; compute addition + mov AX, V0 ; move pointer into register for dereference + mov [V0], V0 ; return value + ret + mov 0, V0 ; fallback return 0 + ret + +_main: ; int main() + mov 0, [$7223] ; int color = 0 +_body_11: + not 1 + bz + jmp _end_12 ; if while condition fails, jump to end + call _getMouseIndex ; int getMouseIndex() + mov V0, [$7225] ; int mouseIndex = V0 + call _getMouseState ; int getMouseState() + mov V0, [$7227] ; int mouseState = V0 push [$7227] ; left operand mov 1, V1 ; right operand pop V0 ; get left operand back and V0, V1 ; compute and not DX bz ; check if condition - jmp _end_5 ; if succeeds, continue, else jump to end - push [$7221] ; left operand + jmp _end_13 ; if succeeds, continue, else jump to end + push [$7223] ; left operand mov 3, V1 ; right operand pop V0 ; get left operand back mod V0, V1 ; compute modulus @@ -157,35 +230,35 @@ _body_3: add V0, V1 ; compute addition push AX ; arg int i push [$7225] ; arg int c - call _setPixelIndex_int_i__int_c; int setPixelIndex(int i, int c) -_end_5: + call _setPixel_int_i__int_c; int setPixel(int i, int c) +_end_13: push 67 ; arg int scanCode call _getKeyState_int_scanCode; int getKeyState(int scanCode) not V0 bz ; check if condition - jmp _end_6 ; if succeeds, continue, else jump to end + jmp _end_14 ; if succeeds, continue, else jump to end push 8 ; arg int c call _clear_int_c ; int clear(int c) -_end_6: +_end_14: push 65 ; arg int scanCode call _getKeyState_int_scanCode; int getKeyState(int scanCode) not V0 bz ; check if condition - jmp _end_7 ; if succeeds, continue, else jump to end - push [$7221] ; i++ - inc [$7221] + jmp _end_15 ; if succeeds, continue, else jump to end + push [$7223] ; i++ + inc [$7223] pop V0 -_body_8: +_body_16: push 65 ; arg int scanCode call _getKeyState_int_scanCode; int getKeyState(int scanCode) not V0 bz - jmp _end_9 ; if while condition fails, jump to end - jmp _body_8 -_end_9: -_end_7: - jmp _body_3 -_end_4: + jmp _end_17 ; if while condition fails, jump to end + jmp _body_16 +_end_17: +_end_15: + jmp _body_11 +_end_12: mov 0, V0 ; fallback return 0 ret diff --git a/ROMs/paint.c b/ROMs/paint.c index 443719a..7e0427b 100644 --- a/ROMs/paint.c +++ b/ROMs/paint.c @@ -1,55 +1,23 @@ -int setPixel(int x, int y, int c) -{ - char* screenStart = 0x402F; - int i = (y * 128) + x; - screenStart[i] = c; -} - -int setPixelIndex(int i, int c) -{ - char* screenStart = 0x402F; - screenStart[i] = c; -} - -int clear(int c) -{ - char* screenStart = 0x402F; - char screenSize = 64 * 128; - for (int i = 0; i < screenSize; i++) - { - screenStart[i] = c; - } -} - -int getKeyState(int scanCode) -{ - char* hid = 0x6033; - int index = scanCode / 8; - int bit = scanCode % 8; - - int val = hid[index - 1]; - return (val & (1 << bit)) != 0; -} +#include "lib.c" int main() { int color = 0; - while (1) + while (true) { - int* hid = 0x602F; - int mouseIndex = hid[0]; - int mouseState = hid[1]; + int mouseIndex = getMouseIndex(); + int mouseState = getMouseState(); - if (mouseState & 1) - setPixelIndex(mouseIndex, (color % 3) + 1); + if (mouseState & MS_LEFT) + setPixel(mouseIndex, (color % 3) + 1); - if (getKeyState(67)) - clear(8); + if (getKeyState(SK_C)) + clear(COLOR_BLACK); - if (getKeyState(65)) + if (getKeyState(SK_A)) { color++; - while (getKeyState(65)) { } + while (getKeyState(SK_A)) { } } } } \ No newline at end of file diff --git a/ROMs/paint.q1 b/ROMs/paint.q1 index 98929bb..5105eb8 100644 Binary files a/ROMs/paint.q1 and b/ROMs/paint.q1 differ diff --git a/ROMs/test_lib.c b/ROMs/test_lib.c deleted file mode 100644 index e779c2c..0000000 --- a/ROMs/test_lib.c +++ /dev/null @@ -1,7 +0,0 @@ -int main() -{ - while (1) - { - - } -} \ No newline at end of file diff --git a/ROMs/typewriter.asm b/ROMs/typewriter.asm index df4e7ca..8a9017b 100644 --- a/ROMs/typewriter.asm +++ b/ROMs/typewriter.asm @@ -26,150 +26,72 @@ _setPixel_int_x__int_y__int_c:; int setPixel(int x, int y, int c) mov 0, V0 ; fallback return 0 ret -_drawChar_int_ch__int_x__int_y__int_color:; int drawChar(int ch, int x, int y, int color) +_setPixel_int_i__int_c: ; int setPixel(int i, int c) pop V0 ; preserve function return address - pop [$7209] ; param int ch - pop [$720B] ; param int x - pop [$720D] ; param int y - pop [$720F] ; param int color + pop [$7209] ; param int i + pop [$720B] ; param int c push V0 ; restore function return address - ; adjust for jmp instruction - push $1FFF ; left operand - mov 2, V1 ; right operand - pop V0 ; get left operand back - add V0, V1 ; compute addition - mov AX, [$7211] ; int fontStart = AX - push [$7209] ; left operand - mov 8, V1 ; right operand - pop V0 ; get left operand back - mul V0, V1 ; compute multiplication - mov AX, [$7213] ; int charStart = AX - mov 0, [$7215] ; int yOff = 0 -_body_1: - push [$7215] ; left operand - mov 8, V1 ; right operand - pop V0 ; get left operand back - lt V0, V1 ; compute less than check - not LX - bz - jmp _end_2 ; if for condition fails, jump to end - push [$7211] ; store index obj - push [$7213] ; left operand - mov [$7215], V1 ; right operand - pop V0 ; get left operand back - add V0, V1 ; compute addition - mov AX, V1 ; store indexer + mov $402F, [$720D] ; int screenStart = $402F + push [$720D] ; store index obj + mov [$7209], V1 ; store indexer pop V0 ; restore index obj mul V1, 1 ; multiply indexer by 1 add AX, V0 ; add indexer and index obj mov AX, V0 ; move to general purpose register - mov [V0], [$7217] ; int row = [V0] - mov 0, [$7219] ; int xOff = 0 -_body_3: - push [$7219] ; left operand - mov 8, V1 ; right operand - pop V0 ; get left operand back - lt V0, V1 ; compute less than check - not LX - bz - jmp _end_4 ; if for condition fails, jump to end - push [$7217] ; left operand - push 7 ; left operand - mov [$7219], V1 ; right operand - pop V0 ; get left operand back - sub V0, V1 ; compute subtraction - mov AX, V1 ; right operand - pop V0 ; get left operand back - shr V0, V1 ; compute right shift - push DX ; left operand - mov 1, V1 ; right operand - pop V0 ; get left operand back - and V0, V1 ; compute and - mov DX, [$721B] ; int bit = DX - bz [$721B] ; check ternary condition - jmp _then_5 - mov [$720F], V0 ; store ternary fail result - jmp _end_6 -_then_5: - mov 8, V0 ; store ternary success result -_end_6: - mov V0, [$721D] ; int pix = V0 - push [$721D] ; arg int x - push [$720D] ; left operand - mov [$7215], V1 ; right operand - pop V0 ; get left operand back - add V0, V1 ; compute addition - push AX ; arg int y - push [$720B] ; left operand - mov [$7219], V1 ; right operand - pop V0 ; get left operand back - add V0, V1 ; compute addition - push AX ; arg int c - call _setPixel_int_x__int_y__int_c; int setPixel(int x, int y, int c) - push [$7219] ; i++ - inc [$7219] - pop V0 - jmp _body_3 -_end_4: - push [$7215] ; i++ - inc [$7215] - pop V0 - jmp _body_1 -_end_2: + movb [$720B], [V0] ; [V0] = [$720B] mov 0, V0 ; fallback return 0 ret _clear_int_c: ; int clear(int c) pop V0 ; preserve function return address - pop [$721F] ; param int c + pop [$720F] ; param int c push V0 ; restore function return address - mov $402F, [$7221] ; int screenStart = $402F - push 64 ; left operand - mov 128, V1 ; right operand + mov $402F, [$7211] ; int screenStart = $402F + mov 0, [$7213] ; int i = 0 +_body_1: + push [$7213] ; left operand + push 128 ; left operand + mov 64, V1 ; right operand pop V0 ; get left operand back mul V0, V1 ; compute multiplication - mov AX, [$7223] ; int screenSize = AX - mov 0, [$7225] ; int i = 0 -_body_7: - push [$7225] ; left operand - mov [$7223], V1 ; right operand + mov AX, V1 ; right operand pop V0 ; get left operand back lt V0, V1 ; compute less than check not LX bz - jmp _end_8 ; if for condition fails, jump to end - push [$7221] ; store index obj - mov [$7225], V1 ; store indexer + jmp _end_2 ; if for condition fails, jump to end + push [$7211] ; store index obj + mov [$7213], V1 ; store indexer pop V0 ; restore index obj mul V1, 1 ; multiply indexer by 1 add AX, V0 ; add indexer and index obj mov AX, V0 ; move to general purpose register - movb [$721F], [V0] ; [V0] = [$721F] - push [$7225] ; i++ - inc [$7225] + movb [$720F], [V0] ; [V0] = [$720F] + push [$7213] ; i++ + inc [$7213] pop V0 - jmp _body_7 -_end_8: + jmp _body_1 +_end_2: mov 0, V0 ; fallback return 0 ret _getKeyState_int_scanCode: ; int getKeyState(int scanCode) pop V0 ; preserve function return address - pop [$7227] ; param int scanCode + pop [$7215] ; param int scanCode push V0 ; restore function return address - mov $6033, [$7229] ; int hid = $6033 - push [$7227] ; left operand + mov $6033, [$7217] ; int hid = $6033 + push [$7215] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back div V0, V1 ; compute division - mov AX, [$722B] ; int index = AX - push [$7227] ; left operand + mov AX, [$7219] ; int index = AX + push [$7215] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back mod V0, V1 ; compute modulus - mov AX, [$722D] ; int bit = AX - push [$7229] ; store index obj - push [$722B] ; left operand + mov AX, [$721B] ; int bit = AX + push [$7217] ; store index obj + push [$7219] ; left operand mov 1, V1 ; right operand pop V0 ; get left operand back sub V0, V1 ; compute subtraction @@ -178,10 +100,10 @@ _getKeyState_int_scanCode: ; int getKeyState(int scanCode) mul V1, 1 ; multiply indexer by 1 add AX, V0 ; add indexer and index obj mov AX, V0 ; move to general purpose register - mov [V0], [$722F] ; int val = [V0] - push [$722F] ; left operand + mov [V0], [$721D] ; int val = [V0] + push [$721D] ; left operand push 1 ; left operand - mov [$722D], V1 ; right operand + mov [$721B], V1 ; right operand pop V0 ; get left operand back shl V0, V1 ; compute left shift mov DX, V1 ; right operand @@ -197,34 +119,212 @@ _getKeyState_int_scanCode: ; int getKeyState(int scanCode) mov 0, V0 ; fallback return 0 ret -_print_int*_text__int_len__int_x__int_y__int_color:; int print(int* text, int len, int x, int y, int color) +_getPressedKey: ; int getPressedKey() + mov 0, [$721F] ; int sk = 0 +_body_3: + push [$721F] ; left operand + mov 256, V1 ; right operand + pop V0 ; get left operand back + lt V0, V1 ; compute less than check + not LX + bz + jmp _end_4 ; if for condition fails, jump to end + push [$721F] ; arg int scanCode + call _getKeyState_int_scanCode; int getKeyState(int scanCode) + cmp V0, 0 ; logical and + not + bz + jmp _clause_5 + mov 0, V0 ; first operand failed, store 0 and skip second + jmp _end_6 +_clause_5: + push [$721F] ; left operand + mov 160, V1 ; right operand + pop V0 ; get left operand back + cmp V0, V1 ; compute inequality check + not + cmp LX, 0 ; compare result from second operand to 0 + not + mov LX, V0 ; store second operand result +_end_6: + not V0 + bz ; check if condition + jmp _end_7 ; if succeeds, continue, else jump to end + mov [$721F], V0 ; return value + ret +_end_7: + push [$721F] ; i++ + inc [$721F] + pop V0 + jmp _body_3 +_end_4: + mov 0, V0 ; return value + ret + mov 0, V0 ; fallback return 0 + ret + +_readKey: ; int readKey() +_body_8: + not 1 + bz + jmp _end_9 ; if while condition fails, jump to end + call _getPressedKey ; int getPressedKey() + mov V0, [$7221] ; int sk = V0 + push [$7221] ; left operand + mov 0, V1 ; right operand + pop V0 ; get left operand back + cmp V0, V1 ; compute inequality check + not + not LX + bz ; check if condition + jmp _end_10 ; if succeeds, continue, else jump to end + mov [$7221], V0 ; return value + ret +_end_10: + jmp _body_8 +_end_9: + mov 0, V0 ; fallback return 0 + ret + +_getMouseIndex: ; int getMouseIndex() + mov $602F, V0 ; move pointer into register for dereference + mov [V0], V0 ; return value + ret + mov 0, V0 ; fallback return 0 + ret + +_getMouseState: ; int getMouseState() + push $602F ; left operand + mov 2, V1 ; right operand + pop V0 ; get left operand back + add V0, V1 ; compute addition + mov AX, V0 ; move pointer into register for dereference + mov [V0], V0 ; return value + ret + mov 0, V0 ; fallback return 0 + ret + +_drawChar_int_ch__int_x__int_y__int_color:; int drawChar(int ch, int x, int y, int color) pop V0 ; preserve function return address - pop [$7231] ; param int* text - pop [$7233] ; param int len - pop [$7235] ; param int x - pop [$7237] ; param int y - pop [$7239] ; param int color + pop [$7223] ; param int ch + pop [$7225] ; param int x + pop [$7227] ; param int y + pop [$7229] ; param int color push V0 ; restore function return address - mov 0, [$723B] ; int i = 0 -_body_9: - push [$723B] ; left operand + ; adjust for jmp instruction + push $1FFF ; left operand + mov 2, V1 ; right operand + pop V0 ; get left operand back + add V0, V1 ; compute addition + mov AX, [$722B] ; int fontStart = AX + push [$7223] ; left operand + mov 8, V1 ; right operand + pop V0 ; get left operand back + mul V0, V1 ; compute multiplication + mov AX, [$722D] ; int charStart = AX + mov 0, [$722F] ; int yOff = 0 +_body_11: + push [$722F] ; left operand + mov 8, V1 ; right operand + pop V0 ; get left operand back + lt V0, V1 ; compute less than check + not LX + bz + jmp _end_12 ; if for condition fails, jump to end + push [$722B] ; store index obj + push [$722D] ; left operand + mov [$722F], V1 ; right operand + pop V0 ; get left operand back + add V0, V1 ; compute addition + mov AX, V1 ; store indexer + pop V0 ; restore index obj + mul V1, 1 ; multiply indexer by 1 + add AX, V0 ; add indexer and index obj + mov AX, V0 ; move to general purpose register + mov [V0], [$7231] ; int row = [V0] + mov 0, [$7233] ; int xOff = 0 +_body_13: + push [$7233] ; left operand + mov 8, V1 ; right operand + pop V0 ; get left operand back + lt V0, V1 ; compute less than check + not LX + bz + jmp _end_14 ; if for condition fails, jump to end + push [$7231] ; left operand + push 7 ; left operand mov [$7233], V1 ; right operand pop V0 ; get left operand back + sub V0, V1 ; compute subtraction + mov AX, V1 ; right operand + pop V0 ; get left operand back + shr V0, V1 ; compute right shift + push DX ; left operand + mov 1, V1 ; right operand + pop V0 ; get left operand back + and V0, V1 ; compute and + mov DX, [$7235] ; int bit = DX + bz [$7235] ; check ternary condition + jmp _then_15 + mov [$7229], V0 ; store ternary fail result + jmp _end_16 +_then_15: + mov 8, V0 ; store ternary success result +_end_16: + mov V0, [$7237] ; int pix = V0 + push [$7237] ; arg int x + push [$7227] ; left operand + mov [$722F], V1 ; right operand + pop V0 ; get left operand back + add V0, V1 ; compute addition + push AX ; arg int y + push [$7225] ; left operand + mov [$7233], V1 ; right operand + pop V0 ; get left operand back + add V0, V1 ; compute addition + push AX ; arg int c + call _setPixel_int_x__int_y__int_c; int setPixel(int x, int y, int c) + push [$7233] ; i++ + inc [$7233] + pop V0 + jmp _body_13 +_end_14: + push [$722F] ; i++ + inc [$722F] + pop V0 + jmp _body_11 +_end_12: + mov 0, V0 ; fallback return 0 + ret + +_print_int*_text__int_len__int_x__int_y__int_color:; int print(int* text, int len, int x, int y, int color) + pop V0 ; preserve function return address + pop [$7239] ; param int* text + pop [$723B] ; param int len + pop [$723D] ; param int x + pop [$723F] ; param int y + pop [$7241] ; param int color + push V0 ; restore function return address + mov 0, [$7243] ; int i = 0 +_body_17: + push [$7243] ; left operand + mov [$723B], V1 ; right operand + pop V0 ; get left operand back lt V0, V1 ; compute less than check not LX bz - jmp _end_10 ; if for condition fails, jump to end - push [$7231] ; store index obj - mov [$723B], V1 ; store indexer + jmp _end_18 ; if for condition fails, jump to end + push [$7239] ; store index obj + mov [$7243], V1 ; store indexer pop V0 ; restore index obj mul V1, 2 ; multiply indexer by 2 add AX, V0 ; add indexer and index obj mov AX, V0 ; move to general purpose register - mov [V0], [$723D] ; int ch = [V0] - push [$7239] ; arg int ch - push [$7237] ; arg int x - push [$7235] ; left operand - push [$723B] ; left operand + mov [V0], [$7245] ; int ch = [V0] + push [$7241] ; arg int ch + push [$723F] ; arg int x + push [$723D] ; left operand + push [$7243] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back mul V0, V1 ; compute multiplication @@ -232,156 +332,130 @@ _body_9: pop V0 ; get left operand back add V0, V1 ; compute addition push AX ; arg int y - push [$723D] ; arg int color + push [$7245] ; arg int color call _drawChar_int_ch__int_x__int_y__int_color; int drawChar(int ch, int x, int y, int color) - push [$723B] ; i++ - inc [$723B] + push [$7243] ; i++ + inc [$7243] pop V0 - jmp _body_9 -_end_10: + jmp _body_17 +_end_18: mov 0, V0 ; fallback return 0 ret _main: ; int main() - mov 0, [$723F] ; int x = 0 - mov 0, [$7241] ; int y = 0 -_body_11: + mov 0, [$7247] ; int x = 0 + mov 0, [$7249] ; int y = 0 +_body_19: not 1 bz - jmp _end_12 ; if while condition fails, jump to end - mov 0, [$7243] ; int sk = 0 -_body_13: - push [$7243] ; left operand - mov 256, V1 ; right operand + jmp _end_20 ; if while condition fails, jump to end + push [$7247] ; left operand + mov 128, V1 ; right operand pop V0 ; get left operand back - lt V0, V1 ; compute less than check + cmp V0, V1 ; compute equality check + bz + jmp _end_21 + gt V0, V1 +_end_21: not LX + bz ; check if condition + jmp _end_22 ; if succeeds, continue, else jump to end + mov 0, [$7247] ; [$7247] = 0 + push [$7249] ; left operand + mov 8, V1 ; right operand + pop V0 ; get left operand back + add V0, V1 ; compute addition + push AX ; computation results + pop V1 ; get computation results back + mov V1, [$7249] ; store back into left param +_end_22: + push [$7249] ; left operand + mov 64, V1 ; right operand + pop V0 ; get left operand back + cmp V0, V1 ; compute equality check bz - jmp _end_14 ; if for condition fails, jump to end - push [$7243] ; arg int scanCode - call _getKeyState_int_scanCode; int getKeyState(int scanCode) - not V0 + jmp _end_23 + gt V0, V1 +_end_23: + not LX bz ; check if condition - jmp _end_15 ; if succeeds, continue, else jump to end -_body_16: - push [$7243] ; arg int scanCode + jmp _end_24 ; if succeeds, continue, else jump to end + mov 0, [$7247] ; [$7247] = 0 + mov 0, [$7249] ; [$7249] = 0 + push 8 ; arg int c + call _clear_int_c ; int clear(int c) +_end_24: + call _readKey ; int readKey() + mov V0, [$724B] ; int sk = V0 +_body_25: + push [$724B] ; arg int scanCode call _getKeyState_int_scanCode; int getKeyState(int scanCode) not V0 bz - jmp _end_17 ; if while condition fails, jump to end - jmp _body_16 -_end_17: - ; Newline pressed - push [$7243] ; left operand + jmp _end_26 ; if while condition fails, jump to end + jmp _body_25 +_end_26: + push [$724B] ; left operand mov 13, V1 ; right operand pop V0 ; get left operand back cmp V0, V1 ; compute equality check not LX bz ; check if condition - jmp _end_18 ; if succeeds, continue, else jump to end - push [$7241] ; left operand + jmp _end_27 ; if succeeds, continue, else jump to end + push [$7249] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back add V0, V1 ; compute addition push AX ; computation results pop V1 ; get computation results back - mov V1, [$7241] ; store back into left param - mov 0, [$723F] ; [$723F] = 0 - jmp _body_13 ; continue -_end_18: - ; Backspace pressed - push [$7243] ; left operand + mov V1, [$7249] ; store back into left param + mov 0, [$7247] ; [$7247] = 0 + jmp _body_19 ; continue +_end_27: + push [$724B] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back cmp V0, V1 ; compute equality check not LX bz ; check if condition - jmp _end_19 ; if succeeds, continue, else jump to end - push [$723F] ; left operand + jmp _end_28 ; if succeeds, continue, else jump to end + push [$7247] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back sub V0, V1 ; compute subtraction push AX ; computation results pop V1 ; get computation results back - mov V1, [$723F] ; store back into left param - push [$723F] ; left operand + mov V1, [$7247] ; store back into left param + push [$7247] ; left operand mov 0, V1 ; right operand pop V0 ; get left operand back lt V0, V1 ; compute less than check not LX bz ; check if condition - jmp _end_20 ; if succeeds, continue, else jump to end - mov 0, [$723F] ; [$723F] = 0 -_end_20: - mov 32, [$7247] - mov $7247, [$7245] ; int text = $7247 - push 1 ; arg int* text - push [$7241] ; arg int len - push [$723F] ; arg int x - push 1 ; arg int y - push [$7245] ; arg int color - call _print_int*_text__int_len__int_x__int_y__int_color; int print(int* text, int len, int x, int y, int color) - jmp _body_13 ; continue -_end_19: - mov [$7243], [$724B] - mov $724B, [$7249] ; int text = $724B - push 1 ; arg int* text - push [$7241] ; arg int len - push [$723F] ; arg int x - push 1 ; arg int y - push [$7249] ; arg int color - call _print_int*_text__int_len__int_x__int_y__int_color; int print(int* text, int len, int x, int y, int color) - push [$723F] ; left operand - mov 8, V1 ; right operand - pop V0 ; get left operand back - add V0, V1 ; compute addition - push AX ; computation results - pop V1 ; get computation results back - mov V1, [$723F] ; store back into left param -_end_15: - push [$7243] ; i++ - inc [$7243] - pop V0 - jmp _body_13 -_end_14: - push [$723F] ; left operand - mov 128, V1 ; right operand - pop V0 ; get left operand back - cmp V0, V1 ; compute equality check - bz - jmp _end_21 - gt V0, V1 -_end_21: - not LX - bz ; check if condition - jmp _end_22 ; if succeeds, continue, else jump to end - mov 0, [$723F] ; [$723F] = 0 - push [$7241] ; left operand + jmp _end_29 ; if succeeds, continue, else jump to end + mov 0, [$7247] ; [$7247] = 0 +_end_29: + push 1 ; arg int ch + push [$7249] ; arg int x + push [$7247] ; arg int y + push 32 ; arg int color + call _drawChar_int_ch__int_x__int_y__int_color; int drawChar(int ch, int x, int y, int color) + jmp _body_19 ; continue +_end_28: + push 1 ; arg int ch + push [$7249] ; arg int x + push [$7247] ; arg int y + push [$724B] ; arg int color + call _drawChar_int_ch__int_x__int_y__int_color; int drawChar(int ch, int x, int y, int color) + push [$7247] ; left operand mov 8, V1 ; right operand pop V0 ; get left operand back add V0, V1 ; compute addition push AX ; computation results pop V1 ; get computation results back - mov V1, [$7241] ; store back into left param -_end_22: - push [$7241] ; left operand - mov 64, V1 ; right operand - pop V0 ; get left operand back - cmp V0, V1 ; compute equality check - bz - jmp _end_23 - gt V0, V1 -_end_23: - not LX - bz ; check if condition - jmp _end_24 ; if succeeds, continue, else jump to end - mov 0, [$723F] ; [$723F] = 0 - mov 0, [$7241] ; [$7241] = 0 - push 8 ; arg int c - call _clear_int_c ; int clear(int c) -_end_24: - jmp _body_11 -_end_12: + mov V1, [$7247] ; store back into left param + jmp _body_19 +_end_20: mov 0, V0 ; fallback return 0 ret diff --git a/ROMs/typewriter.c b/ROMs/typewriter.c index 0886532..a63a77d 100644 --- a/ROMs/typewriter.c +++ b/ROMs/typewriter.c @@ -1,4 +1,5 @@ #load "font.bin" +#include "lib.c" int drawChar(int ch, int x, int y, int color) { @@ -19,33 +20,6 @@ int drawChar(int ch, int x, int y, int color) } } -int setPixel(int x, int y, int c) -{ - char* screenStart = 0x402F; - int i = (y * 128) + x; - screenStart[i] = c; -} - -int clear(int c) -{ - char* screenStart = 0x402F; - char screenSize = 64 * 128; - for (int i = 0; i < screenSize; i++) - { - screenStart[i] = c; - } -} - -int getKeyState(int scanCode) -{ - char* hid = 0x6033; - int index = scanCode / 8; - int bit = scanCode % 8; - - int val = hid[index - 1]; - return (val & (1 << bit)) != 0; -} - int print(int* text, int len, int x, int y, int color) { for (int i = 0; i < len; i++) @@ -59,49 +33,41 @@ int main() { int x = 0; int y = 0; - while (1) + while (true) { - for (int sk = 0; sk < 256; sk++) - { - if (getKeyState(sk)) - { - while (getKeyState(sk)) { } - // Newline pressed - if (sk == 13) - { - y += 8; - x = 0; - continue; - } - // Backspace pressed - if (sk == 8) - { - x -= 8; - if (x < 0) - x = 0; - int* text = " "; - print(text, 1, x, y, 1); - continue; - } - - int* text = new int[] { sk }; - print(text, 1, x, y, 1); - x += 8; - } - } - - if (x >= 128) + if (x >= SCREEN_WIDTH) { x = 0; y += 8; } - if (y >= 64) + if (y >= SCREEN_HEIGHT) { x = 0; y = 0; clear(8); } + + int sk = readKey(); + while (getKeyState(sk)) { } + + if (sk == SK_NL) + { + y += 8; + x = 0; + continue; + } + + if (sk == SK_BS) + { + x -= 8; + if (x < 0) + x = 0; + drawChar(' ', x, y, 1); + continue; + } + drawChar(sk, x, y, 1); + x += 8; } } \ No newline at end of file diff --git a/ROMs/typewriter.q1 b/ROMs/typewriter.q1 index ab14869..163bb6f 100644 Binary files a/ROMs/typewriter.q1 and b/ROMs/typewriter.q1 differ -- cgit v1.3.1