/
memory_card.cpp
162 lines (142 loc) · 4.58 KB
/
memory_card.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
#include "memory_card.h"
#include "config.h"
namespace peripherals {
MemoryCard::MemoryCard(int port) : AbstractDevice(Type::MemoryCard, port) { verbose = config["debug"]["log"]["memoryCard"]; }
uint8_t MemoryCard::handle(uint8_t byte) {
if (state == 0) command = Command::None;
if (verbose >= 3) printf("[MEMCARD] state %d\n", state);
if (command == Command::Read) return handleRead(byte);
if (command == Command::Write) return handleWrite(byte);
if (command == Command::ID) return handleId(byte);
switch (state) {
case 0:
if (byte == 0x81) {
state++;
return 0xff;
}
return 0xff;
case 1: {
state++;
if (byte == 'R') {
command = Command::Read;
} else if (byte == 'W') {
command = Command::Write;
} else if (byte == 'S') {
command = Command::ID;
} else {
if (verbose >= 1) printf("[MEMCARD] Unsupported command 0x%02x\n", byte);
state = 0;
}
uint8_t ret = flag._reg;
flag.error = 0;
return ret;
}
default: state = 0; return 0xff;
}
}
uint8_t MemoryCard::handleRead(uint8_t byte) {
if (state >= 10 && state < 10 + 128) {
int n = state - 10;
uint8_t d = data[(address._reg * 128) + n];
checksum ^= d;
state++;
return d;
}
switch (state) {
case 2: state++; return 0x5A; // Card ID1
case 3: state++; return 0x5D; // Card ID2
case 4:
address.write(1, byte); // MSB
state++;
return 0;
case 5:
address.write(0, byte); // LSB
// Check if address is out of bounds
if (address._reg > 1024 - 1) {
if (verbose >= 1) printf("[MEMCARD] Out of bounds read 0x%04x\n", address._reg);
address._reg &= 0x3ff;
} else {
if (verbose >= 2) printf("[MEMCARD] Reading 0x%04x\n", address._reg);
}
state++;
return 0;
case 6: state++; return 0x5C; // ACK 1
case 7: state++; return 0x5D; // ACK 2
case 8:
state++;
checksum = address.read(1);
return address.read(1); // Address MSB
case 9:
state++;
checksum ^= address.read(0);
return address.read(0); // Address LSB
case 138: state++; return checksum;
case 139:
state = 0;
command = Command::None;
return 'G';
default:
state = 0;
command = Command::None;
return 0xff;
}
}
uint8_t MemoryCard::handleWrite(uint8_t byte) {
if (state >= 6 && state < 6 + 128) {
int n = state - 6;
data[(address._reg * 128) + n] = byte;
checksum ^= byte;
state++;
return 0;
}
switch (state) {
case 2: state++; return 0x5A; // Card ID1
case 3: state++; return 0x5D; // Card ID2
case 4:
address.write(1, byte); // MSB
state++;
return 0;
case 5:
address.write(0, byte); // LSB
checksum = address.read(1);
checksum ^= address.read(0);
writeStatus = WriteStatus::Good;
// Check if address is out of bounds
if (address._reg > 1024 - 1) {
flag.error = 1;
if (verbose >= 1) printf("[MEMCARD] Out of bounds write 0x%04x\n", address._reg);
address._reg &= 0x3ff;
writeStatus = WriteStatus::BadSector;
} else {
if (verbose >= 2) printf("[MEMCARD] Write 0x%04x\n", address._reg);
}
state++;
return 0;
case 134:
state++;
if (byte != checksum) {
flag.error = 1;
writeStatus = WriteStatus::BadChecksum;
}
return 0x00;
case 135: state++; return 0x5C;
case 136: state++; return 0x5D;
case 137:
dirty = true;
flag.fresh = 0;
state = 0;
command = Command::None;
return static_cast<uint8_t>(writeStatus);
default:
state = 0;
command = Command::None;
return 0xff;
}
}
uint8_t MemoryCard::handleId(uint8_t byte) {
(void)byte;
if (verbose >= 1) printf("[MEMCARD] Unsupported ID command\n");
command = Command::None;
return 0xff;
}
} // namespace peripherals