Previous 15994 Revisions Next

r7956 Saturday 1st May, 2010 at 12:58:44 UTC by Fabio Priuli
[NES] moved even more variables to driver data class and simplified handlers initialization
[src/mess/includes]nes.h
[src/mess/machine]nes.c nes_ines.c nes_mmc.c nes_mmc.h nes_unif.c

src/mess/includes/nes.h
r7955r7956
3030   UINT32 i0, i1, i2;
3131};
3232
33/*PPU fast banking constants and structures */
34
35#define CHRROM 0
36#define CHRRAM 1
37
38typedef struct
39{
40   int source;   //defines source of base pointer
41   int origin; //defines offset of 0x400 byte segment at base pointer
42   UINT8* access;   //source translated + origin -> valid pointer!
43} chr_bank;
44
45/*PPU nametable fast banking constants and structures */
46
47#define CIRAM 0
48#define ROM 1
49#define EXRAM 2
50#define MMC5FILL 3
51#define CART_NTRAM 4
52
53typedef struct
54{
55   int source;      /* defines source of base pointer */
56   int origin;      /* defines offset of 0x400 byte segment at base pointer */
57   int writable;   /* ExRAM, at least, can be write-protected AND used as nametable */
58   UINT8* access;   /* direct access when possible */
59} name_table;
60
61
3362class nes_state
3463{
3564public:
r7955r7956
4069   /* input_related - this part has to be cleaned up (e.g. in_2 and in_3 are not really necessary here...) */
4170   nes_input in_0, in_1, in_2, in_3;
4271
72   chr_bank      chr_map[8];  //quick banking structure, because some of this changes multiple times per scanline!
73   name_table    nt_page[4];  //quick banking structure for a maximum of 4K of RAM/ROM/ExRAM
74
75   int MMC5_floodtile;
76   int MMC5_floodattr;
77   UINT8 mmc5_vram[0x400];
78   int mmc5_vram_control;
79   UINT8 *extended_ntram;
80
4381   /* video-related */
4482   int nes_vram_sprite[8]; /* Used only by mmc5 for now */
4583   int last_frame_flip;
r7955r7956
5290
5391   /* misc region to be allocated at init */
5492   // variables which don't change at run-time
55   UINT8 *rom;
56   UINT8 *vrom;
57   UINT8 *vram;
58   UINT8 *wram;
59   UINT8 *ciram; //PPU nametable RAM - external to PPU!
93   UINT8      *rom;
94   UINT8      *vrom;
95   UINT8      *vram;
96   UINT8      *wram;
97   UINT8      *ciram; //PPU nametable RAM - external to PPU!
6098   // Variables which can change
61   UINT8 mid_ram_enable;
99   UINT8      mid_ram_enable;
62100
63101   /* SRAM-related (we have two elements due to the init order, but it would be better to verify they both are still needed) */
64   UINT8 *battery_ram;
65   UINT8 battery_data[NES_BATTERY_SIZE];
102   UINT8      *battery_ram;
103   UINT8      battery_data[NES_BATTERY_SIZE];
66104
105   /***** Mapper-related variables *****/
106   
107   // common ones
108   int        IRQ_enable, IRQ_enable_latch;
109   UINT16     IRQ_count, IRQ_count_latch;
110   UINT8      IRQ_toggle;
111   UINT8      IRQ_reset;
112   UINT8      IRQ_status;
113   UINT8      IRQ_mode;
114   int        mult1, mult2;
115   
116   UINT8 mmc_chr_source;         // This is set at init to CHRROM or CHRRAM. a few mappers can swap between
117                           // the two (this is done in the specific handlers).
118   
119   UINT8 mmc_cmd1, mmc_cmd2;      // These represent registers where the mapper writes important values
120   UINT8 mmc_count;            // This is used as counter in mappers like 1 and 45
121   
122   int mmc_prg_base, mmc_prg_mask;   // MMC3 based multigame carts select a block of banks by using these (and then act like normal MMC3),
123   int mmc_chr_base, mmc_chr_mask;   // while MMC3 and clones (mapper 118 & 119) simply set them as 0 and 0xff resp.
124   
125   UINT8 prg_bank[4];            // Many mappers writes only some bits of the selected bank (for both PRG and CHR),
126   UINT8 vrom_bank[16];         // hence these are handy to latch bank values.
127
128   UINT8 extra_bank[16];         // some MMC3 clone have 2 series of PRG/CHR banks...
129                           // we collect them all here: first 4 elements PRG banks, then 6/8 CHR banks
130   
67131   /***** NES-cart related *****/
68132
69133   /* load-time cart variables which remain constant */
r7955r7956
85149   
86150   /***** FDS-floppy related *****/
87151
88   UINT8 *fds_data;
89   UINT8 fds_sides;
90   UINT8 *fds_ram;
152   UINT8   *fds_data;
153   UINT8   fds_sides;
154   UINT8   *fds_ram;
91155   
92156   /* Variables which can change */
93   UINT8 fds_motor_on;
94   UINT8 fds_door_closed;
95   UINT8 fds_current_side;
96   UINT32 fds_head_position;
97   UINT8 fds_status0;
98   UINT8 fds_read_mode;
99   UINT8 fds_write_reg;
157   UINT8   fds_motor_on;
158   UINT8   fds_door_closed;
159   UINT8   fds_current_side;
160   UINT32  fds_head_position;
161   UINT8   fds_status0;
162   UINT8   fds_read_mode;
163   UINT8   fds_write_reg;
164
165   /* these are used in the mapper 20 handlers */
166   int     fds_last_side;
167   int     fds_count;
168
169   // misc
170   write8_space_func   mmc_write_low;
171   read8_space_func    mmc_read_low;
172   write8_space_func   mmc_write_mid;
173   write8_space_func   mmc_write;
100174};
101175
102176
r7955r7956
105179WRITE8_HANDLER ( nes_IN0_w );
106180WRITE8_HANDLER ( nes_IN1_w );
107181
108extern unsigned char *nes_battery_ram;
109
110182/* protos */
111183
112184DEVICE_IMAGE_LOAD(nes_cart);
src/mess/machine/nes.c
r7955r7956
11#include "emu.h"
22#include "crsshair.h"
3#include "hash.h"
34#include "cpu/m6502/m6502.h"
45#include "video/ppu2c0x.h"
56#include "includes/nes.h"
67#include "machine/nes_mmc.h"
7#include "sound/nes_apu.h"
88#include "devices/cartslot.h"
99#include "devices/flopdrv.h"
10#include "formats/flopimg.h"
11#include "image.h"
12#include "hash.h"
1310
14
1511/***************************************************************************
1612    CONSTANTS
1713***************************************************************************/
r7955r7956
10096   /* Set up the mapper callbacks */
10197   if (state->format == 1)
10298   {
103      mmc_intf intf = { 0 };
104      const mmc *mapper;
105
106      mapper = nes_mapper_lookup(state->mapper);
99      const mmc *mapper = nes_mapper_lookup(state->mapper);
107100      if (mapper)
108101      {
109         intf.mmc_write_low = mapper->mmc_write_low;
110         intf.mmc_read_low = mapper->mmc_read_low;
111         intf.mmc_write_mid = mapper->mmc_write_mid;
112         intf.mmc_write = mapper->mmc_write;
113         nes_mapper_init(&intf);
102         state->mmc_write_low = mapper->mmc_write_low;
103         state->mmc_read_low = mapper->mmc_read_low;
104         state->mmc_write_mid = mapper->mmc_write_mid;
105         state->mmc_write = mapper->mmc_write;
114106         ppu_latch = mapper->ppu_latch;
115107      }
116108      else
117109      {
118110         logerror("Mapper %d is not yet supported, defaulting to no mapper.\n",state->mapper);
119         nes_mapper_init(&intf);
111         state->mmc_write_low = NULL;
112         state->mmc_read_low = NULL;
113         state->mmc_write_mid = NULL;
114         state->mmc_write = NULL;
115         ppu_latch = NULL;
120116      }
121117   }
122118   else if (state->format == 2)
123119   {
124      mmc_intf intf = { 0 };
125      const unif *board;
126
127      board = nes_unif_lookup(state->board);
120      const unif *board = nes_unif_lookup(state->board);
128121      if (board)
129122      {
130         intf.mmc_write_low = board->mmc_write_low;
131         intf.mmc_read_low = board->mmc_read_low;
132         intf.mmc_write_mid = board->mmc_write_mid;
133         intf.mmc_write = board->mmc_write;
134         nes_mapper_init(&intf);
123         state->mmc_write_low = board->mmc_write_low;
124         state->mmc_read_low = board->mmc_read_low;
125         state->mmc_write_mid = board->mmc_write_mid;
126         state->mmc_write = board->mmc_write;
135127         ppu_latch = board->ppu_latch;
136128      }
137129      else
138130      {
139131         logerror("Board %s is not yet supported, defaulting to no mapper.\n", state->board);
140         nes_mapper_init(&intf);
132         state->mmc_write_low = NULL;
133         state->mmc_read_low = NULL;
134         state->mmc_write_mid = NULL;
135         state->mmc_write = NULL;
141136         ppu_latch = NULL;
142137      }
143138   }
src/mess/machine/nes_unif.c
r7955r7956
168168
169169static void bmc_s24in1sc03_set_prg( running_machine *machine )
170170{
171   nes_state *state = (nes_state *)machine->driver_data;
171172   static const UINT8 masks[8] = {0x3f, 0x1f, 0x0f, 0x01, 0x03, 0x00, 0x00, 0x00};
172   UINT8 prg_flip = (mmc_cmd1 & 0x40) ? 2 : 0;
173   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
173174   int prg_base = bmc_s24in1sc03_reg[1] << 1;
174175   int prg_mask = masks[bmc_s24in1sc03_reg[0] & 0x07];
175176
176   prg8_89(machine, prg_base | (prg_bank[0 ^ prg_flip] & prg_mask));
177   prg8_ab(machine, prg_base | (prg_bank[1] & prg_mask));
178   prg8_cd(machine, prg_base | (prg_bank[2 ^ prg_flip] & prg_mask));
179   prg8_ef(machine, prg_base | (prg_bank[3] & prg_mask));
177   prg8_89(machine, prg_base | (state->prg_bank[0 ^ prg_flip] & prg_mask));
178   prg8_ab(machine, prg_base | (state->prg_bank[1] & prg_mask));
179   prg8_cd(machine, prg_base | (state->prg_bank[2 ^ prg_flip] & prg_mask));
180   prg8_ef(machine, prg_base | (state->prg_bank[3] & prg_mask));
180181}
181182
182183static void bmc_s24in1sc03_set_chr( running_machine *machine )
183184{
184   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
185   nes_state *state = (nes_state *)machine->driver_data;
186   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
185187   UINT8 chr = (bmc_s24in1sc03_reg[0] & 0x20) ? CHRRAM : CHRROM;
186188   int chr_base = (bmc_s24in1sc03_reg[2] << 3) & 0xf00;
187189
188   chr1_x(machine, chr_page ^ 0, chr_base | (vrom_bank[0] & ~0x01), chr);
189   chr1_x(machine, chr_page ^ 1, chr_base | (vrom_bank[0] |  0x01), chr);
190   chr1_x(machine, chr_page ^ 2, chr_base | (vrom_bank[1] & ~0x01), chr);
191   chr1_x(machine, chr_page ^ 3, chr_base | (vrom_bank[1] |  0x01), chr);
192   chr1_x(machine, chr_page ^ 4, chr_base | vrom_bank[2], chr);
193   chr1_x(machine, chr_page ^ 5, chr_base | vrom_bank[3], chr);
194   chr1_x(machine, chr_page ^ 6, chr_base | vrom_bank[4], chr);
195   chr1_x(machine, chr_page ^ 7, chr_base | vrom_bank[5], chr);
190   chr1_x(machine, chr_page ^ 0, chr_base | (state->vrom_bank[0] & ~0x01), chr);
191   chr1_x(machine, chr_page ^ 1, chr_base | (state->vrom_bank[0] |  0x01), chr);
192   chr1_x(machine, chr_page ^ 2, chr_base | (state->vrom_bank[1] & ~0x01), chr);
193   chr1_x(machine, chr_page ^ 3, chr_base | (state->vrom_bank[1] |  0x01), chr);
194   chr1_x(machine, chr_page ^ 4, chr_base | state->vrom_bank[2], chr);
195   chr1_x(machine, chr_page ^ 5, chr_base | state->vrom_bank[3], chr);
196   chr1_x(machine, chr_page ^ 6, chr_base | state->vrom_bank[4], chr);
197   chr1_x(machine, chr_page ^ 7, chr_base | state->vrom_bank[5], chr);
196198}
197199
198200static WRITE8_HANDLER( bmc_s24in1sc03_l_w )
r7955r7956
222224
223225static WRITE8_HANDLER( bmc_s24in1sc03_w )
224226{
227   nes_state *state = (nes_state *)space->machine->driver_data;
225228   UINT8 cmd, bmc_s24in1sc03_helper;
226229   LOG_MMC(("bmc_s24in1sc03_w offset: %04x, data: %02x\n", offset, data));
227230
228231   switch (offset & 0x6001)
229232   {
230233      case 0x0000:
231         bmc_s24in1sc03_helper = mmc_cmd1 ^ data;
232         mmc_cmd1 = data;
234         bmc_s24in1sc03_helper = state->mmc_cmd1 ^ data;
235         state->mmc_cmd1 = data;
233236
234237         /* Has PRG Mode changed? */
235238         if (bmc_s24in1sc03_helper & 0x40)
r7955r7956
241244         break;
242245
243246      case 0x0001:
244         cmd = mmc_cmd1 & 0x07;
247         cmd = state->mmc_cmd1 & 0x07;
245248         switch (cmd)
246249         {
247250            case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
248251            case 2: case 3: case 4: case 5:
249               vrom_bank[cmd] = data;
252               state->vrom_bank[cmd] = data;
250253               bmc_s24in1sc03_set_chr(space->machine);
251254               break;
252255            case 6:
253256            case 7:
254               prg_bank[cmd - 6] = data;
257               state->prg_bank[cmd - 6] = data;
255258               bmc_s24in1sc03_set_prg(space->machine);
256259               break;
257260         }
r7955r7956
275278
276279static WRITE8_HANDLER( bmc_t262_w )
277280{
281   nes_state *state = (nes_state *)space->machine->driver_data;
278282   UINT8 mmc_helper;
279283   LOG_MMC(("bmc_t262_w offset: %04x, data: %02x\n", offset, data));
280284
281   if (mmc_cmd2 || offset == 0)
285   if (state->mmc_cmd2 || offset == 0)
282286   {
283      mmc_cmd1 = (mmc_cmd1 & 0x38) | (data & 0x07);
284      prg16_89ab(space->machine, mmc_cmd1);
287      state->mmc_cmd1 = (state->mmc_cmd1 & 0x38) | (data & 0x07);
288      prg16_89ab(space->machine, state->mmc_cmd1);
285289   }
286290   else
287291   {
288      mmc_cmd2 = 1;
292      state->mmc_cmd2 = 1;
289293      set_nt_mirroring(space->machine, BIT(data, 1) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
290294      mmc_helper = ((offset >> 3) & 0x20) | ((offset >> 2) & 0x18);
291      mmc_cmd1 = mmc_helper | (mmc_cmd1 & 0x07);
292      prg16_89ab(space->machine, mmc_cmd1);
295      state->mmc_cmd1 = mmc_helper | (state->mmc_cmd1 & 0x07);
296      prg16_89ab(space->machine, state->mmc_cmd1);
293297      prg16_cdef(space->machine, mmc_helper | 0x07);
294298   }
295299}
r7955r7956
306310
307311static WRITE8_HANDLER( bmc_ws_m_w )
308312{
313   nes_state *state = (nes_state *)space->machine->driver_data;
309314   UINT8 mmc_helper;
310315   LOG_MMC(("bmc_ws_m_w offset: %04x, data: %02x\n", offset, data));
311316
r7955r7956
314319      switch (offset & 0x01)
315320      {
316321      case 0:
317         if (!mmc_cmd1)
322         if (!state->mmc_cmd1)
318323         {
319            mmc_cmd1 = data & 0x20;
324            state->mmc_cmd1 = data & 0x20;
320325            set_nt_mirroring(space->machine, BIT(data, 4) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
321326            mmc_helper = (~data & 0x08) >> 3;
322327            prg16_89ab(space->machine, data & ~mmc_helper);
r7955r7956
324329         }
325330         break;
326331      case 1:
327         if (!mmc_cmd1)
332         if (!state->mmc_cmd1)
328333         {
329334            chr8(space->machine, data, CHRROM);
330335         }
r7955r7956
366371
367372static void unl_8237_set_prg( running_machine *machine )
368373{
369   UINT8 prg_flip = (mmc_cmd1 & 0x40) ? 2 : 0;
374   nes_state *state = (nes_state *)machine->driver_data;
375   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
370376
371377   if (!(unl_8237_reg[0] & 0x80))
372378   {
373      prg8_89(machine, prg_bank[0 ^ prg_flip]);
374      prg8_ab(machine, prg_bank[1]);
375      prg8_cd(machine, prg_bank[2 ^ prg_flip]);
376      prg8_ef(machine, prg_bank[3]);
379      prg8_89(machine, state->prg_bank[0 ^ prg_flip]);
380      prg8_ab(machine, state->prg_bank[1]);
381      prg8_cd(machine, state->prg_bank[2 ^ prg_flip]);
382      prg8_ef(machine, state->prg_bank[3]);
377383   }
378384}
379385
380386static void unl_8237_set_chr( running_machine *machine )
381387{
382   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
388   nes_state *state = (nes_state *)machine->driver_data;
389   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
383390   UINT8 bank[8];
384391   int i;
385392
386393   for(i = 0; i < 6; i++)
387      bank[i] = vrom_bank[i] | ((unl_8237_reg[1] << 6) & 0x100);
394      bank[i] = state->vrom_bank[i] | ((unl_8237_reg[1] << 6) & 0x100);
388395
389396   chr1_x(machine, chr_page ^ 0, (bank[0] & ~0x01), CHRROM);
390397   chr1_x(machine, chr_page ^ 1, (bank[0] |  0x01), CHRROM);
r7955r7956
427434
428435static WRITE8_HANDLER( unl_8237_w )
429436{
437   nes_state *state = (nes_state *)space->machine->driver_data;
430438   UINT8 unl_8237_helper, cmd;
431439   static const UINT8 conv_table[8] = {0,2,6,1,7,3,4,5};
432440   LOG_MMC(("unl_8237_w offset: %04x, data: %02x\n", offset, data));
r7955r7956
437445      case 0x3000:
438446         unl_8237_reg[2] = 1;
439447         data = (data & 0xc0) | conv_table[data & 0x07];
440         unl_8237_helper = mmc_cmd1 ^ data;
441         mmc_cmd1 = data;
448         unl_8237_helper = state->mmc_cmd1 ^ data;
449         state->mmc_cmd1 = data;
442450
443451         /* Has PRG Mode changed? */
444452         if (unl_8237_helper & 0x40)
r7955r7956
454462         if (unl_8237_reg[2])
455463         {
456464            unl_8237_reg[2] = 0;
457            cmd = mmc_cmd1 & 0x07;
465            cmd = state->mmc_cmd1 & 0x07;
458466            switch (cmd)
459467            {
460468               case 0: case 1:
461469               case 2: case 3: case 4: case 5:
462                  vrom_bank[cmd] = data;
470                  state->vrom_bank[cmd] = data;
463471                  unl_8237_set_chr(space->machine);
464472                  break;
465473               case 6:
466474               case 7:
467                  prg_bank[cmd - 6] = data;
475                  state->prg_bank[cmd - 6] = data;
468476                  unl_8237_set_prg(space->machine);
469477                  break;
470478            }
r7955r7956
499507
500508static void unl_ax5705_set_prg( running_machine *machine )
501509{
502   prg8_89(machine, prg_bank[0]);
503   prg8_ab(machine, prg_bank[1]);
510   nes_state *state = (nes_state *)machine->driver_data;
511   prg8_89(machine, state->prg_bank[0]);
512   prg8_ab(machine, state->prg_bank[1]);
504513}
505514
506515static WRITE8_HANDLER( unl_ax5705_w )
507516{
517   nes_state *state = (nes_state *)space->machine->driver_data;
508518   UINT8 bank;
509519   LOG_MMC(("unl_ax5705_w offset: %04x, data: %02x\n", offset, data));
510520
511521   switch (offset & 0x700f)
512522   {
513523   case 0x0000:
514      prg_bank[0] = (data & 0x05) | ((data & 0x08) >> 2) | ((data & 0x02) << 2);
524      state->prg_bank[0] = (data & 0x05) | ((data & 0x08) >> 2) | ((data & 0x02) << 2);
515525      unl_ax5705_set_prg(space->machine);
516526      break;
517527   case 0x0008:
518528      set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
519529      break;
520530   case 0x2000:
521      prg_bank[1] = (data & 0x05) | ((data & 0x08) >> 2) | ((data & 0x02) << 2);
531      state->prg_bank[1] = (data & 0x05) | ((data & 0x08) >> 2) | ((data & 0x02) << 2);
522532      unl_ax5705_set_prg(space->machine);
523533      break;
524534   /* CHR banks 0, 1, 4, 5 */
r7955r7956
527537   case 0x4008:
528538   case 0x400a:
529539      bank = ((offset & 0x4000) ? 4 : 0) + ((offset & 0x0002) ? 1 : 0);
530      vrom_bank[bank] = (vrom_bank[bank] & 0xf0) | (data & 0x0f);
531      chr1_x(space->machine, bank, vrom_bank[bank], CHRROM);
540      state->vrom_bank[bank] = (state->vrom_bank[bank] & 0xf0) | (data & 0x0f);
541      chr1_x(space->machine, bank, state->vrom_bank[bank], CHRROM);
532542      break;
533543   case 0x2009:
534544   case 0x200b:
535545   case 0x4009:
536546   case 0x400b:
537547      bank = ((offset & 0x4000) ? 4 : 0) + ((offset & 0x0002) ? 1 : 0);
538      vrom_bank[bank] = (vrom_bank[bank] & 0x0f) | ((data & 0x04) << 3) | ((data & 0x02) << 5) | ((data & 0x09) << 4);
539      chr1_x(space->machine, bank, vrom_bank[bank], CHRROM);
548      state->vrom_bank[bank] = (state->vrom_bank[bank] & 0x0f) | ((data & 0x04) << 3) | ((data & 0x02) << 5) | ((data & 0x09) << 4);
549      chr1_x(space->machine, bank, state->vrom_bank[bank], CHRROM);
540550      break;
541551   /* CHR banks 2, 3, 6, 7 */
542552   case 0x4000:
r7955r7956
544554   case 0x6000:
545555   case 0x6002:
546556      bank = 2 + ((offset & 0x2000) ? 4 : 0) + ((offset & 0x0002) ? 1 : 0);
547      vrom_bank[bank] = (vrom_bank[bank] & 0xf0) | (data & 0x0f);
548      chr1_x(space->machine, bank, vrom_bank[bank], CHRROM);
557      state->vrom_bank[bank] = (state->vrom_bank[bank] & 0xf0) | (data & 0x0f);
558      chr1_x(space->machine, bank, state->vrom_bank[bank], CHRROM);
549559      break;
550560   case 0x4001:
551561   case 0x4003:
552562   case 0x6001:
553563   case 0x6003:
554564      bank = 2 + ((offset & 0x2000) ? 4 : 0) + ((offset & 0x0002) ? 1 : 0);
555      vrom_bank[bank] = (vrom_bank[bank] & 0x0f) | ((data & 0x04) << 3) | ((data & 0x02) << 5) | ((data & 0x09) << 4);
556      chr1_x(space->machine, bank, vrom_bank[bank], CHRROM);
565      state->vrom_bank[bank] = (state->vrom_bank[bank] & 0x0f) | ((data & 0x04) << 3) | ((data & 0x02) << 5) | ((data & 0x09) << 4);
566      chr1_x(space->machine, bank, state->vrom_bank[bank], CHRROM);
557567      break;
558568   }
559569}
src/mess/machine/nes_mmc.c
r7955r7956
136136#define LOG_MMC(x) do { if (VERBOSE) logerror x; } while (0)
137137#define LOG_FDS(x) do { if (VERBOSE) logerror x; } while (0)
138138
139/*PPU fast banking constants and structures */
140139
141#define CHRROM 0
142#define CHRRAM 1
143
144typedef struct
145{
146   int source;   //defines source of base pointer
147   int origin; //defines offset of 0x400 byte segment at base pointer
148   UINT8* access;   //source translated + origin -> valid pointer!
149} chr_bank;
150
151/*PPU nametable fast banking constants and structures */
152
153#define CIRAM 0
154#define ROM 1
155#define EXRAM 2
156#define MMC5FILL 3
157#define CART_NTRAM 4
158
159typedef struct
160{
161   int source;      /* defines source of base pointer */
162   int origin;      /* defines offset of 0x400 byte segment at base pointer */
163   int writable;   /* ExRAM, at least, can be write-protected AND used as nametable */
164   UINT8* access;   /* direct access when possible */
165} name_table;
166
167/* Global variables */
168static chr_bank chr_map[8];  //quick banking structure, because some of this changes multiple times per scanline!
169static name_table nt_page[4];  //quick banking structure for a maximum of 4K of RAM/ROM/ExRAM
170
171static int IRQ_enable, IRQ_enable_latch;
172static UINT16 IRQ_count, IRQ_count_latch;
173static UINT8 IRQ_toggle;
174static UINT8 IRQ_reset;
175static UINT8 IRQ_status;
176static UINT8 IRQ_mode;
177
178//int MMC1_extended;    /* 0 = normal MMC1 cart, 1 = 512k MMC1, 2 = 1024k MMC1 */
179
180static write8_space_func mmc_write_low;
181static read8_space_func mmc_read_low;
182static write8_space_func mmc_write_mid;
183static write8_space_func mmc_write;
184
185static int mult1, mult2;
186
187/* common local variables */
188static UINT8 mmc_chr_source;      // This is set at init to CHRROM or CHRRAM. a few mappers can swap between
189                  // the two (this is done in the specific handlers).
190
191static UINT8 mmc_cmd1, mmc_cmd2;   // These represent registers where the mapper writes important values
192static UINT8 mmc_count;         // This is used as counter in mappers like 1 and 45
193
194static int mmc_prg_base, mmc_prg_mask;   // MMC3 based multigame carts select a block of banks by using these (and then act like normal MMC3),
195static int mmc_chr_base, mmc_chr_mask;   // while MMC3 and clones (mapper 118 & 119) simply set them as 0 and 0xff resp.
196
197static UINT8 prg_bank[4];         // Many mappers writes only some bits of the selected bank (for both PRG and CHR),
198static UINT8 vrom_bank[16];         // hence these are handy to latch bank values.
199
200
201140// FIXME: mapper specific variables should be unified as much as possible to make easier future implementation of save states
202141/* Local variables (a few more are defined just before handlers that use them...) */
203142
r7955r7956
205144
206145static UINT8 MMC2_regs[4];   // these replace bank0/bank0_hi/bank1/bank1_hi
207146
208static UINT8* extended_ntram;
209
210
211147static int MMC5_rom_bank_mode;
212148static int MMC5_vrom_bank_mode;
213149static int MMC5_vram_protect;
214150static int MMC5_scanline;
215static int MMC5_floodtile;
216static int MMC5_floodattr;
217UINT8 nes_mmc5_vram[0x400];
218int nes_mmc5_vram_control;
219151
220// these might be unified in single mmc_reg[] array, together with mmc_cmd1 & mmc_cmd2
221// but be careful that MMC3 clones often use mmc_cmd1/mmc_cmd2 (from base MMC3) AND additional regs below!
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!
222154static UINT8 mapper45_reg[4];
223155static UINT8 mapper51_reg[3];
224156static UINT8 mapper83_reg[10];
r7955r7956
243175static UINT8 bmc_s24in1sc03_reg[3];
244176
245177
246static UINT8 extra_bank[16];   // some MMC3 clone have 2 series of PRG/CHR banks...
247               // we collect them all here: first 4 elements PRG banks, then 6/8 CHR banks
248
249178// mapper 68 needs these for NT mirroring!
250179static int m68_mirror;
251180static int m0, m1;
252181
253
254// FDS / mapper 20 need these for disks (to be added to fds struct?)
255static int fds_last_side;
256static int fds_count;
257
258182// Famicom Jump II is identified as Mapper 153, but it works in a completely different way.
259183// in particular, it requires these (not needed by other Mapper 153 games)
260184static UINT8 map153_reg[8];
r7955r7956
286210
287211WRITE8_HANDLER( nes_chr_w )
288212{
213   nes_state *state = (nes_state *)space->machine->driver_data;   
289214   int bank = offset >> 10;
290   if(chr_map[bank].source == CHRRAM)
215
216   if (state->chr_map[bank].source == CHRRAM)
291217   {
292      chr_map[bank].access[offset & 0x3ff] = data;
218      state->chr_map[bank].access[offset & 0x3ff] = data;
293219   }
294220}
295221
296222READ8_HANDLER( nes_chr_r )
297223{
224   nes_state *state = (nes_state *)space->machine->driver_data;
298225   int bank = offset >> 10;
299   return chr_map[bank].access[offset & 0x3ff];
226
227   return state->chr_map[bank].access[offset & 0x3ff];
300228}
301229
302230WRITE8_HANDLER( nes_nt_w )
303231{
232   nes_state *state = (nes_state *)space->machine->driver_data;
304233   int page = ((offset & 0xc00) >> 10);
305234
306   if (nt_page[page].writable == 0)
235   if (state->nt_page[page].writable == 0)
307236      return;
308237
309   nt_page[page].access[offset & 0x3ff] = data;
238   state->nt_page[page].access[offset & 0x3ff] = data;
310239}
311240
312241READ8_HANDLER( nes_nt_r )
313242{
243   nes_state *state = (nes_state *)space->machine->driver_data;
314244   int page = ((offset & 0xc00) >> 10);
315   if (nt_page[page].source == MMC5FILL)
245
246   if (state->nt_page[page].source == MMC5FILL)
316247   {
317248      if ((offset & 0x3ff) >= 0x3c0)
318         return MMC5_floodattr;
249         return state->MMC5_floodattr;
319250
320      return MMC5_floodtile;
251      return state->MMC5_floodtile;
321252   }
322   return nt_page[page].access[offset & 0x3ff];
253   return state->nt_page[page].access[offset & 0x3ff];
323254}
324255
325256WRITE8_HANDLER( nes_low_mapper_w )
326257{
327   if (mmc_write_low)
328      (*mmc_write_low)(space, offset, data);
258   nes_state *state = (nes_state *)space->machine->driver_data;
259
260   if (state->mmc_write_low)
261      (*state->mmc_write_low)(space, offset, data);
329262   else
330263   {
331264      logerror("Unimplemented LOW mapper write, offset: %04x, data: %02x\n", offset, data);
r7955r7956
341274
342275READ8_HANDLER( nes_low_mapper_r )
343276{
344   if (mmc_read_low)
345      return (*mmc_read_low)(space, offset);
277   nes_state *state = (nes_state *)space->machine->driver_data;
278
279   if (state->mmc_read_low)
280      return (*state->mmc_read_low)(space, offset);
346281   else
347282      logerror("low mapper area read, addr: %04x\n", offset + 0x4100);
348283
r7955r7956
353288{
354289   nes_state *state = (nes_state *)space->machine->driver_data;
355290   
356   if (mmc_write_mid)
357      (*mmc_write_mid)(space, offset, data);
291   if (state->mmc_write_mid)
292      (*state->mmc_write_mid)(space, offset, data);
358293   else if (state->mid_ram_enable)
359294      state->battery_ram[offset] = data;
360295   else
r7955r7956
382317
383318WRITE8_HANDLER( nes_mapper_w )
384319{
385   if (mmc_write)
386      (*mmc_write)(space, offset, data);
320   nes_state *state = (nes_state *)space->machine->driver_data;
321
322   if (state->mmc_write)
323      (*state->mmc_write)(space, offset, data);
387324   else
388325   {
389326      logerror("Unimplemented mapper write, offset: %04x, data: %02x\n", offset, data);
r7955r7956
523460   bank &= (state->chr_chunks - 1);
524461   for (i = 0; i < 8; i++)
525462   {
526      chr_map[i].source = source;
527      chr_map[i].origin = (bank * 0x2000) + (i * 0x400); // for save state uses!
463      state->chr_map[i].source = source;
464      state->chr_map[i].origin = (bank * 0x2000) + (i * 0x400); // for save state uses!
528465
529466      if (source == CHRRAM)
530         chr_map[i].access = &state->vram[chr_map[i].origin];
467         state->chr_map[i].access = &state->vram[state->chr_map[i].origin];
531468      else
532         chr_map[i].access = &state->vrom[chr_map[i].origin];
469         state->chr_map[i].access = &state->vrom[state->chr_map[i].origin];
533470   }
534471}
535472
r7955r7956
541478   bank &= ((state->chr_chunks << 1) - 1);
542479   for (i = 0; i < 4; i++)
543480   {
544      chr_map[i + start].source = source;
545      chr_map[i + start].origin = (bank * 0x1000) + (i * 0x400); // for save state uses!
481      state->chr_map[i + start].source = source;
482      state->chr_map[i + start].origin = (bank * 0x1000) + (i * 0x400); // for save state uses!
546483
547484      if (source == CHRRAM)
548         chr_map[i + start].access = &state->vram[chr_map[i + start].origin];
485         state->chr_map[i + start].access = &state->vram[state->chr_map[i + start].origin];
549486      else
550         chr_map[i + start].access = &state->vrom[chr_map[i + start].origin];
487         state->chr_map[i + start].access = &state->vrom[state->chr_map[i + start].origin];
551488   }
552489}
553490
r7955r7956
569506   bank &= ((state->chr_chunks << 2) - 1);
570507   for (i = 0; i < 2; i++)
571508   {
572      chr_map[i + start].source = source;
573      chr_map[i + start].origin = (bank * 0x800) + (i * 0x400);
509      state->chr_map[i + start].source = source;
510      state->chr_map[i + start].origin = (bank * 0x800) + (i * 0x400);
574511
575512      if (source == CHRRAM)
576         chr_map[i + start].access = &state->vram[chr_map[i + start].origin];
513         state->chr_map[i + start].access = &state->vram[state->chr_map[i + start].origin];
577514      else
578         chr_map[i + start].access = &state->vrom[chr_map[i + start].origin];
515         state->chr_map[i + start].access = &state->vrom[state->chr_map[i + start].origin];
579516   }
580517}
581518
r7955r7956
603540{
604541   nes_state *state = (nes_state *)machine->driver_data;
605542
606   chr_map[start].source = source;
543   state->chr_map[start].source = source;
607544   bank &= ((state->chr_chunks << 3) - 1);
608   chr_map[start].origin = bank * 0x400;
545   state->chr_map[start].origin = bank * 0x400;
609546
610547   if (source == CHRRAM)
611      chr_map[start].access = &state->vram[chr_map[start].origin];
548      state->chr_map[start].access = &state->vram[state->chr_map[start].origin];
612549   else
613      chr_map[start].access = &state->vrom[chr_map[start].origin];
550      state->chr_map[start].access = &state->vrom[state->chr_map[start].origin];
614551}
615552
616553static void chr1_0 (running_machine *machine, int bank, int source)
r7955r7956
664601   switch (source)
665602   {
666603      case CART_NTRAM:
667         base_ptr = extended_ntram;
604         base_ptr = state->extended_ntram;
668605         break;
669606      case MMC5FILL:
670607         base_ptr = NULL;
r7955r7956
673610         base_ptr = state->vrom;
674611         break;
675612      case EXRAM:
676         base_ptr = nes_mmc5_vram;
613         base_ptr = state->mmc5_vram;
677614         break;
678615      case CIRAM:
679616      default:
r7955r7956
682619   }
683620
684621   page &= 3; /* mask down to the 4 logical pages */
685   nt_page[page].source = source;
686   if (NULL != base_ptr)
622   state->nt_page[page].source = source;
623
624   if (base_ptr != NULL)
687625   {
688      nt_page[page].origin = bank * 0x400;
689      nt_page[page].access = base_ptr + nt_page[page].origin;
626      state->nt_page[page].origin = bank * 0x400;
627      state->nt_page[page].access = base_ptr + state->nt_page[page].origin;
690628   }
691   nt_page[page].writable = writable;
629
630   state->nt_page[page].writable = writable;
692631}
693632
694633void set_nt_mirroring( running_machine *machine, int mirroring )
r7955r7956
778717         break;
779718      case 1:
780719      case 155:
781         mmc_cmd1 = 0;
782         mmc_count = 0;
720         state->mmc_cmd1 = 0;
721         state->mmc_count = 0;
783722         MMC1_regs[0] = 0x0f;
784723         MMC1_regs[1] = MMC1_regs[2] = MMC1_regs[3] = 0;
785724         set_nt_mirroring(machine, PPU_MIRROR_HORZ);
786         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
725         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
787726         MMC1_set_chr(machine);
788727         MMC1_set_prg(machine);
789728         break;
r7955r7956
806745         /* Only MMC3 and clones seem to use 4-screen! */
807746         /* hardware uses an 8K RAM chip, but can't address the high 4K! */
808747         /* that much is documented for Nintendo Gauntlet boards */
809         if(state->four_screen_vram)
748         if (state->four_screen_vram)
810749         {
811            extended_ntram = auto_alloc_array(machine, UINT8, 0x2000);
750            state->extended_ntram = auto_alloc_array(machine, UINT8, 0x2000);
812751            set_nt_page(machine, 0, CART_NTRAM, 0, 1);
813752            set_nt_page(machine, 1, CART_NTRAM, 1, 1);
814753            set_nt_page(machine, 2, CART_NTRAM, 2, 1);
r7955r7956
825764      case 196:
826765      case 245:
827766      case 250:
828         prg_bank[0] = prg_bank[2] = 0xfe;   // prg_bank[2] & prg_bank[3] remain always the same in most MMC3 variants
829         prg_bank[1] = prg_bank[3] = 0xff;   // but some pirate clone mappers change them after writing certain registers
830         mmc_cmd1 = 0;
831         mmc_cmd2 = 0x80;
832         mmc_prg_base = mmc_chr_base = 0;
833         mmc_prg_mask = mmc_chr_mask = 0xff;   // these could be init'ed as xxx_chunks-1 and they would work the same
834         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
835         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
836         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
767         state->prg_bank[0] = state->prg_bank[2] = 0xfe;   // prg_bank[2] & prg_bank[3] remain always the same in most MMC3 variants
768         state->prg_bank[1] = state->prg_bank[3] = 0xff;   // but some pirate clone mappers change them after writing certain registers
769         state->mmc_cmd1 = 0;
770         state->mmc_cmd2 = 0x80;
771         state->mmc_prg_base = state->mmc_chr_base = 0;
772         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;   // these could be init'ed as xxx_chunks-1 and they would work the same
773         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
774         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
775         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
837776         break;
838777      case 5:
839778         /* Can switch 8k prg banks, but they are saved as 16k in size */
r7955r7956
845784         prg16_cdef(machine, state->prg_chunks - 1);
846785         break;
847786      case 6:
848         mmc_cmd1 = 0;
849         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
787         state->mmc_cmd1 = 0;
788         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
850789         prg16_89ab(machine, 0);
851790         prg16_cdef(machine, 7);
852791         break;
r7955r7956
863802      case 9:
864803         MMC2_regs[0] = MMC2_regs[2] = 0;
865804         MMC2_regs[1] = MMC2_regs[3] = 0;
866         mmc_cmd1 = mmc_cmd2 = 0xfe;
805         state->mmc_cmd1 = state->mmc_cmd2 = 0xfe;
867806         prg8_89(machine, 0);
868807         //ugly hack to deal with iNES header usage of chunk count.
869808         prg8_ab(machine, (state->prg_chunks << 1) - 3);
r7955r7956
874813         /* Reset VROM latches */
875814         MMC2_regs[0] = MMC2_regs[2] = 0;
876815         MMC2_regs[1] = MMC2_regs[3] = 0;
877         mmc_cmd1 = mmc_cmd2 = 0xfe;
816         state->mmc_cmd1 = state->mmc_cmd2 = 0xfe;
878817         prg16_89ab(machine, 0);
879818         prg16_cdef(machine, state->prg_chunks - 1);
880819         break;
881820      case 11:
882         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;   // NINA-07 has no CHRROM
821         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;   // NINA-07 has no CHRROM
883822         prg32(machine, 0);
884823         break;
885824      case 14:
886         extra_bank[2] = 0xfe;
887         extra_bank[3] = 0xff;
888         extra_bank[0] = extra_bank[1] = extra_bank[4] = extra_bank[5] = extra_bank[6] = 0;
889         extra_bank[7] = extra_bank[8] = extra_bank[9] = extra_bank[0xa] = extra_bank[0xb] = 0;
825         state->extra_bank[2] = 0xfe;
826         state->extra_bank[3] = 0xff;
827         state->extra_bank[0] = state->extra_bank[1] = state->extra_bank[4] = state->extra_bank[5] = state->extra_bank[6] = 0;
828         state->extra_bank[7] = state->extra_bank[8] = state->extra_bank[9] = state->extra_bank[0xa] = state->extra_bank[0xb] = 0;
890829         map14_reg[0] = map14_reg[1] = 0;
891         prg_bank[0] = prg_bank[2] = 0xfe;
892         prg_bank[1] = prg_bank[3] = 0xff;
893         mmc_cmd1 = 0;
894         mmc_cmd2 = 0x80;
895         mmc_prg_base = mmc_chr_base = 0;
896         mmc_prg_mask = mmc_chr_mask = 0xff;
897         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
898         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
899         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
830         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
831         state->prg_bank[1] = state->prg_bank[3] = 0xff;
832         state->mmc_cmd1 = 0;
833         state->mmc_cmd2 = 0x80;
834         state->mmc_prg_base = state->mmc_chr_base = 0;
835         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
836         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
837         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
838         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
900839         break;
901840      case 15:
902841         set_nt_mirroring(machine, PPU_MIRROR_VERT);
r7955r7956
907846      case 18:
908847      case 157:
909848      case 159:
910         mmc_cmd1 = 0;
849         state->mmc_cmd1 = 0;
911850         prg16_89ab(machine, 0);
912851         prg16_cdef(machine, state->prg_chunks - 1);
913         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
852         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
914853         break;
915854      case 19:
916         mmc_cmd1 = 0;
855         state->mmc_cmd1 = 0;
917856         prg16_89ab(machine, 0);
918857         prg16_cdef(machine, state->prg_chunks - 1);
919         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
858         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
920859         set_nt_mirroring(machine, PPU_MIRROR_VERT);
921860         break;
922861      case 20:
923         fds_last_side = 0;
924         fds_count = 0;
862         state->fds_last_side = 0;
863         state->fds_count = 0;
925864         state->fds_motor_on = 0;
926865         state->fds_door_closed = 0;
927866         state->fds_current_side = 1;
r7955r7956
947886      case 207:
948887      case 222:
949888      case 252:
950         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
889         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
951890         prg16_89ab(machine, 0);
952891         prg16_cdef(machine, state->prg_chunks - 1);
953892         break;
954893      case 32:
955         mmc_cmd1 = 0;
894         state->mmc_cmd1 = 0;
956895         prg16_89ab(machine, 0);
957896         prg16_cdef(machine, state->prg_chunks - 1);
958897         if (state->crc_hack)
r7955r7956
965904         prg32(machine, 0xff);
966905         break;
967906      case 37:
968         prg_bank[0] = prg_bank[2] = 0xfe;
969         prg_bank[1] = prg_bank[3] = 0xff;
970         mmc_cmd1 = 0;
971         mmc_cmd2 = 0x80;
972         mmc_prg_base = mmc_chr_base = 0;
973         mmc_prg_mask = 0x07;
974         mmc_chr_mask = 0x7f;
975         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
976         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
977         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
907         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
908         state->prg_bank[1] = state->prg_bank[3] = 0xff;
909         state->mmc_cmd1 = 0;
910         state->mmc_cmd2 = 0x80;
911         state->mmc_prg_base = state->mmc_chr_base = 0;
912         state->mmc_prg_mask = 0x07;
913         state->mmc_chr_mask = 0x7f;
914         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
915         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
916         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
978917         break;
979918      case 40:
980919         prg8_67(machine, 0xfe);
r7955r7956
984923         prg8_ef(machine, 0xff);
985924         break;
986925      case 41:
987         mmc_cmd1 = 0;
926         state->mmc_cmd1 = 0;
988927         prg32(machine, 0);
989928         break;
990929      case 42:
r7955r7956
997936      case 44:
998937      case 47:
999938      case 49:
1000         prg_bank[0] = prg_bank[2] = 0xfe;
1001         prg_bank[1] = prg_bank[3] = 0xff;
1002         mmc_cmd1 = 0;
1003         mmc_cmd2 = 0x80;
1004         mmc_prg_base = mmc_chr_base = 0;
1005         mmc_prg_mask = 0x0f;
1006         mmc_chr_mask = 0x7f;
1007         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1008         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1009         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
939         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
940         state->prg_bank[1] = state->prg_bank[3] = 0xff;
941         state->mmc_cmd1 = 0;
942         state->mmc_cmd2 = 0x80;
943         state->mmc_prg_base = state->mmc_chr_base = 0;
944         state->mmc_prg_mask = 0x0f;
945         state->mmc_chr_mask = 0x7f;
946         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
947         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
948         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1010949         break;
1011950      case 45:
1012         prg_bank[0] = prg_bank[2] = 0xfe;
1013         prg_bank[1] = prg_bank[3] = 0xff;
1014         mmc_cmd1 = 0;
1015         mmc_cmd2 = 0x80;
1016         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
951         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
952         state->prg_bank[1] = state->prg_bank[3] = 0xff;
953         state->mmc_cmd1 = 0;
954         state->mmc_cmd2 = 0x80;
955         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1017956         mapper45_reg[0] = mapper45_reg[1] = mapper45_reg[2] = mapper45_reg[3] = 0;
1018         mmc_prg_base = 0x30;
1019         mmc_prg_mask = 0x3f;
1020         mmc_chr_base = 0;
1021         mmc_chr_mask = 0x7f;
1022         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1023         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
957         state->mmc_prg_base = 0x30;
958         state->mmc_prg_mask = 0x3f;
959         state->mmc_chr_base = 0;
960         state->mmc_chr_mask = 0x7f;
961         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
962         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
1024963         memory_set_bankptr(machine, "bank5", state->wram);
1025964         break;
1026965      case 46:
r7955r7956
1045984         mapper51_set_banks(machine);
1046985         break;
1047986      case 52:
1048         prg_bank[0] = 0xfe;
1049         prg_bank[2] = 0xfe;
1050         prg_bank[1] = 0xff;
1051         prg_bank[3] = 0xff;
1052         mmc_cmd1 = 0;
1053         mmc_cmd2 = 0x80;
987         state->prg_bank[0] = 0xfe;
988         state->prg_bank[2] = 0xfe;
989         state->prg_bank[1] = 0xff;
990         state->prg_bank[3] = 0xff;
991         state->mmc_cmd1 = 0;
992         state->mmc_cmd2 = 0x80;
1054993         map52_reg_written = 0;
1055         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1056         mmc_prg_base = 0;
1057         mmc_prg_mask = 0x1f;
1058         mmc_chr_base = 0;
1059         mmc_chr_mask = 0xff;
1060         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1061         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
994         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
995         state->mmc_prg_base = 0;
996         state->mmc_prg_mask = 0x1f;
997         state->mmc_chr_base = 0;
998         state->mmc_chr_mask = 0xff;
999         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1000         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
10621001         memory_set_bankptr(machine, "bank5", state->wram);
10631002         break;
10641003      case 57:
1065         mmc_cmd1 = 0x00;
1066         mmc_cmd2 = 0x00;
1004         state->mmc_cmd1 = 0x00;
1005         state->mmc_cmd2 = 0x00;
10671006         prg16_89ab(machine, 0);
10681007         prg16_cdef(machine, 0);
10691008         chr8(machine, 0, CHRROM);
r7955r7956
10791018         break;
10801019      case 64:
10811020      case 158:
1082         mmc_cmd1 = 0;
1021         state->mmc_cmd1 = 0;
10831022         prg16_89ab(machine, state->prg_chunks - 1);
10841023         prg16_cdef(machine, state->prg_chunks - 1);
10851024         break;
r7955r7956
11151054         chr2_2(machine, 1, CHRROM);
11161055         chr2_4(machine, 2, CHRROM);
11171056         chr2_6(machine, 3, CHRROM);
1118         mmc_cmd1 = 0;
1057         state->mmc_cmd1 = 0;
11191058         break;
11201059      case 77:
11211060         prg32(machine, 0);
r7955r7956
11681107         prg16_cdef(machine, state->prg_chunks - 1);
11691108         break;
11701109      case 112:
1171         mmc_cmd1 = 0;
1110         state->mmc_cmd1 = 0;
11721111         prg16_89ab(machine, 0);
11731112         prg16_cdef(machine, state->prg_chunks - 1);
11741113         break;
r7955r7956
12011140         break;
12021141      case 114:
12031142         map114_reg = map114_reg_enabled = 0;
1204         prg_bank[0] = prg_bank[2] = 0xfe;
1205         prg_bank[1] = prg_bank[3] = 0xff;
1206         mmc_cmd1 = 0;
1207         mmc_cmd2 = 0x80;
1208         mmc_prg_base = mmc_chr_base = 0;
1209         mmc_prg_mask = mmc_chr_mask = 0xff;
1210         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1211         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1212         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1143         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1144         state->prg_bank[1] = state->prg_bank[3] = 0xff;
1145         state->mmc_cmd1 = 0;
1146         state->mmc_cmd2 = 0x80;
1147         state->mmc_prg_base = state->mmc_chr_base = 0;
1148         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1149         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1150         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1151         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
12131152         break;
12141153      case 115:
12151154         mapper115_reg[0] = 0;
1216         prg_bank[0] = prg_bank[2] = 0xfe;
1217         prg_bank[1] = prg_bank[3] = 0xff;
1218         mmc_cmd1 = 0;
1219         mmc_cmd2 = 0x80;
1220         mmc_prg_base = mmc_chr_base = 0;
1221         mmc_prg_mask = mmc_chr_mask = 0xff;
1222         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1223         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1224         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1155         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1156         state->prg_bank[1] = state->prg_bank[3] = 0xff;
1157         state->mmc_cmd1 = 0;
1158         state->mmc_cmd2 = 0x80;
1159         state->mmc_prg_base = state->mmc_chr_base = 0;
1160         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1161         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1162         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1163         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
12251164         break;
12261165      case 121:
1227         prg_bank[0] = prg_bank[2] = 0xfe;
1228         prg_bank[1] = prg_bank[3] = 0xff;
1166         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1167         state->prg_bank[1] = state->prg_bank[3] = 0xff;
12291168         mapper121_reg[0] = mapper121_reg[1] = mapper121_reg[2] = 0;
1230         mmc_cmd1 = 0;
1231         mmc_cmd2 = 0x80;
1232         mmc_prg_base = mmc_chr_base = 0;
1233         mmc_prg_mask = mmc_chr_mask = 0xff;
1234         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1235         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1236         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
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);
12371176         break;
12381177      case 132:
12391178      case 172:
r7955r7956
12421181         prg32(machine, 0);
12431182         break;
12441183      case 134:
1245         prg_bank[0] = prg_bank[2] = 0xfe;
1246         prg_bank[1] = prg_bank[3] = 0xff;
1247         mmc_cmd1 = 0;
1248         mmc_cmd2 = 0x80;
1249         mmc_prg_base = mmc_chr_base = 0;
1250         mmc_prg_mask = 0x1f;
1251         mmc_chr_mask = 0xff;
1252         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1253         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1254         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1184         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1185         state->prg_bank[1] = state->prg_bank[3] = 0xff;
1186         state->mmc_cmd1 = 0;
1187         state->mmc_cmd2 = 0x80;
1188         state->mmc_prg_base = state->mmc_chr_base = 0;
1189         state->mmc_prg_mask = 0x1f;
1190         state->mmc_chr_mask = 0xff;
1191         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1192         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1193         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
12551194         break;
12561195      case 166:
12571196         subor_reg[0] = subor_reg[1] = subor_reg[2] = subor_reg[3] = 0;
r7955r7956
12641203         prg16_cdef(machine, 0x20);
12651204         break;
12661205      case 136:
1267         mmc_cmd1 = 0;
1206         state->mmc_cmd1 = 0;
12681207         prg32(machine, 0);
12691208         break;
12701209      case 137:
1271         mmc_cmd1 = 0;
1210         state->mmc_cmd1 = 0;
12721211         prg32(machine, 0);
12731212         chr8(machine, state->chr_chunks - 1, CHRROM);
12741213         set_nt_mirroring(machine, PPU_MIRROR_VERT);
r7955r7956
12761215      case 138:
12771216      case 139:
12781217      case 141:
1279         mmc_cmd1 = 0;
1280         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1218         state->mmc_cmd1 = 0;
1219         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
12811220         prg32(machine, 0);
1282         chr8(machine, 0, mmc_chr_source);
1221         chr8(machine, 0, state->mmc_chr_source);
12831222         set_nt_mirroring(machine, PPU_MIRROR_VERT);
12841223         break;
12851224      case 143:
r7955r7956
12971236         chr8(machine, 0, CHRROM);
12981237         break;
12991238      case 150:
1300         mmc_cmd1 = 0;
1239         state->mmc_cmd1 = 0;
13011240         prg32(machine, 0);
13021241         chr8(machine, 0, CHRROM);
13031242         set_nt_mirroring(machine, PPU_MIRROR_VERT);
r7955r7956
13081247         chr8(machine, 0, CHRROM);
13091248         break;
13101249      case 153:
1311         mmc_cmd1 = 0;
1250         state->mmc_cmd1 = 0;
13121251         prg16_89ab(machine, 0);
13131252         prg16_cdef(machine, state->prg_chunks - 1);
1314         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1253         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
13151254         // Famicom Jump II needs also the following!
13161255         for (i = 0; i < 8; i++)
13171256            map153_reg[i] = 0;
r7955r7956
13371276         prg32(machine, (state->prg_chunks - 1) >> 1);
13381277         break;
13391278      case 178:
1340         mmc_cmd1 = 0;
1279         state->mmc_cmd1 = 0;
13411280         prg32(machine, 0);
13421281         break;
13431282      case 180:
r7955r7956
13521291         prg16_cdef(machine, (state->prg_chunks - 1) ^ 0x08);
13531292         break;
13541293      case 187:
1355         prg_bank[0] = prg_bank[2] = 0xfe;
1356         prg_bank[1] = prg_bank[3] = 0xff;
1294         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1295         state->prg_bank[1] = state->prg_bank[3] = 0xff;
13571296         mapper187_reg[0] = mapper187_reg[1] = mapper187_reg[2] = mapper187_reg[3] = 0;
1358         mmc_cmd1 = 0;
1359         mmc_cmd2 = 0x80;
1360         mmc_prg_base = mmc_chr_base = 0;
1361         mmc_prg_mask = mmc_chr_mask = 0xff;
1362         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1363         mapper187_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1364         mapper187_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1297         state->mmc_cmd1 = 0;
1298         state->mmc_cmd2 = 0x80;
1299         state->mmc_prg_base = state->mmc_chr_base = 0;
1300         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1301         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1302         mapper187_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1303         mapper187_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13651304         break;
13661305      case 189:
1367         mmc_cmd1 = 0;
1368         mmc_cmd2 = 0x80;
1369         mmc_chr_base = 0;
1370         mmc_chr_mask = 0xff;
1371         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1306         state->mmc_cmd1 = 0;
1307         state->mmc_cmd2 = 0x80;
1308         state->mmc_chr_base = 0;
1309         state->mmc_chr_mask = 0xff;
1310         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
13721311         prg32(machine, 0);
1373         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1312         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13741313         break;
13751314      case 193:
13761315         prg32(machine, (state->prg_chunks - 1) >> 1);
13771316         set_nt_mirroring(machine, PPU_MIRROR_VERT);
13781317         break;
13791318      case 197:
1380         prg_bank[0] = prg_bank[2] = 0xfe;
1381         prg_bank[1] = prg_bank[3] = 0xff;
1382         mmc_cmd1 = 0;
1383         mmc_cmd2 = 0x80;
1384         mmc_prg_base = mmc_chr_base = 0;
1385         mmc_prg_mask = mmc_chr_mask = 0xff;
1386         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1387         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1388         mapper197_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1319         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1320         state->prg_bank[1] = state->prg_bank[3] = 0xff;
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         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1327         mapper197_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13891328         break;
13901329      case 198:
1391         prg_bank[0] = 0x00;
1392         prg_bank[1] = 0x01;
1393         prg_bank[2] = 0x4e;
1394         prg_bank[3] = 0x4f;
1395         mmc_cmd1 = 0;
1396         mmc_cmd2 = 0x80;
1397         mmc_prg_base = mmc_chr_base = 0;
1398         mmc_prg_mask = mmc_chr_mask = 0xff;
1399         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1400         mapper198_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1401         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1330         state->prg_bank[0] = 0x00;
1331         state->prg_bank[1] = 0x01;
1332         state->prg_bank[2] = 0x4e;
1333         state->prg_bank[3] = 0x4f;
1334         state->mmc_cmd1 = 0;
1335         state->mmc_cmd2 = 0x80;
1336         state->mmc_prg_base = state->mmc_chr_base = 0;
1337         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1338         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1339         mapper198_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1340         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
14021341         break;
14031342      case 199:
1404         prg_bank[0] = 0x00;
1405         prg_bank[1] = 0x01;
1406         prg_bank[2] = 0x3e;
1407         prg_bank[3] = 0x3f;
1408         mmc_cmd1 = 0;
1409         mmc_cmd2 = 0x80;
1410         mmc_prg_base = mmc_chr_base = 0;
1411         mmc_prg_mask = mmc_chr_mask = 0xff;
1412         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1413         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1414         mapper199_set_chr(machine, mmc_chr_base, mmc_chr_mask);
1343         state->prg_bank[0] = 0x00;
1344         state->prg_bank[1] = 0x01;
1345         state->prg_bank[2] = 0x3e;
1346         state->prg_bank[3] = 0x3f;
1347         state->mmc_cmd1 = 0;
1348         state->mmc_cmd2 = 0x80;
1349         state->mmc_prg_base = state->mmc_chr_base = 0;
1350         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1351         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1352         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1353         mapper199_set_chr(machine, state->mmc_chr_base, state->mmc_chr_mask);
14151354         break;
14161355      case 200:
14171356         prg16_89ab(machine, state->prg_chunks - 1);
r7955r7956
14311370         chr8(machine, 0, CHRROM);
14321371         break;
14331372      case 205:
1434         prg_bank[0] = prg_bank[2] = 0xfe;
1435         prg_bank[1] = prg_bank[3] = 0xff;
1436         mmc_cmd1 = 0;
1437         mmc_cmd2 = 0x80;
1438         mmc_prg_base = 0x10;
1439         mmc_prg_mask = 0x1f;
1440         mmc_chr_base = 0;
1441         mmc_chr_mask = 0xff;
1442         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1443         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1444         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1373         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1374         state->prg_bank[1] = state->prg_bank[3] = 0xff;
1375         state->mmc_cmd1 = 0;
1376         state->mmc_cmd2 = 0x80;
1377         state->mmc_prg_base = 0x10;
1378         state->mmc_prg_mask = 0x1f;
1379         state->mmc_chr_base = 0;
1380         state->mmc_chr_mask = 0xff;
1381         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1382         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1383         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
14451384         break;
14461385      case 208:
1447         prg_bank[0] = prg_bank[2] = 0xfe;
1448         prg_bank[1] = prg_bank[3] = 0xff;
1386         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1387         state->prg_bank[1] = state->prg_bank[3] = 0xff;
14491388         map208_reg[0] = map208_reg[1] = map208_reg[2] = map208_reg[3] = map208_reg[4] = 0;
1450         mmc_cmd1 = 0;
1451         mmc_cmd2 = 0x80;
1452         mmc_prg_base = mmc_chr_base = 0;
1453         mmc_prg_mask = mmc_chr_mask = 0xff;
1454         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1455         mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1456         mapper4_set_chr(machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1389         state->mmc_cmd1 = 0;
1390         state->mmc_cmd2 = 0x80;
1391         state->mmc_prg_base = state->mmc_chr_base = 0;
1392         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1393         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1394         mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1395         mapper4_set_chr(machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
14571396         break;
14581397      case 212:
14591398         chr8(machine, 0xff, CHRROM);
14601399         prg32(machine, 0xff);
14611400         break;
14621401      case 215:
1463         prg_bank[0] = 0x00;
1464         prg_bank[2] = 0xfe;
1465         prg_bank[1] = 0x01;
1466         prg_bank[3] = 0xff;
1467         mmc_cmd1 = 0;
1468         mmc_cmd2 = 0x80;
1402         state->prg_bank[0] = 0x00;
1403         state->prg_bank[2] = 0xfe;
1404         state->prg_bank[1] = 0x01;
1405         state->prg_bank[3] = 0xff;
1406         state->mmc_cmd1 = 0;
1407         state->mmc_cmd2 = 0x80;
14691408         map215_reg[0] = 0x00;
14701409         map215_reg[1] = 0xff;
14711410         map215_reg[2] = 0x04;
14721411         map215_reg[3] = 0;
1473         mmc_prg_base = 0;
1474         mmc_prg_mask = 0x1f;
1475         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1476         mapper215_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1477         mapper215_set_chr(machine, mmc_chr_source);
1412         state->mmc_prg_base = 0;
1413         state->mmc_prg_mask = 0x1f;
1414         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1415         mapper215_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1416         mapper215_set_chr(machine, state->mmc_chr_source);
14781417         break;
14791418      case 216:
1480         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1419         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
14811420         prg32(machine, 0);
1482         chr8(machine, 0, mmc_chr_source);
1421         chr8(machine, 0, state->mmc_chr_source);
14831422         break;
14841423      case 217:
14851424         map217_reg[0] = 0x00;
14861425         map217_reg[1] = 0xff;
14871426         map217_reg[2] = 0x03;
14881427         map217_reg[3] = 0;
1489         prg_bank[0] = prg_bank[2] = 0xfe;
1490         prg_bank[1] = prg_bank[3] = 0xff;
1491         mmc_cmd1 = 0;
1492         mmc_cmd2 = 0x80;
1493         mmc_prg_base = mmc_chr_base = 0;
1494         mmc_prg_mask = mmc_chr_mask = 0xff;
1495         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1496         mapper217_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1497         mapper217_set_chr(machine, mmc_chr_source);
1428         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1429         state->prg_bank[1] = state->prg_bank[3] = 0xff;
1430         state->mmc_cmd1 = 0;
1431         state->mmc_cmd2 = 0x80;
1432         state->mmc_prg_base = state->mmc_chr_base = 0;
1433         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1434         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1435         mapper217_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1436         mapper217_set_chr(machine, state->mmc_chr_source);
14981437         break;
14991438      case 221:
1500         mmc_cmd1 = 0;
1501         mmc_cmd2 = 0;
1439         state->mmc_cmd1 = 0;
1440         state->mmc_cmd2 = 0;
15021441         prg16_89ab(machine, 0);
15031442         prg16_cdef(machine, 0);
15041443         break;
r7955r7956
15061445         prg32(machine, 0);
15071446         break;
15081447      case 226:
1509         mmc_cmd1 = 0;
1510         mmc_cmd2 = 0;
1448         state->mmc_cmd1 = 0;
1449         state->mmc_cmd2 = 0;
15111450         prg32(machine, 0);
15121451         break;
15131452      case 227:
r7955r7956
15311470         set_nt_mirroring(machine, PPU_MIRROR_VERT);
15321471         break;
15331472      case 232:
1534         mmc_cmd1 = 0x18;
1535         mmc_cmd2 = 0x00;
1473         state->mmc_cmd1 = 0x18;
1474         state->mmc_cmd2 = 0x00;
15361475         mapper232_set_prg(machine);
15371476         break;
15381477      case 243:
1539         vrom_bank[0] = 3;
1540         mmc_cmd1 = 0;
1478         state->vrom_bank[0] = 3;
1479         state->mmc_cmd1 = 0;
15411480         chr8(machine, 3, CHRROM);
15421481         prg32(machine, 0);
15431482         set_nt_mirroring(machine, PPU_MIRROR_VERT);
r7955r7956
15461485         prg32(machine, 0xff);
15471486         break;
15481487      case 249:
1549         prg_bank[0] = prg_bank[2] = 0xfe;
1550         prg_bank[1] = prg_bank[3] = 0xff;
1551         mmc_cmd1 = 0;
1552         mmc_cmd2 = 0x80;
1488         state->prg_bank[0] = state->prg_bank[2] = 0xfe;
1489         state->prg_bank[1] = state->prg_bank[3] = 0xff;
1490         state->mmc_cmd1 = 0;
1491         state->mmc_cmd2 = 0x80;
15531492         map249_reg = 0;
1554         mmc_prg_base = mmc_chr_base = 0;
1555         mmc_prg_mask = mmc_chr_mask = 0xff;
1556         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1557         mapper249_set_prg(machine, mmc_prg_base, mmc_prg_mask);
1558         mapper249_set_chr(machine, mmc_chr_base, mmc_chr_mask);
1493         state->mmc_prg_base = state->mmc_chr_base = 0;
1494         state->mmc_prg_mask = state->mmc_chr_mask = 0xff;
1495         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1496         mapper249_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
1497         mapper249_set_chr(machine, state->mmc_chr_base, state->mmc_chr_mask);
15591498         break;
15601499      default:
15611500         /* Mapper not supported */
r7955r7956
16041543
16051544   mapper_warning = 0;
16061545
1607   nes_mmc5_vram_control = 0;
1546   state->mmc5_vram_control = 0;
16081547
16091548   /* Point the WRAM/battery area to the first RAM bank */
16101549   if (mmc_num != 20)
r7955r7956
16121551
16131552   /* Here, we init a few helpers: 4 prg banks and 16 chr banks - some mappers use them */
16141553   for (i = 0; i < 4; i++)
1615      prg_bank[i] = 0;
1554      state->prg_bank[i] = 0;
16161555   for (i = 0; i < 16; i++)
1617      vrom_bank[i] = 0;
1556      state->vrom_bank[i] = 0;
16181557   for (i = 0; i < 16; i++)
1619      extra_bank[i] = 0;
1558      state->extra_bank[i] = 0;
16201559
16211560   /* Finally, we init IRQ-related quantities. */
1622   IRQ_enable = IRQ_enable_latch = 0;
1623   IRQ_count = IRQ_count_latch = 0;
1624   IRQ_toggle = 0;
1561   state->IRQ_enable = state->IRQ_enable_latch = 0;
1562   state->IRQ_count = state->IRQ_count_latch = 0;
1563   state->IRQ_toggle = 0;
16251564
16261565   err = mapper_initialize(machine, mmc_num);
16271566
16281567   return err;
16291568}
16301569
1631void nes_mapper_init(const mmc_intf *intf)
1632{
1633   mmc_write_low = intf->mmc_write_low;
1634   mmc_read_low = intf->mmc_read_low;
1635   mmc_write_mid = intf->mmc_write_mid;
1636   mmc_write = intf->mmc_write;
1637}
1638
16391570/*************************************************************
16401571
16411572    Support for .unf Files
r7955r7956
16991630         bmc_s24in1sc03_reg[0] = 0x24;
17001631         bmc_s24in1sc03_reg[1] = 0x9f;
17011632         bmc_s24in1sc03_reg[2] = 0;
1702         prg_bank[0] = 0xfe;
1703         prg_bank[1] = 0xff;
1704         prg_bank[2] = 0xfe;
1705         prg_bank[3] = 0xff;
1706         mmc_cmd1 = 0;
1707         mmc_cmd2 = 0x80;
1633         state->prg_bank[0] = 0xfe;
1634         state->prg_bank[1] = 0xff;
1635         state->prg_bank[2] = 0xfe;
1636         state->prg_bank[3] = 0xff;
1637         state->mmc_cmd1 = 0;
1638         state->mmc_cmd2 = 0x80;
17081639         bmc_s24in1sc03_set_prg(machine);
17091640         bmc_s24in1sc03_set_chr(machine);
17101641         break;
17111642      case BMC_T262:
1712         mmc_cmd1 = 0;
1713         mmc_cmd2 = 0;
1643         state->mmc_cmd1 = 0;
1644         state->mmc_cmd2 = 0;
17141645         prg16_89ab(machine, 0);
17151646         prg16_cdef(machine, 7);
17161647         break;
17171648      case BMC_WS:
1718         mmc_cmd1 = 0;
1649         state->mmc_cmd1 = 0;
17191650         prg32(machine, 0);
17201651         break;
17211652      case DREAMTECH:
r7955r7956
17251656         break;
17261657      case UNL_8237:
17271658         unl_8237_reg[0] = unl_8237_reg[1] = unl_8237_reg[2] = 0;
1728         prg_bank[0] = 0x00;
1729         prg_bank[1] = 0x01;
1730         prg_bank[2] = 0xfe;
1731         prg_bank[3] = 0xff;
1732         mmc_cmd1 = 0;
1733         mmc_cmd2 = 0x80;
1659         state->prg_bank[0] = 0x00;
1660         state->prg_bank[1] = 0x01;
1661         state->prg_bank[2] = 0xfe;
1662         state->prg_bank[3] = 0xff;
1663         state->mmc_cmd1 = 0;
1664         state->mmc_cmd2 = 0x80;
17341665         unl_8237_set_prg(machine);
17351666         unl_8237_set_chr(machine);
17361667         break;
17371668      case UNL_AX5705:
1738         prg_bank[0] = 0;
1739         prg_bank[1] = 1;
1740         prg8_89(machine, prg_bank[0]);
1741         prg8_ab(machine, prg_bank[1]);
1669         state->prg_bank[0] = 0;
1670         state->prg_bank[1] = 1;
1671         prg8_89(machine, state->prg_bank[0]);
1672         prg8_ab(machine, state->prg_bank[1]);
17421673         prg8_cd(machine, 0xfe);
17431674         prg8_ef(machine, 0xff);
17441675         break;
r7955r7956
17501681         mapper_initialize(machine, 4);
17511682         break;
17521683      case UNL_T230:
1753         mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1684         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
17541685         prg16_89ab(machine, 0);
17551686         prg16_cdef(machine, state->prg_chunks - 1);
17561687         break;
src/mess/machine/nes_mmc.h
r7955r7956
5252
5353#define MMC5_VRAM
5454
55extern UINT8 nes_mmc5_vram[0x400];
56extern int nes_mmc5_vram_control;
57
5855void nes_mapper_init( const mmc_intf *intf );
5956int nes_mapper_reset( running_machine *machine, int mapperNum );
6057int nes_unif_reset( running_machine *machine, const char *board );
src/mess/machine/nes_ines.c
r7955r7956
6969
7070static void MMC1_set_chr( running_machine *machine )
7171{
72   nes_state *state = (nes_state *)machine->driver_data;
7273   UINT8 chr_mode = MMC1_regs[0] & 0x10;
7374
7475   if (chr_mode)
7576   {
76      chr4_0(machine, MMC1_regs[1] & 0x1f, mmc_chr_source);
77      chr4_4(machine, MMC1_regs[2] & 0x1f, mmc_chr_source);
77      chr4_0(machine, MMC1_regs[1] & 0x1f, state->mmc_chr_source);
78      chr4_4(machine, MMC1_regs[2] & 0x1f, state->mmc_chr_source);
7879   }
7980   else
80      chr8(machine, (MMC1_regs[1] & 0x1f) >> 1, mmc_chr_source);
81      chr8(machine, (MMC1_regs[1] & 0x1f) >> 1, state->mmc_chr_source);
8182}
8283
8384static WRITE8_HANDLER( mapper1_w )
8485{
86   nes_state *state = (nes_state *)space->machine->driver_data;
8587   /* Note that there is only one latch and shift counter, shared amongst the 4 regs */
8688   /* Space Shuttle will not work if they have independent variables. */
8789   LOG_MMC(("mapper1_w offset: %04x, data: %02x\n", offset, data));
r7955r7956
9294       AD&D Hillsfar and Bill & Ted rely on this behavior!! */
9395   if (data & 0x80)
9496   {
95      mmc_count = 0;
96      mmc_cmd1 = 0;
97      state->mmc_count = 0;
98      state->mmc_cmd1 = 0;
9799
98100      /* Set reg at 0x8000 to size 16k and lower half swap - needed for Robocop 3, Dynowars */
99101      MMC1_regs[0] |= 0x0c;
r7955r7956
103105      return;
104106   }
105107
106   if (mmc_count < 5)
108   if (state->mmc_count < 5)
107109   {
108      if (mmc_count == 0) mmc_cmd1 = 0;
109      mmc_cmd1 >>= 1;
110      mmc_cmd1 |= (data & 0x01) ? 0x10 : 0x00;
111      mmc_count++;
110      if (state->mmc_count == 0) state->mmc_cmd1 = 0;
111      state->mmc_cmd1 >>= 1;
112      state->mmc_cmd1 |= (data & 0x01) ? 0x10 : 0x00;
113      state->mmc_count++;
112114   }
113115
114   if (mmc_count == 5)
116   if (state->mmc_count == 5)
115117   {
116118      switch (offset & 0x6000)   /* Which reg shall we write to? */
117119      {
118120      case 0x0000:
119         MMC1_regs[0] = mmc_cmd1;
121         MMC1_regs[0] = state->mmc_cmd1;
120122
121123         switch (MMC1_regs[0] & 0x03)
122124         {
r7955r7956
129131         MMC1_set_prg(space->machine);
130132         break;
131133      case 0x2000:
132         MMC1_regs[1] = mmc_cmd1;
134         MMC1_regs[1] = state->mmc_cmd1;
133135         MMC1_set_chr(space->machine);
134136         MMC1_set_prg(space->machine);
135137         break;
136138      case 0x4000:
137         MMC1_regs[2] = mmc_cmd1;
139         MMC1_regs[2] = state->mmc_cmd1;
138140         MMC1_set_chr(space->machine);
139141         break;
140142      case 0x6000:
141         MMC1_regs[3] = mmc_cmd1;
143         MMC1_regs[3] = state->mmc_cmd1;
142144         MMC1_set_prg(space->machine);
143145         break;
144146      }
145147
146      mmc_count = 0;
148      state->mmc_count = 0;
147149   }
148150}
149151
r7955r7956
213215
214216static void mapper4_set_prg( running_machine *machine, int prg_base, int prg_mask )
215217{
216   UINT8 prg_flip = (mmc_cmd1 & 0x40) ? 2 : 0;
218   nes_state *state = (nes_state *)machine->driver_data;
219   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
217220
218   prg8_89(machine, prg_base | (prg_bank[0 ^ prg_flip] & prg_mask));
219   prg8_ab(machine, prg_base | (prg_bank[1] & prg_mask));
220   prg8_cd(machine, prg_base | (prg_bank[2 ^ prg_flip] & prg_mask));
221   prg8_ef(machine, prg_base | (prg_bank[3] & prg_mask));
221   prg8_89(machine, prg_base | (state->prg_bank[0 ^ prg_flip] & prg_mask));
222   prg8_ab(machine, prg_base | (state->prg_bank[1] & prg_mask));
223   prg8_cd(machine, prg_base | (state->prg_bank[2 ^ prg_flip] & prg_mask));
224   prg8_ef(machine, prg_base | (state->prg_bank[3] & prg_mask));
222225}
223226
224227static void mapper4_set_chr( running_machine *machine, UINT8 chr, int chr_base, int chr_mask )
225228{
226   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
229   nes_state *state = (nes_state *)machine->driver_data;
230   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
227231
228   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), chr);
229   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), chr);
230   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), chr);
231   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), chr);
232   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr);
233   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr);
234   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr);
235   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr);
232   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr);
233   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), chr);
234   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr);
235   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), chr);
236   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr);
237   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr);
238   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr);
239   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr);
236240}
237241
238242/* Here, IRQ counter decrements every scanline. */
239243static void mapper4_irq( running_device *device, int scanline, int vblank, int blanked )
240244{
245   nes_state *state = (nes_state *)device->machine->driver_data;
246
241247   if (scanline < PPU_BOTTOM_VISIBLE_SCANLINE)
242248   {
243      int priorCount = IRQ_count;
244      if ((IRQ_count == 0))
249      int priorCount = state->IRQ_count;
250      if ((state->IRQ_count == 0))
245251      {
246         IRQ_count = IRQ_count_latch;
252         state->IRQ_count = state->IRQ_count_latch;
247253      }
248254      else
249         IRQ_count--;
255         state->IRQ_count--;
250256
251      if (IRQ_enable && !blanked && (IRQ_count == 0) && priorCount)
257      if (state->IRQ_enable && !blanked && (state->IRQ_count == 0) && priorCount)
252258      {
253259         LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline,
254260               video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen)));
r7955r7956
267273   switch (offset & 0x6001)
268274   {
269275      case 0x0000:
270         MMC3_helper = mmc_cmd1 ^ data;
271         mmc_cmd1 = data;
276         MMC3_helper = state->mmc_cmd1 ^ data;
277         state->mmc_cmd1 = data;
272278
273279         /* Has PRG Mode changed? */
274280         if (MMC3_helper & 0x40)
275            mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
281            mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
276282
277283         /* Has CHR Mode changed? */
278284         if (MMC3_helper & 0x80)
279            mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
285            mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
280286         break;
281287
282288      case 0x0001:
283         cmd = mmc_cmd1 & 0x07;
289         cmd = state->mmc_cmd1 & 0x07;
284290         switch (cmd)
285291         {
286292            case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
287293            case 2: case 3: case 4: case 5:
288               vrom_bank[cmd] = data;
289               mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
294               state->vrom_bank[cmd] = data;
295               mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
290296               break;
291297            case 6:
292298            case 7:
293               prg_bank[cmd - 6] = data;
294               mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
299               state->prg_bank[cmd - 6] = data;
300               mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
295301               break;
296302         }
297303         break;
r7955r7956
301307         break;
302308
303309      case 0x2001: /* extra RAM enable/disable */
304         mmc_cmd2 = data;   /* This actually is made of two parts: data&0x80 = WRAM enabled and data&0x40 = WRAM readonly!  */
305                  /* We save this twice because we will need mmc_cmd2 in some clone mapper */
310         state->mmc_cmd2 = data;   /* This actually is made of two parts: data&0x80 = WRAM enabled and data&0x40 = WRAM readonly!  */
311                  /* We save this twice because we will need state->mmc_cmd2 in some clone mapper */
306312         state->mid_ram_enable = data;
307313         break;
308314
309315      case 0x4000:
310         IRQ_count_latch = data;
316         state->IRQ_count_latch = data;
311317         break;
312318
313      case 0x4001: /* some sources state that here we must have IRQ_count = IRQ_count_latch */
314         IRQ_count = 0;
319      case 0x4001: /* some sources state that here we must have state->IRQ_count = state->IRQ_count_latch */
320         state->IRQ_count = 0;
315321         break;
316322
317323      case 0x6000:
318         IRQ_enable = 0;
324         state->IRQ_enable = 0;
319325         break;
320326
321327      case 0x6001:
322         IRQ_enable = 1;
328         state->IRQ_enable = 1;
323329         break;
324330
325331      default:
r7955r7956
342348
343349static void mapper5_irq( running_device *device, int scanline, int vblank, int blanked )
344350{
351   nes_state *state = (nes_state *)device->machine->driver_data;
352
345353#if 1
346354   if (scanline == 0)
347      IRQ_status |= 0x40;
355      state->IRQ_status |= 0x40;
348356   else if (scanline > PPU_BOTTOM_VISIBLE_SCANLINE)
349      IRQ_status &= ~0x40;
357      state->IRQ_status &= ~0x40;
350358#endif
351359
352   if (scanline == IRQ_count)
360   if (scanline == state->IRQ_count)
353361   {
354      if (IRQ_enable)
362      if (state->IRQ_enable)
355363         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
356364
357      IRQ_status = 0xff;
365      state->IRQ_status = 0xff;
358366   }
359367}
360368
r7955r7956
382390
383391static READ8_HANDLER( mapper5_l_r )
384392{
393   nes_state *state = (nes_state *)space->machine->driver_data;
385394   int retVal;
386395
387396   /* $5c00 - $5fff: extended videoram attributes */
388397   if ((offset >= 0x1b00) && (offset <= 0x1eff))
389398   {
390      return nes_mmc5_vram[offset - 0x1b00];
399      return state->mmc5_vram[offset - 0x1b00];
391400   }
392401
393402   switch (offset)
r7955r7956
399408         else
400409            return 0x00;
401410#else
402         retVal = IRQ_status;
403         IRQ_status &= ~0x80;
411         retVal = state->IRQ_status;
412         state->IRQ_status &= ~0x80;
404413         return retVal;
405414#endif
406415
407416      case 0x1105: /* $5205 */
408         return (mult1 * mult2) & 0xff;
417         return (state->mult1 * state->mult2) & 0xff;
409418      case 0x1106: /* $5206 */
410         return ((mult1 * mult2) & 0xff00) >> 8;
419         return ((state->mult1 * state->mult2) & 0xff00) >> 8;
411420
412421      default:
413422         logerror("** MMC5 uncaught read, offset: %04x\n", offset + 0x4100);
r7955r7956
420429//  int i;
421430//
422431//  for (i = 0; i < 8; i ++)
423//      nes_vram[i] = vrom_bank[0 + (mode * 8)] * 64;
432//      nes_vram[i] = state->vrom_bank[0 + (mode * 8)] * 64;
424433//}
425434
426435static WRITE8_HANDLER( mapper5_l_w )
r7955r7956
443452   if ((offset >= 0x1b00) && (offset <= 0x1eff))
444453   {
445454      if (MMC5_vram_protect == 0x03)
446         nes_mmc5_vram[offset - 0x1b00] = data;
455         state->mmc5_vram[offset - 0x1b00] = data;
447456      return;
448457   }
449458
r7955r7956
475484         break;
476485
477486      case 0x1004: /* $5104 - Extra VRAM (EXRAM) control */
478         nes_mmc5_vram_control = data;
487         state->mmc5_vram_control = data;
479488         LOG_MMC(("MMC5 exram control: %02x\n", data));
480489         break;
481490
r7955r7956
488497
489498      /* tile data for MMC5 flood-fill NT mode */
490499      case 0x1006:
491         MMC5_floodtile = data;
500         state->MMC5_floodtile = data;
492501         break;
493502
494503      /* attr data for MMC5 flood-fill NT mode */
r7955r7956
496505         switch (data & 3)
497506         {
498507         default:
499         case 0: MMC5_floodattr = 0x00; break;
500         case 1: MMC5_floodattr = 0x55; break;
501         case 2: MMC5_floodattr = 0xaa; break;
502         case 3: MMC5_floodattr = 0xff; break;
508         case 0: state->MMC5_floodattr = 0x00; break;
509         case 1: state->MMC5_floodattr = 0x55; break;
510         case 2: state->MMC5_floodattr = 0xaa; break;
511         case 3: state->MMC5_floodattr = 0xff; break;
503512         }
504513         break;
505514
r7955r7956
620629         {
621630            case 0x03:
622631               /* 1k switch */
623               vrom_bank[0] = data;
632               state->vrom_bank[0] = data;
624633//                  mapper5_sync_vrom(0);
625               chr1_0(space->machine, vrom_bank[0], CHRROM);
626//                  state->nes_vram_sprite[0] = vrom_bank[0] * 64;
634               chr1_0(space->machine, state->vrom_bank[0], CHRROM);
635//                  state->nes_vram_sprite[0] = state->vrom_bank[0] * 64;
627636//                  vrom_next[0] = 4;
628637//                  vrom_page_a = 1;
629638//                  vrom_page_b = 0;
r7955r7956
640649               break;
641650            case 0x03:
642651               /* 1k switch */
643               vrom_bank[1] = data;
652               state->vrom_bank[1] = data;
644653//                  mapper5_sync_vrom(0);
645               chr1_1(space->machine, vrom_bank[1], CHRROM);
646//                  state->nes_vram_sprite[1] = vrom_bank[0] * 64;
654               chr1_1(space->machine, state->vrom_bank[1], CHRROM);
655//                  state->nes_vram_sprite[1] = state->vrom_bank[0] * 64;
647656//                  vrom_next[1] = 5;
648657//                  vrom_page_a = 1;
649658//                  vrom_page_b = 0;
r7955r7956
656665         {
657666            case 0x03:
658667               /* 1k switch */
659               vrom_bank[2] = data;
668               state->vrom_bank[2] = data;
660669//                  mapper5_sync_vrom(0);
661               chr1_2(space->machine, vrom_bank[2], CHRROM);
662//                  state->nes_vram_sprite[2] = vrom_bank[0] * 64;
670               chr1_2(space->machine, state->vrom_bank[2], CHRROM);
671//                  state->nes_vram_sprite[2] = state->vrom_bank[0] * 64;
663672//                  vrom_next[2] = 6;
664673//                  vrom_page_a = 1;
665674//                  vrom_page_b = 0;
r7955r7956
679688               break;
680689            case 0x03:
681690               /* 1k switch */
682               vrom_bank[3] = data;
691               state->vrom_bank[3] = data;
683692//                  mapper5_sync_vrom(0);
684               chr1_3(space->machine, vrom_bank[3], CHRROM);
685//                  state->nes_vram_sprite[3] = vrom_bank[0] * 64;
693               chr1_3(space->machine, state->vrom_bank[3], CHRROM);
694//                  state->nes_vram_sprite[3] = state->vrom_bank[0] * 64;
686695//                  vrom_next[3] = 7;
687696//                  vrom_page_a = 1;
688697//                  vrom_page_b = 0;
r7955r7956
695704         {
696705            case 0x03:
697706               /* 1k switch */
698               vrom_bank[4] = data;
707               state->vrom_bank[4] = data;
699708//                  mapper5_sync_vrom(0);
700               chr1_4(space->machine, vrom_bank[4], CHRROM);
701//                  state->nes_vram_sprite[4] = vrom_bank[0] * 64;
709               chr1_4(space->machine, state->vrom_bank[4], CHRROM);
710//                  state->nes_vram_sprite[4] = state->vrom_bank[0] * 64;
702711//                  vrom_next[0] = 0;
703712//                  vrom_page_a = 0;
704713//                  vrom_page_b = 0;
r7955r7956
715724               break;
716725            case 0x03:
717726               /* 1k switch */
718               vrom_bank[5] = data;
727               state->vrom_bank[5] = data;
719728//                  mapper5_sync_vrom(0);
720               chr1_5(space->machine, vrom_bank[5], CHRROM);
721//                  state->nes_vram_sprite[5] = vrom_bank[0] * 64;
729               chr1_5(space->machine, state->vrom_bank[5], CHRROM);
730//                  state->nes_vram_sprite[5] = state->vrom_bank[0] * 64;
722731//                  vrom_next[1] = 1;
723732//                  vrom_page_a = 0;
724733//                  vrom_page_b = 0;
r7955r7956
731740         {
732741            case 0x03:
733742               /* 1k switch */
734               vrom_bank[6] = data;
743               state->vrom_bank[6] = data;
735744//                  mapper5_sync_vrom(0);
736               chr1_6(space->machine, vrom_bank[6], CHRROM);
737//                  state->nes_vram_sprite[6] = vrom_bank[0] * 64;
745               chr1_6(space->machine, state->vrom_bank[6], CHRROM);
746//                  state->nes_vram_sprite[6] = state->vrom_bank[0] * 64;
738747//                  vrom_next[2] = 2;
739748//                  vrom_page_a = 0;
740749//                  vrom_page_b = 0;
r7955r7956
759768               break;
760769            case 0x03:
761770               /* 1k switch */
762               vrom_bank[7] = data;
771               state->vrom_bank[7] = data;
763772//                  mapper5_sync_vrom(0);
764               chr1_7(space->machine, vrom_bank[7], CHRROM);
765//                  state->nes_vram_sprite[7] = vrom_bank[0] * 64;
773               chr1_7(space->machine, state->vrom_bank[7], CHRROM);
774//                  state->nes_vram_sprite[7] = state->vrom_bank[0] * 64;
766775//                  vrom_next[3] = 3;
767776//                  vrom_page_a = 0;
768777//                  vrom_page_b = 0;
r7955r7956
775784         {
776785            case 0x03:
777786               /* 1k switch */
778               vrom_bank[8] = vrom_bank[12] = data;
787               state->vrom_bank[8] = state->vrom_bank[12] = data;
779788//                  nes_vram[vrom_next[0]] = data * 64;
780789//                  nes_vram[0 + (vrom_page_a*4)] = data * 64;
781790//                  nes_vram[0] = data * 64;
r7955r7956
800809               break;
801810            case 0x03:
802811               /* 1k switch */
803               vrom_bank[9] = vrom_bank[13] = data;
812               state->vrom_bank[9] = state->vrom_bank[13] = data;
804813//                  nes_vram[vrom_next[1]] = data * 64;
805814//                  nes_vram[1 + (vrom_page_a*4)] = data * 64;
806815//                  nes_vram[1] = data * 64;
r7955r7956
820829         {
821830            case 0x03:
822831               /* 1k switch */
823               vrom_bank[10] = vrom_bank[14] = data;
832               state->vrom_bank[10] = state->vrom_bank[14] = data;
824833//                  nes_vram[vrom_next[2]] = data * 64;
825834//                  nes_vram[2 + (vrom_page_a*4)] = data * 64;
826835//                  nes_vram[2] = data * 64;
r7955r7956
856865               break;
857866            case 0x03:
858867               /* 1k switch */
859               vrom_bank[11] = vrom_bank[15] = data;
868               state->vrom_bank[11] = state->vrom_bank[15] = data;
860869//                  nes_vram[vrom_next[3]] = data * 64;
861870//                  nes_vram[3 + (vrom_page_a*4)] = data * 64;
862871//                  nes_vram[3] = data * 64;
r7955r7956
872881         break;
873882
874883      case 0x1103: /* $5203 */
875         IRQ_count = data;
884         state->IRQ_count = data;
876885         MMC5_scanline = data;
877         LOG_MMC(("MMC5 irq scanline: %d\n", IRQ_count));
886         LOG_MMC(("MMC5 irq scanline: %d\n", state->IRQ_count));
878887         break;
879888      case 0x1104: /* $5204 */
880         IRQ_enable = data & 0x80;
889         state->IRQ_enable = data & 0x80;
881890         LOG_MMC(("MMC5 irq enable: %02x\n", data));
882891         break;
883892      case 0x1105: /* $5205 */
884         mult1 = data;
893         state->mult1 = data;
885894         break;
886895      case 0x1106: /* $5206 */
887         mult2 = data;
896         state->mult2 = data;
888897         break;
889898
890899      default:
r7955r7956
915924there are 3 dots to every 1 CPU cycle, hence 114 is the number of cycles per scanline ) */
916925static void ffe_irq( running_device *device, int scanline, int vblank, int blanked )
917926{
927   nes_state *state = (nes_state *)device->machine->driver_data;
928
918929   /* 114 is the number of cycles per scanline */
919930   /* TODO: change to reflect the actual number of cycles spent */
920   if (IRQ_enable)
931   if (state->IRQ_enable)
921932   {
922      if ((0xffff - IRQ_count) < 114)
933      if ((0xffff - state->IRQ_count) < 114)
923934      {
924935         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
925         IRQ_count = 0xffff;
926         IRQ_enable = 0;
936         state->IRQ_count = 0xffff;
937         state->IRQ_enable = 0;
927938      }
928      IRQ_count -= 114;
939      state->IRQ_count -= 114;
929940   }
930941}
931942
932943static WRITE8_HANDLER( mapper6_l_w )
933944{
945   nes_state *state = (nes_state *)space->machine->driver_data;
934946   LOG_MMC(("mapper6_l_w, offset: %04x, data: %02x\n", offset, data));
935947
936948   switch (offset)
937949   {
938950      case 0x1fe:
939         mmc_cmd1 = data & 0x80;
951         state->mmc_cmd1 = data & 0x80;
940952         set_nt_mirroring(space->machine, BIT(data, 4) ? PPU_MIRROR_HIGH : PPU_MIRROR_LOW);
941953         break;
942954      case 0x1ff:
r7955r7956
944956         break;
945957
946958      case 0x401:
947         IRQ_enable = data & 0x01;
959         state->IRQ_enable = data & 0x01;
948960         break;
949961      case 0x402:
950         IRQ_count = (IRQ_count & 0xff00) | data;
962         state->IRQ_count = (state->IRQ_count & 0xff00) | data;
951963         break;
952964      case 0x403:
953         IRQ_enable = 1;
954         IRQ_count = (IRQ_count & 0x00ff) | (data << 8);
965         state->IRQ_enable = 1;
966         state->IRQ_count = (state->IRQ_count & 0x00ff) | (data << 8);
955967         break;
956968   }
957969}
958970
959971static WRITE8_HANDLER( mapper6_w )
960972{
973   nes_state *state = (nes_state *)space->machine->driver_data;
961974   LOG_MMC(("mapper6_w, offset: %04x, data: %02x\n", offset, data));
962975
963   if (!mmc_cmd1)   // when in "FFE mode" we are forced to use CHRRAM/EXRAM bank?
976   if (!state->mmc_cmd1)   // when in "FFE mode" we are forced to use CHRRAM/EXRAM bank?
964977   {
965978      prg16_89ab(space->machine, data >> 2);
966979      // chr8(space->machine, data & 0x03, ???);
967980      // due to lack of info on the exact behavior, we simply act as if mmc_cmd=1
968      if (mmc_chr_source == CHRROM)
981      if (state->mmc_chr_source == CHRROM)
969982         chr8(space->machine, data & 0x03, CHRROM);
970983   }
971   else if (mmc_chr_source == CHRROM)         // otherwise, we can use CHRROM (when present)
984   else if (state->mmc_chr_source == CHRROM)         // otherwise, we can use CHRROM (when present)
972985      chr8(space->machine, data, CHRROM);
973986}
974987
r7955r7956
10241037
10251038static void mapper9_latch (running_device *device, offs_t offset)
10261039{
1040   nes_state *state = (nes_state *)device->machine->driver_data;
10271041   if ((offset & 0x3ff0) == 0x0fd0)
10281042   {
10291043      LOG_MMC(("mapper9 vrom latch switch (bank 0 low): %02x\n", MMC2_regs[0]));
1030      mmc_cmd1 = 0xfd;
1044      state->mmc_cmd1 = 0xfd;
10311045      chr4_0(device->machine, MMC2_regs[0], CHRROM);
10321046   }
10331047   else if ((offset & 0x3ff0) == 0x0fe0)
10341048   {
10351049      LOG_MMC(("mapper9 vrom latch switch (bank 0 high): %02x\n", MMC2_regs[1]));
1036      mmc_cmd1 = 0xfe;
1050      state->mmc_cmd1 = 0xfe;
10371051      chr4_0(device->machine, MMC2_regs[1], CHRROM);
10381052   }
10391053   else if ((offset & 0x3ff0) == 0x1fd0)
10401054   {
10411055      LOG_MMC(("mapper9 vrom latch switch (bank 1 low): %02x\n", MMC2_regs[2]));
1042      mmc_cmd2 = 0xfd;
1056      state->mmc_cmd2 = 0xfd;
10431057      chr4_4(device->machine, MMC2_regs[2], CHRROM);
10441058   }
10451059   else if ((offset & 0x3ff0) == 0x1fe0)
10461060   {
10471061      LOG_MMC(("mapper9 vrom latch switch (bank 0 high): %02x\n", MMC2_regs[3]));
1048      mmc_cmd2 = 0xfe;
1062      state->mmc_cmd2 = 0xfe;
10491063      chr4_4(device->machine, MMC2_regs[3], CHRROM);
10501064   }
10511065}
10521066
10531067static WRITE8_HANDLER( mapper9_w )
10541068{
1069   nes_state *state = (nes_state *)space->machine->driver_data;
10551070   LOG_MMC(("mapper9_w, offset: %04x, data: %02x\n", offset, data));
10561071   switch (offset & 0x7000)
10571072   {
r7955r7956
10601075         break;
10611076      case 0x3000:
10621077         MMC2_regs[0] = data;
1063         if (mmc_cmd1 == 0xfd)
1078         if (state->mmc_cmd1 == 0xfd)
10641079            chr4_0(space->machine, MMC2_regs[0], CHRROM);
10651080         break;
10661081      case 0x4000:
10671082         MMC2_regs[1] = data;
1068         if (mmc_cmd1 == 0xfe)
1083         if (state->mmc_cmd1 == 0xfe)
10691084            chr4_0(space->machine, MMC2_regs[1], CHRROM);
10701085         break;
10711086      case 0x5000:
10721087         MMC2_regs[2] = data;
1073         if (mmc_cmd2 == 0xfd)
1088         if (state->mmc_cmd2 == 0xfd)
10741089            chr4_4(space->machine, MMC2_regs[2], CHRROM);
10751090         break;
10761091      case 0x6000:
10771092         MMC2_regs[3] = data;
1078         if (mmc_cmd2 == 0xfe)
1093         if (state->mmc_cmd2 == 0xfe)
10791094            chr4_4(space->machine, MMC2_regs[3], CHRROM);
10801095         break;
10811096      case 0x7000:
r7955r7956
11261141
11271142static WRITE8_HANDLER( mapper11_w )
11281143{
1144   nes_state *state = (nes_state *)space->machine->driver_data;
11291145   LOG_MMC(("mapper11_w, offset: %04x, data: %02x\n", offset, data));
11301146
1131   chr8(space->machine, data >> 4, mmc_chr_source);
1147   chr8(space->machine, data >> 4, state->mmc_chr_source);
11321148   prg32(space->machine, data & 0x0f);
11331149}
11341150
r7955r7956
11611177
11621178static void mapper12_set_chr( running_machine *machine )
11631179{
1164   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
1180   nes_state *state = (nes_state *)machine->driver_data;
1181   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
11651182
1166   chr1_x(machine, chr_page ^ 0, ((mapper12_reg & 0x01) << 8) | (vrom_bank[0] & ~0x01), CHRROM);
1167   chr1_x(machine, chr_page ^ 1, ((mapper12_reg & 0x01) << 8) | (vrom_bank[0] |  0x01), CHRROM);
1168   chr1_x(machine, chr_page ^ 2, ((mapper12_reg & 0x01) << 8) | (vrom_bank[1] & ~0x01), CHRROM);
1169   chr1_x(machine, chr_page ^ 3, ((mapper12_reg & 0x01) << 8) | (vrom_bank[1] |  0x01), CHRROM);
1170   chr1_x(machine, chr_page ^ 4, ((mapper12_reg & 0x10) << 4) | vrom_bank[2], CHRROM);
1171   chr1_x(machine, chr_page ^ 5, ((mapper12_reg & 0x10) << 4) | vrom_bank[3], CHRROM);
1172   chr1_x(machine, chr_page ^ 6, ((mapper12_reg & 0x10) << 4) | vrom_bank[4], CHRROM);
1173   chr1_x(machine, chr_page ^ 7, ((mapper12_reg & 0x10) << 4) | vrom_bank[5], CHRROM);
1183   chr1_x(machine, chr_page ^ 0, ((mapper12_reg & 0x01) << 8) | (state->vrom_bank[0] & ~0x01), CHRROM);
1184   chr1_x(machine, chr_page ^ 1, ((mapper12_reg & 0x01) << 8) | (state->vrom_bank[0] |  0x01), CHRROM);
1185   chr1_x(machine, chr_page ^ 2, ((mapper12_reg & 0x01) << 8) | (state->vrom_bank[1] & ~0x01), CHRROM);
1186   chr1_x(machine, chr_page ^ 3, ((mapper12_reg & 0x01) << 8) | (state->vrom_bank[1] |  0x01), CHRROM);
1187   chr1_x(machine, chr_page ^ 4, ((mapper12_reg & 0x10) << 4) | state->vrom_bank[2], CHRROM);
1188   chr1_x(machine, chr_page ^ 5, ((mapper12_reg & 0x10) << 4) | state->vrom_bank[3], CHRROM);
1189   chr1_x(machine, chr_page ^ 6, ((mapper12_reg & 0x10) << 4) | state->vrom_bank[4], CHRROM);
1190   chr1_x(machine, chr_page ^ 7, ((mapper12_reg & 0x10) << 4) | state->vrom_bank[5], CHRROM);
11741191}
11751192
11761193static WRITE8_HANDLER( mapper12_w )
11771194{
1195   nes_state *state = (nes_state *)space->machine->driver_data;
11781196   UINT8 MMC3_helper, cmd;
11791197   LOG_MMC(("mapper12_w, offset: %04x, data: %02x\n", offset, data));
11801198
11811199   switch (offset & 0x6001)
11821200   {
11831201   case 0x0000:
1184      MMC3_helper = mmc_cmd1 ^ data;
1185      mmc_cmd1 = data;
1202      MMC3_helper = state->mmc_cmd1 ^ data;
1203      state->mmc_cmd1 = data;
11861204
11871205      /* Has PRG Mode changed? */
11881206      if (MMC3_helper & 0x40)
1189         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
1207         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
11901208
11911209      /* Has CHR Mode changed? */
11921210      if (MMC3_helper & 0x80)
r7955r7956
11941212      break;
11951213
11961214   case 0x0001:
1197      cmd = mmc_cmd1 & 0x07;
1215      cmd = state->mmc_cmd1 & 0x07;
11981216      switch (cmd)
11991217      {
12001218      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
12011219      case 2: case 3: case 4: case 5:
1202         vrom_bank[cmd] = data;
1220         state->vrom_bank[cmd] = data;
12031221         mapper12_set_chr(space->machine);
12041222         break;
12051223      case 6:
12061224      case 7:
1207         prg_bank[cmd - 6] = data;
1208         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
1225         state->prg_bank[cmd - 6] = data;
1226         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
12091227         break;
12101228      }
12111229      break;
r7955r7956
12481266
12491267static void mapper14_set_prg( running_machine *machine, int prg_base, int prg_mask )
12501268{
1269   nes_state *state = (nes_state *)machine->driver_data;
1270
12511271   if (map14_reg[0] & 0x02)
12521272   {
12531273      mapper4_set_prg(machine, prg_base, prg_mask);
12541274   }
12551275   else
12561276   {
1257      prg8_89(machine, extra_bank[0]);
1258      prg8_ab(machine, extra_bank[1]);
1259      prg8_cd(machine, extra_bank[2]);
1260      prg8_ef(machine, extra_bank[3]);
1277      prg8_89(machine, state->extra_bank[0]);
1278      prg8_ab(machine, state->extra_bank[1]);
1279      prg8_cd(machine, state->extra_bank[2]);
1280      prg8_ef(machine, state->extra_bank[3]);
12611281   }
12621282}
12631283
12641284static void mapper14_set_chr( running_machine *machine, UINT8 chr, int chr_base, int chr_mask )
12651285{
1286   nes_state *state = (nes_state *)machine->driver_data;
12661287   static const UINT8 conv_table[8] = {5, 5, 5, 5, 3, 3, 1, 1};
1267   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
1288   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
12681289   UINT8 bank[8];
12691290   UINT8 chr_base2[8];
12701291   int i;
r7955r7956
12731294   {
12741295      for(i = 0; i < 8; i++)
12751296      {
1276         bank[i] = vrom_bank[i];
1297         bank[i] = state->vrom_bank[i];
12771298         chr_base2[i] = chr_base | ((map14_reg[0] << conv_table[i]) & 0x100);
12781299      }
12791300   }
r7955r7956
12811302   {
12821303      for(i = 0; i < 8; i++)
12831304      {
1284         bank[i] = extra_bank[i + 4];   // first 4 extra_banks are PRG
1305         bank[i] = state->extra_bank[i + 4];   // first 4 state->extra_banks are PRG
12851306         chr_base2[i] = chr_base;
12861307      }
12871308   }
r7955r7956
12981319
12991320static WRITE8_HANDLER( mapper14_w )
13001321{
1322   nes_state *state = (nes_state *)space->machine->driver_data;
13011323   UINT8 map14_helper1, map14_helper2, MMC3_helper, cmd;
13021324   LOG_MMC(("mapper14_w, offset: %04x, data: %02x\n", offset, data));
13031325
13041326   if (offset == 0x2131)
13051327   {
13061328      map14_reg[0] = data;
1307      mapper14_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
1308      mapper14_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1329      mapper14_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
1330      mapper14_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13091331
13101332      if (!(map14_reg[0] & 0x02))
13111333         set_nt_mirroring(space->machine, BIT(map14_reg[1], 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
r7955r7956
13161338      switch (offset & 0x6001)
13171339      {
13181340      case 0x0000:
1319         MMC3_helper = mmc_cmd1 ^ data;
1320         mmc_cmd1 = data;
1341         MMC3_helper = state->mmc_cmd1 ^ data;
1342         state->mmc_cmd1 = data;
13211343
13221344         /* Has PRG Mode changed? */
13231345         if (MMC3_helper & 0x40)
1324            mapper14_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
1346            mapper14_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
13251347
13261348         /* Has CHR Mode changed? */
13271349         if (MMC3_helper & 0x80)
1328            mapper14_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1350            mapper14_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13291351         break;
13301352
13311353      case 0x0001:
1332         cmd = mmc_cmd1 & 0x07;
1354         cmd = state->mmc_cmd1 & 0x07;
13331355         switch (cmd)
13341356         {
13351357            case 0: case 1:   // these have to be changed due to the different way mapper14_set_chr works (it handles 1k banks)!
1336               vrom_bank[2 * cmd] = data;
1337               vrom_bank[2 * cmd + 1] = data;
1338               mapper14_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1358               state->vrom_bank[2 * cmd] = data;
1359               state->vrom_bank[2 * cmd + 1] = data;
1360               mapper14_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13391361               break;
13401362            case 2: case 3: case 4: case 5:
1341               vrom_bank[cmd + 2] = data;
1342               mapper14_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1363               state->vrom_bank[cmd + 2] = data;
1364               mapper14_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13431365               break;
13441366            case 6:
13451367            case 7:
1346               prg_bank[cmd - 6] = data;
1347               mapper14_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
1368               state->prg_bank[cmd - 6] = data;
1369               mapper14_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
13481370               break;
13491371         }
13501372         break;
r7955r7956
13621384   {
13631385      map14_helper1 = (offset & 0x01) << 2;
13641386      offset = ((offset & 0x02) | (offset >> 10)) >> 1;
1365      map14_helper2 = ((offset + 2) & 0x07) + 4; // '+4' because first 4 extra_banks are for PRG!
1366      extra_bank[map14_helper2] = (extra_bank[map14_helper2] & (0xf0 >> map14_helper1)) | ((data & 0x0f) << map14_helper1);
1367      mapper14_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
1387      map14_helper2 = ((offset + 2) & 0x07) + 4; // '+4' because first 4 state->extra_banks are for PRG!
1388      state->extra_bank[map14_helper2] = (state->extra_bank[map14_helper2] & (0xf0 >> map14_helper1)) | ((data & 0x0f) << map14_helper1);
1389      mapper14_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
13681390   }
13691391   else
13701392   {
r7955r7956
13721394      {
13731395      case 0x0000:
13741396      case 0x2000:
1375         extra_bank[offset >> 13] = data;
1376         mapper14_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
1397         state->extra_bank[offset >> 13] = data;
1398         mapper14_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
13771399         break;
13781400
13791401      case 0x1000:
r7955r7956
14561478there are 3 dots to every 1 CPU cycle, hence 114 is the number of cycles per scanline ) */
14571479static void bandai_irq( running_device *device, int scanline, int vblank, int blanked )
14581480{
1481   nes_state *state = (nes_state *)device->machine->driver_data;
1482
14591483   /* 114 is the number of cycles per scanline */
14601484   /* TODO: change to reflect the actual number of cycles spent */
1461   if (IRQ_enable)
1485   if (state->IRQ_enable)
14621486   {
1463      if (IRQ_count <= 114)
1487      if (state->IRQ_count <= 114)
14641488      {
14651489         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
1466         IRQ_count = (0xffff - 114 + IRQ_count);    // wrap around the 16 bits counter
1490         state->IRQ_count = (0xffff - 114 + state->IRQ_count);    // wrap around the 16 bits counter
14671491      }
1468      IRQ_count -= 114;
1492      state->IRQ_count -= 114;
14691493   }
14701494}
14711495
14721496static WRITE8_HANDLER( mapper16_m_w )
14731497{
1498   nes_state *state = (nes_state *)space->machine->driver_data;
14741499   LOG_MMC(("mapper16_m_w, offset: %04x, data: %02x\n", offset, data));
14751500
14761501   switch (offset & 0x000f)
r7955r7956
14791504      case 2: case 3:
14801505      case 4: case 5:
14811506      case 6: case 7:
1482         chr1_x(space->machine, offset & 0x07, data, mmc_chr_source);
1507         chr1_x(space->machine, offset & 0x07, data, state->mmc_chr_source);
14831508         break;
14841509      case 8:
14851510         prg16_89ab(space->machine, data);
r7955r7956
14941519         }
14951520         break;
14961521      case 0x0a:
1497         IRQ_enable = data & 0x01;
1522         state->IRQ_enable = data & 0x01;
14981523         break;
14991524      case 0x0b:
1500         IRQ_count = (IRQ_count & 0xff00) | data;
1525         state->IRQ_count = (state->IRQ_count & 0xff00) | data;
15011526         break;
15021527      case 0x0c:
1503         IRQ_count = (IRQ_count & 0x00ff) | (data << 8);
1528         state->IRQ_count = (state->IRQ_count & 0x00ff) | (data << 8);
15041529         break;
15051530      default:
15061531         logerror("** uncaught mapper 16 write, offset: %04x, data: %02x\n", offset, data);
r7955r7956
15291554
15301555static WRITE8_HANDLER( mapper17_l_w )
15311556{
1557   nes_state *state = (nes_state *)space->machine->driver_data;
15321558   LOG_MMC(("mapper17_l_w, offset: %04x, data: %02x\n", offset, data));
15331559
15341560   switch (offset)
r7955r7956
15411567         break;
15421568
15431569      case 0x401:
1544         IRQ_enable = data & 0x01;
1570         state->IRQ_enable = data & 0x01;
15451571         break;
15461572      case 0x402:
1547         IRQ_count = (IRQ_count & 0xff00) | data;
1573         state->IRQ_count = (state->IRQ_count & 0xff00) | data;
15481574         break;
15491575      case 0x403:
1550         IRQ_enable = 1;
1551         IRQ_count = (IRQ_count & 0x00ff) | (data << 8);
1576         state->IRQ_enable = 1;
1577         state->IRQ_count = (state->IRQ_count & 0x00ff) | (data << 8);
15521578         break;
15531579
15541580      case 0x404:
r7955r7956
15941620there are 3 dots to every 1 CPU cycle, hence 114 is the number of cycles per scanline ) */
15951621static void jaleco_irq( running_device *device, int scanline, int vblank, int blanked )
15961622{
1623   nes_state *state = (nes_state *)device->machine->driver_data;
1624
15971625   /* Increment & check the IRQ scanline counter */
1598   if (IRQ_enable)
1626   if (state->IRQ_enable)
15991627   {
1600      LOG_MMC(("scanline: %d, irq count: %04x\n", scanline, IRQ_count));
1601      if (IRQ_mode & 0x08)
1628      LOG_MMC(("scanline: %d, irq count: %04x\n", scanline, state->IRQ_count));
1629      if (state->IRQ_mode & 0x08)
16021630      {
1603         if ((IRQ_count & 0x000f) < 114)   // always true, but we only update the IRQ once per scanlines so we cannot be more precise :(
1631         if ((state->IRQ_count & 0x000f) < 114)   // always true, but we only update the IRQ once per scanlines so we cannot be more precise :(
16041632         {
16051633            cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
1606            IRQ_count = (IRQ_count & ~0x000f) | (0x0f - (114 & 0x0f) + (IRQ_count & 0x000f)); // sort of wrap around the counter
1634            state->IRQ_count = (state->IRQ_count & ~0x000f) | (0x0f - (114 & 0x0f) + (state->IRQ_count & 0x000f)); // sort of wrap around the counter
16071635         }
16081636         // decrements should not affect upper bits, so we don't do anything here (114 > 0x0f)
16091637      }
1610      else if (IRQ_mode & 0x04)
1638      else if (state->IRQ_mode & 0x04)
16111639      {
1612         if ((IRQ_count & 0x00ff) < 114)
1640         if ((state->IRQ_count & 0x00ff) < 114)
16131641         {
16141642            cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
1615            IRQ_count = (IRQ_count & ~0x00ff) | (0xff - 114 + (IRQ_count & 0x00ff));   // wrap around the 8 bits counter
1643            state->IRQ_count = (state->IRQ_count & ~0x00ff) | (0xff - 114 + (state->IRQ_count & 0x00ff));   // wrap around the 8 bits counter
16161644         }
16171645         else
1618            IRQ_count -= 114;
1646            state->IRQ_count -= 114;
16191647      }
1620      else if (IRQ_mode & 0x02)
1648      else if (state->IRQ_mode & 0x02)
16211649      {
1622         if ((IRQ_count & 0x0fff)  < 114)
1650         if ((state->IRQ_count & 0x0fff)  < 114)
16231651         {
16241652            cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
1625            IRQ_count = (IRQ_count & ~0x0fff) | (0xfff - 114 + (IRQ_count & 0x0fff));   // wrap around the 12 bits counter
1653            state->IRQ_count = (state->IRQ_count & ~0x0fff) | (0xfff - 114 + (state->IRQ_count & 0x0fff));   // wrap around the 12 bits counter
16261654         }
16271655         else
1628            IRQ_count -= 114;
1656            state->IRQ_count -= 114;
16291657      }
1630      else if (IRQ_count < 114)
1658      else if (state->IRQ_count < 114)
16311659      {
16321660         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
1633         IRQ_count = (0xffff - 114 + IRQ_count);    // wrap around the 16 bits counter
1661         state->IRQ_count = (0xffff - 114 + state->IRQ_count);    // wrap around the 16 bits counter
16341662      }
16351663      else
1636         IRQ_count -= 114;
1664         state->IRQ_count -= 114;
16371665   }
16381666}
16391667
16401668static WRITE8_HANDLER( mapper18_w )
16411669{
1670   nes_state *state = (nes_state *)space->machine->driver_data;
16421671   UINT8 bank;
16431672   LOG_MMC(("mapper18_w, offset: %04x, data: %02x\n", offset, data));
16441673
16451674   switch (offset & 0x7003)
16461675   {
16471676      case 0x0000:
1648         prg_bank[0] = (prg_bank[0] & 0xf0) | (data & 0x0f);
1649         prg8_89(space->machine, prg_bank[0]);
1677         state->prg_bank[0] = (state->prg_bank[0] & 0xf0) | (data & 0x0f);
1678         prg8_89(space->machine, state->prg_bank[0]);
16501679         break;
16511680      case 0x0001:
1652         prg_bank[0] = (prg_bank[0] & 0x0f) | (data << 4);
1653         prg8_89(space->machine, prg_bank[0]);
1681         state->prg_bank[0] = (state->prg_bank[0] & 0x0f) | (data << 4);
1682         prg8_89(space->machine, state->prg_bank[0]);
16541683         break;
16551684      case 0x0002:
1656         prg_bank[1] = (prg_bank[1] & 0xf0) | (data & 0x0f);
1657         prg8_ab(space->machine, prg_bank[1]);
1685         state->prg_bank[1] = (state->prg_bank[1] & 0xf0) | (data & 0x0f);
1686         prg8_ab(space->machine, state->prg_bank[1]);
16581687         break;
16591688      case 0x0003:
1660         prg_bank[1] = (prg_bank[1] & 0x0f) | (data << 4);
1661         prg8_ab(space->machine, prg_bank[1]);
1689         state->prg_bank[1] = (state->prg_bank[1] & 0x0f) | (data << 4);
1690         prg8_ab(space->machine, state->prg_bank[1]);
16621691         break;
16631692      case 0x1000:
1664         prg_bank[2] = (prg_bank[2] & 0xf0) | (data & 0x0f);
1665         prg8_cd(space->machine, prg_bank[2]);
1693         state->prg_bank[2] = (state->prg_bank[2] & 0xf0) | (data & 0x0f);
1694         prg8_cd(space->machine, state->prg_bank[2]);
16661695         break;
16671696      case 0x1001:
1668         prg_bank[2] = (prg_bank[2] & 0x0f) | (data << 4);
1669         prg8_cd(space->machine, prg_bank[2]);
1697         state->prg_bank[2] = (state->prg_bank[2] & 0x0f) | (data << 4);
1698         prg8_cd(space->machine, state->prg_bank[2]);
16701699         break;
16711700
16721701      /* $9002, 3 (1002, 3) uncaught = Jaleco Baseball writes 0 */
r7955r7956
16781707      case 0x5000: case 0x5001: case 0x5002: case 0x5003:
16791708         bank = ((offset & 0x7000) - 0x2000) / 0x0800 + ((offset & 0x0002) >> 1);
16801709         if (offset & 0x0001)
1681            vrom_bank[bank] = (vrom_bank[bank] & 0x0f) | ((data & 0x0f)<< 4);
1710            state->vrom_bank[bank] = (state->vrom_bank[bank] & 0x0f) | ((data & 0x0f)<< 4);
16821711         else
1683            vrom_bank[bank] = (vrom_bank[bank] & 0xf0) | (data & 0x0f);
1712            state->vrom_bank[bank] = (state->vrom_bank[bank] & 0xf0) | (data & 0x0f);
16841713
1685         chr1_x(space->machine, bank, vrom_bank[bank], CHRROM);
1714         chr1_x(space->machine, bank, state->vrom_bank[bank], CHRROM);
16861715         break;
16871716
16881717      case 0x6000:
1689         IRQ_count_latch = (IRQ_count_latch & 0xfff0) | (data & 0x0f);
1718         state->IRQ_count_latch = (state->IRQ_count_latch & 0xfff0) | (data & 0x0f);
16901719         break;
16911720      case 0x6001:
1692         IRQ_count_latch = (IRQ_count_latch & 0xff0f) | ((data & 0x0f) << 4);
1721         state->IRQ_count_latch = (state->IRQ_count_latch & 0xff0f) | ((data & 0x0f) << 4);
16931722         break;
16941723      case 0x6002:
1695         IRQ_count_latch = (IRQ_count_latch & 0xf0ff) | ((data & 0x0f) << 8);
1724         state->IRQ_count_latch = (state->IRQ_count_latch & 0xf0ff) | ((data & 0x0f) << 8);
16961725         break;
16971726      case 0x6003:
1698         IRQ_count_latch = (IRQ_count_latch & 0x0fff) | ((data & 0x0f) << 12);
1727         state->IRQ_count_latch = (state->IRQ_count_latch & 0x0fff) | ((data & 0x0f) << 12);
16991728         break;
17001729
17011730      case 0x7000:
1702         IRQ_count = IRQ_count_latch;
1731         state->IRQ_count = state->IRQ_count_latch;
17031732         break;
17041733      case 0x7001:
1705         IRQ_enable = data & 0x01;
1706         IRQ_mode = data & 0x0e;
1734         state->IRQ_enable = data & 0x01;
1735         state->IRQ_mode = data & 0x0e;
17071736         break;
17081737
17091738      case 0x7002:
r7955r7956
17391768there are 3 dots to every 1 CPU cycle, hence 114 is the number of cycles per scanline ) */
17401769static void namcot_irq( running_device *device, int scanline, int vblank, int blanked )
17411770{
1742   if (IRQ_enable)
1771   nes_state *state = (nes_state *)device->machine->driver_data;
1772
1773   if (state->IRQ_enable)
17431774   {
1744      if (IRQ_count >= (0x7fff - 114))
1775      if (state->IRQ_count >= (0x7fff - 114))
17451776      {
17461777         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
1747         IRQ_count = 0;
1778         state->IRQ_count = 0;
17481779      }
17491780      else
1750         IRQ_count += 114;
1781         state->IRQ_count += 114;
17511782   }
17521783}
17531784
17541785static WRITE8_HANDLER( mapper19_l_w )
17551786{
1787   nes_state *state = (nes_state *)space->machine->driver_data;
17561788   LOG_MMC(("mapper19_l_w, offset: %04x, data: %02x\n", offset, data));
17571789   offset += 0x100;
17581790
r7955r7956
17621794         LOG_MMC(("Mapper 19 sound port write, data: %02x\n", data));
17631795         break;
17641796      case 0x1000: /* low byte of IRQ */
1765         IRQ_count = (IRQ_count & 0x7f00) | data;
1797         state->IRQ_count = (state->IRQ_count & 0x7f00) | data;
17661798         break;
17671799      case 0x1800: /* high byte of IRQ, IRQ enable in high bit */
1768         IRQ_count = (IRQ_count & 0xff) | ((data & 0x7f) << 8);
1769         IRQ_enable = data & 0x80;
1800         state->IRQ_count = (state->IRQ_count & 0xff) | ((data & 0x7f) << 8);
1801         state->IRQ_enable = data & 0x80;
17701802         break;
17711803   }
17721804}
17731805
17741806static READ8_HANDLER( mapper19_l_r )
17751807{
1808   nes_state *state = (nes_state *)space->machine->driver_data;
17761809   LOG_MMC(("mapper19_l_r, offset: %04x\n", offset));
17771810   offset += 0x100;
17781811
17791812   switch (offset & 0x1800)
17801813   {
17811814      case 0x1000:
1782         return IRQ_count & 0xff;
1815         return state->IRQ_count & 0xff;
17831816      case 0x1800:
1784         return (IRQ_count >> 8) & 0xff;
1817         return (state->IRQ_count >> 8) & 0xff;
17851818      default:
17861819         return 0x00;
17871820   }
r7955r7956
17971830
17981831static WRITE8_HANDLER( mapper19_w )
17991832{
1833   nes_state *state = (nes_state *)space->machine->driver_data;
18001834   LOG_MMC(("mapper19_w, offset: %04x, data: %02x\n", offset, data));
18011835   switch (offset & 0x7800)
18021836   {
r7955r7956
18221856         prg8_89(space->machine, data & 0x3f);
18231857         break;
18241858      case 0x6800:
1825         mmc_cmd1 = data & 0xc0;      // this should enable High CHRRAM, but we still have to properly implement it!
1859         state->mmc_cmd1 = data & 0xc0;      // this should enable High CHRRAM, but we still have to properly implement it!
18261860         prg8_ab(space->machine, data & 0x3f);
18271861         break;
18281862      case 0x7000:
r7955r7956
18491883{
18501884   nes_state *state = (nes_state *)device->machine->driver_data;
18511885
1852   if (IRQ_enable_latch)
1886   if (state->IRQ_enable_latch)
18531887      cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
18541888
1855   if (IRQ_enable)
1889   if (state->IRQ_enable)
18561890   {
1857      if (IRQ_count <= 114)
1891      if (state->IRQ_count <= 114)
18581892      {
18591893         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
1860         IRQ_enable = 0;
1894         state->IRQ_enable = 0;
18611895         state->fds_status0 |= 0x01;
18621896      }
18631897      else
1864         IRQ_count -= 114;
1898         state->IRQ_count -= 114;
18651899   }
18661900}
18671901
r7955r7956
18911925         /* return "no disk" status if disk is unloaded */
18921926         if (state->fds_data == NULL)
18931927            ret = 1;
1894         else if (fds_last_side != state->fds_current_side)
1928         else if (state->fds_last_side != state->fds_current_side)
18951929         {
18961930            /* If we've switched disks, report "no disk" for a few reads */
18971931            ret = 1;
1898            fds_count ++;
1899            if (fds_count == 50)
1932            state->fds_count ++;
1933            if (state->fds_count == 50)
19001934            {
1901               fds_last_side = state->fds_current_side;
1902               fds_count = 0;
1935               state->fds_last_side = state->fds_current_side;
1936               state->fds_count = 0;
19031937            }
19041938         }
19051939         else
r7955r7956
19261960   switch (offset)
19271961   {
19281962      case 0x00:
1929         IRQ_count_latch = (IRQ_count_latch & 0xff00) | data;
1963         state->IRQ_count_latch = (state->IRQ_count_latch & 0xff00) | data;
19301964         break;
19311965      case 0x01:
1932         IRQ_count_latch = (IRQ_count_latch & 0x00ff) | (data << 8);
1966         state->IRQ_count_latch = (state->IRQ_count_latch & 0x00ff) | (data << 8);
19331967         break;
19341968      case 0x02:
1935         IRQ_count = IRQ_count_latch;
1936         IRQ_enable = data;
1969         state->IRQ_count = state->IRQ_count_latch;
1970         state->IRQ_enable = data;
19371971         break;
19381972      case 0x03:
19391973         // d0 = sound io (1 = enable)
r7955r7956
19541988         if ((!(data & 0x40)) && (state->fds_write_reg & 0x40))
19551989            state->fds_head_position -= 2; // ???
19561990
1957         IRQ_enable_latch = BIT(data, 7);
1991         state->IRQ_enable_latch = BIT(data, 7);
19581992         state->fds_write_reg = data;
19591993         break;
19601994   }
r7955r7956
19762010
19772011static void vrc4_set_prg( running_machine *machine )
19782012{
1979   if (mmc_cmd1 & 0x02)
2013   nes_state *state = (nes_state *)machine->driver_data;
2014   if (state->mmc_cmd1 & 0x02)
19802015   {
19812016      prg8_89(machine, 0xfe);
1982      prg8_cd(machine, prg_bank[0]);
2017      prg8_cd(machine, state->prg_bank[0]);
19832018   }
19842019   else
19852020   {
1986      prg8_89(machine, prg_bank[0]);
2021      prg8_89(machine, state->prg_bank[0]);
19872022      prg8_cd(machine, 0xfe);
19882023   }
19892024}
19902025
19912026static void konami_irq( running_device *device, int scanline, int vblank, int blanked )
19922027{
2028   nes_state *state = (nes_state *)device->machine->driver_data;
19932029   /* Increment & check the IRQ scanline counter */
1994   if (IRQ_enable && (++IRQ_count == 0x100))
2030   if (state->IRQ_enable && (++state->IRQ_count == 0x100))
19952031   {
1996      IRQ_count = IRQ_count_latch;
1997      IRQ_enable = IRQ_enable_latch;
2032      state->IRQ_count = state->IRQ_count_latch;
2033      state->IRQ_enable = state->IRQ_enable_latch;
19982034      cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
19992035   }
20002036}
20012037
20022038static WRITE8_HANDLER( konami_vrc4a_w )
20032039{
2040   nes_state *state = (nes_state *)space->machine->driver_data;
20042041   LOG_MMC(("konami_vrc4a_w, offset: %04x, data: %02x\n", offset, data));
20052042
20062043   switch (offset & 0x7fff)
r7955r7956
20122049      case 0x0040:
20132050      case 0x0080:
20142051      case 0x00c0:
2015         prg_bank[0] = data;
2052         state->prg_bank[0] = data;
20162053         vrc4_set_prg(space->machine);
20172054         break;
20182055
r7955r7956
20322069      case 0x1006:
20332070      case 0x1080:
20342071      case 0x10c0:
2035         mmc_cmd1 = data & 0x02;
2072         state->mmc_cmd1 = data & 0x02;
20362073         vrc4_set_prg(space->machine);
20372074         break;
20382075
r7955r7956
20472084         break;
20482085
20492086      case 0x3000:
2050         vrom_bank[0] = (vrom_bank[0] & 0xf0) | (data & 0x0f);
2051         chr1_0(space->machine, vrom_bank[0], CHRROM);
2087         state->vrom_bank[0] = (state->vrom_bank[0] & 0xf0) | (data & 0x0f);
2088         chr1_0(space->machine, state->vrom_bank[0], CHRROM);
20522089         break;
20532090      case 0x3002:
20542091      case 0x3040:
2055         vrom_bank[0] = (vrom_bank[0] & 0x0f) | (data << 4);
2056         chr1_0(space->machine, vrom_bank[0], CHRROM);
2092         state->vrom_bank[0] = (state->vrom_bank[0] & 0x0f) | (data << 4);
2093         chr1_0(space->machine, state->vrom_bank[0], CHRROM);
20572094         break;
20582095      case 0x3004:
20592096      case 0x3080:
2060         vrom_bank[1] = (vrom_bank[1] & 0xf0) | (data & 0x0f);
2061         chr1_1(space->machine, vrom_bank[1], CHRROM);
2097         state->vrom_bank[1] = (state->vrom_bank[1] & 0xf0) | (data & 0x0f);
2098         chr1_1(space->machine, state->vrom_bank[1], CHRROM);
20622099         break;
20632100      case 0x3006:
20642101      case 0x30c0:
2065         vrom_bank[1] = (vrom_bank[1] & 0x0f) | (data << 4);
2066         chr1_1(space->machine, vrom_bank[1], CHRROM);
2102         state->vrom_bank[1] = (state->vrom_bank[1] & 0x0f) | (data << 4);
2103         chr1_1(space->machine, state->vrom_bank[1], CHRROM);
20672104         break;
20682105      case 0x4000:
2069         vrom_bank[2] = (vrom_bank[2] & 0xf0) | (data & 0x0f);
2070         chr1_2(space->machine, vrom_bank[2], CHRROM);
2106         state->vrom_bank[2] = (state->vrom_bank[2] & 0xf0) | (data & 0x0f);
2107         chr1_2(space->machine, state->vrom_bank[2], CHRROM);
20712108         break;
20722109      case 0x4002:
20732110      case 0x4040:
2074         vrom_bank[2] = (vrom_bank[2] & 0x0f) | (data << 4);
2075         chr1_2(space->machine, vrom_bank[2], CHRROM);
2111         state->vrom_bank[2] = (state->vrom_bank[2] & 0x0f) | (data << 4);
2112         chr1_2(space->machine, state->vrom_bank[2], CHRROM);
20762113         break;
20772114      case 0x4004:
20782115      case 0x4080:
2079         vrom_bank[3] = (vrom_bank[3] & 0xf0) | (data & 0x0f);
2080         chr1_3(space->machine, vrom_bank[3], CHRROM);
2116         state->vrom_bank[3] = (state->vrom_bank[3] & 0xf0) | (data & 0x0f);
2117         chr1_3(space->machine, state->vrom_bank[3], CHRROM);
20812118         break;
20822119      case 0x4006:
20832120      case 0x40c0:
2084         vrom_bank[3] = (vrom_bank[3] & 0x0f) | (data << 4);
2085         chr1_3(space->machine, vrom_bank[3], CHRROM);
2121         state->vrom_bank[3] = (state->vrom_bank[3] & 0x0f) | (data << 4);
2122         chr1_3(space->machine, state->vrom_bank[3], CHRROM);
20862123         break;
20872124      case 0x5000:
2088         vrom_bank[4] = (vrom_bank[4] & 0xf0) | (data & 0x0f);
2089         chr1_4(space->machine, vrom_bank[4], CHRROM);
2125         state->vrom_bank[4] = (state->vrom_bank[4] & 0xf0) | (data & 0x0f);
2126         chr1_4(space->machine, state->vrom_bank[4], CHRROM);
20902127         break;
20912128      case 0x5002:
20922129      case 0x5040:
2093         vrom_bank[4] = (vrom_bank[4] & 0x0f) | (data << 4);
2094         chr1_4(space->machine, vrom_bank[4], CHRROM);
2130         state->vrom_bank[4] = (state->vrom_bank[4] & 0x0f) | (data << 4);
2131         chr1_4(space->machine, state->vrom_bank[4], CHRROM);
20952132         break;
20962133      case 0x5004:
20972134      case 0x5080:
2098         vrom_bank[5] = (vrom_bank[5] & 0xf0) | (data & 0x0f);
2099         chr1_5(space->machine, vrom_bank[5], CHRROM);
2135         state->vrom_bank[5] = (state->vrom_bank[5] & 0xf0) | (data & 0x0f);
2136         chr1_5(space->machine, state->vrom_bank[5], CHRROM);
21002137         break;
21012138      case 0x5006:
21022139      case 0x50c0:
2103         vrom_bank[5] = (vrom_bank[5] & 0x0f) | (data << 4);
2104         chr1_5(space->machine, vrom_bank[5], CHRROM);
2140         state->vrom_bank[5] = (state->vrom_bank[5] & 0x0f) | (data << 4);
2141         chr1_5(space->machine, state->vrom_bank[5], CHRROM);
21052142         break;
21062143      case 0x6000:
2107         vrom_bank[6] = (vrom_bank[6] & 0xf0) | (data & 0x0f);
2108         chr1_6(space->machine, vrom_bank[6], CHRROM);
2144         state->vrom_bank[6] = (state->vrom_bank[6] & 0xf0) | (data & 0x0f);
2145         chr1_6(space->machine, state->vrom_bank[6], CHRROM);
21092146         break;
21102147      case 0x6002:
21112148      case 0x6040:
2112         vrom_bank[6] = (vrom_bank[6] & 0x0f) | (data << 4);
2113         chr1_6(space->machine, vrom_bank[6], CHRROM);
2149         state->vrom_bank[6] = (state->vrom_bank[6] & 0x0f) | (data << 4);
2150         chr1_6(space->machine, state->vrom_bank[6], CHRROM);
21142151         break;
21152152      case 0x6004:
21162153      case 0x6080:
2117         vrom_bank[7] = (vrom_bank[7] & 0xf0) | (data & 0x0f);
2118         chr1_7(space->machine, vrom_bank[7], CHRROM);
2154         state->vrom_bank[7] = (state->vrom_bank[7] & 0xf0) | (data & 0x0f);
2155         chr1_7(space->machine, state->vrom_bank[7], CHRROM);
21192156         break;
21202157      case 0x6006:
21212158      case 0x60c0:
2122         vrom_bank[7] = (vrom_bank[7] & 0x0f) | (data << 4);
2123         chr1_7(space->machine, vrom_bank[7], CHRROM);
2159         state->vrom_bank[7] = (state->vrom_bank[7] & 0x0f) | (data << 4);
2160         chr1_7(space->machine, state->vrom_bank[7], CHRROM);
21242161         break;
21252162
21262163      case 0x7000:
2127         IRQ_count_latch = (IRQ_count_latch & 0xf0) | (data & 0x0f);
2164         state->IRQ_count_latch = (state->IRQ_count_latch & 0xf0) | (data & 0x0f);
21282165         break;
21292166      case 0x7002:
21302167      case 0x7040:
2131         IRQ_count_latch = (IRQ_count_latch & 0x0f) | ((data & 0x0f) << 4 );
2168         state->IRQ_count_latch = (state->IRQ_count_latch & 0x0f) | ((data & 0x0f) << 4 );
21322169         break;
21332170      case 0x7004:
21342171      case 0x7080:
2135         IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2136         IRQ_enable = data & 0x02;
2137         IRQ_enable_latch = data & 0x01;
2172         state->IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2173         state->IRQ_enable = data & 0x02;
2174         state->IRQ_enable_latch = data & 0x01;
21382175         if (data & 0x02)
2139            IRQ_count = IRQ_count_latch;
2176            state->IRQ_count = state->IRQ_count_latch;
21402177         break;
21412178      case 0x7006:
21422179      case 0x70c0:
2143         IRQ_enable = IRQ_enable_latch;
2180         state->IRQ_enable = state->IRQ_enable_latch;
21442181         break;
21452182      default:
21462183         LOG_MMC(("konami_vrc4a_w uncaught offset: %04x value: %02x\n", offset, data));
r7955r7956
21502187
21512188static WRITE8_HANDLER( konami_vrc4b_w )
21522189{
2190   nes_state *state = (nes_state *)space->machine->driver_data;
21532191   LOG_MMC(("konami_vrc4b_w, offset: %04x, data: %02x\n", offset, data));
21542192
21552193   switch (offset & 0x7fff)
r7955r7956
21612199      case 0x0004:
21622200      case 0x0008:
21632201      case 0x000c:
2164         prg_bank[0] = data;
2202         state->prg_bank[0] = data;
21652203         vrc4_set_prg(space->machine);
21662204         break;
21672205
r7955r7956
21812219      case 0x1003:
21822220      case 0x1004:
21832221      case 0x100c:
2184         mmc_cmd1 = data & 0x02;
2222         state->mmc_cmd1 = data & 0x02;
21852223         vrc4_set_prg(space->machine);
21862224         break;
21872225
r7955r7956
21962234         break;
21972235
21982236      case 0x3000:
2199         vrom_bank[0] = (vrom_bank[0] & 0xf0) | (data & 0x0f);
2200         chr1_0(space->machine, vrom_bank[0], CHRROM);
2237         state->vrom_bank[0] = (state->vrom_bank[0] & 0xf0) | (data & 0x0f);
2238         chr1_0(space->machine, state->vrom_bank[0], CHRROM);
22012239         break;
22022240      case 0x3002:
22032241      case 0x3008:
2204         vrom_bank[0] = (vrom_bank[0] & 0x0f) | (data << 4);
2205         chr1_0(space->machine, vrom_bank[0], CHRROM);
2242         state->vrom_bank[0] = (state->vrom_bank[0] & 0x0f) | (data << 4);
2243         chr1_0(space->machine, state->vrom_bank[0], CHRROM);
22062244         break;
22072245      case 0x3001:
22082246      case 0x3004:
2209         vrom_bank[1] = (vrom_bank[1] & 0xf0) | (data & 0x0f);
2210         chr1_1(space->machine, vrom_bank[1], CHRROM);
2247         state->vrom_bank[1] = (state->vrom_bank[1] & 0xf0) | (data & 0x0f);
2248         chr1_1(space->machine, state->vrom_bank[1], CHRROM);
22112249         break;
22122250      case 0x3003:
22132251      case 0x300c:
2214         vrom_bank[1] = (vrom_bank[1] & 0x0f) | (data << 4);
2215         chr1_1(space->machine, vrom_bank[1], CHRROM);
2252         state->vrom_bank[1] = (state->vrom_bank[1] & 0x0f) | (data << 4);
2253         chr1_1(space->machine, state->vrom_bank[1], CHRROM);
22162254         break;
22172255      case 0x4000:
2218         vrom_bank[2] = (vrom_bank[2] & 0xf0) | (data & 0x0f);
2219         chr1_2(space->machine, vrom_bank[2], CHRROM);
2256         state->vrom_bank[2] = (state->vrom_bank[2] & 0xf0) | (data & 0x0f);
2257         chr1_2(space->machine, state->vrom_bank[2], CHRROM);
22202258         break;
22212259      case 0x4002:
22222260      case 0x4008:
2223         vrom_bank[2] = (vrom_bank[2] & 0x0f) | (data << 4);
2224         chr1_2(space->machine, vrom_bank[2], CHRROM);
2261         state->vrom_bank[2] = (state->vrom_bank[2] & 0x0f) | (data << 4);
2262         chr1_2(space->machine, state->vrom_bank[2], CHRROM);
22252263         break;
22262264      case 0x4001:
22272265      case 0x4004:
2228         vrom_bank[3] = (vrom_bank[3] & 0xf0) | (data & 0x0f);
2229         chr1_3(space->machine, vrom_bank[3], CHRROM);
2266         state->vrom_bank[3] = (state->vrom_bank[3] & 0xf0) | (data & 0x0f);
2267         chr1_3(space->machine, state->vrom_bank[3], CHRROM);
22302268         break;
22312269      case 0x4003:
22322270      case 0x400c:
2233         vrom_bank[3] = (vrom_bank[3] & 0x0f) | (data << 4);
2234         chr1_3(space->machine, vrom_bank[3], CHRROM);
2271         state->vrom_bank[3] = (state->vrom_bank[3] & 0x0f) | (data << 4);
2272         chr1_3(space->machine, state->vrom_bank[3], CHRROM);
22352273         break;
22362274      case 0x5000:
2237         vrom_bank[4] = (vrom_bank[4] & 0xf0) | (data & 0x0f);
2238         chr1_4(space->machine, vrom_bank[4], CHRROM);
2275         state->vrom_bank[4] = (state->vrom_bank[4] & 0xf0) | (data & 0x0f);
2276         chr1_4(space->machine, state->vrom_bank[4], CHRROM);
22392277         break;
22402278      case 0x5002:
22412279      case 0x5008:
2242         vrom_bank[4] = (vrom_bank[4] & 0x0f) | (data << 4);
2243         chr1_4(space->machine, vrom_bank[4], CHRROM);
2280         state->vrom_bank[4] = (state->vrom_bank[4] & 0x0f) | (data << 4);
2281         chr1_4(space->machine, state->vrom_bank[4], CHRROM);
22442282         break;
22452283      case 0x5001:
22462284      case 0x5004:
2247         vrom_bank[5] = (vrom_bank[5] & 0xf0) | (data & 0x0f);
2248         chr1_5(space->machine, vrom_bank[5], CHRROM);
2285         state->vrom_bank[5] = (state->vrom_bank[5] & 0xf0) | (data & 0x0f);
2286         chr1_5(space->machine, state->vrom_bank[5], CHRROM);
22492287         break;
22502288      case 0x5003:
22512289      case 0x500c:
2252         vrom_bank[5] = (vrom_bank[5] & 0x0f) | (data << 4);
2253         chr1_5(space->machine, vrom_bank[5], CHRROM);
2290         state->vrom_bank[5] = (state->vrom_bank[5] & 0x0f) | (data << 4);
2291         chr1_5(space->machine, state->vrom_bank[5], CHRROM);
22542292         break;
22552293      case 0x6000:
2256         vrom_bank[6] = (vrom_bank[6] & 0xf0) | (data & 0x0f);
2257         chr1_6(space->machine, vrom_bank[6], CHRROM);
2294         state->vrom_bank[6] = (state->vrom_bank[6] & 0xf0) | (data & 0x0f);
2295         chr1_6(space->machine, state->vrom_bank[6], CHRROM);
22582296         break;
22592297      case 0x6002:
22602298      case 0x6008:
2261         vrom_bank[6] = (vrom_bank[6] & 0x0f) | (data << 4);
2262         chr1_6(space->machine, vrom_bank[6], CHRROM);
2299         state->vrom_bank[6] = (state->vrom_bank[6] & 0x0f) | (data << 4);
2300         chr1_6(space->machine, state->vrom_bank[6], CHRROM);
22632301         break;
22642302      case 0x6001:
22652303      case 0x6004:
2266         vrom_bank[7] = (vrom_bank[7] & 0xf0) | (data & 0x0f);
2267         chr1_7(space->machine, vrom_bank[7], CHRROM);
2304         state->vrom_bank[7] = (state->vrom_bank[7] & 0xf0) | (data & 0x0f);
2305         chr1_7(space->machine, state->vrom_bank[7], CHRROM);
22682306         break;
22692307      case 0x6003:
22702308      case 0x600c:
2271         vrom_bank[7] = (vrom_bank[7] & 0x0f) | (data << 4);
2272         chr1_7(space->machine, vrom_bank[7], CHRROM);
2309         state->vrom_bank[7] = (state->vrom_bank[7] & 0x0f) | (data << 4);
2310         chr1_7(space->machine, state->vrom_bank[7], CHRROM);
22732311         break;
22742312
22752313      case 0x7000:
2276         IRQ_count_latch = (IRQ_count_latch & 0xf0) | (data & 0x0f);
2314         state->IRQ_count_latch = (state->IRQ_count_latch & 0xf0) | (data & 0x0f);
22772315         break;
22782316      case 0x7002:
22792317      case 0x7008:
2280         IRQ_count_latch = (IRQ_count_latch & 0x0f) | ((data & 0x0f) << 4 );
2318         state->IRQ_count_latch = (state->IRQ_count_latch & 0x0f) | ((data & 0x0f) << 4 );
22812319         break;
22822320      case 0x7001:
22832321      case 0x7004:
2284         IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2285         IRQ_enable = data & 0x02;
2286         IRQ_enable_latch = data & 0x01;
2322         state->IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2323         state->IRQ_enable = data & 0x02;
2324         state->IRQ_enable_latch = data & 0x01;
22872325         if (data & 0x02)
2288            IRQ_count = IRQ_count_latch;
2326            state->IRQ_count = state->IRQ_count_latch;
22892327         break;
22902328      case 0x7003:
22912329      case 0x700c:
2292         IRQ_enable = IRQ_enable_latch;
2330         state->IRQ_enable = state->IRQ_enable_latch;
22932331         break;
22942332      default:
22952333         LOG_MMC(("konami_vrc4b_w uncaught offset: %04x value: %02x\n", offset, data));
r7955r7956
23102348
23112349static WRITE8_HANDLER( konami_vrc2a_w )
23122350{
2351   nes_state *state = (nes_state *)space->machine->driver_data;
23132352   UINT8 bank;
23142353   LOG_MMC(("konami_vrc2a_w, offset: %04x, data: %02x\n", offset, data));
23152354
r7955r7956
23352374   case 0x5000:
23362375   case 0x6000:
23372376      bank = ((offset & 0x7000) - 0x3000) / 0x0800 + (offset & 0x0001);
2338      /* Notice that we store the banks as in other VRC, but then we take vrom_bank>>1 because it's VRC2A!*/
2377      /* Notice that we store the banks as in other VRC, but then we take state->vrom_bank>>1 because it's VRC2A!*/
23392378      if (offset & 0x0002)
2340         vrom_bank[bank] = (vrom_bank[bank] & 0x0f) | (data << 4);
2379         state->vrom_bank[bank] = (state->vrom_bank[bank] & 0x0f) | (data << 4);
23412380      else
2342         vrom_bank[bank] = (vrom_bank[bank] & 0xf0) | (data & 0x0f);
2381         state->vrom_bank[bank] = (state->vrom_bank[bank] & 0xf0) | (data & 0x0f);
23432382
2344      chr1_x(space->machine, bank, vrom_bank[bank] >> 1, CHRROM);
2383      chr1_x(space->machine, bank, state->vrom_bank[bank] >> 1, CHRROM);
23452384      break;
23462385
23472386   default:
r7955r7956
23632402
23642403static WRITE8_HANDLER( konami_vrc2b_w )
23652404{
2405   nes_state *state = (nes_state *)space->machine->driver_data;
23662406   UINT16 select;
23672407   UINT8 bank;
23682408
r7955r7956
24172457   case 0x6003:
24182458      bank = ((select & 0x7000) - 0x3000) / 0x0800 + ((select & 0x0002) >> 1);
24192459      if (select & 0x0001)
2420         vrom_bank[bank] = (vrom_bank[bank] & 0x0f) | (data << 4);
2460         state->vrom_bank[bank] = (state->vrom_bank[bank] & 0x0f) | (data << 4);
24212461      else
2422         vrom_bank[bank] = (vrom_bank[bank] & 0xf0) | (data & 0x0f);
2462         state->vrom_bank[bank] = (state->vrom_bank[bank] & 0xf0) | (data & 0x0f);
24232463
2424      chr1_x(space->machine, bank, vrom_bank[bank], mmc_chr_source);
2464      chr1_x(space->machine, bank, state->vrom_bank[bank], state->mmc_chr_source);
24252465      break;
24262466   case 0x7000:
2427      IRQ_count_latch &= ~0x0f;
2428      IRQ_count_latch |= data & 0x0f;
2467      state->IRQ_count_latch &= ~0x0f;
2468      state->IRQ_count_latch |= data & 0x0f;
24292469      break;
24302470   case 0x7001:
2431      IRQ_count_latch &= ~0xf0;
2432      IRQ_count_latch |= (data << 4) & 0xf0;
2471      state->IRQ_count_latch &= ~0xf0;
2472      state->IRQ_count_latch |= (data << 4) & 0xf0;
24332473      break;
24342474   case 0x7002:
2435      IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2436      IRQ_enable = data & 0x02;
2437      IRQ_enable_latch = data & 0x01;
2475      state->IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2476      state->IRQ_enable = data & 0x02;
2477      state->IRQ_enable_latch = data & 0x01;
24382478      if (data & 0x02)
2439         IRQ_count = IRQ_count_latch;
2479         state->IRQ_count = state->IRQ_count_latch;
24402480      break;
24412481
24422482   default:
r7955r7956
24582498
24592499static WRITE8_HANDLER( konami_vrc6a_w )
24602500{
2501   nes_state *state = (nes_state *)space->machine->driver_data;
24612502   UINT8 bank;
24622503   LOG_MMC(("konami_vrc6a_w, offset: %04x, data: %02x\n", offset, data));
24632504
r7955r7956
25022543         break;
25032544
25042545      case 0x7000:
2505         IRQ_count_latch = data;
2546         state->IRQ_count_latch = data;
25062547         break;
25072548      case 0x7001:
2508         IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2509         IRQ_enable = data & 0x02;
2510         IRQ_enable_latch = data & 0x01;
2549         state->IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2550         state->IRQ_enable = data & 0x02;
2551         state->IRQ_enable_latch = data & 0x01;
25112552         if (data & 0x02)
2512            IRQ_count = IRQ_count_latch;
2553            state->IRQ_count = state->IRQ_count_latch;
25132554         break;
25142555      case 0x7002:
2515         IRQ_enable = IRQ_enable_latch;
2556         state->IRQ_enable = state->IRQ_enable_latch;
25162557         break;
25172558
25182559      default:
r7955r7956
25342575
25352576static WRITE8_HANDLER( konami_vrc6b_w )
25362577{
2578   nes_state *state = (nes_state *)space->machine->driver_data;
25372579   UINT8 bank;
25382580
25392581   LOG_MMC(("konami_vrc6b_w, offset: %04x, data: %02x\n", offset, data));
r7955r7956
25722614         chr1_x(space->machine, bank, data, CHRROM);
25732615         break;
25742616      case 0x7000:
2575         IRQ_count_latch = data;
2617         state->IRQ_count_latch = data;
25762618         break;
25772619      case 0x7001:
2578         IRQ_enable = IRQ_enable_latch;
2620         state->IRQ_enable = state->IRQ_enable_latch;
25792621         break;
25802622      case 0x7002:
2581         IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2582         IRQ_enable = data & 0x02;
2583         IRQ_enable_latch = data & 0x01;
2623         state->IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
2624         state->IRQ_enable = data & 0x02;
2625         state->IRQ_enable_latch = data & 0x01;
25842626         if (data & 0x02)
2585            IRQ_count = IRQ_count_latch;
2627            state->IRQ_count = state->IRQ_count_latch;
25862628         break;
25872629
25882630      default:
r7955r7956
26692711   switch (offset & 0x7000)
26702712   {
26712713      case 0x0000:
2672         mmc_cmd1 ? prg8_cd(space->machine, data) : prg8_89(space->machine, data);
2714         state->mmc_cmd1 ? prg8_cd(space->machine, data) : prg8_89(space->machine, data);
26732715         break;
26742716      case 0x1000:
2675         mmc_cmd1 = data & 0x02;
2717         state->mmc_cmd1 = data & 0x02;
26762718         if (!state->crc_hack)   // Major League has hardwired mirroring (it would have required a separate mapper)
26772719            set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
26782720         break;
r7955r7956
27982840
27992841static void mapper35_irq( running_device *device, int scanline, int vblank, int blanked )
28002842{
2801   if (scanline < PPU_BOTTOM_VISIBLE_SCANLINE && IRQ_enable)
2843   nes_state *state = (nes_state *)device->machine->driver_data;
2844
2845   if (scanline < PPU_BOTTOM_VISIBLE_SCANLINE && state->IRQ_enable)
28022846   {
2803      IRQ_count--;
2847      state->IRQ_count--;
28042848
2805      if (!blanked && (IRQ_count == 0))
2849      if (!blanked && (state->IRQ_count == 0))
28062850      {
28072851         LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline,
28082852               video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen)));
28092853         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
2810         IRQ_enable = 0;
2854         state->IRQ_enable = 0;
28112855      }
28122856   }
28132857}
28142858
28152859static WRITE8_HANDLER( mapper35_w )
28162860{
2861   nes_state *state = (nes_state *)space->machine->driver_data;
28172862   LOG_MMC(("mapper35_w, offset: %04x, data: %02x\n", offset, data));
28182863
28192864   switch (offset)
r7955r7956
28252870      prg8_ab(space->machine, data);
28262871      break;
28272872   case 0x0002:
2828//      prg_bank[offset & 0x02] = data;
2873//      state->prg_bank[offset & 0x02] = data;
28292874      prg8_cd(space->machine, data);
28302875      break;
28312876   case 0x1000:
r7955r7956
28362881   case 0x1005:
28372882   case 0x1006:
28382883   case 0x1007:
2839//      vrom_bank[offset & 0x07] = data;
2884//      state->vrom_bank[offset & 0x07] = data;
28402885      chr1_x(space->machine, offset & 0x07, data, CHRROM);
28412886      break;
28422887   case 0x4002:
2843      IRQ_enable = 0;
2888      state->IRQ_enable = 0;
28442889      break;
28452890   case 0x4003:
2846      IRQ_enable = 1;
2891      state->IRQ_enable = 1;
28472892      break;
28482893   case 0x4005:
2849      IRQ_count = data;
2894      state->IRQ_count = data;
28502895      break;
28512896   case 0x5001:
28522897      set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
r7955r7956
28912936
28922937static WRITE8_HANDLER( mapper37_m_w )
28932938{
2939   nes_state *state = (nes_state *)space->machine->driver_data;
28942940   UINT8 map37_helper;
28952941   LOG_MMC(("mapper37_m_w, offset: %04x, data: %02x\n", offset, data));
28962942
28972943   map37_helper = (data & 0x06) >> 1;
28982944
2899   mmc_prg_base = map37_helper << 3;
2900   mmc_prg_mask = (map37_helper == 2) ? 0x0f : 0x07;
2901   mmc_chr_base = map37_helper << 6;
2902   mmc_chr_mask = 0x7f;
2903   mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
2904   mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
2945   state->mmc_prg_base = map37_helper << 3;
2946   state->mmc_prg_mask = (map37_helper == 2) ? 0x0f : 0x07;
2947   state->mmc_chr_base = map37_helper << 6;
2948   state->mmc_chr_mask = 0x7f;
2949   mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
2950   mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
29052951}
29062952
29072953/*************************************************************
r7955r7956
29543000
29553001static void mapper40_irq( running_device *device, int scanline, int vblank, int blanked )
29563002{
2957   if (IRQ_enable)
3003   nes_state *state = (nes_state *)device->machine->driver_data;
3004
3005   if (state->IRQ_enable)
29583006   {
2959      if ((0xfff - IRQ_count) <= 114)
3007      if ((0xfff - state->IRQ_count) <= 114)
29603008      {
2961         IRQ_count = (IRQ_count + 1) & 0xfff;
2962         IRQ_enable = 0;
3009         state->IRQ_count = (state->IRQ_count + 1) & 0xfff;
3010         state->IRQ_enable = 0;
29633011         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
29643012      }
29653013      else
2966         IRQ_count += 114;
3014         state->IRQ_count += 114;
29673015   }
29683016}
29693017
29703018static WRITE8_HANDLER( mapper40_w )
29713019{
3020   nes_state *state = (nes_state *)space->machine->driver_data;
29723021   LOG_MMC(("mapper40_w, offset: %04x, data: %02x\n", offset, data));
29733022
29743023   switch (offset & 0x6000)
29753024   {
29763025      case 0x0000:
2977         IRQ_enable = 0;
2978         IRQ_count = 0;
3026         state->IRQ_enable = 0;
3027         state->IRQ_count = 0;
29793028         break;
29803029      case 0x2000:
2981         IRQ_enable = 1;
3030         state->IRQ_enable = 1;
29823031         break;
29833032      case 0x6000:
29843033         prg8_cd(space->machine, data);
r7955r7956
29993048
30003049static WRITE8_HANDLER( mapper41_m_w )
30013050{
3051   nes_state *state = (nes_state *)space->machine->driver_data;
30023052   LOG_MMC(("mapper41_m_w, offset: %04x, data: %02x\n", offset, data));
30033053
3004   mmc_cmd1 = offset & 0xff;
3054   state->mmc_cmd1 = offset & 0xff;
30053055   set_nt_mirroring(space->machine, BIT(data, 5) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
30063056   prg32(space->machine, offset & 0x07);
30073057}
30083058
30093059static WRITE8_HANDLER( mapper41_w )
30103060{
3061   nes_state *state = (nes_state *)space->machine->driver_data;
30113062   LOG_MMC(("mapper41_w, offset: %04x, data: %02x\n", offset, data));
30123063
3013   if (mmc_cmd1 & 0x04)
3014      chr8(space->machine, ((mmc_cmd1 & 0x18) >> 1) | (data & 0x03), CHRROM);
3064   if (state->mmc_cmd1 & 0x04)
3065      chr8(space->machine, ((state->mmc_cmd1 & 0x18) >> 1) | (data & 0x03), CHRROM);
30153066}
30163067
30173068/*************************************************************
r7955r7956
30293080
30303081static WRITE8_HANDLER( mapper42_w )
30313082{
3083   nes_state *state = (nes_state *)space->machine->driver_data;
30323084   LOG_MMC(("mapper42_w, offset: %04x, data: %02x\n", offset, data));
30333085
30343086   if (offset >= 0x7000)
r7955r7956
30433095         break;
30443096      case 0x02:
30453097         /* Check if IRQ is being enabled */
3046         if (!IRQ_enable && (data & 0x02))
3098         if (!state->IRQ_enable && (data & 0x02))
30473099         {
3048            IRQ_enable = 1;
3100            state->IRQ_enable = 1;
30493101            timer_adjust_oneshot(nes_irq_timer, cputag_clocks_to_attotime(space->machine, "maincpu", 24576), 0);
30503102         }
30513103         if (!(data & 0x02))
30523104         {
3053            IRQ_enable = 0;
3105            state->IRQ_enable = 0;
30543106            timer_adjust_oneshot(nes_irq_timer, attotime_never, 0);
30553107         }
30563108         break;
r7955r7956
31383190
31393191static WRITE8_HANDLER( mapper44_w )
31403192{
3193   nes_state *state = (nes_state *)space->machine->driver_data;
31413194   UINT8 page;
31423195   LOG_MMC(("mapper44_w offset: %04x, data: %02x\n", offset, data));
31433196
r7955r7956
31483201      if (page > 6)
31493202         page = 6;
31503203
3151      mmc_prg_base = page << 4;
3152      mmc_prg_mask = (page > 5) ? 0x1f : 0x0f;
3153      mmc_chr_base = page << 7;
3154      mmc_chr_mask = (page > 5) ? 0xff : 0x7f;
3155      mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
3156      mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
3204      state->mmc_prg_base = page << 4;
3205      state->mmc_prg_mask = (page > 5) ? 0x1f : 0x0f;
3206      state->mmc_chr_base = page << 7;
3207      state->mmc_chr_mask = (page > 5) ? 0xff : 0x7f;
3208      mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
3209      mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
31573210      break;
31583211
31593212   default:
r7955r7956
31843237      state->wram[offset] = data;
31853238   else
31863239   {
3187      mapper45_reg[mmc_count] = data;
3188      mmc_count = (mmc_count + 1) & 0x03;
3240      mapper45_reg[state->mmc_count] = data;
3241      state->mmc_count = (state->mmc_count + 1) & 0x03;
31893242
3190      if (!mmc_count)
3243      if (!state->mmc_count)
31913244      {
31923245         LOG_MMC(("mapper45_m_w, command completed %02x %02x %02x %02x\n", mapper45_reg[3],
31933246            mapper45_reg[2], mapper45_reg[1], mapper45_reg[0]));
31943247
3195         mmc_prg_base = mapper45_reg[1];
3196         mmc_prg_mask = 0x3f ^ (mapper45_reg[3] & 0x3f);
3197         mmc_chr_base = ((mapper45_reg[2] & 0xf0) << 4) + mapper45_reg[0];
3198         mmc_chr_mask = 0xff >> (~mapper45_reg[2] & 0x0f);
3199         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
3200         mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
3248         state->mmc_prg_base = mapper45_reg[1];
3249         state->mmc_prg_mask = 0x3f ^ (mapper45_reg[3] & 0x3f);
3250         state->mmc_chr_base = ((mapper45_reg[2] & 0xf0) << 4) + mapper45_reg[0];
3251         state->mmc_chr_mask = 0xff >> (~mapper45_reg[2] & 0x0f);
3252         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
3253         mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
32013254      }
32023255   }
32033256}
r7955r7956
32153268
32163269static WRITE8_HANDLER( mapper46_m_w )
32173270{
3271   nes_state *state = (nes_state *)space->machine->driver_data;
32183272   LOG_MMC(("mapper46_m_w, offset: %04x, data: %02x\n", offset, data));
32193273
3220   prg_bank[0] = (prg_bank[0] & 0x01) | ((data & 0x0f) << 1);
3221   vrom_bank[0] = (vrom_bank[0] & 0x07) | ((data & 0xf0) >> 1);
3222   prg32(space->machine, prg_bank[0]);
3223   chr8(space->machine, vrom_bank[0], CHRROM);
3274   state->prg_bank[0] = (state->prg_bank[0] & 0x01) | ((data & 0x0f) << 1);
3275   state->vrom_bank[0] = (state->vrom_bank[0] & 0x07) | ((data & 0xf0) >> 1);
3276   prg32(space->machine, state->prg_bank[0]);
3277   chr8(space->machine, state->vrom_bank[0], CHRROM);
32243278}
32253279
32263280static WRITE8_HANDLER( mapper46_w )
32273281{
3282   nes_state *state = (nes_state *)space->machine->driver_data;
32283283   LOG_MMC(("mapper46_w, offset: %04x, data: %02x\n", offset, data));
32293284
3230   prg_bank[0] = (prg_bank[0] & ~0x01) | (data & 0x01);
3231   vrom_bank[0] = (vrom_bank[0] & ~0x07) | ((data & 0x70) >> 4);
3232   prg32(space->machine, prg_bank[0]);
3233   chr8(space->machine, vrom_bank[0], CHRROM);
3285   state->prg_bank[0] = (state->prg_bank[0] & ~0x01) | (data & 0x01);
3286   state->vrom_bank[0] = (state->vrom_bank[0] & ~0x07) | ((data & 0x70) >> 4);
3287   prg32(space->machine, state->prg_bank[0]);
3288   chr8(space->machine, state->vrom_bank[0], CHRROM);
32343289}
32353290
32363291/*************************************************************
r7955r7956
32463301
32473302static WRITE8_HANDLER( mapper47_m_w )
32483303{
3304   nes_state *state = (nes_state *)space->machine->driver_data;
32493305   LOG_MMC(("mapper47_m_w, offset: %04x, data: %02x\n", offset, data));
32503306
3251   mmc_prg_base = (data & 0x01) << 4;
3252   mmc_prg_mask = 0x0f;
3253   mmc_chr_base = (data & 0x01) << 7;
3254   mmc_chr_mask = 0x7f;
3255   mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
3256   mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
3307   state->mmc_prg_base = (data & 0x01) << 4;
3308   state->mmc_prg_mask = 0x0f;
3309   state->mmc_chr_base = (data & 0x01) << 7;
3310   state->mmc_chr_mask = 0x7f;
3311   mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
3312   mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
32573313}
32583314
32593315/*************************************************************
r7955r7956
32763332
32773333static WRITE8_HANDLER( mapper48_w )
32783334{
3335   nes_state *state = (nes_state *)space->machine->driver_data;
32793336   LOG_MMC(("mapper48_w, offset: %04x, data: %02x\n", offset, data));
32803337
32813338   switch (offset & 0x6003)
r7955r7956
33053362         chr1_7(space->machine, data, CHRROM);
33063363         break;
33073364      case 0x4000:
3308         IRQ_count_latch = (0x100 - data) & 0xff;
3365         state->IRQ_count_latch = (0x100 - data) & 0xff;
33093366         break;
33103367      case 0x4001:
3311         IRQ_count = IRQ_count_latch;
3368         state->IRQ_count = state->IRQ_count_latch;
33123369         break;
33133370      case 0x4002:
3314         IRQ_enable = 1;
3371         state->IRQ_enable = 1;
33153372         break;
33163373      case 0x4003:
3317         IRQ_enable = 0;
3374         state->IRQ_enable = 0;
33183375         break;
33193376      case 0x6000:
33203377         set_nt_mirroring(space->machine, BIT(data, 6) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
r7955r7956
33353392
33363393static WRITE8_HANDLER( mapper49_m_w )
33373394{
3395   nes_state *state = (nes_state *)space->machine->driver_data;
33383396   LOG_MMC(("mapper49_m_w, offset: %04x, data: %02x\n", offset, data));
33393397
33403398   /* mid writes only work when WRAM is enabled. not sure if I should
3341       change the condition to mmc_cmd2==0x80 (i.e. what is the effect of
3399       change the condition to state->mmc_cmd2==0x80 (i.e. what is the effect of
33423400       the read-only bit?) */
3343   if (mmc_cmd2 & 0x80)
3401   if (state->mmc_cmd2 & 0x80)
33443402   {
33453403      if (data & 0x01)   /* if this is 0, then we have 32k PRG blocks */
33463404      {
3347         mmc_prg_base = (data & 0xc0) >> 2;
3348         mmc_prg_mask = 0x0f;
3349         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
3405         state->mmc_prg_base = (data & 0xc0) >> 2;
3406         state->mmc_prg_mask = 0x0f;
3407         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
33503408      }
33513409      else
33523410         prg32(space->machine, (data & 0x30) >> 4);
33533411
3354      mmc_chr_base = (data & 0xc0) << 1;
3355      mmc_chr_mask = 0x7f;
3356      mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
3412      state->mmc_chr_base = (data & 0xc0) << 1;
3413      state->mmc_chr_mask = 0x7f;
3414      mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
33573415   }
33583416}
33593417
r7955r7956
33723430
33733431static void mapper50_irq( running_device *device, int scanline, int vblank, int blanked )
33743432{
3375   if (IRQ_enable)
3433   nes_state *state = (nes_state *)device->machine->driver_data;
3434   if (state->IRQ_enable)
33763435   {
3377      if (IRQ_count < 0x1000)
3436      if (state->IRQ_count < 0x1000)
33783437      {
3379         if ((0x1000 - IRQ_count) <= 114)
3438         if ((0x1000 - state->IRQ_count) <= 114)
33803439            cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
33813440         else
3382            IRQ_count += 114;
3441            state->IRQ_count += 114;
33833442      }
33843443      else
3385         IRQ_count += 114;
3444         state->IRQ_count += 114;
33863445
3387      IRQ_count &= 0xffff;   // according to docs is 16bit counter -> it wraps only after 0xffff
3446      state->IRQ_count &= 0xffff;   // according to docs is 16bit counter -> it wraps only after 0xffff
33883447   }
33893448}
33903449
33913450static WRITE8_HANDLER( mapper50_l_w )
33923451{
3452   nes_state *state = (nes_state *)space->machine->driver_data;
33933453   UINT8 prg;
33943454   LOG_MMC(("mapper50_l_w, offset: %04x, data: %02x\n", offset, data));
33953455   offset += 0x100;
r7955r7956
34013461      prg8_cd(space->machine, prg);
34023462      break;
34033463   case 0x120:
3404      IRQ_enable = data & 0x01;
3464      state->IRQ_enable = data & 0x01;
34053465      break;
34063466   }
34073467}
r7955r7956
34873547   LOG_MMC(("mapper52_m_w, offset: %04x, data: %02x\n", offset, data));
34883548
34893549   /* mid writes only work when WRAM is enabled. not sure if I should
3490       change the condition to mmc_cmd2==0x80 (i.e. what is the effect of
3550       change the condition to state->mmc_cmd2==0x80 (i.e. what is the effect of
34913551       the read-only bit?) and it only can happen once! */
3492   if ((mmc_cmd2 & 0x80) && !map52_reg_written)
3552   if ((state->mmc_cmd2 & 0x80) && !map52_reg_written)
34933553   {
34943554      map52_helper1 = (data & 0x08);
34953555      map52_helper2 = (data & 0x40);
34963556
3497      mmc_prg_base = map52_helper1 ? ((data & 0x07) << 4) : ((data & 0x06) << 4);
3498      mmc_prg_mask = map52_helper1 ? 0x0f : 0x1f;
3499      mmc_chr_base = ((data & 0x20) << 4) | ((data & 0x04) << 6) | (map52_helper2 ? ((data & 0x10) << 3) : 0);
3500      mmc_chr_mask = map52_helper2 ? 0x7f : 0xff;
3501      mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
3502      mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
3557      state->mmc_prg_base = map52_helper1 ? ((data & 0x07) << 4) : ((data & 0x06) << 4);
3558      state->mmc_prg_mask = map52_helper1 ? 0x0f : 0x1f;
3559      state->mmc_chr_base = ((data & 0x20) << 4) | ((data & 0x04) << 6) | (map52_helper2 ? ((data & 0x10) << 3) : 0);
3560      state->mmc_chr_mask = map52_helper2 ? 0x7f : 0xff;
3561      mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
3562      mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
35033563
35043564      map52_reg_written = 1;
35053565   }
r7955r7956
35753635
35763636static WRITE8_HANDLER( mapper57_w )
35773637{
3638   nes_state *state = (nes_state *)space->machine->driver_data;
35783639   LOG_MMC(("mapper57_w, offset: %04x, data: %02x\n", offset, data));
35793640
35803641   if (offset & 0x0800)
3581   {
3582      mmc_cmd2 = data;
3583   }
3642      state->mmc_cmd2 = data;
35843643   else
3585   {
3586      mmc_cmd1 = data;
3587   }
3644      state->mmc_cmd1 = data;
35883645
3589   if (mmc_cmd2 & 0x80)
3590   {
3591      prg32(space->machine, 2 | (mmc_cmd2 >> 6));
3592   }
3646   if (state->mmc_cmd2 & 0x80)
3647      prg32(space->machine, 2 | (state->mmc_cmd2 >> 6));
35933648   else
35943649   {
3595      prg16_89ab(space->machine, (mmc_cmd2 >> 5) & 0x03);
3596      prg16_cdef(space->machine, (mmc_cmd2 >> 5) & 0x03);
3650      prg16_89ab(space->machine, (state->mmc_cmd2 >> 5) & 0x03);
3651      prg16_cdef(space->machine, (state->mmc_cmd2 >> 5) & 0x03);
35973652   }
35983653
3599   set_nt_mirroring(space->machine, (mmc_cmd2 & 0x08) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
3654   set_nt_mirroring(space->machine, (state->mmc_cmd2 & 0x08) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
36003655
3601   chr8(space->machine, (mmc_cmd1 & 0x03) | (mmc_cmd2 & 0x07) | ((mmc_cmd2 & 0x10) >> 1), CHRROM);
3656   chr8(space->machine, (state->mmc_cmd1 & 0x03) | (state->mmc_cmd2 & 0x07) | ((state->mmc_cmd2 & 0x10) >> 1), CHRROM);
36023657}
36033658
36043659/*************************************************************
r7955r7956
37363791
37373792static void mapper64_irq( running_device *device, int scanline, int vblank, int blanked )
37383793{
3739   if (!IRQ_mode)   // we are in scanline mode!
3794   nes_state *state = (nes_state *)device->machine->driver_data;
3795   if (!state->IRQ_mode)   // we are in scanline mode!
37403796   {
37413797      if (scanline < PPU_BOTTOM_VISIBLE_SCANLINE)
37423798      {
3743         if (!IRQ_reset)
3799         if (!state->IRQ_reset)
37443800         {
3745            if (!IRQ_count)
3746               IRQ_count = IRQ_count_latch;
3801            if (!state->IRQ_count)
3802               state->IRQ_count = state->IRQ_count_latch;
37473803            else
37483804            {
3749               IRQ_count--;
3750               if (IRQ_enable && !blanked && !IRQ_count)
3805               state->IRQ_count--;
3806               if (state->IRQ_enable && !blanked && !state->IRQ_count)
37513807               {
37523808                  LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline,
37533809                        video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen)));
r7955r7956
37573813         }
37583814         else
37593815         {
3760            IRQ_reset = 0;
3761            IRQ_count = IRQ_count_latch + 1;
3816            state->IRQ_reset = 0;
3817            state->IRQ_count = state->IRQ_count_latch + 1;
37623818         }
37633819      }
37643820   }
r7955r7956
37673823     --> Skulls and Crossbones does not show anything!! */
37683824   else
37693825   {
3770//      if (!IRQ_reset)
3826//      if (!state->IRQ_reset)
37713827      {
3772         if (IRQ_count <= 114)
3773            IRQ_count = IRQ_count_latch;
3828         if (state->IRQ_count <= 114)
3829            state->IRQ_count = state->IRQ_count_latch;
37743830         else
37753831         {
3776            IRQ_count -= 114;
3777            if (IRQ_enable && !blanked && (IRQ_count <= 114))
3832            state->IRQ_count -= 114;
3833            if (state->IRQ_enable && !blanked && (state->IRQ_count <= 114))
37783834            {
37793835               LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline,
37803836                     video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen)));
r7955r7956
37843840      }
37853841//      else
37863842//      {
3787//          IRQ_reset = 0;
3788//          IRQ_count = IRQ_count_latch + 1;
3843//          state->IRQ_reset = 0;
3844//          state->IRQ_count = state->IRQ_count_latch + 1;
37893845//      }
37903846   }
37913847}
37923848
37933849static void mapper64_set_prg( running_machine *machine )
37943850{
3795   UINT8 prg_mode = mmc_cmd1 & 0x40;
3851   nes_state *state = (nes_state *)machine->driver_data;
3852   UINT8 prg_mode = state->mmc_cmd1 & 0x40;
37963853
3797   prg8_89(machine, prg_bank[prg_mode ? 2: 0]);
3798   prg8_ab(machine, prg_bank[prg_mode ? 0: 1]);
3799   prg8_cd(machine, prg_bank[prg_mode ? 1: 2]);
3854   prg8_89(machine, state->prg_bank[prg_mode ? 2: 0]);
3855   prg8_ab(machine, state->prg_bank[prg_mode ? 0: 1]);
3856   prg8_cd(machine, state->prg_bank[prg_mode ? 1: 2]);
38003857}
38013858
38023859static void mapper64_set_chr( running_machine *machine )
38033860{
3804   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
3861   nes_state *state = (nes_state *)machine->driver_data;
3862   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
38053863
3806   if (mmc_cmd1 & 0x20)
3864   if (state->mmc_cmd1 & 0x20)
38073865   {
3808      chr1_x(machine, 0 ^ chr_page, vrom_bank[0], CHRROM);
3809      chr1_x(machine, 1 ^ chr_page, vrom_bank[8], CHRROM);
3810      chr1_x(machine, 2 ^ chr_page, vrom_bank[1], CHRROM);
3811      chr1_x(machine, 3 ^ chr_page, vrom_bank[9], CHRROM);
3866      chr1_x(machine, 0 ^ chr_page, state->vrom_bank[0], CHRROM);
3867      chr1_x(machine, 1 ^ chr_page, state->vrom_bank[8], CHRROM);
3868      chr1_x(machine, 2 ^ chr_page, state->vrom_bank[1], CHRROM);
3869      chr1_x(machine, 3 ^ chr_page, state->vrom_bank[9], CHRROM);
38123870   }
38133871   else
38143872   {
3815      chr1_x(machine, 0 ^ chr_page, vrom_bank[0] & ~0x01, CHRROM);
3816      chr1_x(machine, 1 ^ chr_page, vrom_bank[0] |  0x01, CHRROM);
3817      chr1_x(machine, 2 ^ chr_page, vrom_bank[1] & ~0x01, CHRROM);
3818      chr1_x(machine, 3 ^ chr_page, vrom_bank[1] |  0x01, CHRROM);
3873      chr1_x(machine, 0 ^ chr_page, state->vrom_bank[0] & ~0x01, CHRROM);
3874      chr1_x(machine, 1 ^ chr_page, state->vrom_bank[0] |  0x01, CHRROM);
3875      chr1_x(machine, 2 ^ chr_page, state->vrom_bank[1] & ~0x01, CHRROM);
3876      chr1_x(machine, 3 ^ chr_page, state->vrom_bank[1] |  0x01, CHRROM);
38193877   }
38203878
3821   chr1_x(machine, 4 ^ chr_page, vrom_bank[2], CHRROM);
3822   chr1_x(machine, 5 ^ chr_page, vrom_bank[3], CHRROM);
3823   chr1_x(machine, 6 ^ chr_page, vrom_bank[4], CHRROM);
3824   chr1_x(machine, 7 ^ chr_page, vrom_bank[5], CHRROM);
3879   chr1_x(machine, 4 ^ chr_page, state->vrom_bank[2], CHRROM);
3880   chr1_x(machine, 5 ^ chr_page, state->vrom_bank[3], CHRROM);
3881   chr1_x(machine, 6 ^ chr_page, state->vrom_bank[4], CHRROM);
3882   chr1_x(machine, 7 ^ chr_page, state->vrom_bank[5], CHRROM);
38253883}
38263884
38273885static WRITE8_HANDLER( mapper64_w )
38283886{
3887   nes_state *state = (nes_state *)space->machine->driver_data;
38293888   UINT8 map64_helper, cmd;
38303889   LOG_MMC(("mapper64_w offset: %04x, data: %02x\n", offset, data));
38313890
38323891   switch (offset & 0x6001)
38333892   {
38343893      case 0x0000:
3835         map64_helper = mmc_cmd1 ^ data;
3836         mmc_cmd1 = data;
3894         map64_helper = state->mmc_cmd1 ^ data;
3895         state->mmc_cmd1 = data;
38373896
38383897         /* Has PRG Mode changed? */
38393898         if (map64_helper & 0x40)
r7955r7956
38453904         break;
38463905
38473906      case 0x0001:
3848         cmd = mmc_cmd1 & 0x0f;
3907         cmd = state->mmc_cmd1 & 0x0f;
38493908         switch (cmd)
38503909         {
38513910            case 0: case 1:
38523911            case 2: case 3:
38533912            case 4: case 5:
3854               vrom_bank[cmd] = data;
3913               state->vrom_bank[cmd] = data;
38553914               mapper64_set_chr(space->machine);
38563915               break;
38573916            case 6: case 7:
3858               prg_bank[cmd - 6] = data;
3917               state->prg_bank[cmd - 6] = data;
38593918               mapper64_set_prg(space->machine);
38603919               break;
38613920            case 8: case 9:
3862               vrom_bank[cmd - 2] = data;
3921               state->vrom_bank[cmd - 2] = data;
38633922               mapper64_set_chr(space->machine);
38643923               break;
38653924            case 0x0f:
3866               prg_bank[2] = data;
3925               state->prg_bank[2] = data;
38673926               mapper64_set_prg(space->machine);
38683927               break;
38693928         }
r7955r7956
38743933         break;
38753934
38763935      case 0x4000:
3877         IRQ_count_latch = data;
3936         state->IRQ_count_latch = data;
38783937         break;
38793938
38803939      case 0x4001: /* $c001 - IRQ scanline latch */
3881         IRQ_mode = data & 0x01;
3882         IRQ_reset = 1;
3940         state->IRQ_mode = data & 0x01;
3941         state->IRQ_reset = 1;
38833942         break;
38843943
38853944      case 0x6000:
3886         IRQ_enable = 0;
3945         state->IRQ_enable = 0;
38873946         break;
38883947
38893948      case 0x6001:
3890         IRQ_enable = 1;
3949         state->IRQ_enable = 1;
38913950         break;
38923951
38933952      default:
r7955r7956
39133972there are 3 dots to every 1 CPU cycle, hence 114 is the number of cycles per scanline ) */
39143973static void irem_irq( running_device *device, int scanline, int vblank, int blanked )
39153974{
3916   if (IRQ_enable)
3975   nes_state *state = (nes_state *)device->machine->driver_data;
3976   if (state->IRQ_enable)
39173977   {
3918      IRQ_count -= 114;
3978      state->IRQ_count -= 114;
39193979
3920      if (IRQ_count <= 114)
3980      if (state->IRQ_count <= 114)
39213981      {
3922         IRQ_enable = 0;
3982         state->IRQ_enable = 0;
39233983         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
39243984      }
39253985   }
r7955r7956
39273987
39283988static WRITE8_HANDLER( mapper65_w )
39293989{
3990   nes_state *state = (nes_state *)space->machine->driver_data;
39303991   LOG_MMC(("mapper65_w, offset %04x, data: %02x\n", offset, data));
39313992
39323993   switch (offset & 0x7fff)
r7955r7956
39404001         break;
39414002
39424003      case 0x1003:
3943         IRQ_enable = data & 0x80;
4004         state->IRQ_enable = data & 0x80;
39444005         break;
39454006
39464007      case 0x1004:
3947         IRQ_count = IRQ_count_latch;
4008         state->IRQ_count = state->IRQ_count_latch;
39484009         break;
39494010
39504011      case 0x1005:
3951         IRQ_count_latch = (IRQ_count_latch & 0x00ff) | (data << 8);
4012         state->IRQ_count_latch = (state->IRQ_count_latch & 0x00ff) | (data << 8);
39524013         break;
39534014
39544015      case 0x1006:
3955         IRQ_count_latch = (IRQ_count_latch & 0xff00) | data;
4016         state->IRQ_count_latch = (state->IRQ_count_latch & 0xff00) | data;
39564017         break;
39574018
39584019      case 0x2000:
r7955r7956
40154076there are 3 dots to every 1 CPU cycle, hence 114 is the number of cycles per scanline ) */
40164077static void mapper67_irq( running_device *device, int scanline, int vblank, int blanked )
40174078{
4079   nes_state *state = (nes_state *)device->machine->driver_data;
4080
40184081   /* TODO: change to reflect the actual number of cycles spent: both using 114 or cycling 114,114,113
40194082    produces a 1-line glitch in Fantasy Zone 2: it really requires the counter to be updated each CPU cycle! */
4020   if (IRQ_enable)
4083   if (state->IRQ_enable)
40214084   {
4022      if (IRQ_count <= 114)
4085      if (state->IRQ_count <= 114)
40234086      {
4024         IRQ_enable = 0;
4025         IRQ_count = 0xffff;
4087         state->IRQ_enable = 0;
4088         state->IRQ_count = 0xffff;
40264089         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
40274090      }
40284091      else
4029         IRQ_count -= 114;
4092         state->IRQ_count -= 114;
40304093   }
40314094}
40324095
40334096static WRITE8_HANDLER( mapper67_w )
40344097{
4098   nes_state *state = (nes_state *)space->machine->driver_data;
40354099   LOG_MMC(("mapper67_w, offset %04x, data: %02x\n", offset, data));
4100
40364101   switch (offset & 0x7800)
40374102   {
40384103   case 0x0800:
r7955r7956
40494114      break;
40504115   case 0x4000:
40514116   case 0x4800:
4052      IRQ_toggle ^= 1;
4053      if (IRQ_toggle)
4054         IRQ_count = (IRQ_count & 0x00ff) | (data << 8);
4117      state->IRQ_toggle ^= 1;
4118      if (state->IRQ_toggle)
4119         state->IRQ_count = (state->IRQ_count & 0x00ff) | (data << 8);
40554120      else
4056         IRQ_count = (IRQ_count & 0xff00) | data;
4121         state->IRQ_count = (state->IRQ_count & 0xff00) | data;
40574122      break;
40584123   case 0x5800:
4059      IRQ_enable = data & 0x10;
4060      IRQ_toggle = 0;
4124      state->IRQ_enable = data & 0x10;
4125      state->IRQ_toggle = 0;
40614126      break;
40624127   case 0x6800:
40634128      switch (data & 3)
r7955r7956
41874252there are 3 dots to every 1 CPU cycle, hence 114 is the number of cycles per scanline ) */
41884253static void mapper69_irq( running_device *device, int scanline, int vblank, int blanked )
41894254{
4255   nes_state *state = (nes_state *)device->machine->driver_data;
4256
41904257   /* TODO: change to reflect the actual number of cycles spent */
4191   if ((IRQ_enable & 0x80) && (IRQ_enable & 0x01))
4258   if ((state->IRQ_enable & 0x80) && (state->IRQ_enable & 0x01))
41924259   {
4193      if (IRQ_count <= 114)
4260      if (state->IRQ_count <= 114)
41944261      {
4195         IRQ_count = 0xffff;
4262         state->IRQ_count = 0xffff;
41964263         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
41974264      }
41984265      else
4199         IRQ_count -= 114;
4266         state->IRQ_count -= 114;
42004267   }
4201   else if (IRQ_enable & 0x01)   // if enable bit is not set, only decrement the counter!
4268   else if (state->IRQ_enable & 0x01)   // if enable bit is not set, only decrement the counter!
42024269   {
4203      if (IRQ_count <= 114)
4204         IRQ_count = 0xffff;
4270      if (state->IRQ_count <= 114)
4271         state->IRQ_count = 0xffff;
42054272      else
4206         IRQ_count -= 114;
4273         state->IRQ_count -= 114;
42074274   }
42084275}
42094276
r7955r7956
42154282   switch (offset & 0x6000)
42164283   {
42174284      case 0x0000:
4218         mmc_cmd1 = data & 0x0f;
4285         state->mmc_cmd1 = data & 0x0f;
42194286         break;
42204287
42214288      case 0x2000:
4222         switch (mmc_cmd1)
4289         switch (state->mmc_cmd1)
42234290         {
42244291            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
4225               chr1_x(space->machine, mmc_cmd1, data, CHRROM);
4292               chr1_x(space->machine, state->mmc_cmd1, data, CHRROM);
42264293               break;
42274294
42284295            /* TODO: deal properly with bankswitching/write-protecting the mid-mapper area */
r7955r7956
42524319               }
42534320               break;
42544321            case 0x0d:
4255               IRQ_enable = data;
4322               state->IRQ_enable = data;
42564323               break;
42574324            case 0x0e:
4258               IRQ_count = (IRQ_count & 0xff00) | data;
4325               state->IRQ_count = (state->IRQ_count & 0xff00) | data;
42594326               break;
42604327            case 0x0f:
4261               IRQ_count = (IRQ_count & 0x00ff) | (data << 8);
4328               state->IRQ_count = (state->IRQ_count & 0x00ff) | (data << 8);
42624329               break;
42634330         }
42644331         break;
r7955r7956
43704437
43714438static WRITE8_HANDLER( mapper73_w )
43724439{
4440   nes_state *state = (nes_state *)space->machine->driver_data;
4441
43734442   switch (offset & 0x7000)
43744443   {
43754444      case 0x0000:
43764445      case 0x1000:
43774446         /* dunno which address controls these */
4378         IRQ_count_latch = data;
4379         IRQ_enable_latch = data;
4447         state->IRQ_count_latch = data;
4448         state->IRQ_enable_latch = data;
43804449         break;
43814450      case 0x2000:
4382         IRQ_enable = data;
4451         state->IRQ_enable = data;
43834452         break;
43844453      case 0x3000:
4385         IRQ_count &= ~0x0f;
4386         IRQ_count |= data & 0x0f;
4454         state->IRQ_count &= ~0x0f;
4455         state->IRQ_count |= data & 0x0f;
43874456         break;
43884457      case 0x4000:
4389         IRQ_count &= ~0xf0;
4390         IRQ_count |= (data & 0x0f) << 4;
4458         state->IRQ_count &= ~0xf0;
4459         state->IRQ_count |= (data & 0x0f) << 4;
43914460         break;
43924461      case 0x7000:
43934462         prg16_89ab(space->machine, data);
r7955r7956
44384507
44394508static void mapper74_set_chr( running_machine *machine, int chr_base, int chr_mask )
44404509{
4441   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
4510   nes_state *state = (nes_state *)machine->driver_data;
4511   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
44424512   UINT8 chr_src[6];
44434513   int i;
44444514
44454515   for (i = 0; i < 6; i++)
4446      chr_src[i] = ((vrom_bank[i] == 8) || (vrom_bank[i] == 9)) ? CHRRAM : CHRROM;
4516      chr_src[i] = ((state->vrom_bank[i] == 8) || (state->vrom_bank[i] == 9)) ? CHRRAM : CHRROM;
44474517
4448   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
4449   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
4450   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
4451   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
4452   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr_src[2]);
4453   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr_src[3]);
4454   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr_src[4]);
4455   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr_src[5]);
4518   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
4519   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
4520   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
4521   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
4522   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr_src[2]);
4523   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr_src[3]);
4524   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr_src[4]);
4525   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr_src[5]);
44564526}
44574527
44584528static WRITE8_HANDLER( mapper74_w )
44594529{
4530   nes_state *state = (nes_state *)space->machine->driver_data;
44604531   UINT8 MMC3_helper, cmd;
44614532   LOG_MMC(("mapper74_w, offset: %04x, data: %02x\n", offset, data));
44624533
44634534   switch (offset & 0x6001)
44644535   {
44654536   case 0x0000:
4466      MMC3_helper = mmc_cmd1 ^ data;
4467      mmc_cmd1 = data;
4537      MMC3_helper = state->mmc_cmd1 ^ data;
4538      state->mmc_cmd1 = data;
44684539
44694540      /* Has PRG Mode changed? */
44704541      if (MMC3_helper & 0x40)
4471         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
4542         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
44724543
44734544      /* Has CHR Mode changed? */
44744545      if (MMC3_helper & 0x80)
4475         mapper74_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
4546         mapper74_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
44764547      break;
44774548
44784549   case 0x0001:
4479      cmd = mmc_cmd1 & 0x07;
4550      cmd = state->mmc_cmd1 & 0x07;
44804551      switch (cmd)
44814552      {
44824553      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
44834554      case 2: case 3: case 4: case 5:
4484         vrom_bank[cmd] = data;
4485         mapper74_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
4555         state->vrom_bank[cmd] = data;
4556         mapper74_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
44864557         break;
44874558      case 6:
44884559      case 7:
4489         prg_bank[cmd - 6] = data;
4490         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
4560         state->prg_bank[cmd - 6] = data;
4561         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
44914562         break;
44924563      }
44934564      break;
r7955r7956
45184589
45194590static WRITE8_HANDLER( mapper75_w )
45204591{
4592   nes_state *state = (nes_state *)space->machine->driver_data;
45214593   LOG_MMC(("mapper75_w, offset: %04x, data: %02x\n", offset, data));
45224594
45234595   switch (offset & 0x7000)
r7955r7956
45274599         break;
45284600      case 0x1000:
45294601         set_nt_mirroring(space->machine, (data & 0x01) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
4530         vrom_bank[0] = (vrom_bank[0] & 0x0f) | ((data & 0x02) << 3);
4531         vrom_bank[1] = (vrom_bank[1] & 0x0f) | ((data & 0x04) << 2);
4532         chr4_0(space->machine, vrom_bank[0], CHRROM);
4533         chr4_4(space->machine, vrom_bank[1], CHRROM);
4602         state->vrom_bank[0] = (state->vrom_bank[0] & 0x0f) | ((data & 0x02) << 3);
4603         state->vrom_bank[1] = (state->vrom_bank[1] & 0x0f) | ((data & 0x04) << 2);
4604         chr4_0(space->machine, state->vrom_bank[0], CHRROM);
4605         chr4_4(space->machine, state->vrom_bank[1], CHRROM);
45344606         break;
45354607      case 0x2000:
45364608         prg8_ab(space->machine, data);
r7955r7956
45394611         prg8_cd(space->machine, data);
45404612         break;
45414613      case 0x6000:
4542         vrom_bank[0] = (vrom_bank[0] & 0x10) | (data & 0x0f);
4543         chr4_0(space->machine, vrom_bank[0], CHRROM);
4614         state->vrom_bank[0] = (state->vrom_bank[0] & 0x10) | (data & 0x0f);
4615         chr4_0(space->machine, state->vrom_bank[0], CHRROM);
45444616         break;
45454617      case 0x7000:
4546         vrom_bank[1] = (vrom_bank[1] & 0x10) | (data & 0x0f);
4547         chr4_4(space->machine, vrom_bank[1], CHRROM);
4618         state->vrom_bank[1] = (state->vrom_bank[1] & 0x10) | (data & 0x0f);
4619         chr4_4(space->machine, state->vrom_bank[1], CHRROM);
45484620         break;
45494621   }
45504622}
r7955r7956
45624634
45634635static WRITE8_HANDLER( mapper76_w )
45644636{
4637   nes_state *state = (nes_state *)space->machine->driver_data;
45654638   LOG_MMC(("mapper76_w, offset: %04x, data: %02x\n", offset, data));
45664639
45674640   switch (offset & 0x7001)
45684641   {
45694642   case 0:
4570      mmc_cmd1 = data;
4643      state->mmc_cmd1 = data;
45714644      break;
45724645   case 1:
45734646      {
4574         switch (mmc_cmd1 & 0x07)
4647         switch (state->mmc_cmd1 & 0x07)
45754648         {
45764649         case 2: chr2_0(space->machine, data, CHRROM); break;
45774650         case 3: chr2_2(space->machine, data, CHRROM); break;
45784651         case 4: chr2_4(space->machine, data, CHRROM); break;
45794652         case 5: chr2_6(space->machine, data, CHRROM); break;
45804653         case 6:
4581            (mmc_cmd1 & 0x40) ? prg8_cd(space->machine, data) : prg8_89(space->machine, data);
4654            (state->mmc_cmd1 & 0x40) ? prg8_cd(space->machine, data) : prg8_89(space->machine, data);
45824655            break;
45834656         case 7: prg8_ab(space->machine, data); break;
4584         default: logerror("mapper76 unsupported command: %02x, data: %02x\n", mmc_cmd1, data); break;
4657         default: logerror("mapper76 unsupported command: %02x, data: %02x\n", state->mmc_cmd1, data); break;
45854658         }
45864659      }
45874660   case 0x2000:
r7955r7956
47504823
47514824static WRITE8_HANDLER( mapper82_m_w )
47524825{
4826   nes_state *state = (nes_state *)space->machine->driver_data;
47534827   LOG_MMC(("mapper82_m_w, offset: %04x, data: %02x\n", offset, data));
47544828
47554829   switch (offset)
47564830   {
47574831      case 0x1ef0:
4758         if (mmc_cmd1)
4832         if (state->mmc_cmd1)
47594833            chr2_4(space->machine, data >> 1, CHRROM);
47604834         else
47614835            chr2_0(space->machine, data >> 1, CHRROM);
47624836         break;
47634837      case 0x1ef1:
4764         if (mmc_cmd1)
4838         if (state->mmc_cmd1)
47654839            chr2_6(space->machine, data, CHRROM);
47664840         else
47674841            chr2_2(space->machine, data, CHRROM);
47684842         break;
47694843      case 0x1ef2:
4770         chr1_x(space->machine, 4 ^ mmc_cmd1, data, CHRROM);
4844         chr1_x(space->machine, 4 ^ state->mmc_cmd1, data, CHRROM);
47714845         break;
47724846      case 0x1ef3:
4773         chr1_x(space->machine, 5 ^ mmc_cmd1, data, CHRROM);
4847         chr1_x(space->machine, 5 ^ state->mmc_cmd1, data, CHRROM);
47744848         break;
47754849      case 0x1ef4:
4776         chr1_x(space->machine, 6 ^ mmc_cmd1, data, CHRROM);
4850         chr1_x(space->machine, 6 ^ state->mmc_cmd1, data, CHRROM);
47774851         break;
47784852      case 0x1ef5:
4779         chr1_x(space->machine, 7 ^ mmc_cmd1, data, CHRROM);
4853         chr1_x(space->machine, 7 ^ state->mmc_cmd1, data, CHRROM);
47804854         break;
47814855      case 0x1ef6:
47824856         set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
4783         mmc_cmd1 = ((data & 0x02) << 1);
4857         state->mmc_cmd1 = ((data & 0x02) << 1);
47844858         break;
47854859
47864860      case 0x1efa:
r7955r7956
48404914
48414915static WRITE8_HANDLER( mapper83_w )
48424916{
4917   nes_state *state = (nes_state *)space->machine->driver_data;
48434918   LOG_MMC(("mapper83_w, offset: %04x, data: %02x\n", offset, data));
48444919
48454920   switch (offset)
r7955r7956
48704945      }
48714946      break;
48724947   case 0x0200:
4873      IRQ_count = (IRQ_count & 0xff00) | data;
4948      state->IRQ_count = (state->IRQ_count & 0xff00) | data;
48744949      break;
48754950   case 0x0201:
4876      IRQ_enable = 1;
4877      IRQ_count = (data << 8) | (IRQ_count & 0xff);
4951      state->IRQ_enable = 1;
4952      state->IRQ_count = (data << 8) | (state->IRQ_count & 0xff);
48784953      break;
48794954   case 0x0300:
48804955      prg8_89(space->machine, data);
r7955r7956
49275002
49285003static WRITE8_HANDLER( konami_vrc7_w )
49295004{
5005   nes_state *state = (nes_state *)space->machine->driver_data;
49305006   UINT8 bank;
49315007   LOG_MMC(("konami_vrc7_w, offset: %04x, data: %02x\n", offset, data));
49325008
r7955r7956
49645040      case 0x5010:
49655041      case 0x5018:
49665042         bank = ((offset & 0x7000) - 0x2000) / 0x0800 + ((offset & 0x0018) ? 1 : 0);
4967         chr1_x(space->machine, bank, data, mmc_chr_source);
5043         chr1_x(space->machine, bank, data, state->mmc_chr_source);
49685044         break;
49695045
49705046      case 0x6000:
r7955r7956
49775053         }
49785054         break;
49795055      case 0x6008: case 0x6010: case 0x6018:
4980         IRQ_count_latch = data;
5056         state->IRQ_count_latch = data;
49815057         break;
49825058      case 0x7000:
4983         IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
4984         IRQ_enable = data & 0x02;
4985         IRQ_enable_latch = data & 0x01;
5059         state->IRQ_mode = data & 0x04;   // currently not implemented: 0 = prescaler mode / 1 = CPU mode
5060         state->IRQ_enable = data & 0x02;
5061         state->IRQ_enable_latch = data & 0x01;
49865062         if (data & 0x02)
4987            IRQ_count = IRQ_count_latch;
5063            state->IRQ_count = state->IRQ_count_latch;
49885064         break;
49895065      case 0x7008: case 0x7010: case 0x7018:
4990         IRQ_enable = IRQ_enable_latch;
5066         state->IRQ_enable = state->IRQ_enable_latch;
49915067         break;
49925068
49935069      default:
r7955r7956
50515127
50525128static WRITE8_HANDLER( mapper88_w )
50535129{
5130   nes_state *state = (nes_state *)space->machine->driver_data;
50545131   LOG_MMC(("mapper88_w, offset: %04x, data: %02x\n", offset, data));
50555132
50565133   switch (offset & 1)
50575134   {
50585135   case 0:
5059      mmc_cmd1 = data & 7;
5136      state->mmc_cmd1 = data & 7;
50605137      break;
50615138   case 1:
5062      switch (mmc_cmd1)
5139      switch (state->mmc_cmd1)
50635140      {
50645141         case 0: chr2_0(space->machine, data >> 1, CHRROM); break;
50655142         case 1: chr2_2(space->machine, data >> 1, CHRROM); break;
r7955r7956
51255202
51265203static WRITE8_HANDLER( mapper91_m_w )
51275204{
5205   nes_state *state = (nes_state *)space->machine->driver_data;
51285206   LOG_MMC(("mapper91_m_w, offset: %04x, data: %02x\n", offset, data));
51295207
51305208   switch (offset & 0x1000)
r7955r7956
51435221         {
51445222            case 0x00: prg8_89(space->machine, data); break;
51455223            case 0x01: prg8_ab(space->machine, data); break;
5146            case 0x02: IRQ_enable = 0; IRQ_count = 0; break;
5147            case 0x03: IRQ_enable = 1; IRQ_count = 7; break;
5224            case 0x02: state->IRQ_enable = 0; state->IRQ_count = 0; break;
5225            case 0x03: state->IRQ_enable = 1; state->IRQ_count = 7; break;
51485226         }
51495227         break;
51505228      default:
r7955r7956
52325310
52335311static WRITE8_HANDLER( mapper95_w )
52345312{
5313   nes_state *state = (nes_state *)space->machine->driver_data;
52355314   LOG_MMC(("mapper95_w, offset: %04x, data: %02x\n", offset, data));
52365315
52375316   switch (offset & 1)
52385317   {
52395318   case 0:
52405319      {
5241         mmc_cmd1 = data;
5320         state->mmc_cmd1 = data;
52425321         break;
52435322      }
52445323   case 1:
52455324      {
5246         switch (mmc_cmd1 & 0x07)
5325         switch (state->mmc_cmd1 & 0x07)
52475326         {
52485327            case 0:
52495328            case 1:
5250               chr2_x(space->machine, mmc_cmd1 ? 2 : 0, (data >> 1) & 0x1f, CHRROM);
5329               chr2_x(space->machine, state->mmc_cmd1 ? 2 : 0, (data >> 1) & 0x1f, CHRROM);
52515330               break;
52525331            case 2:
52535332            case 3:
52545333            case 4:
52555334            case 5:
5256               chr1_x(space->machine, 2 + mmc_cmd1, data & 0x1f, CHRROM);
5257               mapper95_reg[mmc_cmd1 - 2] = data & 0x20;
5258               if (!(mmc_cmd1 & 0x80))
5335               chr1_x(space->machine, 2 + state->mmc_cmd1, data & 0x1f, CHRROM);
5336               mapper95_reg[state->mmc_cmd1 - 2] = data & 0x20;
5337               if (!(state->mmc_cmd1 & 0x80))
52595338               {
52605339                  set_nt_page(space->machine, 0, CIRAM, mapper95_reg[0] ? 1 : 0, 1);
52615340                  set_nt_page(space->machine, 1, CIRAM, mapper95_reg[1] ? 1 : 0, 1);
r7955r7956
54115490
54125491static WRITE8_HANDLER( mapper104_w )
54135492{
5493   nes_state *state = (nes_state *)space->machine->driver_data;
54145494   LOG_MMC(("mapper104_w, offset: %04x, data: %02x\n", offset, data));
54155495
54165496   if (offset < 0x4000)
54175497   {
54185498      if (data & 0x08)
54195499      {
5420         prg_bank[0] = ((data & 0x07) << 4) | (prg_bank[0] & 0x0f);
5421         prg16_89ab(space->machine, prg_bank[0]);
5500         state->prg_bank[0] = ((data & 0x07) << 4) | (state->prg_bank[0] & 0x0f);
5501         prg16_89ab(space->machine, state->prg_bank[0]);
54225502         prg16_cdef(space->machine, ((data & 0x07) << 4) | 0x0f);
54235503      }
54245504
54255505   }
54265506   else
54275507   {
5428         prg_bank[0] = (prg_bank[0] & 0x70) | (data & 0x0f);
5429         prg16_89ab(space->machine, prg_bank[0]);
5508         state->prg_bank[0] = (state->prg_bank[0] & 0x70) | (data & 0x0f);
5509         prg16_89ab(space->machine, state->prg_bank[0]);
54305510   }
54315511}
54325512
r7955r7956
54545534
54555535static void mapper106_irq( running_device *device, int scanline, int vblank, int blanked )
54565536{
5457   if (IRQ_enable)
5537   nes_state *state = (nes_state *)device->machine->driver_data;
5538
5539   if (state->IRQ_enable)
54585540   {
5459      if ((0xffff - IRQ_count) < 114)
5541      if ((0xffff - state->IRQ_count) < 114)
54605542      {
54615543         cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
5462         IRQ_enable = 0;
5544         state->IRQ_enable = 0;
54635545      }
54645546
5465      IRQ_count = (IRQ_count + 114) & 0xffff;
5547      state->IRQ_count = (state->IRQ_count + 114) & 0xffff;
54665548   }
54675549}
54685550
54695551static WRITE8_HANDLER( mapper106_w )
54705552{
5553   nes_state *state = (nes_state *)space->machine->driver_data;
54715554   LOG_MMC(("mapper106_w, offset: %04x, data: %02x\n", offset, data));
54725555   switch (offset & 0x0f)
54735556   {
r7955r7956
54995582      set_nt_mirroring(space->machine, BIT(data, 0) ?  PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
55005583      break;
55015584   case 0x0d:
5502      IRQ_count = 0;
5503      IRQ_enable = 0;
5585      state->IRQ_count = 0;
5586      state->IRQ_enable = 0;
55045587      break;
55055588   case 0x0e:
5506      IRQ_count = (IRQ_count & 0xff00) | data;
5589      state->IRQ_count = (state->IRQ_count & 0xff00) | data;
55075590      break;
55085591   case 0x0f:
5509      IRQ_count = (IRQ_count & 0x00ff) | (data << 8);
5510      IRQ_enable = 1;
5592      state->IRQ_count = (state->IRQ_count & 0x00ff) | (data << 8);
5593      state->IRQ_enable = 1;
55115594      break;
55125595   }
55135596}
r7955r7956
55975680
55985681static WRITE8_HANDLER( mapper112_w )
55995682{
5683   nes_state *state = (nes_state *)space->machine->driver_data;
56005684   LOG_MMC(("mapper112_w, offset: %04x, data: %02x\n", offset, data));
56015685
56025686   switch (offset)
56035687   {
56045688   case 0x0000:
5605      mmc_cmd1 = data & 0x07;
5689      state->mmc_cmd1 = data & 0x07;
56065690      break;
56075691   case 0x2000:
5608      switch (mmc_cmd1)
5692      switch (state->mmc_cmd1)
56095693      {
56105694      case 0:
56115695         prg8_89(space->machine, data);
r7955r7956
56845768
56855769static WRITE8_HANDLER( mapper114_m_w )
56865770{
5771   nes_state *state = (nes_state *)space->machine->driver_data;
56875772   LOG_MMC(("mapper114_m_w, offset: %04x, data: %02x\n", offset, data));
56885773
56895774   map114_reg = data;
r7955r7956
56945779      prg16_cdef(space->machine, data & 0x1f);
56955780   }
56965781   else
5697      mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
5782      mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
56985783
56995784}
57005785
r7955r7956
57645849
57655850static WRITE8_HANDLER( mapper115_m_w )
57665851{
5852   nes_state *state = (nes_state *)space->machine->driver_data;
57675853   LOG_MMC(("mapper115_m_w, offset: %04x, data: %02x\n", offset, data));
57685854
57695855   switch (offset & 0x01)
57705856   {
57715857   case 0x00:
57725858      mapper115_reg[0] = data;
5773      mapper115_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
5859      mapper115_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
57745860      break;
57755861   case 0x01:
5776      mmc_chr_base = (data & 0x01) ? 0x100 : 0x000;
5777      mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
5862      state->mmc_chr_base = (data & 0x01) ? 0x100 : 0x000;
5863      mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
57785864      break;
57795865   }
57805866}
57815867
57825868static WRITE8_HANDLER( mapper115_w )
57835869{
5870   nes_state *state = (nes_state *)space->machine->driver_data;
57845871   UINT8 map115_helper, cmd;
57855872   LOG_MMC(("mapper115_w, offset: %04x, data: %02x\n", offset, data));
57865873
57875874   switch (offset & 0x6001)
57885875   {
57895876      case 0x0000:
5790         map115_helper = mmc_cmd1 ^ data;
5791         mmc_cmd1 = data;
5877         map115_helper = state->mmc_cmd1 ^ data;
5878         state->mmc_cmd1 = data;
57925879
57935880         /* Has PRG Mode changed? */
57945881         if (map115_helper & 0x40)
5795            mapper115_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
5882            mapper115_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
57965883
57975884         /* Has CHR Mode changed? */
57985885         if (map115_helper & 0x80)
5799            mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
5886            mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
58005887         break;
58015888
58025889      case 0x0001:
5803         cmd = mmc_cmd1 & 0x07;
5890         cmd = state->mmc_cmd1 & 0x07;
58045891         switch (cmd)
58055892         {
58065893            case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
58075894            case 2: case 3: case 4: case 5:
5808               vrom_bank[cmd] = data;
5809               mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
5895               state->vrom_bank[cmd] = data;
5896               mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
58105897               break;
58115898            case 6:
58125899            case 7:
5813               prg_bank[cmd - 6] = data;
5814               mapper115_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
5900               state->prg_bank[cmd - 6] = data;
5901               mapper115_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
58155902               break;
58165903         }
58175904         break;
r7955r7956
58465933
58475934static void mapper117_irq( running_device *device, int scanline, int vblank, int blanked )
58485935{
5936   nes_state *state = (nes_state *)device->machine->driver_data;
58495937//  if (scanline < PPU_BOTTOM_VISIBLE_SCANLINE)
58505938   {
5851      if (IRQ_enable && IRQ_count)
5939      if (state->IRQ_enable && state->IRQ_count)
58525940      {
5853         IRQ_count--;
5854         if (!IRQ_count)
5941         state->IRQ_count--;
5942         if (!state->IRQ_count)
58555943            cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
58565944      }
58575945   }
r7955r7956
58595947
58605948static WRITE8_HANDLER( mapper117_w )
58615949{
5950   nes_state *state = (nes_state *)space->machine->driver_data;
58625951   LOG_MMC(("mapper117_w, offset: %04x, data: %02x\n", offset, data));
58635952
58645953   switch (offset)
r7955r7956
58915980      break;
58925981
58935982   case 0x4001:
5894      IRQ_count_latch = data;
5983      state->IRQ_count_latch = data;
58955984      break;
58965985   case 0x4002:
58975986      // IRQ cleared
58985987      break;
58995988   case 0x4003:
5900      IRQ_count = IRQ_count_latch;
5989      state->IRQ_count = state->IRQ_count_latch;
59015990      break;
59025991   case 0x6000:
5903      IRQ_enable = data & 0x01;
5992      state->IRQ_enable = data & 0x01;
59045993      break;
59055994   }
59065995}
r7955r7956
59196008
59206009static void mapper118_set_mirror( running_machine *machine )
59216010{
5922   if (mmc_cmd1 & 0x80)
6011   nes_state *state = (nes_state *)machine->driver_data;
6012   if (state->mmc_cmd1 & 0x80)
59236013   {
5924      set_nt_page(machine, 0, CIRAM, (vrom_bank[2] & 0x80) >> 7, 1);
5925      set_nt_page(machine, 1, CIRAM, (vrom_bank[3] & 0x80) >> 7, 1);
5926      set_nt_page(machine, 2, CIRAM, (vrom_bank[4] & 0x80) >> 7, 1);
5927      set_nt_page(machine, 3, CIRAM, (vrom_bank[5] & 0x80) >> 7, 1);
6014      set_nt_page(machine, 0, CIRAM, (state->vrom_bank[2] & 0x80) >> 7, 1);
6015      set_nt_page(machine, 1, CIRAM, (state->vrom_bank[3] & 0x80) >> 7, 1);
6016      set_nt_page(machine, 2, CIRAM, (state->vrom_bank[4] & 0x80) >> 7, 1);
6017      set_nt_page(machine, 3, CIRAM, (state->vrom_bank[5] & 0x80) >> 7, 1);
59286018   }
59296019   else
59306020   {
5931      set_nt_page(machine, 0, CIRAM, (vrom_bank[0] & 0x80) >> 7, 1);
5932      set_nt_page(machine, 1, CIRAM, (vrom_bank[0] & 0x80) >> 7, 1);
5933      set_nt_page(machine, 2, CIRAM, (vrom_bank[1] & 0x80) >> 7, 1);
5934      set_nt_page(machine, 3, CIRAM, (vrom_bank[1] & 0x80) >> 7, 1);
6021      set_nt_page(machine, 0, CIRAM, (state->vrom_bank[0] & 0x80) >> 7, 1);
6022      set_nt_page(machine, 1, CIRAM, (state->vrom_bank[0] & 0x80) >> 7, 1);
6023      set_nt_page(machine, 2, CIRAM, (state->vrom_bank[1] & 0x80) >> 7, 1);
6024      set_nt_page(machine, 3, CIRAM, (state->vrom_bank[1] & 0x80) >> 7, 1);
59356025   }
59366026}
59376027
59386028static WRITE8_HANDLER( mapper118_w )
59396029{
6030   nes_state *state = (nes_state *)space->machine->driver_data;
59406031   UINT8 MMC3_helper, cmd;
59416032   LOG_MMC(("mapper118_w, offset: %04x, data: %02x\n", offset, data));
59426033
59436034   switch (offset & 0x6001)
59446035   {
59456036      case 0x0000:
5946         MMC3_helper = mmc_cmd1 ^ data;
5947         mmc_cmd1 = data;
6037         MMC3_helper = state->mmc_cmd1 ^ data;
6038         state->mmc_cmd1 = data;
59486039
59496040         /* Has PRG Mode changed? */
59506041         if (MMC3_helper & 0x40)
5951            mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
6042            mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
59526043
59536044         /* Has CHR Mode changed? */
59546045         if (MMC3_helper & 0x80)
59556046         {
59566047            mapper118_set_mirror(space->machine);
5957            mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
6048            mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
59586049         }
59596050         break;
59606051
59616052      case 0x0001:
5962         cmd = mmc_cmd1 & 0x07;
6053         cmd = state->mmc_cmd1 & 0x07;
59636054         switch (cmd)
59646055         {
59656056            case 0: case 1:
59666057            case 2: case 3: case 4: case 5:
5967               vrom_bank[cmd] = data;
6058               state->vrom_bank[cmd] = data;
59686059               mapper118_set_mirror(space->machine);
5969               mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
6060               mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
59706061               break;
59716062            case 6:
59726063            case 7:
5973               prg_bank[cmd - 6] = data;
5974               mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
6064               state->prg_bank[cmd - 6] = data;
6065               mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
59756066               break;
59766067         }
59776068         break;
r7955r7956
59986089
59996090static void mapper119_set_chr( running_machine *machine )
60006091{
6001   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
6092   nes_state *state = (nes_state *)machine->driver_data;
6093   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
60026094   UINT8 chr_src[6], chr_mask[6];
60036095   int i;
60046096
60056097   for (i = 0; i < 6; i++)
60066098   {
6007      chr_src[i] = (vrom_bank[i] & 0x40) ? CHRRAM : CHRROM;
6008      chr_mask[i] =  (vrom_bank[i] & 0x40) ? 0x07 : 0x3f;
6099      chr_src[i] = (state->vrom_bank[i] & 0x40) ? CHRRAM : CHRROM;
6100      chr_mask[i] =  (state->vrom_bank[i] & 0x40) ? 0x07 : 0x3f;
60096101   }
60106102
6011   chr1_x(machine, chr_page ^ 0, ((vrom_bank[0] & ~0x01) & chr_mask[0]), chr_src[0]);
6012   chr1_x(machine, chr_page ^ 1, ((vrom_bank[0] |  0x01) & chr_mask[0]), chr_src[0]);
6013   chr1_x(machine, chr_page ^ 2, ((vrom_bank[1] & ~0x01) & chr_mask[1]), chr_src[1]);
6014   chr1_x(machine, chr_page ^ 3, ((vrom_bank[1] |  0x01) & chr_mask[1]), chr_src[1]);
6015   chr1_x(machine, chr_page ^ 4, (vrom_bank[2] & chr_mask[2]), chr_src[2]);
6016   chr1_x(machine, chr_page ^ 5, (vrom_bank[3] & chr_mask[3]), chr_src[3]);
6017   chr1_x(machine, chr_page ^ 6, (vrom_bank[4] & chr_mask[4]), chr_src[4]);
6018   chr1_x(machine, chr_page ^ 7, (vrom_bank[5] & chr_mask[5]), chr_src[5]);
6103   chr1_x(machine, chr_page ^ 0, ((state->vrom_bank[0] & ~0x01) & chr_mask[0]), chr_src[0]);
6104   chr1_x(machine, chr_page ^ 1, ((state->vrom_bank[0] |  0x01) & chr_mask[0]), chr_src[0]);
6105   chr1_x(machine, chr_page ^ 2, ((state->vrom_bank[1] & ~0x01) & chr_mask[1]), chr_src[1]);
6106   chr1_x(machine, chr_page ^ 3, ((state->vrom_bank[1] |  0x01) & chr_mask[1]), chr_src[1]);
6107   chr1_x(machine, chr_page ^ 4, (state->vrom_bank[2] & chr_mask[2]), chr_src[2]);
6108   chr1_x(machine, chr_page ^ 5, (state->vrom_bank[3] & chr_mask[3]), chr_src[3]);
6109   chr1_x(machine, chr_page ^ 6, (state->vrom_bank[4] & chr_mask[4]), chr_src[4]);
6110   chr1_x(machine, chr_page ^ 7, (state->vrom_bank[5] & chr_mask[5]), chr_src[5]);
60196111}
60206112
60216113static WRITE8_HANDLER( mapper119_w )
60226114{
6115   nes_state *state = (nes_state *)space->machine->driver_data;
60236116   UINT8 MMC3_helper, cmd;
60246117   LOG_MMC(("mapper119_w, offset: %04x, data: %02x\n", offset, data));
60256118
60266119   switch (offset & 0x6001)
60276120   {
60286121      case 0x0000:
6029         MMC3_helper = mmc_cmd1 ^ data;
6030         mmc_cmd1 = data;
6122         MMC3_helper = state->mmc_cmd1 ^ data;
6123         state->mmc_cmd1 = data;
60316124
60326125         /* Has PRG Mode changed? */
60336126         if (MMC3_helper & 0x40)
6034            mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
6127            mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
60356128
60366129         /* Has CHR Mode changed? */
60376130         if (MMC3_helper & 0x80)
60386131            mapper119_set_chr(space->machine);
60396132         break;
60406133      case 0x0001: /* $8001 */
6041         cmd = mmc_cmd1 & 0x07;
6134         cmd = state->mmc_cmd1 & 0x07;
60426135         switch (cmd)
60436136         {
60446137            case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
60456138            case 2: case 3: case 4: case 5:
6046               vrom_bank[cmd] = data;
6139               state->vrom_bank[cmd] = data;
60476140               mapper119_set_chr(space->machine);
60486141               break;
60496142            case 6:
60506143            case 7:
6051               prg_bank[cmd - 6] = data;
6052               mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
6144               state->prg_bank[cmd - 6] = data;
6145               mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
60536146               break;
60546147         }
60556148         break;
r7955r7956
61206213
61216214static void mapper121_set_prg( running_machine *machine, int prg_base, int prg_mask )
61226215{
6123   UINT8 prg_flip = (mmc_cmd1 & 0x40) ? 2 : 0;
6216   nes_state *state = (nes_state *)machine->driver_data;
6217   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
61246218   UINT8 new_bank2, new_bank3;
61256219
6126   new_bank2 = mapper121_reg[0] ? mapper121_reg[0] : prg_bank[2 ^ prg_flip];
6127   new_bank3 = mapper121_reg[1] ? mapper121_reg[1] : prg_bank[3];
6220   new_bank2 = mapper121_reg[0] ? mapper121_reg[0] : state->prg_bank[2 ^ prg_flip];
6221   new_bank3 = mapper121_reg[1] ? mapper121_reg[1] : state->prg_bank[3];
61286222
6129   prg8_89(machine, prg_base | (prg_bank[0 ^ prg_flip] & prg_mask));
6130   prg8_ab(machine, prg_base | (prg_bank[1] & prg_mask));
6223   prg8_89(machine, prg_base | (state->prg_bank[0 ^ prg_flip] & prg_mask));
6224   prg8_ab(machine, prg_base | (state->prg_bank[1] & prg_mask));
61316225   prg8_cd(machine, prg_base | (new_bank2 & prg_mask));
61326226   prg8_ef(machine, prg_base | (new_bank3 & prg_mask));
61336227}
61346228
61356229static WRITE8_HANDLER( mapper121_w )
61366230{
6231   nes_state *state = (nes_state *)space->machine->driver_data;
61376232   UINT8 MMC3_helper, cmd;
61386233   LOG_MMC(("mapper121_w, offset: %04x, data: %02x\n", offset, data));
61396234
r7955r7956
61746269      switch (offset & 0x6001)
61756270      {
61766271      case 0x0000:
6177         MMC3_helper = mmc_cmd1 ^ data;
6178         mmc_cmd1 = data;
6272         MMC3_helper = state->mmc_cmd1 ^ data;
6273         state->mmc_cmd1 = data;
61796274
61806275         /* Has PRG Mode changed? */
61816276         if (MMC3_helper & 0x40)
6182            mapper121_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
6277            mapper121_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
61836278
61846279         /* Has CHR Mode changed? */
61856280         if (MMC3_helper & 0x80)
6186            mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
6281            mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
61876282         break;
61886283
61896284      case 0x0001:
6190         cmd = mmc_cmd1 & 0x07;
6285         cmd = state->mmc_cmd1 & 0x07;
61916286         switch (cmd)
61926287         {
61936288         case 0: case 1:
61946289         case 2: case 3: case 4: case 5:
6195            vrom_bank[cmd] = data;
6196            mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
6290            state->vrom_bank[cmd] = data;
6291            mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
61976292            break;
61986293         case 6:
61996294         case 7:
6200            prg_bank[cmd - 6] = data;
6201            mapper121_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
6295            state->prg_bank[cmd - 6] = data;
6296            mapper121_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
62026297            break;
62036298         }
62046299         break;
r7955r7956
62086303         break;
62096304      }
62106305   }
6211   mapper121_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
6306   mapper121_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
62126307}
62136308
62146309/*************************************************************
r7955r7956
64036498
64046499static WRITE8_HANDLER( mapper134_m_w )
64056500{
6501   nes_state *state = (nes_state *)space->machine->driver_data;
64066502   LOG_MMC(("mapper134_m_w, offset: %04x, data: %02x\n", offset, data));
64076503
64086504   if (offset == 0x01)
64096505   {
6410      mmc_prg_base = (data & 0x02) << 4;
6411      mmc_prg_mask = 0x1f;
6412      mmc_chr_base = (data & 0x20) << 3;
6413      mmc_chr_mask = 0xff;
6414      mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
6415      mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
6506      state->mmc_prg_base = (data & 0x02) << 4;
6507      state->mmc_prg_mask = 0x1f;
6508      state->mmc_chr_base = (data & 0x20) << 3;
6509      state->mmc_chr_mask = 0xff;
6510      mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
6511      mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
64166512   }
64176513}
64186514
r7955r7956
64406536
64416537static WRITE8_HANDLER( mapper136_l_w )
64426538{
6539   nes_state *state = (nes_state *)space->machine->driver_data;
64436540   LOG_MMC(("mapper136_l_w, offset: %04x, data: %02x\n", offset, data));
64446541
64456542   if ((offset & 0x103) == 0x002)
64466543   {
6447      mmc_cmd1 = (data & 0x30) | ((data + 3) & 0x0f);
6448      chr8(space->machine, mmc_cmd1, CHRROM);
6544      state->mmc_cmd1 = (data & 0x30) | ((data + 3) & 0x0f);
6545      chr8(space->machine, state->mmc_cmd1, CHRROM);
64496546   }
64506547}
64516548
64526549static READ8_HANDLER( mapper136_l_r )
64536550{
6551   nes_state *state = (nes_state *)space->machine->driver_data;
64546552   LOG_MMC(("mapper136_l_r, offset: %04x\n", offset));
64556553
64566554   if ((offset & 0x103) == 0x000)
6457      return mmc_cmd1 | 0x40;
6555      return state->mmc_cmd1 | 0x40;
64586556   else
64596557      return 0x00;
64606558}
r7955r7956
64956593
64966594static WRITE8_HANDLER( mapper137_l_w )
64976595{
6596   nes_state *state = (nes_state *)space->machine->driver_data;
64986597   LOG_MMC(("mapper137_l_w, offset: %04x, data: %02x\n", offset, data));
64996598
65006599   /* write happens only if we are at 0x4100 + k * 0x200, but 0x4100 is offset = 0 */
65016600   if (!(offset & 0x100))
65026601   {
65036602      if (!(offset & 0x01))
6504         mmc_cmd1 = data & 0x07;
6603         state->mmc_cmd1 = data & 0x07;
65056604      else
65066605      {
6507         sachen_reg[mmc_cmd1] = data;
6606         sachen_reg[state->mmc_cmd1] = data;
65086607
6509         switch (mmc_cmd1)
6608         switch (state->mmc_cmd1)
65106609         {
65116610         case 0x05:
65126611            prg32(space->machine, data);
r7955r7956
65156614            sachen_set_mirror(space->machine, BIT(data, 0) ? 0 : (data >> 1) & 0x03);
65166615            break;
65176616         default:
6518            if (mmc_chr_source == CHRROM)
6617            if (state->mmc_chr_source == CHRROM)
65196618            {
65206619               chr1_0(space->machine, (sachen_reg[0] & 0x07), CHRROM);
65216620               chr1_1(space->machine, (sachen_reg[1] & 0x07) | (sachen_reg[4] << 4 & 0x10), CHRROM);
r7955r7956
65486647
65496648static WRITE8_HANDLER( mapper138_l_w )
65506649{
6650   nes_state *state = (nes_state *)space->machine->driver_data;
65516651   UINT8 bank_helper1 = 0, bank_helper2 = 0;
65526652   LOG_MMC(("mapper138_l_w, offset: %04x, data: %02x\n", offset, data));
65536653
r7955r7956
65556655   if (!(offset & 0x100))
65566656   {
65576657      if (!(offset & 0x01))
6558         mmc_cmd1 = data & 0x07;
6658         state->mmc_cmd1 = data & 0x07;
65596659      else
65606660      {
6561         sachen_reg[mmc_cmd1] = data;
6661         sachen_reg[state->mmc_cmd1] = data;
65626662
6563         switch (mmc_cmd1)
6663         switch (state->mmc_cmd1)
65646664         {
65656665         case 0x05:
65666666            prg32(space->machine, data);
r7955r7956
65696669            sachen_set_mirror(space->machine, BIT(data, 0) ? 0 : (data >> 1) & 0x03);
65706670            break;
65716671         default:
6572            if (mmc_chr_source == CHRROM)
6672            if (state->mmc_chr_source == CHRROM)
65736673            {
65746674               bank_helper1 = sachen_reg[7] & 0x01;
65756675               bank_helper2 = (sachen_reg[4] & 0x07) << 3;
r7955r7956
66046704
66056705static WRITE8_HANDLER( mapper139_l_w )
66066706{
6707   nes_state *state = (nes_state *)space->machine->driver_data;
66076708   UINT8 bank_helper1 = 0, bank_helper2 = 0;
66086709   LOG_MMC(("mapper139_l_w, offset: %04x, data: %02x\n", offset, data));
66096710
r7955r7956
66116712   if (!(offset & 0x100))
66126713   {
66136714      if (!(offset & 0x01))
6614         mmc_cmd1 = data & 0x07;
6715         state->mmc_cmd1 = data & 0x07;
66156716      else
66166717      {
6617         sachen_reg[mmc_cmd1] = data;
6718         sachen_reg[state->mmc_cmd1] = data;
66186719
6619         switch (mmc_cmd1)
6720         switch (state->mmc_cmd1)
66206721         {
66216722         case 0x05:
66226723            prg32(space->machine, data);
r7955r7956
66256726            sachen_set_mirror(space->machine, BIT(data, 0) ? 0 : (data >> 1) & 0x03);
66266727            break;
66276728         default:
6628            if (mmc_chr_source == CHRROM)
6729            if (state->mmc_chr_source == CHRROM)
66296730            {
66306731               bank_helper1 = sachen_reg[7] & 0x01;
66316732               bank_helper2 = (sachen_reg[4] & 0x07) << 3;
r7955r7956
66806781
66816782static WRITE8_HANDLER( mapper141_l_w )
66826783{
6784   nes_state *state = (nes_state *)space->machine->driver_data;
66836785   UINT8 bank_helper1 = 0, bank_helper2 = 0;
66846786   LOG_MMC(("mapper141_l_w, offset: %04x, data: %02x\n", offset, data));
66856787
r7955r7956
66876789   if (!(offset & 0x100))
66886790   {
66896791      if (!(offset & 0x01))
6690         mmc_cmd1 = data & 0x07;
6792         state->mmc_cmd1 = data & 0x07;
66916793      else
66926794      {
6693         sachen_reg[mmc_cmd1] = data;
6795         sachen_reg[state->mmc_cmd1] = data;
66946796
6695         switch (mmc_cmd1)
6797         switch (state->mmc_cmd1)
66966798         {
66976799         case 0x05:
66986800            prg32(space->machine, data);
r7955r7956
67016803            sachen_set_mirror(space->machine, BIT(data, 0) ? 0 : (data >> 1) & 0x03);
67026804            break;
67036805         default:
6704            if (mmc_chr_source == CHRROM)
6806            if (state->mmc_chr_source == CHRROM)
67056807            {
67066808               bank_helper1 = sachen_reg[7] & 0x01;
67076809               bank_helper2 = (sachen_reg[4] & 0x07) << 3;
6708               chr2_0(space->machine, ((sachen_reg[bank_helper1 ? 0 : 0] & 0x07) | bank_helper2) << 1, mmc_chr_source);
6709               chr2_2(space->machine, ((sachen_reg[bank_helper1 ? 0 : 1] & 0x07) | bank_helper2) << 1 | 0x01, mmc_chr_source);
6710               chr2_4(space->machine, ((sachen_reg[bank_helper1 ? 0 : 2] & 0x07) | bank_helper2) << 1, mmc_chr_source);
6711               chr2_6(space->machine, ((sachen_reg[bank_helper1 ? 0 : 3] & 0x07) | bank_helper2) << 1 | 0x01, mmc_chr_source);
6810               chr2_0(space->machine, ((sachen_reg[bank_helper1 ? 0 : 0] & 0x07) | bank_helper2) << 1, state->mmc_chr_source);
6811               chr2_2(space->machine, ((sachen_reg[bank_helper1 ? 0 : 1] & 0x07) | bank_helper2) << 1 | 0x01, state->mmc_chr_source);
6812               chr2_4(space->machine, ((sachen_reg[bank_helper1 ? 0 : 2] & 0x07) | bank_helper2) << 1, state->mmc_chr_source);
6813               chr2_6(space->machine, ((sachen_reg[bank_helper1 ? 0 : 3] & 0x07) | bank_helper2) << 1 | 0x01, state->mmc_chr_source);
67126814            }
67136815            break;
67146816         }
r7955r7956
69137015
69147016static WRITE8_HANDLER( mapper150_l_w )
69157017{
7018   nes_state *state = (nes_state *)space->machine->driver_data;
69167019   LOG_MMC(("mapper150_l_w, offset: %04x, data: %02x\n", offset, data));
69177020
69187021   /* write happens only if we are at 0x4100 + k * 0x200, but 0x4100 is offset = 0 */
69197022   if (!(offset & 0x100))
69207023   {
69217024      if (!(offset & 0x01))
6922         mmc_cmd1 = data & 0x07;
7025         state->mmc_cmd1 = data & 0x07;
69237026      else
69247027      {
6925         switch (mmc_cmd1)
7028         switch (state->mmc_cmd1)
69267029         {
69277030         case 0x02:
6928            vrom_bank[0] = (vrom_bank[0] & ~0x08) | ((data << 3) & 0x08);
6929            chr8(space->machine, vrom_bank[0], CHRROM);
7031            state->vrom_bank[0] = (state->vrom_bank[0] & ~0x08) | ((data << 3) & 0x08);
7032            chr8(space->machine, state->vrom_bank[0], CHRROM);
69307033            prg32(space->machine, data & 0x01);
69317034            break;
69327035         case 0x04:
6933            vrom_bank[0] = (vrom_bank[0] & ~0x04) | ((data << 2) & 0x04);
6934            chr8(space->machine, vrom_bank[0], CHRROM);
7036            state->vrom_bank[0] = (state->vrom_bank[0] & ~0x04) | ((data << 2) & 0x04);
7037            chr8(space->machine, state->vrom_bank[0], CHRROM);
69357038            break;
69367039         case 0x05:
69377040            prg32(space->machine, data & 0x07);
69387041            break;
69397042         case 0x06:
6940            vrom_bank[0] = (vrom_bank[0] & ~0x03) | (data << 0 & 0x03);
6941            chr8(space->machine, vrom_bank[0], CHRROM);
7043            state->vrom_bank[0] = (state->vrom_bank[0] & ~0x03) | (data << 0 & 0x03);
7044            chr8(space->machine, state->vrom_bank[0], CHRROM);
69427045            break;
69437046         case 0x07:
69447047            sachen_set_mirror(space->machine, (data >> 1) & 0x03);
r7955r7956
69527055
69537056static READ8_HANDLER( mapper150_l_r )
69547057{
7058   nes_state *state = (nes_state *)space->machine->driver_data;
69557059   LOG_MMC(("mapper150_l_r, offset: %04x", offset));
69567060
69577061   /* read  happens only if we are at 0x4100 + k * 0x200, but 0x4100 is offset = 0 */
69587062   if (!(offset & 0x100))
6959      return (~mmc_cmd1 & 0x3f) /* ^ dips*/;   // we would need to check the Dips here
7063      return (~state->mmc_cmd1 & 0x3f) /* ^ dips*/;   // we would need to check the Dips here
69607064   else
69617065      return 0;
69627066}
r7955r7956
70847188
70857189static WRITE8_HANDLER( mapper154_w )
70867190{
7191   nes_state *state = (nes_state *)space->machine->driver_data;
70877192   LOG_MMC(("mapper154_w, offset: %04x, data: %02x\n", offset, data));
70887193   switch (offset & 1)
70897194   {
70907195   case 0:
70917196      set_nt_mirroring(space->machine, BIT(data, 6) ? PPU_MIRROR_HIGH : PPU_MIRROR_LOW);
7092      mmc_cmd1 = data & 7;
7197      state->mmc_cmd1 = data & 7;
70937198      break;
70947199   case 1:
7095      switch (mmc_cmd1)
7200      switch (state->mmc_cmd1)
70967201      {
70977202         case 0: chr2_0(space->machine, data >> 1, CHRROM); break;
70987203         case 1: chr2_2(space->machine, data >> 1, CHRROM); break;
r7955r7956
71987303// probably wrong...
71997304static void mapper158_set_mirror( running_machine *machine )
72007305{
7201   UINT8 nt_mode = mmc_cmd1 & 0x80;
7306   nes_state *state = (nes_state *)machine->driver_data;
7307   UINT8 nt_mode = state->mmc_cmd1 & 0x80;
72027308
7203   set_nt_page(machine, 0, ROM, vrom_bank[nt_mode ? 2 : 0], 0);
7204   set_nt_page(machine, 1, ROM, vrom_bank[nt_mode ? 3 : 0], 0);
7205   set_nt_page(machine, 2, ROM, vrom_bank[nt_mode ? 4 : 1], 0);
7206   set_nt_page(machine, 3, ROM, vrom_bank[nt_mode ? 5 : 1], 0);
7309   set_nt_page(machine, 0, ROM, state->vrom_bank[nt_mode ? 2 : 0], 0);
7310   set_nt_page(machine, 1, ROM, state->vrom_bank[nt_mode ? 3 : 0], 0);
7311   set_nt_page(machine, 2, ROM, state->vrom_bank[nt_mode ? 4 : 1], 0);
7312   set_nt_page(machine, 3, ROM, state->vrom_bank[nt_mode ? 5 : 1], 0);
72077313}
72087314
72097315static WRITE8_HANDLER( mapper158_w )
72107316{
7317   nes_state *state = (nes_state *)space->machine->driver_data;
72117318   UINT8 map158_helper, cmd;
72127319   LOG_MMC(("mapper158_w offset: %04x, data: %02x\n", offset, data));
72137320
72147321   switch (offset & 0x6001)
72157322   {
72167323      case 0x0000:
7217         map158_helper = mmc_cmd1 ^ data;
7218         mmc_cmd1 = data;
7324         map158_helper = state->mmc_cmd1 ^ data;
7325         state->mmc_cmd1 = data;
72197326
72207327         /* Has PRG Mode changed? */
72217328         if (map158_helper & 0x40)
r7955r7956
72307337         break;
72317338
72327339      case 0x0001:
7233         cmd = mmc_cmd1 & 0x0f;
7340         cmd = state->mmc_cmd1 & 0x0f;
72347341         switch (cmd)
72357342         {
72367343            case 0: case 1:
72377344            case 2: case 3:
72387345            case 4: case 5:
7239               vrom_bank[cmd] = data;
7346               state->vrom_bank[cmd] = data;
72407347               mapper64_set_chr(space->machine);
72417348               mapper158_set_mirror(space->machine);
72427349               break;
72437350            case 6: case 7:
7244               prg_bank[cmd - 6] = data;
7351               state->prg_bank[cmd - 6] = data;
72457352               mapper64_set_prg(space->machine);
72467353               break;
72477354            case 8: case 9:
7248               vrom_bank[cmd - 2] = data;
7355               state->vrom_bank[cmd - 2] = data;
72497356               mapper64_set_chr(space->machine);
72507357               mapper158_set_mirror(space->machine);
72517358               break;
72527359            case 0x0f:
7253               prg_bank[2] = data;
7360               state->prg_bank[2] = data;
72547361               mapper64_set_prg(space->machine);
72557362               break;
72567363         }
r7955r7956
76527759
76537760static WRITE8_HANDLER( mapper178_l_w )
76547761{
7762   nes_state *state = (nes_state *)space->machine->driver_data;
76557763   LOG_MMC(("mapper178_l_w, offset: %04x, data: %02x\n", offset, data));
76567764
76577765   switch (offset)
r7955r7956
76607768      set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
76617769      break;
76627770   case 0x701:
7663      mmc_cmd1 = (mmc_cmd1 & 0x0c) | ((data >> 1) & 0x03);
7664      prg32(space->machine, mmc_cmd1);
7771      state->mmc_cmd1 = (state->mmc_cmd1 & 0x0c) | ((data >> 1) & 0x03);
7772      prg32(space->machine, state->mmc_cmd1);
76657773      break;
76667774   case 0x702:
7667      mmc_cmd1 = (mmc_cmd1 & 0x03) | ((data << 2) & 0x0c);
7775      state->mmc_cmd1 = (state->mmc_cmd1 & 0x03) | ((data << 2) & 0x0c);
76687776      break;
76697777   }
76707778}
r7955r7956
77457853
77467854static WRITE8_HANDLER( mapper182_w )
77477855{
7856   nes_state *state = (nes_state *)space->machine->driver_data;
77487857   LOG_MMC(("mapper182_w, offset: %04x, data: %02x\n", offset, data));
77497858
77507859   switch (offset & 0x7003)
r7955r7956
77537862      set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
77547863      break;
77557864   case 0x2000:
7756      mmc_cmd1 = data;
7865      state->mmc_cmd1 = data;
77577866      break;
77587867   case 0x4000:
7759      switch (mmc_cmd1)
7868      switch (state->mmc_cmd1)
77607869      {
77617870      case 0:
77627871         chr2_0(space->machine, data >> 1, CHRROM);
r7955r7956
77877896   case 0x6003:
77887897      if (data)
77897898      {
7790         IRQ_count = data;
7791         IRQ_enable = 1;
7899         state->IRQ_count = data;
7900         state->IRQ_enable = 1;
77927901      }
77937902      else
7794         IRQ_enable = 0;
7903         state->IRQ_enable = 0;
77957904      break;
77967905   }
77977906}
r7955r7956
78767985
78777986static void mapper187_set_prg( running_machine *machine, int prg_base, int prg_mask )
78787987{
7879   UINT8 prg_flip = (mmc_cmd1 & 0x40) ? 2 : 0;
7988   nes_state *state = (nes_state *)machine->driver_data;
7989   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
78807990
78817991   if (!(mapper187_reg[0] & 0x80))
78827992   {
7883      prg8_89(machine, prg_base | (prg_bank[0 ^ prg_flip] & prg_mask));
7884      prg8_ab(machine, prg_base | (prg_bank[1] & prg_mask));
7885      prg8_cd(machine, prg_base | (prg_bank[2 ^ prg_flip] & prg_mask));
7886      prg8_ef(machine, prg_base | (prg_bank[3] & prg_mask));
7993      prg8_89(machine, prg_base | (state->prg_bank[0 ^ prg_flip] & prg_mask));
7994      prg8_ab(machine, prg_base | (state->prg_bank[1] & prg_mask));
7995      prg8_cd(machine, prg_base | (state->prg_bank[2 ^ prg_flip] & prg_mask));
7996      prg8_ef(machine, prg_base | (state->prg_bank[3] & prg_mask));
78877997   }
78887998}
78897999
78908000static void mapper187_set_chr( running_machine *machine, UINT8 chr, int chr_base, int chr_mask )
78918001{
7892   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
8002   nes_state *state = (nes_state *)machine->driver_data;
8003   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
78938004   UINT8 new_base[8];
78948005   int i;
78958006
78968007   for (i = 0; i < 8; i++)
78978008      new_base[i] = ((i & 0x04) == chr_page)? (chr_base | 0x100) : chr_base;
78988009
7899   chr1_x(machine, chr_page ^ 0, new_base[0] | ((vrom_bank[0] & ~0x01) & chr_mask), chr);
7900   chr1_x(machine, chr_page ^ 1, new_base[1] | ((vrom_bank[0] |  0x01) & chr_mask), chr);
7901   chr1_x(machine, chr_page ^ 2, new_base[2] | ((vrom_bank[1] & ~0x01) & chr_mask), chr);
7902   chr1_x(machine, chr_page ^ 3, new_base[3] | ((vrom_bank[1] |  0x01) & chr_mask), chr);
7903   chr1_x(machine, chr_page ^ 4, new_base[4] | (vrom_bank[2] & chr_mask), chr);
7904   chr1_x(machine, chr_page ^ 5, new_base[5] | (vrom_bank[3] & chr_mask), chr);
7905   chr1_x(machine, chr_page ^ 6, new_base[6] | (vrom_bank[4] & chr_mask), chr);
7906   chr1_x(machine, chr_page ^ 7, new_base[7] | (vrom_bank[5] & chr_mask), chr);
8010   chr1_x(machine, chr_page ^ 0, new_base[0] | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr);
8011   chr1_x(machine, chr_page ^ 1, new_base[1] | ((state->vrom_bank[0] |  0x01) & chr_mask), chr);
8012   chr1_x(machine, chr_page ^ 2, new_base[2] | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr);
8013   chr1_x(machine, chr_page ^ 3, new_base[3] | ((state->vrom_bank[1] |  0x01) & chr_mask), chr);
8014   chr1_x(machine, chr_page ^ 4, new_base[4] | (state->vrom_bank[2] & chr_mask), chr);
8015   chr1_x(machine, chr_page ^ 5, new_base[5] | (state->vrom_bank[3] & chr_mask), chr);
8016   chr1_x(machine, chr_page ^ 6, new_base[6] | (state->vrom_bank[4] & chr_mask), chr);
8017   chr1_x(machine, chr_page ^ 7, new_base[7] | (state->vrom_bank[5] & chr_mask), chr);
79078018}
79088019
79098020static WRITE8_HANDLER( mapper187_l_w )
79108021{
8022   nes_state *state = (nes_state *)space->machine->driver_data;
79118023   UINT8 new_bank;
79128024   LOG_MMC(("mapper187_l_w, offset: %04x, data: %02x\n", offset, data));
79138025   offset += 0x100;
r7955r7956
79468058         }
79478059      }
79488060      else
7949         mapper187_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8061         mapper187_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
79508062   }
79518063}
79528064
r7955r7956
79638075
79648076static WRITE8_HANDLER( mapper187_w )
79658077{
8078   nes_state *state = (nes_state *)space->machine->driver_data;
79668079   UINT8 MMC3_helper, cmd;
79678080   LOG_MMC(("mapper187_w, offset: %04x, data: %02x\n", offset, data));
79688081
r7955r7956
79708083   {
79718084   case 0x0000:
79728085      mapper187_reg[2] = 1;
7973      MMC3_helper = mmc_cmd1 ^ data;
7974      mmc_cmd1 = data;
8086      MMC3_helper = state->mmc_cmd1 ^ data;
8087      state->mmc_cmd1 = data;
79758088
79768089      /* Has PRG Mode changed? */
79778090      if (MMC3_helper & 0x40)
7978         mapper187_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8091         mapper187_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
79798092
79808093      /* Has CHR Mode changed? */
79818094      if (MMC3_helper & 0x80)
7982         mapper187_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8095         mapper187_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
79838096      break;
79848097
79858098   case 0x0001:
79868099      if (mapper187_reg[2])
79878100      {
7988         cmd = mmc_cmd1 & 0x07;
8101         cmd = state->mmc_cmd1 & 0x07;
79898102         switch (cmd)
79908103         {
79918104         case 0: case 1:
79928105         case 2: case 3: case 4: case 5:
7993            vrom_bank[cmd] = data;
7994            mapper187_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8106            state->vrom_bank[cmd] = data;
8107            mapper187_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
79958108            break;
79968109         case 6:
79978110         case 7:
7998            prg_bank[cmd - 6] = data;
7999            mapper187_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8111            state->prg_bank[cmd - 6] = data;
8112            mapper187_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
80008113            break;
80018114         }
80028115      }
r7955r7956
80688181/* This is like mapper4 but with no PRG bankswitch (beacuse it is handled by low writes) */
80698182static WRITE8_HANDLER( mapper189_w )
80708183{
8184   nes_state *state = (nes_state *)space->machine->driver_data;
80718185   UINT8 MMC3_helper, cmd;
80728186   LOG_MMC(("mapper189_w, offset: %04x, data: %02x\n", offset, data));
80738187
80748188   switch (offset & 0x6001)
80758189   {
80768190   case 0x0000:
8077      MMC3_helper = mmc_cmd1 ^ data;
8078      mmc_cmd1 = data;
8191      MMC3_helper = state->mmc_cmd1 ^ data;
8192      state->mmc_cmd1 = data;
80798193
80808194      /* Has CHR Mode changed? */
80818195      if (MMC3_helper & 0x80)
8082         mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8196         mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
80838197      break;
80848198
80858199   case 0x0001:
8086      cmd = mmc_cmd1 & 0x07;
8200      cmd = state->mmc_cmd1 & 0x07;
80878201      switch (cmd)
80888202      {
80898203      case 0: case 1:
80908204      case 2: case 3: case 4: case 5:
8091         vrom_bank[cmd] = data;
8092         mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8205         state->vrom_bank[cmd] = data;
8206         mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
80938207         break;
80948208      }
80958209      break;
r7955r7956
81288242
81298243static void mapper191_set_chr( running_machine *machine, int chr_base, int chr_mask )
81308244{
8131   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
8245   nes_state *state = (nes_state *)machine->driver_data;
8246   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
81328247   UINT8 chr_src[6];
81338248   int i;
81348249
81358250   for (i = 0; i < 6; i++)
8136      chr_src[i] = (vrom_bank[i] & 0x80) ? CHRRAM : CHRROM;
8251      chr_src[i] = (state->vrom_bank[i] & 0x80) ? CHRRAM : CHRROM;
81378252
8138   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
8139   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
8140   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
8141   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
8142   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr_src[2]);
8143   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr_src[3]);
8144   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr_src[4]);
8145   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr_src[5]);
8253   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
8254   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
8255   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
8256   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
8257   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr_src[2]);
8258   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr_src[3]);
8259   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr_src[4]);
8260   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr_src[5]);
81468261}
81478262
81488263static WRITE8_HANDLER( mapper191_w )
81498264{
8265   nes_state *state = (nes_state *)space->machine->driver_data;
81508266   UINT8 MMC3_helper, cmd;
81518267   LOG_MMC(("mapper191_w, offset: %04x, data: %02x\n", offset, data));
81528268
81538269   switch (offset & 0x6001)
81548270   {
81558271   case 0x0000:
8156      MMC3_helper = mmc_cmd1 ^ data;
8157      mmc_cmd1 = data;
8272      MMC3_helper = state->mmc_cmd1 ^ data;
8273      state->mmc_cmd1 = data;
81588274
81598275      /* Has PRG Mode changed? */
81608276      if (MMC3_helper & 0x40)
8161         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8277         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
81628278
81638279      /* Has CHR Mode changed? */
81648280      if (MMC3_helper & 0x80)
8165         mapper191_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8281         mapper191_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
81668282      break;
81678283
81688284   case 0x0001:
8169      cmd = mmc_cmd1 & 0x07;
8285      cmd = state->mmc_cmd1 & 0x07;
81708286      switch (cmd)
81718287      {
81728288      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
81738289      case 2: case 3: case 4: case 5:
8174         vrom_bank[cmd] = data;
8175         mapper191_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8290         state->vrom_bank[cmd] = data;
8291         mapper191_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
81768292         break;
81778293      case 6:
81788294      case 7:
8179         prg_bank[cmd - 6] = data;
8180         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8295         state->prg_bank[cmd - 6] = data;
8296         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
81818297         break;
81828298      }
81838299      break;
r7955r7956
82058321
82068322static void mapper192_set_chr( running_machine *machine, int chr_base, int chr_mask )
82078323{
8208   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
8324   nes_state *state = (nes_state *)machine->driver_data;
8325   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
82098326   UINT8 chr_src[6];
82108327   int i;
82118328
82128329   for (i = 0; i < 6; i++)
8213      chr_src[i] = ((vrom_bank[i] == 0x08) || (vrom_bank[i] == 0x09) || (vrom_bank[i] == 0x0a) || (vrom_bank[i] == 0x0b)) ? CHRRAM : CHRROM;
8330      chr_src[i] = ((state->vrom_bank[i] == 0x08) || (state->vrom_bank[i] == 0x09) || (state->vrom_bank[i] == 0x0a) || (state->vrom_bank[i] == 0x0b)) ? CHRRAM : CHRROM;
82148331
8215   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
8216   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
8217   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
8218   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
8219   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr_src[2]);
8220   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr_src[3]);
8221   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr_src[4]);
8222   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr_src[5]);
8332   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
8333   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
8334   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
8335   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
8336   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr_src[2]);
8337   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr_src[3]);
8338   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr_src[4]);
8339   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr_src[5]);
82238340}
82248341
82258342static WRITE8_HANDLER( mapper192_w )
82268343{
8344   nes_state *state = (nes_state *)space->machine->driver_data;
82278345   UINT8 MMC3_helper, cmd;
82288346   LOG_MMC(("mapper192_w, offset: %04x, data: %02x\n", offset, data));
82298347
82308348   switch (offset & 0x6001)
82318349   {
82328350   case 0x0000:
8233      MMC3_helper = mmc_cmd1 ^ data;
8234      mmc_cmd1 = data;
8351      MMC3_helper = state->mmc_cmd1 ^ data;
8352      state->mmc_cmd1 = data;
82358353
82368354      /* Has PRG Mode changed? */
82378355      if (MMC3_helper & 0x40)
8238         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8356         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
82398357
82408358      /* Has CHR Mode changed? */
82418359      if (MMC3_helper & 0x80)
8242         mapper192_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8360         mapper192_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
82438361      break;
82448362
82458363   case 0x0001:
8246      cmd = mmc_cmd1 & 0x07;
8364      cmd = state->mmc_cmd1 & 0x07;
82478365      switch (cmd)
82488366      {
82498367      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
82508368      case 2: case 3: case 4: case 5:
8251         vrom_bank[cmd] = data;
8252         mapper192_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8369         state->vrom_bank[cmd] = data;
8370         mapper192_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
82538371         break;
82548372      case 6:
82558373      case 7:
8256         prg_bank[cmd - 6] = data;
8257         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8374         state->prg_bank[cmd - 6] = data;
8375         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
82588376         break;
82598377      }
82608378      break;
r7955r7956
83178435
83188436static void mapper194_set_chr( running_machine *machine, int chr_base, int chr_mask )
83198437{
8320   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
8438   nes_state *state = (nes_state *)machine->driver_data;
8439   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
83218440   UINT8 chr_src[6];
83228441   int i;
83238442
83248443   for (i = 0; i < 6; i++)
8325      chr_src[i] = (vrom_bank[i] < 0x02) ? CHRRAM : CHRROM;
8444      chr_src[i] = (state->vrom_bank[i] < 0x02) ? CHRRAM : CHRROM;
83268445
8327   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
8328   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
8329   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
8330   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
8331   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr_src[2]);
8332   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr_src[3]);
8333   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr_src[4]);
8334   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr_src[5]);
8446   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
8447   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
8448   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
8449   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
8450   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr_src[2]);
8451   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr_src[3]);
8452   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr_src[4]);
8453   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr_src[5]);
83358454}
83368455
83378456static WRITE8_HANDLER( mapper194_w )
83388457{
8458   nes_state *state = (nes_state *)space->machine->driver_data;
83398459   UINT8 MMC3_helper, cmd;
83408460   LOG_MMC(("mapper194_w, offset: %04x, data: %02x\n", offset, data));
83418461
83428462   switch (offset & 0x6001)
83438463   {
83448464   case 0x0000:
8345      MMC3_helper = mmc_cmd1 ^ data;
8346      mmc_cmd1 = data;
8465      MMC3_helper = state->mmc_cmd1 ^ data;
8466      state->mmc_cmd1 = data;
83478467
83488468      /* Has PRG Mode changed? */
83498469      if (MMC3_helper & 0x40)
8350         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8470         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
83518471
83528472      /* Has CHR Mode changed? */
83538473      if (MMC3_helper & 0x80)
8354         mapper194_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8474         mapper194_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
83558475      break;
83568476
83578477   case 0x0001:
8358      cmd = mmc_cmd1 & 0x07;
8478      cmd = state->mmc_cmd1 & 0x07;
83598479      switch (cmd)
83608480      {
83618481      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
83628482      case 2: case 3: case 4: case 5:
8363         vrom_bank[cmd] = data;
8364         mapper194_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8483         state->vrom_bank[cmd] = data;
8484         mapper194_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
83658485         break;
83668486      case 6:
83678487      case 7:
8368         prg_bank[cmd - 6] = data;
8369         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8488         state->prg_bank[cmd - 6] = data;
8489         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
83708490         break;
83718491      }
83728492      break;
r7955r7956
83958515
83968516static void mapper195_set_chr( running_machine *machine, int chr_base, int chr_mask )
83978517{
8398   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
8518   nes_state *state = (nes_state *)machine->driver_data;
8519   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
83998520   UINT8 chr_src[6];
84008521   int i;
84018522
84028523   for (i = 0; i < 6; i++)
8403      chr_src[i] = (vrom_bank[i] < 0x04) ? CHRRAM : CHRROM;
8524      chr_src[i] = (state->vrom_bank[i] < 0x04) ? CHRRAM : CHRROM;
84048525
8405   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
8406   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
8407   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
8408   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
8409   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr_src[2]);
8410   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr_src[3]);
8411   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr_src[4]);
8412   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr_src[5]);
8526   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr_src[0]);
8527   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), chr_src[0]);
8528   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr_src[1]);
8529   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), chr_src[1]);
8530   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr_src[2]);
8531   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr_src[3]);
8532   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr_src[4]);
8533   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr_src[5]);
84138534}
84148535
84158536static WRITE8_HANDLER( mapper195_w )
84168537{
8538   nes_state *state = (nes_state *)space->machine->driver_data;
84178539   UINT8 MMC3_helper, cmd;
84188540   LOG_MMC(("mapper195_w, offset: %04x, data: %02x\n", offset, data));
84198541
84208542   switch (offset & 0x6001)
84218543   {
84228544   case 0x0000:
8423      MMC3_helper = mmc_cmd1 ^ data;
8424      mmc_cmd1 = data;
8545      MMC3_helper = state->mmc_cmd1 ^ data;
8546      state->mmc_cmd1 = data;
84258547
84268548      /* Has PRG Mode changed? */
84278549      if (MMC3_helper & 0x40)
8428         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8550         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
84298551
84308552      /* Has CHR Mode changed? */
84318553      if (MMC3_helper & 0x80)
8432         mapper195_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8554         mapper195_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
84338555      break;
84348556
84358557   case 0x0001:
8436      cmd = mmc_cmd1 & 0x07;
8558      cmd = state->mmc_cmd1 & 0x07;
84378559      switch (cmd)
84388560      {
84398561      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
84408562      case 2: case 3: case 4: case 5:
8441         vrom_bank[cmd] = data;
8442         mapper195_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8563         state->vrom_bank[cmd] = data;
8564         mapper195_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
84438565         break;
84448566      case 6:
84458567      case 7:
8446         prg_bank[cmd - 6] = data;
8447         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8568         state->prg_bank[cmd - 6] = data;
8569         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
84488570         break;
84498571      }
84508572      break;
r7955r7956
84918613
84928614static void mapper197_set_chr( running_machine *machine, UINT8 chr_source, int chr_base, int chr_mask )
84938615{
8494   chr4_0(machine, chr_base | ((vrom_bank[0] >> 1) & chr_mask), chr_source);
8495   chr2_4(machine, chr_base | (vrom_bank[1] & chr_mask), chr_source);
8496   chr2_6(machine, chr_base | (vrom_bank[2] & chr_mask), chr_source);
8616   nes_state *state = (nes_state *)machine->driver_data;
8617   chr4_0(machine, chr_base | ((state->vrom_bank[0] >> 1) & chr_mask), chr_source);
8618   chr2_4(machine, chr_base | (state->vrom_bank[1] & chr_mask), chr_source);
8619   chr2_6(machine, chr_base | (state->vrom_bank[2] & chr_mask), chr_source);
84978620}
84988621
84998622static WRITE8_HANDLER( mapper197_w )
85008623{
8624   nes_state *state = (nes_state *)space->machine->driver_data;
85018625   UINT8 MMC3_helper, cmd;
85028626   LOG_MMC(("mapper197_w, offset: %04x, data: %02x\n", offset, data));
85038627
85048628   switch (offset & 0x6001)
85058629   {
85068630   case 0x0000:
8507      MMC3_helper = mmc_cmd1 ^ data;
8508      mmc_cmd1 = data;
8631      MMC3_helper = state->mmc_cmd1 ^ data;
8632      state->mmc_cmd1 = data;
85098633
85108634      /* Has PRG Mode changed? */
85118635      if (MMC3_helper & 0x40)
8512         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8636         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
85138637
85148638      /* Has CHR Mode changed? */
85158639      if (MMC3_helper & 0x80)
8516         mapper197_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8640         mapper197_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
85178641      break;
85188642
85198643   case 0x0001:
8520      cmd = mmc_cmd1 & 0x0f;
8644      cmd = state->mmc_cmd1 & 0x0f;
85218645      switch (cmd)
85228646      {
85238647      case 0: case 2: case 4:
8524         vrom_bank[cmd >> 1] = data;
8525         mapper197_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8648         state->vrom_bank[cmd >> 1] = data;
8649         mapper197_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
85268650         break;
85278651      case 6:
85288652      case 7:
8529         prg_bank[cmd - 6] = data;
8530         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8653         state->prg_bank[cmd - 6] = data;
8654         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
85318655         break;
85328656      }
85338657      break;
r7955r7956
85538677
85548678static void mapper198_set_prg( running_machine *machine, int prg_base, int prg_mask )
85558679{
8556   UINT8 prg_flip = (mmc_cmd1 & 0x40) ? 2 : 0;
8680   nes_state *state = (nes_state *)machine->driver_data;
8681   UINT8 prg_flip = (state->mmc_cmd1 & 0x40) ? 2 : 0;
85578682
8558   prg8_89(machine, prg_base | (prg_bank[0 ^ prg_flip] & prg_mask));
8559   prg8_ab(machine, prg_base | (prg_bank[1] & prg_mask));
8560   prg8_cd(machine, prg_base | (prg_bank[2 ^ prg_flip] & prg_mask));
8561   prg8_ef(machine, prg_base | (prg_bank[3] & prg_mask));
8683   prg8_89(machine, prg_base | (state->prg_bank[0 ^ prg_flip] & prg_mask));
8684   prg8_ab(machine, prg_base | (state->prg_bank[1] & prg_mask));
8685   prg8_cd(machine, prg_base | (state->prg_bank[2 ^ prg_flip] & prg_mask));
8686   prg8_ef(machine, prg_base | (state->prg_bank[3] & prg_mask));
85628687}
85638688
85648689static WRITE8_HANDLER( mapper198_w )
85658690{
8691   nes_state *state = (nes_state *)space->machine->driver_data;
85668692   UINT8 MMC3_helper, cmd;
85678693   LOG_MMC(("mapper198_w, offset: %04x, data: %02x\n", offset, data));
85688694
85698695   switch (offset & 0x6001)
85708696   {
85718697   case 0x0000:
8572      MMC3_helper = mmc_cmd1 ^ data;
8573      mmc_cmd1 = data;
8698      MMC3_helper = state->mmc_cmd1 ^ data;
8699      state->mmc_cmd1 = data;
85748700
85758701      /* Has PRG Mode changed? */
85768702      if (MMC3_helper & 0x40)
8577         mapper198_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8703         mapper198_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
85788704
85798705      /* Has CHR Mode changed? */
85808706      if (MMC3_helper & 0x80)
8581         mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8707         mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
85828708      break;
85838709
85848710   case 0x0001:
8585      cmd = mmc_cmd1 & 0x07;
8711      cmd = state->mmc_cmd1 & 0x07;
85868712      switch (cmd)
85878713      {
85888714      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
85898715      case 2: case 3: case 4: case 5:
8590         vrom_bank[cmd] = data;
8591         mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8716         state->vrom_bank[cmd] = data;
8717         mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
85928718         break;
85938719      case 6:
85948720      case 7:
8595         prg_bank[cmd - 6] = data & ((data > 0x3f) ? 0x4f : 0x3f);
8596         mapper198_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8721         state->prg_bank[cmd - 6] = data & ((data > 0x3f) ? 0x4f : 0x3f);
8722         mapper198_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
85978723         break;
85988724      }
85998725      break;
r7955r7956
86208746
86218747static void mapper199_set_chr( running_machine *machine, int chr_base, int chr_mask )
86228748{
8623   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
8749   nes_state *state = (nes_state *)machine->driver_data;
8750   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
86248751   UINT8 chr_src[8];
86258752   int i;
86268753
86278754   for (i = 0; i < 8; i++)
8628      chr_src[i] = (vrom_bank[i] < 0x08) ? CHRRAM : CHRROM;
8755      chr_src[i] = (state->vrom_bank[i] < 0x08) ? CHRRAM : CHRROM;
86298756
8630   chr1_x(machine, chr_page ^ 0, chr_base | (vrom_bank[0] & chr_mask), chr_src[0]);
8631   chr1_x(machine, chr_page ^ 1, chr_base | (vrom_bank[6] & chr_mask), chr_src[6]);
8632   chr1_x(machine, chr_page ^ 2, chr_base | (vrom_bank[1] & chr_mask), chr_src[1]);
8633   chr1_x(machine, chr_page ^ 3, chr_base | (vrom_bank[7] & chr_mask), chr_src[7]);
8634   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr_src[2]);
8635   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr_src[3]);
8636   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr_src[4]);
8637   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr_src[5]);
8757   chr1_x(machine, chr_page ^ 0, chr_base | (state->vrom_bank[0] & chr_mask), chr_src[0]);
8758   chr1_x(machine, chr_page ^ 1, chr_base | (state->vrom_bank[6] & chr_mask), chr_src[6]);
8759   chr1_x(machine, chr_page ^ 2, chr_base | (state->vrom_bank[1] & chr_mask), chr_src[1]);
8760   chr1_x(machine, chr_page ^ 3, chr_base | (state->vrom_bank[7] & chr_mask), chr_src[7]);
8761   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr_src[2]);
8762   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr_src[3]);
8763   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr_src[4]);
8764   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr_src[5]);
86388765}
86398766
86408767static WRITE8_HANDLER( mapper199_w )
86418768{
8769   nes_state *state = (nes_state *)space->machine->driver_data;
86428770   UINT8 MMC3_helper, cmd;
86438771   LOG_MMC(("mapper199_w, offset: %04x, data: %02x\n", offset, data));
86448772
86458773   switch (offset & 0x6001)
86468774   {
86478775   case 0x0000:
8648      MMC3_helper = mmc_cmd1 ^ data;
8649      mmc_cmd1 = data;
8776      MMC3_helper = state->mmc_cmd1 ^ data;
8777      state->mmc_cmd1 = data;
86508778
86518779      /* Has PRG Mode changed? */
86528780      if (MMC3_helper & 0x40)
8653         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8781         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
86548782
86558783      /* Has CHR Mode changed? */
86568784      if (MMC3_helper & 0x80)
8657         mapper199_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8785         mapper199_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
86588786      break;
86598787
86608788   case 0x0001:
8661      cmd = mmc_cmd1 & 0x0f;
8789      cmd = state->mmc_cmd1 & 0x0f;
86628790      switch (cmd)
86638791      {
86648792      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
86658793      case 2: case 3: case 4: case 5:
8666         vrom_bank[cmd] = data;
8667         mapper199_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8794         state->vrom_bank[cmd] = data;
8795         mapper199_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
86688796         break;
86698797      case 6:
86708798      case 7:
86718799      case 8:
86728800      case 9:
8673         prg_bank[cmd - 6] = data;
8674         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8801         state->prg_bank[cmd - 6] = data;
8802         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
86758803         break;
86768804      case 0x0a: case 0x0b:
8677         vrom_bank[cmd - 4] = data;
8678         mapper199_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
8805         state->vrom_bank[cmd - 4] = data;
8806         mapper199_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
86798807         break;
86808808      }
86818809      break;
r7955r7956
88108938
88118939static WRITE8_HANDLER( mapper205_m_w )
88128940{
8941   nes_state *state = (nes_state *)space->machine->driver_data;
88138942   LOG_MMC(("mapper205_m_w, offset: %04x, data: %02x\n", offset, data));
88148943
88158944   if (offset & 0x0800)
88168945   {
8817      mmc_prg_base = (data & 0x03) << 4;
8818      mmc_prg_mask = (data & 0x02) ? 0x0f : 0x1f;
8819      mmc_chr_base = (data & 0x03) << 7;
8820      mmc_chr_mask = (data & 0x02) ? 0x7f : 0xff;
8821      mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
8822      mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
8946      state->mmc_prg_base = (data & 0x03) << 4;
8947      state->mmc_prg_mask = (data & 0x02) ? 0x0f : 0x1f;
8948      state->mmc_chr_base = (data & 0x03) << 7;
8949      state->mmc_chr_mask = (data & 0x02) ? 0x7f : 0xff;
8950      mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
8951      mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
88238952   }
88248953}
88258954
r7955r7956
89739102// replace with map189_w... they are the same!!
89749103static WRITE8_HANDLER( mapper208_w )
89759104{
9105   nes_state *state = (nes_state *)space->machine->driver_data;
89769106   UINT8 MMC3_helper, cmd;
89779107   LOG_MMC(("mapper208_w, offset: %04x, data: %02x\n", offset, data));
89789108
89799109   switch (offset & 0x6001)
89809110   {
89819111   case 0x0000:
8982      MMC3_helper = mmc_cmd1 ^ data;
8983      mmc_cmd1 = data;
9112      MMC3_helper = state->mmc_cmd1 ^ data;
9113      state->mmc_cmd1 = data;
89849114
89859115      /* Has CHR Mode changed? */
89869116      if (MMC3_helper & 0x80)
8987         mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
9117         mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
89889118      break;
89899119
89909120   case 0x0001:
8991      cmd = mmc_cmd1 & 0x07;
9121      cmd = state->mmc_cmd1 & 0x07;
89929122      switch (cmd)
89939123      {
89949124      case 0: case 1:
89959125      case 2: case 3: case 4: case 5:
8996         vrom_bank[cmd] = data;
8997         mapper4_set_chr(space->machine, mmc_chr_source, mmc_chr_base, mmc_chr_mask);
9126         state->vrom_bank[cmd] = data;
9127         mapper4_set_chr(space->machine, state->mmc_chr_source, state->mmc_chr_base, state->mmc_chr_mask);
89989128         break;
89999129      }
90009130      break;
r7955r7956
91319261// remove mask & base parameters!
91329262static void mapper215_set_prg( running_machine *machine, int prg_base, int prg_mask )
91339263{
9264   nes_state *state = (nes_state *)machine->driver_data;
91349265   if (map215_reg[0] & 0x80)
91359266   {
91369267      prg16_89ab(machine, (map215_reg[0] & 0xf0) | (map215_reg[1] & 0x10));
r7955r7956
91389269   }
91399270   else
91409271   {
9141      mmc_prg_base = (map215_reg[1] & 0x08) ? 0x20 : (map215_reg[1] & 0x10);
9142      mmc_prg_mask = (map215_reg[1] & 0x08) ? 0x1f : 0x0f;
9272      state->mmc_prg_base = (map215_reg[1] & 0x08) ? 0x20 : (map215_reg[1] & 0x10);
9273      state->mmc_prg_mask = (map215_reg[1] & 0x08) ? 0x1f : 0x0f;
91439274
9144      mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
9275      mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
91459276   }
91469277}
91479278
91489279static void mapper215_set_chr( running_machine *machine, UINT8 chr )
91499280{
9150   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
9281   nes_state *state = (nes_state *)machine->driver_data;
9282   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
91519283   int chr_base = (map215_reg[1] & 0x04) ? 0x100 : ((map215_reg[1] & 0x10) << 3);
91529284   int chr_mask = (map215_reg[1] & 0x04) ? 0xff : 0x7f;
91539285
9154   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), chr);
9155   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), chr);
9156   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), chr);
9157   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), chr);
9158   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr);
9159   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr);
9160   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr);
9161   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr);
9286   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr);
9287   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), chr);
9288   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr);
9289   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), chr);
9290   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr);
9291   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr);
9292   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr);
9293   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr);
91629294}
91639295
91649296static WRITE8_HANDLER( mapper215_l_w )
91659297{
9298   nes_state *state = (nes_state *)space->machine->driver_data;
91669299   LOG_MMC(("mapper215_l_w, offset: %04x, data: %02x\n", offset, data));
91679300   offset += 0x100;
91689301
91699302   if (offset == 0x1000)
91709303   {
91719304      map215_reg[0] = data;
9172      mapper215_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9305      mapper215_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
91739306   }
91749307   else if (offset == 0x1001)
91759308   {
91769309      map215_reg[1] = data;
9177      mapper215_set_chr(space->machine, mmc_chr_source);
9310      mapper215_set_chr(space->machine, state->mmc_chr_source);
91789311   }
91799312   else if (offset == 0x1007)
91809313   {
9181      mmc_cmd1 = 0;
9314      state->mmc_cmd1 = 0;
91829315      map215_reg[2] = data;
9183      mapper215_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9184      mapper215_set_chr(space->machine, mmc_chr_source);
9316      mapper215_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
9317      mapper215_set_chr(space->machine, state->mmc_chr_source);
91859318   }
91869319}
91879320
91889321static WRITE8_HANDLER( mapper215_w )
91899322{
9323   nes_state *state = (nes_state *)space->machine->driver_data;
91909324   UINT8 MMC3_helper, cmd;
91919325   static const UINT8 conv_table[8] = {0,2,5,3,6,1,7,4};
91929326   LOG_MMC(("mapper215_w, offset: %04x, data: %02x\n", offset, data));
r7955r7956
91969330      switch (offset & 0x6001)
91979331      {
91989332      case 0x0000:
9199         MMC3_helper = mmc_cmd1 ^ data;
9200         mmc_cmd1 = data;
9333         MMC3_helper = state->mmc_cmd1 ^ data;
9334         state->mmc_cmd1 = data;
92019335
92029336         /* Has PRG Mode changed? */
92039337         if (MMC3_helper & 0x40)
9204            mapper215_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9338            mapper215_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
92059339
92069340         /* Has CHR Mode changed? */
92079341         if (MMC3_helper & 0x80)
9208            mapper215_set_chr(space->machine, mmc_chr_source);
9342            mapper215_set_chr(space->machine, state->mmc_chr_source);
92099343         break;
92109344
92119345      case 0x0001:
9212         cmd = mmc_cmd1 & 0x07;
9346         cmd = state->mmc_cmd1 & 0x07;
92139347         switch (cmd)
92149348         {
92159349         case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
92169350         case 2: case 3: case 4: case 5:
9217            vrom_bank[cmd] = data;
9218            mapper215_set_chr(space->machine, mmc_chr_source);
9351            state->vrom_bank[cmd] = data;
9352            mapper215_set_chr(space->machine, state->mmc_chr_source);
92199353            break;
92209354         case 6:
92219355         case 7:
9222            prg_bank[cmd - 6] = data;
9223            mapper215_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9356            state->prg_bank[cmd - 6] = data;
9357            mapper215_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
92249358            break;
92259359         }
92269360         break;
r7955r7956
92389372         break;
92399373
92409374      case 0x0001:
9241         cmd = mmc_cmd1 & 0x07;
9375         cmd = state->mmc_cmd1 & 0x07;
92429376         if (map215_reg[3] && ((map215_reg[0] & 0x80) == 0 || cmd < 6))   // if we use the prg16 banks and cmd=6,7 DON'T enter!
92439377         {
92449378            map215_reg[3] = 0;
r7955r7956
92469380            {
92479381            case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
92489382            case 2: case 3: case 4: case 5:
9249               vrom_bank[cmd] = data;
9250               mapper215_set_chr(space->machine, mmc_chr_source);
9383               state->vrom_bank[cmd] = data;
9384               mapper215_set_chr(space->machine, state->mmc_chr_source);
92519385               break;
92529386            case 6:
92539387            case 7:
9254               prg_bank[cmd - 6] = data;
9255               mapper215_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9388               state->prg_bank[cmd - 6] = data;
9389               mapper215_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
92569390               break;
92579391            }
92589392         }
r7955r7956
92609394
92619395      case 0x2000:
92629396         data = (data & 0xc0) | conv_table[data & 0x07];
9263         MMC3_helper = mmc_cmd1 ^ data;
9264         mmc_cmd1 = data;
9397         MMC3_helper = state->mmc_cmd1 ^ data;
9398         state->mmc_cmd1 = data;
92659399
92669400         /* Has PRG Mode changed? */
92679401         if (MMC3_helper & 0x40)
9268            mapper215_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9402            mapper215_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
92699403
92709404         /* Has CHR Mode changed? */
92719405         if (MMC3_helper & 0x80)
9272            mapper215_set_chr(space->machine, mmc_chr_source);
9406            mapper215_set_chr(space->machine, state->mmc_chr_source);
92739407
92749408         map215_reg[3] = 1;
92759409         break;
r7955r7956
93189452
93199453static WRITE8_HANDLER( mapper216_w )
93209454{
9455   nes_state *state = (nes_state *)space->machine->driver_data;
93219456   LOG_MMC(("mapper216_w, offset: %04x, data: %02x\n", offset, data));
93229457
93239458   prg32(space->machine, offset);
9324   chr8(space->machine, offset >> 1, mmc_chr_source);
9459   chr8(space->machine, offset >> 1, state->mmc_chr_source);
93259460}
93269461
93279462/*************************************************************
r7955r7956
93409475// remove mask & base parameters!
93419476static void mapper217_set_prg( running_machine *machine, int prg_base, int prg_mask )
93429477{
9343   mmc_prg_base = (map217_reg[1] & 0x08) ? 0 : (map217_reg[1] & 0x10);
9344   mmc_prg_mask = (map217_reg[1] & 0x08) ? 0x1f : 0x0f;
9478   nes_state *state = (nes_state *)machine->driver_data;
9479   state->mmc_prg_base = (map217_reg[1] & 0x08) ? 0 : (map217_reg[1] & 0x10);
9480   state->mmc_prg_mask = (map217_reg[1] & 0x08) ? 0x1f : 0x0f;
93459481
9346   mmc_prg_base |= ((map217_reg[1] & 0x03) << 5);
9482   state->mmc_prg_base |= ((map217_reg[1] & 0x03) << 5);
93479483
9348   mapper4_set_prg(machine, mmc_prg_base, mmc_prg_mask);
9484   mapper4_set_prg(machine, state->mmc_prg_base, state->mmc_prg_mask);
93499485}
93509486
93519487static void mapper217_set_chr( running_machine *machine, UINT8 chr )
93529488{
9353   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
9489   nes_state *state = (nes_state *)machine->driver_data;
9490   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
93549491   int chr_base = (map217_reg[1] & 0x08) ? 0x00 : ((map217_reg[1] & 0x10) << 3);
93559492   int chr_mask = (map217_reg[1] & 0x08) ? 0xff : 0x7f;
93569493
93579494   chr_base |= ((map217_reg[1] & 0x03) << 8);
93589495
9359   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), chr);
9360   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), chr);
9361   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), chr);
9362   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), chr);
9363   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), chr);
9364   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), chr);
9365   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), chr);
9366   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), chr);
9496   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), chr);
9497   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), chr);
9498   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), chr);
9499   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), chr);
9500   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), chr);
9501   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), chr);
9502   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), chr);
9503   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), chr);
93679504}
93689505
93699506static WRITE8_HANDLER( mapper217_l_w )
93709507{
9508   nes_state *state = (nes_state *)space->machine->driver_data;
93719509   UINT8 bank;
93729510   LOG_MMC(("mapper217_l_w, offset: %04x, data: %02x\n", offset, data));
93739511   offset += 0x100;
r7955r7956
93829520         prg16_cdef(space->machine, bank);
93839521      }
93849522      else
9385         mapper217_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9523         mapper217_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
93869524   }
93879525   else if (offset == 0x1001)
93889526   {
93899527      map217_reg[1] = data;
9390      mapper217_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9528      mapper217_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
93919529   }
93929530   else if (offset == 0x1007)
93939531   {
r7955r7956
93979535
93989536static WRITE8_HANDLER( mapper217_w )
93999537{
9538   nes_state *state = (nes_state *)space->machine->driver_data;
94009539   UINT8 MMC3_helper, cmd;
94019540   static const UINT8 conv_table[8] = {0, 6, 3, 7, 5, 2, 4, 1};
94029541   LOG_MMC(("mapper217_w, offset: %04x, data: %02x\n", offset, data));
r7955r7956
94069545      switch (offset & 0x6001)
94079546      {
94089547      case 0x0000:
9409         MMC3_helper = mmc_cmd1 ^ data;
9410         mmc_cmd1 = data;
9548         MMC3_helper = state->mmc_cmd1 ^ data;
9549         state->mmc_cmd1 = data;
94119550
94129551         /* Has PRG Mode changed? */
94139552         if (MMC3_helper & 0x40)
9414            mapper217_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9553            mapper217_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
94159554
94169555         /* Has CHR Mode changed? */
94179556         if (MMC3_helper & 0x80)
9418            mapper217_set_chr(space->machine, mmc_chr_source);
9557            mapper217_set_chr(space->machine, state->mmc_chr_source);
94199558         break;
94209559
94219560      case 0x0001:
9422         cmd = mmc_cmd1 & 0x07;
9561         cmd = state->mmc_cmd1 & 0x07;
94239562         switch (cmd)
94249563         {
94259564         case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
94269565         case 2: case 3: case 4: case 5:
9427            vrom_bank[cmd] = data;
9428            mapper217_set_chr(space->machine, mmc_chr_source);
9566            state->vrom_bank[cmd] = data;
9567            mapper217_set_chr(space->machine, state->mmc_chr_source);
94299568            break;
94309569         case 6:
94319570         case 7:
9432            prg_bank[cmd - 6] = data;
9433            mapper217_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9571            state->prg_bank[cmd - 6] = data;
9572            mapper217_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
94349573            break;
94359574         }
94369575         break;
r7955r7956
94509589
94519590      case 0x0001:
94529591         data = (data & 0xc0) | conv_table[data & 0x07];
9453         MMC3_helper = mmc_cmd1 ^ data;
9454         mmc_cmd1 = data;
9592         MMC3_helper = state->mmc_cmd1 ^ data;
9593         state->mmc_cmd1 = data;
94559594
94569595         /* Has PRG Mode changed? */
94579596         if (MMC3_helper & 0x40)
9458            mapper217_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9597            mapper217_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
94599598
94609599         /* Has CHR Mode changed? */
94619600         if (MMC3_helper & 0x80)
9462            mapper217_set_chr(space->machine, mmc_chr_source);
9601            mapper217_set_chr(space->machine, state->mmc_chr_source);
94639602
94649603         map217_reg[3] = 1;
94659604         break;
94669605
94679606      case 0x2000:
9468         cmd = mmc_cmd1 & 0x07;
9607         cmd = state->mmc_cmd1 & 0x07;
94699608         if (map217_reg[3])
94709609         {
94719610            map217_reg[3] = 0;
r7955r7956
94739612            {
94749613            case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
94759614            case 2: case 3: case 4: case 5:
9476               vrom_bank[cmd] = data;
9477               mapper217_set_chr(space->machine, mmc_chr_source);
9615               state->vrom_bank[cmd] = data;
9616               mapper217_set_chr(space->machine, state->mmc_chr_source);
94789617               break;
94799618            case 6:
94809619            case 7:
9481               prg_bank[cmd - 6] = data;
9482               mapper217_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
9620               state->prg_bank[cmd - 6] = data;
9621               mapper217_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
94839622               break;
94849623            }
94859624         }
r7955r7956
95589697
95599698static WRITE8_HANDLER( mapper221_w )
95609699{
9700   nes_state *state = (nes_state *)space->machine->driver_data;
95619701   LOG_MMC(("mapper221_w, offset: %04x, data: %02x\n", offset, data));
95629702
95639703   if (offset < 0x4000)
r7955r7956
95659705      set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
95669706      offset = (offset >> 1) & 0xff;
95679707
9568      if (mmc_cmd1 != offset)
9708      if (state->mmc_cmd1 != offset)
95699709      {
9570         mmc_cmd1 = offset;
9571         mapper221_set_prg(space->machine, mmc_cmd1, mmc_cmd2);
9710         state->mmc_cmd1 = offset;
9711         mapper221_set_prg(space->machine, state->mmc_cmd1, state->mmc_cmd2);
95729712      }
95739713   }
95749714   else
95759715   {
95769716      offset &= 0x07;
95779717
9578      if (mmc_cmd2 != offset)
9718      if (state->mmc_cmd2 != offset)
95799719      {
9580         mmc_cmd2 = offset;
9581         mapper221_set_prg(space->machine, mmc_cmd1, mmc_cmd2);
9720         state->mmc_cmd2 = offset;
9721         mapper221_set_prg(space->machine, state->mmc_cmd1, state->mmc_cmd2);
95829722      }
95839723   }
95849724}
r7955r7956
95979737/* Scanline based IRQ ? */
95989738static void mapper222_irq( running_device *device, int scanline, int vblank, int blanked )
95999739{
9740   nes_state *state = (nes_state *)device->machine->driver_data;
96009741   if (scanline < PPU_BOTTOM_VISIBLE_SCANLINE)
96019742   {
9602      if (!IRQ_count || ++IRQ_count < 240)
9743      if (!state->IRQ_count || ++state->IRQ_count < 240)
96039744         return;
96049745
9605      IRQ_count = 0;
9746      state->IRQ_count = 0;
96069747      LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline,
96079748            video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen)));
96089749      cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE);
r7955r7956
96119752
96129753static WRITE8_HANDLER( mapper222_w )
96139754{
9755   nes_state *state = (nes_state *)space->machine->driver_data;
96149756   UINT8 bank;
96159757   LOG_MMC(("mapper222_w, offset: %04x, data: %02x\n", offset, data));
96169758
r7955r7956
96379779      chr1_x(space->machine, bank, data, CHRROM);
96389780      break;
96399781   case 0x7000:
9640      IRQ_count = data;
9782      state->IRQ_count = data;
96419783      break;
96429784   }
96439785}
r7955r7956
97199861
97209862static WRITE8_HANDLER( mapper226_w )
97219863{
9864   nes_state *state = (nes_state *)space->machine->driver_data;
97229865   int hi_bank;
97239866   int size_16;
97249867   int bank;
r7955r7956
97269869   LOG_MMC(("mapper226_w, offset: %04x, data: %02x\n", offset, data));
97279870
97289871   if (offset & 0x01)
9729   {
9730      mmc_cmd2 = data;
9731   }
9872      state->mmc_cmd2 = data;
97329873   else
9733   {
9734      mmc_cmd1 = data;
9735   }
9874      state->mmc_cmd1 = data;
97369875
9737   set_nt_mirroring(space->machine, BIT(mmc_cmd1, 6) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
9876   set_nt_mirroring(space->machine, BIT(state->mmc_cmd1, 6) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
97389877
9739   hi_bank = mmc_cmd1 & 0x01;
9740   size_16 = mmc_cmd1 & 0x20;
9741   bank = ((mmc_cmd1 & 0x1e) >> 1) | ((mmc_cmd1 & 0x80) >> 3) | ((mmc_cmd2 & 0x01) << 5);
9878   hi_bank = state->mmc_cmd1 & 0x01;
9879   size_16 = state->mmc_cmd1 & 0x20;
9880   bank = ((state->mmc_cmd1 & 0x1e) >> 1) | ((state->mmc_cmd1 & 0x80) >> 3) | ((state->mmc_cmd2 & 0x01) << 5);
97429881
97439882   if (size_16)
97449883   {
r7955r7956
994710086
994810087static void mapper232_set_prg( running_machine *machine )
994910088{
9950   prg16_89ab(machine, (mmc_cmd2 & 0x03) | ((mmc_cmd1 & 0x18) >> 1));
9951   prg16_cdef(machine, 0x03 | ((mmc_cmd1 & 0x18) >> 1));
10089   nes_state *state = (nes_state *)machine->driver_data;
10090   prg16_89ab(machine, (state->mmc_cmd2 & 0x03) | ((state->mmc_cmd1 & 0x18) >> 1));
10091   prg16_cdef(machine, 0x03 | ((state->mmc_cmd1 & 0x18) >> 1));
995210092}
995310093
995410094static WRITE8_HANDLER( mapper232_w )
995510095{
10096   nes_state *state = (nes_state *)space->machine->driver_data;
995610097   LOG_MMC(("mapper232_w, offset: %04x, data: %02x\n", offset, data));
995710098
995810099   if (offset < 0x2000)
9959      mmc_cmd1 = data;
10100      state->mmc_cmd1 = data;
996010101   else
9961      mmc_cmd2 = data;
10102      state->mmc_cmd2 = data;
996210103
996310104   mapper232_set_prg(space->machine);
996410105}
r7955r7956
1015010291
1015110292static WRITE8_HANDLER( mapper243_l_w )
1015210293{
10294   nes_state *state = (nes_state *)space->machine->driver_data;
1015310295   LOG_MMC(("mapper243_l_w, offset: %04x, data: %02x\n", offset, data));
1015410296
1015510297   /* write happens only if we are at 0x4100 + k * 0x200, but 0x4100 is offset = 0 */
1015610298   if (!(offset & 0x100))
1015710299   {
1015810300      if (!(offset & 0x01))
10159         mmc_cmd1 = data;
10301         state->mmc_cmd1 = data;
1016010302      else
1016110303      {
10162         switch (mmc_cmd1 & 0x07)
10304         switch (state->mmc_cmd1 & 0x07)
1016310305         {
1016410306         case 0x00:
1016510307            prg32(space->machine, 0);
1016610308            chr8(space->machine, 3, CHRROM);
1016710309            break;
1016810310         case 0x02:
10169            vrom_bank[0] = (vrom_bank[0] & ~0x08) | (data << 3 & 0x08);
10170            chr8(space->machine, vrom_bank[0], CHRROM);
10311            state->vrom_bank[0] = (state->vrom_bank[0] & ~0x08) | (data << 3 & 0x08);
10312            chr8(space->machine, state->vrom_bank[0], CHRROM);
1017110313            break;
1017210314         case 0x04:
10173            vrom_bank[0] = (vrom_bank[0] & ~0x01) | (data << 0 & 0x01);
10174            chr8(space->machine, vrom_bank[0], CHRROM);
10315            state->vrom_bank[0] = (state->vrom_bank[0] & ~0x01) | (data << 0 & 0x01);
10316            chr8(space->machine, state->vrom_bank[0], CHRROM);
1017510317            break;
1017610318         case 0x05:
1017710319            prg32(space->machine, data & 0x01);
1017810320            break;
1017910321         case 0x06:
10180            vrom_bank[0] = (vrom_bank[0] & ~0x06) | (data << 1 & 0x06);
10181            chr8(space->machine, vrom_bank[0], CHRROM);
10322            state->vrom_bank[0] = (state->vrom_bank[0] & ~0x06) | (data << 1 & 0x06);
10323            chr8(space->machine, state->vrom_bank[0], CHRROM);
1018210324            break;
1018310325         case 0x07:
1018410326            set_nt_mirroring(space->machine, BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
r7955r7956
1024410386   switch (offset & 0x6001)
1024510387   {
1024610388   case 0x0001:
10247      cmd = mmc_cmd1 & 0x0f;
10389      cmd = state->mmc_cmd1 & 0x0f;
1024810390      switch (cmd)
1024910391      {
10250      case 0:    // in this case we set prg_base in addition to vrom_bank!
10251         mmc_prg_base = (data << 5) & 0x40;
10252         mmc_prg_mask = 0x3f;
10253         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
10392      case 0:    // in this case we set prg_base in addition to state->vrom_bank!
10393         state->mmc_prg_base = (data << 5) & 0x40;
10394         state->mmc_prg_mask = 0x3f;
10395         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
1025410396      case 1: case 2: case 3: case 4: case 5:
10255         vrom_bank[cmd] = data;
10397         state->vrom_bank[cmd] = data;
1025610398         if (state->chr_chunks > 0)
10257            mapper4_set_chr(space->machine, CHRROM, mmc_chr_base, mmc_chr_mask);
10258         else   // according to Disch's docs, mmc_cmd1&0x80 swaps 4k CHRRAM banks
10399            mapper4_set_chr(space->machine, CHRROM, state->mmc_chr_base, state->mmc_chr_mask);
10400         else   // according to Disch's docs, state->mmc_cmd1&0x80 swaps 4k CHRRAM banks
1025910401         {
10260            map245_helper = /*(mmc_cmd1 & 0x80) ? 1 : */0;
10402            map245_helper = /*(state->mmc_cmd1 & 0x80) ? 1 : */0;
1026110403            chr4_0(space->machine, 0 ^ map245_helper, CHRRAM);
1026210404            chr4_4(space->machine, 1 ^ map245_helper, CHRRAM);
1026310405         }
1026410406         break;
1026510407      case 6:
1026610408      case 7:
10267         prg_bank[cmd - 6] = data;
10268         mapper4_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
10409         state->prg_bank[cmd - 6] = data;
10410         mapper4_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
1026910411         break;
1027010412      }
1027110413      break;
r7955r7956
1037210514
1037310515static void mapper249_set_prg( running_machine *machine, int prg_base, int prg_mask )
1037410516{
10517   nes_state *state = (nes_state *)machine->driver_data;
1037510518   int i;
1037610519
1037710520   if (map249_reg)
1037810521   {
1037910522      for (i = 0; i < 4; i ++)   // should this apply only to bank[0],bank[1]?
1038010523      {
10381         prg_bank[i] = ((prg_bank[i] & 0x01)) | ((prg_bank[i] >> 3) & 0x02) |
10382               ((prg_bank[i] >> 1) & 0x04) | ((prg_bank[i] << 2) & 0x18);
10524         state->prg_bank[i] = ((state->prg_bank[i] & 0x01)) | ((state->prg_bank[i] >> 3) & 0x02) |
10525               ((state->prg_bank[i] >> 1) & 0x04) | ((state->prg_bank[i] << 2) & 0x18);
1038310526      }
1038410527   }
1038510528
r7955r7956
1038810531
1038910532static void mapper249_set_chr( running_machine *machine, int chr_base, int chr_mask )
1039010533{
10391   UINT8 chr_page = (mmc_cmd1 & 0x80) >> 5;
10534   nes_state *state = (nes_state *)machine->driver_data;
10535   UINT8 chr_page = (state->mmc_cmd1 & 0x80) >> 5;
1039210536   int i;
1039310537
1039410538   if (map249_reg)
1039510539   {
1039610540      for (i = 0; i < 6; i ++)
1039710541      {
10398         vrom_bank[i] = ((vrom_bank[i] & 0x03)) | ((vrom_bank[i] >> 1) & 0x04) |
10399                  ((vrom_bank[i] >> 4) & 0x08) | ((vrom_bank[i] >> 2) & 0x10) |
10400                  ((vrom_bank[i] << 3) & 0x20) | ((vrom_bank[i] << 2) & 0xc0);
10542         state->vrom_bank[i] = ((state->vrom_bank[i] & 0x03)) | ((state->vrom_bank[i] >> 1) & 0x04) |
10543                  ((state->vrom_bank[i] >> 4) & 0x08) | ((state->vrom_bank[i] >> 2) & 0x10) |
10544                  ((state->vrom_bank[i] << 3) & 0x20) | ((state->vrom_bank[i] << 2) & 0xc0);
1040110545      }
1040210546   }
1040310547
10404   chr1_x(machine, chr_page ^ 0, chr_base | ((vrom_bank[0] & ~0x01) & chr_mask), mmc_chr_source);
10405   chr1_x(machine, chr_page ^ 1, chr_base | ((vrom_bank[0] |  0x01) & chr_mask), mmc_chr_source);
10406   chr1_x(machine, chr_page ^ 2, chr_base | ((vrom_bank[1] & ~0x01) & chr_mask), mmc_chr_source);
10407   chr1_x(machine, chr_page ^ 3, chr_base | ((vrom_bank[1] |  0x01) & chr_mask), mmc_chr_source);
10408   chr1_x(machine, chr_page ^ 4, chr_base | (vrom_bank[2] & chr_mask), mmc_chr_source);
10409   chr1_x(machine, chr_page ^ 5, chr_base | (vrom_bank[3] & chr_mask), mmc_chr_source);
10410   chr1_x(machine, chr_page ^ 6, chr_base | (vrom_bank[4] & chr_mask), mmc_chr_source);
10411   chr1_x(machine, chr_page ^ 7, chr_base | (vrom_bank[5] & chr_mask), mmc_chr_source);
10548   chr1_x(machine, chr_page ^ 0, chr_base | ((state->vrom_bank[0] & ~0x01) & chr_mask), state->mmc_chr_source);
10549   chr1_x(machine, chr_page ^ 1, chr_base | ((state->vrom_bank[0] |  0x01) & chr_mask), state->mmc_chr_source);
10550   chr1_x(machine, chr_page ^ 2, chr_base | ((state->vrom_bank[1] & ~0x01) & chr_mask), state->mmc_chr_source);
10551   chr1_x(machine, chr_page ^ 3, chr_base | ((state->vrom_bank[1] |  0x01) & chr_mask), state->mmc_chr_source);
10552   chr1_x(machine, chr_page ^ 4, chr_base | (state->vrom_bank[2] & chr_mask), state->mmc_chr_source);
10553   chr1_x(machine, chr_page ^ 5, chr_base | (state->vrom_bank[3] & chr_mask), state->mmc_chr_source);
10554   chr1_x(machine, chr_page ^ 6, chr_base | (state->vrom_bank[4] & chr_mask), state->mmc_chr_source);
10555   chr1_x(machine, chr_page ^ 7, chr_base | (state->vrom_bank[5] & chr_mask), state->mmc_chr_source);
1041210556}
1041310557
1041410558static WRITE8_HANDLER( mapper249_l_w )
1041510559{
10560   nes_state *state = (nes_state *)space->machine->driver_data;
1041610561   LOG_MMC(("mapper249_l_w, offset: %04x, data: %02x\n", offset, data));
1041710562
1041810563   offset += 0x100;
r7955r7956
1042010565   if (offset == 0x1000)
1042110566   {
1042210567      map249_reg = data & 0x02;
10423      mapper249_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
10424      mapper249_set_chr(space->machine, mmc_prg_base, mmc_prg_mask);
10568      mapper249_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
10569      mapper249_set_chr(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
1042510570   }
1042610571}
1042710572
1042810573static WRITE8_HANDLER( mapper249_w )
1042910574{
10575   nes_state *state = (nes_state *)space->machine->driver_data;
1043010576   UINT8 MMC3_helper, cmd;
1043110577   LOG_MMC(("mapper249_w, offset: %04x, data: %02x\n", offset, data));
1043210578
1043310579   switch (offset & 0x6001)
1043410580   {
1043510581   case 0x0000:
10436      MMC3_helper = mmc_cmd1 ^ data;
10437      mmc_cmd1 = data;
10582      MMC3_helper = state->mmc_cmd1 ^ data;
10583      state->mmc_cmd1 = data;
1043810584
1043910585      /* Has PRG Mode changed? */
1044010586      if (MMC3_helper & 0x40)
10441         mapper249_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
10587         mapper249_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
1044210588
1044310589      /* Has CHR Mode changed? */
1044410590      if (MMC3_helper & 0x80)
10445         mapper249_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
10591         mapper249_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
1044610592      break;
1044710593
1044810594   case 0x0001:
10449      cmd = mmc_cmd1 & 0x07;
10595      cmd = state->mmc_cmd1 & 0x07;
1045010596      switch (cmd)
1045110597      {
1045210598      case 0: case 1:   // these do not need to be separated: we take care of them in set_chr!
1045310599      case 2: case 3: case 4: case 5:
10454         vrom_bank[cmd] = data;
10455         mapper249_set_chr(space->machine, mmc_chr_base, mmc_chr_mask);
10600         state->vrom_bank[cmd] = data;
10601         mapper249_set_chr(space->machine, state->mmc_chr_base, state->mmc_chr_mask);
1045610602         break;
1045710603      case 6:
1045810604      case 7:
10459         prg_bank[cmd - 6] = data;
10460         mapper249_set_prg(space->machine, mmc_prg_base, mmc_prg_mask);
10605         state->prg_bank[cmd - 6] = data;
10606         mapper249_set_prg(space->machine, state->mmc_prg_base, state->mmc_prg_mask);
1046110607         break;
1046210608      }
1046310609      break;
r7955r7956
1051510661
1051610662static WRITE8_HANDLER( mapper252_w )
1051710663{
10664   nes_state *state = (nes_state *)space->machine->driver_data;
1051810665   UINT8 mmc_helper, bank;
1051910666   LOG_MMC(("mapper252_w, offset: %04x, data: %02x\n", offset, data));
1052010667
r7955r7956
1053310680      bank = ((offset & 0x7000) - 0x3000) / 0x0800 + ((offset & 0x0008) >> 3);
1053410681      mmc_helper = offset & 0x04;
1053510682      if (mmc_helper)
10536         vrom_bank[bank] = (vrom_bank[bank] & 0x0f) | ((data & 0x0f) << 4);
10683         state->vrom_bank[bank] = (state->vrom_bank[bank] & 0x0f) | ((data & 0x0f) << 4);
1053710684      else
10538         vrom_bank[bank] = (vrom_bank[bank] & 0xf0) | (data & 0x0f);
10539      chr1_x(space->machine, bank, vrom_bank[bank], CHRROM);
10685         state->vrom_bank[bank] = (state->vrom_bank[bank] & 0xf0) | (data & 0x0f);
10686      chr1_x(space->machine, bank, state->vrom_bank[bank], CHRROM);
1054010687      break;
1054110688   case 0x7000:
1054210689      switch (offset & 0x0c)
1054310690      {
1054410691      case 0x00:
10545         IRQ_count_latch = (IRQ_count_latch & 0xf0) | (data & 0x0f);
10692         state->IRQ_count_latch = (state->IRQ_count_latch & 0xf0) | (data & 0x0f);
1054610693         break;
1054710694      case 0x04:
10548         IRQ_count_latch = (IRQ_count_latch & 0x0f) | ((data & 0x0f) << 4);
10695         state->IRQ_count_latch = (state->IRQ_count_latch & 0x0f) | ((data & 0x0f) << 4);
1054910696         break;
1055010697      case 0x08:
10551         IRQ_enable = data & 0x02;
10552         IRQ_enable_latch = data & 0x01;
10698         state->IRQ_enable = data & 0x02;
10699         state->IRQ_enable_latch = data & 0x01;
1055310700         if (data & 0x02)
10554            IRQ_count = IRQ_count_latch;
10701            state->IRQ_count = state->IRQ_count_latch;
1055510702         break;
1055610703      case 0x0c:
10557         IRQ_enable = IRQ_enable_latch;
10704         state->IRQ_enable = state->IRQ_enable_latch;
1055810705         break;
1055910706      }
1056010707      break;

Previous 15994 Revisions Next


© 1998-2018 The MESS Team