From a9f3ec694ce1ed80ba7cc2ac5406f4f8c3e62a5d Mon Sep 17 00:00:00 2001 From: zongor Date: Sat, 19 Jul 2025 20:20:28 -0400 Subject: [PATCH] add string example --- src/arch/linux/main.c | 38 ++++++++++++++++++++++++-------------- src/opcodes.h | 5 +++++ src/test.c | 24 +++++++++++++++++++++++- src/test.h | 1 + 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/arch/linux/main.c b/src/arch/linux/main.c index 2fe33bb..31dc0db 100644 --- a/src/arch/linux/main.c +++ b/src/arch/linux/main.c @@ -10,9 +10,17 @@ int main(int argc, char **argv) { vm.stack_size = STACK_SIZE; vm.memory_size = MEMORY_SIZE; - uint32_t end = test_loop_compile(vm.memory); + uint32_t end = 0; + + /* test_add_compile(vm.memory); */ /* test_add_function_compile(vm.memory); */ + /* test_loop_compile(vm.memory); */ + test_hello_world_compile(vm.memory); /* test_recursive_function_compile(vm.memory); */ + while (step_vm(&vm)); + core_dump(&vm); + return 0; + uint32_t buffer_size = 640 * 480 * sizeof(uint32_t); Device screen; @@ -25,9 +33,10 @@ int main(int argc, char **argv) { vm.mp += buffer_size; /* Create window and renderer */ - SDL_Window *window = SDL_CreateWindow( - "Reality Engine VM", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - screen.s.width, screen.s.height, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); + SDL_Window *window = + SDL_CreateWindow("Reality Engine VM", SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, screen.s.width, screen.s.height, + SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); SDL_Renderer *renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); @@ -40,25 +49,28 @@ int main(int argc, char **argv) { /* Enable nearest-neighbor scaling (preserves pixel art) */ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); /* "0" = nearest-neighbor */ - while (step_vm(&vm)) { + bool running = true; + while (running) { + step_vm(&vm); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: - return 0; + running = false; case SDL_MOUSEBUTTONDOWN: - /* event.button.x, event.button.y, event.button.button */ + printf("mouse_down: x=%d, y=%d, btn=%d\n", event.button.x, + event.button.y, event.button.button); break; case SDL_MOUSEMOTION: - /* handleInput(event.motion.x, event.motion.y, 0); 0 for no button */ + printf("mouse_move: x=%d, y=%d\n", event.motion.x, event.motion.y); break; case SDL_FINGERDOWN: - /* handleInput(event.tfinger.x * screen.s.width, */ - /* event.tfinger.y * screen.s.height, 1); // 1 for touch */ + printf("touch_down x=%f, y=%f\n", event.tfinger.x * screen.s.width, + event.tfinger.y * screen.s.height); break; case SDL_FINGERMOTION: - /* handleInput(event.tfinger.x * screen.s.width, */ - /* event.tfinger.y * screen.s.height, 0); // 0 for no button */ + printf("touch_move x=%f, y=%f\n", event.tfinger.x * screen.s.width, + event.tfinger.y * screen.s.height); break; } } @@ -68,7 +80,6 @@ int main(int argc, char **argv) { SDL_RenderClear(renderer); - /* Optional: Set logical size to maintain 640x480 aspect ratio */ /* (Scales to fit screen while preserving pixel grid) */ SDL_Rect output_rect; SDL_RenderGetViewport(renderer, &output_rect); @@ -84,6 +95,5 @@ int main(int argc, char **argv) { SDL_RenderCopy(renderer, texture, NULL, &dstrect); SDL_RenderPresent(renderer); } - core_dump(&vm); return 0; } diff --git a/src/opcodes.h b/src/opcodes.h index f0580d0..37deebd 100644 --- a/src/opcodes.h +++ b/src/opcodes.h @@ -15,6 +15,11 @@ typedef struct slice_s { uint32_t end; } Slice; +typedef struct cell_s { + uint32_t car; + uint32_t cdr; +} Cell; + #define MAX_REGS 32 typedef struct frame_s { Value registers[MAX_REGS]; /* R0-R31 */ diff --git a/src/test.c b/src/test.c index c2dd4b3..c2342cb 100644 --- a/src/test.c +++ b/src/test.c @@ -1,5 +1,27 @@ #include "test.h" +uint32_t test_hello_world_compile(Value *memory) { + uint32_t i = 0; + memory[i++].u = OP(OP_LOADU, 1, 0, 0); + memory[i++].u = 4; + memory[i++].u = OP(OP_PRINT_STRING, 0, 1, 0); /* print("nuqneH 'u'?"); */ + memory[i++].u = OP(OP_HALT, 0, 0, 0); /* explicit halt */ + memory[i++].u = 12; + memory[i].c[0] = 'n'; + memory[i].c[1] = 'u'; + memory[i].c[2] = 'q'; + memory[i++].c[3] = 'n'; + memory[i].c[0] = 'e'; + memory[i].c[1] = 'H'; + memory[i].c[2] = ' '; + memory[i++].c[3] = '\''; + memory[i].c[0] = 'u'; + memory[i].c[1] = '\''; + memory[i].c[2] = '?'; + memory[i++].c[3] = '\0'; + return i; +} + uint32_t test_add_compile(Value *memory) { uint32_t i = 0; memory[i++].u = OP(OP_LOADU, 0, 0, 0); @@ -18,7 +40,7 @@ uint32_t test_loop_compile(Value *memory) { memory[i++].u = OP(OP_LOADF, 0, 0, 0); /* let a = 5.0 */ memory[i++].f = 5.0f; memory[i++].u = OP(OP_LOADI, 1, 0, 0); /* do (i = 5, 0, -1) { */ - memory[i++].i = 5; + memory[i++].i = 5000; memory[i++].u = OP(OP_LOADI, 2, 0, 0); /* loop check value */ memory[i++].i = 0; memory[i++].u = OP(OP_LOADI, 3, 0, 0); /* loop incriment value */ diff --git a/src/test.h b/src/test.h index 3720b8c..b79bc3c 100644 --- a/src/test.h +++ b/src/test.h @@ -3,6 +3,7 @@ #include "opcodes.h" +uint32_t test_hello_world_compile (Value *memory); uint32_t test_add_compile (Value *memory); uint32_t test_loop_compile (Value *memory); uint32_t test_add_function_compile(Value *memory);