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

Contents of /public/pc/tools/oric/tap2dsk/tap2dsk.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 845 - (show annotations)
Wed Sep 19 01:51:02 2012 UTC (7 years, 1 month ago) by mmu_man
File MIME type: text/plain
File size: 6919 byte(s)
Make sure we return something on success instead of whatever random value is kept in the return register...

1 /* tap2dsk : converts an Oric tape image to a sedoric/stratsed dsk image */
2 /* (c) F.Frances 2004 */
3
4 /* Limitations :
5 * - only 1 bitmap sector, thus limits the capacity to ~2048 sectors
6 * - only 1 side with 80 tracks max.
7 */
8
9 #include <stdio.h>
10 #define NBTRACKS 21
11 #define NBSECT 17
12
13 typedef unsigned char byte;
14 typedef byte sector[256];
15
16 struct {
17 char signature[8];
18 int sides;
19 int tracks;
20 int sect;
21 char pad[256-20];
22 } imageheader = { "ORICDISK", 1, NBTRACKS, NBSECT };
23
24 sector disk[80][NBSECT];
25 sector directory,descriptor;
26 sector system_sect={ // some system parameters
27 0xD0,0xD0,0xD0,0xD0, // drive table
28 0, // keyboard type
29 100,0,10,0, // RENUM parameters
30 'T','A','P','2','D','S','K',' ','S','A','M','P','L','E',' ','D','I','S','C',' ',' '
31 };
32 sector bitmap={
33 0xFF, // signature
34 0,
35 0,0, // number of free sectors
36 0,0, // number of files
37 NBTRACKS, // number of tracks/side
38 NBSECT, // number of sectors/track
39 1, // number of directory sectors
40 NBTRACKS, // number of tracks + 128 if double sided
41 0 // master/slave/gameinit indicator
42 };
43
44 byte sedoric[]=
45 {
46 #include "sedoric3.h"
47 };
48
49 int track,sect=1;
50 int dir_offset=0x10;
51 int used_sectors;
52
53
54
55 void allocate_sector(int track, int sect, byte *contents)
56 {
57 int linear_sector=track*NBSECT+sect-1;
58 bitmap[0x10+linear_sector/8] &= ~(1<<linear_sector%8);
59 memcpy(disk[track][sect-1],contents,sizeof(sector));
60 used_sectors++;
61 }
62
63 void find_free_sector(byte *contents)
64 {
65 int linear_sector=0;
66 while ((bitmap[0x10+linear_sector/8] & (1<<linear_sector%8)) == 0)
67 linear_sector++;
68
69 track=linear_sector/NBSECT;
70 sect=linear_sector%NBSECT+1;
71 if (track==80) {
72 fprintf(stderr,"Not enough space on sample disc");
73 exit(1);
74 }
75 allocate_sector(track,sect,contents);
76 }
77
78 void update_sector(int track, int sect, byte *contents)
79 {
80 memcpy(disk[track][sect-1],contents,sizeof(sector));
81 }
82
83 void convert_basename(char *dest, char *name)
84 {
85 static int filenumber=0;
86 int dest_offset=0, src_offset=0;
87 printf("Storing ");
88 while (dest_offset<9 && src_offset<17 && name[src_offset]) {
89 if (name[src_offset]>='a' && name[src_offset]<='z')
90 name[src_offset]-=0x20;
91 if (name[src_offset]>='0' && name[src_offset]<='9'
92 || name[src_offset]>='A' && name[src_offset]<='Z') {
93 putchar(name[src_offset]);
94 dest[dest_offset++]=name[src_offset];
95 }
96 src_offset++;
97 }
98 if (dest_offset) {
99 while (dest_offset<9)
100 dest[dest_offset++]=' ';
101 } else {
102 printf("NONAME%03d",filenumber);
103 sprintf(dest,"NONAME%03d",filenumber++);
104 }
105 }
106
107 void store_file(byte *buf, char *name, byte *header)
108 {
109 int start=(header[6]<<8)+header[7];
110 int end=(header[4]<<8)+header[5];
111 int exec=header[3];
112 int block=header[2];
113 int sectors=(end-start)/sizeof(sector)+1;
114 int desc_sect,desc_track;
115 int desc_off=0x0C;
116 int offset;
117
118 memset(descriptor,0,sizeof(sector));
119 find_free_sector(descriptor);
120 desc_track=track; desc_sect=sect;
121 descriptor[2]=0xFF;
122 descriptor[3]=(block?0x40:0x80)+(exec?1:0);
123 descriptor[4]=header[7];
124 descriptor[5]=header[6];
125 descriptor[6]=header[5];
126 descriptor[7]=header[4];
127 if (exec) {
128 descriptor[8]=descriptor[4];
129 descriptor[9]=descriptor[5];
130 } else {
131 descriptor[8]=descriptor[9]=0;
132 }
133 descriptor[10]=sectors&0xFF;
134 descriptor[11]=sectors>>8;
135
136 convert_basename(directory+dir_offset,name);
137 if (exec) {
138 sprintf(directory+dir_offset+9,"COM");
139 printf(".COM");
140 } else {
141 sprintf(directory+dir_offset+9,"%s",block?"BIN":"BAS");
142 printf(".%s",block?"BIN":"BAS");
143 }
144 printf("\n");
145 directory[dir_offset+12]=desc_track;
146 directory[dir_offset+13]=desc_sect;
147 directory[dir_offset+14]=sectors+1+(sectors>=(256-12)/2?1:0); // Sedoric bug work-around : allocate a second descriptor when exactly 122 sectors of data
148 directory[dir_offset+15]=0x40; // UNPROT
149
150 while (sectors--) {
151 find_free_sector(buf);
152 buf+=sizeof(sector);
153 descriptor[desc_off++]=track;
154 descriptor[desc_off++]=sect;
155 if (desc_off==0x100) { // Sedoric bug work-around : allocate a second descriptor when the first is full, even if not needed
156 find_free_sector(descriptor);
157 descriptor[0]=track;
158 descriptor[1]=sect;
159 update_sector(desc_track,desc_sect,descriptor);
160 memset(descriptor,0,sizeof(sector));
161 desc_track=track;
162 desc_sect=sect;
163 desc_off=2;
164 }
165 }
166 update_sector(desc_track,desc_sect,descriptor);
167 }
168
169 int main(int argc, char *argv[])
170 {
171 byte header[9];
172 char name[17];
173 byte file_buffer[48*1024];
174 FILE *tape, *dsk;
175 int dir_track=20, dir_sect=4;
176 int tracks=21; // minimum track number
177 int total_sectors,free_sectors;
178 int tape_num,i;
179
180 printf("Tap2dsk V2.0\n");
181 if (argc<2) {
182 fprintf(stderr,"Usage: tap2dsk <tape_image1> ...<tape_imageN> <old_disk_image>\n");
183 exit(1);
184 }
185
186 dsk=fopen(argv[argc-1],"wb");
187 if (dsk==NULL) {
188 fprintf(stderr,"Cannot open %s for writing\n",argv[2]);
189 exit(1);
190 }
191
192 memset(bitmap+0x10,0xFF,sizeof(sector)-0x10);
193 for (i=0; i<99; i++) { // 99 secteurs pour sedoric
194 int track= i/NBSECT;
195 int sector = i%NBSECT + 1;
196 allocate_sector(track,sector,sedoric+i*256);
197 }
198 allocate_sector(20,1,system_sect);
199 allocate_sector(20,2,bitmap);
200 allocate_sector(20,4,directory);
201
202 for (tape_num=1; tape_num<argc-1; tape_num++) {
203 tape=fopen(argv[tape_num],"rb");
204 if (tape==NULL) {
205 fprintf(stderr,"Cannot read tape image %s\n",argv[1]);
206 exit(1);
207 }
208 printf("Reading %s\n",argv[tape_num]);
209
210
211 while (fgetc(tape)!=EOF) {
212 int start,end,i;
213 while (fgetc(tape)!=0x24)
214 ;
215 for (i=0;i<9;i++) header[i]=fgetc(tape);
216 for (i=0;i<17;i++) {
217 name[i]=fgetc(tape);
218 if (name[i]==0) break;
219 }
220 start=(header[6]<<8)+header[7];
221 end=(header[4]<<8)+header[5];
222 for (i=0; i<end+1-start; i++)
223 file_buffer[i]=fgetc(tape);
224 printf("Found %s\n",name);
225 store_file(file_buffer,name,header);
226 bitmap[4]++; // number of files
227 dir_offset+=16;
228 if (dir_offset==0x100) {
229 find_free_sector(directory);
230 directory[0]=track;
231 directory[1]=sect;
232 update_sector(dir_track,dir_sect,directory);
233 memset(directory,0,sizeof(sector));
234 dir_track=track;
235 dir_sect=sect;
236 dir_offset=0x10;
237 update_sector(dir_track,dir_sect,directory);
238 }
239 }
240 fclose(tape);
241 tape=NULL;
242 }
243 directory[2]=dir_offset;
244 update_sector(dir_track,dir_sect,directory);
245
246 if (track>=tracks) tracks=track+1;
247 total_sectors=tracks*NBSECT;
248 free_sectors=total_sectors-used_sectors;
249 bitmap[2]=free_sectors & 0xFF;
250 bitmap[3]=free_sectors >> 8;
251 bitmap[6]=tracks;
252 bitmap[9]=tracks;
253 update_sector(20,2,bitmap);
254
255 imageheader.tracks=tracks;
256 fwrite(&imageheader,sizeof(imageheader),1,dsk);
257 for (track=0;track<tracks;track++)
258 for (sect=1;sect<=NBSECT;sect++)
259 fwrite(disk[track][sect-1],sizeof(sector),1,dsk);
260
261 return 0;
262 }
263
264

  ViewVC Help
Powered by ViewVC 1.1.26