src/mess/formats/coupedsk.c
| r2252 | r2253 | |
| 13 | 13 | #define SAD_HEADER_LEN 22 |
| 14 | 14 | #define SAD_SIGNATURE "Aley's disk backup" |
| 15 | 15 | |
| 16 | #define SDF_TAG "sdftag" |
| 17 | #define SDF_TRACKSIZE (512 * 12) |
| 16 | 18 | |
| 17 | 19 | |
| 20 | struct sdf_tag |
| 21 | { |
| 22 | UINT8 heads; |
| 23 | UINT8 tracks; |
| 24 | }; |
| 25 | |
| 26 | |
| 27 | |
| 18 | 28 | /************************************* |
| 19 | 29 | * |
| 20 | 30 | * MGT disk image format |
| r2252 | r2253 | |
| 196 | 206 | /************************************* |
| 197 | 207 | * |
| 198 | 208 | * SDF disk image format |
| 209 | * |
| 210 | * TODO: wd17xx status codes are |
| 211 | * currently ignored |
| 199 | 212 | * |
| 200 | 213 | *************************************/ |
| 201 | 214 | |
| 202 | | FLOPPY_CONSTRUCT( coupe_sdf_construct ) |
| 215 | static int coupe_sdf_get_heads_per_disk(floppy_image *floppy) |
| 203 | 216 | { |
| 217 | struct sdf_tag *tag = floppy_tag(floppy, SDF_TAG); |
| 218 | return tag->heads; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static int coupe_sdf_get_tracks_per_disk(floppy_image *floppy) |
| 223 | { |
| 224 | struct sdf_tag *tag = floppy_tag(floppy, SDF_TAG); |
| 225 | return tag->tracks; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | static UINT32 coupe_sdf_get_track_size(floppy_image *floppy, int head, int track) |
| 230 | { |
| 231 | return SDF_TRACKSIZE; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static floperr_t coupe_sdf_get_offset(floppy_image *floppy, |
| 236 | int head, int track, UINT64 *offset) |
| 237 | { |
| 238 | struct sdf_tag *tag = floppy_tag(floppy, SDF_TAG); |
| 239 | |
| 240 | if (head > tag->heads || track > tag->tracks) |
| 241 | return FLOPPY_ERROR_SEEKERROR; |
| 242 | |
| 243 | *offset = (head * tag->tracks + track) * SDF_TRACKSIZE; |
| 244 | |
| 245 | return FLOPPY_ERROR_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | static floperr_t coupe_sdf_read_track(floppy_image *floppy, |
| 250 | int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 251 | { |
| 252 | floperr_t err; |
| 253 | UINT64 track_offset; |
| 254 | |
| 255 | /* get the offset to this track */ |
| 256 | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 257 | if (err) return err; |
| 258 | |
| 259 | /* read track data into buffer */ |
| 260 | floppy_image_read(floppy, buffer, offset + track_offset, buflen); |
| 261 | |
| 262 | return FLOPPY_ERROR_SUCCESS; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | static floperr_t coupe_sdf_write_track(floppy_image *floppy, |
| 267 | int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 268 | { |
| 269 | floperr_t err; |
| 270 | UINT64 track_offset; |
| 271 | |
| 272 | /* get the offset to this track */ |
| 273 | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 274 | if (err) return err; |
| 275 | |
| 276 | /* write buffer to image */ |
| 277 | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 278 | |
| 279 | return FLOPPY_ERROR_SUCCESS; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | static floperr_t coupe_sdf_get_sector_offset(floppy_image *floppy, |
| 284 | int head, int track, int sector, UINT64 *offset) |
| 285 | { |
| 286 | floperr_t err; |
| 287 | UINT8 buffer[SDF_TRACKSIZE]; |
| 288 | int i, buffer_pos = 1; |
| 289 | |
| 290 | /* get track data */ |
| 291 | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 292 | if (err) return err; |
| 293 | |
| 294 | /* check if the sector is available in this track */ |
| 295 | if (sector >= buffer[0]) |
| 296 | return FLOPPY_ERROR_SEEKERROR; |
| 297 | |
| 298 | /* find the right sector in this track */ |
| 299 | for (i = 0; i < buffer[0]; i++) |
| 300 | { |
| 301 | int sector_number = buffer[buffer_pos + 4]; |
| 302 | |
| 303 | if (sector_number - 1 == sector) |
| 304 | { |
| 305 | *offset = buffer_pos; |
| 306 | return FLOPPY_ERROR_SUCCESS; |
| 307 | } |
| 308 | |
| 309 | buffer_pos += (128 << buffer[buffer_pos + 5]) + 8; |
| 310 | } |
| 311 | |
| 204 | 312 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 205 | 313 | } |
| 206 | 314 | |
| 207 | 315 | |
| 316 | static floperr_t coupe_sdf_get_total_sector_offset(floppy_image *floppy, |
| 317 | int head, int track, int sector, UINT64 *offset) |
| 318 | { |
| 319 | floperr_t err; |
| 320 | UINT64 track_offset, sector_offset; |
| 321 | |
| 322 | /* get offset to the track start */ |
| 323 | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 324 | if (err) return err; |
| 325 | |
| 326 | /* get offset to the start of the sector */ |
| 327 | err = coupe_sdf_get_sector_offset(floppy, head, track, sector, §or_offset); |
| 328 | if (err) return err; |
| 329 | |
| 330 | *offset = track_offset + sector_offset; |
| 331 | |
| 332 | return FLOPPY_ERROR_SUCCESS; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | static floperr_t coupe_sdf_get_sector_length(floppy_image *floppy, |
| 337 | int head, int track, int sector, UINT32 *sector_length) |
| 338 | { |
| 339 | floperr_t err; |
| 340 | UINT8 buffer[SDF_TRACKSIZE]; |
| 341 | UINT64 offset; |
| 342 | |
| 343 | /* get track data */ |
| 344 | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 345 | if (err) return err; |
| 346 | |
| 347 | /* get offset to the start of the sector */ |
| 348 | err = coupe_sdf_get_sector_offset(floppy, head, track, sector, &offset); |
| 349 | if (err) return err; |
| 350 | |
| 351 | /* get size */ |
| 352 | *sector_length = 128 << buffer[offset + 5]; |
| 353 | |
| 354 | return FLOPPY_ERROR_SUCCESS; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | static floperr_t coupe_sdf_get_indexed_sector_info(floppy_image *floppy, |
| 359 | int head, int track, int sector_index, |
| 360 | int *cylinder, int *side, int *sector, UINT32 *sector_length) |
| 361 | { |
| 362 | floperr_t err; |
| 363 | UINT8 buffer[SDF_TRACKSIZE]; |
| 364 | UINT64 offset; |
| 365 | |
| 366 | /* get track data */ |
| 367 | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 368 | if (err) return err; |
| 369 | |
| 370 | /* get offset to the start of the sector */ |
| 371 | err = coupe_sdf_get_sector_offset(floppy, head, track, sector_index, &offset); |
| 372 | if (err) return err; |
| 373 | |
| 374 | /* extract data */ |
| 375 | if (cylinder) *cylinder = buffer[offset + 2]; |
| 376 | if (side) *side = buffer[offset + 3]; |
| 377 | if (sector) *sector = buffer[offset + 4]; |
| 378 | if (sector_length) *sector_length = 128 << buffer[offset + 5]; |
| 379 | |
| 380 | return FLOPPY_ERROR_SUCCESS; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | static floperr_t coupe_sdf_read_indexed_sector(floppy_image *floppy, |
| 385 | int head, int track, int sector, void *buffer, size_t buflen) |
| 386 | { |
| 387 | floperr_t err; |
| 388 | UINT64 offset; |
| 389 | |
| 390 | err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset); |
| 391 | if (err) return err; |
| 392 | |
| 393 | /* read sector data into buffer */ |
| 394 | floppy_image_read(floppy, buffer, offset + 8, buflen); |
| 395 | |
| 396 | return FLOPPY_ERROR_SUCCESS; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static floperr_t coupe_sdf_write_indexed_sector(floppy_image *floppy, |
| 401 | int head, int track, int sector, const void *buffer, size_t buflen) |
| 402 | { |
| 403 | floperr_t err; |
| 404 | UINT64 offset; |
| 405 | |
| 406 | err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset); |
| 407 | if (err) return err; |
| 408 | |
| 409 | /* write buffer into image */ |
| 410 | floppy_image_write(floppy, buffer, offset + 8, buflen); |
| 411 | |
| 412 | return FLOPPY_ERROR_SUCCESS; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | static void coupe_sdf_interpret_header(floppy_image *floppy, int *heads, int *tracks) |
| 417 | { |
| 418 | UINT64 size = floppy_image_size(floppy); |
| 419 | |
| 420 | if (size % SDF_TRACKSIZE == 0) |
| 421 | { |
| 422 | *heads = 2; |
| 423 | *tracks = size / (SDF_TRACKSIZE * 2); |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | *heads = 0; |
| 428 | *tracks = 0; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | |
| 433 | FLOPPY_CONSTRUCT( coupe_sdf_construct ) |
| 434 | { |
| 435 | struct FloppyCallbacks *callbacks; |
| 436 | struct sdf_tag *tag; |
| 437 | int heads, tracks; |
| 438 | |
| 439 | if (params) |
| 440 | { |
| 441 | /* create */ |
| 442 | return FLOPPY_ERROR_UNSUPPORTED; |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | /* load */ |
| 447 | coupe_sdf_interpret_header(floppy, &heads, &tracks); |
| 448 | } |
| 449 | |
| 450 | tag = floppy_create_tag(floppy, SDF_TAG, sizeof(struct sdf_tag)); |
| 451 | |
| 452 | if (!tag) |
| 453 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 454 | |
| 455 | tag->heads = heads; |
| 456 | tag->tracks = tracks; |
| 457 | |
| 458 | callbacks = floppy_callbacks(floppy); |
| 459 | callbacks->read_track = coupe_sdf_read_track; |
| 460 | callbacks->write_track = coupe_sdf_write_track; |
| 461 | callbacks->get_heads_per_disk = coupe_sdf_get_heads_per_disk; |
| 462 | callbacks->get_tracks_per_disk = coupe_sdf_get_tracks_per_disk; |
| 463 | callbacks->get_track_size = coupe_sdf_get_track_size; |
| 464 | callbacks->get_sector_length = coupe_sdf_get_sector_length; |
| 465 | callbacks->read_indexed_sector = coupe_sdf_read_indexed_sector; |
| 466 | callbacks->write_indexed_sector = coupe_sdf_write_indexed_sector; |
| 467 | callbacks->get_indexed_sector_info = coupe_sdf_get_indexed_sector_info; |
| 468 | |
| 469 | return FLOPPY_ERROR_SUCCESS; |
| 470 | } |
| 471 | |
| 472 | |
| 208 | 473 | FLOPPY_IDENTIFY( coupe_sdf_identify ) |
| 209 | 474 | { |
| 210 | | *vote = 0; |
| 475 | int heads, tracks; |
| 476 | |
| 477 | /* read header values */ |
| 478 | coupe_sdf_interpret_header(floppy, &heads, &tracks); |
| 479 | |
| 480 | /* check for sensible values */ |
| 481 | if (heads > 0 && heads < 3 && tracks > 0 && tracks < 84) |
| 482 | *vote = 100; |
| 483 | else |
| 484 | *vote = 0; |
| 485 | |
| 211 | 486 | return FLOPPY_ERROR_SUCCESS; |
| 212 | 487 | } |
| 213 | 488 | |
| r2252 | r2253 | |
| 231 | 506 | ) |
| 232 | 507 | FLOPPY_OPTION |
| 233 | 508 | ( |
| 234 | | coupe_sad, "sad", "SAM Coupe SAD disk image", coupe_sad_identify, coupe_sad_construct, |
| 509 | coupe_sad, "sad,dsk", "SAM Coupe SAD disk image", coupe_sad_identify, coupe_sad_construct, |
| 235 | 510 | HEADS(1-[2]-255) |
| 236 | 511 | TRACKS(1-[80]-255) |
| 237 | 512 | SECTORS(1-[10]-255) |
| r2252 | r2253 | |
| 240 | 515 | ) |
| 241 | 516 | FLOPPY_OPTION |
| 242 | 517 | ( |
| 243 | | coupe_sdf, "sdf", "SAM Coupe SDF disk image", coupe_sdf_identify, coupe_sdf_construct, |
| 244 | | HEADS([2]) |
| 245 | | TRACKS([80]) |
| 246 | | SECTORS([10]) |
| 247 | | SECTOR_LENGTH([512]) |
| 518 | coupe_sdf, "sdf,dsk,sad", "SAM Coupe SDF disk image", coupe_sdf_identify, coupe_sdf_construct, |
| 519 | HEADS(1-[2]) |
| 520 | TRACKS(1-[80]-83) |
| 521 | SECTORS(1-[10]-12) |
| 522 | SECTOR_LENGTH(128/256/[512]/1024) |
| 248 | 523 | FIRST_SECTOR_ID([1]) |
| 249 | 524 | ) |
| 250 | 525 | FLOPPY_OPTIONS_END |