// needs to be compiled with -m -C="-lraylib" extern InitWindow extern SetTargetFPS extern WindowShouldClose extern BeginDrawing extern ClearBackground extern DrawRectangle extern EndDrawing extern IsKeyDown struct CHIP8 memory: ptr pc: i64 stack: array sp: i64 reg: ptr I: i64 delay_timer: i64 sound_timer: i64 keyboard: array display: ptr keyboard_map: array func chip8_create[] : CHIP8 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->reg = mem.alloc(16) mem.zero(c->reg, 16) c->stack = [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] 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] for i in 0..80 c->memory[i] = array.nth(fonts, i) mem.free(fonts) c->pc = 0x200 // see KeyboardKey in raylib.h c->keyboard_map = [49, 50, 51, 52, 53, 265, 32, 263, 264, 262, 87, 69, 82, 84, 89, 85] return c func chip8_disassemble[c: CHIP8, ins_count: i64] : void for i in 0..ins_count io.print("0x") io.print_i64_hex(c->pc) io.print(": ") let high: i64 = c->memory[c->pc] let low: i64 = c->memory[c->pc + 1] c->pc = c->pc + 2 let ins: i64 = (high << 8) | low let nnn: i64 = ins & 0x0fff let kk: i64 = ins & 0x00ff let n: i64 = ins & 0x000f let x: i64 = (ins >> 8) & 0x0f let y: i64 = (ins >> 4) & 0x0f let op: i64 = (ins >> 12) & 0x0f if op == 0x0 if nnn == 0x0e0 io.println("CLS") else if nnn == 0x0ee io.println("RET") else io.print("SYS ") io.println_i64(nnn) else if op == 0x1 io.print("JP 0x") io.print_i64_hex(nnn) io.print("\n") else if op == 0x2 io.print("CALL ") io.println_i64(nnn) else if op == 0x3 io.print("SE V") io.print_i64_hex(x) io.print(", ") io.println_i64(kk) else if op == 0x4 io.print("SNE V") io.print_i64_hex(x) io.print(", ") io.println_i64(kk) else if op == 0x5 io.print("SE V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if op == 0x6 io.print("LD V") io.print_i64_hex(x) io.print(", ") io.println_i64(kk) else if op == 0x7 io.print("ADD V") io.print_i64_hex(x) io.print(", ") io.println_i64(kk) else if op == 0x8 if n == 0x0 io.print("LD V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if n == 0x1 io.print("OR V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if n == 0x2 io.print("AND V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if n == 0x3 io.print("XOR V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if n == 0x4 io.print("ADD V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if n == 0x5 io.print("SUB V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if n == 0x6 io.print("SHR V") io.print_i64_hex(x) io.print("\n") else if n == 0x7 io.print("SUBN V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if n == 0xE io.print("SHL V") io.print_i64_hex(x) io.print("\n") else io.print("??? (") io.print_i64_hex(ins) io.println(")") else if op == 0x9 io.print("SNE V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print("\n") else if op == 0xa io.print("LD I, 0x") io.print_i64_hex(nnn) io.print("\n") else if op == 0xb io.print("JP V0, ") io.println_i64(nnn) else if op == 0xc io.print("RND V") io.print_i64_hex(x) io.print(", ") io.println_i64(kk) else if op == 0xd io.print("DRW V") io.print_i64_hex(x) io.print(", V") io.print_i64_hex(y) io.print(", ") io.println_i64(n) else if op == 0xe if kk == 0x9e io.print("SKP V") io.print_i64_hex(x) io.print("\n") else if kk == 0xa1 io.print("SKNP V") io.print_i64_hex(x) io.print("\n") else io.print("??? (") io.print_i64_hex(ins) io.println(")") else if op == 0xf if kk == 0x07 io.print("LD V") io.print_i64_hex(x) io.println(", DT") else if kk == 0x0A io.print("LD V") io.print_i64_hex(x) io.println(", K") else if kk == 0x15 io.print("LD DT, V") io.print_i64_hex(x) io.print("\n") else if kk == 0x18 io.print("LD ST, V") io.print_i64_hex(x) io.print("\n") else if kk == 0x1E io.print("ADD I, V") io.print_i64_hex(x) io.print("\n") else if kk == 0x29 io.print("LD F, V") io.print_i64_hex(x) io.print("\n") else if kk == 0x33 io.print("LD B, V") io.print_i64_hex(x) io.print("\n") else if kk == 0x55 io.print("LD [I], V") io.print_i64_hex(x) io.print("\n") else if kk == 0x65 io.print("LD V") io.print_i64_hex(x) io.println(", [I]") else io.print("??? (") io.print_i64_hex(ins) io.println(")") else io.print("??? (") io.print_i64_hex(ins) io.println(")") func chip8_step[c: CHIP8] : void let high: i64 = c->memory[c->pc] let low: i64 = c->memory[c->pc + 1] c->pc = c->pc + 2 let ins: i64 = (high << 8) | low let nnn: i64 = ins & 0x0fff let kk: i64 = ins & 0x00ff let n: i64 = ins & 0x000f let x: i64 = (ins >> 8) & 0x0f let y: i64 = (ins >> 4) & 0x0f let op: i64 = (ins >> 12) & 0x0f if op == 0x0 if nnn == 0x0e0 mem.zero(c->display, 64 * 32) else if nnn == 0x0ee c->pc = array.nth(c->stack, c->sp) c->sp = c->sp - 1 else if op == 0x1 c->pc = nnn else if op == 0x2 c->sp = c->sp + 1 array.set(c->stack, c->sp, c->pc) c->pc = nnn else if op == 0x3 if c->reg[x] == kk c->pc = c->pc + 2 else if op == 0x4 if c->reg[x] != kk c->pc = c->pc + 2 else if op == 0x5 if c->reg[x] == c->reg[y] c->pc = c->pc + 2 else if op == 0x6 c->reg[x] = kk else if op == 0x7 c->reg[x] = c->reg[x] + kk else if op == 0x8 if n == 0x0 c->reg[x] = c->reg[y] else if n == 0x1 c->reg[x] = c->reg[x] | c->reg[y] else if n == 0x2 c->reg[x] = c->reg[x] & c->reg[y] else if n == 0x3 c->reg[x] = c->reg[x] ^ c->reg[y] else if n == 0x4 let res: i64 = c->reg[x] + c->reg[y] c->reg[0xf] = res > 0xff c->reg[x] = res else if n == 0x5 c->reg[0xf] = c->reg[x] > c->reg[y] c->reg[x] = c->reg[x] - c->reg[y] else if n == 0x6 c->reg[0xf] = c->reg[x] & 0x1 c->reg[x] = c->reg[x] >> 1 else if n == 0x7 c->reg[0xf] = c->reg[y] > c->reg[x] c->reg[x] = c->reg[y] - c->reg[x] else if n == 0xE c->reg[0xf] = (c->reg[x] & 0x80) >> 7 c->reg[x] = c->reg[x] << 1 else if op == 0x9 if c->reg[x] != c->reg[y] c->pc = c->pc + 2 else if op == 0xa c->I = nnn else if op == 0xb c->pc = nnn + c->reg[0] else if op == 0xc c->reg[x] = (math.floor(os.urandom_i64()) % 256) & kk else if op == 0xd c->reg[0xf] = 0 for row in 0..n for col in 0..8 if (c->memory[c->I + row] & (0x80 >> col)) != 0 let pixel_x: i64 = (c->reg[x] + col) % 64 let pixel_y: i64 = (c->reg[y] + row) % 32 let offset: i64 = pixel_x + (pixel_y * 64) if c->display[offset] == 1 c->reg[0xf] = 1 c->display[offset] = c->display[offset] ^ 1 else if op == 0xe if kk == 0x9e if IsKeyDown(array.nth(c->keyboard_map, c->reg[x])) c->pc = c->pc + 2 else if kk == 0xa1 if !IsKeyDown(array.nth(c->keyboard_map, c->reg[x])) c->pc = c->pc + 2 else if op == 0xf if kk == 0x07 c->reg[x] = c->delay_timer else if kk == 0x0A let key_pressed: bool = false while !key_pressed && !WindowShouldClose() for i in 0..16 if IsKeyDown(array.nth(c->keyboard_map, i)) key_pressed = true break else if kk == 0x15 c->delay_timer = c->reg[x] else if kk == 0x18 c->sound_timer = c->reg[x] else if kk == 0x1E c->I = c->I + c->reg[x] else if kk == 0x29 c->I = c->reg[x] * 5 else if kk == 0x33 c->memory[c->I] = c->reg[x] / 100 c->memory[c->I + 1] = (c->reg[x] / 10) % 10 c->memory[c->I + 2] = c->reg[x] % 10 else if kk == 0x55 for i in 0..x+1 c->memory[c->I + i] = c->reg[i] else if kk == 0x65 for i in 0..x+1 c->reg[i] = c->memory[c->I + i] func chip8_free[c: CHIP8] : void mem.free(c->memory) array.free(c->stack) mem.free(c->reg) array.free(c->keyboard) mem.free(c->display) array.free(c->keyboard_map) mem.free(c) 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, 0xff000000) EndDrawing()