mirror of
https://github.com/ariejan/i6502.git
synced 2024-05-28 22:41:34 +00:00
Add Cpu program loading
This commit is contained in:
parent
c8d2f31a65
commit
0fe91268ad
14
cpu.go
14
cpu.go
|
@ -8,9 +8,10 @@ type Cpu struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ResetVector = 0xFFFC
|
ResetVector = 0xFFFC // 0xFFFC-FFFD
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Create an new Cpu instance with the specified AddressBus
|
||||||
func NewCpu(bus *AddressBus) (*Cpu, error) {
|
func NewCpu(bus *AddressBus) (*Cpu, error) {
|
||||||
return &Cpu{bus: bus}, nil
|
return &Cpu{bus: bus}, nil
|
||||||
}
|
}
|
||||||
|
@ -19,7 +20,18 @@ func (c *Cpu) HasAddressBus() bool {
|
||||||
return c.bus != nil
|
return c.bus != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset the CPU, emulating the RESB pin.
|
||||||
func (c *Cpu) Reset() {
|
func (c *Cpu) Reset() {
|
||||||
c.PC = c.bus.Read16(ResetVector)
|
c.PC = c.bus.Read16(ResetVector)
|
||||||
c.P = 0x34
|
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
|
||||||
|
}
|
||||||
|
|
19
cpu_test.go
19
cpu_test.go
|
@ -12,6 +12,8 @@ func NewRamMachine() (*Cpu, *AddressBus, *Ram) {
|
||||||
bus.Attach(ram, 0x0000)
|
bus.Attach(ram, 0x0000)
|
||||||
cpu, _ := NewCpu(bus)
|
cpu, _ := NewCpu(bus)
|
||||||
|
|
||||||
|
cpu.Reset()
|
||||||
|
|
||||||
return cpu, bus, ram
|
return cpu, bus, ram
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +31,7 @@ func TestCpuAddressBus(t *testing.T) {
|
||||||
assert.True(cpu.HasAddressBus())
|
assert.True(cpu.HasAddressBus())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCpuState(t *testing.T) {
|
func TestCpuReset(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
cpu, _, _ := NewRamMachine()
|
cpu, _, _ := NewRamMachine()
|
||||||
|
@ -46,3 +48,18 @@ func TestCpuState(t *testing.T) {
|
||||||
// Read PC from $FFFC-FFFD
|
// Read PC from $FFFC-FFFD
|
||||||
assert.Equal(0x1234, cpu.PC)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user