Previous 15994 Revisions Next

r4403 Sunday 8th March, 2009 at 15:20:45 UTC by Wilbert Pol
[GAMEGEAR] Improvements to the sms compatibility mode. (part of bug #1379)
Styling cleanups.
[src/mess/machine]sms.c

src/mess/machine/sms.c
r4402r4403
1313#define CF_KOREAN_MAPPER   0x02
1414#define CF_93C46_EEPROM      0x04
1515#define CF_ONCART_RAM      0x08
16#define CF_GG_SMS_MODE      0x10
1617
1718#define MAX_CARTRIDGES      16
1819
r4402r4403
5758static UINT8 sports_pad_2_x;
5859static UINT8 sports_pad_2_y;
5960
60static struct {
61static struct
62{
6163   UINT8   *ROM;         /* Pointer to ROM image data */
6264   UINT32   size;         /* Size of the ROM image */
6365   UINT8   features;      /* on-cartridge special hardware */
r4402r4403
7173
7274static void setup_rom(const address_space *space);
7375
74static TIMER_CALLBACK( rapid_fire_callback ) {
76
77static TIMER_CALLBACK( rapid_fire_callback )
78{
7579   rapid_fire_state_1 ^= 0xFF;
7680   rapid_fire_state_2 ^= 0xFF;
7781}
7882
79static WRITE8_HANDLER( sms_input_write ) {
80   switch( offset ) {
83
84static WRITE8_HANDLER( sms_input_write )
85{
86   switch( offset )
87   {
8188   case 0:
82      switch( input_port_read(space->machine, "CTRLSEL") & 0x0F ) {
89      switch( input_port_read(space->machine, "CTRLSEL") & 0x0F )
90      {
8391      case 0x03:   /* Sports Pad */
84         if ( data != sports_pad_last_data_1 ) {
92         if ( data != sports_pad_last_data_1 )
93         {
8594            UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu);
8695
8796            sports_pad_last_data_1 = data;
88            if ( cpu_cycles - last_sports_pad_time_1 > 512 ) {
97            if ( cpu_cycles - last_sports_pad_time_1 > 512 )
98            {
8999               sports_pad_state_1 = 3;
90100               sports_pad_1_x = input_port_read(space->machine, "SPORT0");
91101               sports_pad_1_y = input_port_read(space->machine, "SPORT1");
r4402r4403
96106         break;
97107      }
98108      break;
109
99110   case 1:
100      switch( input_port_read(space->machine, "CTRLSEL") >> 4 ) {
111      switch( input_port_read(space->machine, "CTRLSEL") >> 4 )
112      {
101113      case 0x03:   /* Sports Pad */
102         if ( data != sports_pad_last_data_2 ) {
114         if ( data != sports_pad_last_data_2 )
115         {
103116            UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu);
104117
105118            sports_pad_last_data_2 = data;
106            if ( cpu_cycles - last_sports_pad_time_2 > 2048 ) {
119            if ( cpu_cycles - last_sports_pad_time_2 > 2048 )
120            {
107121               sports_pad_state_2 = 3;
108122               sports_pad_2_x = input_port_read(space->machine, "SPORT2");
109123               sports_pad_2_y = input_port_read(space->machine, "SPORT3");
r4402r4403
117131   }
118132}
119133
120static void sms_get_inputs(const address_space *space) {
134static void sms_get_inputs(const address_space *space)
135{
121136   UINT8 data = 0x00;
122137   UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu);
123138   running_machine *machine = space->machine;
r4402r4403
131146   }
132147
133148   /* Player 1 */
134   switch( input_port_read(machine, "CTRLSEL") & 0x0F ) {
149   switch( input_port_read(machine, "CTRLSEL") & 0x0F )
150   {
135151   case 0x00:  /* Joystick */
136152      data = input_port_read(machine, "PORT_DC");
137153      /* Rapid Fire setting for Button A */
138      if ( input_port_read(machine, "RFU") & 0x01 ) {
154      if ( input_port_read(machine, "RFU") & 0x01 )
155      {
139156         data = ( data & 0xEF ) | ( rapid_fire_state_1 & 0x10 );
140157      }
141158      /* Check Rapid Fire setting for Button B */
142      if ( input_port_read(machine, "RFU") & 0x02 ) {
159      if ( input_port_read(machine, "RFU") & 0x02 )
160      {
143161         data = ( data & 0xDF ) | ( rapid_fire_state_1 & 0x20 );
144162      }
145163      sms_input_port0 = ( sms_input_port0 & 0xC0 ) | ( data & 0x3F );
146164      break;
165
147166   case 0x01:  /* Light Phaser */
148167      break;
168
149169   case 0x02:  /* Paddle Control */
150170      /* Get button A state */
151171      data = input_port_read(machine, "PADDLE0");
152      if ( paddle_read_state ) {
172      if ( paddle_read_state )
173      {
153174         data = data >> 4;
154175      }
155176      sms_input_port0 = ( sms_input_port0 & 0xC0 ) | ( data & 0x0F ) | ( paddle_read_state & 0x20 )
156177                      | ( ( input_port_read(machine, "CTRLIPT") & 0x02 ) << 3 );
157178      break;
179
158180   case 0x03:   /* Sega Sports Pad */
159      switch( sports_pad_state_1 ) {
181      switch( sports_pad_state_1 )
182      {
160183      case 0:
161184         data = ( sports_pad_1_x >> 4 ) & 0x0F;
162185         break;
r4402r4403
175198   }
176199
177200   /* Player 2 */
178   switch( input_port_read(machine, "CTRLSEL") >> 4 ) {
201   switch( input_port_read(machine, "CTRLSEL") >> 4 )
202   {
179203   case 0x00:   /* Joystick */
180204      data = input_port_read(machine, "PORT_DC");
181205      sms_input_port0 = ( sms_input_port0 & 0x3F ) | ( data & 0xC0 );
182206      data = input_port_read(machine, "PORT_DD");
183      if ( input_port_read(machine, "RFU") & 0x04 ) {
207      if ( input_port_read(machine, "RFU") & 0x04 )
208      {
184209         data = ( data & 0xFB ) | ( rapid_fire_state_2 & 0x04 );
185210      }
186      if ( input_port_read(machine, "RFU") & 0x08 ) {
211      if ( input_port_read(machine, "RFU") & 0x08 )
212      {
187213         data = ( data & 0xF7 ) | ( rapid_fire_state_2 & 0x08 );
188214      }
189215      sms_input_port1 = ( sms_input_port1 & 0xF0 ) | ( data & 0x0F );
190216      break;
217
191218   case 0x01:   /* Light Phaser */
192219      break;
220
193221   case 0x02:   /* Paddle Control */
194222      /* Get button A state */
195223      data = input_port_read(machine, "PADDLE1");
196      if ( paddle_read_state ) {
224      if ( paddle_read_state )
225      {
197226         data = data >> 4;
198227      }
199228      sms_input_port0 = ( sms_input_port0 & 0x3F ) | ( ( data & 0x03 ) << 6 );
200229      sms_input_port1 = ( sms_input_port1 & 0xF0 ) | ( ( data & 0x0C ) >> 2 ) | ( paddle_read_state & 0x08 )
201230                      | ( ( input_port_read(machine, "CTRLIPT") & 0x20 ) >> 3 );
202231      break;
232
203233   case 0x03:   /* Sega Sports Pad */
204      switch( sports_pad_state_2 ) {
234      switch( sports_pad_state_2 )
235      {
205236      case 0:
206237         data = sports_pad_2_x & 0x0F;
207238         break;
r4402r4403
220251   }
221252}
222253
223WRITE8_HANDLER(sms_fm_detect_w) {
224   if ( HAS_FM ) {
254
255WRITE8_HANDLER(sms_fm_detect_w)
256{
257   if ( HAS_FM )
258   {
225259      smsFMDetect = (data & 0x01);
226260   }
227261}
228262
229READ8_HANDLER(sms_fm_detect_r) {
230   if ( HAS_FM ) {
263
264READ8_HANDLER(sms_fm_detect_r)
265{
266   if ( HAS_FM )
267   {
231268      return smsFMDetect;
232   } else {
233      if ( biosPort & IO_CHIP ) {
269   }
270   else
271   {
272      if ( biosPort & IO_CHIP )
273      {
234274         return 0xFF;
235      } else {
275      }
276      else
277      {
236278         sms_get_inputs(space);
237279         return sms_input_port0;
238280      }
239281   }
240282}
241283
242WRITE8_HANDLER(sms_version_w) {
243   if ((data & 0x01) && (data & 0x04)) {
284
285WRITE8_HANDLER(sms_version_w)
286{
287   if ((data & 0x01) && (data & 0x04))
288   {
244289      smsVersion = (data & 0xA0);
245290   }
246   if ( data & 0x08 ) {
291   if ( data & 0x08 )
292   {
247293      sms_input_write( space, 0, ( data & 0x20 ) >> 5 );
248294   }
249   if ( data & 0x02 ) {
295   if ( data & 0x02 )
296   {
250297      sms_input_write( space, 1, ( data & 0x80 ) >> 7 );
251298   }
252299}
253300
254READ8_HANDLER(sms_version_r) {
301
302READ8_HANDLER(sms_version_r)
303{
255304   UINT8 temp;
256305
257   if (biosPort & IO_CHIP) {
306   if (biosPort & IO_CHIP)
307   {
258308      return (0xFF);
259309   }
260310
r4402r4403
262312   temp = (smsVersion & 0x80) | (smsVersion & 0x20) << 1;
263313
264314   /* Inverse version detect value for Japanese machines */
265   if ( IS_REGION_JAPAN ) {
315   if ( IS_REGION_JAPAN )
316   {
266317      temp ^= 0xC0;
267318   }
268319
r4402r4403
273324   return (temp);
274325}
275326
276READ8_HANDLER(sms_count_r) {
277   if ( offset & 0x01 ) {
327
328READ8_HANDLER(sms_count_r)
329{
330   if ( offset & 0x01 )
331   {
278332      return sms_vdp_hcount_r(space, offset);
279   } else {
333   }
334   else
335   {
280336      /* VCount read */
281337      return sms_vdp_vcount_r(space, offset);
282338   }
283339}
284340
285void sms_check_pause_button(const address_space *space) {
286   if ( ! IS_GAMEGEAR ) {
287      if ( ! (input_port_read(space->machine, "PAUSE") & 0x80) ) {
288         if ( ! smsPaused ) {
289            cpu_set_input_line(space->machine->cpu[0], INPUT_LINE_NMI, ASSERT_LINE );
290            cpu_set_input_line(space->machine->cpu[0], INPUT_LINE_NMI, CLEAR_LINE );
291         }
292         smsPaused = 1;
293      } else {
294         smsPaused = 0;
341
342/*
343  Check if the pause button is pressed.
344  If the gamegear is in sms mode, check if the start button is pressed.
345 */
346void sms_check_pause_button(const address_space *space)
347{
348   if ( IS_GAMEGEAR && ! ( sms_cartridge[sms_current_cartridge].features & CF_GG_SMS_MODE ) )
349   {
350      return;
351   }
352   if ( ! (input_port_read(space->machine, IS_GAMEGEAR ? "START" : "PAUSE") & 0x80) ) {
353      if ( ! smsPaused ) {
354         cpu_set_input_line(space->machine->cpu[0], INPUT_LINE_NMI, PULSE_LINE );
295355      }
356      smsPaused = 1;
296357   }
358   else
359   {
360      smsPaused = 0;
361   }
297362}
298363
299READ8_HANDLER(sms_input_port_0_r) {
300   if (biosPort & IO_CHIP) {
364
365READ8_HANDLER(sms_input_port_0_r)
366{
367   if (biosPort & IO_CHIP)
368   {
301369      return (0xFF);
302   } else {
370   }
371   else
372   {
303373      sms_get_inputs(space);
304374      return sms_input_port0;
305375   }
306376}
307377
378
308379WRITE8_HANDLER(sms_ym2413_register_port_0_w)
309380{
310381   if ( HAS_FM )
r4402r4403
314385   }
315386}
316387
388
317389WRITE8_HANDLER(sms_ym2413_data_port_0_w)
318390{
319391   if ( HAS_FM )
r4402r4403
324396   }
325397}
326398
327 READ8_HANDLER(gg_input_port_2_r) {
399
400 READ8_HANDLER(gg_input_port_2_r)
401{
328402   //logerror("joy 2 read, val: %02x, pc: %04x\n", (( IS_REGION_JAPAN ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
329403   return (( IS_REGION_JAPAN ? 0x00 : 0x40 ) | (input_port_read(space->machine, "START") & 0x80));
330404}
331405
406
332407 READ8_HANDLER(sms_mapper_r)
333408{
334409   return sms_mapper[offset];
335410}
336411
412
337413WRITE8_HANDLER(sms_mapper_w)
338414{
339415   int page;
r4402r4403
347423   sms_mapper[offset] = data;
348424   sms_mapper_ram[offset] = data;
349425
350   if ( sms_cartridge[sms_current_cartridge].ROM ) {
426   if ( sms_cartridge[sms_current_cartridge].ROM )
427   {
351428      SOURCE_CART = sms_cartridge[sms_current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000;
352   } else {
429   }
430   else
431   {
353432      SOURCE_CART = sms_banking_none[1];
354433   }
355   if ( BIOS ) {
434   if ( BIOS )
435   {
356436      SOURCE_BIOS = BIOS + ( (smsBiosPageCount > 0) ? data % smsBiosPageCount : 0 ) * 0x4000;
357   } else {
437   }
438   else
439   {
358440      SOURCE_BIOS = sms_banking_none[1];
359441   }
360442
r4402r4403
381463      SOURCE = SOURCE_BIOS;
382464   }
383465
384   switch(offset) {
466   switch(offset)
467   {
385468      case 0: /* Control */
386469         /* Is it ram or rom? */
387         if (data & 0x08) { /* it's ram */
470         if (data & 0x08) /* it's ram */
471         {
388472            sms_cartridge[sms_current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
389            if (data & 0x04) {
473            if (data & 0x04)
474            {
390475               LOG(("ram 1 paged.\n"));
391476               SOURCE = sms_cartridge[sms_current_cartridge].cartSRAM + 0x4000;
392            } else {
477            }
478            else
479            {
393480               LOG(("ram 0 paged.\n"));
394481               SOURCE = sms_cartridge[sms_current_cartridge].cartSRAM;
395482            }
396483            memory_set_bankptr(space->machine,  4, SOURCE );
397484            memory_set_bankptr(space->machine,  5, SOURCE + 0x2000 );
398         } else { /* it's rom */
399            if ( biosPort & IO_BIOS_ROM || ! HAS_BIOS ) {
485         }
486         else /* it's rom */
487         {
488            if ( biosPort & IO_BIOS_ROM || ! HAS_BIOS )
489            {
400490               page = ( rom_page_count > 0) ? sms_mapper[3] % rom_page_count : 0;
401491               SOURCE = sms_banking_cart[4];
402            } else {
492            }
493            else
494            {
403495               page = (smsBiosPageCount > 0) ? sms_mapper[3] % smsBiosPageCount : 0;
404496               SOURCE = sms_banking_bios[4];
405497            }
r4402r4403
408500            memory_set_bankptr(space->machine,  5, SOURCE + 0x2000 );
409501         }
410502         break;
503
411504      case 1: /* Select 16k ROM bank for 0400-3FFF */
412505         LOG(("rom 0 paged in %x.\n", page));
413506         sms_banking_bios[2] = SOURCE_BIOS + 0x0400;
414507         sms_banking_cart[2] = SOURCE_CART + 0x0400;
415         if ( IS_GAMEGEAR ) {
508         if ( IS_GAMEGEAR )
509         {
416510            SOURCE = SOURCE_CART;
417511         }
418512         memory_set_bankptr(space->machine,  2, SOURCE + 0x0400 );
419513         break;
514
420515      case 2: /* Select 16k ROM bank for 4000-7FFF */
421516         LOG(("rom 1 paged in %x.\n", page));
422517         sms_banking_bios[3] = SOURCE_BIOS;
423518         sms_banking_cart[3] = SOURCE_CART;
424         if ( IS_GAMEGEAR ) {
519         if ( IS_GAMEGEAR )
520         {
425521            SOURCE = SOURCE_CART;
426522         }
427523         memory_set_bankptr(space->machine,  3, SOURCE );
428524         break;
525
429526      case 3: /* Select 16k ROM bank for 8000-BFFF */
430527         sms_banking_bios[4] = SOURCE_BIOS;
431         if ( IS_GAMEGEAR ) {
528         if ( IS_GAMEGEAR )
529         {
432530            SOURCE = SOURCE_CART;
433531         }
434         if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) {
435            if ( SOURCE == SOURCE_CART ) {
532         if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER )
533         {
534            if ( SOURCE == SOURCE_CART )
535            {
436536               SOURCE = sms_banking_cart[4];
437537            }
438         } else {
538         }
539         else
540         {
439541            sms_banking_cart[4] = SOURCE_CART;
440542         }
441         if ( ! ( sms_mapper[0] & 0x08 ) ) { /* is RAM disabled? */
543         if ( ! ( sms_mapper[0] & 0x08 ) ) /* is RAM disabled? */
544         {
442545            LOG(("rom 2 paged in %x.\n", page));
443546            memory_set_bankptr(space->machine,  4, SOURCE );
444547            memory_set_bankptr(space->machine,  5, SOURCE + 0x2000 );
r4402r4403
447550   }
448551}
449552
450static WRITE8_HANDLER(sms_codemasters_page0_w) {
451   if ( sms_cartridge[sms_current_cartridge].ROM && sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) {
553
554static WRITE8_HANDLER(sms_codemasters_page0_w)
555{
556   if ( sms_cartridge[sms_current_cartridge].ROM && sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER )
557   {
452558      UINT8 rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000;
453559      sms_banking_cart[1] = sms_cartridge[sms_current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000;
454560      sms_banking_cart[2] = sms_banking_cart[1] + 0x0400;
r4402r4403
457563   }
458564}
459565
460static WRITE8_HANDLER(sms_codemasters_page1_w) {
461   if ( sms_cartridge[sms_current_cartridge].ROM && sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) {
566
567static WRITE8_HANDLER(sms_codemasters_page1_w)
568{
569   if ( sms_cartridge[sms_current_cartridge].ROM && sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER )
570   {
462571      /* Check if we need to switch in some RAM */
463      if ( data & 0x80 ) {
572      if ( data & 0x80 )
573      {
464574         sms_cartridge[sms_current_cartridge].ram_page = data & 0x07;
465575         memory_set_bankptr(space->machine,  5, sms_cartridge[sms_current_cartridge].cartRAM + sms_cartridge[sms_current_cartridge].ram_page * 0x2000 );
466      } else {
576      }
577      else
578      {
467579         UINT8 rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000;
468580         sms_banking_cart[3] = sms_cartridge[sms_current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000;
469581         memory_set_bankptr(space->machine,  3, sms_banking_cart[3] );
r4402r4403
472584   }
473585}
474586
475WRITE8_HANDLER(sms_bios_w) {
587
588WRITE8_HANDLER(sms_bios_w)
589{
476590   biosPort = data;
477591
478592   logerror("bios write %02x, pc: %04x\n", data, cpu_get_pc(space->cpu));
r4402r4403
480594   setup_rom(space);
481595}
482596
483WRITE8_HANDLER(sms_cartram2_w) {
484   if (sms_mapper[0] & 0x08) {
597
598WRITE8_HANDLER(sms_cartram2_w)
599{
600   if (sms_mapper[0] & 0x08)
601   {
485602      logerror("write %02X to cartram at offset #%04X\n", data, offset + 0x2000);
486      if (sms_mapper[0] & 0x04) {
603      if (sms_mapper[0] & 0x04)
604      {
487605         sms_cartridge[sms_current_cartridge].cartSRAM[offset + 0x6000] = data;
488      } else {
606      }
607      else
608      {
489609         sms_cartridge[sms_current_cartridge].cartSRAM[offset + 0x2000] = data;
490610      }
491611   }
492   if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) {
612   if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER )
613   {
493614      sms_cartridge[sms_current_cartridge].cartRAM[sms_cartridge[sms_current_cartridge].ram_page * 0x2000 + offset] = data;
494615   }
495   if ( sms_cartridge[sms_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0 ) { /* Dodgeball King mapper */
616   if ( sms_cartridge[sms_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0 ) /* Dodgeball King mapper */
617   {
496618      UINT8   rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000;
497619      int      page = (rom_page_count > 0) ? data % rom_page_count : 0;
498620      if ( ! sms_cartridge[sms_current_cartridge].ROM )
r4402r4403
504626   }
505627}
506628
507WRITE8_HANDLER(sms_cartram_w) {
629
630WRITE8_HANDLER(sms_cartram_w)
631{
508632   int page;
509633
510   if (sms_mapper[0] & 0x08) {
634   if (sms_mapper[0] & 0x08)
635   {
511636      logerror("write %02X to cartram at offset #%04X\n", data, offset);
512      if (sms_mapper[0] & 0x04) {
637      if (sms_mapper[0] & 0x04)
638      {
513639         sms_cartridge[sms_current_cartridge].cartSRAM[offset + 0x4000] = data;
514      } else {
640      }
641      else
642      {
515643         sms_cartridge[sms_current_cartridge].cartSRAM[offset] = data;
516644      }
517   } else {
518      if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0 ) { /* Codemasters mapper */
645   }
646   else
647   {
648      if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0 ) /* Codemasters mapper */
649      {
519650         UINT8   rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000;
520651         page = (rom_page_count > 0) ? data % rom_page_count : 0;
521652         if ( ! sms_cartridge[sms_current_cartridge].ROM )
r4402r4403
524655         memory_set_bankptr(space->machine,  4, sms_banking_cart[4] );
525656         memory_set_bankptr(space->machine,  5, sms_banking_cart[4] + 0x2000 );
526657         LOG(("rom 2 paged in %x codemasters.\n", page));
527      } else if ( sms_cartridge[sms_current_cartridge].features & CF_ONCART_RAM ) {
658      }
659      else if ( sms_cartridge[sms_current_cartridge].features & CF_ONCART_RAM )
660      {
528661         sms_cartridge[sms_current_cartridge].cartRAM[offset & ( sms_cartridge[sms_current_cartridge].ram_size - 1 ) ] = data;
529      } else {
662      }
663      else
664      {
530665         logerror("INVALID write %02X to cartram at offset #%04X\n", data, offset);
531666      }
532667   }
533668}
534669
535WRITE8_HANDLER(gg_sio_w) {
670
671WRITE8_HANDLER(gg_sio_w)
672{
536673   logerror("*** write %02X to SIO register #%d\n", data, offset);
537674
538675   ggSIO[offset & 0x07] = data;
539   switch(offset & 7) {
676   switch(offset & 7)
677   {
540678      case 0x00: /* Parallel Data */
541679         break;
680
542681      case 0x01: /* Data Direction/ NMI Enable */
543682         break;
683
544684      case 0x02: /* Serial Output */
545685         break;
686
546687      case 0x03: /* Serial Input */
547688         break;
689
548690      case 0x04: /* Serial Control / Status */
549691         break;
550692   }
551693}
552694
553 READ8_HANDLER(gg_sio_r) {
695
696 READ8_HANDLER(gg_sio_r)
697{
554698   logerror("*** read SIO register #%d\n", offset);
555699
556   switch(offset & 7) {
700   switch(offset & 7)
701   {
557702      case 0x00: /* Parallel Data */
558703         break;
704
559705      case 0x01: /* Data Direction/ NMI Enable */
560706         break;
707
561708      case 0x02: /* Serial Output */
562709         break;
710
563711      case 0x03: /* Serial Input */
564712         break;
713
565714      case 0x04: /* Serial Control / Status */
566715         break;
567716   }
r4402r4403
569718   return ggSIO[offset];
570719}
571720
572 READ8_HANDLER(gg_psg_r) {
721
722 READ8_HANDLER(gg_psg_r)
723{
573724   return 0xFF;
574725}
575726
576WRITE8_HANDLER(gg_psg_w) {
727
728WRITE8_HANDLER(gg_psg_w)
729{
577730   logerror("write %02X to psg at offset #%d.\n",data , offset);
578731
579732   /* D7 = Noise Left */
r4402r4403
587740   /* D0 = Tone1 Right */
588741}
589742
590static void sms_machine_stop(running_machine *machine) {
743
744static void sms_machine_stop(running_machine *machine)
745{
591746   /* Does the cartridge have SRAM that should be saved? */
592747   if ( sms_cartridge[sms_current_cartridge].sram_save )
593748      image_battery_save(devtag_get_device(machine, CARTSLOT, "cart1"), sms_cartridge[sms_current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
594749}
595750
751
596752static void setup_rom(const address_space *space)
597753{
598754   running_machine *machine = space->machine;
r4402r4403
605761   memory_set_bankptr(machine,  5, sms_banking_none[4] + 0x2000 );
606762
607763   /* 2. check and set up expansion port */
608   if (!(biosPort & IO_EXPANSION) && (biosPort & IO_CARTRIDGE) && (biosPort & IO_CARD)) {
764   if (!(biosPort & IO_EXPANSION) && (biosPort & IO_CARTRIDGE) && (biosPort & IO_CARD))
765   {
609766      /* TODO: Implement me */
610767      logerror( "Switching to unsupported expansion port.\n" );
611768   }
612769
613770   /* 3. check and set up card rom */
614   if (!(biosPort & IO_CARD) && (biosPort & IO_CARTRIDGE) && (biosPort & IO_EXPANSION)) {
771   if (!(biosPort & IO_CARD) && (biosPort & IO_CARTRIDGE) && (biosPort & IO_EXPANSION))
772   {
615773      /* TODO: Implement me */
616774      logerror( "Switching to unsupported card rom port.\n" );
617775   }
r4402r4403
619777   /* 4. check and set up cartridge rom */
620778   /* if ( ( !(biosPort & IO_CARTRIDGE) && (biosPort & IO_EXPANSION) && (biosPort & IO_CARD) ) || IS_GAMEGEAR ) { */
621779   /* Out Run Europa initially writes a value to port 3E where IO_CARTRIDGE, IO_EXPANSION and IO_CARD are reset */
622   if ( ( ! ( biosPort & IO_CARTRIDGE ) ) || IS_GAMEGEAR ) {
780   if ( ( ! ( biosPort & IO_CARTRIDGE ) ) || IS_GAMEGEAR )
781   {
623782      memory_set_bankptr(machine,  1, sms_banking_cart[1] );
624783      memory_set_bankptr(machine,  2, sms_banking_cart[2] );
625784      memory_set_bankptr(machine,  3, sms_banking_cart[3] );
r4402r4403
629788   }
630789
631790   /* 5. check and set up bios rom */
632   if ( !(biosPort & IO_BIOS_ROM) ) {
791   if ( !(biosPort & IO_BIOS_ROM) )
792   {
633793      /* 0x0400 bioses */
634      if ( HAS_BIOS_0400 ) {
794      if ( HAS_BIOS_0400 )
795      {
635796         memory_set_bankptr(machine,  1, sms_banking_bios[1] );
636797         logerror( "Switched in 0x0400 bios.\n" );
637798      }
638799      /* 0x2000 bioses */
639      if ( HAS_BIOS_2000 ) {
800      if ( HAS_BIOS_2000 )
801      {
640802         memory_set_bankptr(machine,  1, sms_banking_bios[1] );
641803         memory_set_bankptr(machine,  2, sms_banking_bios[2] );
642804         logerror( "Switched in 0x2000 bios.\n" );
643805      }
644      if ( HAS_BIOS_FULL ) {
806      if ( HAS_BIOS_FULL )
807      {
645808         memory_set_bankptr(machine,  1, sms_banking_bios[1] );
646809         memory_set_bankptr(machine,  2, sms_banking_bios[2] );
647810         memory_set_bankptr(machine,  3, sms_banking_bios[3] );
r4402r4403
651814      }
652815   }
653816
654   if ( sms_cartridge[sms_current_cartridge].features & CF_ONCART_RAM ) {
817   if ( sms_cartridge[sms_current_cartridge].features & CF_ONCART_RAM )
818   {
655819      memory_set_bankptr(machine,  4, sms_cartridge[sms_current_cartridge].cartRAM );
656820      memory_set_bankptr(machine,  5, sms_cartridge[sms_current_cartridge].cartRAM );
657821   }
658822}
659823
660static int sms_verify_cart(UINT8 *magic, int size) {
824
825static int sms_verify_cart(UINT8 *magic, int size)
826{
661827   int retval;
662828
663829   retval = IMAGE_VERIFY_FAIL;
664830
665831   /* Verify the file is a valid image - check $7ff0 for "TMR SEGA" */
666   if (size >= 0x8000) {
667      if (!strncmp((char*)&magic[0x7FF0], "TMR SEGA", 8)) {
832   if (size >= 0x8000)
833   {
834      if (!strncmp((char*)&magic[0x7FF0], "TMR SEGA", 8))
835      {
668836         /* Technically, it should be this, but remove for now until verified:
669         if (!strcmp(sysname, "gamegear")) {
837         if (!strcmp(sysname, "gamegear"))
838         {
670839            if ((unsigned char)magic[0x7FFD] < 0x50)
671840               retval = IMAGE_VERIFY_PASS;
672841         }
673         if (!strcmp(sysname, "sms")) {
842         if (!strcmp(sysname, "sms"))
843         {
674844            if ((unsigned char)magic[0x7FFD] >= 0x50)
675845               retval = IMAGE_VERIFY_PASS;
676846         }
r4402r4403
715885
716886The Korean game Jang Pung II also seems to use a codemasters style mapper.
717887 */
718static int detect_codemasters_mapper( UINT8 *rom ) {
888static int detect_codemasters_mapper( UINT8 *rom )
889{
719890   static const UINT8 jang_pung2[16] = { 0x00, 0xBA, 0x38, 0x0D, 0x00, 0xB8, 0x38, 0x0C, 0x00, 0xB6, 0x38, 0x0B, 0x00, 0xB4, 0x38, 0x0A };
720891
721892   if ( ( ( rom[0x7fe0] & 0x0F ) <= 9 ) &&
722893        ( rom[0x7fe3] == 0x93 || rom[0x7fe3] == 0x94 || rom[0x7fe3] == 0x95 ) &&
723        rom[0x7fef] == 0x00 ) {
894        rom[0x7fef] == 0x00 )
895   {
724896      return 1;
725897   }
726898
727   if ( ! memcmp( &rom[0x7ff0], jang_pung2, 16 ) ) {
899   if ( ! memcmp( &rom[0x7ff0], jang_pung2, 16 ) )
900   {
728901      return 1;
729902   }
730903
731904   return 0;
732905}
733906
734static int detect_korean_mapper( UINT8 *rom ) {
735   static const UINT8 signatures[2][16] = {
907
908static int detect_korean_mapper( UINT8 *rom )
909{
910   static const UINT8 signatures[2][16] =
911   {
736912      { 0x3E, 0x11, 0x32, 0x00, 0xA0, 0x78, 0xCD, 0x84, 0x85, 0x3E, 0x02, 0x32, 0x00, 0xA0, 0xC9, 0xFF }, /* Dodgeball King */
737913      { 0x41, 0x48, 0x37, 0x37, 0x44, 0x37, 0x4E, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20 },   /* Sangokushi 3 */
738914   };
739915   int i;
740916
741   for( i = 0; i < 2; i++ ) {
742      if ( !memcmp( &rom[0x7ff0], signatures[i], 16 ) ) {
917   for( i = 0; i < 2; i++ )
918   {
919      if ( !memcmp( &rom[0x7ff0], signatures[i], 16 ) )
920      {
743921         return 1;
744922      }
745923   }
746924   return 0;
747925}
748926
749DEVICE_START( sms_cart ) {
927
928DEVICE_START( sms_cart )
929{
750930   running_machine *machine = device->machine;
751931   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
752932   int i;
753933
754   for ( i = 0; i < MAX_CARTRIDGES; i++ ) {
934   for ( i = 0; i < MAX_CARTRIDGES; i++ )
935   {
755936      sms_cartridge[i].ROM = NULL;
756937      sms_cartridge[i].size = 0;
757938      sms_cartridge[i].features = 0;
r4402r4403
764945   sms_current_cartridge = 0;
765946
766947   biosPort = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
767   if ( ! IS_GAMEGEAR && ! HAS_BIOS ) {
948   if ( ! IS_GAMEGEAR && ! HAS_BIOS )
949   {
768950      biosPort &= ~(IO_CARTRIDGE);
769951      biosPort |= IO_BIOS_ROM;
770952   }
771
772   /* Initially set the vdp to not use sms compatibility mode for GG */
773   sms_set_ggsmsmode( 0 );
774953}
775954
955
776956DEVICE_IMAGE_LOAD( sms_cart )
777957{
778958   running_machine *machine = image->machine;
r4402r4403
783963   const char *extrainfo = image_extrainfo( image );
784964   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
785965
786   if (strcmp(image->tag,"cart1")==0) {
966   if (strcmp(image->tag,"cart1")==0)
967   {
787968      index = 0;
788969   }
789   if (strcmp(image->tag,"cart2")==0) {
970   if (strcmp(image->tag,"cart2")==0)
971   {
790972      index = 1;
791973   }
792   if (strcmp(image->tag,"cart3")==0) {
974   if (strcmp(image->tag,"cart3")==0)
975   {
793976      index = 2;
794977   }
795   if (strcmp(image->tag,"cart4")==0) {
978   if (strcmp(image->tag,"cart4")==0)
979   {
796980      index = 3;
797981   }   
798   if (strcmp(image->tag,"cart5")==0) {
982   if (strcmp(image->tag,"cart5")==0)
983   {
799984      index = 4;
800985   }   
801986   /* Check for 512-byte header */
r4402r4403
805990      size -= 512;
806991   }
807992
808   if ( ! size ) {
993   if ( ! size )
994   {
809995      image_seterror( image, IMAGE_ERROR_UNSPECIFIED, "Invalid ROM image: ROM image is too small" );
810996      return INIT_FAIL;
811997   }
r4402r4403
8211007   size = image_fread(image, sms_cartridge[index].ROM, size);
8221008
8231009   /* check the image */
824   if ( ! HAS_BIOS ) {
825      if (sms_verify_cart(sms_cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL) {
1010   if ( ! HAS_BIOS )
1011   {
1012      if (sms_verify_cart(sms_cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL)
1013      {
8261014         logerror("Warning loading image: sms_verify_cart failed\n");
8271015      }
8281016   }
r4402r4403
8301018   sms_cartridge[index].features = 0;
8311019
8321020   /* Detect special features from the extrainfo field */
833   if ( extrainfo ) {
1021   if ( extrainfo )
1022   {
8341023      /* Check for codemasters mapper */
835      if ( strstr( extrainfo, "CODEMASTERS" ) ) {
1024      if ( strstr( extrainfo, "CODEMASTERS" ) )
1025      {
8361026         sms_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
8371027      }
8381028
8391029      /* Check for korean mapper */
840      if ( strstr( extrainfo, "KOREAN" ) ) {
1030      if ( strstr( extrainfo, "KOREAN" ) )
1031      {
8411032         sms_cartridge[index].features |= CF_KOREAN_MAPPER;
8421033      }
8431034
8441035      /* Check for special SMS Compatibility mode gamegear cartridges */
845      if ( IS_GAMEGEAR && strstr( extrainfo, "GGSMS" ) ) {
846         sms_set_ggsmsmode( 1 );
1036      if ( IS_GAMEGEAR && strstr( extrainfo, "GGSMS" ) )
1037      {
1038         sms_cartridge[index].features |= CF_GG_SMS_MODE;
8471039      }
8481040
8491041      /* Check for 93C46 eeprom */
850      if ( strstr( extrainfo, "93C46" ) ) {
1042      if ( strstr( extrainfo, "93C46" ) )
1043      {
8511044         sms_cartridge[index].features |= CF_93C46_EEPROM;
8521045      }
8531046
8541047      /* Check for 8KB on-cart RAM */
855      if ( strstr( extrainfo, "8KB_CART_RAM" ) ) {
1048      if ( strstr( extrainfo, "8KB_CART_RAM" ) )
1049       {
8561050         sms_cartridge[index].features |= CF_ONCART_RAM;
8571051         sms_cartridge[index].ram_size = 0x2000;
8581052         sms_cartridge[index].cartRAM = auto_malloc( sms_cartridge[index].ram_size );
8591053      }
860   } else {
1054   }
1055   else
1056   {
8611057      /* If no extrainfo information is available try to find special information out on our own */
8621058      /* Check for special cartridge features */
863      if ( size >= 0x8000 ) {
1059      if ( size >= 0x8000 )
1060      {
8641061         /* Check for special mappers */
865         if ( detect_codemasters_mapper( sms_cartridge[index].ROM ) ) {
1062         if ( detect_codemasters_mapper( sms_cartridge[index].ROM ) )
1063         {
8661064            sms_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
8671065         }
868         if ( detect_korean_mapper( sms_cartridge[index].ROM ) ) {
1066         if ( detect_korean_mapper( sms_cartridge[index].ROM ) )
1067         {
8691068            sms_cartridge[index].features |= CF_KOREAN_MAPPER;
8701069         }
8711070      }
8721071
8731072      /* Check for special SMS Compatibility mode gamegear cartridges */
874      if ( IS_GAMEGEAR ) {
1073      if ( IS_GAMEGEAR )
1074      {
8751075         /* Just in case someone passes us an sms file */
876         if ( fname_len > 3 && ! mame_stricmp( fname + fname_len - 3, "sms" ) ) {
877            sms_set_ggsmsmode( 1 );
1076         if ( fname_len > 3 && ! mame_stricmp( fname + fname_len - 3, "sms" ) )
1077         {
1078            sms_cartridge[index].features |= CF_GG_SMS_MODE;
8781079         }
8791080      }
8801081   }
8811082
882   if ( sms_cartridge[index].features & CF_CODEMASTERS_MAPPER ) {
1083   if ( sms_cartridge[index].features & CF_CODEMASTERS_MAPPER )
1084   {
8831085      sms_cartridge[index].ram_size = 0x10000;
8841086      sms_cartridge[index].cartRAM = auto_malloc( sms_cartridge[index].ram_size );
8851087      sms_cartridge[index].ram_page = 0;
r4402r4403
8911093   return INIT_PASS;
8921094}
8931095
894static void setup_cart_banks( void ) {
895   if ( sms_cartridge[sms_current_cartridge].ROM ) {
1096
1097static void setup_cart_banks( void )
1098{
1099   if ( sms_cartridge[sms_current_cartridge].ROM )
1100   {
8961101      UINT8   rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000;
8971102      sms_banking_cart[1] = sms_cartridge[sms_current_cartridge].ROM;
8981103      sms_banking_cart[2] = sms_cartridge[sms_current_cartridge].ROM + 0x0400;
r4402r4403
9021107      if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) {
9031108         sms_banking_cart[4] = sms_cartridge[sms_current_cartridge].ROM;
9041109      }
905   } else {
1110   }
1111   else
1112   {
9061113      sms_banking_cart[1] = sms_banking_none[1];
9071114      sms_banking_cart[2] = sms_banking_none[2];
9081115      sms_banking_cart[3] = sms_banking_none[3];
r4402r4403
9101117   }
9111118}
9121119
913static void setup_banks( running_machine *machine ) {
1120
1121static void setup_banks( running_machine *machine )
1122{
9141123   UINT8 *mem = memory_region(machine, "maincpu");
9151124   sms_banking_bios[1] = sms_banking_cart[1] = sms_banking_none[1] = mem;
9161125   sms_banking_bios[2] = sms_banking_cart[2] = sms_banking_none[2] = mem;
r4402r4403
9231132
9241133   setup_cart_banks();
9251134
926   if ( BIOS == NULL || BIOS[0] == 0x00 ) {
1135   if ( BIOS == NULL || BIOS[0] == 0x00 )
1136   {
9271137      BIOS = NULL;
9281138                biosPort |= IO_BIOS_ROM;
9291139   }
9301140
931   if ( BIOS ) {
1141   if ( BIOS )
1142   {
9321143      sms_banking_bios[1] = BIOS;
9331144      sms_banking_bios[2] = BIOS + 0x0400;
9341145      sms_banking_bios[3] = BIOS + ( ( 1 < smsBiosPageCount) ? 0x4000 : 0 );
r4402r4403
9361147   }
9371148}
9381149
939MACHINE_START(sms) {
1150
1151MACHINE_START(sms)
1152{
9401153   add_exit_callback(machine, sms_machine_stop);
9411154}
9421155
1156
9431157MACHINE_RESET(sms)
9441158{
9451159   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
9461160
9471161   smsVersion = 0x00;
948   if ( HAS_FM ) {
1162   if ( HAS_FM )
1163   {
9491164      smsFMDetect = 0x01;
9501165   }
9511166
9521167   sms_mapper_ram = memory_get_write_ptr( space, 0xDFFC );
9531168
954   if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) {
1169   if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER )
1170   {
9551171      /* Install special memory handlers */
9561172      memory_install_write8_handler(space, 0x0000, 0x0000, 0, 0, sms_codemasters_page0_w );
9571173      memory_install_write8_handler(space, 0x4000, 0x4000, 0, 0, sms_codemasters_page1_w );
9581174   }
9591175
1176   if ( sms_cartridge[sms_current_cartridge].features & CF_GG_SMS_MODE )
1177   {
1178      sms_set_ggsmsmode( 1 );
1179   }
1180
9601181   /* Initialize SIO stuff for GG */
9611182   ggSIO[0] = 0x7F;
9621183   ggSIO[1] = 0xFF;
r4402r4403
9901211   sports_pad_2_y = 0;
9911212}
9921213
993READ8_HANDLER(sms_store_cart_select_r) {
1214
1215READ8_HANDLER(sms_store_cart_select_r)
1216{
9941217   return 0xFF;
9951218}
9961219
997WRITE8_HANDLER(sms_store_cart_select_w) {
1220
1221WRITE8_HANDLER(sms_store_cart_select_w)
1222{
9981223   UINT8 slot = data >> 4;
9991224   UINT8 slottype = data & 0x08;
10001225
10011226   logerror("switching in part of %s slot #%d\n", slottype ? "card" : "cartridge", slot );
10021227   /* cartridge? slot #0 */
1003   if ( slottype == 0 ) {
1228   if ( slottype == 0 )
1229   {
10041230      sms_current_cartridge = slot;
10051231   }
10061232   setup_cart_banks();
r4402r4403
10081234   setup_rom(space);
10091235}
10101236
1011READ8_HANDLER(sms_store_select1) {
1237
1238READ8_HANDLER(sms_store_select1)
1239{
10121240   return 0xFF;
10131241}
10141242
1015READ8_HANDLER(sms_store_select2) {
1243
1244READ8_HANDLER(sms_store_select2)
1245{
10161246   return 0xFF;
10171247}
10181248
1019READ8_HANDLER(sms_store_control_r) {
1249
1250READ8_HANDLER(sms_store_control_r)
1251{
10201252   return sms_store_control;
10211253}
10221254
1023WRITE8_HANDLER(sms_store_control_w) {
1255
1256WRITE8_HANDLER(sms_store_control_w)
1257{
10241258   logerror( "0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(space->cpu), data );
1025   if ( data & 0x02 ) {
1259   if ( data & 0x02 )
1260   {
10261261      cpu_resume( space->machine->cpu[0], SUSPEND_REASON_HALT );
1027   } else {
1262   }
1263   else
1264   {
10281265      /* Pull reset line of CPU #0 low */
10291266      cpu_suspend( space->machine->cpu[0], SUSPEND_REASON_HALT, 1 );
10301267      device_reset(space->machine->cpu[0]);
r4402r4403
10321269   sms_store_control = data;
10331270}
10341271
1035void sms_int_callback( running_machine *machine, int state ) {
1272
1273void sms_int_callback( running_machine *machine, int state )
1274{
10361275   cpu_set_input_line(machine->cpu[0], 0, state );
10371276}
10381277
1039void sms_store_int_callback( running_machine *machine, int state ) {
1278
1279void sms_store_int_callback( running_machine *machine, int state )
1280{
10401281   cpu_set_input_line(machine->cpu[0], sms_store_control & 0x01 ? 1 : 0, state );
10411282}
10421283

Previous 15994 Revisions Next


© 1998-2018 The MESS Team