Pattern tables :D[
[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 <N > item evaluation n of OAM> 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 replace item temp of evaluation line with evaluation slot
25 replace item temp + 1 of evaluation line with evaluation slot
26 replace item temp + 2 of evaluation line with evaluation slot
27 replace item temp + 3 of evaluation line with evaluation slot
28 replace item temp + 4 of evaluation line with evaluation slot
29 replace item temp + 5 of evaluation line with evaluation slot
30 replace item temp + 6 of evaluation line with evaluation slot
31 replace item temp + 7 of evaluation line with evaluation slot
32 change evaluation slot by 4
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 get pattern tile: (tile) scanline: (scanline)
40 get bit mask (join "0x" ((item (16 * tile + scanline) of CHR-ROM)))
41 set lower mask to mask
42 get bit mask (join "0x" ((item (16 * tile + scanline+8) of CHR-ROM)))
43
44 define-atomic compute pixel
45 if item mX+1 of evaluation line = -1 then
46 ; "Background pixel. TODO nametables"
47 set tmp to 0
48 else
49 ; "A sprite! That I can do! Fetch it from CHR and decode"
50 get pattern tile: item ((item mX+1 of evaluation line))+1 of secondary OAM scanline: (mY - (item (item mX+1 of evaluation line) of secondary OAM))
51 set offset to mX - item (item mX+1 of evaluation line + 3) of secondary OAM
52 change offset by 1
53 set tmp to (2 * letter offset of lower mask) + letter offset of mask
54 if tmp > 1 then
55 if tmp = 2 then
56 set tmp to "0xFF0000"
57 else
58 set tmp to "0xFFFFFF"
59 end
60 else
61 if tmp = 0 then
62 set tmp to "0x000000"
63 else
64 set tmp to "0x0000FF"
65 end
66 end
67 end
68
69 define emulate frame
70 go to x: -128 y: 128
71 set mY to -1
72 ; "show variable mX"
73 ; "show variable mY"
74 repeat 224
75 change mY by 1
76 evaluate sprites scanline: (mY)
77 go to x: -128 y: y position - 1
78 pen down
79 set mX to 0
80 repeat until mX > 254
81 step CPU
82 repeat cycles
83 compute pixel
84 set pen color to (tmp*1)
85 change mX by 1
86 change x by 1
87 compute pixel
88 set pen color to (tmp*1)
89 change x by 1
90 change mX by 1
91 compute pixel
92 set pen color to (tmp*1)
93 change x by 1
94 change mX by 1
95 end
96 set cycles to 0
97 end
98 pen up
99 end
100 set PPU vblank?1 to 1
101 if PPU generate NMI = 1 then
102 interrupt: vector "0xFFFA"
103 end
104 repeat until cycles > 2380
105 step CPU
106 end
107 set cycles to 0
108
109 define initialize PPU
110 pen up
111 clear
112 set pen size to 1
113 delete all of OAM
114 repeat 256
115 add "0" to OAM
116 end
117 hide
118
119 define read PPU register (N)
120 if N = 2 then
121 set M to 128 * PPU vblank?1 + 64 * PPU sprite 0?1 + 32 * PPU sprite overflow?1
122 set PPU address latch to 0
123 set PPU vblank?1 to 0
124 else
125 if N = 7 then
126 read PPU memory
127 end
128 end
129
130 define get bit mask (N)
131 set temp to N
132 set mask to ""
133 repeat 8
134 set mask to (join (<temp mod 2 = 1> * 1) (mask))
135 set temp to (temp - temp mod 2) * 0.5
136 end
137
138 ; "TODO: use O(logN) lookup instead of O(N)"
139
140 define write PPU register (N) value: (V)
141 if N = 0 then
142 get bit mask (V)
143 set PPU base nametable address to 2 * letter 7 of mask + letter 8 of mask
144 set PPU VRAM increment to letter 6 of mask * 31 + 1
145 set PPU Sprite pattern table to letter 5 of mask * 4096
146 set PPU Background pattern table to letter 4 of mask * 4096
147 set PPU Sprite size to letter 3 of mask
148 set PPU master slave select to letter 2 of mask
149 set PPU generate NMI to letter 1 of mask
150 else
151 if N = 1 then
152 get bit mask (V)
153 set PPU grayscale to letter 8 of mask
154 set PPU show left8 bg to letter 7 of mask
155 set PPU show left8 sprites to letter 6 of mask
156 set PPU show bg to letter 5 of mask
157 set PPU show sprites to letter 4 of mask
158 set PPU emphasize blue to letter 3 of mask
159 set PPU emphasize green to letter 2 of mask
160 set PPU emphasize red to letter 1 of mask
161 else
162 if N = 3 then
163 set PPU OAMADDR to V
164 else
165 if N = 4 then
166 replace item PPU OAMADDR mod 256 + 1 of OAM with V
167 change PPU OAMADDR by 1
168 else
169 if N = 5 then
170 if PPU address latch = 0 then
171 set PPU fine x scroll to V
172 else
173 set PPU fine y scroll to V
174 end
175 else
176 if N = 6 then
177 if PPU address latch = 0 then
178 set PPU high address to V
179 else
180 set PPU low address to V
181 end
182 else
183 if N = 7 then
184 write PPU memory (V)
185 end
186 end
187 end
188 end
189 end
190 end
191 end
192
193 define OAM DMA (pagebase)
194 set temp to 0
195 repeat 256
196 mapper read (256 * pagebase + PPU OAMADDR + temp)
197 change temp by 1
198 replace item temp of OAM with M
199 end
200
201 define read PPU memory
202 if PPU high address < 32 then
203 set M to item (1 + PPU high address * 256 + PPU low address) of Pattern tables
204 else
205 if PPU high address < 48 then
206 set M to item (PPU high address * 256 + PPU low address - 8191) of Nametables
207 else
208 if PPU high address < 63 then
209 set M to item (PPU high address * 256 + PPU low address - 12287) of Nametables
210 else
211 if PPU high address < 64 then
212 set M to item (PPU low address mod 32 + 1) of Palette
213 else
214 ; "TODO: PPU memory mirroring"
215 end
216 end
217 end
218 end
219
220 define write PPU memory (V)
221 if PPU high address < 32 then
222 replace item 1 + PPU high address * 256 + PPU low address of Pattern tables with V
223 else
224 if PPU high address < 48 then
225 replace item PPU high address * 256 + PPU low address - 8191 of Nametables with V
226 else
227 if PPU high address < 63 then
228 replace item PPU high address * 256 + PPU low address - 12287 of Nametables with V
229 else
230 if PPU high address < 64 then
231 replace item PPU low address mod 32 + 1 of Palette with V
232 else
233 ; "TODO: PPU memory mirroring"
234 end
235 end
236 end
237 end
This page took 0.096364 seconds and 4 git commands to generate.