From e0021fda816c2eb5f472b3ba594c43911feeeaf9 Mon Sep 17 00:00:00 2001 From: "S.D" Date: Sun, 11 Oct 2020 20:05:00 +0100 Subject: [PATCH] some documenation, restructruing, minor fixes --- chip8/chip8.go | 46 ++++++++++++++++++++++++++++++++++----------- chip8/chip8_test.go | 30 +++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/chip8/chip8.go b/chip8/chip8.go index 02e513d..65b38dc 100644 --- a/chip8/chip8.go +++ b/chip8/chip8.go @@ -60,30 +60,43 @@ func (cpu *Chip8) GetGraphicsBuffer() [graphicsBufferSize]byte { return cpu.graphics } -func (cpu *Chip8) clearDisplay() { - // fuck it the gc can do the hard work for us - cpu.graphics = [64 * 32]byte{} - cpu.drawRequired = true +func (cpu *Chip8) TickTimers() { + if cpu.delayTimer > 0 { + cpu.delayTimer-- + } + if cpu.beepTimer > 0 { + cpu.beepTimer-- + } } -// what if there's nothing in the stack? -// return something sensible -func (cpu *Chip8) leaveFunction() { - cpu.pc = cpu.stack[len(cpu.stack)-1] - cpu.stack[len(cpu.stack)-1] = 0 - cpu.stack = cpu.stack[:len(cpu.stack)-1] +func (cpu *Chip8) zeroIndexOpcodes() { + if cpu.opcode == 0x00E0 { + // fuck it the gc can do the hard work for us + cpu.graphics = [64 * 32]byte{} + cpu.drawRequired = true + } else if cpu.opcode == 0x00EE { + // what if there's nothing in the stack? + // return something sensible + cpu.pc = cpu.stack[len(cpu.stack)-1] + cpu.stack[len(cpu.stack)-1] = 0 + cpu.stack = cpu.stack[:len(cpu.stack)-1] + } } func (cpu *Chip8) goTo() { + // GOTO 1NNN cpu.pc = cpu.opcode & 0x0FFF } func (cpu *Chip8) callSubroutine() { + // Call subroutine at 0x2NNN cpu.stack = append(cpu.stack, cpu.pc) cpu.pc = cpu.opcode & 0x0FFF } func (cpu *Chip8) skipIfRegisterEqual() { + // 3XNN + // If register x == NN skip next instruction r := (cpu.opcode) >> 8 & 0x0F if cpu.registers[r] == byte(cpu.opcode&0xFF) { cpu.pc += 2 @@ -91,6 +104,8 @@ func (cpu *Chip8) skipIfRegisterEqual() { } func (cpu *Chip8) skipIfRegisterNotEqual() { + // 4XNN + // If register x != NN skip next instruction r := (cpu.opcode) >> 8 & 0x0F if cpu.registers[r] != byte(cpu.opcode&0xFF) { cpu.pc += 2 @@ -98,6 +113,8 @@ func (cpu *Chip8) skipIfRegisterNotEqual() { } func (cpu *Chip8) skipIfRegistersEqual() { + // 5XY0 + // If register X == register Y skip next instruction x, y := (cpu.opcode>>8)&0x0F, (cpu.opcode>>4)&0x0F if cpu.registers[x] == cpu.registers[y] { cpu.pc += 2 @@ -105,11 +122,18 @@ func (cpu *Chip8) skipIfRegistersEqual() { } func (cpu *Chip8) setRegisterTo() { + // 6XNN + // Set register X to NN r := (cpu.opcode >> 8) & 0x0F cpu.registers[r] = byte(cpu.opcode & 0xFF) } -func (cpu *Chip8) registerPlusEqualsNN() {} // QUESTION HERE - WHAT DO IF IT WOULD WRAP ROUND? +func (cpu *Chip8) registerPlusEqualsNN() { + // 7XNN + // register X += NN (carry flag not set) + reg := (cpu.opcode >> 8) & 0x0F + cpu.registers[reg] += uint8(cpu.opcode & 0xFF) +} func (cpu *Chip8) bitOpsAndMath() { instruction := cpu.opcode & 0x0F diff --git a/chip8/chip8_test.go b/chip8/chip8_test.go index 68329d4..0d88064 100644 --- a/chip8/chip8_test.go +++ b/chip8/chip8_test.go @@ -28,11 +28,11 @@ func TestCreateCPU(t *testing.T) { } func TestClearDisplay(t *testing.T) { - cpu := Chip8{} + cpu := Chip8{opcode: 0x00E0} for i := range cpu.graphics { cpu.graphics[i] = byte(i % 255) } - cpu.clearDisplay() + cpu.zeroIndexOpcodes() graphicsArray := cpu.GetGraphicsBuffer() graphicsSlice := graphicsArray[:] emptySlice := make([]byte, len(cpu.graphics)) @@ -42,9 +42,9 @@ func TestClearDisplay(t *testing.T) { } func TestLeaveFunction(t *testing.T) { - cpu := Chip8{pc: 50} + cpu := Chip8{pc: 50, opcode: 0x00EE} cpu.stack = append(cpu.stack, 1, 2, 3, 4, 5) - cpu.leaveFunction() + cpu.zeroIndexOpcodes() if cpu.pc != 4 && len(cpu.stack) != 4 { t.Errorf("TestLeaveFunction not in expected state") } @@ -147,3 +147,25 @@ func TestSetRegisterTo(t *testing.T) { t.Errorf("Register 8 is %d wanted %d", cpu.registers[8], 0x24) } } + +func TestRegisterPlusEqualsNN(t *testing.T) { + var tests = []struct { + register, initalVal, toAdd, expected byte + }{ + {0, 2, 2, 4}, + {0, 254, 3, 1}, + {15, 2, 0, 2}, + {15, 250, 6, 0}, + } + for _, tt := range tests { + testname := fmt.Sprintf("Register:%d,inital:%d,toAdd:%d,expected:%d", tt.register, tt.initalVal, tt.toAdd, tt.expected) + t.Run(testname, func(t *testing.T) { + cpu := Chip8{opcode: uint16(tt.register)<<8 + uint16(tt.toAdd)} + cpu.registers[tt.register] = tt.initalVal + cpu.registerPlusEqualsNN() + if cpu.registers[tt.register] != tt.expected { + t.Errorf("Register is %d, wanted %d", cpu.registers[tt.register], tt.expected) + } + }) + } +}