map160.c
  1  /*
  2  ** Nofrendo (c) 1998-2000 Matthew Conte (matt@conte.com)
  3  **
  4  **
  5  ** This program is free software; you can redistribute it and/or
  6  ** modify it under the terms of version 2 of the GNU Library General 
  7  ** Public License as published by the Free Software Foundation.
  8  **
  9  ** This program is distributed in the hope that it will be useful, 
 10  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 12  ** Library General Public License for more details.  To obtain a 
 13  ** copy of the GNU Library General Public License, write to the Free 
 14  ** Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 15  **
 16  ** Any permitted reproduction of these routines, in whole or in part,
 17  ** must bear this legend.
 18  **
 19  **
 20  ** map160.c
 21  **
 22  ** mapper 160 interface
 23  ** $Id: map160.c,v 1.2 2001/04/27 14:37:11 neil Exp $
 24  */
 25  
 26  #include "noftypes.h"
 27  #include "nes_mmc.h"
 28  #include "nes_ppu.h"
 29  #include "nes.h"
 30  
 31  static struct
 32  {
 33     bool enabled, expired;
 34     int counter;
 35     int latch_c005, latch_c003;
 36  } irq;
 37  
 38  static void map160_write(uint32 address, uint8 value)
 39  {
 40     if (address >= 0x8000 && address <= 0x8003)
 41     {
 42        mmc_bankrom(8, 0x8000 + 0x2000 * (address & 3), value);
 43     }
 44     else if (address >= 0x9000 && address <= 0x9007)
 45     {
 46        mmc_bankvrom(1, 0x400 * (address & 7), value);
 47     }
 48     else if (0xC002 == address)
 49     {
 50        irq.enabled = false;
 51        irq.latch_c005 = irq.latch_c003;
 52     }
 53     else if (0xC003 == address)
 54     {
 55        if (false == irq.expired)
 56        {
 57           irq.counter = value;
 58        }
 59        else
 60        {
 61           irq.expired = false;
 62           irq.enabled = true;
 63           irq.counter = irq.latch_c005;
 64        }
 65     }
 66     else if (0xC005 == address)
 67     {
 68        irq.latch_c005 = value;
 69        irq.counter = value;
 70     }
 71  #ifdef NOFRENDO_DEBUG
 72     else
 73     {
 74        log_printf("mapper 160: untrapped write $%02X to $%04X\n", value, address);
 75     }
 76  #endif /* NOFRENDO_DEBUG */
 77  }
 78  
 79  static void map160_hblank(int vblank)
 80  {
 81     if (!vblank)
 82     {
 83        if (ppu_enabled() && irq.enabled)
 84        {
 85           if (0 == irq.counter && false == irq.expired)
 86           {
 87              irq.expired = true;
 88              nes_irq();
 89           }
 90           else
 91           {
 92              irq.counter--;
 93           }
 94        }
 95     }
 96  }
 97  
 98  static void map160_init(void)
 99  {
100     irq.enabled = false;
101     irq.expired = false;
102     irq.counter = 0;
103     irq.latch_c003 = irq.latch_c005 = 0;
104  }
105  
106  static const map_memwrite map160_memwrite[] =
107  {
108     { 0x8000, 0xFFFF, map160_write },
109     {     -1,     -1, NULL }
110  };
111  
112  const mapintf_t map160_intf =
113  {
114     160, /* mapper number */
115     "Aladdin (pirate)", /* mapper name */
116     map160_init, /* init routine */
117     NULL, /* vblank callback */
118     map160_hblank, /* hblank callback */
119     NULL, /* get state (snss) */
120     NULL, /* set state (snss) */
121     NULL, /* memory read structure */
122     map160_memwrite, /* memory write structure */
123     NULL /* external sound device */
124  };
125  
126  /*
127  ** $Log: map160.c,v $
128  ** Revision 1.2  2001/04/27 14:37:11  neil
129  ** wheeee
130  **
131  ** Revision 1.1  2001/04/27 12:54:40  neil
132  ** blah
133  **
134  ** Revision 1.1  2001/04/27 10:57:41  neil
135  ** wheee
136  **
137  ** Revision 1.1  2000/12/27 04:24:46  matt
138  ** initial revision
139  **
140  */