src/mess/formats/thom_dsk.c
| r5702 | r5703 | |
| 1 | | /********************************************************************** |
| 1 | /********************************************************************* |
| 2 | 2 | |
| 3 | | Copyright (C) Antoine Mine' 2006 |
| 3 | formats/thom_dsk.c |
| 4 | 4 | |
| 5 | | Thomson 8-bit computers |
| 5 | Thomson disk images |
| 6 | |
| 7 | Based on work of Antoine Mine' |
| 6 | 8 | |
| 7 | | **********************************************************************/ |
| 9 | *********************************************************************/ |
| 8 | 10 | |
| 11 | #include <string.h> |
| 9 | 12 | #include "driver.h" |
| 10 | | #include "device.h" |
| 11 | 13 | #include "formats/thom_dsk.h" |
| 12 | | #include "includes/thomson.h" |
| 14 | #include "formats/basicdsk.h" |
| 13 | 15 | |
| 14 | | #define VERBOSE 0 /* 0, 1 or 2 */ |
| 16 | #define SAP_DSK_TAG "sapdsktag" |
| 15 | 17 | |
| 16 | | #define PRINT(x) mame_printf_info x |
| 18 | static const int sap_magic_num = 0xB3; /* simple XOR crypt */ |
| 17 | 19 | |
| 18 | | #define LOG(x) do { if (VERBOSE > 0) logerror x; } while (0) |
| 19 | | #define VLOG(x) do { if (VERBOSE > 1) logerror x; } while (0) |
| 20 | 20 | |
| 21 | static const char sap_header[] = |
| 22 | "SYSTEME D'ARCHIVAGE PUKALL S.A.P. " |
| 23 | "(c) Alexandre PUKALL Avril 1998"; |
| 21 | 24 | |
| 22 | | /********************* floppy image / drive *********************/ |
| 23 | 25 | |
| 24 | | |
| 25 | | /* sector flags */ |
| 26 | | #define THOM_SECT_OK 0 |
| 27 | | #define THOM_SECT_INVALID 1 /* invalid header */ |
| 28 | | |
| 29 | | |
| 30 | | typedef struct |
| 26 | static const UINT16 sap_crc[] = |
| 31 | 27 | { |
| 28 | 0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387, |
| 29 | 0x8408, 0x9489, 0xa50a, 0xb58b, 0xc60c, 0xd68d, 0xe70e, 0xf78f, |
| 30 | }; |
| 32 | 31 | |
| 33 | | /* disk info */ |
| 34 | | thom_floppy_type type; |
| 35 | | UINT8* data; /* raw contents */ |
| 36 | | UINT8 sides; /* 1 or 2 */ |
| 37 | | DENSITY density; |
| 38 | | UINT16 sector_size; /* 128 or 256 */ |
| 39 | | UINT16 sectors; /* sectors per track: 16 or 400 (QDD) */ |
| 40 | | UINT16 tracks; /* tracks per side: 40, 80, or 1 */ |
| 41 | | UINT8 sect_flag[2560]; /* one flag per sector */ |
| 42 | | |
| 43 | | /* current state */ |
| 44 | | UINT8 cur_track; |
| 45 | | |
| 46 | | /* for write-back */ |
| 47 | | const device_config* image; |
| 48 | | UINT8 has_changed; |
| 49 | | |
| 50 | | } thom_floppy_drive; |
| 51 | | |
| 52 | | static thom_floppy_drive thom_floppy_drives[4]; |
| 53 | | |
| 54 | | static DENSITY thom_density; |
| 55 | | |
| 56 | | |
| 57 | | |
| 58 | | /*********************** floppy access ************************/ |
| 59 | | |
| 60 | | /* default interlacing factor for floppies */ |
| 61 | | static const int thom_floppy_interlace = 7; |
| 62 | | |
| 63 | | /* fixed interlacing map for QDDs */ |
| 64 | | static int thom_qdd_map[400]; |
| 65 | | |
| 66 | | |
| 67 | | static void thom_qdd_compute_map ( void ) |
| 32 | struct sap_dsk_tag |
| 68 | 33 | { |
| 69 | | /* this map is hardcoded in the QDD BIOS */ |
| 70 | | static const int p[6][4] = |
| 71 | | { |
| 72 | | { 20, 2, 14, 8 }, { 21, 19, 13, 7 }, |
| 73 | | { 22, 18, 12, 6 }, { 23, 17, 11, 5 }, |
| 74 | | { 24, 16, 10, 4 }, { 1, 15, 9, 3 } |
| 75 | | }; |
| 76 | | static const int q[4] = { 0, 8, 4, 12 }; |
| 77 | | int t, s; |
| 78 | | for ( t = 0; t < 24; t++ ) |
| 79 | | { |
| 80 | | for ( s = 0; s < 16; s++ ) |
| 81 | | { |
| 82 | | thom_qdd_map[ t*16 + s ] = p[ t/4 ][ s%4 ] * 16 + (s/4) + 4*(t%4); |
| 83 | | } |
| 84 | | } |
| 85 | | for ( s = 0; s < 16; s++ ) |
| 86 | | { |
| 87 | | thom_qdd_map[ 24*16 + s ] = q[ s%4 ] + (s/4); |
| 88 | | } |
| 89 | | LOG(( "thom_qdd_compute_map: QDD sector map\n" )); |
| 90 | | for ( s = 0; s < 25; s++ ) |
| 91 | | { |
| 92 | | for ( t = 0; t < 16; t++ ) |
| 93 | | { |
| 94 | | VLOG(( "%03i,", thom_qdd_map[ s*16 + t ] + 1 )); |
| 95 | | } |
| 96 | | VLOG(( "\n" )); |
| 97 | | } |
| 98 | | } |
| 34 | int heads; |
| 35 | int tracks; |
| 36 | int sector_size; |
| 37 | }; |
| 99 | 38 | |
| 100 | 39 | |
| 101 | | |
| 102 | | static UINT8* thom_floppy_sector_ptr ( thom_floppy_drive* d, int sector, int track, int side ) |
| 40 | static struct sap_dsk_tag *get_tag(floppy_image *floppy) |
| 103 | 41 | { |
| 104 | | if ( track >= d->tracks ) |
| 105 | | track = d->tracks - 1; |
| 106 | | if ( track < 0 ) |
| 107 | | track = 0; |
| 108 | | |
| 109 | | if ( d->data && sector >= 1 && sector <= d->sectors && side >= 0 && side < d->sides ) |
| 110 | | { |
| 111 | | int s = track * d->sectors + sector - 1; |
| 112 | | if ( d -> type == THOM_FLOPPY_QDD ) |
| 113 | | { |
| 114 | | if ( s >= 0 && s < 400 ) |
| 115 | | s = thom_qdd_map[ s ]; |
| 116 | | else |
| 117 | | return NULL; |
| 118 | | } |
| 119 | | return d->data + ( side * d->tracks * d->sectors + s ) * d->sector_size; |
| 120 | | } |
| 121 | | else |
| 122 | | return NULL; |
| 42 | struct sap_dsk_tag *tag; |
| 43 | tag = floppy_tag(floppy, SAP_DSK_TAG); |
| 44 | return tag; |
| 123 | 45 | } |
| 124 | 46 | |
| 125 | 47 | |
| 126 | | |
| 127 | | static UINT8* thom_floppy_sector_flag ( thom_floppy_drive* d, |
| 128 | | int sector, int track, int side ) |
| 48 | static FLOPPY_IDENTIFY(sap_dsk_identify) |
| 129 | 49 | { |
| 130 | | int s = track * d->sectors + sector - 1; |
| 131 | | if ( d -> type == THOM_FLOPPY_QDD ) |
| 132 | | s = thom_qdd_map[ s ]; |
| 133 | | return |
| 134 | | d->sect_flag + side * d->tracks * d->sectors + s; |
| 135 | | } |
| 136 | | |
| 137 | | |
| 138 | | |
| 139 | | /* dump some info on the floppy for the user */ |
| 140 | | static void thom_floppy_show ( thom_floppy_drive* d ) |
| 141 | | { |
| 142 | | /* format */ |
| 143 | | PRINT (( "thom_floppy: drive %li, %s floppy, %s-sided, %i-byte sectors, %s, %i track(s), %i sectors, %i KB\n", |
| 144 | | (long) (d - thom_floppy_drives), |
| 145 | | (d->type == THOM_FLOPPY_5_1_4) ? "5\"1/4" : |
| 146 | | (d->type == THOM_FLOPPY_3_1_2) ? "3\"1/2" : |
| 147 | | (d->type == THOM_FLOPPY_QDD) ? "QDD" : "unknown type", |
| 148 | | (d->sides == 1) ? "one" : "two", |
| 149 | | d->sector_size, |
| 150 | | (d->density == DEN_FM_LO) ? "FM" : "MFM", |
| 151 | | d->tracks, d->sectors, |
| 152 | | (d->sides * d->tracks * d->sectors * d->sector_size) / 1024 )); |
| 153 | | LOG (( "thom_floppy: drive %d loaded\n", (int)(FPTR)(d - thom_floppy_drives) )); |
| 154 | | |
| 155 | | if ( d->data ) |
| 156 | | { |
| 157 | | UINT8* dir = d->data + 20 * 16 * d->sector_size; |
| 158 | | char name[] = "01234567"; |
| 159 | | int i, j; |
| 160 | | |
| 161 | | /* BASIC name */ |
| 162 | | memcpy( name, dir, 8 ); |
| 163 | | for ( i = 0; name[i]; i++ ) |
| 164 | | { |
| 165 | | if ( name[i] < ' ' || name[i] >= 127 ) |
| 166 | | name[i] = '?'; |
| 167 | | } |
| 168 | | PRINT (( "thom_floppy: floppy name \"%s\"\n", name )); |
| 169 | | |
| 170 | | /* BASIC directory */ |
| 171 | | dir += d->sector_size*2; |
| 172 | | for ( j = 0; j < d->sector_size * 14 / 32; j++, dir += 32 ) |
| 173 | | { |
| 174 | | char name[] = "01234567.ABC"; |
| 175 | | char comment[] = "01234567"; |
| 176 | | if ( *dir == 255 ) |
| 177 | | break; /* end of directory */ |
| 178 | | if ( *dir < ' ' || *dir > 127 ) |
| 179 | | continue; /* empty entry */ |
| 180 | | memcpy( name, dir, 8 ); |
| 181 | | memcpy( name+9, dir+8, 3 ); |
| 182 | | for ( i = 0; name[i]; i++) |
| 183 | | { |
| 184 | | if ( name[i] < ' ' || name[i] >= 127 ) |
| 185 | | name[i] = '?'; |
| 186 | | } |
| 187 | | memcpy( comment, dir + 16, 8 ); |
| 188 | | for ( i = 0; comment[i]; i++) |
| 189 | | { |
| 190 | | if ( comment[i] < ' ' || comment[i] >= 127 ) |
| 191 | | comment[i] = '?'; |
| 192 | | } |
| 193 | | PRINT (( "thom_floppy: file \"%s\" type=%s,%s comment=\"%s\"\n", name, |
| 194 | | (dir[11] == 0) ? "bas" : (dir[11] == 1) ? "dat" : |
| 195 | | (dir[11] == 2) ? "bin" : (dir[11] == 3) ? "asm" : "???", |
| 196 | | (dir[12] == 0) ? "a" : (dir[12] == 0xff) ? "b" : "?", comment )); |
| 197 | | } |
| 50 | char header[0x100]; |
| 51 | floppy_image_read(floppy, header, 1, sizeof(sap_header)); |
| 52 | if (!memcmp( header, sap_header, sizeof(sap_header) ) ) |
| 53 | { |
| 54 | *vote= 100; |
| 55 | } else { |
| 56 | *vote = 0; |
| 198 | 57 | } |
| 58 | return FLOPPY_ERROR_SUCCESS; |
| 199 | 59 | } |
| 200 | | |
| 201 | | |
| 202 | | |
| 203 | | static thom_floppy_drive* thom_floppy_drive_of_image ( const device_config *image ) |
| 60 | static int sap_get_heads_per_disk(floppy_image *floppy) |
| 204 | 61 | { |
| 205 | | return thom_floppy_drives + image_index_in_device( image ); |
| 62 | return 1; |
| 206 | 63 | } |
| 207 | 64 | |
| 208 | | |
| 209 | | |
| 210 | | static void thom_floppy_seek ( const device_config *image, int physical_track ) |
| 65 | static int sap_get_tracks_per_disk(floppy_image *floppy) |
| 211 | 66 | { |
| 212 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 213 | | thom_floppy_active( image->machine, 0 ); |
| 214 | | if ( physical_track < 0 ) |
| 215 | | physical_track = 0; |
| 216 | | if ( physical_track >= 80 ) |
| 217 | | physical_track = 79; |
| 218 | | //LOG(( "%f thom_floppy_seek: dev=%d track=%i\n", attotime_to_double(timer_get_time(machine)), (int)(FPTR)(d - thom_floppy_drives), physical_track )); |
| 219 | | d->cur_track = physical_track; |
| 67 | return get_tag(floppy)->tracks; |
| 220 | 68 | } |
| 221 | 69 | |
| 222 | 70 | |
| 223 | | |
| 224 | | static int thom_floppy_get_sectors_per_track( const device_config *image, int side ) |
| 71 | static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset) |
| 225 | 72 | { |
| 226 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 227 | | VLOG(( "thom_floppy_get_sectors_per_track: track=%i/%i dens=%s/%s\n", d->cur_track, d->tracks, |
| 228 | | (thom_density == DEN_FM_LO) ? "FM" : "MFM", |
| 229 | | (d->density == DEN_FM_LO) ? "FM" : "MFM" )); |
| 230 | | return d->sectors; |
| 231 | | } |
| 232 | | |
| 233 | | |
| 234 | | |
| 235 | | static void thom_floppy_get_id ( const device_config* image, chrn_id* id, |
| 236 | | int id_index, int physical_side ) |
| 237 | | { |
| 238 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 239 | | int trk = d->cur_track; |
| 240 | | if ( trk >= d->tracks ) |
| 241 | | trk = d->tracks - 1; |
| 242 | | VLOG(( "thom_floppy_get_id: dev=%d track=%i idx=%i phy-side=%i dens=%s/%s\n", |
| 243 | | (int)(FPTR)(d - thom_floppy_drives), trk, id_index, physical_side, |
| 244 | | (thom_density == DEN_FM_LO) ? "FM" : "MFM", |
| 245 | | (d->density == DEN_FM_LO) ? "FM" : "MFM" )); |
| 246 | | thom_floppy_active( image->machine, 0 ); |
| 247 | | if ( thom_floppy_sector_ptr( d, id_index+1, trk, 0 ) && |
| 248 | | #if 0 |
| 249 | | /* fails with bobwinter */ |
| 250 | | * thom_floppy_sector_flag( d, id_index+1, trk, 0 ) == |
| 251 | | THOM_SECT_OK && |
| 252 | | #endif |
| 253 | | thom_density == d->density ) |
| 73 | UINT64 offs; |
| 74 | /* translate the sector to a raw sector */ |
| 75 | if (!sector_is_index) |
| 254 | 76 | { |
| 255 | | int index = ( id_index * thom_floppy_interlace ) % d->sectors; |
| 256 | | id->C = d->cur_track; |
| 257 | | id->H = physical_side; |
| 258 | | id->R = 1 + index; |
| 259 | | id->N = (d->sector_size == 256) ? 1 : 0; |
| 260 | | id->data_id = 1 + index; |
| 261 | | id->flags = 0; |
| 77 | sector -= 1; |
| 262 | 78 | } |
| 263 | | else |
| 264 | | { |
| 265 | | /* error */ |
| 266 | | id->C = 0; |
| 267 | | id->H = 0; |
| 268 | | id->R = 0; |
| 269 | | id->N = 0; |
| 270 | | id->data_id = 0; |
| 271 | | id->flags = 0; |
| 272 | | logerror( "thom_floppy_get_id: invalid operation dev=%li track=%i idx=%i phy-side=%i dens=%s/%s\n", |
| 273 | | (long) (d - thom_floppy_drives), d->cur_track, id_index, physical_side, |
| 274 | | (thom_density == DEN_FM_LO) ? "FM" : "MFM", |
| 275 | | (d->density == DEN_FM_LO) ? "FM" : "MFM" ); |
| 276 | | } |
| 277 | | } |
| 79 | /* check to see if we are out of range */ |
| 80 | if ((head < 0) || (head >= 1) || (track < 0) || (track >=get_tag(floppy)->tracks) |
| 81 | || (sector < 0) || (sector >= 16)) |
| 82 | return FLOPPY_ERROR_SEEKERROR; |
| 278 | 83 | |
| 279 | | |
| 280 | | |
| 281 | | static void thom_floppy_read_sector_data_into_buffer ( const device_config* image, int side, int index1, char *ptr, int length ) |
| 282 | | { |
| 283 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 284 | | UINT8* src = thom_floppy_sector_ptr( d, index1, d->cur_track, 0 ); |
| 285 | | thom_floppy_active( image->machine, 0 ); |
| 286 | | LOG(( "thom_floppy_read_sector_data_into_buffer: dev=%d track=%i idx=%i side=%i len=%i\n", (int)(FPTR)(d - thom_floppy_drives), d->cur_track, index1, side, length )); |
| 287 | | if ( length > d->sector_size ) |
| 288 | | { |
| 289 | | logerror( "thom_floppy_read_sector_data_into_buffer: sector size %i truncated to %i\n", length, d->sector_size ); |
| 290 | | length = d->sector_size; |
| 291 | | } |
| 292 | | if ( src ) |
| 293 | | memcpy( ptr, src, length ); |
| 84 | offs = 0x42 + track * ((get_tag(floppy)->sector_size + 6)* 16) + sector * (get_tag(floppy)->sector_size + 6) + 4; |
| 85 | if (offset) |
| 86 | *offset = offs; |
| 87 | return FLOPPY_ERROR_SUCCESS; |
| 294 | 88 | } |
| 295 | 89 | |
| 296 | 90 | |
| 297 | 91 | |
| 298 | | static void thom_floppy_write_sector_data_from_buffer ( const device_config *image, int side, int data_id, const char *ptr, int length, int ddam ) |
| 92 | static floperr_t internal_sap_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 299 | 93 | { |
| 300 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 301 | | UINT8* dst = thom_floppy_sector_ptr( d, data_id, d->cur_track, 0 ); |
| 302 | | thom_floppy_active( image->machine, 1 ); |
| 303 | | LOG(( "thom_floppy_write_sector_data_into_buffer: dev=%d track=%i idx=%i side=%i ddam=%i len=%i\n", (int)(FPTR)(d - thom_floppy_drives), d->cur_track, data_id, side, ddam, length )); |
| 304 | | if ( length > d->sector_size ) |
| 305 | | { |
| 306 | | logerror( "thom_floppy_write_sector_data_from_buffer: sector size %i truncated to %i\n", length, d->sector_size ); |
| 307 | | length = d->sector_size; |
| 94 | UINT64 offset; |
| 95 | floperr_t err; |
| 96 | int i; |
| 97 | UINT8 *buf; |
| 98 | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 99 | if (err) |
| 100 | return err; |
| 101 | |
| 102 | floppy_image_read(floppy, buffer, offset, buflen); |
| 103 | buf = (UINT8*)buffer; |
| 104 | for (i=0;i<buflen;i++) { |
| 105 | buf[i] ^= sap_magic_num; |
| 308 | 106 | } |
| 309 | | if ( dst ) |
| 310 | | { |
| 311 | | memcpy( dst, ptr, length ); |
| 312 | | d->has_changed = 1; |
| 313 | | } |
| 107 | return FLOPPY_ERROR_SUCCESS; |
| 314 | 108 | } |
| 315 | 109 | |
| 316 | 110 | |
| 317 | 111 | |
| 318 | | static void thom_floppy_format_sector ( const device_config *image, int side, int sector_index, int c, int h, int r, int n, int filler ) |
| 112 | static floperr_t internal_sap_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam) |
| 319 | 113 | { |
| 320 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 321 | | int sector_size = 128 << n; |
| 322 | | UINT8* dst; |
| 323 | | thom_floppy_active( image->machine, 1 ); |
| 324 | | LOG(( "thom_floppy_format_sector: dev=%d track=%i/%i side=%i/%i idx=%i/%i c=%i h=%i r=%i n=%i filler=$%02X\n", |
| 325 | | (int)(FPTR)(d - thom_floppy_drives), |
| 326 | | d->cur_track, d->tracks, side, d->sides, sector_index, d->sectors, |
| 327 | | c, h, r, n, filler )); |
| 114 | UINT64 offset; |
| 115 | floperr_t err; |
| 116 | UINT8 *buf; |
| 117 | int i; |
| 328 | 118 | |
| 329 | | if ( d -> type != THOM_FLOPPY_QDD ) |
| 330 | | { |
| 331 | | if ( sector_size != d->sector_size || ( d->cur_track >= d->tracks && d->tracks == 40 ) ) |
| 332 | | { |
| 333 | | /* need to change image params */ |
| 334 | | if ( d->cur_track >= 40 ) |
| 335 | | d->tracks = 80; |
| 336 | | d->sector_size = sector_size; |
| 337 | | d->density = thom_density; |
| 338 | | d->data = realloc( d->data, d->sides * d->tracks * d->sectors * d->sector_size ); |
| 339 | | LOG (( "thom_floppy_format_sector: image enlarged to sides=%i tracks=%i sectors=%i sector_size=%i\n",d->sides, d->tracks, d->sectors, d->sector_size )); |
| 340 | | assert( d->data ); |
| 341 | | } |
| 342 | | } |
| 119 | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 120 | if (err) |
| 121 | return err; |
| 343 | 122 | |
| 344 | | dst = thom_floppy_sector_ptr( d, r, d->cur_track, 0 ); |
| 345 | | if ( dst ) |
| 346 | | { |
| 347 | | memset( dst, filler, d->sector_size ); |
| 348 | | * thom_floppy_sector_flag( d, r, d->cur_track, 0 ) = THOM_SECT_OK; |
| 349 | | d->has_changed = 1; |
| 123 | buf = (UINT8*)buffer; |
| 124 | for (i=0;i<buflen;i++) { |
| 125 | buf[i] ^= sap_magic_num; |
| 350 | 126 | } |
| 351 | | else |
| 352 | | logerror( "thom_floppy_format_sector: invalid operation\n" ); |
| 127 | floppy_image_write(floppy, buffer, offset, buflen); |
| 128 | return FLOPPY_ERROR_SUCCESS; |
| 353 | 129 | } |
| 354 | 130 | |
| 355 | 131 | |
| 356 | | |
| 357 | | static const floppy_interface thom_floppy_interface = |
| 132 | static floperr_t sap_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 358 | 133 | { |
| 359 | | thom_floppy_seek, |
| 360 | | thom_floppy_get_sectors_per_track, |
| 361 | | thom_floppy_get_id, |
| 362 | | thom_floppy_read_sector_data_into_buffer, |
| 363 | | thom_floppy_write_sector_data_from_buffer, |
| 364 | | NULL, |
| 365 | | NULL, |
| 366 | | thom_floppy_format_sector |
| 367 | | }; |
| 368 | | |
| 369 | | |
| 370 | | |
| 371 | | /*********************** .fd format ************************/ |
| 372 | | |
| 373 | | |
| 374 | | static int thom_floppy_fd_load ( const device_config* image ) |
| 375 | | { |
| 376 | | thom_floppy_drive* d = thom_floppy_drives + image_index_in_device( image ); |
| 377 | | int size = image_length( image ); |
| 378 | | |
| 379 | | /* O-sized => create */ |
| 380 | | if ( !size ) |
| 381 | | { |
| 382 | | PRINT(( "new image created in drive %i\n", image_index_in_device( image ) )); |
| 383 | | d->sides = 1; |
| 384 | | d->sectors = 16; |
| 385 | | d->tracks = 40; |
| 386 | | d->sector_size = 128; |
| 387 | | d->density = DEN_FM_LO; |
| 388 | | d->type = THOM_FLOPPY_5_1_4; |
| 389 | | d->data = malloc( d->sector_size * d->sectors * d->tracks * d->sides ); |
| 390 | | assert( d->data ); |
| 391 | | memset( d->data, 0xff, d->sector_size * d->sectors * d->tracks * d->sides ); |
| 392 | | return INIT_PASS; |
| 393 | | } |
| 394 | | |
| 395 | | /* load */ |
| 396 | | d->data = malloc( size ); |
| 397 | | assert( d->data ); |
| 398 | | if ( image_fread( image, d->data, size ) != size ) |
| 399 | | { |
| 400 | | logerror( "thom_floppy_fd_load: read error\n" ); |
| 401 | | goto error; |
| 402 | | } |
| 403 | | |
| 404 | | /* guess format: TODO two-sided images */ |
| 405 | | if ( size == 81920 ) |
| 406 | | { |
| 407 | | d->type = THOM_FLOPPY_5_1_4; |
| 408 | | d->tracks = 40; |
| 409 | | d->sectors = 16; |
| 410 | | d->sides = 1; |
| 411 | | d->density = DEN_FM_LO; |
| 412 | | d->sector_size = 128; |
| 413 | | } |
| 414 | | else if ( size == 163840 ) |
| 415 | | { |
| 416 | | #if 0 |
| 417 | | /* this undocumented case can actually be built by the TO9 */ |
| 418 | | d->type = THOM_FLOPPY_3_1_2; |
| 419 | | d->tracks = 80; |
| 420 | | d->sectors = 16; |
| 421 | | d->sides = 1; |
| 422 | | d->density = DEN_FM_LO; |
| 423 | | d->sector_size = 128; |
| 424 | | #endif |
| 425 | | d->type = THOM_FLOPPY_5_1_4; |
| 426 | | d->tracks = 40; |
| 427 | | d->sectors = 16; |
| 428 | | d->sides = 1; |
| 429 | | d->density = DEN_MFM_LO; |
| 430 | | d->sector_size = 256; |
| 431 | | } |
| 432 | | else if ( size == 327680 ) |
| 433 | | { |
| 434 | | d->type = THOM_FLOPPY_3_1_2; |
| 435 | | d->tracks = 80; |
| 436 | | d->sectors = 16; |
| 437 | | d->sides = 1; |
| 438 | | d->density = DEN_MFM_LO; |
| 439 | | d->sector_size = 256; |
| 440 | | } |
| 441 | | else |
| 442 | | { |
| 443 | | logerror( "thom_floppy_fd_load: disk format not recognized\n" ); |
| 444 | | goto error; |
| 445 | | } |
| 446 | | |
| 447 | | memset( d->sect_flag, THOM_SECT_OK, sizeof( d->sect_flag ) ); |
| 448 | | |
| 449 | | thom_floppy_show( d ); |
| 450 | | assert( size == d->sides * d->tracks * d->sectors * d->sector_size ); |
| 451 | | |
| 452 | | return INIT_PASS; |
| 453 | | |
| 454 | | error: |
| 455 | | PRINT(( "could not load floppy image in drive %i\n", image_index_in_device( image ) )); |
| 456 | | if ( d->data ) |
| 457 | | free( d->data ); |
| 458 | | d->data = NULL; |
| 459 | | d->type = THOM_FLOPPY_NONE; |
| 460 | | return INIT_FAIL; |
| 134 | return internal_sap_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 461 | 135 | } |
| 462 | 136 | |
| 463 | | |
| 464 | | |
| 465 | | static void thom_floppy_fd_save ( const device_config* image ) |
| 137 | static floperr_t sap_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 466 | 138 | { |
| 467 | | thom_floppy_drive* d = thom_floppy_drives + image_index_in_device( image ); |
| 468 | | unsigned size = d->sides * d->tracks * d->sectors * d->sector_size; |
| 469 | | if ( !d->data ) |
| 470 | | return; |
| 471 | | if ( !d->has_changed ) |
| 472 | | return; |
| 473 | | PRINT (( "thom_floppy_fd_save: saving floppy %li, %i KB\n", (long) (d - thom_floppy_drives), size / 1024 )); |
| 474 | | |
| 475 | | /* TODO: truncation */ |
| 476 | | image_fseek( image, 0, SEEK_SET ); |
| 477 | | image_fwrite( image, d->data, size ); |
| 139 | return internal_sap_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam); |
| 478 | 140 | } |
| 479 | 141 | |
| 480 | | |
| 481 | | |
| 482 | | /*********************** .qd format ************************/ |
| 483 | | |
| 484 | | /* quick-disks (QDD) are Thomson-specific 2"8 disks, with 50 KB capacity: |
| 485 | | - one spiraling physical track, with 400 128-byte sectors |
| 486 | | - all 400 sectors are stepped on each pass |
| 487 | | - the interlacing of tracks / sectors is fixed, it is optimized so that |
| 488 | | many contiguous sectors can be read in one pass |
| 489 | | */ |
| 490 | | |
| 491 | | |
| 492 | | static int thom_floppy_qd_load ( const device_config* image ) |
| 142 | static floperr_t sap_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 493 | 143 | { |
| 494 | | thom_floppy_drive* d = thom_floppy_drives + image_index_in_device( image ); |
| 495 | | int size = image_length( image ); |
| 496 | | |
| 497 | | /* only one format, TODO two-sided images */ |
| 498 | | if ( size == 51200 || size == 0 ) |
| 499 | | { |
| 500 | | d->type = THOM_FLOPPY_QDD; |
| 501 | | d->tracks = 1; |
| 502 | | d->sectors = 400; |
| 503 | | d->sides = 1; |
| 504 | | d->density = DEN_MFM_LO; |
| 505 | | d->sector_size = 128; |
| 506 | | } |
| 507 | | else |
| 508 | | { |
| 509 | | logerror( "thom_floppy_qd_load: disk format not recognized\n" ); |
| 510 | | return INIT_FAIL; |
| 511 | | } |
| 512 | | |
| 513 | | d->data = malloc( d->sector_size * d->sectors * d->tracks * d->sides ); |
| 514 | | assert( d->data ); |
| 515 | | memset( d->data, 0xff, d->sector_size * d->sectors * d->tracks * d->sides ); |
| 516 | | memset( d->sect_flag, THOM_SECT_OK, sizeof( d->sect_flag ) ); |
| 517 | | |
| 518 | | if ( !size ) |
| 519 | | { |
| 520 | | PRINT(( "new image created in drive %i\n", image_index_in_device( image ) )); |
| 521 | | return INIT_PASS; |
| 522 | | } |
| 523 | | |
| 524 | | if ( image_fread( image, d->data, size ) != size ) |
| 525 | | { |
| 526 | | logerror( "thom_floppy_qd_load: read error\n" ); |
| 527 | | goto error; |
| 528 | | } |
| 529 | | thom_floppy_show( d ); |
| 530 | | assert( size == d->sides * d->tracks * d->sectors * d->sector_size ); |
| 531 | | |
| 532 | | return INIT_PASS; |
| 533 | | |
| 534 | | error: |
| 535 | | PRINT(( "could not load floppy image in drive %i\n", image_index_in_device( image ) )); |
| 536 | | if ( d->data ) |
| 537 | | free( d->data ); |
| 538 | | d->data = NULL; |
| 539 | | d->type = THOM_FLOPPY_NONE; |
| 540 | | return INIT_FAIL; |
| 144 | return internal_sap_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 541 | 145 | } |
| 542 | 146 | |
| 543 | | |
| 544 | | |
| 545 | | static void thom_floppy_qd_save ( const device_config* image ) |
| 147 | static floperr_t sap_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 546 | 148 | { |
| 547 | | thom_floppy_drive* d = thom_floppy_drives + image_index_in_device( image ); |
| 548 | | unsigned size = d->sides * d->tracks * d->sectors * d->sector_size; |
| 549 | | if ( !d->data ) |
| 550 | | return; |
| 551 | | if ( !d->has_changed ) |
| 552 | | return; |
| 553 | | PRINT (( "thom_floppy_qd_save: saving floppy %li, %i KB\n", (long) (d - thom_floppy_drives), size / 1024 )); |
| 554 | | /* TODO: truncation */ |
| 555 | | image_fseek( image, 0, SEEK_SET ); |
| 556 | | image_fwrite( image, d->data, size ); |
| 149 | return internal_sap_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam); |
| 557 | 150 | } |
| 558 | 151 | |
| 559 | | |
| 560 | | |
| 561 | | /*********************** .sap format ************************/ |
| 562 | | |
| 563 | | /* SAP format, Copyright 1998 by Alexandre Pukall */ |
| 564 | | |
| 565 | | static const int sap_magic_num = 0xB3; /* simple XOR crypt */ |
| 566 | | |
| 567 | | |
| 568 | | static const char sap_header[] = |
| 569 | | "\001SYSTEME D'ARCHIVAGE PUKALL S.A.P. " |
| 570 | | "(c) Alexandre PUKALL Avril 1998"; |
| 571 | | |
| 572 | | |
| 573 | | static const UINT16 sap_crc[] = |
| 152 | static floperr_t sap_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 574 | 153 | { |
| 575 | | 0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387, |
| 576 | | 0x8408, 0x9489, 0xa50a, 0xb58b, 0xc60c, 0xd68d, 0xe70e, 0xf78f, |
| 577 | | }; |
| 154 | floperr_t err; |
| 155 | err = get_offset(floppy, head, track, sector, FALSE, NULL); |
| 156 | if (err) |
| 157 | return err; |
| 578 | 158 | |
| 579 | | |
| 580 | | |
| 581 | | static UINT16 thom_sap_crc( UINT8* data, int size ) |
| 582 | | { |
| 583 | | int i; |
| 584 | | UINT16 crc = 0xffff, crc2; |
| 585 | | for ( i = 0; i < size; i++ ) |
| 586 | | { |
| 587 | | crc2 = ( crc >> 4 ) ^ sap_crc[ ( crc ^ data[i] ) & 15 ]; |
| 588 | | crc = ( crc2 >> 4 ) ^ sap_crc[ ( crc2 ^ (data[i] >> 4) ) & 15 ]; |
| 159 | if (sector_length) { |
| 160 | *sector_length = get_tag(floppy)->sector_size; |
| 589 | 161 | } |
| 590 | | return crc; |
| 162 | return FLOPPY_ERROR_SUCCESS; |
| 591 | 163 | } |
| 592 | 164 | |
| 593 | 165 | |
| 594 | 166 | |
| 595 | | /* TODO: protection */ |
| 596 | | static int thom_floppy_sap_load ( const device_config* image ) |
| 167 | static floperr_t sap_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) |
| 597 | 168 | { |
| 598 | | int drive = image_index_in_device( image ); |
| 599 | | thom_floppy_drive* d = thom_floppy_drives + drive; |
| 600 | | UINT8 buf[262]; |
| 601 | | int size = image_length( image ); |
| 602 | | int i, format; |
| 603 | | |
| 604 | | /* O-sized => create */ |
| 605 | | if ( !size ) |
| 606 | | { |
| 607 | | PRINT(( "new image created in drive %i\n", image_index_in_device( image ) )); |
| 608 | | d->sides = 1; |
| 609 | | d->sectors = 16; |
| 610 | | d->tracks = 80; |
| 611 | | d->sector_size = 256; |
| 612 | | d->density = DEN_MFM_LO; |
| 613 | | d->type = THOM_FLOPPY_3_1_2; |
| 614 | | d->data = malloc( d->sector_size * d->sectors * d->tracks * d->sides ); |
| 615 | | assert( d->data ); |
| 616 | | memset( d->data, 0xff, d->sector_size * d->sectors * d->tracks * d->sides ); |
| 617 | | return INIT_PASS; |
| 618 | | } |
| 619 | | |
| 620 | | /* check header */ |
| 621 | | image_fread( image, buf, 66 ); |
| 622 | | if ( memcmp( buf+1, sap_header+1, 65 ) ) |
| 623 | | { |
| 624 | | logerror( "thom_floppy_sap_load: invalid sap header\n" ); |
| 625 | | return INIT_FAIL; |
| 626 | | } |
| 627 | | |
| 628 | | /* guess format */ |
| 629 | | image_fread( image, buf, 4 ); |
| 630 | | format = buf[0]; |
| 631 | | d->sides = 1; /* always one side per sap file */ |
| 632 | | d->density = (format==1) ? DEN_FM_LO : DEN_MFM_LO; |
| 633 | | d->sector_size = (format==1) ? 128 : 256; |
| 634 | | d->tracks = 0; |
| 635 | | d->sectors = 16; |
| 636 | | for ( i = 66; i+4 < size; i += d->sector_size + 6 ) |
| 637 | | { |
| 638 | | int fmt, prot, track, sector; |
| 639 | | image_fseek( image, i, SEEK_SET ); |
| 640 | | image_fread( image, buf, 4 ); |
| 641 | | fmt = buf[0]; |
| 642 | | prot = buf[1]; |
| 643 | | track = buf[2]; |
| 644 | | sector = buf[3]; |
| 645 | | LOG(( "thom_floppy_sap_load: drive %i found sector %i on track %i, fmt=%i, prot=%i\n", drive, sector, track, fmt, prot )); |
| 646 | | if ( sector < 1 || sector > d->sectors ) |
| 647 | | { |
| 648 | | logerror( "thom_floppy_sap_load: invalid sector %i (must be in 1..16)\n", sector ); |
| 649 | | goto error; |
| 650 | | } |
| 651 | | if ( track >= d->tracks ) |
| 652 | | d->tracks = track+1; |
| 653 | | } |
| 654 | | |
| 655 | | switch ( d->tracks ) |
| 656 | | { |
| 657 | | case 40: d->type = THOM_FLOPPY_5_1_4; break; |
| 658 | | case 80: d->type = THOM_FLOPPY_3_1_2; break; |
| 659 | | default: |
| 660 | | logerror( "thom_floppy_sap_load: invalid track number %i\n", d->tracks ); |
| 661 | | goto error; |
| 662 | | } |
| 663 | | |
| 664 | | /* load */ |
| 665 | | d->data = malloc( d->tracks * d->sectors * d->sector_size ); |
| 666 | | assert( d->data ); |
| 667 | | for ( i = 0; i < d->tracks * d->sectors; i++ ) |
| 668 | | { |
| 669 | | UINT16 crc; |
| 670 | | int j, fmt, /*prot,*/ track, sector; |
| 671 | | |
| 672 | | /* load */ |
| 673 | | image_fseek( image, i * (d->sector_size + 6) + 66, SEEK_SET ); |
| 674 | | image_fread( image, buf, d->sector_size + 6 ); |
| 675 | | fmt = buf[0]; |
| 676 | | /* prot = buf[1]; */ |
| 677 | | track = buf[2]; |
| 678 | | sector = buf[3]; |
| 679 | | |
| 680 | | /* decrypt */ |
| 681 | | for ( j = 0; j < d->sector_size; j++ ) |
| 682 | | buf[ j + 4 ] ^= sap_magic_num; |
| 683 | | |
| 684 | | memcpy( thom_floppy_sector_ptr( d, sector, track, 0 ), |
| 685 | | buf + 4, d->sector_size ); |
| 686 | | |
| 687 | | * thom_floppy_sector_flag( d, sector, track, 0 ) = |
| 688 | | (fmt == 4) ? THOM_SECT_INVALID : THOM_SECT_OK; |
| 689 | | |
| 690 | | /* check CRC */ |
| 691 | | crc = thom_sap_crc( buf, d->sector_size + 4 ); |
| 692 | | if ( ( (crc >> 8) != buf[ d->sector_size + 4 ] ) || |
| 693 | | ( (crc & 0xff) != buf[ d->sector_size + 5 ] ) ) |
| 694 | | { |
| 695 | | logerror( "thom_floppy_sap_load: CRC error for track %i, sector %i\n", track, sector ); |
| 696 | | } |
| 697 | | |
| 698 | | |
| 699 | | } |
| 700 | | |
| 701 | | thom_floppy_show( d ); |
| 702 | | |
| 703 | | return INIT_PASS; |
| 704 | | |
| 705 | | error: |
| 706 | | PRINT(( "could not load floppy image in drive %i\n", image_index_in_device( image ) )); |
| 707 | | if ( d->data ) free( d->data ); |
| 708 | | d->data = NULL; |
| 709 | | d->type = THOM_FLOPPY_NONE; |
| 710 | | return INIT_FAIL; |
| 169 | floperr_t err; |
| 170 | UINT8 header[4]; |
| 171 | UINT64 offset; |
| 172 | sector_index += 1; |
| 173 | err = get_offset(floppy, head, track, sector_index, FALSE, &offset); |
| 174 | |
| 175 | floppy_image_read(floppy, header, offset-4, 4); |
| 176 | if (cylinder) |
| 177 | *cylinder = header[2]; |
| 178 | if (side) |
| 179 | *side = head; |
| 180 | if (sector) |
| 181 | *sector = header[3]; |
| 182 | if (sector_length) |
| 183 | *sector_length = get_tag(floppy)->sector_size; |
| 184 | if (flags) |
| 185 | /* TODO: read DAM or DDAM and determine flags */ |
| 186 | *flags = 0; |
| 187 | return err; |
| 711 | 188 | } |
| 712 | 189 | |
| 713 | 190 | |
| 714 | | |
| 715 | | static void thom_floppy_sap_save ( const device_config* image ) |
| 191 | static FLOPPY_CONSTRUCT(sap_dsk_construct) |
| 716 | 192 | { |
| 717 | | thom_floppy_drive* d = thom_floppy_drives + image_index_in_device( image ); |
| 718 | | unsigned size = d->sides * d->tracks * d->sectors * d->sector_size; |
| 719 | | int format, sector, track, i; |
| 720 | | UINT8 buf[262]; |
| 721 | | if ( !d->data ) |
| 722 | | return; |
| 723 | | if ( !d->has_changed ) |
| 724 | | return; |
| 725 | | PRINT (( "thom_floppy_sap_save: saving floppy %li, %i KB\n", (long) (d - thom_floppy_drives), size / 1024 )); |
| 193 | struct FloppyCallbacks *callbacks; |
| 194 | struct sap_dsk_tag *tag; |
| 195 | int i; |
| 196 | UINT8 header[4]; |
| 197 | tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, SAP_DSK_TAG, sizeof(struct sap_dsk_tag)); |
| 198 | if (!tag) |
| 199 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 726 | 200 | |
| 727 | | /* TODO: truncation */ |
| 728 | | |
| 729 | | /* write header*/ |
| 730 | | image_fseek( image, 0, SEEK_SET ); |
| 731 | | image_fwrite( image, sap_header, 66 ); |
| 732 | | |
| 733 | | /* get format */ |
| 734 | | format = (d->sector_size == 128) ? 1 : 0; |
| 735 | | |
| 736 | | /* write sectors */ |
| 737 | | for ( track = 0; track < d->tracks; track++ ) |
| 201 | floppy_image_read(floppy, header, 0, 1); |
| 202 | if (header[0]==1) tag->sector_size = 256; else tag->sector_size = 128; |
| 203 | for ( i = 0x42; i+4 < floppy_image_size(floppy); i += tag->sector_size + 6 ) // CRC 2 bytes + 4 bytes sector header |
| 738 | 204 | { |
| 739 | | for ( sector = 1; sector <= d->sectors; sector++ ) |
| 740 | | { |
| 741 | | UINT8 *p = thom_floppy_sector_ptr( d, sector, track, 0 ); |
| 742 | | UINT16 crc; |
| 743 | | |
| 744 | | /* sector header */ |
| 745 | | buf[ 0 ] = format; |
| 746 | | buf[ 1 ] = 0; |
| 747 | | buf[ 2 ] = track; |
| 748 | | buf[ 3 ] = sector; |
| 749 | | |
| 750 | | memcpy( buf + 4, p, d->sector_size ); |
| 751 | | |
| 752 | | /* CRC */ |
| 753 | | crc = thom_sap_crc( buf, d->sector_size + 4 ); |
| 754 | | buf[ d->sector_size + 4 ] = crc >> 8; |
| 755 | | buf[ d->sector_size + 5 ] = crc & 0xff; |
| 756 | | |
| 757 | | /* crypt data */ |
| 758 | | for ( i = 0; i < d->sector_size; i++ ) |
| 759 | | buf[ i + 4 ] ^= sap_magic_num; |
| 760 | | |
| 761 | | image_fwrite( image, buf, d->sector_size + 6 ); |
| 762 | | } |
| 205 | floppy_image_read(floppy, header, i, 4); |
| 206 | tag->tracks = header[2] + 1; |
| 763 | 207 | } |
| 208 | callbacks = floppy_callbacks(floppy); |
| 209 | callbacks->read_sector = sap_read_sector; |
| 210 | callbacks->write_sector = sap_write_sector; |
| 211 | callbacks->read_indexed_sector = sap_read_indexed_sector; |
| 212 | callbacks->write_indexed_sector = sap_write_indexed_sector; |
| 213 | callbacks->get_sector_length = sap_get_sector_length; |
| 214 | callbacks->get_heads_per_disk = sap_get_heads_per_disk; |
| 215 | callbacks->get_tracks_per_disk = sap_get_tracks_per_disk; |
| 216 | callbacks->get_indexed_sector_info = sap_get_indexed_sector_info; |
| 217 | |
| 218 | return FLOPPY_ERROR_SUCCESS; |
| 764 | 219 | } |
| 765 | 220 | |
| 766 | | |
| 767 | | |
| 768 | | /*********************** device ** ************************/ |
| 769 | | |
| 770 | | |
| 771 | | |
| 772 | | static void thom_floppy_reset ( thom_floppy_drive* d ) |
| 221 | static FLOPPY_IDENTIFY(qdd_dsk_identify) |
| 773 | 222 | { |
| 774 | | if ( d->data ) |
| 775 | | free( d->data ); |
| 776 | | d->data = NULL; |
| 777 | | d->cur_track = 0; |
| 778 | | d->tracks = 0; |
| 779 | | d->type = THOM_FLOPPY_NONE; |
| 780 | | memset( d->sect_flag, THOM_SECT_INVALID, sizeof( d->sect_flag ) ); |
| 223 | *vote = (floppy_image_size(floppy) == (51200)) ? 100 : 0; |
| 224 | return FLOPPY_ERROR_SUCCESS; |
| 781 | 225 | } |
| 782 | 226 | |
| 227 | /* fixed interlacing map for QDDs */ |
| 228 | static int thom_qdd_map[400]; |
| 783 | 229 | |
| 784 | | |
| 785 | | DEVICE_START( thom_floppy ) |
| 230 | static int qdd_translate_sector(floppy_image *floppy, int sector) |
| 786 | 231 | { |
| 787 | | thom_qdd_compute_map(); |
| 788 | | thom_floppy_reset( thom_floppy_drive_of_image( device ) ); |
| 789 | | floppy_drive_init( device, &thom_floppy_interface ); |
| 232 | return thom_qdd_map[sector-1]; |
| 790 | 233 | } |
| 791 | 234 | |
| 792 | | |
| 793 | | |
| 794 | | DEVICE_IMAGE_LOAD( thom_floppy ) |
| 235 | static void thom_qdd_compute_map ( void ) |
| 795 | 236 | { |
| 796 | | const char* typ = image_filetype( image ); |
| 797 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 798 | | thom_floppy_reset( d ); |
| 799 | | |
| 800 | | d->image = image; |
| 801 | | d->has_changed = 0; |
| 802 | | d->cur_track = 0; |
| 803 | | |
| 804 | | /* dispatch according to extension */ |
| 805 | | if ( typ ) |
| 237 | /* this map is hardcoded in the QDD BIOS */ |
| 238 | static const int p[6][4] = |
| 806 | 239 | { |
| 807 | | if ( !mame_stricmp( typ, "sap" ) ) |
| 808 | | return thom_floppy_sap_load( image ); |
| 809 | | else if ( !mame_stricmp( typ, "fd" ) ) |
| 810 | | return thom_floppy_fd_load( image ); |
| 811 | | else if ( !mame_stricmp( typ, "qd" ) ) |
| 812 | | return thom_floppy_qd_load( image ); |
| 813 | | } |
| 814 | | |
| 815 | | return INIT_FAIL; |
| 816 | | } |
| 817 | | |
| 818 | | |
| 819 | | |
| 820 | | DEVICE_IMAGE_UNLOAD(thom_floppy) |
| 821 | | { |
| 822 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 823 | | const char* typ = image_filetype( image ); |
| 824 | | if ( typ ) |
| 240 | { 20, 2, 14, 8 }, { 21, 19, 13, 7 }, |
| 241 | { 22, 18, 12, 6 }, { 23, 17, 11, 5 }, |
| 242 | { 24, 16, 10, 4 }, { 1, 15, 9, 3 } |
| 243 | }; |
| 244 | static const int q[4] = { 0, 8, 4, 12 }; |
| 245 | int t, s; |
| 246 | for ( t = 0; t < 24; t++ ) |
| 825 | 247 | { |
| 826 | | if ( !mame_stricmp( typ, "sap" ) ) |
| 827 | | thom_floppy_sap_save( image ); |
| 828 | | else if ( !mame_stricmp( typ, "fd" ) ) |
| 829 | | thom_floppy_fd_save( image ); |
| 830 | | else if ( !mame_stricmp( typ, "qd" ) ) |
| 831 | | thom_floppy_qd_save( image ); |
| 248 | for ( s = 0; s < 16; s++ ) |
| 249 | { |
| 250 | thom_qdd_map[ t*16 + s ] = p[ t/4 ][ s%4 ] * 16 + (s/4) + 4*(t%4); |
| 251 | } |
| 832 | 252 | } |
| 833 | | thom_floppy_reset( d ); |
| 834 | | } |
| 835 | | |
| 836 | | |
| 837 | | |
| 838 | | DEVICE_IMAGE_CREATE( thom_floppy ) |
| 839 | | { |
| 840 | | thom_floppy_drive* d = thom_floppy_drive_of_image( image ); |
| 841 | | const char* typ = image_filetype( image ); |
| 842 | | thom_floppy_reset( d ); |
| 843 | | |
| 844 | | d->image = image; |
| 845 | | d->cur_track = 0; |
| 846 | | if ( typ && !mame_stricmp( typ, "qd" ) ) |
| 253 | for ( s = 0; s < 16; s++ ) |
| 847 | 254 | { |
| 848 | | d->sides = 1; |
| 849 | | d->sectors = 400; |
| 850 | | d->tracks = 1; |
| 851 | | d->sector_size = 128; |
| 852 | | d->density = DEN_MFM_LO; |
| 853 | | d->type = THOM_FLOPPY_QDD; |
| 255 | thom_qdd_map[ 24*16 + s ] = q[ s%4 ] + (s/4); |
| 854 | 256 | } |
| 855 | | else if ( typ && !mame_stricmp( typ, "fd" ) ) |
| 856 | | { |
| 857 | | d->sides = 1; |
| 858 | | d->sectors = 16; |
| 859 | | d->tracks = 40; |
| 860 | | d->sector_size = 128; |
| 861 | | d->density = DEN_FM_LO; |
| 862 | | d->type = THOM_FLOPPY_5_1_4; |
| 863 | | } |
| 864 | | else |
| 865 | | { |
| 866 | | d->sides = 1; |
| 867 | | d->sectors = 16; |
| 868 | | d->tracks = 40; |
| 869 | | d->sector_size = 256; |
| 870 | | d->density = DEN_MFM_LO; |
| 871 | | d->type = THOM_FLOPPY_5_1_4; |
| 872 | | } |
| 873 | | |
| 874 | | d->data = malloc( d->sector_size * d->sectors * d->tracks * d->sides ); |
| 875 | | assert( d->data ); |
| 876 | | memset( d->data, 0xff, d->sector_size * d->sectors * d->tracks * d->sides ); |
| 877 | | |
| 878 | | PRINT(( "new image created in drive %i\n", image_index_in_device( image ) )); |
| 879 | | |
| 880 | | /* not much to do: |
| 881 | | - the image is created in memory upon formatting |
| 882 | | - it is stored to the file-system upon unload |
| 883 | | */ |
| 884 | | |
| 885 | | return INIT_PASS; |
| 886 | 257 | } |
| 887 | 258 | |
| 888 | | |
| 889 | | |
| 890 | | void thom_floppy_set_density ( DENSITY density ) |
| 259 | static FLOPPY_CONSTRUCT(qdd_dsk_construct) |
| 891 | 260 | { |
| 892 | | thom_density = density; |
| 261 | struct basicdsk_geometry geometry; |
| 262 | |
| 263 | thom_qdd_compute_map(); |
| 264 | |
| 265 | memset(&geometry, 0, sizeof(geometry)); |
| 266 | geometry.heads = 1; |
| 267 | geometry.first_sector_id = 1; |
| 268 | geometry.sector_length = 128; |
| 269 | geometry.tracks = 1; |
| 270 | geometry.sectors = 400; |
| 271 | geometry.translate_sector = qdd_translate_sector; |
| 272 | return basicdsk_construct(floppy, &geometry); |
| 893 | 273 | } |
| 894 | 274 | |
| 895 | 275 | |
| 276 | /* ----------------------------------------------------------------------- */ |
| 896 | 277 | |
| 897 | | DENSITY thom_floppy_get_density ( void ) |
| 898 | | { |
| 899 | | return thom_density; |
| 900 | | } |
| 278 | FLOPPY_OPTIONS_START(thomson) |
| 279 | FLOPPY_OPTION(qdd,"qd", "Thomson QDD floppy disk image", qdd_dsk_identify, qdd_dsk_construct, |
| 280 | HEADS([1]) |
| 281 | TRACKS([1]) |
| 282 | SECTORS([400]) |
| 283 | SECTOR_LENGTH([128]) |
| 284 | FIRST_SECTOR_ID([0])) |
| 285 | FLOPPY_OPTION(sap,"sap", "Thomson SAP floppy disk image", sap_dsk_identify, sap_dsk_construct, NULL) |
| 286 | FLOPPY_OPTION(fd, "fd", "Thomson FD (FM) 40 tracks disk image", basicdsk_identify_default, basicdsk_construct_default, |
| 287 | HEADS([1]) |
| 288 | TRACKS([40]) |
| 289 | SECTORS([16]) |
| 290 | SECTOR_LENGTH([128]) |
| 291 | FIRST_SECTOR_ID([1])) |
| 292 | FLOPPY_OPTION(fd2, "fd", "Thomson FD (FM) 80 tracks disk image", basicdsk_identify_default, basicdsk_construct_default, |
| 293 | HEADS([1]) |
| 294 | TRACKS([80]) |
| 295 | SECTORS([16]) |
| 296 | SECTOR_LENGTH([128]) |
| 297 | FIRST_SECTOR_ID([1])) |
| 298 | FLOPPY_OPTION(fdmfm, "fd", "Thomson FD (MFM) 40 tracks disk image", basicdsk_identify_default, basicdsk_construct_default, |
| 299 | HEADS([1]) |
| 300 | TRACKS([40]) |
| 301 | SECTORS([16]) |
| 302 | SECTOR_LENGTH([256]) |
| 303 | FIRST_SECTOR_ID([1])) |
| 304 | FLOPPY_OPTION(fdmfm2, "fd", "Thomson FD (MFM) 80 tracks disk image", basicdsk_identify_default, basicdsk_construct_default, |
| 305 | HEADS([1]) |
| 306 | TRACKS([80]) |
| 307 | SECTORS([16]) |
| 308 | SECTOR_LENGTH([256]) |
| 309 | FIRST_SECTOR_ID([1])) |
| 310 | FLOPPY_OPTIONS_END |
| 901 | 311 | |
| 902 | | |
| 903 | | |
| 904 | | thom_floppy_type thom_floppy_get_type ( int drive ) |
| 905 | | { |
| 906 | | if ( drive < 0 || drive >= 4 ) |
| 907 | | return THOM_FLOPPY_NONE; |
| 908 | | else |
| 909 | | return thom_floppy_drives[ drive ].type; |
| 910 | | } |
| 911 | | |
| 912 | | |
| 913 | | |
| 914 | | void thom_floppy_getinfo( const mess_device_class *devclass, UINT32 state, union devinfo *info ) |
| 915 | | { |
| 916 | | switch ( state ) |
| 917 | | { |
| 918 | | case MESS_DEVINFO_INT_TYPE: |
| 919 | | info->i = IO_FLOPPY; |
| 920 | | break; |
| 921 | | case MESS_DEVINFO_INT_READABLE: |
| 922 | | info->i = 1; |
| 923 | | break; |
| 924 | | case MESS_DEVINFO_INT_WRITEABLE: |
| 925 | | info->i = 1; |
| 926 | | break; |
| 927 | | case MESS_DEVINFO_INT_CREATABLE: |
| 928 | | info->i = 1; |
| 929 | | break; |
| 930 | | case MESS_DEVINFO_PTR_START: |
| 931 | | info->start = DEVICE_START_NAME(thom_floppy); |
| 932 | | break; |
| 933 | | case MESS_DEVINFO_PTR_LOAD: |
| 934 | | info->load = DEVICE_IMAGE_LOAD_NAME(thom_floppy); |
| 935 | | break; |
| 936 | | case MESS_DEVINFO_PTR_UNLOAD: |
| 937 | | info->unload = DEVICE_IMAGE_UNLOAD_NAME(thom_floppy); |
| 938 | | break; |
| 939 | | case MESS_DEVINFO_PTR_CREATE: |
| 940 | | info->create = DEVICE_IMAGE_CREATE_NAME(thom_floppy); |
| 941 | | break; |
| 942 | | case MESS_DEVINFO_INT_COUNT: |
| 943 | | info->i = 4; |
| 944 | | break; |
| 945 | | case MESS_DEVINFO_STR_NAME+0: |
| 946 | | case MESS_DEVINFO_STR_NAME+1: |
| 947 | | case MESS_DEVINFO_STR_NAME+2: |
| 948 | | case MESS_DEVINFO_STR_NAME+3: |
| 949 | | strcpy( info->s = device_temp_str(), "floppydisk0" ); |
| 950 | | info->s[ strlen( info->s ) - 1 ] += state - MESS_DEVINFO_STR_NAME; |
| 951 | | break; |
| 952 | | case MESS_DEVINFO_STR_SHORT_NAME+0: |
| 953 | | case MESS_DEVINFO_STR_SHORT_NAME+1: |
| 954 | | case MESS_DEVINFO_STR_SHORT_NAME+2: |
| 955 | | case MESS_DEVINFO_STR_SHORT_NAME+3: |
| 956 | | strcpy( info->s = device_temp_str(), "flop0"); |
| 957 | | info->s[ strlen( info->s ) - 1 ] += state - MESS_DEVINFO_STR_SHORT_NAME; |
| 958 | | break; |
| 959 | | case MESS_DEVINFO_STR_DESCRIPTION+0: |
| 960 | | case MESS_DEVINFO_STR_DESCRIPTION+1: |
| 961 | | case MESS_DEVINFO_STR_DESCRIPTION+2: |
| 962 | | case MESS_DEVINFO_STR_DESCRIPTION+3: |
| 963 | | strcpy( info->s = device_temp_str(), "Floppy #0" ); |
| 964 | | info->s[ strlen( info->s ) - 1 ] += state - MESS_DEVINFO_STR_DESCRIPTION; |
| 965 | | break; |
| 966 | | case MESS_DEVINFO_STR_DEV_FILE: |
| 967 | | strcpy( info->s = device_temp_str(), __FILE__ ); |
| 968 | | break; |
| 969 | | case MESS_DEVINFO_STR_FILE_EXTENSIONS: |
| 970 | | strcpy( info->s = device_temp_str(), "fd,qd,sap" ); |
| 971 | | break; |
| 972 | | } |
| 973 | | } |