--- xmil/io/ctc.c 2004/08/11 13:53:53 1.6 +++ xmil/io/ctc.c 2004/08/15 17:51:53 1.15 @@ -6,6 +6,33 @@ #include "ievent.h" +static SINT32 minclock(const CTCCH *ch) { + + UINT32 event; + UINT i; + UINT32 clock; + + 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); +} + static REG8 ctcwork(CTCCH *ch) { UINT32 baseclock; @@ -25,10 +52,7 @@ static REG8 ctcwork(CTCCH *ch) { // 0 if (!(ch->cmd[0] & 0x02)) { - pulse = stepclock; // 2MHz - if (!(ch->cmd[0] & 0x40)) { - pulse = pulse * 2; // 4MHz - } + pulse = stepclock; count = ch->count[0]; count -= pulse; if (count <= 0) { @@ -43,7 +67,7 @@ static REG8 ctcwork(CTCCH *ch) { // 3 if (!(ch->cmd[3] & 0x02)) { if (!(ch->cmd[3] & 0x40)) { - pulse3 = stepclock * 2; // 4MHz + pulse3 = stepclock; } count = ch->count[3]; count -= pulse3; @@ -56,10 +80,7 @@ static REG8 ctcwork(CTCCH *ch) { // 1 if (!(ch->cmd[1] & 0x02)) { - pulse = stepclock; // 2MHz - if (!(ch->cmd[1] & 0x40)) { - pulse = pulse * 2; // 4MHz - } + pulse = stepclock; count = ch->count[1]; count -= pulse; if (count <= 0) { @@ -71,10 +92,7 @@ static REG8 ctcwork(CTCCH *ch) { // 2 if (!(ch->cmd[2] & 0x02)) { - pulse = stepclock; // 2MHz - if (!(ch->cmd[2] & 0x40)) { - pulse = pulse * 2; // 4MHz - } + pulse = stepclock; count = ch->count[2]; count -= pulse; if (count <= 0) { @@ -100,35 +118,11 @@ static void ctcstep(CTCCH *ch) { static void ctcnextevent(CTCCH *ch) { UINT32 event; - UINT i; - UINT32 clock; if (ch->intr) { return; } - event = 0x04000000; - for (i=0; i<3; i++) { - if ((ch->cmd[i] & 0x82) == 0x80) { - clock = ch->count[i]; - if (ch->cmd[i] & 0x40) { - clock = clock * 2; - } - 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]; - if (ch->cmd[0] & 0x40) { - clock = clock * 2; - } - } - event = min(event, clock); - } - event /= 2; - event *= pccore.multiple; + event = minclock(ch) * pccore.multiple; nevent_set(NEVENT_CTC0 + ch->num, event, neitem_ctc, NEVENT_ABSOLUTE); } @@ -161,24 +155,31 @@ BRESULT ieitem_ctc(UINT id) { intr |= ch->intr; r = FALSE; if (intr) { - for (i=0, bit=1; i<4; i++, bit<<=1) { + 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)) { + intr ^= bit; + } #if 1 // アークスのタイミング→あとで修正 - if (0) + else if (0) +#elif 1 + else if ((ch->countmax[i] - ch->count[i]) >= ch->range[i]) #elif 0 - if (((ch->count[i] * 17) >> 4) < ch->countmax[i]) + else if (((ch->cmd[i] & 0x10) == 0) && + ((ch->countmax[i] - ch->count[i]) >= (256 >> 1))) #else - if (ch->count[i] != ch->countmax[i]) + else if (ch->count[i] != ch->countmax[i]) #endif { intr ^= bit; } - else if (!(ch->cmd[i] & 0x80)) { - intr ^= bit; - } else if (!r) { r = TRUE; intr ^= bit; + ch->irq = (UINT8)i; +// TRACEOUT(("ctc int %d %d [%.2x]", ch->num, i, ch->cmd[i])); Z80_INTERRUPT((REG8)(ch->vector + (i << 1))); } } @@ -194,6 +195,30 @@ BRESULT ieitem_ctc(UINT id) { return(r); } +void ieeoi_ctc(UINT id) { + + CTCCH *ch; + REG8 intr; + UINT curirq; + + ch = ctc.ch + (id - IEVENT_CTC0); + intr = ctcwork(ch) | ch->intr; + curirq = ch->irq; + if (intr & (1 << curirq)) { // 割り込み中に割り込んだ… + // カウンタが0でなければ割り込みを消す + if ((ch->countmax[curirq] - ch->count[curirq]) >= ch->range[curirq]) { + intr ^= (1 << curirq); + } + } + ch->intr = intr; + if (intr) { + ievent_set(id); + } + else { + ctcnextevent(ch); + } +} + // ---- @@ -206,22 +231,20 @@ static void ctcch_o(CTCCH *ch, UINT port if (ch->cmd[port] & 0x04) { ctcstep(ch); ch->basecnt[port] = value; - count = 256; - if (value) { - count = (SINT32)value; - } + count = ((value - 1) & 0xff) + 1; scale = 0; if (!(ch->cmd[port] & 0x40)) { if (ch->cmd[port] & 0x20) { - scale = 8; + scale = 8 - 1; } else { - scale = 4; + scale = 4 - 1; } } - count <<= scale; - ch->countmax[port] = count; - ch->count[port] = count; + ch->scale[port] = scale; + ch->countmax[port] = count << scale; + ch->count[port] = count << scale; + ch->range[port] = 1 << scale; ch->cmd[port] &= ~6; ctcnextevent(ch); } @@ -237,24 +260,13 @@ static void ctcch_o(CTCCH *ch, UINT port static REG8 ctcch_i(CTCCH *ch, UINT port) { - REG8 scale; - port &= 3; if (port != 3) { return(ch->basecnt[port]); } else { ctcstep(ch); - scale = 0; - if (!(ch->cmd[3] & 0x40)) { - if (ch->cmd[3] & 0x20) { - scale = 8; - } - else { - scale = 4; - } - } - return((REG8)(ch->count[3] >> scale)); + return((REG8)(ch->count[3] >> ch->scale[3])); } } @@ -280,7 +292,7 @@ void IOOUTCALL ctc_o(UINT port, REG8 val CTCCH *ch; -// TRACEOUT(("ctc - %.4x %.2x [%.4x]", port, value, Z80_PC)); + TRACEOUT(("ctc - %.4x %.2x [%.4x]", port, value, Z80_PC)); ch = getctcch(port); if (ch != NULL) { ctcch_o(ch, port, value); @@ -312,9 +324,10 @@ void ctc_reset(void) { for (i=0; i<3; i++) { ctc.ch[i].num = (UINT8)i; for (j=0; j<4; j++) { - ctc.ch[i].cmd[j] = 0x03; - ctc.ch[i].count[j] = 256 << 8; - ctc.ch[i].countmax[j] = 256 << 8; + 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; } } }