2019-02-16 19:15:41 +00:00
|
|
|
package core6502
|
2019-01-26 16:05:51 +00:00
|
|
|
|
2019-05-15 14:01:04 +00:00
|
|
|
import "io/ioutil"
|
2019-02-09 23:15:14 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
// Memory represents the addressable space of the processor
|
2019-02-22 17:00:53 +00:00
|
|
|
type Memory interface {
|
|
|
|
Peek(address uint16) uint8
|
|
|
|
Poke(address uint16, value uint8)
|
2020-08-14 15:19:24 +00:00
|
|
|
|
|
|
|
// PeekCode can bu used to optimize the memory manager to requests with more
|
|
|
|
// locality. It must return the same as a call to Peek()
|
|
|
|
PeekCode(address uint16) uint8
|
2019-02-14 23:41:56 +00:00
|
|
|
}
|
2019-02-12 23:03:43 +00:00
|
|
|
|
2019-02-22 17:00:53 +00:00
|
|
|
func getWord(m Memory, address uint16) uint16 {
|
|
|
|
return uint16(m.Peek(address)) + 0x100*uint16(m.Peek(address+1))
|
2019-02-12 23:03:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:00:53 +00:00
|
|
|
func getZeroPageWord(m Memory, address uint8) uint16 {
|
|
|
|
return uint16(m.Peek(uint16(address))) + 0x100*uint16(m.Peek(uint16(address+1)))
|
2019-02-12 23:03:43 +00:00
|
|
|
}
|
2019-01-27 17:13:16 +00:00
|
|
|
|
2019-05-15 14:01:04 +00:00
|
|
|
// FlatMemory puts RAM on the 64Kb addressable by the processor
|
2019-02-22 17:00:53 +00:00
|
|
|
type FlatMemory struct {
|
|
|
|
data [65536]uint8
|
2019-01-27 17:13:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:00:53 +00:00
|
|
|
// Peek returns the data on the given address
|
|
|
|
func (m *FlatMemory) Peek(address uint16) uint8 {
|
|
|
|
return m.data[address]
|
2019-02-12 23:03:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 13:41:21 +00:00
|
|
|
// PeekCode returns the data on the given address
|
|
|
|
func (m *FlatMemory) PeekCode(address uint16) uint8 {
|
|
|
|
return m.data[address]
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:00:53 +00:00
|
|
|
// Poke sets the data at the given address
|
|
|
|
func (m *FlatMemory) Poke(address uint16, value uint8) {
|
|
|
|
m.data[address] = value
|
2019-01-27 17:13:16 +00:00
|
|
|
}
|
2019-02-09 23:15:14 +00:00
|
|
|
|
2019-10-05 23:26:00 +00:00
|
|
|
func (m *FlatMemory) loadBinary(filename string) error {
|
2019-05-15 14:01:04 +00:00
|
|
|
bytes, err := ioutil.ReadFile(filename)
|
2019-02-09 23:15:14 +00:00
|
|
|
if err != nil {
|
2019-10-05 23:26:00 +00:00
|
|
|
return err
|
2019-02-09 23:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range bytes {
|
2019-02-16 19:15:41 +00:00
|
|
|
m.Poke(uint16(i), uint8(v))
|
2019-02-09 23:15:14 +00:00
|
|
|
}
|
2019-10-05 23:26:00 +00:00
|
|
|
|
|
|
|
return nil
|
2019-02-09 23:15:14 +00:00
|
|
|
}
|