-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path1541job.cpp
executable file
·470 lines (376 loc) · 9.98 KB
/
1541job.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Frodo, Commodore 64 emulator for the iPhone
Copyright (C) 1994-1997,2002 Christian Bauer
See gpl.txt for license information.
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* 1541job.cpp - Emulation of 1541 GCR disk reading/writing
*
* Frodo (C) 1994-1997,2002 Christian Bauer
*
*
* Notes:
* ------
*
* - This is only used for processor-level 1541 emulation.
* It simulates the 1541 disk controller hardware (R/W head,
* GCR reading/writing).
* - The preferences settings for drive 8 are used to
* specify the .d64 file
*
* Incompatibilities:
* ------------------
*
* - No GCR writing possible (WriteSector is a ROM patch)
* - Programs depending on the exact timing of head movement/disk
* rotation don't work
* - The .d64 error info is unused
*/
#include "sysdeps.h"
#include "1541job.h"
#include "CPU1541.h"
#include "Prefs.h"
// Number of tracks/sectors
const int NUM_TRACKS = 35;
const int NUM_SECTORS = 683;
// Size of GCR encoded data
const int GCR_SECTOR_SIZE = 1+10+9+1+325+8; // SYNC Header Gap SYNC Data Gap (should be 5 SYNC bytes each)
const int GCR_TRACK_SIZE = GCR_SECTOR_SIZE * 21; // Each track in gcr_data has 21 sectors
const int GCR_DISK_SIZE = GCR_TRACK_SIZE * NUM_TRACKS;
// Job return codes
const int RET_OK = 1; // No error
const int RET_NOT_FOUND = 2; // Block not found
const int RET_NOT_READY = 15; // Drive not ready
// Number of sectors of each track
const int num_sectors[36] = {
0,
21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,
19,19,19,19,19,19,19,
18,18,18,18,18,18,
17,17,17,17,17
};
// Sector offset of start of track in .d64 file
const int sector_offset[36] = {
0,
0,21,42,63,84,105,126,147,168,189,210,231,252,273,294,315,336,
357,376,395,414,433,452,471,
490,508,526,544,562,580,
598,615,632,649,666
};
/*
* Constructor: Open .d64 file if processor-level 1541
* emulation is enabled
*/
Job1541::Job1541(uint8 *ram1541) : ram(ram1541)
{
the_file = NULL;
gcr_data = gcr_ptr = gcr_track_start = new uint8[GCR_DISK_SIZE];
gcr_track_end = gcr_track_start + GCR_TRACK_SIZE;
current_halftrack = 2;
disk_changed = true;
if (ThePrefs.Emul1541Proc)
open_d64_file(ThePrefs.DrivePath);
}
/*
* Destructor: Close .d64 file
*/
Job1541::~Job1541()
{
close_d64_file();
delete[] gcr_data;
}
/*
* Preferences may have changed
*/
void Job1541::NewPrefs(Prefs *prefs)
{
// 1541 emulation turned off?
if (!prefs->Emul1541Proc)
close_d64_file();
// 1541 emulation turned on?
else if (!ThePrefs.Emul1541Proc && prefs->Emul1541Proc)
open_d64_file(prefs->DrivePath);
// .d64 file name changed?
else if (strcmp(ThePrefs.DrivePath, prefs->DrivePath)) {
close_d64_file();
open_d64_file(prefs->DrivePath);
disk_changed = true;
}
}
/*
* Open .d64 file
*/
void Job1541::open_d64_file(char *filepath)
{
long size;
uint8 magic[4];
uint8 bam[256];
// Clear GCR buffer
memset(gcr_data, 0x55, GCR_DISK_SIZE);
// Try opening the file for reading/writing first, then for reading only
write_protected = false;
the_file = fopen(filepath, "rb+");
if (the_file == NULL) {
write_protected = true;
the_file = fopen(filepath, "rb");
}
if (the_file != NULL) {
// Check length
fseek(the_file, 0, SEEK_END);
if ((size = ftell(the_file)) < NUM_SECTORS * 256) {
fclose(the_file);
the_file = NULL;
return;
}
// x64 image?
fseek(the_file, 0, SEEK_SET);
fread(&magic, 4, 1, the_file);
if (magic[0] == 0x43 && magic[1] == 0x15 && magic[2] == 0x41 && magic[3] == 0x64)
image_header = 64;
else
image_header = 0;
// Preset error info (all sectors no error)
memset(error_info, 1, NUM_SECTORS);
// Load sector error info from .d64 file, if present
if (!image_header && size == NUM_SECTORS * 257) {
fseek(the_file, NUM_SECTORS * 256, SEEK_SET);
fread(&error_info, NUM_SECTORS, 1, the_file);
};
// Read BAM and get ID
read_sector(18, 0, bam);
id1 = bam[162];
id2 = bam[163];
// Create GCR encoded disk data from image
disk2gcr();
}
}
/*
* Close .d64 file
*/
void Job1541::close_d64_file(void)
{
if (the_file != NULL) {
fclose(the_file);
the_file = NULL;
}
}
/*
* Write sector to disk (1541 ROM patch)
*/
void Job1541::WriteSector(void)
{
int track = ram[0x18];
int sector = ram[0x19];
uint16 buf = ram[0x30] | (ram[0x31] << 8);
if (buf <= 0x0700)
if (write_sector(track, sector, ram + buf))
sector2gcr(track, sector);
}
/*
* Format one track (1541 ROM patch)
*/
void Job1541::FormatTrack(void)
{
int track = ram[0x51];
// Get new ID
uint8 bufnum = ram[0x3d];
id1 = ram[0x12 + bufnum];
id2 = ram[0x13 + bufnum];
// Create empty block
uint8 buf[256];
memset(buf, 1, 256);
buf[0] = 0x4b;
// Write block to all sectors on track
for(int sector=0; sector<num_sectors[track]; sector++) {
write_sector(track, sector, buf);
sector2gcr(track, sector);
}
// Clear error info (all sectors no error)
if (track == 35)
memset(error_info, 1, NUM_SECTORS);
// Write error_info to disk?
}
/*
* Read sector (256 bytes)
* true: success, false: error
*/
inline bool Job1541::read_sector(int track, int sector, uint8 *buffer)
{
int offset;
// Convert track/sector to byte offset in file
if ((offset = offset_from_ts(track, sector)) < 0)
return false;
fseek(the_file, offset + image_header, SEEK_SET);
fread(buffer, 256, 1, the_file);
return true;
}
/*
* Write sector (256 bytes) !! -> GCR
* true: success, false: error
*/
inline bool Job1541::write_sector(int track, int sector, uint8 *buffer)
{
int offset;
// Convert track/sector to byte offset in file
if ((offset = offset_from_ts(track, sector)) < 0)
return false;
fseek(the_file, offset + image_header, SEEK_SET);
fwrite(buffer, 256, 1, the_file);
return true;
}
/*
* Convert track/sector to offset
*/
inline int Job1541::secnum_from_ts(int track, int sector)
{
return sector_offset[track] + sector;
}
inline int Job1541::offset_from_ts(int track, int sector)
{
if ((track < 1) || (track > NUM_TRACKS) ||
(sector < 0) || (sector >= num_sectors[track]))
return -1;
return (sector_offset[track] + sector) << 8;
}
/*
* Convert 4 bytes to 5 GCR encoded bytes
*/
const uint16 gcr_table[16] = {
0x0a, 0x0b, 0x12, 0x13, 0x0e, 0x0f, 0x16, 0x17,
0x09, 0x19, 0x1a, 0x1b, 0x0d, 0x1d, 0x1e, 0x15
};
inline void Job1541::gcr_conv4(uint8 *from, uint8 *to)
{
uint16 g;
g = (gcr_table[*from >> 4] << 5) | gcr_table[*from & 15];
*to++ = g >> 2;
*to = (g << 6) & 0xc0;
from++;
g = (gcr_table[*from >> 4] << 5) | gcr_table[*from & 15];
*to++ |= (g >> 4) & 0x3f;
*to = (g << 4) & 0xf0;
from++;
g = (gcr_table[*from >> 4] << 5) | gcr_table[*from & 15];
*to++ |= (g >> 6) & 0x0f;
*to = (g << 2) & 0xfc;
from++;
g = (gcr_table[*from >> 4] << 5) | gcr_table[*from & 15];
*to++ |= (g >> 8) & 0x03;
*to = g;
}
/*
* Create GCR encoded disk data from image
*/
void Job1541::sector2gcr(int track, int sector)
{
uint8 block[256];
uint8 buf[4];
uint8 *p = gcr_data + (track-1) * GCR_TRACK_SIZE + sector * GCR_SECTOR_SIZE;
read_sector(track, sector, block);
// Create GCR header
*p++ = 0xff; // SYNC
buf[0] = 0x08; // Header mark
buf[1] = sector ^ track ^ id2 ^ id1; // Checksum
buf[2] = sector;
buf[3] = track;
gcr_conv4(buf, p);
buf[0] = id2;
buf[1] = id1;
buf[2] = 0x0f;
buf[3] = 0x0f;
gcr_conv4(buf, p+5);
p += 10;
memset(p, 0x55, 9); // Gap
p += 9;
// Create GCR data
uint8 sum;
*p++ = 0xff; // SYNC
buf[0] = 0x07; // Data mark
sum = buf[1] = block[0];
sum ^= buf[2] = block[1];
sum ^= buf[3] = block[2];
gcr_conv4(buf, p);
p += 5;
for (int i=3; i<255; i+=4) {
sum ^= buf[0] = block[i];
sum ^= buf[1] = block[i+1];
sum ^= buf[2] = block[i+2];
sum ^= buf[3] = block[i+3];
gcr_conv4(buf, p);
p += 5;
}
sum ^= buf[0] = block[255];
buf[1] = sum; // Checksum
buf[2] = 0;
buf[3] = 0;
gcr_conv4(buf, p);
p += 5;
memset(p, 0x55, 8); // Gap
}
void Job1541::disk2gcr(void)
{
// Convert all tracks and sectors
for (int track=1; track<=NUM_TRACKS; track++)
for(int sector=0; sector<num_sectors[track]; sector++)
sector2gcr(track, sector);
}
/*
* Move R/W head out (lower track numbers)
*/
void Job1541::MoveHeadOut(void)
{
if (current_halftrack == 2)
return;
current_halftrack--;
#ifndef __riscos__
printf("Head move %d\n", current_halftrack);
#endif
gcr_ptr = gcr_track_start = gcr_data + ((current_halftrack >> 1) - 1) * GCR_TRACK_SIZE;
gcr_track_end = gcr_track_start + num_sectors[current_halftrack >> 1] * GCR_SECTOR_SIZE;
}
/*
* Move R/W head in (higher track numbers)
*/
void Job1541::MoveHeadIn(void)
{
if (current_halftrack == NUM_TRACKS*2)
return;
current_halftrack++;
#ifndef __riscos__
printf("Head move %d\n", current_halftrack);
#endif
gcr_ptr = gcr_track_start = gcr_data + ((current_halftrack >> 1) - 1) * GCR_TRACK_SIZE;
gcr_track_end = gcr_track_start + num_sectors[current_halftrack >> 1] * GCR_SECTOR_SIZE;
}
/*
* Get state
*/
void Job1541::GetState(Job1541State *state)
{
state->current_halftrack = current_halftrack;
state->gcr_ptr = gcr_ptr - gcr_data;
state->write_protected = write_protected;
state->disk_changed = disk_changed;
}
/*
* Set state
*/
void Job1541::SetState(Job1541State *state)
{
current_halftrack = state->current_halftrack;
gcr_ptr = gcr_data + state->gcr_ptr;
gcr_track_start = gcr_data + ((current_halftrack >> 1) - 1) * GCR_TRACK_SIZE;
gcr_track_end = gcr_track_start + num_sectors[current_halftrack >> 1] * GCR_SECTOR_SIZE;
write_protected = state->write_protected;
disk_changed = state->disk_changed;
}