-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharcimg-112.pas
476 lines (402 loc) · 11.4 KB
/
arcimg-112.pas
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
471
472
473
474
475
476
{********** modified October 2001 by Tom Humphrey to cope with F format (1.6 Mb) disks}
uses dos,crt;
type DDTP=record
filla:array[0..2] of byte;
sectorsize:byte;
lastsector:byte;
gap:byte;
translength:byte;
sectorgap:byte;
fillbyte:byte;
fillb:word;
end;
type address_fields=record
cylinder:byte;
head:byte;
sector:byte;
bytes_per_sec:byte;
end;
var prompt,command:string;
buffer:array[1..10240] of byte; {******* changed from 5120}
last_buff:integer;
no_dsk,arc_disk:boolean;
pDDTP:^DDTP;
vDDTP:DDTP;
oldDDTP:pointer;
total_filesize:longint;
arc_title:string;
job:string;
outfile:file;
key:char;
AvTRACK:byte;
AvSECTOR:byte;
AvHEAD:byte;
read_result:byte;
format_result:byte;
sectors_to_read:byte;
re_read_count:byte;
numread:integer;
sectors_per_track:byte; {***** d/e formats use 5, f uses 10}
param_read:boolean;
param_write:boolean;
param_format:boolean;
i:integer;
function upstr(s:string):string;
var i:integer;
begin
for i:=1 to length(s) do s[i]:=upcase(s[i]);
upstr:=s;
end;
procedure init_arc_dsk;
var Reg: Registers;
begin
with Reg do
begin
AH:=0;
DL:=0;
end;
Intr($13,Reg);
getintvec($1e,oldDDTP);
pDDTP:=oldDDTP;
vDDTP:=pDDTP^;
vDDTP.sectorsize:=3; {1024 bytes}
vDDTP.lastsector:=sectors_per_track; {this is correct, not sectors-1}
vDDTP.gap:=32;
vDDTP.sectorgap:=90;
setintvec($1e,@vddtp);
last_buff:=-1;
end;
function sec_offs(vTRACK,vSECTOR,vHEAD:byte):integer;
var tmp:integer;
begin
sec_offs:=(vTRACK*10)+(vHEAD*5)+vSECTOR;
end;
function set_media_for_format:byte;
var Reg:Registers;
result:byte;
begin
with Reg do
begin
AH:=$18; {Set media type for format}
CH:=80; {number of tracks}
CL:=sectors_per_track;
DL:=0; {drive A:}
{AH:=$17;
AL:=2;
DL:=0;}
end;
Intr($13,Reg);
result:=Reg.AH;
writeln('Media init result:',result:3);
set_media_for_format:=result;
end;
function format_arc_track(vTRACK,vHEAD:byte):byte;
var result:byte;
af:array[0..10] of address_fields;
af_seg:word;
af_ofs:word;
Reg:Registers;
i:integer;
skew:array[0..10] of byte;
begin
if sectors_per_track=10 then
begin
{attempt to set proper skew for sectors on HD disk
this doesn't seem to work quite right ... any ideas?}
skew[0]:=0;
skew[1]:=7;
skew[2]:=4;
skew[3]:=1;
skew[4]:=8;
skew[5]:=5;
skew[6]:=2;
skew[7]:=9;
skew[8]:=6;
skew[9]:=3;
end
else
begin
for i:=0 to 4 do skew[i]:=i; {no skew on 800k discs}
{skew[0]:=0
skew[1]:=3
skew[2]:=1
skew[3]:=4
skew[4]:=2
}
end;
for i:=0 to (sectors_per_track-1) do
begin
af[i].cylinder:=vTRACK;
af[i].head:=vHEAD;
af[i].sector:=skew[i]; {sector number}
af[i].bytes_per_sec:=3; {3=1024}
end;
af_seg:=seg(af[0]); af_ofs:=ofs(af[0]);
{Most of the documentation I could find on formatting floppies appears to be WRONG.}
{the correct approach appears to be to use the parameters required for formatting a hard disc!}
with Reg do
begin
AH:=$05;
CH:=vTRACK;
DH:=vHEAD;
DL:=0;
ES:=af_seg; {for each sector, four bytes as above}
BX:=af_ofs;
end;
Intr($13,Reg);
{result:=Reg.AH;}
format_arc_track:=Reg.AH;
{write('=',result:3);}
end;
function read_arc_sectors(vSECTORS,vTRACK,vSECTOR,vHEAD:byte):byte;
var buf_seg,buf_ofs:word;
result:byte;
Reg:Registers;
begin
if ((vSECTORS in[1..sectors_per_track]) and (vHEAD in[0,1]) and
(vTRACK in[0..79]) and (vSECTOR in[0..(sectors_per_track-1)])) then
{********* Changed sector values from 1..5 and 0..4 respectively}
begin
buf_seg:=seg(buffer[1]); buf_ofs:=ofs(buffer[1]);
with Reg do
begin {Read a sector}
AH:=2;
AL:=vSECTORS;
CH:=vTRACK;
CL:=vSECTOR;
DH:=vHEAD;
DL:=0;
ES:=buf_seg;
BX:=buf_ofs;
end;
intr($13,Reg);
result:=Reg.AH;
last_buff:=sec_offs(vTRACK,vSECTOR,vHEAD);
read_arc_sectors:=result;
end
else read_arc_sectors:=$ff;
end;
function write_arc_sectors(vSECTORS,vTRACK,vSECTOR,vHEAD:byte):byte;
var buf_seg,buf_ofs:word;
result:byte;
Reg:Registers;
begin
if ((vSECTORS in[1..sectors_per_track]) and (vHEAD in[0,1]) and
(vTRACK in[0..79]) and (vSECTOR in[0..(sectors_per_track-1)])) then
{********* Changed sector values from 1..5 and 0..4 respectively}
begin
buf_seg:=seg(buffer[1]); buf_ofs:=ofs(buffer[1]);
with Reg do
begin {Write a sector}
ah:=3;
al:=vSECTORS;
ch:=vTRACK;
cl:=vSECTOR;
dh:=vHEAD;
dl:=0;
es:=buf_seg;
bx:=buf_ofs;
end;
intr($13,Reg);
result:=Reg.ah;
write_arc_sectors:=result;
end
else write_arc_sectors:=$ff;
end;
procedure done_arc_dsk;
begin
if oldDDTP<>nil then setintvec($1e,oldDDTP);
oldDDTP:=nil;
arc_disk:=false;
last_buff:=-1;
end;
function is_arc_dsk:boolean;
var result:byte;
begin
{I've altered this to try to autodetect if 800 or 1600k disk}
sectors_per_track:=10;
init_arc_dsk;
result:=read_arc_sectors(1,0,5,0); {try to read sector 5, (only there on big disks)}
if result=6 then result:=read_arc_sectors(1,0,5,0);
if result=4 then
begin
sectors_per_track:=5;
done_arc_dsk; {put back old values before they are overwritten again!}
init_arc_dsk;
result:=read_arc_sectors(1,0,0,0);
if (result=0) then writeln('800k Archimedes disk in drive A:');
end
else if (result=0) then writeln('1.6Mb Archimedes disk in drive A:');
if result<>128 then no_dsk:=false else no_dsk:=true;
{writeln('Disk result',result:3)}
is_arc_dsk:=(result=0);
end;
procedure ver;
begin
writeln(#13#10'Archimedes disk image program for PC');
writeln('Version 1.1 - Jasper Renow-Clarke 1997,99');
writeln('Version 1.1.2 modified Tom Humphrey 2001 ([email protected])');
end;
procedure show_syntax;
begin
ver;
writeln('Syntax :');
writeln(' ARCIMG diskimage [operation]'#13#10);
writeln(' Operation :');
writeln(' /read - Reads disk into image (Default)');
writeln(' /write - Writes disk from image');
writeln(' /format800 - Formats 800k disk ***DOESN''T WORK***');
writeln(' /format1600 - Formats 1.6Mb disk');
writeln(#10' NB: discs formatted on an Archimedes are quicker and more reliable.');
writeln( ' than those formatted here.');
halt;
end;
begin
param_read:=false;param_format:=false;param_write:=false;
if paramcount < 1 then show_syntax;
ver;
arc_disk:=false; no_dsk:=true;{job:='/read';}
if paramcount=1 then
param_read:=true
else
begin
for i:=2 to paramcount do
begin
if paramstr(i)='/format800' then
begin
param_format:=TRUE;
sectors_per_track:=5;
end
else if paramstr(i)='/format1600' then
begin
param_format:=TRUE;
sectors_per_track:=10;
end
else if paramstr(i)='/read' then param_read:=TRUE
else if paramstr(i)='/write' then param_write:=TRUE
else
show_syntax;
end;
end;
if ( ((param_format and param_read)=TRUE) or ((param_write and param_read)=TRUE) ) then
show_syntax;
{writeln('file=',paramstr(1),#10#13'read=',param_read,' write=',param_write,' format=',param_format);}
if param_format then {/format}
begin
writeln('Formating Archimedes disk...');
init_arc_dsk;
AvTRACK:=0;
i:=set_media_for_format;
repeat
format_result:=format_arc_track(AvTRACK,0); {head 0}
format_result:=format_arc_track(AvTRACK,1); {head 1}
inc(AvTRACK);
write(AvTRACK:3);
until (AvTRACK>79) or keypressed or (format_result=128);
done_arc_dsk;
if format_result=128 then
begin
Writeln(#10#13'No disc in drive A:');
halt;
end;
end;
if param_write then {/write}
if is_arc_dsk then
begin
writeln('Writing disk from image : ',paramstr(1));
arc_disk:=true;
AvHEAD:=0; AvSECTOR:=0; AvTRACK:=0;
sectors_to_read:=sectors_per_track; {******** was 5}
assign(outfile, paramstr(1));
reset(outfile,1);
{Main loop to make diskimage}
repeat
{Clear Buffer}
fillchar(buffer,sizeof(buffer),$AF);
blockread(outfile,buffer,sectors_to_read*1024,numread);
if numread>0 then
begin
read_result:=write_arc_sectors(sectors_to_read,AvTRACK,AvSECTOR,AvHEAD);
if read_result=0 then
if AvHEAD=1 then write(AvTRACK:3);
end;
inc(AvHEAD);
if AvHEAD=2 then
begin
AvHEAD:=0;
inc(AvTRACK);
if (AvTRACK>79) then AvTRACK:=255;
end;
until (AvTRACK=255) or keypressed;
close(outfile);
writeln;
writeln(#13#10'Finished writing disk from image');
end
else
begin
Writeln('Not an Archimdes disc');
done_arc_dsk;
end;
if param_read then {/read (default)}
if is_arc_dsk then
begin
writeln('Reading image from disk : ', paramstr(1));
arc_disk:=true;
AvHEAD:=0; AvSECTOR:=0; AvTRACK:=0;
sectors_to_read:=sectors_per_track; {******** was 5}
assign(outfile, paramstr(1));
rewrite(outfile,1);
{Main loop to make diskimage}
repeat
read_result:=read_arc_sectors(sectors_to_read,AvTRACK,AvSECTOR,AvHEAD);
if read_result <> 0 then {Attempt to each sector individualy}
begin
for AvSECTOR:=0 to (sectors_per_track-1) do
begin
re_read_count :=0; {will do up to 3 retries / sector}
repeat
read_result:=read_arc_sectors(1,AvTRACK,AvSECTOR,AvHEAD);
if read_result=0 then
begin
blockwrite(outfile,buffer,1024); {read success}
end;
inc(re_read_count);
until (re_read_count=3) or (read_result=0);
if read_result <> 0 then
begin
{need to write dummy block so that image file is still correct size}
fillchar(buffer,1024,$af);
{for i:=0 TO 1023 do buffer[i]=255;}
blockwrite(outfile,buffer,1024); {write dummy block to image}
textcolor(red);
writeln('Error:',AvTRACK:3,' ?',AvSector:3);
textcolor(lightgray);
end;
end;
AvSECTOR:=0; {reset back to start value}
end
else {have good data}
begin
if AvHEAD=1 then write(AvTRACK:3);
blockwrite(outfile,buffer,sectors_to_read*1024);
end;
inc(AvHEAD);
if AvHEAD=2 then
begin
AvHEAD:=0;
inc(AvTRACK);
if (AvTRACK>79) then AvTRACK:=255;
end;
until (AvTRACK=255) or keypressed;
close(outfile);
writeln;
writeln(#13#10'Finished reading image from disk');
end
else
begin
writeln('Not an Archimedes disk');
done_arc_dsk;
end;
if no_dsk then writeln('No disk in drive A');
if arc_disk then done_arc_dsk;
end.