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