src/lib/formats/ipf_dsk.c
| r0 | r13029 | |
| 1 | #include "emu.h" |
| 2 | #include "ipf_dsk.h" |
| 3 | |
| 4 | const floppy_format_type FLOPPY_IPF_FORMAT = &floppy_image_format_creator<ipf_format>; |
| 5 | |
| 6 | ipf_format::ipf_format() |
| 7 | { |
| 8 | } |
| 9 | |
| 10 | const char *ipf_format::name() const |
| 11 | { |
| 12 | return "ipf"; |
| 13 | } |
| 14 | |
| 15 | const char *ipf_format::description() const |
| 16 | { |
| 17 | return "SPS floppy disk image"; |
| 18 | } |
| 19 | |
| 20 | const char *ipf_format::extensions() const |
| 21 | { |
| 22 | return "ipf"; |
| 23 | } |
| 24 | |
| 25 | bool ipf_format::supports_save() const |
| 26 | { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | int ipf_format::identify(io_generic *io) |
| 31 | { |
| 32 | static const UINT8 refh[12] = { 0x43, 0x41, 0x50, 0x53, 0x00, 0x00, 0x00, 0x0c, 0x1c, 0xd5, 0x73, 0xba }; |
| 33 | UINT8 h[12]; |
| 34 | io_generic_read(io, h, 0, 12); |
| 35 | |
| 36 | if(!memcmp(h, refh, 12)) |
| 37 | return 100; |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | bool ipf_format::load(io_generic *io, floppy_image *image) |
| 43 | { |
| 44 | UINT32 size = io_generic_size(io); |
| 45 | UINT8 *data = global_alloc_array(UINT8, size); |
| 46 | io_generic_read(io, data, 0, size); |
| 47 | bool res = parse(data, size, image); |
| 48 | global_free(data); |
| 49 | return res; |
| 50 | } |
| 51 | |
| 52 | UINT32 ipf_format::r32(const UINT8 *p) |
| 53 | { |
| 54 | return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | UINT32 ipf_format::rb(const UINT8 *&p, int count) |
| 59 | { |
| 60 | UINT32 v = 0; |
| 61 | for(int i=0; i<count; i++) |
| 62 | v = (v << 8) | *p++; |
| 63 | return v; |
| 64 | } |
| 65 | |
| 66 | UINT32 ipf_format::crc32r(const UINT8 *data, UINT32 size) |
| 67 | { |
| 68 | // Reversed crc32 |
| 69 | UINT32 crc = 0xffffffff; |
| 70 | for(UINT32 i=0; i != size; i++) { |
| 71 | crc = crc ^ data[i]; |
| 72 | for(int j=0; j<8; j++) |
| 73 | if(crc & 1) |
| 74 | crc = (crc >> 1) ^ 0xedb88320; |
| 75 | else |
| 76 | crc = crc >> 1; |
| 77 | } |
| 78 | return ~crc; |
| 79 | } |
| 80 | |
| 81 | bool ipf_format::parse(UINT8 *data, UINT32 size, floppy_image *image) |
| 82 | { |
| 83 | tcount = 84*2+1; // Usual max |
| 84 | tinfos = global_alloc_array_clear(track_info, tcount); |
| 85 | bool res = scan_all_tags(data, size); |
| 86 | if(res) |
| 87 | res = generate_tracks(image); |
| 88 | global_free(tinfos); |
| 89 | return res; |
| 90 | } |
| 91 | |
| 92 | bool ipf_format::parse_info(const UINT8 *info) |
| 93 | { |
| 94 | type = r32(info+12); |
| 95 | if(type != 1) |
| 96 | return false; |
| 97 | f4 = r32(info+16); // Usually 2 |
| 98 | f8 = r32(info+20); // Usually 1 |
| 99 | release = r32(info+24); |
| 100 | revision = r32(info+28); |
| 101 | f14 = r32(info+32); // Perhaps some kind of (unchecked) checksum |
| 102 | min_cylinder = r32(info+36); |
| 103 | max_cylinder = r32(info+40); |
| 104 | min_head = r32(info+44); |
| 105 | max_head = r32(info+48); |
| 106 | credit_day = r32(info+52); // year*1e4 + month*1e2 + day |
| 107 | credit_time = r32(info+56); // hour*1e7 + min*1e5 + sec*1e3 + msec |
| 108 | for(int i=0; i<4; i++) |
| 109 | platform[i] = r32(info+60+4*i); |
| 110 | for(int i=0; i<5; i++) |
| 111 | extra[i] = r32(info+76+4*i); |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | ipf_format::track_info *ipf_format::get_index(UINT32 idx) |
| 116 | { |
| 117 | if(idx > 1000) |
| 118 | return 0; |
| 119 | if(idx >= tcount) { |
| 120 | track_info *ti1 = global_alloc_array_clear(track_info, idx+1); |
| 121 | memcpy(ti1, tinfos, tcount*sizeof(tinfos)); |
| 122 | global_free(tinfos); |
| 123 | tcount = idx+1; |
| 124 | tinfos = ti1; |
| 125 | } |
| 126 | |
| 127 | return tinfos+idx; |
| 128 | } |
| 129 | |
| 130 | bool ipf_format::parse_imge(const UINT8 *imge) |
| 131 | { |
| 132 | track_info *t = get_index(r32(imge+64)); |
| 133 | if(!t) |
| 134 | return false; |
| 135 | |
| 136 | t->info_set = true; |
| 137 | |
| 138 | t->cylinder = r32(imge+12); |
| 139 | if(t->cylinder < min_cylinder || t->cylinder > max_cylinder) |
| 140 | return false; |
| 141 | |
| 142 | t->head = r32(imge+16); |
| 143 | if(t->head < min_head || t->head > max_head) |
| 144 | return false; |
| 145 | |
| 146 | t->type = r32(imge+20); |
| 147 | t->t12 = r32(imge+24); // Usually 1 |
| 148 | t->size_bytes = r32(imge+28); |
| 149 | t->index_bytes = r32(imge+32); |
| 150 | t->index_cells = r32(imge+36); |
| 151 | t->datasize_cells = r32(imge+40); |
| 152 | t->gapsize_cells = r32(imge+44); |
| 153 | t->size_cells = r32(imge+48); |
| 154 | t->block_count = r32(imge+52); |
| 155 | t->t44 = r32(imge+56); // Usually 0 |
| 156 | t->weak_bits = r32(imge+60); |
| 157 | t->t56 = r32(imge+68); |
| 158 | t->t60 = r32(imge+72); |
| 159 | t->t64 = r32(imge+76); |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool ipf_format::parse_data(const UINT8 *data, UINT32 &pos, UINT32 max_extra_size) |
| 165 | { |
| 166 | track_info *t = get_index(r32(data+24)); |
| 167 | if(!t) |
| 168 | return false; |
| 169 | |
| 170 | t->d4 = r32(data+16); // Somehow related to the number of data cells |
| 171 | t->data = data+28; |
| 172 | t->data_size = r32(data+12); |
| 173 | if(t->data_size > max_extra_size) |
| 174 | return false; |
| 175 | if(crc32r(t->data, t->data_size) != r32(data+20)) |
| 176 | return false; |
| 177 | pos += t->data_size; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | bool ipf_format::scan_one_tag(UINT8 *data, UINT32 size, UINT32 &pos, UINT8 *&tag, UINT32 &tsize) |
| 182 | { |
| 183 | if(size-pos < 12) |
| 184 | return false; |
| 185 | tag = data+pos; |
| 186 | tsize = r32(tag+4); |
| 187 | if(size-pos < tsize) |
| 188 | return false; |
| 189 | UINT32 crc = r32(tag+8); |
| 190 | tag[8] = tag[9] = tag[10] = tag[11] = 0; |
| 191 | if(crc32r(tag, tsize) != crc) |
| 192 | return false; |
| 193 | pos += tsize; |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | bool ipf_format::scan_all_tags(UINT8 *data, UINT32 size) |
| 198 | { |
| 199 | UINT32 pos = 0; |
| 200 | while(pos != size) { |
| 201 | UINT8 *tag; |
| 202 | UINT32 tsize; |
| 203 | |
| 204 | if(!scan_one_tag(data, size, pos, tag, tsize)) |
| 205 | return false; |
| 206 | |
| 207 | switch(r32(tag)) { |
| 208 | case 0x43415053: // CAPS |
| 209 | if(tsize != 12) |
| 210 | return false; |
| 211 | break; |
| 212 | |
| 213 | case 0x494e464f: // INFO |
| 214 | if(tsize != 96) |
| 215 | return false; |
| 216 | if(!parse_info(tag)) |
| 217 | return false; |
| 218 | break; |
| 219 | |
| 220 | case 0x494d4745: // IMGE |
| 221 | if(tsize != 80) |
| 222 | return false; |
| 223 | if(!parse_imge(tag)) |
| 224 | return false; |
| 225 | break; |
| 226 | |
| 227 | case 0x44415441: // DATA |
| 228 | if(tsize != 28) |
| 229 | return false; |
| 230 | if(!parse_data(tag, pos, size-pos)) |
| 231 | return false; |
| 232 | break; |
| 233 | |
| 234 | default: |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | bool ipf_format::generate_tracks(floppy_image *image) |
| 242 | { |
| 243 | for(UINT32 i = 0; i != tcount; i++) { |
| 244 | track_info *t = tinfos + i; |
| 245 | if(t->info_set && t->data) { |
| 246 | if(!generate_track(t, image)) |
| 247 | return false; |
| 248 | |
| 249 | } else if(t->info_set || t->data) |
| 250 | return false; |
| 251 | } |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | void ipf_format::rotate(UINT32 *track, UINT32 offset, UINT32 size) |
| 256 | { |
| 257 | UINT32 done = 0; |
| 258 | for(UINT32 bpos=0; done < size; bpos++) { |
| 259 | UINT32 pos = bpos; |
| 260 | UINT32 hold = track[pos]; |
| 261 | for(;;) { |
| 262 | UINT32 npos = pos+offset; |
| 263 | if(npos >= size) |
| 264 | npos -= size; |
| 265 | if(npos == bpos) |
| 266 | break; |
| 267 | track[pos] = track[npos]; |
| 268 | pos = npos; |
| 269 | done++; |
| 270 | } |
| 271 | track[pos] = hold; |
| 272 | done++; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void ipf_format::mark_track_splice(UINT32 *t) |
| 277 | { |
| 278 | for(int i=0; i<3; i++) { |
| 279 | UINT32 v = *t; |
| 280 | if((v & floppy_image::MG_MASK) == MG_0) |
| 281 | v = (v & floppy_image::TIME_MASK) | MG_1; |
| 282 | else if((v & floppy_image::MG_MASK) == MG_1) |
| 283 | v = (v & floppy_image::TIME_MASK) | MG_0; |
| 284 | *t++ = v; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void ipf_format::timing_set(UINT32 *track, UINT32 start, UINT32 end, UINT32 time) |
| 289 | { |
| 290 | for(UINT32 i=start; i != end; i++) |
| 291 | track[i] = (track[i] & floppy_image::MG_MASK) | time; |
| 292 | } |
| 293 | |
| 294 | bool ipf_format::generate_timings(track_info *t, UINT32 *track, const UINT32 *data_pos, const UINT32 *gap_pos) |
| 295 | { |
| 296 | timing_set(track, 0, t->size_cells, 2000); |
| 297 | |
| 298 | switch(t->type) { |
| 299 | case 2: break; |
| 300 | |
| 301 | case 3: |
| 302 | if(t->block_count >= 4) |
| 303 | timing_set(track, gap_pos[3], data_pos[4], 1890); |
| 304 | if(t->block_count >= 5) { |
| 305 | timing_set(track, data_pos[4], gap_pos[4], 1890); |
| 306 | timing_set(track, gap_pos[4], data_pos[5], 1990); |
| 307 | } |
| 308 | if(t->block_count >= 6) { |
| 309 | timing_set(track, data_pos[5], gap_pos[5], 1990); |
| 310 | timing_set(track, gap_pos[5], data_pos[6], 2090); |
| 311 | } |
| 312 | if(t->block_count >= 7) |
| 313 | timing_set(track, data_pos[6], gap_pos[6], 2090); |
| 314 | break; |
| 315 | |
| 316 | case 4: |
| 317 | timing_set(track, gap_pos[t->block_count-1], data_pos[0], 1890); |
| 318 | timing_set(track, data_pos[0], gap_pos[0], 1890); |
| 319 | timing_set(track, gap_pos[0], data_pos[1], 1990); |
| 320 | if(t->block_count >= 2) { |
| 321 | timing_set(track, data_pos[1], gap_pos[1], 1990); |
| 322 | timing_set(track, gap_pos[1], data_pos[2], 2090); |
| 323 | } |
| 324 | if(t->block_count >= 3) |
| 325 | timing_set(track, data_pos[2], gap_pos[2], 2090); |
| 326 | break; |
| 327 | |
| 328 | case 5: |
| 329 | if(t->block_count >= 6) |
| 330 | timing_set(track, data_pos[5], gap_pos[5], 2100); |
| 331 | break; |
| 332 | |
| 333 | case 6: |
| 334 | if(t->block_count >= 2) |
| 335 | timing_set(track, data_pos[1], gap_pos[1], 2200); |
| 336 | if(t->block_count >= 3) |
| 337 | timing_set(track, data_pos[2], gap_pos[2], 1800); |
| 338 | break; |
| 339 | |
| 340 | case 7: |
| 341 | if(t->block_count >= 2) |
| 342 | timing_set(track, data_pos[1], gap_pos[1], 2100); |
| 343 | break; |
| 344 | |
| 345 | case 8: |
| 346 | if(t->block_count >= 2) |
| 347 | timing_set(track, data_pos[1], gap_pos[1], 2200); |
| 348 | if(t->block_count >= 3) |
| 349 | timing_set(track, data_pos[2], gap_pos[2], 2100); |
| 350 | if(t->block_count >= 5) |
| 351 | timing_set(track, data_pos[4], gap_pos[4], 1900); |
| 352 | if(t->block_count >= 6) |
| 353 | timing_set(track, data_pos[5], gap_pos[5], 1800); |
| 354 | if(t->block_count >= 7) |
| 355 | timing_set(track, data_pos[6], gap_pos[6], 1700); |
| 356 | break; |
| 357 | |
| 358 | case 9: { |
| 359 | UINT32 mask = r32(t->data + 32*t->block_count + 12); |
| 360 | for(UINT32 i=1; i<t->block_count; i++) |
| 361 | timing_set(track, data_pos[i], gap_pos[i], mask & (1 << (i-1)) ? 1900 : 2100); |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | default: |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | bool ipf_format::generate_track(track_info *t, floppy_image *image) |
| 373 | { |
| 374 | if(!t->size_cells) |
| 375 | return true; |
| 376 | |
| 377 | if(t->data_size < 32*t->block_count) |
| 378 | return false; |
| 379 | |
| 380 | // Annoyingly enough, too small gaps are ignored, changing the |
| 381 | // total track size. Artifact stemming from the byte-only support |
| 382 | // of old times? |
| 383 | t->size_cells = block_compute_real_size(t); |
| 384 | |
| 385 | if(t->index_cells >= t->size_cells) |
| 386 | return false; |
| 387 | |
| 388 | UINT32 *track = global_alloc_array(UINT32, t->size_cells); |
| 389 | |
| 390 | UINT32 *data_pos = new UINT32[t->block_count+1]; |
| 391 | UINT32 *gap_pos = new UINT32[t->block_count]; |
| 392 | UINT32 *splice_pos = new UINT32[t->block_count]; |
| 393 | |
| 394 | bool context = false; |
| 395 | UINT32 pos = 0; |
| 396 | for(UINT32 i = 0; i != t->block_count; i++) { |
| 397 | if(!generate_block(t, i, i == t->block_count-1 ? t->size_cells - t->index_cells : 0xffffffff, track, pos, data_pos[i], gap_pos[i], splice_pos[i], context)) { |
| 398 | global_free(track); |
| 399 | return false; |
| 400 | } |
| 401 | } |
| 402 | if(pos != t->size_cells) { |
| 403 | global_free(track); |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | data_pos[t->block_count] = pos; |
| 408 | |
| 409 | mark_track_splice(track + splice_pos[t->block_count-1]); |
| 410 | |
| 411 | if(!generate_timings(t, track, data_pos, gap_pos)) { |
| 412 | global_free(track); |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | if(t->index_cells) |
| 417 | rotate(track, t->size_cells - t->index_cells, t->size_cells); |
| 418 | |
| 419 | generate_track_from_levels(t->cylinder, t->head, track, t->size_cells, splice_pos[t->block_count-1] + t->index_cells, image); |
| 420 | global_free(track); |
| 421 | |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | void ipf_format::track_write_raw(UINT32 *&track, const UINT8 *data, UINT32 cells, bool &context) |
| 426 | { |
| 427 | for(UINT32 i=0; i != cells; i++) |
| 428 | *track++ = data[i>>3] & (0x80 >> (i & 7)) ? MG_1 : MG_0; |
| 429 | if(cells) |
| 430 | context = track[-1] == MG_1; |
| 431 | } |
| 432 | |
| 433 | void ipf_format::track_write_mfm(UINT32 *&track, const UINT8 *data, UINT32 start_offset, UINT32 patlen, UINT32 cells, bool &context) |
| 434 | { |
| 435 | patlen *= 2; |
| 436 | for(UINT32 i=0; i != cells; i++) { |
| 437 | UINT32 pos = (i + start_offset) % patlen; |
| 438 | bool bit = data[pos>>4] & (0x80 >> ((pos >> 1) & 7)); |
| 439 | if(pos & 1) { |
| 440 | *track++ = bit ? MG_1 : MG_0; |
| 441 | context = bit; |
| 442 | } else |
| 443 | *track++ = context || bit ? MG_0 : MG_1; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void ipf_format::track_write_weak(UINT32 *&track, UINT32 cells) |
| 448 | { |
| 449 | for(UINT32 i=0; i != cells; i++) |
| 450 | *track++ = MG_W; |
| 451 | } |
| 452 | |
| 453 | bool ipf_format::generate_block_data(const UINT8 *data, const UINT8 *dlimit, UINT32 *track, UINT32 *tlimit, bool &context) |
| 454 | { |
| 455 | for(;;) { |
| 456 | if(data >= dlimit) |
| 457 | return false; |
| 458 | UINT8 val = *data++; |
| 459 | if((val >> 5) > dlimit-data) |
| 460 | return false; |
| 461 | UINT32 param = rb(data, val >> 5); |
| 462 | UINT32 tleft = tlimit - track; |
| 463 | switch(val & 0x1f) { |
| 464 | case 0: // End of description |
| 465 | return !tleft; |
| 466 | |
| 467 | case 1: // Raw bytes |
| 468 | if(8*param > tleft) |
| 469 | return false; |
| 470 | track_write_raw(track, data, 8*param, context); |
| 471 | data += param; |
| 472 | break; |
| 473 | |
| 474 | case 2: // MFM-decoded data bytes |
| 475 | case 3: // MFM-decoded gap bytes |
| 476 | if(16*param > tleft) |
| 477 | return false; |
| 478 | track_write_mfm(track, data, 0, 8*param, 16*param, context); |
| 479 | data += param; |
| 480 | break; |
| 481 | |
| 482 | case 5: // Weak bytes |
| 483 | if(16*param > tleft) |
| 484 | return false; |
| 485 | track_write_weak(track, 16*param); |
| 486 | context = 0; |
| 487 | break; |
| 488 | |
| 489 | default: |
| 490 | return false; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | bool ipf_format::generate_block_gap_0(UINT32 gap_cells, UINT8 pattern, UINT32 &spos, UINT32 ipos, UINT32 *track, bool &context) |
| 496 | { |
| 497 | spos = ipos >= 16 && ipos+16 <= gap_cells ? ipos : gap_cells >> 1; |
| 498 | track_write_mfm(track, &pattern, 0, 8, spos, context); |
| 499 | UINT32 delta = 0; |
| 500 | if(gap_cells & 1) { |
| 501 | *track++ = MG_0; |
| 502 | delta++; |
| 503 | } |
| 504 | track_write_mfm(track, &pattern, spos+delta-gap_cells, 8, gap_cells-spos-delta, context); |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | bool ipf_format::gap_description_to_reserved_size(const UINT8 *&data, const UINT8 *dlimit, UINT32 &res_size) |
| 509 | { |
| 510 | res_size = 0; |
| 511 | for(;;) { |
| 512 | if(data >= dlimit) |
| 513 | return false; |
| 514 | UINT8 val = *data++; |
| 515 | if((val >> 5) > dlimit-data) |
| 516 | return false; |
| 517 | UINT32 param = rb(data, val >> 5); |
| 518 | switch(val & 0x1f) { |
| 519 | case 0: |
| 520 | return true; |
| 521 | case 1: |
| 522 | res_size += param*2; |
| 523 | break; |
| 524 | case 2: |
| 525 | data += (param+7)/8; |
| 526 | break; |
| 527 | default: |
| 528 | return false; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | bool ipf_format::generate_gap_from_description(const UINT8 *&data, const UINT8 *dlimit, UINT32 *track, UINT32 size, bool pre, bool &context) |
| 534 | { |
| 535 | const UINT8 *data1 = data; |
| 536 | UINT32 res_size; |
| 537 | if(!gap_description_to_reserved_size(data1, dlimit, res_size)) |
| 538 | return false; |
| 539 | |
| 540 | if(res_size > size) |
| 541 | return false; |
| 542 | UINT8 pattern[16]; |
| 543 | memset(pattern, 0, sizeof(pattern)); |
| 544 | UINT32 pattern_size = 0; |
| 545 | |
| 546 | UINT32 pos = 0, block_size = 0; |
| 547 | for(;;) { |
| 548 | UINT8 val = *data++; |
| 549 | UINT32 param = rb(data, val >> 5); |
| 550 | switch(val & 0x1f) { |
| 551 | case 0: |
| 552 | return size == pos; |
| 553 | |
| 554 | case 1: |
| 555 | if(block_size) |
| 556 | return false; |
| 557 | block_size = param*2; |
| 558 | pattern_size = 0; |
| 559 | break; |
| 560 | |
| 561 | case 2: |
| 562 | // You can't have a pattern at the start of a pre-slice |
| 563 | // gap if there's a size afterwards |
| 564 | if(pre && res_size && !block_size) |
| 565 | return false; |
| 566 | // You can't have two consecutive patterns |
| 567 | if(pattern_size) |
| 568 | return false; |
| 569 | pattern_size = param; |
| 570 | if(pattern_size > sizeof(pattern)*8) |
| 571 | return false; |
| 572 | |
| 573 | memcpy(pattern, data, (pattern_size+7)/8); |
| 574 | data += (pattern_size+7)/8; |
| 575 | if(pre) { |
| 576 | if(!block_size) |
| 577 | block_size = size; |
| 578 | else if(pos + block_size == res_size) |
| 579 | block_size = size - pos; |
| 580 | if(pos + block_size > size) |
| 581 | return false; |
| 582 | // printf("pat=%02x size=%d pre\n", pattern[0], block_size); |
| 583 | track_write_mfm(track, pattern, 0, pattern_size, block_size, context); |
| 584 | pos += block_size; |
| 585 | } else { |
| 586 | if(pos == 0 && block_size && res_size != size) |
| 587 | block_size = size - (res_size-block_size); |
| 588 | if(!block_size) |
| 589 | block_size = size - res_size; |
| 590 | if(pos + block_size > size) |
| 591 | return false; |
| 592 | // printf("pat=%02x block_size=%d size=%d res_size=%d post\n", pattern[0], block_size, size, res_size); |
| 593 | track_write_mfm(track, pattern, -block_size, pattern_size, block_size, context); |
| 594 | pos += block_size; |
| 595 | } |
| 596 | block_size = 0; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | |
| 603 | bool ipf_format::generate_block_gap_1(UINT32 gap_cells, UINT32 &spos, const UINT8 *data, const UINT8 *dlimit, UINT32 *track, bool &context) |
| 604 | { |
| 605 | spos = 0; |
| 606 | return generate_gap_from_description(data, dlimit, track, gap_cells, true, context); |
| 607 | } |
| 608 | |
| 609 | bool ipf_format::generate_block_gap_2(UINT32 gap_cells, UINT32 &spos, const UINT8 *data, const UINT8 *dlimit, UINT32 *track, bool &context) |
| 610 | { |
| 611 | spos = gap_cells; |
| 612 | return generate_gap_from_description(data, dlimit, track, gap_cells, false, context); |
| 613 | } |
| 614 | |
| 615 | bool ipf_format::generate_block_gap_3(UINT32 gap_cells, UINT32 &spos, UINT32 ipos, const UINT8 *data, const UINT8 *dlimit, UINT32 *track, bool &context) |
| 616 | { |
| 617 | if(ipos >= 16 && ipos < gap_cells-16) |
| 618 | spos = ipos; |
| 619 | else { |
| 620 | UINT32 presize, postsize; |
| 621 | const UINT8 *data1 = data; |
| 622 | if(!gap_description_to_reserved_size(data1, dlimit, presize)) |
| 623 | return false; |
| 624 | if(!gap_description_to_reserved_size(data1, dlimit, postsize)) |
| 625 | return false; |
| 626 | if(presize+postsize > gap_cells) |
| 627 | return false; |
| 628 | |
| 629 | spos = presize + (gap_cells - presize - postsize)/2; |
| 630 | } |
| 631 | if(!generate_gap_from_description(data, dlimit, track, spos, true, context)) |
| 632 | return false; |
| 633 | UINT32 delta = 0; |
| 634 | if(gap_cells & 1) { |
| 635 | track[spos] = MG_0; |
| 636 | delta++; |
| 637 | } |
| 638 | |
| 639 | return generate_gap_from_description(data, dlimit, track+spos+delta, gap_cells - spos - delta, false, context); |
| 640 | } |
| 641 | |
| 642 | bool ipf_format::generate_block_gap(UINT32 gap_type, UINT32 gap_cells, UINT8 pattern, UINT32 &spos, UINT32 ipos, const UINT8 *data, const UINT8 *dlimit, UINT32 *track, bool &context) |
| 643 | { |
| 644 | switch(gap_type) { |
| 645 | case 0: |
| 646 | return generate_block_gap_0(gap_cells, pattern, spos, ipos, track, context); |
| 647 | case 1: |
| 648 | return generate_block_gap_1(gap_cells, spos, data, dlimit, track, context); |
| 649 | case 2: |
| 650 | return generate_block_gap_2(gap_cells, spos, data, dlimit, track, context); |
| 651 | case 3: |
| 652 | return generate_block_gap_3(gap_cells, spos, ipos, data, dlimit, track, context); |
| 653 | default: |
| 654 | return false; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | bool ipf_format::generate_block(track_info *t, UINT32 idx, UINT32 ipos, UINT32 *track, UINT32 &pos, UINT32 &dpos, UINT32 &gpos, UINT32 &spos, bool &context) |
| 659 | { |
| 660 | const UINT8 *data = t->data; |
| 661 | const UINT8 *data_end = t->data + t->data_size; |
| 662 | const UINT8 *thead = data + 32*idx; |
| 663 | UINT32 data_cells = r32(thead); |
| 664 | UINT32 gap_cells = r32(thead+4); |
| 665 | |
| 666 | if(gap_cells < 8) |
| 667 | gap_cells = 0; |
| 668 | |
| 669 | // +8 = gap description offset / datasize in bytes (when gap type = 0) |
| 670 | // +12 = 1 / gap size in bytes (when gap type = 0) |
| 671 | // +16 = 1 |
| 672 | // +20 = gap type |
| 673 | // +24 = type 0 gap pattern (8 bits) / speed mask for sector 0 track type 9 |
| 674 | // +28 = data description offset |
| 675 | |
| 676 | dpos = pos; |
| 677 | gpos = dpos + data_cells; |
| 678 | pos = gpos + gap_cells; |
| 679 | if(pos > t->size_cells) |
| 680 | return false; |
| 681 | if(!generate_block_data(data + r32(thead+28), data_end, track+dpos, track+gpos, context)) |
| 682 | return false; |
| 683 | if(!generate_block_gap(r32(thead+20), gap_cells, r32(thead+24), spos, ipos-gpos, data + r32(thead+8), data_end, track+gpos, context)) |
| 684 | return false; |
| 685 | spos += gpos; |
| 686 | |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | UINT32 ipf_format::block_compute_real_size(track_info *t) |
| 691 | { |
| 692 | UINT32 size = 0; |
| 693 | const UINT8 *thead = t->data; |
| 694 | for(unsigned int i=0; i != t->block_count; i++) { |
| 695 | UINT32 data_cells = r32(thead); |
| 696 | UINT32 gap_cells = r32(thead+4); |
| 697 | if(gap_cells < 8) |
| 698 | gap_cells = 0; |
| 699 | |
| 700 | size += data_cells + gap_cells; |
| 701 | thead += 32; |
| 702 | } |
| 703 | return size; |
| 704 | } |
src/lib/formats/ipf_dsk.h
| r0 | r13029 | |
| 1 | #ifndef IPF_DSK_H_ |
| 2 | #define IPF_DSK_H_ |
| 3 | |
| 4 | #include "flopimg.h" |
| 5 | |
| 6 | class ipf_format : public floppy_image_format_t |
| 7 | { |
| 8 | public: |
| 9 | ipf_format(); |
| 10 | |
| 11 | virtual int identify(io_generic *io); |
| 12 | virtual bool load(io_generic *io, floppy_image *image); |
| 13 | |
| 14 | virtual const char *name() const; |
| 15 | virtual const char *description() const; |
| 16 | virtual const char *extensions() const; |
| 17 | virtual bool supports_save() const; |
| 18 | |
| 19 | private: |
| 20 | struct track_info { |
| 21 | UINT32 cylinder, head, type; |
| 22 | UINT32 t12, t44, t56, t60, t64; |
| 23 | UINT32 size_bytes, size_cells; |
| 24 | UINT32 index_bytes, index_cells; |
| 25 | UINT32 datasize_cells, gapsize_cells; |
| 26 | UINT32 block_count, weak_bits; |
| 27 | |
| 28 | UINT32 d4; |
| 29 | |
| 30 | bool info_set; |
| 31 | |
| 32 | const UINT8 *data; |
| 33 | UINT32 data_size; |
| 34 | }; |
| 35 | |
| 36 | track_info *tinfos; |
| 37 | UINT32 tcount; |
| 38 | |
| 39 | UINT32 type, release, revision; |
| 40 | UINT32 f4, f8, f14; |
| 41 | UINT32 min_cylinder, max_cylinder, min_head, max_head; |
| 42 | UINT32 credit_day, credit_time; |
| 43 | UINT32 platform[4], extra[5]; |
| 44 | |
| 45 | UINT32 crc32r(const UINT8 *data, UINT32 size); |
| 46 | |
| 47 | bool parse_info(const UINT8 *info); |
| 48 | bool parse_imge(const UINT8 *imge); |
| 49 | bool parse_data(const UINT8 *data, UINT32 &pos, UINT32 max_extra_size); |
| 50 | |
| 51 | bool scan_one_tag(UINT8 *data, UINT32 size, UINT32 &pos, UINT8 *&tag, UINT32 &tsize); |
| 52 | bool scan_all_tags(UINT8 *data, UINT32 size); |
| 53 | static UINT32 r32(const UINT8 *p); |
| 54 | static UINT32 rb(const UINT8 *&p, int count); |
| 55 | |
| 56 | track_info *get_index(UINT32 idx); |
| 57 | |
| 58 | void track_write_raw(UINT32 *&track, const UINT8 *data, UINT32 cells, bool &context); |
| 59 | void track_write_mfm(UINT32 *&track, const UINT8 *data, UINT32 start_offset, UINT32 patlen, UINT32 cells, bool &context); |
| 60 | void track_write_weak(UINT32 *&track, UINT32 cells); |
| 61 | bool generate_block_data(const UINT8 *data, const UINT8 *dlimit, UINT32 *track, UINT32 *tlimit, bool &context); |
| 62 | |
| 63 | bool gap_description_to_reserved_size(const UINT8 *&data, const UINT8 *dlimit, UINT32 &res_size); |
| 64 | bool generate_gap_from_description(const UINT8 *&data, const UINT8 *dlimit, UINT32 *track, UINT32 size, bool pre, bool &context); |
| 65 | bool generate_block_gap_0(UINT32 gap_cells, UINT8 pattern, UINT32 &spos, UINT32 ipos, UINT32 *track, bool &context); |
| 66 | bool generate_block_gap_1(UINT32 gap_cells, UINT32 &spos, const UINT8 *data, const UINT8 *dlimit, UINT32 *track, bool &context); |
| 67 | bool generate_block_gap_2(UINT32 gap_cells, UINT32 &spos, const UINT8 *data, const UINT8 *dlimit, UINT32 *track, bool &context); |
| 68 | bool generate_block_gap_3(UINT32 gap_cells, UINT32 &spos, UINT32 ipos, const UINT8 *data, const UINT8 *dlimit, UINT32 *track, bool &context); |
| 69 | bool generate_block_gap(UINT32 gap_type, UINT32 gap_cells, UINT8 pattern, UINT32 &spos, UINT32 ipos, const UINT8 *data, const UINT8 *dlimit, UINT32 *track, bool &context); |
| 70 | |
| 71 | bool generate_block(track_info *t, UINT32 idx, UINT32 ipos, UINT32 *track, UINT32 &pos, UINT32 &dpos, UINT32 &gpos, UINT32 &spos, bool &context); |
| 72 | UINT32 block_compute_real_size(track_info *t); |
| 73 | |
| 74 | void timing_set(UINT32 *track, UINT32 start, UINT32 end, UINT32 time); |
| 75 | bool generate_timings(track_info *t, UINT32 *track, const UINT32 *data_pos, const UINT32 *gap_pos); |
| 76 | |
| 77 | void rotate(UINT32 *track, UINT32 offset, UINT32 size); |
| 78 | void mark_track_splice(UINT32 *t); |
| 79 | bool generate_track(track_info *t, floppy_image *image); |
| 80 | bool generate_tracks(floppy_image *image); |
| 81 | |
| 82 | bool parse(UINT8 *data, UINT32 size, floppy_image *image); |
| 83 | }; |
| 84 | |
| 85 | extern const floppy_format_type FLOPPY_IPF_FORMAT; |
| 86 | |
| 87 | #endif /*IPF_DSK_H_*/ |