some documenation, restructruing, minor fixes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sid 2020-10-11 20:05:00 +01:00
parent ae77007578
commit e0021fda81
2 changed files with 61 additions and 15 deletions

View File

@ -60,30 +60,43 @@ func (cpu *Chip8) GetGraphicsBuffer() [graphicsBufferSize]byte {
return cpu.graphics return cpu.graphics
} }
func (cpu *Chip8) clearDisplay() { func (cpu *Chip8) TickTimers() {
// fuck it the gc can do the hard work for us if cpu.delayTimer > 0 {
cpu.graphics = [64 * 32]byte{} cpu.delayTimer--
cpu.drawRequired = true }
if cpu.beepTimer > 0 {
cpu.beepTimer--
}
} }
// what if there's nothing in the stack? func (cpu *Chip8) zeroIndexOpcodes() {
// return something sensible if cpu.opcode == 0x00E0 {
func (cpu *Chip8) leaveFunction() { // fuck it the gc can do the hard work for us
cpu.pc = cpu.stack[len(cpu.stack)-1] cpu.graphics = [64 * 32]byte{}
cpu.stack[len(cpu.stack)-1] = 0 cpu.drawRequired = true
cpu.stack = cpu.stack[:len(cpu.stack)-1] } 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() { func (cpu *Chip8) goTo() {
// GOTO 1NNN
cpu.pc = cpu.opcode & 0x0FFF cpu.pc = cpu.opcode & 0x0FFF
} }
func (cpu *Chip8) callSubroutine() { func (cpu *Chip8) callSubroutine() {
// Call subroutine at 0x2NNN
cpu.stack = append(cpu.stack, cpu.pc) cpu.stack = append(cpu.stack, cpu.pc)
cpu.pc = cpu.opcode & 0x0FFF cpu.pc = cpu.opcode & 0x0FFF
} }
func (cpu *Chip8) skipIfRegisterEqual() { func (cpu *Chip8) skipIfRegisterEqual() {
// 3XNN
// If register x == NN skip next instruction
r := (cpu.opcode) >> 8 & 0x0F r := (cpu.opcode) >> 8 & 0x0F
if cpu.registers[r] == byte(cpu.opcode&0xFF) { if cpu.registers[r] == byte(cpu.opcode&0xFF) {
cpu.pc += 2 cpu.pc += 2
@ -91,6 +104,8 @@ func (cpu *Chip8) skipIfRegisterEqual() {
} }
func (cpu *Chip8) skipIfRegisterNotEqual() { func (cpu *Chip8) skipIfRegisterNotEqual() {
// 4XNN
// If register x != NN skip next instruction
r := (cpu.opcode) >> 8 & 0x0F r := (cpu.opcode) >> 8 & 0x0F
if cpu.registers[r] != byte(cpu.opcode&0xFF) { if cpu.registers[r] != byte(cpu.opcode&0xFF) {
cpu.pc += 2 cpu.pc += 2
@ -98,6 +113,8 @@ func (cpu *Chip8) skipIfRegisterNotEqual() {
} }
func (cpu *Chip8) skipIfRegistersEqual() { func (cpu *Chip8) skipIfRegistersEqual() {
// 5XY0
// If register X == register Y skip next instruction
x, y := (cpu.opcode>>8)&0x0F, (cpu.opcode>>4)&0x0F x, y := (cpu.opcode>>8)&0x0F, (cpu.opcode>>4)&0x0F
if cpu.registers[x] == cpu.registers[y] { if cpu.registers[x] == cpu.registers[y] {
cpu.pc += 2 cpu.pc += 2
@ -105,11 +122,18 @@ func (cpu *Chip8) skipIfRegistersEqual() {
} }
func (cpu *Chip8) setRegisterTo() { func (cpu *Chip8) setRegisterTo() {
// 6XNN
// Set register X to NN
r := (cpu.opcode >> 8) & 0x0F r := (cpu.opcode >> 8) & 0x0F
cpu.registers[r] = byte(cpu.opcode & 0xFF) 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() { func (cpu *Chip8) bitOpsAndMath() {
instruction := cpu.opcode & 0x0F instruction := cpu.opcode & 0x0F

View File

@ -28,11 +28,11 @@ func TestCreateCPU(t *testing.T) {
} }
func TestClearDisplay(t *testing.T) { func TestClearDisplay(t *testing.T) {
cpu := Chip8{} cpu := Chip8{opcode: 0x00E0}
for i := range cpu.graphics { for i := range cpu.graphics {
cpu.graphics[i] = byte(i % 255) cpu.graphics[i] = byte(i % 255)
} }
cpu.clearDisplay() cpu.zeroIndexOpcodes()
graphicsArray := cpu.GetGraphicsBuffer() graphicsArray := cpu.GetGraphicsBuffer()
graphicsSlice := graphicsArray[:] graphicsSlice := graphicsArray[:]
emptySlice := make([]byte, len(cpu.graphics)) emptySlice := make([]byte, len(cpu.graphics))
@ -42,9 +42,9 @@ func TestClearDisplay(t *testing.T) {
} }
func TestLeaveFunction(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.stack = append(cpu.stack, 1, 2, 3, 4, 5)
cpu.leaveFunction() cpu.zeroIndexOpcodes()
if cpu.pc != 4 && len(cpu.stack) != 4 { if cpu.pc != 4 && len(cpu.stack) != 4 {
t.Errorf("TestLeaveFunction not in expected state") 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) 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)
}
})
}
}