2019-02-16 19:15:41 +00:00
|
|
|
package core6502
|
2019-01-26 16:05:51 +00:00
|
|
|
|
2019-02-09 23:15:14 +00:00
|
|
|
import (
|
|
|
|
"bufio"
|
2019-02-10 13:01:57 +00:00
|
|
|
"fmt"
|
2019-02-09 23:15:14 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
// MemoryPage is a data page of 256 bytes
|
|
|
|
type MemoryPage interface {
|
|
|
|
Peek(uint8) uint8
|
|
|
|
Poke(uint8, uint8)
|
2019-02-12 23:03:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
// Memory represents the addressable space of the processor
|
|
|
|
type Memory struct {
|
|
|
|
data [256]MemoryPage
|
2019-02-14 23:41:56 +00:00
|
|
|
}
|
2019-02-12 23:03:43 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
// Peek returns the data on the given address
|
|
|
|
func (m *Memory) Peek(address uint16) uint8 {
|
2019-02-12 23:03:43 +00:00
|
|
|
hi := uint8(address >> 8)
|
|
|
|
lo := uint8(address)
|
2019-02-16 19:15:41 +00:00
|
|
|
return m.data[hi].Peek(lo)
|
2019-02-12 23:03:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
// Poke sets the data at the given address
|
|
|
|
func (m *Memory) Poke(address uint16, value uint8) {
|
2019-02-12 23:03:43 +00:00
|
|
|
hi := uint8(address >> 8)
|
|
|
|
lo := uint8(address)
|
|
|
|
//fmt.Println(hi)
|
2019-02-16 19:15:41 +00:00
|
|
|
m.data[hi].Poke(lo, value)
|
2019-02-12 23:03:43 +00:00
|
|
|
}
|
2019-01-27 17:13:16 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
// SetPage assigns a MemoryPage implementation on the page given
|
|
|
|
func (m *Memory) SetPage(index uint8, page MemoryPage) {
|
|
|
|
m.data[index] = page
|
2019-01-27 17:13:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (m *Memory) getWord(address uint16) uint16 {
|
|
|
|
return uint16(m.Peek(address)) + 0x100*uint16(m.Peek(address+1))
|
2019-02-12 23:03:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (m *Memory) getZeroPageWord(address uint8) uint16 {
|
|
|
|
return uint16(m.Peek(uint16(address))) + 0x100*uint16(m.Peek(uint16(address+1)))
|
2019-01-27 17:13:16 +00:00
|
|
|
}
|
2019-02-09 23:15:14 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (m *Memory) loadBinary(filename string) {
|
2019-02-12 23:03:43 +00:00
|
|
|
// Load file
|
2019-02-09 23:15:14 +00:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
stats, statsErr := f.Stat()
|
|
|
|
if statsErr != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
size := stats.Size()
|
|
|
|
bytes := make([]byte, size)
|
|
|
|
|
|
|
|
buf := bufio.NewReader(f)
|
|
|
|
buf.Read(bytes)
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
m.InitWithRAM()
|
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-02-10 13:01:57 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (m *Memory) printPage(page uint8) {
|
2019-02-10 13:01:57 +00:00
|
|
|
address := uint16(page) * 0x100
|
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
fmt.Printf("%#04x: ", address)
|
|
|
|
for j := 0; j < 16; j++ {
|
2019-02-14 23:41:56 +00:00
|
|
|
fmt.Printf("%02x ", m.data[address])
|
2019-02-10 13:01:57 +00:00
|
|
|
address++
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|