rearrange globals
[picodrive.git] / pico / sms.c
... / ...
CommitLineData
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 */
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
14 * - SN76496 DAC-like usage
15 * - H counter
16 */
17#include "pico_int.h"
18#include "memory.h"
19#include "sound/sn76496.h"
20
21static unsigned char vdp_data_read(void)
22{
23 struct PicoVideo *pv = &Pico.video;
24 unsigned char d;
25
26 d = PicoMem.vramb[pv->addr];
27 pv->addr = (pv->addr + 1) & 0x3fff;
28 pv->pending = 0;
29 return d;
30}
31
32static unsigned char vdp_ctl_read(void)
33{
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;
40
41 elprintf(EL_SR, "VDP sr: %02x", d);
42 return d;
43}
44
45static void vdp_data_write(unsigned char d)
46{
47 struct PicoVideo *pv = &Pico.video;
48
49 if (pv->type == 3) {
50 PicoMem.cram[pv->addr & 0x1f] = d;
51 Pico.m.dirtyPal = 1;
52 } else {
53 PicoMem.vramb[pv->addr] = d;
54 }
55 pv->addr = (pv->addr + 1) & 0x3fff;
56
57 pv->pending = 0;
58}
59
60static 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;
67 elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
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;
77}
78
79static unsigned char z80_sms_in(unsigned short a)
80{
81 unsigned char d = 0;
82
83 elprintf(EL_IO, "z80 port %04x read", a);
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;
94 elprintf(EL_HVCNT, "V counter read: %02x", d);
95 break;
96
97 case 0x41: /* H counter */
98 d = Pico.m.rotate++;
99 elprintf(EL_HVCNT, "H counter read: %02x", d);
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 */
111 d = ~((PicoIn.pad[0] & 0x3f) | (PicoIn.pad[1] << 6));
112 break;
113
114 case 0xc1: /* I/O port B and miscellaneous */
115 d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
116 d |= ~(PicoIn.pad[1] >> 2) & 0x0f;
117 break;
118 }
119
120 elprintf(EL_IO, "ret = %02x", d);
121 return d;
122}
123
124static void z80_sms_out(unsigned short a, unsigned char d)
125{
126 elprintf(EL_IO, "z80 port %04x write %02x", a, d);
127 a &= 0xc1;
128 switch (a)
129 {
130 case 0x01:
131 Pico.ms.io_ctl = d;
132 break;
133
134 case 0x40:
135 case 0x41:
136 if (PicoIn.opt & POPT_EN_PSG)
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
150static int bank_mask;
151
152static void write_bank(unsigned short a, unsigned char d)
153{
154 elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
155 switch (a & 0x0f)
156 {
157 case 0x0c:
158 elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
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:
165 d &= bank_mask;
166 z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
167#ifdef _USE_CZ80
168 Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
169#endif
170 break;
171 case 0x0f:
172 d &= bank_mask;
173 z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
174#ifdef _USE_CZ80
175 Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
176#endif
177 break;
178 }
179 Pico.ms.carthw[a & 0x0f] = d;
180}
181
182static void xwrite(unsigned int a, unsigned char d)
183{
184 elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
185 if (a >= 0xc000)
186 PicoMem.zram[a & 0x1fff] = d;
187 if (a >= 0xfff8)
188 write_bank(a, d);
189}
190
191void PicoResetMS(void)
192{
193 z80_reset();
194 PsndReset(); // pal must be known here
195}
196
197void PicoPowerMS(void)
198{
199 int s, tmp;
200
201 memset(&PicoMem,0,sizeof(PicoMem));
202 memset(&Pico.video,0,sizeof(Pico.video));
203 memset(&Pico.m,0,sizeof(Pico.m));
204 Pico.m.pal = 0;
205
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
216 Pico.ms.carthw[0x0e] = 1;
217 Pico.ms.carthw[0x0f] = 2;
218
219 PicoReset();
220}
221
222void PicoMemSetupMS(void)
223{
224 z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
225 z80_map_set(z80_read_map, 0xc000, 0xdfff, PicoMem.zram, 0);
226 z80_map_set(z80_read_map, 0xe000, 0xffff, PicoMem.zram, 0);
227
228 z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
229 z80_map_set(z80_write_map, 0xc000, 0xdfff, PicoMem.zram, 0);
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
237 Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
238 Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)PicoMem.zram);
239 Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)PicoMem.zram);
240 Cz80_Set_INPort(&CZ80, z80_sms_in);
241 Cz80_Set_OUTPort(&CZ80, z80_sms_out);
242#endif
243}
244
245void PicoStateLoadedMS(void)
246{
247 write_bank(0xfffe, Pico.ms.carthw[0x0e]);
248 write_bank(0xffff, Pico.ms.carthw[0x0f]);
249}
250
251void PicoFrameMS(void)
252{
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;
258 int skip = PicoIn.skipFrame;
259 int lines_vis = 192;
260 int hint; // Hint counter
261 int nmi;
262 int y;
263
264 PsndStartFrame();
265
266 nmi = (PicoIn.pad[0] >> 7) & 1;
267 if (!Pico.ms.nmi_state && nmi)
268 z80_nmi();
269 Pico.ms.nmi_state = nmi;
270
271 PicoFrameStartMode4();
272 hint = pv->reg[0x0a];
273
274 for (y = 0; y < lines; y++)
275 {
276 pv->v_counter = Pico.m.scanline = y;
277 if (y > 218)
278 pv->v_counter = y - 6;
279
280 if (y < lines_vis && !skip)
281 PicoLineMode4(y);
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 }
295 else if (y == lines_vis + 1) {
296 pv->pending_ints |= 1;
297 if (pv->reg[1] & 0x20) {
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
307 if (PsndOut)
308 PsndGetSamplesMS();
309}
310
311void 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