src/mess/devices/basicdsk.c
| r5640 | r5641 | |
| 1 | | /* This is a basic disc image format. |
| 2 | | Each driver which uses this must use basicdsk_set_geometry, so that |
| 3 | | the data will be accessed correctly */ |
| 4 | | |
| 5 | | |
| 6 | | /* THIS DISK IMAGE CODE USED TO BE PART OF THE WD179X EMULATION, EXTRACTED INTO THIS FILE */ |
| 7 | | #include "driver.h" |
| 8 | | #include "basicdsk.h" |
| 9 | | #include "flopdrv.h" |
| 10 | | |
| 11 | | |
| 12 | | #define basicdsk_MAX_DRIVES 4 |
| 13 | | #define VERBOSE 1 |
| 14 | | #define LOG(x) do { if (VERBOSE) logerror x; } while (0) |
| 15 | | |
| 16 | | |
| 17 | | typedef struct |
| 18 | | { |
| 19 | | UINT8 track; |
| 20 | | UINT8 sector; |
| 21 | | UINT8 status; |
| 22 | | } SECMAP; |
| 23 | | |
| 24 | | typedef struct |
| 25 | | { |
| 26 | | const char *image_name; /* file name for disc image */ |
| 27 | | const device_config *image_file; /* file handle for disc image */ |
| 28 | | int mode; /* open mode == 0 read only, != 0 read/write */ |
| 29 | | unsigned long image_size; /* size of image file */ |
| 30 | | |
| 31 | | SECMAP *secmap; |
| 32 | | |
| 33 | | UINT8 unit; /* unit number if image_file == REAL_FDD */ |
| 34 | | |
| 35 | | UINT8 tracks; /* maximum # of tracks */ |
| 36 | | UINT8 heads; /* maximum # of heads */ |
| 37 | | |
| 38 | | UINT16 offset; /* track 0 offset */ |
| 39 | | UINT8 first_sector_id; /* id of first sector */ |
| 40 | | UINT8 sec_per_track; /* sectors per track */ |
| 41 | | |
| 42 | | UINT8 head; /* current head # */ |
| 43 | | UINT8 track; /* current track # */ |
| 44 | | |
| 45 | | UINT8 N; |
| 46 | | UINT16 sector_length; /* sector length (byte) */ |
| 47 | | |
| 48 | | int track_divider; /* 2 if using a 40-track image in a 80-track drive, 1 otherwise */ |
| 49 | | |
| 50 | | /* a bit for each sector in the image. If the bit is set, this sector |
| 51 | | has a deleted data address mark. If the bit is not set, this sector |
| 52 | | has a data address mark */ |
| 53 | | UINT8 *ddam_map; |
| 54 | | unsigned long ddam_map_size; |
| 55 | | |
| 56 | | unsigned long (*calcoffset)(UINT8 t, UINT8 h, UINT8 s, |
| 57 | | UINT8 tracks, UINT8 heads, UINT8 sec_per_track, UINT16 sector_length, UINT8 first_sector_id, UINT16 offset_track_zero); |
| 58 | | } basicdsk; |
| 59 | | |
| 60 | | static basicdsk basicdsk_drives[basicdsk_MAX_DRIVES]; |
| 61 | | |
| 62 | | static void basicdsk_seek_callback(const device_config *img, int); |
| 63 | | static int basicdsk_get_sectors_per_track(const device_config *img, int); |
| 64 | | static void basicdsk_get_id_callback(const device_config *img, chrn_id *, int, int); |
| 65 | | static void basicdsk_read_sector_data_into_buffer(const device_config *img, int side, int index1, char *ptr, int length); |
| 66 | | static void basicdsk_write_sector_data_from_buffer(const device_config *img, int side, int index1, const char *ptr, int length,int ddam); |
| 67 | | |
| 68 | | const floppy_interface basicdsk_floppy_interface = |
| 69 | | { |
| 70 | | basicdsk_seek_callback, |
| 71 | | basicdsk_get_sectors_per_track, /* done */ |
| 72 | | basicdsk_get_id_callback, /* done */ |
| 73 | | basicdsk_read_sector_data_into_buffer, /* done */ |
| 74 | | basicdsk_write_sector_data_from_buffer, /* done */\ |
| 75 | | NULL, |
| 76 | | NULL |
| 77 | | }; |
| 78 | | |
| 79 | | static basicdsk *get_basicdsk(const device_config *img) |
| 80 | | { |
| 81 | | int drive = image_index_in_device(img); |
| 82 | | assert(drive >= 0); |
| 83 | | assert(drive < (sizeof(basicdsk_drives) / sizeof(basicdsk_drives[0]))); |
| 84 | | return &basicdsk_drives[drive]; |
| 85 | | } |
| 86 | | |
| 87 | | DEVICE_START( basicdsk_floppy ) |
| 88 | | { |
| 89 | | floppy_drive_init(device, &basicdsk_floppy_interface); |
| 90 | | } |
| 91 | | |
| 92 | | /* attempt to insert a disk into the drive specified with id */ |
| 93 | | DEVICE_IMAGE_LOAD( basicdsk_floppy ) |
| 94 | | { |
| 95 | | basicdsk *w = get_basicdsk(image); |
| 96 | | |
| 97 | | w->image_file = image; |
| 98 | | w->mode = image_is_writable(image); |
| 99 | | |
| 100 | | /* this will be setup in the set_geometry function */ |
| 101 | | w->ddam_map = NULL; |
| 102 | | |
| 103 | | /* the following line is unsafe, but floppy_drives_init assumes we start on track 0, |
| 104 | | so we need to reflect this */ |
| 105 | | w->track = 0; |
| 106 | | |
| 107 | | floppy_drive_set_disk_image_interface(image, &basicdsk_floppy_interface); |
| 108 | | |
| 109 | | return INIT_PASS; |
| 110 | | } |
| 111 | | |
| 112 | | DEVICE_IMAGE_UNLOAD( basicdsk_floppy ) |
| 113 | | { |
| 114 | | basicdsk *w = get_basicdsk(image); |
| 115 | | w->image_file = NULL; |
| 116 | | w->mode = 0; |
| 117 | | } |
| 118 | | |
| 119 | | /* set data mark/deleted data mark for the sector specified. If ddam!=0, the sector will |
| 120 | | have a deleted data mark, if ddam==0, the sector will have a data mark */ |
| 121 | | void basicdsk_set_ddam(const device_config *img, UINT8 physical_track, UINT8 physical_side, UINT8 sector_id,UINT8 ddam) |
| 122 | | { |
| 123 | | unsigned long ddam_bit_offset, ddam_bit_index, ddam_byte_offset; |
| 124 | | basicdsk *pDisk = get_basicdsk(img); |
| 125 | | |
| 126 | | if (!pDisk->ddam_map) |
| 127 | | return; |
| 128 | | |
| 129 | | logerror("basicdsk_set_ddam: T:$%02x H:%d S:$%02x = %d\n", physical_track, physical_side, sector_id,ddam); |
| 130 | | |
| 131 | | /* calculate bit-offset into map */ |
| 132 | | ddam_bit_offset = (((physical_track * pDisk->heads) + physical_side)*pDisk->sec_per_track) + |
| 133 | | sector_id - pDisk->first_sector_id; |
| 134 | | |
| 135 | | /* if offset exceeds the number of bits that are stored in the ddam map return 0 */ |
| 136 | | if (ddam_bit_offset>=(pDisk->ddam_map_size<<3)) |
| 137 | | return; |
| 138 | | |
| 139 | | /* calculate byte offset */ |
| 140 | | ddam_byte_offset = ddam_bit_offset>>3; |
| 141 | | /* calc bit index within byte */ |
| 142 | | ddam_bit_index = ddam_bit_offset & 0x07; |
| 143 | | |
| 144 | | /* clear bit */ |
| 145 | | pDisk->ddam_map[ddam_byte_offset] &= ~(1<<ddam_bit_index); |
| 146 | | |
| 147 | | /* deleted dam? */ |
| 148 | | if (ddam) |
| 149 | | { |
| 150 | | /* set deleted dam */ |
| 151 | | pDisk->ddam_map[ddam_byte_offset] |= (1<<ddam_bit_index); |
| 152 | | } |
| 153 | | } |
| 154 | | |
| 155 | | /* get dam state for specified sector */ |
| 156 | | static int basicdsk_get_ddam(const device_config *img, UINT8 physical_track, UINT8 physical_side, UINT8 sector_id) |
| 157 | | { |
| 158 | | unsigned long ddam_bit_offset, ddam_bit_index, ddam_byte_offset; |
| 159 | | basicdsk *pDisk = get_basicdsk(img); |
| 160 | | |
| 161 | | if (!pDisk->ddam_map || !image_exists(img)) |
| 162 | | return 0; |
| 163 | | |
| 164 | | /* calculate bit-offset into map */ |
| 165 | | ddam_bit_offset = (((physical_track * pDisk->heads) + physical_side)*pDisk->sec_per_track) + |
| 166 | | sector_id - pDisk->first_sector_id; |
| 167 | | |
| 168 | | /* if offset exceeds the number of bits that are stored in the ddam map return 0 */ |
| 169 | | if (ddam_bit_offset>=(pDisk->ddam_map_size<<3)) |
| 170 | | return 0; |
| 171 | | |
| 172 | | /* calculate byte offset */ |
| 173 | | ddam_byte_offset = ddam_bit_offset>>3; |
| 174 | | /* calc bit index within byte */ |
| 175 | | ddam_bit_index = ddam_bit_offset & 0x07; |
| 176 | | |
| 177 | | /* clear bit */ |
| 178 | | return ((pDisk->ddam_map[ddam_byte_offset] & (1<<ddam_bit_index))!=0); |
| 179 | | } |
| 180 | | |
| 181 | | |
| 182 | | void basicdsk_set_calcoffset(const device_config *img, unsigned long (*calcoffset)(UINT8 t, UINT8 h, UINT8 s, |
| 183 | | UINT8 tracks, UINT8 heads, UINT8 sec_per_track, UINT16 sector_length, UINT8 first_sector_id, UINT16 offset_track_zero)) |
| 184 | | { |
| 185 | | basicdsk *pDisk = get_basicdsk(img); |
| 186 | | pDisk->calcoffset = calcoffset; |
| 187 | | } |
| 188 | | |
| 189 | | /* dir_sector is a relative offset from the start of the disc, |
| 190 | | dir_length is a relative offset from the start of the disc */ |
| 191 | | void basicdsk_set_geometry(const device_config *img, UINT8 tracks, UINT8 heads, UINT8 sec_per_track, UINT16 sector_length, UINT8 first_sector_id, UINT16 offset_track_zero, int track_skipping) |
| 192 | | { |
| 193 | | unsigned long N; |
| 194 | | unsigned long ShiftCount; |
| 195 | | basicdsk *pDisk = get_basicdsk(img); |
| 196 | | |
| 197 | | pDisk->tracks = tracks; |
| 198 | | pDisk->heads = heads; |
| 199 | | pDisk->first_sector_id = first_sector_id; |
| 200 | | pDisk->sec_per_track = sec_per_track; |
| 201 | | pDisk->sector_length = sector_length; |
| 202 | | pDisk->offset = offset_track_zero; |
| 203 | | |
| 204 | | pDisk->track_divider = track_skipping ? 2 : 1; |
| 205 | | |
| 206 | | floppy_drive_set_geometry_absolute(img, tracks * pDisk->track_divider, heads ); |
| 207 | | |
| 208 | | pDisk->image_size = pDisk->tracks * pDisk->heads * pDisk->sec_per_track * pDisk->sector_length; |
| 209 | | |
| 210 | | /* setup a new ddam map */ |
| 211 | | pDisk->ddam_map_size = ((pDisk->tracks * pDisk->heads * pDisk->sec_per_track)+7)>>3; |
| 212 | | pDisk->ddam_map = (UINT8 *) image_realloc(img, pDisk->ddam_map, pDisk->ddam_map_size); |
| 213 | | |
| 214 | | if (pDisk->ddam_map!=NULL) |
| 215 | | { |
| 216 | | memset(pDisk->ddam_map, 0, pDisk->ddam_map_size); |
| 217 | | } |
| 218 | | |
| 219 | | |
| 220 | | /* from sector length calculate N value for sector id's */ |
| 221 | | /* N = 0 for 128, N = 1 for 256, N = 2 for 512 ... */ |
| 222 | | N = (pDisk->sector_length); |
| 223 | | ShiftCount = 0; |
| 224 | | |
| 225 | | if (N!=0) |
| 226 | | { |
| 227 | | while ((N & 0x080000000)==0) |
| 228 | | { |
| 229 | | N = N<<1; |
| 230 | | ShiftCount++; |
| 231 | | } |
| 232 | | |
| 233 | | /* get left-shift required to shift 1 to this |
| 234 | | power of 2 */ |
| 235 | | |
| 236 | | pDisk->N = (31 - ShiftCount)-7; |
| 237 | | } |
| 238 | | else |
| 239 | | { |
| 240 | | pDisk->N = 0; |
| 241 | | } |
| 242 | | } |
| 243 | | |
| 244 | | |
| 245 | | /* seek to track/head/sector relative position in image file */ |
| 246 | | static int basicdsk_seek(basicdsk * w, UINT8 t, UINT8 h, UINT8 s) |
| 247 | | { |
| 248 | | unsigned long offset; |
| 249 | | |
| 250 | | /* allow two additional tracks */ |
| 251 | | if (t >= w->tracks + 2) |
| 252 | | { |
| 253 | | logerror("basicdsk track %d >= %d\n", t, w->tracks + 2); |
| 254 | | return 0; |
| 255 | | } |
| 256 | | |
| 257 | | if (h >= w->heads) |
| 258 | | { |
| 259 | | logerror("basicdsk head %d >= %d\n", h, w->heads); |
| 260 | | return 0; |
| 261 | | } |
| 262 | | |
| 263 | | if (s >= (w->first_sector_id + w->sec_per_track)) |
| 264 | | { |
| 265 | | logerror("basicdsk sector %d\n", w->sec_per_track+w->first_sector_id); |
| 266 | | return 0; |
| 267 | | } |
| 268 | | |
| 269 | | if (w->calcoffset) |
| 270 | | { |
| 271 | | offset = w->calcoffset(t, h, s, w->tracks, w->heads, w->sec_per_track, w->sector_length, w->first_sector_id, w->offset); |
| 272 | | } |
| 273 | | else |
| 274 | | { |
| 275 | | offset = 0; |
| 276 | | offset += t; |
| 277 | | offset *= w->heads; |
| 278 | | offset += h; |
| 279 | | offset *= w->sec_per_track; |
| 280 | | offset += (s-w->first_sector_id); |
| 281 | | offset *= w->sector_length; |
| 282 | | offset += w->offset; |
| 283 | | } |
| 284 | | |
| 285 | | |
| 286 | | LOG(("basicdsk seek track:%d head:%d sector:%d-> offset #0x%08lX\n", |
| 287 | | t, h, s, offset)); |
| 288 | | |
| 289 | | if (offset > w->image_size) |
| 290 | | { |
| 291 | | logerror("basicdsk seek offset %ld >= %ld\n", offset, w->image_size); |
| 292 | | return 0; |
| 293 | | } |
| 294 | | |
| 295 | | if (image_fseek(w->image_file, offset, SEEK_SET) < 0) |
| 296 | | { |
| 297 | | logerror("basicdsk seek failed\n"); |
| 298 | | return 0; |
| 299 | | } |
| 300 | | |
| 301 | | return 1; |
| 302 | | } |
| 303 | | |
| 304 | | |
| 305 | | |
| 306 | | |
| 307 | | #if 0 |
| 308 | | w->status = seek(w, w->track, w->head, w->sector); |
| 309 | | if (w->status == 0) |
| 310 | | read_sector(w); |
| 311 | | |
| 312 | | |
| 313 | | /* if a track was just formatted */ |
| 314 | | if (w->dam_cnt) |
| 315 | | { |
| 316 | | int i; |
| 317 | | for (i = 0; i < w->dam_cnt; i++) |
| 318 | | { |
| 319 | | if (w->track == w->dam_list[i][0] && |
| 320 | | w->head == w->dam_list[i][1] && |
| 321 | | w->sector == w->dam_list[i][2]) |
| 322 | | { |
| 323 | | LOG(("basicdsk reading formatted sector %d, track %d, head %d\n", w->sector, w->track, w->head)); |
| 324 | | w->data_offset = w->dam_data[i]; |
| 325 | | return; |
| 326 | | } |
| 327 | | } |
| 328 | | /* sector not found, now the track buffer is invalid */ |
| 329 | | w->dam_cnt = 0; |
| 330 | | } |
| 331 | | |
| 332 | | /* if this is the real thing */ |
| 333 | | if (w->image_file == REAL_FDD) |
| 334 | | { |
| 335 | | int tries = 3; |
| 336 | | do { |
| 337 | | w->status = osd_fdc_get_sector(w->track, w->head, w->head, w->sector, w->buffer); |
| 338 | | tries--; |
| 339 | | } while (tries && (w->status & (STA_2_REC_N_FND | STA_2_CRC_ERR | STA_2_LOST_DAT))); |
| 340 | | /* no error bits set ? */ |
| 341 | | if ((w->status & (STA_2_REC_N_FND | STA_2_CRC_ERR | STA_2_LOST_DAT)) == 0) |
| 342 | | { |
| 343 | | /* start transferring data to the emulation now */ |
| 344 | | w->status_drq = STA_2_DRQ; |
| 345 | | if (w->callback) |
| 346 | | (*w->callback) (basicdsk_DRQ_SET); |
| 347 | | w->status |= STA_2_DRQ | STA_2_BUSY; |
| 348 | | } |
| 349 | | return; |
| 350 | | } |
| 351 | | else if (image_fread(w->image_file, w->buffer, w->sector_length) != w->sector_length) |
| 352 | | { |
| 353 | | w->status = STA_2_LOST_DAT; |
| 354 | | return; |
| 355 | | } |
| 356 | | |
| 357 | | |
| 358 | | #endif |
| 359 | | |
| 360 | | |
| 361 | | |
| 362 | | /*void basicdsk_step_callback(basicdsk *w, int drive, int direction) |
| 363 | | { |
| 364 | | w->track += direction; |
| 365 | | }*/ |
| 366 | | |
| 367 | | #if 0 |
| 368 | | /* write a sector */ |
| 369 | | static void basicdsk_write_sector(basicdsk *w) |
| 370 | | { |
| 371 | | if (w->image_file == REAL_FDD) |
| 372 | | { |
| 373 | | osd_fdc_put_sector(w->track, w->head, w->head, w->sector, w->buffer, w->write_cmd & FDC_DELETED_AM); |
| 374 | | return; |
| 375 | | } |
| 376 | | |
| 377 | | seek(w, w->track, w->head, w->sector); |
| 378 | | image_fwrite(w->image_file, w->buffer, w->data_offset) |
| 379 | | } |
| 380 | | |
| 381 | | |
| 382 | | /* write an entire track by extracting the sectors */ |
| 383 | | static void basicdsk_write_track(basicdsk *w) |
| 384 | | { |
| 385 | | if (floppy_drive_get_flag_state(drv,FLOPPY_DRIVE_DISK_WRITE_PROTECTED)) |
| 386 | | { |
| 387 | | w->status |= STA_1_WRITE_PRO; |
| 388 | | return; |
| 389 | | } |
| 390 | | #endif |
| 391 | | |
| 392 | | #if 0 |
| 393 | | UINT8 *f; |
| 394 | | int cnt; |
| 395 | | w->dam_cnt = 0; |
| 396 | | if (w->image_file != REAL_FDD && w->mode == 0) |
| 397 | | { |
| 398 | | LOG(("basicdsk write_track write protected image\n")); |
| 399 | | w->status = STA_2_WRITE_PRO; |
| 400 | | return; |
| 401 | | } |
| 402 | | |
| 403 | | memset(w->dam_list, 0xff, sizeof(w->dam_list)); |
| 404 | | memset(w->dam_data, 0x00, sizeof(w->dam_data)); |
| 405 | | |
| 406 | | f = w->buffer; |
| 407 | | LOG(("basicdsk write_track %s_LOW\n", (w->density) ? "MFM" : "FM" )); |
| 408 | | cnt = (w->density) ? TRKSIZE_DD : TRKSIZE_SD; |
| 409 | | |
| 410 | | do |
| 411 | | { |
| 412 | | while ((--cnt > 0) && (*f != 0xfe)) /* start of DAM ?? */ |
| 413 | | f++; |
| 414 | | |
| 415 | | if (cnt > 4) |
| 416 | | { |
| 417 | | int seclen; |
| 418 | | cnt -= 5; |
| 419 | | f++; /* skip FE */ |
| 420 | | w->dam_list[w->dam_cnt][0] = *f++; /* copy track number */ |
| 421 | | w->dam_list[w->dam_cnt][1] = *f++; /* copy head number */ |
| 422 | | w->dam_list[w->dam_cnt][2] = *f++; /* copy sector number */ |
| 423 | | w->dam_list[w->dam_cnt][3] = *f++; /* copy sector length */ |
| 424 | | /* sector length in bytes */ |
| 425 | | seclen = 128 << w->dam_list[w->dam_cnt][3]; |
| 426 | | LOG(("basicdsk write_track FE @%5d T:%02X H:%02X S:%02X L:%02X\n", |
| 427 | | (int)(f - w->buffer), |
| 428 | | w->dam_list[w->dam_cnt][0],w->dam_list[w->dam_cnt][1], |
| 429 | | w->dam_list[w->dam_cnt][2],w->dam_list[w->dam_cnt][3])); |
| 430 | | |
| 431 | | /* search start of DATA */ |
| 432 | | while ((--cnt > 0) && (*f != 0xf9) && (*f != 0xfa) && (*f != 0xfb)) |
| 433 | | f++; |
| 434 | | if (cnt > seclen) |
| 435 | | { |
| 436 | | cnt--; |
| 437 | | /* skip data address mark */ |
| 438 | | f++; |
| 439 | | /* set pointer to DATA to later write the sectors contents */ |
| 440 | | w->dam_data[w->dam_cnt] = (int)(f - w->buffer); |
| 441 | | w->dam_cnt++; |
| 442 | | |
| 443 | | LOG(("basicdsk write_track %02X @%5d data: %02X %02X %02X %02X ... %02X %02X %02X %02X\n", |
| 444 | | f[-1], |
| 445 | | (int)(f - w->buffer), |
| 446 | | f[0], f[1], f[2], f[3], |
| 447 | | f[seclen-4], f[seclen-3], f[seclen-2], f[seclen-1])); |
| 448 | | |
| 449 | | f += seclen; |
| 450 | | cnt -= seclen; |
| 451 | | } |
| 452 | | } |
| 453 | | } while (cnt > 0); |
| 454 | | |
| 455 | | if (w->image_file == REAL_FDD) |
| 456 | | { |
| 457 | | w->status = osd_fdc_format(w->track, w->head, w->dam_cnt, w->dam_list[0]); |
| 458 | | |
| 459 | | if ((w->status & 0xfc) == 0) |
| 460 | | { |
| 461 | | /* now put all sectors contained in the format buffer */ |
| 462 | | for (cnt = 0; cnt < w->dam_cnt; cnt++) |
| 463 | | { |
| 464 | | w->status = osd_fdc_put_sector(w->track, w->head, cnt, w->buffer[dam_data[cnt]], 0); |
| 465 | | /* bail out if an error occured */ |
| 466 | | if (w->status & 0xfc) |
| 467 | | break; |
| 468 | | } |
| 469 | | } |
| 470 | | } |
| 471 | | else |
| 472 | | { |
| 473 | | /* now put all sectors contained in the format buffer */ |
| 474 | | for (cnt = 0; cnt < w->dam_cnt; cnt++) |
| 475 | | { |
| 476 | | w->status = seek(w, w->track, w->head, w->dam_list[cnt][2]); |
| 477 | | if (w->status == 0) |
| 478 | | { |
| 479 | | if (image_fwrite(w->image_file, &w->buffer[w->dam_data[cnt]], w->sector_length) != w->sector_length) |
| 480 | | { |
| 481 | | w->status = STA_2_LOST_DAT; |
| 482 | | return; |
| 483 | | } |
| 484 | | } |
| 485 | | } |
| 486 | | } |
| 487 | | } |
| 488 | | #endif |
| 489 | | #if 0 |
| 490 | | if (w->image_file != REAL_FDD) |
| 491 | | { |
| 492 | | /* read normal or deleted data address mark ? */ |
| 493 | | w->status |= deleted_dam(w); |
| 494 | | } |
| 495 | | #endif |
| 496 | | |
| 497 | | |
| 498 | | #if 0 |
| 499 | | |
| 500 | | if ((data | 1) == 0xff) /* change single/double density ? */ |
| 501 | | { |
| 502 | | /* only supports FM/LO and MFM/LO */ |
| 503 | | w->density = (data & 1) ? DEN_MFM_LO : DEN_FM_LO; |
| 504 | | #if 0 |
| 505 | | if (w->image_file == REAL_FDD) |
| 506 | | osd_fdc_density(w->unit, w->density, w->tracks, w->sec_per_track, w->sec_per_track, 1); |
| 507 | | #endif |
| 508 | | return; |
| 509 | | } |
| 510 | | #endif |
| 511 | | |
| 512 | | |
| 513 | | static void basicdsk_get_id_callback(const device_config *img, chrn_id *id, int id_index, int side) |
| 514 | | { |
| 515 | | basicdsk *w = get_basicdsk(img); |
| 516 | | |
| 517 | | /* construct a id value */ |
| 518 | | id->C = w->track; |
| 519 | | id->H = side; |
| 520 | | id->R = w->first_sector_id + id_index; |
| 521 | | id->N = w->N; |
| 522 | | id->data_id = w->first_sector_id + id_index; |
| 523 | | id->flags = 0; |
| 524 | | |
| 525 | | /* get dam */ |
| 526 | | if (basicdsk_get_ddam(img, w->track, side, id->R)) |
| 527 | | { |
| 528 | | id->flags |= ID_FLAG_DELETED_DATA; |
| 529 | | } |
| 530 | | |
| 531 | | } |
| 532 | | |
| 533 | | static int basicdsk_get_sectors_per_track(const device_config *img, int side) |
| 534 | | { |
| 535 | | basicdsk *w = get_basicdsk(img); |
| 536 | | |
| 537 | | /* attempting to access an invalid side or track? */ |
| 538 | | if ((side>=w->heads) || (w->track>=w->tracks)) |
| 539 | | { |
| 540 | | /* no sectors */ |
| 541 | | return 0; |
| 542 | | } |
| 543 | | /* return number of sectors per track */ |
| 544 | | return w->sec_per_track; |
| 545 | | } |
| 546 | | |
| 547 | | static void basicdsk_seek_callback(const device_config *img, int physical_track) |
| 548 | | { |
| 549 | | basicdsk *w = get_basicdsk(img); |
| 550 | | assert(w->track_divider); |
| 551 | | w->track = physical_track / w->track_divider; |
| 552 | | } |
| 553 | | |
| 554 | | static void basicdsk_write_sector_data_from_buffer(const device_config *img, int side, int index1, const char *ptr, int length, int ddam) |
| 555 | | { |
| 556 | | basicdsk *w = get_basicdsk(img); |
| 557 | | |
| 558 | | if (basicdsk_seek(w, w->track, side, index1)&&w->mode) |
| 559 | | { |
| 560 | | image_fwrite(w->image_file, ptr, length); |
| 561 | | } |
| 562 | | |
| 563 | | basicdsk_set_ddam(img, w->track, side, index1, ddam); |
| 564 | | } |
| 565 | | |
| 566 | | static void basicdsk_read_sector_data_into_buffer(const device_config *img, int side, int index1, char *ptr, int length) |
| 567 | | { |
| 568 | | basicdsk *w = get_basicdsk(img); |
| 569 | | |
| 570 | | if (basicdsk_seek(w, w->track, side, index1)) |
| 571 | | { |
| 572 | | image_fread(w->image_file, ptr, length); |
| 573 | | } |
| 574 | | } |
| 575 | | |
| 576 | | |
| 577 | | |
| 578 | | void legacybasicdsk_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 579 | | { |
| 580 | | switch(state) |
| 581 | | { |
| 582 | | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 583 | | case MESS_DEVINFO_INT_TYPE: info->i = IO_FLOPPY; break; |
| 584 | | case MESS_DEVINFO_INT_READABLE: info->i = 1; break; |
| 585 | | case MESS_DEVINFO_INT_WRITEABLE: info->i = 1; break; |
| 586 | | case MESS_DEVINFO_INT_CREATABLE: info->i = 1; break; |
| 587 | | |
| 588 | | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 589 | | case MESS_DEVINFO_PTR_START: info->start = DEVICE_START_NAME(basicdsk_floppy); break; |
| 590 | | case MESS_DEVINFO_PTR_UNLOAD: info->unload = DEVICE_IMAGE_UNLOAD_NAME(basicdsk_floppy); break; |
| 591 | | |
| 592 | | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 593 | | case MESS_DEVINFO_STR_DEV_FILE: strcpy(info->s = device_temp_str(), __FILE__); break; |
| 594 | | } |
| 595 | | } |
| 596 | | |
| 597 | | |
src/mess/formats/svi_dsk.c
| r0 | r5641 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | formats/svi_dsk.c |
| 4 | |
| 5 | SVI318 disk images |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | |
| 9 | #include <string.h> |
| 10 | |
| 11 | #include "formats/svi_dsk.h" |
| 12 | #include "formats/basicdsk.h" |
| 13 | #include "driver.h" |
| 14 | |
| 15 | static FLOPPY_IDENTIFY(svi_dsk_identify) |
| 16 | { |
| 17 | *vote = ((floppy_image_size(floppy) == (172032)) || (floppy_image_size(floppy) == (346112))) ? 100 : 0; |
| 18 | return FLOPPY_ERROR_SUCCESS; |
| 19 | } |
| 20 | |
| 21 | static int svi_get_heads_per_disk(floppy_image *floppy) |
| 22 | { |
| 23 | return (floppy_image_size(floppy) == (172032)) ? 1 : 2; |
| 24 | } |
| 25 | |
| 26 | static int svi_get_tracks_per_disk(floppy_image *floppy) |
| 27 | { |
| 28 | return 40; |
| 29 | } |
| 30 | |
| 31 | static UINT64 svi_translate_offset(floppy_image *floppy, |
| 32 | int track, int head, int sector) |
| 33 | { |
| 34 | UINT64 o; |
| 35 | if ((track==0) && (head==0)) |
| 36 | o = sector*128; |
| 37 | else |
| 38 | o = ((track*((floppy_image_size(floppy) == (172032)) ? 1 : 2)+head)*17+sector)*256-2048; /* (17*256)-(18*128)=2048 */ |
| 39 | |
| 40 | return o; |
| 41 | } |
| 42 | |
| 43 | static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset) |
| 44 | { |
| 45 | UINT64 offs; |
| 46 | /* translate the sector to a raw sector */ |
| 47 | if (!sector_is_index) |
| 48 | { |
| 49 | sector -= 1; |
| 50 | } |
| 51 | /* check to see if we are out of range */ |
| 52 | if ((head < 0) || (head >= ((floppy_image_size(floppy) == (172032)) ? 1 : 2)) || (track < 0) || (track >= 40) |
| 53 | || (sector < 0) || (sector >= ((head==0 && track==0) ? 18 : 17))) |
| 54 | return FLOPPY_ERROR_SEEKERROR; |
| 55 | |
| 56 | offs = svi_translate_offset(floppy, track, head, sector); |
| 57 | if (offset) |
| 58 | *offset = offs; |
| 59 | return FLOPPY_ERROR_SUCCESS; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | static floperr_t internal_svi_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 65 | { |
| 66 | UINT64 offset; |
| 67 | floperr_t err; |
| 68 | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 69 | if (err) |
| 70 | return err; |
| 71 | |
| 72 | floppy_image_read(floppy, buffer, offset, buflen); |
| 73 | return FLOPPY_ERROR_SUCCESS; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | static floperr_t internal_svi_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam) |
| 79 | { |
| 80 | UINT64 offset; |
| 81 | floperr_t err; |
| 82 | |
| 83 | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 84 | if (err) |
| 85 | return err; |
| 86 | |
| 87 | floppy_image_write(floppy, buffer, offset, buflen); |
| 88 | return FLOPPY_ERROR_SUCCESS; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | static floperr_t svi_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 94 | { |
| 95 | return internal_svi_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 96 | } |
| 97 | |
| 98 | static floperr_t svi_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 99 | { |
| 100 | return internal_svi_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam); |
| 101 | } |
| 102 | |
| 103 | static floperr_t svi_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 104 | { |
| 105 | return internal_svi_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 106 | } |
| 107 | |
| 108 | static floperr_t svi_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 109 | { |
| 110 | return internal_svi_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam); |
| 111 | } |
| 112 | |
| 113 | static floperr_t svi_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 114 | { |
| 115 | floperr_t err; |
| 116 | err = get_offset(floppy, head, track, sector, FALSE, NULL); |
| 117 | if (err) |
| 118 | return err; |
| 119 | |
| 120 | if (sector_length) { |
| 121 | *sector_length = (head==0 && track==0) ? 128 : 256; |
| 122 | } |
| 123 | return FLOPPY_ERROR_SUCCESS; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | static floperr_t svi_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 129 | { |
| 130 | sector_index += 1; |
| 131 | if (cylinder) |
| 132 | *cylinder = track; |
| 133 | if (side) |
| 134 | *side = head; |
| 135 | if (sector) |
| 136 | *sector = sector_index; |
| 137 | if (flags) |
| 138 | /* TODO: read DAM or DDAM and determine flags */ |
| 139 | *flags = 0; |
| 140 | return svi_get_sector_length(floppy, head, track, sector_index, sector_length); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static FLOPPY_CONSTRUCT(svi_dsk_construct) |
| 145 | { |
| 146 | struct FloppyCallbacks *callbacks; |
| 147 | callbacks = floppy_callbacks(floppy); |
| 148 | callbacks->read_sector = svi_read_sector; |
| 149 | callbacks->write_sector = svi_write_sector; |
| 150 | callbacks->read_indexed_sector = svi_read_indexed_sector; |
| 151 | callbacks->write_indexed_sector = svi_write_indexed_sector; |
| 152 | callbacks->get_sector_length = svi_get_sector_length; |
| 153 | callbacks->get_heads_per_disk = svi_get_heads_per_disk; |
| 154 | callbacks->get_tracks_per_disk = svi_get_tracks_per_disk; |
| 155 | callbacks->get_indexed_sector_info = svi_get_indexed_sector_info; |
| 156 | |
| 157 | return FLOPPY_ERROR_SUCCESS; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | /* ----------------------------------------------------------------------- */ |
| 163 | |
| 164 | FLOPPY_OPTIONS_START( svi318 ) |
| 165 | FLOPPY_OPTION( svi_dsk, "dsk", "SVI-318 floppy disk image", svi_dsk_identify, svi_dsk_construct, NULL) |
| 166 | FLOPPY_OPTION( svi_cpm, "dsk", "SVI-728 DSDD CP/M disk image", basicdsk_identify_default, basicdsk_construct_default, |
| 167 | HEADS([2]) |
| 168 | TRACKS([40]) |
| 169 | SECTORS([17]) |
| 170 | SECTOR_LENGTH([256]) |
| 171 | FIRST_SECTOR_ID([1])) |
| 172 | FLOPPY_OPTIONS_END |