-
Notifications
You must be signed in to change notification settings - Fork 0
/
rd.cpp
149 lines (113 loc) · 4.46 KB
/
rd.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
/**
@file rd.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 RapiDOS
//---------------------------------------------------------------------------------
#include "windows.h"
#include "p80.h"
#include "vdi.h"
#include "osi.h"
#include "td4.h"
#include "rd.h"
//---------------------------------------------------------------------------------
// Validate DOS version and define operating parameters
//---------------------------------------------------------------------------------
DWORD CRD::Load(CVDI* pVDI, DWORD dwFlags)
{
BYTE nSides;
DWORD dwBytes;
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;
}
// Read boot sector
if ((dwError = m_pVDI->Read(m_DG.FT.nTrack, m_DG.FT.nFirstSide, m_DG.FT.nFirstSector + 1, m_Buffer, m_DG.FT.wSectorSize)) != NO_ERROR) // [PATCH]
goto Done;
// Get directory track and reset MSB (set on some DOS to indicate the disk density)
m_nDirTrack = m_Buffer[2] & 0x7F;
// Calculate number of directory sectors
m_nDirSectors = m_nSectorsPerTrack * m_nSides;
// Max Dir Sectors = HIT Size / Entries per Sector
m_nMaxDirSectors = (m_DG.LT.wSectorSize / (BYTE)(m_DG.LT.wSectorSize / sizeof(TD4_FPDE)));
// If dir sectors exceeds max sectors, limit to max
if (m_nDirSectors > m_nMaxDirSectors)
m_nDirSectors = m_nMaxDirSectors;
// If not the first Load, release the previously allocated memory
if (m_pDir != NULL)
free(m_pDir);
// Calculate the needed memory to hold the entire directory
dwBytes = m_nDirSectors * m_DG.LT.wSectorSize + (V80_MEM - (m_nDirSectors * m_DG.LT.wSectorSize) % V80_MEM);
// Allocate memory for the entire directory
if ((m_pDir = (BYTE*)calloc(dwBytes,1)) == NULL)
{
dwError = ERROR_OUTOFMEMORY;
goto Done;
}
// Load directory into memory
if ((dwError = DirRW(TD4_DIR_READ)) != NO_ERROR)
goto Done;
// Calculate DOS disk parameters
nSides = ((((TD4_GAT*)m_pDir)->nDiskConfig & TD4_GAT_SIDES) >> 5) + 1;
if (!(m_dwFlags & (V80_FLAG_SS+V80_FLAG_DS)) && (nSides < m_nSides))
m_nSides = nSides;
m_nGranulesPerTrack = (((TD4_GAT*)m_pDir)->nDiskConfig & TD4_GAT_GRANULES) + 1;
m_nGranulesPerCylinder = m_nGranulesPerTrack * m_nSides;
m_nSectorsPerGranule = m_nSectorsPerTrack / m_nGranulesPerTrack;
// GranulesPerCylinder must fit in a 8-bit GAT byte
if (m_nGranulesPerCylinder < 2 || m_nGranulesPerCylinder > 8)
{
dwError = ERROR_NOT_DOS_DISK;
goto Done;
}
// This division must leave no remainder
if (m_nSectorsPerTrack % m_nGranulesPerTrack != 0)
{
dwError = ERROR_NOT_DOS_DISK;
goto Done;
}
// SectorsPerGranule must be either 5 or 8 for SD disks
if (m_DG.LT.nDensity == VDI_DENSITY_SINGLE && m_nSectorsPerGranule != 5 && m_nSectorsPerGranule != 8)
{
dwError = ERROR_NOT_DOS_DISK;
goto Done;
}
// SectorsPerGranule must be either 6 or 10 for DD disks
if (m_DG.LT.nDensity == VDI_DENSITY_DOUBLE && m_nSectorsPerGranule != 6 && m_nSectorsPerGranule != 10)
{
dwError = ERROR_NOT_DOS_DISK;
goto Done;
}
// Check the directory structure
if (!(m_dwFlags & V80_FLAG_CHKDIR))
if ((dwError = CheckDir()) != NO_ERROR)
goto Done;
Done:
return dwError;
}