mapvrc.c 9.99 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/*
** Nofrendo (c) 1998-2000 Matthew Conte (matt@conte.com)
**
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of version 2 of the GNU Library General 
** Public License as published by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful, 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
** Library General Public License for more details.  To obtain a 
** copy of the GNU Library General Public License, write to the Free 
** Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** Any permitted reproduction of these routines, in whole or in part,
** must bear this legend.
**
**
** map_vrc.c
**
** VRC mapper interface
** $Id: mapvrc.c,v 1.2 2001/04/27 14:37:11 neil Exp $
*/

#include <noftypes.h>
#include <nes_mmc.h>
#include <nes.h>
#include <log.h>

#define VRC_VBANK(bank, value, high) \
{ \
   if ((high)) \
      highnybbles[(bank)] = (value) & 0x0F; \
   else \
      lownybbles[(bank)] = (value) & 0x0F; \
   mmc_bankvrom(1, (bank) << 10, (highnybbles[(bank)] << 4)+lownybbles[(bank)]); \
}

static struct
{
   int counter, enabled;
   int latch, wait_state;
} irq;

static int select_c000 = 0;
static uint8 lownybbles[8];
static uint8 highnybbles[8];

static void vrc_init(void)
{
   irq.counter = irq.enabled = 0;
   irq.latch = irq.wait_state = 0;
}

static void map21_write(uint32 address, uint8 value)
{
   switch (address)
   {
   case 0x8000:
      if (select_c000) 
         mmc_bankrom(8, 0xC000,value);
      else
         mmc_bankrom(8, 0x8000,value);
      break;

   case 0x9000:
      switch (value & 3)
      {
      case 0:
         ppu_mirror(0, 1, 0, 1); /* vertical */
         break;

      case 1: 
         ppu_mirror(0, 0, 1, 1); /* horizontal */
         break;

      case 2: 
         ppu_mirror(0, 0, 0, 0); 
         break;

      case 3: 
         ppu_mirror(1, 1, 1, 1); 
         break;

      default: 
         break;
      }
      break;
   case 0x9002: select_c000=(value&0x02)>>1; break;
   case 0xA000: mmc_bankrom(8, 0xA000,value); break;

   case 0xB000: VRC_VBANK(0,value,0); break;
   case 0xB002:
   case 0xB040: VRC_VBANK(0,value,1); break;
   case 0xB001:
   case 0xB004:
   case 0xB080: VRC_VBANK(1,value,0); break;
   case 0xB003:
   case 0xB006:
   case 0xB0C0: VRC_VBANK(1,value,1); break;
   case 0xC000: VRC_VBANK(2,value,0); break;
   case 0xC002:
   case 0xC040: VRC_VBANK(2,value,1); break;
   case 0xC001:
   case 0xC004:
   case 0xC080: VRC_VBANK(3,value,0); break;
   case 0xC003:
   case 0xC006:
   case 0xC0C0: VRC_VBANK(3,value,1); break;
   case 0xD000: VRC_VBANK(4,value,0); break;
   case 0xD002:
   case 0xD040: VRC_VBANK(4,value,1); break;
   case 0xD001:
   case 0xD004:
   case 0xD080: VRC_VBANK(5,value,0); break;
   case 0xD003:
   case 0xD006:
   case 0xD0C0: VRC_VBANK(5,value,1); break;
   case 0xE000: VRC_VBANK(6,value,0); break;
   case 0xE002:
   case 0xE040: VRC_VBANK(6,value,1); break;
   case 0xE001:
   case 0xE004:
   case 0xE080: VRC_VBANK(7,value,0); break;
   case 0xE003:
   case 0xE006:
   case 0xE0C0: VRC_VBANK(7,value,1); break;

   case 0xF000:
      irq.latch &= 0xF0;
      irq.latch |= (value & 0x0F);
      break;
   case 0xF002:
   case 0xF040:
      irq.latch &= 0x0F;
      irq.latch |= ((value & 0x0F) << 4);
      break;
   case 0xF004:
   case 0xF001:
   case 0xF080:
      irq.enabled = (value >> 1) & 0x01;
      irq.wait_state = value & 0x01;
      irq.counter = irq.latch;
      break;
   case 0xF006:
   case 0xF003:
   case 0xF0C0:
      irq.enabled = irq.wait_state;
      break;

   default:
#ifdef NOFRENDO_DEBUG
      log_printf("wrote $%02X to $%04X", value, address);
#endif
      break;
   }
}

