map040.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  ** map40.c
 21  **
 22  ** mapper 40 interface
 23  ** $Id: map040.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.h"
 29  #include "libsnss.h"
 30  #include "log.h"
 31  
 32  #define  MAP40_IRQ_PERIOD  (4096 / 113.666666)
 33  
 34  static struct
 35  {
 36     int enabled, counter;
 37  } irq;
 38  
 39  /* mapper 40: SMB 2j (hack) */
 40  static void map40_init(void)
 41  {
 42     mmc_bankrom(8, 0x6000, 6);
 43     mmc_bankrom(8, 0x8000, 4);
 44     mmc_bankrom(8, 0xA000, 5);
 45     mmc_bankrom(8, 0xE000, 7);
 46  
 47     irq.enabled = false;
 48     irq.counter = (int) MAP40_IRQ_PERIOD;
 49  }
 50  
 51  static void map40_hblank(int vblank)
 52  {
 53     UNUSED(vblank);
 54  
 55     if (irq.enabled && irq.counter)
 56     {
 57        irq.counter--;
 58        if (0 == irq.counter)
 59        {
 60           nes_irq();
 61           irq.enabled = false;
 62        }
 63     }
 64  }
 65  
 66  static void map40_write(uint32 address, uint8 value)
 67  {
 68     int range = (address >> 13) - 4;
 69  
 70     switch (range)
 71     {
 72     case 0: /* 0x8000-0x9FFF */
 73        irq.enabled = false;
 74        irq.counter = (int) MAP40_IRQ_PERIOD;
 75        break;
 76  
 77     case 1: /* 0xA000-0xBFFF */
 78        irq.enabled = true;
 79        break;
 80  
 81     case 3: /* 0xE000-0xFFFF */
 82        mmc_bankrom(8, 0xC000, value & 7);
 83        break;
 84  
 85     default:
 86        break;
 87     }
 88  }
 89  
 90  static void map40_getstate(SnssMapperBlock *state)
 91  {
 92     state->extraData.mapper40.irqCounter = irq.counter;
 93     state->extraData.mapper40.irqCounterEnabled = irq.enabled;
 94  }
 95  
 96  static void map40_setstate(SnssMapperBlock *state)
 97  {
 98     irq.counter = state->extraData.mapper40.irqCounter;
 99     irq.enabled = state->extraData.mapper40.irqCounterEnabled;
100  }
101  
102  static const map_memwrite map40_memwrite[] =
103  {
104     { 0x8000, 0xFFFF, map40_write },
105     {     -1,     -1, NULL }
106  };
107  
108  const mapintf_t map40_intf =
109  {
110     40, /* mapper number */
111     "SMB 2j (pirate)", /* mapper name */
112     map40_init, /* init routine */
113     NULL, /* vblank callback */
114     map40_hblank, /* hblank callback */
115     map40_getstate, /* get state (snss) */
116     map40_setstate, /* set state (snss) */
117     NULL, /* memory read structure */
118     map40_memwrite, /* memory write structure */
119     NULL /* external sound device */
120  };
121  
122  /*
123  ** $Log: map040.c,v $
124  ** Revision 1.2  2001/04/27 14:37:11  neil
125  ** wheeee
126  **
127  ** Revision 1.1  2001/04/27 12:54:40  neil
128  ** blah
129  **
130  ** Revision 1.1.1.1  2001/04/27 07:03:54  neil
131  ** initial
132  **
133  ** Revision 1.1  2000/10/24 12:19:33  matt
134  ** changed directory structure
135  **
136  ** Revision 1.9  2000/10/23 15:53:27  matt
137  ** suppressed warnings
138  **
139  ** Revision 1.8  2000/10/22 19:17:46  matt
140  ** mapper cleanups galore
141  **
142  ** Revision 1.7  2000/10/21 19:33:38  matt
143  ** many more cleanups
144  **
145  ** Revision 1.6  2000/10/10 13:58:17  matt
146  ** stroustrup squeezing his way in the door
147  **
148  ** Revision 1.5  2000/08/16 02:50:11  matt
149  ** random mapper cleanups
150  **
151  ** Revision 1.4  2000/07/15 23:52:19  matt
152  ** rounded out a bunch more mapper interfaces
153  **
154  ** Revision 1.3  2000/07/10 13:51:25  matt
155  ** using generic nes_irq() routine now
156  **
157  ** Revision 1.2  2000/07/06 02:48:43  matt
158  ** clearly labelled structure members
159  **
160  ** Revision 1.1  2000/07/05 05:05:18  matt
161  ** initial revision
162  **
163  */