Add keyboard instruction

This commit is contained in:
Vinicius Silva 2024-02-20 22:19:37 -03:00
parent 7873308e7b
commit 198af8f9a2
2 changed files with 108 additions and 42 deletions

View File

@ -0,0 +1 @@
https://literaleval.hashnode.dev/guide-to-writing-your-first-emulator-chip-8

View File

@ -1,6 +1,7 @@
import rand import rand
import readline
const font := [ const font := [
[u8(0xF0), u8(0x90), u8(0x90), u8(0x90), u8(0xF0)], // 0 [u8(0xF0), u8(0x90), u8(0x90), u8(0x90), u8(0xF0)], // 0
@ -21,6 +22,37 @@ const font := [
[u8(0xF0), u8(0x80), u8(0xF0), u8(0x80), u8(0x80)], // F [u8(0xF0), u8(0x80), u8(0xF0), u8(0x80), u8(0x80)], // F
] ]
const keyboard := {
0x1 : 0x1
0x2 : 0x2
0x3 : 0x3
0x4 : 0x4
0x71 :
0x51 :
0x77 :
0x57 :
0x45 :
0x65 :
0x72 :
0x52 :
0x41 :
0x61 :
0x73 :
0x53 :
0x64 :
0x44 :
0x46 :
0x66 :
0x7a :
0x5a :
0x58 :
0x78 :
0x43 :
0x63 :
0x56 :
0x76 :
}
struct Screen{ struct Screen{
pub: pub:
display_width int = 64 display_width int = 64
@ -42,6 +74,7 @@ struct Chip8{
i u16 i u16
stack Stack stack Stack
delay_timer u8 delay_timer u8
sount_timer u8
is_draw bool is_draw bool
} }
@ -345,48 +378,52 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
// } // }
//}, //},
// //
//0xF000 { 0xF000 {
// x = (opcode & 0x0F00) >> 8 x = (opcode & 0x0F00) >> 8
// s_opcode = opcode & 0x00FF s_opcode = opcode & 0x00FF
//
// match s_opcode { match s_opcode {
// 0x07{ 0x07{
// chip.v[x] = chip.delay_timer
// }, }
//
// 0x0A{ 0x0A{
//
// }, }
//
// 0x15{ 0x15{
// chip.delay_timer = chip.v[x]
// }, }
//
// 0x18{ 0x18{
// chip.sound_timer = chip.v[x]
// }, }
//
// 0x1E{ 0x1E{
// chip.i += chip.v[x] chip.i += chip.v[x]
// }, }
//
// 0x29{ 0x29{
//
// }, }
//
// 0x33{ 0x33{
//
// }, }
//
// 0x55{ 0x55{
//
// }, }
//
// 0x65{ 0x65{
//
// }, }
// }
//}, else {
panic('Invalid instruction! 0x${instruction.hex()}')
}
}
}
else { else {
panic('Invalid instruction! 0x${instruction.hex()}') panic('Invalid instruction! 0x${instruction.hex()}')
@ -395,3 +432,31 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
if !is_jump { chip.pc += 2 } if !is_jump { chip.pc += 2 }
} }
fn get_key()
{
mut r := readline.Readline{}
key := r.read_char() or { panic(err) }
match key {
0x1 {
}
0x2 {
}
0x3 {
}
0x4 {
}
0x1 {
}
}
}