libsidplayfp  1.0.1
mmu.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright 2000 Simon White
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef MMU_H
24 #define MMU_H
25 
26 #include "sidplayfp/sidendian.h"
27 #include "sidplayfp/siddefs.h"
28 #include "sidplayfp/sidmemory.h"
29 
30 #include "Banks/Bank.h"
31 #include "Banks/IOBank.h"
32 #include "Banks/SystemRAMBank.h"
33 #include "Banks/SystemROMBanks.h"
34 #include "Banks/ZeroRAMBank.h"
35 
36 #include <string.h>
37 
41 class MMU : public PLA, public sidmemory
42 {
43 private:
44  EventContext &context;
45 
47  bool loram, hiram, charen;
48 
50  Bank* cpuReadMap[16];
51 
53  Bank* cpuWriteMap[16];
54 
56  Bank* ioBank;
57 
59  KernalRomBank kernalRomBank;
60 
62  BasicRomBank basicRomBank;
63 
65  CharacterRomBank characterRomBank;
66 
68  SystemRAMBank ramBank;
69 
70  ZeroRAMBank zeroRAMBank;
71 
72 private:
73  void setCpuPort(int state);
74  void updateMappingPHI2();
75  uint8_t getLastReadByte() const { return 0; }
76  event_clock_t getPhi2Time() const { return context.getTime(EVENT_CLOCK_PHI2); }
77 
78 public:
79  MMU(EventContext *context, Bank* ioBank);
80  ~MMU () {}
81 
82  void reset();
83 
84  void setRoms(const uint8_t* kernal, const uint8_t* basic, const uint8_t* character)
85  {
86  kernalRomBank.set(kernal);
87  basicRomBank.set(basic);
88  characterRomBank.set(character);
89  }
90 
91  // RAM access methods
92  uint8_t readMemByte(uint_least16_t addr) { return ramBank.peek(addr); }
93  uint_least16_t readMemWord(uint_least16_t addr) { return endian_little16(ramBank.array()+addr); }
94 
95  void writeMemByte(uint_least16_t addr, uint8_t value) { ramBank.poke(addr, value); }
96  void writeMemWord(uint_least16_t addr, uint_least16_t value) { endian_little16(ramBank.array()+addr, value); }
97 
98  void fillRam(uint_least16_t start, uint8_t value, unsigned int size)
99  {
100  memset(ramBank.array()+start, value, size);
101  }
102  void fillRam(uint_least16_t start, const uint8_t* source, unsigned int size)
103  {
104  memcpy(ramBank.array()+start, source, size);
105  }
106 
107  // SID specific hacks
108  void installResetHook(uint_least16_t addr) { kernalRomBank.installResetHook(addr); }
109 
110  void installBasicTrap(uint_least16_t addr) { basicRomBank.installTrap(addr); }
111 
112  void setBasicSubtune(uint8_t tune) { basicRomBank.setSubtune(tune); }
113 
120  uint8_t cpuRead(uint_least16_t addr) const { return cpuReadMap[addr >> 12]->peek(addr); }
121 
128  void cpuWrite(uint_least16_t addr, uint8_t data) { cpuWriteMap[addr >> 12]->poke(addr, data); }
129 };
130 
131 #endif