diff --git a/chip8/instruction.go b/chip8/instruction.go index 27dbbd6..622681f 100644 --- a/chip8/instruction.go +++ b/chip8/instruction.go @@ -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 @@ -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() } @@ -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}, @@ -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}, } @@ -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) +} diff --git a/chip8/vm_test.go b/chip8/vm_test.go index e8352ca..ad3b7ea 100644 --- a/chip8/vm_test.go +++ b/chip8/vm_test.go @@ -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))