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