Files
zern/examples/chip8.zr

327 lines
9.8 KiB
Plaintext

// needs to be compiled with -m -C "-lraylib"
include "std/io.zr"
include "std/os.zr"
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: u8
sound_timer: u8
keyboard: Array
display: ptr
keyboard_map: Array
func CHIP8.init[c: CHIP8] : void
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]
fonts := [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] = fonts->nth(i)
fonts->free()
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]
func CHIP8.free[c: CHIP8] : void
c->memory->free()
c->stack->free()
c->reg->free()
c->keyboard->free()
c->display->free()
c->keyboard_map->free()
mem.free(c)
func CHIP8.disassemble[c: CHIP8, ins_count: i64] : void
for i in 0..ins_count
io.printf("0x%x: ", c->pc)
high := c->memory[c->pc] as i64
low := c->memory[c->pc + 1] as i64
c->pc += 2
ins := (high << 8) | low
nnn := ins & 0x0fff
kk := ins & 0x00ff
n := ins & 0x000f
x := (ins >> 8) & 0x0f
y := (ins >> 4) & 0x0f
op := (ins >> 12) & 0x0f
if op == 0x0
if nnn == 0x0e0
io.println("CLS")
else if nnn == 0x0ee
io.println("RET")
else
io.printf("SYS %d\n", nnn)
else if op == 0x1
io.printf("JP 0x%x\n", nnn)
else if op == 0x2
io.printf("CALL %d\n", nnn)
else if op == 0x3
io.printf("SE V%x, %d\n", x, kk)
else if op == 0x4
io.printf("SNE V%x, %d\n", x, kk)
else if op == 0x5
io.printf("SE V%x, V%x\n", x, y)
else if op == 0x6
io.printf("LD V%x, %d\n", x, kk)
else if op == 0x7
io.printf("ADD V%x, %d\n", x, kk)
else if op == 0x8
if n == 0x0
io.printf("LD V%x, V%x\n", x, y)
else if n == 0x1
io.printf("OR V%x, V%x\n", x, y)
else if n == 0x2
io.printf("AND V%x, V%x\n", x, y)
else if n == 0x3
io.printf("XOR V%x, V%x\n", x, y)
else if n == 0x4
io.printf("ADD V%x, V%x\n", x, y)
else if n == 0x5
io.printf("SUB V%x, V%x\n", x, y)
else if n == 0x6
io.printf("SHR V%x\n", x)
else if n == 0x7
io.printf("SUBN V%x, V%x\n", x, y)
else if n == 0xE
io.printf("SHL V%x\n", x)
else
io.printf("??? (%x)\n", ins)
else if op == 0x9
io.printf("SNE V%x, V%x\n", x, y)
else if op == 0xa
io.printf("LD I, 0x%x\n", nnn)
else if op == 0xb
io.printf("JP V0, %d\n", nnn)
else if op == 0xc
io.printf("RND V%x, %d\n", x, kk)
else if op == 0xd
io.printf("DRW V%x, V%x, %d\n", x, y, n)
else if op == 0xe
if kk == 0x9e
io.printf("SKP V%x\n", x)
else if kk == 0xa1
io.printf("SKNP V%x\n", x)
else
io.printf("??? (%x)\n", ins)
else if op == 0xf
if kk == 0x07
io.printf("LD V%x, DT\n", x)
else if kk == 0x0A
io.printf("LD V%x, K\n", x)
else if kk == 0x15
io.printf("LD DT, V%x\n", x)
else if kk == 0x18
io.printf("LD ST, V%x\n", x)
else if kk == 0x1E
io.printf("ADD I, V%x\n", x)
else if kk == 0x29
io.printf("LD F, V%x\n", x)
else if kk == 0x33
io.printf("LD B, V%x\n", x)
else if kk == 0x55
io.printf("LD [I], V%x\n", x)
else if kk == 0x65
io.printf("LD V%x, [I]\n", x)
else
io.printf("??? (%x)\n", ins)
else
io.printf("??? (%x)\n", ins)
func CHIP8.step[c: CHIP8] : void
high := c->memory[c->pc] as i64
low := c->memory[c->pc + 1] as i64
c->pc += 2
ins := (high << 8) | low
nnn := ins & 0x0fff
kk := ins & 0x00ff
n := ins & 0x000f
x := (ins >> 8) & 0x0f
y := (ins >> 4) & 0x0f
op := (ins >> 12) & 0x0f
if op == 0x0
if nnn == 0x0e0
mem.zero(c->display, 64 * 32)
else if nnn == 0x0ee
c->pc = c->stack->nth(c->sp)
c->sp = c->sp - 1
else if op == 0x1
c->pc = nnn
else if op == 0x2
c->sp += 1
c->stack->set(c->sp, c->pc)
c->pc = nnn
else if op == 0x3
if c->reg[x] == kk
c->pc += 2
else if op == 0x4
if c->reg[x] != kk
c->pc += 2
else if op == 0x5
if c->reg[x] == c->reg[y]
c->pc += 2
else if op == 0x6
c->reg[x] = kk
else if op == 0x7
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
res : u8 = c->reg[x] + c->reg[y]
c->reg[0xf] = (res > 0xff) as u8
c->reg[x] = res
else if n == 0x5
c->reg[0xf] = (c->reg[x] > c->reg[y]) as u8
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]) as u8
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 += 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] = (os.urandom_i64()->abs() % 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
pixel_x : u8 = (c->reg[x] + col) % 64
pixel_y : u8 = (c->reg[y] + row) % 32
offset := pixel_x as i64 + (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(c->keyboard_map->nth(c->reg[x] as i64))
c->pc += 2
else if kk == 0xa1
if !IsKeyDown(c->keyboard_map->nth(c->reg[x] as i64))
c->pc += 2
else if op == 0xf
if kk == 0x07
c->reg[x] = c->delay_timer
else if kk == 0x0A
key_pressed := false
while !key_pressed && !WindowShouldClose()
for i in 0..16
if IsKeyDown(c->keyboard_map->nth(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->reg[x]
else if kk == 0x29
c->I = c->reg[x] as i64 * 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 main[argc: i64, argv: ptr] : i64
path := 0 as str
disassemble := false
for i in 1..argc
arg := mem.read64(argv + i * 8) as str
if arg->equal("-d")
disassemble = true
else
path = arg
if path as i64 == 0
io.println("Usage: chip8 -d <path>")
return 1
c := new CHIP8
c->init()
buffer, ok := io.read_binary_file(path)
if !ok
panic("failed to read file")
for i in 0..buffer->size
c->memory[0x200 + i] = buffer->data[i]
if disassemble
c->disassemble(buffer->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
c->step()
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()