libsidplayfp  1.5.3
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 <stdint.h>
27 
28 #include "sidplayfp/event.h"
29 #include "sidplayfp/sidendian.h"
30 #include "sidplayfp/sidmemory.h"
31 
32 #include "Banks/Bank.h"
33 #include "Banks/SystemRAMBank.h"
34 #include "Banks/SystemROMBanks.h"
35 #include "Banks/ZeroRAMBank.h"
36 
37 #include <string.h>
38 
42 class MMU : public PLA, public sidmemory
43 {
44 private:
45  EventContext &context;
46 
48  bool loram, hiram, charen;
49 
51  Bank* cpuReadMap[16];
52 
54  Bank* cpuWriteMap[16];
55 
57  Bank* ioBank;
58 
60  KernalRomBank kernalRomBank;
61 
63  BasicRomBank basicRomBank;
64 
66  CharacterRomBank characterRomBank;
67 
69  SystemRAMBank ramBank;
70 
72  ZeroRAMBank zeroRAMBank;
73 
74 private:
75  void setCpuPort(int state);
76  void updateMappingPHI2();
77  uint8_t getLastReadByte() const { return 0; }
78  event_clock_t getPhi2Time() const { return context.getTime(EVENT_CLOCK_PHI2); }
79 
80 public:
81  MMU(EventContext *context, Bank* ioBank);
82  ~MMU () {}
83 
84  void reset();
85 
86  void setRoms(const uint8_t* kernal, const uint8_t* basic, const uint8_t* character)
87  {
88  kernalRomBank.set(kernal);
89  basicRomBank.set(basic);
90  characterRomBank.set(character);
91  }
92 
93  // RAM access methods
94  uint8_t readMemByte(uint_least16_t addr) { return ramBank.peek(addr); }
95  uint_least16_t readMemWord(uint_least16_t addr) { return endian_little16(ramBank.ram+addr); }
96 
97  void writeMemByte(uint_least16_t addr, uint8_t value) { ramBank.poke(addr, value); }
98  void writeMemWord(uint_least16_t addr, uint_least16_t value) { endian_little16(ramBank.ram+addr, value); }
99 
100  void fillRam(uint_least16_t start, uint8_t value, unsigned int size)
101  {
102  memset(ramBank.ram+start, value, size);
103  }
104  void fillRam(uint_least16_t start, const uint8_t* source, unsigned int size)
105  {
106  memcpy(ramBank.ram+start, source, size);
107  }
108 
109  // SID specific hacks
110  void installResetHook(uint_least16_t addr) { kernalRomBank.installResetHook(addr); }
111 
112  void installBasicTrap(uint_least16_t addr) { basicRomBank.installTrap(addr); }
113 
114  void setBasicSubtune(uint8_t tune) { basicRomBank.setSubtune(tune); }
115 
122  uint8_t cpuRead(uint_least16_t addr) const { return cpuReadMap[addr >> 12]->peek(addr); }
123 
130  void cpuWrite(uint_least16_t addr, uint8_t data) { cpuWriteMap[addr >> 12]->poke(addr, data); }
131 };
132 
133 #endif