src/mame/machine/stvcd.c
| r12516 | r12517 | |
| 44 | 44 | #define MAX_FILTERS (24) |
| 45 | 45 | #define MAX_BLOCKS (200) |
| 46 | 46 | #define MAX_DIR_SIZE (256*1024) |
| 47 | | #define CD_SPEED 75*1 /* TODO: should be x2 */ |
| 48 | 47 | |
| 49 | 48 | typedef struct |
| 50 | 49 | { |
| r12516 | r12517 | |
| 124 | 123 | static trans32T xfertype32; |
| 125 | 124 | static UINT32 xfercount, calcsize; |
| 126 | 125 | static UINT32 xferoffs, xfersect, xfersectpos, xfersectnum, xferdnum; |
| 127 | | static UINT8 command; |
| 128 | 126 | |
| 129 | 127 | static filterT filters[MAX_FILTERS]; |
| 130 | 128 | static filterT *cddevice; |
| r12516 | r12517 | |
| 139 | 137 | static UINT32 fadstoplay = 0; |
| 140 | 138 | static int buffull, sectorstore, freeblocks; |
| 141 | 139 | static int cur_track; |
| 140 | static UINT8 cmd_pending; |
| 141 | static UINT8 cd_speed; |
| 142 | 142 | |
| 143 | 143 | // iso9660 utilities |
| 144 | 144 | static void read_new_dir(running_machine &machine, UINT32 fileno); |
| r12516 | r12517 | |
| 191 | 191 | cr4 = cd_curfad; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | | TIMER_DEVICE_CALLBACK( stv_sector_cb ) |
| 195 | | { |
| 196 | | if (fadstoplay) |
| 197 | | { |
| 198 | | cd_playdata(); |
| 199 | | } |
| 200 | | //else |
| 201 | | { |
| 202 | | hirqreg |= SCDQ; |
| 203 | | } |
| 194 | static void cd_free_block(blockT *blktofree); |
| 195 | static void cd_defragblocks(partitionT *part); |
| 196 | static void cd_getsectoroffsetnum(UINT32 bufnum, UINT32 *sectoffs, UINT32 *sectnum); |
| 204 | 197 | |
| 205 | | cd_stat |= CD_STAT_PERI; |
| 206 | | cr_standard_return(cd_stat); |
| 207 | | |
| 208 | | timer.adjust(attotime::from_hz(CD_SPEED)); |
| 209 | | } |
| 210 | | |
| 211 | | // global functions |
| 212 | | void stvcd_reset(running_machine &machine) |
| 198 | static void cd_exec_command(running_machine &machine) |
| 213 | 199 | { |
| 214 | | INT32 i, j; |
| 200 | UINT32 temp; |
| 215 | 201 | |
| 216 | | hirqmask = 0xffff; |
| 217 | | hirqreg = 0xffff; |
| 218 | | cr1 = 'C'; |
| 219 | | cr2 = ('D'<<8) | 'B'; |
| 220 | | cr3 = ('L'<<8) | 'O'; |
| 221 | | cr4 = ('C'<<8) | 'K'; |
| 222 | | cd_stat = CD_STAT_PAUSE; |
| 223 | | cur_track = 0xff; |
| 224 | | |
| 225 | | if (curdir != (direntryT *)NULL) |
| 226 | | auto_free(machine, curdir); |
| 227 | | curdir = (direntryT *)NULL; // no directory yet |
| 228 | | |
| 229 | | xfertype = XFERTYPE_INVALID; |
| 230 | | xfertype32 = XFERTYPE32_INVALID; |
| 231 | | |
| 232 | | // reset flag vars |
| 233 | | buffull = sectorstore = 0; |
| 234 | | |
| 235 | | freeblocks = 200; |
| 236 | | |
| 237 | | sectlenin = sectlenout = 2048; |
| 238 | | |
| 239 | | lastbuf = 0xff; |
| 240 | | |
| 241 | | // reset buffer partitions |
| 242 | | for (i = 0; i < MAX_FILTERS; i++) |
| 202 | if (!cdrom) |
| 243 | 203 | { |
| 244 | | partitions[i].size = -1; |
| 245 | | partitions[i].numblks = 0; |
| 246 | | |
| 247 | | for (j = 0; j < MAX_BLOCKS; j++) |
| 248 | | { |
| 249 | | partitions[i].blocks[j] = (blockT *)NULL; |
| 250 | | partitions[i].bnum[j] = 0xff; |
| 251 | | } |
| 252 | | } |
| 253 | | |
| 254 | | // reset blocks |
| 255 | | for (i = 0; i < MAX_BLOCKS; i++) |
| 256 | | { |
| 257 | | blocks[i].size = -1; |
| 258 | | memset(&blocks[i].data, 0, CD_MAX_SECTOR_DATA); |
| 259 | | } |
| 260 | | |
| 261 | | // open device |
| 262 | | if (cdrom) |
| 263 | | { |
| 264 | | cdrom_close(cdrom); |
| 265 | | cdrom = (cdrom_file *)NULL; |
| 266 | | } |
| 267 | | |
| 268 | | #ifdef MESS |
| 269 | | cdrom = machine.device<cdrom_image_device>("cdrom")->get_cdrom_file(); |
| 270 | | #else |
| 271 | | cdrom = cdrom_open(get_disk_handle(machine, "cdrom")); |
| 272 | | #endif |
| 273 | | |
| 274 | | cdda_set_cdrom( machine.device("cdda"), cdrom ); |
| 275 | | |
| 276 | | if (cdrom) |
| 277 | | { |
| 278 | | CDROM_LOG(("Opened CD-ROM successfully, reading root directory\n")) |
| 279 | | read_new_dir(machine, 0xffffff); // read root directory |
| 280 | | } |
| 281 | | else |
| 282 | | { |
| 283 | 204 | cd_stat = CD_STAT_OPEN; |
| 284 | | } |
| 285 | | |
| 286 | | sector_timer = machine.device<timer_device>("sector_timer"); |
| 287 | | //sector_timer->adjust(attotime::from_hz(CD_SPEED)); // 150 sectors / second = 300kBytes/second |
| 288 | | } |
| 289 | | |
| 290 | | static blockT *cd_alloc_block(UINT8 *blknum) |
| 291 | | { |
| 292 | | INT32 i; |
| 293 | | |
| 294 | | // search the 200 available blocks for a free one |
| 295 | | for (i = 0; i < 200; i++) |
| 296 | | { |
| 297 | | if (blocks[i].size == -1) |
| 298 | | { |
| 299 | | freeblocks--; |
| 300 | | if (freeblocks <= 0) |
| 301 | | { |
| 302 | | buffull = 1; |
| 303 | | } |
| 304 | | |
| 305 | | blocks[i].size = sectlenin; |
| 306 | | *blknum = i; |
| 307 | | |
| 308 | | CDROM_LOG(("CD: allocating block %d, size %x\n", i, sectlenin)) |
| 309 | | |
| 310 | | return &blocks[i]; |
| 311 | | } |
| 312 | | } |
| 313 | | |
| 314 | | buffull = 1; |
| 315 | | return (blockT *)NULL; |
| 316 | | } |
| 317 | | |
| 318 | | static void cd_free_block(blockT *blktofree) |
| 319 | | { |
| 320 | | INT32 i; |
| 321 | | |
| 322 | | CDROM_LOG(("cd_free_block: %x\n", (UINT32)(FPTR)blktofree)) |
| 323 | | |
| 324 | | if(blktofree == NULL) |
| 325 | | { |
| 205 | cr1 = cd_stat | 0xff; |
| 206 | cr2 = 0xffff; |
| 207 | cr3 = 0xffff; |
| 208 | cr4 = 0xffff; |
| 209 | hirqreg |= CMOK; |
| 326 | 210 | return; |
| 327 | 211 | } |
| 328 | 212 | |
| 329 | | for (i = 0; i < 200; i++) |
| 330 | | { |
| 331 | | if (&blocks[i] == blktofree) |
| 332 | | { |
| 333 | | CDROM_LOG(("CD: freeing block %d\n", i)) |
| 334 | | } |
| 335 | | } |
| 213 | if(cr1 != 0 && ((cr1 & 0xff00) != 0x5100) && 1) |
| 214 | printf("CD: command exec %04x %04x %04x %04x %04x (stat %04x)\n", hirqreg, cr1, cr2, cr3, cr4, cd_stat); |
| 336 | 215 | |
| 337 | | blktofree->size = -1; |
| 338 | | freeblocks++; |
| 339 | | buffull = 0; |
| 340 | | hirqreg &= ~BFUL; |
| 341 | | } |
| 342 | | |
| 343 | | static void cd_getsectoroffsetnum(UINT32 bufnum, UINT32 *sectoffs, UINT32 *sectnum) |
| 344 | | { |
| 345 | | if (*sectoffs == 0xffff) |
| 216 | switch (cr1 & 0xff00) |
| 346 | 217 | { |
| 347 | | // last sector |
| 348 | | printf("CD: Don't know how to handle offset ffff\n"); |
| 349 | | } |
| 350 | | else if (*sectnum == 0xffff) |
| 351 | | { |
| 352 | | *sectnum = partitions[bufnum].numblks - *sectoffs; |
| 353 | | } |
| 354 | | } |
| 355 | | |
| 356 | | static void cd_defragblocks(partitionT *part) |
| 357 | | { |
| 358 | | UINT32 i, j; |
| 359 | | blockT *temp; |
| 360 | | UINT8 temp2; |
| 361 | | |
| 362 | | for (i = 0; i < (MAX_BLOCKS-1); i++) |
| 363 | | { |
| 364 | | for (j = i+1; j < MAX_BLOCKS; j++) |
| 365 | | { |
| 366 | | if ((part->blocks[i] == (blockT *)NULL) && (part->blocks[j] != (blockT *)NULL)) |
| 367 | | { |
| 368 | | temp = part->blocks[i]; |
| 369 | | part->blocks[i] = part->blocks[j]; |
| 370 | | part->blocks[j] = temp; |
| 371 | | |
| 372 | | temp2 = part->bnum[i]; |
| 373 | | part->bnum[i] = part->bnum[j]; |
| 374 | | part->bnum[j] = temp2; |
| 375 | | } |
| 376 | | } |
| 377 | | } |
| 378 | | } |
| 379 | | |
| 380 | | static UINT16 cd_readWord(UINT32 addr) |
| 381 | | { |
| 382 | | UINT16 rv; |
| 383 | | |
| 384 | | switch (addr & 0xffff) |
| 385 | | { |
| 386 | | case 0x0008: // read HIRQ register |
| 387 | | case 0x000a: |
| 388 | | rv = hirqreg; |
| 389 | | |
| 390 | | rv &= ~DCHG; // always clear bit 6 (tray open) |
| 391 | | |
| 392 | | if (buffull) rv |= BFUL; else rv &= ~BFUL; |
| 393 | | if (sectorstore) rv |= CSCT; else rv &= ~CSCT; |
| 394 | | |
| 395 | | hirqreg = rv; |
| 396 | | |
| 397 | | // CDROM_LOG(("RW HIRQ: %04x\n", rv)) |
| 398 | | |
| 399 | | return rv; |
| 400 | | |
| 401 | | case 0x000c: |
| 402 | | case 0x000e: |
| 403 | | CDROM_LOG(("RW HIRM: %04x\n", hirqmask)) |
| 404 | | return hirqmask; |
| 405 | | |
| 406 | | case 0x0018: |
| 407 | | case 0x001a: |
| 408 | | // CDROM_LOG(("RW CR1: %04x\n", cr1)) |
| 409 | | return cr1; |
| 410 | | |
| 411 | | case 0x001c: |
| 412 | | case 0x001e: |
| 413 | | // CDROM_LOG(("RW CR2: %04x\n", cr2)) |
| 414 | | return cr2; |
| 415 | | |
| 416 | | case 0x0020: |
| 417 | | case 0x0022: |
| 418 | | // CDROM_LOG(("RW CR3: %04x\n", cr3)) |
| 419 | | return cr3; |
| 420 | | |
| 421 | | case 0x0024: |
| 422 | | case 0x0026: |
| 423 | | // CDROM_LOG(("RW CR4: %04x\n", cr4)) |
| 424 | | //popmessage("%04x %04x %04x %04x",cr1,cr2,cr3,cr4); |
| 425 | | return cr4; |
| 426 | | |
| 427 | | case 0x8000: |
| 428 | | rv = 0xffff; |
| 429 | | switch (xfertype) |
| 430 | | { |
| 431 | | case XFERTYPE_TOC: |
| 432 | | rv = tocbuf[xfercount]<<8 | tocbuf[xfercount+1]; |
| 433 | | |
| 434 | | xfercount += 2; |
| 435 | | xferdnum += 2; |
| 436 | | |
| 437 | | if (xfercount > 102*4) |
| 438 | | { |
| 439 | | xfercount = 0; |
| 440 | | xfertype = XFERTYPE_INVALID; |
| 441 | | } |
| 442 | | break; |
| 443 | | |
| 444 | | case XFERTYPE_FILEINFO_1: |
| 445 | | rv = finfbuf[xfercount]<<8 | finfbuf[xfercount+1]; |
| 446 | | xfercount += 2; |
| 447 | | xferdnum += 2; |
| 448 | | |
| 449 | | if (xfercount > 6*2) |
| 450 | | { |
| 451 | | xfercount = 0; |
| 452 | | xfertype = XFERTYPE_INVALID; |
| 453 | | } |
| 454 | | break; |
| 455 | | |
| 456 | | case XFERTYPE_FILEINFO_254: // Lunar 2 |
| 457 | | if((xfercount % (6 * 2)) == 0) |
| 458 | | { |
| 459 | | UINT32 temp = 2 + (xfercount / (0x6 * 2)); |
| 460 | | |
| 461 | | // first 4 bytes = FAD |
| 462 | | finfbuf[0] = (curdir[temp].firstfad>>24)&0xff; |
| 463 | | finfbuf[1] = (curdir[temp].firstfad>>16)&0xff; |
| 464 | | finfbuf[2] = (curdir[temp].firstfad>>8)&0xff; |
| 465 | | finfbuf[3] = (curdir[temp].firstfad&0xff); |
| 466 | | // second 4 bytes = length of file |
| 467 | | finfbuf[4] = (curdir[temp].length>>24)&0xff; |
| 468 | | finfbuf[5] = (curdir[temp].length>>16)&0xff; |
| 469 | | finfbuf[6] = (curdir[temp].length>>8)&0xff; |
| 470 | | finfbuf[7] = (curdir[temp].length&0xff); |
| 471 | | finfbuf[8] = 0x00; |
| 472 | | finfbuf[9] = 0x00; |
| 473 | | finfbuf[10] = temp; |
| 474 | | finfbuf[11] = curdir[temp].flags; |
| 475 | | } |
| 476 | | |
| 477 | | rv = finfbuf[xfercount % (6 * 2)]<<8 | finfbuf[(xfercount % (6 * 2)) +1]; |
| 478 | | |
| 479 | | xfercount += 2; |
| 480 | | xferdnum += 2; |
| 481 | | |
| 482 | | if (xfercount > (254 * 6 * 2)) |
| 483 | | { |
| 484 | | xfercount = 0; |
| 485 | | xfertype = XFERTYPE_INVALID; |
| 486 | | } |
| 487 | | break; |
| 488 | | |
| 489 | | default: |
| 490 | | CDROM_LOG(("STVCD: Unhandled xfer type %d\n", (int)xfertype)) |
| 491 | | rv = 0; |
| 492 | | break; |
| 493 | | } |
| 494 | | |
| 495 | | return rv; |
| 496 | | |
| 497 | | default: |
| 498 | | CDROM_LOG(("CD: RW %08x\n", addr)) |
| 499 | | return 0xffff; |
| 500 | | } |
| 501 | | |
| 502 | | } |
| 503 | | |
| 504 | | static UINT32 cd_readLong(UINT32 addr) |
| 505 | | { |
| 506 | | UINT32 rv = 0; |
| 507 | | |
| 508 | | switch (addr & 0xffff) |
| 509 | | { |
| 510 | | case 0x8000: |
| 511 | | switch (xfertype32) |
| 512 | | { |
| 513 | | case XFERTYPE32_GETSECTOR: |
| 514 | | case XFERTYPE32_GETDELETESECTOR: |
| 515 | | // make sure we have sectors left |
| 516 | | if (xfersect < xfersectnum) |
| 517 | | { |
| 518 | | // get next longword |
| 519 | | rv = transpart->blocks[xfersectpos+xfersect]->data[xferoffs]<<24 | |
| 520 | | transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 1]<<16 | |
| 521 | | transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 2]<<8 | |
| 522 | | transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 3]; |
| 523 | | |
| 524 | | xferdnum += 4; |
| 525 | | xferoffs += 4; |
| 526 | | |
| 527 | | // did we run out of sector? |
| 528 | | if (xferoffs >= transpart->blocks[xfersect]->size) |
| 529 | | { |
| 530 | | CDROM_LOG(("CD: finished xfer of block %d of %d\n", xfersect+1, xfersectnum)) |
| 531 | | |
| 532 | | xferoffs = 0; |
| 533 | | xfersect++; |
| 534 | | } |
| 535 | | } |
| 536 | | else // sectors are done, kill 'em all if we can |
| 537 | | { |
| 538 | | if (xfertype32 == XFERTYPE32_GETDELETESECTOR) |
| 539 | | { |
| 540 | | INT32 i; |
| 541 | | |
| 542 | | CDROM_LOG(("Killing sectors in done\n")) |
| 543 | | |
| 544 | | // deallocate the blocks |
| 545 | | for (i = xfersectpos; i < xfersectpos+xfersectnum; i++) |
| 546 | | { |
| 547 | | cd_free_block(transpart->blocks[i]); |
| 548 | | transpart->blocks[i] = (blockT *)NULL; |
| 549 | | transpart->bnum[i] = 0xff; |
| 550 | | } |
| 551 | | |
| 552 | | // defrag what's left |
| 553 | | cd_defragblocks(transpart); |
| 554 | | |
| 555 | | // clean up our state |
| 556 | | transpart->size -= xferdnum; |
| 557 | | transpart->numblks -= xfersectnum; |
| 558 | | |
| 559 | | xfertype32 = XFERTYPE32_INVALID; |
| 560 | | } |
| 561 | | } |
| 562 | | break; |
| 563 | | |
| 564 | | default: |
| 565 | | CDROM_LOG(("CD: unhandled 32-bit transfer type\n")) |
| 566 | | break; |
| 567 | | } |
| 568 | | |
| 569 | | return rv; |
| 570 | | |
| 571 | | default: |
| 572 | | CDROM_LOG(("CD: RL %08x\n", addr)) |
| 573 | | return 0xffff; |
| 574 | | } |
| 575 | | } |
| 576 | | |
| 577 | | static void cd_writeWord(running_machine &machine, UINT32 addr, UINT16 data) |
| 578 | | { |
| 579 | | UINT32 temp; |
| 580 | | |
| 581 | | switch(addr & 0xffff) |
| 582 | | { |
| 583 | | case 0x0008: |
| 584 | | case 0x000a: |
| 585 | | // CDROM_LOG(("%s:WW HIRQ: %04x & %04x => %04x\n", machine.describe_context(), hirqreg, data, hirqreg & data)) |
| 586 | | hirqreg &= data; |
| 587 | | return; |
| 588 | | case 0x000c: |
| 589 | | case 0x000e: |
| 590 | | CDROM_LOG(("WW HIRM: %04x => %04x\n", hirqmask, data)) |
| 591 | | hirqmask = data; |
| 592 | | return; |
| 593 | | case 0x0018: |
| 594 | | case 0x001a: |
| 595 | | // CDROM_LOG(("WW CR1: %04x\n", data)) |
| 596 | | cr1 = data; |
| 597 | | cd_stat &= ~CD_STAT_PERI; |
| 598 | | command |= 1; |
| 599 | | break; |
| 600 | | case 0x001c: |
| 601 | | case 0x001e: |
| 602 | | // CDROM_LOG(("WW CR2: %04x\n", data)) |
| 603 | | cr2 = data; |
| 604 | | command |= 2; |
| 605 | | break; |
| 606 | | case 0x0020: |
| 607 | | case 0x0022: |
| 608 | | // CDROM_LOG(("WW CR3: %04x\n", data)) |
| 609 | | cr3 = data; |
| 610 | | command |= 4; |
| 611 | | break; |
| 612 | | case 0x0024: |
| 613 | | case 0x0026: |
| 614 | | // CDROM_LOG(("WW CR4: %04x\n", data)) |
| 615 | | cr4 = data; |
| 616 | | |
| 617 | | command |= 8; |
| 618 | | |
| 619 | | if(command != 0xf) |
| 620 | | return; |
| 621 | | |
| 622 | | command = 0; |
| 623 | | |
| 624 | | if(cr1 != 0 && ((cr1 & 0xff00) != 0x5100) && 1) |
| 625 | | printf("CD: command exec %04x %04x %04x %04x %04x (stat %04x)\n", hirqreg, cr1, cr2, cr3, cr4, cd_stat); |
| 626 | | |
| 627 | | if (!cdrom) |
| 628 | | { |
| 629 | | cd_stat = CD_STAT_OPEN; |
| 630 | | cr1 = cd_stat | 0xff; |
| 631 | | cr2 = 0xffff; |
| 632 | | cr3 = 0xffff; |
| 633 | | cr4 = 0xffff; |
| 634 | | hirqreg |= CMOK; |
| 635 | | return; |
| 636 | | } |
| 637 | | |
| 638 | | switch (cr1 & 0xff00) |
| 639 | | { |
| 640 | 218 | case 0x0000: |
| 641 | 219 | //CDROM_LOG(("%s:CD: Get Status\n", machine.describe_context())) |
| 642 | 220 | hirqreg |= CMOK; |
| r12516 | r12517 | |
| 651 | 229 | cr2 = 0x0201; |
| 652 | 230 | cr3 = 0x0000; |
| 653 | 231 | cr4 = 0x0400; |
| 654 | | /* wants a periodic response after this? */ |
| 655 | | sector_timer->reset(); |
| 656 | | sector_timer->adjust(attotime::from_hz(CD_SPEED)); // 150 sectors / second = 300kBytes/second |
| 657 | 232 | break; |
| 658 | 233 | |
| 659 | 234 | case 0x0200: // Get TOC |
| r12516 | r12517 | |
| 715 | 290 | buffull = 0; |
| 716 | 291 | hirqreg &= 0xffe5; |
| 717 | 292 | } |
| 293 | cd_speed = ((cr1 & 0x10) >> 4) + 1; |
| 294 | |
| 718 | 295 | hirqreg |= (CMOK|ESEL); |
| 719 | 296 | cr_standard_return(cd_stat); |
| 720 | 297 | break; |
| r12516 | r12517 | |
| 726 | 303 | |
| 727 | 304 | // clear the "transfer" flag |
| 728 | 305 | cd_stat &= ~CD_STAT_TRANS; |
| 729 | | // hack for the bootloader |
| 730 | | cd_stat |= CD_STAT_PERI; |
| 731 | 306 | |
| 732 | 307 | if (xferdnum) |
| 733 | 308 | { |
| r12516 | r12517 | |
| 788 | 363 | } |
| 789 | 364 | |
| 790 | 365 | // and kick the CD if there's more to read |
| 791 | | cd_playdata(); |
| 366 | //cd_playdata(); |
| 792 | 367 | |
| 793 | 368 | xferdnum = 0; |
| 794 | 369 | hirqreg |= CMOK; |
| r12516 | r12517 | |
| 886 | 461 | { |
| 887 | 462 | cdda_pause_audio( machine.device( "cdda" ), 0 ); |
| 888 | 463 | cdda_start_audio( machine.device( "cdda" ), cd_curfad, fadstoplay ); |
| 464 | cd_speed = 1; |
| 889 | 465 | } |
| 890 | 466 | else |
| 467 | { |
| 891 | 468 | cdda_stop_audio( machine.device( "cdda" ) ); //stop any pending CD-DA |
| 469 | cd_speed = 2; //TODO: previous setting |
| 470 | } |
| 892 | 471 | |
| 893 | | //else |
| 894 | | //{ |
| 895 | | sector_timer->reset(); |
| 896 | | sector_timer->adjust(attotime::from_hz(CD_SPEED)); // 150 sectors / second = 300kBytes/second |
| 897 | | //} |
| 898 | 472 | break; |
| 899 | 473 | |
| 900 | 474 | case 0x1100: // disc seek |
| r12516 | r12517 | |
| 1585 | 1159 | |
| 1586 | 1160 | hirqreg |= (CMOK); |
| 1587 | 1161 | |
| 1588 | | // and do the disc I/O |
| 1589 | | sector_timer->reset(); |
| 1590 | | sector_timer->adjust(attotime::from_hz(CD_SPEED)); // 150 sectors / second = 300kBytes/second |
| 1591 | 1162 | break; |
| 1592 | 1163 | |
| 1593 | 1164 | case 0x7500: |
| r12516 | r12517 | |
| 1623 | 1194 | CDROM_LOG(("CD: Unknown command %04x\n", cr1)) |
| 1624 | 1195 | hirqreg |= (CMOK); |
| 1625 | 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | TIMER_DEVICE_CALLBACK( stv_sh1_sim ) |
| 1201 | { |
| 1202 | if(cmd_pending) |
| 1203 | { |
| 1204 | cd_exec_command(timer.machine()); |
| 1205 | return; |
| 1206 | } |
| 1207 | |
| 1208 | cd_stat |= CD_STAT_PERI; |
| 1209 | cr_standard_return(cd_stat); |
| 1210 | hirqreg |= SCDQ; |
| 1211 | } |
| 1212 | |
| 1213 | TIMER_DEVICE_CALLBACK( stv_sector_cb ) |
| 1214 | { |
| 1215 | //sector_timer->reset(); |
| 1216 | |
| 1217 | //popmessage("%08x %08x %d %d",cd_curfad,fadstoplay,cmd_pending,cd_speed); |
| 1218 | |
| 1219 | if (fadstoplay) |
| 1220 | cd_playdata(); |
| 1221 | |
| 1222 | sector_timer->adjust(attotime::from_hz(75*cd_speed)); // 150 sectors / second = 300kBytes/second |
| 1223 | } |
| 1224 | |
| 1225 | // global functions |
| 1226 | void stvcd_reset(running_machine &machine) |
| 1227 | { |
| 1228 | INT32 i, j; |
| 1229 | |
| 1230 | hirqmask = 0xffff; |
| 1231 | hirqreg = 0xffff; |
| 1232 | cr1 = 'C'; |
| 1233 | cr2 = ('D'<<8) | 'B'; |
| 1234 | cr3 = ('L'<<8) | 'O'; |
| 1235 | cr4 = ('C'<<8) | 'K'; |
| 1236 | cd_stat = CD_STAT_PAUSE; |
| 1237 | cur_track = 0xff; |
| 1238 | |
| 1239 | if (curdir != (direntryT *)NULL) |
| 1240 | auto_free(machine, curdir); |
| 1241 | curdir = (direntryT *)NULL; // no directory yet |
| 1242 | |
| 1243 | xfertype = XFERTYPE_INVALID; |
| 1244 | xfertype32 = XFERTYPE32_INVALID; |
| 1245 | |
| 1246 | // reset flag vars |
| 1247 | buffull = sectorstore = 0; |
| 1248 | |
| 1249 | freeblocks = 200; |
| 1250 | |
| 1251 | sectlenin = sectlenout = 2048; |
| 1252 | |
| 1253 | lastbuf = 0xff; |
| 1254 | |
| 1255 | // reset buffer partitions |
| 1256 | for (i = 0; i < MAX_FILTERS; i++) |
| 1257 | { |
| 1258 | partitions[i].size = -1; |
| 1259 | partitions[i].numblks = 0; |
| 1260 | |
| 1261 | for (j = 0; j < MAX_BLOCKS; j++) |
| 1262 | { |
| 1263 | partitions[i].blocks[j] = (blockT *)NULL; |
| 1264 | partitions[i].bnum[j] = 0xff; |
| 1626 | 1265 | } |
| 1627 | | // CDROM_LOG(("ret: %04x %04x %04x %04x %04x\n", hirqreg, cr1, cr2, cr3, cr4)) |
| 1266 | } |
| 1267 | |
| 1268 | // reset blocks |
| 1269 | for (i = 0; i < MAX_BLOCKS; i++) |
| 1270 | { |
| 1271 | blocks[i].size = -1; |
| 1272 | memset(&blocks[i].data, 0, CD_MAX_SECTOR_DATA); |
| 1273 | } |
| 1274 | |
| 1275 | // open device |
| 1276 | if (cdrom) |
| 1277 | { |
| 1278 | cdrom_close(cdrom); |
| 1279 | cdrom = (cdrom_file *)NULL; |
| 1280 | } |
| 1281 | |
| 1282 | #ifdef MESS |
| 1283 | cdrom = machine.device<cdrom_image_device>("cdrom")->get_cdrom_file(); |
| 1284 | #else |
| 1285 | cdrom = cdrom_open(get_disk_handle(machine, "cdrom")); |
| 1286 | #endif |
| 1287 | |
| 1288 | cdda_set_cdrom( machine.device("cdda"), cdrom ); |
| 1289 | |
| 1290 | if (cdrom) |
| 1291 | { |
| 1292 | CDROM_LOG(("Opened CD-ROM successfully, reading root directory\n")) |
| 1293 | read_new_dir(machine, 0xffffff); // read root directory |
| 1294 | } |
| 1295 | else |
| 1296 | { |
| 1297 | cd_stat = CD_STAT_OPEN; |
| 1298 | } |
| 1299 | |
| 1300 | cd_speed = 2; |
| 1301 | |
| 1302 | sector_timer = machine.device<timer_device>("sector_timer"); |
| 1303 | sector_timer->adjust(attotime::from_hz(150)); // 150 sectors / second = 300kBytes/second |
| 1304 | } |
| 1305 | |
| 1306 | static blockT *cd_alloc_block(UINT8 *blknum) |
| 1307 | { |
| 1308 | INT32 i; |
| 1309 | |
| 1310 | // search the 200 available blocks for a free one |
| 1311 | for (i = 0; i < 200; i++) |
| 1312 | { |
| 1313 | if (blocks[i].size == -1) |
| 1314 | { |
| 1315 | freeblocks--; |
| 1316 | if (freeblocks <= 0) |
| 1317 | { |
| 1318 | buffull = 1; |
| 1319 | } |
| 1320 | |
| 1321 | blocks[i].size = sectlenin; |
| 1322 | *blknum = i; |
| 1323 | |
| 1324 | CDROM_LOG(("CD: allocating block %d, size %x\n", i, sectlenin)) |
| 1325 | |
| 1326 | return &blocks[i]; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | buffull = 1; |
| 1331 | return (blockT *)NULL; |
| 1332 | } |
| 1333 | |
| 1334 | static void cd_free_block(blockT *blktofree) |
| 1335 | { |
| 1336 | INT32 i; |
| 1337 | |
| 1338 | CDROM_LOG(("cd_free_block: %x\n", (UINT32)(FPTR)blktofree)) |
| 1339 | |
| 1340 | if(blktofree == NULL) |
| 1341 | { |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | for (i = 0; i < 200; i++) |
| 1346 | { |
| 1347 | if (&blocks[i] == blktofree) |
| 1348 | { |
| 1349 | CDROM_LOG(("CD: freeing block %d\n", i)) |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | blktofree->size = -1; |
| 1354 | freeblocks++; |
| 1355 | buffull = 0; |
| 1356 | hirqreg &= ~BFUL; |
| 1357 | } |
| 1358 | |
| 1359 | static void cd_getsectoroffsetnum(UINT32 bufnum, UINT32 *sectoffs, UINT32 *sectnum) |
| 1360 | { |
| 1361 | if (*sectoffs == 0xffff) |
| 1362 | { |
| 1363 | // last sector |
| 1364 | printf("CD: Don't know how to handle offset ffff\n"); |
| 1365 | } |
| 1366 | else if (*sectnum == 0xffff) |
| 1367 | { |
| 1368 | *sectnum = partitions[bufnum].numblks - *sectoffs; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | static void cd_defragblocks(partitionT *part) |
| 1373 | { |
| 1374 | UINT32 i, j; |
| 1375 | blockT *temp; |
| 1376 | UINT8 temp2; |
| 1377 | |
| 1378 | for (i = 0; i < (MAX_BLOCKS-1); i++) |
| 1379 | { |
| 1380 | for (j = i+1; j < MAX_BLOCKS; j++) |
| 1381 | { |
| 1382 | if ((part->blocks[i] == (blockT *)NULL) && (part->blocks[j] != (blockT *)NULL)) |
| 1383 | { |
| 1384 | temp = part->blocks[i]; |
| 1385 | part->blocks[i] = part->blocks[j]; |
| 1386 | part->blocks[j] = temp; |
| 1387 | |
| 1388 | temp2 = part->bnum[i]; |
| 1389 | part->bnum[i] = part->bnum[j]; |
| 1390 | part->bnum[j] = temp2; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | static UINT16 cd_readWord(UINT32 addr) |
| 1397 | { |
| 1398 | UINT16 rv; |
| 1399 | |
| 1400 | switch (addr & 0xffff) |
| 1401 | { |
| 1402 | case 0x0008: // read HIRQ register |
| 1403 | case 0x000a: |
| 1404 | rv = hirqreg; |
| 1405 | |
| 1406 | rv &= ~DCHG; // always clear bit 6 (tray open) |
| 1407 | |
| 1408 | if (buffull) rv |= BFUL; else rv &= ~BFUL; |
| 1409 | if (sectorstore) rv |= CSCT; else rv &= ~CSCT; |
| 1410 | |
| 1411 | hirqreg = rv; |
| 1412 | |
| 1413 | // CDROM_LOG(("RW HIRQ: %04x\n", rv)) |
| 1414 | |
| 1415 | return rv; |
| 1416 | |
| 1417 | case 0x000c: |
| 1418 | case 0x000e: |
| 1419 | CDROM_LOG(("RW HIRM: %04x\n", hirqmask)) |
| 1420 | return hirqmask; |
| 1421 | |
| 1422 | case 0x0018: |
| 1423 | case 0x001a: |
| 1424 | // CDROM_LOG(("RW CR1: %04x\n", cr1)) |
| 1425 | return cr1; |
| 1426 | |
| 1427 | case 0x001c: |
| 1428 | case 0x001e: |
| 1429 | // CDROM_LOG(("RW CR2: %04x\n", cr2)) |
| 1430 | return cr2; |
| 1431 | |
| 1432 | case 0x0020: |
| 1433 | case 0x0022: |
| 1434 | // CDROM_LOG(("RW CR3: %04x\n", cr3)) |
| 1435 | return cr3; |
| 1436 | |
| 1437 | case 0x0024: |
| 1438 | case 0x0026: |
| 1439 | // CDROM_LOG(("RW CR4: %04x\n", cr4)) |
| 1440 | //popmessage("%04x %04x %04x %04x",cr1,cr2,cr3,cr4); |
| 1441 | cmd_pending = 0; |
| 1442 | return cr4; |
| 1443 | |
| 1444 | case 0x8000: |
| 1445 | rv = 0xffff; |
| 1446 | switch (xfertype) |
| 1447 | { |
| 1448 | case XFERTYPE_TOC: |
| 1449 | rv = tocbuf[xfercount]<<8 | tocbuf[xfercount+1]; |
| 1450 | |
| 1451 | xfercount += 2; |
| 1452 | xferdnum += 2; |
| 1453 | |
| 1454 | if (xfercount > 102*4) |
| 1455 | { |
| 1456 | xfercount = 0; |
| 1457 | xfertype = XFERTYPE_INVALID; |
| 1458 | } |
| 1459 | break; |
| 1460 | |
| 1461 | case XFERTYPE_FILEINFO_1: |
| 1462 | rv = finfbuf[xfercount]<<8 | finfbuf[xfercount+1]; |
| 1463 | xfercount += 2; |
| 1464 | xferdnum += 2; |
| 1465 | |
| 1466 | if (xfercount > 6*2) |
| 1467 | { |
| 1468 | xfercount = 0; |
| 1469 | xfertype = XFERTYPE_INVALID; |
| 1470 | } |
| 1471 | break; |
| 1472 | |
| 1473 | case XFERTYPE_FILEINFO_254: // Lunar 2 |
| 1474 | if((xfercount % (6 * 2)) == 0) |
| 1475 | { |
| 1476 | UINT32 temp = 2 + (xfercount / (0x6 * 2)); |
| 1477 | |
| 1478 | // first 4 bytes = FAD |
| 1479 | finfbuf[0] = (curdir[temp].firstfad>>24)&0xff; |
| 1480 | finfbuf[1] = (curdir[temp].firstfad>>16)&0xff; |
| 1481 | finfbuf[2] = (curdir[temp].firstfad>>8)&0xff; |
| 1482 | finfbuf[3] = (curdir[temp].firstfad&0xff); |
| 1483 | // second 4 bytes = length of file |
| 1484 | finfbuf[4] = (curdir[temp].length>>24)&0xff; |
| 1485 | finfbuf[5] = (curdir[temp].length>>16)&0xff; |
| 1486 | finfbuf[6] = (curdir[temp].length>>8)&0xff; |
| 1487 | finfbuf[7] = (curdir[temp].length&0xff); |
| 1488 | finfbuf[8] = 0x00; |
| 1489 | finfbuf[9] = 0x00; |
| 1490 | finfbuf[10] = temp; |
| 1491 | finfbuf[11] = curdir[temp].flags; |
| 1492 | } |
| 1493 | |
| 1494 | rv = finfbuf[xfercount % (6 * 2)]<<8 | finfbuf[(xfercount % (6 * 2)) +1]; |
| 1495 | |
| 1496 | xfercount += 2; |
| 1497 | xferdnum += 2; |
| 1498 | |
| 1499 | if (xfercount > (254 * 6 * 2)) |
| 1500 | { |
| 1501 | xfercount = 0; |
| 1502 | xfertype = XFERTYPE_INVALID; |
| 1503 | } |
| 1504 | break; |
| 1505 | |
| 1506 | default: |
| 1507 | CDROM_LOG(("STVCD: Unhandled xfer type %d\n", (int)xfertype)) |
| 1508 | rv = 0; |
| 1509 | break; |
| 1510 | } |
| 1511 | |
| 1512 | return rv; |
| 1513 | |
| 1514 | default: |
| 1515 | CDROM_LOG(("CD: RW %08x\n", addr)) |
| 1516 | return 0xffff; |
| 1517 | } |
| 1518 | |
| 1519 | } |
| 1520 | |
| 1521 | static UINT32 cd_readLong(UINT32 addr) |
| 1522 | { |
| 1523 | UINT32 rv = 0; |
| 1524 | |
| 1525 | switch (addr & 0xffff) |
| 1526 | { |
| 1527 | case 0x8000: |
| 1528 | switch (xfertype32) |
| 1529 | { |
| 1530 | case XFERTYPE32_GETSECTOR: |
| 1531 | case XFERTYPE32_GETDELETESECTOR: |
| 1532 | // make sure we have sectors left |
| 1533 | if (xfersect < xfersectnum) |
| 1534 | { |
| 1535 | // get next longword |
| 1536 | rv = transpart->blocks[xfersectpos+xfersect]->data[xferoffs]<<24 | |
| 1537 | transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 1]<<16 | |
| 1538 | transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 2]<<8 | |
| 1539 | transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 3]; |
| 1540 | |
| 1541 | xferdnum += 4; |
| 1542 | xferoffs += 4; |
| 1543 | |
| 1544 | // did we run out of sector? |
| 1545 | if (xferoffs >= transpart->blocks[xfersect]->size) |
| 1546 | { |
| 1547 | CDROM_LOG(("CD: finished xfer of block %d of %d\n", xfersect+1, xfersectnum)) |
| 1548 | |
| 1549 | xferoffs = 0; |
| 1550 | xfersect++; |
| 1551 | } |
| 1552 | } |
| 1553 | else // sectors are done, kill 'em all if we can |
| 1554 | { |
| 1555 | if (xfertype32 == XFERTYPE32_GETDELETESECTOR) |
| 1556 | { |
| 1557 | INT32 i; |
| 1558 | |
| 1559 | CDROM_LOG(("Killing sectors in done\n")) |
| 1560 | |
| 1561 | // deallocate the blocks |
| 1562 | for (i = xfersectpos; i < xfersectpos+xfersectnum; i++) |
| 1563 | { |
| 1564 | cd_free_block(transpart->blocks[i]); |
| 1565 | transpart->blocks[i] = (blockT *)NULL; |
| 1566 | transpart->bnum[i] = 0xff; |
| 1567 | } |
| 1568 | |
| 1569 | // defrag what's left |
| 1570 | cd_defragblocks(transpart); |
| 1571 | |
| 1572 | // clean up our state |
| 1573 | transpart->size -= xferdnum; |
| 1574 | transpart->numblks -= xfersectnum; |
| 1575 | |
| 1576 | xfertype32 = XFERTYPE32_INVALID; |
| 1577 | } |
| 1578 | } |
| 1579 | break; |
| 1580 | |
| 1581 | default: |
| 1582 | CDROM_LOG(("CD: unhandled 32-bit transfer type\n")) |
| 1583 | break; |
| 1584 | } |
| 1585 | |
| 1586 | return rv; |
| 1587 | |
| 1588 | default: |
| 1589 | CDROM_LOG(("CD: RL %08x\n", addr)) |
| 1590 | return 0xffff; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | static void cd_writeWord(running_machine &machine, UINT32 addr, UINT16 data) |
| 1595 | { |
| 1596 | switch(addr & 0xffff) |
| 1597 | { |
| 1598 | case 0x0008: |
| 1599 | case 0x000a: |
| 1600 | // CDROM_LOG(("%s:WW HIRQ: %04x & %04x => %04x\n", machine.describe_context(), hirqreg, data, hirqreg & data)) |
| 1601 | hirqreg &= data; |
| 1602 | return; |
| 1603 | case 0x000c: |
| 1604 | case 0x000e: |
| 1605 | CDROM_LOG(("WW HIRM: %04x => %04x\n", hirqmask, data)) |
| 1606 | hirqmask = data; |
| 1607 | return; |
| 1608 | case 0x0018: |
| 1609 | case 0x001a: |
| 1610 | // CDROM_LOG(("WW CR1: %04x\n", data)) |
| 1611 | cr1 = data; |
| 1612 | cd_stat &= ~CD_STAT_PERI; |
| 1613 | cmd_pending = 1; |
| 1628 | 1614 | break; |
| 1615 | case 0x001c: |
| 1616 | case 0x001e: |
| 1617 | // CDROM_LOG(("WW CR2: %04x\n", data)) |
| 1618 | cr2 = data; |
| 1619 | break; |
| 1620 | case 0x0020: |
| 1621 | case 0x0022: |
| 1622 | // CDROM_LOG(("WW CR3: %04x\n", data)) |
| 1623 | cr3 = data; |
| 1624 | break; |
| 1625 | case 0x0024: |
| 1626 | case 0x0026: |
| 1627 | // CDROM_LOG(("WW CR4: %04x\n", data)) |
| 1628 | cr4 = data; |
| 1629 | break; |
| 1629 | 1630 | default: |
| 1630 | 1631 | CDROM_LOG(("CD: WW %08x %04x\n", addr, data)) |
| 1631 | 1632 | break; |
| r12516 | r12517 | |
| 2116 | 2117 | } |
| 2117 | 2118 | |
| 2118 | 2119 | // read a single sector off the CD, applying the current filter(s) as necessary |
| 2119 | | static partitionT *cd_read_filtered_sector(INT32 fad) |
| 2120 | static partitionT *cd_read_filtered_sector(INT32 fad, UINT8 *p_ok) |
| 2120 | 2121 | { |
| 2121 | 2122 | int trktype; |
| 2122 | 2123 | |
| 2123 | 2124 | if ((cddevice != NULL) && (!buffull)) |
| 2124 | 2125 | { |
| 2126 | *p_ok = 1; |
| 2125 | 2127 | // find out the track's type |
| 2126 | 2128 | trktype = cdrom_get_track_type(cdrom, cdrom_get_track(cdrom, fad-150)); |
| 2127 | 2129 | |
| r12516 | r12517 | |
| 2138 | 2140 | { |
| 2139 | 2141 | cdrom_read_data(cdrom, fad-150, curblock.data, CD_TRACK_AUDIO); |
| 2140 | 2142 | } |
| 2141 | | cr3 = 0x100 | (fad>>16); // update cr3/4 with the current fad |
| 2142 | | cr4 = fad; |
| 2143 | |
| 2143 | 2144 | curblock.size = sectlenin; |
| 2144 | 2145 | curblock.FAD = fad; |
| 2145 | 2146 | |
| r12516 | r12517 | |
| 2161 | 2162 | return cd_filterdata(cddevice, trktype); |
| 2162 | 2163 | } |
| 2163 | 2164 | |
| 2165 | *p_ok = 0; |
| 2164 | 2166 | return (partitionT *)NULL; |
| 2165 | 2167 | } |
| 2166 | 2168 | |
| r12516 | r12517 | |
| 2175 | 2177 | |
| 2176 | 2178 | if (cdrom) |
| 2177 | 2179 | { |
| 2180 | UINT8 p_ok; |
| 2181 | |
| 2178 | 2182 | if(cdrom_get_track_type(cdrom, cdrom_get_track(cdrom, cd_curfad)) != CD_TRACK_AUDIO) |
| 2179 | | cd_read_filtered_sector(cd_curfad); |
| 2183 | cd_read_filtered_sector(cd_curfad,&p_ok); |
| 2184 | else |
| 2185 | p_ok = 1; // TODO |
| 2180 | 2186 | |
| 2181 | | /* TODO: condition is wrong (should be "if partition isn't null") */ |
| 2182 | | if(!buffull) |
| 2187 | if(p_ok) |
| 2183 | 2188 | { |
| 2184 | 2189 | cd_curfad++; |
| 2185 | 2190 | fadstoplay--; |
| 2186 | | } |
| 2187 | | //else |
| 2188 | | // hirqreg |= SCDQ; |
| 2191 | hirqreg |= CSCT; |
| 2192 | sectorstore = 1; |
| 2189 | 2193 | |
| 2190 | | hirqreg |= CSCT; |
| 2194 | if (!fadstoplay) |
| 2195 | { |
| 2196 | CDROM_LOG(("cd_playdata: playback ended\n")) |
| 2197 | cd_stat = CD_STAT_PAUSE; |
| 2191 | 2198 | |
| 2192 | | if (!fadstoplay) |
| 2193 | | { |
| 2194 | | CDROM_LOG(("cd_playdata: playback ended\n")) |
| 2195 | | cd_stat = CD_STAT_PAUSE; |
| 2199 | hirqreg |= PEND; |
| 2196 | 2200 | |
| 2197 | | hirqreg |= PEND; |
| 2198 | | |
| 2199 | | if (playtype == 1) |
| 2200 | | { |
| 2201 | | CDROM_LOG(("cd_playdata: setting EFLS\n")) |
| 2202 | | hirqreg |= EFLS; |
| 2201 | if (playtype == 1) |
| 2202 | { |
| 2203 | CDROM_LOG(("cd_playdata: setting EFLS\n")) |
| 2204 | hirqreg |= EFLS; |
| 2205 | } |
| 2203 | 2206 | } |
| 2204 | 2207 | } |
| 2208 | |
| 2205 | 2209 | } |
| 2206 | 2210 | } |
| 2207 | 2211 | } |