1
0
mirror of https://github.com/ariejan/i6502.git synced 2024-12-26 23:29:17 +00:00

Add ROM Memory

This commit is contained in:
Ariejan de Vroom 2014-08-17 16:20:30 +02:00
parent b4bbadd369
commit 0e1d9eaa8f
5 changed files with 80 additions and 6 deletions

View File

@ -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))
}

38
rom.go Normal file
View File

@ -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))
}

39
rom_test.go Normal file
View File

@ -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)
}

BIN
test/16kb.rom Normal file

Binary file not shown.

BIN
test/8kb.rom Normal file

Binary file not shown.