Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 101 lines (82 sloc) 1.777 kB
981cb3e @scottferg Initial commit
authored
1 package main
2
ef959c1 @scottferg Pass more ppu vbl nmi tests
authored
3 import (
3a762e4 @scottferg Run gofmt
authored
4 "fmt"
ef959c1 @scottferg Pass more ppu vbl nmi tests
authored
5 )
6
981cb3e @scottferg Initial commit
authored
7 type Word uint8
2ab533d @scottferg Add more tests
authored
8
4258491 @scottferg Fix PPU addressing
authored
9 type Memory [0x10000]Word
2ab533d @scottferg Add more tests
authored
10
11 type MemoryError struct {
c3e4401 @scottferg Run gofmt
authored
12 ErrorText string
2ab533d @scottferg Add more tests
authored
13 }
14
15 func (e MemoryError) Error() string {
c3e4401 @scottferg Run gofmt
authored
16 return e.ErrorText
2ab533d @scottferg Add more tests
authored
17 }
981cb3e @scottferg Initial commit
authored
18
19 var (
c3e4401 @scottferg Run gofmt
authored
20 Ram Memory
981cb3e @scottferg Initial commit
authored
21 )
2ab533d @scottferg Add more tests
authored
22
23 func fitAddressSize(addr interface{}) (v int, e error) {
c3e4401 @scottferg Run gofmt
authored
24 switch a := addr.(type) {
25 case Word:
26 v = int(a)
27 case int:
28 v = int(a)
29 default:
30 e = MemoryError{ErrorText: "Invalid type used"}
31 }
2ab533d @scottferg Add more tests
authored
32
c3e4401 @scottferg Run gofmt
authored
33 return
2ab533d @scottferg Add more tests
authored
34 }
35
36 func (m *Memory) Init() {
c3e4401 @scottferg Run gofmt
authored
37 for index, _ := range m {
4258491 @scottferg Fix PPU addressing
authored
38 m[index] = 0x00
c3e4401 @scottferg Run gofmt
authored
39 }
2ab533d @scottferg Add more tests
authored
40 }
41
ef959c1 @scottferg Pass more ppu vbl nmi tests
authored
42 func (m *Memory) ReadMirroredRam(a int) Word {
3a762e4 @scottferg Run gofmt
authored
43 offset := a % 0x8
44 return m[0x2000+offset]
ef959c1 @scottferg Pass more ppu vbl nmi tests
authored
45 }
46
39962e1 @scottferg Continued work on PPU fixes
authored
47 func (m *Memory) WriteMirroredRam(v Word, a int) {
3a762e4 @scottferg Run gofmt
authored
48 offset := a % 0x8
49 m[0x2000+offset] = v
39962e1 @scottferg Continued work on PPU fixes
authored
50 }
51
2ab533d @scottferg Add more tests
authored
52 func (m *Memory) Write(address interface{}, val Word) error {
c3e4401 @scottferg Run gofmt
authored
53 if a, err := fitAddressSize(address); err == nil {
3a762e4 @scottferg Run gofmt
authored
54 if a >= 0x2008 && a < 0x4000 {
55 fmt.Printf("Address write: 0x%X\n", a)
56 }
39962e1 @scottferg Continued work on PPU fixes
authored
57
ef959c1 @scottferg Pass more ppu vbl nmi tests
authored
58 if a >= 0x2000 && a <= 0x2007 {
39962e1 @scottferg Continued work on PPU fixes
authored
59 ppu.PpuRegWrite(val, a)
3a762e4 @scottferg Run gofmt
authored
60 // m.WriteMirroredRam(val, a)
0cadfc4 @scottferg Broken
authored
61 } else if a == 0x4014 {
39962e1 @scottferg Continued work on PPU fixes
authored
62 ppu.PpuRegWrite(val, a)
3a762e4 @scottferg Run gofmt
authored
63 m[a] = val
0cadfc4 @scottferg Broken
authored
64 } else if a == 0x4016 {
65 controller.Write(val)
3a762e4 @scottferg Run gofmt
authored
66 m[a] = val
ed8a35f @scottferg Fix weird crash in Zelda
authored
67 } else if a == 0x4017 {
68 // controller.WritePad2(val)
3a762e4 @scottferg Run gofmt
authored
69 m[a] = 0
39962e1 @scottferg Continued work on PPU fixes
authored
70 } else if a >= 0x8000 && a <= 0xFFFF {
71 // MMC1
f35bf0a @scottferg Refactor ROM loading, SMB works with 261 scanlines
authored
72 rom.Write(val, a)
39962e1 @scottferg Continued work on PPU fixes
authored
73 return nil
062d9e4 @scottferg Add flag to checkif battery RAM is present
authored
74 } else if a >= 0x6000 && a < 0x8000 {
3a762e4 @scottferg Run gofmt
authored
75 m[a] = val
76 } else {
77 m[a] = val
78 }
2ab533d @scottferg Add more tests
authored
79
c3e4401 @scottferg Run gofmt
authored
80 return nil
81 }
2ab533d @scottferg Add more tests
authored
82
c3e4401 @scottferg Run gofmt
authored
83 return MemoryError{ErrorText: "Invalid address used"}
2ab533d @scottferg Add more tests
authored
84 }
85
86 func (m *Memory) Read(address interface{}) (Word, error) {
c3e4401 @scottferg Run gofmt
authored
87 a, _ := fitAddressSize(address)
211d612 @scottferg Fixed stack operations
authored
88
3a762e4 @scottferg Run gofmt
authored
89 if a >= 0x2008 && a < 0x4000 {
90 offset := a % 0x8
ef959c1 @scottferg Pass more ppu vbl nmi tests
authored
91 return ppu.PpuRegRead(0x2000 + offset)
3a762e4 @scottferg Run gofmt
authored
92 } else if a <= 0x2007 && a >= 0x2000 {
09e9aa7 @scottferg Run gofmt
authored
93 //ppu.Run(cpu.Timestamp)
39962e1 @scottferg Continued work on PPU fixes
authored
94 return ppu.PpuRegRead(a)
0cadfc4 @scottferg Broken
authored
95 } else if a == 0x4016 {
96 return controller.Read(), nil
97 }
2ab533d @scottferg Add more tests
authored
98
c3e4401 @scottferg Run gofmt
authored
99 return m[a], nil
2ab533d @scottferg Add more tests
authored
100 }
Something went wrong with that request. Please try again.