Compare commits

..

No commits in common. "71622c31339d68300ba98aba5aa0a47a1123019a" and "19aa72018e41d9e23ec904feff3b76d44fc4b365" have entirely different histories.

10 changed files with 47 additions and 72 deletions

1
.gitignore vendored
View File

@ -107,4 +107,3 @@ memory_dump.bin
build/ build/
.gdb_history .gdb_history
.vscode .vscode
.clangd

View File

@ -79,13 +79,14 @@ i32 screen_read(void *data, u8 *buffer, u32 size) {
i32 screen_write(void *data, const u8 *buffer, u32 size) { i32 screen_write(void *data, const u8 *buffer, u32 size) {
ScreenDeviceData *screen = (ScreenDeviceData *)data; ScreenDeviceData *screen = (ScreenDeviceData *)data;
USED(buffer);
if (size > screen->size * sizeof(u8)) { if (size > screen->size * sizeof(u8)) {
return -1; return -1;
} }
if (screen->texture && screen->renderer) { if (screen->texture && screen->renderer) {
SDL_UpdateTexture(screen->texture, nil, buffer, SDL_UpdateTexture(screen->texture, nil, &buffer[screen->pos],
screen->width); screen->width);
SDL_RenderClear(screen->renderer); SDL_RenderClear(screen->renderer);

View File

@ -146,9 +146,7 @@ bool compileAndSave(const char *source_file, const char *output_file, VM *vm) {
printf("Parse failed.\n"); printf("Parse failed.\n");
return false; return false;
} else { } else {
#ifdef ASM_DEBUG
expr_print(ast, 0); expr_print(ast, 0);
#endif
assemble(vm, ast); assemble(vm, ast);
expr_free(ast); expr_free(ast);
@ -243,6 +241,9 @@ void register_sdl_devices(VM *vm) {
screen_data.width = 640; screen_data.width = 640;
screen_data.height = 480; screen_data.height = 480;
screen_data.size = 640 * 480; screen_data.size = 640 * 480;
screen_data.pos = vm->mp;
vm->mp += screen_data.size; /* advance memory pointer */
vm->frames[vm->fp].end = vm->mp;
vm_register_device(vm, "/dev/screen/0", "screen", &screen_data, &screen_ops); vm_register_device(vm, "/dev/screen/0", "screen", &screen_data, &screen_ops);
@ -253,6 +254,9 @@ void register_sdl_devices(VM *vm) {
mouse_data.btn3 = 0; mouse_data.btn3 = 0;
mouse_data.btn4 = 0; mouse_data.btn4 = 0;
mouse_data.size = 12; mouse_data.size = 12;
mouse_data.pos = vm->mp;
vm->mp += mouse_data.size; /* advance memory pointer */
vm->frames[vm->fp].end = vm->mp;
vm_register_device(vm, "/dev/mouse/0", "mouse", &mouse_data, &mouse_ops); vm_register_device(vm, "/dev/mouse/0", "mouse", &mouse_data, &mouse_ops);

View File

@ -1,6 +1,7 @@
#ifndef ZRE_COMMON_H #ifndef ZRE_COMMON_H
#define ZRE_COMMON_H #define ZRE_COMMON_H
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@ -11,10 +12,6 @@ typedef int16_t i16;
typedef uint32_t u32; typedef uint32_t u32;
typedef int32_t i32; typedef int32_t i32;
#define true 1
#define false 0
#define bool u8
#define nil NULL #define nil NULL
#define USED(x) ((void)(x)) #define USED(x) ((void)(x))

View File

@ -5,6 +5,8 @@
(load-immediate $0 &screen-namespace) (load-immediate $0 &screen-namespace)
(load-immediate $11 0) (load-immediate $11 0)
(syscall OPEN $0 $11) (syscall OPEN $0 $11)
; draw to the device because otherwise the screen wont open
(syscall WRITE $0 $0 $0)
(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
@ -13,11 +15,10 @@
(load-immediate $1 12) ; offset for width (load-immediate $1 12) ; offset for width
(add-nat $19 $18 $1) (add-nat $19 $18 $1)
(load-r $20 $19) ; load width (load-r $20 $19) ; load width
(load-immediate $1 8) ; offset for size (load-immediate $1 4) ; offset for width
(add-nat $19 $18 $1) (add-nat $19 $18 $1)
(load-r $22 $19) ; load size (load-r $21 $19) ; load base pointer
(malloc $21 $22) ; malloc frame buffer (add-nat $21 $21 $1) ; need to add offset for fat pointer size
(syscall WRITE $0 $21 $22)
(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
@ -42,8 +43,6 @@
(load-immediate $14 20) ; box size (load-immediate $14 20) ; box size
; first row ; first row
(push $21)
(push $20)
(load $1 &BLACK) (load $1 &BLACK)
(push $1) (push $1)
(load-immediate $12 1) (load-immediate $12 1)
@ -61,8 +60,6 @@
(push $9) ; is btn1 clicked? (push $9) ; is btn1 clicked?
(call &set-color-if-clicked) (call &set-color-if-clicked)
(push $21)
(push $20)
(load $1 &WHITE) (load $1 &WHITE)
(push $1) (push $1)
(load-immediate $12 21) (load-immediate $12 21)
@ -81,8 +78,6 @@
(call &set-color-if-clicked) (call &set-color-if-clicked)
; row 2 ; row 2
(push $21)
(push $20)
(load $1 &CHARCOAL) (load $1 &CHARCOAL)
(push $1) (push $1)
(load-immediate $12 1) (load-immediate $12 1)
@ -100,8 +95,6 @@
(push $9) ; is btn1 clicked? (push $9) ; is btn1 clicked?
(call &set-color-if-clicked) (call &set-color-if-clicked)
(push $21)
(push $20)
(load $1 &DARK-GRAY) (load $1 &DARK-GRAY)
(push $1) (push $1)
(load-immediate $12 21) (load-immediate $12 21)
@ -120,8 +113,6 @@
(call &set-color-if-clicked) (call &set-color-if-clicked)
; row 3 ; row 3
(push $21)
(push $20)
(load $1 &RED) (load $1 &RED)
(push $1) (push $1)
(load-immediate $12 1) (load-immediate $12 1)
@ -139,8 +130,6 @@
(push $9) ; is btn1 clicked? (push $9) ; is btn1 clicked?
(call &set-color-if-clicked) (call &set-color-if-clicked)
(push $21)
(push $20)
(load $1 &ORANGE) (load $1 &ORANGE)
(push $1) (push $1)
(load-immediate $12 21) (load-immediate $12 21)
@ -159,8 +148,6 @@
(call &set-color-if-clicked) (call &set-color-if-clicked)
; row 3 ; row 3
(push $21)
(push $20)
(load $1 &YELLOW) (load $1 &YELLOW)
(push $1) (push $1)
(load-immediate $12 1) (load-immediate $12 1)
@ -178,8 +165,6 @@
(push $9) ; is btn1 clicked? (push $9) ; is btn1 clicked?
(call &set-color-if-clicked) (call &set-color-if-clicked)
(push $21)
(push $20)
(load $1 &GREEN) (load $1 &GREEN)
(push $1) (push $1)
(load-immediate $12 21) (load-immediate $12 21)
@ -198,8 +183,6 @@
(call &set-color-if-clicked) (call &set-color-if-clicked)
; row 4 ; row 4
(push $21)
(push $20)
(load $1 &BLUE) (load $1 &BLUE)
(push $1) (push $1)
(load-immediate $12 1) (load-immediate $12 1)
@ -217,8 +200,6 @@
(push $9) ; is btn1 clicked? (push $9) ; is btn1 clicked?
(call &set-color-if-clicked) (call &set-color-if-clicked)
(push $21)
(push $20)
(load $1 &PURPLE) (load $1 &PURPLE)
(push $1) (push $1)
(load-immediate $12 21) (load-immediate $12 21)
@ -236,7 +217,7 @@
(push $9) ; is btn1 clicked? (push $9) ; is btn1 clicked?
(call &set-color-if-clicked) (call &set-color-if-clicked)
(syscall WRITE $0 $21 $22) (syscall WRITE $0 $0 $0)
(jump-eq-nat &draw-loop $9 $11) (jump-eq-nat &draw-loop $9 $11)
@ -301,24 +282,13 @@
(pop $3) ; y (pop $3) ; y
(pop $2) ; x (pop $2) ; x
(pop $1) ; color (pop $1) ; color
(pop $20)
(pop $21)
; Constants ; Constants
(load $4 &GRAY) (load $4 &GRAY)
(load $10 &SELECTED-COLOR)
(jump-eq-int &set-selected $10 $1)
(jump-eq-int &end-set-selected $4 $4)
(label set-selected)
(load $4 &DARK-GRAY)
(label end-set-selected)
(load-immediate $5 20) ; outline size (load-immediate $5 20) ; outline size
(load-immediate $6 17) ; fill size (load-immediate $6 17) ; fill size
(load-immediate $7 2) ; offset (load-immediate $7 2) ; offset
(push $21) ; base
(push $20) ; width
(push $4) ; color (gray) (push $4) ; color (gray)
(push $2) ; x (push $2) ; x
(push $3) ; y (push $3) ; y
@ -329,8 +299,6 @@
(add-int $8 $2 $7) ; x + 2 (add-int $8 $2 $7) ; x + 2
(add-int $9 $3 $7) ; y + 2 (add-int $9 $3 $7) ; y + 2
(push $21) ; base
(push $20) ; width
(push $1) ; color (original) (push $1) ; color (original)
(push $8) ; x + 2 (push $8) ; x + 2
(push $9) ; y + 2 (push $9) ; y + 2
@ -349,8 +317,20 @@
(pop $13) ; y_start (pop $13) ; y_start
(pop $11) ; x_start (pop $11) ; x_start
(pop $3) ; color (pop $3) ; color
(pop $2) ; width
(pop $21) ; base ;; get the screen width dynamically from the device
(load-immediate $0 &screen-namespace)
(load-immediate $16 1) ; device info call
(load-immediate $17 16) ; sizeof screen device info
(malloc $18 $17)
(syscall IOCTL $0 $16 $18)
(load-immediate $1 12) ; offset for width
(add-nat $19 $18 $1)
(load-r $2 $19) ; load width
(load-immediate $1 4) ; offset for width
(add-nat $19 $18 $1)
(load-r $21 $19) ; load base pointer
(add-nat $21 $21 $1) ; need to add offset for fat pointer size
; Constants ; Constants
(load-immediate $1 1) ; increment (load-immediate $1 1) ; increment
@ -359,8 +339,6 @@
(mul-int $15 $13 $2) ; $15 = y * 640 (mul-int $15 $13 $2) ; $15 = y * 640
(add-int $15 $15 $11) ; $15 += x (add-int $15 $15 $11) ; $15 += x
(add-nat $15 $21 $15) ; $15 = base + pixel_offset (add-nat $15 $21 $15) ; $15 = base + pixel_offset
(load-immediate $25 4)
(add-nat $15 $15 $25) ; need to add offset for fat pointer size
(register-move $4 $15) (register-move $4 $15)
; Outer loop: height times ; Outer loop: height times

View File

@ -3,6 +3,7 @@
(load-immediate $0 &screen-namespace) (load-immediate $0 &screen-namespace)
(load-immediate $11 0) (load-immediate $11 0)
(syscall OPEN $0 $11) (syscall OPEN $0 $11)
(syscall WRITE $0 $0 $0)
(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
@ -11,11 +12,10 @@
(load-immediate $1 12) ; offset for width (load-immediate $1 12) ; offset for width
(add-nat $19 $18 $1) (add-nat $19 $18 $1)
(load-r $20 $19) ; load width (load-r $20 $19) ; load width
(load-immediate $1 8) ; offset for size (load-immediate $1 4) ; offset for width
(add-nat $19 $18 $1) (add-nat $19 $18 $1)
(load-r $22 $19) ; load size (load-r $21 $19) ; load base pointer
(malloc $21 $22) ; malloc frame buffer (add-nat $21 $21 $1) ; need to add offset for fat pointer size
(syscall WRITE $0 $21 $22)
(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
@ -24,33 +24,29 @@
(label draw-loop (label draw-loop
; load mouse click data ; load mouse click data
(syscall READ $16 $2 $3 $4) (syscall READ $16 $2 $3 $4)
(load-immediate $5 12) ; offset for btn1
(add-nat $6 $5 $2)
(load-r8 $9 $6) ; load btn1 pressed
(jump-eq-nat &draw-loop $9 $11)
(load-immediate $5 4) ; offset for x (load-immediate $5 4) ; offset for x
(add-nat $6 $5 $2) (add-nat $6 $5 $2)
(load-r $7 $6) ; load x (load-r $7 $6) ; load x
(load-immediate $5 8) ; offset for y (load-immediate $5 8) ; offset for y
(add-nat $6 $5 $2) (add-nat $6 $5 $2)
(load-r $8 $6) ; load y (load-r $8 $6) ; load y
(load-immediate $5 12) ; offset for btn1
(add-nat $6 $5 $2)
(load-r8 $9 $6) ; load btn1 pressed
(load-immediate $5 13) ; offset for btn2
(add-nat $6 $5 $2)
(load-r8 $10 $6) ; load btn2 pressed
(jump-eq-nat &draw-loop $9 $11)
; Compute start address: y*width + x ; Compute start address: y*width + x
(mul-nat $15 $8 $20) ; $15 = y * width (mul-nat $15 $8 $20) ; $15 = y * width
(add-nat $15 $15 $7) ; $15 += x (add-nat $15 $15 $7) ; $15 += x
(add-nat $15 $21 $15) ; $15 = base + pixel_offset (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)
(load $3 &WHITE) ; color (load $3 &WHITE) ; color
(store-8 $15 $3) ; draw color at screen [x,y] (store-8 $15 $3) ; draw color at screen [x,y]
(syscall WRITE $0 $21 $22) ; redraw (syscall WRITE $0 $0 $0) ; redraw
(load-immediate $5 13) ; offset for btn2
(add-nat $6 $5 $2)
(load-r8 $10 $6) ; load btn2 pressed
(jump-eq-nat &draw-loop $10 $11)) (jump-eq-nat &draw-loop $10 $11))
(halt)) (halt))