src/mess/machine/vc1541.c
| r3311 | r3312 | |
| 98 | 98 | */ |
| 99 | 99 | /* 2008-09 FP |
| 100 | 100 | started some work on the floppy emulation code: |
| 101 | | + made the functions / structs less vc1541-centric |
| 101 | + made the functions / structs less vc1541-centric & d64-centric |
| 102 | 102 | + unified config & reset functions, using type variable |
| 103 | 103 | to choose the actions to be done |
| 104 | + moved GCR encoding at loading time (wip untested code) |
| 104 | 105 | |
| 105 | 106 | a limitation is clear: only a single occurrence of emulated drive |
| 106 | 107 | is expected. |
| r3311 | r3312 | |
| 108 | 109 | an index to choose between the drives |
| 109 | 110 | |
| 110 | 111 | current status: |
| 111 | | + preliminary vc1541 & c1551 implementation (I still have to |
| 112 | | investigate how good) |
| 112 | + preliminary vc1541 & c1551 implementation (seems very slow) |
| 113 | 113 | + no support for other drive types (even if now I list all |
| 114 | 114 | the variants supported by Vice in the enum) |
| 115 | + communications between the drive CPU and the computer CPU have |
| 116 | to be checked, debugged and improved |
| 117 | + most functions / constants are d64 independent, even if other |
| 118 | formats are not supported yet! |
| 119 | + the floppy handling still uses CBM_Drive_Emu struct instead of |
| 120 | functions similar to other floppy formats |
| 115 | 121 | */ |
| 116 | 122 | |
| 117 | 123 | #include "driver.h" |
| r3311 | r3312 | |
| 138 | 144 | |
| 139 | 145 | #define XTAL_16_MHz 16000000 |
| 140 | 146 | |
| 147 | |
| 148 | /* |
| 149 | * only for testing at the moment |
| 150 | */ |
| 151 | |
| 152 | #define WRITEPROTECTED 0 |
| 153 | |
| 154 | |
| 155 | /******************************************* |
| 156 | |
| 157 | Structures |
| 158 | |
| 159 | *******************************************/ |
| 160 | |
| 161 | /* 0 summarized, 1 computer, 2 vc1541 */ |
| 162 | static struct |
| 163 | { |
| 164 | int atn[3], data[3], clock[3]; |
| 165 | } |
| 166 | serial; |
| 167 | |
| 168 | /* Legacy code. At a later stage, we should really use code more similar |
| 169 | to the MESS core floppy code. However, fixing the C64 / Floppy drive |
| 170 | communications is a priority over the clean up of this part */ |
| 171 | typedef struct |
| 172 | { |
| 173 | int cpunumber; |
| 174 | int type; |
| 175 | union { |
| 176 | struct { |
| 177 | int deviceid; |
| 178 | int serial_atn, serial_clock, serial_data; |
| 179 | int acka, data; |
| 180 | } serial; |
| 181 | struct { |
| 182 | int deviceid; |
| 183 | } ieee488; |
| 184 | struct { |
| 185 | UINT8 cpu_ddr, cpu_port; |
| 186 | void *irq_timer; |
| 187 | } c1551; |
| 188 | } drive; |
| 189 | |
| 190 | int via0irq, via1irq; |
| 191 | |
| 192 | int led, motor, frequency; |
| 193 | |
| 194 | double track; |
| 195 | int clock; |
| 196 | int pos; // from the beginning of the current track! |
| 197 | |
| 198 | void *timer; |
| 199 | |
| 200 | struct { |
| 201 | int sync; // active after > 9 '1's |
| 202 | int ready; // needed for 1551 |
| 203 | int ones_count; // still needed? |
| 204 | UINT8 data[0x500000]; // entire d64 image encoded at loading time |
| 205 | } gcr; |
| 206 | } CBM_Drive_Emu; |
| 207 | |
| 208 | static CBM_Drive_Emu drive_static= { 0 }, *drive = &drive_static; |
| 209 | |
| 210 | |
| 211 | |
| 212 | /******************************************* |
| 213 | |
| 214 | Format specific code |
| 215 | |
| 216 | *******************************************/ |
| 217 | |
| 218 | |
| 141 | 219 | /* Are there other formats to support which could require more tracks? */ |
| 142 | 220 | #define IMAGE_MAX_TRACKS 154 |
| 143 | 221 | |
| r3311 | r3312 | |
| 203 | 281 | static int image_offset[IMAGE_MAX_TRACKS] = { 0 }; |
| 204 | 282 | |
| 205 | 283 | |
| 284 | /******************************************* |
| 285 | |
| 286 | Offset initialization |
| 287 | |
| 288 | *******************************************/ |
| 289 | |
| 290 | |
| 206 | 291 | static void offset_init (int format) |
| 207 | 292 | { |
| 208 | 293 | int i; |
| r3311 | r3312 | |
| 213 | 298 | switch (format) |
| 214 | 299 | { |
| 215 | 300 | case format_d64: |
| 301 | case format_d64_err: |
| 216 | 302 | for (i = 1; i <= D64_MAX_TRACKS; i++) |
| 217 | 303 | image_offset[i] = image_offset[i - 1] + d64_sectors_per_track[i - 1] * 256; |
| 218 | 304 | break; |
| 219 | 305 | |
| 220 | 306 | case format_d64_40t: |
| 307 | case format_d64_40t_err: |
| 221 | 308 | for (i = 1; i <= D64_40T_MAX_TRACKS; i++) |
| 222 | 309 | image_offset[i] = image_offset[i - 1] + d64_sectors_per_track[i - 1] * 256; |
| 223 | 310 | break; |
| 224 | 311 | |
| 225 | 312 | case format_d71: |
| 313 | case format_d71_err: |
| 226 | 314 | for (i = 1; i <= D71_MAX_TRACKS; i++) |
| 227 | 315 | image_offset[i] = image_offset[i - 1] + d71_sectors_per_track[i - 1] * 256; |
| 228 | 316 | break; |
| r3311 | r3312 | |
| 243 | 331 | break; |
| 244 | 332 | |
| 245 | 333 | default: |
| 246 | | logerror("Unsupported format"); |
| 334 | /* Still have to work on d67 & g64 images */ |
| 335 | logerror("Unsupported format\n"); |
| 247 | 336 | break; |
| 248 | 337 | } |
| 249 | 338 | } |
| 250 | 339 | |
| 251 | 340 | |
| 252 | | /* calculates offset to beginning of the image file for sector beginning */ |
| 253 | | /* since sectors are always 256 bytes large, this works for each file format! */ |
| 341 | /******************************************* |
| 342 | |
| 343 | Generic image handling |
| 344 | |
| 345 | *******************************************/ |
| 346 | |
| 347 | |
| 348 | /* This calculates starting offset (from the beginning of the image file) of a sector */ |
| 349 | /* Since sectors are always 256 bytes large, this works no matter the file format! */ |
| 254 | 350 | static int image_tracksector2offset (int track, int sector) |
| 255 | 351 | { |
| 256 | 352 | return image_offset[track - 1] + sector * 256; |
| 257 | 353 | } |
| 258 | 354 | |
| 259 | | |
| 260 | | #define D64_TRACK_ID1 (image_tracksector2offset(18,0)+162) |
| 261 | | #define D64_TRACK_ID2 (image_tracksector2offset(18,0)+163) |
| 262 | | |
| 263 | 355 | /* |
| 264 | | * only for testing at the moment |
| 265 | | */ |
| 356 | // WARNING: this would work with a d64 image... it has to be adapted to work with GCR image we store at loading time |
| 357 | static void image_offset2tracksector (int offset) |
| 358 | { |
| 359 | int track = 0, sector = 0, helper, i = 0; |
| 360 | |
| 361 | helper = offset; |
| 362 | |
| 363 | do |
| 364 | { |
| 365 | helper -= (d64_sectors_per_track[++i] * 256); |
| 366 | } while ( helper > 0); |
| 367 | |
| 368 | track = i; |
| 266 | 369 | |
| 267 | | #define WRITEPROTECTED 0 |
| 370 | sector = (offset - image_offset[track]) / 256; |
| 268 | 371 | |
| 269 | | /* 0 summarized, 1 computer, 2 vc1541 */ |
| 270 | | static struct |
| 271 | | { |
| 272 | | int atn[3], data[3], clock[3]; |
| 372 | logerror("We are reading at offset %d, which is in track %d and sector %d \n", offset, track, sector); |
| 273 | 373 | } |
| 274 | | serial; |
| 374 | */ |
| 275 | 375 | |
| 276 | | /* G64 or D64 image |
| 277 | | implementation as writeback system |
| 278 | | */ |
| 279 | 376 | |
| 280 | | typedef struct |
| 377 | /* These functions return the Disk ID stored in the disk BAM */ |
| 378 | /* It is not necessarily the one some copy protection checks, and |
| 379 | indeed, in these cases a g64 image is needed (which stores the |
| 380 | Disk ID as coded in the GCR data) */ |
| 381 | |
| 382 | static int disk_id1 (int format) |
| 281 | 383 | { |
| 282 | | int cpunumber; |
| 283 | | int type; |
| 284 | | union { |
| 285 | | struct { |
| 286 | | int deviceid; |
| 287 | | int serial_atn, serial_clock, serial_data; |
| 288 | | int acka, data; |
| 289 | | } serial; |
| 290 | | struct { |
| 291 | | int deviceid; |
| 292 | | } ieee488; |
| 293 | | struct { |
| 294 | | UINT8 cpu_ddr, cpu_port; |
| 295 | | void *irq_timer; |
| 296 | | } c1551; |
| 297 | | } drive; |
| 384 | int id1 = 0; |
| 298 | 385 | |
| 299 | | int via0irq, via1irq; |
| 386 | switch (format) |
| 387 | { |
| 388 | case format_d64: |
| 389 | case format_d64_err: |
| 390 | case format_d64_40t: |
| 391 | case format_d64_40t_err: |
| 392 | case format_d71: |
| 393 | case format_d71_err: |
| 394 | id1 = image_tracksector2offset(18, 0) + 0xa2; |
| 395 | break; |
| 300 | 396 | |
| 301 | | int led, motor, frequency; |
| 397 | case format_d81: |
| 398 | id1 = image_tracksector2offset(40, 0) + 0x16; |
| 399 | break; |
| 302 | 400 | |
| 303 | | double track; |
| 304 | | int clock; |
| 401 | case format_d80: |
| 402 | case format_d82: |
| 403 | id1 = image_tracksector2offset(39, 0) + 0x18; |
| 404 | break; |
| 305 | 405 | |
| 306 | | void *timer; |
| 406 | default: |
| 407 | /* Still have to work on d67 & g64 images */ |
| 408 | logerror("Unsupported format\n"); |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | return id1; |
| 413 | } |
| 307 | 414 | |
| 308 | | struct { |
| 309 | | UINT8 data[(1+2+2+1+256/4+4)*5]; |
| 310 | | int sync; |
| 311 | | int ready; |
| 312 | | int ffcount; |
| 313 | | } head; |
| 314 | 415 | |
| 315 | | struct { |
| 316 | | int pos; /* position in sector */ |
| 317 | | int sector; |
| 318 | | UINT8 *data; |
| 319 | | } image; |
| 320 | | } CBM_Drive_Emu; |
| 416 | static int disk_id2 (int format) |
| 417 | { |
| 418 | int id2 = 0; |
| 321 | 419 | |
| 322 | | static CBM_Drive_Emu drive_static= { 0 }, *drive = &drive_static; |
| 420 | switch (format) |
| 421 | { |
| 422 | case format_d64: |
| 423 | case format_d64_err: |
| 424 | case format_d64_40t: |
| 425 | case format_d64_40t_err: |
| 426 | case format_d71: |
| 427 | case format_d71_err: |
| 428 | id2 = image_tracksector2offset(18, 0) + 0xa3; |
| 429 | break; |
| 323 | 430 | |
| 431 | case format_d81: |
| 432 | id2 = image_tracksector2offset(40, 0) + 0x17; |
| 433 | break; |
| 324 | 434 | |
| 325 | | /************************************** |
| 435 | case format_d80: |
| 436 | case format_d82: |
| 437 | id2 = image_tracksector2offset(39, 0) + 0x19; |
| 438 | break; |
| 326 | 439 | |
| 327 | | Generic image handling |
| 440 | default: |
| 441 | /* Still have to work on d67 & g64 images */ |
| 442 | logerror("Unsupported format\n"); |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | return id2; |
| 447 | } |
| 328 | 448 | |
| 329 | | **************************************/ |
| 330 | 449 | |
| 331 | | |
| 332 | | /* four different frequencies for the 4 different zones on the disk */ |
| 333 | | static const double drive_times[4]= { |
| 334 | | 13/16e6, 14/16e6, 15/16e6, 16/16e6 |
| 450 | /* Four different frequencies for the 4 different zones on the disk */ |
| 451 | /* These work for 1541 drives, are the same for other drives? */ |
| 452 | static const double drive_times[4]= |
| 453 | { |
| 454 | XTAL_16_MHz / 13, XTAL_16_MHz / 14, XTAL_16_MHz / 15, XTAL_16_MHz / 16 |
| 335 | 455 | }; |
| 336 | 456 | |
| 457 | |
| 458 | /* Commodore GCR format */ |
| 337 | 459 | /* |
| 338 | | * gcr encoding 4 bits to 5 bits |
| 339 | | * 1 change, 0 none |
| 340 | | * |
| 341 | | * physical encoding of the data on a track |
| 342 | | * sector header |
| 343 | | * sync (5x 0xff not gcr encoded) |
| 344 | | * sync mark (0x08) |
| 345 | | * checksum (chksum xor sector xor track xor id1 xor id2 gives 0) |
| 346 | | * sector# |
| 347 | | * track# |
| 348 | | * id2 (disk id to prevent writing to disk after disk change) |
| 349 | | * id1 |
| 350 | | * 0x0f |
| 351 | | * 0x0f |
| 352 | | * cap normally 10 (min 5) byte? |
| 353 | | * sector data |
| 354 | | * sync (5x 0xff not gcr encoded) |
| 355 | | * sync mark (0x07) |
| 356 | | * 256 bytes |
| 357 | | * checksum (256 bytes xored) |
| 358 | | * cap |
| 359 | | * |
| 360 | | * max 42 tracks, stepper resolution 84 tracks |
| 361 | | */ |
| 460 | Original Encoded |
| 461 | 4 bits 5 bits |
| 462 | |
| 463 | 0000 -> 01010 = 0x0a |
| 464 | 0001 -> 01011 = 0x0b |
| 465 | 0010 -> 10010 = 0x12 |
| 466 | 0011 -> 10011 = 0x13 |
| 467 | 0100 -> 01110 = 0x0e |
| 468 | 0101 -> 01111 = 0x0f |
| 469 | 0110 -> 10110 = 0x16 |
| 470 | 0111 -> 10111 = 0x17 |
| 471 | 1000 -> 01001 = 0x09 |
| 472 | 1001 -> 11001 = 0x19 |
| 473 | 1010 -> 11010 = 0x1a |
| 474 | 1011 -> 11011 = 0x1b |
| 475 | 1100 -> 01101 = 0x0d |
| 476 | 1101 -> 11101 = 0x1d |
| 477 | 1110 -> 11110 = 0x1e |
| 478 | 1111 -> 10101 = 0x15 |
| 479 | |
| 480 | We use the encoded values in bytes because we use them to encode |
| 481 | groups of 4 bytes into groups of 5 bytes, below. |
| 482 | */ |
| 483 | |
| 362 | 484 | static const int bin_2_gcr[] = |
| 363 | 485 | { |
| 364 | 486 | 0x0a, 0x0b, 0x12, 0x13, 0x0e, 0x0f, 0x16, 0x17, |
| 365 | 487 | 0x09, 0x19, 0x1a, 0x1b, 0x0d, 0x1d, 0x1e, 0x15 |
| 366 | 488 | }; |
| 367 | 489 | |
| 368 | | #if 0 |
| 369 | | static const int gcr_2_bin[] = { |
| 370 | | -1, -1, -1, -1, |
| 371 | | -1, -1, -1, -1, |
| 372 | | -1, 8, 0, 1, |
| 373 | | -1, 0xc, 4, 5, |
| 374 | | -1, -1, 2, 3, |
| 375 | | -1, 0xf, 6, 7, |
| 376 | | -1, 9, 0xa, 0xb, |
| 377 | | -1, 0xd, 0xe, -1 |
| 490 | |
| 491 | /* This could be of use if we ever implement saving in .d64 format, to convert back GCR -> d64 */ |
| 492 | /* |
| 493 | static const int gcr_2_bin[] = |
| 494 | { |
| 495 | -1, -1, -1, -1, |
| 496 | -1, -1, -1, -1, |
| 497 | -1, 0x08, 0x00, 0x01, |
| 498 | -1, 0x0c, 0x04, 0x05, |
| 499 | -1, -1, 0x02, 0x03, |
| 500 | -1, 0x0f, 0x06, 0x07, |
| 501 | -1, 0x09, 0x0a, 0x0b, |
| 502 | -1, 0x0d, 0x0e, -1 |
| 378 | 503 | }; |
| 379 | | #endif |
| 504 | */ |
| 380 | 505 | |
| 506 | |
| 507 | /* gcr_double_2_gcr takes 4 bytes (a, b, c, d) and shuffles their nibbles to obtain 5 bytes in dest */ |
| 508 | /* The result is basically res = (enc(a) << 15) | (enc(b) << 10) | (enc(c) << 5) | enc(d) |
| 509 | * with res being 5 bytes long and enc(x) being the GCR encode of x. |
| 510 | * In fact, we store the result as five separate bytes in the dest argument |
| 511 | */ |
| 381 | 512 | static void gcr_double_2_gcr(UINT8 a, UINT8 b, UINT8 c, UINT8 d, UINT8 *dest) |
| 382 | 513 | { |
| 383 | 514 | UINT8 gcr[8]; |
| 384 | | gcr[0]=bin_2_gcr[a>>4]; |
| 385 | | gcr[1]=bin_2_gcr[a&0xf]; |
| 386 | | gcr[2]=bin_2_gcr[b>>4]; |
| 387 | | gcr[3]=bin_2_gcr[b&0xf]; |
| 388 | | gcr[4]=bin_2_gcr[c>>4]; |
| 389 | | gcr[5]=bin_2_gcr[c&0xf]; |
| 390 | | gcr[6]=bin_2_gcr[d>>4]; |
| 391 | | gcr[7]=bin_2_gcr[d&0xf]; |
| 392 | | dest[0]=(gcr[0]<<3)|(gcr[1]>>2); |
| 393 | | dest[1]=(gcr[1]<<6)|(gcr[2]<<1)|(gcr[3]>>4); |
| 394 | | dest[2]=(gcr[3]<<4)|(gcr[4]>>1); |
| 395 | | dest[3]=(gcr[4]<<7)|(gcr[5]<<2)|(gcr[6]>>3); |
| 396 | | dest[4]=(gcr[6]<<5)|gcr[7]; |
| 397 | | } |
| 398 | 515 | |
| 399 | | static struct { |
| 400 | | int count; |
| 401 | | int data[4]; |
| 402 | | } gcr_helper; |
| 516 | /* Encode each nibble to 5 bits */ |
| 517 | gcr[0] = bin_2_gcr[a >> 4]; |
| 518 | gcr[1] = bin_2_gcr[a & 0x0f]; |
| 519 | gcr[2] = bin_2_gcr[b >> 4]; |
| 520 | gcr[3] = bin_2_gcr[b & 0x0f]; |
| 521 | gcr[4] = bin_2_gcr[c >> 4]; |
| 522 | gcr[5] = bin_2_gcr[c & 0x0f]; |
| 523 | gcr[6] = bin_2_gcr[d >> 4]; |
| 524 | gcr[7] = bin_2_gcr[d & 0x0f]; |
| 403 | 525 | |
| 404 | | static void drive_sector_start(void) |
| 405 | | { |
| 406 | | gcr_helper.count=0; |
| 526 | /* Re-order the encoded data to only keep the 5 lower bits of each byte */ |
| 527 | dest[0] = (gcr[0] << 3) | (gcr[1] >> 2); |
| 528 | dest[1] = (gcr[1] << 6) | (gcr[2] << 1) | (gcr[3] >> 4); |
| 529 | dest[2] = (gcr[3] << 4) | (gcr[4] >> 1); |
| 530 | dest[3] = (gcr[4] << 7) | (gcr[5] << 2) | (gcr[6] >> 3); |
| 531 | dest[4] = (gcr[6] << 5) | gcr[7]; |
| 407 | 532 | } |
| 408 | 533 | |
| 409 | | static void drive_sector_data(UINT8 data, int *pos) |
| 410 | | { |
| 411 | | gcr_helper.data[gcr_helper.count++]=data; |
| 412 | | if (gcr_helper.count==4) { |
| 413 | | gcr_double_2_gcr(gcr_helper.data[0], gcr_helper.data[1], |
| 414 | | gcr_helper.data[2], gcr_helper.data[3], |
| 415 | | drive->head.data+*pos); |
| 416 | | *pos=*pos+5; |
| 417 | | gcr_helper.count=0; |
| 418 | | } |
| 419 | | } |
| 420 | 534 | |
| 421 | | static void drive_sector_end(int *pos) |
| 422 | | { |
| 423 | | assert(gcr_helper.count==0); |
| 424 | | } |
| 425 | | |
| 426 | | static void drive_sector_to_gcr(int track, int sector) |
| 427 | | { |
| 428 | | int i=0, j, offset, chksum=0; |
| 429 | | |
| 430 | | if (drive->image.data==NULL) return; |
| 431 | | drive->head.data[i++]=0xff; |
| 432 | | drive->head.data[i++]=0xff; |
| 433 | | drive->head.data[i++]=0xff; |
| 434 | | drive->head.data[i++]=0xff; |
| 435 | | drive->head.data[i++]=0xff; |
| 436 | | drive_sector_start(); |
| 437 | | |
| 438 | | drive_sector_data(8, &i); |
| 439 | | chksum= sector^track |
| 440 | | ^drive->image.data[D64_TRACK_ID1]^drive->image.data[D64_TRACK_ID2]; |
| 441 | | drive_sector_data(chksum, &i); |
| 442 | | drive_sector_data(sector, &i); |
| 443 | | drive_sector_data(track, &i); |
| 444 | | drive_sector_data(drive->image.data[D64_TRACK_ID1], &i); |
| 445 | | drive_sector_data(drive->image.data[D64_TRACK_ID2], &i); |
| 446 | | drive_sector_data(0xf, &i); |
| 447 | | drive_sector_data(0xf, &i); |
| 448 | | drive_sector_end(&i); |
| 449 | | |
| 450 | | /* 5 - 10 gcr bytes cap */ |
| 451 | | gcr_double_2_gcr(0, 0, 0, 0, drive->head.data+i);i+=5; |
| 452 | | gcr_double_2_gcr(0, 0, 0, 0, drive->head.data+i);i+=5; |
| 453 | | drive->head.data[i++]=0xff; |
| 454 | | drive->head.data[i++]=0xff; |
| 455 | | drive->head.data[i++]=0xff; |
| 456 | | drive->head.data[i++]=0xff; |
| 457 | | drive->head.data[i++]=0xff; |
| 458 | | drive_sector_data(0x7, &i); |
| 459 | | |
| 460 | | chksum=0; |
| 461 | | for (offset=image_tracksector2offset(track,sector), j=0; j<256; j++) { |
| 462 | | chksum^=drive->image.data[offset]; |
| 463 | | drive_sector_data(drive->image.data[offset++], &i); |
| 464 | | } |
| 465 | | drive_sector_data(chksum, &i); |
| 466 | | drive_sector_data(0, &i); /* padding up */ |
| 467 | | drive_sector_data(0, &i); |
| 468 | | drive_sector_end(&i); |
| 469 | | gcr_double_2_gcr(0, 0, 0, 0, drive->head.data+i);i+=5; |
| 470 | | gcr_double_2_gcr(0, 0, 0, 0, drive->head.data+i);i+=5; |
| 471 | | } |
| 472 | | |
| 473 | | |
| 474 | 535 | /************************************** |
| 475 | 536 | |
| 476 | 537 | 1541 / 1541II |
| r3311 | r3312 | |
| 480 | 541 | |
| 481 | 542 | static ADDRESS_MAP_START( _1541_map, ADDRESS_SPACE_PROGRAM, 8 ) |
| 482 | 543 | AM_RANGE(0x0000, 0x07ff) AM_RAM |
| 483 | | AM_RANGE(0x1800, 0x180f) AM_READWRITE(via_2_r, via_2_w) /* 0 and 1 used in vc20 */ |
| 484 | | AM_RANGE(0x1810, 0x189f) AM_READ(SMH_NOP) /* for debugger */ |
| 544 | AM_RANGE(0x1800, 0x180f) AM_READWRITE(via_2_r, via_2_w) /* 0 and 1 used in vc20 */ |
| 545 | AM_RANGE(0x1810, 0x189f) AM_READ(SMH_NOP) /* for debugger */ |
| 485 | 546 | AM_RANGE(0x1c00, 0x1c0f) AM_READWRITE(via_3_r, via_3_w) |
| 486 | | AM_RANGE(0x1c10, 0x1c9f) AM_READ(SMH_NOP) /* for debugger */ |
| 547 | AM_RANGE(0x1c10, 0x1c9f) AM_READ(SMH_NOP) /* for debugger */ |
| 487 | 548 | AM_RANGE(0xc000, 0xffff) AM_ROM |
| 488 | 549 | ADDRESS_MAP_END |
| 489 | 550 | |
| 490 | 551 | |
| 491 | 552 | static ADDRESS_MAP_START( dolphin_map, ADDRESS_SPACE_PROGRAM, 8 ) |
| 492 | 553 | AM_RANGE(0x0000, 0x07ff) AM_RAM |
| 493 | | AM_RANGE(0x1800, 0x180f) AM_READWRITE(via_2_r, via_2_w) /* 0 and 1 used in vc20 */ |
| 554 | AM_RANGE(0x1800, 0x180f) AM_READWRITE(via_2_r, via_2_w) /* 0 and 1 used in vc20 */ |
| 494 | 555 | AM_RANGE(0x1c00, 0x1c0f) AM_READWRITE(via_3_r, via_3_w) |
| 495 | 556 | AM_RANGE(0x8000, 0x9fff) AM_RAM |
| 496 | 557 | AM_RANGE(0xa000, 0xffff) AM_ROM |
| 497 | 558 | ADDRESS_MAP_END |
| 498 | 559 | |
| 499 | 560 | |
| 500 | | #if 0 |
| 501 | | INPUT_PORTS_START (vc1541) |
| 502 | | PORT_START |
| 503 | | PORT_DIPNAME (0x60, 0x00, "Device #", CODE_NONE) |
| 504 | | PORT_DIPSETTING (0x00, "8") |
| 505 | | PORT_DIPSETTING (0x20, "9") |
| 506 | | PORT_DIPSETTING (0x40, "10") |
| 507 | | PORT_DIPSETTING (0x60, "11") |
| 508 | | INPUT_PORTS_END |
| 509 | | #endif |
| 510 | | |
| 511 | 561 | static TIMER_CALLBACK(drive_timer) |
| 512 | 562 | { |
| 513 | | if (drive->clock==0) { |
| 514 | | drive->clock=1; |
| 515 | | drive->head.ready=0; |
| 516 | | drive->head.sync=0; |
| 517 | | if (drive->type==type_1541) { |
| 563 | if (drive->clock == 0) |
| 564 | { |
| 565 | drive->gcr.ready = 0; |
| 566 | // why put sync back to 0 here?!? it would break the sync count, isn't it? |
| 567 | // or is it the way the serial port reads the sync count?!? |
| 568 | drive->gcr.sync = 0; |
| 569 | |
| 570 | if (drive->type == type_1541) |
| 571 | { |
| 518 | 572 | cpunum_set_input_line(machine, drive->cpunumber, M6502_SET_OVERFLOW, 1); |
| 519 | 573 | via_3_ca1_w(machine, 0,1); |
| 520 | 574 | } |
| 575 | |
| 576 | drive->clock = 1; |
| 521 | 577 | return; |
| 522 | 578 | } |
| 523 | | if (++(drive->image.pos)>=sizeof(drive->head.data)) { |
| 524 | | if (++(drive->image.sector)>= |
| 525 | | d64_sectors_per_track[(int)drive->track-1]) { |
| 526 | | drive->image.sector=0; |
| 527 | | } |
| 528 | | drive_sector_to_gcr((int)drive->track,drive->image.sector); |
| 529 | | drive->image.pos=0; |
| 579 | |
| 580 | // this code has 2 effects: move on the pos on the data and reset it to 0 at the end of the track! |
| 581 | // size of the track is: (# sectors in the track) * (size of sector GCR endoded) |
| 582 | // Here, size of sector is 368 because of our choices in the loading: 9 bytes of header gap & 14 bytes of sector gap |
| 583 | // We will need to set the size at loading time when adding support for G64 images |
| 584 | if (++(drive->pos) >= (d64_sectors_per_track[(int)drive->track - 1] * 368)) |
| 585 | drive->pos = 0; |
| 586 | // should we check if there is a change of track? to reset the pos in that case as well? |
| 587 | |
| 588 | drive->gcr.ready = 1; |
| 589 | |
| 590 | // notice that if there is a sequence of more than 40 1s it correctly does not reset the sync |
| 591 | // counter until a 0 is found |
| 592 | if (drive->gcr.data[drive->pos] == 0xff) |
| 593 | { |
| 594 | drive->gcr.ones_count++; |
| 595 | |
| 596 | // if (drive->gcr.ones_count == 2) // 9 ones should already mark it as a sync, even if CBM chose to use 40 of them |
| 597 | if (drive->gcr.ones_count == 5) |
| 598 | drive->gcr.sync = 1; |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | drive->gcr.ones_count = 0; |
| 603 | drive->gcr.sync = 0; |
| 530 | 604 | } |
| 531 | | drive->head.ready=1; |
| 532 | | if (drive->head.data[drive->image.pos]==0xff) { |
| 533 | | drive->head.ffcount++; |
| 534 | | if (drive->head.ffcount==5) { |
| 535 | | drive->head.sync=1; |
| 536 | | } |
| 537 | | } else { |
| 538 | | drive->head.ffcount=0; |
| 539 | | drive->head.sync=0; |
| 540 | | } |
| 541 | | if (drive->type==type_1541) { |
| 605 | |
| 606 | if (drive->type == type_1541) |
| 607 | { |
| 542 | 608 | cpunum_set_input_line(machine, drive->cpunumber, M6502_SET_OVERFLOW, 0); |
| 543 | | via_3_ca1_w(machine, 0,0); |
| 609 | via_3_ca1_w(machine, 0, 0); |
| 544 | 610 | } |
| 545 | | drive->clock=0; |
| 611 | |
| 612 | drive->clock = 0; |
| 546 | 613 | } |
| 547 | 614 | |
| 615 | |
| 548 | 616 | /* |
| 549 | 617 | * via 6522 at 0x1800 |
| 550 | 618 | * port b |
| r3311 | r3312 | |
| 560 | 628 | * also ca1 (books says cb2) |
| 561 | 629 | * irq to m6502 irq connected (or with second via irq) |
| 562 | 630 | */ |
| 631 | |
| 563 | 632 | static void vc1541_via0_irq (running_machine *machine, int level) |
| 564 | 633 | { |
| 565 | 634 | drive->via0irq = level; |
| 566 | 635 | DBG_LOG(2, "vc1541 via0 irq",("level %d %d\n",drive->via0irq,drive->via1irq)); |
| 567 | | cpunum_set_input_line (machine, drive->cpunumber, |
| 568 | | M6502_IRQ_LINE, drive->via1irq || drive->via0irq); |
| 636 | |
| 637 | cpunum_set_input_line (machine, drive->cpunumber, M6502_IRQ_LINE, drive->via1irq || drive->via0irq); |
| 569 | 638 | } |
| 570 | 639 | |
| 571 | 640 | static READ8_HANDLER( vc1541_via0_read_portb ) |
| 572 | 641 | { |
| 573 | | static int old=-1; |
| 574 | 642 | int value = 0x7a; |
| 575 | 643 | |
| 576 | 644 | if (!drive->drive.serial.serial_data || !serial.data[0]) |
| 577 | 645 | value |= 1; |
| 646 | |
| 578 | 647 | if (!drive->drive.serial.serial_clock || !serial.clock[0]) |
| 579 | 648 | value |= 4; |
| 649 | |
| 580 | 650 | if (!serial.atn[0]) value |= 0x80; |
| 581 | 651 | |
| 582 | 652 | switch (drive->drive.serial.deviceid) |
| 583 | 653 | { |
| 584 | | case 8: |
| 585 | | value &= ~0x60; |
| 586 | | break; |
| 587 | | case 9: |
| 588 | | value &= ~0x40; |
| 589 | | break; |
| 590 | | case 10: |
| 591 | | value &= ~0x20; |
| 592 | | break; |
| 593 | | case 11: |
| 594 | | break; |
| 654 | case 8: |
| 655 | value &= ~0x60; |
| 656 | break; |
| 657 | case 9: |
| 658 | value &= ~0x40; |
| 659 | break; |
| 660 | case 10: |
| 661 | value &= ~0x20; |
| 662 | break; |
| 663 | case 11: |
| 664 | break; |
| 595 | 665 | } |
| 596 | | if (value!=old) { |
| 597 | 666 | |
| 598 | | DBG_LOG(2, "vc1541 serial read",("%s %s %s\n", |
| 599 | | serial.atn[0]?"ATN":"atn", |
| 600 | | serial.clock[0]?"CLOCK":"clock", |
| 601 | | serial.data[0]?"DATA":"data")); |
| 602 | | |
| 603 | | DBG_LOG(2, "vc1541 serial read",("%s %s %s\n", |
| 604 | | value&0x80?"ATN":"atn", |
| 605 | | value&4?"CLOCK":"clock", |
| 606 | | value&1?"DATA":"data")); |
| 607 | | old=value; |
| 608 | | } |
| 609 | | |
| 610 | 667 | return value; |
| 611 | 668 | } |
| 612 | 669 | |
| 613 | 670 | static WRITE8_HANDLER( vc1541_via0_write_portb ) |
| 614 | 671 | { |
| 615 | 672 | DBG_LOG(2, "vc1541 serial write",("%s %s %s\n", |
| 616 | | data&0x10?"ATN":"atn", |
| 617 | | data&8?"CLOCK":"clock", |
| 618 | | data&2?"DATA":"data")); |
| 673 | data & 0x10 ? "ATN" : "atn", |
| 674 | data & 0x08 ? "CLOCK" : "clock", |
| 675 | data & 0x02 ? "DATA" : "data")); |
| 619 | 676 | |
| 620 | | drive->drive.serial.data=data&2?0:1; |
| 621 | | drive->drive.serial.acka=(data&0x10)?1:0; |
| 677 | drive->drive.serial.data = data & 0x02 ? 0 : 1; |
| 678 | drive->drive.serial.acka = data & 0x10 ? 1 : 0; |
| 622 | 679 | |
| 623 | | if ((!(data & 2)) != drive->drive.serial.serial_data) |
| 680 | if ((!(data & 0x02)) != drive->drive.serial.serial_data) |
| 624 | 681 | { |
| 625 | | vc1541_serial_data_write (1, drive->drive.serial.serial_data = !(data & 2)); |
| 682 | vc1541_serial_data_write (1, drive->drive.serial.serial_data = !(data & 0x02)); |
| 626 | 683 | } |
| 627 | 684 | |
| 628 | | if ((!(data & 8)) != drive->drive.serial.serial_clock) |
| 685 | if ((!(data & 0x08)) != drive->drive.serial.serial_clock) |
| 629 | 686 | { |
| 630 | | vc1541_serial_clock_write (1, drive->drive.serial.serial_clock = !(data & 8)); |
| 687 | vc1541_serial_clock_write (1, drive->drive.serial.serial_clock = !(data & 0x08)); |
| 631 | 688 | } |
| 689 | |
| 632 | 690 | vc1541_serial_atn_write (machine, 1, drive->drive.serial.serial_atn = 1); |
| 633 | 691 | } |
| 634 | 692 | |
| 693 | |
| 635 | 694 | /* |
| 636 | 695 | * via 6522 at 0x1c00 |
| 637 | 696 | * port a |
| r3311 | r3312 | |
| 659 | 718 | * |
| 660 | 719 | * irq to m6502 irq connected |
| 661 | 720 | */ |
| 721 | |
| 662 | 722 | static void vc1541_via1_irq (running_machine *machine, int level) |
| 663 | 723 | { |
| 664 | 724 | drive->via1irq = level; |
| 665 | | DBG_LOG(2, "vc1541 via1 irq",("level %d %d\n",drive->via0irq,drive->via1irq)); |
| 666 | | cpunum_set_input_line (machine, drive->cpunumber, |
| 667 | | M6502_IRQ_LINE, drive->via1irq || drive->via0irq); |
| 725 | DBG_LOG(2, "vc1541 via1 irq", ("level %d %d\n", drive->via0irq, drive->via1irq)); |
| 726 | |
| 727 | cpunum_set_input_line (machine, drive->cpunumber, M6502_IRQ_LINE, drive->via1irq || drive->via0irq); |
| 668 | 728 | } |
| 669 | 729 | |
| 670 | 730 | static READ8_HANDLER( vc1541_via1_read_porta ) |
| 671 | 731 | { |
| 672 | | int data=drive->head.data[drive->image.pos]; |
| 673 | | DBG_LOG(2, "vc1541 drive",("port a read %.2x\n", data)); |
| 732 | int data = drive->gcr.data[drive->pos]; |
| 733 | DBG_LOG(2, "vc1541 drive", ("port a read %.2x\n", data)); |
| 674 | 734 | return data; |
| 675 | 735 | } |
| 676 | 736 | |
| 677 | 737 | static WRITE8_HANDLER( vc1541_via1_write_porta ) |
| 678 | 738 | { |
| 679 | | DBG_LOG(1, "vc1541 drive",("port a write %.2x\n", data)); |
| 739 | DBG_LOG(1, "vc1541 drive", ("port a write %.2x\n", data)); |
| 680 | 740 | } |
| 681 | 741 | |
| 682 | 742 | static READ8_HANDLER( vc1541_via1_read_portb ) |
| r3311 | r3312 | |
| 687 | 747 | if (WRITEPROTECTED) |
| 688 | 748 | value &= ~0x10; |
| 689 | 749 | #endif |
| 690 | | if (drive->head.sync) { |
| 691 | | value&=~0x80; |
| 750 | |
| 751 | if (drive->gcr.sync) |
| 752 | { |
| 753 | value &= ~0x80; |
| 692 | 754 | } |
| 693 | 755 | |
| 694 | 756 | return value; |
| 695 | 757 | } |
| 696 | 758 | |
| 759 | static int old = 0; |
| 760 | |
| 697 | 761 | static WRITE8_HANDLER( vc1541_via1_write_portb ) |
| 698 | 762 | { |
| 699 | | static int old=0; |
| 700 | | if (data!=old) { |
| 763 | if (data != old) |
| 764 | { |
| 701 | 765 | DBG_LOG(1, "vc1541 drive",("%.2x\n", data)); |
| 702 | | if ((old&3)!=(data&3)) { |
| 703 | | switch (old&3) { |
| 766 | |
| 767 | if ((old & 0x03) != (data & 0x03)) |
| 768 | { |
| 769 | switch (old & 0x03) |
| 770 | { |
| 704 | 771 | case 0: |
| 705 | | if ((data&3)==1) drive->track+=0.5; |
| 706 | | else if ((data&3)==3) drive->track-=0.5; |
| 772 | if ((data & 0x03) == 1) |
| 773 | drive->track += 0.5; |
| 774 | else if ((data & 0x03) == 3) |
| 775 | drive->track -= 0.5; |
| 707 | 776 | break; |
| 777 | |
| 708 | 778 | case 1: |
| 709 | | if ((data&3)==2) drive->track+=0.5; |
| 710 | | else if ((data&3)==0) drive->track-=0.5; |
| 779 | if ((data & 0x03) == 2) |
| 780 | drive->track += 0.5; |
| 781 | else if ((data & 0x03) == 0) |
| 782 | drive->track -= 0.5; |
| 711 | 783 | break; |
| 784 | |
| 712 | 785 | case 2: |
| 713 | | if ((data&3)==3) drive->track+=0.5; |
| 714 | | else if ((data&3)==1) drive->track-=0.5; |
| 786 | if ((data & 0x03) == 3) |
| 787 | drive->track += 0.5; |
| 788 | else if ((data & 0x03) == 1) |
| 789 | drive->track -= 0.5; |
| 715 | 790 | break; |
| 791 | |
| 716 | 792 | case 3: |
| 717 | | if ((data&3)==0) drive->track+=0.5; |
| 718 | | else if ((data&3)==2) drive->track-=0.5; |
| 793 | if ((data & 0x03) == 0) |
| 794 | drive->track += 0.5; |
| 795 | else if ((data & 0x03) == 2) |
| 796 | drive->track -= 0.5; |
| 719 | 797 | break; |
| 720 | 798 | } |
| 721 | | if (drive->track<1) drive->track=1.0; |
| 722 | | if (drive->track>35) drive->track=35; |
| 799 | |
| 800 | logerror("Track #: %f\n", drive->track); |
| 801 | |
| 802 | /* If we have reached the first or last track, we cannot proceed further! */ |
| 803 | if (drive->track < 1) |
| 804 | drive->track = 1.0; |
| 805 | if (drive->track > 35) |
| 806 | drive->track = 35.0; |
| 723 | 807 | } |
| 724 | | if ( (drive->motor!=(data&4))||(drive->frequency!=(data&0x60)) ) |
| 808 | |
| 809 | if ((drive->motor != (data & 0x04)) || (drive->frequency != (data & 0x60))) |
| 725 | 810 | { |
| 726 | 811 | double tme; |
| 727 | | drive->motor = data & 4; |
| 812 | drive->motor = data & 0x04; |
| 728 | 813 | drive->frequency = data & 0x60; |
| 729 | | tme=drive_times[drive->frequency>>5]*8*2; |
| 814 | tme = drive_times[drive->frequency >> 5]; |
| 815 | |
| 730 | 816 | if (drive->motor) |
| 731 | 817 | { |
| 732 | 818 | if (attotime_to_double(timer_timeelapsed(drive->timer)) > 1.0e29) |
| 819 | { |
| 733 | 820 | timer_reset(drive->timer, attotime_never); |
| 821 | } |
| 734 | 822 | else |
| 735 | | timer_adjust_periodic(drive->timer, attotime_zero, 0, double_to_attotime(tme)); |
| 823 | { |
| 824 | timer_adjust_periodic(drive->timer, attotime_zero, 0, ATTOTIME_IN_HZ(tme)); |
| 825 | } |
| 736 | 826 | } |
| 737 | 827 | else |
| 738 | 828 | { |
| 739 | 829 | timer_reset(drive->timer, attotime_never); |
| 740 | 830 | } |
| 741 | 831 | } |
| 742 | | old=data; |
| 832 | old = data; |
| 743 | 833 | } |
| 744 | | drive->led = data & 8; |
| 834 | drive->led = data & 0x08; |
| 745 | 835 | } |
| 746 | 836 | |
| 837 | static WRITE8_HANDLER( vc1541_via1_write_portca1 ) |
| 838 | { |
| 839 | } |
| 840 | |
| 841 | static WRITE8_HANDLER( vc1541_via1_write_portca2 ) |
| 842 | { |
| 843 | } |
| 844 | |
| 747 | 845 | static const struct via6522_interface via2 = |
| 748 | 846 | { |
| 749 | 847 | 0, /*vc1541_via0_read_porta, */ |
| r3311 | r3312 | |
| 769 | 867 | 0, /*via3_read_cb2, */ |
| 770 | 868 | vc1541_via1_write_porta, |
| 771 | 869 | vc1541_via1_write_portb, |
| 772 | | 0, /*via3_write_ca1, */ |
| 870 | vc1541_via1_write_portca1, /*via3_write_ca1, */ |
| 773 | 871 | 0, /*via3_write_cb1, */ |
| 774 | | 0, /*via3_write_ca2, */ |
| 872 | vc1541_via1_write_portca2, /*via3_write_ca2, */ |
| 775 | 873 | 0, /*via3_write_cb2, */ |
| 776 | 874 | vc1541_via1_irq |
| 777 | 875 | }; |
| r3311 | r3312 | |
| 962 | 1060 | 5,6: output frequency select |
| 963 | 1061 | 7: input byte ready |
| 964 | 1062 | */ |
| 1063 | |
| 965 | 1064 | static WRITE8_HANDLER ( c1551_port_w ) |
| 966 | 1065 | { |
| 967 | | static int old=0; |
| 968 | | if (offset) { |
| 969 | | DBG_LOG(1, "c1551 port",("write %.2x\n",data)); |
| 970 | | drive->drive.c1551.cpu_port=data; |
| 1066 | if (offset) |
| 1067 | { |
| 1068 | DBG_LOG(1, "c1551 port",("write %.2x\n", data)); |
| 1069 | drive->drive.c1551.cpu_port = data; |
| 971 | 1070 | |
| 972 | | if (data!=old) { |
| 1071 | if (data != old) |
| 1072 | { |
| 973 | 1073 | DBG_LOG(1, "vc1541 drive",("%.2x\n", data)); |
| 974 | | if ((old&3)!=(data&3)) { |
| 975 | | switch (old&3) { |
| 1074 | |
| 1075 | if ((old & 0x03) != (data & 0x03)) |
| 1076 | { |
| 1077 | switch (old & 0x03) |
| 1078 | { |
| 976 | 1079 | case 0: |
| 977 | | if ((data&3)==1) drive->track+=0.5; |
| 978 | | else if ((data&3)==3) drive->track-=0.5; |
| 1080 | if ((data & 0x03) == 1) |
| 1081 | drive->track += 0.5; |
| 1082 | else if ((data & 0x03) == 3) |
| 1083 | drive->track -= 0.5; |
| 979 | 1084 | break; |
| 1085 | |
| 980 | 1086 | case 1: |
| 981 | | if ((data&3)==2) drive->track+=0.5; |
| 982 | | else if ((data&3)==0) drive->track-=0.5; |
| 1087 | if ((data & 0x03) == 2) |
| 1088 | drive->track += 0.5; |
| 1089 | else if ((data & 0x03) == 0) |
| 1090 | drive->track -= 0.5; |
| 983 | 1091 | break; |
| 1092 | |
| 984 | 1093 | case 2: |
| 985 | | if ((data&3)==3) drive->track+=0.5; |
| 986 | | else if ((data&3)==1) drive->track-=0.5; |
| 1094 | if ((data & 0x03) == 3) |
| 1095 | drive->track += 0.5; |
| 1096 | else if ((data & 0x03) == 1) |
| 1097 | drive->track -= 0.5; |
| 987 | 1098 | break; |
| 1099 | |
| 988 | 1100 | case 3: |
| 989 | | if ((data&3)==0) drive->track+=0.5; |
| 990 | | else if ((data&3)==2) drive->track-=0.5; |
| 1101 | if ((data & 0x03) == 0) |
| 1102 | drive->track += 0.5; |
| 1103 | else if ((data & 0x03) == 2) |
| 1104 | drive->track -= 0.5; |
| 991 | 1105 | break; |
| 992 | 1106 | } |
| 993 | | if (drive->track<1) drive->track=1.0; |
| 994 | | if (drive->track>35) drive->track=35; |
| 1107 | |
| 1108 | /* If we have reached the first or last track, we cannot proceed further! */ |
| 1109 | if (drive->track < 1) |
| 1110 | drive->track = 1.0; |
| 1111 | if (drive->track > 35) |
| 1112 | drive->track = 35.0; |
| 995 | 1113 | } |
| 996 | | if ( (drive->motor!=(data&4))||(drive->frequency!=(data&0x60)) ) { |
| 1114 | |
| 1115 | if ( (drive->motor != (data & 0x04)) || (drive->frequency != (data & 0x60)) ) |
| 1116 | { |
| 997 | 1117 | double tme; |
| 998 | | drive->motor = data & 4; |
| 1118 | drive->motor = data & 0x04; |
| 999 | 1119 | drive->frequency = data & 0x60; |
| 1000 | | tme=drive_times[drive->frequency>>5]*8*2; |
| 1120 | |
| 1121 | tme = drive_times[drive->frequency >> 5]; |
| 1001 | 1122 | if (drive->motor) |
| 1002 | 1123 | { |
| 1003 | 1124 | if (attotime_to_double(timer_timeelapsed(drive->timer)) > 1.0e29) |
| 1004 | 1125 | timer_reset(drive->timer, attotime_never); |
| 1005 | 1126 | else |
| 1006 | | timer_adjust_periodic(drive->timer, attotime_zero, 0, double_to_attotime(tme)); |
| 1127 | timer_adjust_periodic(drive->timer, attotime_zero, 0, ATTOTIME_IN_HZ(tme)); |
| 1007 | 1128 | } |
| 1008 | 1129 | else |
| 1009 | 1130 | { |
| 1010 | 1131 | timer_reset(drive->timer, attotime_never); |
| 1011 | 1132 | } |
| 1012 | 1133 | } |
| 1013 | | old=data; |
| 1134 | old = data; |
| 1014 | 1135 | } |
| 1015 | | drive->led = data & 8; |
| 1016 | | } else { |
| 1017 | | drive->drive.c1551.cpu_ddr=data; |
| 1018 | | DBG_LOG(1, "c1551 ddr",("write %.2x\n",data)); |
| 1136 | drive->led = data & 0x08; |
| 1137 | } |
| 1138 | else |
| 1139 | { |
| 1140 | drive->drive.c1551.cpu_ddr = data; |
| 1141 | DBG_LOG(1, "c1551 ddr", ("write %.2x\n", data)); |
| 1019 | 1142 | } |
| 1020 | 1143 | } |
| 1021 | 1144 | |
| r3311 | r3312 | |
| 1023 | 1146 | { |
| 1024 | 1147 | int data; |
| 1025 | 1148 | |
| 1026 | | if (offset) { |
| 1027 | | data=0x7f; |
| 1149 | if (offset) |
| 1150 | { |
| 1151 | data = 0x7f; |
| 1028 | 1152 | #if 0 |
| 1029 | 1153 | if (WRITEPROTECTED) |
| 1030 | 1154 | data &= ~0x10; |
| 1031 | 1155 | #endif |
| 1032 | | if (drive->head.ready) { |
| 1033 | | data|=0x80; |
| 1034 | | drive->head.ready=0; |
| 1156 | if (drive->gcr.ready) |
| 1157 | { |
| 1158 | data |= 0x80; |
| 1159 | drive->gcr.ready = 0; |
| 1035 | 1160 | } |
| 1036 | | data&=~drive->drive.c1551.cpu_ddr; |
| 1037 | | data|=drive->drive.c1551.cpu_ddr&drive->drive.c1551.cpu_port; |
| 1161 | |
| 1162 | data &= ~drive->drive.c1551.cpu_ddr; |
| 1163 | data |= drive->drive.c1551.cpu_ddr & drive->drive.c1551.cpu_port; |
| 1038 | 1164 | DBG_LOG(3, "c1551 port",("read %.2x\n", data)); |
| 1039 | | } else { |
| 1040 | | data=drive->drive.c1551.cpu_ddr; |
| 1165 | } |
| 1166 | else |
| 1167 | { |
| 1168 | data = drive->drive.c1551.cpu_ddr; |
| 1041 | 1169 | DBG_LOG(3, "c1551 ddr",("read %.2x\n", data)); |
| 1042 | 1170 | } |
| 1043 | 1171 | return data; |
| r3311 | r3312 | |
| 1061 | 1189 | */ |
| 1062 | 1190 | static int c1551_port_c_r(void) |
| 1063 | 1191 | { |
| 1064 | | int data=0xff; |
| 1065 | | data&=~0x20; |
| 1066 | | if (drive->head.sync) data&=~0x40; |
| 1192 | int data = 0xff; |
| 1193 | |
| 1194 | data &= ~0x20; |
| 1195 | |
| 1196 | if (drive->gcr.sync) |
| 1197 | data &= ~0x40; |
| 1198 | |
| 1067 | 1199 | return data; |
| 1068 | 1200 | } |
| 1069 | 1201 | |
| 1070 | 1202 | static int c1551_port_b_r (void) |
| 1071 | 1203 | { |
| 1072 | | int data=drive->head.data[drive->image.pos]; |
| 1204 | int data = drive->gcr.data[drive->pos]; |
| 1073 | 1205 | DBG_LOG(2, "c1551 drive",("port a read %.2x\n", data)); |
| 1074 | 1206 | return data; |
| 1075 | 1207 | } |
| r3311 | r3312 | |
| 1085 | 1217 | |
| 1086 | 1218 | static void c1551x_write_data (running_machine *machine, TPI6525 *This, int data) |
| 1087 | 1219 | { |
| 1088 | | DBG_LOG(1, "c1551 cpu", ("%d write data %.2x\n", |
| 1089 | | cpu_getactivecpu (), data)); |
| 1220 | DBG_LOG(1, "c1551 cpu", ("%d write data %.2x\n", cpu_getactivecpu (), data)); |
| 1090 | 1221 | #ifdef CPU_SYNC |
| 1091 | 1222 | cpu_sync(); |
| 1092 | 1223 | #endif |
| 1224 | |
| 1093 | 1225 | tpi6525_0_port_a_w(machine, 0,data); |
| 1094 | 1226 | } |
| 1095 | 1227 | |
| 1096 | 1228 | static int c1551x_read_data (running_machine *machine, TPI6525 *This) |
| 1097 | 1229 | { |
| 1098 | | int data=0xff; |
| 1230 | int data = 0xff; |
| 1099 | 1231 | #ifdef CPU_SYNC |
| 1100 | 1232 | cpu_sync (); |
| 1101 | 1233 | #endif |
| 1102 | | data=tpi6525_0_port_a_r(machine, 0); |
| 1103 | | DBG_LOG(2, "c1551 cpu",("%d read data %.2x\n", |
| 1104 | | cpu_getactivecpu (), data)); |
| 1234 | |
| 1235 | data = tpi6525_0_port_a_r(machine, 0); |
| 1236 | DBG_LOG(2, "c1551 cpu",("%d read data %.2x\n", cpu_getactivecpu (), data)); |
| 1105 | 1237 | return data; |
| 1106 | 1238 | } |
| 1107 | 1239 | |
| 1108 | 1240 | static void c1551x_write_handshake (running_machine *machine, TPI6525 *This, int data) |
| 1109 | 1241 | { |
| 1110 | | DBG_LOG(1, "c1551 cpu",("%d write handshake %.2x\n", |
| 1111 | | cpu_getactivecpu (), data)); |
| 1242 | DBG_LOG(1, "c1551 cpu",("%d write handshake %.2x\n", cpu_getactivecpu (), data)); |
| 1112 | 1243 | #ifdef CPU_SYNC |
| 1113 | 1244 | cpu_sync(); |
| 1114 | 1245 | #endif |
| 1246 | |
| 1115 | 1247 | tpi6525_0_port_c_w(machine, 0,data&0x40?0xff:0x7f); |
| 1116 | 1248 | } |
| 1117 | 1249 | |
| 1118 | 1250 | static int c1551x_read_handshake (running_machine *machine, TPI6525 *This) |
| 1119 | 1251 | { |
| 1120 | | int data=0xff; |
| 1252 | int data = 0xff; |
| 1121 | 1253 | #ifdef CPU_SYNC |
| 1122 | 1254 | cpu_sync(); |
| 1123 | 1255 | #endif |
| 1124 | | data=tpi6525_0_port_c_r(machine, 0)&8?0x80:0; |
| 1125 | | DBG_LOG(2, "c1551 cpu",("%d read handshake %.2x\n", |
| 1126 | | cpu_getactivecpu (), data)); |
| 1256 | |
| 1257 | data = tpi6525_0_port_c_r(machine, 0) & 0x08 ? 0x80 : 0x00; |
| 1258 | DBG_LOG(2, "c1551 cpu",("%d read handshake %.2x\n", cpu_getactivecpu (), data)); |
| 1127 | 1259 | return data; |
| 1128 | 1260 | } |
| 1129 | 1261 | |
| 1130 | 1262 | static int c1551x_read_status (running_machine *machine, TPI6525 *This) |
| 1131 | 1263 | { |
| 1132 | | int data=0xff; |
| 1264 | int data = 0xff; |
| 1133 | 1265 | #ifdef CPU_SYNC |
| 1134 | 1266 | cpu_sync(); |
| 1135 | 1267 | #endif |
| 1136 | | data=tpi6525_0_port_c_r(machine, 0)&3; |
| 1137 | | DBG_LOG(1, "c1551 cpu",("%d read status %.2x\n", |
| 1138 | | cpu_getactivecpu (), data)); |
| 1268 | |
| 1269 | data = tpi6525_0_port_c_r(machine, 0) & 0x03; |
| 1270 | DBG_LOG(1, "c1551 cpu",("%d read status %.2x\n", cpu_getactivecpu (), data)); |
| 1271 | |
| 1139 | 1272 | return data; |
| 1140 | 1273 | } |
| 1141 | 1274 | |
| r3311 | r3312 | |
| 1170 | 1303 | |
| 1171 | 1304 | **************************************/ |
| 1172 | 1305 | |
| 1173 | | |
| 1174 | 1306 | int drive_config (int type, int id, int mode, int cpunr, int devicenr) |
| 1175 | 1307 | { |
| 1176 | 1308 | drive->type = type; |
| r3311 | r3312 | |
| 1186 | 1318 | |
| 1187 | 1319 | if (type == type_1551) |
| 1188 | 1320 | { |
| 1189 | | tpi6525[0].c.read=c1551_port_c_r; |
| 1190 | | tpi6525[0].b.read=c1551_port_b_r; |
| 1321 | tpi6525[0].c.read = c1551_port_c_r; |
| 1322 | tpi6525[0].b.read = c1551_port_b_r; |
| 1191 | 1323 | |
| 1192 | 1324 | /* time should be small enough to allow quitting of the irq |
| 1193 | 1325 | line before the next interrupt is triggered */ |
| r3311 | r3312 | |
| 1257 | 1389 | MDRV_CPU_PROGRAM_MAP(_1571_map, 0) |
| 1258 | 1390 | MACHINE_DRIVER_END |
| 1259 | 1391 | |
| 1392 | |
| 1260 | 1393 | /************************************** |
| 1261 | 1394 | |
| 1262 | 1395 | Device configurations |
| 1263 | 1396 | |
| 1264 | 1397 | **************************************/ |
| 1265 | 1398 | |
| 1266 | | DEVICE_IMAGE_LOAD(vc1541) |
| 1399 | /* At a later stage we should move the loading and encoding part to an external function |
| 1400 | for all formats / drive and then call it here and in similar DEVICE_IMAGE_LOADs for |
| 1401 | the other drive types with 'image' as a parameter */ |
| 1402 | static DEVICE_IMAGE_LOAD( _1541 ) |
| 1267 | 1403 | { |
| 1268 | | offset_init (format_d64); |
| 1404 | UINT8 sector_checksum, format_id_1, format_id_2; |
| 1405 | int i = 0, j, k, l; |
| 1406 | int format = 0, pos, filesize; |
| 1407 | UINT8 *temp_copy = NULL; |
| 1408 | const char *filetype; |
| 1269 | 1409 | |
| 1270 | | /* Fix me: we should load directly the GCR data */ |
| 1271 | | drive->image.data = image_ptr(image); |
| 1272 | | if (!drive->image.data) |
| 1410 | filesize = image_length(image); |
| 1411 | filetype = image_filetype(image); |
| 1412 | |
| 1413 | /* Find out which type of image we are loading */ |
| 1414 | if (!mame_stricmp (filetype, "d64")) |
| 1415 | { |
| 1416 | switch (filesize) |
| 1417 | { |
| 1418 | case 0x2ab00: |
| 1419 | format = format_d64; |
| 1420 | break; |
| 1421 | case (0x2ab00 + 0x2ab): |
| 1422 | format = format_d64_err; |
| 1423 | break; |
| 1424 | case 0x30000: |
| 1425 | format = format_d64_40t; |
| 1426 | break; |
| 1427 | case (0x30000 + 0x300): |
| 1428 | format = format_d64_40t_err; |
| 1429 | break; |
| 1430 | default: |
| 1431 | logerror("The image you're trying to load has not the regular size of a .d64 image: \n"); |
| 1432 | logerror("Image %s size %d\n", image_filename(image), filesize); |
| 1433 | return INIT_FAIL; |
| 1434 | } |
| 1435 | } |
| 1436 | // these are not supported yet! |
| 1437 | else if (!mame_stricmp (filetype, "d71")) |
| 1438 | { |
| 1439 | switch (filesize) |
| 1440 | { |
| 1441 | case 0x55600: |
| 1442 | format = format_d71; |
| 1443 | break; |
| 1444 | case (0x55600 + 0x556): |
| 1445 | format = format_d71_err; |
| 1446 | break; |
| 1447 | default: |
| 1448 | logerror("The image you're trying to load has not the regular size of a .d71 image: \n"); |
| 1449 | logerror("Image %s size %d\n", image_filename(image), filesize); |
| 1450 | return INIT_FAIL; |
| 1451 | } |
| 1452 | } |
| 1453 | else if (!mame_stricmp (filetype, "d81")) |
| 1454 | format = format_d81; |
| 1455 | else if (!mame_stricmp (filetype, "d80")) |
| 1456 | format = format_d80; |
| 1457 | else if (!mame_stricmp (filetype, "d82")) |
| 1458 | format = format_d82; |
| 1459 | else |
| 1460 | { |
| 1461 | /* Still have to work on d67 & g64 images */ |
| 1462 | logerror("Unsupported format\n"); |
| 1273 | 1463 | return INIT_FAIL; |
| 1464 | } |
| 1465 | |
| 1466 | /* Set up track / sector map according to the format of the image */ |
| 1467 | offset_init (format); |
| 1274 | 1468 | |
| 1275 | | logerror("floppy image %s loaded\n", image_filename(image)); |
| 1469 | /* Claim memory */ |
| 1470 | temp_copy = auto_malloc(filesize); |
| 1276 | 1471 | |
| 1472 | if ((image_fread( image, temp_copy, filesize ) != filesize) || !filesize) |
| 1473 | return INIT_FAIL; |
| 1474 | |
| 1475 | /* Start GCR encoding */ |
| 1476 | format_id_1 = temp_copy[disk_id1(format)]; // can be modified by error code 29 -> ^ 0xff |
| 1477 | format_id_2 = temp_copy[disk_id2(format)]; |
| 1478 | |
| 1479 | for (k = 1; k <= D64_MAX_TRACKS; k++) |
| 1480 | { |
| 1481 | /* logerror("decoding track %d offset %d %d %d %d %d\n", k, |
| 1482 | image_tracksector2offset(k, 0) / (16*16*16*16), |
| 1483 | (image_tracksector2offset(k, 0) % (16*16*16*16)) / (16*16*16), |
| 1484 | (image_tracksector2offset(k, 0) % (16*16*16)) / (16*16), |
| 1485 | (image_tracksector2offset(k, 0) % (16*16)) / 16, |
| 1486 | image_tracksector2offset(k, 0) % 16); |
| 1487 | */ |
| 1488 | |
| 1489 | for (l = 0; l < d64_sectors_per_track[k - 1]; l++) |
| 1490 | { |
| 1491 | // here we convert the sector data to gcr directly! |
| 1492 | // IMPORTANT: we shall implement errors in reading sectors! |
| 1493 | // these can modify e.g. header info $01 & $05 |
| 1494 | |
| 1495 | // first we set the position at which sector data starts in the image |
| 1496 | pos = image_tracksector2offset(k, l); |
| 1497 | |
| 1498 | /* |
| 1499 | 1. Header sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 1500 | 2. Header info 52 54 B5 29 4B 7A 5E 95 55 55 (10 GCR bytes) |
| 1501 | 3. Header gap 55 55 55 55 55 55 55 55 55 (9 bytes, never read) |
| 1502 | 4. Data sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 1503 | 5. Data block 55...4A (325 GCR bytes) |
| 1504 | 6. Inter-sector gap 55 55 55 55...55 55 (4 to 19 bytes, never read) |
| 1505 | */ |
| 1506 | |
| 1507 | /* Header sync */ |
| 1508 | for (j = 0; j < 5; j++) |
| 1509 | drive->gcr.data[i + j] = 0xff; |
| 1510 | i += 5; |
| 1511 | |
| 1512 | /* Header info */ |
| 1513 | /* These are 8 bytes unencoded, which become 10 bytes encoded */ |
| 1514 | // $00 - header block ID ($08) // this byte can be modified by error code 20 -> 0xff |
| 1515 | // $01 - header block checksum (EOR of $02-$05) // this byte can be modified by error code 27 -> ^ 0xff |
| 1516 | // $02 - Sector# of data block |
| 1517 | // $03 - Track# of data block |
| 1518 | gcr_double_2_gcr(0x08, l ^ k ^ format_id_2 ^ format_id_1, l, k, drive->gcr.data + i); |
| 1519 | i += 5; |
| 1520 | |
| 1521 | // $04 - Format ID byte #2 |
| 1522 | // $05 - Format ID byte #1 |
| 1523 | // $06 - $0F ("off" byte) |
| 1524 | // $07 - $0F ("off" byte) |
| 1525 | gcr_double_2_gcr(format_id_2, format_id_1, 0x0f, 0x0f, drive->gcr.data + i); |
| 1526 | i += 5; |
| 1527 | |
| 1528 | /* Header gap */ |
| 1529 | for (j = 0; j < 9; j++) |
| 1530 | drive->gcr.data[i + j] = 0x55; |
| 1531 | i += 9; |
| 1532 | |
| 1533 | /* Data sync */ |
| 1534 | for (j = 0; j < 5; j++) |
| 1535 | drive->gcr.data[i + j] = 0xff; |
| 1536 | i += 5; |
| 1537 | |
| 1538 | /* Data block */ |
| 1539 | // we first need to calculate the checksum of the 256 bytes of the sector |
| 1540 | sector_checksum = temp_copy[pos]; |
| 1541 | for (j = 1; j < 256; j++) |
| 1542 | sector_checksum ^= temp_copy[pos + j]; |
| 1543 | |
| 1544 | /* |
| 1545 | $00 - data block ID ($07) |
| 1546 | $01-100 - 256 bytes sector data |
| 1547 | $101 - data block checksum (EOR of $01-100) |
| 1548 | $102-103 - $00 ("off" bytes, to make the sector size a multiple of 5) |
| 1549 | */ |
| 1550 | gcr_double_2_gcr(0x07, temp_copy[pos], temp_copy[pos + 1], |
| 1551 | temp_copy[pos + 2], drive->gcr.data + i); |
| 1552 | i += 5; |
| 1553 | |
| 1554 | for (j = 1; j < 64; j++) |
| 1555 | { |
| 1556 | gcr_double_2_gcr(temp_copy[pos + 4 * j - 1], temp_copy[pos + 4 * j], |
| 1557 | temp_copy[pos + 4 * j + 1], temp_copy[pos + 4 * j + 2], drive->gcr.data + i); |
| 1558 | i += 5; |
| 1559 | } |
| 1560 | |
| 1561 | gcr_double_2_gcr(temp_copy[pos + 255], sector_checksum, 0x00, 0x00, drive->gcr.data + i); |
| 1562 | i += 5; |
| 1563 | |
| 1564 | /* Inter-sector gap */ |
| 1565 | // "In tests that the author conducted on a real 1541 disk, gap sizes of 8 to 19 bytes were seen." |
| 1566 | // Here we put 14 as an average... |
| 1567 | for (j = 0; j < 14; j++) |
| 1568 | drive->gcr.data[i + j] = 0x55; |
| 1569 | i += 14; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | logerror("Floppy image %s successfully loaded\n", image_filename(image)); |
| 1277 | 1574 | return INIT_PASS; |
| 1278 | 1575 | } |
| 1279 | 1576 | |
| 1280 | | DEVICE_IMAGE_UNLOAD(vc1541) |
| 1577 | static DEVICE_IMAGE_UNLOAD( _1541 ) |
| 1281 | 1578 | { |
| 1282 | | /* writeback of image data */ |
| 1283 | | drive->image.data = NULL; |
| 1284 | 1579 | } |
| 1285 | 1580 | |
| 1286 | 1581 | void vc1541_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| r3311 | r3312 | |
| 1296 | 1591 | case MESS_DEVINFO_INT_RESET_ON_LOAD: info->i = 1; break; |
| 1297 | 1592 | |
| 1298 | 1593 | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 1299 | | case MESS_DEVINFO_PTR_LOAD: info->load = DEVICE_IMAGE_LOAD_NAME(vc1541); break; |
| 1300 | | case MESS_DEVINFO_PTR_UNLOAD: info->unload = DEVICE_IMAGE_UNLOAD_NAME(vc1541); break; |
| 1594 | case MESS_DEVINFO_PTR_LOAD: info->load = DEVICE_IMAGE_LOAD_NAME(_1541); break; |
| 1595 | case MESS_DEVINFO_PTR_UNLOAD: info->unload = DEVICE_IMAGE_UNLOAD_NAME(_1541); break; |
| 1301 | 1596 | case MESS_DEVINFO_PTR_DEV_SPECIFIC: info->f = (genf *) drive_config; break; |
| 1302 | 1597 | |
| 1303 | 1598 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| r3311 | r3312 | |
| 1306 | 1601 | } |
| 1307 | 1602 | |
| 1308 | 1603 | |
| 1309 | | |
| 1310 | 1604 | void c2031_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 1311 | 1605 | { |
| 1312 | 1606 | vc1541_device_getinfo(devclass, state, info); |
| 1313 | 1607 | } |
| 1314 | 1608 | |
| 1315 | 1609 | |
| 1316 | | |
| 1317 | 1610 | void c1551_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 1318 | 1611 | { |
| 1319 | 1612 | switch(state) |
| r3311 | r3312 | |
| 1326 | 1619 | } |
| 1327 | 1620 | |
| 1328 | 1621 | |
| 1329 | | |
| 1330 | 1622 | void c1571_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 1331 | 1623 | { |
| 1332 | 1624 | vc1541_device_getinfo(devclass, state, info); |