-
Notifications
You must be signed in to change notification settings - Fork 0
/
jv1.cpp
228 lines (178 loc) · 6.02 KB
/
jv1.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
/**
@file jv1.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.
*/
//---------------------------------------------------------------------------------
// Virtual Disk Interface for JV1 images
//---------------------------------------------------------------------------------
#include "windows.h"
#include <typeinfo>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include "p80.h"
#include "vdi.h"
#include "jv1.h"
static DWORD GetFileSize(HANDLE hFile)
{
struct stat buf;
int no = fileno(hFile);
fstat(no, (struct stat *) &buf);
return(buf.st_size);
}
//---------------------------------------------------------------------------------
// Validate disk format and detect disk geometry
//---------------------------------------------------------------------------------
DWORD CJV1::Load(HANDLE hFile, DWORD dwFlags)
{
DWORD dwFileSize;
DWORD dwError = NO_ERROR;
// Zero the disk geometry structure
memset(&m_DG, 0, sizeof(m_DG));
// Set sector size fields to JV1 standard
m_DG.FT.wSectorSize = JV1_SECTORSIZE;
m_DG.LT.wSectorSize = JV1_SECTORSIZE;
// Get file size
dwFileSize = GetFileSize(hFile);
// It must be a multiple of a sector size (there are no headers or anything)
if (dwFileSize % JV1_SECTORSIZE != 0)
{
dwError = ERROR_UNRECOGNIZED_MEDIA;
goto Done;
}
// Calculate number of disk sectors
m_wSectors = dwFileSize / JV1_SECTORSIZE;
if (m_wSectors <= 40*10*1) // 40 Tracks, 10 Sectors per Track, Single Sided
{
m_DG.LT.nTrack = m_wSectors / 10 - 1;
m_DG.FT.nLastSector = 9;
m_DG.LT.nLastSector = 9;
}
else if (m_wSectors <= 40*18*1) // 40 Tracks, 18 Sectors per Track, Single Sided
{
m_DG.LT.nTrack = m_wSectors / 18 - 1;
m_DG.FT.nLastSector = 17;
m_DG.LT.nLastSector = 17;
m_DG.FT.nDensity = VDI_DENSITY_DOUBLE;
m_DG.LT.nDensity = VDI_DENSITY_DOUBLE;
}
else if (m_wSectors <= 40*10*2) // 40 Tracks, 10 Sectors per Track, Double Sided
{
m_DG.LT.nTrack = m_wSectors / 20 - 1;
m_DG.FT.nLastSector = 9;
m_DG.LT.nLastSector = 9;
m_DG.FT.nLastSide = 1;
m_DG.LT.nLastSide = 1;
}
else // +40 Tracks, 18 Sectors per Track, Double Sided
{
m_DG.LT.nTrack = m_wSectors / 36 - 1;
m_DG.FT.nLastSector = 17;
m_DG.LT.nLastSector = 17;
m_DG.FT.nLastSide = 1;
m_DG.LT.nLastSide = 1;
m_DG.FT.nDensity = VDI_DENSITY_DOUBLE;
m_DG.LT.nDensity = VDI_DENSITY_DOUBLE;
}
// Track count must be exact (no remainder) or this is not a JV1 image
if ((m_wSectors % (m_DG.FT.nLastSector + 1)) != 0)
{
dwError = ERROR_UNRECOGNIZED_MEDIA;
goto Done;
}
// Track count must be between 34 and 96 or this is probably not a JV1 image
if (!(dwFlags & V80_FLAG_CHKDSK) && (m_DG.LT.nTrack < 33 || m_DG.LT.nTrack > 95))
{
dwError = ERROR_UNRECOGNIZED_MEDIA;
goto Done;
}
// Copy file handle and user flags to member variables
m_hFile = hFile;
m_dwFlags = dwFlags;
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Read one sector from the disk
//---------------------------------------------------------------------------------
DWORD CJV1::Read(BYTE nTrack, BYTE nSide, BYTE nSector, BYTE* pBuffer, WORD wSize)
{
DWORD dwBytes;
DWORD dwError = NO_ERROR;
// Check caller's buffer size
if (wSize < JV1_SECTORSIZE)
{
dwError = ERROR_INVALID_USER_BUFFER;
goto Done;
}
// Set file pointer
if ((dwError = Seek(nTrack, nSide, nSector)) != NO_ERROR)
goto Done;
// Read one sector directly to the caller's buffer
if ((dwBytes = fread(pBuffer, 1, JV1_SECTORSIZE,m_hFile)) != JV1_SECTORSIZE)
{
dwError = ERROR_READ_FAULT;
goto Done;
}
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Write one sector to the disk
//---------------------------------------------------------------------------------
DWORD CJV1::Write(BYTE nTrack, BYTE nSide, BYTE nSector, BYTE* pBuffer, WORD wSize)
{
DWORD dwBytes;
DWORD dwError = NO_ERROR;
// Check caller's buffer size
if (wSize > JV1_SECTORSIZE)
wSize = JV1_SECTORSIZE;
// Set file pointer
if ((dwError = Seek(nTrack, nSide, nSector)) != NO_ERROR)
goto Done;
// Write one sector directly from the caller's buffer
if ((dwBytes = fwrite(pBuffer, 1, wSize, m_hFile)) != wSize )
{
dwError = ERROR_WRITE_FAULT;
goto Done;
}
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Position the file pointer
//---------------------------------------------------------------------------------
DWORD CJV1::Seek(BYTE nTrack, BYTE nSide, BYTE nSector)
{
DWORD dwOffset;
DWORD dwError = NO_ERROR;
// Validate Track, Side, Sector
if (nTrack > m_DG.LT.nTrack || nSide > m_DG.LT.nLastSide || nSector > m_DG.LT.nLastSector)
{
dwError = ERROR_SECTOR_NOT_FOUND;
goto Done;
}
// Compute sector offset
dwOffset = ((nTrack * (m_DG.LT.nLastSide + 1) + nSide) * (m_DG.LT.nLastSector + 1) + nSector) * 256;
// Set file pointer
if (fseek(m_hFile, dwOffset, 0) == -1)
{
dwError = ERROR_SEEK;
goto Done;
}
Done:
return dwError;
}