comparison blastem.c @ 404:88fa2ad53e64

Minor refactor of io_read functions to avoid duplication of logic between byte and word versions
author Mike Pavone <pavone@retrodev.com>
date Sun, 16 Jun 2013 11:59:45 -0700
parents de2c085ce174
children 042c4ba4a838
comparison
equal deleted inserted replaced
403:f0a3f86595ae 404:88fa2ad53e64
601 return context; 601 return context;
602 } 602 }
603 603
604 m68k_context * io_write_w(uint32_t location, m68k_context * context, uint16_t value) 604 m68k_context * io_write_w(uint32_t location, m68k_context * context, uint16_t value)
605 { 605 {
606 genesis_context * gen = context->system; 606 if (location < 0x10000 || (location & 0x1FFF) >= 0x100) {
607 if (location < 0x10000) { 607 return io_write(location, context, value >> 8);
608 if (busack_cycle <= context->current_cycle) {
609 busack = new_busack;
610 busack_cycle = CYCLE_NEVER;
611 }
612 if (!(busack || reset)) {
613 location &= 0x7FFF;
614 if (location < 0x4000) {
615 z80_ram[location & 0x1FFE] = value >> 8;
616 z80_handle_code_write(location & 0x1FFE, gen->z80);
617 } else if (location < 0x6000) {
618 sync_sound(gen, context->current_cycle * MCLKS_PER_68K);
619 if (location & 1) {
620 ym_data_write(gen->ym, value >> 8);
621 } else if(location & 2) {
622 ym_address_write_part2(gen->ym, value >> 8);
623 } else {
624 ym_address_write_part1(gen->ym, value >> 8);
625 }
626 }
627 }
628 } else { 608 } else {
629 location &= 0x1FFF; 609 return io_write(location, context, value);
630 if (location < 0x100) { 610 }
631 switch(location/2)
632 {
633 case 0x1:
634 io_data_write(&gamepad_1, context, value);
635 break;
636 case 0x2:
637 io_data_write(&gamepad_2, context, value);
638 break;
639 case 0x3://PORT C Data
640 break;
641 case 0x4:
642 gamepad_1.control = value;
643 break;
644 case 0x5:
645 gamepad_2.control = value;
646 break;
647 }
648 } else {
649 //printf("IO Write of %X to %X @ %d\n", value, location, context->current_cycle);
650 if (location == 0x1100) {
651 sync_z80(gen->z80, context->current_cycle * MCLKS_PER_68K);
652 if (busack_cycle <= context->current_cycle) {
653 busack = new_busack;
654 busack_cycle = CYCLE_NEVER;
655 }
656 if (value & 0x100) {
657 dprintf("bus requesting Z80 @ %d\n", (context->current_cycle * MCLKS_PER_68K) / MCLKS_PER_Z80);
658
659 if(!reset && !busreq) {
660 busack_cycle = ((gen->z80->current_cycle + Z80_ACK_DELAY) * MCLKS_PER_Z80) / MCLKS_PER_68K;//context->current_cycle + Z80_ACK_DELAY;
661 new_busack = Z80_REQ_ACK;
662 }
663 busreq = 1;
664 } else {
665 if (busreq) {
666 dprintf("releasing Z80 bus @ %d\n", (context->current_cycle * MCLKS_PER_68K) / MCLKS_PER_Z80);
667 #ifdef DO_DEBUG_PRINT
668 char fname[20];
669 sprintf(fname, "zram-%d", zram_counter++);
670 FILE * f = fopen(fname, "wb");
671 fwrite(z80_ram, 1, sizeof(z80_ram), f);
672 fclose(f);
673 #endif
674 busack_cycle = ((gen->z80->current_cycle + Z80_BUSY_DELAY) * MCLKS_PER_Z80) / MCLKS_PER_68K;
675 new_busack = Z80_REQ_BUSY;
676 busreq = 0;
677 }
678 //busack_cycle = CYCLE_NEVER;
679 //busack = Z80_REQ_BUSY;
680 }
681 } else if (location == 0x1200) {
682 sync_z80(gen->z80, context->current_cycle * MCLKS_PER_68K);
683 if (value & 0x100) {
684 if (reset && busreq) {
685 new_busack = 0;
686 busack_cycle = ((gen->z80->current_cycle + Z80_ACK_DELAY) * MCLKS_PER_Z80) / MCLKS_PER_68K;//context->current_cycle + Z80_ACK_DELAY;
687 }
688 //TODO: Deal with the scenario in which reset is not asserted long enough
689 if (reset) {
690 need_reset = 1;
691 //TODO: Add necessary delay between release of reset and start of execution
692 gen->z80->current_cycle = (context->current_cycle * MCLKS_PER_68K) / MCLKS_PER_Z80;
693 }
694 reset = 0;
695 } else {
696 reset = 1;
697 }
698 }
699 }
700 }
701 return context;
702 } 611 }
703 612
704 #define USA 0x80 613 #define USA 0x80
705 #define JAP 0x00 614 #define JAP 0x00
706 #define EUR 0xC0 615 #define EUR 0xC0
774 return value; 683 return value;
775 } 684 }
776 685
777 uint16_t io_read_w(uint32_t location, m68k_context * context) 686 uint16_t io_read_w(uint32_t location, m68k_context * context)
778 { 687 {
779 uint16_t value; 688 uint16_t value = io_read(location, context);
780 genesis_context * gen = context->system; 689 if (location < 0x10000 || (location & 0x1FFF) < 0x100) {
781 if (location < 0x10000) { 690 value = value | (value << 8);
782 if (busack_cycle <= context->current_cycle) {
783 busack = new_busack;
784 busack_cycle = CYCLE_NEVER;
785 }
786 if (!(busack==Z80_REQ_BUSY || reset)) {
787 location &= 0x7FFF;
788 if (location < 0x4000) {
789 value = z80_ram[location & 0x1FFE];
790 } else if (location < 0x6000) {
791 sync_sound(gen, context->current_cycle * MCLKS_PER_68K);
792 value = ym_read_status(gen->ym);
793 } else {
794 value = 0xFF;
795 }
796 value = value | (value << 8);
797 } else {
798 value = 0xFFFF;
799 }
800 } else { 691 } else {
801 location &= 0x1FFF; 692 value <<= 8;
802 if (location < 0x100) {
803 switch(location/2)
804 {
805 case 0x0:
806 //version bits should be 0 for now since we're not emulating TMSS
807 //Not sure about the other bits
808 value = version_reg;
809 break;
810 case 0x1:
811 value = io_data_read(&gamepad_1, context);
812 break;
813 case 0x2:
814 value = io_data_read(&gamepad_2, context);
815 break;
816 case 0x3://PORT C Data
817 break;
818 case 0x4:
819 value = gamepad_1.control;
820 break;
821 case 0x5:
822 value = gamepad_2.control;
823 break;
824 case 0x6:
825 //PORT C Control
826 value = 0;
827 break;
828 default:
829 value = 0;
830 }
831 value = value | (value << 8);
832 //printf("Word read to %X returned %d\n", location, value);
833 } else {
834 if (location == 0x1100) {
835 if (busack_cycle <= context->current_cycle) {
836 busack = new_busack;
837 busack_cycle = CYCLE_NEVER;
838 }
839 value = (Z80_RES_BUSACK || busack) << 8;
840 //printf("Word read of BUSREQ returned %d\n", value);
841 } else if (location == 0x1200) {
842 value = (!reset) << 8;
843 } else {
844 printf("Word read of unknown IO location: %X\n", location);
845 }
846 }
847 } 693 }
848 return value; 694 return value;
849 } 695 }
850 696
851 z80_context * z80_write_ym(uint16_t location, z80_context * context, uint8_t value) 697 z80_context * z80_write_ym(uint16_t location, z80_context * context, uint8_t value)