diff --git a/chip8/chip8.go b/chip8/chip8.go index 2144af5..3ef4e8b 100644 --- a/chip8/chip8.go +++ b/chip8/chip8.go @@ -1 +1,35 @@ package chip8 + +type Emulator struct { + V [16]uint8 // general registers + I uint16 // address register + SP uint16 // stack pointer + PC uint16 // program counter + Memory [4096]uint8 // 4KB of system RAM + Stack []uint8 // 32 bytes of stack. starting at 0x0EA0 + Screen []uint8 // 256 bytes of display buffer. starting at 0x0F00 + Timer struct { + Delay uint8 // delay timer + Sound uint8 // sound timer + } + ROM []uint8 +} + +func NewEmulator() *Emulator { + emulator := new(Emulator) + emulator.Reset() + return emulator +} + +func (emulator *Emulator) Reset() { + emulator.V = [16]uint8{} + emulator.I = 0x00 + emulator.SP = 0x00 + emulator.PC = 0x0200 + emulator.Memory = [4096]uint8{} + emulator.Stack = emulator.Memory[0x0EA0:] + emulator.Screen = emulator.Memory[0x0F00:] + emulator.Timer.Delay = 0 + emulator.Timer.Sound = 0 + copy(emulator.Memory[0x0200:], emulator.ROM) +} diff --git a/chip8/chip8_test.go b/chip8/chip8_test.go index 3a3f634..f490828 100644 --- a/chip8/chip8_test.go +++ b/chip8/chip8_test.go @@ -1 +1,19 @@ package chip8_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tangzero/chip8-emulator/chip8" +) + +func TestEmulator_Reset(t *testing.T) { + emulator := chip8.NewEmulator() + emulator.V[0x03] = 0xFF + emulator.V[0x0F] = 0xBB + + emulator.Reset() + + assert.Equal(t, uint8(0x00), emulator.V[0x03]) + assert.Equal(t, uint8(0x00), emulator.V[0x0F]) +} diff --git a/go.mod b/go.mod index 84a78b7..6621ede 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module github.com/tangzero/chip8-emulator go 1.17 + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.7.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +)