Previous 15994 Revisions Next

r3348 Monday 29th September, 2008 at 18:29:46 UTC by Wilbert Pol
[CGENIE] Added proper cassette emulation. .wav only for now.
[src/mess/drivers]cgenie.c
[src/mess/includes]cgenie.h
[src/mess/machine]cgenie.c

src/mess/includes/cgenie.h
r3347r3348
2020
2121extern UINT8 *cgenie_fontram;
2222
23DEVICE_IMAGE_LOAD( cgenie_cassette );
2423DEVICE_IMAGE_LOAD( cgenie_floppy );
2524
2625extern int cgenie_tv_mode;
src/mess/machine/cgenie.c
r3347r3348
1313#include "machine/wd17xx.h"
1414#include "devices/basicdsk.h"
1515#include "devices/cartslot.h"
16#include "devices/cassette.h"
1617#include "sound/ay8910.h"
1718#include "sound/dac.h"
1819
r3347r3348
2728
2829
2930int cgenie_tv_mode = -1;
30static int cgenie_load_cas = 0;
3131static int port_ff = 0xff;
3232
3333#define CGENIE_DRIVE_INFO
r3347r3348
7878static UINT8 motor_drive;
7979static UINT8 head;
8080
81/* current tape file handles */
82static char tape_name[12+1];
83static mame_file *tape_get_file;
81static UINT8 cass_level;
82static UINT8 cass_bit;
8483
85/* tape buffer for the first eight bytes at write (to extract a filename) */
86static UINT8 tape_buffer[9];
87
88/* file offset within tape file */
89static int tape_count;
90
91/* number of sync and data bits that were written */
92static int put_bit_count;
93
94/* number of sync and data bits to read */
95static int get_bit_count;
96
97/* sync and data bits mask */
98static int tape_bits;
99
100/* time in cycles for the next bit at read */
101static int tape_time;
102
103/* flag if writing to tape detected the sync header A5 already */
104static int in_sync;
105
106/* cycle count at last output port change */
107static int put_cycles;
108
109/* cycle count at last input port read */
110static int get_cycles;
111
112/* a prototype to be called from cgenie_stop_machine */
113static void tape_put_close(running_machine *machine);
114
115/* Buffer for image reads */
116static UINT8 *image_buff;
117
118static OPBASE_HANDLER (opbaseoverride)
84static TIMER_CALLBACK( handle_cassette_input )
11985{
120   UINT8 *RAM = memory_region(machine, "main");
121   const device_config *img;
122   UINT8 *buff, *s, data;
123   UINT16 size, entry = 0, block_len, block_ofs = 0;
86   UINT8 new_level = ( cassette_input( device_list_find_by_tag( machine->config->devicelist, CASSETTE, "cassette" ) ) > 0.0 ) ? 1 : 0;
12487
125   /* check if the BASIC prompt is visible on the screen */
126   if( cgenie_load_cas && RAM[0x4400+3*40] == 0x3e )
88   if ( new_level != cass_level )
12789   {
128      cgenie_load_cas = 0;
129
130      img = image_from_devtype_and_index(IO_CASSETTE, 0);
131
132      if (image_exists(img))
133      {
134         buff = image_buff;
135         size = image_fread(img, buff, 65536);
136         s = buff;
137         if( memcmp(s, TAPE_HEADER, sizeof(TAPE_HEADER)-1) == 0 )
138         {
139            s = (UINT8*)memchr(s, 26, size);
140            if( s )
141            {
142               *s++ = '\n';
143               *s++ = '\0';
144               logerror("%s",s);
145            }
146            size -= s - buff;
147         }
148         if( s[0] == 0x66 && s[1] == 0x55 && s[8] == 0x3c )
149         {
150            logerror("image name: [%-6.6s]\n",s+1);
151            s += 8;
152            size -= 8;
153            while( size > 3 )
154            {
155               data = *s++;
156               switch( data )
157               {
158               case 0x01:         /* CMD file header */
159               case 0x07:         /* another type of CMD file header */
160               case 0x3c:         /* CAS file header */
161                  block_len = *s++;
162                  /* on CMD files size zero means size 256 */
163                  if( block_len == 0 )
164                     block_len = 256;
165                  block_ofs = *s++;
166                  block_ofs += 256 * *s++;
167                  if( data != 0x3c )
168                  {
169                     block_len -= 2;
170                     if( block_len == 0 )
171                        block_len = 256;
172                  }
173                  size -= 4;
174                  logerror("cgenie_cmd_load block ($%02X) %d at $%04X\n", data, block_len, block_ofs);
175                  while( block_len && size )
176                  {
177                     program_write_byte(block_ofs, *s);
178                     s++;
179                     block_ofs++;
180                     block_len--;
181                     size--;
182                  }
183                  if( data == 0x3c )
184                     s++;
185                  break;
186               case 0x02:
187                  block_len = *s++;
188                  size -= 1;
189               case 0x78:
190                  block_ofs = *s++;
191                  block_ofs += 256 * *s++;
192                  if( !entry )
193                     entry = block_ofs;
194                  logerror( "cgenie_cmd_load entry ($%02X) at $%04X\n", data, entry);
195                  size -= 3;
196                  if( size <= 3 )
197                  {
198                     logerror("starting program at $%04X\n", block_ofs);
199                  }
200                  break;
201               default:
202                  size--;
203               }
204            }
205            activecpu_set_reg(REG_PC, entry);
206         }
207      }
208      memory_set_opbase_handler(0,NULL);
90      cass_level = new_level;
91      cass_bit ^= 1;
20992   }
210   return address;
21193}
21294
95
21396static void cgenie_fdc_callback(running_machine *machine, wd17xx_state_t event, void *param);
21497
21598MACHINE_RESET( cgenie )
r3347r3348
287170      memset(&memory_region(machine, "main")[0x0e000], 0x00, 0x1000);
288171   }
289172
290   cgenie_load_cas = 1;
291   memory_set_opbase_handler(0, opbaseoverride);
173   cass_level = 0;
174   cass_bit = 1;
175   timer_pulse( ATTOTIME_IN_HZ(11025), NULL, 0, handle_cassette_input );
292176}
293177
294178MACHINE_START( cgenie )
r3347r3348
297181   int i;
298182
299183   /* initialize static variables */
300   memset(tape_buffer, 0, sizeof(tape_buffer));
301   memset(tape_name, 0, sizeof(tape_name));
302   tape_get_file = NULL;
303184   irq_status = 0;
304185   motor_drive = 0;
305186   head = 0;
306   tape_count = 0;
307   put_bit_count = 0;
308   get_bit_count = 0;
309   tape_bits = 0;
310   tape_time = 0;
311   in_sync = 0;
312   put_cycles = 0;
313   get_cycles = 0;
314   image_buff = auto_malloc(65536);
315187
316188   /*
317189    * Every fifth cycle is a wait cycle, so I reduced
318190    * the overlocking by one fitfth
191    * Underclocking causes the tape loading to not work.
319192    */
320   cpunum_set_clockscale(machine, 0, 0.80);
193//   cpunum_set_clockscale(machine, 0, 0.80);
321194
322195   /* Initialize some patterns to be displayed in graphics mode */
323196   for( i = 0; i < 256; i++ )
r3347r3348
331204
332205   /* set up FDC */
333206   wd17xx_init(machine, WD_TYPE_179X, cgenie_fdc_callback, NULL);
334
335   add_exit_callback(machine, tape_put_close);
336207}
337208
338209
339DEVICE_IMAGE_LOAD( cgenie_cassette )
340{
341   return INIT_PASS;
342}
343
344210/* basic-dsk is a disk image format which has the tracks and sectors
345211 * stored in order, no information is stored which details the number
346212 * of tracks, number of sides, number of sectors etc, so we need to
r3347r3348
436302   return INIT_PASS;
437303}
438304
439/*************************************
440 *
441 *            Tape emulation.
442 *
443 *************************************/
444305
445/*******************************************************************
446 * tape_put_byte
447 * write next data byte to virtual tape. After collecting the first
448 * nine bytes try to extract the kind of data and filename.
449 *******************************************************************/
450static void tape_put_byte(UINT8 value)
451{
452   mame_file *file;
453
454   if( tape_count < 9 )
455   {
456      tape_name[0] = '\0';
457      tape_buffer[tape_count++] = value;
458      if( tape_count == 9 )
459      {
460         /* BASIC tape ? */
461         if( tape_buffer[1] != 0x55 || tape_buffer[8] != 0x3c )
462            sprintf(tape_name, "basic%c.cas", tape_buffer[1]);
463         else
464         /* SYSTEM tape ? */
465         if( tape_buffer[1] == 0x55 && tape_buffer[8] == 0x3c )
466            sprintf(tape_name, "%-6.6s.cas", tape_buffer + 2);
467         else
468            strcpy(tape_name, "unknown.cas");
469         mame_fopen(SEARCHPATH_IMAGE, tape_name, OPEN_FLAG_WRITE, &file);
470      }
471   }
472   else
473   {
474      tape_count++;
475   }
476}
477
478/*******************************************************************
479 * tape_put_close
480 * eventuall flush output buffer and close an open
481 * virtual tape output file.
482 *******************************************************************/
483static void tape_put_close(running_machine *machine)
484{
485   tape_count = 0;
486}
487
488/*******************************************************************
489 * tape_put_bit
490 * port FF tape status bit changed. Figure out what to do with it ;-)
491 *******************************************************************/
492static void tape_put_bit(running_machine *machine)
493{
494   int now_cycles = activecpu_gettotalcycles();
495   int diff = now_cycles - put_cycles;
496   int limit = 12 * (memory_region(machine, "main")[0x4310] + memory_region(machine, "main")[0x4311]);
497   UINT8 value;
498
499   /* overrun since last write ? */
500   if( diff > 4000 )
501   {
502      /* reset tape output */
503      tape_put_close(machine);
504      put_bit_count = tape_bits = in_sync = 0;
505   }
506   else
507   {
508      /* change within time for a 1 bit ? */
509      if( diff < limit )
510      {
511         tape_bits = (tape_bits << 1) | 1;
512         switch( in_sync )
513         {
514            case 0:       /* look for sync AA */
515               if( (tape_bits & 0xffff) == 0xcccc )
516               {
517                  popmessage("Tape sync1 written");
518                  in_sync = 1;
519               }
520               break;
521            case 1:       /* look for sync 66 */
522               if( (tape_bits & 0xffff) == 0x3c3c )
523               {
524                  in_sync = 2;
525                  popmessage("Tape sync2 written");
526                  put_bit_count = 16;
527               }
528               break;
529            case 2:       /* count 1 bit */
530               put_bit_count += 1;
531         }
532      }
533      /* no change within time indicates a 0 bit */
534      else
535      {
536         tape_bits <<= 2;
537         switch (in_sync)
538         {
539            case 0:       /* look for sync AA */
540               if( (tape_bits & 0xffff) == 0xcccc )
541               {
542                  popmessage("Tape sync1 written");
543                  in_sync = 1;
544               }
545               break;
546            case 1:       /* look for sync 66 */
547               if( (tape_bits & 0xffff) == 0x3c3c )
548               {
549                  popmessage("Tape sync2 written");
550                  in_sync = 2;
551                  put_bit_count = 16;
552               }
553               break;
554            case 2:       /* count 2 bits */
555               put_bit_count += 2;
556         }
557      }
558
559      logerror("%4d %4d %d bits %04X\n", diff, limit, in_sync, tape_bits & 0xffff);
560
561      /* collected 8 sync plus 8 data bits ? */
562      if( put_bit_count >= 16 )
563      {
564         /* extract data bits to value */
565         value = 0;
566         if( tape_bits & 0x8000 )
567            value |= 0x80;
568         if( tape_bits & 0x2000 )
569            value |= 0x40;
570         if( tape_bits & 0x0800 )
571            value |= 0x20;
572         if( tape_bits & 0x0200 )
573            value |= 0x10;
574         if( tape_bits & 0x0080 )
575            value |= 0x08;
576         if( tape_bits & 0x0020 )
577            value |= 0x04;
578         if( tape_bits & 0x0008 )
579            value |= 0x02;
580         if( tape_bits & 0x0002 )
581            value |= 0x01;
582         put_bit_count -= 16;
583         tape_bits = 0;
584         tape_put_byte(value);
585      }
586   }
587/* remember the cycle count of this write */
588   put_cycles = now_cycles;
589}
590
591/*******************************************************************
592 * tape_get_byte
593 * read next byte from input tape image file.
594 * the first 32 bytes are faked to be sync header AA.
595 *******************************************************************/
596static void tape_get_byte(void)
597{
598   UINT8 value;
599
600   if( tape_get_file )
601   {
602      if( tape_count < 32 )
603      {
604         popmessage("Tape load sync1");
605         value = 0xaa;
606      }
607      else
608      {
609         popmessage("Tape load '%s' $%04X bytes", tape_name, tape_count);
610         mame_fread(tape_get_file, &value, 1);
611      }
612      tape_bits |= 0xaaaa;
613      if( value & 0x80 )
614         tape_bits ^= 0x4000;
615      if( value & 0x40 )
616         tape_bits ^= 0x1000;
617      if( value & 0x20 )
618         tape_bits ^= 0x0400;
619      if( value & 0x10 )
620         tape_bits ^= 0x0100;
621      if( value & 0x08 )
622         tape_bits ^= 0x0040;
623      if( value & 0x04 )
624         tape_bits ^= 0x0010;
625      if( value & 0x02 )
626         tape_bits ^= 0x0004;
627      if( value & 0x01 )
628         tape_bits ^= 0x0001;
629      get_bit_count = 16;
630      tape_count++;
631   }
632}
633
634/*******************************************************************
635 * tape_get_open
636 * Open a virtual tape image file for input. Look for a special
637 * header and skip leading description, if present.
638 * The filename is taken from BASIC input buffer at 41E8 ff.
639 *******************************************************************/
640static void tape_get_open(running_machine *machine)
641{
642   if( !tape_get_file )
643   {
644      char buffer[sizeof(TAPE_HEADER)];
645      UINT8 *ram = memory_region(machine, "main");
646      char *p;
647
648      sprintf(tape_name, "%-6.6s", ram + 0x41e8);
649      p = strchr(tape_name, ' ');
650      if( p ) *p = '\0';
651      strcat(tape_name, ".cas");
652      if (tape_name[0] != ' ')
653      {
654         logerror("tape_get_open '%s'\n", tape_name);
655         mame_fopen(SEARCHPATH_IMAGE, tape_name, OPEN_FLAG_READ, &tape_get_file);
656      }
657      if( tape_get_file )
658      {
659         popmessage("Tape load '%s'", tape_name);
660         mame_fread(tape_get_file, buffer, sizeof(TAPE_HEADER));
661         if( strncmp(buffer, TAPE_HEADER, sizeof(TAPE_HEADER) - 1) == 0 )
662         {
663            UINT8 data;
664            /* skip data until zero byte */
665            do
666            {
667               mame_fread(tape_get_file, &data, 1);
668            } while( data );
669         }
670         else
671         {
672            /* seek back to start of tape */
673            mame_fseek(tape_get_file, 0, SEEK_SET);
674         }
675      }
676      tape_count = 0;
677   }
678}
679
680static void tape_get_bit(running_machine *machine)
681{
682   int now_cycles = activecpu_gettotalcycles();
683   int limit = 10 * memory_region(machine, "main")[0x4312];
684   int diff = now_cycles - get_cycles;
685
686   /* overrun since last read ? */
687   if( diff >= 4000 )
688   {
689      if( tape_get_file )
690      {
691         mame_fclose(tape_get_file);
692         tape_get_file = NULL;
693         popmessage("Tape file closed");
694      }
695      get_bit_count = tape_bits = tape_time = 0;
696   }
697   else
698   /* check what he will get for input */
699   {
700      /* count down cycles */
701      tape_time -= diff;
702
703      /* time for the next sync or data bit ? */
704      if( tape_time <= 0 )
705      {
706         /* approx time for a bit */
707         tape_time += limit;
708         /* need to read get new data ? */
709         if( --get_bit_count <= 0 )
710         {
711            tape_get_open(machine);
712            tape_get_byte();
713         }
714         /* shift next sync or data bit to bit 16 */
715         tape_bits <<= 1;
716         if( tape_bits & 0x10000 )
717            port_ff ^= 1;
718      }
719   }
720   /* remember the cycle count of this read */
721   get_cycles = now_cycles;
722}
723
724306/*************************************
725307 *
726308 *            Port handlers.
r3347r3348
742324{
743325   int port_ff_changed = port_ff ^ data;
744326
745   if( port_ff_changed & FF_CAS )   /* casette port changed ? */
746   {
747      /* virtual tape ? */
748      if( input_port_read(machine, "DSW0") & 0x08 )
749         tape_put_bit(machine);
750      else
751         dac_data_w(0,(data & FF_CAS) ? 127:0 );
752   }
327   cassette_output ( device_list_find_by_tag( machine->config->devicelist, CASSETTE, "cassette" ), data & 0x01 ? -1.0 : 1.0 );
753328
754329   /* background bits changed ? */
755330   if( port_ff_changed & FF_BGD )
r3347r3348
816391   port_ff = data;
817392}
818393
394
819395READ8_HANDLER( cgenie_port_ff_r )
820396{
821   /* virtual tape ? */
397   UINT8   data = port_ff & ~0x01;
822398
823   if( input_port_read(machine, "DSW0") & 0x08 )
824      tape_get_bit(machine);
399   data |= cass_bit;
825400
826   return port_ff;
401   return data;
827402}
828403
829404int cgenie_port_xx_r( int offset )
src/mess/drivers/cgenie.c
r3347r3348
3030#include "includes/cgenie.h"
3131#include "devices/basicdsk.h"
3232#include "devices/cartslot.h"
33#include "devices/cassette.h"
3334#include "sound/ay8910.h"
3435
3536static ADDRESS_MAP_START (cgenie_mem, ADDRESS_SPACE_PROGRAM, 8)
r3347r3348
7576   PORT_DIPNAME( 0x10, 0x10, "Video Display accuracy") PORT_CODE(KEYCODE_F5) PORT_TOGGLE
7677   PORT_DIPSETTING(   0x10, "TV set" )
7778   PORT_DIPSETTING(   0x00, "RGB monitor" )
78   PORT_DIPNAME( 0x08, 0x08, "Virtual tape support") PORT_CODE(KEYCODE_F6) PORT_TOGGLE
79   PORT_DIPSETTING(   0x00, DEF_STR( Off ) )
80   PORT_DIPSETTING(   0x08, DEF_STR( On ) )
81   PORT_BIT(0x07, 0x07, IPT_UNUSED)
79   PORT_BIT(0x0f, 0x0f, IPT_UNUSED)
8280
8381/**************************************************************************
8482   +-------------------------------+     +-------------------------------+
r3347r3348
423421   MDRV_SOUND_ADD("ay8910", AY8910, 2000000)
424422   MDRV_SOUND_CONFIG(cgenie_ay8910_interface)
425423   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
424
425   MDRV_CASSETTE_ADD( "cassette", default_cassette_config )
426426MACHINE_DRIVER_END
427427
428428/***************************************************************************
r3347r3348
463463   }
464464}
465465
466static void cgenie_cassette_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info)
467{
468   /* cassette */
469   switch(state)
470   {
471      /* --- the following bits of info are returned as 64-bit signed integers --- */
472      case MESS_DEVINFO_INT_TYPE:                     info->i = IO_CASSETTE; break;
473      case MESS_DEVINFO_INT_READABLE:                  info->i = 0;   /* INVALID */ break;
474      case MESS_DEVINFO_INT_WRITEABLE:                  info->i = 0;   /* INVALID */ break;
475      case MESS_DEVINFO_INT_CREATABLE:                  info->i = 0;   /* INVALID */ break;
476      case MESS_DEVINFO_INT_COUNT:                     info->i = 1; break;
477466
478      /* --- the following bits of info are returned as pointers to data or functions --- */
479      case MESS_DEVINFO_PTR_LOAD:                     info->load = DEVICE_IMAGE_LOAD_NAME(cgenie_cassette); break;
480
481      /* --- the following bits of info are returned as NULL-terminated strings --- */
482      case MESS_DEVINFO_STR_FILE_EXTENSIONS:            strcpy(info->s = device_temp_str(), "cas"); break;
483   }
484}
485
486467static SYSTEM_CONFIG_START(cgenie)
487468   CONFIG_DEVICE(cartslot_device_getinfo)
488469   CONFIG_DEVICE(cgenie_floppy_getinfo)
489   CONFIG_DEVICE(cgenie_cassette_getinfo)
490470   CONFIG_RAM_DEFAULT   (16 * 1024)
491471   CONFIG_RAM         (32 * 1024)
492472SYSTEM_CONFIG_END

Previous 15994 Revisions Next


© 1998-2018 The MESS Team