87b0845f |
1 | /* |
2 | * TODO: |
3 | * - start in a state as if BIOS ran |
4 | * - remaining status flags (OVR/COL) |
5 | * - RAM support in mapper |
6 | * - region support |
7 | * - Pause button (NMI) |
8 | * - SN76496 DAC-like usage |
9 | * - H counter |
10 | */ |
3e49ffd0 |
11 | #include "pico_int.h" |
af37bca8 |
12 | #include "memory.h" |
2ec9bec5 |
13 | #include "sound/sn76496.h" |
3e49ffd0 |
14 | |
2ec9bec5 |
15 | static unsigned char vdp_data_read(void) |
3e49ffd0 |
16 | { |
2ec9bec5 |
17 | struct PicoVideo *pv = &Pico.video; |
18 | unsigned char d; |
19 | |
20 | d = Pico.vramb[pv->addr]; |
21 | pv->addr = (pv->addr + 1) & 0x3fff; |
22 | pv->pending = 0; |
23 | return d; |
24 | } |
25 | |
26 | static unsigned char vdp_ctl_read(void) |
27 | { |
28 | unsigned char d = Pico.video.pending_ints << 7; |
29 | Pico.video.pending = 0; |
30 | Pico.video.pending_ints = 0; |
31 | |
32 | elprintf(EL_SR, "VDP sr: %02x", d); |
33 | return d; |
34 | } |
35 | |
36 | static void vdp_data_write(unsigned char d) |
37 | { |
38 | struct PicoVideo *pv = &Pico.video; |
39 | |
40 | if (pv->type == 3) { |
41 | Pico.cram[pv->addr & 0x1f] = d; |
200772b7 |
42 | Pico.m.dirtyPal = 1; |
2ec9bec5 |
43 | } else { |
44 | Pico.vramb[pv->addr] = d; |
45 | } |
46 | pv->addr = (pv->addr + 1) & 0x3fff; |
2ec9bec5 |
47 | |
48 | pv->pending = 0; |
49 | } |
50 | |
51 | static void vdp_ctl_write(unsigned char d) |
52 | { |
53 | struct PicoVideo *pv = &Pico.video; |
54 | |
55 | if (pv->pending) { |
56 | if ((d >> 6) == 2) { |
57 | pv->reg[d & 0x0f] = pv->addr; |
200772b7 |
58 | elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff); |
2ec9bec5 |
59 | } |
60 | pv->type = d >> 6; |
61 | pv->addr &= 0x00ff; |
62 | pv->addr |= (d & 0x3f) << 8; |
63 | } else { |
64 | pv->addr &= 0x3f00; |
65 | pv->addr |= d; |
66 | } |
67 | pv->pending ^= 1; |
3e49ffd0 |
68 | } |
69 | |
2ec9bec5 |
70 | static unsigned char z80_sms_in(unsigned short a) |
3e49ffd0 |
71 | { |
2ec9bec5 |
72 | unsigned char d = 0; |
73 | |
200772b7 |
74 | elprintf(EL_IO, "z80 port %04x read", a); |
2ec9bec5 |
75 | a &= 0xc1; |
76 | switch (a) |
77 | { |
78 | case 0x00: |
79 | case 0x01: |
80 | d = 0xff; |
81 | break; |
82 | |
83 | case 0x40: /* V counter */ |
84 | d = Pico.video.v_counter; |
87b0845f |
85 | elprintf(EL_HVCNT, "V counter read: %02x", d); |
2ec9bec5 |
86 | break; |
87 | |
88 | case 0x41: /* H counter */ |
89 | d = Pico.m.rotate++; |
87b0845f |
90 | elprintf(EL_HVCNT, "H counter read: %02x", d); |
2ec9bec5 |
91 | break; |
92 | |
93 | case 0x80: |
94 | d = vdp_data_read(); |
95 | break; |
96 | |
97 | case 0x81: |
98 | d = vdp_ctl_read(); |
99 | break; |
100 | |
101 | case 0xc0: /* I/O port A and B */ |
102 | d = ~((PicoPad[0] & 0x3f) | (PicoPad[1] << 6)); |
103 | break; |
104 | |
105 | case 0xc1: /* I/O port B and miscellaneous */ |
b4db550e |
106 | d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30; |
2ec9bec5 |
107 | d |= ~(PicoPad[1] >> 2) & 0x0f; |
108 | break; |
109 | } |
110 | |
200772b7 |
111 | elprintf(EL_IO, "ret = %02x", d); |
2ec9bec5 |
112 | return d; |
113 | } |
114 | |
115 | static void z80_sms_out(unsigned short a, unsigned char d) |
116 | { |
200772b7 |
117 | elprintf(EL_IO, "z80 port %04x write %02x", a, d); |
2ec9bec5 |
118 | a &= 0xc1; |
119 | switch (a) |
120 | { |
121 | case 0x01: |
b4db550e |
122 | Pico.ms.io_ctl = d; |
2ec9bec5 |
123 | break; |
124 | |
125 | case 0x40: |
126 | case 0x41: |
127 | if (PicoOpt & POPT_EN_PSG) |
128 | SN76496Write(d); |
129 | break; |
130 | |
131 | case 0x80: |
132 | vdp_data_write(d); |
133 | break; |
134 | |
135 | case 0x81: |
136 | vdp_ctl_write(d); |
137 | break; |
138 | } |
139 | } |
140 | |
87b0845f |
141 | static int bank_mask; |
142 | |
2ec9bec5 |
143 | static void write_bank(unsigned short a, unsigned char d) |
144 | { |
460603fa |
145 | elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc()); |
2ec9bec5 |
146 | switch (a & 0x0f) |
147 | { |
148 | case 0x0c: |
87b0845f |
149 | elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d); |
2ec9bec5 |
150 | break; |
151 | case 0x0d: |
152 | if (d != 0) |
153 | elprintf(EL_STATUS|EL_ANOMALY, "bank0 changed to %d!", d); |
154 | break; |
155 | case 0x0e: |
87b0845f |
156 | d &= bank_mask; |
2ec9bec5 |
157 | z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0); |
158 | #ifdef _USE_CZ80 |
b8a1c09a |
159 | Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14)); |
2ec9bec5 |
160 | #endif |
161 | break; |
162 | case 0x0f: |
87b0845f |
163 | d &= bank_mask; |
2ec9bec5 |
164 | z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0); |
165 | #ifdef _USE_CZ80 |
b8a1c09a |
166 | Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14)); |
2ec9bec5 |
167 | #endif |
168 | break; |
169 | } |
b4db550e |
170 | Pico.ms.carthw[a & 0x0f] = d; |
3e49ffd0 |
171 | } |
172 | |
553c3eaa |
173 | static void xwrite(unsigned int a, unsigned char d) |
2ec9bec5 |
174 | { |
200772b7 |
175 | elprintf(EL_IO, "z80 write [%04x] %02x", a, d); |
2ec9bec5 |
176 | if (a >= 0xc000) |
177 | Pico.zram[a & 0x1fff] = d; |
b4db550e |
178 | if (a >= 0xfff8) |
2ec9bec5 |
179 | write_bank(a, d); |
3e49ffd0 |
180 | } |
181 | |
2ec9bec5 |
182 | void PicoResetMS(void) |
3e49ffd0 |
183 | { |
2ec9bec5 |
184 | z80_reset(); |
185 | PsndReset(); // pal must be known here |
3e49ffd0 |
186 | } |
187 | |
188 | void PicoPowerMS(void) |
189 | { |
87b0845f |
190 | int s, tmp; |
191 | |
b8a1c09a |
192 | memset(&Pico.ram,0,(unsigned char *)&Pico.rom - Pico.ram); |
2ec9bec5 |
193 | memset(&Pico.video,0,sizeof(Pico.video)); |
194 | memset(&Pico.m,0,sizeof(Pico.m)); |
195 | Pico.m.pal = 0; |
196 | |
87b0845f |
197 | // calculate a mask for bank writes. |
198 | // ROM loader has aligned the size for us, so this is safe. |
199 | s = 0; tmp = Pico.romsize; |
200 | while ((tmp >>= 1) != 0) |
201 | s++; |
202 | if (Pico.romsize > (1 << s)) |
203 | s++; |
204 | tmp = 1 << s; |
205 | bank_mask = (tmp - 1) >> 14; |
206 | |
b4db550e |
207 | Pico.ms.carthw[0x0e] = 1; |
208 | Pico.ms.carthw[0x0f] = 2; |
209 | |
2ec9bec5 |
210 | PicoReset(); |
3e49ffd0 |
211 | } |
212 | |
213 | void PicoMemSetupMS(void) |
214 | { |
215 | z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0); |
216 | z80_map_set(z80_read_map, 0xc000, 0xdfff, Pico.zram, 0); |
200772b7 |
217 | z80_map_set(z80_read_map, 0xe000, 0xffff, Pico.zram, 0); |
3e49ffd0 |
218 | |
2ec9bec5 |
219 | z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1); |
3e49ffd0 |
220 | z80_map_set(z80_write_map, 0xc000, 0xdfff, Pico.zram, 0); |
221 | z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1); |
222 | |
223 | #ifdef _USE_DRZ80 |
224 | drZ80.z80_in = z80_sms_in; |
225 | drZ80.z80_out = z80_sms_out; |
226 | #endif |
227 | #ifdef _USE_CZ80 |
b8a1c09a |
228 | Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom); |
229 | Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)Pico.zram); |
230 | Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)Pico.zram); |
3e49ffd0 |
231 | Cz80_Set_INPort(&CZ80, z80_sms_in); |
232 | Cz80_Set_OUTPort(&CZ80, z80_sms_out); |
233 | #endif |
234 | } |
235 | |
b4db550e |
236 | void PicoStateLoadedMS(void) |
237 | { |
238 | write_bank(0xfffe, Pico.ms.carthw[0x0e]); |
239 | write_bank(0xffff, Pico.ms.carthw[0x0f]); |
240 | } |
241 | |
3e49ffd0 |
242 | void PicoFrameMS(void) |
243 | { |
2ec9bec5 |
244 | struct PicoVideo *pv = &Pico.video; |
245 | int is_pal = Pico.m.pal; |
246 | int lines = is_pal ? 313 : 262; |
247 | int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */ |
248 | int cycles_done = 0, cycles_aim = 0; |
19954be1 |
249 | int skip = PicoSkipFrame; |
2ec9bec5 |
250 | int lines_vis = 192; |
87b0845f |
251 | int hint; // Hint counter |
2ec9bec5 |
252 | int y; |
253 | |
200772b7 |
254 | PicoFrameStartMode4(); |
87b0845f |
255 | hint = pv->reg[0x0a]; |
200772b7 |
256 | |
2ec9bec5 |
257 | for (y = 0; y < lines; y++) |
258 | { |
259 | pv->v_counter = Pico.m.scanline = y; |
03065bb6 |
260 | if (y > 218) |
261 | pv->v_counter = y - 6; |
2ec9bec5 |
262 | |
19954be1 |
263 | if (y < lines_vis && !skip) |
200772b7 |
264 | PicoLineMode4(y); |
87b0845f |
265 | |
266 | if (y <= lines_vis) |
267 | { |
268 | if (--hint < 0) |
269 | { |
270 | hint = pv->reg[0x0a]; |
271 | pv->pending_ints |= 2; |
272 | if (pv->reg[0] & 0x10) { |
273 | elprintf(EL_INTS, "hint"); |
274 | z80_int(); |
275 | } |
276 | } |
277 | } |
200772b7 |
278 | else if (y == lines_vis + 1) { |
87b0845f |
279 | pv->pending_ints |= 1; |
280 | if (pv->reg[1] & 0x20) { |
2ec9bec5 |
281 | elprintf(EL_INTS, "vint"); |
282 | z80_int(); |
283 | } |
284 | } |
285 | |
286 | cycles_aim += cycles_line; |
287 | cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8; |
288 | } |
289 | |
460603fa |
290 | if (PsndOut) |
291 | PsndGetSamplesMS(); |
3e49ffd0 |
292 | } |
293 | |
87b0845f |
294 | void PicoFrameDrawOnlyMS(void) |
295 | { |
296 | int lines_vis = 192; |
297 | int y; |
298 | |
299 | PicoFrameStartMode4(); |
300 | |
301 | for (y = 0; y < lines_vis; y++) |
302 | PicoLineMode4(y); |
303 | } |
304 | |