2019-05-16 20:51:04 +00:00
|
|
|
package apple2
|
|
|
|
|
2019-05-17 21:28:20 +00:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2019-05-16 20:51:04 +00:00
|
|
|
type memoryRange struct {
|
|
|
|
base uint16
|
|
|
|
data []uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMemoryRange(base uint16, data []uint8) *memoryRange {
|
|
|
|
var m memoryRange
|
|
|
|
m.base = base
|
|
|
|
m.data = data
|
|
|
|
return &m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryRange) peek(address uint16) uint8 {
|
|
|
|
return m.data[address-m.base]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryRange) poke(address uint16, value uint8) {
|
|
|
|
m.data[address-m.base] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryRange) subRange(a, b uint16) []uint8 {
|
|
|
|
return m.data[a-m.base : b-m.base]
|
|
|
|
}
|
2019-05-17 21:28:20 +00:00
|
|
|
|
2019-10-05 23:26:00 +00:00
|
|
|
func (m *memoryRange) save(w io.Writer) error {
|
|
|
|
err := binary.Write(w, binary.BigEndian, m.base)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = binary.Write(w, binary.BigEndian, m.data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-17 21:28:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-05 23:26:00 +00:00
|
|
|
func (m *memoryRange) load(r io.Reader) error {
|
|
|
|
err := binary.Read(r, binary.BigEndian, &m.base)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = binary.Read(r, binary.BigEndian, &m.data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-17 21:28:20 +00:00
|
|
|
}
|