118 lines
3.1 KiB
C
118 lines
3.1 KiB
C
#include "../../vm/vm.h"
|
|
#include "../../vm/device.h"
|
|
#include "devices.h"
|
|
#include <SDL2/SDL.h>
|
|
#include <emscripten.h>
|
|
#include <emscripten/html5.h>
|
|
#include <stdio.h>
|
|
|
|
VM vm = {0};
|
|
|
|
static DeviceOps screen_ops = {.open = screen_open,
|
|
.read = screen_read,
|
|
.write = screen_write,
|
|
.close = screen_close,
|
|
.ioctl = screen_ioctl};
|
|
|
|
static DeviceOps mouse_ops = {.open = mouse_open,
|
|
.read = mouse_read,
|
|
.write = mouse_write,
|
|
.close = mouse_close,
|
|
.ioctl = nil};
|
|
|
|
static DeviceOps console_device_ops = {
|
|
.open = console_open,
|
|
.read = console_read,
|
|
.write = console_write,
|
|
.close = console_close,
|
|
.ioctl = console_ioctl,
|
|
};
|
|
|
|
static ScreenDeviceData screen_data = {0};
|
|
static MouseDeviceData mouse_data = {0};
|
|
|
|
void mainloop() {
|
|
if (!step_vm(&vm)) {
|
|
emscripten_cancel_main_loop();
|
|
printf("VM execution completed\n");
|
|
}
|
|
}
|
|
|
|
bool loadVM(const char *filename, VM *vm) {
|
|
FILE *file = fopen(filename, "rb");
|
|
if (!file) {
|
|
printf("Failed to open ROM file: %s\n", filename);
|
|
return false;
|
|
}
|
|
|
|
// Read VM state
|
|
if (fread(&vm->pc, sizeof(u32), 1, file) != 1 ||
|
|
fread(&vm->cp, sizeof(u32), 1, file) != 1 ||
|
|
fread(&vm->fp, sizeof(u32), 1, file) != 1 ||
|
|
fread(&vm->sp, sizeof(u32), 1, file) != 1 ||
|
|
fread(&vm->rp, sizeof(u32), 1, file) != 1 ||
|
|
fread(&vm->mp, sizeof(u32), 1, file) != 1 ||
|
|
fread(&vm->dc, sizeof(u32), 1, file) != 1 ||
|
|
fread(&vm->flag, sizeof(i32), 1, file) != 1) {
|
|
printf("Failed to read VM state\n");
|
|
fclose(file);
|
|
return false;
|
|
}
|
|
|
|
// Read code section
|
|
if (fread(vm->code, 1, vm->cp, file) != vm->cp) {
|
|
printf("Failed to read code section\n");
|
|
fclose(file);
|
|
return false;
|
|
}
|
|
|
|
// Read memory section
|
|
if (fread(vm->memory, 1, vm->mp, file) != vm->mp) {
|
|
printf("Failed to read memory section\n");
|
|
fclose(file);
|
|
return false;
|
|
}
|
|
|
|
fclose(file);
|
|
return true;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
printf("SDL initialization failed: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
emscripten_set_canvas_element_size("#canvas", 640, 480);
|
|
#endif
|
|
|
|
loadVM("paint.rom", &vm);
|
|
printf("VM loaded successfully\n");
|
|
|
|
// Initialize device data
|
|
screen_data.width = 640;
|
|
screen_data.height = 480;
|
|
screen_data.size = 640 * 480;
|
|
screen_data.window = NULL;
|
|
screen_data.renderer = NULL;
|
|
screen_data.texture = NULL;
|
|
|
|
mouse_data.x = 0;
|
|
mouse_data.y = 0;
|
|
mouse_data.btn1 = 0;
|
|
mouse_data.btn2 = 0;
|
|
mouse_data.btn3 = 0;
|
|
mouse_data.btn4 = 0;
|
|
mouse_data.size = 12;
|
|
|
|
// Register devices
|
|
vm_register_device(&vm, "/dev/screen/0", "screen", &screen_data, &screen_ops);
|
|
vm_register_device(&vm, "/dev/mouse/0", "mouse", &mouse_data, &mouse_ops);
|
|
vm_register_device(&vm, "/dev/term/0", "terminal", NULL, &console_device_ops);
|
|
|
|
// Set up main loop
|
|
emscripten_set_main_loop(mainloop, 0, 1);
|
|
return 0;
|
|
} |