static void map22_write(uint32 address, uint8 value)
{
   int reg = address >> 12;
   
   switch (reg)
   {
   case 0x8:
      mmc_bankrom(8, 0x8000, value);
      break;
   
   case 0xA:
      mmc_bankrom(8, 0xA000, value);
      break;

   case 0x9:
      switch (value & 3)
      {
      case 0:
         ppu_mirror(0, 1, 0, 1); /* vertical */
         break;

      case 1: 
         ppu_mirror(0, 0, 1, 1); /* horizontal */
         break;

      case 2:
         ppu_mirror(1, 1, 1, 1);
         break;

      case 3:
         ppu_mirror(0, 0, 0, 0);
         break;
      }
      break;

   case 0xB:
   case 0xC:
   case 0xD:
   case 0xE:
      {
         int loc = (((reg - 0xB) << 1) + (address & 1)) << 10;
         mmc_bankvrom(1, loc, value >> 1);
      }
      break;

   default:
      break;
   }
}

static void map23_write(uint32 address, uint8 value)
{
   switch (address)
   {
   case 0x8000:
   case 0x8FFF:
      mmc_bankrom(8, 0x8000, value);
      break;

   case 0xA000:
   case 0xAFFF:
      mmc_bankrom(8, 0xA000, value);
      break;
   
   case 0x9000:
   case 0x9004:
   case 0x9008:
      switch(value & 3)
      {
      case 0:
         ppu_mirror(0, 1, 0, 1); /* vertical */
         break;

      case 1: 
         ppu_mirror(0, 0, 1, 1); /* horizontal */
         break;

      case 2:
         ppu_mirror(0, 0, 0, 0);
         break;

      case 3:
         ppu_mirror(1, 1, 1, 1);
         break;
      }
      break;

   case 0xB000: VRC_VBANK(0,value,0); break;
   case 0xB001:
   case 0xB004: VRC_VBANK(0,value,1); break;
   case 0xB002:
   case 0xB008: VRC_VBANK(1,value,0); break;
   case 0xB003:
   case 0xB00C: VRC_VBANK(1,value,1); break;
   case 0xC000: VRC_VBANK(2,value,0); break;
   case 0xC001:
   case 0xC004: VRC_VBANK(2,value,1); break;
   case 0xC002:
   case 0xC008: VRC_VBANK(3,value,0); break;
   case 0xC003:
   case 0xC00C: VRC_VBANK(3,value,1); break;
   case 0xD000: VRC_VBANK(4,value,0); break;
   case 0xD001:
   case 0xD004: VRC_VBANK(4,value,1); break;
   case 0xD002:
   case 0xD008: VRC_VBANK(5,value,0); break;
   case 0xD003:
   case 0xD00C: VRC_VBANK(5,value,1); break;
   case 0xE000: VRC_VBANK(6,value,0); break;
   case 0xE001:
   case 0xE004: VRC_VBANK(6,value,1); break;
   case 0xE002:
   case 0xE008: VRC_VBANK(7,value,0); break;
   case 0xE003:
   case 0xE00C: VRC_VBANK(7,value,1); break;

   case 0xF000: 
      irq.latch &= 0xF0;
      irq.latch |= (value & 0x0F);
      break;

   case 0xF004: 
      irq.latch &= 0x0F;
      irq.latch |= ((value & 0x0F) << 4);
      break;

   case 0xF008:
      irq.enabled = (value >> 1) & 0x01;
      irq.wait_state = value & 0x01;
      irq.counter = irq.latch;
      break;

   case 0xF00C:
      irq.enabled = irq.wait_state;
      break;

   default:
#ifdef NOFRENDO_DEBUG
      log_printf("wrote $%02X to $%04X",value,address);
#endif
      break;
   }
}

