Compare commits

..

No commits in common. "51f7c33011bd3bf732a524685f03703aed0b6ae6" and "4e5ebdb7496b159d0e2bd8ef0202ab1fc9af4ab3" have entirely different histories.

12 changed files with 60 additions and 105 deletions

View File

@ -53,24 +53,8 @@ i32 screen_open(void *data, u32 mode) {
SDL_WINDOWPOS_CENTERED, screen->width, screen->height, SDL_WINDOWPOS_CENTERED, screen->width, screen->height,
SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (!screen->window) { if (!screen->window)
return -1; return -1;
}
screen->renderer =
SDL_CreateRenderer(screen->window, -1, SDL_RENDERER_ACCELERATED);
if (!screen->renderer) {
return -1;
}
screen->texture = SDL_CreateTexture(screen->renderer, SDL_PIXELFORMAT_RGB332,
SDL_TEXTUREACCESS_STREAMING,
screen->width, screen->height);
if (!screen->texture) {
fprintf(stderr, "SDL_CreateTexture failed: %s\n", SDL_GetError());
return -1;
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
return 0; return 0;
@ -89,29 +73,30 @@ i32 screen_write(void *data, const u8 *buffer, u32 size) {
if (size > screen->size * sizeof(u8)) { if (size > screen->size * sizeof(u8)) {
return -1; return -1;
} }
// Update texture with new frame data
SDL_UpdateTexture(screen->texture, NULL, buffer, screen->width);
// Clear and render if (!screen->surface && screen->window) {
SDL_RenderClear(screen->renderer); const u8 *pixel_buffer = buffer;
int pitch = screen->width; // bytes per row
SDL_Rect output_rect; screen->surface = SDL_CreateRGBSurfaceFrom((void *)pixel_buffer,
SDL_RenderGetViewport(screen->renderer, &output_rect); screen->width, screen->height,
8, // bits per pixel
pitch,
0xE0, // R mask (RGB332)
0x1C, // G mask
0x03, // B mask
0x00 // no alpha
);
// Calculate aspect ratio preserving scaling if (!screen->surface) {
float scale_x = (float)output_rect.w / screen->width; fprintf(stderr, "SDL_CreateRGBSurfaceFrom failed: %s\n", SDL_GetError());
float scale_y = (float)output_rect.h / screen->height; return -1;
float scale = SDL_min(scale_x, scale_y); }
}
SDL_Rect dstrect = { SDL_BlitScaled(screen->surface, NULL, SDL_GetWindowSurface(screen->window),
(i32)((output_rect.w - screen->width * scale) / 2), NULL);
(i32)((output_rect.h - screen->height * scale) / 2), SDL_UpdateWindowSurface(screen->window);
(i32)(screen->width * scale),
(i32)(screen->height * scale)
};
SDL_RenderCopy(screen->renderer, screen->texture, NULL, &dstrect);
SDL_RenderPresent(screen->renderer);
return 0; return 0;
} }

View File

@ -11,8 +11,7 @@ typedef struct screen_device_data_s {
u32 pos; u32 pos;
u32 size; u32 size;
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Surface *surface;
SDL_Texture *texture;
} ScreenDeviceData; } ScreenDeviceData;
/* Mouse device data */ /* Mouse device data */

View File

@ -415,45 +415,28 @@ i32 main(i32 argc, char *argv[]) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
bool running = true;
vm_register_device(&vm, "/dev/term/0", "terminal", nil, &console_device_ops); vm_register_device(&vm, "/dev/term/0", "terminal", nil, &console_device_ops);
if (gui_mode) { if (gui_mode) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) { if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL initialization failed: %s\n", SDL_GetError()); printf("SDL initialization failed: %s\n", SDL_GetError());
return 1; return 1;
} }
register_sdl_devices(&vm);
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0"); SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
screen_data.width = 640;
screen_data.height = 480;
screen_data.size = screen_data.width * screen_data.height;
vm_register_device(&vm, "/dev/screen/0", "screen",
&screen_data, &screen_ops);
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;
vm_register_device(&vm, "/dev/mouse/0", "mouse", &mouse_data, &mouse_ops);
keyboard_data.keys = SDL_GetKeyboardState(&keyboard_data.key_count);
vm_register_device(&vm, "/dev/keyboard/0", "keyboard", &keyboard_data,
&keyboard_ops);
bool running = true;
while (running) { while (running) {
if (!step_vm(&vm)) { #ifdef ASM_DEBUG
running = false; printf("| %s %d\n", opcode_to_string(vm.code[vm.pc]), vm.pc);
break; #endif
} running = step_vm(&vm);
} }
} else { } else {
bool running = true;
while (running) { while (running) {
#ifdef ASM_DEBUG
printf("| %s %d\n", opcode_to_string(vm.code[vm.pc]), vm.pc);
#endif
running = step_vm(&vm); running = step_vm(&vm);
} }
} }

