Previous 15994 Revisions Next

r8240 Sunday 6th June, 2010 at 19:22:14 UTC by Fabio Priuli
[NES] misc refactoring:
* moved the remaining static variable to nes_state (I will sort and cleanup them later)
* moved some mapper/unif specific code to the correspondent source file
* slightly changed the PPU latch data handling to behave like NEStopia
[src/mame/video]ppu2c0x.c
[src/mess/includes]nes.h
[src/mess/machine]nes.c nes_ines.c nes_mmc.c nes_mmc.h nes_unif.c

src/mame/video/ppu2c0x.c
r8239r8240
966966
967967   if (offset >= PPU_MAX_REG)
968968   {
969      logerror("PPU %s(r): Attempting to read past the chip\n", device->tag());
969      logerror("PPU %s: Attempting to read past the chip: offset %x\n", device->tag(), offset);
970970      offset &= PPU_MAX_REG - 1;
971971   }
972972
973973   // see which register to read
974974   switch (offset & 7)
975975   {
976      case PPU_STATUS:
976      case PPU_STATUS: /* 2 */
977977         // The top 3 bits of the status register are the only ones that report data. The
978978         // remainder contain whatever was last in the PPU data latch, except on the RC2C05 (protection)
979979         if (ppu2c0x->security_value)
r8239r8240
989989            ppu2c0x->regs[PPU_STATUS] &= 0x60;
990990         break;
991991
992      case PPU_SPRITE_DATA:
992      case PPU_SPRITE_DATA: /* 4 */
993993         ppu2c0x->data_latch = ppu2c0x->spriteram[ppu2c0x->regs[PPU_SPRITE_ADDRESS]];
994994         break;
995995
996      case PPU_DATA:
996      case PPU_DATA: /* 7 */
997997         if (ppu_latch)
998998            (*ppu_latch)(device, ppu2c0x->videomem_addr & 0x3fff);
999999
r8239r8240
10351035
10361036   if (offset >= PPU_MAX_REG)
10371037   {
1038      logerror("PPU: Attempting to write past the chip\n");
1038      logerror("PPU %s: Attempting to write past the chip: offset %x, data %x\n", device->tag(), offset, data);
10391039      offset &= PPU_MAX_REG - 1;
10401040   }
10411041
r8239r8240
10531053
10541054   switch (offset & 7)
10551055   {
1056      case PPU_CONTROL0:
1056      case PPU_CONTROL0: /* 0 */
10571057         ppu2c0x->regs[PPU_CONTROL0] = data;
10581058
10591059         /* update the name table number on our refresh latches */
r8239r8240
10681068//          logerror("control0 write: %02x (scanline: %d)\n", data, ppu2c0x->scanline);
10691069         break;
10701070
1071      case PPU_CONTROL1:
1071      case PPU_CONTROL1: /* 1 */
10721072         /* if color intensity has changed, change all the color tables to reflect them */
10731073         if ((data & PPU_CONTROL1_COLOR_EMPHASIS) != (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS))
10741074         {
r8239r8240
10851085         ppu2c0x->regs[PPU_CONTROL1] = data;
10861086         break;
10871087
1088      case PPU_SPRITE_ADDRESS:
1088      case PPU_SPRITE_ADDRESS: /* 3 */
10891089         ppu2c0x->regs[PPU_SPRITE_ADDRESS] = data;
10901090         break;
10911091
1092      case PPU_SPRITE_DATA:
1092      case PPU_SPRITE_DATA: /* 4 */
10931093         // If the PPU is currently rendering the screen, 0xff is written instead of the desired data.
10941094         if (ppu2c0x->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE)
10951095            data = 0xff;
r8239r8240
10971097         ppu2c0x->regs[PPU_SPRITE_ADDRESS] = (ppu2c0x->regs[PPU_SPRITE_ADDRESS] + 1) & 0xff;
10981098         break;
10991099
1100      case PPU_SCROLL:
1100      case PPU_SCROLL: /* 5 */
11011101         if (ppu2c0x->toggle)
11021102         {
11031103            /* second write */
r8239r8240
11191119         ppu2c0x->toggle ^= 1;
11201120         break;
11211121
1122      case PPU_ADDRESS:
1122      case PPU_ADDRESS: /* 6 */
11231123         if (ppu2c0x->toggle)
11241124         {
11251125            /* second write */
r8239r8240
11411141         ppu2c0x->toggle ^= 1;
11421142         break;
11431143
1144      case PPU_DATA:
1144      case PPU_DATA: /* 7 */
11451145         {
11461146            int tempAddr = ppu2c0x->videomem_addr & 0x3fff;
11471147
r8239r8240
11721172         /* ignore other registers writes */
11731173         break;
11741174   }
1175
1176   ppu2c0x->data_latch = data;
11751177}
11761178
11771179/*************************************
src/mess/includes/nes.h
r8239r8240
8080   UINT8 mmc5_vram[0x400];
8181   int mmc5_vram_control;
8282   UINT8 mmc5_high_chr;
83   UINT8 mmc5_split_scr;
8384   UINT8 *extended_ntram;
8485
8586   /* video-related */
r8239r8240
141142   UINT8 mmc_extra_bank[16];         // some MMC3 clone have 2 series of PRG/CHR banks...
142143                              // we collect them all here: first 4 elements PRG banks, then 6/8 CHR banks
143144
145   // misc mapper related variables which should be merged with the above one, where possible
146   UINT8 MMC1_regs[4];
147   UINT8 MMC2_regs[4];   // these replace bank0/bank0_hi/bank1/bank1_hi
148   
149   int MMC5_rom_bank_mode;
150   int MMC5_vrom_bank_mode;
151   int MMC5_vram_protect;
152   int MMC5_scanline;
153   int vrom_page_a;
154   int vrom_page_b;
155   // int vrom_next[4];
156   
157   // these might be unified in single mmc_reg[] array, together with state->mmc_cmd1 & state->mmc_cmd2
158   // but be careful that MMC3 clones often use state->mmc_cmd1/state->mmc_cmd2 (from base MMC3) AND additional regs below!
159   UINT8 mapper45_reg[4];
160   UINT8 mapper51_reg[3];
161   UINT8 mapper83_reg[10];
162   UINT8 mapper83_low_reg[4];
163   UINT8 mapper95_reg[4];
164   UINT8 mapper115_reg[4];
165   UINT8 txc_reg[4];   // used by mappers 132, 172 & 173
166   UINT8 subor_reg[4];   // used by mappers 166 & 167
167   UINT8 sachen_reg[8];   // used by mappers 137, 138, 139 & 141
168   UINT8 mapper12_reg;
169   UINT8 map52_reg_written;
170   UINT8 map114_reg, map114_reg_enabled;
171   UINT8 map215_reg[4];
172   UINT8 map217_reg[4];
173   UINT8 map249_reg;
174   UINT8 map14_reg[2];
175   UINT8 mapper121_reg[3];
176   UINT8 mapper187_reg[4];
177   UINT8 map208_reg[5];
178   UINT8 bmc_64in1nr_reg[4];
179   UINT8 unl_8237_reg[3];
180   UINT8 bmc_s24in1sc03_reg[3];
181   
182   // mapper 68 needs these for NT mirroring!
183   int m68_mirror;
184   int m0, m1;
185   
186   // Famicom Jump II is identified as Mapper 153, but it works in a completely different way.
187   // in particular, it requires these (not needed by other Mapper 153 games)
188   UINT8 map153_reg[8];
189   UINT8 map153_bank_latch;
190
191
144192   /***** NES-cart related *****/
145193
146194   /* load-time cart variables which remain constant */
src/mess/machine/nes.c
r8239r8240
140140   /* Set up the mapper callbacks */
141141   if (state->format == 1)
142142   {
143      const mmc *mapper = nes_mapper_lookup(state->mapper);
144      if (mapper)
145      {
146         state->mmc_write_low = mapper->mmc_write_low;
147         state->mmc_write_mid = mapper->mmc_write_mid;
148         state->mmc_write = mapper->mmc_write;
149         state->mmc_read_low = mapper->mmc_read_low;
150         state->mmc_read_mid = NULL;   // in progress
151         state->mmc_read = NULL;   // in progress
152         ppu_latch = mapper->ppu_latch;
153      }
154      else
155      {
156         logerror("Mapper %d is not yet supported, defaulting to no mapper.\n",state->mapper);
157         state->mmc_write_low = NULL;
158         state->mmc_write_mid = NULL;
159         state->mmc_write = NULL;
160         state->mmc_read_low = NULL;
161         state->mmc_read_mid = NULL;   // in progress
162         state->mmc_read = NULL;   // in progress
163         ppu_latch = NULL;
164      }
143      mapper_handlers_setup(machine);
165144   }
166145   else if (state->format == 2)
167146   {
168      const unif *board = nes_unif_lookup(state->board);
169      if (board)
170      {
171         state->mmc_write_low = board->mmc_write_low;
172         state->mmc_write_mid = board->mmc_write_mid;
173         state->mmc_write = board->mmc_write;
174         state->mmc_read_low = board->mmc_read_low;
175         state->mmc_read_mid = NULL;   // in progress
176         state->mmc_read = NULL;   // in progress
177         ppu_latch = board->ppu_latch;
178      }
179      else
180      {
181         logerror("Board %s is not yet supported, defaulting to no mapper.\n", state->board);
182         state->mmc_write_low = NULL;
183         state->mmc_write_mid = NULL;
184         state->mmc_write = NULL;
185         state->mmc_read_low = NULL;
186         state->mmc_read_mid = NULL;   // in progress
187         state->mmc_read = NULL;   // in progress
188         ppu_latch = NULL;
189      }
147      unif_handlers_setup(machine);
190148   }
191149}
192150
r8239r8240
205163
206164   /* Reset the mapper variables. Will also mark the char-gen ram as dirty */
207165   if (state->format == 1)
208      nes_mapper_reset(machine, state->mapper);
166      nes_mapper_reset(machine);
209167
210168   if (state->format == 2)
211      nes_unif_reset(machine, state->board);
169      nes_unif_reset(machine);
212170   
213171   /* Reset the serial input ports */
214172   state->in_0.shift = 0;
r8239r8240
733691         UINT32 prg_start = 0, chr_start = 0, prg_size;
734692         char unif_mapr[32];   // here we should store MAPR chunks
735693         UINT32 size = image_length(image);
736         const unif *unif_board;
737694         int mapr_chunk_found = 0;
738695
739696         state->format = 2;   // we use this to select between mapper_reset / unif_reset
r8239r8240
771728                  if (chunk_length <= 0x20)
772729                     image_fread(image, &unif_mapr, chunk_length);
773730
774                  unif_board = nes_unif_lookup(unif_mapr);
775                  logerror("%s\n", unif_mapr);
731                  // find out prg/chr size, battery, wram, etc.
732                  unif_mapr_setup(image->machine, unif_mapr);
776733
777                  if (unif_board == NULL)
778                     fatalerror("Unknown UNIF board %s.", unif_mapr);
779
780                  state->mapper = 0;   // this allows us to set up memory handlers without duplicating code (for the moment)
781                  state->board = unif_board->board;
782                  state->battery = unif_board->nvwram;   // we should implement WRAM banks based on the size of this...
783                  state->battery_size = NES_BATTERY_SIZE; // FIXME: we should allow for smaller battery!
784                  state->prg_ram = unif_board->wram;   // we should implement WRAM banks based on the size of this...
785                  // state->hard_mirroring = unif_board->nt;
786                  // state->four_screen_vram = ;
787
788734                  /* Free the regions that were allocated by the ROM loader */
789735                  memory_region_free(image->machine, "maincpu");
790736                  memory_region_free(image->machine, "gfx1");
791737
792738                  /* Allocate them again with the large enough size */
793                  prg_size = (unif_board->prgrom == 1) ? 2 * 0x4000 : unif_board->prgrom * 0x4000;
739                  prg_size = (state->prg_chunks == 1) ? 2 * 0x4000 : state->prg_chunks * 0x4000;
794740                  memory_region_alloc(image->machine, "maincpu", 0x10000 + prg_size, 0);
795                  if (unif_board->chrrom)
796                     memory_region_alloc(image->machine, "gfx1", unif_board->chrrom * 0x2000, 0);
741                  if (state->chr_chunks)
742                     memory_region_alloc(image->machine, "gfx1", state->chr_chunks * 0x2000, 0);
797743
798744                  state->rom = memory_region(image->machine, "maincpu");
799745                  state->vrom = memory_region(image->machine, "gfx1");
r8239r8240
802748                  state->wram_size = 0x10000;
803749                  state->wram = auto_alloc_array(image->machine, UINT8, state->wram_size);
804750
805                  /* the exact number of chunks will be determined while reading the file */
751                  /* reset prg/chr chunks: the exact number of chunks will be determined while reading the file */
806752                  state->prg_chunks = 0;
807753                  state->chr_chunks = 0;
808754
src/mess/machine/nes_unif.c
r8239r8240
2323
2424static void bmc_64in1nr_set_prg( running_machine *machine )
2525{
26   UINT8 helper1 = (bmc_64in1nr_reg[1] & 0x1f);
27   UINT8 helper2 = (helper1 << 1) | ((bmc_64in1nr_reg[1] & 0x40) >> 6);
26   nes_state *state = (nes_state *)machine->driver_data;
27   UINT8 helper1 = (state->bmc_64in1nr_reg[1] & 0x1f);
28   UINT8 helper2 = (helper1 << 1) | ((state->bmc_64in1nr_reg[1] & 0x40) >> 6);
2829
29   if (bmc_64in1nr_reg[0] & 0x80)
30   if (state->bmc_64in1nr_reg[0] & 0x80)
3031   {
31      if (bmc_64in1nr_reg[1] & 0x80)
32      if (state->bmc_64in1nr_reg[1] & 0x80)
3233         prg32(machine, helper1);
3334      else
3435      {
r8239r8240
4243
4344static WRITE8_HANDLER( bmc_64in1nr_l_w )
4445{
46   nes_state *state = (nes_state *)space->machine->driver_data;
4547   LOG_MMC(("bmc_64in1nr_l_w offset: %04x, data: %02x\n", offset, data));
4648   offset += 0x100;
4749
r8239r8240
5153   case 0x1001:
5254   case 0x1002:
5355   case 0x1003:
54      bmc_64in1nr_reg[offset & 0x03] = data;
56      state->bmc_64in1nr_reg[offset & 0x03] = data;
5557      bmc_64in1nr_set_prg(space->machine);
56      chr8(space->machine, ((bmc_64in1nr_reg[0] >> 1) & 0x03) | (bmc_64in1nr_reg[2] << 2), CHRROM);
58      chr8(space->machine, ((state->bmc_64in1nr_reg[0] >> 1) & 0x03) | (state->bmc_64in1nr_reg[2] << 2), CHRROM);
5759      break;
5860   }
5961   if (offset == 0x1000)   /* reg[0] also sets mirroring */
r8239r8240
6264
6365static WRITE8_HANDLER( bmc_64in1nr_w )
6466{
67   nes_state *state = (nes_state *)space->machine->driver_data;
6568   LOG_MMC(("bmc_64in1nr_w offset: %04x, data: %02x\n", offset, data));
6669
67   bmc_64in1nr_reg[3] = data;   // reg[3] is currently unused?!?
70   state->bmc_64in1nr_reg[3] = data;   // reg[3] is currently unused?!?
6871}
6972
7073/*************************************************************
r8239r8240
171174   nes_state *state = (nes_state *)machine->driver_data;
172175   static const UINT8 masks[8] = {0x3f, 0x1f, 0x0f, 0x01, 0x03, 0x00, 0x00, 0x00};
173176   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
174   int prg_base = bmc_s24in1sc03_reg[1] << 1;
175   int prg_mask = masks[bmc_s24in1sc03_reg[0] & 0x07];
177   int prg_base = state->bmc_s24in1sc03_reg[1] << 1;
178   int prg_mask = masks[state->bmc_s24in1sc03_reg[0] & 0x07];
176179
177180   prg8_89(machine, prg_base | (state->mmc_prg_bank[0 ^ prg_flip] & prg_mask));
178181   prg8_ab(machine, prg_base | (state->mmc_prg_bank[1] & prg_mask));
r8239r8240
184187{
185188   nes_state *state = (nes_state *)machine->driver_data;
186189   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
187   UINT8 chr = (bmc_s24in1sc03_reg[0] & 0x20) ? CHRRAM : CHRROM;
188   int chr_base = (bmc_s24in1sc03_reg[2] << 3) & 0xf00;
190   UINT8 chr = (state->bmc_s24in1sc03_reg[0] & 0x20) ? CHRRAM : CHRROM;
191   int chr_base = (state->bmc_s24in1sc03_reg[2] << 3) & 0xf00;
189192
190193   chr1_x(machine, chr_page ^ 0, chr_base | (state->mmc_vrom_bank[0] & ~0x01), chr);
191194   chr1_x(machine, chr_page ^ 1, chr_base | (state->mmc_vrom_bank[0] |  0x01), chr);
r8239r8240
199202
200203static WRITE8_HANDLER( bmc_s24in1sc03_l_w )
201204{
205   nes_state *state = (nes_state *)space->machine->driver_data;
202206   LOG_MMC(("bmc_s24in1sc03_l_w offset: %04x, data: %02x\n", offset, data));
203207   offset += 0x100;
204208
205209   if (offset == 0x1ff0)
206210   {
207      bmc_s24in1sc03_reg[0] = data;
211      state->bmc_s24in1sc03_reg[0] = data;
208212      bmc_s24in1sc03_set_prg(space->machine);
209213      bmc_s24in1sc03_set_chr(space->machine);
210214   }
211215
212216   if (offset == 0x1ff1)
213217   {
214      bmc_s24in1sc03_reg[1] = data;
218      state->bmc_s24in1sc03_reg[1] = data;
215219      bmc_s24in1sc03_set_prg(space->machine);
216220   }
217221
218222   if (offset == 0x1ff2)
219223   {
220      bmc_s24in1sc03_reg[2] = data;
224      state->bmc_s24in1sc03_reg[2] = data;
221225      bmc_s24in1sc03_set_chr(space->machine);
222226   }
223227}
r8239r8240
374378   nes_state *state = (nes_state *)machine->driver_data;
375379   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
376380
377   if (!(unl_8237_reg[0] & 0x80))
381   if (!(state->unl_8237_reg[0] & 0x80))
378382   {
379383      prg8_89(machine, state->mmc_prg_bank[0 ^ prg_flip]);
380384      prg8_ab(machine, state->mmc_prg_bank[1]);
r8239r8240
391395   int i;
392396
393397   for(i = 0; i < 6; i++)
394      bank[i] = state->mmc_vrom_bank[i] | ((unl_8237_reg[1] << 6) & 0x100);
398      bank[i] = state->mmc_vrom_bank[i] | ((state->unl_8237_reg[1] << 6) & 0x100);
395399
396400   chr1_x(machine, chr_page ^ 0, (bank[0] & ~0x01), CHRROM);
397401   chr1_x(machine, chr_page ^ 1, (bank[0] |  0x01), CHRROM);
r8239r8240
405409
406410static WRITE8_HANDLER( unl_8237_l_w )
407411{
412   nes_state *state = (nes_state *)space->machine->driver_data;
408413   LOG_MMC(("unl_8237_l_w offset: %04x, data: %02x\n", offset, data));
409414   offset += 0x100;
410415
411416   if (offset == 0x1000)
412417   {
413      unl_8237_reg[0] = data;
414      if (unl_8237_reg[0] & 0x80)
418      state->unl_8237_reg[0] = data;
419      if (state->unl_8237_reg[0] & 0x80)
415420      {
416         if (unl_8237_reg[0] & 0x20)
417            prg32(space->machine, (unl_8237_reg[0] & 0x0f) >> 1);
421         if (state->unl_8237_reg[0] & 0x20)
422            prg32(space->machine, (state->unl_8237_reg[0] & 0x0f) >> 1);
418423         else
419424         {
420            prg16_89ab(space->machine, unl_8237_reg[0] & 0x1f);
421            prg16_cdef(space->machine, unl_8237_reg[0] & 0x1f);
425            prg16_89ab(space->machine, state->unl_8237_reg[0] & 0x1f);
426            prg16_cdef(space->machine, state->unl_8237_reg[0] & 0x1f);
422427         }
423428      }
424429      else
r8239r8240
427432
428433   if (offset == 0x1001)
429434   {
430      unl_8237_reg[1] = data;
435      state->unl_8237_reg[1] = data;
431436      unl_8237_set_chr(space->machine);
432437   }
433438}
r8239r8240
443448   {
444449      case 0x2000:
445450      case 0x3000:
446         unl_8237_reg[2] = 1;
451         state->unl_8237_reg[2] = 1;
447452         data = (data & 0xc0) | conv_table[data & 0x07];
448453         unl_8237_helper = state->mmc_cmd1 ^ data;
449454         state->mmc_cmd1 = data;
r8239r8240
459464
460465      case 0x4000:
461466      case 0x5000:
462         if (unl_8237_reg[2])
467         if (state->unl_8237_reg[2])
463468         {
464            unl_8237_reg[2] = 0;
469            state->unl_8237_reg[2] = 0;
465470            cmd = state->mmc_cmd1 & 0x07;
466471            switch (cmd)
467472            {
r8239r8240
13471352   }
13481353   return NULL;
13491354}
1355
1356// WIP code
1357static int unif_initialize( running_machine *machine, int idx )
1358{
1359   nes_state *state = (nes_state *)machine->driver_data;
1360   int err = 0;
1361   
1362   switch (idx)
1363   {
1364      case BMC_64IN1NR:
1365         state->bmc_64in1nr_reg[0] = 0x80;
1366         state->bmc_64in1nr_reg[1] = 0x43;
1367         state->bmc_64in1nr_reg[2] = state->bmc_64in1nr_reg[3] = 0;
1368         bmc_64in1nr_set_prg(machine);
1369         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1370         chr8(machine, 0, CHRROM);
1371         break;
1372      case BMC_190IN1:
1373         prg16_89ab(machine, 0);
1374         prg16_cdef(machine, 0);
1375         chr8(machine, 0, CHRROM);
1376         break;
1377      case BMC_A65AS:
1378         prg16_89ab(machine, 0);
1379         prg16_cdef(machine, 7);
1380         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1381         break;
1382      case BMC_GS2004:
1383      case BMC_GS2013:
1384         prg32(machine, 0xff);
1385         chr8(machine, 0, CHRRAM);
1386         break;
1387      case BMC_S24IN1SC03:
1388         state->bmc_s24in1sc03_reg[0] = 0x24;
1389         state->bmc_s24in1sc03_reg[1] = 0x9f;
1390         state->bmc_s24in1sc03_reg[2] = 0;
1391         state->mmc_prg_bank[0] = 0xfe;
1392         state->mmc_prg_bank[1] = 0xff;
1393         state->mmc_prg_bank[2] = 0xfe;
1394         state->mmc_prg_bank[3] = 0xff;
1395         state->mmc_cmd1 = 0;
1396         state->mmc_cmd2 = 0x80;
1397         bmc_s24in1sc03_set_prg(machine);
1398         bmc_s24in1sc03_set_chr(machine);
1399         break;
1400      case BMC_T262:
1401         state->mmc_cmd1 = 0;
1402         state->mmc_cmd2 = 0;
1403         prg16_89ab(machine, 0);
1404         prg16_cdef(machine, 7);
1405         break;
1406      case BMC_WS:
1407         state->mmc_cmd1 = 0;
1408         prg32(machine, 0);
1409         break;
1410      case DREAMTECH:
1411         prg16_89ab(machine, 0);
1412         prg16_cdef(machine, 8);
1413         chr8(machine, 0, CHRRAM);
1414         break;
1415      case UNL_8237:
1416         state->unl_8237_reg[0] = state->unl_8237_reg[1] = state->unl_8237_reg[2] = 0;
1417         state->mmc_prg_bank[0] = 0x00;
1418         state->mmc_prg_bank[1] = 0x01;
1419         state->mmc_prg_bank[2] = 0xfe;
1420         state->mmc_prg_bank[3] = 0xff;
1421         state->mmc_cmd1 = 0;
1422         state->mmc_cmd2 = 0x80;
1423         unl_8237_set_prg(machine);
1424         unl_8237_set_chr(machine);
1425         break;
1426      case UNL_AX5705:
1427         state->mmc_prg_bank[0] = 0;
1428         state->mmc_prg_bank[1] = 1;
1429         prg8_89(machine, state->mmc_prg_bank[0]);
1430         prg8_ab(machine, state->mmc_prg_bank[1]);
1431         prg8_cd(machine, 0xfe);
1432         prg8_ef(machine, 0xff);
1433         break;
1434      case UNL_CC21:
1435         prg32(machine, 0);
1436         chr8(machine, 0, CHRROM);
1437         break;
1438      case UNL_KOF97:
1439         mapper_initialize(machine, 4);
1440         break;
1441      case UNL_T230:
1442         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1443         prg16_89ab(machine, 0);
1444         prg16_cdef(machine, state->prg_chunks - 1);
1445         break;
1446         /* For the Boards corresponding to a Mapper, fall back to the mapper init */
1447      case STD_NROM:
1448      case HVC_FAMBASIC:
1449      case JALECO_JF01:
1450      case JALECO_JF02:
1451      case JALECO_JF03:
1452      case JALECO_JF04:
1453         mapper_initialize(machine, 0);
1454         break;
1455      case STD_SAROM:
1456      case STD_SBROM:
1457      case STD_SCROM:
1458      case STD_SEROM:
1459      case STD_SFROM:
1460      case STD_SGROM:
1461      case STD_SHROM:
1462      case STD_SJROM:
1463      case STD_SKROM:
1464      case STD_SLROM:
1465      case STD_SNROM:
1466      case STD_SOROM:
1467      case STD_SUROM:
1468      case STD_SXROM:
1469         mapper_initialize(machine, 1);
1470         break;
1471      case STD_UNROM:
1472      case STD_UOROM:
1473      case UNL_UXROM:
1474      case JALECO_JF15:
1475      case JALECO_JF18:
1476      case JALECO_JF39:
1477         mapper_initialize(machine, 2);
1478         break;
1479      case AVE_74161:
1480      case BANDAI_PT554:
1481      case STD_CNROM:
1482      case NTDEC_N715062:
1483      case TENGEN_800008:
1484         mapper_initialize(machine, 3);
1485         break;
1486      case STD_HKROM:
1487      case STD_TEROM:
1488      case STD_TBROM:
1489      case STD_TFROM:
1490      case STD_TGROM:
1491      case STD_TKROM:
1492      case STD_TLROM:
1493      case STD_TNROM:
1494      case STD_TR1ROM:
1495      case STD_TSROM:
1496      case STD_TVROM:
1497         mapper_initialize(machine, 4);
1498         break;
1499      case STD_EKROM:
1500      case STD_ELROM:
1501      case STD_ETROM:
1502      case STD_EWROM:
1503         mapper_initialize(machine, 5);
1504         break;
1505      case STD_AMROM:
1506      case STD_ANROM:
1507      case STD_AN1ROM:
1508      case STD_AOROM:
1509         mapper_initialize(machine, 7);
1510         break;
1511      case STD_FJROM:
1512      case STD_FKROM:
1513         mapper_initialize(machine, 10);
1514         break;
1515      case AGCI_47516:
1516      case AVE_NINA07:
1517      case COLORDREAMS:
1518         mapper_initialize(machine, 11);
1519         break;
1520      case REXSOFT_DBZ5:
1521         mapper_initialize(machine, 12);
1522         break;
1523      case REXSOFT_SL1632:
1524         mapper_initialize(machine, 14);
1525         break;
1526      case WAIXING_PS2:
1527         mapper_initialize(machine, 15);
1528         break;
1529      case BANDAI_LZ93D50_A:
1530      case BANDAI_LZ93D50_B:
1531         mapper_initialize(machine, 16);
1532         break;
1533      case JALECO_JF23:
1534      case JALECO_JF24:
1535      case JALECO_JF25:
1536      case JALECO_JF27:
1537      case JALECO_JF29:
1538      case JALECO_JF30:
1539      case JALECO_JF31:
1540      case JALECO_JF32:
1541      case JALECO_JF33:
1542      case JALECO_JF34:
1543      case JALECO_JF35:
1544      case JALECO_JF36:
1545      case JALECO_JF37:
1546      case JALECO_JF38:
1547      case JALECO_JF40:
1548      case JALECO_JF41:
1549         mapper_initialize(machine, 18);
1550         break;
1551      case NAMCOT_163:
1552         mapper_initialize(machine, 19);
1553         break;
1554      case KONAMI_VRC4:
1555         mapper_initialize(machine, 21);
1556         break;
1557      case KONAMI_VRC2:
1558         mapper_initialize(machine, 22);
1559         break;
1560      case KONAMI_VRC6:
1561         mapper_initialize(machine, 24);
1562         break;
1563      case IREM_G101:
1564      case IREM_G101A:
1565      case IREM_G101B:
1566         mapper_initialize(machine, 32);
1567         break;
1568      case TAITO_TC0190FMC:
1569         mapper_initialize(machine, 33);
1570         break;
1571      case STD_BNROM:
1572      case AVE_NINA01:
1573      case AVE_NINA02:
1574         mapper_initialize(machine, 34);
1575         break;
1576      case UNL_SC127:
1577         mapper_initialize(machine, 35);
1578         break;
1579      case NES_ZZ:
1580         mapper_initialize(machine, 37);
1581         break;
1582      case DISCRETE_74_161_138:
1583         mapper_initialize(machine, 38);
1584         break;
1585      case BTL_SMB2_A:
1586         mapper_initialize(machine, 40);
1587         break;
1588      case CALTRON_6IN1:
1589         mapper_initialize(machine, 41);
1590         break;
1591      case BTL_MARIOBABY:
1592      case BTL_AISENSHINICOL:
1593         mapper_initialize(machine, 42);
1594         break;
1595      case UNL_SMB2J:
1596         mapper_initialize(machine, 43);
1597         break;
1598      case BMC_SUPERBIG_7IN1:
1599         mapper_initialize(machine, 44);
1600         break;
1601      case CUSTOM_RUMBLESTATION:
1602         mapper_initialize(machine, 46);
1603         break;
1604      case NES_QJ:
1605         mapper_initialize(machine, 47);
1606         break;
1607      case TAITO_TC0190FMCP:
1608         mapper_initialize(machine, 48);
1609         break;
1610      case BMC_SUPERHIK_4IN1:
1611         mapper_initialize(machine, 49);
1612         break;
1613      case BTL_SMB2_B:
1614         mapper_initialize(machine, 50);
1615         break;
1616      case BMC_BALLGAMES_11IN1:
1617         mapper_initialize(machine, 51);
1618         break;
1619      case BMC_MARIOPARTY_7IN1:
1620         mapper_initialize(machine, 52);
1621         break;
1622      case BMC_NOVELDIAMOND:
1623         mapper_initialize(machine, 54);
1624         break;
1625      case BMC_GKA:
1626         mapper_initialize(machine, 57);
1627         break;
1628      case BMC_GKB:
1629         mapper_initialize(machine, 58);
1630         break;
1631      case RCM_TETRISFAMILY:
1632         mapper_initialize(machine, 61);
1633         break;
1634      case BMC_SUPER_700IN1:
1635         mapper_initialize(machine, 62);
1636         break;
1637      case TENGEN_800032:
1638         mapper_initialize(machine, 64);
1639         break;
1640      case IREM_H3001:
1641         mapper_initialize(machine, 65);
1642         break;
1643      case STD_GNROM:
1644      case STD_MHROM:
1645         mapper_initialize(machine, 66);
1646         break;
1647      case SUNSOFT_3:
1648         mapper_initialize(machine, 67);
1649         break;
1650      case STD_NTBROM:
1651      case SUNSOFT_4:
1652      case TENGEN_800042:
1653         mapper_initialize(machine, 68);
1654         break;
1655      case STD_JLROM:
1656      case STD_JSROM:
1657      case NES_BTR:
1658      case SUNSOFT_5B:
1659      case SUNSOFT_FME7:
1660         mapper_initialize(machine, 69);
1661         break;
1662      case BANDAI_74161:
1663      case TAITO_74161:
1664         mapper_initialize(machine, 70);
1665         break;
1666      case CAMERICA_ALGN:
1667      case CAMERICA_9093:
1668         mapper_initialize(machine, 71);
1669         break;
1670      case CAMERICA_9097:
1671         state->crc_hack = 0xf0;
1672         mapper_initialize(machine, 71);
1673         break;
1674      case JALECO_JF17:
1675      case JALECO_JF26:
1676      case JALECO_JF28:
1677         mapper_initialize(machine, 72);
1678         break;
1679      case KONAMI_VRC3:
1680         mapper_initialize(machine, 73);
1681         break;
1682      case WAIXING_TYPE_A:
1683         mapper_initialize(machine, 74);
1684         break;
1685      case KONAMI_VRC1:
1686      case JALECO_JF20:
1687      case JALECO_JF22:
1688         mapper_initialize(machine, 75);
1689         break;
1690      case NAMCOT_3446:
1691         mapper_initialize(machine, 76);
1692         break;
1693      case IREM_74161:
1694         mapper_initialize(machine, 77);
1695         break;
1696      case IREM_HOLYDIV:
1697      case JALECO_JF16:
1698         mapper_initialize(machine, 78);
1699         break;
1700      case AVE_NINA03:
1701      case AVE_NINA06:
1702      case AVE_MB91:
1703         mapper_initialize(machine, 79);
1704         break;
1705      case TAITO_X1005:
1706         mapper_initialize(machine, 80);
1707         break;
1708      case TAITO_X1017:
1709         mapper_initialize(machine, 82);
1710         break;
1711      case CONY_STD:
1712         mapper_initialize(machine, 83);
1713         break;
1714      case KONAMI_VRC7:
1715         mapper_initialize(machine, 85);
1716         break;
1717      case JALECO_JF13:
1718         mapper_initialize(machine, 86);
1719         break;
1720      case TAITO_74139:
1721      case KONAMI_74139:
1722      case JALECO_JF05:
1723      case JALECO_JF06:
1724      case JALECO_JF07:
1725      case JALECO_JF08:
1726      case JALECO_JF09:
1727      case JALECO_JF10:
1728         mapper_initialize(machine, 87);
1729         break;
1730      case NAMCOT_3443:
1731      case NAMCOT_3433:
1732         mapper_initialize(machine, 88);
1733         break;
1734      case SUNSOFT_2B:
1735         mapper_initialize(machine, 89);
1736         break;
1737      case UNL_MORTALKOMBAT2:
1738         mapper_initialize(machine, 91);
1739         break;
1740      case JALECO_JF19:
1741      case JALECO_JF21:
1742         mapper_initialize(machine, 92);
1743         break;
1744      case STD_UN1ROM:
1745         mapper_initialize(machine, 94);
1746         break;
1747      case NAMCOT_3425:
1748         mapper_initialize(machine, 95);
1749         break;
1750      case BANDAI_OEKAKIDS:
1751         mapper_initialize(machine, 96);
1752         break;
1753      case CAMERICA_GOLDENFIVE:
1754         mapper_initialize(machine, 104);
1755         break;
1756      case BTL_SMB3:
1757         mapper_initialize(machine, 106);
1758         break;
1759      case MAGICSERIES_MAGICDRAGON:
1760         mapper_initialize(machine, 107);
1761         break;
1762      case NTDEC_ASDER:
1763         mapper_initialize(machine, 112);
1764         break;
1765      case HES_STD:
1766         mapper_initialize(machine, 113);
1767         break;
1768      case SUPERGAME_LIONKING:
1769         mapper_initialize(machine, 114);
1770         break;
1771      case KASING_STD:
1772         mapper_initialize(machine, 115);
1773         break;
1774      case SOMERITEAM_SL12:
1775         mapper_initialize(machine, 116);
1776         break;
1777      case FUTUREMEDIA_STD:
1778         mapper_initialize(machine, 117);
1779         break;
1780      case STD_TKSROM:
1781      case STD_TLSROM:
1782         mapper_initialize(machine, 118);
1783         break;
1784      case STD_TQROM:
1785         mapper_initialize(machine, 119);
1786         break;
1787      case KAY_PANDAPRINCE:
1788         mapper_initialize(machine, 121);
1789         break;
1790      case TXC_22211A:
1791         mapper_initialize(machine, 132);
1792         break;
1793      case SACHEN_SA72008:
1794         mapper_initialize(machine, 133);
1795         break;
1796      case BMC_FAMILY_4646B:
1797         mapper_initialize(machine, 134);
1798         break;
1799      case SACHEN_TCU02:
1800         mapper_initialize(machine, 136);
1801         break;
1802      case SACHEN_8259D:
1803         mapper_initialize(machine, 137);
1804         break;
1805      case SACHEN_8259B:
1806         mapper_initialize(machine, 138);
1807         break;
1808      case SACHEN_8259C:
1809         mapper_initialize(machine, 139);
1810         break;
1811      case JALECO_JF11:
1812      case JALECO_JF12:
1813      case JALECO_JF14:
1814         mapper_initialize(machine, 140);
1815         break;
1816      case SACHEN_8259A:
1817         mapper_initialize(machine, 141);
1818         break;
1819      case SACHEN_TCA01:
1820         mapper_initialize(machine, 143);
1821         break;
1822      case AGCI_50282:
1823         mapper_initialize(machine, 144);
1824         break;
1825      case SACHEN_SA72007:
1826         mapper_initialize(machine, 145);
1827         break;
1828      case SACHEN_SA0161M:
1829         mapper_initialize(machine, 146);
1830         break;
1831      case SACHEN_TCU01:
1832         mapper_initialize(machine, 147);
1833         break;
1834      case SACHEN_SA0037:
1835         mapper_initialize(machine, 148);
1836         break;
1837      case SACHEN_SA0036:
1838         mapper_initialize(machine, 149);
1839         break;
1840      case SACHEN_74LS374:
1841         mapper_initialize(machine, 150);
1842         break;
1843      case BANDAI_FCG1:
1844      case BANDAI_FCG2:
1845      case BANDAI_JUMP2:
1846         mapper_initialize(machine, 153);
1847         break;
1848      case OPENCORP_DAOU306:
1849         mapper_initialize(machine, 156);
1850         break;
1851      case BANDAI_DATACH:
1852         mapper_initialize(machine, 157);
1853         break;
1854      case TENGEN_800037:
1855         mapper_initialize(machine, 158);
1856         break;
1857      case WAIXING_FFV:
1858         mapper_initialize(machine, 164);
1859         break;
1860      case SUBOR_TYPE1:
1861         mapper_initialize(machine, 166);
1862         break;
1863      case SUBOR_TYPE0:
1864         mapper_initialize(machine, 167);
1865         break;
1866      case KAISER_KS7058:
1867         mapper_initialize(machine, 171);
1868         break;
1869      case UNL_XZY:
1870         mapper_initialize(machine, 176);
1871         break;
1872      case HENGEDIANZI_STD:
1873         mapper_initialize(machine, 177);
1874         break;
1875      case WAIXING_SGZLZ:
1876         mapper_initialize(machine, 178);
1877         break;
1878      case HOSENKAN_STD:
1879         mapper_initialize(machine, 182);
1880         break;
1881      case SUNSOFT_1:
1882         mapper_initialize(machine, 184);
1883         break;
1884      case UNL_KINGOFFIGHTERS96:
1885         mapper_initialize(machine, 187);
1886         break;
1887      case BANDAI_KARAOKE:
1888         mapper_initialize(machine, 188);
1889         break;
1890      case TXC_TW:
1891         mapper_initialize(machine, 189);
1892         break;
1893      case WAIXING_TYPE_B:
1894         mapper_initialize(machine, 191);
1895         break;
1896      case WAIXING_TYPE_C:
1897         mapper_initialize(machine, 192);
1898         break;
1899      case NTDEC_FIGHTINGHERO:
1900         mapper_initialize(machine, 193);
1901         break;
1902      case WAIXING_TYPE_D:
1903         mapper_initialize(machine, 194);
1904         break;
1905      case WAIXING_TYPE_E:
1906         mapper_initialize(machine, 195);
1907         break;
1908      case BTL_SUPERBROS11:
1909         mapper_initialize(machine, 196);
1910         break;
1911      case UNL_SUPERFIGHTER3:
1912         mapper_initialize(machine, 197);
1913         break;
1914      case WAIXING_TYPE_F:
1915         mapper_initialize(machine, 198);
1916         break;
1917      case WAIXING_TYPE_G:
1918         mapper_initialize(machine, 199);
1919         break;
1920      case BMC_36IN1:
1921         mapper_initialize(machine, 200);
1922         break;
1923      case BMC_21IN1:
1924         mapper_initialize(machine, 201);
1925         break;
1926      case BMC_150IN1:
1927         mapper_initialize(machine, 202);
1928         break;
1929      case BMC_35IN1:
1930         mapper_initialize(machine, 203);
1931         break;
1932      case BMC_64IN1:
1933         mapper_initialize(machine, 204);
1934         break;
1935      case BMC_15IN1:
1936         mapper_initialize(machine, 205);
1937         break;
1938      case STD_DEROM:
1939      case STD_DE1ROM:
1940      case STD_DRROM:
1941      case TENGEN_800002:
1942      case TENGEN_800004:
1943      case TENGEN_800030:
1944         mapper_initialize(machine, 206);
1945         break;
1946      case GOUDER_37017:
1947         mapper_initialize(machine, 208);
1948         break;
1949      case BMC_SUPERHIK_300IN1:
1950         mapper_initialize(machine, 212);
1951         break;
1952      case BMC_9999999IN1:
1953         mapper_initialize(machine, 213);
1954         break;
1955      case BMC_SUPERGUN_20IN1:
1956         mapper_initialize(machine, 214);
1957         break;
1958      case SUPERGAME_BOOGERMAN:
1959         mapper_initialize(machine, 215);
1960         break;
1961      case RCM_GS2015:
1962         mapper_initialize(machine, 216);
1963         break;
1964      case BMC_GOLDENCARD_6IN1:
1965         mapper_initialize(machine, 217);
1966         break;
1967      case UNL_N625092:
1968         mapper_initialize(machine, 221);
1969         break;
1970      case BTL_DRAGONNINJA:
1971         mapper_initialize(machine, 222);
1972         break;
1973      case BMC_72IN1:
1974         mapper_initialize(machine, 225);
1975         break;
1976      case BMC_76IN1:
1977      case BMC_SUPER_42IN1:
1978         mapper_initialize(machine, 226);
1979         break;
1980      case BMC_1200IN1:
1981         mapper_initialize(machine, 227);
1982         break;
1983      case ACTENT_ACT52:
1984         mapper_initialize(machine, 228);
1985         break;
1986      case BMC_31IN1:
1987         mapper_initialize(machine, 229);
1988         break;
1989      case BMC_22GAMES:
1990         mapper_initialize(machine, 230);
1991         break;
1992      case BMC_20IN1:
1993         mapper_initialize(machine, 231);
1994         break;
1995      case CAMERICA_ALGQ:
1996      case CAMERICA_9096:
1997         mapper_initialize(machine, 232);
1998         break;
1999      case CNE_SHLZ:
2000         mapper_initialize(machine, 240);
2001         break;
2002      case TXC_MXMDHTWO:
2003         mapper_initialize(machine, 241);
2004         break;
2005      case WAIXING_ZS:
2006         mapper_initialize(machine, 242);
2007         break;
2008      case CNE_DECATHLON:
2009         mapper_initialize(machine, 244);
2010         break;
2011      case WAIXING_TYPE_H:
2012         mapper_initialize(machine, 245);
2013         break;
2014      case CNE_PSB:
2015         mapper_initialize(machine, 246);
2016         break;
2017      case WAIXING_SECURITY:
2018         mapper_initialize(machine, 249);
2019         break;
2020      case NITRA_TDA:
2021         mapper_initialize(machine, 250);
2022         break;
2023      case WAIXING_SGZ:
2024         mapper_initialize(machine, 252);
2025         break;
2026      case BMC_110IN1:
2027         mapper_initialize(machine, 255);
2028         break;
2029         
2030      case UNSUPPORTED_BOARD:
2031      default:
2032         /* Mapper not supported */
2033         err = 2;
2034         break;
2035   }
2036   
2037   return err;
2038}
2039
2040/*************************************************************
2041 
2042 nes_unif_reset
2043 
2044 Resets the mapper bankswitch areas to their defaults.
2045 It returns a value "err" that indicates if it was
2046 successful. Possible values for err are:
2047 
2048 0 = success
2049 1 = no mapper found
2050 2 = mapper not supported
2051 
2052 *************************************************************/
2053
2054int nes_unif_reset( running_machine *machine )
2055{
2056   nes_state *state = (nes_state *)machine->driver_data;
2057   int err = 0;
2058   const unif *unif_board;
2059   
2060   if (state->chr_chunks == 0)
2061      chr8(machine, 0,CHRRAM);
2062   else
2063      chr8(machine, 0,CHRROM);
2064   
2065   /* Set the mapper irq callback */
2066   unif_board = nes_unif_lookup(state->board);
2067   
2068   if (unif_board == NULL)
2069      fatalerror("Unimplemented UNIF Board");
2070   
2071   ppu2c0x_set_scanline_callback(state->ppu, unif_board ? unif_board->mmc_scanline : NULL);
2072   ppu2c0x_set_hblank_callback(state->ppu, unif_board ? unif_board->mmc_hblank : NULL);
2073   
2074   err = unif_initialize(machine, unif_board->board_idx);
2075   
2076   return err;
2077}
src/mess/machine/nes_mmc.c
r8239r8240
137137#define LOG_FDS(x) do { if (VERBOSE) logerror x; } while (0)
138138
139139
140// FIXME: mapper specific variables should be unified as much as possible to make easier future implementation of save states
141/* Local variables (a few more are defined just before handlers that use them...) */
140typedef struct __mmc
141{
142   int iNesMapper; /* iNES Mapper # */
143   
144   const char *desc;     /* Mapper description */
145   write8_space_func mmc_write_low; /* $4100-$5fff write routine */
146   read8_space_func mmc_read_low; /* $4100-$5fff read routine */
147   write8_space_func mmc_write_mid; /* $6000-$7fff write routine */
148   write8_space_func mmc_write; /* $8000-$ffff write routine */
149   void (*ppu_latch)(running_device *device, offs_t offset);
150   ppu2c0x_scanline_cb      mmc_scanline;
151   ppu2c0x_hblank_cb      mmc_hblank;
152} mmc;
142153
143static UINT8 MMC1_regs[4];
154typedef struct __unif
155{
156   const char *board; /* UNIF board */
157   
158   write8_space_func mmc_write_low; /* $4100-$5fff write routine */
159   read8_space_func mmc_read_low; /* $4100-$5fff read routine */
160   write8_space_func mmc_write_mid; /* $6000-$7fff write routine */
161   write8_space_func mmc_write; /* $8000-$ffff write routine */
162   void (*ppu_latch)(running_device *device, offs_t offset);
163   ppu2c0x_scanline_cb      mmc_scanline;
164   ppu2c0x_hblank_cb      mmc_hblank;
165   
166   int prgrom;
167   int chrrom;
168   int nvwram;
169   int wram;
170   int chrram;
171   int nt;
172   int board_idx;
173} unif;
144174
145static UINT8 MMC2_regs[4];   // these replace bank0/bank0_hi/bank1/bank1_hi
146175
147static int MMC5_rom_bank_mode;
148static int MMC5_vrom_bank_mode;
149static int MMC5_vram_protect;
150static int MMC5_scanline;
151
152// these might be unified in single mmc_reg[] array, together with state->mmc_cmd1 & state->mmc_cmd2
153// but be careful that MMC3 clones often use state->mmc_cmd1/state->mmc_cmd2 (from base MMC3) AND additional regs below!
154static UINT8 mapper45_reg[4];
155static UINT8 mapper51_reg[3];
156static UINT8 mapper83_reg[10];
157static UINT8 mapper83_low_reg[4];
158static UINT8 mapper95_reg[4];
159static UINT8 mapper115_reg[4];
160static UINT8 txc_reg[4];   // used by mappers 132, 172 & 173
161static UINT8 subor_reg[4];   // used by mappers 166 & 167
162static UINT8 sachen_reg[8];   // used by mappers 137, 138, 139 & 141
163static UINT8 mapper12_reg;
164static UINT8 map52_reg_written;
165static UINT8 map114_reg, map114_reg_enabled;
166static UINT8 map215_reg[4];
167static UINT8 map217_reg[4];
168static UINT8 map249_reg;
169static UINT8 map14_reg[2];
170static UINT8 mapper121_reg[3];
171static UINT8 mapper187_reg[4];
172static UINT8 map208_reg[5];
173static UINT8 bmc_64in1nr_reg[4];
174static UINT8 unl_8237_reg[3];
175static UINT8 bmc_s24in1sc03_reg[3];
176
177
178// mapper 68 needs these for NT mirroring!
179static int m68_mirror;
180static int m0, m1;
181
182// Famicom Jump II is identified as Mapper 153, but it works in a completely different way.
183// in particular, it requires these (not needed by other Mapper 153 games)
184static UINT8 map153_reg[8];
185static UINT8 map153_bank_latch;
186
187
188176/*************************************************************
189177
190178    Base emulation (see drivers/nes.c):
r8239r8240
710698/* Include emulation of iNES Mappers for .nes files */
711699#include "machine/nes_ines.c"
712700
701
713702/*************************************************************
714703
715    mapper_initialize
704    Support for .unf Files
716705
717    Initialize various constants needed by mappers, depending
718    on the mapper number. As long as the code is mainly iNES
719    centric, also UNIF images will call this function to
720    initialize their bankswitch and registers.
721
722706*************************************************************/
723707
724static int mapper_initialize( running_machine *machine, int mmc_num )
725{
726   nes_state *state = (nes_state *)machine->driver_data;
727   int err = 0, i;
708/* Include emulation of UNIF Boards for .unf files */
709#include "machine/nes_unif.c"
728710
729   switch (mmc_num)
730   {
731      case 0:
732         err = 1; /* No mapper found */
733         prg32(machine, 0);
734         break;
735      case 1:
736      case 155:
737         state->mmc_cmd1 = 0;
738         state->mmc_count = 0;
739         MMC1_regs[0] = 0x0f;
740         MMC1_regs[1] = MMC1_regs[2] = MMC1_regs[3] = 0;
741         set_nt_mirroring(machine, PPU_MIRROR_HORZ);
742         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
743         MMC1_set_chr(machine);
744         MMC1_set_prg(machine);
745         break;
746      case 2:
747         /* These games don't switch VROM, but some ROMs incorrectly have CHR chunks */
748         state->chr_chunks = 0;
749         prg16_89ab(machine, 0);
750         prg16_cdef(machine, state->prg_chunks - 1);
751         break;
752      case 13:
753         chr4_0(machine, 0, CHRRAM);
754         prg32(machine, 0);
755         break;
756      case 3:
757      case 185:
758         prg32(machine, 0);
759         break;
760      case 4:
761      case 206:
762         /* Only MMC3 and clones seem to use 4-screen! */
763         /* hardware uses an 8K RAM chip, but can't address the high 4K! */
764         /* that much is documented for Nintendo Gauntlet boards */
765         if (state->four_screen_vram)
766         {
767            state->extended_ntram = auto_alloc_array(machine, UINT8, 0x2000);
768            state_save_register_global_pointer(machine, state->extended_ntram, 0x2000);
769            set_nt_page(machine, 0, CART_NTRAM, 0, 1);
770            set_nt_page(machine, 1, CART_NTRAM, 1, 1);
771            set_nt_page(machine, 2, CART_NTRAM, 2, 1);
772            set_nt_page(machine, 3, CART_NTRAM, 3, 1);
773         }
774      case 12:
775      case 74:
776      case 118:
777      case 119:
778      case 191:
779      case 192:
780      case 194:
781      case 195:
782      case 196:
783      case 245:
784      case 250:
785         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;   // prg_bank[2] & prg_bank[3] remain always the same in most MMC3 variants
786         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;   // but some pirate clone mappers change them after writing certain registers
787         state->mmc_cmd1 = 0;
788         state->mmc_cmd2 = 0x80;
789         state->mmc_prg_base = state->mmc_chr_base = 0;
790         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;   // these could be init'ed as xxx_chunks-1 and they would work the same
791         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
792         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
793         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
794         break;
795      case 5:
796         /* Can switch 8k prg banks, but they are saved as 16k in size */
797         MMC5_rom_bank_mode = 3;
798         MMC5_vrom_bank_mode = 0;
799         MMC5_vram_protect = 0;
800         state->mmc5_high_chr = 0;
801         state->mmc5_vram_control = 0;
802         memset(state->MMC5_vrom_bank, 0, ARRAY_LENGTH(state->MMC5_vrom_bank));
803         state->mid_ram_enable = 0;
804         prg16_89ab(machine, state->prg_chunks - 2);
805         prg16_cdef(machine, state->prg_chunks - 1);
806         break;
807      case 6:
808         state->mmc_cmd1 = 0;
809         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
810         prg16_89ab(machine, 0);
811         prg16_cdef(machine, 7);
812         break;
813      case 7:
814         /* Bankswitches 32k at a time */
815         set_nt_mirroring(machine, PPU_MIRROR_LOW);
816         prg16_89ab(machine, 0);
817         prg16_cdef(machine, state->prg_chunks - 1);
818         break;
819      case 8:
820      case 96:
821         prg32(machine, 0);
822         break;
823      case 9:
824         MMC2_regs[0] = MMC2_regs[2] = 0;
825         MMC2_regs[1] = MMC2_regs[3] = 0;
826         state->mmc_cmd1 = state->mmc_cmd2 = 0xfe;
827         prg8_89(machine, 0);
828         //ugly hack to deal with iNES header usage of chunk count.
829         prg8_ab(machine, (state->prg_chunks << 1) - 3);
830         prg8_cd(machine, (state->prg_chunks << 1) - 2);
831         prg8_ef(machine, (state->prg_chunks << 1) - 1);
832         break;
833      case 10:
834         /* Reset VROM latches */
835         MMC2_regs[0] = MMC2_regs[2] = 0;
836         MMC2_regs[1] = MMC2_regs[3] = 0;
837         state->mmc_cmd1 = state->mmc_cmd2 = 0xfe;
838         prg16_89ab(machine, 0);
839         prg16_cdef(machine, state->prg_chunks - 1);
840         break;
841      case 11:
842         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;   // NINA-07 has no CHRROM
843         prg32(machine, 0);
844         break;
845      case 14:
846         state->mmc_extra_bank[2] = 0xfe;
847         state->mmc_extra_bank[3] = 0xff;
848         state->mmc_extra_bank[0] = state->mmc_extra_bank[1] = state->mmc_extra_bank[4] = state->mmc_extra_bank[5] = state->mmc_extra_bank[6] = 0;
849         state->mmc_extra_bank[7] = state->mmc_extra_bank[8] = state->mmc_extra_bank[9] = state->mmc_extra_bank[0xa] = state->mmc_extra_bank[0xb] = 0;
850         map14_reg[0] = map14_reg[1] = 0;
851         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
852         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
853         state->mmc_cmd1 = 0;
854         state->mmc_cmd2 = 0x80;
855         state->mmc_prg_base = state->mmc_chr_base = 0;
856         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
857         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
858         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
859         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
860         break;
861      case 15:
862         set_nt_mirroring(machine, PPU_MIRROR_VERT);
863         prg32(machine, 0);
864         break;
865      case 16:
866      case 17:
867      case 18:
868      case 157:
869      case 159:
870         state->mmc_cmd1 = 0;
871         prg16_89ab(machine, 0);
872         prg16_cdef(machine, state->prg_chunks - 1);
873         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
874         break;
875      case 19:
876         state->mmc_cmd1 = 0;
877         prg16_89ab(machine, 0);
878         prg16_cdef(machine, state->prg_chunks - 1);
879         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
880         set_nt_mirroring(machine, PPU_MIRROR_VERT);
881         break;
882      case 20:
883         state->fds_last_side = 0;
884         state->fds_count = 0;
885         state->fds_motor_on = 0;
886         state->fds_door_closed = 0;
887         state->fds_current_side = 1;
888         state->fds_head_position = 0;
889         state->fds_status0 = 0;
890         state->fds_read_mode = state->fds_write_reg = 0;
891         break;
892      case 21:
893      case 22:
894      case 23:
895      case 24:
896      case 25:
897      case 26:
898      case 33:
899      case 48:
900      case 73:
901      case 75:
902      case 80:
903      case 82:
904      case 85:
905      case 87:
906      case 117:
907      case 207:
908      case 222:
909      case 252:
910         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
911         prg16_89ab(machine, 0);
912         prg16_cdef(machine, state->prg_chunks - 1);
913         break;
914      case 32:
915         state->mmc_cmd1 = 0;
916         prg16_89ab(machine, 0);
917         prg16_cdef(machine, state->prg_chunks - 1);
918         if (state->crc_hack)
919            set_nt_mirroring(machine, PPU_MIRROR_HIGH);  // needed by Major League
920         break;
921      case 34:
922         prg32(machine, 0);
923         break;
924      case 35:
925         prg32(machine, 0xff);
926         break;
927      case 37:
928         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
929         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
930         state->mmc_cmd1 = 0;
931         state->mmc_cmd2 = 0x80;
932         state->mmc_prg_base = state->mmc_chr_base = 0;
933         state->mmc_prg_mask = 0x07;
934         state->mmc_chr_mask = 0x7f;
935         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
936         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
937         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
938         break;
939      case 40:
940         prg8_67(machine, 0xfe);
941         prg8_89(machine, 0xfc);
942         prg8_ab(machine, 0xfd);
943         prg8_cd(machine, 0xfe);
944         prg8_ef(machine, 0xff);
945         break;
946      case 41:
947         state->mmc_cmd1 = 0;
948         prg32(machine, 0);
949         break;
950      case 42:
951         prg32(machine, state->prg_chunks - 1);
952         break;
953      case 43:
954         prg32(machine, 0);
955         if (state->battery)
956            memset(state->battery_ram, 0x2000, 0xff);
957         else if (state->prg_ram)
958            memset(state->wram, 0x2000, 0xff);
959         break;
960      case 44:
961      case 47:
962      case 49:
963         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
964         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
965         state->mmc_cmd1 = 0;
966         state->mmc_cmd2 = 0x80;
967         state->mmc_prg_base = state->mmc_chr_base = 0;
968         state->mmc_prg_mask = 0x0f;
969         state->mmc_chr_mask = 0x7f;
970         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
971         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
972         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
973         break;
974      case 45:
975         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
976         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
977         state->mmc_cmd1 = 0;
978         state->mmc_cmd2 = 0x80;
979         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
980         mapper45_reg[0] = mapper45_reg[1] = mapper45_reg[2] = mapper45_reg[3] = 0;
981         state->mmc_prg_base = 0x30;
982         state->mmc_prg_mask = 0x3f;
983         state->mmc_chr_base = 0;
984         state->mmc_chr_mask = 0x7f;
985         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
986         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
987//         memory_set_bankptr(machine, "bank5", state->wram);
988         break;
989      case 46:
990         prg32(machine, 0);
991         chr8(machine, 0, CHRROM);
992         break;
993      case 54:
994         prg32(machine, 0);
995         chr8(machine, 0, CHRROM);
996         set_nt_mirroring(machine, PPU_MIRROR_VERT);
997         break;
998      case 50:
999         prg8_67(machine, 0x0f);
1000         prg8_89(machine, 0x08);
1001         prg8_ab(machine, 0x09);
1002         prg8_cd(machine, 0);
1003         prg8_ef(machine, 0x0b);
1004         break;
1005      case 51:
1006         mapper51_reg[0] = 0x01;
1007         mapper51_reg[1] = 0x00;
1008         mapper51_set_banks(machine);
1009         break;
1010      case 52:
1011         state->mmc_prg_bank[0] = 0xfe;
1012         state->mmc_prg_bank[2] = 0xfe;
1013         state->mmc_prg_bank[1] = 0xff;
1014         state->mmc_prg_bank[3] = 0xff;
1015         state->mmc_cmd1 = 0;
1016         state->mmc_cmd2 = 0x80;
1017         map52_reg_written = 0;
1018         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1019         state->mmc_prg_base = 0;
1020         state->mmc_prg_mask = 0x1f;
1021         state->mmc_chr_base = 0;
1022         state->mmc_chr_mask = 0xff;
1023         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1024         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1025//         memory_set_bankptr(machine, "bank5", state->wram);
1026         break;
1027      case 57:
1028         state->mmc_cmd1 = 0x00;
1029         state->mmc_cmd2 = 0x00;
1030         prg16_89ab(machine, 0);
1031         prg16_cdef(machine, 0);
1032         chr8(machine, 0, CHRROM);
1033         break;
1034      case 58:
1035         prg32(machine, 0);
1036         chr8(machine, 0, CHRROM);
1037         break;
1038      case 61:
1039      case 62:
1040      case 86:
1041         prg32(machine, 0);
1042         break;
1043      case 64:
1044      case 158:
1045         state->mmc_cmd1 = 0;
1046         prg16_89ab(machine, state->prg_chunks - 1);
1047         prg16_cdef(machine, state->prg_chunks - 1);
1048         break;
1049      case 65:
1050      case 67:
1051      case 69:
1052      case 72:
1053      case 78:
1054      case 92:
1055         prg16_89ab(machine, 0);
1056         prg16_cdef(machine, state->prg_chunks - 1);
1057         break;
1058      case 68:
1059         m68_mirror = m0 = m1 = 0;
1060         prg16_89ab(machine, 0);
1061         prg16_cdef(machine, state->prg_chunks - 1);
1062         break;
1063      case 70:
1064         prg16_89ab(machine, state->prg_chunks - 2);
1065         prg16_cdef(machine, state->prg_chunks - 1);
1066         break;
1067      case 71:
1068         set_nt_mirroring(machine, PPU_MIRROR_HORZ);
1069         prg16_89ab(machine, 0);
1070         prg16_cdef(machine, state->prg_chunks - 1);
1071         break;
1072      case 76:
1073         prg8_89(machine, 0);
1074         prg8_ab(machine, 1);
1075         //cd is bankable, but this should init all banks just fine.
1076         prg16_cdef(machine, state->prg_chunks - 1);
1077         chr2_0(machine, 0, CHRROM);
1078         chr2_2(machine, 1, CHRROM);
1079         chr2_4(machine, 2, CHRROM);
1080         chr2_6(machine, 3, CHRROM);
1081         state->mmc_cmd1 = 0;
1082         break;
1083      case 77:
1084         prg32(machine, 0);
1085         chr2_2(machine, 0, CHRROM);
1086         chr2_4(machine, 1, CHRROM);
1087         chr2_6(machine, 2, CHRROM);
1088         break;
1089      case 79:
1090      case 146:
1091         /* Mirroring always horizontal...? */
1092//          Mirroring = 1;
1093         set_nt_mirroring(machine, PPU_MIRROR_HORZ);
1094         chr8(machine, 0, CHRROM);
1095         prg32(machine, 0);
1096         break;
1097      case 228:
1098         chr8(machine, 0, CHRROM);
1099         prg32(machine, 0);
1100         break;
1101      case 83:
1102         mapper83_reg[9] = 0x0f;
1103         prg8_cd(machine, 0x1e);
1104         prg8_ef(machine, 0x1f);
1105         break;
1106      case 88:
1107         prg8_89(machine, 0xc);
1108         prg8_ab(machine, 0xd);
1109         prg8_cd(machine, 0xe);
1110         prg8_ef(machine, 0xf);
1111         break;
1112      case 89:
1113         prg16_89ab(machine, 0);
1114         prg16_cdef(machine, state->prg_chunks - 1);
1115         set_nt_mirroring(machine, PPU_MIRROR_LOW);
1116         break;
1117      case 91:
1118         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1119         prg16_89ab(machine, state->prg_chunks - 1);
1120         prg16_cdef(machine, state->prg_chunks - 1);
1121         break;
1122      case 93:
1123      case 94:
1124      case 95:
1125      case 101:
1126         prg16_89ab(machine, 0);
1127         prg16_cdef(machine, state->prg_chunks - 1);
1128         break;
1129      case 97:
1130         prg16_89ab(machine, state->prg_chunks - 1);
1131         prg16_cdef(machine, state->prg_chunks - 1);
1132         break;
1133      case 112:
1134         state->mmc_cmd1 = 0;
1135         prg16_89ab(machine, 0);
1136         prg16_cdef(machine, state->prg_chunks - 1);
1137         break;
1138      case 36:
1139      case 38:
1140      case 39:
1141      case 66:
1142      case 107:
1143      case 113:
1144      case 177:
1145      case 179:
1146      case 184:
1147      case 140:
1148      case 144:
1149      case 240:
1150      case 241:
1151      case 242:
1152      case 244:
1153         prg32(machine, 0);
1154         break;
1155      case 104:
1156         prg16_89ab(machine, 0x00);
1157         prg16_cdef(machine, 0x0f);
1158         break;
1159      case 106:
1160         prg8_89(machine, (state->prg_chunks << 1) - 1);
1161         prg8_ab(machine, 0);
1162         prg8_cd(machine, 0);
1163         prg8_ef(machine, (state->prg_chunks << 1) - 1);
1164         break;
1165      case 114:
1166         map114_reg = map114_reg_enabled = 0;
1167         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1168         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1169         state->mmc_cmd1 = 0;
1170         state->mmc_cmd2 = 0x80;
1171         state->mmc_prg_base = state->mmc_chr_base = 0;
1172         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1173         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1174         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1175         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1176         break;
1177      case 115:
1178         mapper115_reg[0] = 0;
1179         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1180         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1181         state->mmc_cmd1 = 0;
1182         state->mmc_cmd2 = 0x80;
1183         state->mmc_prg_base = state->mmc_chr_base = 0;
1184         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1185         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1186         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1187         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1188         break;
1189      case 121:
1190         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1191         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1192         mapper121_reg[0] = mapper121_reg[1] = mapper121_reg[2] = 0;
1193         state->mmc_cmd1 = 0;
1194         state->mmc_cmd2 = 0x80;
1195         state->mmc_prg_base = state->mmc_chr_base = 0;
1196         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1197         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1198         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1199         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1200         break;
1201      case 132:
1202      case 172:
1203      case 173:
1204         txc_reg[0] = txc_reg[1] = txc_reg[2] = txc_reg[3] = 0;
1205         prg32(machine, 0);
1206         break;
1207      case 134:
1208         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1209         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1210         state->mmc_cmd1 = 0;
1211         state->mmc_cmd2 = 0x80;
1212         state->mmc_prg_base = state->mmc_chr_base = 0;
1213         state->mmc_prg_mask = 0x1f;
1214         state->mmc_chr_mask = 0xff;
1215         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1216         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1217         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1218         break;
1219      case 166:
1220         subor_reg[0] = subor_reg[1] = subor_reg[2] = subor_reg[3] = 0;
1221         prg16_89ab(machine, 0);
1222         prg16_cdef(machine, 0x07);
1223         break;
1224      case 167:
1225         subor_reg[0] = subor_reg[1] = subor_reg[2] = subor_reg[3] = 0;
1226         prg16_89ab(machine, 0);
1227         prg16_cdef(machine, 0x20);
1228         break;
1229      case 136:
1230         state->mmc_cmd1 = 0;
1231         prg32(machine, 0);
1232         break;
1233      case 137:
1234         state->mmc_cmd1 = 0;
1235         prg32(machine, 0);
1236         chr8(machine, state->chr_chunks - 1, CHRROM);
1237         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1238         break;
1239      case 138:
1240      case 139:
1241      case 141:
1242         state->mmc_cmd1 = 0;
1243         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1244         prg32(machine, 0);
1245         chr8(machine, 0, state->mmc_chr_source);
1246         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1247         break;
1248      case 143:
1249         prg16_89ab(machine, 0);
1250         prg16_cdef(machine, 1);
1251         chr8(machine, 0, CHRROM);
1252         break;
1253      case 133:
1254      case 145:
1255      case 147:
1256      case 148:
1257      case 149:
1258      case 171:
1259         prg32(machine, 0);
1260         chr8(machine, 0, CHRROM);
1261         break;
1262      case 150:
1263         state->mmc_cmd1 = 0;
1264         prg32(machine, 0);
1265         chr8(machine, 0, CHRROM);
1266         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1267         break;
1268      case 152:
1269         prg16_89ab(machine, 0);
1270         prg16_cdef(machine, state->prg_chunks - 1);
1271         chr8(machine, 0, CHRROM);
1272         break;
1273      case 153:
1274         state->mmc_cmd1 = 0;
1275         prg16_89ab(machine, 0);
1276         prg16_cdef(machine, state->prg_chunks - 1);
1277         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1278         // Famicom Jump II needs also the following!
1279         for (i = 0; i < 8; i++)
1280            map153_reg[i] = 0;
1281711
1282         map153_bank_latch = 0;
1283         if (state->crc_hack)
1284            mapper153_set_prg(machine);
1285         break;
1286      case 154:
1287         prg16_89ab(machine, state->prg_chunks - 2);
1288         prg16_cdef(machine, state->prg_chunks - 1);
1289         break;
1290      case 156:
1291         prg16_89ab(machine, state->prg_chunks - 2);
1292         prg16_cdef(machine, state->prg_chunks - 1);
1293         set_nt_mirroring(machine, PPU_MIRROR_LOW);
1294         break;
1295      case 164:
1296         prg32(machine, 0xff);
1297         state->mid_ram_enable = 1;
1298         break;
1299      case 176:
1300         prg32(machine, (state->prg_chunks - 1) >> 1);
1301         break;
1302      case 178:
1303         state->mmc_cmd1 = 0;
1304         prg32(machine, 0);
1305         break;
1306      case 180:
1307         prg16_89ab(machine, 0);
1308         prg16_cdef(machine, 0);
1309         break;
1310      case 182:
1311         prg32(machine, (state->prg_chunks - 1) >> 1);
1312         break;
1313      case 188:
1314         prg16_89ab(machine, 0);
1315         prg16_cdef(machine, (state->prg_chunks - 1) ^ 0x08);
1316         break;
1317      case 187:
1318         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1319         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1320         mapper187_reg[0] = mapper187_reg[1] = mapper187_reg[2] = mapper187_reg[3] = 0;
1321         state->mmc_cmd1 = 0;
1322         state->mmc_cmd2 = 0x80;
1323         state->mmc_prg_base = state->mmc_chr_base = 0;
1324         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1325         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1326         mapper187_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1327         mapper187_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1328         break;
1329      case 189:
1330         state->mmc_cmd1 = 0;
1331         state->mmc_cmd2 = 0x80;
1332         state->mmc_chr_base = 0;
1333         state->mmc_chr_mask = 0xff;
1334         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1335         prg32(machine, 0);
1336         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1337         break;
1338      case 193:
1339         prg32(machine, (state->prg_chunks - 1) >> 1);
1340         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1341         break;
1342      case 197:
1343         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1344         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1345         state->mmc_cmd1 = 0;
1346         state->mmc_cmd2 = 0x80;
1347         state->mmc_prg_base = state->mmc_chr_base = 0;
1348         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1349         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1350         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1351         mapper197_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1352         break;
1353      case 198:
1354         state->mmc_prg_bank[0] = 0x00;
1355         state->mmc_prg_bank[1] = 0x01;
1356         state->mmc_prg_bank[2] = 0x4e;
1357         state->mmc_prg_bank[3] = 0x4f;
1358         state->mmc_cmd1 = 0;
1359         state->mmc_cmd2 = 0x80;
1360         state->mmc_prg_base = state->mmc_chr_base = 0;
1361         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1362         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1363         mapper198_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1364         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1365         break;
1366      case 199:
1367         state->mmc_prg_bank[0] = 0x00;
1368         state->mmc_prg_bank[1] = 0x01;
1369         state->mmc_prg_bank[2] = 0x3e;
1370         state->mmc_prg_bank[3] = 0x3f;
1371         state->mmc_cmd1 = 0;
1372         state->mmc_cmd2 = 0x80;
1373         state->mmc_prg_base = state->mmc_chr_base = 0;
1374         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1375         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1376         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1377         mapper199_set_chr(machine, state->mmc_chr_base, state->mmc_chr_mask);
1378         break;
1379      case 200:
1380         prg16_89ab(machine, state->prg_chunks - 1);
1381         prg16_cdef(machine, state->prg_chunks - 1);
1382         break;
1383      case 201:
1384      case 213:
1385         prg32(machine, 0);
1386         chr8(machine, 0, CHRROM);
1387         break;
1388      case 202:
1389      case 203:
1390      case 204:
1391      case 214:
1392         prg16_89ab(machine, 0);
1393         prg16_cdef(machine, 0);
1394         chr8(machine, 0, CHRROM);
1395         break;
1396      case 205:
1397         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1398         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1399         state->mmc_cmd1 = 0;
1400         state->mmc_cmd2 = 0x80;
1401         state->mmc_prg_base = 0x10;
1402         state->mmc_prg_mask = 0x1f;
1403         state->mmc_chr_base = 0;
1404         state->mmc_chr_mask = 0xff;
1405         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1406         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1407         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1408         break;
1409      case 208:
1410         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1411         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1412         map208_reg[0] = map208_reg[1] = map208_reg[2] = map208_reg[3] = map208_reg[4] = 0;
1413         state->mmc_cmd1 = 0;
1414         state->mmc_cmd2 = 0x80;
1415         state->mmc_prg_base = state->mmc_chr_base = 0;
1416         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1417         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1418         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1419         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1420         break;
1421      case 212:
1422         chr8(machine, 0xff, CHRROM);
1423         prg32(machine, 0xff);
1424         break;
1425      case 215:
1426         state->mmc_prg_bank[0] = 0x00;
1427         state->mmc_prg_bank[2] = 0xfe;
1428         state->mmc_prg_bank[1] = 0x01;
1429         state->mmc_prg_bank[3] = 0xff;
1430         state->mmc_cmd1 = 0;
1431         state->mmc_cmd2 = 0x80;
1432         map215_reg[0] = 0x00;
1433         map215_reg[1] = 0xff;
1434         map215_reg[2] = 0x04;
1435         map215_reg[3] = 0;
1436         state->mmc_prg_base = 0;
1437         state->mmc_prg_mask = 0x1f;
1438         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1439         mapper215_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1440         mapper215_set_chr(machine, state->mmc_chr_source);
1441         break;
1442      case 216:
1443         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1444         prg32(machine, 0);
1445         chr8(machine, 0, state->mmc_chr_source);
1446         break;
1447      case 217:
1448         map217_reg[0] = 0x00;
1449         map217_reg[1] = 0xff;
1450         map217_reg[2] = 0x03;
1451         map217_reg[3] = 0;
1452         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1453         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1454         state->mmc_cmd1 = 0;
1455         state->mmc_cmd2 = 0x80;
1456         state->mmc_prg_base = state->mmc_chr_base = 0;
1457         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1458         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1459         mapper217_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1460         mapper217_set_chr(machine, state->mmc_chr_source);
1461         break;
1462      case 221:
1463         state->mmc_cmd1 = 0;
1464         state->mmc_cmd2 = 0;
1465         prg16_89ab(machine, 0);
1466         prg16_cdef(machine, 0);
1467         break;
1468      case 225:
1469         prg32(machine, 0);
1470         break;
1471      case 226:
1472         state->mmc_cmd1 = 0;
1473         state->mmc_cmd2 = 0;
1474         prg32(machine, 0);
1475         break;
1476      case 227:
1477         prg16_89ab(machine, 0);
1478         prg16_cdef(machine, 0);
1479         break;
1480      case 229:
1481      case 255:
1482         prg16_89ab(machine, 0);
1483         prg16_cdef(machine, 1);
1484         chr8(machine, 0, CHRROM);
1485         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1486         break;
1487      case 230:
1488         prg16_89ab(machine, 0);
1489         prg16_cdef(machine, 7);
1490         break;
1491      case 231:
1492         prg16_89ab(machine, 0);
1493         prg16_cdef(machine, state->prg_chunks - 1);
1494         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1495         break;
1496      case 232:
1497         state->mmc_cmd1 = 0x18;
1498         state->mmc_cmd2 = 0x00;
1499         mapper232_set_prg(machine);
1500         break;
1501      case 243:
1502         state->mmc_vrom_bank[0] = 3;
1503         state->mmc_cmd1 = 0;
1504         chr8(machine, 3, CHRROM);
1505         prg32(machine, 0);
1506         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1507         break;
1508      case 246:
1509         prg32(machine, 0xff);
1510         break;
1511      case 249:
1512         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
1513         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
1514         state->mmc_cmd1 = 0;
1515         state->mmc_cmd2 = 0x80;
1516         map249_reg = 0;
1517         state->mmc_prg_base = state->mmc_chr_base = 0;
1518         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1519         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1520         mapper249_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1521         mapper249_set_chr(machine, state->mmc_chr_base, state->mmc_chr_mask);
1522         break;
1523      default:
1524         /* Mapper not supported */
1525         err = 2;
1526         break;
1527   }
1528
1529   return err;
1530}
1531
1532712/*************************************************************
713 
714    Setting up memory handlers
715 
716 *************************************************************/
1533717
1534    nes_mapper_reset
1535
1536    Resets the mapper bankswitch areas to their defaults.
1537    It returns a value "err" that indicates if it was
1538    successful. Possible values for err are:
1539
1540    0 = success
1541    1 = no mapper found
1542    2 = mapper not supported
1543
1544*************************************************************/
1545
1546int nes_mapper_reset( running_machine *machine, int mmc_num )
718void mapper_handlers_setup( running_machine *machine )
1547719{
1548720   nes_state *state = (nes_state *)machine->driver_data;
1549   int err = 0, i;
1550   const mmc *mapper;
721   const mmc *mapper = nes_mapper_lookup(state->mapper);
1551722
1552   if (state->chr_chunks == 0)
1553      chr8(machine, 0,CHRRAM);
723   if (mapper)
724   {
725      state->mmc_write_low = mapper->mmc_write_low;
726      state->mmc_write_mid = mapper->mmc_write_mid;
727      state->mmc_write = mapper->mmc_write;
728      state->mmc_read_low = mapper->mmc_read_low;
729      state->mmc_read_mid = NULL;   // in progress
730      state->mmc_read = NULL;   // in progress
731      ppu_latch = mapper->ppu_latch;
732   }
1554733   else
1555      chr8(machine, 0,CHRROM);
1556
1557   /* Set the mapper irq callback */
1558   mapper = nes_mapper_lookup(mmc_num);
1559
1560   if (mapper == NULL)
1561      fatalerror("Unimplemented Mapper %d", mmc_num);
1562//      logerror("Mapper %d is not yet supported, defaulting to no mapper.\n", mmc_num);    // this one would be a better output
1563
1564   ppu2c0x_set_scanline_callback(state->ppu, mapper ? mapper->mmc_scanline : NULL);
1565   ppu2c0x_set_hblank_callback(state->ppu, mapper ? mapper->mmc_hblank : NULL);
1566
1567   state->mmc5_vram_control = 0;
1568
1569   /* Here, we init a few helpers: 4 prg banks and 16 chr banks - some mappers use them */
1570   for (i = 0; i < 4; i++)
1571      state->mmc_prg_bank[i] = 0;
1572   for (i = 0; i < 16; i++)
1573      state->mmc_vrom_bank[i] = 0;
1574   for (i = 0; i < 16; i++)
1575      state->mmc_extra_bank[i] = 0;
1576
1577   /* Finally, we init IRQ-related quantities. */
1578   state->IRQ_enable = state->IRQ_enable_latch = 0;
1579   state->IRQ_count = state->IRQ_count_latch = 0;
1580   state->IRQ_toggle = 0;
1581
1582   err = mapper_initialize(machine, mmc_num);
1583
1584   return err;
734   {
735      logerror("Mapper %d is not yet supported, defaulting to no mapper.\n",state->mapper);
736      state->mmc_write_low = NULL;
737      state->mmc_write_mid = NULL;
738      state->mmc_write = NULL;
739      state->mmc_read_low = NULL;
740      state->mmc_read_mid = NULL;   // in progress
741      state->mmc_read = NULL;   // in progress
742      ppu_latch = NULL;
743   }
1585744}
1586745
1587/*************************************************************
1588
1589    Support for .unf Files
1590
1591*************************************************************/
1592
1593/* Include emulation of UNIF Boards for .unf files */
1594#include "machine/nes_unif.c"
1595
1596// WIP code
1597static int unif_initialize( running_machine *machine, int idx )
746void unif_handlers_setup( running_machine *machine )
1598747{
1599748   nes_state *state = (nes_state *)machine->driver_data;
1600   int err = 0;
1601   
1602   switch (idx)
749   const unif *board = nes_unif_lookup(state->board);
750
751   if (board)
1603752   {
1604      case BMC_64IN1NR:
1605         bmc_64in1nr_reg[0] = 0x80;
1606         bmc_64in1nr_reg[1] = 0x43;
1607         bmc_64in1nr_reg[2] = bmc_64in1nr_reg[3] = 0;
1608         bmc_64in1nr_set_prg(machine);
1609         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1610         chr8(machine, 0, CHRROM);
1611         break;
1612      case BMC_190IN1:
1613         prg16_89ab(machine, 0);
1614         prg16_cdef(machine, 0);
1615         chr8(machine, 0, CHRROM);
1616         break;
1617      case BMC_A65AS:
1618         prg16_89ab(machine, 0);
1619         prg16_cdef(machine, 7);
1620         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1621         break;
1622      case BMC_GS2004:
1623      case BMC_GS2013:
1624         prg32(machine, 0xff);
1625         chr8(machine, 0, CHRRAM);
1626         break;
1627      case BMC_S24IN1SC03:
1628         bmc_s24in1sc03_reg[0] = 0x24;
1629         bmc_s24in1sc03_reg[1] = 0x9f;
1630         bmc_s24in1sc03_reg[2] = 0;
1631         state->mmc_prg_bank[0] = 0xfe;
1632         state->mmc_prg_bank[1] = 0xff;
1633         state->mmc_prg_bank[2] = 0xfe;
1634         state->mmc_prg_bank[3] = 0xff;
1635         state->mmc_cmd1 = 0;
1636         state->mmc_cmd2 = 0x80;
1637         bmc_s24in1sc03_set_prg(machine);
1638         bmc_s24in1sc03_set_chr(machine);
1639         break;
1640      case BMC_T262:
1641         state->mmc_cmd1 = 0;
1642         state->mmc_cmd2 = 0;
1643         prg16_89ab(machine, 0);
1644         prg16_cdef(machine, 7);
1645         break;
1646      case BMC_WS:
1647         state->mmc_cmd1 = 0;
1648         prg32(machine, 0);
1649         break;
1650      case DREAMTECH:
1651         prg16_89ab(machine, 0);
1652         prg16_cdef(machine, 8);
1653         chr8(machine, 0, CHRRAM);
1654         break;
1655      case UNL_8237:
1656         unl_8237_reg[0] = unl_8237_reg[1] = unl_8237_reg[2] = 0;
1657         state->mmc_prg_bank[0] = 0x00;
1658         state->mmc_prg_bank[1] = 0x01;
1659         state->mmc_prg_bank[2] = 0xfe;
1660         state->mmc_prg_bank[3] = 0xff;
1661         state->mmc_cmd1 = 0;
1662         state->mmc_cmd2 = 0x80;
1663         unl_8237_set_prg(machine);
1664         unl_8237_set_chr(machine);
1665         break;
1666      case UNL_AX5705:
1667         state->mmc_prg_bank[0] = 0;
1668         state->mmc_prg_bank[1] = 1;
1669         prg8_89(machine, state->mmc_prg_bank[0]);
1670         prg8_ab(machine, state->mmc_prg_bank[1]);
1671         prg8_cd(machine, 0xfe);
1672         prg8_ef(machine, 0xff);
1673         break;
1674      case UNL_CC21:
1675         prg32(machine, 0);
1676         chr8(machine, 0, CHRROM);
1677         break;
1678      case UNL_KOF97:
1679         mapper_initialize(machine, 4);
1680         break;
1681      case UNL_T230:
1682         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1683         prg16_89ab(machine, 0);
1684         prg16_cdef(machine, state->prg_chunks - 1);
1685         break;
1686         /* For the Boards corresponding to a Mapper, fall back to the mapper init */
1687      case STD_NROM:
1688      case HVC_FAMBASIC:
1689      case JALECO_JF01:
1690      case JALECO_JF02:
1691      case JALECO_JF03:
1692      case JALECO_JF04:
1693         mapper_initialize(machine, 0);
1694         break;
1695      case STD_SAROM:
1696      case STD_SBROM:
1697      case STD_SCROM:
1698      case STD_SEROM:
1699      case STD_SFROM:
1700      case STD_SGROM:
1701      case STD_SHROM:
1702      case STD_SJROM:
1703      case STD_SKROM:
1704      case STD_SLROM:
1705      case STD_SNROM:
1706      case STD_SOROM:
1707      case STD_SUROM:
1708      case STD_SXROM:
1709         mapper_initialize(machine, 1);
1710         break;
1711      case STD_UNROM:
1712      case STD_UOROM:
1713      case UNL_UXROM:
1714      case JALECO_JF15:
1715      case JALECO_JF18:
1716      case JALECO_JF39:
1717         mapper_initialize(machine, 2);
1718         break;
1719      case AVE_74161:
1720      case BANDAI_PT554:
1721      case STD_CNROM:
1722      case NTDEC_N715062:
1723      case TENGEN_800008:
1724         mapper_initialize(machine, 3);
1725         break;
1726      case STD_HKROM:
1727      case STD_TEROM:
1728      case STD_TBROM:
1729      case STD_TFROM:
1730      case STD_TGROM:
1731      case STD_TKROM:
1732      case STD_TLROM:
1733      case STD_TNROM:
1734      case STD_TR1ROM:
1735      case STD_TSROM:
1736      case STD_TVROM:
1737         mapper_initialize(machine, 4);
1738         break;
1739      case STD_EKROM:
1740      case STD_ELROM:
1741      case STD_ETROM:
1742      case STD_EWROM:
1743         mapper_initialize(machine, 5);
1744         break;
1745      case STD_AMROM:
1746      case STD_ANROM:
1747      case STD_AN1ROM:
1748      case STD_AOROM:
1749         mapper_initialize(machine, 7);
1750         break;
1751      case STD_FJROM:
1752      case STD_FKROM:
1753         mapper_initialize(machine, 10);
1754         break;
1755      case AGCI_47516:
1756      case AVE_NINA07:
1757      case COLORDREAMS:
1758         mapper_initialize(machine, 11);
1759         break;
1760      case REXSOFT_DBZ5:
1761         mapper_initialize(machine, 12);
1762         break;
1763      case REXSOFT_SL1632:
1764         mapper_initialize(machine, 14);
1765         break;
1766      case WAIXING_PS2:
1767         mapper_initialize(machine, 15);
1768         break;
1769      case BANDAI_LZ93D50_A:
1770      case BANDAI_LZ93D50_B:
1771         mapper_initialize(machine, 16);
1772         break;
1773      case JALECO_JF23:
1774      case JALECO_JF24:
1775      case JALECO_JF25:
1776      case JALECO_JF27:
1777      case JALECO_JF29:
1778      case JALECO_JF30:
1779      case JALECO_JF31:
1780      case JALECO_JF32:
1781      case JALECO_JF33:
1782      case JALECO_JF34:
1783      case JALECO_JF35:
1784      case JALECO_JF36:
1785      case JALECO_JF37:
1786      case JALECO_JF38:
1787      case JALECO_JF40:
1788      case JALECO_JF41:
1789         mapper_initialize(machine, 18);
1790         break;
1791      case NAMCOT_163:
1792         mapper_initialize(machine, 19);
1793         break;
1794      case KONAMI_VRC4:
1795         mapper_initialize(machine, 21);
1796         break;
1797      case KONAMI_VRC2:
1798         mapper_initialize(machine, 22);
1799         break;
1800      case KONAMI_VRC6:
1801         mapper_initialize(machine, 24);
1802         break;
1803      case IREM_G101:
1804      case IREM_G101A:
1805      case IREM_G101B:
1806         mapper_initialize(machine, 32);
1807         break;
1808      case TAITO_TC0190FMC:
1809         mapper_initialize(machine, 33);
1810         break;
1811      case STD_BNROM:
1812      case AVE_NINA01:
1813      case AVE_NINA02:
1814         mapper_initialize(machine, 34);
1815         break;
1816      case UNL_SC127:
1817         mapper_initialize(machine, 35);
1818         break;
1819      case NES_ZZ:
1820         mapper_initialize(machine, 37);
1821         break;
1822      case DISCRETE_74_161_138:
1823         mapper_initialize(machine, 38);
1824         break;
1825      case BTL_SMB2_A:
1826         mapper_initialize(machine, 40);
1827         break;
1828      case CALTRON_6IN1:
1829         mapper_initialize(machine, 41);
1830         break;
1831      case BTL_MARIOBABY:
1832      case BTL_AISENSHINICOL:
1833         mapper_initialize(machine, 42);
1834         break;
1835      case UNL_SMB2J:
1836         mapper_initialize(machine, 43);
1837         break;
1838      case BMC_SUPERBIG_7IN1:
1839         mapper_initialize(machine, 44);
1840         break;
1841      case CUSTOM_RUMBLESTATION:
1842         mapper_initialize(machine, 46);
1843         break;
1844      case NES_QJ:
1845         mapper_initialize(machine, 47);
1846         break;
1847      case TAITO_TC0190FMCP:
1848         mapper_initialize(machine, 48);
1849         break;
1850      case BMC_SUPERHIK_4IN1:
1851         mapper_initialize(machine, 49);
1852         break;
1853      case BTL_SMB2_B:
1854         mapper_initialize(machine, 50);
1855         break;
1856      case BMC_BALLGAMES_11IN1:
1857         mapper_initialize(machine, 51);
1858         break;
1859      case BMC_MARIOPARTY_7IN1:
1860         mapper_initialize(machine, 52);
1861         break;
1862      case BMC_NOVELDIAMOND:
1863         mapper_initialize(machine, 54);
1864         break;
1865      case BMC_GKA:
1866         mapper_initialize(machine, 57);
1867         break;
1868      case BMC_GKB:
1869         mapper_initialize(machine, 58);
1870         break;
1871      case RCM_TETRISFAMILY:
1872         mapper_initialize(machine, 61);
1873         break;
1874      case BMC_SUPER_700IN1:
1875         mapper_initialize(machine, 62);
1876         break;
1877      case TENGEN_800032:
1878         mapper_initialize(machine, 64);
1879         break;
1880      case IREM_H3001:
1881         mapper_initialize(machine, 65);
1882         break;
1883      case STD_GNROM:
1884      case STD_MHROM:
1885         mapper_initialize(machine, 66);
1886         break;
1887      case SUNSOFT_3:
1888         mapper_initialize(machine, 67);
1889         break;
1890      case STD_NTBROM:
1891      case SUNSOFT_4:
1892      case TENGEN_800042:
1893         mapper_initialize(machine, 68);
1894         break;
1895      case STD_JLROM:
1896      case STD_JSROM:
1897      case NES_BTR:
1898      case SUNSOFT_5B:
1899      case SUNSOFT_FME7:
1900         mapper_initialize(machine, 69);
1901         break;
1902      case BANDAI_74161:
1903      case TAITO_74161:
1904         mapper_initialize(machine, 70);
1905         break;
1906      case CAMERICA_ALGN:
1907      case CAMERICA_9093:
1908         mapper_initialize(machine, 71);
1909         break;
1910      case CAMERICA_9097:
1911         state->crc_hack = 0xf0;
1912         mapper_initialize(machine, 71);
1913         break;
1914      case JALECO_JF17:
1915      case JALECO_JF26:
1916      case JALECO_JF28:
1917         mapper_initialize(machine, 72);
1918         break;
1919      case KONAMI_VRC3:
1920         mapper_initialize(machine, 73);
1921         break;
1922      case WAIXING_TYPE_A:
1923         mapper_initialize(machine, 74);
1924         break;
1925      case KONAMI_VRC1:
1926      case JALECO_JF20:
1927      case JALECO_JF22:
1928         mapper_initialize(machine, 75);
1929         break;
1930      case NAMCOT_3446:
1931         mapper_initialize(machine, 76);
1932         break;
1933      case IREM_74161:
1934         mapper_initialize(machine, 77);
1935         break;
1936      case IREM_HOLYDIV:
1937      case JALECO_JF16:
1938         mapper_initialize(machine, 78);
1939         break;
1940      case AVE_NINA03:
1941      case AVE_NINA06:
1942      case AVE_MB91:
1943         mapper_initialize(machine, 79);
1944         break;
1945      case TAITO_X1005:
1946         mapper_initialize(machine, 80);
1947         break;
1948      case TAITO_X1017:
1949         mapper_initialize(machine, 82);
1950         break;
1951      case CONY_STD:
1952         mapper_initialize(machine, 83);
1953         break;
1954      case KONAMI_VRC7:
1955         mapper_initialize(machine, 85);
1956         break;
1957      case JALECO_JF13:
1958         mapper_initialize(machine, 86);
1959         break;
1960      case TAITO_74139:
1961      case KONAMI_74139:
1962      case JALECO_JF05:
1963      case JALECO_JF06:
1964      case JALECO_JF07:
1965      case JALECO_JF08:
1966      case JALECO_JF09:
1967      case JALECO_JF10:
1968         mapper_initialize(machine, 87);
1969         break;
1970      case NAMCOT_3443:
1971      case NAMCOT_3433:
1972         mapper_initialize(machine, 88);
1973         break;
1974      case SUNSOFT_2B:
1975         mapper_initialize(machine, 89);
1976         break;
1977      case UNL_MORTALKOMBAT2:
1978         mapper_initialize(machine, 91);
1979         break;
1980      case JALECO_JF19:
1981      case JALECO_JF21:
1982         mapper_initialize(machine, 92);
1983         break;
1984      case STD_UN1ROM:
1985         mapper_initialize(machine, 94);
1986         break;
1987      case NAMCOT_3425:
1988         mapper_initialize(machine, 95);
1989         break;
1990      case BANDAI_OEKAKIDS:
1991         mapper_initialize(machine, 96);
1992         break;
1993      case CAMERICA_GOLDENFIVE:
1994         mapper_initialize(machine, 104);
1995         break;
1996      case BTL_SMB3:
1997         mapper_initialize(machine, 106);
1998         break;
1999      case MAGICSERIES_MAGICDRAGON:
2000         mapper_initialize(machine, 107);
2001         break;
2002      case NTDEC_ASDER:
2003         mapper_initialize(machine, 112);
2004         break;
2005      case HES_STD:
2006         mapper_initialize(machine, 113);
2007         break;
2008      case SUPERGAME_LIONKING:
2009         mapper_initialize(machine, 114);
2010         break;
2011      case KASING_STD:
2012         mapper_initialize(machine, 115);
2013         break;
2014      case SOMERITEAM_SL12:
2015         mapper_initialize(machine, 116);
2016         break;
2017      case FUTUREMEDIA_STD:
2018         mapper_initialize(machine, 117);
2019         break;
2020      case STD_TKSROM:
2021      case STD_TLSROM:
2022         mapper_initialize(machine, 118);
2023         break;
2024      case STD_TQROM:
2025         mapper_initialize(machine, 119);
2026         break;
2027      case KAY_PANDAPRINCE:
2028         mapper_initialize(machine, 121);
2029         break;
2030      case TXC_22211A:
2031         mapper_initialize(machine, 132);
2032         break;
2033      case SACHEN_SA72008:
2034         mapper_initialize(machine, 133);
2035         break;
2036      case BMC_FAMILY_4646B:
2037         mapper_initialize(machine, 134);
2038         break;
2039      case SACHEN_TCU02:
2040         mapper_initialize(machine, 136);
2041         break;
2042      case SACHEN_8259D:
2043         mapper_initialize(machine, 137);
2044         break;
2045      case SACHEN_8259B:
2046         mapper_initialize(machine, 138);
2047         break;
2048      case SACHEN_8259C:
2049         mapper_initialize(machine, 139);
2050         break;
2051      case JALECO_JF11:
2052      case JALECO_JF12:
2053      case JALECO_JF14:
2054         mapper_initialize(machine, 140);
2055         break;
2056      case SACHEN_8259A:
2057         mapper_initialize(machine, 141);
2058         break;
2059      case SACHEN_TCA01:
2060         mapper_initialize(machine, 143);
2061         break;
2062      case AGCI_50282:
2063         mapper_initialize(machine, 144);
2064         break;
2065      case SACHEN_SA72007:
2066         mapper_initialize(machine, 145);
2067         break;
2068      case SACHEN_SA0161M:
2069         mapper_initialize(machine, 146);
2070         break;
2071      case SACHEN_TCU01:
2072         mapper_initialize(machine, 147);
2073         break;
2074      case SACHEN_SA0037:
2075         mapper_initialize(machine, 148);
2076         break;
2077      case SACHEN_SA0036:
2078         mapper_initialize(machine, 149);
2079         break;
2080      case SACHEN_74LS374:
2081         mapper_initialize(machine, 150);
2082         break;
2083      case BANDAI_FCG1:
2084      case BANDAI_FCG2:
2085      case BANDAI_JUMP2:
2086         mapper_initialize(machine, 153);
2087         break;
2088      case OPENCORP_DAOU306:
2089         mapper_initialize(machine, 156);
2090         break;
2091      case BANDAI_DATACH:
2092         mapper_initialize(machine, 157);
2093         break;
2094      case TENGEN_800037:
2095         mapper_initialize(machine, 158);
2096         break;
2097      case WAIXING_FFV:
2098         mapper_initialize(machine, 164);
2099         break;
2100      case SUBOR_TYPE1:
2101         mapper_initialize(machine, 166);
2102         break;
2103      case SUBOR_TYPE0:
2104         mapper_initialize(machine, 167);
2105         break;
2106      case KAISER_KS7058:
2107         mapper_initialize(machine, 171);
2108         break;
2109      case UNL_XZY:
2110         mapper_initialize(machine, 176);
2111         break;
2112      case HENGEDIANZI_STD:
2113         mapper_initialize(machine, 177);
2114         break;
2115      case WAIXING_SGZLZ:
2116         mapper_initialize(machine, 178);
2117         break;
2118      case HOSENKAN_STD:
2119         mapper_initialize(machine, 182);
2120         break;
2121      case SUNSOFT_1:
2122         mapper_initialize(machine, 184);
2123         break;
2124      case UNL_KINGOFFIGHTERS96:
2125         mapper_initialize(machine, 187);
2126         break;
2127      case BANDAI_KARAOKE:
2128         mapper_initialize(machine, 188);
2129         break;
2130      case TXC_TW:
2131         mapper_initialize(machine, 189);
2132         break;
2133      case WAIXING_TYPE_B:
2134         mapper_initialize(machine, 191);
2135         break;
2136      case WAIXING_TYPE_C:
2137         mapper_initialize(machine, 192);
2138         break;
2139      case NTDEC_FIGHTINGHERO:
2140         mapper_initialize(machine, 193);
2141         break;
2142      case WAIXING_TYPE_D:
2143         mapper_initialize(machine, 194);
2144         break;
2145      case WAIXING_TYPE_E:
2146         mapper_initialize(machine, 195);
2147         break;
2148      case BTL_SUPERBROS11:
2149         mapper_initialize(machine, 196);
2150         break;
2151      case UNL_SUPERFIGHTER3:
2152         mapper_initialize(machine, 197);
2153         break;
2154      case WAIXING_TYPE_F:
2155         mapper_initialize(machine, 198);
2156         break;
2157      case WAIXING_TYPE_G:
2158         mapper_initialize(machine, 199);
2159         break;
2160      case BMC_36IN1:
2161         mapper_initialize(machine, 200);
2162         break;
2163      case BMC_21IN1:
2164         mapper_initialize(machine, 201);
2165         break;
2166      case BMC_150IN1:
2167         mapper_initialize(machine, 202);
2168         break;
2169      case BMC_35IN1:
2170         mapper_initialize(machine, 203);
2171         break;
2172      case BMC_64IN1:
2173         mapper_initialize(machine, 204);
2174         break;
2175      case BMC_15IN1:
2176         mapper_initialize(machine, 205);
2177         break;
2178      case STD_DEROM:
2179      case STD_DE1ROM:
2180      case STD_DRROM:
2181      case TENGEN_800002:
2182      case TENGEN_800004:
2183      case TENGEN_800030:
2184         mapper_initialize(machine, 206);
2185         break;
2186      case GOUDER_37017:
2187         mapper_initialize(machine, 208);
2188         break;
2189      case BMC_SUPERHIK_300IN1:
2190         mapper_initialize(machine, 212);
2191         break;
2192      case BMC_9999999IN1:
2193         mapper_initialize(machine, 213);
2194         break;
2195      case BMC_SUPERGUN_20IN1:
2196         mapper_initialize(machine, 214);
2197         break;
2198      case SUPERGAME_BOOGERMAN:
2199         mapper_initialize(machine, 215);
2200         break;
2201      case RCM_GS2015:
2202         mapper_initialize(machine, 216);
2203         break;
2204      case BMC_GOLDENCARD_6IN1:
2205         mapper_initialize(machine, 217);
2206         break;
2207      case UNL_N625092:
2208         mapper_initialize(machine, 221);
2209         break;
2210      case BTL_DRAGONNINJA:
2211         mapper_initialize(machine, 222);
2212         break;
2213      case BMC_72IN1:
2214         mapper_initialize(machine, 225);
2215         break;
2216      case BMC_76IN1:
2217      case BMC_SUPER_42IN1:
2218         mapper_initialize(machine, 226);
2219         break;
2220      case BMC_1200IN1:
2221         mapper_initialize(machine, 227);
2222         break;
2223      case ACTENT_ACT52:
2224         mapper_initialize(machine, 228);
2225         break;
2226      case BMC_31IN1:
2227         mapper_initialize(machine, 229);
2228         break;
2229      case BMC_22GAMES:
2230         mapper_initialize(machine, 230);
2231         break;
2232      case BMC_20IN1:
2233         mapper_initialize(machine, 231);
2234         break;
2235      case CAMERICA_ALGQ:
2236      case CAMERICA_9096:
2237         mapper_initialize(machine, 232);
2238         break;
2239      case CNE_SHLZ:
2240         mapper_initialize(machine, 240);
2241         break;
2242      case TXC_MXMDHTWO:
2243         mapper_initialize(machine, 241);
2244         break;
2245      case WAIXING_ZS:
2246         mapper_initialize(machine, 242);
2247         break;
2248      case CNE_DECATHLON:
2249         mapper_initialize(machine, 244);
2250         break;
2251      case WAIXING_TYPE_H:
2252         mapper_initialize(machine, 245);
2253         break;
2254      case CNE_PSB:
2255         mapper_initialize(machine, 246);
2256         break;
2257      case WAIXING_SECURITY:
2258         mapper_initialize(machine, 249);
2259         break;
2260      case NITRA_TDA:
2261         mapper_initialize(machine, 250);
2262         break;
2263      case WAIXING_SGZ:
2264         mapper_initialize(machine, 252);
2265         break;
2266      case BMC_110IN1:
2267         mapper_initialize(machine, 255);
2268         break;
2269         
2270      case UNSUPPORTED_BOARD:
2271      default:
2272         /* Mapper not supported */
2273         err = 2;
2274         break;
753      state->mmc_write_low = board->mmc_write_low;
754      state->mmc_write_mid = board->mmc_write_mid;
755      state->mmc_write = board->mmc_write;
756      state->mmc_read_low = board->mmc_read_low;
757      state->mmc_read_mid = NULL;   // in progress
758      state->mmc_read = NULL;   // in progress
759      ppu_latch = board->ppu_latch;
2275760   }
2276
2277   return err;
761   else
762   {
763      logerror("Board %s is not yet supported, defaulting to no mapper.\n", state->board);
764      state->mmc_write_low = NULL;
765      state->mmc_write_mid = NULL;
766      state->mmc_write = NULL;
767      state->mmc_read_low = NULL;
768      state->mmc_read_mid = NULL;   // in progress
769      state->mmc_read = NULL;   // in progress
770      ppu_latch = NULL;
771   }
2278772}
2279773
2280int nes_unif_reset( running_machine *machine, const char *board )
774void unif_mapr_setup( running_machine *machine, const char *board )
2281775{
2282776   nes_state *state = (nes_state *)machine->driver_data;
2283   int err = 0;
2284   const unif *unif_board;
777   const unif *unif_board = nes_unif_lookup(board);
2285778
2286   if (state->chr_chunks == 0)
2287      chr8(machine, 0,CHRRAM);
2288   else
2289      chr8(machine, 0,CHRROM);
2290
2291   /* Set the mapper irq callback */
2292   unif_board = nes_unif_lookup(board);
2293
779   logerror("%s\n", board);
780   
2294781   if (unif_board == NULL)
2295      fatalerror("Unimplemented UNIF Board");
2296
2297   ppu2c0x_set_scanline_callback(state->ppu, unif_board ? unif_board->mmc_scanline : NULL);
2298   ppu2c0x_set_hblank_callback(state->ppu, unif_board ? unif_board->mmc_hblank : NULL);
2299
2300   err = unif_initialize(machine, unif_board->board_idx);
2301
2302   return err;
782      fatalerror("Unknown UNIF board %s.", board);
783   
784   state->mapper = 0;   // this allows us to set up memory handlers without duplicating code (for the moment)
785   state->board = unif_board->board;
786   state->battery = unif_board->nvwram;   // we should implement WRAM banks based on the size of this...
787   state->battery_size = NES_BATTERY_SIZE; // FIXME: we should allow for smaller battery!
788   state->prg_ram = unif_board->wram;   // we should implement WRAM banks based on the size of this...
789   // state->hard_mirroring = unif_board->nt;
790   // state->four_screen_vram = ;
791   
792   // these are just "large enough" values, needed to allocate sufficient amount of memory regions
793   // the real values will be determined by PRG and CHR chunks loading!
794   state->prg_chunks = unif_board->prgrom;
795   state->chr_chunks = unif_board->chrrom;
2303796}
src/mess/machine/nes_mmc.h
r8239r8240
11#ifndef __MMC_H
22#define __MMC_H
33
4typedef struct __mmc_intf
5{
6   write8_space_func mmc_write_low; /* $4100-$5fff write routine */
7   read8_space_func mmc_read_low; /* $4100-$5fff read routine */
8   write8_space_func mmc_write_mid; /* $6000-$7fff write routine */
9   write8_space_func mmc_write; /* $8000-$ffff write routine */
10} mmc_intf;
11
12typedef struct __mmc
13{
14   int iNesMapper; /* iNES Mapper # */
15
16   const char *desc;     /* Mapper description */
17   write8_space_func mmc_write_low; /* $4100-$5fff write routine */
18   read8_space_func mmc_read_low; /* $4100-$5fff read routine */
19   write8_space_func mmc_write_mid; /* $6000-$7fff write routine */
20   write8_space_func mmc_write; /* $8000-$ffff write routine */
21   void (*ppu_latch)(running_device *device, offs_t offset);
22   ppu2c0x_scanline_cb      mmc_scanline;
23   ppu2c0x_hblank_cb      mmc_hblank;
24} mmc;
25
26const mmc *nes_mapper_lookup(int mapper);
27
28typedef struct __unif
29{
30   const char *board; /* UNIF board */
31
32   write8_space_func mmc_write_low; /* $4100-$5fff write routine */
33   read8_space_func mmc_read_low; /* $4100-$5fff read routine */
34   write8_space_func mmc_write_mid; /* $6000-$7fff write routine */
35   write8_space_func mmc_write; /* $8000-$ffff write routine */
36   void (*ppu_latch)(running_device *device, offs_t offset);
37   ppu2c0x_scanline_cb      mmc_scanline;
38   ppu2c0x_hblank_cb      mmc_hblank;
39
40   int prgrom;
41   int chrrom;
42   int nvwram;
43   int wram;
44   int chrram;
45   int nt;
46   int board_idx;
47} unif;
48
49const unif *nes_unif_lookup(const char *board);
50
514//extern int MMC1_extended; /* 0 = normal MMC1 cart, 1 = 512k MMC1, 2 = 1024k MMC1 */
525
536#define MMC5_VRAM
547
55void nes_mapper_init( const mmc_intf *intf );
56int nes_mapper_reset( running_machine *machine, int mapperNum );
57int nes_unif_reset( running_machine *machine, const char *board );
8int nes_mapper_reset(running_machine *machine);
9int nes_unif_reset(running_machine *machine);
5810
11void mapper_handlers_setup(running_machine *machine);
12void unif_handlers_setup(running_machine *machine);
13void unif_mapr_setup(running_machine *machine, const char *board);
14
5915WRITE8_HANDLER( nes_low_mapper_w );
6016WRITE8_HANDLER( nes_mid_mapper_w );
6117WRITE8_HANDLER( nes_mapper_w );
src/mess/machine/nes_ines.c
r8239r8240
3737
3838static void MMC1_set_prg( running_machine *machine )
3939{
40   nes_state *state = (nes_state *)machine->driver_data;
4041   UINT8 prg_mode, prg_offset;
4142
42   prg_mode = MMC1_regs[0] & 0x0c;
43   prg_mode = state->MMC1_regs[0] & 0x0c;
4344   /* prg_mode&0x8 determines bank size: 32k (if 0) or 16k (if 1)? when in 16k mode,
4445       prg_mode&0x4 determines which half of the PRG space we can swap: if it is 4,
4546       MMC1_regs[3] sets banks at 0x8000; if it is 0, MMC1_regs[3] sets banks at 0xc000. */
4647
47   prg_offset = MMC1_regs[1] & 0x10;
48   prg_offset = state->MMC1_regs[1] & 0x10;
4849   /* In principle, MMC1_regs[2]&0x10 might affect "extended" banks as well, when chr_mode=1.
4950       However, quoting Disch's docs: When in 4k CHR mode, 0x10 in both $A000 and $C000 *must* be
5051       set to the same value, or else pages will constantly be swapped as graphics render!
r8239r8240
5455   {
5556   case 0x00:
5657   case 0x04:
57      prg32(machine, prg_offset + MMC1_regs[3]);
58      prg32(machine, prg_offset + state->MMC1_regs[3]);
5859      break;
5960   case 0x08:
6061      prg16_89ab(machine, prg_offset + 0);
61      prg16_cdef(machine, prg_offset + MMC1_regs[3]);
62      prg16_cdef(machine, prg_offset + state->MMC1_regs[3]);
6263      break;
6364   case 0x0c:
64      prg16_89ab(machine, prg_offset + MMC1_regs[3]);
65      prg16_89ab(machine, prg_offset + state->MMC1_regs[3]);
6566      prg16_cdef(machine, prg_offset + 0x0f);
6667      break;
6768   }
r8239r8240
7071static void MMC1_set_chr( running_machine *machine )
7172{
7273   nes_state *state = (nes_state *)machine->driver_data;
73   UINT8 chr_mode = MMC1_regs[0] & 0x10;
74   UINT8 chr_mode = state->MMC1_regs[0] & 0x10;
7475
7576   if (chr_mode)
7677   {
77      chr4_0(machine, MMC1_regs[1] & 0x1f, state->mmc_chr_source);
78      chr4_4(machine, MMC1_regs[2] & 0x1f, state->mmc_chr_source);
78      chr4_0(machine, state->MMC1_regs[1] & 0x1f, state->mmc_chr_source);
79      chr4_4(machine, state->MMC1_regs[2] & 0x1f, state->mmc_chr_source);
7980   }
8081   else
81      chr8(machine, (MMC1_regs[1] & 0x1f) >> 1, state->mmc_chr_source);
82      chr8(machine, (state->MMC1_regs[1] & 0x1f) >> 1, state->mmc_chr_source);
8283}
8384
8485static WRITE8_HANDLER( mapper1_w )
r8239r8240
9899      state->mmc_cmd1 = 0;
99100
100101      /* Set reg at 0x8000 to size 16k and lower half swap - needed for Robocop 3, Dynowars */
101      MMC1_regs[0] |= 0x0c;
102      state->MMC1_regs[0] |= 0x0c;
102103      MMC1_set_prg(space->machine);
103104
104105      LOG_MMC(("=== MMC1 regs reset to default\n"));
r8239r8240
118119      switch (offset & 0x6000)   /* Which reg shall we write to? */
119120      {
120121      case 0x0000:
121         MMC1_regs[0] = state->mmc_cmd1;
122         state->MMC1_regs[0] = state->mmc_cmd1;
122123
123         switch (MMC1_regs[0] & 0x03)
124         switch (state->MMC1_regs[0] & 0x03)
124125         {
125126            case 0: set_nt_mirroring(space->machine, PPU_MIRROR_LOW); break;
126127            case 1: set_nt_mirroring(space->machine, PPU_MIRROR_HIGH); break;
r8239r8240
131132         MMC1_set_prg(space->machine);
132133         break;
133134      case 0x2000:
134         MMC1_regs[1] = state->mmc_cmd1;
135         state->MMC1_regs[1] = state->mmc_cmd1;
135136         MMC1_set_chr(space->machine);
136137         MMC1_set_prg(space->machine);
137138         break;
138139      case 0x4000:
139         MMC1_regs[2] = state->mmc_cmd1;
140         state->MMC1_regs[2] = state->mmc_cmd1;
140141         MMC1_set_chr(space->machine);
141142         break;
142143      case 0x6000:
143         MMC1_regs[3] = state->mmc_cmd1;
144         state->MMC1_regs[3] = state->mmc_cmd1;
144145         MMC1_set_prg(space->machine);
145146         break;
146147      }
r8239r8240
428429static WRITE8_HANDLER( mapper5_l_w )
429430{
430431   nes_state *state = (nes_state *)space->machine->driver_data;
431//  static int vrom_next[4];
432   static int vrom_page_a;
433   static int vrom_page_b;
434432
435433//  LOG_MMC(("Mapper 5 write, offset: %04x, data: %02x\n", offset + 0x4100, data));
436434   /* Send $5000-$5015 to the sound chip */
r8239r8240
443441   /* $5c00 - $5fff: extended videoram attributes */
444442   if ((offset >= 0x1b00) && (offset <= 0x1eff))
445443   {
446      if (MMC5_vram_protect == 0x03)
444      if (state->MMC5_vram_protect == 0x03)
447445         state->mmc5_vram[offset - 0x1b00] = data;
448446      return;
449447   }
r8239r8240
451449   switch (offset)
452450   {
453451      case 0x1000: /* $5100 */
454         MMC5_rom_bank_mode = data & 0x03;
452         state->MMC5_rom_bank_mode = data & 0x03;
455453         LOG_MMC(("MMC5 rom bank mode: %02x\n", data));
456454         break;
457455
458456      case 0x1001: /* $5101 */
459         MMC5_vrom_bank_mode = data & 0x03;
457         state->MMC5_vrom_bank_mode = data & 0x03;
460458         LOG_MMC(("MMC5 vrom bank mode: %02x\n", data));
461459         break;
462460
463461      case 0x1002: /* $5102 */
464462         if (data == 0x02)
465            MMC5_vram_protect |= 1;
463            state->MMC5_vram_protect |= 1;
466464         else
467            MMC5_vram_protect = 0;
465            state->MMC5_vram_protect = 0;
468466         LOG_MMC(("MMC5 vram protect 1: %02x\n", data));
469467         break;
470468      case 0x1003: /* 5103 */
471469         if (data == 0x01)
472            MMC5_vram_protect |= 2;
470            state->MMC5_vram_protect |= 2;
473471         else
474            MMC5_vram_protect = 0;
472            state->MMC5_vram_protect = 0;
475473         LOG_MMC(("MMC5 vram protect 2: %02x\n", data));
476474         break;
477475
r8239r8240
511509         break;
512510
513511      case 0x1014: /* $5114 */
514         LOG_MMC(("MMC5 $5114 bank select: %02x (mode: %d)\n", data, MMC5_rom_bank_mode));
515         switch (MMC5_rom_bank_mode)
512         LOG_MMC(("MMC5 $5114 bank select: %02x (mode: %d)\n", data, state->MMC5_rom_bank_mode));
513         switch (state->MMC5_rom_bank_mode)
516514         {
517515            case 0x03:
518516               /* 8k switch */
r8239r8240
533531         }
534532         break;
535533      case 0x1015: /* $5115 */
536         LOG_MMC(("MMC5 $5115 bank select: %02x (mode: %d)\n", data, MMC5_rom_bank_mode));
537         switch (MMC5_rom_bank_mode)
534         LOG_MMC(("MMC5 $5115 bank select: %02x (mode: %d)\n", data, state->MMC5_rom_bank_mode));
535         switch (state->MMC5_rom_bank_mode)
538536         {
539537            case 0x01:
540538            case 0x02:
r8239r8240
573571         }
574572         break;
575573      case 0x1016: /* $5116 */
576         LOG_MMC(("MMC5 $5116 bank select: %02x (mode: %d)\n", data, MMC5_rom_bank_mode));
577         switch (MMC5_rom_bank_mode)
574         LOG_MMC(("MMC5 $5116 bank select: %02x (mode: %d)\n", data, state->MMC5_rom_bank_mode));
575         switch (state->MMC5_rom_bank_mode)
578576         {
579577            case 0x02:
580578            case 0x03:
r8239r8240
596594         }
597595         break;
598596      case 0x1017: /* $5117 */
599         LOG_MMC(("MMC5 $5117 bank select: %02x (mode: %d)\n", data, MMC5_rom_bank_mode));
600         switch (MMC5_rom_bank_mode)
597         LOG_MMC(("MMC5 $5117 bank select: %02x (mode: %d)\n", data, state->MMC5_rom_bank_mode));
598         switch (state->MMC5_rom_bank_mode)
601599         {
602600            case 0x00:
603601               /* 32k switch - ROM only */
r8239r8240
618616         }
619617         break;
620618      case 0x1020: /* $5120 */
621         LOG_MMC(("MMC5 $5120 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
622         switch (MMC5_vrom_bank_mode)
619         LOG_MMC(("MMC5 $5120 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
620         switch (state->MMC5_vrom_bank_mode)
623621         {
624622            case 0x03:
625623               /* 1k switch */
r8239r8240
634632         }
635633         break;
636634      case 0x1021: /* $5121 */
637         LOG_MMC(("MMC5 $5121 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
638         switch (MMC5_vrom_bank_mode)
635         LOG_MMC(("MMC5 $5121 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
636         switch (state->MMC5_vrom_bank_mode)
639637         {
640638            case 0x02:
641639               /* 2k switch */
r8239r8240
654652         }
655653         break;
656654      case 0x1022: /* $5122 */
657         LOG_MMC(("MMC5 $5122 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
658         switch (MMC5_vrom_bank_mode)
655         LOG_MMC(("MMC5 $5122 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
656         switch (state->MMC5_vrom_bank_mode)
659657         {
660658            case 0x03:
661659               /* 1k switch */
r8239r8240
670668         }
671669         break;
672670      case 0x1023: /* $5123 */
673         LOG_MMC(("MMC5 $5123 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
674         switch (MMC5_vrom_bank_mode)
671         LOG_MMC(("MMC5 $5123 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
672         switch (state->MMC5_vrom_bank_mode)
675673         {
676674            case 0x01:
677675               chr4_0(space->machine, data, CHRROM);
r8239r8240
693691         }
694692         break;
695693      case 0x1024: /* $5124 */
696         LOG_MMC(("MMC5 $5124 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
697         switch (MMC5_vrom_bank_mode)
694         LOG_MMC(("MMC5 $5124 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
695         switch (state->MMC5_vrom_bank_mode)
698696         {
699697            case 0x03:
700698               /* 1k switch */
r8239r8240
709707         }
710708         break;
711709      case 0x1025: /* $5125 */
712         LOG_MMC(("MMC5 $5125 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
713         switch (MMC5_vrom_bank_mode)
710         LOG_MMC(("MMC5 $5125 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
711         switch (state->MMC5_vrom_bank_mode)
714712         {
715713            case 0x02:
716714               /* 2k switch */
r8239r8240
729727         }
730728         break;
731729      case 0x1026: /* $5126 */
732         LOG_MMC(("MMC5 $5126 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
733         switch (MMC5_vrom_bank_mode)
730         LOG_MMC(("MMC5 $5126 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
731         switch (state->MMC5_vrom_bank_mode)
734732         {
735733            case 0x03:
736734               /* 1k switch */
r8239r8240
745743         }
746744         break;
747745      case 0x1027: /* $5127 */
748         LOG_MMC(("MMC5 $5127 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
749         switch (MMC5_vrom_bank_mode)
746         LOG_MMC(("MMC5 $5127 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
747         switch (state->MMC5_vrom_bank_mode)
750748         {
751749            case 0x00:
752750               /* 8k switch */
r8239r8240
773771         }
774772         break;
775773      case 0x1028: /* $5128 */
776         LOG_MMC(("MMC5 $5128 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
777         switch (MMC5_vrom_bank_mode)
774         LOG_MMC(("MMC5 $5128 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
775         switch (state->MMC5_vrom_bank_mode)
778776         {
779777            case 0x03:
780778               /* 1k switch */
r8239r8240
784782//                  nes_vram[0] = data * 64;
785783               chr1_4(space->machine, state->MMC5_vrom_bank[8], CHRROM);
786784//                  mapper5_sync_vrom(1);
787               if (!vrom_page_b)
785               if (!state->vrom_page_b)
788786               {
789                  vrom_page_a ^= 0x01;
790                  vrom_page_b = 1;
787                  state->vrom_page_a ^= 0x01;
788                  state->vrom_page_b = 1;
791789               }
792790               break;
793791         }
794792         break;
795793      case 0x1029: /* $5129 */
796         LOG_MMC(("MMC5 $5129 vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
797         switch (MMC5_vrom_bank_mode)
794         LOG_MMC(("MMC5 $5129 vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
795         switch (state->MMC5_vrom_bank_mode)
798796         {
799797            case 0x02:
800798               /* 2k switch */
r8239r8240
809807//                  nes_vram[1] = data * 64;
810808               chr1_5(space->machine, state->MMC5_vrom_bank[9], CHRROM);
811809//                  mapper5_sync_vrom(1);
812               if (!vrom_page_b)
810               if (!state->vrom_page_b)
813811               {
814                  vrom_page_a ^= 0x01;
815                  vrom_page_b = 1;
812                  state->vrom_page_a ^= 0x01;
813                  state->vrom_page_b = 1;
816814               }
817815               break;
818816         }
819817         break;
820818      case 0x102a: /* $512a */
821         LOG_MMC(("MMC5 $512a vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
822         switch (MMC5_vrom_bank_mode)
819         LOG_MMC(("MMC5 $512a vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
820         switch (state->MMC5_vrom_bank_mode)
823821         {
824822            case 0x03:
825823               /* 1k switch */
r8239r8240
829827//                  nes_vram[2] = data * 64;
830828               chr1_6(space->machine, state->MMC5_vrom_bank[10], CHRROM);
831829//                  mapper5_sync_vrom(1);
832               if (!vrom_page_b)
830               if (!state->vrom_page_b)
833831               {
834                  vrom_page_a ^= 0x01;
835                  vrom_page_b = 1;
832                  state->vrom_page_a ^= 0x01;
833                  state->vrom_page_b = 1;
836834               }
837835               break;
838836         }
839837         break;
840838      case 0x102b: /* $512b */
841         LOG_MMC(("MMC5 $512b vrom select: %02x (mode: %d)\n", data, MMC5_vrom_bank_mode));
842         switch (MMC5_vrom_bank_mode)
839         LOG_MMC(("MMC5 $512b vrom select: %02x (mode: %d)\n", data, state->MMC5_vrom_bank_mode));
840         switch (state->MMC5_vrom_bank_mode)
843841         {
844842            case 0x00:
845843               /* 8k switch */
r8239r8240
865863//                  nes_vram[3] = data * 64;
866864               chr1_7(space->machine, state->MMC5_vrom_bank[11], CHRROM);
867865//                  mapper5_sync_vrom(1);
868               if (!vrom_page_b)
866               if (!state->vrom_page_b)
869867               {
870                  vrom_page_a ^= 0x01;
871                  vrom_page_b = 1;
868                  state->vrom_page_a ^= 0x01;
869                  state->vrom_page_b = 1;
872870               }
873871               break;
874872         }
r8239r8240
883881         }
884882         break;
885883
884         
885      case 0x1100: /* $5200 */
886         state->mmc5_split_scr = data;
887         // in EX2 and EX3 modes, no split screen
888         if (state->mmc5_vram_control & 0x02)
889            state->mmc5_split_scr &= 0x7f;
890         break;
891
886892      case 0x1103: /* $5203 */
887893         state->IRQ_count = data;
888         MMC5_scanline = data;
894         state->MMC5_scanline = data;
889895         LOG_MMC(("MMC5 irq scanline: %d\n", state->IRQ_count));
890896         break;
891897      case 0x1104: /* $5204 */
r8239r8240
10431049   nes_state *state = (nes_state *)device->machine->driver_data;
10441050   if ((offset & 0x3ff0) == 0x0fd0)
10451051   {
1046      LOG_MMC(("mapper9 vrom latch switch (bank 0 low): %02x\n", MMC2_regs[0]));
1052      LOG_MMC(("mapper9 vrom latch switch (bank 0 low): %02x\n", state->MMC2_regs[0]));
10471053      state->mmc_cmd1 = 0xfd;
1048      chr4_0(device->machine, MMC2_regs[0], CHRROM);
1054      chr4_0(device->machine, state->MMC2_regs[0], CHRROM);
10491055   }
10501056   else if ((offset & 0x3ff0) == 0x0fe0)
10511057   {
1052      LOG_MMC(("mapper9 vrom latch switch (bank 0 high): %02x\n", MMC2_regs[1]));
1058      LOG_MMC(("mapper9 vrom latch switch (bank 0 high): %02x\n", state->MMC2_regs[1]));
10531059      state->mmc_cmd1 = 0xfe;
1054      chr4_0(device->machine, MMC2_regs[1], CHRROM);
1060      chr4_0(device->machine, state->MMC2_regs[1], CHRROM);
10551061   }
10561062   else if ((offset & 0x3ff0) == 0x1fd0)
10571063   {
1058      LOG_MMC(("mapper9 vrom latch switch (bank 1 low): %02x\n", MMC2_regs[2]));
1064      LOG_MMC(("mapper9 vrom latch switch (bank 1 low): %02x\n", state->MMC2_regs[2]));
10591065      state->mmc_cmd2 = 0xfd;
1060      chr4_4(device->machine, MMC2_regs[2], CHRROM);
1066      chr4_4(device->machine, state->MMC2_regs[2], CHRROM);
10611067   }
10621068   else if ((offset & 0x3ff0) == 0x1fe0)
10631069   {
1064      LOG_MMC(("mapper9 vrom latch switch (bank 0 high): %02x\n", MMC2_regs[3]));
1070      LOG_MMC(("mapper9 vrom latch switch (bank 0 high): %02x\n", state->MMC2_regs[3]));
10651071      state->mmc_cmd2 = 0xfe;
1066      chr4_4(device->machine, MMC2_regs[3], CHRROM);
1072      chr4_4(device->machine, state->MMC2_regs[3], CHRROM);
10671073   }
10681074}
10691075
r8239r8240
10771083         prg8_89(space->machine, data);
10781084         break;
10791085      case 0x3000:
1080         MMC2_regs[0] = data;
1086         state->MMC2_regs[0] = data;
10811087         if (state->mmc_cmd1 == 0xfd)
1082            chr4_0(space->machine, MMC2_regs[0], CHRROM);
1088            chr4_0(space->machine, state->MMC2_regs[0], CHRROM);
10831089         break;
10841090      case 0x4000:
1085         MMC2_regs[1] = data;
1091         state->MMC2_regs[1] = data;
10861092         if (state->mmc_cmd1 == 0xfe)
1087            chr4_0(space->machine, MMC2_regs[1], CHRROM);
1093            chr4_0(space->machine, state->MMC2_regs[1], CHRROM);
10881094         break;
10891095      case 0x5000:
1090         MMC2_regs[2] = data;
1096         state->MMC2_regs[2] = data;
10911097         if (state->mmc_cmd2 == 0xfd)
1092            chr4_4(space->machine, MMC2_regs[2], CHRROM);
1098            chr4_4(space->machine, state->MMC2_regs[2], CHRROM);
10931099         break;
10941100      case 0x6000:
1095         MMC2_regs[3] = data;
1101         state->MMC2_regs[3] = data;
10961102         if (state->mmc_cmd2 == 0xfe)
1097            chr4_4(space->machine, MMC2_regs[3], CHRROM);
1103            chr4_4(space->machine, state->MMC2_regs[3], CHRROM);
10981104         break;
10991105      case 0x7000:
11001106         set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
r8239r8240
11661172
11671173static WRITE8_HANDLER( mapper12_l_w )
11681174{
1175   nes_state *state = (nes_state *)space->machine->driver_data;
1176
11691177   LOG_MMC(("mapper12_l_w, offset: %04x, data: %02x\n", offset, data));
11701178
1171   mapper12_reg = data;
1179   state->mapper12_reg = data;
11721180}
11731181
11741182/* we would need to use this read handler in 0x6000-0x7fff as well */
r8239r8240
11831191   nes_state *state = (nes_state *)machine->driver_data;
11841192   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
11851193
1186   chr1_x(machine, chr_page ^ 0, ((mapper12_reg & 0x01) << 8) | (state->mmc_vrom_bank[0] & ~0x01), CHRROM);
1187   chr1_x(machine, chr_page ^ 1, ((mapper12_reg & 0x01) << 8) | (state->mmc_vrom_bank[0] |  0x01), CHRROM);
1188   chr1_x(machine, chr_page ^ 2, ((mapper12_reg & 0x01) << 8) | (state->mmc_vrom_bank[1] & ~0x01), CHRROM);
1189   chr1_x(machine, chr_page ^ 3, ((mapper12_reg & 0x01) << 8) | (state->mmc_vrom_bank[1] |  0x01), CHRROM);
1190   chr1_x(machine, chr_page ^ 4, ((mapper12_reg & 0x10) << 4) | state->mmc_vrom_bank[2], CHRROM);
1191   chr1_x(machine, chr_page ^ 5, ((mapper12_reg & 0x10) << 4) | state->mmc_vrom_bank[3], CHRROM);
1192   chr1_x(machine, chr_page ^ 6, ((mapper12_reg & 0x10) << 4) | state->mmc_vrom_bank[4], CHRROM);
1193   chr1_x(machine, chr_page ^ 7, ((mapper12_reg & 0x10) << 4) | state->mmc_vrom_bank[5], CHRROM);
1194   chr1_x(machine, chr_page ^ 0, ((state->mapper12_reg & 0x01) << 8) | (state->mmc_vrom_bank[0] & ~0x01), CHRROM);
1195   chr1_x(machine, chr_page ^ 1, ((state->mapper12_reg & 0x01) << 8) | (state->mmc_vrom_bank[0] |  0x01), CHRROM);
1196   chr1_x(machine, chr_page ^ 2, ((state->mapper12_reg & 0x01) << 8) | (state->mmc_vrom_bank[1] & ~0x01), CHRROM);
1197   chr1_x(machine, chr_page ^ 3, ((state->mapper12_reg & 0x01) << 8) | (state->mmc_vrom_bank[1] |  0x01), CHRROM);
1198   chr1_x(machine, chr_page ^ 4, ((state->mapper12_reg & 0x10) << 4) | state->mmc_vrom_bank[2], CHRROM);
1199   chr1_x(machine, chr_page ^ 5, ((state->mapper12_reg & 0x10) << 4) | state->mmc_vrom_bank[3], CHRROM);
1200   chr1_x(machine, chr_page ^ 6, ((state->mapper12_reg & 0x10) << 4) | state->mmc_vrom_bank[4], CHRROM);
1201   chr1_x(machine, chr_page ^ 7, ((state->mapper12_reg & 0x10) << 4) | state->mmc_vrom_bank[5], CHRROM);
11941202}
11951203
11961204static WRITE8_HANDLER( mapper12_w )
r8239r8240
12711279{
12721280   nes_state *state = (nes_state *)machine->driver_data;
12731281
1274   if (map14_reg[0] & 0x02)
1282   if (state->map14_reg[0] & 0x02)
12751283   {
12761284      mapper4_set_prg(machine, prg_base, prg_mask);
12771285   }
r8239r8240
12931301   UINT8 chr_base2[8];
12941302   int i;
12951303
1296   if (map14_reg[0] & 0x02)
1304   if (state->map14_reg[0] & 0x02)
12971305   {
12981306      for(i = 0; i < 8; i++)
12991307      {
13001308         bank[i] = state->mmc_vrom_bank[i];
1301         chr_base2[i] = chr_base | ((map14_reg[0] << conv_table[i]) & 0x100);
1309         chr_base2[i] = chr_base | ((state->map14_reg[0] << conv_table[i]) & 0x100);
13021310      }
13031311   }
13041312   else
r8239r8240
13281336
13291337   if (offset == 0x2131)
13301338   {
1331      map14_reg[0] = data;
1339      state->map14_reg[0] = data;
13321340      mapper14_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
13331341      mapper14_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13341342
1335      if (!(map14_reg[0] & 0x02))
1336         set_nt_mirroring(space->machine, BIT(map14_reg[1], 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
1343      if (!(state->map14_reg[0] & 0x02))
1344         set_nt_mirroring(space->machine, BIT(state->map14_reg[1], 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
13371345   }
13381346
1339   if (map14_reg[0] & 0x02)
1347   if (state->map14_reg[0] & 0x02)
13401348   {
13411349      switch (offset & 0x6001)
13421350      {
r8239r8240
13751383         break;
13761384
13771385      case 0x2000:
1378         set_nt_mirroring(space->machine, BIT(map14_reg[1], 0) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
1386         set_nt_mirroring(space->machine, BIT(state->map14_reg[1], 0) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
13791387         break;
13801388
13811389      default:
r8239r8240
14021410         break;
14031411
14041412      case 0x1000:
1405         map14_reg[1] = data;
1406         set_nt_mirroring(space->machine, BIT(map14_reg[1], 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
1413         state->map14_reg[1] = data;
1414         set_nt_mirroring(space->machine, BIT(state->map14_reg[1], 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
14071415         break;
14081416      }
14091417   }
r8239r8240
32343242
32353243   /* This bit is the "register lock". Once register are locked, writes go to WRAM
32363244        and there is no way to unlock them (except by resetting the machine) */
3237   if (mapper45_reg[3] & 0x40)
3245   if (state->mapper45_reg[3] & 0x40)
32383246      state->wram[offset] = data;
32393247   else
32403248   {
3241      mapper45_reg[state->mmc_count] = data;
3249      state->mapper45_reg[state->mmc_count] = data;
32423250      state->mmc_count = (state->mmc_count + 1) & 0x03;
32433251
32443252      if (!state->mmc_count)
32453253      {
3246         LOG_MMC(("mapper45_m_w, command completed %02x %02x %02x %02x\n", mapper45_reg[3],
3247            mapper45_reg[2], mapper45_reg[1], mapper45_reg[0]));
3254         LOG_MMC(("mapper45_m_w, command completed %02x %02x %02x %02x\n", state->mapper45_reg[3],
3255            state->mapper45_reg[2], state->mapper45_reg[1], state->mapper45_reg[0]));
32483256
3249         state->mmc_prg_base = mapper45_reg[1];
3250         state->mmc_prg_mask = 0x3f ^ (mapper45_reg[3] & 0x3f);
3251         state->mmc_chr_base = ((mapper45_reg[2] & 0xf0) << 4) + mapper45_reg[0];
3252         state->mmc_chr_mask = 0xff >> (~mapper45_reg[2] & 0x0f);
3257         state->mmc_prg_base = state->mapper45_reg[1];
3258         state->mmc_prg_mask = 0x3f ^ (state->mapper45_reg[3] & 0x3f);
3259         state->mmc_chr_base = ((state->mapper45_reg[2] & 0xf0) << 4) + state->mapper45_reg[0];
3260         state->mmc_chr_mask = 0xff >> (~state->mapper45_reg[2] & 0x0f);
32533261         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
32543262         mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
32553263      }
r8239r8240
34903498
34913499static void mapper51_set_banks( running_machine *machine )
34923500{
3493   set_nt_mirroring(machine, (mapper51_reg[0] == 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
3501   nes_state *state = (nes_state *)machine->driver_data;
3502   set_nt_mirroring(machine, (state->mapper51_reg[0] == 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
34943503
3495   if (mapper51_reg[0] & 0x01)
3504   if (state->mapper51_reg[0] & 0x01)
34963505   {
3497      prg32(machine, mapper51_reg[1]);
3506      prg32(machine, state->mapper51_reg[1]);
34983507   }
34993508   else
35003509   {
3501      prg16_89ab(machine, (mapper51_reg[1] << 1) | (mapper51_reg[0] >> 1));
3502      prg16_cdef(machine, (mapper51_reg[1] << 1) | 0x07);
3510      prg16_89ab(machine, (state->mapper51_reg[1] << 1) | (state->mapper51_reg[0] >> 1));
3511      prg16_cdef(machine, (state->mapper51_reg[1] << 1) | 0x07);
35033512   }
35043513}
35053514
35063515static WRITE8_HANDLER( mapper51_m_w )
35073516{
3517   nes_state *state = (nes_state *)space->machine->driver_data;
3518
35083519   LOG_MMC(("mapper51_m_w, offset: %04x, data: %02x\n", offset, data));
35093520
3510   mapper51_reg[0] = ((data >> 1) & 0x01) | ((data >> 3) & 0x02);
3521   state->mapper51_reg[0] = ((data >> 1) & 0x01) | ((data >> 3) & 0x02);
35113522   mapper51_set_banks(space->machine);
35123523}
35133524
35143525static WRITE8_HANDLER( mapper51_w )
35153526{
3527   nes_state *state = (nes_state *)space->machine->driver_data;
3528
35163529   LOG_MMC(("mapper51_w, offset: %04x, data: %02x\n", offset, data));
35173530
35183531   switch (offset & 0x6000)
35193532   {
35203533   case 0x4000:   // here we also update reg[0] upper bit
3521      mapper51_reg[0] = (mapper51_reg[0] & 0x01) | ((data >> 3) & 0x02);
3534      state->mapper51_reg[0] = (state->mapper51_reg[0] & 0x01) | ((data >> 3) & 0x02);
35223535   case 0x0000:
35233536   case 0x2000:
35243537   case 0x6000:
3525      mapper51_reg[1] = data & 0x0f;
3538      state->mapper51_reg[1] = data & 0x0f;
35263539      mapper51_set_banks(space->machine);
35273540      break;
35283541   }
r8239r8240
35503563   /* mid writes only work when WRAM is enabled. not sure if I should
35513564       change the condition to state->mmc_cmd2==0x80 (i.e. what is the effect of
35523565       the read-only bit?) and it only can happen once! */
3553   if ((state->mmc_cmd2 & 0x80) && !map52_reg_written)
3566   if ((state->mmc_cmd2 & 0x80) && !state->map52_reg_written)
35543567   {
35553568      map52_helper1 = (data & 0x08);
35563569      map52_helper2 = (data & 0x40);
r8239r8240
35623575      mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
35633576      mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
35643577
3565      map52_reg_written = 1;
3578      state->map52_reg_written = 1;
35663579   }
35673580   else
35683581      state->wram[offset] = data;
r8239r8240
41994212
42004213static WRITE8_HANDLER( mapper68_w )
42014214{
4215   nes_state *state = (nes_state *)space->machine->driver_data;
4216
42024217   LOG_MMC(("mapper68_w, offset %04x, data: %02x\n", offset, data));
42034218
42044219   switch (offset & 0x7000)
r8239r8240
42164231         chr2_6(space->machine, data, CHRROM);
42174232         break;
42184233      case 0x4000:
4219         m0 = data & 0x7f;
4220         mapper68_mirror(space->machine, m68_mirror, m0, m1);
4234         state->m0 = data & 0x7f;
4235         mapper68_mirror(space->machine, state->m68_mirror, state->m0, state->m1);
42214236         break;
42224237      case 0x5000:
4223         m1 = data & 0x7f;
4224         mapper68_mirror(space->machine, m68_mirror, m0, m1);
4238         state->m1 = data & 0x7f;
4239         mapper68_mirror(space->machine, state->m68_mirror, state->m0, state->m1);
42254240         break;
42264241      case 0x6000:
4227         m68_mirror = data & 0x13;
4228         mapper68_mirror(space->machine, m68_mirror, m0, m1);
4242         state->m68_mirror = data & 0x13;
4243         mapper68_mirror(space->machine, state->m68_mirror, state->m0, state->m1);
42294244         break;
42304245      case 0x7000:
42314246         prg16_89ab(space->machine, data);
r8239r8240
48944909
48954910static WRITE8_HANDLER( mapper83_l_w )
48964911{
4897   mapper83_low_reg[offset & 0x03] = data;
4912   nes_state *state = (nes_state *)space->machine->driver_data;
4913   state->mapper83_low_reg[offset & 0x03] = data;
48984914}
48994915
49004916static READ8_HANDLER( mapper83_l_r )
49014917{
4902   return mapper83_low_reg[offset & 0x03];
4918   nes_state *state = (nes_state *)space->machine->driver_data;
4919   return state->mapper83_low_reg[offset & 0x03];
49034920}
49044921
49054922static void mapper83_set_prg( running_machine *machine )
49064923{
4907   prg16_89ab(machine, mapper83_reg[8] & 0x3f);
4908   prg16_cdef(machine, (mapper83_reg[8] & 0x30) | 0x0f);
4924   nes_state *state = (nes_state *)machine->driver_data;
4925   prg16_89ab(machine, state->mapper83_reg[8] & 0x3f);
4926   prg16_cdef(machine, (state->mapper83_reg[8] & 0x30) | 0x0f);
49094927}
49104928
49114929static void mapper83_set_chr( running_machine *machine )
49124930{
4913   chr1_0(machine, mapper83_reg[0] | ((mapper83_reg[8] & 0x30) << 4), CHRROM);
4914   chr1_1(machine, mapper83_reg[1] | ((mapper83_reg[8] & 0x30) << 4), CHRROM);
4915   chr1_2(machine, mapper83_reg[2] | ((mapper83_reg[8] & 0x30) << 4), CHRROM);
4916   chr1_3(machine, mapper83_reg[3] | ((mapper83_reg[8] & 0x30) << 4), CHRROM);
4917   chr1_4(machine, mapper83_reg[4] | ((mapper83_reg[8] & 0x30) << 4), CHRROM);
4918   chr1_5(machine, mapper83_reg[5] | ((mapper83_reg[8] & 0x30) << 4), CHRROM);
4919   chr1_6(machine, mapper83_reg[6] | ((mapper83_reg[8] & 0x30) << 4), CHRROM);
4920   chr1_7(machine, mapper83_reg[7] | ((mapper83_reg[8] & 0x30) << 4), CHRROM);
4931   nes_state *state = (nes_state *)machine->driver_data;
4932   chr1_0(machine, state->mapper83_reg[0] | ((state->mapper83_reg[8] & 0x30) << 4), CHRROM);
4933   chr1_1(machine, state->mapper83_reg[1] | ((state->mapper83_reg[8] & 0x30) << 4), CHRROM);
4934   chr1_2(machine, state->mapper83_reg[2] | ((state->mapper83_reg[8] & 0x30) << 4), CHRROM);
4935   chr1_3(machine, state->mapper83_reg[3] | ((state->mapper83_reg[8] & 0x30) << 4), CHRROM);
4936   chr1_4(machine, state->mapper83_reg[4] | ((state->mapper83_reg[8] & 0x30) << 4), CHRROM);
4937   chr1_5(machine, state->mapper83_reg[5] | ((state->mapper83_reg[8] & 0x30) << 4), CHRROM);
4938   chr1_6(machine, state->mapper83_reg[6] | ((state->mapper83_reg[8] & 0x30) << 4), CHRROM);
4939   chr1_7(machine, state->mapper83_reg[7] | ((state->mapper83_reg[8] & 0x30) << 4), CHRROM);
49214940}
49224941
49234942static WRITE8_HANDLER( mapper83_w )
r8239r8240
49314950   case 0x3000:
49324951   case 0x30ff:
49334952   case 0x31ff:
4934      mapper83_reg[8] = data;
4953      state->mapper83_reg[8] = data;
49354954      mapper83_set_prg(space->machine);
49364955      mapper83_set_chr(space->machine);
49374956      break;
r8239r8240
49764995   case 0x0315:
49774996   case 0x0316:
49784997   case 0x0317:
4979      mapper83_reg[offset - 0x0310] = data;
4998      state->mapper83_reg[offset - 0x0310] = data;
49804999      mapper83_set_chr(space->machine);
49815000      break;
49825001   case 0x0318:
4983      mapper83_reg[9] = data;
5002      state->mapper83_reg[9] = data;
49845003      mapper83_set_prg(space->machine);
49855004      break;
49865005   }
r8239r8240
53415360            case 4:
53425361            case 5:
53435362               chr1_x(space->machine, 2 + state->mmc_cmd1, data & 0x1f, CHRROM);
5344               mapper95_reg[state->mmc_cmd1 - 2] = data & 0x20;
5363               state->mapper95_reg[state->mmc_cmd1 - 2] = data & 0x20;
53455364               if (!(state->mmc_cmd1 & 0x80))
53465365               {
5347                  set_nt_page(space->machine, 0, CIRAM, mapper95_reg[0] ? 1 : 0, 1);
5348                  set_nt_page(space->machine, 1, CIRAM, mapper95_reg[1] ? 1 : 0, 1);
5349                  set_nt_page(space->machine, 2, CIRAM, mapper95_reg[2] ? 1 : 0, 1);
5350                  set_nt_page(space->machine, 3, CIRAM, mapper95_reg[3] ? 1 : 0, 1);
5366                  set_nt_page(space->machine, 0, CIRAM, state->mapper95_reg[0] ? 1 : 0, 1);
5367                  set_nt_page(space->machine, 1, CIRAM, state->mapper95_reg[1] ? 1 : 0, 1);
5368                  set_nt_page(space->machine, 2, CIRAM, state->mapper95_reg[2] ? 1 : 0, 1);
5369                  set_nt_page(space->machine, 3, CIRAM, state->mapper95_reg[3] ? 1 : 0, 1);
53515370               }
53525371               else
53535372                  set_nt_mirroring(space->machine, PPU_MIRROR_HORZ);
r8239r8240
57795798   nes_state *state = (nes_state *)space->machine->driver_data;
57805799   LOG_MMC(("mapper114_m_w, offset: %04x, data: %02x\n", offset, data));
57815800
5782   map114_reg = data;
5801   state->map114_reg = data;
57835802
5784   if (map114_reg & 0x80)
5803   if (state->map114_reg & 0x80)
57855804   {
57865805      prg16_89ab(space->machine, data & 0x1f);
57875806      prg16_cdef(space->machine, data & 0x1f);
r8239r8240
57935812
57945813static WRITE8_HANDLER( mapper114_w )
57955814{
5815   nes_state *state = (nes_state *)space->machine->driver_data;
57965816   static const UINT8 conv_table[8] = {0, 3, 1, 5, 6, 7, 2, 4};
57975817   LOG_MMC(("mapper114_w, offset: %04x, data: %02x\n", offset, data));
57985818
r8239r8240
58045824         set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
58055825         break;
58065826      case 0x2000:
5807         map114_reg_enabled = 1;
5827         state->map114_reg_enabled = 1;
58085828         data = (data & 0xc0) | conv_table[data & 0x07];
58095829         mapper4_w(space, 0x8000, data);
58105830         break;
58115831      case 0x4000:
5812         if (map114_reg_enabled && (map114_reg & 0x80) == 0)
5832         if (state->map114_reg_enabled && (state->map114_reg & 0x80) == 0)
58135833         {
5814            map114_reg_enabled = 0;
5834            state->map114_reg_enabled = 0;
58155835            mapper4_w(space, 0x8001, data);
58165836         }
58175837         break;
r8239r8240
58495869
58505870static void mapper115_set_prg( running_machine *machine, int prg_base, int prg_mask  )
58515871{
5852   if (mapper115_reg[0] & 0x80)
5853      prg32(machine, mapper115_reg[0] >> 1);
5872   nes_state *state = (nes_state *)machine->driver_data;
5873   if (state->mapper115_reg[0] & 0x80)
5874      prg32(machine, state->mapper115_reg[0] >> 1);
58545875   else
58555876      mapper4_set_prg(machine, prg_base, prg_mask);
58565877}
r8239r8240
58635884   switch (offset & 0x01)
58645885   {
58655886   case 0x00:
5866      mapper115_reg[0] = data;
5887      state->mapper115_reg[0] = data;
58675888      mapper115_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
58685889      break;
58695890   case 0x01:
r8239r8240
61876208
61886209static WRITE8_HANDLER( mapper121_l_w )
61896210{
6211   nes_state *state = (nes_state *)space->machine->driver_data;
61906212   LOG_MMC(("mapper12_l_w, offset: %04x, data: %02x\n", offset, data));
61916213   offset += 0x100;
61926214
r8239r8240
61966218      {
61976219      case 0x00:
61986220      case 0x03:
6199         mapper121_reg[2] = 0x00;
6221         state->mapper121_reg[2] = 0x00;
62006222         break;
62016223      case 0x01:
6202         mapper121_reg[2] = 0x83;
6224         state->mapper121_reg[2] = 0x83;
62036225         break;
62046226      case 0x02:
6205         mapper121_reg[2] = 0x42;
6227         state->mapper121_reg[2] = 0x42;
62066228         break;
62076229      }
62086230   }
r8239r8240
62106232
62116233static READ8_HANDLER( mapper121_l_r )
62126234{
6235   nes_state *state = (nes_state *)space->machine->driver_data;
62136236   LOG_MMC(("mapper12_l_r, offset: %04x\n", offset));
62146237   offset += 0x100;
62156238
62166239   if (!(offset < 0x1000))
6217      return mapper121_reg[2];
6240      return state->mapper121_reg[2];
62186241   else
62196242      return 0;
62206243}
r8239r8240
62256248   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
62266249   UINT8 new_bank2, new_bank3;
62276250
6228   new_bank2 = mapper121_reg[0] ? mapper121_reg[0] : state->mmc_prg_bank[2 ^ prg_flip];
6229   new_bank3 = mapper121_reg[1] ? mapper121_reg[1] : state->mmc_prg_bank[3];
6251   new_bank2 = state->mapper121_reg[0] ? state->mapper121_reg[0] : state->mmc_prg_bank[2 ^ prg_flip];
6252   new_bank3 = state->mapper121_reg[1] ? state->mapper121_reg[1] : state->mmc_prg_bank[3];
62306253
62316254   prg8_89(machine, prg_base | (state->mmc_prg_bank[0 ^ prg_flip] & prg_mask));
62326255   prg8_ab(machine, prg_base | (state->mmc_prg_bank[1] & prg_mask));
r8239r8240
62456268      switch (data)
62466269      {
62476270      case 0x20:
6248         mapper121_reg[1] = 0x13;
6271         state->mapper121_reg[1] = 0x13;
62496272         break;
62506273      case 0x26:
6251         mapper121_reg[1] = 0x08;
6274         state->mapper121_reg[1] = 0x08;
62526275         break;
62536276      case 0x28:
6254         mapper121_reg[0] = 0x0c;
6277         state->mapper121_reg[0] = 0x0c;
62556278         break;
62566279      case 0x29:
6257         mapper121_reg[1] = 0x1b;
6280         state->mapper121_reg[1] = 0x1b;
62586281         break;
62596282      case 0xab:
6260         mapper121_reg[1] = 0x07;
6283         state->mapper121_reg[1] = 0x07;
62616284         break;
62626285      case 0xec:
62636286      case 0xef:
6264         mapper121_reg[1] = 0x0d;
6287         state->mapper121_reg[1] = 0x0d;
62656288         break;
62666289      case 0xff:
6267         mapper121_reg[1] = 0x09;
6290         state->mapper121_reg[1] = 0x09;
62686291         break;
62696292      default:
6270         mapper121_reg[0] = 0x0;
6271         mapper121_reg[1] = 0x0;
6293         state->mapper121_reg[0] = 0x0;
6294         state->mapper121_reg[1] = 0x0;
62726295         break;
62736296      }
62746297   }
r8239r8240
64416464
64426465static WRITE8_HANDLER( mapper132_l_w )
64436466{
6467   nes_state *state = (nes_state *)space->machine->driver_data;
64446468   LOG_MMC(("mapper132_l_w, offset: %04x, data: %02x\n", offset, data));
64456469
64466470   if (offset < 4)
6447      txc_reg[offset & 0x03] = data;
6471      state->txc_reg[offset & 0x03] = data;
64486472}
64496473
64506474static READ8_HANDLER( mapper132_l_r )
64516475{
6476   nes_state *state = (nes_state *)space->machine->driver_data;
64526477   LOG_MMC(("mapper132_l_r, offset: %04x\n", offset));
64536478
64546479   if (offset == 0x0000)
6455      return (txc_reg[1] ^ txc_reg[2]) | 0x40;
6480      return (state->txc_reg[1] ^ state->txc_reg[2]) | 0x40;
64566481   else
64576482      return 0x00;
64586483}
64596484
64606485static WRITE8_HANDLER( mapper132_w )
64616486{
6487   nes_state *state = (nes_state *)space->machine->driver_data;
64626488   LOG_MMC(("mapper132_w, offset: %04x, data: %02x\n", offset, data));
64636489
6464   prg32(space->machine, txc_reg[2] >> 2);
6465   chr8(space->machine, txc_reg[2], CHRROM);
6490   prg32(space->machine, state->txc_reg[2] >> 2);
6491   chr8(space->machine, state->txc_reg[2], CHRROM);
64666492}
64676493
64686494/*************************************************************
r8239r8240
66116637         state->mmc_cmd1 = data & 0x07;
66126638      else
66136639      {
6614         sachen_reg[state->mmc_cmd1] = data;
6640         state->sachen_reg[state->mmc_cmd1] = data;
66156641
66166642         switch (state->mmc_cmd1)
66176643         {
r8239r8240
66246650         default:
66256651            if (state->mmc_chr_source == CHRROM)
66266652            {
6627               chr1_0(space->machine, (sachen_reg[0] & 0x07), CHRROM);
6628               chr1_1(space->machine, (sachen_reg[1] & 0x07) | (sachen_reg[4] << 4 & 0x10), CHRROM);
6629               chr1_2(space->machine, (sachen_reg[2] & 0x07) | (sachen_reg[4] << 3 & 0x10), CHRROM);
6630               chr1_3(space->machine, (sachen_reg[3] & 0x07) | (sachen_reg[4] << 2 & 0x10) | (sachen_reg[6] << 3 & 0x08), CHRROM);
6653               chr1_0(space->machine, (state->sachen_reg[0] & 0x07), CHRROM);
6654               chr1_1(space->machine, (state->sachen_reg[1] & 0x07) | (state->sachen_reg[4] << 4 & 0x10), CHRROM);
6655               chr1_2(space->machine, (state->sachen_reg[2] & 0x07) | (state->sachen_reg[4] << 3 & 0x10), CHRROM);
6656               chr1_3(space->machine, (state->sachen_reg[3] & 0x07) | (state->sachen_reg[4] << 2 & 0x10) | (state->sachen_reg[6] << 3 & 0x08), CHRROM);
66316657            }
66326658            break;
66336659         }
r8239r8240
66666692         state->mmc_cmd1 = data & 0x07;
66676693      else
66686694      {
6669         sachen_reg[state->mmc_cmd1] = data;
6695         state->sachen_reg[state->mmc_cmd1] = data;
66706696
66716697         switch (state->mmc_cmd1)
66726698         {
r8239r8240
66796705         default:
66806706            if (state->mmc_chr_source == CHRROM)
66816707            {
6682               bank_helper1 = sachen_reg[7] & 0x01;
6683               bank_helper2 = (sachen_reg[4] & 0x07) << 3;
6684               chr2_0(space->machine, ((sachen_reg[bank_helper1 ? 0 : 0] & 0x07) | bank_helper2), CHRROM);
6685               chr2_2(space->machine, ((sachen_reg[bank_helper1 ? 0 : 1] & 0x07) | bank_helper2), CHRROM);
6686               chr2_4(space->machine, ((sachen_reg[bank_helper1 ? 0 : 2] & 0x07) | bank_helper2), CHRROM);
6687               chr2_6(space->machine, ((sachen_reg[bank_helper1 ? 0 : 3] & 0x07) | bank_helper2), CHRROM);
6708               bank_helper1 = state->sachen_reg[7] & 0x01;
6709               bank_helper2 = (state->sachen_reg[4] & 0x07) << 3;
6710               chr2_0(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 0] & 0x07) | bank_helper2), CHRROM);
6711               chr2_2(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 1] & 0x07) | bank_helper2), CHRROM);
6712               chr2_4(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 2] & 0x07) | bank_helper2), CHRROM);
6713               chr2_6(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 3] & 0x07) | bank_helper2), CHRROM);
66886714            }
66896715            break;
66906716         }
r8239r8240
67236749         state->mmc_cmd1 = data & 0x07;
67246750      else
67256751      {
6726         sachen_reg[state->mmc_cmd1] = data;
6752         state->sachen_reg[state->mmc_cmd1] = data;
67276753
67286754         switch (state->mmc_cmd1)
67296755         {
r8239r8240
67366762         default:
67376763            if (state->mmc_chr_source == CHRROM)
67386764            {
6739               bank_helper1 = sachen_reg[7] & 0x01;
6740               bank_helper2 = (sachen_reg[4] & 0x07) << 3;
6741               chr2_0(space->machine, ((sachen_reg[bank_helper1 ? 0 : 0] & 0x07) | bank_helper2) << 2, CHRROM);
6742               chr2_2(space->machine, ((sachen_reg[bank_helper1 ? 0 : 1] & 0x07) | bank_helper2) << 2 | 0x01, CHRROM);
6743               chr2_4(space->machine, ((sachen_reg[bank_helper1 ? 0 : 2] & 0x07) | bank_helper2) << 2 | 0x02, CHRROM)   ;
6744               chr2_6(space->machine, ((sachen_reg[bank_helper1 ? 0 : 3] & 0x07) | bank_helper2) << 2 | 0x03, CHRROM);
6765               bank_helper1 = state->sachen_reg[7] & 0x01;
6766               bank_helper2 = (state->sachen_reg[4] & 0x07) << 3;
6767               chr2_0(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 0] & 0x07) | bank_helper2) << 2, CHRROM);
6768               chr2_2(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 1] & 0x07) | bank_helper2) << 2 | 0x01, CHRROM);
6769               chr2_4(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 2] & 0x07) | bank_helper2) << 2 | 0x02, CHRROM)   ;
6770               chr2_6(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 3] & 0x07) | bank_helper2) << 2 | 0x03, CHRROM);
67456771            }
67466772            break;
67476773         }
r8239r8240
68006826         state->mmc_cmd1 = data & 0x07;
68016827      else
68026828      {
6803         sachen_reg[state->mmc_cmd1] = data;
6829         state->sachen_reg[state->mmc_cmd1] = data;
68046830
68056831         switch (state->mmc_cmd1)
68066832         {
r8239r8240
68136839         default:
68146840            if (state->mmc_chr_source == CHRROM)
68156841            {
6816               bank_helper1 = sachen_reg[7] & 0x01;
6817               bank_helper2 = (sachen_reg[4] & 0x07) << 3;
6818               chr2_0(space->machine, ((sachen_reg[bank_helper1 ? 0 : 0] & 0x07) | bank_helper2) << 1, state->mmc_chr_source);
6819               chr2_2(space->machine, ((sachen_reg[bank_helper1 ? 0 : 1] & 0x07) | bank_helper2) << 1 | 0x01, state->mmc_chr_source);
6820               chr2_4(space->machine, ((sachen_reg[bank_helper1 ? 0 : 2] & 0x07) | bank_helper2) << 1, state->mmc_chr_source);
6821               chr2_6(space->machine, ((sachen_reg[bank_helper1 ? 0 : 3] & 0x07) | bank_helper2) << 1 | 0x01, state->mmc_chr_source);
6842               bank_helper1 = state->sachen_reg[7] & 0x01;
6843               bank_helper2 = (state->sachen_reg[4] & 0x07) << 3;
6844               chr2_0(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 0] & 0x07) | bank_helper2) << 1, state->mmc_chr_source);
6845               chr2_2(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 1] & 0x07) | bank_helper2) << 1 | 0x01, state->mmc_chr_source);
6846               chr2_4(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 2] & 0x07) | bank_helper2) << 1, state->mmc_chr_source);
6847               chr2_6(space->machine, ((state->sachen_reg[bank_helper1 ? 0 : 3] & 0x07) | bank_helper2) << 1 | 0x01, state->mmc_chr_source);
68226848            }
68236849            break;
68246850         }
r8239r8240
71337159
71347160static void mapper153_set_prg( running_machine *machine )
71357161{
7162   nes_state *state = (nes_state *)machine->driver_data;
71367163   UINT8 mmc_helper = 0;
71377164   int i;
71387165
71397166   for (i = 0; i < 8; i++)
7140      mmc_helper |= ((map153_reg[i] & 0x01) << 4);
7167      mmc_helper |= ((state->map153_reg[i] & 0x01) << 4);
71417168
7142   map153_bank_latch = mmc_helper | (map153_bank_latch & 0x0f);
7169   state->map153_bank_latch = mmc_helper | (state->map153_bank_latch & 0x0f);
71437170
7144   prg16_89ab(machine, map153_bank_latch);
7171   prg16_89ab(machine, state->map153_bank_latch);
71457172   prg16_cdef(machine, mmc_helper | 0x0f);
71467173}
71477174
r8239r8240
71587185      case 2: case 3:
71597186      case 4: case 5:
71607187      case 6: case 7:
7161         map153_reg[offset & 0x07] = data;
7188         state->map153_reg[offset & 0x07] = data;
71627189         mapper153_set_prg(space->machine);
71637190         break;
71647191      case 8:
7165         map153_bank_latch = (data & 0x0f) | (map153_bank_latch & 0x10);
7166         prg16_89ab(space->machine, map153_bank_latch);
7192         state->map153_bank_latch = (data & 0x0f) | (state->map153_bank_latch & 0x10);
7193         prg16_89ab(space->machine, state->map153_bank_latch);
71677194         break;
71687195      default:
71697196         mapper16_m_w(space, offset & 0x0f, data);
r8239r8240
74937520
74947521static WRITE8_HANDLER( mapper166_w )
74957522{
7523   nes_state *state = (nes_state *)space->machine->driver_data;
74967524   UINT8 map166_helper1, map166_helper2;
74977525   LOG_MMC(("mapper166_w, offset: %04x, data: %02x\n", offset, data));
74987526
7499   subor_reg[(offset >> 13) & 0x03] = data;
7500   map166_helper1 = ((subor_reg[0] ^ subor_reg[1]) << 1) & 0x20;
7501   map166_helper2 = ((subor_reg[2] ^ subor_reg[3]) << 0) & 0x1f;
7527   state->subor_reg[(offset >> 13) & 0x03] = data;
7528   map166_helper1 = ((state->subor_reg[0] ^ state->subor_reg[1]) << 1) & 0x20;
7529   map166_helper2 = ((state->subor_reg[2] ^ state->subor_reg[3]) << 0) & 0x1f;
75027530
7503   if (subor_reg[1] & 0x08)
7531   if (state->subor_reg[1] & 0x08)
75047532   {
75057533      map166_helper1 += map166_helper2 & 0xfe;
75067534      map166_helper2 = map166_helper1;
75077535      map166_helper2 += 1;
75087536   }
7509   else if (subor_reg[1] & 0x04)
7537   else if (state->subor_reg[1] & 0x04)
75107538   {
75117539      map166_helper2 += map166_helper1;
75127540      map166_helper1 = 0x1f;
r8239r8240
75377565
75387566static WRITE8_HANDLER( mapper167_w )
75397567{
7568   nes_state *state = (nes_state *)space->machine->driver_data;
75407569   UINT8 map167_helper1, map167_helper2;
75417570   LOG_MMC(("mapper167_w, offset: %04x, data: %02x\n", offset, data));
75427571
7543   subor_reg[(offset >> 13) & 0x03] = data;
7544   map167_helper1 = ((subor_reg[0] ^ subor_reg[1]) << 1) & 0x20;
7545   map167_helper2 = ((subor_reg[2] ^ subor_reg[3]) << 0) & 0x1f;
7572   state->subor_reg[(offset >> 13) & 0x03] = data;
7573   map167_helper1 = ((state->subor_reg[0] ^ state->subor_reg[1]) << 1) & 0x20;
7574   map167_helper2 = ((state->subor_reg[2] ^ state->subor_reg[3]) << 0) & 0x1f;
75467575
7547   if (subor_reg[1] & 0x08)
7576   if (state->subor_reg[1] & 0x08)
75487577   {
75497578      map167_helper1 += map167_helper2 & 0xfe;
75507579      map167_helper2 = map167_helper1;
75517580      map167_helper1 += 1;
75527581   }
7553   else if (subor_reg[1] & 0x04)
7582   else if (state->subor_reg[1] & 0x04)
75547583   {
75557584      map167_helper2 += map167_helper1;
75567585      map167_helper1 = 0x1f;
r8239r8240
76477676
76487677static WRITE8_HANDLER( mapper172_w )
76497678{
7679   nes_state *state = (nes_state *)space->machine->driver_data;
76507680   LOG_MMC(("mapper172_w, offset: %04x, data: %02x\n", offset, data));
76517681
7652   prg32(space->machine, txc_reg[2] >> 2);
7653   chr8(space->machine, (((data ^ txc_reg[2]) >> 3) & 0x02) | (((data ^ txc_reg[2]) >> 5) & 0x01), CHRROM);
7682   prg32(space->machine, state->txc_reg[2] >> 2);
7683   chr8(space->machine, (((data ^ state->txc_reg[2]) >> 3) & 0x02) | (((data ^ state->txc_reg[2]) >> 5) & 0x01), CHRROM);
76547684}
76557685
76567686/*************************************************************
r8239r8240
76707700
76717701static READ8_HANDLER( mapper173_l_r )
76727702{
7703   nes_state *state = (nes_state *)space->machine->driver_data;
76737704   LOG_MMC(("mapper173_l_r, offset: %04x\n", offset));
76747705
76757706   if (offset == 0x0000)
7676      return (txc_reg[1] ^ txc_reg[2]) | 0x41;
7707      return (state->txc_reg[1] ^ state->txc_reg[2]) | 0x41;
76777708   else
76787709      return 0x00;
76797710}
r8239r8240
79968027   nes_state *state = (nes_state *)machine->driver_data;
79978028   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
79988029
7999   if (!(mapper187_reg[0] & 0x80))
8030   if (!(state->mapper187_reg[0] & 0x80))
80008031   {
80018032      prg8_89(machine, prg_base | (state->mmc_prg_bank[0 ^ prg_flip] & prg_mask));
80028033      prg8_ab(machine, prg_base | (state->mmc_prg_bank[1] & prg_mask));
r8239r8240
80388069      {
80398070      case 0x00:
80408071      case 0x01:
8041         mapper187_reg[1] = 0x83;
8072         state->mapper187_reg[1] = 0x83;
80428073         break;
80438074      case 0x02:
8044         mapper187_reg[1] = 0x42;
8075         state->mapper187_reg[1] = 0x42;
80458076         break;
80468077      case 0x03:
8047         mapper187_reg[1] = 0x00;
8078         state->mapper187_reg[1] = 0x00;
80488079         break;
80498080      }
80508081   }
80518082
80528083   if (!(offset <= 0x1000))
80538084   {
8054      mapper187_reg[0] = data;
8085      state->mapper187_reg[0] = data;
80558086
8056      if (mapper187_reg[0] & 0x80)
8087      if (state->mapper187_reg[0] & 0x80)
80578088      {
8058         new_bank = (mapper187_reg[0] & 0x1f);
8089         new_bank = (state->mapper187_reg[0] & 0x1f);
80598090
8060         if (mapper187_reg[0] & 0x20)
8091         if (state->mapper187_reg[0] & 0x20)
80618092            prg32(space->machine, new_bank >> 2);
80628093         else
80638094         {
r8239r8240
80728103
80738104static READ8_HANDLER( mapper187_l_r )
80748105{
8106   nes_state *state = (nes_state *)space->machine->driver_data;
80758107   LOG_MMC(("mapper187_l_r, offset: %04x\n", offset));
80768108   offset += 0x100;
80778109
80788110   if (!(offset < 0x1000))
8079      return mapper187_reg[1];
8111      return state->mapper187_reg[1];
80808112   else
80818113      return 0;
80828114}
r8239r8240
80908122   switch (offset & 0x6003)
80918123   {
80928124   case 0x0000:
8093      mapper187_reg[2] = 1;
8125      state->mapper187_reg[2] = 1;
80948126      MMC3_helper = state->mmc_cmd1 ^ data;
80958127      state->mmc_cmd1 = data;
80968128
r8239r8240
81048136      break;
81058137
81068138   case 0x0001:
8107      if (mapper187_reg[2])
8139      if (state->mapper187_reg[2])
81088140      {
81098141         cmd = state->mmc_cmd1 & 0x07;
81108142         switch (cmd)
r8239r8240
81278159      break;
81288160
81298161   case 0x0003:
8130      mapper187_reg[2] = 0;
8162      state->mapper187_reg[2] = 0;
81318163
81328164      if (data == 0x28)
81338165         prg8_cd(space->machine, 0x17);
r8239r8240
90669098
90679099static WRITE8_HANDLER( mapper208_l_w )
90689100{
9101   nes_state *state = (nes_state *)space->machine->driver_data;
90699102   static const UINT8 conv_table[256] =
90709103   {
90719104      0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x49,0x19,0x09,0x59,0x49,0x19,0x09,
r8239r8240
90899122   LOG_MMC(("mapper208_l_w, offset: %04x, data: %02x\n", offset, data));
90909123
90919124   if (!(offset < 0x1700))
9092      map208_reg[offset & 0x03] = data ^ conv_table[map208_reg[4]];
9125      state->map208_reg[offset & 0x03] = data ^ conv_table[state->map208_reg[4]];
90939126   else if (!(offset < 0xf00))
9094      map208_reg[4] = data;
9127      state->map208_reg[4] = data;
90959128   else if (!(offset < 0x700))
90969129      prg32(space->machine, ((data >> 3) & 0x02) | (data & 0x01));
90979130}
90989131
90999132static READ8_HANDLER( mapper208_l_r )
91009133{
9134   nes_state *state = (nes_state *)space->machine->driver_data;
91019135   LOG_MMC(("mapper208_l_r, offset: %04x\n", offset));
91029136
91039137   if (!(offset < 0x1700))
9104      return map208_reg[offset & 0x03];
9138      return state->map208_reg[offset & 0x03];
91059139
91069140   return 0x00;
91079141}
r8239r8240
92709304static void mapper215_set_prg( running_machine *machine, int prg_base, int prg_mask )
92719305{
92729306   nes_state *state = (nes_state *)machine->driver_data;
9273   if (map215_reg[0] & 0x80)
9307   if (state->map215_reg[0] & 0x80)
92749308   {
9275      prg16_89ab(machine, (map215_reg[0] & 0xf0) | (map215_reg[1] & 0x10));
9276      prg16_cdef(machine, (map215_reg[0] & 0xf0) | (map215_reg[1] & 0x10));
9309      prg16_89ab(machine, (state->map215_reg[0] & 0xf0) | (state->map215_reg[1] & 0x10));
9310      prg16_cdef(machine, (state->map215_reg[0] & 0xf0) | (state->map215_reg[1] & 0x10));
92779311   }
92789312   else
92799313   {
9280      state->mmc_prg_base = (map215_reg[1] & 0x08) ? 0x20 : (map215_reg[1] & 0x10);
9281      state->mmc_prg_mask = (map215_reg[1] & 0x08) ? 0x1f : 0x0f;
9314      state->mmc_prg_base = (state->map215_reg[1] & 0x08) ? 0x20 : (state->map215_reg[1] & 0x10);
9315      state->mmc_prg_mask = (state->map215_reg[1] & 0x08) ? 0x1f : 0x0f;
92829316
92839317      mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
92849318   }
r8239r8240
92889322{
92899323   nes_state *state = (nes_state *)machine->driver_data;
92909324   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
9291   int chr_base = (map215_reg[1] & 0x04) ? 0x100 : ((map215_reg[1] & 0x10) << 3);
9292   int chr_mask = (map215_reg[1] & 0x04) ? 0xff : 0x7f;
9325   int chr_base = (state->map215_reg[1] & 0x04) ? 0x100 : ((state->map215_reg[1] & 0x10) << 3);
9326   int chr_mask = (state->map215_reg[1] & 0x04) ? 0xff : 0x7f;
92939327
92949328   chr1_x(machine, chr_page ^ 0, chr_base | ((state->mmc_vrom_bank[0] & ~0x01) & chr_mask), chr);
92959329   chr1_x(machine, chr_page ^ 1, chr_base | ((state->mmc_vrom_bank[0] |  0x01) & chr_mask), chr);
r8239r8240
93099343
93109344   if (offset == 0x1000)
93119345   {
9312      map215_reg[0] = data;
9346      state->map215_reg[0] = data;
93139347      mapper215_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
93149348   }
93159349   else if (offset == 0x1001)
93169350   {
9317      map215_reg[1] = data;
9351      state->map215_reg[1] = data;
93189352      mapper215_set_chr(space->machine, state->mmc_chr_source);
93199353   }
93209354   else if (offset == 0x1007)
93219355   {
93229356      state->mmc_cmd1 = 0;
9323      map215_reg[2] = data;
9357      state->map215_reg[2] = data;
93249358      mapper215_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
93259359      mapper215_set_chr(space->machine, state->mmc_chr_source);
93269360   }
r8239r8240
93339367   static const UINT8 conv_table[8] = {0,2,5,3,6,1,7,4};
93349368   LOG_MMC(("mapper215_w, offset: %04x, data: %02x\n", offset, data));
93359369
9336   if (!map215_reg[2]) // in this case we act like MMC3, only with alt prg/chr handlers
9370   if (!state->map215_reg[2]) // in this case we act like MMC3, only with alt prg/chr handlers
93379371   {
93389372      switch (offset & 0x6001)
93399373      {
r8239r8240
93819415
93829416      case 0x0001:
93839417         cmd = state->mmc_cmd1 & 0x07;
9384         if (map215_reg[3] && ((map215_reg[0] & 0x80) == 0 || cmd < 6))   // if we use the prg16 banks and cmd=6,7 DON'T enter!
9418         if (state->map215_reg[3] && ((state->map215_reg[0] & 0x80) == 0 || cmd < 6))   // if we use the prg16 banks and cmd=6,7 DON'T enter!
93859419         {
9386            map215_reg[3] = 0;
9420            state->map215_reg[3] = 0;
93879421            switch (cmd)
93889422            {
93899423            case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
r8239r8240
94139447         if (MMC3_helper & 0x80)
94149448            mapper215_set_chr(space->machine, state->mmc_chr_source);
94159449
9416         map215_reg[3] = 1;
9450         state->map215_reg[3] = 1;
94179451         break;
94189452
94199453//      case 0x2001:
r8239r8240
94849518static void mapper217_set_prg( running_machine *machine, int prg_base, int prg_mask )
94859519{
94869520   nes_state *state = (nes_state *)machine->driver_data;
9487   state->mmc_prg_base = (map217_reg[1] & 0x08) ? 0 : (map217_reg[1] & 0x10);
9488   state->mmc_prg_mask = (map217_reg[1] & 0x08) ? 0x1f : 0x0f;
9521   state->mmc_prg_base = (state->map217_reg[1] & 0x08) ? 0 : (state->map217_reg[1] & 0x10);
9522   state->mmc_prg_mask = (state->map217_reg[1] & 0x08) ? 0x1f : 0x0f;
94899523
9490   state->mmc_prg_base |= ((map217_reg[1] & 0x03) << 5);
9524   state->mmc_prg_base |= ((state->map217_reg[1] & 0x03) << 5);
94919525
94929526   mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
94939527}
r8239r8240
94969530{
94979531   nes_state *state = (nes_state *)machine->driver_data;
94989532   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
9499   int chr_base = (map217_reg[1] & 0x08) ? 0x00 : ((map217_reg[1] & 0x10) << 3);
9500   int chr_mask = (map217_reg[1] & 0x08) ? 0xff : 0x7f;
9533   int chr_base = (state->map217_reg[1] & 0x08) ? 0x00 : ((state->map217_reg[1] & 0x10) << 3);
9534   int chr_mask = (state->map217_reg[1] & 0x08) ? 0xff : 0x7f;
95019535
9502   chr_base |= ((map217_reg[1] & 0x03) << 8);
9536   chr_base |= ((state->map217_reg[1] & 0x03) << 8);
95039537
95049538   chr1_x(machine, chr_page ^ 0, chr_base | ((state->mmc_vrom_bank[0] & ~0x01) & chr_mask), chr);
95059539   chr1_x(machine, chr_page ^ 1, chr_base | ((state->mmc_vrom_bank[0] |  0x01) & chr_mask), chr);
r8239r8240
95209554
95219555   if (offset == 0x1000)
95229556   {
9523      map217_reg[0] = data;
9557      state->map217_reg[0] = data;
95249558      if (data & 0x80)
95259559      {
9526         bank = (data & 0x0f) | ((map217_reg[1] & 0x03) << 4);
9560         bank = (data & 0x0f) | ((state->map217_reg[1] & 0x03) << 4);
95279561         prg16_89ab(space->machine, bank);
95289562         prg16_cdef(space->machine, bank);
95299563      }
r8239r8240
95329566   }
95339567   else if (offset == 0x1001)
95349568   {
9535      map217_reg[1] = data;
9569      state->map217_reg[1] = data;
95369570      mapper217_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
95379571   }
95389572   else if (offset == 0x1007)
95399573   {
9540      map217_reg[2] = data;
9574      state->map217_reg[2] = data;
95419575   }
95429576}
95439577
r8239r8240
95489582   static const UINT8 conv_table[8] = {0, 6, 3, 7, 5, 2, 4, 1};
95499583   LOG_MMC(("mapper217_w, offset: %04x, data: %02x\n", offset, data));
95509584
9551   if (!map217_reg[2]) // in this case we act like MMC3, only with alt prg/chr handlers
9585   if (!state->map217_reg[2]) // in this case we act like MMC3, only with alt prg/chr handlers
95529586   {
95539587      switch (offset & 0x6001)
95549588      {
r8239r8240
96089642         if (MMC3_helper & 0x80)
96099643            mapper217_set_chr(space->machine, state->mmc_chr_source);
96109644
9611         map217_reg[3] = 1;
9645         state->map217_reg[3] = 1;
96129646         break;
96139647
96149648      case 0x2000:
96159649         cmd = state->mmc_cmd1 & 0x07;
9616         if (map217_reg[3])
9650         if (state->map217_reg[3])
96179651         {
9618            map217_reg[3] = 0;
9652            state->map217_reg[3] = 0;
96199653            switch (cmd)
96209654            {
96219655            case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
r8239r8240
1052510559   nes_state *state = (nes_state *)machine->driver_data;
1052610560   int i;
1052710561
10528   if (map249_reg)
10562   if (state->map249_reg)
1052910563   {
1053010564      for (i = 0; i < 4; i ++)   // should this apply only to bank[0],bank[1]?
1053110565      {
r8239r8240
1054310577   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
1054410578   int i;
1054510579
10546   if (map249_reg)
10580   if (state->map249_reg)
1054710581   {
1054810582      for (i = 0; i < 6; i ++)
1054910583      {
r8239r8240
1057210606
1057310607   if (offset == 0x1000)
1057410608   {
10575      map249_reg = data & 0x02;
10609      state->map249_reg = data & 0x02;
1057610610      mapper249_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
1057710611      mapper249_set_chr(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
1057810612   }
r8239r8240
1103811072
1103911073   return NULL;
1104011074}
11075
11076/*************************************************************
11077 
11078 mapper_initialize
11079 
11080 Initialize various constants needed by mappers, depending
11081 on the mapper number. As long as the code is mainly iNES
11082 centric, also UNIF images will call this function to
11083 initialize their bankswitch and registers.
11084 
11085 *************************************************************/
11086
11087static int mapper_initialize( running_machine *machine, int mmc_num )
11088{
11089   nes_state *state = (nes_state *)machine->driver_data;
11090   int err = 0, i;
11091   
11092   switch (mmc_num)
11093   {
11094      case 0:
11095         err = 1; /* No mapper found */
11096         prg32(machine, 0);
11097         break;
11098      case 1:
11099      case 155:
11100         state->mmc_cmd1 = 0;
11101         state->mmc_count = 0;
11102         state->MMC1_regs[0] = 0x0f;
11103         state->MMC1_regs[1] = state->MMC1_regs[2] = state->MMC1_regs[3] = 0;
11104         set_nt_mirroring(machine, PPU_MIRROR_HORZ);
11105         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11106         MMC1_set_chr(machine);
11107         MMC1_set_prg(machine);
11108         break;
11109      case 2:
11110         /* These games don't switch VROM, but some ROMs incorrectly have CHR chunks */
11111         state->chr_chunks = 0;
11112         prg16_89ab(machine, 0);
11113         prg16_cdef(machine, state->prg_chunks - 1);
11114         break;
11115      case 13:
11116         chr4_0(machine, 0, CHRRAM);
11117         prg32(machine, 0);
11118         break;
11119      case 3:
11120      case 185:
11121         prg32(machine, 0);
11122         break;
11123      case 4:
11124      case 206:
11125         /* Only MMC3 and clones seem to use 4-screen! */
11126         /* hardware uses an 8K RAM chip, but can't address the high 4K! */
11127         /* that much is documented for Nintendo Gauntlet boards */
11128         if (state->four_screen_vram)
11129         {
11130            state->extended_ntram = auto_alloc_array(machine, UINT8, 0x2000);
11131            state_save_register_global_pointer(machine, state->extended_ntram, 0x2000);
11132            set_nt_page(machine, 0, CART_NTRAM, 0, 1);
11133            set_nt_page(machine, 1, CART_NTRAM, 1, 1);
11134            set_nt_page(machine, 2, CART_NTRAM, 2, 1);
11135            set_nt_page(machine, 3, CART_NTRAM, 3, 1);
11136         }
11137      case 12:
11138      case 74:
11139      case 118:
11140      case 119:
11141      case 191:
11142      case 192:
11143      case 194:
11144      case 195:
11145      case 196:
11146      case 245:
11147      case 250:
11148         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;   // prg_bank[2] & prg_bank[3] remain always the same in most MMC3 variants
11149         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;   // but some pirate clone mappers change them after writing certain registers
11150         state->mmc_cmd1 = 0;
11151         state->mmc_cmd2 = 0x80;
11152         state->mmc_prg_base = state->mmc_chr_base = 0;
11153         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;   // these could be init'ed as xxx_chunks-1 and they would work the same
11154         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11155         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11156         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11157         break;
11158      case 5:
11159         /* Can switch 8k prg banks, but they are saved as 16k in size */
11160         state->MMC5_rom_bank_mode = 3;
11161         state->MMC5_vrom_bank_mode = 0;
11162         state->MMC5_vram_protect = 0;
11163         state->mmc5_high_chr = 0;
11164         state->mmc5_vram_control = 0;
11165         state->mmc5_split_scr = 0;
11166         memset(state->MMC5_vrom_bank, 0, ARRAY_LENGTH(state->MMC5_vrom_bank));
11167         state->mid_ram_enable = 0;
11168         prg16_89ab(machine, state->prg_chunks - 2);
11169         prg16_cdef(machine, state->prg_chunks - 1);
11170         break;
11171      case 6:
11172         state->mmc_cmd1 = 0;
11173         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11174         prg16_89ab(machine, 0);
11175         prg16_cdef(machine, 7);
11176         break;
11177      case 7:
11178         /* Bankswitches 32k at a time */
11179         set_nt_mirroring(machine, PPU_MIRROR_LOW);
11180         prg16_89ab(machine, 0);
11181         prg16_cdef(machine, state->prg_chunks - 1);
11182         break;
11183      case 8:
11184      case 96:
11185         prg32(machine, 0);
11186         break;
11187      case 9:
11188         state->MMC2_regs[0] = state->MMC2_regs[2] = 0;
11189         state->MMC2_regs[1] = state->MMC2_regs[3] = 0;
11190         state->mmc_cmd1 = state->mmc_cmd2 = 0xfe;
11191         prg8_89(machine, 0);
11192         //ugly hack to deal with iNES header usage of chunk count.
11193         prg8_ab(machine, (state->prg_chunks << 1) - 3);
11194         prg8_cd(machine, (state->prg_chunks << 1) - 2);
11195         prg8_ef(machine, (state->prg_chunks << 1) - 1);
11196         break;
11197      case 10:
11198         /* Reset VROM latches */
11199         state->MMC2_regs[0] = state->MMC2_regs[2] = 0;
11200         state->MMC2_regs[1] = state->MMC2_regs[3] = 0;
11201         state->mmc_cmd1 = state->mmc_cmd2 = 0xfe;
11202         prg16_89ab(machine, 0);
11203         prg16_cdef(machine, state->prg_chunks - 1);
11204         break;
11205      case 11:
11206         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;   // NINA-07 has no CHRROM
11207         prg32(machine, 0);
11208         break;
11209      case 14:
11210         state->mmc_extra_bank[2] = 0xfe;
11211         state->mmc_extra_bank[3] = 0xff;
11212         state->mmc_extra_bank[0] = state->mmc_extra_bank[1] = state->mmc_extra_bank[4] = state->mmc_extra_bank[5] = state->mmc_extra_bank[6] = 0;
11213         state->mmc_extra_bank[7] = state->mmc_extra_bank[8] = state->mmc_extra_bank[9] = state->mmc_extra_bank[0xa] = state->mmc_extra_bank[0xb] = 0;
11214         state->map14_reg[0] = state->map14_reg[1] = 0;
11215         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11216         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11217         state->mmc_cmd1 = 0;
11218         state->mmc_cmd2 = 0x80;
11219         state->mmc_prg_base = state->mmc_chr_base = 0;
11220         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11221         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11222         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11223         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11224         break;
11225      case 15:
11226         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11227         prg32(machine, 0);
11228         break;
11229      case 16:
11230      case 17:
11231      case 18:
11232      case 157:
11233      case 159:
11234         state->mmc_cmd1 = 0;
11235         prg16_89ab(machine, 0);
11236         prg16_cdef(machine, state->prg_chunks - 1);
11237         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11238         break;
11239      case 19:
11240         state->mmc_cmd1 = 0;
11241         prg16_89ab(machine, 0);
11242         prg16_cdef(machine, state->prg_chunks - 1);
11243         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11244         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11245         break;
11246      case 20:
11247         state->fds_last_side = 0;
11248         state->fds_count = 0;
11249         state->fds_motor_on = 0;
11250         state->fds_door_closed = 0;
11251         state->fds_current_side = 1;
11252         state->fds_head_position = 0;
11253         state->fds_status0 = 0;
11254         state->fds_read_mode = state->fds_write_reg = 0;
11255         break;
11256      case 21:
11257      case 22:
11258      case 23:
11259      case 24:
11260      case 25:
11261      case 26:
11262      case 33:
11263      case 48:
11264      case 73:
11265      case 75:
11266      case 80:
11267      case 82:
11268      case 85:
11269      case 87:
11270      case 117:
11271      case 207:
11272      case 222:
11273      case 252:
11274         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11275         prg16_89ab(machine, 0);
11276         prg16_cdef(machine, state->prg_chunks - 1);
11277         break;
11278      case 32:
11279         state->mmc_cmd1 = 0;
11280         prg16_89ab(machine, 0);
11281         prg16_cdef(machine, state->prg_chunks - 1);
11282         if (state->crc_hack)
11283            set_nt_mirroring(machine, PPU_MIRROR_HIGH);  // needed by Major League
11284         break;
11285      case 34:
11286         prg32(machine, 0);
11287         break;
11288      case 35:
11289         prg32(machine, 0xff);
11290         break;
11291      case 37:
11292         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11293         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11294         state->mmc_cmd1 = 0;
11295         state->mmc_cmd2 = 0x80;
11296         state->mmc_prg_base = state->mmc_chr_base = 0;
11297         state->mmc_prg_mask = 0x07;
11298         state->mmc_chr_mask = 0x7f;
11299         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11300         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11301         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11302         break;
11303      case 40:
11304         prg8_67(machine, 0xfe);
11305         prg8_89(machine, 0xfc);
11306         prg8_ab(machine, 0xfd);
11307         prg8_cd(machine, 0xfe);
11308         prg8_ef(machine, 0xff);
11309         break;
11310      case 41:
11311         state->mmc_cmd1 = 0;
11312         prg32(machine, 0);
11313         break;
11314      case 42:
11315         prg32(machine, state->prg_chunks - 1);
11316         break;
11317      case 43:
11318         prg32(machine, 0);
11319         if (state->battery)
11320            memset(state->battery_ram, 0x2000, 0xff);
11321         else if (state->prg_ram)
11322            memset(state->wram, 0x2000, 0xff);
11323         break;
11324      case 44:
11325      case 47:
11326      case 49:
11327         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11328         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11329         state->mmc_cmd1 = 0;
11330         state->mmc_cmd2 = 0x80;
11331         state->mmc_prg_base = state->mmc_chr_base = 0;
11332         state->mmc_prg_mask = 0x0f;
11333         state->mmc_chr_mask = 0x7f;
11334         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11335         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11336         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11337         break;
11338      case 45:
11339         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11340         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11341         state->mmc_cmd1 = 0;
11342         state->mmc_cmd2 = 0x80;
11343         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11344         state->mapper45_reg[0] = state->mapper45_reg[1] = state->mapper45_reg[2] = state->mapper45_reg[3] = 0;
11345         state->mmc_prg_base = 0x30;
11346         state->mmc_prg_mask = 0x3f;
11347         state->mmc_chr_base = 0;
11348         state->mmc_chr_mask = 0x7f;
11349         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11350         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11351         //         memory_set_bankptr(machine, "bank5", state->wram);
11352         break;
11353      case 46:
11354         prg32(machine, 0);
11355         chr8(machine, 0, CHRROM);
11356         break;
11357      case 54:
11358         prg32(machine, 0);
11359         chr8(machine, 0, CHRROM);
11360         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11361         break;
11362      case 50:
11363         prg8_67(machine, 0x0f);
11364         prg8_89(machine, 0x08);
11365         prg8_ab(machine, 0x09);
11366         prg8_cd(machine, 0);
11367         prg8_ef(machine, 0x0b);
11368         break;
11369      case 51:
11370         state->mapper51_reg[0] = 0x01;
11371         state->mapper51_reg[1] = 0x00;
11372         mapper51_set_banks(machine);
11373         break;
11374      case 52:
11375         state->mmc_prg_bank[0] = 0xfe;
11376         state->mmc_prg_bank[2] = 0xfe;
11377         state->mmc_prg_bank[1] = 0xff;
11378         state->mmc_prg_bank[3] = 0xff;
11379         state->mmc_cmd1 = 0;
11380         state->mmc_cmd2 = 0x80;
11381         state->map52_reg_written = 0;
11382         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11383         state->mmc_prg_base = 0;
11384         state->mmc_prg_mask = 0x1f;
11385         state->mmc_chr_base = 0;
11386         state->mmc_chr_mask = 0xff;
11387         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11388         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11389         //         memory_set_bankptr(machine, "bank5", state->wram);
11390         break;
11391      case 57:
11392         state->mmc_cmd1 = 0x00;
11393         state->mmc_cmd2 = 0x00;
11394         prg16_89ab(machine, 0);
11395         prg16_cdef(machine, 0);
11396         chr8(machine, 0, CHRROM);
11397         break;
11398      case 58:
11399         prg32(machine, 0);
11400         chr8(machine, 0, CHRROM);
11401         break;
11402      case 61:
11403      case 62:
11404      case 86:
11405         prg32(machine, 0);
11406         break;
11407      case 64:
11408      case 158:
11409         state->mmc_cmd1 = 0;
11410         prg16_89ab(machine, state->prg_chunks - 1);
11411         prg16_cdef(machine, state->prg_chunks - 1);
11412         break;
11413      case 65:
11414      case 67:
11415      case 69:
11416      case 72:
11417      case 78:
11418      case 92:
11419         prg16_89ab(machine, 0);
11420         prg16_cdef(machine, state->prg_chunks - 1);
11421         break;
11422      case 68:
11423         state->m68_mirror = state->m0 = state->m1 = 0;
11424         prg16_89ab(machine, 0);
11425         prg16_cdef(machine, state->prg_chunks - 1);
11426         break;
11427      case 70:
11428         prg16_89ab(machine, state->prg_chunks - 2);
11429         prg16_cdef(machine, state->prg_chunks - 1);
11430         break;
11431      case 71:
11432         set_nt_mirroring(machine, PPU_MIRROR_HORZ);
11433         prg16_89ab(machine, 0);
11434         prg16_cdef(machine, state->prg_chunks - 1);
11435         break;
11436      case 76:
11437         prg8_89(machine, 0);
11438         prg8_ab(machine, 1);
11439         //cd is bankable, but this should init all banks just fine.
11440         prg16_cdef(machine, state->prg_chunks - 1);
11441         chr2_0(machine, 0, CHRROM);
11442         chr2_2(machine, 1, CHRROM);
11443         chr2_4(machine, 2, CHRROM);
11444         chr2_6(machine, 3, CHRROM);
11445         state->mmc_cmd1 = 0;
11446         break;
11447      case 77:
11448         prg32(machine, 0);
11449         chr2_2(machine, 0, CHRROM);
11450         chr2_4(machine, 1, CHRROM);
11451         chr2_6(machine, 2, CHRROM);
11452         break;
11453      case 79:
11454      case 146:
11455         /* Mirroring always horizontal...? */
11456         //          Mirroring = 1;
11457         set_nt_mirroring(machine, PPU_MIRROR_HORZ);
11458         chr8(machine, 0, CHRROM);
11459         prg32(machine, 0);
11460         break;
11461      case 228:
11462         chr8(machine, 0, CHRROM);
11463         prg32(machine, 0);
11464         break;
11465      case 83:
11466         state->mapper83_reg[9] = 0x0f;
11467         prg8_cd(machine, 0x1e);
11468         prg8_ef(machine, 0x1f);
11469         break;
11470      case 88:
11471         prg8_89(machine, 0xc);
11472         prg8_ab(machine, 0xd);
11473         prg8_cd(machine, 0xe);
11474         prg8_ef(machine, 0xf);
11475         break;
11476      case 89:
11477         prg16_89ab(machine, 0);
11478         prg16_cdef(machine, state->prg_chunks - 1);
11479         set_nt_mirroring(machine, PPU_MIRROR_LOW);
11480         break;
11481      case 91:
11482         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11483         prg16_89ab(machine, state->prg_chunks - 1);
11484         prg16_cdef(machine, state->prg_chunks - 1);
11485         break;
11486      case 93:
11487      case 94:
11488      case 95:
11489      case 101:
11490         prg16_89ab(machine, 0);
11491         prg16_cdef(machine, state->prg_chunks - 1);
11492         break;
11493      case 97:
11494         prg16_89ab(machine, state->prg_chunks - 1);
11495         prg16_cdef(machine, state->prg_chunks - 1);
11496         break;
11497      case 112:
11498         state->mmc_cmd1 = 0;
11499         prg16_89ab(machine, 0);
11500         prg16_cdef(machine, state->prg_chunks - 1);
11501         break;
11502      case 36:
11503      case 38:
11504      case 39:
11505      case 66:
11506      case 107:
11507      case 113:
11508      case 177:
11509      case 179:
11510      case 184:
11511      case 140:
11512      case 144:
11513      case 240:
11514      case 241:
11515      case 242:
11516      case 244:
11517         prg32(machine, 0);
11518         break;
11519      case 104:
11520         prg16_89ab(machine, 0x00);
11521         prg16_cdef(machine, 0x0f);
11522         break;
11523      case 106:
11524         prg8_89(machine, (state->prg_chunks << 1) - 1);
11525         prg8_ab(machine, 0);
11526         prg8_cd(machine, 0);
11527         prg8_ef(machine, (state->prg_chunks << 1) - 1);
11528         break;
11529      case 114:
11530         state->map114_reg = state->map114_reg_enabled = 0;
11531         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11532         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11533         state->mmc_cmd1 = 0;
11534         state->mmc_cmd2 = 0x80;
11535         state->mmc_prg_base = state->mmc_chr_base = 0;
11536         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11537         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11538         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11539         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11540         break;
11541      case 115:
11542         state->mapper115_reg[0] = 0;
11543         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11544         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11545         state->mmc_cmd1 = 0;
11546         state->mmc_cmd2 = 0x80;
11547         state->mmc_prg_base = state->mmc_chr_base = 0;
11548         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11549         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11550         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11551         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11552         break;
11553      case 121:
11554         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11555         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11556         state->mapper121_reg[0] = state->mapper121_reg[1] = state->mapper121_reg[2] = 0;
11557         state->mmc_cmd1 = 0;
11558         state->mmc_cmd2 = 0x80;
11559         state->mmc_prg_base = state->mmc_chr_base = 0;
11560         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11561         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11562         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11563         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11564         break;
11565      case 132:
11566      case 172:
11567      case 173:
11568         state->txc_reg[0] = state->txc_reg[1] = state->txc_reg[2] = state->txc_reg[3] = 0;
11569         prg32(machine, 0);
11570         break;
11571      case 134:
11572         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11573         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11574         state->mmc_cmd1 = 0;
11575         state->mmc_cmd2 = 0x80;
11576         state->mmc_prg_base = state->mmc_chr_base = 0;
11577         state->mmc_prg_mask = 0x1f;
11578         state->mmc_chr_mask = 0xff;
11579         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11580         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11581         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11582         break;
11583      case 166:
11584         state->subor_reg[0] = state->subor_reg[1] = state->subor_reg[2] = state->subor_reg[3] = 0;
11585         prg16_89ab(machine, 0);
11586         prg16_cdef(machine, 0x07);
11587         break;
11588      case 167:
11589         state->subor_reg[0] = state->subor_reg[1] = state->subor_reg[2] = state->subor_reg[3] = 0;
11590         prg16_89ab(machine, 0);
11591         prg16_cdef(machine, 0x20);
11592         break;
11593      case 136:
11594         state->mmc_cmd1 = 0;
11595         prg32(machine, 0);
11596         break;
11597      case 137:
11598         state->mmc_cmd1 = 0;
11599         prg32(machine, 0);
11600         chr8(machine, state->chr_chunks - 1, CHRROM);
11601         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11602         break;
11603      case 138:
11604      case 139:
11605      case 141:
11606         state->mmc_cmd1 = 0;
11607         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11608         prg32(machine, 0);
11609         chr8(machine, 0, state->mmc_chr_source);
11610         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11611         break;
11612      case 143:
11613         prg16_89ab(machine, 0);
11614         prg16_cdef(machine, 1);
11615         chr8(machine, 0, CHRROM);
11616         break;
11617      case 133:
11618      case 145:
11619      case 147:
11620      case 148:
11621      case 149:
11622      case 171:
11623         prg32(machine, 0);
11624         chr8(machine, 0, CHRROM);
11625         break;
11626      case 150:
11627         state->mmc_cmd1 = 0;
11628         prg32(machine, 0);
11629         chr8(machine, 0, CHRROM);
11630         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11631         break;
11632      case 152:
11633         prg16_89ab(machine, 0);
11634         prg16_cdef(machine, state->prg_chunks - 1);
11635         chr8(machine, 0, CHRROM);
11636         break;
11637      case 153:
11638         state->mmc_cmd1 = 0;
11639         prg16_89ab(machine, 0);
11640         prg16_cdef(machine, state->prg_chunks - 1);
11641         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11642         // Famicom Jump II needs also the following!
11643         for (i = 0; i < 8; i++)
11644            state->map153_reg[i] = 0;
11645         
11646         state->map153_bank_latch = 0;
11647         if (state->crc_hack)
11648            mapper153_set_prg(machine);
11649         break;
11650      case 154:
11651         prg16_89ab(machine, state->prg_chunks - 2);
11652         prg16_cdef(machine, state->prg_chunks - 1);
11653         break;
11654      case 156:
11655         prg16_89ab(machine, state->prg_chunks - 2);
11656         prg16_cdef(machine, state->prg_chunks - 1);
11657         set_nt_mirroring(machine, PPU_MIRROR_LOW);
11658         break;
11659      case 164:
11660         prg32(machine, 0xff);
11661         state->mid_ram_enable = 1;
11662         break;
11663      case 176:
11664         prg32(machine, (state->prg_chunks - 1) >> 1);
11665         break;
11666      case 178:
11667         state->mmc_cmd1 = 0;
11668         prg32(machine, 0);
11669         break;
11670      case 180:
11671         prg16_89ab(machine, 0);
11672         prg16_cdef(machine, 0);
11673         break;
11674      case 182:
11675         prg32(machine, (state->prg_chunks - 1) >> 1);
11676         break;
11677      case 188:
11678         prg16_89ab(machine, 0);
11679         prg16_cdef(machine, (state->prg_chunks - 1) ^ 0x08);
11680         break;
11681      case 187:
11682         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11683         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11684         state->mapper187_reg[0] = state->mapper187_reg[1] = state->mapper187_reg[2] = state->mapper187_reg[3] = 0;
11685         state->mmc_cmd1 = 0;
11686         state->mmc_cmd2 = 0x80;
11687         state->mmc_prg_base = state->mmc_chr_base = 0;
11688         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11689         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11690         mapper187_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11691         mapper187_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11692         break;
11693      case 189:
11694         state->mmc_cmd1 = 0;
11695         state->mmc_cmd2 = 0x80;
11696         state->mmc_chr_base = 0;
11697         state->mmc_chr_mask = 0xff;
11698         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11699         prg32(machine, 0);
11700         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11701         break;
11702      case 193:
11703         prg32(machine, (state->prg_chunks - 1) >> 1);
11704         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11705         break;
11706      case 197:
11707         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11708         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11709         state->mmc_cmd1 = 0;
11710         state->mmc_cmd2 = 0x80;
11711         state->mmc_prg_base = state->mmc_chr_base = 0;
11712         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11713         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11714         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11715         mapper197_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11716         break;
11717      case 198:
11718         state->mmc_prg_bank[0] = 0x00;
11719         state->mmc_prg_bank[1] = 0x01;
11720         state->mmc_prg_bank[2] = 0x4e;
11721         state->mmc_prg_bank[3] = 0x4f;
11722         state->mmc_cmd1 = 0;
11723         state->mmc_cmd2 = 0x80;
11724         state->mmc_prg_base = state->mmc_chr_base = 0;
11725         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11726         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11727         mapper198_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11728         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11729         break;
11730      case 199:
11731         state->mmc_prg_bank[0] = 0x00;
11732         state->mmc_prg_bank[1] = 0x01;
11733         state->mmc_prg_bank[2] = 0x3e;
11734         state->mmc_prg_bank[3] = 0x3f;
11735         state->mmc_cmd1 = 0;
11736         state->mmc_cmd2 = 0x80;
11737         state->mmc_prg_base = state->mmc_chr_base = 0;
11738         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11739         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11740         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11741         mapper199_set_chr(machine, state->mmc_chr_base, state->mmc_chr_mask);
11742         break;
11743      case 200:
11744         prg16_89ab(machine, state->prg_chunks - 1);
11745         prg16_cdef(machine, state->prg_chunks - 1);
11746         break;
11747      case 201:
11748      case 213:
11749         prg32(machine, 0);
11750         chr8(machine, 0, CHRROM);
11751         break;
11752      case 202:
11753      case 203:
11754      case 204:
11755      case 214:
11756         prg16_89ab(machine, 0);
11757         prg16_cdef(machine, 0);
11758         chr8(machine, 0, CHRROM);
11759         break;
11760      case 205:
11761         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11762         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11763         state->mmc_cmd1 = 0;
11764         state->mmc_cmd2 = 0x80;
11765         state->mmc_prg_base = 0x10;
11766         state->mmc_prg_mask = 0x1f;
11767         state->mmc_chr_base = 0;
11768         state->mmc_chr_mask = 0xff;
11769         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11770         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11771         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11772         break;
11773      case 208:
11774         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11775         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11776         state->map208_reg[0] = state->map208_reg[1] = state->map208_reg[2] = state->map208_reg[3] = state->map208_reg[4] = 0;
11777         state->mmc_cmd1 = 0;
11778         state->mmc_cmd2 = 0x80;
11779         state->mmc_prg_base = state->mmc_chr_base = 0;
11780         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11781         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11782         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11783         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
11784         break;
11785      case 212:
11786         chr8(machine, 0xff, CHRROM);
11787         prg32(machine, 0xff);
11788         break;
11789      case 215:
11790         state->mmc_prg_bank[0] = 0x00;
11791         state->mmc_prg_bank[2] = 0xfe;
11792         state->mmc_prg_bank[1] = 0x01;
11793         state->mmc_prg_bank[3] = 0xff;
11794         state->mmc_cmd1 = 0;
11795         state->mmc_cmd2 = 0x80;
11796         state->map215_reg[0] = 0x00;
11797         state->map215_reg[1] = 0xff;
11798         state->map215_reg[2] = 0x04;
11799         state->map215_reg[3] = 0;
11800         state->mmc_prg_base = 0;
11801         state->mmc_prg_mask = 0x1f;
11802         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11803         mapper215_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11804         mapper215_set_chr(machine, state->mmc_chr_source);
11805         break;
11806      case 216:
11807         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11808         prg32(machine, 0);
11809         chr8(machine, 0, state->mmc_chr_source);
11810         break;
11811      case 217:
11812         state->map217_reg[0] = 0x00;
11813         state->map217_reg[1] = 0xff;
11814         state->map217_reg[2] = 0x03;
11815         state->map217_reg[3] = 0;
11816         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11817         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11818         state->mmc_cmd1 = 0;
11819         state->mmc_cmd2 = 0x80;
11820         state->mmc_prg_base = state->mmc_chr_base = 0;
11821         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11822         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11823         mapper217_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11824         mapper217_set_chr(machine, state->mmc_chr_source);
11825         break;
11826      case 221:
11827         state->mmc_cmd1 = 0;
11828         state->mmc_cmd2 = 0;
11829         prg16_89ab(machine, 0);
11830         prg16_cdef(machine, 0);
11831         break;
11832      case 225:
11833         prg32(machine, 0);
11834         break;
11835      case 226:
11836         state->mmc_cmd1 = 0;
11837         state->mmc_cmd2 = 0;
11838         prg32(machine, 0);
11839         break;
11840      case 227:
11841         prg16_89ab(machine, 0);
11842         prg16_cdef(machine, 0);
11843         break;
11844      case 229:
11845      case 255:
11846         prg16_89ab(machine, 0);
11847         prg16_cdef(machine, 1);
11848         chr8(machine, 0, CHRROM);
11849         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11850         break;
11851      case 230:
11852         prg16_89ab(machine, 0);
11853         prg16_cdef(machine, 7);
11854         break;
11855      case 231:
11856         prg16_89ab(machine, 0);
11857         prg16_cdef(machine, state->prg_chunks - 1);
11858         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11859         break;
11860      case 232:
11861         state->mmc_cmd1 = 0x18;
11862         state->mmc_cmd2 = 0x00;
11863         mapper232_set_prg(machine);
11864         break;
11865      case 243:
11866         state->mmc_vrom_bank[0] = 3;
11867         state->mmc_cmd1 = 0;
11868         chr8(machine, 3, CHRROM);
11869         prg32(machine, 0);
11870         set_nt_mirroring(machine, PPU_MIRROR_VERT);
11871         break;
11872      case 246:
11873         prg32(machine, 0xff);
11874         break;
11875      case 249:
11876         state->mmc_prg_bank[0] = state->mmc_prg_bank[2] = 0xfe;
11877         state->mmc_prg_bank[1] = state->mmc_prg_bank[3] = 0xff;
11878         state->mmc_cmd1 = 0;
11879         state->mmc_cmd2 = 0x80;
11880         state->map249_reg = 0;
11881         state->mmc_prg_base = state->mmc_chr_base = 0;
11882         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
11883         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
11884         mapper249_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
11885         mapper249_set_chr(machine, state->mmc_chr_base, state->mmc_chr_mask);
11886         break;
11887      default:
11888         /* Mapper not supported */
11889         err = 2;
11890         break;
11891   }
11892   
11893   return err;
11894}
11895
11896/*************************************************************
11897 
11898 nes_mapper_reset
11899 
11900 Resets the mapper bankswitch areas to their defaults.
11901 It returns a value "err" that indicates if it was
11902 successful. Possible values for err are:
11903 
11904 0 = success
11905 1 = no mapper found
11906 2 = mapper not supported
11907 
11908 *************************************************************/
11909
11910int nes_mapper_reset( running_machine *machine )
11911{
11912   nes_state *state = (nes_state *)machine->driver_data;
11913   int err = 0, i;
11914   const mmc *mapper;
11915   
11916   if (state->chr_chunks == 0)
11917      chr8(machine, 0,CHRRAM);
11918   else
11919      chr8(machine, 0,CHRROM);
11920   
11921   /* Set the mapper irq callback */
11922   mapper = nes_mapper_lookup(state->mapper);
11923   
11924   if (mapper == NULL)
11925      fatalerror("Unimplemented Mapper %d", state->mapper);
11926   //      logerror("Mapper %d is not yet supported, defaulting to no mapper.\n", state->mapper);    // this one would be a better output
11927   
11928   ppu2c0x_set_scanline_callback(state->ppu, mapper ? mapper->mmc_scanline : NULL);
11929   ppu2c0x_set_hblank_callback(state->ppu, mapper ? mapper->mmc_hblank : NULL);
11930   
11931   state->mmc5_vram_control = 0;
11932   
11933   /* Here, we init a few helpers: 4 prg banks and 16 chr banks - some mappers use them */
11934   for (i = 0; i < 4; i++)
11935      state->mmc_prg_bank[i] = 0;
11936   for (i = 0; i < 16; i++)
11937      state->mmc_vrom_bank[i] = 0;
11938   for (i = 0; i < 16; i++)
11939      state->mmc_extra_bank[i] = 0;
11940   
11941   /* Finally, we init IRQ-related quantities. */
11942   state->IRQ_enable = state->IRQ_enable_latch = 0;
11943   state->IRQ_count = state->IRQ_count_latch = 0;
11944   state->IRQ_toggle = 0;
11945   
11946   err = mapper_initialize(machine, state->mapper);
11947   
11948   return err;
11949}
11950

Previous 15994 Revisions Next


© 1998-2018 The MESS Team