From 835f96996372715f26b6f5df84fd1e27619c653f Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Mon, 19 Feb 2024 22:21:14 -0300 Subject: [PATCH] Finalize chip8 and init fix graphics --- gl | 0 sgl | 0 src/chip8.v | 51 ++++++++++++++++++++++----------------------------- src/main.v | 43 ++++++++++++++++++++++++++++--------------- src/utils.v | 2 +- 5 files changed, 51 insertions(+), 45 deletions(-) delete mode 100644 gl delete mode 100644 sgl diff --git a/gl b/gl deleted file mode 100644 index e69de29..0000000 diff --git a/sgl b/sgl deleted file mode 100644 index e69de29..0000000 diff --git a/src/chip8.v b/src/chip8.v index 8260d58..3a0a2af 100644 --- a/src/chip8.v +++ b/src/chip8.v @@ -39,35 +39,32 @@ struct Chip8{ v [num_of_registers]u8 screen Screen pc u16 = 0x200 - i int + i u16 stack Stack delay_timer u8 + is_draw bool } fn (mut chip Chip8) start_cpu(){ chip.screen = Screen{} - mut index := 0 // load font in the memory for sprite in font { - chip.set_ram(sprite, index) + chip.set_ram(sprite, chip.i) } } -fn (mut chip Chip8) run(){ - - for{ - mut instruction := chip.fetch() - println(instruction) - chip.decode_and_run(instruction) - } +fn (mut chip Chip8) run(){ + mut instruction := chip.fetch() + chip.decode_and_run(instruction) } -fn (mut chip Chip8) set_ram(instructions []u8, index int) { +fn (mut chip Chip8) set_ram(instructions []u8, index u16) { mut j := index + for i := 0; i < instructions.len; i++ { chip.ram[j] = instructions[i] j++ @@ -96,6 +93,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { mut opcode_msb := instruction & 0xF000 mut opcode_lsb := instruction & 0x00FF mut is_jump := false + chip.is_draw = false match opcode_msb{ @@ -108,7 +106,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { } 0x00E0 { - + chip.is_draw = true for i := 0; i < chip.screen.display_height; i++{ for j := 0; j < chip.screen.display_width; j++ { chip.screen.display[i][j] = 0 @@ -125,7 +123,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { 0x1000 { nnn = instruction & 0x0FFF - chip.pc = u8(nnn) + chip.pc = u16(nnn) is_jump = true // Jumps to address NNN } @@ -278,7 +276,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { 0xA000 { nnn = instruction & 0x0FFF - chip.i = nnn + chip.i = u16(nnn) } 0xB000 { @@ -298,37 +296,32 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { } 0xD000 { + + chip.is_draw = true x = (instruction & 0x0F00) >> 8 y = (instruction & 0x00F0) >> 4 n = (instruction & 0x000F) - mut regvy := u8(chip.v[y] % chip.screen.display_height) - mut regvx := u8(chip.v[x] % chip.screen.display_width) + mut regvy := u16(chip.v[y]) + mut regvx := u16(chip.v[x]) chip.v[f] = 0 for y_coord := 0; y_coord < n; y_coord++ { - - regn := chip.ram[chip.i + y_coord] + pixel := chip.ram[chip.i + y_coord] for x_coord := 0; x_coord < 8; x_coord++ { - if (regvx + x_coord) < chip.screen.display_width && (regvy + y_coord) < chip.screen.display_height { - - if regn & (0x80 >> x_coord) == 1 && chip.screen.display[regvy + y_coord][regvx + x_coord] == 1 { + if pixel & (0x80 >> x_coord) == 1 { + if chip.screen.display[regvy + y_coord][regvx + x_coord] == 1 { chip.v[f] = 1 - chip.screen.display[regvy + y_coord][regvx + x_coord] = 0 + //chip.screen.display[regvy + y_coord][regvx + x_coord] = 0 } - - - chip.screen.display[regvy + y_coord][regvx + x_coord] = chip.ram[chip.i + y_coord] - - regvx = u8(chip.v[x + x_coord] % chip.screen.display_width) + + chip.screen.display[regvy + y_coord][regvx + x_coord] ^= 1 } } - - regvy = u8(chip.v[y + y_coord] % chip.screen.display_height) } } diff --git a/src/main.v b/src/main.v index fa92195..8846bb9 100644 --- a/src/main.v +++ b/src/main.v @@ -13,7 +13,7 @@ struct Emulator{ } fn (mut emulator Emulator) draw_block(i f32, j f32) { - emulator.graphic.draw_rect_filled(f32((j - 1) * 20) + 14, f32((i - 1) * 20), f32(20 - 1), f32(20 - 1), gx.rgb(255,255,255)) + emulator.graphic.draw_rect_filled(j,i, f32(20-1), f32(20-1), gx.rgb(0,255,0)) } fn (mut emulator Emulator) load_rom() !{ @@ -28,8 +28,8 @@ fn (mut emulator Emulator) load_rom() !{ println(' Loading ROM in the memory...\n') load_animate() - mut instructions := file.read_bytes_at(1024, 0) - mut index := 0x200 + mut instructions := file.read_bytes(1024) + mut index := u16(0x200) emulator.chip8.set_ram(instructions, index) println('ROM successfully loaded into memory!') @@ -48,9 +48,9 @@ fn draw_screen(mut emulator Emulator){ for x := 0; x < display_width; x++ { pixel := emulator.chip8.screen.display[y][x] - + if pixel == 1 { - emulator.draw_block(f32(y*10), f32(x*10)) + emulator.draw_block(f32((y-1)*20), f32((x-1)*20)) } } } @@ -152,17 +152,30 @@ fn main() { emulator.load_rom()! emulator.chip8.start_cpu() - emulator.chip8.run() - print('oi') + emulator.graphic = gg.new_context( - bg_color: gx.rgb(0, 0, 0) - width: 1280 - height: 640 - window_title: 'V CHIP-8 Emulator' - frame_fn : draw_screen - user_data: emulator - ) - //emulator.show_display() + bg_color: gx.rgb(0, 0, 0) + width: 1280 + height: 640 + window_title: 'V CHIP-8 Emulator' + frame_fn : draw_screen + user_data: emulator + ) + + + + for{ + emulator.chip8.run() + + emulator.graphic.quit() + if emulator.chip8.is_draw { + emulator.graphic.begin() + emulator.show_display() + emulator.graphic.end() + + } + } + }else{ panic('System is not graphic!') } diff --git a/src/utils.v b/src/utils.v index e050abe..70077fb 100644 --- a/src/utils.v +++ b/src/utils.v @@ -33,7 +33,7 @@ fn load_animate() { mut bars := ['|','/','-','\\'] - for i := 0; i < 2000; i++ { + for i := 0; i < 4000; i++ { print('[${bars[i%4]}]\r ') time.sleep(400000) }