From b674df5a59b7442cba3b8112a2436a344c5b39be Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Mon, 19 Feb 2024 12:14:28 -0300 Subject: [PATCH] Fix compiling erros --- gl | 0 roms/IBM Logo.ch8 | Bin 0 -> 132 bytes roms/test_opcode.8o | 205 +++++++++++++++++++++++++++++++++++++++++++ roms/test_opcode.ch8 | Bin 0 -> 478 bytes sgl | 0 src/chip8.v | 121 ++++++++++++++----------- src/main.v | 58 ++++++------ src/utils.v | 2 +- 8 files changed, 304 insertions(+), 82 deletions(-) create mode 100644 gl create mode 100644 roms/IBM Logo.ch8 create mode 100644 roms/test_opcode.8o create mode 100644 roms/test_opcode.ch8 create mode 100644 sgl diff --git a/gl b/gl new file mode 100644 index 0000000..e69de29 diff --git a/roms/IBM Logo.ch8 b/roms/IBM Logo.ch8 new file mode 100644 index 0000000000000000000000000000000000000000..113338e67097e0a2ad227e5b79b3a5d1a586a660 GIT binary patch literal 132 zcmZR0ut+O`Cz0cVd;#Yo%M0?0JPJT;mPO$}l4DUCm@K^@FQoCG;Xi{76av{Gt_6cV z5Ly6P{~H({0AT|Vg6SU&e;Dk5a@Gu%3_lotFf*_-@H6aZ_|5Q{;W2|VOes`300ZwW A9{>OV literal 0 HcmV?d00001 diff --git a/roms/test_opcode.8o b/roms/test_opcode.8o new file mode 100644 index 0000000..6756767 --- /dev/null +++ b/roms/test_opcode.8o @@ -0,0 +1,205 @@ +:alias x0 v8 +:alias x1 v9 +:alias x2 vA +:alias y vB +: imageok + 0xEA 0xAC 0xAA 0xEA +: imagefalse + 0xCE 0xAA 0xAA 0xAE +: im0 + 0xE0 0xA0 0xA0 0xE0 +: im1 + 0xC0 0x40 0x40 0xE0 +: im2 + 0xE0 0x20 0xC0 0xE0 +: im3 + 0xE0 0x60 0x20 0xE0 +: im4 + 0xA0 0xE0 0x20 0x20 +: im5 + 0x60 0x40 0x20 0x40 +: im6 + 0xE0 0x80 0xE0 0xE0 +: im7 + 0xE0 0x20 0x20 0x20 +: im8 + 0xE0 0xE0 0xA0 0xE0 +: im9 + 0xE0 0xE0 0x20 0xE0 +: imA + 0x40 0xA0 0xE0 0xA0 +: imE + 0xE0 0xC0 0x80 0xE0 +: imF + 0xE0 0x80 0xC0 0x80 +: imX + 0xA0 0x40 0xA0 0xA0 + +:macro drawop A B { +i := A +sprite x0 y 4 +i := B +sprite x1 y 4 +} + +: testAX +#test Ax +i := imageok +sprite x2 y 4 +return + +: test1x +i := imageok +sprite x2 y 4 +jump endtest + +: main +x0 := 1 +x1 := 5 +x2 := 10 +y := 1 +v5 := 42 +v6 := 43 +#test 3x +drawop im3 imX +i := imageok +if v6 != 43 then i := imagefalse +sprite x2 y 4 +#test 4x +y := 6 +drawop im4 imX +i := imagefalse +if v5 == 42 then i := imageok +sprite x2 y 4 +#test 5x +y := 11 +drawop im5 imX +i := imagefalse +if v5 != v6 then i := imageok +sprite x2 y 4 +#test 7x +y := 16 +drawop im7 imX +i := imagefalse +v6 += 255 +if v6 == 42 then i := imageok +sprite x2 y 4 +#test 9x +y := 21 +drawop im9 imX +i := imagefalse +if v5 == v6 then i := imageok +sprite x2 y 4 +#test AX +y := 26 +drawop imA imX +testAX +#test 0E +x0 := 23 +x1 := 27 +x2 := 32 +y := 1 +drawop im0 imE +i := imageok +sprite x2 y 4 +#test 8xy0 +y := 6 +drawop im8 im0 +i := imagefalse +v7 := v5 +if v7 == 42 then i := imageok +sprite x2 y 4 +#test 8xy1 +y := 11 +drawop im8 im1 +i := imagefalse +v7 := 42 +v7 |= y +if v7 == 43 then i := imageok +sprite x2 y 4 +#test 8xy2 +y := 16 +drawop im8 im2 +i := imagefalse +v6 := 120 +v7 := 31 +v7 &= v6 +if v7 == 24 then i := imageok +sprite x2 y 4 +#test 8xy3 +y := 21 +drawop im8 im3 +i := imagefalse +v6 := 120 +v7 := 31 +v7 ^= v6 +if v7 == 103 then i := imageok +sprite x2 y 4 +#test 8xy4 +y := 26 +drawop im8 im4 +i := imagefalse +v6 := 140 +v7 := 140 +v7 += v6 +if v7 == 24 then i := imageok +sprite x2 y 4 +#test 8xy5 +x0 := 44 +x1 := 48 +x2 := 52 +y := 1 +drawop im8 im5 +i := imagefalse +v6 := 140 +v7 := 120 +v7 -= v6 +if v7 == 236 then i := imageok +sprite x2 y 4 +#test 8xy6 +y := 6 +drawop im8 im6 +i := imagefalse +v6 := 224 +v6 <<= v6 +if v6 == 192 then i := imageok +sprite x2 y 4 +#test 8xyE +y := 11 +drawop im8 imE +i := imagefalse +v6 := 15 +v6 >>= v6 +if v6 == 7 then i := imageok +sprite x2 y 4 +#test Fx55,Fx65 +y := 16 +drawop imF im5 +i := 1000 +v0 := 0 +v1 := 48 +save v1 +i := 1001 +load v0 +i := imagefalse +if v0 == 48 then i := imageok +sprite x2 y 4 +#test Fx33 +y := 21 +drawop imF im3 +i := 1000 +v6 := 137 +bcd v6 +load v2 +i := imageok +if v0 != 1 then i := imagefalse +if v1 != 3 then i := imagefalse +if v2 != 7 then i := imagefalse +sprite x2 y 4 +#test 1x +y := 26 +drawop im1 imX +jump test1x +: endtest +loop +again diff --git a/roms/test_opcode.ch8 b/roms/test_opcode.ch8 new file mode 100644 index 0000000000000000000000000000000000000000..f540f69ecfc06e6c7b30881234469cc10847bff9 GIT binary patch literal 478 zcmYk&ze~eF6bJC8;uTt`rKMFO)T4t#DiW)7aF9c=aT1sA@kf%j0Y~YWsiDx-J-lv3 z1R;Mw{sLz$9o|@rh&s6H-K7zijmckgkn@idt{U=U!$7$S`jO^N|hYBI{u zLvEBJ7rsFj@J-|)dXX2fAb|8VOudV>H0vnc)n3+BEX}S%S^y zPBf8sb5KpBZAvM?b|SYwtvFSN-Nf$VlnRYlYR8@R?q-j=3S_x7MK{?=O%i)k6%A5AWWClvaIsC#&di4 cz5iuFT33MVtpI6c> 8 nn = instruction & 0x00FF chip.v[x] = u8(nn) @@ -161,7 +173,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { } 0x7000 { - x = instruction & 0xF00 + x = (instruction & 0xF00) >> 8 nn = instruction & 0x00FF chip.v[x] += u8(nn) @@ -273,6 +285,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { nnn = instruction & 0x0FFF chip.pc = u16(nnn + chip.v[0]) + is_jump = true } 0xC000 { @@ -289,23 +302,33 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { 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) + chip.v[f] = 0 for y_coord := 0; y_coord < n; y_coord++ { - - regvy := chip.v[y + y_coord] & chip.screen.display_height-1 - + + + regn := chip.ram[chip.i + y_coord] + for x_coord := 0; x_coord < 8; x_coord++ { - regvx := chip.v[x + x_coord] & chip.screen.display_width-1 + 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 { + chip.v[f] = 1 + chip.screen.display[regvy + y_coord][regvx + x_coord] = 0 + } - if chip.screen.display[regvx + y_coord][regvy + x_coord] == 1 { - chip.v[f] = 1 - chip.screen.display[regvx + y_coord][regvy + 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[regvx + y_coord][regvy + x_coord] = chip.ram[chip.i + y_coord] } + + regvy = u8(chip.v[y + y_coord] % chip.screen.display_height) } } @@ -372,6 +395,6 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { panic('Invalid instruction!') } } - + if !is_jump { chip.pc += 2 } } diff --git a/src/main.v b/src/main.v index c8b84db..fa92195 100644 --- a/src/main.v +++ b/src/main.v @@ -12,11 +12,10 @@ struct Emulator{ is_graphic bool = is_graphic() } -fn (emulator Emulator) draw_block(i int, j int, mut ctx gg.Context) { - emulator.graphic.draw_rect_filled(f32((j - 1) * 20) + 1, f32((i - 1) * 20), f32(20 - 1), f32(20 - 1), gx.rgb(255,255,255)) +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)) } - fn (mut emulator Emulator) load_rom() !{ arguments := os.args.clone() @@ -29,9 +28,9 @@ fn (mut emulator Emulator) load_rom() !{ println(' Loading ROM in the memory...\n') load_animate() - for instruction in file.read_bytes_at(1024, 0) { - emulator.chip8.set_ram(u8(instruction)) - } + mut instructions := file.read_bytes_at(1024, 0) + mut index := 0x200 + emulator.chip8.set_ram(instructions, index) println('ROM successfully loaded into memory!') @@ -40,29 +39,21 @@ fn (mut emulator Emulator) load_rom() !{ } } -fn (mut emulator Emulator) draw_screen(){ - - emulator.graphic.begin() +fn draw_screen(mut emulator Emulator){ display_height := emulator.chip8.screen.display_height display_width := emulator.chip8.screen.display_width - mut buf := []u8{len: display_height*display_width} - mut i := 0 + for y := 0; y < display_height; y++ { + for x := 0; x < display_width; x++ { - for y := 0; y < emulator.chip8.screen.display_height; y++ { - for x := 0; x < emulator.chip8.screen.display_width; x++ { - pixel := emulator.chip8.screen.display[y][x] - buf[i] = u8((0xFFFFFF00 * pixel) | 0x000000FF); - i++ + if pixel == 1 { + emulator.draw_block(f32(y*10), f32(x*10)) + } } } - - emulator.graphic.create_image_from_memory(unsafe{ &buf[0] }, buf.len) or { panic(err) } - emulator.graphic.end() - } fn (mut emulator Emulator) show_display(){ @@ -147,18 +138,14 @@ fn is_graphic() bool{ return os.environ()['DISPLAY'] != '' } +fn frame(){ + print('oi') +} + fn main() { - mut emulator := Emulator{ - + mut emulator := &Emulator{ chip8 : Chip8{} - - graphic : gg.new_context( - bg_color: gx.rgb(0, 0, 0) - width: 1280 - height: 640 - window_title: 'V CHIP-8 Emulator' - ) } if emulator.is_graphic { @@ -166,9 +153,16 @@ fn main() { emulator.load_rom()! emulator.chip8.start_cpu() emulator.chip8.run() - - emulator.draw_screen() - emulator.show_display() + 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() }else{ panic('System is not graphic!') } diff --git a/src/utils.v b/src/utils.v index 70077fb..e050abe 100644 --- a/src/utils.v +++ b/src/utils.v @@ -33,7 +33,7 @@ fn load_animate() { mut bars := ['|','/','-','\\'] - for i := 0; i < 4000; i++ { + for i := 0; i < 2000; i++ { print('[${bars[i%4]}]\r ') time.sleep(400000) }