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