View File

@ -8,8 +8,8 @@ i32 vm_register_device(VM *vm, const char *path, const char *type, void *data,
if (vm->dc >= DEVICES_SIZE) if (vm->dc >= DEVICES_SIZE)
return -1; return -1;
dev = &vm->devices[vm->dc]; dev = &vm->devices[vm->dc++];
dev->handle = vm->dc++;
strcopy(dev->path, path, DEVICE_PATH_MAX_LENGTH); strcopy(dev->path, path, DEVICE_PATH_MAX_LENGTH);
dev->path[DEVICE_PATH_MAX_LENGTH - 1] = '\0'; dev->path[DEVICE_PATH_MAX_LENGTH - 1] = '\0';
@ -19,7 +19,7 @@ i32 vm_register_device(VM *vm, const char *path, const char *type, void *data,
dev->data = data; dev->data = data;
dev->ops = ops; dev->ops = ops;
dev->flags = 0; dev->flags = 0;
return dev->handle; return 0;
} }
/* Find device by path */ /* Find device by path */

View File

@ -105,7 +105,7 @@ typedef struct device_ops_s {
const u8 *buffer); /* optional control */ const u8 *buffer); /* optional control */
} DeviceOps; } DeviceOps;
#define DEVICE_TYPE_MAX_LENGTH 16 /* 15 chars + null terminator */ #define DEVICE_TYPE_MAX_LENGTH 24 /* 23 chars + null terminator */
#define DEVICE_PATH_MAX_LENGTH 64 /* 63 chars + null terminator */ #define DEVICE_PATH_MAX_LENGTH 64 /* 63 chars + null terminator */
typedef struct device_s { typedef struct device_s {
@ -115,7 +115,6 @@ typedef struct device_s {
void *data; /* device-specific data */ void *data; /* device-specific data */
DeviceOps *ops; /* operations vtable */ DeviceOps *ops; /* operations vtable */
u32 flags; /* permissions, status, etc. */ u32 flags; /* permissions, status, etc. */
u32 handle; /* id for fast access in VM */
} Device; } Device;
#define MEMORY_SIZE (640 * 480 + 65536) #define MEMORY_SIZE (640 * 480 + 65536)

View File

@ -269,7 +269,6 @@ bool step_vm(VM *vm) {
if (dev) { if (dev) {
if (dev->ops->open) { if (dev->ops->open) {
vm->flag = dev->ops->open(dev->data, mode); vm->flag = dev->ops->open(dev->data, mode);
vm->stack[vm->sp++] = dev->handle;
} else { } else {
vm->flag = 1; /* success, no open needed */ vm->flag = 1; /* success, no open needed */
} }
@ -283,11 +282,11 @@ bool step_vm(VM *vm) {
case SYSCALL_DEVICE_READ: { case SYSCALL_DEVICE_READ: {
Device *dev; Device *dev;
u32 handle = vm->stack[--vm->sp]; /* path pointer */ u32 path_ptr = vm->stack[--vm->sp]; /* path pointer */
u32 size = vm->stack[--vm->sp]; /* size */ u32 size = vm->stack[--vm->sp]; /* size */
u32 buffer_ptr = vm->stack[--vm->sp]; u32 buffer_ptr = vm->stack[--vm->sp];
dev = &vm->devices[handle]; dev = find_device_by_path(vm, (const char *)&vm->memory[path_ptr + 4]);
if (dev && dev->ops->read) { if (dev && dev->ops->read) {
vm->flag = dev->ops->read(dev->data, &vm->memory[buffer_ptr + 4], size); vm->flag = dev->ops->read(dev->data, &vm->memory[buffer_ptr + 4], size);
vm->stack[vm->sp++] = buffer_ptr; vm->stack[vm->sp++] = buffer_ptr;
@ -300,11 +299,11 @@ bool step_vm(VM *vm) {
case SYSCALL_DEVICE_WRITE: { case SYSCALL_DEVICE_WRITE: {
Device *dev; Device *dev;
u32 handle = vm->stack[--vm->sp]; /* path pointer */ u32 path_ptr = vm->stack[--vm->sp]; /* path pointer */
u32 size = vm->stack[--vm->sp]; /* size */ u32 size = vm->stack[--vm->sp]; /* size */
u32 buffer_ptr = vm->stack[--vm->sp]; /* buffer pointer */ u32 buffer_ptr = vm->stack[--vm->sp]; /* buffer pointer */
dev = &vm->devices[handle]; dev = find_device_by_path(vm, (const char *)&vm->memory[path_ptr + 4]);
if (dev && dev->ops->write) { if (dev && dev->ops->write) {
vm->flag = dev->ops->write( vm->flag = dev->ops->write(
dev->data, (const u8 *)&vm->memory[buffer_ptr + 4], size); dev->data, (const u8 *)&vm->memory[buffer_ptr + 4], size);
@ -317,9 +316,10 @@ bool step_vm(VM *vm) {
case SYSCALL_DEVICE_CLOSE: { case SYSCALL_DEVICE_CLOSE: {
Device *dev; Device *dev;
u32 handle = vm->stack[--vm->sp]; /* path pointer */ u32 path_ptr = vm->stack[--vm->sp]; /* path pointer */
dev = find_device_by_path(vm, (const char *)&vm->memory[path_ptr + 4]);
dev = &vm->devices[handle];
if (dev && dev->ops->close) { if (dev && dev->ops->close) {
i32 result = dev->ops->close(dev->data); i32 result = dev->ops->close(dev->data);
vm->flag = result; vm->flag = result;
@ -332,11 +332,12 @@ bool step_vm(VM *vm) {
case SYSCALL_DEVICE_IOCTL: { case SYSCALL_DEVICE_IOCTL: {
Device *dev; Device *dev;
u32 handle = vm->stack[--vm->sp]; /* device path */ u32 path_ptr = vm->stack[--vm->sp]; /* device path */
u32 cmd = vm->stack[--vm->sp]; /* ioctl command */ u32 cmd = vm->stack[--vm->sp]; /* ioctl command */
u32 args_ptr = vm->stack[--vm->sp]; /* args pointer */ u32 args_ptr = vm->stack[--vm->sp]; /* args pointer */
dev = &vm->devices[handle]; dev = find_device_by_path(vm, (const char *)&vm->memory[path_ptr + 4]);
if (dev && dev->ops && dev->ops->ioctl) { if (dev && dev->ops && dev->ops->ioctl) {
i32 result = dev->ops->ioctl(dev->data, cmd, &vm->memory[args_ptr]); i32 result = dev->ops->ioctl(dev->data, cmd, &vm->memory[args_ptr]);
vm->flag = result; vm->flag = result;

View File

@ -1,7 +1,6 @@
((code ((code
(label main (label main
(load-immediate $0 &terminal-namespace) ; load terminal namespace (load-immediate $0 &terminal-namespace) ; load terminal namespace
(syscall OPEN $0 $0 $0)
(load-immediate $1 &hello-str) ; load hello string ptr (load-immediate $1 &hello-str) ; load hello string ptr
(string-length $2 $1) ; get length to write to stdout (string-length $2 $1) ; get length to write to stdout
(syscall WRITE $0 $1 $2) ; do the write syscall (syscall WRITE $0 $1 $2) ; do the write syscall

View File

@ -29,7 +29,6 @@
(halt)) (halt))
(label pln (label pln
(load-immediate $0 &terminal-namespace) (load-immediate $0 &terminal-namespace)
(syscall OPEN $0 $0 $0)
(load-immediate $3 &new-line) (load-immediate $3 &new-line)
(pop $1) (pop $1)
(string-length $2 $1) (string-length $2 $1)

View File

@ -12,7 +12,6 @@
(halt)) (halt))
(label pln (label pln
(load-immediate $0 &terminal-namespace) (load-immediate $0 &terminal-namespace)
(syscall OPEN $0 $0 $0)
(load-immediate $3 &new-line) (load-immediate $3 &new-line)
(pop $1) (pop $1)
(string-length $2 $1) (string-length $2 $1)

View File

@ -4,7 +4,7 @@
; use load immediate because it is a pointer to a string, not a value ; use load immediate because it is a pointer to a string, not a value
(load-immediate $0 &screen-namespace) (load-immediate $0 &screen-namespace)
(load-immediate $11 0) (load-immediate $11 0)
(syscall OPEN $0 $0 $11) (syscall OPEN $0 $11)
(load-immediate $16 1) ; device info call (load-immediate $16 1) ; device info call
(load-immediate $17 16) ; sizeof screen device info (load-immediate $17 16) ; sizeof screen device info
@ -22,7 +22,6 @@
(load-immediate $16 &mouse-namespace) (load-immediate $16 &mouse-namespace)
(load-immediate $3 12) ; malloc sizeof mouse data (load-immediate $3 12) ; malloc sizeof mouse data
(malloc $4 $3) (malloc $4 $3)
(syscall OPEN $16 $16 $4)
(label draw-loop (label draw-loop
; load mouse click data ; load mouse click data
@ -241,17 +240,14 @@
(jump-eq-nat &draw-loop $9 $11) (jump-eq-nat &draw-loop $9 $11)
(load $22 &SELECTED-COLOR) ; color (mul-nat $15 $8 $20) ; $15 = y * width
(load-immediate $1 5) ; size of brush (add-nat $15 $15 $7) ; $15 += x
(add-nat $15 $21 $15) ; $15 = base + pixel_offset
(load-immediate $1 4) ; need to add offset for fat pointer size
(add-nat $15 $15 $1)
(push $21) ;base (load $22 &SELECTED-COLOR) ; color
(push $20) ;width (store-8 $15 $22) ; draw color at screen [x,y]
(push $22) ; color
(push $7) ;x
(push $8) ;y
(push $1)
(push $1)
(call &draw-box)
(jump-eq-nat &draw-loop $10 $11)) (jump-eq-nat &draw-loop $10 $11))

View File

@ -9,7 +9,6 @@
(halt)) (halt))
(label pln (label pln
(load-immediate $0 &terminal-namespace) (load-immediate $0 &terminal-namespace)
(syscall OPEN $0 $0 $0)
(load-immediate $3 &new-line) (load-immediate $3 &new-line)
(pop $1) (pop $1)
(string-length $2 $1) (string-length $2 $1)

View File

@ -2,9 +2,7 @@
(label main (label main
(load-immediate $0 &screen-namespace) (load-immediate $0 &screen-namespace)
(load-immediate $11 0) (load-immediate $11 0)
(syscall OPEN $0 $0 $11) (syscall OPEN $0 $11)
(load-immediate $32 &terminal-namespace)
(syscall OPEN $32 $32 $11)
(load-immediate $16 1) ; device info call (load-immediate $16 1) ; device info call
(load-immediate $17 16) ; sizeof screen device info (load-immediate $17 16) ; sizeof screen device info
@ -22,10 +20,8 @@
(load-immediate $16 &mouse-namespace) (load-immediate $16 &mouse-namespace)
(load-immediate $3 12) ; malloc sizeof mouse data (load-immediate $3 12) ; malloc sizeof mouse data
(malloc $4 $3) (malloc $4 $3)
(syscall OPEN $16 $16 $4)
(nat-to-string $5 $4) (nat-to-string $5 $4)
(push $32)
(push $5) (push $5)
(call &pln) (call &pln)
@ -63,9 +59,9 @@
(jump-eq-nat &draw-loop $10 $11)) (jump-eq-nat &draw-loop $10 $11))
(halt)) (halt))
(label pln (label pln
(load-immediate $0 &terminal-namespace)
(load-immediate $3 &new-line) (load-immediate $3 &new-line)
(pop $1) (pop $1)
(pop $0)
(string-length $2 $1) (string-length $2 $1)
(syscall WRITE $0 $1 $2) (syscall WRITE $0 $1 $2)
(string-length $4 $3) (string-length $4 $3)