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