Add Cpu program loading

This commit is contained in:
Ariejan de Vroom 2014-08-13 11:49:49 +02:00
parent c8d2f31a65
commit 0fe91268ad
2 changed files with 31 additions and 2 deletions

14
cpu.go
View File

@ -8,9 +8,10 @@ type Cpu struct {
}
const (
ResetVector = 0xFFFC
ResetVector = 0xFFFC // 0xFFFC-FFFD
)
// Create an new Cpu instance with the specified AddressBus
func NewCpu(bus *AddressBus) (*Cpu, error) {
return &Cpu{bus: bus}, nil
}
@ -19,7 +20,18 @@ func (c *Cpu) HasAddressBus() bool {
return c.bus != nil
}
// Reset the CPU, emulating the RESB pin.
func (c *Cpu) Reset() {
c.PC = c.bus.Read16(ResetVector)
c.P = 0x34
}
// Load the specified program data at the given memory location
// and point the Program Counter to the beginning of the program
func (c *Cpu) LoadProgram(data []byte, location uint16) {
for i, b := range data {
c.bus.Write(location+uint16(i), b)
}
c.PC = location
}

View File

@ -12,6 +12,8 @@ func NewRamMachine() (*Cpu, *AddressBus, *Ram) {
bus.Attach(ram, 0x0000)
cpu, _ := NewCpu(bus)
cpu.Reset()
return cpu, bus, ram
}
@ -29,7 +31,7 @@ func TestCpuAddressBus(t *testing.T) {
assert.True(cpu.HasAddressBus())
}
func TestCpuState(t *testing.T) {
func TestCpuReset(t *testing.T) {
assert := assert.New(t)
cpu, _, _ := NewRamMachine()
@ -46,3 +48,18 @@ func TestCpuState(t *testing.T) {
// Read PC from $FFFC-FFFD
assert.Equal(0x1234, cpu.PC)
}
func TestProgramLoading(t *testing.T) {
assert := assert.New(t)
program := []byte{0xEA, 0xEB, 0xEC}
cpu, bus, _ := NewRamMachine()
cpu.LoadProgram(program, 0x0300)
assert.Equal(0xEA, bus.Read(0x0300))
assert.Equal(0xEB, bus.Read(0x0301))
assert.Equal(0xEC, bus.Read(0x0302))
assert.Equal(0x0300, cpu.PC)
}