static void vrc_hblank(int vblank) 
{
   UNUSED(vblank);

   if (irq.enabled)
   {
      if (256 == ++irq.counter)
      {
         irq.counter = irq.latch;
         nes_irq();
         //irq.enabled = false;
         irq.enabled = irq.wait_state;
      }
   }
}



static map_memwrite map21_memwrite[] =
{
   { 0x8000, 0xFFFF, map21_write },
   {     -1,     -1, NULL }
};

static map_memwrite map22_memwrite[] =
{
   { 0x8000, 0xFFFF, map22_write },
   {     -1,     -1, NULL }
};

static map_memwrite map23_memwrite[] =
{
   { 0x8000, 0xFFFF, map23_write },
   {     -1,     -1, NULL }
};

static void map21_getstate(SnssMapperBlock *state)
{
   state->extraData.mapper21.irqCounter = irq.counter;
   state->extraData.mapper21.irqCounterEnabled = irq.enabled;
}

static void map21_setstate(SnssMapperBlock *state)
{
   irq.counter = state->extraData.mapper21.irqCounter;
   irq.enabled = state->extraData.mapper21.irqCounterEnabled;
}

mapintf_t map21_intf =
{
   21, /* mapper number */
   "Konami VRC4 A", /* mapper name */
   vrc_init, /* init routine */
   NULL, /* vblank callback */
   vrc_hblank, /* hblank callback */
   map21_getstate, /* get state (snss) */
   map21_setstate, /* set state (snss) */
   NULL, /* memory read structure */
   map21_memwrite, /* memory write structure */
   NULL /* external sound device */
};

mapintf_t map22_intf =
{
   22, /* mapper number */
   "Konami VRC2 A", /* mapper name */
   vrc_init, /* init routine */
   NULL, /* vblank callback */
   NULL, /* hblank callback */
   NULL, /* get state (snss) */
   NULL, /* set state (snss) */
   NULL, /* memory read structure */
   map22_memwrite, /* memory write structure */
   NULL /* external sound device */
};

mapintf_t map23_intf =
{
   23, /* mapper number */
   "Konami VRC2 B", /* mapper name */
   vrc_init, /* init routine */
   NULL, /* vblank callback */
   vrc_hblank, /* hblank callback */
   NULL, /* get state (snss) */
   NULL, /* set state (snss) */
   NULL, /* memory read structure */
   map23_memwrite, /* memory write structure */
   NULL /* external sound device */
};

mapintf_t map25_intf =
{
   25, /* mapper number */
   "Konami VRC4 B", /* mapper name */
   NULL, /* init routine */
   NULL, /* vblank callback */
   vrc_hblank, /* hblank callback */
   NULL, /* get state (snss) */
   NULL, /* set state (snss) */
   NULL, /* memory read structure */
   map21_memwrite, /* memory write structure */
   NULL /* external sound device */
};

/*
** $Log: mapvrc.c,v $
** Revision 1.2  2001/04/27 14:37:11  neil
** wheeee
**
** Revision 1.1  2001/04/27 12:54:40  neil
** blah
**
** Revision 1.1.1.1  2001/04/27 07:03:54  neil
** initial
**
** Revision 1.1  2000/10/24 12:19:33  matt
** changed directory structure
**
** Revision 1.10  2000/10/22 19:17:46  matt
** mapper cleanups galore
**
** Revision 1.9  2000/10/22 15:03:14  matt
** simplified mirroring
**
** Revision 1.8  2000/10/21 19:33:38  matt
** many more cleanups
**
** Revision 1.7  2000/10/10 13:58:17  matt
** stroustrup squeezing his way in the door
**
** Revision 1.6  2000/08/16 02:50:11  matt
** random mapper cleanups
**
** Revision 1.5  2000/07/15 23:52:20  matt
** rounded out a bunch more mapper interfaces
**
** Revision 1.4  2000/07/10 13:51:25  matt
** using generic nes_irq() routine now
**
** Revision 1.3  2000/07/10 05:29:03  matt
** cleaned up some mirroring issues
**
** Revision 1.2  2000/07/06 02:48:43  matt
** clearly labelled structure members
**
** Revision 1.1  2000/07/06 01:01:56  matt
** initial revision
**
*/