Skip to content

Commit

Permalink
Add functionality for skipIfKey. Keypad test ROM now works, mostly.
Browse files Browse the repository at this point in the history
  • Loading branch information
daddyshortlegs committed Jul 24, 2023
1 parent a283058 commit 902552b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
29 changes: 28 additions & 1 deletion chip8/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const SetIndexRegister = 0xA
const JumpWithOffset = 0xB
const OpRandom = 0xC
const Display = 0xD
const SkipIfKey = 0xE
const FurtherOperations = 0xF

const setVxToVy = 0x00
Expand Down Expand Up @@ -87,7 +88,7 @@ func (i *Instruction) execute() {

function := i.getInstructionFromOpcode()
if function == nil {
fmt.Printf("Name: %s, unknown instruction %x", name, i.opCode)
i.print()
} else {
function()
}
Expand Down Expand Up @@ -362,6 +363,27 @@ func (i *Instruction) setSoundTimer() {
println("vx = ", i.vx)
}

func (i *Instruction) skipIfKey() {

if i.secondByte == 0x9E {
key := i.vm.display.GetKey()
println("****** skipIfKey = ", key)

if byte(key) == i.vm.registers[i.vx] {
i.vm.pc += 2
}

} else if i.secondByte == 0xA1 {
key := i.vm.display.GetKey()
println("****** skipIfNotKey = ", key)

if byte(key) != i.vm.registers[i.vx] {
i.vm.pc += 2
}
}

}

func (i *Instruction) primaryOpcodes() map[byte]Opcode {
return map[byte]Opcode{
ClearScreen: {name: "ClearScreen", function: i.clearScreen},
Expand All @@ -378,6 +400,7 @@ func (i *Instruction) primaryOpcodes() map[byte]Opcode {
JumpWithOffset: {name: "JumpWithOffset", function: i.jumpWithOffset},
OpRandom: {name: "OpRandom", function: i.opRandom},
Display: {name: "Display", function: i.opDisplay},
SkipIfKey: {name: "SkipIfKey", function: i.skipIfKey},
BitwiseOperations: {name: "BitwiseOperations", function: i.executeArithmeticInstructions},
FurtherOperations: {function: i.furtherOperations},
}
Expand Down Expand Up @@ -410,3 +433,7 @@ func (i *Instruction) furtherOpcodes() map[byte]Opcode {
SetSoundTimer: {name: "SetSoundTimer", function: i.setSoundTimer},
}
}

func (i *Instruction) print() {
fmt.Printf("Instuction: %x, Name: %s, Opcode %x\n", i.instr, i.getOpcodeName(), i.opCode)
}
37 changes: 28 additions & 9 deletions chip8/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,28 +686,47 @@ FX18: Timers
*/

/*
func (suite *Chip8TestSuite) TestSkipIfKeyPressed() {
suite.mockDisplay = mockDisplay{false, drawPatternValues{}, KeyboardEvent, 55}
suite.mockRandom = MockRandom{55}
suite.mockDisplay.SetKey(55)
suite.mockDisplay.SetKey(0xC)
suite.vm = NewVM(&suite.mockDisplay, suite.mockRandom)

suite.Equal(uint16(0x200), suite.vm.pc)

suite.asm.SetRegister(0, 0x11)
suite.asm.SkipIfKeyPressed(0xA)
suite.asm.Data([]byte{0x00, 0x00}) //this would exit the program
suite.asm.SetRegister(0xa, 0x69) // instead this registr should be set
suite.asm.SetRegister(0, 0xC)
suite.asm.SkipIfKeyPressed(0)
suite.asm.SetRegister(0xA, 0x22) // instead this registr should NOT be set
suite.asm.SetRegister(0xB, 0x69) // instead this registr should be set
suite.vm.Load(suite.asm.Assemble())
suite.vm.Run()
suite.Equal(byte(55), suite.vm.registers[3])

suite.executeInstructions()

suite.Equal(uint16(0x208), suite.vm.pc)
suite.NotEqual(byte(0x22), suite.vm.registers[0xA])
suite.Equal(byte(0x69), suite.vm.registers[0xB])
}

func (suite *Chip8TestSuite) TestSkipIfKeyNotPressed() {
suite.mockDisplay = mockDisplay{false, drawPatternValues{}, KeyboardEvent, 55}
suite.mockRandom = MockRandom{55}
suite.mockDisplay.SetKey(0xC)
suite.vm = NewVM(&suite.mockDisplay, suite.mockRandom)

suite.Equal(uint16(0x200), suite.vm.pc)

suite.asm.SetRegister(0, 0x9)
suite.asm.SkipIfKeyNotPressed(0)
suite.asm.SetRegister(0xA, 0x22) // This register should NOT be set as it is skipped
suite.asm.SetRegister(0xB, 0x69) // instead this registr should be set
suite.vm.Load(suite.asm.Assemble())
suite.vm.Run()

suite.executeInstructions()

suite.NotEqual(byte(0x22), suite.vm.registers[0xA])
suite.Equal(byte(0x69), suite.vm.registers[0xB])
}
*/

func TestChip8TestSuite(t *testing.T) {
suite.Run(t, new(Chip8TestSuite))
Expand Down

0 comments on commit 902552b

Please sign in to comment.