-
Notifications
You must be signed in to change notification settings - Fork 0
/
md.cpp
439 lines (323 loc) · 12.6 KB
/
md.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
@file md.cpp
PUNY-80
TRS-80 disk image tool for Linux and other *nix operating systems
Copyright (c) 2024 Stefan Vogt and Shawn Sijnstra
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
See file LICENSE for details.
Based on TRS-80 Virtual Disk Kit by Miguel Dutra and Mike Gore.
*/
//---------------------------------------------------------------------------------
// Operating System Interface for MicroDOS and OS/80 III
//---------------------------------------------------------------------------------
#include "windows.h"
#include "p80.h"
#include "vdi.h"
#include "osi.h"
#include "md.h"
//---------------------------------------------------------------------------------
// Initialize member variables
//---------------------------------------------------------------------------------
CMD::CMD()
: m_Flavor(MD_MICRODOS), m_Dir(), m_nSides(0), m_nSectorsPerTrack(0), m_nFile(0),
m_wSectors(0), m_dwFilePos(0), m_wSector(0), m_Buffer()
{
}
//---------------------------------------------------------------------------------
// Release allocated memory
//---------------------------------------------------------------------------------
CMD::~CMD()
{
}
//---------------------------------------------------------------------------------
// Validate DOS version and define operating parameters
//---------------------------------------------------------------------------------
DWORD CMD::Load(CVDI* pVDI, DWORD dwFlags)
{
DWORD dwError = NO_ERROR;
// Copy VDI pointer and user flags to member variables
m_pVDI = pVDI;
m_dwFlags = dwFlags;
// Get disk geometry
m_pVDI->GetDG(m_DG);
// Calculate the number of disk sides
if (m_dwFlags & V80_FLAG_SS)
m_nSides = 1;
else if (m_dwFlags & V80_FLAG_DS)
m_nSides = 2;
else
m_nSides = m_DG.LT.nLastSide - m_DG.LT.nFirstSide + 1;
// Calculate the number of sectors per track
m_nSectorsPerTrack = m_DG.LT.nLastSector - m_DG.LT.nFirstSector + 1;
// Check internal buffer size (just in case)
if (sizeof(m_Buffer) < m_DG.FT.wSectorSize)
{
dwError = ERROR_INVALID_USER_BUFFER;
goto Done;
}
m_Flavor = MD_MICRODOS;
// Read track 0, sector 0
if ((dwError = m_pVDI->Read(m_DG.FT.nTrack, m_DG.FT.nFirstSide, m_DG.FT.nFirstSector, m_Buffer, m_DG.FT.wSectorSize)) != NO_ERROR)
goto Done;
// Test for MicroDOS
if (strncmp((const char*)m_Buffer + 241, "MICRODOS", 8) == 0)
goto FillDir;
m_Flavor = MD_OS80;
// Read track 1, sector 0
if ((dwError = m_pVDI->Read(m_DG.FT.nTrack + 1, m_DG.FT.nFirstSide, m_DG.FT.nFirstSector, m_Buffer, m_DG.FT.wSectorSize)) != NO_ERROR)
goto Done;
// Test for OS/80 III
if (strncmp((const char*)m_Buffer + 20, "OS/80 III", 9) != 0)
{
dwError = ERROR_NOT_DOS_DISK;
goto Done;
}
FillDir:
m_wSectors = (m_DG.LT.nTrack - m_DG.FT.nTrack + 1) * (m_DG.LT.nLastSide - m_DG.LT.nFirstSide + 1) * (m_DG.LT.nLastSector - m_DG.LT.nFirstSector + 1);
strcpy(m_Dir[0].szName, "SYSTEM");
strcpy(m_Dir[0].szType, "SYS");
m_Dir[0].dwSize = 20 * m_DG.LT.wSectorSize;
m_Dir[0].bSystem = true;
m_Dir[0].bInvisible = true;
strcpy(m_Dir[1].szName, "DATA");
strcpy(m_Dir[1].szType, "TXT");
m_Dir[1].dwSize = (m_wSectors - 20 - (m_Flavor == MD_OS80 ? m_DG.FT.nLastSector - m_DG.FT.nFirstSector + 1 : 0)) * m_DG.LT.wSectorSize;
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Return a pointer to the first/next directory entry
//---------------------------------------------------------------------------------
DWORD CMD::Dir(void** pFile, OSI_DIR nFlag)
{
DWORD dwError = NO_ERROR;
if (nFlag == OSI_DIR_FIND_FIRST)
m_nFile = 0;
if (m_nFile > 1)
{
dwError = ERROR_NO_MORE_FILES;
goto Done;
}
*pFile = &m_Dir[m_nFile++];
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Return a pointer to the directory entry matching the file name
//---------------------------------------------------------------------------------
DWORD CMD::Open(void** pFile, const char cName[11])
{
DWORD dwError = NO_ERROR;
for (int i = 0; i < 2; i++)
{
// FIXME if (memcmp(m_Dir[i].szName, cName, sizeof(cName)) == 0)
if (memcmp(m_Dir[i].szName, cName, 11) == 0)
{
*pFile = &m_Dir[i];
goto Done;
}
}
dwError = ERROR_FILE_NOT_FOUND;
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Create a new file with the indicated name and size
//---------------------------------------------------------------------------------
DWORD CMD::Create(void** pFile, OSI_FILE& File)
{
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Move the file pointer
//---------------------------------------------------------------------------------
DWORD CMD::Seek(void* pFile, DWORD dwPos)
{
DWORD dwError = NO_ERROR;
// Check whether dwPos exceeds the file size
if (dwPos > ((OSI_FILE*)pFile)->dwSize)
{
dwError = ERROR_HANDLE_EOF;
goto Done;
}
// Calculate absolute sector corresponding to the relative file position
m_wSector = dwPos / m_DG.LT.wSectorSize + (pFile == &m_Dir[1] ? 20 : 0) + (m_Flavor == MD_OS80 ? m_DG.FT.nLastSector - m_DG.FT.nFirstSector + 1 : 0);
m_dwFilePos = dwPos;
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Read data from file
//---------------------------------------------------------------------------------
DWORD CMD::Read(void* pFile, BYTE* pBuffer, DWORD& dwBytes)
{
VDI_TRACK* pTrack;
BYTE nTrack;
BYTE nSide;
BYTE nSector;
DWORD dwFileSize;
WORD wSectorBegin;
WORD wSectorEnd;
WORD wLength;
DWORD dwRead = 0;
DWORD dwError = NO_ERROR;
// Get file size
dwFileSize = ((OSI_FILE*)pFile)->dwSize;
// Repeat while there is something left to read
while (dwBytes - dwRead > 0)
{
// Check whether last Seek() was successful
if (m_wSector == 0xFFFF)
{
dwError = ERROR_SEEK;
break;
}
// Check whether requested position doesn't exceed the file size
if (m_dwFilePos > dwFileSize)
{
dwError = ERROR_READ_FAULT;
break;
}
// Convert relative sector into Track, Side, Sector
CHS(m_wSector, nTrack, nSide, nSector);
// Get a pointer to the correct track descriptor
pTrack = (nTrack == m_DG.FT.nTrack ? &m_DG.FT : &m_DG.LT);
// Read sector into internal buffer
if ((dwError = m_pVDI->Read(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
// Calculate starting transfer point
wSectorBegin = m_dwFilePos % pTrack->wSectorSize;
// Calculate ending transfer point
wSectorEnd = (dwFileSize - m_dwFilePos >= pTrack->wSectorSize ? pTrack->wSectorSize : dwFileSize % pTrack->wSectorSize);
// Calculate transfer length
wLength = ((wSectorEnd - wSectorBegin) <= (dwBytes - dwRead) ? wSectorEnd - wSectorBegin : dwBytes - dwRead);
// Copy data from the internal buffer to the caller's buffer
memcpy(pBuffer, &m_Buffer[wSectorBegin], wLength);
// Update bytes count and caller's buffer pointer
dwRead += wLength;
pBuffer += wLength;
// Advance file pointer
Seek(pFile, m_dwFilePos + wLength);
}
dwBytes = dwRead;
return dwError;
}
//---------------------------------------------------------------------------------
// Save data to file
//---------------------------------------------------------------------------------
DWORD CMD::Write(void* pFile, BYTE* pBuffer, DWORD& dwBytes)
{
VDI_TRACK* pTrack;
BYTE nTrack;
BYTE nSide;
BYTE nSector;
DWORD dwFileSize;
WORD wSectorBegin;
WORD wSectorEnd;
WORD wLength;
DWORD dwWritten = 0;
DWORD dwError = NO_ERROR;
// Get file size
dwFileSize = ((OSI_FILE*)pFile)->dwSize;
while (dwBytes - dwWritten > 0)
{
// Check whether last Seek() was successful
if (m_wSector == 0xFFFF)
{
dwError = ERROR_SEEK;
break;
}
// Check whether requested position doesn't exceed the file size
if (m_dwFilePos > dwFileSize)
{
dwError = ERROR_WRITE_FAULT;
break;
}
// Convert relative sector into Track, Side, Sector
CHS(m_wSector, nTrack, nSide, nSector);
// Get a pointer to the correct track descriptor
pTrack = (nTrack == m_DG.FT.nTrack ? &m_DG.FT : &m_DG.LT);
// Calculate starting transfer point
wSectorBegin = m_dwFilePos % pTrack->wSectorSize;
// Calculate ending transfer point
wSectorEnd = (dwFileSize - m_dwFilePos >= pTrack->wSectorSize ? pTrack->wSectorSize : dwFileSize % pTrack->wSectorSize);
// Calculate transfer length
wLength = ((wSectorEnd - wSectorBegin) <= (dwBytes - dwWritten) ? wSectorEnd - wSectorBegin : dwBytes - dwWritten);
// If needed, read the sector before writing to it
if (wLength < pTrack->wSectorSize)
{
if ((dwError = m_pVDI->Read(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
}
// Copy data from caller's buffer to the internal buffer
memcpy(&m_Buffer[wSectorBegin], pBuffer, wLength);
// Write sector from internal buffer
if ((dwError = m_pVDI->Write(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
// Update bytes count and caller's buffer pointer
dwWritten += wLength;
pBuffer += wLength;
// Advance file pointer
Seek(pFile, m_dwFilePos + wLength);
}
dwBytes = dwWritten;
return dwError;
}
//---------------------------------------------------------------------------------
// Delete file
//---------------------------------------------------------------------------------
DWORD CMD::Delete(void* pFile)
{
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Get DOS information
//---------------------------------------------------------------------------------
void CMD::GetDOS(OSI_DOS& DOS)
{
// Zero the structure
memset(&DOS, 0, sizeof(OSI_DOS));
}
//---------------------------------------------------------------------------------
// Set DOS information
//---------------------------------------------------------------------------------
DWORD CMD::SetDOS(OSI_DOS& DOS)
{
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Get file information
//---------------------------------------------------------------------------------
void CMD::GetFile(void* pFile, OSI_FILE& File)
{
// Just copy the information
memcpy(&File, pFile, sizeof(File));
}
//---------------------------------------------------------------------------------
// Set file information (public)
//---------------------------------------------------------------------------------
DWORD CMD::SetFile(void* pFile, OSI_FILE& File)
{
return ERROR_NOT_SUPPORTED;
}
//---------------------------------------------------------------------------------
// Return the Cylinder/Head/Sector (CHS) of a given relative sector
//---------------------------------------------------------------------------------
void CMD::CHS(WORD wSector, BYTE& nTrack, BYTE& nSide, BYTE& nSector)
{
// Calculate SectorsPerCylinder
int nSectorsPerCylinder = m_nSectorsPerTrack * m_nSides;
// Track = RelativeSector / SectorsPerCylinder
nTrack = wSector / nSectorsPerCylinder;
// Side = Remainder / SectorsPerTrack
nSide = (wSector - (nTrack * nSectorsPerCylinder)) / m_nSectorsPerTrack;
// Sector = Remainder
nSector = (wSector - (nTrack * nSectorsPerCylinder + nSide * m_nSectorsPerTrack));
// Adjust Track, Side, Sector
nTrack += m_DG.FT.nTrack;
nSide += (nTrack == m_DG.FT.nTrack ? m_DG.FT.nFirstSide : m_DG.LT.nFirstSide);
nSector += (nTrack == m_DG.FT.nTrack ? m_DG.FT.nFirstSector : m_DG.LT.nFirstSector);
}