32x and sms savestates. Core-independent z80 state. SS bugfixing/refactoring.
[picodrive.git] / pico / sms.c
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  */
11 #include "pico_int.h"
12 #include "memory.h"
13 #include "sound/sn76496.h"
14
15 static unsigned char vdp_data_read(void)
16 {
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;
42     Pico.m.dirtyPal = 1;
43   } else {
44     Pico.vramb[pv->addr] = d;
45   }
46   pv->addr = (pv->addr + 1) & 0x3fff;
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;
58       elprintf(EL_IO, "  VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
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;
68 }
69
70 static unsigned char z80_sms_in(unsigned short a)
71 {
72   unsigned char d = 0;
73
74   elprintf(EL_IO, "z80 port %04x read", a);
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;
85       elprintf(EL_HVCNT, "V counter read: %02x", d);
86       break;
87
88     case 0x41: /* H counter */
89       d = Pico.m.rotate++;
90       elprintf(EL_HVCNT, "H counter read: %02x", d);
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 */
106       d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
107       d |= ~(PicoPad[1] >> 2) & 0x0f;
108       break;
109   }
110
111   elprintf(EL_IO, "ret = %02x", d);
112   return d;
113 }
114
115 static void z80_sms_out(unsigned short a, unsigned char d)
116 {
117   elprintf(EL_IO, "z80 port %04x write %02x", a, d);
118   a &= 0xc1;
119   switch (a)
120   {
121     case 0x01:
122       Pico.ms.io_ctl = d;
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
141 static int bank_mask;
142
143 static void write_bank(unsigned short a, unsigned char d)
144 {
145   elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
146   switch (a & 0x0f)
147   {
148     case 0x0c:
149       elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
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:
156       d &= bank_mask;
157       z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
158 #ifdef _USE_CZ80
159       Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
160 #endif
161       break;
162     case 0x0f:
163       d &= bank_mask;
164       z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
165 #ifdef _USE_CZ80
166       Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
167 #endif
168       break;
169   }
170   Pico.ms.carthw[a & 0x0f] = d;
171 }
172
173 static void xwrite(unsigned int a, unsigned char d)
174 {
175   elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
176   if (a >= 0xc000)
177     Pico.zram[a & 0x1fff] = d;
178   if (a >= 0xfff8)
179     write_bank(a, d);
180 }
181
182 void PicoResetMS(void)
183 {
184   z80_reset();
185   PsndReset(); // pal must be known here
186 }
187
188 void PicoPowerMS(void)
189 {
190   int s, tmp;
191
192   memset(&Pico.ram,0,(unsigned char *)&Pico.rom - Pico.ram);
193   memset(&Pico.video,0,sizeof(Pico.video));
194   memset(&Pico.m,0,sizeof(Pico.m));
195   Pico.m.pal = 0;
196
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
207   Pico.ms.carthw[0x0e] = 1;
208   Pico.ms.carthw[0x0f] = 2;
209
210   PicoReset();
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);
217   z80_map_set(z80_read_map, 0xe000, 0xffff, Pico.zram, 0);
218
219   z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
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
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);
231   Cz80_Set_INPort(&CZ80, z80_sms_in);
232   Cz80_Set_OUTPort(&CZ80, z80_sms_out);
233 #endif
234 }
235
236 void PicoStateLoadedMS(void)
237 {
238   write_bank(0xfffe, Pico.ms.carthw[0x0e]);
239   write_bank(0xffff, Pico.ms.carthw[0x0f]);
240 }
241
242 void PicoFrameMS(void)
243 {
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;
249   int skip = PicoSkipFrame;
250   int lines_vis = 192;
251   int hint; // Hint counter
252   int y;
253
254   PicoFrameStartMode4();
255   hint = pv->reg[0x0a];
256
257   for (y = 0; y < lines; y++)
258   {
259     pv->v_counter = Pico.m.scanline = y;
260
261     if (y < lines_vis && !skip)
262       PicoLineMode4(y);
263
264     if (y <= lines_vis)
265     {
266       if (--hint < 0)
267       {
268         hint = pv->reg[0x0a];
269         pv->pending_ints |= 2;
270         if (pv->reg[0] & 0x10) {
271           elprintf(EL_INTS, "hint");
272           z80_int();
273         }
274       }
275     }
276     else if (y == lines_vis + 1) {
277       pv->pending_ints |= 1;
278       if (pv->reg[1] & 0x20) {
279         elprintf(EL_INTS, "vint");
280         z80_int();
281       }
282     }
283
284     cycles_aim += cycles_line;
285     cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
286   }
287
288   if (PsndOut)
289     PsndGetSamplesMS();
290 }
291
292 void PicoFrameDrawOnlyMS(void)
293 {
294   int lines_vis = 192;
295   int y;
296
297   PicoFrameStartMode4();
298
299   for (y = 0; y < lines_vis; y++)
300     PicoLineMode4(y);
301 }
302