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