/[projet1]/public/pc/tools/oric/dsk/readdsk.c
Defence Force logotype

Contents of /public/pc/tools/oric/dsk/readdsk.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 837 - (show annotations)
Tue Sep 18 13:21:54 2012 UTC (7 years, 1 month ago) by mmu_man
File MIME type: text/plain
File size: 8098 byte(s)
Rename to lowercase for consistency.

1 /* Watcom compiler */
2 #include <i86.h>
3 #include <bios.h>
4 #include <stdio.h>
5 char *version="Readdsk 2.0";
6 char diskid[8]="MFM_DISK";
7 char pad[256];
8 int drive;
9 int tracks=0,sectors=0,sides=2;
10 char __far *biosparams;
11 struct diskinfo_t di;
12 FILE *fd;
13
14 /***********************************************************************************/
15 /* writing to the virtual disk */
16 /***********************************************************************************/
17
18 unsigned char trackbuf[6400];
19 int ptr_track;
20 int crc;
21
22 int crctab[256]={
23 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
24 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
25 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
26 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
27 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
28 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
29 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
30 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
31 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
32 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
33 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
34 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
35 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
36 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
37 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
38 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
39 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
40 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
41 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
42 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
43 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
44 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
45 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
46 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
47 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
48 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
49 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
50 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
51 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
52 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
53 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
54 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
55 };
56
57
58 reset_crc()
59 {
60 crc=0xFFFF;
61 }
62
63 int get_crc()
64 {
65 return crc;
66 }
67
68 store_byte(int val)
69 {
70 trackbuf[ptr_track++]=val;
71 crc=crctab[(crc>>8)^val]^((crc&0xFF)<<8);
72 }
73
74 store_address_mark(int mark)
75 {
76 reset_crc();
77 store_byte(0xA1);
78 store_byte(0xA1);
79 store_byte(0xA1);
80 store_byte(mark);
81 }
82
83 store_id(int cyl,int head,int record,int n)
84 {
85 store_address_mark(0xFE);
86 store_byte(cyl);
87 store_byte(head);
88 store_byte(record);
89 store_byte(n);
90 }
91
92 store_data(char buf[],int n)
93 {
94 int i, nbytes=128<<n;
95 store_address_mark(0xFB);
96 for (i=0;i<nbytes;i++) store_byte(buf[i]);
97 }
98
99 store_crc()
100 {
101 int crc=get_crc();
102 store_byte(crc>>8);
103 store_byte(crc&0xFF);
104 }
105
106 store_bad_crc()
107 {
108 store_byte(0);
109 store_byte(0);
110 }
111
112 store_gap(int size,int val)
113 {
114 int i;
115 for (i=0;i<size;i++) store_byte(val);
116 }
117
118 store_sector(char buf[],int cyl,int head,int sect,int sectsize)
119 {
120 store_gap(12,0);
121 store_id(cyl,head,sect,sectsize);
122 store_crc();
123 store_gap(22,0x4E);
124 store_gap(12,0)
125 store_data(buf,sectsize);
126 store_crc();
127 store_gap(40,0x4E);
128 }
129
130 store_sector_bad_crc(char buf[],int cyl,int head,int sect,int sectsize)
131 {
132 store_gap(12,0);
133 store_id(cyl,head,sect,sectsize);
134 store_crc();
135 store_gap(22,0x4E);
136 store_gap(12,0)
137 store_data(buf,sectsize);
138 store_bad_crc();
139 store_gap(40,0x4E);
140 }
141
142 init_track()
143 {
144 int i;
145 for (i=0;i<6400;i++) trackbuf[i]=0x4E;
146 ptr_track=0;
147 }
148
149 flush_track(FILE *fd)
150 {
151 int i;
152 fwrite(trackbuf,6400,1,fd);
153 }
154
155 /*********************************************************************************/
156 /* Reading floppy at bios level */
157 /*********************************************************************************/
158 #define OK 0
159 #define BADCRC 0x10
160 unsigned char buf[1024];
161
162 int read_sector(int cyl,int head,int sect)
163 {
164 struct diskinfo_t di;
165 int try, status;
166
167 for(try=0;try<3;try++) {
168 di.drive=drive;
169 di.head=head;
170 di.track=cyl;
171 di.sector=sect;
172 di.nsectors=1;
173 di.buffer=(char __far *)buf;
174 status = _bios_disk(_DISK_READ,&di) >> 8 ;
175 if (status==OK) break;
176 }
177 return status;
178 }
179
180 void read_track(int cyl,int head,int sectsize)
181 {
182 int sectmax;
183 if (sectors) sectmax=sectors;
184 else switch (sectsize) {
185 case 0: sectmax=35; break; /* I don't know how many 128-bytes sectors fit in */
186 case 1: sectmax=18; break; /* 256-bytes sectors */
187 case 2: sectmax=10; break; /* 512-bytes sectors */
188 case 3: sectmax=5; break; /* 1024-bytes sectors */
189 }
190 init_track();
191 for (i=1;i<=sectmax;i++) {
192 status=read_sector(cyl,head,i);
193 if (status==OK) store_sector(buf,cyl,head,i,sectsize);
194 else if (status==BADCRC) store_sector_badcrc(buf,cyl,head,i,sectsize);
195 else break;
196 }
197 }
198
199 int test_track(int track,int side,int &sectsize)
200 {
201 int n= (*sectsize);
202 int status;
203
204 do {
205 status=read_sector(track,side,1,*sectsize);
206 if (status==OK || status==BADCRC) return status;
207 (*sectsize) = ((*sectsize)+1)&3);
208 biosparams[3]=(*sectsize);
209 di.drive=drive;
210 _bios_disk(_DISK_RESET,&di);
211 } while (*sectsize!=n);
212 return status;
213 }
214
215
216 int transfer_side(int side, FILE *fd)
217 {
218 int ntrack;
219 sectsize=biosparams[3]=1; /* most often, 256-bytes sectors will be read */
220 di.drive=drive;
221 _bios_disk(_DISK_RESET,&di);
222
223 if (tracks) ntrack=tracks;
224 for (i=0;i<ntrack;i++) {
225 status=test_track(i,side,&sectsize);
226 if (status!=OK && status!=BADCRC) return i;
227 printf("Reading side %d track %d\n",side,i);
228 read_track(i,side,sectsize);
229 flush_track(fd);
230 }
231 return ntrack;
232 }
233
234
235 quit(char *msg)
236 {
237 printf(msg);
238 biosparams[3]=2; /* restore normal sector size (512) */
239 exit(0);
240 }
241
242 usage()
243 {
244 printf("Usage: readdsk [options] <floppy-drive:> <disk-image>\n");
245 printf("Options:\n");
246 printf("\t-1 (read one side only)\n");
247 printf("\t-t:nn (specify number of tracks, e.g -t:42)\n");
248 printf("\t-s:nn (specify number of sectors per track, e.g -s:17)\n");
249 printf("Nota: specifying the sectors per track number prevents the program from searching\n");
250 printf("inexistent sectors\n");
251 exit(1);
252 }
253
254 int scan_arg(char *arg)
255 {
256 if (arg[0]!=':' && arg[0]!='=') usage();
257 return atoi(arg+1);
258 }
259
260 main(int argc,char *argv[])
261 {
262 FILE *fd;
263 int t,h,tracks,sides,geometry;
264 char diskid[8], *signature="MFM_DISK";
265
266 printf("%s\n",version);
267 if (argc<3) usage();
268 for (i=1; i<argc-2; i++) {
269 if (argv[i][0]!='-' && argv[i][0]!='/') usage();
270 switch (argv[i][1]) {
271 case '1': sides=1; break;
272 case 't': tracks=scan_arg(argv[i]+2); break;
273 case 's': sectors=scan_arg(argv[i]+2); break;
274 default: usage();
275 }
276 }
277 if (strcmp(argv[i],"a:")==0 || strcmp(argv[i],"A:")==0)
278 drive=0;
279 else if (strcmp(argv[i],"b:")==0 || strcmp(argv[i],"B:")==0)
280 drive=1;
281 else {
282 printf("Invalid floppy drive specification (%s)\n",argv[2]);
283 exit(1);
284 }
285 fd=fopen(argv[argc-1],"wb");
286 if (fd==NULL) {
287 printf("Unable to open %s for writing\n",argv[argc-1]);
288 exit(1);
289 }
290 fwrite(pad,256,1,fd);
291 biosparams= *(char __far * __far *)MK_FP(0,4*0x1E);
292
293 tracks=transfer_side(0,fd);
294 if (sides>1) transfer_side(1,fd);
295
296 rewind(fd);
297 fwrite(diskid,8,1,fd);
298 fwrite(&sides,1,2,fd); fwrite(pad,1,2,fd);
299 fwrite(&tracks,1,2,fd); fwrite(pad,1,2,fd);
300 fwrite(&geometry,1,2,fd); fwrite(pad,1,2,fd);
301 fclose(fd);
302 }
303

  ViewVC Help
Powered by ViewVC 1.1.26