|
|
| version 1.4, 2004/08/07 07:19:56 | version 1.14, 2004/08/15 15:18:00 |
|---|---|
| Line 2 | Line 2 |
| #include "z80core.h" | #include "z80core.h" |
| #include "pccore.h" | #include "pccore.h" |
| #include "iocore.h" | #include "iocore.h" |
| #include "nevent.h" | |
| #include "ievent.h" | |
| void x1_ctc_int(void) { | static SINT32 minclock(const CTCCH *ch) { |
| CTCCH *ch; | UINT32 event; |
| UINT r; | |
| SINT32 clk0; | |
| SINT32 clk4; | |
| SINT32 clk2; | |
| SINT32 subcnt; | |
| UINT i; | UINT i; |
| REG8 bit; | UINT32 clock; |
| REG8 ctcint_flg; | |
| event = 0x01000000; | |
| for (i=0; i<3; i++) { | |
| if ((ch->cmd[i] & 0x82) == 0x80) { | |
| clock = ch->count[i]; | |
| event = min(event, clock); | |
| } | |
| } | |
| if ((ch->cmd[3] & 0x82) == 0x80) { | |
| clock = ch->count[3]; | |
| if (ch->cmd[3] & 0x40) { | |
| clock = (clock - 1) * ch->countmax[0]; | |
| clock += ch->count[0]; | |
| } | |
| event = min(event, clock); | |
| } | |
| if (event == 0) { | |
| event = 1; | |
| } | |
| return(event); | |
| } | |
| ctcint_flg = Z80_ABLEINTERRUPT(); | static REG8 ctcwork(CTCCH *ch) { |
| clk4 = pccore.HSYNC_CLK; | UINT32 baseclock; |
| clk2 = clk4 >> 1; | SINT32 stepclock; |
| REG8 intr; | |
| SINT32 pulse3; | |
| SINT32 pulse; | |
| SINT32 count; | |
| if (pccore.ROM_TYPE >= 2) { | baseclock = CPU_CLOCK + CPU_BASECLOCK - CPU_REMCLOCK; |
| ch = ctc.ch + 0; | stepclock = baseclock - ch->baseclock; |
| r = 3; | stepclock /= pccore.multiple; |
| ch->baseclock += stepclock * pccore.multiple; | |
| intr = 0; | |
| pulse3 = 0; | |
| // 0 | |
| if (!(ch->cmd[0] & 0x02)) { | |
| pulse = stepclock; | |
| count = ch->count[0]; | |
| count -= pulse; | |
| if (count <= 0) { | |
| pulse3 = (0 - count) / ch->countmax[0]; | |
| pulse3 += 1; | |
| count += pulse3 * ch->countmax[0]; | |
| intr |= (ch->cmd[0] & 0x80) >> (7 - 0); | |
| } | |
| ch->count[0] = count; | |
| } | } |
| else { | |
| ch = ctc.ch + 2; | // 3 |
| r = 1; | if (!(ch->cmd[3] & 0x02)) { |
| if (!(ch->cmd[3] & 0x40)) { | |
| pulse3 = stepclock; | |
| } | |
| count = ch->count[3]; | |
| count -= pulse3; | |
| if (count <= 0) { | |
| count = ch->countmax[3] - ((0 - count) % ch->countmax[3]); | |
| intr |= (ch->cmd[3] & 0x80) >> (7 - 3); | |
| } | |
| ch->count[3] = count; | |
| } | } |
| do { | // 1 |
| clk0 = 0; | if (!(ch->cmd[1] & 0x02)) { |
| bit = 1; | pulse = stepclock; |
| for (i=0; i<4; i++) { | count = ch->count[1]; |
| if (!(ch->cmd[i] & 0x02)) { | count -= pulse; |
| if (ch->count[i] <= 0) { | if (count <= 0) { |
| ch->count[i] += ch->countmax[i]; | count = ch->countmax[1] - ((0 - count) % ch->countmax[1]); |
| } | intr |= (ch->cmd[1] & 0x80) >> (7 - 1); |
| if (!(ch->cmd[i] & 0x40)) { | |
| subcnt = clk4; | |
| } | |
| else if (i == 3) { | |
| subcnt = clk0; | |
| } | |
| else { | |
| subcnt = clk2; | |
| } | |
| ch->count[i] -= subcnt; | |
| if (ch->count[i] <= 0) { | |
| ch->int_flag |= bit; | |
| if (!i) { | |
| clk0 = 1; | |
| } | |
| } | |
| } | |
| bit <<= 1; | |
| } | } |
| ch->count[1] = count; | |
| } | |
| // 2 | |
| if (!(ch->cmd[2] & 0x02)) { | |
| pulse = stepclock; | |
| count = ch->count[2]; | |
| count -= pulse; | |
| if (count <= 0) { | |
| count = ch->countmax[2] - ((0 - count) % ch->countmax[2]); | |
| intr |= (ch->cmd[2] & 0x80) >> (7 - 2); | |
| } | |
| ch->count[2] = count; | |
| } | |
| return(intr); | |
| } | |
| static void ctcstep(CTCCH *ch) { | |
| REG8 intr; | |
| intr = ctcwork(ch); | |
| if (intr) { | |
| ch->intr |= intr; | |
| ievent_set(IEVENT_CTC0 + ch->num); | |
| } | |
| } | |
| static void ctcnextevent(CTCCH *ch) { | |
| UINT32 event; | |
| bit = 1; | if (ch->intr) { |
| for (i=0; i<4; i++) { | return; |
| if (ch->int_flag & bit) { | } |
| event = minclock(ch) * pccore.multiple; | |
| nevent_set(NEVENT_CTC0 + ch->num, event, neitem_ctc, NEVENT_ABSOLUTE); | |
| } | |
| void neitem_ctc(UINT id) { | |
| CTCCH *ch; | |
| REG8 intr; | |
| ch = ctc.ch + (id - NEVENT_CTC0); | |
| intr = ctcwork(ch); | |
| if (intr) { | |
| ch->intr |= intr; | |
| ievent_set(IEVENT_CTC0 + ch->num); | |
| } | |
| else { | |
| ctcnextevent(ch); | |
| } | |
| } | |
| BRESULT ieitem_ctc(UINT id) { | |
| CTCCH *ch; | |
| REG8 intr; | |
| BRESULT r; | |
| UINT i; | |
| REG8 bit; | |
| ch = ctc.ch + (id - IEVENT_CTC0); | |
| intr = ctcwork(ch); | |
| intr |= ch->intr; | |
| r = FALSE; | |
| if (intr) { | |
| for (i=0, bit=1; i<4; i++, bit<<=1) | |
| // for (i=4, bit=8; i--; bit>>=1) | |
| { | |
| if (intr & bit) { | |
| if (!(ch->cmd[i] & 0x80)) { | if (!(ch->cmd[i] & 0x80)) { |
| ch->int_flag ^= bit; | intr ^= bit; |
| } | } |
| else if (ctcint_flg) { | #if 0 // アークスのタイミング→あとで修正 |
| ctcint_flg = 0; | else if (0) |
| ch->int_flag ^= bit; | #elif 1 |
| // TRACEOUT(("ctc%u int -- %d", 3 - r, i)); | else if ((ch->countmax[i] - ch->count[i]) >= ch->range[i]) |
| Z80_INT((REG8)(ch->vector + (i << 1))); | #elif 0 |
| else if (((ch->cmd[i] & 0x10) == 0) && | |
| ((ch->countmax[i] - ch->count[i]) >= (256 >> 1))) | |
| #else | |
| else if (ch->count[i] != ch->countmax[i]) | |
| #endif | |
| { | |
| intr ^= bit; | |
| } | |
| else if (!r) { | |
| r = TRUE; | |
| intr ^= bit; | |
| TRACEOUT(("ctc int %d %d [%.2x]", ch->num, i, ch->cmd[i])); | |
| Z80_INTERRUPT((REG8)(ch->vector + (i << 1))); | |
| } | } |
| } | } |
| bit <<= 1; | |
| } | } |
| ch++; | } |
| } while(--r); | ch->intr = intr; |
| if (intr) { | |
| ievent_set(IEVENT_CTC0 + ch->num); | |
| } | |
| else { | |
| ctcnextevent(ch); | |
| } | |
| return(r); | |
| } | } |
| Line 82 void x1_ctc_int(void) { | Line 199 void x1_ctc_int(void) { |
| static void ctcch_o(CTCCH *ch, UINT port, REG8 value) { | static void ctcch_o(CTCCH *ch, UINT port, REG8 value) { |
| REG8 scale; | |
| SINT32 count; | SINT32 count; |
| REG8 scale; | |
| port &= 3; | port &= 3; |
| if (ch->cmd[port] & 0x04) { | if (ch->cmd[port] & 0x04) { |
| scale = 0; | ctcstep(ch); |
| count = 256; | |
| ch->basecnt[port] = value; | ch->basecnt[port] = value; |
| if (value) { | count = ((value - 1) & 0xff) + 1; |
| count = (SINT32)value; | scale = 0; |
| } | |
| if (!(ch->cmd[port] & 0x40)) { | if (!(ch->cmd[port] & 0x40)) { |
| if (ch->cmd[port] & 0x20) { | if (ch->cmd[port] & 0x20) { |
| scale = 8; | scale = 8 - 1; |
| } | } |
| else { | else { |
| scale = 4; | scale = 4 - 1; |
| } | } |
| } | } |
| count <<= scale; | ch->scale[port] = scale; |
| ch->countmax[port] = count; | ch->countmax[port] = count << scale; |
| ch->count[port] = count; | ch->count[port] = count << scale; |
| // ch->range[port] = ((count + 3) >> 2) << scale; | |
| ch->range[port] = 4 << scale; | |
| ch->cmd[port] &= ~6; | ch->cmd[port] &= ~6; |
| ctcnextevent(ch); | |
| } | } |
| else if (value & 1) { | else if (value & 1) { |
| ctcstep(ch); | |
| ch->cmd[port] = value; | ch->cmd[port] = value; |
| ctcnextevent(ch); | |
| } | } |
| else if (!port) { // ver0.25 | else if (!port) { // ver0.25 |
| ch->vector = (UINT8)(value & 0xf8); | ch->vector = (UINT8)(value & 0xf8); |
| Line 116 static void ctcch_o(CTCCH *ch, UINT port | Line 236 static void ctcch_o(CTCCH *ch, UINT port |
| static REG8 ctcch_i(CTCCH *ch, UINT port) { | static REG8 ctcch_i(CTCCH *ch, UINT port) { |
| REG8 scale; | |
| port &= 3; | port &= 3; |
| if (port != 3) { | if (port != 3) { |
| return(ch->basecnt[port]); | return(ch->basecnt[port]); |
| } | } |
| else { | else { |
| scale = 0; | ctcstep(ch); |
| if (!(ch->cmd[3] & 0x40)) { | return((REG8)(ch->count[3] >> ch->scale[3])); |
| if (ch->cmd[3] & 0x20) { | |
| scale = 8; | |
| } | |
| else { | |
| scale = 4; | |
| } | |
| } | |
| return((REG8)(ch->count[3] >> scale)); | |
| } | } |
| } | } |
| Line 148 static CTCCH *getctcch(UINT port) { | Line 258 static CTCCH *getctcch(UINT port) { |
| if (port == 0x1fa8) { | if (port == 0x1fa8) { |
| return(ctc.ch + 1); | return(ctc.ch + 1); |
| } | } |
| if ((pccore.SOUND_SW) && (port == 0x0704)) { | if (port == 0x0704) { |
| return(ctc.ch + 2); | return(ctc.ch + 2); |
| } | } |
| return(NULL); | return(NULL); |
| Line 158 void IOOUTCALL ctc_o(UINT port, REG8 val | Line 268 void IOOUTCALL ctc_o(UINT port, REG8 val |
| CTCCH *ch; | CTCCH *ch; |
| // TRACEOUT(("ctc - %.4x %.2x [%.4x]", port, value, Z80_PC)); | TRACEOUT(("ctc - %.4x %.2x [%.4x]", port, value, Z80_PC)); |
| ch = getctcch(port); | ch = getctcch(port); |
| if (ch != NULL) { | if (ch != NULL) { |
| ctcch_o(ch, port, value); | ctcch_o(ch, port, value); |
| Line 174 REG8 IOINPCALL ctc_i(UINT port) { | Line 284 REG8 IOINPCALL ctc_i(UINT port) { |
| return(ctcch_i(ch, port)); | return(ctcch_i(ch, port)); |
| } | } |
| else { | else { |
| return((REG8)0xff); | return(0xff); |
| } | } |
| } | } |
| Line 183 REG8 IOINPCALL ctc_i(UINT port) { | Line 293 REG8 IOINPCALL ctc_i(UINT port) { |
| void ctc_reset(void) { | void ctc_reset(void) { |
| UINT i, j; | UINT i; |
| UINT j; | |
| ZeroMemory(&ctc, sizeof(ctc)); | ZeroMemory(&ctc, sizeof(ctc)); |
| for (i=0; i<3; i++) { | for (i=0; i<3; i++) { |
| ctc.ch[i].num = (UINT8)i; | |
| for (j=0; j<4; j++) { | for (j=0; j<4; j++) { |
| ctc.ch[i].cmd[j] = 0x83; | ctc.ch[i].cmd[j] = 0x23; |
| ctc.ch[i].scale[j] = 7; | |
| ctc.ch[i].count[j] = 256 << 7; | |
| ctc.ch[i].countmax[j] = 256 << 7; | |
| } | } |
| } | } |
| } | } |