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