some documenation, restructruing, minor fixes
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ae77007578
commit
e0021fda81
@ -60,30 +60,43 @@ func (cpu *Chip8) GetGraphicsBuffer() [graphicsBufferSize]byte {
|
||||
return cpu.graphics
|
||||
}
|
||||
|
||||
func (cpu *Chip8) clearDisplay() {
|
||||
func (cpu *Chip8) TickTimers() {
|
||||
if cpu.delayTimer > 0 {
|
||||
cpu.delayTimer--
|
||||
}
|
||||
if cpu.beepTimer > 0 {
|
||||
cpu.beepTimer--
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// what if there's nothing in the stack?
|
||||
// return something sensible
|
||||
func (cpu *Chip8) leaveFunction() {
|
||||
} 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
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user