| 1 |
|
| #include <stdio.h> |
| 2 |
|
| #include <stdlib.h> |
| 3 |
|
| #include <string.h> |
| 4 |
|
| #include "diskimage.h" |
| 5 |
|
| |
| 6 |
|
| |
| 7 |
|
| typedef struct errormessage { |
| 8 |
|
| signed int number; |
| 9 |
|
| char *string; |
| 10 |
|
| } ErrorMessage; |
| 11 |
|
| |
| 12 |
|
| |
| 13 |
|
| ErrorMessage error_msg[] = { |
| 14 |
|
| /* non-errors */ |
| 15 |
|
| { 0, "ok" }, |
| 16 |
|
| { 1, "files scratched" }, |
| 17 |
|
| { 2, "partition selected" }, |
| 18 |
|
| /* errors */ |
| 19 |
|
| { 20, "read error (block header not found)" }, |
| 20 |
|
| { 21, "read error (drive not ready)" }, |
| 21 |
|
| { 22, "read error (data block not found)" }, |
| 22 |
|
| { 23, "read error (crc error in data block)" }, |
| 23 |
|
| { 24, "read error (byte sector header)" }, |
| 24 |
|
| { 25, "write error (write-verify error)" }, |
| 25 |
|
| { 26, "write protect on" }, |
| 26 |
|
| { 27, "read error (crc error in header)" }, |
| 27 |
|
| { 30, "syntax error (general syntax)" }, |
| 28 |
|
| { 31, "syntax error (invalid command)" }, |
| 29 |
|
| { 32, "syntax error (long line)" }, |
| 30 |
|
| { 33, "syntax error (invalid file name)" }, |
| 31 |
|
| { 34, "syntax error (no file given)" }, |
| 32 |
|
| { 39, "syntax error (invalid command)" }, |
| 33 |
|
| { 50, "record not present" }, |
| 34 |
|
| { 51, "overflow in record" }, |
| 35 |
|
| { 52, "file too large" }, |
| 36 |
|
| { 60, "write file open" }, |
| 37 |
|
| { 61, "file not open" }, |
| 38 |
|
| { 62, "file not found" }, |
| 39 |
|
| { 63, "file exists" }, |
| 40 |
|
| { 64, "file type mismatch" }, |
| 41 |
|
| { 65, "no block" }, |
| 42 |
|
| { 66, "illegal track and sector" }, |
| 43 |
|
| { 67, "illegal system t or s" }, |
| 44 |
|
| { 70, "no channel" }, |
| 45 |
|
| { 71, "directory error" }, |
| 46 |
|
| { 72, "disk full" }, |
| 47 |
|
| { 73, "dos mismatch" }, |
| 48 |
|
| { 74, "drive not ready" }, |
| 49 |
|
| { 75, "format error" }, |
| 50 |
|
| { 76, "controller error" }, |
| 51 |
|
| { 77, "selected partition illegal" }, |
| 52 |
|
| { -1, NULL } |
| 53 |
|
| }; |
| 54 |
|
| |
| 55 |
|
| |
| 56 |
|
| /* convert to rawname */ |
| 57 |
57 |
| int di_rawname_from_name(unsigned char *rawname, char *name) { |
| 58 |
|
| int i; |
| 59 |
|
| |
| 60 |
57 |
| memset(rawname, 0xa0, 16); |
| 61 |
621 |
▶ | for (i = 0; i < 16 && name[i]; ++i) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < 16 && name[i]; ++i) { |
596 |
25 |
621 |
| for (i = 0; i < 16 && name[i]; ++i) { |
564 |
32 |
596 |
|
| 62 |
564 |
| rawname[i] = name[i]; |
| 63 |
|
| } |
| 64 |
57 |
| return(i); |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| |
| 68 |
|
| /* convert from rawname */ |
| 69 |
25 |
| int di_name_from_rawname(char *name, unsigned char *rawname) { |
| 70 |
|
| int i; |
| 71 |
|
| |
| 72 |
244 |
▶ | for (i = 0; i < 16 && rawname[i] != 0xa0; ++i) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < 16 && rawname[i] != 0xa0; ++i) { |
241 |
3 |
244 |
| for (i = 0; i < 16 && rawname[i] != 0xa0; ++i) { |
219 |
22 |
241 |
|
| 73 |
219 |
| name[i] = rawname[i]; |
| 74 |
|
| } |
| 75 |
25 |
| name[i] = 0; |
| 76 |
25 |
| return(i); |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| |
| 80 |
|
| /* return status string */ |
| 81 |
3 |
| int di_status(DiskImage *di, char *status) { |
| 82 |
3 |
| ErrorMessage *err = error_msg; |
| 83 |
|
| |
| 84 |
|
| /* special case for power up */ |
| 85 |
3 |
▶ | if (di->status == 254) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (di->status == 254) { |
1 |
2 |
3 |
|
| 86 |
1 |
| switch (di->type) { |
| 87 |
1 |
| case D64: |
| 88 |
1 |
| sprintf(status, "73,cbm dos v2.6 1541,00,00"); |
| 89 |
1 |
| break; |
| 90 |
0 |
| case D71: |
| 91 |
0 |
| sprintf(status, "73,cbm dos v3.0 1571,00,00"); |
| 92 |
0 |
| break; |
| 93 |
0 |
| case D81: |
| 94 |
0 |
| sprintf(status, "73,copyright cbm dos v10 1581,00,00"); |
| 95 |
0 |
| break; |
| 96 |
|
| } |
| 97 |
1 |
| return(73); |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
2 |
▶ | while (err->number >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (err->number >= 0) { |
2 |
0 |
2 |
|
| 101 |
2 |
▶ | if (di->status == err->number) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (di->status == err->number) { |
2 |
0 |
2 |
|
| 102 |
2 |
| sprintf(status, "%02d,%s,%02d,%02d", di->status, err->string, di->statusts.track, di->statusts.sector); |
| 103 |
2 |
| return(di->status); |
| 104 |
|
| } |
| 105 |
0 |
| err->number++; |
| 106 |
|
| } |
| 107 |
0 |
| sprintf(status, "%02d,unknown error,%02d,%02d", di->status, di->statusts.track, di->statusts.sector); |
| 108 |
0 |
| return(di->status); |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
|
| |
| 112 |
95 |
| int set_status(DiskImage *di, int status, int track, int sector) { |
| 113 |
95 |
| di->status = status; |
| 114 |
95 |
| di->statusts.track = track; |
| 115 |
95 |
| di->statusts.sector = sector; |
| 116 |
95 |
| return(status); |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| |
| 120 |
|
| /* return write interleave */ |
| 121 |
12 |
| int interleave(ImageType type) { |
| 122 |
12 |
| switch (type) { |
| 123 |
12 |
| case D64: |
| 124 |
12 |
| return(10); |
| 125 |
|
| break; |
| 126 |
0 |
| case D71: |
| 127 |
0 |
| return(6); |
| 128 |
|
| break; |
| 129 |
0 |
| default: |
| 130 |
0 |
| return(1); |
| 131 |
|
| break; |
| 132 |
|
| } |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
|
| |
| 136 |
|
| /* return number of tracks for image type */ |
| 137 |
2761 |
| int di_tracks(ImageType type) { |
| 138 |
2761 |
| switch (type) { |
| 139 |
2759 |
| case D64: |
| 140 |
2759 |
| return(35); |
| 141 |
|
| break; |
| 142 |
1 |
| case D71: |
| 143 |
1 |
| return(70); |
| 144 |
|
| break; |
| 145 |
1 |
| case D81: |
| 146 |
1 |
| return(80); |
| 147 |
|
| break; |
| 148 |
|
| } |
| 149 |
0 |
| return(0); |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
|
| |
| 153 |
|
| /* return disk geometry for track */ |
| 154 |
16585 |
| int di_sectors_per_track(ImageType type, int track) { |
| 155 |
16585 |
| switch (type) { |
| 156 |
2 |
| case D71: |
| 157 |
2 |
▶ | if (track > 35) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (track > 35) { |
1 |
1 |
2 |
|
| 158 |
1 |
| track -= 35; |
| 159 |
|
| } |
| 160 |
|
| // fall through |
| 161 |
|
| case D64: |
| 162 |
16583 |
▶ | if (track < 18) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (track < 18) { |
8633 |
7950 |
16583 |
|
| 163 |
8633 |
| return(21); |
| 164 |
7950 |
▶ | } else if (track < 25) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (track < 25) { |
3256 |
4694 |
7950 |
|
| 165 |
3256 |
| return(19); |
| 166 |
4694 |
▶ | } else if (track < 31) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (track < 31) { |
2622 |
2072 |
4694 |
|
| 167 |
2622 |
| return(18); |
| 168 |
|
| } else { |
| 169 |
2072 |
| return(17); |
| 170 |
|
| } |
| 171 |
|
| break; |
| 172 |
2 |
| case D81: |
| 173 |
2 |
| return(40); |
| 174 |
|
| break; |
| 175 |
|
| } |
| 176 |
0 |
| return(0); |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
|
| /* convert track, sector to blocknum */ |
| 180 |
17703 |
| int get_block_num(ImageType type, TrackSector ts) { |
| 181 |
|
| int block; |
| 182 |
|
| |
| 183 |
17703 |
| switch (type) { |
| 184 |
17703 |
| case D64: |
| 185 |
17703 |
▶ | if (ts.track < 18) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track < 18) { |
18 |
17685 |
17703 |
|
| 186 |
18 |
| block = (ts.track - 1) * 21; |
| 187 |
17685 |
▶ | } else if (ts.track < 25) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ts.track < 25) { |
17685 |
0 |
17685 |
|
| 188 |
17685 |
| block = (ts.track - 18) * 19 + 17 * 21; |
| 189 |
0 |
▶ | } else if (ts.track < 31) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ts.track < 31) { |
0 |
0 |
0 |
|
| 190 |
0 |
| block = (ts.track - 25) * 18 + 17 * 21 + 7 * 19; |
| 191 |
|
| } else { |
| 192 |
0 |
| block = (ts.track - 31) * 17 + 17 * 21 + 7 * 19 + 6 * 18; |
| 193 |
|
| } |
| 194 |
17703 |
| return(block + ts.sector); |
| 195 |
|
| break; |
| 196 |
0 |
| case D71: |
| 197 |
0 |
▶ | if (ts.track > 35) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track > 35) { |
0 |
0 |
0 |
|
| 198 |
0 |
| block = 683; |
| 199 |
0 |
| ts.track -= 35; |
| 200 |
|
| } else { |
| 201 |
0 |
| block = 0; |
| 202 |
|
| } |
| 203 |
0 |
▶ | if (ts.track < 18) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track < 18) { |
0 |
0 |
0 |
|
| 204 |
0 |
| block += (ts.track - 1) * 21; |
| 205 |
0 |
▶ | } else if (ts.track < 25) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ts.track < 25) { |
0 |
0 |
0 |
|
| 206 |
0 |
| block += (ts.track - 18) * 19 + 17 * 21; |
| 207 |
0 |
▶ | } else if (ts.track < 31) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ts.track < 31) { |
0 |
0 |
0 |
|
| 208 |
0 |
| block += (ts.track - 25) * 18 + 17 * 21 + 7 * 19; |
| 209 |
|
| } else { |
| 210 |
0 |
| block += (ts.track - 31) * 17 + 17 * 21 + 7 * 19 + 6 * 18; |
| 211 |
|
| } |
| 212 |
0 |
| return(block + ts.sector); |
| 213 |
|
| break; |
| 214 |
0 |
| case D81: |
| 215 |
0 |
| return((ts.track - 1) * 40 + ts.sector); |
| 216 |
|
| break; |
| 217 |
|
| } |
| 218 |
0 |
| return(0); |
| 219 |
|
| } |
| 220 |
|
| |
| 221 |
|
| |
| 222 |
|
| /* get a pointer to block data */ |
| 223 |
17703 |
| unsigned char *get_ts_addr(DiskImage *di, TrackSector ts) { |
| 224 |
17703 |
| return(di->image + get_block_num(di->type, ts) * 256); |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
|
| |
| 228 |
|
| /* return a pointer to the next block in the chain */ |
| 229 |
53 |
| TrackSector next_ts_in_chain(DiskImage *di, TrackSector ts) { |
| 230 |
|
| unsigned char *p; |
| 231 |
|
| TrackSector newts; |
| 232 |
|
| |
| 233 |
53 |
| p = get_ts_addr(di, ts); |
| 234 |
53 |
| newts.track = p[0]; |
| 235 |
53 |
| newts.sector = p[1]; |
| 236 |
53 |
▶ | if (p[0] > di_tracks(di->type)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p[0] > di_tracks(di->type)) { |
0 |
53 |
53 |
|
| 237 |
0 |
| newts.track = 0; |
| 238 |
0 |
| newts.sector = 0; |
| 239 |
53 |
▶ | } else if (p[1] > di_sectors_per_track(di->type, p[0])) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (p[1] > di_sectors_per_track(di->type, p[0])) { |
17 |
36 |
53 |
|
| 240 |
17 |
| newts.track = 0; |
| 241 |
17 |
| newts.sector = 0; |
| 242 |
|
| } |
| 243 |
53 |
| return(newts); |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
|
| |
| 247 |
|
| /* return a pointer to the disk title */ |
| 248 |
2 |
| unsigned char *di_title(DiskImage *di) { |
| 249 |
2 |
▶ | switch (di->type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (di->type) { |
2 |
0 |
2 |
|
| 250 |
2 |
| default: |
| 251 |
|
| case D64: |
| 252 |
|
| case D71: |
| 253 |
2 |
| return(get_ts_addr(di, di->dir) + 144); |
| 254 |
|
| break; |
| 255 |
0 |
| case D81: |
| 256 |
0 |
| return(get_ts_addr(di, di->dir) + 4); |
| 257 |
|
| break; |
| 258 |
|
| } |
| 259 |
|
| } |
| 260 |
|
| |
| 261 |
|
| |
| 262 |
|
| /* return number of free blocks in track */ |
| 263 |
1771 |
| int di_track_blocks_free(DiskImage *di, int track) { |
| 264 |
|
| unsigned char *bam; |
| 265 |
|
| |
| 266 |
1771 |
| switch (di->type) { |
| 267 |
1771 |
| default: |
| 268 |
|
| case D64: |
| 269 |
1771 |
| bam = get_ts_addr(di, di->bam); |
| 270 |
1771 |
| break; |
| 271 |
0 |
| case D71: |
| 272 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 273 |
0 |
▶ | if (track >= 36) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (track >= 36) { |
0 |
0 |
0 |
|
| 274 |
0 |
| return(bam[track + 185]); |
| 275 |
|
| } |
| 276 |
0 |
| break; |
| 277 |
0 |
| case D81: |
| 278 |
0 |
▶ | if (track <= 40) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (track <= 40) { |
0 |
0 |
0 |
|
| 279 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 280 |
|
| } else { |
| 281 |
0 |
| bam = get_ts_addr(di, di->bam2); |
| 282 |
0 |
| track -= 40; |
| 283 |
|
| } |
| 284 |
0 |
| return(bam[track * 6 + 10]); |
| 285 |
|
| break; |
| 286 |
|
| } |
| 287 |
1771 |
| return(bam[track * 4]); |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
|
| |
| 291 |
|
| /* count number of free blocks */ |
| 292 |
52 |
| int blocks_free(DiskImage *di) { |
| 293 |
|
| int track; |
| 294 |
52 |
| int blocks = 0; |
| 295 |
|
| |
| 296 |
1872 |
▶ | for (track = 1; track <= di_tracks(di->type); ++track) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (track = 1; track <= di_tracks(di->type); ++track) { |
1820 |
52 |
1872 |
|
| 297 |
1820 |
▶ | if (track != di->dir.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (track != di->dir.track) { |
1768 |
52 |
1820 |
|
| 298 |
1768 |
| blocks += di_track_blocks_free(di, track); |
| 299 |
|
| } |
| 300 |
|
| } |
| 301 |
52 |
| return(blocks); |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
|
| |
| 305 |
|
| /* check if track, sector is free in BAM */ |
| 306 |
15 |
| int di_is_ts_free(DiskImage *di, TrackSector ts) { |
| 307 |
|
| unsigned char mask; |
| 308 |
|
| unsigned char *bam; |
| 309 |
|
| |
| 310 |
15 |
| switch (di->type) { |
| 311 |
15 |
| case D64: |
| 312 |
15 |
| bam = get_ts_addr(di, di->bam); |
| 313 |
15 |
▶ | if (bam[ts.track * 4]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bam[ts.track * 4]) { |
15 |
0 |
15 |
|
| 314 |
15 |
| mask = 1<<(ts.sector & 7); |
| 315 |
15 |
| return(bam[ts.track * 4 + ts.sector / 8 + 1] & mask ? 1 : 0); |
| 316 |
|
| } else { |
| 317 |
0 |
| return(0); |
| 318 |
|
| } |
| 319 |
|
| break; |
| 320 |
0 |
| case D71: |
| 321 |
0 |
| mask = 1<<(ts.sector & 7); |
| 322 |
0 |
▶ | if (ts.track < 36) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track < 36) { |
0 |
0 |
0 |
|
| 323 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 324 |
0 |
| return(bam[ts.track * 4 + ts.sector / 8 + 1] & mask ? 1 : 0); |
| 325 |
|
| } else { |
| 326 |
0 |
| bam = get_ts_addr(di, di->bam2); |
| 327 |
0 |
| return(bam[(ts.track - 35) * 3 + ts.sector / 8 - 3] & mask ? 1 : 0); |
| 328 |
|
| } |
| 329 |
|
| break; |
| 330 |
0 |
| case D81: |
| 331 |
0 |
| mask = 1<<(ts.sector & 7); |
| 332 |
0 |
▶ | if (ts.track < 41) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track < 41) { |
0 |
0 |
0 |
|
| 333 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 334 |
|
| } else { |
| 335 |
0 |
| bam = get_ts_addr(di, di->bam2); |
| 336 |
0 |
| ts.track -= 40; |
| 337 |
|
| } |
| 338 |
0 |
| return(bam[ts.track * 6 + ts.sector / 8 + 11] & mask ? 1 : 0); |
| 339 |
|
| break; |
| 340 |
|
| } |
| 341 |
0 |
| return(0); |
| 342 |
|
| } |
| 343 |
|
| |
| 344 |
|
| |
| 345 |
|
| /* allocate track, sector in BAM */ |
| 346 |
62 |
| void di_alloc_ts(DiskImage *di, TrackSector ts) { |
| 347 |
|
| unsigned char mask; |
| 348 |
|
| unsigned char *bam; |
| 349 |
|
| |
| 350 |
62 |
| di->modified = 1; |
| 351 |
62 |
| switch (di->type) { |
| 352 |
62 |
| case D64: |
| 353 |
62 |
| bam = get_ts_addr(di, di->bam); |
| 354 |
62 |
| bam[ts.track * 4] -= 1; |
| 355 |
62 |
| mask = 1<<(ts.sector & 7); |
| 356 |
62 |
| bam[ts.track * 4 + ts.sector / 8 + 1] &= ~mask; |
| 357 |
62 |
| break; |
| 358 |
0 |
| case D71: |
| 359 |
0 |
| mask = 1<<(ts.sector & 7); |
| 360 |
0 |
▶ | if (ts.track < 36) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track < 36) { |
0 |
0 |
0 |
|
| 361 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 362 |
0 |
| bam[ts.track * 4] -= 1; |
| 363 |
0 |
| bam[ts.track * 4 + ts.sector / 8 + 1] &= ~mask; |
| 364 |
|
| } else { |
| 365 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 366 |
0 |
| bam[ts.track + 185] -= 1; |
| 367 |
0 |
| bam = get_ts_addr(di, di->bam2); |
| 368 |
0 |
| bam[(ts.track - 35) * 3 + ts.sector / 8 - 3] &= ~mask; |
| 369 |
|
| } |
| 370 |
0 |
| break; |
| 371 |
0 |
| case D81: |
| 372 |
0 |
▶ | if (ts.track < 41) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track < 41) { |
0 |
0 |
0 |
|
| 373 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 374 |
|
| } else { |
| 375 |
0 |
| bam = get_ts_addr(di, di->bam2); |
| 376 |
0 |
| ts.track -= 40; |
| 377 |
|
| } |
| 378 |
0 |
| bam[ts.track * 6 + 10] -= 1; |
| 379 |
0 |
| mask = 1<<(ts.sector & 7); |
| 380 |
0 |
| bam[ts.track * 6 + ts.sector / 8 + 11] &= ~mask; |
| 381 |
0 |
| break; |
| 382 |
|
| } |
| 383 |
62 |
| } |
| 384 |
|
| |
| 385 |
|
| |
| 386 |
|
| /* allocate next available block */ |
| 387 |
12 |
| TrackSector alloc_next_ts(DiskImage *di, TrackSector prevts) { |
| 388 |
|
| unsigned char *bam; |
| 389 |
|
| int spt, s1, s2, t1, t2, bpt, boff, res1, res2; |
| 390 |
|
| TrackSector ts; |
| 391 |
|
| |
| 392 |
12 |
| switch (di->type) { |
| 393 |
12 |
| default: |
| 394 |
|
| case D64: |
| 395 |
12 |
| s1 = 1; |
| 396 |
12 |
| t1 = 35; |
| 397 |
12 |
| s2 = 1; |
| 398 |
12 |
| t2 = 35; |
| 399 |
12 |
| res1 = 18; |
| 400 |
12 |
| res2 = 0; |
| 401 |
12 |
| bpt = 4; |
| 402 |
12 |
| boff = 0; |
| 403 |
12 |
| break; |
| 404 |
0 |
| case D71: |
| 405 |
0 |
| s1 = 1; |
| 406 |
0 |
| t1 = 35; |
| 407 |
0 |
| s2 = 36; |
| 408 |
0 |
| t2 = 70; |
| 409 |
0 |
| res1 = 18; |
| 410 |
0 |
| res2 = 53; |
| 411 |
0 |
| bpt = 4; |
| 412 |
0 |
| boff = 0; |
| 413 |
0 |
| break; |
| 414 |
0 |
| case D81: |
| 415 |
0 |
| s1 = 1; |
| 416 |
0 |
| t1 = 40; |
| 417 |
0 |
| s2 = 41; |
| 418 |
0 |
| t2 = 80; |
| 419 |
0 |
| res1 = 40; |
| 420 |
0 |
| res2 = 0; |
| 421 |
0 |
| bpt = 6; |
| 422 |
0 |
| boff = 10; |
| 423 |
0 |
| break; |
| 424 |
|
| } |
| 425 |
|
| |
| 426 |
12 |
| bam = get_ts_addr(di, di->bam); |
| 427 |
12 |
▶ | for (ts.track = s1; ts.track <= t1; ++ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ts.track = s1; ts.track <= t1; ++ts.track) { |
12 |
0 |
12 |
|
| 428 |
12 |
▶ | if (ts.track != res1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track != res1) { |
12 |
0 |
12 |
|
| 429 |
12 |
▶ | if (bam[ts.track * bpt + boff]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bam[ts.track * bpt + boff]) { |
12 |
0 |
12 |
|
| 430 |
12 |
| spt = di_sectors_per_track(di->type, ts.track); |
| 431 |
12 |
| ts.sector = (prevts.sector + interleave(di->type)) % spt; |
| 432 |
0 |
| for (; ; ts.sector = (ts.sector + 1) % spt) { |
| 433 |
12 |
▶ | if (di_is_ts_free(di, ts)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (di_is_ts_free(di, ts)) { |
12 |
0 |
12 |
|
| 434 |
12 |
| di_alloc_ts(di, ts); |
| 435 |
12 |
| return(ts); |
| 436 |
|
| } |
| 437 |
|
| } |
| 438 |
|
| } |
| 439 |
|
| } |
| 440 |
|
| } |
| 441 |
|
| |
| 442 |
0 |
▶ | if (di->type == D71 || di->type == D81) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (di->type == D71 || di->type == D81) { |
0 |
0 |
0 |
| if (di->type == D71 || di->type == D81) { |
0 |
0 |
0 |
|
| 443 |
0 |
| bam = get_ts_addr(di, di->bam2); |
| 444 |
0 |
▶ | for (ts.track = s2; ts.track <= t2; ++ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ts.track = s2; ts.track <= t2; ++ts.track) { |
0 |
0 |
0 |
|
| 445 |
0 |
▶ | if (ts.track != res2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track != res2) { |
0 |
0 |
0 |
|
| 446 |
0 |
▶ | if (bam[(ts.track - t1) * bpt + boff]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bam[(ts.track - t1) * bpt + boff]) { |
0 |
0 |
0 |
|
| 447 |
0 |
| spt = di_sectors_per_track(di->type, ts.track); |
| 448 |
0 |
| ts.sector = (prevts.sector + interleave(di->type)) % spt; |
| 449 |
0 |
| for (; ; ts.sector = (ts.sector + 1) % spt) { |
| 450 |
0 |
▶ | if (di_is_ts_free(di, ts)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (di_is_ts_free(di, ts)) { |
0 |
0 |
0 |
|
| 451 |
0 |
| di_alloc_ts(di, ts); |
| 452 |
0 |
| return(ts); |
| 453 |
|
| } |
| 454 |
|
| } |
| 455 |
|
| } |
| 456 |
|
| } |
| 457 |
|
| } |
| 458 |
|
| } |
| 459 |
|
| |
| 460 |
0 |
| ts.track = 0; |
| 461 |
0 |
| ts.sector = 0; |
| 462 |
0 |
| return(ts); |
| 463 |
|
| } |
| 464 |
|
| |
| 465 |
|
| |
| 466 |
|
| /* allocate next available directory block */ |
| 467 |
0 |
| TrackSector alloc_next_dir_ts(DiskImage *di) { |
| 468 |
|
| unsigned char *p; |
| 469 |
|
| int spt; |
| 470 |
|
| TrackSector ts, lastts; |
| 471 |
|
| |
| 472 |
0 |
▶ | if (di_track_blocks_free(di, di->bam.track)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (di_track_blocks_free(di, di->bam.track)) { |
0 |
0 |
0 |
|
| 473 |
0 |
| ts.track = di->bam.track; |
| 474 |
0 |
| ts.sector = 0; |
| 475 |
0 |
▶ | while (ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ts.track) { |
0 |
0 |
0 |
|
| 476 |
0 |
| lastts = ts; |
| 477 |
0 |
| ts = next_ts_in_chain(di, ts); |
| 478 |
|
| } |
| 479 |
0 |
| ts.track = lastts.track; |
| 480 |
0 |
| ts.sector = lastts.sector + 3; |
| 481 |
0 |
| spt = di_sectors_per_track(di->type, ts.track); |
| 482 |
0 |
| for (; ; ts.sector = (ts.sector + 1) % spt) { |
| 483 |
0 |
▶ | if (di_is_ts_free(di, ts)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (di_is_ts_free(di, ts)) { |
0 |
0 |
0 |
|
| 484 |
0 |
| di_alloc_ts(di, ts); |
| 485 |
0 |
| p = get_ts_addr(di, lastts); |
| 486 |
0 |
| p[0] = ts.track; |
| 487 |
0 |
| p[1] = ts.sector; |
| 488 |
0 |
| p = get_ts_addr(di, ts); |
| 489 |
0 |
| memset(p, 0, 256); |
| 490 |
0 |
| p[1] = 0xff; |
| 491 |
0 |
| di->modified = 1; |
| 492 |
0 |
| return(ts); |
| 493 |
|
| } |
| 494 |
|
| } |
| 495 |
|
| } else { |
| 496 |
0 |
| ts.track = 0; |
| 497 |
0 |
| ts.sector = 0; |
| 498 |
0 |
| return(ts); |
| 499 |
|
| } |
| 500 |
|
| } |
| 501 |
|
| |
| 502 |
|
| |
| 503 |
|
| /* free a block in the BAM */ |
| 504 |
15712 |
| void di_free_ts(DiskImage *di, TrackSector ts) { |
| 505 |
|
| unsigned char mask; |
| 506 |
|
| unsigned char *bam; |
| 507 |
|
| |
| 508 |
15712 |
| di->modified = 1; |
| 509 |
15712 |
| switch (di->type) { |
| 510 |
15712 |
| case D64: |
| 511 |
15712 |
| mask = 1<<(ts.sector & 7); |
| 512 |
15712 |
| bam = get_ts_addr(di, di->bam); |
| 513 |
15712 |
| bam[ts.track * 4 + ts.sector / 8 + 1] |= mask; |
| 514 |
15712 |
| bam[ts.track * 4] += 1; |
| 515 |
15712 |
| break; |
| 516 |
0 |
| case D71: |
| 517 |
0 |
| mask = 1<<(ts.sector & 7); |
| 518 |
0 |
▶ | if (ts.track < 36) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track < 36) { |
0 |
0 |
0 |
|
| 519 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 520 |
0 |
| bam[ts.track * 4 + ts.sector / 8 + 1] |= mask; |
| 521 |
0 |
| bam[ts.track * 4] += 1; |
| 522 |
|
| } else { |
| 523 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 524 |
0 |
| bam[ts.track + 185] += 1; |
| 525 |
0 |
| bam = get_ts_addr(di, di->bam2); |
| 526 |
0 |
| bam[(ts.track - 35) * 3 + ts.sector / 8 - 3] |= mask; |
| 527 |
|
| } |
| 528 |
0 |
| break; |
| 529 |
0 |
| case D81: |
| 530 |
0 |
▶ | if (ts.track < 41) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track < 41) { |
0 |
0 |
0 |
|
| 531 |
0 |
| bam = get_ts_addr(di, di->bam); |
| 532 |
|
| } else { |
| 533 |
0 |
| bam = get_ts_addr(di, di->bam2); |
| 534 |
0 |
| ts.track -= 40; |
| 535 |
|
| } |
| 536 |
0 |
| mask = 1<<(ts.sector & 7); |
| 537 |
0 |
| bam[ts.track * 6 + ts.sector / 8 + 11] |= mask; |
| 538 |
0 |
| bam[ts.track * 6 + 10] += 1; |
| 539 |
0 |
| break; |
| 540 |
0 |
| default: |
| 541 |
0 |
| break; |
| 542 |
|
| } |
| 543 |
15712 |
| } |
| 544 |
|
| |
| 545 |
|
| |
| 546 |
|
| /* free a chain of blocks */ |
| 547 |
1 |
| void free_chain(DiskImage *di, TrackSector ts) { |
| 548 |
2 |
▶ | while (ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ts.track) { |
1 |
1 |
2 |
|
| 549 |
1 |
| di_free_ts(di, ts); |
| 550 |
1 |
| ts = next_ts_in_chain(di, ts); |
| 551 |
|
| } |
| 552 |
1 |
| } |
| 553 |
|
| |
| 554 |
|
| |
| 555 |
1 |
| DiskImage *di_load_image(char *name) { |
| 556 |
|
| FILE *file; |
| 557 |
|
| int filesize, l, read; |
| 558 |
|
| DiskImage *di; |
| 559 |
|
| |
| 560 |
|
| /* open image */ |
| 561 |
1 |
▶ | if ((file = fopen(name, "rb")) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((file = fopen(name, "rb")) == NULL) { |
0 |
1 |
1 |
|
| 562 |
|
| //puts("fopen failed"); |
| 563 |
0 |
| return(NULL); |
| 564 |
|
| } |
| 565 |
|
| |
| 566 |
|
| /* get file size*/ |
| 567 |
1 |
▶ | if (fseek(file, 0, SEEK_END)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fseek(file, 0, SEEK_END)) { |
0 |
1 |
1 |
|
| 568 |
|
| //puts("fseek failed"); |
| 569 |
0 |
| fclose(file); |
| 570 |
0 |
| return(NULL); |
| 571 |
|
| } |
| 572 |
1 |
| filesize = ftell(file); |
| 573 |
1 |
| fseek(file, 0, SEEK_SET); |
| 574 |
|
| |
| 575 |
1 |
▶ | if ((di = malloc(sizeof(*di))) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((di = malloc(sizeof(*di))) == NULL) { |
0 |
1 |
1 |
|
| 576 |
|
| //puts("malloc failed"); |
| 577 |
0 |
| fclose(file); |
| 578 |
0 |
| return(NULL); |
| 579 |
|
| } |
| 580 |
|
| |
| 581 |
|
| /* check image type */ |
| 582 |
1 |
| switch (filesize) { |
| 583 |
1 |
| case 174848: // standard D64 |
| 584 |
|
| case 175531: // D64 with error info (which we just ignore) |
| 585 |
1 |
| di->type = D64; |
| 586 |
1 |
| di->bam.track = 18; |
| 587 |
1 |
| di->bam.sector = 0; |
| 588 |
1 |
| di->dir = di->bam; |
| 589 |
1 |
| break; |
| 590 |
0 |
| case 349696: |
| 591 |
0 |
| di->type = D71; |
| 592 |
0 |
| di->bam.track = 18; |
| 593 |
0 |
| di->bam.sector = 0; |
| 594 |
0 |
| di->bam2.track = 53; |
| 595 |
0 |
| di->bam2.sector = 0; |
| 596 |
0 |
| di->dir = di->bam; |
| 597 |
0 |
| break; |
| 598 |
0 |
| case 819200: |
| 599 |
0 |
| di->type = D81; |
| 600 |
0 |
| di->bam.track = 40; |
| 601 |
0 |
| di->bam.sector = 1; |
| 602 |
0 |
| di->bam2.track = 40; |
| 603 |
0 |
| di->bam2.sector = 2; |
| 604 |
0 |
| di->dir.track = 40; |
| 605 |
0 |
| di->dir.sector = 0; |
| 606 |
0 |
| break; |
| 607 |
0 |
| default: |
| 608 |
|
| //puts("unknown type"); |
| 609 |
0 |
| free(di); |
| 610 |
0 |
| fclose(file); |
| 611 |
0 |
| return(NULL); |
| 612 |
|
| } |
| 613 |
|
| |
| 614 |
1 |
| di->size = filesize; |
| 615 |
|
| |
| 616 |
|
| /* allocate buffer for image */ |
| 617 |
1 |
▶ | if ((di->image = malloc(filesize)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((di->image = malloc(filesize)) == NULL) { |
0 |
1 |
1 |
|
| 618 |
|
| //puts("image malloc failed"); |
| 619 |
0 |
| free(di); |
| 620 |
0 |
| fclose(file); |
| 621 |
0 |
| return(NULL); |
| 622 |
|
| } |
| 623 |
|
| |
| 624 |
|
| /* read file into buffer */ |
| 625 |
1 |
| read = 0; |
| 626 |
2 |
▶ | while (read < filesize) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (read < filesize) { |
1 |
1 |
2 |
|
| 627 |
1 |
▶ | if ((l = fread(di->image, 1, filesize - read, file))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((l = fread(di->image, 1, filesize - read, file))) { |
1 |
0 |
1 |
|
| 628 |
1 |
| read += l; |
| 629 |
|
| } else { |
| 630 |
|
| //puts("fread failed"); |
| 631 |
0 |
| free(di->image); |
| 632 |
0 |
| free(di); |
| 633 |
0 |
| fclose(file); |
| 634 |
0 |
| return(NULL); |
| 635 |
|
| } |
| 636 |
|
| } |
| 637 |
|
| |
| 638 |
1 |
| di->filename = malloc(strlen(name) + 1); |
| 639 |
1 |
| strcpy(di->filename, name); |
| 640 |
1 |
| di->openfiles = 0; |
| 641 |
1 |
| di->blocksfree = blocks_free(di); |
| 642 |
1 |
| di->modified = 0; |
| 643 |
1 |
| fclose(file); |
| 644 |
1 |
| set_status(di, 254, 0, 0); |
| 645 |
1 |
| return(di); |
| 646 |
|
| } |
| 647 |
|
| |
| 648 |
|
| |
| 649 |
28 |
| DiskImage *di_create_image(char *name, int size) { |
| 650 |
|
| DiskImage *di; |
| 651 |
|
| |
| 652 |
28 |
▶ | if ((di = malloc(sizeof(*di))) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((di = malloc(sizeof(*di))) == NULL) { |
0 |
28 |
28 |
|
| 653 |
|
| //puts("malloc failed"); |
| 654 |
0 |
| return(NULL); |
| 655 |
|
| } |
| 656 |
|
| |
| 657 |
|
| /* check image type */ |
| 658 |
28 |
| switch (size) { |
| 659 |
28 |
| case 174848: // standard D64 |
| 660 |
|
| case 175531: // D64 with error info (which we just ignore) |
| 661 |
28 |
| di->type = D64; |
| 662 |
28 |
| di->bam.track = 18; |
| 663 |
28 |
| di->bam.sector = 0; |
| 664 |
28 |
| di->dir = di->bam; |
| 665 |
28 |
| break; |
| 666 |
0 |
| case 349696: |
| 667 |
0 |
| di->type = D71; |
| 668 |
0 |
| di->bam.track = 18; |
| 669 |
0 |
| di->bam.sector = 0; |
| 670 |
0 |
| di->bam2.track = 53; |
| 671 |
0 |
| di->bam2.sector = 0; |
| 672 |
0 |
| di->dir = di->bam; |
| 673 |
0 |
| break; |
| 674 |
0 |
| case 819200: |
| 675 |
0 |
| di->type = D81; |
| 676 |
0 |
| di->bam.track = 40; |
| 677 |
0 |
| di->bam.sector = 1; |
| 678 |
0 |
| di->bam2.track = 40; |
| 679 |
0 |
| di->bam2.sector = 2; |
| 680 |
0 |
| di->dir.track = 40; |
| 681 |
0 |
| di->dir.sector = 0; |
| 682 |
0 |
| break; |
| 683 |
0 |
| default: |
| 684 |
|
| //puts("unknown type"); |
| 685 |
0 |
| free(di); |
| 686 |
0 |
| return(NULL); |
| 687 |
|
| } |
| 688 |
|
| |
| 689 |
28 |
| di->size = size; |
| 690 |
|
| |
| 691 |
|
| /* allocate buffer for image */ |
| 692 |
28 |
▶ | if ((di->image = malloc(size)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((di->image = malloc(size)) == NULL) { |
0 |
28 |
28 |
|
| 693 |
|
| //puts("image malloc failed"); |
| 694 |
0 |
| free(di); |
| 695 |
0 |
| return(NULL); |
| 696 |
|
| } |
| 697 |
28 |
| memset(di->image, 0, size); |
| 698 |
|
| |
| 699 |
28 |
| di->filename = malloc(strlen(name) + 1); |
| 700 |
28 |
| strcpy(di->filename, name); |
| 701 |
28 |
| di->openfiles = 0; |
| 702 |
28 |
| di->blocksfree = blocks_free(di); |
| 703 |
28 |
| di->modified = 1; |
| 704 |
28 |
| set_status(di, 254, 0, 0); |
| 705 |
28 |
| return(di); |
| 706 |
|
| } |
| 707 |
|
| |
| 708 |
|
| |
| 709 |
28 |
| void di_sync(DiskImage *di) { |
| 710 |
|
| FILE *file; |
| 711 |
|
| int l, left; |
| 712 |
|
| unsigned char *image; |
| 713 |
|
| |
| 714 |
28 |
▶ | if ((file = fopen(di->filename, "wb"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((file = fopen(di->filename, "wb"))) { |
28 |
0 |
28 |
|
| 715 |
28 |
| image = di->image; |
| 716 |
28 |
| left = di->size; |
| 717 |
28 |
| l = 0; |
| 718 |
56 |
▶ | while (left) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (left) { |
28 |
28 |
56 |
|
| 719 |
28 |
▶ | if ((l = fwrite(image, 1, left, file)) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((l = fwrite(image, 1, left, file)) == 0) { |
0 |
28 |
28 |
|
| 720 |
0 |
| fclose(file); |
| 721 |
0 |
| return; |
| 722 |
|
| } |
| 723 |
28 |
| left -= l; |
| 724 |
28 |
| image += l; |
| 725 |
|
| } |
| 726 |
28 |
| fclose(file); |
| 727 |
28 |
| di->modified = 0; |
| 728 |
|
| } |
| 729 |
|
| } |
| 730 |
|
| |
| 731 |
|
| |
| 732 |
29 |
| void di_free_image(DiskImage *di) { |
| 733 |
29 |
▶ | if (di->modified) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (di->modified) { |
27 |
2 |
29 |
|
| 734 |
27 |
| di_sync(di); |
| 735 |
|
| } |
| 736 |
29 |
▶ | if (di->filename) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (di->filename) { |
29 |
0 |
29 |
|
| 737 |
29 |
| free(di->filename); |
| 738 |
|
| } |
| 739 |
29 |
| free(di->image); |
| 740 |
29 |
| free(di); |
| 741 |
29 |
| } |
| 742 |
|
| |
| 743 |
|
| |
| 744 |
9 |
| int match_pattern(unsigned char *rawpattern, unsigned char *rawname) { |
| 745 |
|
| int i; |
| 746 |
|
| |
| 747 |
76 |
▶ | for (i = 0; i < 16; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; ++i) { |
76 |
0 |
76 |
|
| 748 |
76 |
▶ | if (rawpattern[i] == '*') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawpattern[i] == '*') { |
0 |
76 |
76 |
|
| 749 |
0 |
| return(1); |
| 750 |
|
| } |
| 751 |
76 |
▶ | if (rawname[i] == 0xa0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawname[i] == 0xa0) { |
8 |
68 |
76 |
|
| 752 |
8 |
▶ | if (rawpattern[i] == 0xa0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawpattern[i] == 0xa0) { |
8 |
0 |
8 |
|
| 753 |
8 |
| return(1); |
| 754 |
|
| } else { |
| 755 |
0 |
| return(0); |
| 756 |
|
| } |
| 757 |
|
| } else { |
| 758 |
68 |
▶ | if (rawpattern[i] == '?' || rawpattern[i] == rawname[i]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (rawpattern[i] == '?' || rawpattern[i] == rawname[i]) { |
68 |
0 |
68 |
| if (rawpattern[i] == '?' || rawpattern[i] == rawname[i]) { |
1 |
67 |
68 |
|
| 759 |
|
| } else { |
| 760 |
1 |
| return(0); |
| 761 |
|
| } |
| 762 |
|
| } |
| 763 |
|
| } |
| 764 |
0 |
| return(1); |
| 765 |
|
| } |
| 766 |
|
| |
| 767 |
|
| |
| 768 |
12 |
| RawDirEntry *find_file_entry(DiskImage *di, unsigned char *rawpattern, FileType type) { |
| 769 |
|
| unsigned char *buffer; |
| 770 |
|
| TrackSector ts; |
| 771 |
|
| RawDirEntry *rde; |
| 772 |
|
| int offset; |
| 773 |
|
| |
| 774 |
12 |
| ts = next_ts_in_chain(di, di->bam); |
| 775 |
16 |
▶ | while (ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ts.track) { |
12 |
4 |
16 |
|
| 776 |
12 |
| buffer = get_ts_addr(di, ts); |
| 777 |
44 |
▶ | for (offset = 0; offset < 256; offset += 32) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (offset = 0; offset < 256; offset += 32) { |
40 |
4 |
44 |
|
| 778 |
40 |
| rde = (RawDirEntry *)(buffer + offset); |
| 779 |
40 |
▶ | if ((rde->type & ~0x40) == (type | 0x80)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((rde->type & ~0x40) == (type | 0x80)) { |
9 |
31 |
40 |
|
| 780 |
9 |
▶ | if (match_pattern(rawpattern, rde->rawname)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (match_pattern(rawpattern, rde->rawname)) { |
8 |
1 |
9 |
|
| 781 |
8 |
| return(rde); |
| 782 |
|
| } |
| 783 |
|
| } |
| 784 |
|
| } |
| 785 |
4 |
| ts = next_ts_in_chain(di, ts); |
| 786 |
|
| } |
| 787 |
4 |
| return(NULL); |
| 788 |
|
| } |
| 789 |
|
| |
| 790 |
|
| |
| 791 |
12 |
| RawDirEntry *alloc_file_entry(DiskImage *di, unsigned char *rawname, FileType type) { |
| 792 |
|
| unsigned char *buffer; |
| 793 |
|
| TrackSector ts, lastts; |
| 794 |
|
| RawDirEntry *rde; |
| 795 |
|
| int offset; |
| 796 |
|
| |
| 797 |
|
| /* check if file already exists */ |
| 798 |
12 |
| ts = next_ts_in_chain(di, di->bam); |
| 799 |
24 |
▶ | while (ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ts.track) { |
12 |
12 |
24 |
|
| 800 |
12 |
| buffer = get_ts_addr(di, ts); |
| 801 |
108 |
▶ | for (offset = 0; offset < 256; offset += 32) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (offset = 0; offset < 256; offset += 32) { |
96 |
12 |
108 |
|
| 802 |
96 |
| rde = (RawDirEntry *)(buffer + offset); |
| 803 |
96 |
▶ | if (rde->type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rde->type) { |
0 |
96 |
96 |
|
| 804 |
0 |
▶ | if (strncmp(rawname, rde->rawname, 16) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(rawname, rde->rawname, 16) == 0) { |
0 |
0 |
0 |
|
| 805 |
0 |
| set_status(di, 63, 0, 0); |
| 806 |
|
| //puts("file exists"); |
| 807 |
0 |
| return(NULL); |
| 808 |
|
| } |
| 809 |
|
| } |
| 810 |
|
| } |
| 811 |
12 |
| ts = next_ts_in_chain(di, ts); |
| 812 |
|
| } |
| 813 |
|
| |
| 814 |
|
| /* allocate empty slot */ |
| 815 |
12 |
| ts = next_ts_in_chain(di, di->bam); |
| 816 |
12 |
▶ | while (ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ts.track) { |
12 |
0 |
12 |
|
| 817 |
12 |
| buffer = get_ts_addr(di, ts); |
| 818 |
12 |
▶ | for (offset = 0; offset < 256; offset += 32) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (offset = 0; offset < 256; offset += 32) { |
12 |
0 |
12 |
|
| 819 |
12 |
| rde = (RawDirEntry *)(buffer + offset); |
| 820 |
12 |
▶ | if (rde->type == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rde->type == 0) { |
12 |
0 |
12 |
|
| 821 |
12 |
| memset((unsigned char *)rde + 2, 0, 30); |
| 822 |
12 |
| memcpy(rde->rawname, rawname, 16); |
| 823 |
12 |
| rde->type = type; |
| 824 |
12 |
| di->modified = 1; |
| 825 |
12 |
| return(rde); |
| 826 |
|
| } |
| 827 |
|
| } |
| 828 |
0 |
| lastts = ts; |
| 829 |
0 |
| ts = next_ts_in_chain(di, ts); |
| 830 |
|
| } |
| 831 |
|
| |
| 832 |
|
| /* allocate new dir block */ |
| 833 |
0 |
| ts = alloc_next_dir_ts(di); |
| 834 |
0 |
▶ | if (ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track) { |
0 |
0 |
0 |
|
| 835 |
0 |
| rde = (RawDirEntry *)get_ts_addr(di, ts); |
| 836 |
0 |
| memset((unsigned char *)rde + 2, 0, 30); |
| 837 |
0 |
| memcpy(rde->rawname, rawname, 16); |
| 838 |
0 |
| rde->type = type; |
| 839 |
0 |
| di->modified = 1; |
| 840 |
0 |
| return(rde); |
| 841 |
|
| } else { |
| 842 |
0 |
| set_status(di, 72, 0, 0); |
| 843 |
|
| //puts("directory full"); |
| 844 |
0 |
| return(NULL); |
| 845 |
|
| } |
| 846 |
|
| } |
| 847 |
|
| |
| 848 |
|
| |
| 849 |
|
| /* open a file */ |
| 850 |
20 |
| ImageFile *di_open(DiskImage *di, unsigned char *rawname, FileType type, char *mode) { |
| 851 |
|
| ImageFile *imgfile; |
| 852 |
|
| RawDirEntry *rde; |
| 853 |
|
| unsigned char *p; |
| 854 |
|
| |
| 855 |
20 |
| set_status(di, 255, 0, 0); |
| 856 |
|
| |
| 857 |
20 |
▶ | if (strcmp("rb", mode) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp("rb", mode) == 0) { |
8 |
12 |
20 |
|
| 858 |
|
| |
| 859 |
8 |
▶ | if ((imgfile = malloc(sizeof(*imgfile))) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((imgfile = malloc(sizeof(*imgfile))) == NULL) { |
0 |
8 |
8 |
|
| 860 |
|
| //puts("malloc failed"); |
| 861 |
0 |
| return(NULL); |
| 862 |
|
| } |
| 863 |
8 |
▶ | if (strcmp("$", rawname) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp("$", rawname) == 0) { |
0 |
8 |
8 |
|
| 864 |
0 |
| imgfile->mode = 'r'; |
| 865 |
0 |
| imgfile->ts = di->dir; |
| 866 |
0 |
| p = get_ts_addr(di, di->dir); |
| 867 |
0 |
| imgfile->buffer = p + 2; |
| 868 |
0 |
▶ | if ((di->type == D64) || (di->type == D71)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((di->type == D64) || (di->type == D71)) { |
0 |
0 |
0 |
| if ((di->type == D64) || (di->type == D71)) { |
0 |
0 |
0 |
|
| 869 |
0 |
| imgfile->nextts.track = 18; // 1541/71 ignores bam t/s link |
| 870 |
0 |
| imgfile->nextts.sector = 1; |
| 871 |
|
| } else { |
| 872 |
0 |
| imgfile->nextts.track = p[0]; |
| 873 |
0 |
| imgfile->nextts.sector = p[1]; |
| 874 |
|
| } |
| 875 |
0 |
| imgfile->buflen = 254; |
| 876 |
0 |
| rde = NULL; |
| 877 |
|
| } else { |
| 878 |
8 |
▶ | if ((rde = find_file_entry(di, rawname, type)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((rde = find_file_entry(di, rawname, type)) == NULL) { |
3 |
5 |
8 |
|
| 879 |
3 |
| set_status(di, 62, 0, 0); |
| 880 |
|
| //puts("find_file_entry failed"); |
| 881 |
3 |
| free(imgfile); |
| 882 |
3 |
| return(NULL); |
| 883 |
|
| } |
| 884 |
5 |
| imgfile->mode = 'r'; |
| 885 |
5 |
| imgfile->ts = rde->startts; |
| 886 |
5 |
▶ | if (imgfile->ts.track > di_tracks(di->type)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->ts.track > di_tracks(di->type)) { |
0 |
5 |
5 |
|
| 887 |
0 |
| return(NULL); |
| 888 |
|
| } |
| 889 |
5 |
| p = get_ts_addr(di, rde->startts); |
| 890 |
5 |
| imgfile->buffer = p + 2; |
| 891 |
5 |
| imgfile->nextts.track = p[0]; |
| 892 |
5 |
| imgfile->nextts.sector = p[1]; |
| 893 |
5 |
▶ | if (imgfile->nextts.track == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->nextts.track == 0) { |
5 |
0 |
5 |
|
| 894 |
5 |
| imgfile->buflen = imgfile->nextts.sector - 1; |
| 895 |
|
| } else { |
| 896 |
0 |
| imgfile->buflen = 254; |
| 897 |
|
| } |
| 898 |
|
| } |
| 899 |
|
| |
| 900 |
12 |
▶ | } else if (strcmp("wb", mode) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strcmp("wb", mode) == 0) { |
12 |
0 |
12 |
|
| 901 |
|
| |
| 902 |
12 |
▶ | if ((rde = alloc_file_entry(di, rawname, type)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((rde = alloc_file_entry(di, rawname, type)) == NULL) { |
0 |
12 |
12 |
|
| 903 |
|
| //puts("alloc_file_entry failed"); |
| 904 |
0 |
| return(NULL); |
| 905 |
|
| } |
| 906 |
12 |
▶ | if ((imgfile = malloc(sizeof(*imgfile))) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((imgfile = malloc(sizeof(*imgfile))) == NULL) { |
0 |
12 |
12 |
|
| 907 |
|
| //puts("malloc failed"); |
| 908 |
0 |
| return(NULL); |
| 909 |
|
| } |
| 910 |
12 |
| imgfile->mode = 'w'; |
| 911 |
12 |
| imgfile->ts.track = 0; |
| 912 |
12 |
| imgfile->ts.sector = 0; |
| 913 |
12 |
▶ | if ((imgfile->buffer = malloc(254)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((imgfile->buffer = malloc(254)) == NULL) { |
0 |
12 |
12 |
|
| 914 |
0 |
| free(imgfile); |
| 915 |
|
| //puts("malloc failed"); |
| 916 |
0 |
| return(NULL); |
| 917 |
|
| } |
| 918 |
12 |
| imgfile->buflen = 254; |
| 919 |
12 |
| di->modified = 1; |
| 920 |
|
| |
| 921 |
|
| } else { |
| 922 |
0 |
| return(NULL); |
| 923 |
|
| } |
| 924 |
|
| |
| 925 |
17 |
| imgfile->diskimage = di; |
| 926 |
17 |
| imgfile->rawdirentry = rde; |
| 927 |
17 |
| imgfile->position = 0; |
| 928 |
17 |
| imgfile->bufptr = 0; |
| 929 |
|
| |
| 930 |
17 |
| ++(di->openfiles); |
| 931 |
17 |
| set_status(di, 0, 0, 0); |
| 932 |
17 |
| return(imgfile); |
| 933 |
|
| } |
| 934 |
|
| |
| 935 |
|
| |
| 936 |
3 |
| int di_read(ImageFile *imgfile, unsigned char *buffer, int len) { |
| 937 |
|
| unsigned char *p; |
| 938 |
|
| int bytesleft; |
| 939 |
3 |
| int counter = 0; |
| 940 |
|
| |
| 941 |
6 |
▶ | while (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len) { |
5 |
1 |
6 |
|
| 942 |
5 |
| bytesleft = imgfile->buflen - imgfile->bufptr; |
| 943 |
5 |
▶ | if (bytesleft == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bytesleft == 0) { |
2 |
3 |
5 |
|
| 944 |
2 |
▶ | if (imgfile->nextts.track == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->nextts.track == 0) { |
2 |
0 |
2 |
|
| 945 |
2 |
| return(counter); |
| 946 |
|
| } |
| 947 |
0 |
▶ | if (((imgfile->diskimage->type == D64) || (imgfile->diskimage->type == D71)) && imgfile->ts.track == 18 && imgfile->ts.sector == 0) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (((imgfile->diskimage->type == D64) || (imgfile->diskimage->type == D71)) && imgfile->ts.track == 18 && imgfile->ts.sector == 0) { |
0 |
0 |
0 |
| if (((imgfile->diskimage->type == D64) || (imgfile->diskimage->type == D71)) && imgfile->ts.track == 18 && imgfile->ts.sector == 0) { |
0 |
0 |
0 |
| if (((imgfile->diskimage->type == D64) || (imgfile->diskimage->type == D71)) && imgfile->ts.track == 18 && imgfile->ts.sector == 0) { |
0 |
0 |
0 |
| if (((imgfile->diskimage->type == D64) || (imgfile->diskimage->type == D71)) && imgfile->ts.track == 18 && imgfile->ts.sector == 0) { |
0 |
0 |
0 |
|
| 948 |
0 |
| imgfile->ts.track = 18; |
| 949 |
0 |
| imgfile->ts.sector = 1; |
| 950 |
|
| } else { |
| 951 |
0 |
| imgfile->ts = next_ts_in_chain(imgfile->diskimage, imgfile->ts); |
| 952 |
|
| } |
| 953 |
0 |
▶ | if (imgfile->ts.track == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->ts.track == 0) { |
0 |
0 |
0 |
|
| 954 |
0 |
| return(counter); |
| 955 |
|
| } |
| 956 |
0 |
| p = get_ts_addr(imgfile->diskimage, imgfile->ts); |
| 957 |
0 |
| imgfile->buffer = p + 2; |
| 958 |
0 |
| imgfile->nextts.track = p[0]; |
| 959 |
0 |
| imgfile->nextts.sector = p[1]; |
| 960 |
0 |
▶ | if (imgfile->nextts.track == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->nextts.track == 0) { |
0 |
0 |
0 |
|
| 961 |
0 |
| imgfile->buflen = imgfile->nextts.sector - 1; |
| 962 |
|
| } else { |
| 963 |
0 |
| imgfile->buflen = 254; |
| 964 |
|
| } |
| 965 |
0 |
| imgfile->bufptr = 0; |
| 966 |
|
| } else { |
| 967 |
3 |
▶ | if (len >= bytesleft) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= bytesleft) { |
2 |
1 |
3 |
|
| 968 |
510 |
▶ | while (bytesleft) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (bytesleft) { |
508 |
2 |
510 |
|
| 969 |
508 |
| *buffer++ = imgfile->buffer[imgfile->bufptr++]; |
| 970 |
508 |
| --len; |
| 971 |
508 |
| --bytesleft; |
| 972 |
508 |
| ++counter; |
| 973 |
508 |
| ++(imgfile->position); |
| 974 |
|
| } |
| 975 |
|
| } else { |
| 976 |
6 |
▶ | while (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len) { |
5 |
1 |
6 |
|
| 977 |
5 |
| *buffer++ = imgfile->buffer[imgfile->bufptr++]; |
| 978 |
5 |
| --len; |
| 979 |
5 |
| --bytesleft; |
| 980 |
5 |
| ++counter; |
| 981 |
5 |
| ++(imgfile->position); |
| 982 |
|
| } |
| 983 |
|
| } |
| 984 |
|
| } |
| 985 |
|
| } |
| 986 |
1 |
| return(counter); |
| 987 |
|
| } |
| 988 |
|
| |
| 989 |
|
| |
| 990 |
12 |
| int di_write(ImageFile *imgfile, unsigned char *buffer, int len) { |
| 991 |
|
| unsigned char *p; |
| 992 |
|
| int bytesleft; |
| 993 |
12 |
| int counter = 0; |
| 994 |
|
| |
| 995 |
24 |
▶ | while (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len) { |
12 |
12 |
24 |
|
| 996 |
12 |
| bytesleft = imgfile->buflen - imgfile->bufptr; |
| 997 |
12 |
▶ | if (bytesleft == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bytesleft == 0) { |
0 |
12 |
12 |
|
| 998 |
0 |
▶ | if (imgfile->diskimage->blocksfree == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->diskimage->blocksfree == 0) { |
0 |
0 |
0 |
|
| 999 |
0 |
| set_status(imgfile->diskimage, 72, 0, 0); |
| 1000 |
0 |
| return(counter); |
| 1001 |
|
| } |
| 1002 |
0 |
| imgfile->nextts = alloc_next_ts(imgfile->diskimage, imgfile->ts); |
| 1003 |
0 |
▶ | if (imgfile->ts.track == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->ts.track == 0) { |
0 |
0 |
0 |
|
| 1004 |
0 |
| imgfile->rawdirentry->startts = imgfile->nextts; |
| 1005 |
|
| } else { |
| 1006 |
0 |
| p = get_ts_addr(imgfile->diskimage, imgfile->ts); |
| 1007 |
0 |
| p[0] = imgfile->nextts.track; |
| 1008 |
0 |
| p[1] = imgfile->nextts.sector; |
| 1009 |
|
| } |
| 1010 |
0 |
| imgfile->ts = imgfile->nextts; |
| 1011 |
0 |
| p = get_ts_addr(imgfile->diskimage, imgfile->ts); |
| 1012 |
0 |
| p[0] = 0; |
| 1013 |
0 |
| p[1] = 0xff; |
| 1014 |
0 |
| memcpy(p + 2, imgfile->buffer, 254); |
| 1015 |
0 |
| imgfile->bufptr = 0; |
| 1016 |
0 |
▶ | if (++(imgfile->rawdirentry->sizelo) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++(imgfile->rawdirentry->sizelo) == 0) { |
0 |
0 |
0 |
|
| 1017 |
0 |
| ++(imgfile->rawdirentry->sizehi); |
| 1018 |
|
| } |
| 1019 |
0 |
| --(imgfile->diskimage->blocksfree); |
| 1020 |
|
| } else { |
| 1021 |
12 |
▶ | if (len >= bytesleft) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= bytesleft) { |
0 |
12 |
12 |
|
| 1022 |
0 |
▶ | while (bytesleft) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (bytesleft) { |
0 |
0 |
0 |
|
| 1023 |
0 |
| imgfile->buffer[imgfile->bufptr++] = *buffer++; |
| 1024 |
0 |
| --len; |
| 1025 |
0 |
| --bytesleft; |
| 1026 |
0 |
| ++counter; |
| 1027 |
0 |
| ++(imgfile->position); |
| 1028 |
|
| } |
| 1029 |
|
| } else { |
| 1030 |
110 |
▶ | while (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len) { |
98 |
12 |
110 |
|
| 1031 |
98 |
| imgfile->buffer[imgfile->bufptr++] = *buffer++; |
| 1032 |
98 |
| --len; |
| 1033 |
98 |
| --bytesleft; |
| 1034 |
98 |
| ++counter; |
| 1035 |
98 |
| ++(imgfile->position); |
| 1036 |
|
| } |
| 1037 |
|
| } |
| 1038 |
|
| } |
| 1039 |
|
| } |
| 1040 |
12 |
| return(counter); |
| 1041 |
|
| } |
| 1042 |
|
| |
| 1043 |
|
| |
| 1044 |
16 |
| void di_close(ImageFile *imgfile) { |
| 1045 |
|
| unsigned char *p; |
| 1046 |
|
| |
| 1047 |
16 |
▶ | if (imgfile->mode == 'w') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->mode == 'w') { |
12 |
4 |
16 |
|
| 1048 |
12 |
▶ | if (imgfile->bufptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->bufptr) { |
12 |
0 |
12 |
|
| 1049 |
12 |
▶ | if (imgfile->diskimage->blocksfree) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->diskimage->blocksfree) { |
12 |
0 |
12 |
|
| 1050 |
12 |
| imgfile->nextts = alloc_next_ts(imgfile->diskimage, imgfile->ts); |
| 1051 |
12 |
▶ | if (imgfile->ts.track == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (imgfile->ts.track == 0) { |
12 |
0 |
12 |
|
| 1052 |
12 |
| imgfile->rawdirentry->startts = imgfile->nextts; |
| 1053 |
|
| } else { |
| 1054 |
0 |
| p = get_ts_addr(imgfile->diskimage, imgfile->ts); |
| 1055 |
0 |
| p[0] = imgfile->nextts.track; |
| 1056 |
0 |
| p[1] = imgfile->nextts.sector; |
| 1057 |
|
| } |
| 1058 |
12 |
| imgfile->ts = imgfile->nextts; |
| 1059 |
12 |
| p = get_ts_addr(imgfile->diskimage, imgfile->ts); |
| 1060 |
12 |
| p[0] = 0; |
| 1061 |
12 |
| p[1] = 0xff; |
| 1062 |
12 |
| memcpy(p + 2, imgfile->buffer, 254); |
| 1063 |
12 |
| imgfile->bufptr = 0; |
| 1064 |
12 |
▶ | if (++(imgfile->rawdirentry->sizelo) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++(imgfile->rawdirentry->sizelo) == 0) { |
0 |
12 |
12 |
|
| 1065 |
0 |
| ++(imgfile->rawdirentry->sizehi); |
| 1066 |
|
| } |
| 1067 |
12 |
| --(imgfile->diskimage->blocksfree); |
| 1068 |
12 |
| imgfile->rawdirentry->type |= 0x80; |
| 1069 |
|
| } |
| 1070 |
|
| } else { |
| 1071 |
0 |
| imgfile->rawdirentry->type |= 0x80; |
| 1072 |
|
| } |
| 1073 |
12 |
| free(imgfile->buffer); |
| 1074 |
|
| } |
| 1075 |
16 |
| --(imgfile->diskimage->openfiles); |
| 1076 |
16 |
| free(imgfile); |
| 1077 |
16 |
| } |
| 1078 |
|
| |
| 1079 |
|
| |
| 1080 |
23 |
| int di_format(DiskImage *di, unsigned char *rawname, unsigned char *rawid) { |
| 1081 |
|
| unsigned char *p; |
| 1082 |
|
| TrackSector ts; |
| 1083 |
|
| |
| 1084 |
23 |
| di->modified = 1; |
| 1085 |
|
| |
| 1086 |
23 |
| switch (di->type) { |
| 1087 |
|
| |
| 1088 |
23 |
| case D64: |
| 1089 |
|
| /* erase disk */ |
| 1090 |
23 |
▶ | if (rawid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawid) { |
23 |
0 |
23 |
|
| 1091 |
23 |
| memset(di->image, 0, 174848); |
| 1092 |
|
| } |
| 1093 |
|
| |
| 1094 |
|
| /* get ptr to bam */ |
| 1095 |
23 |
| p = get_ts_addr(di, di->bam); |
| 1096 |
|
| |
| 1097 |
|
| /* setup header */ |
| 1098 |
23 |
| p[0] = 18; |
| 1099 |
23 |
| p[1] = 1; |
| 1100 |
23 |
| p[2] = 'A'; |
| 1101 |
23 |
| p[3] = 0; |
| 1102 |
|
| |
| 1103 |
|
| /* clear bam */ |
| 1104 |
23 |
| memset(p + 4, 0, 0x8c); |
| 1105 |
|
| |
| 1106 |
|
| /* free blocks */ |
| 1107 |
828 |
▶ | for (ts.track = 1; ts.track <= di_tracks(di->type); ++ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ts.track = 1; ts.track <= di_tracks(di->type); ++ts.track) { |
805 |
23 |
828 |
|
| 1108 |
16514 |
▶ | for (ts.sector = 0; ts.sector < di_sectors_per_track(di->type, ts.track); ++ts.sector) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ts.sector = 0; ts.sector < di_sectors_per_track(di->type, ts.track); ++ts.sector) { |
15709 |
805 |
16514 |
|
| 1109 |
15709 |
| di_free_ts(di, ts); |
| 1110 |
|
| } |
| 1111 |
|
| } |
| 1112 |
|
| |
| 1113 |
|
| /* allocate bam and dir */ |
| 1114 |
23 |
| ts.track = 18; |
| 1115 |
23 |
| ts.sector = 0; |
| 1116 |
23 |
| di_alloc_ts(di, ts); |
| 1117 |
23 |
| ts.sector = 1; |
| 1118 |
23 |
| di_alloc_ts(di, ts); |
| 1119 |
|
| |
| 1120 |
|
| /* copy name */ |
| 1121 |
23 |
| memcpy(p + 0x90, rawname, 16); |
| 1122 |
|
| |
| 1123 |
|
| /* set id */ |
| 1124 |
23 |
| memset(p + 0xa0, 0xa0, 2); |
| 1125 |
23 |
▶ | if (rawid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawid) { |
23 |
0 |
23 |
|
| 1126 |
23 |
| memcpy(p + 0xa2, rawid, 2); |
| 1127 |
|
| } |
| 1128 |
23 |
| memset(p + 0xa4, 0xa0, 7); |
| 1129 |
23 |
| p[0xa5] = '2'; |
| 1130 |
23 |
| p[0xa6] = 'A'; |
| 1131 |
|
| |
| 1132 |
|
| /* clear unused bytes */ |
| 1133 |
23 |
| memset(p + 0xab, 0, 0x55); |
| 1134 |
|
| |
| 1135 |
|
| /* clear first dir block */ |
| 1136 |
23 |
| memset(p + 256, 0, 256); |
| 1137 |
23 |
| p[257] = 0xff; |
| 1138 |
23 |
| break; |
| 1139 |
|
| |
| 1140 |
0 |
| case D71: |
| 1141 |
|
| /* erase disk */ |
| 1142 |
0 |
▶ | if (rawid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawid) { |
0 |
0 |
0 |
|
| 1143 |
0 |
| memset(di->image, 0, 349696); |
| 1144 |
|
| } |
| 1145 |
|
| |
| 1146 |
|
| /* get ptr to bam2 */ |
| 1147 |
0 |
| p = get_ts_addr(di, di->bam2); |
| 1148 |
|
| |
| 1149 |
|
| /* clear bam2 */ |
| 1150 |
0 |
| memset(p, 0, 256); |
| 1151 |
|
| |
| 1152 |
|
| /* get ptr to bam */ |
| 1153 |
0 |
| p = get_ts_addr(di, di->bam); |
| 1154 |
|
| |
| 1155 |
|
| /* setup header */ |
| 1156 |
0 |
| p[0] = 18; |
| 1157 |
0 |
| p[1] = 1; |
| 1158 |
0 |
| p[2] = 'A'; |
| 1159 |
0 |
| p[3] = 0x80; |
| 1160 |
|
| |
| 1161 |
|
| /* clear bam */ |
| 1162 |
0 |
| memset(p + 4, 0, 0x8c); |
| 1163 |
|
| |
| 1164 |
|
| /* clear bam2 counters */ |
| 1165 |
0 |
| memset(p + 0xdd, 0, 0x23); |
| 1166 |
|
| |
| 1167 |
|
| /* free blocks */ |
| 1168 |
0 |
▶ | for (ts.track = 1; ts.track <= di_tracks(di->type); ++ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ts.track = 1; ts.track <= di_tracks(di->type); ++ts.track) { |
0 |
0 |
0 |
|
| 1169 |
0 |
▶ | if (ts.track != 53) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.track != 53) { |
0 |
0 |
0 |
|
| 1170 |
0 |
▶ | for (ts.sector = 0; ts.sector < di_sectors_per_track(di->type, ts.track); ++ts.sector) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ts.sector = 0; ts.sector < di_sectors_per_track(di->type, ts.track); ++ts.sector) { |
0 |
0 |
0 |
|
| 1171 |
0 |
| di_free_ts(di, ts); |
| 1172 |
|
| } |
| 1173 |
|
| } |
| 1174 |
|
| } |
| 1175 |
|
| |
| 1176 |
|
| /* allocate bam and dir */ |
| 1177 |
0 |
| ts.track = 18; |
| 1178 |
0 |
| ts.sector = 0; |
| 1179 |
0 |
| di_alloc_ts(di, ts); |
| 1180 |
0 |
| ts.sector = 1; |
| 1181 |
0 |
| di_alloc_ts(di, ts); |
| 1182 |
|
| |
| 1183 |
|
| /* copy name */ |
| 1184 |
0 |
| memcpy(p + 0x90, rawname, 16); |
| 1185 |
|
| |
| 1186 |
|
| /* set id */ |
| 1187 |
0 |
| memset(p + 0xa0, 0xa0, 2); |
| 1188 |
0 |
▶ | if (rawid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawid) { |
0 |
0 |
0 |
|
| 1189 |
0 |
| memcpy(p + 0xa2, rawid, 2); |
| 1190 |
|
| } |
| 1191 |
0 |
| memset(p + 0xa4, 0xa0, 7); |
| 1192 |
0 |
| p[0xa5] = '2'; |
| 1193 |
0 |
| p[0xa6] = 'A'; |
| 1194 |
|
| |
| 1195 |
|
| /* clear unused bytes */ |
| 1196 |
0 |
| memset(p + 0xab, 0, 0x32); |
| 1197 |
|
| |
| 1198 |
|
| /* clear first dir block */ |
| 1199 |
0 |
| memset(p + 256, 0, 256); |
| 1200 |
0 |
| p[257] = 0xff; |
| 1201 |
0 |
| break; |
| 1202 |
|
| |
| 1203 |
0 |
| case D81: |
| 1204 |
|
| /* erase disk */ |
| 1205 |
0 |
▶ | if (rawid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawid) { |
0 |
0 |
0 |
|
| 1206 |
0 |
| memset(di->image, 0, 819200); |
| 1207 |
|
| } |
| 1208 |
|
| |
| 1209 |
|
| /* get ptr to bam */ |
| 1210 |
0 |
| p = get_ts_addr(di, di->bam); |
| 1211 |
|
| |
| 1212 |
|
| /* setup header */ |
| 1213 |
0 |
| p[0] = 0x28; |
| 1214 |
0 |
| p[1] = 0x02; |
| 1215 |
0 |
| p[2] = 0x44; |
| 1216 |
0 |
| p[3] = 0xbb; |
| 1217 |
0 |
| p[6] = 0xc0; |
| 1218 |
|
| |
| 1219 |
|
| /* set id */ |
| 1220 |
0 |
▶ | if (rawid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawid) { |
0 |
0 |
0 |
|
| 1221 |
0 |
| memcpy(p + 4, rawid, 2); |
| 1222 |
|
| } |
| 1223 |
|
| |
| 1224 |
|
| /* clear bam */ |
| 1225 |
0 |
| memset(p + 7, 0, 0xfa); |
| 1226 |
|
| |
| 1227 |
|
| /* get ptr to bam2 */ |
| 1228 |
0 |
| p = get_ts_addr(di, di->bam2); |
| 1229 |
|
| |
| 1230 |
|
| /* setup header */ |
| 1231 |
0 |
| p[0] = 0x00; |
| 1232 |
0 |
| p[1] = 0xff; |
| 1233 |
0 |
| p[2] = 0x44; |
| 1234 |
0 |
| p[3] = 0xbb; |
| 1235 |
0 |
| p[6] = 0xc0; |
| 1236 |
|
| |
| 1237 |
|
| /* set id */ |
| 1238 |
0 |
▶ | if (rawid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawid) { |
0 |
0 |
0 |
|
| 1239 |
0 |
| memcpy(p + 4, rawid, 2); |
| 1240 |
|
| } |
| 1241 |
|
| |
| 1242 |
|
| /* clear bam2 */ |
| 1243 |
0 |
| memset(p + 7, 0, 0xfa); |
| 1244 |
|
| |
| 1245 |
|
| /* free blocks */ |
| 1246 |
0 |
▶ | for (ts.track = 1; ts.track <= di_tracks(di->type); ++ts.track) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ts.track = 1; ts.track <= di_tracks(di->type); ++ts.track) { |
0 |
0 |
0 |
|
| 1247 |
0 |
▶ | for (ts.sector = 0; ts.sector < di_sectors_per_track(di->type, ts.track); ++ts.sector) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ts.sector = 0; ts.sector < di_sectors_per_track(di->type, ts.track); ++ts.sector) { |
0 |
0 |
0 |
|
| 1248 |
0 |
| di_free_ts(di, ts); |
| 1249 |
|
| } |
| 1250 |
|
| } |
| 1251 |
|
| |
| 1252 |
|
| /* allocate bam and dir */ |
| 1253 |
0 |
| ts.track = 40; |
| 1254 |
0 |
| ts.sector = 0; |
| 1255 |
0 |
| di_alloc_ts(di, ts); |
| 1256 |
0 |
| ts.sector = 1; |
| 1257 |
0 |
| di_alloc_ts(di, ts); |
| 1258 |
0 |
| ts.sector = 2; |
| 1259 |
0 |
| di_alloc_ts(di, ts); |
| 1260 |
0 |
| ts.sector = 3; |
| 1261 |
0 |
| di_alloc_ts(di, ts); |
| 1262 |
|
| |
| 1263 |
|
| /* get ptr to dir */ |
| 1264 |
0 |
| p = get_ts_addr(di, di->dir); |
| 1265 |
|
| |
| 1266 |
|
| /* copy name */ |
| 1267 |
0 |
| memcpy(p + 4, rawname, 16); |
| 1268 |
|
| |
| 1269 |
|
| /* set id */ |
| 1270 |
0 |
| memset(p + 0x14, 0xa0, 2); |
| 1271 |
0 |
▶ | if (rawid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rawid) { |
0 |
0 |
0 |
|
| 1272 |
0 |
| memcpy(p + 0x16, rawid, 2); |
| 1273 |
|
| } |
| 1274 |
0 |
| memset(p + 0x18, 0xa0, 5); |
| 1275 |
0 |
| p[0x19] = '3'; |
| 1276 |
0 |
| p[0x1a] = 'D'; |
| 1277 |
|
| |
| 1278 |
|
| /* clear unused bytes */ |
| 1279 |
0 |
| memset(p + 0x1d, 0, 0xe3); |
| 1280 |
|
| |
| 1281 |
|
| /* clear first dir block */ |
| 1282 |
0 |
| memset(p + 768, 0, 256); |
| 1283 |
0 |
| p[769] = 0xff; |
| 1284 |
0 |
| break; |
| 1285 |
|
| |
| 1286 |
|
| } |
| 1287 |
|
| |
| 1288 |
23 |
| di->blocksfree = blocks_free(di); |
| 1289 |
|
| |
| 1290 |
23 |
| return(set_status(di, 0, 0, 0)); |
| 1291 |
|
| } |
| 1292 |
|
| |
| 1293 |
|
| |
| 1294 |
1 |
| int di_delete(DiskImage *di, unsigned char *rawpattern, FileType type) { |
| 1295 |
|
| RawDirEntry *rde; |
| 1296 |
1 |
| int delcount = 0; |
| 1297 |
|
| |
| 1298 |
1 |
▶ | switch (type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (type) { |
1 |
0 |
1 |
|
| 1299 |
|
| |
| 1300 |
1 |
| case T_SEQ: |
| 1301 |
|
| case T_PRG: |
| 1302 |
|
| case T_USR: |
| 1303 |
2 |
▶ | while ((rde = find_file_entry(di, rawpattern, type))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((rde = find_file_entry(di, rawpattern, type))) { |
1 |
1 |
2 |
|
| 1304 |
1 |
| free_chain(di, rde->startts); |
| 1305 |
1 |
| rde->type = 0; |
| 1306 |
1 |
| ++delcount; |
| 1307 |
|
| } |
| 1308 |
1 |
▶ | if (delcount) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (delcount) { |
1 |
0 |
1 |
|
| 1309 |
1 |
| return(set_status(di, 1, delcount, 0)); |
| 1310 |
|
| } else { |
| 1311 |
0 |
| return(set_status(di, 62, 0, 0)); |
| 1312 |
|
| } |
| 1313 |
|
| break; |
| 1314 |
|
| |
| 1315 |
0 |
| default: |
| 1316 |
0 |
| return(set_status(di, 64, 0, 0)); |
| 1317 |
|
| break; |
| 1318 |
|
| |
| 1319 |
|
| } |
| 1320 |
|
| } |
| 1321 |
|
| |
| 1322 |
|
| |
| 1323 |
2 |
| int di_rename(DiskImage *di, unsigned char *oldrawname, unsigned char *newrawname, FileType type) { |
| 1324 |
|
| RawDirEntry *rde; |
| 1325 |
|
| |
| 1326 |
2 |
▶ | if ((rde = find_file_entry(di, oldrawname, type))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((rde = find_file_entry(di, oldrawname, type))) { |
2 |
0 |
2 |
|
| 1327 |
2 |
| memcpy(rde->rawname, newrawname, 16); |
| 1328 |
2 |
| return(set_status(di, 0, 0, 0)); |
| 1329 |
|
| } else { |
| 1330 |
0 |
| return(set_status(di, 62, 0, 0)); |
| 1331 |
|
| } |
| 1332 |
|
| } |