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