From 12f283e5f8e0da4b74917b67cb9e11d66b0bd2a0 Mon Sep 17 00:00:00 2001 From: Toni Date: Wed, 11 Mar 2026 16:21:57 +0100 Subject: [PATCH] zero initialize structs, begin porting my chip8 emulator --- examples/chip8.zr | 96 +++++++++++++++++++++++++++++++++++++++++++ src/codegen_x86_64.rs | 13 +++--- src/std/std.zr | 19 ++++++++- 3 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 examples/chip8.zr diff --git a/examples/chip8.zr b/examples/chip8.zr new file mode 100644 index 0000000..4822b17 --- /dev/null +++ b/examples/chip8.zr @@ -0,0 +1,96 @@ +// needs to be compiled with -m -C="-lraylib" +extern InitWindow +extern SetTargetFPS +extern WindowShouldClose +extern BeginDrawing +extern ClearBackground +extern DrawRectangle +extern EndDrawing + +struct CHIP8 + memory: ptr + pc: i64 + stack: array + sp: i64 + reg: array + I: i64 + delay_timer: i64 + sound_timer: i64 + keyboard: array + display: ptr + +func chip8_create[] : CHIP8 + let fonts: array = [0xf0, 0x90, 0x90, 0x90, 0xf0, 0x20, 0x60, 0x20, 0x20, 0x70, 0xf0, 0x10, 0xf0, 0x80, 0xf0, 0xf0, 0x10, 0xf0, 0x10, 0xf0, 0x90, 0x90, 0xf0, 0x10, 0x10, 0xf0, 0x80, 0xf0, 0x10, 0xf0, 0xf0, 0x80, 0xf0, 0x90, 0xf0, 0xf0, 0x10, 0x20, 0x40, 0x40, 0xf0, 0x90, 0xf0, 0x90, 0xf0, 0xf0, 0x90, 0xf0, 0x10, 0xf0, 0xf0, 0x90, 0xf0, 0x90, 0x90, 0xe0, 0x90, 0xe0, 0x90, 0xe0, 0xf0, 0x80, 0x80, 0x80, 0xf0, 0xe0, 0x90, 0x90, 0x90, 0xe0, 0xf0, 0x80, 0xf0, 0x80, 0xf0, 0xf0, 0x80, 0xf0, 0x80, 0x80] + + let c: CHIP8 = new CHIP8 + c->memory = mem.alloc(4096) + mem.zero(c->memory, 4096) + c->display = mem.alloc(64*32) + mem.zero(c->display, 64*32) + + c->stack = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + c->reg = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + c->keyboard = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + + for i in 0..80 + c->memory[i] = array.nth(fonts, i) + mem.free(fonts) + + c->pc = 0x200 + return c + +func chip8_disassemble[c: CHIP8, ins_count: i64] : void + return 0 + +func chip8_step[c: CHIP8] : void + return 0 + +func main[argc: i64, argv: ptr] : i64 + let path: str = 0 + let disassemble: bool = 0 + + for i in 1..argc + let arg: str = mem.read64(argv + i * 8) + if str.equal(arg, "-d") + disassemble = 1 + else + path = arg + + if path == 0 + io.println("Usage: chip8 -d ") + return 1 + + let c: CHIP8 = chip8_create() + + let bytes_size: ptr = 0 + let bytes: ptr = io.read_binary_file(path, ^bytes_size) + + for i in 0..bytes_size + c->memory[0x200 + i] = bytes[i] + + if disassemble + chip8_disassemble(c, bytes_size / 2) + return 0 + + InitWindow(640, 320, "CHIP-8") + SetTargetFPS(60) + + while !WindowShouldClose() + if c->delay_timer > 0 + c->delay_timer = c->delay_timer - 1 + if c->sound_timer > 0 + c->sound_timer = c->sound_timer - 1 + // TODO: buzzer + + for i in 0..25 + chip8_step(c) + + BeginDrawing() + + ClearBackground(0xffffffff) + for y in 0..32 + for x in 0..64 + if c->display[x + y * 64] == 1 + DrawRectangle(x * 10, y * 10, 10, 10, 0x000000ff) + + EndDrawing() \ No newline at end of file diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index 74d1fb1..d0d8937 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -111,11 +111,6 @@ _builtin_read64: mov rax, qword [rdi] ret -section .text._builtin_set8 -_builtin_set8: - mov [rdi], sil - ret - section .text._builtin_set64 _builtin_set64: mov [rdi], rsi @@ -671,8 +666,14 @@ _builtin_environ: Expr::New(struct_name) => { let struct_fields = &self.analyzer.structs[&struct_name.lexeme]; - emit!(&mut self.output, " mov rdi, {}", struct_fields.len() * 8); + let memory_size = struct_fields.len() * 8; + emit!(&mut self.output, " mov rdi, {}", memory_size); emit!(&mut self.output, " call mem.alloc"); + emit!(&mut self.output, " push rax"); + emit!(&mut self.output, " mov rdi, rax"); + emit!(&mut self.output, " mov rsi, {}", memory_size); + emit!(&mut self.output, " call mem.zero"); + emit!(&mut self.output, " pop rax"); } Expr::MemberAccess { left, field } => { let offset = self.get_field_offset(env, left, field)?; diff --git a/src/std/std.zr b/src/std/std.zr index abed1b7..2b1fdb7 100644 --- a/src/std/std.zr +++ b/src/std/std.zr @@ -77,11 +77,12 @@ func io.read_line[]: str let buffer: str = mem.alloc(MAX_SIZE + 1) let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE) if n < 0 + mem.free(buffer) return "" buffer[n] = 0 return buffer -func io.read_file[path: str]: str +func io.read_file[path: str] : str let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0) if fd <= 0 dbg.panic("failed to open file") @@ -91,8 +92,22 @@ func io.read_file[path: str]: str let buffer: str = mem.alloc(size + 1) let n: i64 = _builtin_syscall(SYS_read, fd, buffer, size) - buffer[n] = 0 _builtin_syscall(SYS_close, fd) + buffer[n] = 0 + return buffer + +func io.read_binary_file[path: str, len_ptr: ptr] : ptr + let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0) + if fd < 0 + dbg.panic("failed to open file") + + let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2) + _builtin_syscall(SYS_lseek, fd, 0, 0) + + let buffer: ptr = mem.alloc(size) + _builtin_syscall(SYS_read, fd, buffer, size) + _builtin_syscall(SYS_close, fd) + mem.write64(len_ptr, size) return buffer func io.write_file[path: str, content: str] : void