-
Notifications
You must be signed in to change notification settings - Fork 1
/
rom.h
156 lines (145 loc) · 4.13 KB
/
rom.h
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
#ifndef ROM_H
#define ROM_H
#include <QString>
#include <stdint.h>
#include <stdio.h>
#include <QFile>
#ifdef _MSC_VER
#define initializerlistconstexpr const
#else
#define initializerlistconstexpr constexpr
#endif
initializerlistconstexpr
auto BASE_SIZES = {0x100000,0x200000,0x300000,0x400000,0x500000,0x600000,0x700000,0x800000};
constexpr auto HEADER_LEN = 512;
class Rom
{
public:
Rom(QString filename)
: _f(filename)
{
_ok = _f.open(QIODevice::ReadOnly); // will reopen RW later
bool validSize = false;
for (auto basesize : BASE_SIZES) {
if (_f.size() == basesize+HEADER_LEN) {
_romoff = HEADER_LEN;
validSize = true;
break;
} else if (_f.size() == basesize) {
validSize = true;
break;
}
}
if (!validSize) {
_f.close();
_ok = false;
}
}
~Rom()
{
_f.close();
_ok = false;
}
private:
public:
unsigned mapaddr(unsigned addr) const {
return addr & ~(0xc00000);
}
bool addrValid(unsigned addr) const {
addr = mapaddr(addr) + _romoff;
return (addr<_f.size());
}
uint8_t read8(unsigned addr)
{
addr = mapaddr(addr) + _romoff;
uint8_t buf[] = {0};
if (_f.seek(addr) && _f.read((char*)&buf, sizeof(buf))) return buf[0];
return 0; // TODO: warn
}
uint16_t read16(unsigned addr)
{
addr = mapaddr(addr) + _romoff;
uint8_t buf[] = {0,0};
if (_f.seek(addr) && _f.read((char*)&buf, sizeof(buf))) {
uint16_t res = buf[1];
res<<=8; res += buf[0];
return res;
}
return 0; // TODO: warn
}
uint32_t read24(unsigned addr)
{
addr = mapaddr(addr) + _romoff;
uint8_t buf[] = {0,0,0};
if (_f.seek(addr) && _f.read((char*)&buf, sizeof(buf))) {
uint32_t res = buf[2];
res<<=8; res += buf[1];
res<<=8; res += buf[0];
return res;
}
return 0; // TODO: warn
}
uint32_t read32(unsigned addr)
{
addr = mapaddr(addr) + _romoff;
uint8_t buf[] = {0,0,0,0};
if (_f.seek(addr) && _f.read((char*)&buf, sizeof(buf))) {
uint32_t res = buf[3];
res<<=8; res += buf[2];
res<<=8; res += buf[1];
res<<=8; res += buf[0];
return res;
}
return 0; // TODO: warn
}
bool readBlock(unsigned addr, void* dst, size_t len) {
addr = mapaddr(addr) + _romoff;
if (_f.seek(addr) && _f.read((char*)dst, len)) return true;
memset(dst, 0, sizeof(len));
return false;
}
QString readString(unsigned addr, size_t maxlen=1024) {
addr = mapaddr(addr) + _romoff;
QString res;
if (_f.seek(addr)) {
char c;
do {
c = (char)_f.read(1)[0];
if (!c) break;
res += c;
} while ((size_t)res.length() < maxlen);
}
return res;
}
bool writeBlock(unsigned addr, const void* src, size_t len) {
addr = mapaddr(addr) + _romoff;
if (_ok && !_f.isWritable()) { // reopen RW
_f.close();
_ok = _f.open(QIODevice::ReadWrite) && _f.isWritable();
}
if (!_ok) return false;
if (_f.seek(addr) && (size_t)_f.write((char*)src, len)==len) {
_f.flush();
return true;
}
return false;
}
bool isOpen() const { return _ok; }
bool saveAs(QString filename) {
if (!_ok) return false;
_f.close();
qDebug("Orig closed\n");
if (QFile::remove(filename)) qDebug("Destination removed\n");
if (!QFile::copy(_f.fileName(), filename)) return false;
qDebug("File copied\n");
_f.setFileName(filename);
_ok = _f.open(QIODevice::ReadOnly);
qDebug(_ok ? "New file opened\n" : "Could not open new file\n");
return _ok;
}
private:
bool _ok = false;
unsigned _romoff = 0; // TODO: detect header
QFile _f;
};
#endif // ROM_H