Previous 15994 Revisions Next

r1011 Wednesday 19th September, 2007 at 17:14:47 UTC by Wilbert Pol
[PCE] Added some small things to the cd interface.
[src/mess/drivers]pce.c
[src/mess/machine]pce.c

src/mess/machine/pce.c
r1010r1011
33#include "video/vdc.h"
44#include "cpu/h6280/h6280.h"
55#include "includes/pce.h"
6#include "devices/chd_cd.h"
67#include "sound/msm5205.h"
78#include "image.h"
89
910/* the largest possible cartridge image (street fighter 2 - 2.5MB) */
10#define PCE_ROM_MAXSIZE  (0x280000)
11#define PCE_BRAM_SIZE   0x800
11#define PCE_ROM_MAXSIZE            (0x280000)
12#define PCE_BRAM_SIZE            0x800
13#define PCE_ADPCM_RAM_SIZE         0x10000
14#define PCE_CD_COMMAND_BUFFER_SIZE   0x100
1215
1316/* system RAM */
1417unsigned char *pce_user_ram;    /* scratch RAM at F8 */
1518
1619/* CD Unit RAM */
1720UINT8   *pce_cd_ram;         /* 64KB RAM from a CD unit */
18static UINT8   *pce_cd_bram;
19
21static UINT8   pce_sys3_card;   /* Is a Super CD System 3 card present */
2022static struct {
2123   UINT8   regs[16];
24   UINT8   *bram;
25   UINT8   *adpcm_ram;
2226   int      bram_locked;
2327   int      adpcm_read_ptr;
2428   int      adpcm_write_ptr;
2529   int      adpcm_length;
30   int      adpcm_clock_count;
31   int      adpcm_clock_divider;
32   /* SCSI signals */
33   int      scsi_BSY;   /* Busy. Bus in use */
34   int      scsi_SEL;   /* Select. Initiator has won arbitration and has selected a target */
35   int      scsi_CD;   /* Control/Data. Target is sending control (data) information */
36   int      scsi_IO;   /* Input/Output. Target is sending (receiving) information */
37   int      scsi_MSG;   /* Message. Target is sending or receiving a message */
38   int      scsi_REQ;   /* Request. Target is requesting a data transfer */
39   int      scsi_ACK;   /* Acknowledge. Initiator acknowledges that it is ready for a data transfer */
40   int      scsi_ATN;   /* Attention. Initiator has a message ready for the target */
41   int      scsi_RST;   /* Reset. Initiator forces all targets and any other initiators to do a warm reset */
42   int      scsi_last_RST;   /* To catch setting of RST signal */
43   int      cd_motor_on;
44   int      selected;
45   UINT8   *command_buffer;
46   int      command_buffer_index;
47   int      status_sent;
48   int      message_after_status;
49   int      message_sent;
50   UINT8   *data_buffer;
51   int      data_buffer_size;
52   int      data_buffer_index;
53   int      data_transferred;
2654} pce_cd;
2755
2856/* MSM5205 ADPCM decoder definition */
r1010r1011
4876static void pce_cd_init( void );
4977
5078
79static mess_image *cdrom_device_image( void ) {
80   return image_from_devtype_and_index( IO_CDROM, 0 );
81}
82
5183static WRITE8_HANDLER( pce_sf2_banking_w ) {
5284   memory_set_bankptr( 2, memory_region(REGION_USER1) + offset * 0x080000 + 0x080000 );
5385   memory_set_bankptr( 3, memory_region(REGION_USER1) + offset * 0x080000 + 0x088000 );
r1010r1011
158190   }
159191
160192   /* Check for CD system card */
193   pce_sys3_card = 0;
161194   if ( ! memcmp( ROM + 0x3FFB6, "PC Engine CD-ROM SYSTEM", 23 ) ) {
162195      /* Check if 192KB additional system card ram should be used */
163196      if ( ! memcmp( ROM + 0x29D1, "VER. 3.", 7 ) || ! memcmp( ROM + 0x29C4, "VER. 3.", 7 ) ) {
197         pce_sys3_card = 1;
164198         cartridge_ram = auto_malloc( 0x30000 );
165199         memory_set_bankptr( 4, cartridge_ram );
166200         memory_install_write8_handler( 0, ADDRESS_SPACE_PROGRAM, 0x0D0000, 0x0FFFFF, 0, 0, pce_cartridge_ram_w );
r1010r1011
220254{
221255   if (read_or_write)
222256   {
223      mame_fwrite(file, pce_cd_bram, PCE_BRAM_SIZE);
257      mame_fwrite(file, pce_cd.bram, PCE_BRAM_SIZE);
224258   }
225259   else
226260   {
227261      /* load battery backed memory from disk */
228262      if (file)
229         mame_fread(file, pce_cd_bram, PCE_BRAM_SIZE);
263         mame_fread(file, pce_cd.bram, PCE_BRAM_SIZE);
230264   }
231265}
232266
233267static void pce_set_cd_bram( void ) {
234   memory_set_bankptr( 10, pce_cd_bram + ( pce_cd.bram_locked ? PCE_BRAM_SIZE : 0 ) );
268   memory_set_bankptr( 10, pce_cd.bram + ( pce_cd.bram_locked ? PCE_BRAM_SIZE : 0 ) );
235269}
236270
237static void pce_cd_msm5205_int( int data ) {
271/* Callback for new data from the MSM5205.
272  The PCE cd unit actually divides the clock signal supplied to
273  the MSM5205. Currently we can only use static clocks for the
274  MSM5205.
275 */
276static void pce_cd_msm5205_int( int chip ) {
277   pce_cd.adpcm_clock_count = ( pce_cd.adpcm_clock_count + 1 ) % pce_cd.adpcm_clock_divider;
278   if ( ! pce_cd.adpcm_clock_count ) {
279      /* Supply new ADPCM data */
280   } else {
281      /* Make sure the sample does not change */
282   }
238283}
239284
240static void pce_cd_reset( void ) {
285#define   SCSI_STATUS_OK         0x00
286#define SCSI_CHECK_CONDITION   0x02
287
288static void pce_cd_reply_status_byte( UINT8 status ) {
289   pce_cd.scsi_CD = pce_cd.scsi_IO = pce_cd.scsi_REQ = 1;
290   pce_cd.scsi_MSG = 0;
291   pce_cd.message_after_status = 1;
292   pce_cd.status_sent = pce_cd.message_sent = 0;
293
294   if ( status == SCSI_STATUS_OK ) {
295      pce_cd.regs[0x01] = 0x00;
296   } else {
297      pce_cd.regs[0x01] = 0x01;
298   }
241299}
242300
301static void pce_cd_test_unit_ready( void ) {
302   logerror("test unit ready\n");
303   if ( image_exists( cdrom_device_image() ) ) {
304      logerror( "Sending STATUS_OK status\n" );
305      pce_cd_reply_status_byte( SCSI_STATUS_OK );
306   } else {
307      logerror( "Sending CHECK_CONDITION status\n" );
308      pce_cd_reply_status_byte( SCSI_CHECK_CONDITION );
309   }
310}
311
312static void pce_cd_nec_get_dir_info( void ) {
313   UINT32 frame, msf, track;
314   mess_image *img = cdrom_device_image();
315   const cdrom_toc*   toc;
316   logerror("nec get dir info\n");
317
318   if ( ! image_exists( img ) ) {
319      /* Throw some error here */
320      pce_cd_reply_status_byte( SCSI_CHECK_CONDITION );
321   }
322
323   toc = cdrom_get_toc( mess_cd_get_cdrom_file(img) );
324
325   switch( pce_cd.command_buffer[1] ) {
326   case 0x00:      /* Get first and last track numbers */
327      pce_cd.data_buffer[0] = dec_2_bcd(1);
328      pce_cd.data_buffer[1] = dec_2_bcd(toc->numtrks);
329      pce_cd.data_buffer_size = 2;
330      break;
331   case 0x01:      /* Get total disk size in MSF format */
332      frame = toc->tracks[toc->numtrks-1].physframeofs;
333      frame += toc->tracks[toc->numtrks-1].frames;
334      msf = lba_to_msf( frame );
335
336      pce_cd.data_buffer[0] = ( msf >> 16 ) & 0xFF;   /* M */
337      pce_cd.data_buffer[1] = ( msf >> 8 ) & 0xFF;   /* S */
338      pce_cd.data_buffer[2] = msf & 0xFF;            /* F */
339      pce_cd.data_buffer_size = 3;
340      break;
341   case 0x02:      /* Get track information */
342      if ( pce_cd.command_buffer[2] == 0xAA ) {
343         frame = toc->tracks[toc->numtrks-1].physframeofs;
344         frame += toc->tracks[toc->numtrks-1].frames;
345         pce_cd.data_buffer[3] = 0x04;   /* correct? */
346      } else {
347         track = MAX( bcd_2_dec( pce_cd.command_buffer[2] ), 1 );
348         frame = toc->tracks[track-1].physframeofs;
349         pce_cd.data_buffer[3] = ( toc->tracks[track-1].trktype == CD_TRACK_AUDIO ) ? 0x00 : 0x04;
350      }
351      msf = lba_to_msf( frame );
352      pce_cd.data_buffer[0] = ( msf >> 16 ) & 0xFF;   /* M */
353      pce_cd.data_buffer[1] = ( msf >> 8 ) & 0xFF;   /* S */
354      pce_cd.data_buffer[2] = msf & 0xFF;            /* F */
355      pce_cd.data_buffer_size = 4;
356      break;
357   default:
358      assert( pce_cd_nec_get_dir_info == NULL );  // Not implemented yet
359      break;
360   }
361
362   pce_cd.data_buffer_index = 0;
363   pce_cd.data_transferred = 1;
364   pce_cd.scsi_IO = 1;
365   pce_cd.scsi_CD = 0;
366}
367
368static void pce_cd_handle_data_output( void ) {
369   static const struct {
370      UINT8   command_byte;
371      UINT8   command_size;
372      void   (*command_handler)(void);
373   } pce_cd_commands[] = {
374      { 0x00, 6, pce_cd_test_unit_ready },   /* TEST UNIT READY */
375      { 0xDE,10, pce_cd_nec_get_dir_info },   /* NEC GET DIR INFO */
376      { 0xFF, 0, NULL }      /* end of list marker */
377   };
378
379   if ( pce_cd.scsi_REQ && pce_cd.scsi_ACK ) {
380      /* Command byte received */
381      logerror( "Command byte $%02X received\n", pce_cd.regs[0x01] );
382
383      /* Check for buffer overflow */
384      assert( pce_cd.command_buffer_index < PCE_CD_COMMAND_BUFFER_SIZE );
385
386      pce_cd.command_buffer[pce_cd.command_buffer_index] = pce_cd.regs[0x01];
387      pce_cd.command_buffer_index++;
388      pce_cd.scsi_REQ = 0;
389   }
390
391   if ( ! pce_cd.scsi_REQ && ! pce_cd.scsi_ACK && pce_cd.command_buffer_index ) {
392      int i = 0;
393
394      logerror( "Check if command done\n" );
395
396      for( i = 0; pce_cd.command_buffer[0] > pce_cd_commands[i].command_byte; i++ );
397
398      /* Check for unknown commands */
399      assert( pce_cd.command_buffer[0] == pce_cd_commands[i].command_byte );
400
401      if ( pce_cd.command_buffer_index == pce_cd_commands[i].command_size ) {
402         (pce_cd_commands[i].command_handler)();
403         pce_cd.command_buffer_index = 0;
404      } else {
405         pce_cd.scsi_REQ = 1;
406      }
407   }
408}
409
410static void pce_cd_handle_data_input( void ) {
411   if ( pce_cd.scsi_CD ) {
412      /* Command / Status byte */
413      if ( pce_cd.scsi_REQ && pce_cd.scsi_ACK ) {
414         logerror( "status sent\n" );
415         pce_cd.scsi_REQ = 0;
416         pce_cd.status_sent = 1;
417      }
418
419      if ( ! pce_cd.scsi_REQ && ! pce_cd.scsi_ACK && pce_cd.status_sent ) {
420         pce_cd.status_sent = 0;
421         if ( pce_cd.message_after_status ) {
422            logerror( "message after status\n" );
423            pce_cd.message_after_status = 0;
424            pce_cd.scsi_MSG = pce_cd.scsi_REQ = 1;
425            pce_cd.regs[0x01] = 0;
426         }
427      }
428   } else {
429      /* Data */
430      if ( pce_cd.scsi_REQ && pce_cd.scsi_ACK ) {
431         pce_cd.scsi_REQ = 0;
432      }
433
434      if ( ! pce_cd.scsi_REQ && ! pce_cd.scsi_ACK ) {
435         if ( pce_cd.data_buffer_index == pce_cd.data_buffer_size ) {
436            if ( pce_cd.data_transferred ) {
437               pce_cd.data_transferred = 0;
438               pce_cd_reply_status_byte( SCSI_STATUS_OK );
439            }
440         } else {
441logerror("Transfer byte\n");
442            pce_cd.regs[0x01] = pce_cd.data_buffer[pce_cd.data_buffer_index];
443            pce_cd.data_buffer_index++;
444            pce_cd.scsi_REQ = 1;
445         }
446      }
447   }
448}
449
450static void pce_cd_handle_message_output( void ) {
451   if ( pce_cd.scsi_REQ && pce_cd.scsi_ACK ) {
452      pce_cd.scsi_REQ = 0;
453   }
454}
455
456static void pce_cd_handle_message_input( void ) {
457   if ( pce_cd.scsi_REQ && pce_cd.scsi_ACK ) {
458      pce_cd.scsi_REQ = 0;
459      pce_cd.message_sent = 1;
460   }
461
462   if ( ! pce_cd.scsi_REQ && ! pce_cd.scsi_ACK && pce_cd.message_sent ) {
463      pce_cd.message_sent = 0;
464      pce_cd.scsi_BSY = 0;
465   }
466}
467
468/* Update internal CD statuses */
469static void pce_cd_update( void ) {
470   /* Check for reset of CD unit */
471   if ( pce_cd.scsi_RST != pce_cd.scsi_last_RST ) {
472      if ( pce_cd.scsi_RST ) {
473         logerror("Performing CD reset\n");
474         /* Reset internal data */
475         pce_cd.scsi_BSY = pce_cd.scsi_SEL = pce_cd.scsi_CD = pce_cd.scsi_IO = 0;
476         pce_cd.scsi_MSG = pce_cd.scsi_REQ = pce_cd.scsi_ATN = 0;
477         pce_cd.cd_motor_on = 0;
478         pce_cd.selected = 0;
479      }
480      pce_cd.scsi_last_RST = pce_cd.scsi_RST;
481   }
482
483   /* Check if bus can be freed */
484   if ( ! pce_cd.scsi_SEL && ! pce_cd.scsi_BSY && pce_cd.selected ) {
485      logerror( "freeing bus\n" );
486      pce_cd.selected = 0;
487      pce_cd.scsi_CD = pce_cd.scsi_MSG = pce_cd.scsi_IO = pce_cd.scsi_REQ = 0;
488   }
489
490   /* Select the CD device */
491   if ( pce_cd.scsi_SEL ) {
492      if ( ! pce_cd.selected ) {
493         pce_cd.selected = 1;
494         pce_cd.scsi_BSY = pce_cd.scsi_REQ = pce_cd.scsi_CD = 1;
495         pce_cd.scsi_MSG = pce_cd.scsi_IO = 0;
496      }
497   }
498
499   if ( pce_cd.scsi_ATN ) {
500   } else {
501      /* Check for data and pessage phases */
502      if ( pce_cd.scsi_BSY ) {
503         if ( pce_cd.scsi_MSG ) {
504            /* message phase */
505            if ( pce_cd.scsi_IO ) {
506               pce_cd_handle_message_input();
507            } else {
508               pce_cd_handle_message_output();
509            }
510         } else {
511            /* data phase */
512            if ( pce_cd.scsi_IO ) {
513               /* Reading data from target */
514               pce_cd_handle_data_input();
515            } else {
516               /* Sending data to target */
517               pce_cd_handle_data_output();
518            }
519         }
520      }
521   }
522}
523
243524static void pce_cd_init( void ) {
244525   /* Initialize pce_cd struct */
245526   memset( &pce_cd, 0, sizeof(pce_cd) );
246527
247528   /* Initialize BRAM */
248   pce_cd_bram = auto_malloc( PCE_BRAM_SIZE * 2 );
249   memset( pce_cd_bram, 0, PCE_BRAM_SIZE );
250   memset( pce_cd_bram + PCE_BRAM_SIZE, 0xFF, PCE_BRAM_SIZE );
529   pce_cd.bram = auto_malloc( PCE_BRAM_SIZE * 2 );
530   memset( pce_cd.bram, 0, PCE_BRAM_SIZE );
531   memset( pce_cd.bram + PCE_BRAM_SIZE, 0xFF, PCE_BRAM_SIZE );
251532   pce_cd.bram_locked = 1;
252533   pce_set_cd_bram();
534
535   /* set up adpcm related things */
536   pce_cd.adpcm_ram = auto_malloc( PCE_ADPCM_RAM_SIZE );
537   memset( pce_cd.adpcm_ram, 0, PCE_ADPCM_RAM_SIZE );
538   pce_cd.adpcm_clock_divider = 1;
539
540   /* Set up cd command buffer */
541   pce_cd.command_buffer = auto_malloc( PCE_CD_COMMAND_BUFFER_SIZE );
542   memset( pce_cd.command_buffer, 0, PCE_CD_COMMAND_BUFFER_SIZE );
543   pce_cd.command_buffer_index = 0;
544
545   pce_cd.data_buffer = auto_malloc( 8192 );
546   memset( pce_cd.data_buffer, 0, 8192 );
547   pce_cd.data_buffer_size = 0;
548   pce_cd.data_buffer_index = 0;
253549}
254550
255551WRITE8_HANDLER( pce_cd_bram_w ) {
256552   if ( ! pce_cd.bram_locked ) {
257      pce_cd_bram[ offset ] = data;
553      pce_cd.bram[ offset ] = data;
258554   }
259555}
260556
261557WRITE8_HANDLER( pce_cd_intf_w ) {
262558   logerror("%04X: write to CD interface offset %02X, data %02X\n", activecpu_get_pc(), offset, data );
559   pce_cd_update();
263560
264   switch( offset & 0x0F ) {
561   switch( offset ) {
265562   case 0x00:   /* CDC status */
563      /* select device (which bits??) */
564      pce_cd.scsi_SEL = 1;
565      pce_cd_update();
566      pce_cd.scsi_SEL = 0;
567      break;
266568   case 0x01:   /* CDC command / status / data */
267569      break;
268570   case 0x02:   /* ADPCM / CD control */
269571      /* 00 is written here during booting */
572      pce_cd.scsi_ACK = data & 0x80;
270573      break;
271574   case 0x03:   /* BRAM lock / CD status - Read Only register */
272575      break;
273576   case 0x04:   /* CD reset */
274      if ( data & 0x02 ) {
275         pce_cd_reset();
276      }
577      pce_cd.scsi_RST = data & 0x02;
277578      break;
278579   case 0x05:   /* Convert PCM data / PCM data */
279580   case 0x06:   /* PCM data */
r1010r1011
306607      }
307608      break;
308609   case 0x0E:   /* ADPCM playback rate */
610      pce_cd.adpcm_clock_divider = 16 - ( data & 0x0F );
611      pce_cd.adpcm_clock_count = pce_cd.adpcm_clock_divider - 1;
309612      break;
310613   case 0x0F:   /* ADPCM and CD audio fade timer */
311614      break;
615   default:
616      return;
312617   }
313
314618   pce_cd.regs[offset] = data;
619   pce_cd_update();
315620}
316621
317622READ8_HANDLER( pce_cd_intf_r ) {
318   UINT8 data = pce_cd.regs[offset];
623   UINT8 data = pce_cd.regs[offset & 0x0F];
319624
625   pce_cd_update();
626
320627   logerror("%04X: read from CD interface offset %02X\n", activecpu_get_pc(), offset );
321   switch( offset & 0x0F ) {
628   switch( offset ) {
322629   case 0x00:   /* CDC status */
630      data &= 0x07;
631      data |= pce_cd.scsi_BSY ? 0x80 : 0;
632      data |= pce_cd.scsi_REQ ? 0x40 : 0;
633      data |= pce_cd.scsi_MSG ? 0x20 : 0;
634      data |= pce_cd.scsi_CD  ? 0x10 : 0;
635      data |= pce_cd.scsi_IO  ? 0x08 : 0;
636      break;
323637   case 0x01:   /* CDC command / status / data */
638      break;
324639   case 0x02:   /* ADPCM / CD control */
325640      break;
326641   case 0x03:   /* BRAM lock / CD status */
r1010r1011
329644      pce_cd.bram_locked = 1;
330645      pce_set_cd_bram();
331646      data = 0;
647      data |= ( pce_cd.cd_motor_on ? 0x10 : 0 );
332648      break;
333649   case 0x04:   /* CD reset */
650      break;
334651   case 0x05:   /* Convert PCM data / PCM data */
335652   case 0x06:   /* PCM data */
336653   case 0x07:   /* BRAM unlock / CD status */
r1010r1011
345662   case 0x0E:   /* ADPCM playback rate */
346663   case 0x0F:   /* ADPCM and CD audio fade timer */
347664      break;
665   case 0xC1:
666      data = pce_sys3_card ? 0xAA : 0xFF;
667      break;
668   case 0xC2:
669      data = pce_sys3_card ? 0x55 : 0xFF;
670      break;
671   case 0xC5:
672      data = pce_sys3_card ? 0x55 : 0xFF;
673      break;
674   case 0xC6:
675      data = pce_sys3_card ? 0xAA : 0xFF;
676      break;
677   default:
678      data = 0xFF;
679      break;
348680   }
349681
350682   return data;
src/mess/drivers/pce.c
r1010r1011
5656#include "cpu/h6280/h6280.h"
5757#include "includes/pce.h"
5858#include "devices/cartslot.h"
59#include "devices/chd_cd.h"
5960#include "sound/c6280.h"
6061#include "sound/msm5205.h"
6162#include "hash.h"
r1010r1011
236237   }
237238}
238239
240static void pce_chdcd_getinfo(const device_class *devclass, UINT32 state, union devinfo *info) {
241   switch( state ) {
242   case DEVINFO_INT_COUNT:         info->i = 1; break;
243   default:                  cdrom_device_getinfo(devclass, state, info);
244   }
245}
246
239247SYSTEM_CONFIG_START(pce)
240248   CONFIG_DEVICE(pce_cartslot_getinfo)
249   CONFIG_DEVICE(pce_chdcd_getinfo)
241250SYSTEM_CONFIG_END
242251
243252/***************************************************************************

Previous 15994 Revisions Next


© 1998-2018 The MESS Team