From 98741356a6c0984b5705d3f7f7e95ac4776355a3 Mon Sep 17 00:00:00 2001 From: "S.D" Date: Sat, 10 Oct 2020 23:08:57 +0100 Subject: [PATCH] get rid of underscores -> camelCase --- pkg/chip8/chip8.go | 47 ++++++++++++++++++++++++++++++----------- pkg/chip8/chip8_test.go | 12 +++++------ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/pkg/chip8/chip8.go b/pkg/chip8/chip8.go index d7ef11b..bd9ae11 100644 --- a/pkg/chip8/chip8.go +++ b/pkg/chip8/chip8.go @@ -5,17 +5,17 @@ import "fmt" const graphicsBufferSize = 64 * 32 type Chip8 struct { - address_register uint16 - beep_timer uint16 - draw_required bool - delay_timer uint16 - graphics [graphicsBufferSize]byte - keys [16]byte - memory [4096]byte - opcode uint16 - pc uint16 - registers [16]byte // an array - has a fixed length - stack []uint16 // a slice - basically a c++ vector + addressRegister uint16 + beepTimer uint16 + drawRequired bool + delayTimer uint16 + graphics [graphicsBufferSize]byte + keys [16]byte + memory [4096]byte + opcode uint16 + pc uint16 + registers [16]byte // an array - has a fixed length + stack []uint16 // a slice - basically a c++ vector } // we can't have const arrays in go @@ -60,7 +60,7 @@ func (cpu *Chip8) GetGraphicsBuffer() [graphicsBufferSize]byte { func (cpu *Chip8) clearDisplay() { // fuck it the gc can do the hard work for us cpu.graphics = [64 * 32]byte{} - cpu.draw_required = true + cpu.drawRequired = true } // what if there's nothing in the stack? @@ -153,9 +153,32 @@ func (cpu *Chip8) BitOpsAndMath() { // Store lsb of reg X in reg F and then shift reg X >> 1 cpu.registers[0x0F] = cpu.registers[regX] & 0x01 cpu.registers[regX] >>= 1 + case 7: + // 8XY57 + // Set register x to X=Y-X (Set VF to 1 when there's a carry and 0 if not) + res := cpu.registers[regY] - cpu.registers[regX] + if res < 0 { + cpu.registers[0x0F] = 0 + } else { + cpu.registers[0x0f] = 1 + } + cpu.registers[regX] = res & 0xFF } } +func (cpu *Chip8) SkipIfRegistersNotEqual() { + x, y := (cpu.opcode>>8)&0x0F, (cpu.opcode>>4)&0x0F + if cpu.registers[x] != cpu.registers[y] { + cpu.pc += 2 + } +} + +func (cpu *Chip8) SetAddressRegister() { + // ANNN + // Sets the address register to NNN + cpu.addressRegister = cpu.opcode & 0x0FFF +} + func main() { fmt.Printf("Hello world!\n") prog := []byte{1, 2, 3, 4} diff --git a/pkg/chip8/chip8_test.go b/pkg/chip8/chip8_test.go index 623caef..68329d4 100644 --- a/pkg/chip8/chip8_test.go +++ b/pkg/chip8/chip8_test.go @@ -21,8 +21,8 @@ func slicesEqual(x, y []byte) bool { // Maybe fix func TestCreateCPU(t *testing.T) { prog := []byte{1, 2, 3, 4} - new_cpu := NewCHIP8(prog) - if !slicesEqual(new_cpu.memory[200:204], prog) { + newCPU := NewCHIP8(prog) + if !slicesEqual(newCPU.memory[200:204], prog) { t.Errorf("CPU not initalized properly") } } @@ -33,10 +33,10 @@ func TestClearDisplay(t *testing.T) { cpu.graphics[i] = byte(i % 255) } cpu.clearDisplay() - graphics_array := cpu.GetGraphicsBuffer() - graphics_slice := graphics_array[:] - empty_slice := make([]byte, len(cpu.graphics)) - if !slicesEqual(graphics_slice, empty_slice) { + graphicsArray := cpu.GetGraphicsBuffer() + graphicsSlice := graphicsArray[:] + emptySlice := make([]byte, len(cpu.graphics)) + if !slicesEqual(graphicsSlice, emptySlice) { t.Errorf("Graphics buffer not cleared properly") } }