-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4002.cpp
177 lines (147 loc) · 5.64 KB
/
4002.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Author:
- Henry Schuler
*/
// Include local header files
#include "4002.h"
// Include gloabl header files
#include <cstring>
// Declaring namespaces
using namespace std;
Intel4002::Intel4002(const uint32_t installedChips) : currentBank(BANK0), currentChip(CHIP0), currentNibbleAddress(0), RAM(nullptr), RAMStatus(nullptr), RAMPort(nullptr), INSTALLEDRAM(nullptr) {
// Initialize RAM, RAMStatus, RAMPort, INSTALLEDRAM
RAM = new uint4_t***[MAX_NUMBER_OF_BANKS]();
RAMStatus = new uint4_t***[MAX_NUMBER_OF_BANKS]();
RAMPort = new uint4_t*[MAX_NUMBER_OF_BANKS]();
INSTALLEDRAM = new bool*[MAX_NUMBER_OF_BANKS]();
uint32_t chips = installedChips;
for (int i = 0; i < MAX_NUMBER_OF_BANKS; i++) {
RAM[i] = new uint4_t**[MAX_NUMBER_OF_RAM_CHIPS]();
RAMStatus[i] = new uint4_t**[MAX_NUMBER_OF_RAM_CHIPS]();
RAMPort[i] = new uint4_t[MAX_NUMBER_OF_RAM_CHIPS];
INSTALLEDRAM[i] = new bool[MAX_NUMBER_OF_RAM_CHIPS];
for (int j = 0; j < MAX_NUMBER_OF_RAM_CHIPS; j++, chips >>= 1) {
RAM[i][j] = new uint4_t*[MAX_NUMBER_OF_RAM_REGISTERS];
RAMStatus[i][j] = new uint4_t*[MAX_NUMBER_OF_RAM_REGISTERS];
INSTALLEDRAM[i][j] = chips & 1;
for (int k = 0; k < MAX_NUMBER_OF_RAM_REGISTERS; k++) {
RAM[i][j][k] = new uint4_t[RAM_CELLS_EACH_REGISTER];
RAMStatus[i][j][k] = new uint4_t[STATUS_CELLS_EACH_REGISTER];
}
}
}
reset();
}
Intel4002::~Intel4002() {
// Delete RAM, RAMStatus, RAMPort, INSTALLEDRAM
for (int i = 0; i < MAX_NUMBER_OF_BANKS; i ++) {
for (int j = 0; j < MAX_NUMBER_OF_RAM_CHIPS; j++) {
for (int k = 0; k < MAX_NUMBER_OF_RAM_REGISTERS; k++) {
delete[] RAM[i][j][k];
delete[] RAMStatus[i][j][k];
}
delete[] RAM[i][j];
delete[] RAMStatus[i][j];
}
delete[] RAM[i];
delete[] RAMStatus[i];
delete[] RAMPort[i];
delete[] INSTALLEDRAM[i];
}
delete[] RAM;
delete[] RAMStatus;
delete[] RAMPort;
delete[] INSTALLEDRAM;
}
void Intel4002::reset() {
currentBank = BANK0;
currentChip = CHIP0;
currentRegister = REG0;
currentNibbleAddress = 0;
for (int i = 0; i < MAX_NUMBER_OF_BANKS; i++) {
for (int j = 0; j < MAX_NUMBER_OF_RAM_CHIPS; j++) {
for (int k = 0; k < MAX_NUMBER_OF_RAM_REGISTERS; k++) {
memset(RAM[i][j][k], 0, RAM_CELLS_EACH_REGISTER);
memset(RAMStatus[i][j][k], 0, STATUS_CELLS_EACH_REGISTER);
}
}
memset(RAMPort[i], 0, MAX_NUMBER_OF_RAM_CHIPS);
}
}
bool Intel4002::isRAMAdrAccessable(const ERAMBank bank, const ERAMChip chip) const {
return INSTALLEDRAM[bank][chip];
}
uint4_t Intel4002::readRAMNibble(const ERAMBank bank, const ERAMChip chip, const ERAMRegister ramregister, const int nibbleaddress) const {
if (isRAMAdrAccessable(bank, chip)) {
return RAM[bank][chip][ramregister][(nibbleaddress & 0b1111)];
}
return 0;
}
bool Intel4002::writeRAMNibble(const ERAMBank bank, const ERAMChip chip, const ERAMRegister ramregister, const int nibbleaddress, const uint4_t value) {
if (isRAMAdrAccessable(bank, chip)) {
RAM[bank][chip][ramregister][(nibbleaddress & 0b1111)] = value;
return true;
}
return false;
}
bool Intel4002::isStatusAdrAccessable(const ERAMBank bank, const ERAMChip chip) const {
return INSTALLEDRAM[bank][chip];
}
uint4_t Intel4002::readStatusNibble(const ERAMBank bank, const ERAMChip chip, const ERAMRegister ramregister, const int nibbleaddress) const {
if (isStatusAdrAccessable(bank, chip) && nibbleaddress <= 0b11) {
return RAMStatus[bank][chip][ramregister][(nibbleaddress & 0b11)];
}
return 0;
}
bool Intel4002::writeStatusNibble(const ERAMBank bank, const ERAMChip chip, const ERAMRegister ramregister, const int nibbleaddress, const uint4_t value) {
if (isStatusAdrAccessable(bank, chip) && nibbleaddress <= 0b11) {
RAMStatus[bank][chip][ramregister][(nibbleaddress & 0b11)] = value;
return true;
}
return false;
}
uint4_t Intel4002::readFromPortBuffer(const ERAMBank bank, const ERAMChip chip) const {
if (isRAMAdrAccessable(bank, chip)) {
return RAMPort[bank][chip];
}
return 0;
}
// Our functions
uint4_t Intel4002::readRAM() {
return readRAMNibble(currentBank, currentChip, currentRegister, currentNibbleAddress);
}
bool Intel4002::writeRAM(const uint4_t value) {
return writeRAMNibble(currentBank, currentChip, currentRegister, currentNibbleAddress, value);
}
uint4_t Intel4002::readStatus(const int address) {
return readStatusNibble(currentBank, currentChip, currentRegister, (address & 0b11));
}
bool Intel4002::writeStatus(const int address, const uint4_t value) {
return writeStatusNibble(currentBank, currentChip, currentRegister, (address & 0b11), value);
}
uint4_t Intel4002::readPortBuffer() {
return readFromPortBuffer(currentBank, currentChip);
}
bool Intel4002::writePortBuffer(uint4_t value) {
if (isRAMAdrAccessable(currentBank, currentChip)) {
RAMPort[currentBank][currentChip] = value;
return true;
}
return false;
}
void Intel4002::setCurrentBank(const ERAMBank bank) {
if (BANK0 <= bank && bank <= BANK7) {
currentBank = bank;
}
}
void Intel4002::setCurrentChip(const ERAMChip chip) {
if (CHIP0 <= chip && chip <= CHIP3) {
currentChip = chip;
}
}
void Intel4002::setCurrentRegister(const ERAMRegister reg) {
currentRegister = reg;
}
void Intel4002::setCurrentNibbleAddress(const int address) {
currentNibbleAddress = (address & 0b1111);
}