2019-01-26 16:05:51 +00:00
|
|
|
package main
|
|
|
|
|
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-12 23:03:43 +00:00
|
|
|
type memoryPage interface {
|
|
|
|
peek(uint8) uint8
|
|
|
|
poke(uint8, uint8)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ramPage [256]uint8
|
|
|
|
type romPage [256]uint8
|
|
|
|
|
|
|
|
type memory [256]memoryPage
|
|
|
|
|
|
|
|
func (p *ramPage) peek(address uint8) uint8 {
|
|
|
|
return p[address]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ramPage) poke(address uint8, value uint8) {
|
|
|
|
p[address] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *romPage) peek(address uint8) uint8 {
|
|
|
|
return p[address]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *romPage) poke(address uint8, value uint8) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) peek(address uint16) uint8 {
|
|
|
|
hi := uint8(address >> 8)
|
|
|
|
lo := uint8(address)
|
|
|
|
return m[hi].peek(lo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) poke(address uint16, value uint8) {
|
|
|
|
hi := uint8(address >> 8)
|
|
|
|
lo := uint8(address)
|
|
|
|
//fmt.Println(hi)
|
|
|
|
m[hi].poke(lo, value)
|
|
|
|
}
|
2019-01-27 17:13:16 +00:00
|
|
|
|
|
|
|
func (m *memory) getWord(address uint16) uint16 {
|
2019-02-12 23:03:43 +00:00
|
|
|
return uint16(m.peek(address)) + 0x100*uint16(m.peek(address+1))
|
2019-01-27 17:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) getZeroPageWord(address uint8) uint16 {
|
2019-02-12 23:03:43 +00:00
|
|
|
return uint16(m.peek(uint16(address))) + 0x100*uint16(m.peek(uint16(address+1)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) initWithRam() {
|
|
|
|
var ramPages [256]ramPage
|
|
|
|
for i := 0; i < 256; i++ {
|
|
|
|
m[i] = &ramPages[i]
|
|
|
|
}
|
2019-01-27 17:13:16 +00:00
|
|
|
}
|
2019-02-09 23:15:14 +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-12 23:03:43 +00:00
|
|
|
m.initWithRam()
|
2019-02-09 23:15:14 +00:00
|
|
|
for i, v := range bytes {
|
2019-02-12 23:03:43 +00:00
|
|
|
m.poke(uint16(i), uint8(v))
|
2019-02-09 23:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-10 13:01:57 +00:00
|
|
|
|
|
|
|
func (m *memory) printPage(page uint8) {
|
|
|
|
address := uint16(page) * 0x100
|
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
fmt.Printf("%#04x: ", address)
|
|
|
|
for j := 0; j < 16; j++ {
|
|
|
|
fmt.Printf("%02x ", m[address])
|
|
|
|
address++
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|