-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanked_memory.cpp
More file actions
71 lines (47 loc) · 1.35 KB
/
banked_memory.cpp
File metadata and controls
71 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdint.h>
#include <malloc.h>
#include <machine.h>
#include <memory.h>
#include <debugging.h>
#include "banked_memory.h"
static class WriteProtect: public Memory::Device {
public:
WriteProtect(): Memory::Device(0), _wp_common(0) {}
virtual void access(Memory::address addr) { _protect->access(addr); }
virtual void operator=(uint8_t b) {
if (_wp_common)
_wp_common |= 0x80;
else
(*_protect) = b;
}
virtual operator uint8_t() { return (uint8_t)(*_protect); }
private:
friend class BankedMemory;
uint8_t _wp_common;
Device *_protect;
} wp;
uint8_t BankedMemory::wp_common() const { return wp._wp_common; }
void BankedMemory::wp_common(uint8_t b) { wp._wp_common = b; }
static BankedMemory::Bank **banks;
Memory::Device *BankedMemory::get(address addr) const {
if (addr < _bank_size)
return _bank > 0? banks[_bank]: Memory::get(addr);
wp._protect = Memory::get(addr);
return ℘
}
void BankedMemory::begin(uint8_t nbanks) {
DBG_MEM("%d banks", nbanks);
_nbanks = nbanks;
banks = new BankedMemory::Bank*[nbanks+1];
for (int i = 1; i <= nbanks; i++)
banks[i] = new Bank(_bank_size);
}
BankedMemory::Bank::Bank(unsigned bytes): Memory::Device(bytes) {
DBG_MEM("new bank %d bytes", bytes);
_mem = (uint8_t *)malloc(bytes);
if (!_mem)
ERR("malloc %d failed", bytes);
}
BankedMemory::Bank::~Bank() {
if (_mem) free(_mem);
}