1 /***************************************************************************
2  *   Copyright (C) 2007-2010 by Sindre Aamås                               *
3  *   aamas@stud.ntnu.no                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License version 2 as     *
7  *   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         *
12  *   GNU General Public License version 2 for more details.                *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   version 2 along with this program; if not, write to the               *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 #ifndef MEMPTRS_H
20 #define MEMPTRS_H
21 
22 namespace gambatte
23 {
24 
25    enum OamDmaSrc { oam_dma_src_rom,
26                  oam_dma_src_sram,
27                  oam_dma_src_vram,
28                  oam_dma_src_wram,
29                  oam_dma_src_invalid,
30                  oam_dma_src_off, };
31 
32    class MemPtrs
33    {
34       public:
35          enum RamFlag
36          {
37             READ_EN  = 1,
38             WRITE_EN = 2,
39             RTC_EN   = 4
40          };
41 
42          MemPtrs();
43          ~MemPtrs();
44          void reset(unsigned rombanks, unsigned rambanks, unsigned wrambanks);
45 
rmem(unsigned area)46          const unsigned char * rmem(unsigned area) const
47          {
48             return rmem_[area];
49          }
50 
wmem(unsigned area)51          unsigned char * wmem(unsigned area) const
52          {
53             return wmem_[area];
54          }
55 
vramdata()56          unsigned char * vramdata() const
57          {
58             return rambankdata_ - 0x4000;
59          }
60 
vramdataend()61          unsigned char * vramdataend() const
62          {
63             return rambankdata_;
64          }
65 
romdata()66          unsigned char * romdata() const
67          {
68             return memchunk_ + 0x4000;
69          }
70 
romdata(unsigned area)71          unsigned char * romdata(unsigned area) const
72          {
73             return romdata_[area];
74          }
75 
romdataend()76          unsigned char * romdataend() const
77          {
78             return rambankdata_ - 0x4000;
79          }
80 
wramdata(unsigned area)81          unsigned char * wramdata(unsigned area) const
82          {
83             return wramdata_[area];
84          }
85 
wramdataend()86          unsigned char * wramdataend() const
87          {
88             return wramdataend_;
89          }
90 
rambankdata()91          unsigned char * rambankdata() const
92          {
93             return rambankdata_;
94          }
95 
rambankdataend()96          unsigned char * rambankdataend() const
97          {
98             return wramdata_[0];
99          }
100 
rdisabledRam()101          const unsigned char * rdisabledRam() const
102          {
103             return rdisabledRamw();
104          }
105 
rsrambankptr()106          const unsigned char * rsrambankptr() const
107          {
108             return rsrambankptr_;
109          }
110 
wsrambankptr()111          unsigned char * wsrambankptr() const
112          {
113             return wsrambankptr_;
114          }
115 
vrambankptr()116          unsigned char * vrambankptr() const
117          {
118             return vrambankptr_;
119          }
120 
oamDmaSrc()121          OamDmaSrc oamDmaSrc() const
122          {
123             return oamDmaSrc_;
124          }
125 
126          void setRombank0(unsigned bank);
127          void setRombank(unsigned bank);
128          void setRambank(unsigned ramFlags, unsigned rambank);
setVrambank(unsigned bank)129          void setVrambank(unsigned bank) { vrambankptr_ = vramdata() + bank * 0x2000ul - 0x8000; }
130          void setWrambank(unsigned bank);
131          void setOamDmaSrc(OamDmaSrc oamDmaSrc);
132 
133       private:
134          unsigned char *romdata_[2];
135          unsigned char *wramdata_[2];
136          const unsigned char *rmem_[0x10];
137          unsigned char *wmem_[0x10];
138          unsigned char *vrambankptr_;
139          unsigned char *rsrambankptr_;
140          unsigned char *wsrambankptr_;
141          unsigned char *memchunk_;
142          unsigned char *rambankdata_;
143          unsigned char *wramdataend_;
144          OamDmaSrc oamDmaSrc_;
145          MemPtrs(const MemPtrs &);
146          MemPtrs & operator=(const MemPtrs &);
147          void disconnectOamDmaAreas();
rdisabledRamw()148          unsigned char * rdisabledRamw() const { return wramdataend_ ; }
wdisabledRam()149          unsigned char * wdisabledRam() const { return wramdataend_ + 0x2000; }
150    };
151 
isCgb(const MemPtrs & memptrs)152    inline bool isCgb(const MemPtrs &memptrs)
153    {
154       return memptrs.wramdataend() - memptrs.wramdata(0) == 0x8000;
155    }
156 
157 }
158 
159 #endif
160