parse cli on our own

This commit is contained in:
2026-03-19 13:42:12 +01:00
parent 8758295641
commit efe9dfe238
5 changed files with 78 additions and 278 deletions

View File

@@ -15,8 +15,8 @@ struct CHIP8
sp: i64
reg: ptr
I: i64
delay_timer: i64
sound_timer: i64
delay_timer: u8
sound_timer: u8
keyboard: Array
display: ptr
keyboard_map: Array
@@ -49,8 +49,8 @@ func chip8_disassemble[c: CHIP8, ins_count: i64] : void
for i in 0..ins_count
io.printf("0x%x: ", c->pc)
let high: i64 = c->memory[c->pc]
let low: i64 = c->memory[c->pc + 1]
let high: i64 = c->memory[c->pc] as i64
let low: i64 = c->memory[c->pc + 1] as i64
c->pc = c->pc + 2
let ins: i64 = (high << 8) | low
@@ -145,8 +145,8 @@ func chip8_disassemble[c: CHIP8, ins_count: i64] : void
io.printf("??? (%x)\n", ins)
func chip8_step[c: CHIP8] : void
let high: i64 = c->memory[c->pc]
let low: i64 = c->memory[c->pc + 1]
let high: i64 = c->memory[c->pc] as i64
let low: i64 = c->memory[c->pc + 1] as i64
c->pc = c->pc + 2
let ins: i64 = (high << 8) | low
@@ -192,17 +192,17 @@ func chip8_step[c: CHIP8] : void
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
let 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]
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]
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
@@ -221,19 +221,19 @@ func chip8_step[c: CHIP8] : void
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)
let pixel_x: u8 = (c->reg[x] + col) % 64
let pixel_y: u8 = (c->reg[y] + row) % 32
let offset: i64 = 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(array.nth(c->keyboard_map, c->reg[x]))
if IsKeyDown(array.nth(c->keyboard_map, c->reg[x] as i64))
c->pc = c->pc + 2
else if kk == 0xa1
if !IsKeyDown(array.nth(c->keyboard_map, c->reg[x]))
if !IsKeyDown(array.nth(c->keyboard_map, c->reg[x] as i64))
c->pc = c->pc + 2
else if op == 0xf
if kk == 0x07
@@ -252,7 +252,7 @@ func chip8_step[c: CHIP8] : void
else if kk == 0x1E
c->I = c->I + c->reg[x]
else if kk == 0x29
c->I = c->reg[x] * 5
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
@@ -274,17 +274,17 @@ func chip8_free[c: CHIP8] : void
mem.free(c)
func main[argc: i64, argv: ptr] : i64
let path: str = 0
let disassemble: bool = 0
let path: str = 0 as str
let disassemble: bool = false
for i in 1..argc
let arg: str = mem.read64(argv + i * 8)
let arg: str = mem.read64(argv + i * 8) as str
if str.equal(arg, "-d")
disassemble = 1
disassemble = true
else
path = arg
if path == 0
if path as i64 == 0
io.println("Usage: chip8 -d <path>")
return 1