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