Previous 15994 Revisions Next

r4597 Friday 1st May, 2009 at 18:35:48 UTC by Wilbert Pol
- [TI99 family] Added functionality for multi-cartridge handling. Added
support for complex/multi-file cartridge images. [Michael Zapf]
[docs]messnew.txt
[src/mess]image.c image.h mess.mak
[src/mess/devices]cartslot.c cartslot.h multcart.c multcart.h ti99cart.c* ti99cart.h*
[src/mess/drivers]ti99_4p.c ti99_4x.c ti99_8.c
[src/mess/machine]ti99_4x.c ti99_4x.h

src/mess/image.c
r4596r4597
17531753****************************************************************************/
17541754
17551755/*-------------------------------------------------
1756    open_battery_file - opens the battery backed
1756    open_battery_file_by_name - opens the battery backed
17571757   NVRAM file for an image
17581758-------------------------------------------------*/
17591759
1760static file_error open_battery_file(const device_config *image, UINT32 openflags, mame_file **file)
1760static file_error open_battery_file_by_name(const char *filename, UINT32 openflags, mame_file **file)
17611761{
17621762   file_error filerr;
1763   char *basename_noext;
1764   astring *fname;
1765
1766   basename_noext = strip_extension(image_basename(image));
1767   if (!basename_noext)
1768      return FILERR_OUT_OF_MEMORY;
1769   fname = astring_assemble_4(astring_alloc(), image->machine->gamedrv->name, PATH_SEPARATOR, basename_noext, ".nv");
1770   filerr = mame_fopen(SEARCHPATH_NVRAM, astring_c(fname), openflags, file);
1771   astring_free(fname);
1772   free(basename_noext);
1763   filerr = mame_fopen(SEARCHPATH_NVRAM, filename, openflags, file);
17731764   return filerr;
17741765}
17751766
1776
1777
17781767/*-------------------------------------------------
1779    image_battery_load - retrieves the battery
1780   backed RAM for an image
1768    image_battery_load_by_name - retrieves the battery
1769   backed RAM for an image. A filename may be supplied
1770   to the function.
17811771-------------------------------------------------*/
17821772
1783void image_battery_load(const device_config *image, void *buffer, int length)
1773void image_battery_load_by_name(const char *filename, void *buffer, int length)
17841774{
17851775   file_error filerr;
17861776   mame_file *file;
r4596r4597
17891779   assert_always(buffer && (length > 0), "Must specify sensical buffer/length");
17901780
17911781   /* try to open the battery file and read it in, if possible */
1792   filerr = open_battery_file(image, OPEN_FLAG_READ, &file);
1782   filerr = open_battery_file_by_name(filename, OPEN_FLAG_READ, &file);
17931783   if (filerr == FILERR_NONE)
17941784   {
17951785      bytes_read = mame_fread(file, buffer, length);
r4596r4597
18011791}
18021792
18031793
1804
18051794/*-------------------------------------------------
1806    image_battery_save - stores the battery
1807   backed RAM for an image
1795    image_battery_load - retrieves the battery
1796   backed RAM for an image. The file name is
1797   created from the machine driver name and the
1798   image name.
18081799-------------------------------------------------*/
18091800
1810void image_battery_save(const device_config *image, const void *buffer, int length)
1801void image_battery_load(const device_config *image, void *buffer, int length)
18111802{
1803   char *basename_noext;
1804   astring *fname;
1805
1806   basename_noext = strip_extension(image_basename(image));
1807   if (!basename_noext)
1808      fatalerror("FILERR_OUT_OF_MEMORY");
1809
1810   fname = astring_assemble_4(astring_alloc(), image->machine->gamedrv->name, PATH_SEPARATOR, basename_noext, ".nv");
1811
1812   image_battery_load_by_name(astring_c(fname), buffer, length);
1813   astring_free(fname);
1814   free(basename_noext);
1815}
1816
1817
1818/*-------------------------------------------------
1819    image_battery_save_by_name - stores the battery
1820   backed RAM for an image. A filename may be supplied
1821   to the function.
1822-------------------------------------------------*/
1823void image_battery_save_by_name(const char *filename, const void *buffer, int length)
1824{
18121825   file_error filerr;
18131826   mame_file *file;
18141827
18151828   assert_always(buffer && (length > 0), "Must specify sensical buffer/length");
18161829
18171830   /* try to open the battery file and write it out, if possible */
1818   filerr = open_battery_file(image, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &file);
1831   filerr = open_battery_file_by_name(filename, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &file);
18191832   if (filerr == FILERR_NONE)
18201833   {
18211834      mame_fwrite(file, buffer, length);
r4596r4597
18231836   }
18241837}
18251838
1839/*-------------------------------------------------
1840    image_battery_save - stores the battery
1841   backed RAM for an image. The file name is
1842   created from the machine driver name and the
1843   image name.
1844-------------------------------------------------*/
18261845
1846void image_battery_save(const device_config *image, const void *buffer, int length)
1847{
1848   astring *fname;
1849   char *basename_noext;
18271850
1851   basename_noext = strip_extension(image_basename(image));
1852   if (!basename_noext)
1853      fatalerror("FILERR_OUT_OF_MEMORY");
1854
1855   fname = astring_assemble_4(astring_alloc(), image->machine->gamedrv->name, PATH_SEPARATOR, basename_noext, ".nv");
1856
1857   image_battery_save_by_name(astring_c(fname), buffer, length);
1858   astring_free(fname);
1859   free(basename_noext);
1860}
1861
1862
1863
18281864/****************************************************************************
18291865  Indexing functions
18301866
src/mess/image.h
r4596r4597
302302  image; typically for cartridges.
303303****************************************************************************/
304304
305void image_battery_load_by_name(const char *filename, void *buffer, int length);
306void image_battery_save_by_name(const char *filename, const void *buffer, int length);
307
305308void image_battery_load(const device_config *img, void *buffer, int length);
306309void image_battery_save(const device_config *img, const void *buffer, int length);
307310
308311
309
310312/****************************************************************************
311313  Indexing functions
312314
src/mess/machine/ti99_4x.c
r4596r4597
221221   The p-code card (-> UCSD Pascal system) contains 8 GROMs, all in port 16.  This port is not
222222   in the range recognized by the TI ROMs (0-15), and therefore it is used by the p-code DSR ROMs
223223   as custom data.  Additionally, some hackers used the extra ports to implement "GRAM" devices.
224
225   The handling of cartridge GROMs has moved to ti99cart.c.   
224226*/
225/* GROM_port_t: descriptor for a port of 8 GROMs */
226typedef struct GROM_port_t
227{
228   /* pointer to GROM data */
229   UINT8 *data_ptr;
230   /* current address pointer for the active GROM in port (16 bits) */
231   unsigned int addr;
232   /* GROM data buffer */
233   UINT8 buf;
234   /* internal flip-flops that are set after the first access to the GROM
235   address so that next access is mapped to the LSB, and cleared after each
236   data access */
237   char raddr_LSB, waddr_LSB;
238} GROM_port_t;
239227
240228/* descriptor for console GROMs */
241static GROM_port_t console_GROMs;
229GROM_port_t console_GROMs;
242230
243231/* true if hsgpl is enabled (i.e. has_hsgpl is true and hsgpl cru bit crdena is
244232set) */
245233static char hsgpl_crdena;
246234
247/*
248   Cartridge support
249*/
250/* pointer on two 8kb cartridge pages */
251static UINT16 *cartridge_pages[2] = {NULL, NULL};
252static UINT8 *cartridge_pages_8[2] = {NULL, NULL};
253/* flag: TRUE if the cartridge is minimemory (4kb of battery-backed SRAM in 0x5000-0x5fff) */
254static char cartridge_minimemory = FALSE;
255/* flag: TRUE if the cartridge is paged */
256static char cartridge_paged = FALSE;
257/* flag: TRUE if the cartridge is MBX-style */
258static char cartridge_mbx = FALSE;
259/* flag on the data for the current page (cartridge_pages[0] if cartridge is not paged) */
260static UINT16 *current_page_ptr;
261static UINT8 *current_page_ptr_8;
262/* keep track of cart file types - required for cleanup... */
263typedef enum _slot_type_t { SLOT_EMPTY = -1, SLOT_GROM = 0, SLOT_CROM = 1, SLOT_DROM = 2, SLOT_MINIMEM = 3, SLOT_MBX = 4 } slot_type_t;
264static slot_type_t slot_type[3] = { SLOT_EMPTY, SLOT_EMPTY, SLOT_EMPTY};
265
266235/* true if 99/4p rom6 is enabled */
267236static int ti99_4p_internal_rom6_enable;
268237/* pointer to the ROM6 data */
269238static UINT16 *ti99_4p_internal_ROM6;
270239
240/* Pointer to the cartridge system for the 99/8 */
241static const device_config *multicart8;
271242
272243/* ti99/8 hardware */
273static UINT8 ti99_8_CRUS;
244static UINT8 ti99_8_enable_rom_and_ports;
274245
275246static UINT32 ti99_8_mapper_regs[16];
276247static UINT8 ti99_8_mapper_status;
277enum
278{
279   ms_WPE = 0x80,   /* Write-Protect Error */
280   ms_XCE = 0x40,   /* eXeCute Error */
281   ms_RPE = 0x20   /* Read-Protect Error */
282};
283248
284249static UINT8 *xRAM_ptr_8;
285250static UINT8 *ROM0_ptr_8;
r4596r4597
292257/* tms9995_ICount: used to implement memory waitstates (hack) */
293258/* NPW 23-Feb-2004 - externs no longer needed because we now use cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),) */
294259
260/* Link to the cartridge system. */
261const device_config *cartslots;
295262
296
297263/*===========================================================================*/
298264/*
299265   General purpose code:
300266   initialization, cart loading, etc.
301267*/
302268
303DRIVER_INIT( ti99_4 )
269static void create_grom_high2k(int first, int last)
304270{
305   int i, j;
271        /* Generate missing chunk of each console GROMs. Although a GROM
272           occupies 8 KiB of address space, it only has 6 KiB capacity, and
273           the last 2 KiB mirror previous content as shown above. */
274        int base;
275        for (base=(first&0xe000); base<((last+1)&0xe000); base+=0x2000)
276        {
277                int j;
278                for (j=0; j<0x800; j++)
279                {
280                        console_GROMs.data_ptr[base+0x1800+j] =
281                                  console_GROMs.data_ptr[base+0x0800+j]
282                                | console_GROMs.data_ptr[base+0x1000+j];
283                }
284        }
285}
306286
287int is_99_8()
288{
289   return ti99_model == model_99_8;
290}
307291
292DRIVER_INIT( ti99_4 )
293{
308294   ti99_model = model_99_4;
309295   has_evpc = FALSE;
310296
311297   /* set up memory pointers */
312298   xRAM_ptr = (UINT16 *) (memory_region(machine, "maincpu") + offset_xram);
313299   console_GROMs.data_ptr = memory_region(machine, region_grom);
314
315   /* Generate missing chunk of each console GROMs */
316   for (i=0; i<2; i++)
317      for (j=0; j<0x800; j++)
318      {
319         console_GROMs.data_ptr[0x2000*i+0x1800+j] = console_GROMs.data_ptr[0x2000*i+0x0800+j]
320                                             | console_GROMs.data_ptr[0x2000*i+0x1000+j];
321      }
300   create_grom_high2k(0x0000, 0x5fff);
322301}
323302
324303DRIVER_INIT( ti99_4a )
r4596r4597
329308   /* set up memory pointers */
330309   xRAM_ptr = (UINT16 *) (memory_region(machine, "maincpu") + offset_xram);
331310   console_GROMs.data_ptr = memory_region(machine, region_grom);
311   create_grom_high2k(0x0000, 0x5fff);
332312}
333313
334314DRIVER_INIT( ti99_4ev )
r4596r4597
339319
340320   /* set up memory pointers */
341321   xRAM_ptr = (UINT16 *) (mem + offset_xram);
342   cartridge_pages[0] = (UINT16 *) (mem+offset_cart);
343   cartridge_pages[1] = (UINT16 *) (mem+offset_cart + 0x2000);
344322   console_GROMs.data_ptr = memory_region(machine, region_grom);
323   create_grom_high2k(0x0000, 0x5fff);
345324}
346325
347326DRIVER_INIT( ti99_8 )
r4596r4597
349328   UINT8 *mem = memory_region(machine, "maincpu");
350329   ti99_model = model_99_8;
351330   has_evpc = FALSE;
352
331   
353332   /* set up memory pointers */
354333   xRAM_ptr_8 = mem + offset_xram_8;
355334   ROM0_ptr_8 = mem + offset_rom0_8;
r4596r4597
358337   ROM3_ptr_8 = ROM2_ptr_8 + 0x2000;
359338   sRAM_ptr_8 = mem + offset_sram_8;
360339
361   cartridge_pages_8[0] = mem+offset_cart_8;
362   cartridge_pages_8[1] = mem+offset_cart_8 + 0x2000;
340   /* Pull this one up here. */
341   ti99_8_enable_rom_and_ports = 1;
342   
363343   console_GROMs.data_ptr = memory_region(machine, region_grom);
344   create_grom_high2k(0x0000, 0x5fff);
345
346   multicart8 = devtag_get_device(machine, "ti99_multicart");
347   assert (multicart8 != NULL);
364348}
365349
366350DRIVER_INIT( ti99_4p )
r4596r4597
371355   /* set up memory pointers */
372356   xRAM_ptr = (UINT16 *) (memory_region(machine, "maincpu") + offset_xram_4p);
373357   /*console_GROMs.data_ptr = memory_region(machine, region_grom);*/
358   create_grom_high2k(0x0000, 0x5fff);
374359}
375360
376DEVICE_START( ti99_cart )
377{
378   int id = 0;
379   if (strcmp(device->tag,"cart1")==0) {
380      id = 0;
381   }
382   if (strcmp(device->tag,"cart2")==0) {
383      id = 1;
384   }
385   if (strcmp(device->tag,"cart3")==0) {
386      id = 2;
387   }
388   
389   cartridge_pages[id] = (UINT16 *) (memory_region(device->machine, "maincpu") + offset_cart + (id * 0x2000));
390}
391
392/*
393   Load ROM.  All files are in raw binary format.
394   1st ROM: GROM (up to 40kb)
395   2nd ROM: CPU ROM (8kb)
396   3rd ROM: CPU ROM, 2nd page (8kb)
397
398   We don't need to support 99/4p, as it has no cartridge port.
399*/
400DEVICE_IMAGE_LOAD( ti99_cart )
401{
402   /* Trick - we identify file types according to their extension */
403   /* Note that if we do not recognize the extension, we revert to the slot location <-> type
404   scheme.  I do this because the extension concept is quite unfamiliar to mac people
405   (I am dead serious). */
406   /* Original idea by Norberto Bensa */
407   const char *name = image_filename(image);
408   const char *ch, *ch2;
409   int id = 0;
410   int i;
411   slot_type_t type;
412   
413   if (strcmp(image->tag,"cart1")==0) {
414      id = 0;
415   }
416   if (strcmp(image->tag,"cart2")==0) {
417      id = 1;
418   }
419   if (strcmp(image->tag,"cart3")==0) {
420      id = 2;
421   }   
422   type = (slot_type_t) id;
423
424   /* There is a circuitry in TI99/4(a) that resets the console when a
425   cartridge is inserted or removed.  We emulate this instead of resetting the
426   emulator (which is the default in MESS). */
427   /*cputag_set_input_line(machine, "maincpu", INPUT_LINE_RESET, PULSE_LINE);
428   tms9901_reset(0);
429   if (! has_evpc)
430      TMS9928A_reset();
431   if (has_evpc)
432      v9938_reset(0);*/
433
434   ch = strrchr(name, '.');
435   ch2 = (ch-1 >= name) ? ch-1 : "";
436
437   if (ch)
438   {
439      if ((! mame_stricmp(ch2, "g.bin")) || (! mame_stricmp(ch, ".grom")) || (! mame_stricmp(ch, ".g")))
440      {
441         /* grom */
442         type = SLOT_GROM;
443      }
444      else if ((! mame_stricmp(ch2, "c.bin")) || (! mame_stricmp(ch, ".crom")) || (! mame_stricmp(ch, ".c")))
445      {
446         /* rom first page */
447         type = SLOT_CROM;
448      }
449      else if ((! mame_stricmp(ch2, "d.bin")) || (! mame_stricmp(ch, ".drom")) || (! mame_stricmp(ch, ".d")))
450      {
451         /* rom second page */
452         type = SLOT_DROM;
453      }
454      else if ((! mame_stricmp(ch2, "m.bin")) || (! mame_stricmp(ch, ".mrom")) || (! mame_stricmp(ch, ".m")))
455      {
456         /* rom minimemory  */
457         type = SLOT_MINIMEM;
458      }
459      else if ((! mame_stricmp(ch2, "b.bin")) || (! mame_stricmp(ch, ".brom")) || (! mame_stricmp(ch, ".b")))
460      {
461         /* rom MBX  */
462         type = SLOT_MBX;
463      }
464   }
465
466   slot_type[id] = type;
467
468   switch (type)
469   {
470   case SLOT_EMPTY:
471      break;
472
473   case SLOT_GROM:
474      image_fread(image, memory_region(image->machine, region_grom) + 0x6000, 0xA000);
475      break;
476
477   case SLOT_MINIMEM:
478      cartridge_minimemory = TRUE;
479      /* Load the NVRAM. Need to BIG_ENDIANIZE it.
480         MiniMemory has only one cartridge page. */
481      image_battery_load(image, cartridge_pages[0]+0x800,0x1000);
482      for (i = 0x800; i < 0x1000; i++)
483         cartridge_pages[0][i] = BIG_ENDIANIZE_INT16(cartridge_pages[0][i]);
484      break;
485   case SLOT_MBX:
486      cartridge_mbx = TRUE;
487      break;
488   case SLOT_CROM:
489      if (ti99_model == model_99_8)
490      {
491         if (cartridge_minimemory)
492            /* Only load 4K so we don't overwrite the NVRAM. */
493            image_fread(image, cartridge_pages_8[0], 0x1000);
494         else
495            image_fread(image, cartridge_pages_8[0], 0x2000);
496         current_page_ptr_8 = cartridge_pages_8[0];
497      }
498      else
499      {
500          if (cartridge_minimemory)
501          {
502         /* Only load 4K so we don't overwrite the NVRAM that
503            has been loaded already. */
504         image_fread(image, cartridge_pages[0], 0x1000);
505         for (i = 0; i < 0x0800; i++)
506            cartridge_pages[0][i] = BIG_ENDIANIZE_INT16(cartridge_pages[0][i]);
507          }
508          else
509          {
510         image_fread(image, cartridge_pages[0], 0x2000);
511         for (i = 0; i < 0x1000; i++)
512            cartridge_pages[0][i] = BIG_ENDIANIZE_INT16(cartridge_pages[0][i]);
513          }
514          current_page_ptr = cartridge_pages[0];
515      }
516      break;
517
518   case SLOT_DROM:
519      cartridge_paged = TRUE;
520      if (ti99_model == model_99_8)
521      {
522         image_fread(image, cartridge_pages_8[1], 0x2000);
523         current_page_ptr_8 = cartridge_pages_8[0];
524      }
525      else
526      {
527         image_fread(image, cartridge_pages[1], 0x2000);
528         for (i = 0; i < 0x1000; i++)
529            cartridge_pages[1][i] = BIG_ENDIANIZE_INT16(cartridge_pages[1][i]);
530         current_page_ptr = cartridge_pages[0];
531      }
532      break;
533   }
534
535   return INIT_PASS;
536}
537
538DEVICE_IMAGE_UNLOAD( ti99_cart )
539{
540   int i;
541   int id = 0;
542   if (strcmp(image->tag,"cart1")==0) {
543      id = 0;
544   }
545   if (strcmp(image->tag,"cart2")==0) {
546      id = 1;
547   }
548   if (strcmp(image->tag,"cart3")==0) {
549      id = 2;
550   }     
551   /* There is a circuitry in TI99/4(a) that resets the console when a
552   cartridge is inserted or removed.  We emulate this instead of resetting the
553   emulator (which is the default in MESS). */
554   /*cputag_set_input_line(machine, "maincpu", INPUT_LINE_RESET, PULSE_LINE);
555   tms9901_reset(0);
556   if (! has_evpc)
557      TMS9928A_reset();
558   if (has_evpc)
559      v9938_reset(0);*/
560
561   switch (slot_type[id])
562   {
563   case SLOT_EMPTY:
564      break;
565
566   case SLOT_GROM:
567      memset(memory_region(image->machine, region_grom) + 0x6000, 0, 0xA000);
568      break;
569
570   case SLOT_MINIMEM:
571      cartridge_minimemory = FALSE;
572      if (ti99_model == model_99_8)
573      {
574         image_battery_save(image, cartridge_pages_8[0] + 0x1000, 0x1000);
575         memset(cartridge_pages_8[0] + 0x1000, 0, 0x1000);
576      }
577      else
578      {
579         /* We BIG_ENDIANIZE before saving. This is consistent
580            with the cartridge save format. */
581         for (i = 0x800; i < 0x1000; i++)
582            cartridge_pages[0][i] = BIG_ENDIANIZE_INT16(cartridge_pages[0][i]);
583         image_battery_save(image, cartridge_pages[0] + 0x800, 0x1000);
584         memset(cartridge_pages[0] + 0x800, 0, 0x1000);
585      }
586      break;
587   case SLOT_MBX:
588      if (slot_type[id] == SLOT_MBX)
589         cartridge_mbx = FALSE;
590         /* maybe we should insert some code to save the memory contents... */
591                break;
592   case SLOT_CROM:
593      if (ti99_model == model_99_8)
594      {
595         if (cartridge_minimemory)
596            /* Don't wipe the RAM before it is saved. */
597            memset(cartridge_pages_8[0], 0, 0x1000);
598         else
599            memset(cartridge_pages_8[0], 0, 0x2000);
600      }
601      else
602      {
603         if (cartridge_minimemory)
604            memset(cartridge_pages[0], 0, 0x1000);
605         else
606            memset(cartridge_pages[0], 0, 0x2000);
607      }
608      break;
609
610   case SLOT_DROM:
611      cartridge_paged = FALSE;
612      if (ti99_model == model_99_8)
613      {
614         current_page_ptr_8 = cartridge_pages_8[0];
615      }
616      else
617      {
618         current_page_ptr = cartridge_pages[0];
619      }
620      break;
621   }
622
623   slot_type[id] = SLOT_EMPTY;
624}
625
626361static const TMS9928a_interface tms9918_interface =
627362{
628363   TMS99x8,
r4596r4597
683418
684419void ti99_common_init(running_machine *machine, const TMS9928a_interface *gfxparm)
685420{
686   if (gfxparm!=0) TMS9928A_configure(gfxparm);
421   if (gfxparm != 0)
422      TMS9928A_configure(gfxparm);
687423
688424        /* Initialize all. Actually, at this point, we don't know
689425           how the switches are set. Later we use the configuration switches to
r4596r4597
694430   ti99_rs232_init(machine);
695431   ti99_hsgpl_init(machine);
696432   ti99_usbsm_init(machine);
433   
434   /* Find the cartslot device and cache it. This is a string search,
435      and we don't want to repeat it on each memory access. */
436   cartslots = devtag_get_device(machine, "ti99_multicart");
437   assert(cartslots != NULL);
697438}
698439
699440
r4596r4597
709450   if (ti99_model == model_99_8)
710451   {
711452      /* ... */
712      ti99_8_CRUS = 1;      /* ??? maybe there is a pull-up */
453      ti99_8_enable_rom_and_ports = 1;      /* ??? maybe there is a pull-up */
454      /* MZ: already setting this in driver init, otherwise machine locks up */
713455   }
714456   else if (ti99_model == model_99_4p)
715457   {
r4596r4597
726468      memory_set_bankptr(machine, 1, memory_region(machine, "maincpu") + offset_sram);
727469   }
728470
729   if (ti99_model != model_99_4p)
730   {   /* reset cartridge mapper */
471/*   if (ti99_model != model_99_4p)
472   {
731473      if (ti99_model == model_99_8)
732474         current_page_ptr_8 = cartridge_pages_8[0];
733475      else
734476         current_page_ptr = cartridge_pages[0];
735   }
477   } */
736478
737479   if (!has_evpc) TMS9928A_reset();
738480        else v9938_reset(0);
r4596r4597
775517   if (ti99_model == model_99_4p)
776518      ti99_4p_internal_dsr_reset(machine);
777519
778        if (has_speech)
520        if (has_speech)
779521   {
780522      static const spchroms_interface speech_intf = { region_speech_rom };
781523
r4596r4597
789531   }
790532   else
791533   {
792      if (ti99_model != model_99_8)
793      {
794         memory_install_read16_handler(space, 0x9000, 0x93ff, 0, 0, ti99_nop_8_r);
795         memory_install_write16_handler(space, 0x9400, 0x97ff, 0, 0, ti99_nop_8_w);
796      }
534      /* which is never true for TI-99/8 */
535      memory_install_read16_handler(space, 0x9000, 0x93ff, 0, 0, ti99_nop_8_r);
536      memory_install_write16_handler(space, 0x9400, 0x97ff, 0, 0, ti99_nop_8_w);
797537   }
538   /* Check whether we have locked the cartslot. */
539   lock_cartridge_slot(cartslots, (input_port_read(machine, "CFG") >> config_cartslot_bit) & config_cartslot_mask);
798540
799541   switch (xRAM_kind)
800542   {
r4596r4597
953695READ16_HANDLER ( ti99_nop_8_r )
954696{
955697   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
698//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
956699
957700   return (0);
958701}
r4596r4597
960703WRITE16_HANDLER ( ti99_nop_8_w )
961704{
962705   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
706//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
963707}
964708
965709/*
966   TI-99 cartridge port: attached to the 8-bit bus.
967
968   Cartridges are usually made of ROM, through a few cartridges include some
969   RAM as well.  Bank switching is common: I know three banking schemes,
970   namely TI, MBX and Supercart.  The TI scheme writes a dummy value in
971   cartridge space, and the write address gives the bank number.  The MBX
972   scheme has a cartridge bank register at 0x6fff.  I think the Supercart
973   scheme has a cartridge bank register in CRU space, but I don't what the
974   register address is, and it is therefore not emulated.
975
976   HSGPL maps here, too, in order to emulate cartridge ROM/RAM.
977*/
978READ16_HANDLER ( ti99_cart_r )
979{
980   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
981
982   if (hsgpl_crdena)
983      /* hsgpl is enabled */
984      return ti99_hsgpl_rom6_r(space, offset, mem_mask);
985
986   if (cartridge_mbx && (offset >= 0x0600) && (offset <= 0x07fe))
987      return (cartridge_pages[0])[offset];
988
989   return current_page_ptr[offset];
990}
991
992WRITE16_HANDLER ( ti99_cart_w )
993{
994   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
995
996   if (hsgpl_crdena)
997      /* hsgpl is enabled */
998      ti99_hsgpl_rom6_w(space, offset, data, mem_mask);
999   else if (cartridge_minimemory && offset >= 0x800)
1000      /* handle minimem RAM */
1001      COMBINE_DATA(current_page_ptr+offset);
1002   else if (cartridge_mbx)
1003   {   /* handle MBX cart */
1004      /* RAM in 0x6c00-0x6ffd (presumably non-paged) */
1005      /* mapper at 0x6ffe */
1006      if ((offset >= 0x0600) && (offset <= 0x07fe))
1007         COMBINE_DATA(cartridge_pages[0]+offset);
1008      else if ((offset == 0x07ff) && ACCESSING_BITS_8_15)
1009         current_page_ptr = cartridge_pages[cartridge_paged ? ((data >> 8) & 1) : 0];
1010   }
1011   else if (cartridge_paged)
1012      /* handle pager */
1013      current_page_ptr = cartridge_pages[offset & 1];
1014}
1015
1016/*
1017710   99/4p cartridge space: mapped either to internal ROM6 or to HSGPL.
1018711*/
1019712READ16_HANDLER ( ti99_4p_cart_r )
r4596r4597
1022715      return ti99_4p_internal_ROM6[offset];
1023716
1024717   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
718//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
1025719
1026720   if (hsgpl_crdena)
1027721      /* hsgpl is enabled */
r4596r4597
1039733   }
1040734
1041735   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
736//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
1042737
1043738   if (hsgpl_crdena)
1044739      /* hsgpl is enabled */
r4596r4597
1081776WRITE16_HANDLER ( ti99_wsnd_w )
1082777{
1083778   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
779//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
1084780
1085781   sn76496_w(devtag_get_device(space->machine, "sn76496"), offset, (data >> 8) & 0xff);
1086782}
r4596r4597
1091787READ16_HANDLER ( ti99_rvdp_r )
1092788{
1093789   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
790//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
1094791
1095792   if (offset & 1)
1096793   {   /* read VDP status */
r4596r4597
1108805WRITE16_HANDLER ( ti99_wvdp_w )
1109806{
1110807   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
808//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
1111809
1112810   if (offset & 1)
1113811   {   /* write VDP address */
r4596r4597
1125823READ16_HANDLER ( ti99_rv38_r )
1126824{
1127825   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
826//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
1128827
1129828   if (offset & 1)
1130829   {   /* read VDP status */
r4596r4597
1142841WRITE16_HANDLER ( ti99_wv38_w )
1143842{
1144843   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
844//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
1145845
1146846   switch (offset & 3)
1147847   {
r4596r4597
1217917   tms5220_data_w(devtag_get_device(space->machine, "tms5220"), offset, (data >> 8) & 0xff);
1218918}
1219919
920static UINT8 GROM_dataread(void)
921{
922   UINT8 reply;
923   if (console_GROMs.addr >= 0x6000)
924   {
925      /* Pass the (one and only) program counter for GROMs. The
926         buffer is set and used in the cartridge chip. */
927      reply = cartridge_grom_read(cartslots, console_GROMs.addr-0x6000);
928   }
929   else
930   {
931      /* GROMs are buffered. Data is retrieved from a buffer,
932      while the buffer is replaced with the next cell
933      content. */
934      reply = console_GROMs.buf;
935     
936      /* Get next value, put it in buffer. Note that the
937      GROM wraps at 8K boundaries. */
938      console_GROMs.buf = console_GROMs.data_ptr[console_GROMs.addr];
939   }
940
941//   printf("GROM address = %04x, reply = %02x\n", console_GROMs.addr, reply>>8);
942   /* The program counter wraps at each GROM chip size (8K),
943      so 0x5fff + 1 = 0x4000. */
944   console_GROMs.addr = ((console_GROMs.addr + 1) & 0x1FFF) | (console_GROMs.addr & 0xE000);
945   
946   /* Reset the read and write address flipflops. */
947   console_GROMs.raddr_LSB = console_GROMs.waddr_LSB = FALSE;
948
949   return reply;
950}
951
1220952/*
1221953   GPL read
1222954*/
1223READ16_HANDLER ( ti99_rgpl_r )
955READ16_HANDLER ( ti99_grom_r )
1224956{
1225   int reply;
957   UINT16 reply;
1226958
1227
1228959   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4 /*20+3*/);      /* from 4 to 23? */
960//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
1229961
962   /* This implementation features a multislot cartridge system which
963      is based on multiple GROM base addresses. The standard base
964      is >9800 (slot 0). When we access the port >9804, we switch to
965      slot 1, >9808 is slot 2, ... >983C is slot 15. Although theoretically
966      we could address up to 256 banks, the TI operating system does not
967      check more than 16 banks.
968      Cartridges may also contain ROMs which need to be banked
969      simultaneously. We use the cartridge slot number to swap the ROM at
970      >6000, or RAM if available. I don't know whether this is the way
971      Texas Instruments envisaged it in the OS, but it is the only
972      plausible way to allow for GROM+ROM cartridges.
973      Note that some cartridges may have programming flaws which only
974      appear when the cartridge is used in a higher-numbered slot.
975      Parsec is one example which only runs in slot 0 as the
976      programmers hard-coded an access to GROM port 0. Note: This *may*
977      be worked around if we make sure that switching only occurs
978      when the address is set, not when data is read or written. But
979      it's not clear whether this has unwanted side effects.
980   */
981   
982   /* Activates a slot in the multi-cartridge extender. */
983   cartridge_slot_set(cartslots, (offset & 0x01fe)/2);
984   
1230985   if (offset & 1)
1231   {   /* read GPL address */
1232      /* the console GROMs are always affected */
1233      console_GROMs.waddr_LSB = FALSE;   /* right??? */
986   {   /* Read GROM address
987         We only have one GROM program counter for the console GROMs
988         and all cartridge GROMs in all banks(!). Hardware
989         implementations must take this into account as well. Thus,
990         we use a structure here in the console, rather than separate
991         counters in each GROM chip. */
992     
993      /* When reading, reset the hi/lo flag byte for writing.
994         TODO: Verify this with a real machine. */
995      console_GROMs.waddr_LSB = FALSE;
1234996
997      /* Address reading is done in two steps; first, the high byte
998         is transferred, then the low byte. */
1235999      if (console_GROMs.raddr_LSB)
12361000      {
1001         /* second pass */
12371002         reply = (console_GROMs.addr << 8) & 0xff00;
12381003         console_GROMs.raddr_LSB = FALSE;
12391004      }
12401005      else
12411006      {
1007         /* first pass */
12421008         reply = console_GROMs.addr & 0xff00;
12431009         console_GROMs.raddr_LSB = TRUE;
12441010      }
12451011   }
12461012   else
1247   {   /* read GPL data */
1248      /* the console GROMs are always affected */
1249      reply = ((int) console_GROMs.buf) << 8;   /* retreive buffer */
1250
1251      /* read ahead */
1252      console_GROMs.buf = console_GROMs.data_ptr[console_GROMs.addr];
1253      console_GROMs.addr = ((console_GROMs.addr + 1) & 0x1FFF) | (console_GROMs.addr & 0xE000);
1254      console_GROMs.raddr_LSB = console_GROMs.waddr_LSB = FALSE;
1013   {   
1014      /* Read GROM data */
1015      reply = ((UINT16)GROM_dataread()) << 8;     
12551016   }
12561017
12571018   if (hsgpl_crdena)
r4596r4597
12641025/*
12651026   GPL write
12661027*/
1267WRITE16_HANDLER ( ti99_wgpl_w )
1028WRITE16_HANDLER ( ti99_grom_w )
12681029{
12691030   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4/*20+3*/);      /* from 4 to 23? */
1031//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
12701032
1033   /* Activates a slot in the multi-cartridge extender. */
1034   cartridge_slot_set(cartslots, (offset & 0x01fe)/2);
1035
1036   // 1001 1wbb bbbb bbr0
1037   
12711038   if (offset & 1)
1272   {   /* write GPL address */
1273      /* the console GROMs are always affected */
1039   {   /* write GROM address */
1040      /* see comments above */
12741041      console_GROMs.raddr_LSB = FALSE;
1275
1042     
1043      /* Implements the internal flipflop.
1044         The Editor/Assembler manuals says that the current address
1045         plus one is returned. This effect is properly emulated
1046         as we are using a read-ahead buffer.
1047      */
12761048      if (console_GROMs.waddr_LSB)
12771049      {
1050         /* Accept low byte (2nd write) */
12781051         console_GROMs.addr = (console_GROMs.addr & 0xFF00) | ((data >> 8) & 0xFF);
1279
1280         /* read ahead */
1281         console_GROMs.buf = console_GROMs.data_ptr[console_GROMs.addr];
1282         console_GROMs.addr = ((console_GROMs.addr + 1) & 0x1FFF) | (console_GROMs.addr & 0xE000);
1283
1052         
1053         /* Setting the address causes a new read, putting the
1054            value into the buffer. We don't need the value here. */
1055         GROM_dataread();
12841056         console_GROMs.waddr_LSB = FALSE;
12851057      }
12861058      else
12871059      {
1060         /* Accept high byte (1st write) */
12881061         console_GROMs.addr = (data & 0xFF00) | (console_GROMs.addr & 0xFF);
1289
12901062         console_GROMs.waddr_LSB = TRUE;
12911063      }
12921064
12931065   }
12941066   else
1295   {   /* write GPL data */
1067   {
1068      /* write GROM data */
12961069      /* the console GROMs are always affected */
1297      /* BTW, console GROMs are never GRAMs, therefore there is no need to
1298      actually write anything */
1299      /* read ahead */
1300      console_GROMs.buf = console_GROMs.data_ptr[console_GROMs.addr];
1301      console_GROMs.addr = ((console_GROMs.addr + 1) & 0x1FFF) | (console_GROMs.addr & 0xE000);
1070      /* BTW, console GROMs are never GRAMs, therefore there is no
1071         need to actually write anything, so we just read ahead
1072         TODO: There could be cartridges with GRAM, so this must be
1073         fixed.
1074      */
1075      GROM_dataread();
13021076      console_GROMs.raddr_LSB = console_GROMs.waddr_LSB = FALSE;
1303
13041077   }
13051078
13061079   if (hsgpl_crdena)
r4596r4597
13081081}
13091082
13101083/*
1311   GPL read
1084   GROM read for TI-99/8
1085   For comments, see the handler for TI-99/4A
13121086*/
1313READ16_HANDLER ( ti99_4p_rgpl_r )
1087READ8_HANDLER ( ti99_grom_r8 )
13141088{
1089   UINT8 reply;
1090
1091   cpu_adjust_icount(space->machine->cpu[0],-4);
1092   
1093   /* Activates a slot in the multi-cartridge extender. */
1094   // 1001 1wbb bbbb bbr0
1095   cartridge_slot_set(cartslots, (offset & 0x03fc)/4);
1096   
1097   if (offset & 2)
1098   {   
1099      console_GROMs.waddr_LSB = FALSE;
1100
1101      if (console_GROMs.raddr_LSB)
1102      {
1103         reply = console_GROMs.addr & 0x00ff;
1104         console_GROMs.raddr_LSB = FALSE;
1105      }
1106      else
1107      {
1108         reply = (console_GROMs.addr>>8) & 0x00ff;
1109         console_GROMs.raddr_LSB = TRUE;
1110      }
1111   }
1112   else
1113      reply = GROM_dataread();     
1114
1115   return reply;
1116}
1117
1118/*
1119   GROM write for TI-99/8
1120   For comments, see the handler for TI-99/4A
1121*/
1122WRITE8_HANDLER ( ti99_grom_w8 )
1123{
1124   cpu_adjust_icount(space->machine->cpu[0],-4/*20+3*/);      /* from 4 to 23? */
1125
1126   /* Activates a slot in the multi-cartridge extender. */
1127   cartridge_slot_set(cartslots, (offset & 0x03fc)/4);
1128   
1129   if (offset & 2)
1130   {
1131      console_GROMs.raddr_LSB = FALSE;
1132      if (console_GROMs.waddr_LSB)
1133      {
1134         console_GROMs.addr = (console_GROMs.addr & 0xFF00) | (data & 0xFF);
1135         
1136         GROM_dataread();
1137         console_GROMs.waddr_LSB = FALSE;
1138      }
1139      else
1140      {
1141         console_GROMs.addr = ((data<<8) & 0xFF00) | (console_GROMs.addr & 0xFF);
1142         console_GROMs.waddr_LSB = TRUE;
1143      }
1144
1145   }
1146   else
1147   {
1148      GROM_dataread();
1149      console_GROMs.raddr_LSB = console_GROMs.waddr_LSB = FALSE;
1150   }
1151}
1152
1153
1154/*
1155   GROM read for TI-99/4p
1156*/
1157READ16_HANDLER ( ti99_4p_grom_r )
1158{
13151159   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);      /* HSGPL is located on 8-bit bus? */
1160//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
13161161
13171162   return /*hsgpl_crdena ?*/ ti99_hsgpl_gpl_r(space, offset, mem_mask) /*: 0*/;
13181163}
13191164
13201165/*
1321   GPL write
1166   GROM write
13221167*/
1323WRITE16_HANDLER ( ti99_4p_wgpl_w )
1168WRITE16_HANDLER ( ti99_4p_grom_w )
13241169{
13251170   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);      /* HSGPL is located on 8-bit bus? */
1171//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
13261172
13271173   /*if (hsgpl_crdena)*/
13281174      ti99_hsgpl_gpl_w(space, offset, data, mem_mask);
r4596r4597
13341180#pragma mark 99/8 MEMORY HANDLERS
13351181#endif
13361182
1337 READ8_HANDLER ( ti99_8_r )
1183READ8_HANDLER( ti99_8_r )
13381184{
1185   /* Set the page. Page size is 4 KiB; page number are the high 4 bits. */
13391186   int page = offset >> 12;
13401187   UINT32 mapper_reg;
1341   int reply = 0;
1188   UINT8 reply = 0;
13421189
1343   /* 03-Apr-2007 - hack because this seems to be called on startup */
1344   if (!xRAM_ptr_8)
1345      return 0;
1346
1347   if (ti99_8_CRUS)
1190   if (ti99_8_enable_rom_and_ports)
13481191   {
1349      /* memory mapped ports enabled */
1192      /* This is only important for address access 0000-1fff and
1193         8000-9fff. Outside, the mapper mode is used. */
13501194      if ((offset >= 0x0000) && (offset < 0x2000))
1351         /* ROM? */
1352         return ROM0_ptr_8[offset & 0x1fff];
1195      {
1196         /* ROM */
1197         reply = ROM0_ptr_8[offset & 0x1fff];
1198         return reply;
1199      }
13531200      else if ((offset >= 0x8000) && (offset < 0xa000))
13541201      {
13551202         /* ti99 scratch pad and memory-mapped registers */
r4596r4597
13571204         switch ((offset - 0x8000) >> 10)
13581205         {
13591206         case 0:
1360            /* RAM */
1207            /* RAM: >8000 - >83ff */
13611208            reply = sRAM_ptr_8[offset & 0x1fff];
13621209            break;
13631210
13641211         case 1:
1365            /* sound write + RAM */
1212            /* >8400 - >840f: sound (cannot read)
1213               >8410 - >87ff: SRAM */
13661214            if (offset >= 0x8410)
13671215               reply = sRAM_ptr_8[offset & 0x1fff];
13681216            break;
r4596r4597
13741222               if (! (offset & 1))
13751223               {
13761224                  if (offset & 2)
1377                     /* read VDP status */
1225                     /* read VDP status >8802 */
13781226                     reply = TMS9928A_register_r(space, 0);
13791227                  else
1380                     /* read VDP RAM */
1228                     /* read VDP RAM >8800 */
13811229                     reply = TMS9928A_vram_r(space, 0);
13821230               }
13831231            }
13841232            else
13851233            {
1234               /* Read mapper >8810 */
13861235               reply = ti99_8_mapper_status;
1387
13881236               ti99_8_mapper_status = 0;
13891237            }
13901238            break;
13911239
13921240         case 4:
1393            /* speech read */
1241            /* speech read: >9000 */
13941242            if (! (offset & 1))
13951243            {
13961244               cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-16*4);      /* this is just a minimum, it can be more */
r4596r4597
13991247            break;
14001248
14011249         case 6:
1402            /* GPL read */
1250            /* GROM read: >9800 */
14031251            if (! (offset & 1))
1404               reply = ti99_rgpl_r(space, offset >> 1, 0) >> 8;
1252               reply = ti99_grom_r8(space, offset);
14051253            break;
14061254
14071255         default:
r4596r4597
14341282      default:
14351283         /* should never happen */
14361284      case 0:
1437         /* unassigned??? */
1285         /* ff0000 - ff1fff: unassigned? */
14381286      case 1:
1439         /* ??? */
1287         /* ff2000 - ff3fff: ? */
14401288      case 4:
1441         /* internal DSR ROM??? (normally enabled with a write to CRU >2700) */
1289         /* ff8000 - ff9fff: internal DSR ROM? (normally enabled with a write to CRU >2700) */
14421290      case 7:
1443         /* ??? */
1291         /* ffe000 - ffffff: ? */
14441292         logerror("unmapped read page=%d offs=%d\n", (int) page, (int) offset);
14451293         break;
14461294
14471295      case 2:
1448         /* DSR space */
1296         /* ff4000 - ff5fff: DSR space */
14491297         reply = ti99_8_peb_r(space, offset & 0x1fff);
14501298         break;
14511299
14521300      case 3:
1453         /* cartridge space */
1301         /* ff6000 - ff7fff: cartridge ROM space */
14541302         offset &= 0x1fff;
1455#if 0
1456         if (hsgpl_crdena)
1457            /* hsgpl is enabled */
1458            reply = ti99_hsgpl_rom6_r(offset, mem_mask);
1459         else
1460#endif
14611303
1462         if (cartridge_mbx && (offset >= 0x0c00) && (offset <= 0x0ffd))
1463            reply = (cartridge_pages_8[0])[offset];
1464         else
1465            reply = current_page_ptr_8[offset];
1304         /* We do not support hsgpl here because it is a 16 bit
1305            system. Just check the cartridges.  */
1306         reply = ti99_multicart8_r(multicart8, offset);
14661307         break;
14671308
14681309      case 5:
1469         /* >2000 ROM (ROM1) */
1310         /* ffa000 - ffbfff: >2000 ROM (ROM1) */
14701311         reply = ROM1_ptr_8[offset & 0x1fff];
14711312         break;
14721313
14731314      case 6:
1474         /* >6000 ROM */
1315         /* ffc000 - ffdfff: >6000 ROM */
14751316         reply = ROM3_ptr_8[offset & 0x1fff];
14761317         break;
14771318      }
r4596r4597
14851326   int page = offset >> 12;
14861327   UINT32 mapper_reg;
14871328
1488   if (ti99_8_CRUS)
1329   if (ti99_8_enable_rom_and_ports)
14891330   {
14901331      /* memory mapped ports enabled */
14911332      if ((offset >= 0x0000) && (offset < 0x2000))
1333      {
14921334         /* ROM? */
14931335         return;
1336      }
14941337      else if ((offset >= 0x8000) && (offset < 0xa000))
14951338      {
14961339         /* ti99 scratch pad and memory-mapped registers */
r4596r4597
15831426         case 7:
15841427            /* GPL write */
15851428            if (! (offset & 1))
1586               ti99_wgpl_w(space, offset >> 1, data << 8, 0);
1429               ti99_grom_w8(space, offset, data);
15871430            break;
15881431
15891432         default:
r4596r4597
15951438   }
15961439
15971440   mapper_reg = ti99_8_mapper_regs[page];
1441   
1442   /* 4 KiB page size */
15981443   offset = (mapper_reg + (offset & 0x0fff)) & 0x00ffffff;
15991444
16001445   /* test write protect */
r4596r4597
16281473
16291474      case 3:
16301475         /* cartridge space */
1631         offset &= 0x1fff;
1632#if 0
1633         if (hsgpl_crdena)
1634            /* hsgpl is enabled */
1635            ti99_hsgpl_rom6_w(offset, data);
1636         else
1637#endif
1638         if (cartridge_minimemory && (offset >= 0x1000))
1639            /* handle minimem RAM */
1640            current_page_ptr_8[offset] = data;
1641         else if (cartridge_mbx)
1642         {   /* handle MBX cart */
1643            /* RAM in 0x6c00-0x6ffd (presumably non-paged) */
1644            /* mapper at 0x6ffe */
1645            if ((offset >= 0x0c00) && (offset <= 0x0ffd))
1646               (cartridge_pages_8[0])[offset] = data;
1647            else if (offset == 0x0ffe)
1648               current_page_ptr_8 = cartridge_pages_8[cartridge_paged ? (data & 1) : 0];
1649         }
1650         else if (cartridge_paged)
1651            /* handle pager */
1652            current_page_ptr_8 = cartridge_pages_8[(offset & 2) >> 1];
1476         /* We do not support HSGPL for the 99/8 at this time. */
1477         ti99_multicart8_w(multicart8, offset & 0x1fff, data);
16531478         break;
16541479
16551480      case 5:
r4596r4597
16621487      }
16631488   }
16641489   else
1490   {
16651491      logerror("unmapped write page=%d offs=%d\n", (int) page, (int) offset);
1492   }
16661493}
16671494
16681495/*===========================================================================*/
r4596r4597
23932220
23942221static WRITE8_DEVICE_HANDLER( ti99_8_WCRUS )
23952222{
2396   ti99_8_CRUS = data;
2223   ti99_8_enable_rom_and_ports = data;
23972224}
23982225
23992226static WRITE8_DEVICE_HANDLER( ti99_8_PTGEN )
r4596r4597
26192446static READ16_HANDLER ( ti99_TIxramlow_r )
26202447{
26212448   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
2449//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
26222450
26232451   return xRAM_ptr[offset];
26242452}
r4596r4597
26262454static WRITE16_HANDLER ( ti99_TIxramlow_w )
26272455{
26282456   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
2457//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
26292458
26302459   COMBINE_DATA(xRAM_ptr + offset);
26312460}
r4596r4597
26342463static READ16_HANDLER ( ti99_TIxramhigh_r )
26352464{
26362465   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
2466//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
26372467
26382468   return xRAM_ptr[offset+0x1000];
26392469}
r4596r4597
26412471static WRITE16_HANDLER ( ti99_TIxramhigh_w )
26422472{
26432473   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
2474//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
26442475
26452476   COMBINE_DATA(xRAM_ptr + offset+0x1000);
26462477}
r4596r4597
27232554static READ16_HANDLER ( ti99_sAMSxramlow_r )
27242555{
27252556   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
2557//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
27262558
27272559   if (sAMS_mapper_on)
27282560      return xRAM_ptr[(offset&0x7ff)+sAMSlookup[(0x1000+offset)>>11]];
r4596r4597
27332565static WRITE16_HANDLER ( ti99_sAMSxramlow_w )
27342566{
27352567   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
2568//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
27362569
27372570   if (sAMS_mapper_on)
27382571      COMBINE_DATA(xRAM_ptr + (offset&0x7ff)+sAMSlookup[(0x1000+offset)>>11]);
r4596r4597
27442577static READ16_HANDLER ( ti99_sAMSxramhigh_r )
27452578{
27462579   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
2580//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
27472581
27482582   if (sAMS_mapper_on)
27492583      return xRAM_ptr[(offset&0x7ff)+sAMSlookup[(0x5000+offset)>>11]];
r4596r4597
27542588static WRITE16_HANDLER ( ti99_sAMSxramhigh_w )
27552589{
27562590   cpu_adjust_icount(cputag_get_cpu(space->machine, "maincpu"),-4);
2591//   cpu_spinuntil_time(space->machine->cpu[0], ATTOTIME_IN_USEC(6));
27572592
27582593   if (sAMS_mapper_on)
27592594      COMBINE_DATA(xRAM_ptr + (offset&0x7ff)+sAMSlookup[(0x5000+offset)>>11]);
src/mess/machine/ti99_4x.h
r4596r4597
44#include "video/tms9928a.h"
55#include "machine/tms9901.h"
66
7#include "devices/ti99cart.h"
8#include "devices/cartslot.h"
9
710/* defines */
811
912#define HAS_99CCFDC 0
1013
1114/* region identifiers */
12#define region_grom "user1"
13#define region_dsr "user2"
14#define region_hsgpl "user3"
15#define region_grom "cons_grom"
16#define region_dsr "dsr"
17#define region_hsgpl "hsgpl"
1518#define region_speech_rom "tms5220"
1619
1720/* offsets for "maincpu" */
r4596r4597
103106#endif
104107} fdc_kind_t;
105108
106
107109/* defines for input port "CFG" */
108110enum
109111{
r4596r4597
127129   config_usbsm_bit   = 11,
128130   config_usbsm_mask   = 0x1,
129131   config_boot_bit      = 13,
130   config_boot_mask   = 0x1
132   config_boot_mask   = 0x1,
133   config_cartslot_bit   = 14,
134   config_cartslot_mask   = 0xf  /* need four bits: covers all current and possibly future cartslots, and one auto mode */
131135};
132136
137enum {
138   STANDARD,
139   EXBAS,
140   MINIMEM,
141   SUPERSPACE,
142   MBX
143};
133144
134145/* prototypes for machine code */
135146
r4596r4597
147158MACHINE_RESET( ti99 );
148159
149160DEVICE_START( ti99_cart );
150DEVICE_IMAGE_LOAD( ti99_cart );
151DEVICE_IMAGE_UNLOAD( ti99_cart );
161// DEVICE_IMAGE_LOAD( ti99_cart );
162// DEVICE_IMAGE_UNLOAD( ti99_cart );
152163
153164VIDEO_START( ti99_4ev );
154165INTERRUPT_GEN( ti99_vblank_interrupt );
r4596r4597
156167
157168void set_hsgpl_crdena(int data);
158169void ti99_common_init(running_machine *machine, const TMS9928a_interface *gfxparm);
170int is_99_8(void);
159171
160172READ16_HANDLER ( ti99_nop_8_r );
161173WRITE16_HANDLER ( ti99_nop_8_w );
162174
175READ8_HANDLER ( ti99_cart_cru_r );
176WRITE8_HANDLER ( ti99_cart_cru_w );
177
163178READ16_HANDLER ( ti99_cart_r );
164179WRITE16_HANDLER ( ti99_cart_w );
165180READ16_HANDLER ( ti99_4p_cart_r );
r4596r4597
172187READ16_HANDLER ( ti99_rv38_r );
173188WRITE16_HANDLER ( ti99_wv38_w );
174189
175READ16_HANDLER ( ti99_rgpl_r );
176WRITE16_HANDLER( ti99_wgpl_w );
177READ16_HANDLER ( ti99_4p_rgpl_r );
178WRITE16_HANDLER ( ti99_4p_wgpl_w );
190READ16_HANDLER ( ti99_grom_r );
191WRITE16_HANDLER( ti99_grom_w );
192READ16_HANDLER ( ti99_4p_grom_r );
193WRITE16_HANDLER ( ti99_4p_grom_w );
179194
180195extern void tms9901_set_int2(running_machine *machine, int state);
181196
src/mess/mess.mak
r4596r4597
682682   $(MESS_DRIVERS)/channelf.o
683683
684684$(MESSOBJ)/ti99.a:      \
685   $(MESS_DEVICES)/ti99cart.o   \
685686   $(MESS_MACHINE)/tms9901.o   \
686687   $(MESS_MACHINE)/tms9902.o   \
687688   $(MESS_MACHINE)/ti99_4x.o   \
src/mess/devices/multcart.h
r4596r4597
3535struct _multicart_resource
3636{
3737   const char *            id;
38   const char *            filename;
3839   multicart_resource *      next;
3940   multicart_resource_type      type;
4041   UINT32                  length;
r4596r4597
6061   const multicart_resource *   resources;
6162   const multicart_socket *   sockets;
6263   const char *            pcb_type;
64   const char *            gamedrv_name; /* need this to find the path to the nvram files */
6365   multicart_private *         data;
6466};
6567
r4596r4597
6971   MCERR_NONE,
7072   MCERR_NOT_MULTICART,
7173   MCERR_CORRUPT,
72   MCERR_OUT_OF_MEMORY
74   MCERR_OUT_OF_MEMORY,
75   MCERR_XML_ERROR,
76   MCERR_INVALID_FILE_REF,
77   MCERR_ZIP_ERROR,
78   MCERR_MISSING_RAM_LENGTH,
79   MCERR_INVALID_RAM_SPEC,
80   MCERR_UNKNOWN_RESOURCE_TYPE,
81   MCERR_INVALID_RESOURCE_REF,
82   MCERR_INVALID_FILE_FORMAT,
83   MCERR_MISSING_LAYOUT,
84   MCERR_NO_PCB_OR_RESOURCES
7385};
7486typedef enum _multicart_open_error multicart_open_error;
7587
88const char *mc_error_text(multicart_open_error error);
7689
7790/***************************************************************************
7891    PROTOTYPES
7992***************************************************************************/
8093
8194/* opens a multicart */
82multicart_open_error multicart_open(const char *filename, multicart_load_flags load_flags, multicart **cart);
95multicart_open_error multicart_open(const char *filename, const char *drvname, multicart_load_flags load_flags, multicart **cart);
8396
8497/* closes a multicart */
8598void multicart_close(multicart *cart);
src/mess/devices/ti99cart.c
r0r4597
1/**********************************************************************
2
3   ti99cart.c
4
5   TI99 family cartridge management
6
7   Michael Zapf, 2009
8   
9*********************************************************************/
10#include "driver.h"
11#include "ti99cart.h"
12#include "cartslot.h"
13#include "machine/ti99_4x.h"
14#include "multcart.h"
15
16typedef int assmfct(const device_config *);
17
18enum
19{
20   TI99CARTINFO_FCT_6000_R = DEVINFO_FCT_DEVICE_SPECIFIC,      /* read16_device_handler */
21   TI99CARTINFO_FCT_6000_W,               /* write16_device_handler */
22   TI99CARTINFO_FCT_6000_R8,               /* read8_device_handler */
23   TI99CARTINFO_FCT_6000_W8,               /* write16_device_handler */
24   TI99CARTINFO_FCT_CRU_R,
25   TI99CARTINFO_FCT_CRU_W,
26   TI99CART_FCT_ASSM,
27   TI99CART_FCT_DISASSM
28};
29
30typedef enum _legacy_cart_t
31{
32   LEG_CART_NONE,
33   LEG_CART_STD,
34   LEG_CART_EXB,
35   LEG_CART_MINI,
36   LEG_CART_MBX
37} legacy_cart_t;
38
39struct _ti99_multicart_t
40{
41   /* Reserves space for all cartridges. This is also used in the legacy
42      cartridge system, but only for slot 0. */
43   cartridge_t cartridge[NUMBER_OF_CARTRIDGE_SLOTS];
44
45   /* Determines which slot is currently active. This value is changed when there
46   are accesses to other GROM base addresses. */
47   int active_slot;
48   
49   /* Used in order to enforce a special slot. This value is retrieved
50      from the dipswitch setting. A value of -1 means automatic, that is,
51      the grom base switch is used. Values 0 .. max refer to the
52      respective slot. */
53   int fixed_slot;
54
55   /* Holds the highest index of a cartridge being plugged in plus one.
56      If we only have one cartridge inserted, we don't want to get a
57      selection option, so we just mirror the memory contents. */
58   int next_free_slot;
59   
60   /* Counts the number of slots which currently contain legacy format
61      cartridge images. */
62   int legacy_slots;
63
64   /* Counts the number of slots which currently contain new format
65      cartridge images. */
66   int multi_slots;
67
68   /* Legacy mode. Stores the slot number where the cartridge part is mounted.
69      Index is from slotc_type_t. */
70   int legacy_slotnumber[5];
71};
72typedef struct _ti99_multicart_t ti99_multicart_t;
73
74#define AUTO -1
75
76typedef enum _slotc_type_t
77{
78   SLOTC_EMPTY = -1,
79   SLOTC_GROM = 0,
80   SLOTC_CROM = 1,
81   SLOTC_DROM = 2,
82   SLOTC_MINIMEM = 3,
83   SLOTC_MBX = 4
84} slotc_type_t;
85
86/* Function declaration; the function itself is in the later part of this file. */
87static UINT8 cartridge_grom_read_legacy(const device_config *cartsys, int cart_offset);
88static void unload_legacy(const device_config *image);
89static int load_legacy(const device_config *image);
90
91/* Access to the pcb. Contained in the token of the pcb instance. */
92struct _ti99_pcb_t
93{
94   /* Read function for this cartridge. */
95   read16_device_func read;
96   
97   /* Write function for this cartridge. */
98   write16_device_func write;
99   
100   /* Read function for this cartridge, TI-99/8. */
101   read8_device_func read8;
102   
103   /* Write function for this cartridge, TI-99/8. */
104   write8_device_func write8;
105
106   /* Read function for this cartridge, CRU. */
107   read8_device_func cruread;
108   
109   /* Write function for this cartridge, CRU. */
110   write8_device_func cruwrite;
111   
112   /* Link up to the cartridge structure which contains this pcb. */
113   cartridge_t *cartridge;
114   
115   /* Function to assemble this cartridge. */
116   assmfct   *assemble;
117   
118   /* Function to disassemble this cartridge. */
119   assmfct   *disassemble;
120};
121typedef struct _ti99_pcb_t ti99_pcb_t;
122
123static int in_legacy_mode(const device_config *device)
124{
125   ti99_multicart_t *cartslots = (ti99_multicart_t *)device->token;
126   if ((cartslots->legacy_slots>0) &&  (cartslots->multi_slots==0))
127      return TRUE;
128   return FALSE;
129}
130
131/*
132   Activates a slot in the multi-cartridge extender.
133   Setting the slot is done by accessing the GROM ports using a
134   specific address:
135   
136   slot n:  read data (read address, write data, write address)
137   
138   slot 0:  0x9800 (0x9802, 0x9c00, 0x9c02)   : cartridge1
139   slot 1:  0x9804 (0x9806, 0x9c04, 0x9c06)   : cartridge2
140   ...
141   slot 15: 0x983c (0x983e, 0x9c3c, 0x9c3e)   : cartridge16
142
143   The following addresses are theoretically available, but the
144   built-in OS does not use them; i.e. cartridges will not be
145   included in the selection list, and their features will not be
146   found by lookup, but they could be accessed directly by user
147   programs.
148   slot 16: 0x9840 (0x9842, 0x9c40, 0x9c42)
149   ...
150   slot 255:  0x9bfc (0x9bfe, 0x9ffc, 0x9ffe)
151   
152   Setting the GROM base should select one cartridge, but the ROMs in the
153   CPU space must also be switched. As there is no known special mechanism
154   we assume that by switching the GROM base, the ROM is automatically
155   switched.
156   
157   Caution: This means that cartridges which do not have at least one
158   GROM cannot be switched with this mechanism.
159   
160   We assume that the slot number is already calculated in the caller:
161   slotnumber>=0 && slotnumber<=255
162   
163   NOTE: The OS will stop searching when it finds slots 1 and 2 empty.
164   Interestingly, cartridge subroutines are found nevertheless, even when
165   the cartridge is plugged into a higher slot.
166*/
167void cartridge_slot_set(const device_config *cartsys, int slotnumber)
168{
169   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
170   assert(slotnumber>=0 && slotnumber<=255);
171//   if (cartslots->active_slot != slotnumber) printf("Setting cartslot to %d\n", slotnumber);
172   if (cartslots->fixed_slot==AUTO)
173      cartslots->active_slot = slotnumber;
174   else
175      cartslots->active_slot = cartslots->fixed_slot;
176}
177
178/*
179   Allows to manually lock the cartslot system to a specific slot.
180   Called from the machine driver, taking the dipswitch setting.
181   We take slot numbers from 0 (automatic mode) to the maximum.
182*/
183void lock_cartridge_slot(const device_config *cartsys, int slotnumber)
184{
185   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
186   assert(slotnumber>=0 && slotnumber<=255);
187   cartslots->fixed_slot = slotnumber-1; /* auto = -1 */
188}
189
190static int slot_is_empty(const device_config *cartsys, int slotnumber)
191{
192   cartridge_t *cart;
193   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
194
195   cart = &cartslots->cartridge[slotnumber];
196
197   if ((cart->rom_size==0) && (cart->ram_size==0) && (cart->grom_size==0))
198      return TRUE;
199   return FALSE;
200}
201
202static void clear_slot(const device_config *cartsys, int slotnumber)
203{
204   cartridge_t *cart;
205   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
206
207   cart = &cartslots->cartridge[slotnumber];
208
209   cart->rom_size = cart->ram_size = cart->grom_size = 0;
210   cart->rom_ptr = NULL;
211   cart->rom2_ptr = NULL;
212   cart->ram_ptr = NULL;
213   cart->grom_ptr = NULL;
214}
215
216/*
217   GROM access. Accesses the GROMs in the cartridge. GROM memory is
218   accessed serially via a set of ports. The GRMWA port (bank 0: 0x9c02)
219   takes two consecutive byte writes which represent the high and low byte
220   of the memory address in the GROM space. Once set, bytes can be read
221   by repeated read accesses on the GRMRD port (bank 0: 0x9800). The
222   address pointer advances on each read. The pointer wraps at the end
223   of each GROM chip memory space, i.e. every 8KiB.
224   This function takes the current GROM address minus 0x6000 which is the
225   lowest address for cartridge GROMs. Lower addresses access GROMs in the
226   console only, which appear in all banks.
227   Note that the GROM access mechanism is identical for all cartridge
228   types.
229*/
230UINT8 cartridge_grom_read(const device_config *device, int cart_offset)
231{
232   UINT8 value;
233   int slot;
234   cartridge_t *cartridge;
235   ti99_multicart_t *cartslots = (ti99_multicart_t *)device->token;
236   
237   /* Handle the legacy mode. */
238   if (in_legacy_mode(device))
239      return cartridge_grom_read_legacy(device, cart_offset);
240   
241   slot = cartslots->active_slot;
242   /*
243      This fixes a minor issue: The selection mechanism of the console
244      concludes that there are multiple cartridges plugged in when it
245      checks the different GROM bases and finds different data. Empty
246      slots should return 0 at all addresses, but when we have only
247      one cartridge, this entails that the second cartridge is
248      different (all zeros), and so we get a meaningless selection
249      option between cartridge 1 and 1 and 1 ...
250     
251      So if next_free_slot==1, we have one cartridge in slot 0.
252      In that case we trick the OS to believe that the addressed
253      cartridge appears at all locations which causes it to assume a
254      standard single cartslot. 
255   */
256   if (cartslots->fixed_slot==AUTO && cartslots->next_free_slot==1)
257      slot=0;
258
259   if (slot >= NUMBER_OF_CARTRIDGE_SLOTS)
260      value = 0;
261   else
262   {
263      cartridge = &cartslots->cartridge[slot];
264     
265      if ((cartridge->grom_size>0) && (cartridge->pcb!=NULL))
266      {
267         /* This is the reason why we need the image size: Read
268            access beyond the image size must be detected. */
269         if (cart_offset > cartridge->grom_size)
270            return 0;
271
272         value = (UINT8)cartridge->grom_buffer;
273         /* read ahead */
274         cartridge->grom_buffer = cartridge->grom_ptr[cart_offset];
275      }
276      else
277      {
278         /* We assume that empty sockets return 0 */
279         value = 0;
280         logerror("Empty socket at address G(%d)>%04x\n", slot, 0x6000 + cart_offset);
281      }
282   }
283//   printf("grom access(port = %x, address = %04x) = %02x\n", slot,  0x6000 + cart_offset*2, value);
284   return value;
285}
286
287/*
288   Find the index of the cartridge name. We assume the format
289   <name><number>, i.e. the number is the longest string from the right
290   which can be interpreted as a number.
291*/
292static int get_index_from_tagname(const device_config *image)
293{
294   const char *tag = image->tag;
295   int maxlen = strlen(tag);
296   int i;
297   for (i=maxlen-1; i >=0; i--)
298      if (tag[i] < 48 || tag[i] > 57) break;
299
300   return atoi(tag+i+1);
301}
302
303/*
304   Common routine to assemble cartridges from resources.
305*/
306static cartridge_t *assemble_common(const device_config *cartslot)
307{
308   /* Pointer to the cartridge structure. */
309   cartridge_t *cartridge;
310   const device_config *cartsys = cartslot->owner;
311   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
312   
313   void *socketcont;
314   int reslength;
315   int i;
316
317   int slotnumber = get_index_from_tagname(cartslot)-1;
318   assert(slotnumber>=0 && slotnumber<NUMBER_OF_CARTRIDGE_SLOTS);
319
320   /* There is a cartridge in this slot, check the maximum slot number. */
321   if (cartslots->next_free_slot <= slotnumber)
322   {
323      cartslots->next_free_slot = slotnumber+1;
324   }
325   cartridge = &cartslots->cartridge[slotnumber];
326
327   /* Get the socket which is retrieved by the multicart instance in
328      the state of the cartslot instance. */
329   socketcont = cartslot_get_socket(cartslot, "grom_socket");
330   reslength = cartslot_get_resource_length(cartslot, "grom_socket");
331   if (socketcont != NULL)
332   {
333      cartridge->grom_ptr = (UINT8 *)socketcont;
334      cartridge->grom_size = reslength;
335   }
336   
337   socketcont = cartslot_get_socket(cartslot, "rom_socket");
338   reslength = cartslot_get_resource_length(cartslot, "rom_socket");
339   if (socketcont != NULL)
340   {
341      cartridge->rom_ptr = socketcont;
342      cartridge->rom_size = reslength;
343      if (!is_99_8())
344      {
345         /* Big-endianize it for 16 bit access. */
346         UINT16 *cont16b = (UINT16 *)socketcont;
347         for (i = 0; i < cartridge->rom_size/2; i++)
348            cont16b[i] = BIG_ENDIANIZE_INT16(cont16b[i]);
349      }
350   }
351   
352   socketcont = cartslot_get_socket(cartslot, "rom2_socket");
353//   reslength = cartslot_get_resource_length(cartslot, "rom2_socket");  /* cannot differ from rom_socket */
354   if (socketcont != NULL)
355   {
356      cartridge->rom2_ptr = (UINT16 *)socketcont;
357      assert(cartridge->rom2_ptr != NULL);
358      if (!is_99_8())
359      {
360         /* Big-endianize it for 16 bit access. */
361         UINT16 *cont16b = (UINT16 *)socketcont;
362         for (i = 0; i < cartridge->rom_size/2; i++)
363            cont16b[i] = BIG_ENDIANIZE_INT16(cont16b[i]);
364      }
365   }
366
367   socketcont = cartslot_get_socket(cartslot, "ram_socket");
368   reslength = cartslot_get_resource_length(cartslot, "ram_socket");
369   if (socketcont != NULL)
370   {
371      cartridge->ram_ptr = socketcont;
372      cartridge->ram_size = reslength;
373      if (!is_99_8())
374      {
375         /* Big-endianize it for 16 bit access. */
376         UINT16 *cont16b = (UINT16 *)socketcont;
377         for (i = 0; i < cartridge->ram_size/2; i++)
378            cont16b[i] = BIG_ENDIANIZE_INT16(cont16b[i]);
379      }
380   }
381   return cartridge;
382}
383
384static void set_pointers(const device_config *pcb, int index)
385{
386   const device_config *cartsys = pcb->owner->owner;
387   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
388   ti99_pcb_t *pcb_def = (ti99_pcb_t *)pcb->token;
389
390   pcb_def->read = (read16_device_func) device_get_info_fct(pcb, TI99CARTINFO_FCT_6000_R);
391   pcb_def->write = (write16_device_func) device_get_info_fct(pcb, TI99CARTINFO_FCT_6000_W);
392   pcb_def->read8 = (read8_device_func) device_get_info_fct(pcb, TI99CARTINFO_FCT_6000_R8);
393   pcb_def->write8 = (write8_device_func) device_get_info_fct(pcb, TI99CARTINFO_FCT_6000_W8);
394   pcb_def->cruread = (read8_device_func) device_get_info_fct(pcb, TI99CARTINFO_FCT_CRU_R);
395   pcb_def->cruwrite = (write8_device_func) device_get_info_fct(pcb, TI99CARTINFO_FCT_CRU_W);
396   
397   pcb_def->assemble = (assmfct *)device_get_info_fct(pcb, TI99CART_FCT_ASSM);
398   pcb_def->disassemble = (assmfct *)device_get_info_fct(pcb, TI99CART_FCT_DISASSM);
399   
400   pcb_def->cartridge = &cartslots->cartridge[index];
401   pcb_def->cartridge->pcb = pcb;
402}
403
404/*****************************************************************************
405  Cartridge type: None
406     This PCB device is just a pseudo device; the legacy mode is handled
407   by dedicated functions.
408******************************************************************************/
409static DEVICE_START(ti99_pcb_none)
410{
411   /* device is ti99_cartslot:cartridge:pcb */
412//   printf("DEVICE_START(ti99_pcb_none), tag of device=%s\n", device->tag);
413   set_pointers(device, get_index_from_tagname(device->owner)-1);
414}
415
416/*****************************************************************************
417  Cartridge type: Standard
418     Most cartridges are built in this type. Every cartridge may contain
419   GROM between 0 and 40 KiB, and ROM with 8 KiB length, no banking.
420******************************************************************************/
421
422static DEVICE_START(ti99_pcb_std)
423{
424   /* device is ti99_cartslot:cartridge:pcb */
425//   printf("DEVICE_START(ti99_pcb_std), tag of device=%s\n", device->tag);
426   set_pointers(device, get_index_from_tagname(device->owner)-1);
427}
428
429/*   
430   Read handler for the CPU address space of the cartridge
431   Images for this area are found in the rom_sockets.
432*/
433static READ16_DEVICE_HANDLER( read_cart_std )
434{
435   /* device is pcb, owner is cartslot */
436   UINT16 value;
437   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
438   cartridge_t *cartridge = pcb->cartridge;
439   
440   if (cartridge->rom_ptr==NULL)
441   {
442//      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
443//      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
444      value = 0;
445   }
446   else
447   {
448      value = ((UINT16 *)cartridge->rom_ptr)[offset];
449//      printf("stdcart/read: %4x = %4x\n", 0x6000 + 2*offset, value);
450   }
451//    printf("accessed cartridge rom[%02x:%04x] = %04x\n", 0, 0x6000+offset*2, value);
452   return value;
453}
454
455static WRITE16_DEVICE_HANDLER( write_cart_std )
456{
457   logerror("Write access to cartridge ROM at address %04x ignored", 0x6000 + 2*offset);
458}
459
460/*
461   TI-99/8 support:
462   Read handler for the CPU address space of the cartridge
463   Images for this area are found in the rom_sockets.
464   For comments see the READ16 handler.
465*/
466static READ8_DEVICE_HANDLER( read_cart_std8 )
467{
468   UINT8 value;
469   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
470   cartridge_t *cartridge = pcb->cartridge;
471   
472   if (cartridge->rom_ptr==NULL)
473      value = 0;
474   else
475      value = ((UINT8 *)cartridge->rom_ptr)[offset];
476   return value;
477}
478
479/*
480   TI-99/8 support:
481   Write handler. No writes are allowed to standard cartridges.
482*/
483static WRITE8_DEVICE_HANDLER( write_cart_std8 )
484{
485   logerror("Write access to cartridge ROM at address %04x ignored", 0x6000 + offset);
486}
487
488/*
489   The standard cartridge assemble routine. We just call the common
490   function here.
491*/
492static int assemble_std(const device_config *image)
493{
494   cartridge_t *cart;
495//   printf("assemble_std, %s\n", image->tag);
496   cart = assemble_common(image);
497     
498   return INIT_PASS;
499}
500
501/*
502   Removes pointers and restores the state before plugging in the
503   cartridge.
504   The pointer to the location after the last cartridge is adjusted.
505   As it seems, we can use the same function for the disassembling of all
506   cartridge types.
507*/
508static int disassemble_std(const device_config *image)
509{
510   int slotnumber;
511   int i;
512   cartridge_t *cart;
513   const device_config *cartsys = image->owner;
514   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
515
516   slotnumber = get_index_from_tagname(image)-1;
517//   printf("Disassemble cartridge %d\n", slotnumber);
518
519   /* Search the highest remaining cartridge. */
520   cartslots->next_free_slot = 0;
521   for (i=NUMBER_OF_CARTRIDGE_SLOTS-1; i >= 0; i--)
522   {
523      if (i != slotnumber)
524      {
525         if (!slot_is_empty(cartsys, i))
526         {
527            cartslots->next_free_slot = i+1;
528//            printf("Setting new next_free_slot to %d\n", cartslots->next_free_slot);
529            break;
530         }
531      }
532   }
533   
534   /* Do we have RAM? If so, swap the bytes (undo the BIG_ENDIANIZE) */
535   cart = &cartslots->cartridge[slotnumber];
536   if (!is_99_8())
537   {
538      for (i = 0; i < cart->ram_size/2; i++)
539         ((UINT16 *)cart->ram_ptr)[i] = BIG_ENDIANIZE_INT16(((UINT16 *)cart->ram_ptr)[i]);
540   }
541   
542   clear_slot(cartsys, slotnumber);
543
544   return INIT_PASS;
545}
546
547/*****************************************************************************
548  Cartridge type: Paged (Extended Basic)
549   This cartridge consists of GROM memory and 2 pages of standard ROM.
550   The page is set by writing any value to a location in
551   the address area, where an even word offset sets the page to 0 and an
552   odd word offset sets the page to 1 (e.g. 6000 = bank 0, and
553   6002 = bank 1).
554******************************************************************************/
555
556static DEVICE_START(ti99_pcb_paged)
557{
558   /* device is ti99_cartslot:cartridge:pcb */
559//   printf("DEVICE_START(ti99_pcb_paged), tag of device=%s\n", device->tag);
560   set_pointers(device, get_index_from_tagname(device->owner)-1);
561}
562
563/*   
564   Read handler for the CPU address space of the cartridge
565   Images for this area are found in the rom_sockets.
566*/
567static READ16_DEVICE_HANDLER( read_cart_paged )
568{
569   /* device is pcb, owner is cartslot */
570   UINT16 value;
571   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
572   cartridge_t *cartridge = pcb->cartridge;
573   UINT16 *rombank;
574   
575   if (cartridge->rom_page==0)
576      rombank = cartridge->rom_ptr;
577   else
578      rombank = cartridge->rom2_ptr;
579   
580   if (rombank==NULL)
581   {
582//      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
583//      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
584      /* TODO: Check for consistency with the GROM memory handling. */
585      value = 0;
586   }
587   else
588   {
589      value = rombank[offset];
590//      printf("stdcart/read: %4x = %4x\n", 0x6000 + 2*offset, value);
591   }
592//    printf("accessed cartridge rom[%02x:%04x] = %04x\n", rompage, 0x6000+offset*2, value);
593   return value;
594}
595
596
597/*
598   Handle paging. Extended Basic switches between ROM banks by
599   using the value of the LSB of the address where any value
600   is written to.
601*/
602static WRITE16_DEVICE_HANDLER( write_cart_paged )
603{
604   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
605   cartridge_t *cartridge = pcb->cartridge;
606   cartridge->rom_page = offset & 1;
607//   printf("setting rompage to %d\n", cartridge->rom_page);
608}
609
610/*   
611   TI-99/8 support:
612   Read handler for the CPU address space of the cartridge.
613   Images for this area are found in the rom_sockets.
614*/
615static READ8_DEVICE_HANDLER( read_cart_paged8 )
616{
617   UINT8 value;
618   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
619   cartridge_t *cartridge = pcb->cartridge;
620   UINT8 *rombank;
621   
622   if (cartridge->rom_page==0)
623      rombank = cartridge->rom_ptr;
624   else
625      rombank = cartridge->rom2_ptr;
626   
627   if (rombank==NULL)
628      value = 0;
629   else
630      value = rombank[offset];
631
632   return value;
633}
634
635
636/*
637   TI-99/8 support:
638   Handle paging. Extended Basic switches between ROM banks by
639   using the value of the LSB of the address where any value
640   is written to. This is the TI-99/8 support.
641   Note that we need to check whether offset/2 is even or odd.
642   
643   FIXME: Extended Basic crashes with TI-99/8 when changing the rom page.
644   This may be an incompatibility of Extended Basic and TI-99/8.
645*/
646static WRITE8_DEVICE_HANDLER( write_cart_paged8 )
647{
648   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
649   cartridge_t *cartridge = pcb->cartridge;
650//   printf("offset = %04x\n", offset);
651   cartridge->rom_page = (offset>>1) & 1;
652}
653
654/*
655   We require paged modules to have at least those two rom banks.
656*/
657static int assemble_paged(const device_config *image)
658{
659   cartridge_t *cart;
660   
661//   printf("assemble_paged, %s\n", image->tag);
662   cart = assemble_common(image);
663   if (cart->rom_ptr==NULL)
664   {
665      logerror("Missing ROM for paged cartridge");
666      return INIT_FAIL;
667   }
668   if (cart->rom2_ptr==NULL)
669   {
670      logerror("Missing second ROM for paged cartridge");
671      return INIT_FAIL;
672   }
673
674   return INIT_PASS;
675}
676
677/*****************************************************************************
678  Cartridge type: Mini Memory
679   GROM: 6 KiB (occupies G>6000 to G>7800)
680   ROM: 4 KiB (romfile is actually 8 K long, half with zeros, 0x6000-0x6fff)
681   persistent RAM: 4 KiB (0x7000-0x7fff)   
682******************************************************************************/
683
684static DEVICE_START(ti99_pcb_minimem)
685{
686//   printf("TI99 pcb minimem start\n");
687   set_pointers(device, get_index_from_tagname(device->owner)-1);
688}
689
690static READ16_DEVICE_HANDLER( read_cart_minimem )
691{
692   /* device is pcb, owner is cartslot */
693   UINT16 value;
694   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
695   cartridge_t *cartridge = pcb->cartridge;
696   
697   if (offset < 0x800)
698   {
699      if (cartridge->rom_ptr==NULL)
700      {
701         //      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
702         //      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
703         value = 0;
704      }
705      else
706      {
707         value = ((UINT16 *)cartridge->rom_ptr)[offset];
708         //      printf("stdcart/read: %4x = %4x\n", 0x6000 + 2*offset, value);
709      }
710      //    printf("accessed cartridge rom[%02x:%04x] = %04x\n", rompage, 0x6000+offset*2, value);
711   }
712   else
713   {
714      if (cartridge->ram_ptr==NULL)
715      {
716         //      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
717         //      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
718         value = 0;
719      }
720      else
721      {
722         value = ((UINT16 *)cartridge->ram_ptr)[offset-0x800];
723         //      printf("stdcart/read: %4x = %4x\n", 0x6000 + 2*offset, value);
724      }
725      //    printf("accessed cartridge rom[%02x:%04x] = %04x\n", rompage, 0x6000+offset*2, value);
726   }
727   return value;
728}
729
730/*
731   Mini Memory cartridge write operation. RAM is located at offset + 4 KiB
732*/
733static WRITE16_DEVICE_HANDLER( write_cart_minimem )
734{
735   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
736   cartridge_t *cartridge = pcb->cartridge;
737   UINT16 *pos;
738   
739   if (offset < 0x800)
740   {
741      logerror("Write access to cartridge ROM at address %04x ignored", 0x6000 + 2*offset);
742   }
743   else
744   {
745      if (cartridge->ram_ptr==NULL)
746      {
747         logerror("No cartridge RAM at address %04x", 0x6000 + 2*offset);
748
749         //      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
750         //      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
751         /* TODO: Check for consistency with the GROM memory handling. */
752      }
753      else
754      {
755         pos = ((UINT16 *)cartridge->ram_ptr) + offset - 0x800;
756         COMBINE_DATA(pos);
757      }
758   }
759}
760
761/*
762   Read handler for MiniMemory in TI-99/8.
763*/
764static READ8_DEVICE_HANDLER( read_cart_minimem8 )
765{
766   /* device is pcb, owner is cartslot */
767   UINT8 value;
768   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
769   cartridge_t *cartridge = pcb->cartridge;
770   
771   if (offset < 0x1000)
772   {
773      if (cartridge->rom_ptr==NULL)
774      {
775         value = 0;
776      }
777      else
778      {
779         value = ((UINT8 *)cartridge->rom_ptr)[offset];
780      }
781   }
782   else
783   {
784      if (cartridge->ram_ptr==NULL)
785      {
786         value = 0;
787      }
788      else
789      {
790         value = ((UINT8 *)cartridge->ram_ptr)[offset-0x1000];
791      }
792   }
793   return value;
794}
795
796/*
797   Mini Memory cartridge write operation. RAM is located at offset + 4 KiB.
798   This is the TI-99/8 support.
799*/
800static WRITE8_DEVICE_HANDLER( write_cart_minimem8 )
801{
802   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
803   cartridge_t *cartridge = pcb->cartridge;
804   
805   if (offset < 0x1000)
806   {
807      logerror("Write access to cartridge ROM at address %04x ignored", 0x6000 + offset);
808   }
809   else
810   {
811      if (cartridge->ram_ptr==NULL)
812      {
813         logerror("No cartridge RAM at address %04x", 0x6000 + offset);
814
815         //      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
816         //      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
817         /* TODO: Check for consistency with the GROM memory handling. */
818      }
819      else
820      {
821         ((UINT8 *)cartridge->ram_ptr)[offset-0x1000] = data;
822      }
823   }
824}
825
826
827static int assemble_minimem(const device_config *image)
828{
829   cartridge_t *cart;
830
831//   printf("assemble_minimem, %s\n", image->tag);
832   cart = assemble_common(image);
833   if (cart->grom_size==0)
834   {
835      logerror("Missing GROM for Mini Memory");
836      return INIT_FAIL;
837   }
838   if (cart->rom_size==0)
839   {
840      logerror("Missing ROM for Mini Memory");
841      return INIT_FAIL;
842   }
843   if (cart->ram_size==0)
844   {
845      logerror("Missing RAM for Mini Memory");
846      return INIT_FAIL;
847   }
848   
849   return INIT_PASS;
850}
851
852
853/*****************************************************************************
854  Cartridge type: SuperSpace II
855   SuperSpace cartridge type. SuperSpace is intended as a user-definable
856   blank cartridge containing buffered RAM
857   It has an Editor/Assembler GROM which helps the user to load
858   the user program into the cartridge. If the user program has a suitable
859   header, the console recognizes the cartridge as runnable, and
860   assigns a number in the selection screen.
861   Switching the RAM banks in this cartridge is achieved by setting
862   CRU bits (the system serial interface).
863
864   GROM: Editor/Assembler GROM
865   ROM: none
866   persistent RAM: 32 KiB (0x6000-0x7fff, 4 banks)
867   Switching the bank is done via CRU write
868******************************************************************************/
869
870static DEVICE_START(ti99_pcb_super)
871{
872//   printf("TI99 pcb super start\n");
873   set_pointers(device, get_index_from_tagname(device->owner)-1);
874}
875
876/*
877   The CRU read handler. The CRU is a serial interface in the console.
878   Using the CRU we can switch the banks in the SuperSpace cartridge.
879*/
880static READ8_DEVICE_HANDLER( read_cart_cru )
881{
882   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
883   cartridge_t *cartridge = pcb->cartridge;
884   /* offset is the bit number. The CRU base address is already divided
885           by 2. */
886        int reply = 0;
887
888   /* ram_page contains the bank number. We have a maximum of
889      4 banks; the Super Space II manual says:
890   
891   Banks are selected by writing a bit pattern to CRU address >0800:
892   
893   Bank #   Value
894   0        >02  = 0000 0010
895   1        >08  = 0000 1000
896   2        >20  = 0010 0000
897   3        >80  = 1000 0000
898   
899   With the bank number (0, 1, 2, or 3) in R0:
900   
901   BNKSW   LI    R12,>0800   Set CRU address
902      LI    R1,2        Load Shift Bit
903      SLA   R0,1        Align Bank Number
904      JEQ   BNKS1       Skip shift if Bank 0
905      SLA   R1,0        Align Shift Bit
906   BNKS1   LDCR  R1,0        Switch Banks
907      SRL   R0,1        Restore Bank Number (optional)
908      RT
909   */
910   if ((offset & 1) == 0 || offset > 7)
911      reply = 0;
912   
913   /* CRU addresses are only 1 bit wide. Bytes are transferred from LSB
914      to MSB. That is, 8 bit are eight consecutive addresses. */
915   reply = (cartridge->ram_page == (offset-1)/2);
916
917   return reply;
918}
919
920static WRITE8_DEVICE_HANDLER( write_cart_cru )
921{
922   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
923   cartridge_t *cartridge = pcb->cartridge;
924   // data is bit
925   // offset is address
926        if (offset < 8)
927   {
928      if (data != 0)
929         cartridge->ram_page = (offset-1)/2;
930   }
931}
932
933/*
934   SuperSpace RAM read operation. RAM is located at 0x6000 to 0x7fff
935   as 4 banks of 8 KiB each
936*/
937static READ16_DEVICE_HANDLER( read_cart_super )
938{
939   /* device is pcb, owner is cartslot */
940   UINT16 value;
941   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
942   cartridge_t *cartridge = pcb->cartridge;
943   offs_t boffset;
944   
945   if (cartridge->ram_ptr==NULL)
946   {
947      //      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
948      //      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
949      value = 0;
950   }
951   else
952   {
953      boffset = cartridge->ram_page * 0x1000 + offset;     
954      value = ((UINT16 *)cartridge->ram_ptr)[boffset];
955      //      printf("stdcart/read: %4x = %4x\n", 0x6000 + 2*offset, value);
956   }
957   //    printf("accessed cartridge ram[%02x:%04x] = %04x\n", cartridge->ram_page, 0x6000+offset*2, value);
958   return value;
959}
960
961/*
962   SuperSpace RAM write operation. RAM is located at 0x6000 to 0x7fff
963   as 4 banks of 8 KiB each
964*/
965static WRITE16_DEVICE_HANDLER( write_cart_super )
966{
967   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
968   cartridge_t *cartridge = pcb->cartridge;
969   UINT16 *pos;
970   offs_t boffset;
971   
972   if (cartridge->ram_ptr==NULL)
973   {
974      logerror("No cartridge RAM at address %04x", 0x6000 + 2*offset);
975     
976      //      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
977      //      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
978   }
979   else
980   {
981      boffset = cartridge->ram_page * 0x1000 + offset;     
982      pos = ((UINT16 *)cartridge->ram_ptr) + boffset;
983      COMBINE_DATA(pos);
984   }
985}
986
987/*
988   SuperSpace RAM read operation. RAM is located at 0x6000 to 0x7fff
989   as 4 banks of 8 KiB each. This is the TI-99/8 support.
990*/
991static READ8_DEVICE_HANDLER( read_cart_super8 )
992{
993   /* device is pcb, owner is cartslot */
994   UINT8 value;
995   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
996   cartridge_t *cartridge = pcb->cartridge;
997   offs_t boffset;
998   
999   if (cartridge->ram_ptr==NULL)
1000   {
1001      //      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
1002      //      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
1003      value = 0;
1004   }
1005   else
1006   {
1007      boffset = cartridge->ram_page * 0x2000 + offset;     
1008      value = ((UINT8 *)cartridge->ram_ptr)[boffset];
1009      //      printf("stdcart/read: %4x = %4x\n", 0x6000 + 2*offset, value);
1010   }
1011   //    printf("accessed cartridge ram[%02x:%04x] = %04x\n", cartridge->ram_page, 0x6000+offset*2, value);
1012   return value;
1013}
1014
1015/*
1016   SuperSpace RAM write operation. RAM is located at 0x6000 to 0x7fff
1017   as 4 banks of 8 KiB each. This is the TI-99/8 support.
1018*/
1019static WRITE8_DEVICE_HANDLER( write_cart_super8 )
1020{
1021   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
1022   cartridge_t *cartridge = pcb->cartridge;
1023   offs_t boffset;
1024   
1025   if (cartridge->ram_ptr==NULL)
1026   {
1027      logerror("No cartridge RAM at address %04x", 0x6000 + offset);
1028     
1029      //      printf("No cartridge ROM in rompage=%d of bank %d\n", rompage, cartslots->active_slot);
1030      //      printf("Cartridge address = %lx\n", (long)pcb->cartridge);
1031   }
1032   else
1033   {
1034      boffset = cartridge->ram_page * 0x2000 + offset;     
1035      ((UINT8 *)cartridge->ram_ptr)[boffset] = data;
1036   }
1037}
1038
1039
1040static int assemble_super(const device_config *image)
1041{
1042   cartridge_t *cart;
1043//   printf("assemble_super, %s\n", image->tag);
1044   
1045   cart = assemble_common(image);
1046   if (cart->ram_size==0)
1047   {
1048      logerror("Missing RAM for SuperSpace");
1049      return INIT_FAIL;
1050   }
1051   return INIT_PASS;
1052}
1053
1054/*****************************************************************************
1055  Cartridge type: MBX
1056     GROM: up to 40 KiB
1057   ROM: up to 16 KiB (in up to 2 banks of 8KiB each)
1058   RAM: 1022 B (0x6c00-0x6ffd, overrides ROM in that area)
1059   ROM mapper: 6ffe
1060******************************************************************************/
1061
1062static DEVICE_START(ti99_pcb_mbx)
1063{
1064//   printf("TI99 pcb mbx start\n");
1065   set_pointers(device, get_index_from_tagname(device->owner)-1);
1066}
1067
1068static READ16_DEVICE_HANDLER( read_cart_mbx )
1069{
1070   /* device is pcb, owner is cartslot */
1071   UINT16 value = 0;
1072   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
1073   cartridge_t *cartridge = pcb->cartridge;
1074   
1075   if ((offset >= 0x0600) && (offset <= 0x07fe))
1076   {
1077      /* This is the RAM area which overrides any ROM. There is no
1078         known banking behavior for the RAM, so we must assume that
1079         there is only one bank. */
1080      if (cartridge->ram_ptr != NULL)
1081         value = ((UINT16 *)cartridge->ram_ptr)[offset - 0x0600];
1082   }
1083   else
1084   {
1085      if (cartridge->rom_ptr != NULL)
1086         value = ((UINT16 *)cartridge->rom_ptr)[offset + cartridge->rom_page * 0x1000];
1087   }
1088   return value;
1089}
1090
1091/*
1092   MBX write operation.
1093*/
1094static WRITE16_DEVICE_HANDLER( write_cart_mbx )
1095{
1096   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
1097   cartridge_t *cartridge = pcb->cartridge;
1098   UINT16 *pos;
1099
1100   if ((offset >= 0x0600) && (offset <= 0x07fe))
1101   {
1102      if (cartridge->ram_ptr == NULL)
1103         return;
1104      pos = ((UINT16 *)cartridge->ram_ptr) + offset - 0x0600;
1105      COMBINE_DATA(pos);
1106   }
1107   else if ((offset == 0x07ff) && ACCESSING_BITS_8_15)
1108   {
1109      if (cartridge->rom_ptr==NULL)
1110         return;
1111      cartridge->rom_page = ((data >> 8) & 1);
1112   }
1113}
1114
1115/*
1116   TI-99/8 support
1117*/
1118static READ8_DEVICE_HANDLER( read_cart_mbx8 )
1119{
1120   /* device is pcb, owner is cartslot */
1121   UINT8 value = 0;
1122   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
1123   cartridge_t *cartridge = pcb->cartridge;
1124   
1125   if ((offset >= 0x0c00) && (offset <= 0x0ffd))
1126   {
1127      /* This is the RAM area which overrides any ROM. There is no
1128         known banking behavior for the RAM, so we must assume that
1129         there is only one bank. */
1130      if (cartridge->ram_ptr != NULL)
1131         value = ((UINT8 *)cartridge->ram_ptr)[offset - 0x0c00];
1132   }
1133   else
1134   {
1135      if (cartridge->rom_ptr != NULL)
1136         value = ((UINT8 *)cartridge->rom_ptr)[offset + cartridge->rom_page * 0x2000];
1137   }
1138   return value;
1139}
1140
1141/*
1142   MBX write operation (TI-99/8)
1143*/
1144static WRITE8_DEVICE_HANDLER( write_cart_mbx8 )
1145{
1146   ti99_pcb_t *pcb = (ti99_pcb_t *)device->token;
1147   cartridge_t *cartridge = pcb->cartridge;
1148
1149   if ((offset >= 0x0c00) && (offset <= 0x0ffd))
1150   {
1151      if (cartridge->ram_ptr == NULL)
1152         return;
1153      ((UINT8 *)cartridge->ram_ptr)[offset - 0x0c00] = data;
1154   }
1155   else if (offset == 0x0ffe || offset == 0x0fff)
1156   {
1157      if (cartridge->rom_ptr==NULL)
1158         return;
1159      cartridge->rom_page = (data & 1); /* TODO: Check */
1160   }
1161}
1162
1163
1164static int assemble_mbx(const device_config *image)
1165{
1166   cartridge_t *cart;
1167
1168//   printf("assemble_mbx, %s\n", image->tag);
1169   cart = assemble_common(image);   
1170   return INIT_PASS;
1171}
1172
1173
1174/*****************************************************************************
1175  Device metadata
1176******************************************************************************/
1177
1178static DEVICE_GET_INFO(ti99_cart_common)
1179{
1180   switch(state)
1181   {
1182      /* --- the following bits of info are returned as 64-bit signed integers --- */
1183      case DEVINFO_INT_TOKEN_BYTES:
1184         info->i = sizeof(ti99_pcb_t);
1185         break;
1186      case DEVINFO_INT_INLINE_CONFIG_BYTES:
1187         info->i = 0;
1188         break;
1189      case DEVINFO_INT_CLASS:
1190         info->i = DEVICE_CLASS_PERIPHERAL;
1191         break;
1192
1193      /* --- the following bits of info are returned as pointers to functions --- */
1194      case DEVINFO_FCT_SET_INFO:
1195         /* Nothing */
1196         break;
1197      case DEVINFO_FCT_START:
1198         info->start = DEVICE_START_NAME(ti99_pcb_std);
1199         break;
1200      case DEVINFO_FCT_STOP:
1201         /* Nothing */
1202         break;
1203      case DEVINFO_FCT_RESET:
1204         /* Nothing */
1205         break;
1206      case TI99CARTINFO_FCT_6000_R:
1207         info->f = (genf *) read_cart_std; break;
1208
1209      case TI99CARTINFO_FCT_6000_W:
1210         info->f = (genf *) write_cart_std; break;
1211
1212      case TI99CARTINFO_FCT_6000_R8:
1213         info->f = (genf *) read_cart_std8; break;
1214
1215      case TI99CARTINFO_FCT_6000_W8:
1216         info->f = (genf *) write_cart_std8; break;
1217
1218      case TI99CARTINFO_FCT_CRU_R:
1219         /* Nothing */
1220         break;
1221
1222      case TI99CARTINFO_FCT_CRU_W:
1223         /* Nothing */
1224         break;
1225
1226      case TI99CART_FCT_ASSM:
1227         info->f = (genf *) assemble_std;
1228         break;
1229
1230      case TI99CART_FCT_DISASSM:
1231         info->f = (genf *) disassemble_std;
1232         break;
1233         
1234      /* --- the following bits of info are returned as NULL-terminated strings --- */
1235      case DEVINFO_STR_NAME:
1236         strcpy(info->s, "TI99 standard cartridge pcb");
1237         break;
1238      case DEVINFO_STR_FAMILY:
1239         strcpy(info->s, "TI99 cartridge pcb");
1240         break;
1241      case DEVINFO_STR_VERSION:
1242         strcpy(info->s, "1.0");
1243         break;
1244      case DEVINFO_STR_SOURCE_FILE:
1245         strcpy(info->s, __FILE__);
1246         break;
1247      case DEVINFO_STR_CREDITS:
1248         /* Nothing */
1249         break;
1250   }
1251}
1252
1253DEVICE_GET_INFO(ti99_cartridge_pcb_none)
1254{
1255   switch(state)
1256   {
1257      case DEVINFO_FCT_START:
1258         info->start = DEVICE_START_NAME(ti99_pcb_none);
1259         break;
1260
1261      case DEVINFO_STR_NAME:
1262         strcpy(info->s, "TI99 empty cartridge");
1263         break;
1264
1265      default:
1266         DEVICE_GET_INFO_CALL(ti99_cart_common);
1267         break;
1268   }
1269}
1270
1271DEVICE_GET_INFO(ti99_cartridge_pcb_std)
1272{
1273   DEVICE_GET_INFO_CALL(ti99_cart_common);
1274}
1275
1276DEVICE_GET_INFO(ti99_cartridge_pcb_paged)
1277{
1278   switch(state)
1279   {
1280      case DEVINFO_FCT_START:
1281         info->start = DEVICE_START_NAME(ti99_pcb_paged);
1282         break;
1283
1284      case TI99CART_FCT_ASSM:
1285         info->f = (genf *) assemble_paged; break;
1286
1287      case TI99CARTINFO_FCT_6000_R:
1288         info->f = (genf *) read_cart_paged;
1289         break;
1290
1291      case TI99CARTINFO_FCT_6000_W:
1292         info->f = (genf *) write_cart_paged;
1293         break;
1294
1295      case TI99CARTINFO_FCT_6000_R8:
1296         info->f = (genf *) read_cart_paged8;
1297         break;
1298
1299      case TI99CARTINFO_FCT_6000_W8:
1300         info->f = (genf *) write_cart_paged8;
1301         break;
1302
1303      case DEVINFO_STR_NAME:
1304         strcpy(info->s, "TI99 paged cartridge pcb");
1305         break;
1306
1307      default:
1308         DEVICE_GET_INFO_CALL(ti99_cart_common);
1309         break;
1310   }
1311}
1312
1313DEVICE_GET_INFO(ti99_cartridge_pcb_minimem)
1314{
1315   switch(state)
1316   {
1317      case DEVINFO_FCT_START:
1318         info->start = DEVICE_START_NAME(ti99_pcb_minimem);
1319         break;
1320
1321      case TI99CART_FCT_ASSM:
1322         info->f = (genf *) assemble_minimem;
1323         break;
1324      case TI99CARTINFO_FCT_6000_R:
1325         info->f = (genf *) read_cart_minimem;
1326         break;
1327      case TI99CARTINFO_FCT_6000_W:
1328         info->f = (genf *) write_cart_minimem;
1329         break;
1330      case TI99CARTINFO_FCT_6000_R8:
1331         info->f = (genf *) read_cart_minimem8;
1332         break;
1333      case TI99CARTINFO_FCT_6000_W8:
1334         info->f = (genf *) write_cart_minimem8;
1335         break;
1336
1337      case DEVINFO_STR_NAME:
1338         strcpy(info->s, "TI99 MiniMemory cartridge pcb");
1339         break;
1340
1341      default:
1342         DEVICE_GET_INFO_CALL(ti99_cart_common);
1343         break;
1344   }
1345}
1346
1347DEVICE_GET_INFO(ti99_cartridge_pcb_super)
1348{
1349   switch(state)
1350   {
1351      case DEVINFO_FCT_START:
1352         info->start = DEVICE_START_NAME(ti99_pcb_super);
1353         break;
1354
1355      case TI99CART_FCT_ASSM:
1356         info->f = (genf *) assemble_super;
1357         break;
1358      case TI99CARTINFO_FCT_6000_R:
1359         info->f = (genf *) read_cart_super;
1360         break;
1361      case TI99CARTINFO_FCT_6000_W:
1362         info->f = (genf *) write_cart_super;
1363         break;
1364      case TI99CARTINFO_FCT_6000_R8:
1365         info->f = (genf *) read_cart_super8;
1366         break;
1367      case TI99CARTINFO_FCT_6000_W8:
1368         info->f = (genf *) write_cart_super8;
1369         break;
1370      case TI99CARTINFO_FCT_CRU_R:
1371         info->f = (genf *) read_cart_cru;
1372         break;
1373      case TI99CARTINFO_FCT_CRU_W:
1374         info->f = (genf *) write_cart_cru;
1375         break;
1376
1377      case DEVINFO_STR_NAME:
1378         strcpy(info->s, "TI99 SuperSpace cartridge pcb");
1379         break;
1380
1381      default:
1382         DEVICE_GET_INFO_CALL(ti99_cart_common);
1383         break;
1384   }
1385}
1386
1387DEVICE_GET_INFO(ti99_cartridge_pcb_mbx)
1388{
1389   switch(state)
1390   {
1391      case DEVINFO_FCT_START:
1392         info->start = DEVICE_START_NAME(ti99_pcb_mbx);
1393         break;
1394
1395      case TI99CART_FCT_ASSM:
1396         info->f = (genf *) assemble_mbx;
1397         break;
1398      case TI99CARTINFO_FCT_6000_R:
1399         info->f = (genf *) read_cart_mbx;
1400         break;
1401      case TI99CARTINFO_FCT_6000_W:
1402         info->f = (genf *) write_cart_mbx;
1403         break;
1404      case TI99CARTINFO_FCT_6000_R8:
1405         info->f = (genf *) read_cart_mbx8;
1406         break;
1407      case TI99CARTINFO_FCT_6000_W8:
1408         info->f = (genf *) write_cart_mbx8;
1409         break;
1410
1411
1412      case DEVINFO_STR_NAME:
1413         strcpy(info->s, "TI99 MBX cartridge pcb");
1414         break;
1415
1416      default:
1417         DEVICE_GET_INFO_CALL(ti99_cart_common);
1418         break;
1419   }
1420}
1421
1422
1423/*****************************************************************************
1424  The cartridge handling of the multi-cartridge system.
1425  Every cartridge contains a PCB device. The memory handlers delegate the calls
1426  to the respective handlers of the cartridges.
1427******************************************************************************/
1428/*
1429   Initialize a cartridge. Each cartridge contains a PCB device.
1430*/
1431static DEVICE_START( ti99_cartridge )
1432{
1433   cartslot_t *cart = device->token;
1434   astring *tempstring = astring_alloc();
1435   
1436   /* find the PCB device */
1437   cart->pcb_device = devtag_get_device(
1438      device->machine,
1439      device_build_tag(tempstring, device, TAG_PCB));
1440   astring_free(tempstring);
1441}
1442
1443/*
1444   Load the cartridge image files. Apart from reading, we set pointers
1445   to the image files so that during runtime we do not need search
1446   operations.
1447*/
1448static DEVICE_IMAGE_LOAD( ti99_cartridge )
1449{   
1450   const device_config *pcbdev = cartslot_get_pcb(image);
1451   const device_config *cartsys = image->owner;
1452   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
1453
1454   int result;
1455   
1456   result = load_legacy(image);
1457   
1458   if (result != INIT_FAIL)
1459      cartslots->legacy_slots++;
1460   else
1461   {
1462      if (pcbdev != NULL)
1463      {
1464         /* If we are here, we have a multicart. */
1465         ti99_pcb_t *pcb = (ti99_pcb_t *)pcbdev->token;
1466         cartslot_t *cart = (cartslot_t *)image->token;
1467         
1468         /* try opening this as a multicart */
1469         /* This line requires that cartslot_t be included in cartslot.h,
1470         otherwise one cannot make use of multicart handling within such a
1471         custom LOAD function. */
1472         multicart_open_error me = multicart_open(image_filename(image), image->machine->gamedrv->name, MULTICART_FLAGS_LOAD_RESOURCES, &cart->mc);
1473         
1474         /* Now that we have loaded the image files, let the PCB put them all
1475         together. This means we put the images in a structure which allows
1476         for a quick access by the memory handlers. Every PCB defines an
1477         own assembly method. */
1478         if (me == MCERR_NONE)
1479            result = pcb->assemble(image);
1480         else
1481            fatalerror("Error loading multicart: %s\n", mc_error_text(me));
1482         
1483         /* This is for legacy support. If we have no multicart left
1484         but there are still legacy dumps, we switch to legacy mode. */
1485         if (result != INIT_FAIL)
1486            cartslots->multi_slots++;
1487      }
1488      else
1489      {
1490         fatalerror("Error loading multicart: no pcb found.\n");
1491      }
1492   }   
1493   return result;
1494}
1495
1496/*
1497   This is called when the cartridge is unplugged (or the emulator is
1498   stopped).
1499*/
1500static DEVICE_IMAGE_UNLOAD( ti99_cartridge )
1501{   
1502   const device_config *pcbdev;
1503   ti99_multicart_t *cartslots = (ti99_multicart_t *)image->owner->token;
1504
1505   if (image->token == NULL)
1506   {
1507      /* This means something went wrong during the pcb
1508         identification (e.g. one of the cartridge files was not
1509         found). We do not need to (and cannot) unload
1510         the cartridge. */
1511      return;
1512   }
1513   pcbdev = cartslot_get_pcb(image);
1514
1515   if (pcbdev != NULL)
1516   {
1517      ti99_pcb_t *pcb = (ti99_pcb_t *)pcbdev->token;
1518      cartslot_t *cart = (cartslot_t *)image->token;
1519     
1520      //   printf("unload\n");
1521      if (cart->mc != NULL)
1522      {
1523         /* Remove pointers and de-big-endianize RAM contents. */
1524         pcb->disassemble(image);
1525         
1526         /* Close the multicart; all RAM resources will be
1527            written to disk */
1528         multicart_close(cart->mc);
1529         cart->mc = NULL;
1530         
1531         /* See above: Legacy support. */
1532         cartslots->multi_slots--;
1533      }
1534//      else
1535//         fatalerror("Lost pointer to multicart in cartridge. Report bug.\n");
1536   }
1537   else
1538   {
1539      unload_legacy(image);
1540      cartslots->legacy_slots--;
1541   }
1542}
1543
1544/*****************************************************************************
1545  The legacy cartridge slot system.
1546 
1547  This is the system which was used for the TI family until MESS 0.130.
1548  Here, plain ROM dumps were mounted on the slots, e.g. the GROM on slot 1,
1549  ROM on slot 2. No multiple cartridges were supported.
1550 
1551  This functionality is deprecated.
1552 
1553  In order to allow for a smooth transition, this feature is included in this
1554  version. It is strongly recommmended to create suitable multicart packs.
1555 
1556  This implementation tolerates both cartridge systems in the following way:
1557  - If there are only single ROM dumps, the system switched to legacy mode.
1558  - If there is at least one multicart pack in any slot, the system switches
1559    to the multicart mode. All plain dumps become invisible; the slots appear
1560    empty to the system
1561  - Both modes may change on plugging and unplugging.
1562******************************************************************************/
1563
1564static UINT8 cartridge_grom_read_legacy(const device_config *cartsys, int cart_offset)
1565{
1566   int slot;
1567   cartridge_t *cartridge;
1568   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
1569
1570   UINT8 value;
1571   
1572   /* Get the GROM slot number. */
1573   slot = cartslots->legacy_slotnumber[SLOTC_GROM];
1574   if (slot<0)
1575      /* No GROM available. */
1576      return 0;
1577
1578   cartridge = &cartslots->cartridge[slot];
1579   
1580   if (cartridge->grom_size>0)
1581   {
1582      /* This is the reason why we need the image size: Read
1583      access beyond the image size must be detected. */
1584      if (cart_offset > cartridge->grom_size)
1585         return 0;
1586     
1587      value = (UINT8)cartridge->grom_buffer;
1588      /* read ahead */
1589      cartridge->grom_buffer = cartridge->grom_ptr[cart_offset];
1590   }
1591   else
1592   {
1593      /* We assume that empty sockets return 0 */
1594      value = 0;
1595      logerror("Empty socket at address G(%d)>%04x\n", slot, 0x6000 + cart_offset);
1596   }
1597
1598   //   printf("grom access(port = %x, address = %04x) = %02x\n", slot,  0x6000 + cart_offset*2, value);
1599   return value;
1600}
1601
1602static READ16_DEVICE_HANDLER( ti99_cart_r_legacy )
1603{
1604/*   if (hsgpl_crdena)
1605      return ti99_hsgpl_rom6_r(space, offset, mem_mask);
1606*/
1607   int slotmbx, slotmini, slotebr2, slotrom;
1608   ti99_multicart_t *cartslots = (ti99_multicart_t *)device->token;
1609
1610   slotmbx = cartslots->legacy_slotnumber[SLOTC_MBX];
1611
1612   if ((slotmbx != -1) && (offset >= 0x0600) && (offset <= 0x07fe))
1613   {
1614      /* We are in the RAM area of MBX cartridges. */
1615      return ((UINT16 *)(cartslots->cartridge[slotmbx].ram_ptr))[offset-0x0600];
1616   }
1617     
1618   slotmini = cartslots->legacy_slotnumber[SLOTC_MINIMEM];
1619   if ((slotmini != -1) && (offset >= 0x0800) && (offset <= 0x0fff))
1620   {
1621      /* We are in the RAM area of the Mini Memory cartridge. */
1622      return ((UINT16 *)(cartslots->cartridge[slotmini].ram_ptr))[offset-0x0800];
1623   }
1624
1625   /* Standard */
1626   slotrom = cartslots->legacy_slotnumber[SLOTC_CROM];
1627   if (slotrom < 0)
1628      return 0;
1629   
1630   slotebr2 = cartslots->legacy_slotnumber[SLOTC_DROM];
1631   if (slotebr2 < 0)
1632      return (((UINT16 *)cartslots->cartridge[slotrom].rom_ptr)[offset]);
1633   else
1634   {
1635      /* Extended Basic or other paged cartridge */
1636      if (cartslots->cartridge[slotrom].rom_page!=0)
1637         return (((UINT16 *)cartslots->cartridge[slotebr2].rom2_ptr)[offset]);
1638      else
1639         return (((UINT16 *)cartslots->cartridge[slotrom].rom_ptr)[offset]);
1640   }
1641   /* Super Space is not supported in legacy mode. */
1642}
1643
1644static WRITE16_DEVICE_HANDLER( ti99_cart_w_legacy )
1645{
1646   
1647/*   if (hsgpl_crdena)
1648      ti99_hsgpl_rom6_w(space, offset, data, mem_mask);
1649      return;
1650      */
1651   int slotmbx, slotmini, slotebr2, slotrom;
1652   ti99_multicart_t *cartslots = (ti99_multicart_t *)device->token;
1653
1654   slotmbx = cartslots->legacy_slotnumber[SLOTC_MBX];
1655   slotrom = cartslots->legacy_slotnumber[SLOTC_CROM];
1656   slotebr2 = cartslots->legacy_slotnumber[SLOTC_DROM];
1657   slotmini = cartslots->legacy_slotnumber[SLOTC_MINIMEM];
1658   
1659   if (slotmbx != -1)
1660   {
1661      if ((offset >= 0x0600) && (offset <= 0x07fe))
1662      {
1663         /* We are in the RAM area of MBX cartridges. */
1664         /* RAM in 0x6c00-0x6ffd (presumably non-paged) */
1665         /* mapper at 0x6ffe */
1666         COMBINE_DATA(((UINT16 *)cartslots->cartridge[slotmbx].ram_ptr) + offset - 0x0600);
1667      }         
1668      else
1669      {
1670         if ((offset == 0x07ff) && ACCESSING_BITS_8_15)
1671         {
1672            if (slotebr2 < 0 || slotrom < 0)
1673               return; /* just ignore if the cartridge is not paged. */
1674            else
1675               cartslots->cartridge[slotrom].rom_page = ((data >> 8) & 1);
1676         }
1677      }
1678      return;
1679   }
1680   if (slotmini != -1)
1681   {
1682      if ((offset >= 0x0800) && (offset <= 0x0fff))
1683         /* We are in the RAM area of a MiniMemory cartridge. */
1684         COMBINE_DATA(((UINT16 *)cartslots->cartridge[slotmini].ram_ptr) + offset - 0x0800);
1685      return;
1686   }
1687   if (slotebr2 != -1 && slotrom != -1)
1688   {
1689      cartslots->cartridge[slotrom].rom_page = (offset & 1);
1690   }
1691}
1692
1693/*
1694   TI-99/8 support.
1695*/
1696static READ8_DEVICE_HANDLER( ti99_cart_r_legacy8 )
1697{
1698/*   if (hsgpl_crdena)
1699      return ti99_hsgpl_rom6_r(space, offset, mem_mask);
1700*/
1701   int slotmbx, slotmini, slotebr2, slotrom;
1702   ti99_multicart_t *cartslots = (ti99_multicart_t *)device->token;
1703
1704   slotmbx = cartslots->legacy_slotnumber[SLOTC_MBX];
1705
1706   if ((slotmbx != -1) && (offset >= 0x0c00) && (offset <= 0x0ffd))
1707   {
1708      /* We are in the RAM area of MBX cartridges. */
1709      return ((UINT8 *)(cartslots->cartridge[slotmbx].ram_ptr))[offset-0x0c00];
1710   }
1711     
1712   slotmini = cartslots->legacy_slotnumber[SLOTC_MINIMEM];
1713   if ((slotmini != -1) && (offset >= 0x1000) && (offset <= 0x1fff))
1714   {
1715      /* We are in the RAM area of the Mini Memory cartridge. */
1716      return ((UINT8 *)(cartslots->cartridge[slotmini].ram_ptr))[offset-0x1000];
1717   }
1718
1719   /* Standard */
1720   slotrom = cartslots->legacy_slotnumber[SLOTC_CROM];
1721   if (slotrom < 0)
1722      return 0;
1723   
1724   slotebr2 = cartslots->legacy_slotnumber[SLOTC_DROM];
1725   if (slotebr2 < 0)
1726      return (((UINT8 *)cartslots->cartridge[slotrom].rom_ptr)[offset]);
1727   else
1728   {
1729      /* Extended Basic or other paged cartridge */
1730      if (cartslots->cartridge[slotrom].rom_page!=0)
1731         return (((UINT8 *)cartslots->cartridge[slotebr2].rom2_ptr)[offset]);
1732      else
1733         return (((UINT8 *)cartslots->cartridge[slotrom].rom_ptr)[offset]);
1734   }
1735   /* Super Space is not supported in legacy mode. */
1736}
1737
1738/*
1739   TI-99/8 support.
1740*/
1741static WRITE8_DEVICE_HANDLER( ti99_cart_w_legacy8 )
1742{
1743   
1744/*   if (hsgpl_crdena)
1745      ti99_hsgpl_rom6_w(space, offset, data, mem_mask);
1746      return;
1747      */
1748   int slotmbx, slotmini, slotebr2, slotrom;
1749   ti99_multicart_t *cartslots = (ti99_multicart_t *)device->token;
1750
1751   slotmbx = cartslots->legacy_slotnumber[SLOTC_MBX];
1752   slotrom = cartslots->legacy_slotnumber[SLOTC_CROM];
1753   slotebr2 = cartslots->legacy_slotnumber[SLOTC_DROM];
1754   slotmini = cartslots->legacy_slotnumber[SLOTC_MINIMEM];
1755   
1756   if (slotmbx != -1)
1757   {
1758      if ((offset >= 0x0c00) && (offset <= 0x0ffd))
1759      {
1760         /* We are in the RAM area of MBX cartridges. */
1761         /* RAM in 0x6c00-0x6ffd (presumably non-paged) */
1762         /* mapper at 0x6ffe */
1763         ((UINT8 *)cartslots->cartridge[slotmbx].ram_ptr)[offset - 0x0c00] = data;
1764      }
1765      else
1766      {
1767         if (offset == 0x0ffe || offset == 0x0fff)
1768         {
1769            if (slotebr2 < 0 || slotrom < 0)
1770               return; /* just ignore if the cartridge is not paged. */
1771            else
1772               cartslots->cartridge[slotrom].rom_page = ((data >> 8) & 1);
1773         }
1774      }
1775      return;
1776   }
1777   if (slotmini != -1)
1778   {
1779      if ((offset >= 0x1000) && (offset <= 0x1fff))
1780         /* We are in the RAM area of a MiniMemory cartridge. */
1781         ((UINT8 *)cartslots->cartridge[slotmini].ram_ptr)[offset - 0x1000] = data;
1782      return;
1783   }
1784   if (slotebr2 != -1 && slotrom != -1)
1785   {
1786      cartslots->cartridge[slotrom].rom_page = (offset & 1);
1787   }
1788}
1789
1790
1791static int load_legacy(const device_config *image)
1792{
1793   const device_config *cartsys = image->owner;
1794   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
1795
1796   /* Some comments originally in machine/ti99_4x.c */
1797     
1798   /* We identify file types according to their extension */
1799   /* Note that if we do not recognize the extension, we revert to the slot location <-> type
1800   scheme. */
1801   int id = 0;   
1802   int i;
1803   int filesize;
1804   int maxrom = 0x2000;
1805   slotc_type_t type;
1806   int savembx;
1807   
1808   const char *ch, *ch2;
1809   const char *name = image_filename(image);
1810
1811   UINT8 *grom = memory_region(image->machine, region_grom) + 0x6000;
1812   UINT16 *cartrom = (UINT16 *) (memory_region(image->machine, "maincpu") + offset_cart);
1813   UINT16 *cartrom2 = (UINT16 *) (memory_region(image->machine, "maincpu") + offset_cart + 0x2000);
1814   UINT8 *cart8rom = memory_region(image->machine, "maincpu") + offset_cart_8;
1815   UINT8 *cart8rom2  = memory_region(image->machine, "maincpu") + offset_cart_8 + 0x2000;
1816
1817   UINT16 *cartmbx;
1818   UINT8 *cartmbx8;
1819   
1820   type = SLOTC_EMPTY;
1821   
1822   /* There is a circuitry in TI99/4(a) that resets the console when a
1823   cartridge is inserted or removed.  We emulate this instead of resetting the
1824   emulator (which is the default in MESS). */
1825   /*cpu_set_input_line(machine->cpu[0], INPUT_LINE_RESET, PULSE_LINE);
1826   tms9901_reset(0);
1827   if (! has_evpc)
1828      TMS9928A_reset();
1829   if (has_evpc)
1830      v9938_reset(0);*/
1831
1832   ch = strrchr(name, '.');
1833   ch2 = (ch-1 >= name) ? ch-1 : "";
1834
1835   if (ch)
1836   {
1837      if (! mame_stricmp(ch2, "g.bin"))
1838      {
1839         /* grom */
1840         type = SLOTC_GROM;
1841      }
1842      else if (! mame_stricmp(ch2, "c.bin"))
1843      {
1844         /* rom first page */
1845         type = SLOTC_CROM;
1846      }
1847      else if (! mame_stricmp(ch2, "d.bin"))
1848      {
1849         /* rom second page */
1850         type = SLOTC_DROM;
1851      }
1852      else if (! mame_stricmp(ch2, "m.bin"))
1853      {
1854         /* rom minimemory  */
1855         type = SLOTC_MINIMEM;
1856      }
1857      else if (! mame_stricmp(ch2, "b.bin"))
1858      {
1859         /* rom MBX  */
1860         type = SLOTC_MBX;
1861      }
1862   }
1863
1864   if (type == SLOTC_EMPTY)
1865   {
1866      return INIT_FAIL;
1867   }
1868
1869   id = get_index_from_tagname(image)-1;
1870   
1871   /* Remember where the cartridge part is plugged in. */
1872   cartslots->legacy_slotnumber[type] = id;
1873   
1874   savembx = FALSE;
1875   
1876   /* We will use the already allocated buffers in the memory regions,
1877      and the pointers in the cartridge structure will point to them. */
1878   switch (type)
1879   {
1880      case SLOTC_EMPTY:
1881         break;
1882
1883      case SLOTC_GROM:
1884         filesize = image_fread(image, grom, 0xA000);
1885         cartslots->cartridge[id].grom_ptr = grom;
1886         cartslots->cartridge[id].grom_size = filesize;
1887         // printf("loaded GROM, size = %d\n", filesize);
1888         break;
1889
1890      case SLOTC_CROM:
1891         maxrom = 0x2000;
1892         if (cartslots->legacy_slotnumber[SLOTC_MINIMEM] != -1)
1893         {
1894            /* This means the NVRAM has already been loaded. So we just
1895            load 4K of ROM to avoid overwriting the NVRAM. */
1896            maxrom = 0x1000;
1897         }
1898
1899         if (cartslots->legacy_slotnumber[SLOTC_MBX] != -1)
1900         {
1901            /* This means the NVRAM has already been loaded.
1902            We need to temporarily save the RAM contents and
1903            write them back because the RAM overrides the
1904            ROM in an intermediate area. */
1905            savembx = TRUE;
1906         }
1907
1908         if (is_99_8())
1909         {
1910            // printf("** is 99/8\n");
1911            if (savembx==TRUE)
1912            {
1913               cartmbx8 = malloc(0x400);
1914               memcpy(cartmbx8, cart8rom + 0x0c00, 0x400);
1915               filesize = image_fread(image, cart8rom, maxrom);
1916               memcpy(cart8rom + 0xc00, cartmbx8, 0x400);
1917               free(cartmbx8);
1918            }
1919            else
1920            {
1921               filesize = image_fread(image, cart8rom, maxrom);
1922            }
1923            cartslots->cartridge[id].rom_ptr = cart8rom;
1924            cartslots->cartridge[id].rom_size = filesize;
1925         }
1926         else
1927         {
1928            if (savembx==TRUE)
1929            {
1930               cartmbx = malloc(0x400);
1931               memcpy(cartmbx, cartrom + 0x0600, 0x400);
1932               filesize = image_fread(image, cartrom, maxrom);
1933               memcpy(cartrom + 0x0600, cartmbx, 0x400);
1934               free(cartmbx);
1935            }
1936            filesize = image_fread(image, cartrom, maxrom);
1937            for (i = 0; i < maxrom/2; i++)
1938               ((UINT16 *)cartrom)[i] = BIG_ENDIANIZE_INT16(((UINT16 *)cartrom)[i]);
1939            cartslots->cartridge[id].rom_ptr = cartrom;
1940            cartslots->cartridge[id].rom_size = filesize;
1941         }
1942         // printf("loaded ROM, size = %d\n", filesize);
1943         break;
1944         
1945      case SLOTC_MINIMEM:
1946         /* Load the NVRAM. Need to BIG_ENDIANIZE it.
1947         MiniMemory has only one cartridge page. */
1948         if (is_99_8())
1949         {
1950            // printf("** is 99/8\n");
1951            image_battery_load(image, cart8rom+0x1000,0x1000);
1952            cartslots->cartridge[id].ram_ptr = cart8rom +  0x1000;
1953         }
1954         else
1955         {           
1956            image_battery_load(image, cartrom+0x800,0x1000);
1957            for (i = 0x800; i < 0x1000; i++)
1958               cartrom[i] = BIG_ENDIANIZE_INT16(cartrom[i]);
1959            cartslots->cartridge[id].ram_ptr = (UINT16 *)cartrom +  0x800;
1960         }
1961         cartslots->cartridge[id].ram_size = 0x1000;
1962         // printf("loaded NVRAM\n");
1963         break;
1964
1965      case SLOTC_MBX:
1966         /* Load the NVRAM. Need to BIG_ENDIANIZE it. */
1967         if (is_99_8())
1968         {
1969            // printf("** is 99/8\n");
1970            image_battery_load(image, cart8rom+0x0c00, 0x0400);
1971            cartslots->cartridge[id].ram_ptr = cart8rom +  0xc00;
1972         }
1973         else
1974         {
1975            image_battery_load(image, cartrom+0x0600, 0x0400);
1976            for (i = 0x0600; i < 0x0800; i++)
1977               cartrom[i] = BIG_ENDIANIZE_INT16(cartrom[i]);
1978            cartslots->cartridge[id].ram_ptr = (UINT16 *)cartrom +  0x600;
1979         }
1980         cartslots->cartridge[id].ram_size = 0x0400;
1981         // printf("loaded NVRAM\n");
1982         break;
1983
1984      case SLOTC_DROM:
1985         maxrom = 0x2000;
1986         if (is_99_8())
1987         {
1988            // printf("** is 99/8\n");
1989            filesize = image_fread(image, cart8rom2, maxrom);
1990            cartslots->cartridge[id].rom2_ptr = cart8rom2;
1991         }
1992         else
1993         {
1994            filesize = image_fread(image, cartrom2, maxrom);
1995            for (i = 0; i < maxrom/2; i++)
1996               cartrom2[i] = BIG_ENDIANIZE_INT16(cartrom2[i]);
1997            cartslots->cartridge[id].rom2_ptr = cartrom2;
1998         }
1999         cartslots->cartridge[id].rom_size = filesize;
2000         // printf("loaded second ROM, size = %d\n", filesize);
2001         break;
2002   }
2003   return INIT_PASS;
2004}
2005
2006static void unload_legacy(const device_config *image)
2007{
2008   int i;
2009   const device_config *cartsys = image->owner;
2010
2011   ti99_multicart_t *cartslots = (ti99_multicart_t *)cartsys->token;
2012
2013   slotc_type_t type = SLOTC_EMPTY;
2014   int slot = get_index_from_tagname(image)-1;
2015   
2016   // printf("non-rpk unload\n");
2017   
2018   for (i=0; (i < 5) && (type==SLOTC_EMPTY); i++)
2019   {
2020      if (cartslots->legacy_slotnumber[i]==slot)
2021      {
2022         cartslots->legacy_slotnumber[i] = -1;
2023         type = (slotc_type_t)i;
2024      }
2025   }
2026
2027   switch (type)
2028   {
2029      case SLOTC_EMPTY:
2030         break;
2031
2032      case SLOTC_GROM:
2033         memset(cartslots->cartridge[slot].grom_ptr, 0, 0xA000);
2034         break;
2035         
2036      case SLOTC_MINIMEM:
2037         if (is_99_8())
2038         {
2039            // printf("** is 99/8\n");
2040            image_battery_save(image, cartslots->cartridge[slot].ram_ptr, 0x1000);
2041            memset(cartslots->cartridge[slot].ram_ptr, 0, 0x1000);
2042         }
2043         else
2044         {
2045            /* We BIG_ENDIANIZE before saving. This is consistent
2046            with the cartridge save format. */
2047            UINT16 *ramcont = (UINT16 *)cartslots->cartridge[slot].ram_ptr;
2048            for (i = 0; i < 0x0800; i++)
2049               ramcont[i] = BIG_ENDIANIZE_INT16(ramcont[i]);
2050            image_battery_save(image, ramcont, 0x1000);
2051            memset(ramcont, 0, 0x1000);
2052         }
2053         break;
2054      case SLOTC_MBX:
2055         if (is_99_8())
2056         {
2057            // printf("** is 99/8\n");
2058            image_battery_save(image, cartslots->cartridge[slot].ram_ptr, 0x0400);
2059            memset(cartslots->cartridge[slot].ram_ptr, 0, 0x0400);           
2060         }
2061         else
2062         {
2063            /* We BIG_ENDIANIZE before saving. This is consistent
2064            with the cartridge save format. */
2065            UINT16 *ramcont = (UINT16 *)cartslots->cartridge[slot].ram_ptr;
2066            for (i = 0; i < 0x0200; i++)
2067               ramcont[i] = BIG_ENDIANIZE_INT16(ramcont[i]);
2068            image_battery_save(image, ramcont, 0x0400);
2069            memset(ramcont, 0, 0x0400);
2070         }         
2071         break;
2072      case SLOTC_CROM:
2073         if (cartslots->legacy_slotnumber[SLOTC_MINIMEM]!=-1)
2074            /* Don't wipe the RAM before it is saved. */
2075            memset(cartslots->cartridge[slot].rom_ptr, 0, 0x1000);
2076         else
2077            memset(cartslots->cartridge[slot].rom_ptr, 0, 0x2000);
2078         break;
2079         
2080      case SLOTC_DROM:
2081         cartslots->cartridge[slot].rom_page = 0;
2082         memset(cartslots->cartridge[slot].rom2_ptr, 0, 0x2000);
2083         break;
2084   }
2085
2086   clear_slot(cartsys, slot);
2087}
2088
2089/*****************************************************************************
2090  The overall multi-cartridge slot system. It contains instances of
2091  cartridges which contain PCB devices. The memory handlers delegate the calls
2092  to the respective handlers of the cartridges.
2093 
2094  Note that the term "multi-cartridge system" and "multicart" are not the same:
2095  A "multicart" may contain multiple resources, organized on a PCB. The multi-
2096  cart system may thus host multiple multicarts.
2097 
2098  Actually, the name of the device should be changed (however, the device name
2099  length is limited)
2100******************************************************************************/
2101/*
2102   Instantiation of a multicart system for the TI family.
2103*/
2104static DEVICE_START(ti99_multicart)
2105{
2106   int i;
2107//   printf("DEVICE_START(ti99_multicart)\n");
2108   ti99_multicart_t *cartslots = (ti99_multicart_t *)device->token;
2109
2110   /* Save this in the shortcut; we don't want to look for it each time
2111      that we have a memory access. And currently we do not plan for
2112      multiple multicart instances. */
2113   cartslots->active_slot = 0;
2114   cartslots->next_free_slot = 0;
2115   
2116   for (i=0; i < NUMBER_OF_CARTRIDGE_SLOTS; i++)
2117   {
2118      cartslots->cartridge[i].pcb = NULL;
2119   }
2120   
2121   /* Initialize the legacy system. */
2122   cartslots->legacy_slotnumber[SLOTC_GROM] = -1;
2123   cartslots->legacy_slotnumber[SLOTC_CROM] = -1;
2124   cartslots->legacy_slotnumber[SLOTC_DROM] = -1;
2125   cartslots->legacy_slotnumber[SLOTC_MINIMEM] = -1;
2126   cartslots->legacy_slotnumber[SLOTC_MBX] = -1;
2127   
2128   cartslots->legacy_slots = 0;
2129   cartslots->multi_slots = 0;
2130   
2131   /* The cartslot system is initialized now. The cartridges themselves
2132      need to check whether their parts are available. */
2133}
2134   
2135static DEVICE_STOP(ti99_multicart)
2136{
2137//   printf("DEVICE_STOP(ti99_multicart)\n");
2138}
2139/*
2140   Accesses the ROM regions of the cartridge for reading.
2141   This is the area from 0x6000 to 0x7fff. Each cartridge is reponsible
2142   for all kinds of magic which is done inside this area, like swapping
2143   banks or doing other control actions. These activities beyond simple
2144   reading are cartridge-specific. Each kind of cartridge requires a
2145   new type.
2146   The currently set GROM bank determines which cartridge is actually
2147   accessed, also for ROM reads.
2148*/
2149READ16_DEVICE_HANDLER( ti99_multicart_r )
2150{
2151   ti99_multicart_t *cartslots = (ti99_multicart_t *) device->token;
2152   cartridge_t *cart = &cartslots->cartridge[cartslots->active_slot];
2153
2154   /* Idle the CPU */
2155        cpu_adjust_icount(cputag_get_cpu(device->machine, "maincpu"),-4);
2156
2157   // Alternatively:
2158   // cpu_spinuntil_time(device->machine->cpu[0], ATTOTIME_IN_USEC(6));
2159
2160//   printf("rpk = %d, bin = %d\n", cartslots->multi_slots,cartslots->legacy_slots);
2161
2162   /* Handle legacy mode. */
2163   if (in_legacy_mode(device))
2164   {
2165      return ti99_cart_r_legacy(device, offset, mem_mask);
2166   }   
2167   
2168   if (cart->pcb!=NULL)
2169   {
2170//      printf("accessing cartridge in slot %d\n", cartslots->active_slot);
2171      ti99_pcb_t *pcbdef = (ti99_pcb_t *)cart->pcb->token;
2172      return (*pcbdef->read)(cart->pcb, offset, mem_mask);
2173   }
2174   else
2175   {
2176//      printf("No cartridge in slot %d\n", cartslots->active_slot);
2177      return 0;
2178   }
2179}
2180
2181/*
2182   Accesses the ROM regions of the cartridge for writing.
2183   This is the area from 0x6000 to 0x7fff. Each cartridge is reponsible
2184   for all kinds of magic which is done inside this area. Specifically,
2185   writing is often used to swap banks for subsequent reads. The actual
2186   effect of writing (beyond setting values in a RAM chip) are
2187   cartridge-specific and must be handled within the cartridge pcb.
2188   The currently set GROM bank determines which cartridge is actually
2189   accessed, also for ROM reads.
2190*/
2191WRITE16_DEVICE_HANDLER( ti99_multicart_w )
2192{
2193   ti99_multicart_t *cartslots = (ti99_multicart_t *) device->token;
2194
2195   cartridge_t *cart = &cartslots->cartridge[cartslots->active_slot];
2196   
2197   /* Idle the CPU */
2198        cpu_adjust_icount(cputag_get_cpu(device->machine, "maincpu"),-4);
2199//   cpu_spinuntil_time(device->machine->cpu[0], ATTOTIME_IN_USEC(6));
2200
2201   /* Handle legacy mode. */
2202   if (in_legacy_mode(device))
2203   {
2204      ti99_cart_w_legacy(device, offset, data, mem_mask);
2205   }   
2206   else
2207   {
2208      if (cart->pcb!=NULL)
2209      {
2210//         printf("writing to cartridge in slot %d\n", cartslots->active_slot);
2211         ti99_pcb_t *pcbdef = (ti99_pcb_t *)cart->pcb->token;
2212         (*pcbdef->write)(cart->pcb, offset, data, mem_mask);
2213      }
2214   }
2215}
2216
2217/*
2218        CRU interface to cartridges, used only by SuperSpace-style cartridges.
2219        The SuperSpace has a CRU-based memory mapper. It is accessed via the
2220        cartridge slot on CRU base >0800.
2221*/
2222READ8_DEVICE_HANDLER( ti99_multicart_cru_r )
2223{
2224   ti99_multicart_t *cartslots = (ti99_multicart_t *) device->token;
2225   cartridge_t *cart = &cartslots->cartridge[cartslots->active_slot];
2226
2227   if (cart->pcb!=NULL)
2228   {
2229      ti99_pcb_t *pcbdef = (ti99_pcb_t *)cart->pcb->token;
2230      if (pcbdef->cruread != NULL)
2231         return (*pcbdef->cruread)(cart->pcb, offset);
2232   }
2233   return 0;
2234}   
2235   
2236/*
2237        Write cartridge mapper CRU interface (SuperSpace)
2238*/
2239WRITE8_DEVICE_HANDLER( ti99_multicart_cru_w )
2240{
2241   ti99_multicart_t *cartslots = (ti99_multicart_t *) device->token;
2242   cartridge_t *cart = &cartslots->cartridge[cartslots->active_slot];
2243
2244   if (cart->pcb!=NULL)
2245   {
2246      ti99_pcb_t *pcbdef = (ti99_pcb_t *)cart->pcb->token;
2247      if (pcbdef->cruwrite != NULL)
2248         (*pcbdef->cruwrite)(cart->pcb, offset, data);
2249   }
2250}
2251
2252/*
2253   TI-99/8 support. We have only 8 bit memory access, so we adapt all
2254   functions to this width.
2255*/
2256READ8_DEVICE_HANDLER(ti99_multicart8_r)
2257{
2258   ti99_multicart_t *cartslots = (ti99_multicart_t *) device->token;
2259   cartridge_t *cart = &cartslots->cartridge[cartslots->active_slot];
2260
2261   /* Idle the CPU. TODO: Check whether this is correct for this machine. */
2262        cpu_adjust_icount(cputag_get_cpu(device->machine, "maincpu"),-4);
2263
2264   /* Handle legacy mode. */
2265   if (in_legacy_mode(device))
2266   {
2267      return ti99_cart_r_legacy8(device, offset);
2268   }   
2269   
2270   if (cart->pcb!=NULL)
2271   {
2272//      printf("accessing cartridge in slot %d\n", cartslots->active_slot);
2273      ti99_pcb_t *pcbdef = (ti99_pcb_t *)cart->pcb->token;
2274      return (*pcbdef->read8)(cart->pcb, offset);
2275   }
2276   else
2277   {
2278//      printf("No cartridge in slot %d\n", cartslots->active_slot);
2279      return 0;
2280   }
2281}
2282
2283WRITE8_DEVICE_HANDLER(ti99_multicart8_w)
2284{
2285   ti99_multicart_t *cartslots = (ti99_multicart_t *) device->token;
2286
2287   cartridge_t *cart = &cartslots->cartridge[cartslots->active_slot];
2288
2289   /* Idle the CPU. TODO: Check whether this is correct for this machine. */
2290        cpu_adjust_icount(cputag_get_cpu(device->machine, "maincpu"),-4);
2291   
2292   /* Handle legacy mode. */
2293   if (in_legacy_mode(device))
2294   {
2295      ti99_cart_w_legacy8(device, offset, data);
2296   }   
2297   else
2298   {
2299      if (cart->pcb!=NULL)
2300      {
2301         ti99_pcb_t *pcbdef = (ti99_pcb_t *)cart->pcb->token;
2302         (*pcbdef->write8)(cart->pcb, offset, data);
2303      }
2304   }
2305}
2306
2307static MACHINE_DRIVER_START(ti99_multicart)
2308    MDRV_CARTSLOT_ADD("cartridge1")
2309   MDRV_CARTSLOT_EXTENSION_LIST("rpk,bin")
2310   MDRV_CARTSLOT_PCBTYPE(0, "none", TI99_CARTRIDGE_PCB_NONE)
2311   MDRV_CARTSLOT_PCBTYPE(1, "standard", TI99_CARTRIDGE_PCB_STD)
2312   MDRV_CARTSLOT_PCBTYPE(2, "paged", TI99_CARTRIDGE_PCB_PAGED)
2313   MDRV_CARTSLOT_PCBTYPE(3, "minimem", TI99_CARTRIDGE_PCB_MINIMEM)
2314   MDRV_CARTSLOT_PCBTYPE(4, "super", TI99_CARTRIDGE_PCB_SUPER)
2315   MDRV_CARTSLOT_PCBTYPE(5, "mbx", TI99_CARTRIDGE_PCB_MBX)
2316//   MDRV_CARTSLOT_PCBTYPE(6, "gk", TI99_CARTRIDGE_PCB_GK)
2317   
2318   MDRV_CARTSLOT_START(ti99_cartridge)
2319   MDRV_CARTSLOT_LOAD(ti99_cartridge)
2320   MDRV_CARTSLOT_UNLOAD(ti99_cartridge)
2321   
2322   MDRV_CARTSLOT_ADD("cartridge2")
2323   MDRV_CARTSLOT_EXTENSION_LIST("rpk,bin")
2324   MDRV_CARTSLOT_PCBTYPE(0, "none", TI99_CARTRIDGE_PCB_NONE)
2325   MDRV_CARTSLOT_PCBTYPE(1, "standard", TI99_CARTRIDGE_PCB_STD)
2326   MDRV_CARTSLOT_PCBTYPE(2, "paged", TI99_CARTRIDGE_PCB_PAGED)
2327   MDRV_CARTSLOT_PCBTYPE(3, "minimem", TI99_CARTRIDGE_PCB_MINIMEM)
2328   MDRV_CARTSLOT_PCBTYPE(4, "super", TI99_CARTRIDGE_PCB_SUPER)
2329   MDRV_CARTSLOT_PCBTYPE(5, "mbx", TI99_CARTRIDGE_PCB_MBX)
2330//   MDRV_CARTSLOT_PCBTYPE(6, "gk", TI99_CARTRIDGE_PCB_GK)
2331   MDRV_CARTSLOT_START(ti99_cartridge)
2332   MDRV_CARTSLOT_LOAD(ti99_cartridge)
2333   MDRV_CARTSLOT_UNLOAD(ti99_cartridge)
2334
2335   MDRV_CARTSLOT_ADD("cartridge3")
2336   MDRV_CARTSLOT_EXTENSION_LIST("rpk,bin")
2337   MDRV_CARTSLOT_PCBTYPE(0, "none", TI99_CARTRIDGE_PCB_NONE)
2338   MDRV_CARTSLOT_PCBTYPE(1, "standard", TI99_CARTRIDGE_PCB_STD)
2339   MDRV_CARTSLOT_PCBTYPE(2, "paged", TI99_CARTRIDGE_PCB_PAGED)
2340   MDRV_CARTSLOT_PCBTYPE(3, "minimem", TI99_CARTRIDGE_PCB_MINIMEM)
2341   MDRV_CARTSLOT_PCBTYPE(4, "super", TI99_CARTRIDGE_PCB_SUPER)
2342   MDRV_CARTSLOT_PCBTYPE(5, "mbx", TI99_CARTRIDGE_PCB_MBX)
2343//   MDRV_CARTSLOT_PCBTYPE(6, "gk", TI99_CARTRIDGE_PCB_GK)
2344   MDRV_CARTSLOT_START(ti99_cartridge)
2345   MDRV_CARTSLOT_LOAD(ti99_cartridge)
2346   MDRV_CARTSLOT_UNLOAD(ti99_cartridge)
2347
2348   MDRV_CARTSLOT_ADD("cartridge4")
2349   MDRV_CARTSLOT_EXTENSION_LIST("rpk,bin")
2350   MDRV_CARTSLOT_PCBTYPE(0, "none", TI99_CARTRIDGE_PCB_NONE)
2351   MDRV_CARTSLOT_PCBTYPE(1, "standard", TI99_CARTRIDGE_PCB_STD)
2352   MDRV_CARTSLOT_PCBTYPE(2, "paged", TI99_CARTRIDGE_PCB_PAGED)
2353   MDRV_CARTSLOT_PCBTYPE(3, "minimem", TI99_CARTRIDGE_PCB_MINIMEM)
2354   MDRV_CARTSLOT_PCBTYPE(4, "super", TI99_CARTRIDGE_PCB_SUPER)
2355   MDRV_CARTSLOT_PCBTYPE(5, "mbx", TI99_CARTRIDGE_PCB_MBX)
2356//   MDRV_CARTSLOT_PCBTYPE(6, "gk", TI99_CARTRIDGE_PCB_GK)
2357   MDRV_CARTSLOT_START(ti99_cartridge)
2358   MDRV_CARTSLOT_LOAD(ti99_cartridge)
2359   MDRV_CARTSLOT_UNLOAD(ti99_cartridge)
2360MACHINE_DRIVER_END
2361
2362static DEVICE_SET_INFO(ti99_multicart)
2363{
2364//   printf("device_set_info(ti99-cart)");
2365}
2366
2367
2368DEVICE_GET_INFO(ti99_multicart)
2369{
2370   switch (state)
2371   {
2372      /* --- the following bits of info are returned as pointers to data --- */
2373      case DEVINFO_PTR_MACHINE_CONFIG:
2374         info->machine_config = MACHINE_DRIVER_NAME(ti99_multicart); break;
2375
2376      /* --- the following bits of info are returned as NULL-terminated strings --- */
2377      case DEVINFO_STR_NAME:
2378         strcpy(info->s, "TI99 Multi-Cartridge Extender");
2379         break;
2380
2381      case DEVINFO_INT_TOKEN_BYTES: /* private storage, automatically allocated */
2382         info->i = sizeof(ti99_multicart_t);
2383         break;
2384      case DEVINFO_INT_CLASS:
2385         info->i = DEVICE_CLASS_PERIPHERAL;
2386         break;
2387         
2388      case DEVINFO_STR_FAMILY:
2389         strcpy(info->s, "Multi-cartridge system");
2390         break;
2391      case DEVINFO_STR_VERSION:
2392         strcpy(info->s, "1.0");
2393         break;
2394      case DEVINFO_STR_SOURCE_FILE:
2395         strcpy(info->s, __FILE__);
2396         break;
2397      /* --- the following bits of info are returned as pointers to functions --- */
2398      case DEVINFO_FCT_SET_INFO:
2399         info->set_info = DEVICE_SET_INFO_NAME(ti99_multicart);
2400         break;         
2401      case DEVINFO_FCT_START:
2402         info->start = DEVICE_START_NAME(ti99_multicart);
2403         break;
2404      case DEVINFO_FCT_STOP:
2405         info->stop = DEVICE_STOP_NAME(ti99_multicart);
2406         break;
2407      case DEVINFO_FCT_RESET:
2408         /* Nothing */
2409         break;
2410   }
2411}
2412
src/mess/devices/ti99cart.h
r0r4597
1/*********************************************************************
2
3   ti99cart.h
4
5   TI99 family cartridge management
6
7*********************************************************************/
8
9#ifndef __TI99CART_H__
10#define __TI99CART_H__
11
12#include "device.h"
13#include "image.h"
14
15#define TI99_MULTICART         DEVICE_GET_INFO_NAME(ti99_multicart)
16
17#define TI99_CARTRIDGE_PCB_NONE      DEVICE_GET_INFO_NAME(ti99_cartridge_pcb_none)
18#define TI99_CARTRIDGE_PCB_STD      DEVICE_GET_INFO_NAME(ti99_cartridge_pcb_std)
19#define TI99_CARTRIDGE_PCB_PAGED   DEVICE_GET_INFO_NAME(ti99_cartridge_pcb_paged)
20#define TI99_CARTRIDGE_PCB_MINIMEM   DEVICE_GET_INFO_NAME(ti99_cartridge_pcb_minimem)
21#define TI99_CARTRIDGE_PCB_SUPER   DEVICE_GET_INFO_NAME(ti99_cartridge_pcb_super)
22#define TI99_CARTRIDGE_PCB_MBX      DEVICE_GET_INFO_NAME(ti99_cartridge_pcb_mbx)
23
24/* We set the number of slots to 8, although we may have up to 16. From a
25   logical point of view we could have 256, but the operating system only checks
26   the first 16 banks. */
27#define NUMBER_OF_CARTRIDGE_SLOTS 8
28
29/* GROM_port_t: descriptor for a port of 8 GROMs */
30struct _GROM_port_t
31{
32   /* pointer to GROM data */
33   UINT8 *data_ptr;
34   /* current address pointer for the active GROM in port (16 bits) */
35   unsigned int addr;
36   /* GROM data buffer */
37   UINT8 buf;
38   /* internal flip-flops that are set after the first access to the GROM
39   address so that next access is mapped to the LSB, and cleared after each
40   data access */
41   char raddr_LSB, waddr_LSB;
42};
43typedef struct _GROM_port_t GROM_port_t;
44
45/* Generic TI99 cartridge structure. */
46struct _cartridge_t
47{
48   /* PCB device associated to this cartridge. If NULL, the slot is empty. */
49   const device_config *pcb;
50   
51        /* GROM buffer size. */
52        int grom_size;
53
54        /* ROM page. */
55        int rom_page;
56        /* RAM page. */
57        int ram_page;
58
59        /* ROM buffer size. All banks have equal sizes. */
60        int rom_size;
61        /* RAM buffer size. All banks have equal sizes. */
62        int ram_size;
63
64        /* pointer to GROM data. */
65        UINT8 *grom_ptr;
66
67        /* GROM buffered data output. */
68        UINT8 grom_buffer;
69
70        /* ROM buffer. We are using this for both 16 bit (99/4a) and 8 bit
71      (99/8) access. */
72        void *rom_ptr;
73   
74        /* ROM buffer for the second bank of paged cartridges. Other cartridges
75      usually store their ROM in one large file. */
76        void *rom2_ptr;
77
78        /* RAM buffer. The persistence service is done by the cartridge system.
79      The RAM space is a consecutive space; all banks are in one buffer. */
80        void *ram_ptr;
81};
82typedef struct _cartridge_t cartridge_t;
83
84/* Functions to be called from the console. */
85void   cartridge_slot_set(const device_config *cartsys, int slotnumber);
86void   lock_cartridge_slot(const device_config *cartsys, int slotnumber);
87UINT8  cartridge_grom_read(const device_config *cartsys, int offset);
88
89DEVICE_GET_INFO(ti99_multicart);
90
91/* cartridge PCB types */
92DEVICE_GET_INFO(ti99_cartridge_pcb_std);
93DEVICE_GET_INFO(ti99_cartridge_pcb_exbas);
94DEVICE_GET_INFO(ti99_cartridge_pcb_minimem);
95DEVICE_GET_INFO(ti99_cartridge_pcb_supersp);
96DEVICE_GET_INFO(ti99_cartridge_pcb_mbx);
97
98READ16_DEVICE_HANDLER(ti99_multicart_r);
99WRITE16_DEVICE_HANDLER(ti99_multicart_w);
100
101/* Support for TI-99/8 */
102READ8_DEVICE_HANDLER(ti99_multicart8_r);
103WRITE8_DEVICE_HANDLER(ti99_multicart8_w);
104
105/* CRU handlers for SuperSpace cartridge */
106READ8_DEVICE_HANDLER( ti99_multicart_cru_r );
107WRITE8_DEVICE_HANDLER( ti99_multicart_cru_w );
108
109#define MDRV_TI99_CARTRIDGE_ADD(_tag) \
110   MDRV_DEVICE_ADD(_tag, TI99_MULTICART, 0)
111
112#endif /* __TI99CART_H__ */
src/mess/devices/cartslot.c
r4596r4597
1111#include "cartslot.h"
1212#include "multcart.h"
1313
14
1514/***************************************************************************
1615    CONSTANTS
1716***************************************************************************/
1817
19#define TAG_PCB      "pcb"
20
21
22/***************************************************************************
23    TYPE DEFINITIONS
24***************************************************************************/
25
26typedef struct _cartslot_t cartslot_t;
27struct _cartslot_t
28{
29   const device_config *pcb_device;
30   multicart *mc;
31};
32
33
3418enum _process_mode
3519{
3620   PROCESS_CLEAR,
r4596r4597
194178
195179
196180/*-------------------------------------------------
197    cartslot_get_pcb
181    cartslot_get_socket
198182-------------------------------------------------*/
199183
200184void *cartslot_get_socket(const device_config *device, const char *socket_name)
r4596r4597
219203   return result;
220204}
221205
206/*-------------------------------------------------
207    cartslot_get_resource_length
208-------------------------------------------------*/
222209
210int cartslot_get_resource_length(const device_config *device, const char *socket_name)
211{
212   cartslot_t *cart = get_token(device);
213   int result = 0;
214
215   if (cart->mc != NULL)
216   {
217      const multicart_socket *socket;
218
219      for (socket = cart->mc->sockets; socket != NULL; socket = socket->next)
220      {
221         if (!strcmp(socket->id, socket_name)) {
222            break;
223         }
224      }
225      if (socket != NULL)
226         result = socket->resource->length;
227   }
228   else
229      result = 0;
230
231   return result;
232}
233
234
223235/*-------------------------------------------------
224236    DEVICE_START( cartslot )
225237-------------------------------------------------*/
r4596r4597
262274      return (*config->device_load)(image);
263275
264276   /* try opening this as if it were a multicart */
265   multicart_open(image_filename(image), MULTICART_FLAGS_LOAD_RESOURCES, &cart->mc);
277   multicart_open(image_filename(image), image->machine->gamedrv->name, MULTICART_FLAGS_LOAD_RESOURCES, &cart->mc);
266278   if (cart->mc == NULL)
267279   {
268280      /* otherwise try the normal route */
r4596r4597
316328   if (image_exists(device))
317329   {
318330      /* try opening this as if it were a multicart */
319      multicart_open(image_filename(device), MULTICART_FLAGS_DONT_LOAD_RESOURCES, &mc);
320      if (mc != NULL)
331      multicart_open_error me = multicart_open(image_filename(device), device->machine->gamedrv->name, MULTICART_FLAGS_DONT_LOAD_RESOURCES, &mc);
332      if (me == MCERR_NONE)
321333      {
322334         /* this was a multicart - read from it */
323335         astring_cpyc(pcb_name, mc->pcb_type);
324336         multicart_close(mc);
325337      }
326      else if (image_pcb(device) != NULL)
338      else
327339      {
328         /* read from hash file */
329         astring_cpyc(pcb_name, image_pcb(device));
340         if (me != MCERR_NOT_MULTICART)
341            fatalerror("multicart error: %s\n", mc_error_text(me));
342         if (image_pcb(device) != NULL)
343         {
344            /* read from hash file */
345            astring_cpyc(pcb_name, image_pcb(device));
346         }
330347      }
331348
332349      /* look for PCB type with matching name */
src/mess/devices/cartslot.h
r4596r4597
99
1010#include "device.h"
1111#include "image.h"
12#include "multcart.h"
1213
1314
1415/***************************************************************************
1516    MACROS
1617***************************************************************************/
18#define TAG_PCB      "pcb"
1719
1820#define ROM_CART_LOAD(tag,offset,length,flags)   \
1921   { NULL, tag, offset, length, ROMENTRYTYPE_CARTRIDGE | (flags) },
r4596r4597
3234    TYPE DEFINITIONS
3335***************************************************************************/
3436
37typedef struct _cartslot_t cartslot_t;
38struct _cartslot_t
39{
40   const device_config *pcb_device;
41   multicart *mc;
42};
43
44
3545typedef struct _cartslot_pcb_type cartslot_pcb_type;
3646struct _cartslot_pcb_type
3747{
r4596r4597
6575/* accesses a particular socket */
6676void *cartslot_get_socket(const device_config *device, const char *socket_name);
6777
78/* accesses a particular socket; gets the length of the associated resource */
79int cartslot_get_resource_length(const device_config *device, const char *socket_name);
6880
81
6982/***************************************************************************
7083    DEVICE CONFIGURATION MACROS
7184***************************************************************************/
src/mess/devices/multcart.c
r4596r4597
1414#include "driver.h"
1515#include "compcfg.h"
1616
17
1817/***************************************************************************
1918    TYPE DEFINITIONS
2019***************************************************************************/
r4596r4597
2928typedef struct _multicart_load_state multicart_load_state;
3029struct _multicart_load_state
3130{
32   multicart *            multicart;
33   zip_file *            zip;
31   multicart *         multicart;
32   zip_file *         zip;
3433   xml_data_node *         layout_xml;
3534   xml_data_node *         resources_node;
3635   xml_data_node *         pcb_node;
37   multicart_resource *   resources;
36   multicart_resource *      resources;
3837   multicart_socket *      sockets;
3938};
4039
40static const char multicart_error_text[14][30] =
41{
42   "no error",
43   "not a multicart",
44   "module definition corrupt",
45   "out of memory",
46   "xml format error",
47   "invalid file reference",
48   "zip file error",
49   "missing ram length",
50   "invalid ram specification",
51   "unknown resource type",
52   "invalid resource reference",
53   "invalid file format",
54   "missing layout",
55   "no pcb or resource found"
56};
4157
58const char *mc_error_text(multicart_open_error error)
59{
60   return multicart_error_text[(int)error];
61}
62
4263/***************************************************************************
4364    IMPLEMENTATION
4465***************************************************************************/
r4596r4597
123144   /* locate the 'file' attribute */
124145   file = xml_get_attribute_string(resource_node, "file", NULL);
125146   if (file == NULL)
126      return MCERR_NOT_MULTICART;
147      return MCERR_XML_ERROR;
127148
128149   /* locate the file in the ZIP file */
129150   header = find_file(state->zip, file);
130151   if (header == NULL)
131      return MCERR_NOT_MULTICART;
152      return MCERR_INVALID_FILE_REF;
132153   resource->length = header->uncompressed_length;
133154
134155   /* allocate bytes for this resource */
r4596r4597
139160   /* and decompress it */
140161   ziperr = zip_file_decompress(state->zip, resource->ptr, resource->length);
141162   if (ziperr != ZIPERR_NONE)
142      return MCERR_NOT_MULTICART;
163      return MCERR_ZIP_ERROR;
143164
144165   return MCERR_NONE;
145166}
r4596r4597
153174   multicart_resource *resource)
154175{
155176   const char *length_string;
177   const char *ram_type;
178   const char *ram_filename;
156179
180   astring *ram_pathname;
181   
157182   /* locate the 'length' attribute */
158183   length_string = xml_get_attribute_string(resource_node, "length", NULL);
159184   if (length_string == NULL)
160      return MCERR_NOT_MULTICART;
161
185      return MCERR_MISSING_RAM_LENGTH;
186   
162187   /* ...and parse it */
163188   resource->length = ram_parse_string(length_string);
164189   if (resource->length <= 0)
165      return MCERR_NOT_MULTICART;
166
190      return MCERR_INVALID_RAM_SPEC;
191   
192   /* allocate bytes for this resource */
193   resource->ptr = pool_malloc(state->multicart->data->pool, resource->length);
194   if (resource->ptr == NULL)
195      return MCERR_OUT_OF_MEMORY;
196   
197   /* Is this a persistent RAM resource? Then try to load it. */
198   ram_type = xml_get_attribute_string(resource_node, "type", NULL);
199   if (ram_type != NULL)
200   {
201      if (strcmp(ram_type, "persistent")==0)
202      {
203         /* Get the file name. */
204         ram_filename = xml_get_attribute_string(resource_node, "file", NULL);
205         if (ram_filename==NULL)
206            return MCERR_XML_ERROR;
207         
208         ram_pathname = astring_assemble_3(astring_alloc(), state->multicart->gamedrv_name, PATH_SEPARATOR, ram_filename);
209         
210         /* Save the file name so that we can write the contents on unloading.
211            If the RAM resource has no filename, we know that it was volatile only. */
212         resource->filename = pool_strdup(state->multicart->data->pool, astring_c(ram_pathname));
213         if (resource->filename == NULL)
214            return MCERR_OUT_OF_MEMORY;
215         
216         astring_free(ram_pathname);
217         
218         image_battery_load_by_name(resource->filename, resource->ptr, resource->length);
219      }
220      /* else this type is volatile, in which case we just have
221         a memory expansion */
222   }
167223   return MCERR_NONE;
168224}
169225
r4596r4597
183239   /* get the 'id' attribute; error if not present */
184240   id = xml_get_attribute_string(resource_node, "id", NULL);
185241   if (id == NULL)
186      return MCERR_NOT_MULTICART;
242      return MCERR_XML_ERROR;
187243
188244   /* allocate memory for the resource */
189245   resource = pool_malloc(state->multicart->data->pool, sizeof(*resource));
r4596r4597
212268         break;
213269
214270      default:
215         return MCERR_NOT_MULTICART;
271         return MCERR_UNKNOWN_RESOURCE_TYPE;
216272   }
217273
218274   /* append the resource */
r4596r4597
249305   return MCERR_NONE;
250306}
251307
308/*-------------------------------------------------
309    save_ram_resources. This is important for persistent RAM. All
310    resources were allocated within the memory pool of this device and will
311    be freed on multicart_close.
312-------------------------------------------------*/
252313
314static multicart_open_error save_ram_resources(multicart *cart)
315{
316   const multicart_resource *resource;
317   
318   for (resource = cart->resources; resource != NULL; resource = resource->next)
319   {
320      if ((resource->type == MULTICART_RESOURCE_TYPE_RAM) && (resource->filename != NULL))
321      {
322         image_battery_save_by_name(resource->filename, resource->ptr, resource->length);
323      }
324   }
325   return MCERR_NONE;
326}
327
253328/*-------------------------------------------------
254329    load_socket
255330-------------------------------------------------*/
r4596r4597
266341   id = xml_get_attribute_string(socket_node, "id", NULL);
267342   uses = xml_get_attribute_string(socket_node, "uses", NULL);
268343   if ((id == NULL) || (uses == NULL))
269      return MCERR_NOT_MULTICART;
344      return MCERR_XML_ERROR;
270345
271346   /* find the resource */
272347   for (resource = state->multicart->resources; resource != NULL; resource = resource->next)
r4596r4597
275350         break;
276351   }
277352   if (resource == NULL)
278      return MCERR_NOT_MULTICART;
353      return MCERR_INVALID_RESOURCE_REF;
279354
280355   /* create the socket */
281356   socket = pool_malloc(state->multicart->data->pool, sizeof(*socket));
r4596r4597
342417    multicart_open - opens a multicart
343418-------------------------------------------------*/
344419
345multicart_open_error multicart_open(const char *filename, multicart_load_flags load_flags, multicart **cart)
420multicart_open_error multicart_open(const char *filename, const char *gamedrv, multicart_load_flags load_flags, multicart **cart)
346421{
347422   multicart_open_error err;
348423   zip_error ziperr;
r4596r4597
392467   header = find_file(state.zip, "layout.xml");
393468   if (header == NULL)
394469   {
395      err = MCERR_NOT_MULTICART;
470      err = MCERR_MISSING_LAYOUT;
396471      goto done;
397472   }
398473
r4596r4597
408483   ziperr = zip_file_decompress(state.zip, layout_text, header->uncompressed_length);
409484   if (ziperr != ZIPERR_NONE)
410485   {
411      err = MCERR_NOT_MULTICART;
486      err = MCERR_ZIP_ERROR;
412487      goto done;
413488   }
414489   layout_text[header->uncompressed_length] = '\0';
r4596r4597
417492   state.layout_xml = xml_string_read(layout_text, NULL);
418493   if (state.layout_xml == NULL)
419494   {
420      err = MCERR_NOT_MULTICART;
495      err = MCERR_XML_ERROR;
421496      goto done;
422497   }
423498
424499   /* locate the PCB node */
425500   if (!find_pcb_and_resource_nodes(state.layout_xml, &state.pcb_node, &state.resources_node))
426501   {
427      err = MCERR_NOT_MULTICART;
502      err = MCERR_NO_PCB_OR_RESOURCES;
428503      goto done;
429504   }
430505
r4596r4597
437512      goto done;
438513   }
439514
515   state.multicart->gamedrv_name = pool_strdup(state.multicart->data->pool, gamedrv);
516   if (state.multicart->gamedrv_name == NULL)
517   {
518      err = MCERR_OUT_OF_MEMORY;
519      goto done;
520   }
521   
440522   /* do we have to load resources? */
441523   if (load_flags & MULTICART_FLAGS_LOAD_RESOURCES)
442524   {
r4596r4597
477559
478560void multicart_close(multicart *cart)
479561{
562   save_ram_resources(cart);
480563   pool_free(cart->data->pool);
481564}
482565
src/mess/drivers/ti99_8.c
r4596r4597
144144    and key matrix are different from both 99/4 and 99/4a.
145145    - P0-P3: column select
146146    - INT6*-INT11*: row inputs (int6* is only used for joystick fire)
147
148
149Known Issues (MZ, 2009-04-26)
150- Extended Basic II does not start when a floppy controller is present
151- Multiple cartridges are not shown in the startup screen; only one
152  cartridge is presented. The slots are scanned, though. Need to check whether
153  this is a bug of the operating system or of the emulation.
154- Emulation speed must be checked. I have inserted adjust_icounts, but I need to
155  check whether the console experienced those delays actually. Unfortunately,
156  there is almost no real hardware available.
157- Extended Basic II gets stuck with OLD MINIMEM. MiniMemory may be incompatible
158  due to the different memory layout. SAVE/OLD only works if the machine is
159  neither reset nor restarted.
147160*/
148161
149162#include "driver.h"
r4596r4597
165178#include "sound/wave.h"
166179#include "devices/harddriv.h"
167180#include "machine/idectrl.h"
181#include "machine/smc92x4.h"
182#include "machine/mm58274c.h"
183#include "devices/ti99cart.h"
168184
169185/*
170186    Memory map - see description above
171187*/
172188
173189static ADDRESS_MAP_START(ti99_8_memmap, ADDRESS_SPACE_PROGRAM, 8)
174   AM_RANGE(0x0000, 0xffff) AM_READWRITE(ti99_8_r, ti99_8_w)
190        /* There is a mapper device involved, so we need to handle
191           the range mapping in a bit more complicated way. */
192        AM_RANGE(0x0000, 0xffff) AM_READWRITE(ti99_8_r, ti99_8_w)
193
175194ADDRESS_MAP_END
176195
177196
r4596r4597
180199*/
181200
182201static ADDRESS_MAP_START(ti99_8_cru_map, ADDRESS_SPACE_IO, 8)
183   AM_RANGE(0x0000, 0x00ff) AM_DEVREAD("tms9901", tms9901_cru_r)
184   AM_RANGE(0x0100, 0x02ff) AM_READ(ti99_8_peb_cru_r)
202//   AM_RANGE(0x0000, 0x00ff) AM_DEVREAD("tms9901", tms9901_cru_r)
203        AM_RANGE(0x0000, 0x007f) AM_DEVREAD("tms9901", tms9901_cru_r)
204        AM_RANGE(0x0080, 0x00ff) AM_DEVREAD("ti99_multicart", ti99_multicart_cru_r)     /* SuperSpace cartridge */
205        AM_RANGE(0x0100, 0x02ff) AM_READ(ti99_8_peb_cru_r)
185206
186   AM_RANGE(0x0000, 0x07ff) AM_DEVWRITE("tms9901", tms9901_cru_w)
187   AM_RANGE(0x0800, 0x17ff) AM_WRITE(ti99_8_peb_cru_w)
207//      AM_RANGE(0x0000, 0x07ff) AM_DEVWRITE("tms9901", tms9901_cru_w)
208        AM_RANGE(0x0000, 0x03ff) AM_DEVWRITE("tms9901", tms9901_cru_w)
209        AM_RANGE(0x0400, 0x07ff) AM_DEVWRITE("ti99_multicart", ti99_multicart_cru_w)    /* SuperSpace cartridge */
210        AM_RANGE(0x0800, 0x17ff) AM_WRITE(ti99_8_peb_cru_w)
211
188212ADDRESS_MAP_END
189213
190214
r4596r4597
389413   1            /* MP9537 mask */
390414};
391415
392static MACHINE_DRIVER_START(ti99_8_cartslot)
393    MDRV_CARTSLOT_ADD("cart1")
394   MDRV_CARTSLOT_EXTENSION_LIST("bin,c,d,g,m,crom,drom,grom,mrom")
395   MDRV_CARTSLOT_NOT_MANDATORY
396   MDRV_CARTSLOT_START(ti99_cart)
397   MDRV_CARTSLOT_LOAD(ti99_cart)
398   MDRV_CARTSLOT_UNLOAD(ti99_cart)
416static const mm58274c_interface floppy_mm58274c_interface =
417{
418   1,   /*    mode 24*/
419   0   /*  first day of week */
420};
399421
400    MDRV_CARTSLOT_ADD("cart2")
401   MDRV_CARTSLOT_EXTENSION_LIST("bin,c,d,g,m,crom,drom,grom,mrom")
402   MDRV_CARTSLOT_NOT_MANDATORY
403   MDRV_CARTSLOT_START(ti99_cart)
404   MDRV_CARTSLOT_LOAD(ti99_cart)
405   MDRV_CARTSLOT_UNLOAD(ti99_cart)
406
407    MDRV_CARTSLOT_ADD("cart3")
408   MDRV_CARTSLOT_EXTENSION_LIST("bin,c,d,g,m,crom,drom,grom,mrom")
409   MDRV_CARTSLOT_NOT_MANDATORY
410   MDRV_CARTSLOT_START(ti99_cart)
411   MDRV_CARTSLOT_LOAD(ti99_cart)
412   MDRV_CARTSLOT_UNLOAD(ti99_cart)
413MACHINE_DRIVER_END
414   
415422static MACHINE_DRIVER_START(ti99_8_60hz)
416423   /* basic machine hardware */
417424   /* TMS9995-MP9537 CPU @ 10.7 MHz */
r4596r4597
441448   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.50)
442449
443450   /* devices */
444   MDRV_IDE_CONTROLLER_ADD( "ide", ti99_ide_interrupt )   /* FIXME */
451   MDRV_IDE_CONTROLLER_ADD( "ide", ti99_ide_interrupt )
445452
446   MDRV_HARDDISK_ADD( "harddisk1" )
447   MDRV_HARDDISK_ADD( "harddisk2" )
448   MDRV_HARDDISK_ADD( "harddisk3" )
453   MDRV_IMPORT_FROM( smc92x4_hd )
449454
450455   MDRV_IDE_HARDDISK_ADD( "ide_harddisk" )
451456
452457   MDRV_CASSETTE_ADD( "cassette", default_cassette_config )
458
459   /* rtc */
460   MDRV_MM58274C_ADD("mm58274c_floppy", floppy_mm58274c_interface)     
453461   
454462   /* tms9901 */
455463   MDRV_TMS9901_ADD("tms9901", tms9901reset_param_ti99_8)   
464
465   MDRV_WD179X_ADD("wd179x", ti99_wd17xx_interface )
456466   
457   MDRV_IMPORT_FROM(ti99_8_cartslot)
467   MDRV_TI99_CARTRIDGE_ADD("ti99_multicart")
458468MACHINE_DRIVER_END
459469
460470
r4596r4597
489499   /* devices */
490500   MDRV_IDE_CONTROLLER_ADD( "ide", ti99_ide_interrupt )   /* FIXME */
491501
492   MDRV_HARDDISK_ADD( "harddisk1" )
493   MDRV_HARDDISK_ADD( "harddisk2" )
494   MDRV_HARDDISK_ADD( "harddisk3" )
502   MDRV_IMPORT_FROM( smc92x4_hd )
495503
496504   MDRV_IDE_HARDDISK_ADD( "ide_harddisk" )
497505
498506   MDRV_CASSETTE_ADD( "cassette", default_cassette_config )
499507
508   /* rtc */
509   MDRV_MM58274C_ADD("mm58274c_floppy", floppy_mm58274c_interface)     
510   
500511   /* tms9901 */
501512   MDRV_TMS9901_ADD("tms9901", tms9901reset_param_ti99_8)   
502   
503   MDRV_IMPORT_FROM(ti99_8_cartslot)
513
514   MDRV_WD179X_ADD("wd179x", ti99_wd17xx_interface )
515
516   MDRV_TI99_CARTRIDGE_ADD("ti99_multicart")
504517MACHINE_DRIVER_END
505518
506519/*
r4596r4597
509522ROM_START(ti99_8)
510523   /*CPU memory space*/
511524   ROM_REGION(region_cpu1_len_8,"maincpu",0)
512   ROM_LOAD("998rom.bin", 0x0000, 0x8000, NO_DUMP)      /* system ROMs */
525   ROM_LOAD("998rom.bin", 0x0000, 0x8000, CRC(b7a06ffd) SHA1(17dc8529fa808172fc47089982efb0bf0548c80c))      /* system ROMs */
513526
514527   /*GROM memory space*/
515528   ROM_REGION(0x10000, region_grom, 0)
516   ROM_LOAD("998grom.bin", 0x0000, 0x6000, NO_DUMP)   /* system GROMs */
529   ROM_LOAD("998grom.bin", 0x0000, 0x6000, CRC(c63806bc) SHA1(cbfa8b04b4aefbbd9a713c54267ad4dd179c13a3))   /* system GROMs */
517530
518531   /*DSR ROM space*/
519532   ROM_REGION(region_dsr_len, region_dsr, 0)
r4596r4597
628641SYSTEM_CONFIG_END
629642
630643/*      YEAR    NAME        PARENT      COMPAT  MACHINE     INPUT   INIT        CONFIG      COMPANY                 FULLNAME */
631COMP(   1983,   ti99_8,      0,   0,   ti99_8_60hz,ti99_8,   ti99_8,   ti99_8,   "Texas Instruments",   "TI-99/8 Computer (US)" , GAME_NOT_WORKING )
632COMP(   1983,   ti99_8e,   ti99_8,   0,   ti99_8_50hz,ti99_8,   ti99_8,   ti99_8,   "Texas Instruments",   "TI-99/8 Computer (Europe)" , GAME_NOT_WORKING )
644COMP(   1983,   ti99_8,      0,   0,   ti99_8_60hz,ti99_8,   ti99_8,   ti99_8,   "Texas Instruments",   "TI-99/8 Computer (US)" , 0)
645COMP(   1983,   ti99_8e,   ti99_8,   0,   ti99_8_50hz,ti99_8,   ti99_8,   ti99_8,   "Texas Instruments",   "TI-99/8 Computer (Europe)" , 0 )
src/mess/drivers/ti99_4p.c
r4596r4597
6161   AM_RANGE(0x8C00, 0x8fff) AM_READWRITE(ti99_nop_8_r, ti99_wv38_w)   /*vdp write*/
6262   AM_RANGE(0x9000, 0x93ff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*speech read - installed dynamically*/
6363   AM_RANGE(0x9400, 0x97ff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*speech write - installed dynamically*/
64   AM_RANGE(0x9800, 0x98ff) AM_READWRITE(ti99_4p_rgpl_r, ti99_nop_8_w)   /*GPL read*/
64   AM_RANGE(0x9800, 0x98ff) AM_READWRITE(ti99_4p_grom_r, ti99_nop_8_w)   /*GPL read*/
6565   AM_RANGE(0x9900, 0x9bff) AM_RAMBANK(11)                        /*extra RAM for debugger*/
66   AM_RANGE(0x9c00, 0x9fff) AM_READWRITE(ti99_nop_8_r, ti99_4p_wgpl_w)   /*GPL write*/
66   AM_RANGE(0x9c00, 0x9fff) AM_READWRITE(ti99_nop_8_r, ti99_4p_grom_w)   /*GPL write*/
6767   AM_RANGE(0xa000, 0xafff) AM_RAMBANK(5)                        /*upper 24kb of RAM extension: AMS bank 10*/
6868   AM_RANGE(0xb000, 0xbfff) AM_RAMBANK(6)                        /*upper 24kb of RAM extension: AMS bank 11*/
6969   AM_RANGE(0xc000, 0xcfff) AM_RAMBANK(7)                        /*upper 24kb of RAM extension: AMS bank 12*/
src/mess/drivers/ti99_4x.c
r4596r4597
3636#include "devices/mflopimg.h"
3737#include "devices/harddriv.h"
3838#include "devices/cassette.h"
39#include "devices/cartslot.h"
4039#include "machine/smartmed.h"
4140#include "sound/5220intf.h"
4241#include "machine/idectrl.h"
4342#include "machine/smc92x4.h"
4443#include "machine/mm58274c.h"
4544
45#include "devices/ti99cart.h"
46
4647/*
4748    memory map
4849*/
r4596r4597
5253   AM_RANGE(0x0000, 0x1fff) AM_ROM                              /*system ROM*/
5354   AM_RANGE(0x2000, 0x3fff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*lower 8kb of RAM extension - installed dynamically*/
5455   AM_RANGE(0x4000, 0x5fff) AM_READWRITE(ti99_4x_peb_r, ti99_4x_peb_w)   /*DSR ROM space*/
55   AM_RANGE(0x6000, 0x7fff) AM_READWRITE(ti99_cart_r, ti99_cart_w)      /*cartridge memory*/
56//   AM_RANGE(0x6000, 0x7fff) AM_READWRITE(ti99_cart_r, ti99_cart_w)      /*cartridge memory*/
57   AM_RANGE(0x6000, 0x7fff) AM_DEVREADWRITE("ti99_multicart", ti99_multicart_r, ti99_multicart_w)
5658   AM_RANGE(0x8000, 0x80ff) AM_MIRROR(0x0300) AM_RAMBANK(1)         /*RAM PAD, mirrored 4 times*/
5759   AM_RANGE(0x8400, 0x87ff) AM_READWRITE(ti99_nop_8_r, ti99_wsnd_w)   /*soundchip write*/
5860   AM_RANGE(0x8800, 0x8bff) AM_READWRITE(ti99_rvdp_r, ti99_nop_8_w)   /*vdp read*/
5961   AM_RANGE(0x8C00, 0x8fff) AM_READWRITE(ti99_nop_8_r, ti99_wvdp_w)   /*vdp write*/
6062   AM_RANGE(0x9000, 0x93ff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*speech read - installed dynamically*/
6163   AM_RANGE(0x9400, 0x97ff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*speech write - installed dynamically*/
62   AM_RANGE(0x9800, 0x9bff) AM_READWRITE(ti99_rgpl_r, ti99_nop_8_w)   /*GPL read*/
63   AM_RANGE(0x9c00, 0x9fff) AM_READWRITE(ti99_nop_8_r, ti99_wgpl_w)   /*GPL write*/
64   AM_RANGE(0x9800, 0x9bff) AM_READWRITE(ti99_grom_r, ti99_nop_8_w)   /*GPL read*/
65   AM_RANGE(0x9c00, 0x9fff) AM_READWRITE(ti99_nop_8_r, ti99_grom_w)   /*GPL write*/
6466   AM_RANGE(0xa000, 0xffff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*upper 24kb of RAM extension - installed dynamically*/
6567ADDRESS_MAP_END
6668
r4596r4597
6971   AM_RANGE(0x0000, 0x1fff) AM_ROM                              /*system ROM*/
7072   AM_RANGE(0x2000, 0x3fff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*lower 8kb of RAM extension - installed dynamically*/
7173   AM_RANGE(0x4000, 0x5fff) AM_READWRITE(ti99_4x_peb_r, ti99_4x_peb_w)   /*DSR ROM space*/
72   AM_RANGE(0x6000, 0x7fff) AM_READWRITE(ti99_cart_r, ti99_cart_w)      /*cartridge memory*/
74   AM_RANGE(0x6000, 0x7fff) AM_DEVREADWRITE("ti99_multicart", ti99_multicart_r, ti99_multicart_w)
7375   AM_RANGE(0x8000, 0x80ff) AM_MIRROR(0x0300) AM_RAMBANK(1)         /*RAM PAD, mirrored 4 times*/
7476   AM_RANGE(0x8400, 0x87ff) AM_READWRITE(ti99_nop_8_r, ti99_wsnd_w)   /*soundchip write*/
7577   AM_RANGE(0x8800, 0x8bff) AM_READWRITE(ti99_rv38_r, ti99_nop_8_w)   /*vdp read*/
7678   AM_RANGE(0x8C00, 0x8fff) AM_READWRITE(ti99_nop_8_r, ti99_wv38_w)   /*vdp write*/
7779   AM_RANGE(0x9000, 0x93ff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*speech read - installed dynamically*/
7880   AM_RANGE(0x9400, 0x97ff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*speech write - installed dynamically*/
79   AM_RANGE(0x9800, 0x9bff) AM_READWRITE(ti99_rgpl_r, ti99_nop_8_w)   /*GPL read*/
80   AM_RANGE(0x9c00, 0x9fff) AM_READWRITE(ti99_nop_8_r, ti99_wgpl_w)   /*GPL write*/
81   AM_RANGE(0x9800, 0x9bff) AM_READWRITE(ti99_grom_r, ti99_nop_8_w)   /*GPL read*/
82   AM_RANGE(0x9c00, 0x9fff) AM_READWRITE(ti99_nop_8_r, ti99_grom_w)   /*GPL write*/
8183   AM_RANGE(0xa000, 0xffff) AM_READWRITE(ti99_nop_8_r, ti99_nop_8_w)   /*upper 24kb of RAM extension - installed dynamically*/
8284ADDRESS_MAP_END
8385
r4596r4597
8688*/
8789
8890static ADDRESS_MAP_START(cru_map, ADDRESS_SPACE_IO, 8)
89   AM_RANGE(0x0000, 0x00ff) AM_DEVREAD("tms9901", tms9901_cru_r)
90   AM_RANGE(0x0100, 0x01ff) AM_READ(ti99_4x_peb_cru_r)
91        AM_RANGE(0x0000, 0x007f) AM_DEVREAD("tms9901", tms9901_cru_r)
92        AM_RANGE(0x0080, 0x00ff) AM_DEVREAD("ti99_multicart", ti99_multicart_cru_r)   /* SuperSpace */
93        AM_RANGE(0x0100, 0x01ff) AM_READ(ti99_4x_peb_cru_r)
9194
92   AM_RANGE(0x0000, 0x07ff) AM_DEVWRITE("tms9901", tms9901_cru_w)
93   AM_RANGE(0x0800, 0x0fff) AM_WRITE(ti99_4x_peb_cru_w)
95   AM_RANGE(0x0000, 0x03ff) AM_DEVWRITE("tms9901", tms9901_cru_w)
96        AM_RANGE(0x0400, 0x07ff) AM_DEVWRITE("ti99_multicart", ti99_multicart_cru_w)   /* SuperSpace */
97        AM_RANGE(0x0800, 0x0fff) AM_WRITE(ti99_4x_peb_cru_w)
9498ADDRESS_MAP_END
9599
96
97100/*
98101    Input ports, used by machine code for TI keyboard and joystick emulation.
99102*/
r4596r4597
502505   0   /*  first day of week */
503506};
504507
505static MACHINE_DRIVER_START(ti99_4_cartslot)
506    MDRV_CARTSLOT_ADD("cart1")
507   MDRV_CARTSLOT_EXTENSION_LIST("bin,c,d,g,m,crom,drom,grom,mrom")
508   MDRV_CARTSLOT_NOT_MANDATORY
509   MDRV_CARTSLOT_START(ti99_cart)
510   MDRV_CARTSLOT_LOAD(ti99_cart)
511   MDRV_CARTSLOT_UNLOAD(ti99_cart)
512
513    MDRV_CARTSLOT_ADD("cart2")
514   MDRV_CARTSLOT_EXTENSION_LIST("bin,c,d,g,m,crom,drom,grom,mrom")
515   MDRV_CARTSLOT_NOT_MANDATORY
516   MDRV_CARTSLOT_START(ti99_cart)
517   MDRV_CARTSLOT_LOAD(ti99_cart)
518   MDRV_CARTSLOT_UNLOAD(ti99_cart)
519
520    MDRV_CARTSLOT_ADD("cart3")
521   MDRV_CARTSLOT_EXTENSION_LIST("bin,c,d,g,m,crom,drom,grom,mrom")
522   MDRV_CARTSLOT_NOT_MANDATORY
523   MDRV_CARTSLOT_START(ti99_cart)
524   MDRV_CARTSLOT_LOAD(ti99_cart)
525   MDRV_CARTSLOT_UNLOAD(ti99_cart)
526MACHINE_DRIVER_END
527
528508static MACHINE_DRIVER_START(ti99_4_60hz)
529509   /* basic machine hardware */
530510   /* TMS9900 CPU @ 3.0 MHz */
r4596r4597
574554   
575555   MDRV_WD179X_ADD("wd179x", ti99_wd17xx_interface )
576556   
577   MDRV_IMPORT_FROM(ti99_4_cartslot)
557   MDRV_TI99_CARTRIDGE_ADD("ti99_multicart")
558   
578559MACHINE_DRIVER_END
579560
580561static MACHINE_DRIVER_START(ti99_4_50hz)
r4596r4597
623604   MDRV_TMS9902_ADD("tms9902_1", tms9902_params_1)     
624605   
625606   MDRV_WD179X_ADD("wd179x", ti99_wd17xx_interface )
607   MDRV_TI99_CARTRIDGE_ADD("ti99_multicart")
626608   
627   MDRV_IMPORT_FROM(ti99_4_cartslot)
628609MACHINE_DRIVER_END
629610
630611static MACHINE_DRIVER_START(ti99_4a_60hz)
r4596r4597
675656   
676657   MDRV_WD179X_ADD("wd179x", ti99_wd17xx_interface )   
677658   
678   MDRV_IMPORT_FROM(ti99_4_cartslot)
659   MDRV_TI99_CARTRIDGE_ADD("ti99_multicart")
679660MACHINE_DRIVER_END
680661
681662
r4596r4597
707688   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.20)
708689
709690   /* devices */
710   MDRV_IDE_CONTROLLER_ADD( "ide", ti99_ide_interrupt )   /* FIXME */
691   MDRV_IDE_CONTROLLER_ADD( "ide", ti99_ide_interrupt )
711692
712693   MDRV_IMPORT_FROM( smc92x4_hd )
713694
r4596r4597
726707   
727708   MDRV_WD179X_ADD("wd179x", ti99_wd17xx_interface )
728709   
729   MDRV_IMPORT_FROM(ti99_4_cartslot)
710   MDRV_TI99_CARTRIDGE_ADD("ti99_multicart")
730711MACHINE_DRIVER_END
731712
732713
r4596r4597
784765   MDRV_TMS9902_ADD("tms9902_1", tms9902_params_1)
785766   
786767   MDRV_WD179X_ADD("wd179x", ti99_wd17xx_interface )   
768   MDRV_TI99_CARTRIDGE_ADD("ti99_multicart")
787769   
788   MDRV_IMPORT_FROM(ti99_4_cartslot)   
789770MACHINE_DRIVER_END
790771
791772
r4596r4597
883864#define rom_ti99_4e rom_ti99_4
884865#define rom_ti99_4ae rom_ti99_4a
885866
886/* a TI99 console only had one cartridge slot, but cutting the ROMs
887 * in 3 files seems to be the only way to handle cartridges until I use
888 * a headered format.
889 * Note that there sometimes was a speech ROM slot in the speech synthesizer,
890 * and you could plug quite a lot of GROMs in the side port.  Neither of these
891 * are emulated.
892 */
893   
894867static void ti99_4_floppy_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info)
895868{
896869   /* floppy */
docs/messnew.txt
r4596r4597
6666
6767- [STUDIO2] Fixed .st2 cartridge loading. [Curt Coder]
6868
69- [TI99 family] Added functionality for multi-cartridge handling. Added
70  support for complex/multi-file cartridge images.  [Michael Zapf]
71
6972- [TRS80] Various improvements: [Robbbert]
7073          - added centronics printer
7174          - cassette works on all clones

Previous 15994 Revisions Next


© 1998-2018 The MESS Team