diff --git a/ram_test.go b/ram_test.go index 6890621..f958986 100644 --- a/ram_test.go +++ b/ram_test.go @@ -6,21 +6,18 @@ import ( ) func TestRamSize(t *testing.T) { - assert := assert.New(t) - ram, _ := NewRam(0x8000) // 32 kB - assert.Equal(0x8000, ram.Size()) + assert.Equal(t, 0x8000, ram.Size()) } func TestRamReadWrite(t *testing.T) { - assert := assert.New(t) ram, _ := NewRam(0x8000) // 32 kB // Ram zeroed out initially for i := 0; i < 0x8000; i++ { - assert.Equal(0x00, ram.data[i]) + assert.Equal(t, 0x00, ram.data[i]) } ram.Write(0x1000, 0x42) - assert.Equal(0x42, ram.Read(0x1000)) + assert.Equal(t, 0x42, ram.Read(0x1000)) } diff --git a/rom.go b/rom.go new file mode 100644 index 0000000..9c8e248 --- /dev/null +++ b/rom.go @@ -0,0 +1,38 @@ +package i6502 + +import ( + "fmt" + "io/ioutil" +) + +/* +Read-Only Memory +*/ +type Rom struct { + data []byte +} + +/* +Create a new Rom component, using the content of `path`. The file automatically +specifies the size of Rom. +*/ +func NewRom(path string) (*Rom, error) { + data, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + + return &Rom{data: data}, nil +} + +func (r *Rom) Size() uint16 { + return uint16(len(r.data)) +} + +func (r *Rom) Read(address uint16) byte { + return r.data[address] +} + +func (r *Rom) Write(address uint16, data byte) { + panic(fmt.Errorf("Trying to write to ROM at 0x%04X", address)) +} diff --git a/rom_test.go b/rom_test.go new file mode 100644 index 0000000..58a24b3 --- /dev/null +++ b/rom_test.go @@ -0,0 +1,39 @@ +package i6502 + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test8kRoms(t *testing.T) { + rom, err := NewRom("test/8kb.rom") + + assert.Nil(t, err) + assert.Equal(t, 0x2000, rom.Size()) + assert.Equal(t, 0x01, rom.Read(0x0000)) + assert.Equal(t, 0xFF, rom.Read(0x2000-1)) +} + +func TestRomWritePanic(t *testing.T) { + rom, _ := NewRom("test/8kb.rom") + + // Writing to rom should panic + assert.Panics(t, func() { + rom.Write(0x1337, 0x42) + }, "Writing to Rom should panic") +} + +func Test16kRom(t *testing.T) { + rom, err := NewRom("test/16kb.rom") + + assert.Nil(t, err) + assert.Equal(t, 0x4000, rom.Size()) + assert.Equal(t, 0x01, rom.Read(0x0000)) + assert.Equal(t, 0xFF, rom.Read(0x4000-1)) +} + +func TestRomNotFound(t *testing.T) { + rom, err := NewRom("test/does-not-exists.rom") + assert.NotNil(t, err) + assert.Nil(t, rom) +} diff --git a/test/16kb.rom b/test/16kb.rom new file mode 100644 index 0000000..7f39590 Binary files /dev/null and b/test/16kb.rom differ diff --git a/test/8kb.rom b/test/8kb.rom new file mode 100644 index 0000000..c138c62 Binary files /dev/null and b/test/8kb.rom differ