start making changes to init_vm

This commit is contained in:
zongor 2025-11-29 10:50:29 -08:00
parent b638fedd81
commit 89a17f06f0
1 changed files with 31 additions and 23 deletions

View File

@ -199,6 +199,7 @@ i32 main(i32 argc, char *argv[]) {
char *output_file = nil; char *output_file = nil;
bool is_rom = false; bool is_rom = false;
bool is_assembly = false; bool is_assembly = false;
bool is_ir = false;
// Parse command line arguments // Parse command line arguments
for (i32 i = 1; i < argc; i++) { for (i32 i = 1; i < argc; i++) {
@ -216,36 +217,43 @@ i32 main(i32 argc, char *argv[]) {
if (ext && (strcmp(ext, ".lisp") == 0)) { if (ext && (strcmp(ext, ".lisp") == 0)) {
is_assembly = true; is_assembly = true;
} }
if (ext && (strcmp(ext, ".ir") == 0)) {
is_ir = true;
}
} else if (output_file == nil && dump_rom) { } else if (output_file == nil && dump_rom) {
// This is the output file for -o flag // This is the output file for -o flag
output_file = argv[i]; output_file = argv[i];
} }
} }
VM vm = {0}; VM *vm;
if (!init_vm(vm)) {
printf("vm did not initialize for some reason.");
return 1;
}
bool compilation_success = true; bool compilation_success = true;
if (input_file) { if (input_file) {
if (is_rom) { if (is_rom) {
// Load ROM file directly // Load ROM file directly
compilation_success = loadVM(input_file, &vm); compilation_success = loadVM(input_file, vm);
} else if (is_assembly) { } else if (is_assembly) {
// Compile Lisp file // Compile Lisp file
if (dump_rom && output_file) { if (dump_rom && output_file) {
compilation_success = assembleAndSave(input_file, output_file, &vm); compilation_success = assembleAndSave(input_file, output_file, vm);
} else { } else {
compilation_success = assembleAndSave(input_file, nil, &vm); compilation_success = assembleAndSave(input_file, nil, vm);
} }
} else { } else {
if (dump_rom && output_file) { if (dump_rom && output_file) {
compilation_success = compileAndSave(input_file, output_file, &vm); compilation_success = compileAndSave(input_file, output_file, vm);
} else { } else {
compilation_success = compileAndSave(input_file, nil, &vm); compilation_success = compileAndSave(input_file, nil, vm);
} }
} }
} else { } else {
printf("usage: undar <src.ul>..."); printf("usage: undar <src.ul>...");
return 0; return 1;
} }
if (dump_rom) { if (dump_rom) {
@ -254,7 +262,7 @@ i32 main(i32 argc, char *argv[]) {
// If dump_rom flag was set without specifying output file, use default // If dump_rom flag was set without specifying output file, use default
if (dump_rom && !is_rom && !output_file) { if (dump_rom && !is_rom && !output_file) {
if (!saveVM("memory_dump.bin", &vm)) { if (!saveVM("memory_dump.bin", vm)) {
printf("Failed to save VM to memory_dump.bin\n"); printf("Failed to save VM to memory_dump.bin\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -262,7 +270,7 @@ i32 main(i32 argc, char *argv[]) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
vm_register_device(&vm, "/dev/term/0", "terminal", &console_data, vm_register_device(vm, "/dev/term/0", "terminal", &console_data,
&console_device_ops, 4); &console_device_ops, 4);
if (SDL_Init(SDL_INIT_VIDEO) < 0) { if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@ -275,7 +283,7 @@ i32 main(i32 argc, char *argv[]) {
screen_data.height = 480; screen_data.height = 480;
screen_data.buffer_size = screen_data.width * screen_data.height; screen_data.buffer_size = screen_data.width * screen_data.height;
vm_register_device(&vm, "/dev/screen/0", "screen", &screen_data, &screen_ops, vm_register_device(vm, "/dev/screen/0", "screen", &screen_data, &screen_ops,
16 + screen_data.buffer_size); 16 + screen_data.buffer_size);
mouse_data.x = 0; mouse_data.x = 0;
@ -285,10 +293,10 @@ i32 main(i32 argc, char *argv[]) {
mouse_data.btn3 = 0; mouse_data.btn3 = 0;
mouse_data.btn4 = 0; mouse_data.btn4 = 0;
vm_register_device(&vm, "/dev/mouse/0", "mouse", &mouse_data, &mouse_ops, 16); vm_register_device(vm, "/dev/mouse/0", "mouse", &mouse_data, &mouse_ops, 16);
keyboard_data.keys = SDL_GetKeyboardState(&keyboard_data.key_count); keyboard_data.keys = SDL_GetKeyboardState(&keyboard_data.key_count);
vm_register_device(&vm, "/dev/keyboard/0", "keyboard", &keyboard_data, vm_register_device(vm, "/dev/keyboard/0", "keyboard", &keyboard_data,
&keyboard_ops, keyboard_data.key_count + 4); &keyboard_ops, keyboard_data.key_count + 4);
SDL_Event event; SDL_Event event;
@ -333,11 +341,11 @@ i32 main(i32 argc, char *argv[]) {
case SDL_FINGERDOWN: case SDL_FINGERDOWN:
case SDL_FINGERUP: { case SDL_FINGERUP: {
float x = event.tfinger.x * 640; f32 x = event.tfinger.x * 640;
float y = event.tfinger.y * 480; f32 y = event.tfinger.y * 480;
mouse_data.x = (int)x; mouse_data.x = (i32)x;
mouse_data.y = (int)y; mouse_data.y = (i32)y;
// Only treat the first finger as mouse input (ignore multi-touch // Only treat the first finger as mouse input (ignore multi-touch
// beyond 1 finger) // beyond 1 finger)
@ -354,10 +362,10 @@ i32 main(i32 argc, char *argv[]) {
} }
// Run VM for a fixed number of cycles or a time slice // Run VM for a fixed number of cycles or a time slice
int cycles_this_frame = 0; i32 cycles_this_frame = 0;
int max_cycles_per_frame = 100; // Adjust this value i32 max_cycles_per_frame = 100; // Adjust this value
while (cycles_this_frame < max_cycles_per_frame) { while (cycles_this_frame < max_cycles_per_frame) {
if (!step_vm(&vm)) { if (!step_vm(vm)) {
running = false; running = false;
break; break;
} }
@ -374,9 +382,9 @@ i32 main(i32 argc, char *argv[]) {
SDL_RenderGetViewport(screen_data.renderer, &output_rect); SDL_RenderGetViewport(screen_data.renderer, &output_rect);
// Calculate aspect ratio preserving scaling // Calculate aspect ratio preserving scaling
float scale_x = (float)output_rect.w / screen_data.width; f32 scale_x = (f32)output_rect.w / screen_data.width;
float scale_y = (float)output_rect.h / screen_data.height; f32 scale_y = (f32)output_rect.h / screen_data.height;
float scale = SDL_min(scale_x, scale_y); f32 scale = SDL_min(scale_x, scale_y);
SDL_Rect dstrect = { SDL_Rect dstrect = {
(i32)((output_rect.w - screen_data.width * scale) / 2), (i32)((output_rect.w - screen_data.width * scale) / 2),
@ -392,5 +400,5 @@ i32 main(i32 argc, char *argv[]) {
} }
} }
return vm.flag; return vm->flag;
} }