Preliminary CPU stepping code
[ScratchNES.git] / src / PPU / PPU.tosh
1 define-atomic evaluate sprites scanline: (N)
2 delete all of secondary OAM
3 delete all of evaluation line
4 repeat 32
5 add "255" to secondary OAM
6 add "-1" to evaluation line
7 add "-1" to evaluation line
8 add "-1" to evaluation line
9 add "-1" to evaluation line
10 add "-1" to evaluation line
11 add "-1" to evaluation line
12 add "-1" to evaluation line
13 add "-1" to evaluation line
14 end
15 set evaluation n to 1
16 set evaluation slot to 1
17 repeat 64
18 if evaluation slot < 32 and item evaluation n of OAM > N and item evaluation n of OAM - 8 < N then
19 replace item evaluation slot of secondary OAM with item evaluation n of OAM
20 replace item evaluation slot + 1 of secondary OAM with item (evaluation n + 1) of OAM
21 replace item evaluation slot + 2 of secondary OAM with item (evaluation n + 2) of OAM
22 set temp to item (evaluation n + 3) of OAM
23 replace item evaluation slot + 3 of secondary OAM with temp
24 change evaluation slot by 4
25 replace item temp of evaluation line with evaluation n
26 replace item temp + 1 of evaluation line with evaluation n
27 replace item temp + 2 of evaluation line with evaluation n
28 replace item temp + 3 of evaluation line with evaluation n
29 replace item temp + 4 of evaluation line with evaluation n
30 replace item temp + 5 of evaluation line with evaluation n
31 replace item temp + 6 of evaluation line with evaluation n
32 replace item temp + 7 of evaluation line with evaluation n
33 end
34 change evaluation n by 4
35 end
36 ; "TODO: evaluate sprites in secondary OAM"
37 ; "TODO: buggy sprite overflow flag"
38
39 define-atomic emulate 3 PPU cycles
40 change X by 3
41 set pen color to ((<not item X of evaluation line = -1> * 16000000 + <not item (X + 1) of evaluation line = -1> * 16000000 + <not item (X + 2) of evaluation line = -1> * 16000000) * 0.33)
42 change x by 3
43
44 define-atomic emulate frame
45 go to x: -128 y: 128
46 set Y to -1
47 pen down
48 repeat 224
49 change Y by 1
50 evaluate sprites scanline: (Y)
51 set x to -128
52 set X to 1
53 repeat until X > 240
54 step CPU
55 repeat cycles
56 emulate 3 PPU cycles
57 end
58 set cycles to 0
59 end
60 set PPU vblank? to 1
61 if PPU generate NMI = 1 then
62 interrupt: vector "0xFFFA"
63 end
64 change y by -1
65 end
66 pen up
67
68 define initialize PPU
69 pen up
70 clear
71 set pen size to 1
72 delete all of OAM
73 repeat 256
74 add "0" to OAM
75 end
76 hide
77
78 define read PPU register (N)
79 if N = 2 then
80 set M to 128 * PPU vblank? + 64 * PPU sprite 0? + 32 * PPU sprite overflow?
81 set PPU address latch to 0
82 set PPU vblank? to 0
83 else
84 if N = 7 then
85 read PPU memory
86 end
87 end
88
89 define get bit mask (N)
90 set temp to N
91 set mask to ""
92 repeat 8
93 set mask to (join (<temp mod 2 = 1> * 1) (mask))
94 set temp to (temp - temp mod 2) * 0.5
95 end
96
97 ; "TODO: use O(logN) lookup instead of O(N)"
98
99 define write PPU register (N) value: (V)
100 if N = 0 then
101 get bit mask (V)
102 set PPU base nametable address to 2 * letter 7 of mask + letter 8 of mask
103 set PPU VRAM increment to letter 6 of mask * 31 + 1
104 set PPU Sprite pattern table to letter 5 of mask * 4096
105 set PPU Background pattern table to letter 4 of mask * 4096
106 set PPU Sprite size to letter 3 of mask
107 set PPU master slave select to letter 2 of mask
108 set PPU generate NMI to letter 1 of mask
109 else
110 if N = 1 then
111 get bit mask (V)
112 set PPU grayscale to letter 8 of mask
113 set PPU show left8 bg to letter 7 of mask
114 set PPU show left8 sprites to letter 6 of mask
115 set PPU show bg to letter 5 of mask
116 set PPU show sprites to letter 4 of mask
117 set PPU emphasize blue to letter 3 of mask
118 set PPU emphasize green to letter 2 of mask
119 set PPU emphasize red to letter 1 of mask
120 else
121 if N = 3 then
122 set PPU OAMADDR to V
123 else
124 if N = 4 then
125 replace item PPU OAMADDR mod 256 + 1 of OAM with V
126 change PPU OAMADDR by 1
127 else
128 if N = 5 then
129 if PPU address latch = 0 then
130 set PPU fine x scroll to V
131 else
132 set PPU fine y scroll to V
133 end
134 else
135 if N = 6 then
136 if PPU address latch = 0 then
137 set PPU high address to V
138 else
139 set PPU low address to V
140 end
141 else
142 if N = 7 then
143 write PPU memory (V)
144 end
145 end
146 end
147 end
148 end
149 end
150 end
151
152 define OAM DMA (pagebase)
153 set temp to 1
154 repeat 256
155 mapper read (pagebase + temp)
156 replace item temp of OAM with M
157 change temp by 1
158 end
159
160 define read PPU memory
161 if PPU high address < 32 then
162 set M to item (1 + PPU high address * 256 + PPU low address) of Pattern tables
163 else
164 if PPU high address < 48 then
165 set M to item ((PPU high address * 256) + PPU low address - 8191) of Nametables
166 else
167 if PPU high address < 63 then
168 set M to item ((PPU high address * 256) + PPU low address - 12287) of Nametables
169 else
170 if PPU high address < 64 then
171 set M to item (PPU low address mod 32) + 1 of Palette
172 else
173 ; "TODO: PPU memory mirroring"
174 end
175 end
176 end
177 end
178
179 define write PPU memory (V)
180 if PPU high address < 32 then
181 replace item (1 + PPU high address * 256 + PPU low address) of Pattern tables with V
182 else
183 if PPU high address < 48 then
184 replace item ((PPU high address * 256) + PPU low address - 8191) of Nametables with V
185 else
186 if PPU high address < 63 then
187 replace item ((PPU high address * 256) + PPU low address - 12287) of Nametables with V
188 else
189 if PPU high address < 64 then
190 replace item (PPU low address mod 32) + 1 of Palette with V
191 else
192 ; "TODO: PPU memory mirroring"
193 end
194 end
195 end
196 end
This page took 0.074808 seconds and 5 git commands to generate.