Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2a5fbca434
Fetching contributors…

Cannot retrieve contributors at this time

101 lines (82 sloc) 1.777 kb
package main
import (
"fmt"
)
type Word uint8
type Memory [0x10000]Word
type MemoryError struct {
ErrorText string
}
func (e MemoryError) Error() string {
return e.ErrorText
}
var (
Ram Memory
)
func fitAddressSize(addr interface{}) (v int, e error) {
switch a := addr.(type) {
case Word:
v = int(a)
case int:
v = int(a)
default:
e = MemoryError{ErrorText: "Invalid type used"}
}
return
}
func (m *Memory) Init() {
for index, _ := range m {
m[index] = 0x00
}
}
func (m *Memory) ReadMirroredRam(a int) Word {
offset := a % 0x8
return m[0x2000+offset]
}
func (m *Memory) WriteMirroredRam(v Word, a int) {
offset := a % 0x8
m[0x2000+offset] = v
}
func (m *Memory) Write(address interface{}, val Word) error {
if a, err := fitAddressSize(address); err == nil {
if a >= 0x2008 && a < 0x4000 {
fmt.Printf("Address write: 0x%X\n", a)
}
if a >= 0x2000 && a <= 0x2007 {
ppu.PpuRegWrite(val, a)
// m.WriteMirroredRam(val, a)
} else if a == 0x4014 {
ppu.PpuRegWrite(val, a)
m[a] = val
} else if a == 0x4016 {
controller.Write(val)
m[a] = val
} else if a == 0x4017 {
// controller.WritePad2(val)
m[a] = 0
} else if a >= 0x8000 && a <= 0xFFFF {
// MMC1
rom.Write(val, a)
return nil
} else if a >= 0x6000 && a < 0x8000 {
m[a] = val
} else {
m[a] = val
}
return nil
}
return MemoryError{ErrorText: "Invalid address used"}
}
func (m *Memory) Read(address interface{}) (Word, error) {
a, _ := fitAddressSize(address)
if a >= 0x2008 && a < 0x4000 {
offset := a % 0x8
return ppu.PpuRegRead(0x2000 + offset)
} else if a <= 0x2007 && a >= 0x2000 {
//ppu.Run(cpu.Timestamp)
return ppu.PpuRegRead(a)
} else if a == 0x4016 {
return controller.Read(), nil
}
return m[a], nil
}
Jump to Line
Something went wrong with that request. Please try again.