-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeos.cpp
More file actions
219 lines (172 loc) · 8.24 KB
/
Copy pathgeos.cpp
File metadata and controls
219 lines (172 loc) · 8.24 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @file geos.cpp
* @brief Implementation of GEOS disk formatting, Info Block parsing, and VLIR record reading for d64lib.
*/
#include "geos.h"
#include <cstring>
#include <algorithm>
#include <ranges>
#include <bitset>
namespace d64lib::geos {
/**
* @brief Extracts a null-terminated or fixed-length string from sector byte data.
* @param sectorData The vector containing sector bytes.
* @param offset The starting byte offset.
* @param maxLength The maximum length of the string to extract.
* @return A standard std::string containing the extracted text.
*/
static std::string extractString(const std::vector<uint8_t>& sectorData, size_t offset, size_t maxLength) {
if (offset >= sectorData.size()) return "";
auto start = sectorData.begin() + offset;
auto maxEnd = start + std::min(maxLength, sectorData.size() - offset);
auto end = std::find(start, maxEnd, 0);
return std::string(start, end);
}
/**
* @brief Check if a disk is formatted for GEOS.
* @param disk Reference to the d64 disk instance.
* @return true if the GEOS format signature string is found, false otherwise.
*/
bool isGeosDisk(d64& disk) {
auto bamSector = disk.readSector(DIRECTORY_TRACK, 0);
if (!bamSector.has_value()) return false;
const auto& data = bamSector.value();
if (data.size() < 0xBD) return false;
std::string sig = extractString(data, 0xAD, 16);
return sig.starts_with("GEOS format");
}
/**
* @brief Format a disk and initialize the GEOS format signature string.
* @param disk Reference to the d64 disk instance.
* @param name Name of the disk.
* @return true on success, false otherwise.
*/
bool formatGeosDisk(d64& disk, std::string_view name) {
if (disk.TRACKS != TRACKS_35) return false;
disk.formatDisk(name);
auto bamSector = disk.readSector(DIRECTORY_TRACK, 0);
if (!bamSector.has_value()) return false;
auto data = bamSector.value();
std::string sig = "GEOS format V1.0";
std::copy(sig.begin(), sig.end(), data.begin() + 0xAD);
data[0xAD + sig.length()] = 0;
return disk.writeSector(DIRECTORY_TRACK, 0, data);
}
/**
* @brief Read the Info Block for a specific GEOS file.
* @param disk Reference to the d64 disk instance.
* @param filename Name of the file.
* @return An optional InfoBlock containing GEOS metadata if found.
*/
std::optional<InfoBlock> readInfoBlock(d64& disk, std::string_view filename) {
auto fileEntry = disk.findFile(filename);
if (!fileEntry.has_value()) return std::nullopt;
uint8_t infoTrack = fileEntry.value()->side.track;
uint8_t infoSector = fileEntry.value()->side.sector;
if (infoTrack == 0) return std::nullopt;
auto sectorOpt = disk.readSector(infoTrack, infoSector);
if (!sectorOpt.has_value()) return std::nullopt;
const auto& sector = sectorOpt.value();
if (sector.size() < 256) return std::nullopt;
InfoBlock info;
info.iconWidth = sector[0x02];
info.iconHeight = 21;
info.iconData.clear();
int iconSizeInBytes = static_cast<int>(info.iconWidth) * info.iconHeight;
if (iconSizeInBytes > 0 && 0x03 + iconSizeInBytes <= 0x44) {
info.iconData.assign(sector.begin() + 0x03, sector.begin() + 0x03 + iconSizeInBytes);
}
info.dosType = sector[0x44];
info.geosType = static_cast<FileType>(sector[0x45]);
info.structure = static_cast<FileStructure>(sector[0x46]);
info.loadAddress = sector[0x47] | (sector[0x48] << 8);
info.endLoadAddress = sector[0x49] | (sector[0x4A] << 8);
info.execAddress = sector[0x4B] | (sector[0x4C] << 8);
info.className = extractString(sector, 0x4D, 12);
info.version = extractString(sector, 0x59, 4);
info.author = extractString(sector, 0x61, 20);
info.description = extractString(sector, 0xA0, 96);
return info;
}
/**
* @brief Read a Sequential GEOS file.
* @param disk Reference to the d64 disk instance.
* @param filename Name of the file.
* @return An optional byte vector containing the file contents.
*/
std::optional<std::vector<uint8_t>> readSequentialFile(d64& disk, std::string_view filename) {
return disk.readFile(std::string(filename));
}
/**
* @brief Read a specific Record from a VLIR file chain.
* @param disk Reference to the d64 disk instance.
* @param filename Name of the file.
* @param recordId 0-based record index (up to 126).
* @return An optional byte vector of the record payload.
*/
std::optional<std::vector<uint8_t>> readVlirRecord(d64& disk, std::string_view filename, int recordId) {
if (recordId < 0 || recordId > 126) return std::nullopt;
auto fileEntry = disk.findFile(filename);
if (!fileEntry.has_value()) return std::nullopt;
uint8_t indexTrack = fileEntry.value()->start.track;
uint8_t indexSector = fileEntry.value()->start.sector;
if (indexTrack == 0) return std::nullopt;
auto indexSectorOpt = disk.readSector(indexTrack, indexSector);
if (!indexSectorOpt.has_value()) return std::nullopt;
const auto& indexBlock = indexSectorOpt.value();
if (indexBlock.size() < 256) return std::nullopt;
int ptrOffset = 2 + (recordId * 2);
uint8_t recordTrack = indexBlock[ptrOffset];
uint8_t recordSector = indexBlock[ptrOffset + 1];
if (recordTrack == 0x00) return std::nullopt;
std::vector<uint8_t> result;
uint8_t currentTrack = recordTrack;
uint8_t currentSector = recordSector;
std::bitset<TRACKS_40 * 21> visited;
while (currentTrack != 0x00) {
if (currentTrack < 1 || currentTrack > TRACKS_40 || currentSector >= 21) break;
size_t idx = static_cast<size_t>(currentTrack - 1) * 21 + currentSector;
if (visited.test(idx)) break;
visited.set(idx);
auto sectorOpt = disk.readSector(currentTrack, currentSector);
if (!sectorOpt.has_value()) break;
const auto& sectorData = sectorOpt.value();
if (sectorData.size() < 2) break;
uint8_t nextTrack = sectorData[0];
uint8_t nextSector = sectorData[1];
if (nextTrack == 0x00) {
if (nextSector >= 2) {
result.insert(result.end(), sectorData.begin() + 2, sectorData.begin() + nextSector);
}
} else {
result.insert(result.end(), sectorData.begin() + 2, sectorData.end());
}
currentTrack = nextTrack;
currentSector = nextSector;
}
return result;
}
/**
* @brief Count the number of active records in a VLIR file.
* @param disk Reference to the d64 disk instance.
* @param filename Name of the file.
* @return The count of registered records.
*/
int getVlirRecordCount(d64& disk, std::string_view filename) {
auto fileEntry = disk.findFile(filename);
if (!fileEntry.has_value()) return 0;
uint8_t indexTrack = fileEntry.value()->start.track;
uint8_t indexSector = fileEntry.value()->start.sector;
if (indexTrack == 0) return 0;
auto indexSectorOpt = disk.readSector(indexTrack, indexSector);
if (!indexSectorOpt.has_value()) return 0;
const auto& indexBlock = indexSectorOpt.value();
auto indices = std::views::iota(0, 127) | std::views::reverse;
auto it = std::ranges::find_if(indices, [&](int i) {
int ptrOffset = 2 + (i * 2);
return static_cast<size_t>(ptrOffset) + 1 < indexBlock.size() &&
(indexBlock[ptrOffset] != 0x00 || indexBlock[ptrOffset + 1] == 0xFF);
});
return (it != indices.end()) ? (*it + 1) : 0;
}
} // namespace d64lib::geos