From 4ff44824e1913d0934f1b71772960a9c82820876 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Wed, 21 Feb 2024 02:09:28 -0300 Subject: [PATCH] Last instructions implemented --- roms/Airplane.ch8 | Bin 0 -> 356 bytes roms/Lunar Lander (Udo Pernisz, 1979).ch8 | Bin 0 -> 1792 bytes roms/Pong.ch8 | Bin 0 -> 246 bytes roms/Soccer.ch8 | Bin 0 -> 334 bytes src/chip8.v | 203 ++++++++++++++-------- src/main.v | 7 +- 6 files changed, 135 insertions(+), 75 deletions(-) create mode 100644 roms/Airplane.ch8 create mode 100644 roms/Lunar Lander (Udo Pernisz, 1979).ch8 create mode 100644 roms/Pong.ch8 create mode 100644 roms/Soccer.ch8 diff --git a/roms/Airplane.ch8 b/roms/Airplane.ch8 new file mode 100644 index 0000000000000000000000000000000000000000..cac880477e824b0948ed65d195c4cc44896577d0 GIT binary patch literal 356 zcmc~|$Y#l5%w@=9QdUz|NH9ob{2=;){iB2dgOG@lld?iBBT$s%!9ry&F9spI#Ys0N zRx;k0=+7wh#ZyA)iP9z^IUh!)93Lj7Dj#O04j&e!X+Eq$))H3$le;T{}>+lu;nmjF$i%buzirI{_o8o z%mMN@!vm0*Fozc-!@C5g4-#379TzGXJ0F1j3A8Qb?jweGDU!(yi^DEE7IPRo2Gj=j3rE$9U0y={P^GSK%k)GonZN@6$=^|0Amq% A0ssI2 literal 0 HcmV?d00001 diff --git a/roms/Lunar Lander (Udo Pernisz, 1979).ch8 b/roms/Lunar Lander (Udo Pernisz, 1979).ch8 new file mode 100644 index 0000000000000000000000000000000000000000..e7972b5bd75793eb598e83cf3d2326a459f5b5f1 GIT binary patch literal 1792 zcmZWq3rtg282)cd0r3H8ZSM^(^1Lmmfr^)&>k3<~9>VS>|S4wuQ3cWlkKM#<;p&>m!CRb4zH@rc z|9$6Nsi;u>Ozv7pNlZZQab;4GCMmhgRX~9$mei73{4^kQ{fgefmr#La#7UP?X|r*r z{4Zsr)O1lnF?b|CUk* z^E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? zmfS^29jk!Ysq~;Vi4kk#xW-*cEfTbTt`A&Ts=9UiQ7PZh OU}>Y{emAkJmi`U7_r^>B literal 0 HcmV?d00001 diff --git a/roms/Pong.ch8 b/roms/Pong.ch8 new file mode 100644 index 0000000000000000000000000000000000000000..65d63106cc088da4fc0e2cfad10e01453e724620 GIT binary patch literal 246 zcmc~|%I3+j&*fS4>ejY9*YX&YuB0(%FeM~>5dFYzz#t@bT)dnk^Z%j`*9svj5*QyW ztp1n40wkFd%J`Se1!P^dXB1kL$kZSTq<4YI6JYXCg2D$0 zr7L|VK$qDWGXh=r&zLcTNl2{nzvsWq|3W7!89kXY853AONR%taOmj-3?;znv4)$^UhpI)J9eeNGMNPG!s7^101}aZZ6@n+IcV`E4gevI$v&kS7c} zf>=ksQO)R7tfA#n6|!zK+w~q5o}s V+BON&*|3(*4EgE8&;ok0y&qFrZ><0T literal 0 HcmV?d00001 diff --git a/src/chip8.v b/src/chip8.v index 1ea8d6b..1ec5ad3 100644 --- a/src/chip8.v +++ b/src/chip8.v @@ -1,7 +1,36 @@ - - import rand +const keyboard := { + 0x1 : 0x1 //1 - 1 + 0x2 : 0x2 //2 - 2 + 0x3 : 0x3 //3 - 3 + 0x4 : 0x43 //4 - C + 0x51 : 0x4 //Q - 4 + 0x71 : 0x4 //q - 4 + 0x57 : 0x5 //W - 5 + 0x77 : 0x5 //w - 5 + 0x45 : 0x6 //E - 6 + 0x65 : 0x6 //e - 6 + 0x52 : 0x44 //R - D + 0x72 : 0x44 //r - D + 0x41 : 0x7 //A - 7 + 0x61 : 0x7 //a - 7 + 0x53 : 0x8 //S - 8 + 0x73 : 0x8 //s - 8 + 0x44 : 0x9 //D - 9 + 0x64 : 0x9 //d - 9 + 0x46 : 0x45 //F - E + 0x66 : 0x45 //f - E + 0x5A : 0x41 //Z - A + 0x7A : 0x41 //z - A + 0x58 : 0x30 //X - 0 + 0x78 : 0x30 //x - 0 + 0x43 : 0x42 //C - B + 0x63 : 0x42 //c - B + 0x56 : 0x46 //V - F + 0x76 : 0x46 //v - F +} + const font := [ [u8(0xF0), u8(0x90), u8(0x90), u8(0x90), u8(0xF0)], // 0 [u8(0x20), u8(0x60), u8(0x20), u8(0x20), u8(0x70)], // 1 @@ -42,8 +71,9 @@ struct Chip8{ i u16 stack Stack delay_timer u8 - sount_timer u8 + sound_timer u8 is_draw bool + key u8 } fn (mut chip Chip8) start_cpu(){ @@ -96,7 +126,9 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { mut is_jump := false chip.is_draw = false - //println(opcode_lsb) + if chip.delay_timer > 0 { chip.delay_timer-- } + if chip.sound_timer > 0 { chip.sound_timer-- } + match opcode_msb{ 0x0000 { @@ -122,7 +154,8 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { //} else{ - panic('Invalid instruction! 0x${instruction.hex()}') + nnn = instruction & 0x0FFF + //panic('Invalid instruction! 0x${instruction.hex()}') } } } @@ -308,8 +341,8 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { y = (instruction & 0x00F0) >> 4 n = (instruction & 0x000F) - mut regvy := u16(chip.v[y]) - mut regvx := u16(chip.v[x]) + mut regvy := u16(chip.v[y] % chip.screen.display_height) + mut regvx := u16(chip.v[x] % chip.screen.display_width) chip.v[f] = 0 @@ -319,83 +352,105 @@ fn (mut chip Chip8) decode_and_run(instruction u16) { for x_coord := 0; x_coord < 8; x_coord++ { - if (pixel & (0x80 >> x_coord)) != 0 { - if chip.screen.display[regvy + y_coord][regvx + x_coord] == 1 { - chip.v[f] = 1 + if (regvy + y_coord) < chip.screen.display_height && (regvx + x_coord) < chip.screen.display_width { + if (pixel & (0x80 >> x_coord)) != 0 { + if chip.screen.display[regvy + y_coord][regvx + x_coord] == 1 { + chip.v[f] = 1 + } + + chip.screen.display[regvy + y_coord][regvx + x_coord] ^= 1 } - - chip.screen.display[regvy + y_coord][regvx + x_coord] ^= 1 } } } } - //0xE000 { - // x = (opcode & 0x0F00) >> 8 - // s_opcode = opcode & 0x00FF -// - // match s_opcode { -// - // 0x9E { -// - // }, -// - // 0xA1 { -// - // }, - // } - //}, -// -// 0xF000 { -// x = (opcode & 0x0F00) >> 8 -// s_opcode = opcode & 0x00FF -// -// match s_opcode { -// 0x07{ -// chip.v[x] = chip.delay_timer -// } -// -// 0x0A{ -// -// } -// -// 0x15{ -// chip.delay_timer = chip.v[x] -// } -// -// 0x18{ -// chip.sound_timer = chip.v[x] -// } -// -// 0x1E{ -// chip.i += chip.v[x] -// } -// -// 0x29{ -// -// } -// -// 0x33{ -// -// } -// -// 0x55{ -// -// } -// -// 0x65{ -// -// } -// -// else { -// panic('Invalid instruction! 0x${instruction.hex()}') -// } -// } -// } + 0xE000 { + x = (instruction & 0x0F00) >> 8 + opcode_lsb = instruction & 0x00FF + + match opcode_lsb { + + 0x9E { + + if chip.key == chip.v[x] { chip.pc += 2 } + } + + 0xA1 { + + if chip.key != chip.v[x] { chip.pc += 2 } + } + + else{ + panic('Invalid instruction 0x${instruction.hex()}') + } + } + } + + 0xF000 { + x = (instruction & 0x0F00) >> 8 + opcode_lsb = instruction & 0x00FF + + match opcode_lsb { + 0x07{ + chip.v[x] = chip.delay_timer + } + + 0x0A{ + chip.v[x] = chip.key + } + + 0x15{ + chip.delay_timer = chip.v[x] + } + + 0x18{ + chip.sound_timer = chip.v[x] + } + + 0x1E{ + chip.i += chip.v[x] + } + + 0x29{ + chip.i = chip.v[x] * 5 + } + + 0x33{ + chip.ram[chip.i] = chip.v[x] / 100; + chip.ram[chip.i + 1] = (chip.v[x] / 10) % 10 + chip.ram[chip.i + 2] = chip.v[x] % 10; + } + + 0x55{ + + for i := 0; i <= x; i++ { + chip.ram[chip.i + i] = chip.v[i] + } + chip.i = u16(chip.i + x + 1) + } + + 0x65{ + for i := 0; i <= x; i++ { + chip.v[x] = chip.ram[chip.i + x] + } + chip.i = u16(chip.i + x + 1) + } + + else { + panic('Invalid instruction! 0x${instruction.hex()}') + } + } + } else { panic('Invalid instruction! 0x${instruction.hex()}') } } if !is_jump { chip.pc += 2 } +} + + +fn (mut chip Chip8) set_key(key int){ + chip.key = u8(keyboard[key]) } \ No newline at end of file diff --git a/src/main.v b/src/main.v index d1890f1..94f60f7 100644 --- a/src/main.v +++ b/src/main.v @@ -159,8 +159,9 @@ fn main() { width: 1280 height: 640 window_title: 'V CHIP-8 Emulator' - frame_fn : frame user_data: emulator + frame_fn : frame + event_fn: on_event ) emulator.show_display() @@ -169,3 +170,7 @@ fn main() { panic('System is not graphic!') } } + +fn on_event(e &gg.Event, mut emulator Emulator){ + emulator.chip8.set_key(e.char_code) +} \ No newline at end of file