ScummVM API documentation
MemoryRegion.h
1 /* Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Dean Beeler, Jerome Fisher
2  * Copyright (C) 2011-2022 Dean Beeler, Jerome Fisher, Sergey V. Mikayev
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 2.1 of the License, or
7  * (at your option) any later version.
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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef MT32EMU_MEMORY_REGION_H
19 #define MT32EMU_MEMORY_REGION_H
20 
21 #include <cstddef>
22 
23 #include "globals.h"
24 #include "Types.h"
25 #include "Structures.h"
26 
27 namespace MT32Emu {
28 
29 enum MemoryRegionType {
30  MR_PatchTemp, MR_RhythmTemp, MR_TimbreTemp, MR_Patches, MR_Timbres, MR_System, MR_Display, MR_Reset
31 };
32 
33 class Synth;
34 
35 class MemoryRegion {
36 private:
37  Synth *synth;
38  Bit8u *realMemory;
39  Bit8u *maxTable;
40 public:
41  MemoryRegionType type;
42  Bit32u startAddr, entrySize, entries;
43 
44  MemoryRegion(Synth *useSynth, Bit8u *useRealMemory, Bit8u *useMaxTable, MemoryRegionType useType, Bit32u useStartAddr, Bit32u useEntrySize, Bit32u useEntries) {
45  synth = useSynth;
46  realMemory = useRealMemory;
47  maxTable = useMaxTable;
48  type = useType;
49  startAddr = useStartAddr;
50  entrySize = useEntrySize;
51  entries = useEntries;
52  }
53  int lastTouched(Bit32u addr, Bit32u len) const {
54  return (offset(addr) + len - 1) / entrySize;
55  }
56  int firstTouchedOffset(Bit32u addr) const {
57  return offset(addr) % entrySize;
58  }
59  int firstTouched(Bit32u addr) const {
60  return offset(addr) / entrySize;
61  }
62  Bit32u regionEnd() const {
63  return startAddr + entrySize * entries;
64  }
65  bool contains(Bit32u addr) const {
66  return addr >= startAddr && addr < regionEnd();
67  }
68  int offset(Bit32u addr) const {
69  return addr - startAddr;
70  }
71  Bit32u getClampedLen(Bit32u addr, Bit32u len) const {
72  if (addr + len > regionEnd())
73  return regionEnd() - addr;
74  return len;
75  }
76  Bit32u next(Bit32u addr, Bit32u len) const {
77  if (addr + len > regionEnd()) {
78  return regionEnd() - addr;
79  }
80  return 0;
81  }
82  Bit8u getMaxValue(int off) const {
83  if (maxTable == NULL)
84  return 0xFF;
85  return maxTable[off % entrySize];
86  }
87  Bit8u *getRealMemory() const {
88  return realMemory;
89  }
90  bool isReadable() const {
91  return getRealMemory() != NULL;
92  }
93  void read(unsigned int entry, unsigned int off, Bit8u *dst, unsigned int len) const;
94  void write(unsigned int entry, unsigned int off, const Bit8u *src, unsigned int len, bool init = false) const;
95 }; // class MemoryRegion
96 
98 public:
99  PatchTempMemoryRegion(Synth *useSynth, Bit8u *useRealMemory, Bit8u *useMaxTable) : MemoryRegion(useSynth, useRealMemory, useMaxTable, MR_PatchTemp, MT32EMU_MEMADDR(0x030000), sizeof(MemParams::PatchTemp), 9) {}
100 };
102 public:
103  RhythmTempMemoryRegion(Synth *useSynth, Bit8u *useRealMemory, Bit8u *useMaxTable) : MemoryRegion(useSynth, useRealMemory, useMaxTable, MR_RhythmTemp, MT32EMU_MEMADDR(0x030110), sizeof(MemParams::RhythmTemp), 85) {}
104 };
106 public:
107  TimbreTempMemoryRegion(Synth *useSynth, Bit8u *useRealMemory, Bit8u *useMaxTable) : MemoryRegion(useSynth, useRealMemory, useMaxTable, MR_TimbreTemp, MT32EMU_MEMADDR(0x040000), sizeof(TimbreParam), 8) {}
108 };
110 public:
111  PatchesMemoryRegion(Synth *useSynth, Bit8u *useRealMemory, Bit8u *useMaxTable) : MemoryRegion(useSynth, useRealMemory, useMaxTable, MR_Patches, MT32EMU_MEMADDR(0x050000), sizeof(PatchParam), 128) {}
112 };
114 public:
115  TimbresMemoryRegion(Synth *useSynth, Bit8u *useRealMemory, Bit8u *useMaxTable) : MemoryRegion(useSynth, useRealMemory, useMaxTable, MR_Timbres, MT32EMU_MEMADDR(0x080000), sizeof(MemParams::PaddedTimbre), 64 + 64 + 64 + 64) {}
116 };
118 public:
119  SystemMemoryRegion(Synth *useSynth, Bit8u *useRealMemory, Bit8u *useMaxTable) : MemoryRegion(useSynth, useRealMemory, useMaxTable, MR_System, MT32EMU_MEMADDR(0x100000), sizeof(MemParams::System), 1) {}
120 };
122 public:
123  // Note, we set realMemory to NULL despite the real devices buffer inbound strings. However, it is impossible to retrieve them.
124  // This entrySize permits emulation of handling a 20-byte display message sent to an old-gen device at address 0x207F7F.
125  DisplayMemoryRegion(Synth *useSynth) : MemoryRegion(useSynth, NULL, NULL, MR_Display, MT32EMU_MEMADDR(0x200000), 0x4013, 1) {}
126 };
128 public:
129  ResetMemoryRegion(Synth *useSynth) : MemoryRegion(useSynth, NULL, NULL, MR_Reset, MT32EMU_MEMADDR(0x7F0000), 0x3FFF, 1) {}
130 };
131 
132 } // namespace MT32Emu
133 
134 #endif // #ifndef MT32EMU_MEMORY_REGION_H
Definition: Structures.h:47
Definition: Structures.h:111
Definition: Analog.h:26
Definition: MemoryRegion.h:127
Definition: MemoryRegion.h:121
Definition: Structures.h:136
Definition: MemoryRegion.h:105
Definition: Structures.h:160
Definition: MemoryRegion.h:109
Definition: Synth.h:131
Definition: MemoryRegion.h:97
Definition: Structures.h:143
Definition: Structures.h:155
Definition: MemoryRegion.h:35
Definition: MemoryRegion.h:101
Definition: MemoryRegion.h:113
Definition: MemoryRegion.h:117