Previous 15994 Revisions Next

r8221 Friday 4th June, 2010 at 23:25:42 UTC by Fabio Priuli
Last batch of refactorizations.
This also adds preliminary support for iNES 2.0 headers: it is basically untested and
it does not much more than logging all the additional fields and handle additional
mapper/prg_chunks/chr_chunks bits
[src/mess/includes]nes.h
[src/mess/machine]nes.c nes_unif.c

src/mess/includes/nes.h
r8220r8221
141141   /***** NES-cart related *****/
142142
143143   /* load-time cart variables which remain constant */
144   UINT16 prg_chunks;   // a recently dumped multigame cart has 256 chunks of both PRG & CHR!
145   UINT16 chr_chunks;
144146   UINT8 trainer;
145147   UINT8 battery;
146   UINT16 prg_chunks;   // a recently dumped multigame cart has 256 chunks of both PRG & CHR!
147   UINT16 chr_chunks;
148   UINT32 battery_size;
148149
149   UINT8 format;   // 1 = iNES, 2 = UNIF
150   int format;   // 1 = iNES, 2 = UNIF
150151
151152   /* system variables which don't change at run-time */
152153   UINT16 mapper;      // for iNES
r8220r8221
155156   UINT8 hard_mirroring;
156157   UINT8 slow_banking;
157158   UINT8 crc_hack;   // this is needed to detect different boards sharing the same Mappers (shame on .nes format)
159   UINT8 ines20;
158160
159
160161   /***** FDS-floppy related *****/
161162
162163   UINT8   fds_sides;
src/mess/machine/nes.c
r8220r8221
174174   /* memory subsystem is set up. When this routine is called */
175175   /* everything is ready, so we can just copy over the data */
176176   /* we loaded before. */
177   memcpy(state->battery_ram, state->battery_data, NES_BATTERY_SIZE);
177   memcpy(state->battery_ram, state->battery_data, state->battery_size);
178178}
179179
180// to be probably removed (it does nothing since a long time)
180181int nes_ppu_vidaccess( running_device *device, int address, int data )
181182{
182//   nes_state *state = (nes_state *)device->machine->driver_data;
183
184   /* TODO: this is a bit of a hack, needed to get Argus, ASO, etc to work */
185   /* but, B-Wings, submath (j) seem to use this location differently... */
186//   if (state->chr_chunks && (address & 0x3fff) < 0x2000)
187//   {
188//      int vrom_loc;
189//      vrom_loc = (nes_vram[(address & 0x1fff) >> 10] * 16) + (address & 0x3ff);
190//      data = state->vrom [vrom_loc];
191//   }
192183   return data;
193184}
194185
r8220r8221
304295
305296   /* Write out the battery file if necessary */
306297   if (state->battery)
307      image_battery_save(state->cart, state->battery_ram, NES_BATTERY_SIZE);
298      image_battery_save(state->cart, state->battery_ram, state->battery_size);
308299}
309300
310301
r8220r8221
521512DEVICE_IMAGE_LOAD( nes_cart )
522513{
523514   nes_state *state = (nes_state *)image->machine->driver_data;
524   const char *mapinfo;
525   int mapint1 = 0, mapint2 = 0, mapint3 = 0, mapint4 = 0, goodcrcinfo = 0;
526   char magic[4];
527   char skank[8];
528   int local_options = 0;
529   char m;
530   int i;
531515
532516   if (image_software_entry(image) == NULL)
533517   {
518      const char *mapinfo;
519      int mapint1 = 0, mapint2 = 0, mapint3 = 0, mapint4 = 0, goodcrcinfo = 0;
520      char magic[4], extend[5];
521      int local_options = 0;
522      char m;
523
534524      /* Check first 4 bytes of the image to decide if it is UNIF or iNES */
535525      /* Unfortunately, many .unf files have been released as .nes, so we cannot rely on extensions only */
536526      memset(magic, '\0', sizeof(magic));
r8220r8221
539529      if ((magic[0] == 'N') && (magic[1] == 'E') && (magic[2] == 'S'))   /* If header starts with 'NES' it is iNES */
540530      {
541531         UINT32 prg_size;
542
543532         state->format = 1;   // we use this to select between mapper_reset / unif_reset
533         state->ines20 = 0;
534         state->battery_size = NES_BATTERY_SIZE; // with iNES we can only support 8K WRAM battery (iNES 2.0 might fix this)
544535
536         // check if the image is recognized by nes.hsi
545537         mapinfo = image_extrainfo(image);
546538
547         if (mapinfo)
539         // image_extrainfo() resets the file position back to start.
540         // Let's skip past the magic header once again.
541         image_fseek(image, 4, SEEK_SET);
542         
543         image_fread(image, &state->prg_chunks, 1);
544         image_fread(image, &state->chr_chunks, 1);
545         /* Read the first ROM option byte (offset 6) */
546         image_fread(image, &m, 1);
547         
548         /* Interpret the iNES header flags */
549         state->mapper = (m & 0xf0) >> 4;
550         local_options = m & 0x0f;
551         
552         /* Read the second ROM option byte (offset 7) */
553         image_fread(image, &m, 1);
554         
555         switch (m & 0xc)
548556         {
557            case 0x4:
558            case 0xc:   
559               // probably the header got corrupted: don't trust upper bits for mapper
560               break;
561
562            case 0x8:   // it's iNES 2.0 format
563               state->ines20 = 1;
564            case 0x0:
565            default:
566               state->mapper = state->mapper | (m & 0xf0);
567               break;
568         }
569
570         if (mapinfo /* && !state->ines20 */)
571         {
549572            if (4 == sscanf(mapinfo,"%d %d %d %d", &mapint1, &mapint2, &mapint3, &mapint4))
550573            {
574               /* image is present in nes.hsi: overwrite the header settings with these */
551575               state->mapper = mapint1;
552576               local_options = mapint2 & 0x0f;
553577               state->crc_hack = mapint2 & 0xf0;   // this is used to differentiate among variants of the same Mapper (see nes_mmc.c)
r8220r8221
555579               state->chr_chunks = mapint4;
556580               logerror("NES.HSI info: %d %d %d %d\n", mapint1, mapint2, mapint3, mapint4);
557581               goodcrcinfo = 1;
582               state->ines20 = 0;
558583            }
559584            else
560585            {
r8220r8221
566591            logerror("NES: No extrainfo found\n");
567592         }
568593
569         if (!goodcrcinfo)
570         {
571            // image_extrainfo() resets the file position back to start.
572            // Let's skip past the magic header once again.
573            image_fseek(image, 4, SEEK_SET);
574
575            image_fread(image, &state->prg_chunks, 1);
576            image_fread(image, &state->chr_chunks, 1);
577            /* Read the first ROM option byte (offset 6) */
578            image_fread(image, &m, 1);
579
580            /* Interpret the iNES header flags */
581            state->mapper = (m & 0xf0) >> 4;
582            local_options = m & 0x0f;
583
584            /* Read the second ROM option byte (offset 7) */
585            image_fread(image, &m, 1);
586
587            /* Check for skanky headers */
588            image_fread(image, &skank, 8);
589
590            /* If the header has junk in the unused bytes, assume the extra mapper byte is also invalid */
591            /* We only check the first 4 unused bytes for now */
592            for (i = 0; i < 4; i ++)
593            {
594               logerror("%02x ", skank[i]);
595               if (skank[i] != 0x00)
596               {
597                  logerror("(skank: %d)", i);
598               }
599            }
600            logerror("\n");
601
602            state->mapper = state->mapper | (m & 0xf0);
603         }
604
605594         state->hard_mirroring = local_options & 0x01;
606595         state->battery = local_options & 0x02;
607596         state->trainer = local_options & 0x04;
r8220r8221
616605         if (state->four_screen_vram)
617606            logerror("-- 4-screen VRAM\n");
618607
608         if (state->ines20)
609         {
610            logerror("Extended iNES format:\n");
611            image_fread(image, &extend, 5);
612            state->mapper |= (extend[0] & 0x0f) << 8;
613            logerror("-- mapper: %d\n", state->mapper);
614            logerror("-- submapper: %d\n", (extend[0] & 0xf0) >> 4);
615            state->prg_chunks |= ((extend[1] & 0x0f) << 8);
616            state->chr_chunks |= ((extend[1] & 0xf0) << 4);
617            logerror("-- PRG chunks: %d\n", state->prg_chunks);
618            logerror("-- CHR chunks: %d\n", state->chr_chunks);
619            logerror("-- PRG NVWRAM: %d\n", extend[2] & 0x0f);
620            logerror("-- PRG WRAM: %d\n", (extend[2] & 0xf0) >> 4);
621            logerror("-- CHR NVWRAM: %d\n", extend[3] & 0x0f);
622            logerror("-- CHR WRAM: %d\n", (extend[3] & 0xf0) >> 4);
623            logerror("-- TV System: %d\n", extend[4] & 3);
624         }
625
619626         /* Free the regions that were allocated by the ROM loader */
620627         memory_region_free(image->machine, "maincpu");
621628         memory_region_free(image->machine, "gfx1");
r8220r8221
639646            image_fread(image, &state->wram[0x1000], 0x200);
640647
641648         /* Read in the program chunks */
649         image_fread(image, &state->rom[0x10000], 0x4000 * state->prg_chunks);
642650         if (state->prg_chunks == 1)
643         {
644            image_fread(image, &state->rom[0x14000], 0x4000);
645            /* Mirror this bank into $8000 */
646            memcpy(&state->rom[0x10000], &state->rom[0x14000], 0x4000);
647         }
648         else
649            image_fread(image, &state->rom[0x10000], 0x4000 * state->prg_chunks);
651            memcpy(&state->rom[0x14000], &state->rom[0x10000], 0x4000);
650652
651653#if SPLIT_PRG
652654         {
r8220r8221
700702         logerror("**\n");
701703         // printf("CHR chunks: %02x, size: %06x\n", state->chr_chunks, 0x4000 * state->chr_chunks);
702704         // printf("**\n");
703
704         /* Attempt to load a battery file for this ROM. If successful, we */
705         /* must wait until later to move it to the system memory. */
706         if (state->battery)
707            image_battery_load(image, state->battery_data, NES_BATTERY_SIZE, 0x00);
708
709705      }
710706      else if ((magic[0] == 'U') && (magic[1] == 'N') && (magic[2] == 'I') && (magic[3] == 'F')) /* If header starts with 'UNIF' it is UNIF */
711707      {
r8220r8221
763759                  state->mapper = 0;   // this allows us to set up memory handlers without duplicating code (for the moment)
764760                  state->board = unif_board->board;
765761                  state->battery = unif_board->wram;   // we should implement WRAM banks...
762                  state->battery_size = NES_BATTERY_SIZE; // FIXME: we should allow for smaller battery!
766763                  // state->hard_mirroring = unif_board->nt;
767764                  // state->four_screen_vram = ;
768765
r8220r8221
10281025
10291026      state->format = 2;   // temporarily treat this as a unif file
10301027      state->board = image_get_feature(image);
1031      state->battery = 0;   // presence of a battery should be read from .xml
10321028      state->mapper = 0;      // this allows us to set up memory handlers without duplicating code (for the moment)
10331029
1030      state->battery = (image_get_software_region(image, "bwram") != NULL) ? 1 : 0;
1031      state->battery_size = image_get_software_region_length(image, "bwram");
1032
10341033      // FIXME: we need to handle the remaining variables. on the short term, we might
10351034      // create a table for the various mappers (based on the 'feature' value in xml),
10361035      // but on the long term we need to rework the whole emulation to first use the
r8220r8221
10411040      //state->hard_mirroring = ;
10421041      //state->trainer = ;
10431042      //state->four_screen_vram = ;
1044
1045      /* Attempt to load a battery file for this ROM. If successful, we */
1046      /* must wait until later to move it to the system memory. */
1047      if (state->battery)
1048         image_battery_load(image, state->battery_data, NES_BATTERY_SIZE, 0x00);
10491043   }
10501044
1045   /* Attempt to load a battery file for this ROM. If successful, we */
1046   /* must wait until later to move it to the system memory. */
1047   if (state->battery)
1048      image_battery_load(image, state->battery_data, state->battery_size, 0x00);
1049   
10511050   return INIT_PASS;
10521051}
10531052
src/mess/machine/nes_unif.c
r8220r8221
11861186   { "VIRGIN-SNROM",               NULL, NULL, NULL, mapper1_w, NULL, NULL, NULL,               16,    0,   8,    0, CHRRAM_8,  NT_HORZ,  STD_SNROM},
11871187   // below are boards we need for the new PCB-based code (and by list support): no unif file is known to use them, but they are needed to handle iNES file like PCBs.
11881188   { "DAOU-306",    NULL, NULL, NULL, mapper156_w, NULL, NULL, NULL,   256,    128,   0,    0, 0,  0,  OPENCORP_DAOU306},   // mapper 156
1189   { "IREM-H-3001",    NULL, NULL, NULL, NULL, NULL, NULL, NULL,   256,    128,   0,    0, 0,  0,  IREM_H3001},   // mapper 65
1189   { "IREM-H-3001",    NULL, NULL, NULL, mapper65_w, NULL, NULL, irem_irq,   256,    128,   0,    0, 0,  0,  IREM_H3001},   // mapper 65
11901190   { "BANDAI-DATACH",    NULL, NULL, mapper16_m_w, mapper16_w, NULL, NULL,  bandai_irq,   256,    128,   0,    0, 0,  0,  BANDAI_DATACH},   // mapper 157
11911191   { "BANDAI-KARAOKE",    NULL, NULL, NULL, mapper188_w, NULL, NULL, NULL,   256,    128,   0,    0, 0,  0,  BANDAI_KARAOKE},   // mapper 188
11921192   { "BANDAI-OEKAKIDS",    NULL, NULL, NULL, mapper96_w, NULL, NULL, NULL,   256,    128,   0,    0, 0,  0,  BANDAI_OEKAKIDS},   // mapper 96

Previous 15994 Revisions Next


© 1998-2018 The MESS Team