src/mess/machine/c1541.c
| r6607 | r6608 | |
| 118 | 118 | |
| 119 | 119 | TODO: |
| 120 | 120 | |
| 121 | | - serial bus |
| 121 | - mechanical track 0 sensing |
| 122 | - get floppy image to driver state |
| 123 | - allocate track buffer |
| 122 | 124 | - activity led |
| 123 | | - bus reset |
| 124 | | - spindle motor |
| 125 | | - stepper motor |
| 126 | 125 | - read G64 |
| 127 | 126 | - write G64 |
| 127 | - D64 to G64 conversion |
| 128 | 128 | - accurate timing |
| 129 | 129 | |
| 130 | 130 | */ |
| r6607 | r6608 | |
| 141 | 141 | PARAMETERS |
| 142 | 142 | ***************************************************************************/ |
| 143 | 143 | |
| 144 | #define LOG 1 |
| 145 | |
| 144 | 146 | #define M6502_TAG "ucd5" |
| 145 | 147 | #define M6522_0_TAG "uab1" |
| 146 | 148 | #define M6522_1_TAG "ucd4" |
| 149 | #define TIMER_BIT_TAG "ue7" |
| 147 | 150 | |
| 151 | static const double C1541_BITRATE[4] = |
| 152 | { |
| 153 | XTAL_16MHz/13.0, XTAL_16MHz/14.0, XTAL_16MHz/15.0, XTAL_16MHz/16.0 |
| 154 | }; |
| 155 | |
| 156 | #define C1541_TRACK_BUFFER_SIZE 0x2000 |
| 157 | |
| 148 | 158 | /*************************************************************************** |
| 149 | 159 | TYPE DEFINITIONS |
| 150 | 160 | ***************************************************************************/ |
| r6607 | r6608 | |
| 152 | 162 | typedef struct _c1541_t c1541_t; |
| 153 | 163 | struct _c1541_t |
| 154 | 164 | { |
| 155 | | UINT8 yb; |
| 156 | | int atna; |
| 165 | /* abstractions */ |
| 166 | int address; /* serial bus address - 8 */ |
| 167 | UINT8 *track_buffer; /* track data buffer */ |
| 168 | int track; /* current position of the actuator (half track) */ |
| 169 | int buffer_pos; /* current byte position within track buffer */ |
| 170 | int bit_pos; /* current bit position within track buffer byte */ |
| 157 | 171 | |
| 172 | /* signals */ |
| 173 | UINT8 yb; /* GCR data byte */ |
| 174 | int byte; /* byte ready */ |
| 175 | int atna; /* attention acknowledge */ |
| 176 | int ds; /* data speed */ |
| 177 | int stp; /* stepper motor phase */ |
| 178 | int soe; /* s? output enable */ |
| 179 | int mode; /* mode (0 = write, 1 = read) */ |
| 180 | |
| 158 | 181 | /* devices */ |
| 159 | 182 | const device_config *cpu; |
| 160 | 183 | const device_config *via0; |
| 161 | 184 | const device_config *via1; |
| 162 | 185 | const device_config *serial_bus; |
| 186 | const device_config *image; |
| 187 | |
| 188 | /* timers */ |
| 189 | emu_timer *bit_timer; |
| 163 | 190 | }; |
| 164 | 191 | |
| 165 | 192 | /*************************************************************************** |
| r6607 | r6608 | |
| 181 | 208 | return (c1541_config *)device->inline_config; |
| 182 | 209 | } |
| 183 | 210 | |
| 211 | INLINE void set_byte_ready(c1541_t *c1541, int byte) |
| 212 | { |
| 213 | if (c1541->byte != byte) |
| 214 | { |
| 215 | int byte_ready = !(c1541->soe && byte_ready); |
| 216 | |
| 217 | cpu_set_input_line(c1541->cpu, M6502_SET_OVERFLOW, byte_ready); |
| 218 | via_ca1_w(c1541->via1, 0, byte_ready); |
| 219 | |
| 220 | c1541->byte = byte; |
| 221 | } |
| 222 | } |
| 223 | |
| 184 | 224 | /*************************************************************************** |
| 185 | 225 | IMPLEMENTATION |
| 186 | 226 | ***************************************************************************/ |
| 187 | 227 | |
| 188 | 228 | /*------------------------------------------------- |
| 229 | TIMER_CALLBACK( bit_tick ) |
| 230 | -------------------------------------------------*/ |
| 231 | |
| 232 | static TIMER_CALLBACK( bit_tick ) |
| 233 | { |
| 234 | const device_config *device = (device_config *) param; |
| 235 | c1541_t *c1541 = get_safe_token(device); |
| 236 | int byte_ready = 0; |
| 237 | |
| 238 | if (c1541->bit_pos == 0x07) |
| 239 | { |
| 240 | c1541->yb = c1541->track_buffer[c1541->buffer_pos]; |
| 241 | c1541->buffer_pos++; |
| 242 | c1541->bit_pos = 0; |
| 243 | |
| 244 | byte_ready = 1; |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | c1541->bit_pos++; |
| 249 | } |
| 250 | |
| 251 | set_byte_ready(c1541, byte_ready); |
| 252 | } |
| 253 | |
| 254 | /*------------------------------------------------- |
| 189 | 255 | c1541_atn_w - serial bus attention |
| 190 | 256 | -------------------------------------------------*/ |
| 191 | 257 | |
| r6607 | r6608 | |
| 193 | 259 | { |
| 194 | 260 | c1541_t *c1541 = get_safe_token(device); |
| 195 | 261 | |
| 196 | | via_ca1_w(c1541->via1, 0, state); |
| 262 | via_ca1_w(c1541->via0, 0, state); |
| 197 | 263 | } |
| 198 | 264 | |
| 199 | 265 | /*------------------------------------------------- |
| r6607 | r6608 | |
| 202 | 268 | |
| 203 | 269 | static WRITE_LINE_DEVICE_HANDLER( c1541_reset_w ) |
| 204 | 270 | { |
| 205 | | // reset cpu and vias |
| 271 | c1541_t *c1541 = get_safe_token(device); |
| 272 | |
| 273 | if (state) |
| 274 | { |
| 275 | device_reset(c1541->cpu); |
| 276 | device_reset(c1541->via0); |
| 277 | device_reset(c1541->via1); |
| 278 | } |
| 206 | 279 | } |
| 207 | 280 | |
| 208 | 281 | /*------------------------------------------------- |
| r6607 | r6608 | |
| 274 | 347 | PB1 STP0 |
| 275 | 348 | PB2 MTR |
| 276 | 349 | PB3 ACT |
| 277 | | PB4 WPS |
| 278 | | PB5 |
| 279 | | PB6 |
| 280 | | PB7 SYNC |
| 350 | PB4 WPS input write protect sensor |
| 351 | PB5 DS0 |
| 352 | PB6 DS1 |
| 353 | PB7 SYNC input |
| 281 | 354 | |
| 282 | 355 | */ |
| 283 | 356 | |
| 284 | | return 0; |
| 357 | c1541_t *c1541 = get_safe_token(device); |
| 358 | UINT8 data = 0; |
| 359 | |
| 360 | /* write protect sensor */ |
| 361 | data |= (floppy_drive_get_flag_state(c1541->image, FLOPPY_DRIVE_DISK_WRITE_PROTECTED)) << 4; |
| 362 | |
| 363 | /* SYNC detected */ |
| 364 | data |= !(c1541->mode && (c1541->yb == 0xff)) << 7; |
| 365 | |
| 366 | return data; |
| 285 | 367 | } |
| 286 | 368 | |
| 287 | 369 | static WRITE8_DEVICE_HANDLER( via0_pb_w ) |
| r6607 | r6608 | |
| 294 | 376 | PB1 STP0 |
| 295 | 377 | PB2 MTR |
| 296 | 378 | PB3 ACT |
| 297 | | PB4 WPS |
| 298 | | PB5 |
| 299 | | PB6 |
| 300 | | PB7 SYNC |
| 379 | PB4 WPS input |
| 380 | PB5 DS0 |
| 381 | PB6 DS1 |
| 382 | PB7 SYNC input |
| 301 | 383 | |
| 302 | 384 | */ |
| 385 | |
| 386 | c1541_t *c1541 = get_safe_token(device); |
| 387 | int stp = data & 0x03; |
| 388 | int ds = (data >> 5) & 0x03; |
| 389 | |
| 390 | /* stepper motor */ |
| 391 | if (c1541->stp != stp) |
| 392 | { |
| 393 | int tracks = 0; |
| 394 | int length; |
| 395 | |
| 396 | switch (c1541->stp) |
| 397 | { |
| 398 | case 0: if (stp == 1) tracks++; else if (stp == 3) tracks--; break; |
| 399 | case 1: if (stp == 2) tracks++; else if (stp == 0) tracks--; break; |
| 400 | case 2: if (stp == 3) tracks++; else if (stp == 1) tracks--; break; |
| 401 | case 3: if (stp == 0) tracks++; else if (stp == 2) tracks--; break; |
| 402 | } |
| 403 | |
| 404 | floppy_drive_seek(c1541->image, tracks); |
| 405 | |
| 406 | // if (c1541->track < 0) c1541->track = 0; // BUMP! |
| 407 | // if (c1541->track > 83) c1541->track = 83; |
| 408 | |
| 409 | // if (LOG) logerror("C1541 Track: %f\n", (float)c1541->track / 2); |
| 410 | |
| 411 | floppy_drive_read_track_data_info_buffer(c1541->image, 0, c1541->track_buffer, &length); |
| 412 | |
| 413 | if (LOG) logerror("C1541 Track length %u\n", length); |
| 414 | |
| 415 | c1541->stp = stp; |
| 416 | } |
| 417 | |
| 418 | /* spindle motor */ |
| 419 | floppy_drive_set_motor_state(c1541->image, BIT(data, 2) ? FLOPPY_DRIVE_MOTOR_ON : 0); |
| 420 | timer_enable(c1541->bit_timer, BIT(data, 2)); |
| 421 | |
| 422 | /* activity LED */ |
| 423 | |
| 424 | /* data speed */ |
| 425 | if (c1541->ds != ds) |
| 426 | { |
| 427 | timer_adjust_periodic(c1541->bit_timer, attotime_zero, 0, ATTOTIME_IN_HZ(C1541_BITRATE[ds])); |
| 428 | c1541->ds = ds; |
| 429 | } |
| 303 | 430 | } |
| 304 | 431 | |
| 305 | 432 | static WRITE_LINE_DEVICE_HANDLER( soe_w ) |
| 306 | 433 | { |
| 434 | c1541_t *c1541 = get_safe_token(device); |
| 435 | int byte_ready = !(state && c1541->byte); |
| 436 | |
| 437 | c1541->soe = state; |
| 438 | |
| 439 | cpu_set_input_line(c1541->cpu, M6502_SET_OVERFLOW, byte_ready); |
| 440 | via_ca1_w(c1541->via0, 0, byte_ready); |
| 307 | 441 | } |
| 308 | 442 | |
| 309 | 443 | static WRITE_LINE_DEVICE_HANDLER( mode_w ) |
| 310 | 444 | { |
| 445 | c1541_t *c1541 = get_safe_token(device); |
| 446 | |
| 447 | c1541->mode = state; |
| 311 | 448 | } |
| 312 | 449 | |
| 313 | 450 | static const via6522_interface c1541_via0_intf = |
| r6607 | r6608 | |
| 344 | 481 | PB2 CLK IN |
| 345 | 482 | PB3 CLK OUT |
| 346 | 483 | PB4 ATNA |
| 347 | | PB5 |
| 348 | | PB6 |
| 484 | PB5 J1 |
| 485 | PB6 J2 |
| 349 | 486 | PB7 ATN IN |
| 350 | 487 | |
| 351 | 488 | */ |
| r6607 | r6608 | |
| 359 | 496 | /* clock in */ |
| 360 | 497 | data |= cbmserial_clk_r(c1541->serial_bus) << 2; |
| 361 | 498 | |
| 499 | /* serial bus address */ |
| 500 | data |= c1541->address << 5; |
| 501 | |
| 362 | 502 | /* attention in */ |
| 363 | 503 | data |= cbmserial_atn_r(c1541->serial_bus) << 7; |
| 364 | 504 | |
| r6607 | r6608 | |
| 376 | 516 | PB2 CLK IN |
| 377 | 517 | PB3 CLK OUT |
| 378 | 518 | PB4 ATNA |
| 379 | | PB5 |
| 380 | | PB6 |
| 519 | PB5 J1 |
| 520 | PB6 J2 |
| 381 | 521 | PB7 ATN IN |
| 382 | 522 | |
| 383 | 523 | */ |
| r6607 | r6608 | |
| 539 | 679 | c1541_t *c1541 = get_safe_token(device); |
| 540 | 680 | const c1541_config *config = get_safe_config(device); |
| 541 | 681 | |
| 682 | /* set serial address */ |
| 683 | assert((config->address > 7) && (config->address < 12)); |
| 684 | c1541->address = config->address - 8; |
| 685 | |
| 542 | 686 | /* find our CPU */ |
| 543 | 687 | c1541->cpu = device_find_child_by_tag(device, M6502_TAG); |
| 544 | 688 | |
| r6607 | r6608 | |
| 547 | 691 | c1541->via1 = device_find_child_by_tag(device, M6522_1_TAG); |
| 548 | 692 | c1541->serial_bus = devtag_get_device(device->machine, config->serial_bus_tag); |
| 549 | 693 | |
| 694 | /* allocate data timer */ |
| 695 | c1541->bit_timer = timer_alloc(device->machine, bit_tick, (void *)device); |
| 696 | timer_adjust_periodic(c1541->bit_timer, attotime_zero, 0, ATTOTIME_IN_HZ(C1541_BITRATE[0])); |
| 697 | |
| 550 | 698 | /* register for state saving */ |
| 551 | 699 | // state_save_register_device_item(device, 0, c1541->); |
| 552 | 700 | } |