very basic emulator structure
This commit is contained in:
parent
05fb9d9f33
commit
249486ee24
|
@ -1 +1,35 @@
|
||||||
package chip8
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1,19 @@
|
||||||
package chip8_test
|
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])
|
||||||
|
}
|
||||||
|
|
7
go.mod
7
go.mod
|
@ -1,3 +1,10 @@
|
||||||
module github.com/tangzero/chip8-emulator
|
module github.com/tangzero/chip8-emulator
|
||||||
|
|
||||||
go 1.17
|
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
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user