--- nono/vm/dmac.cpp 2026/04/29 17:04:38 1.1.1.4 +++ nono/vm/dmac.cpp 2026/04/29 17:04:44 1.1.1.6 @@ -6,6 +6,7 @@ #include "dmac.h" #include "bus.h" +#include "interrupt.h" #include "mpu680x0.h" #include "mystring.h" @@ -41,12 +42,46 @@ DMACDevice::~DMACDevice() { } +void +DMACDevice::ResetHard() +{ + putlog(2, "リセット"); + + // Clears GCR, DCR, OCR, SCR, CCR, CSR, CPR and CER for all channels. + // NIV and EIV are all set to $0F (uninitialized interrupt vector). + // MTC, MAR, DAR, BTC, BAR, MFC, DFC and BFC are not affected. + + dmac.gcr = 0; + for (int ch = 0; ch < countof(dmac.chan); ch++) { + DMAC::Channel *chan = &dmac.chan[ch]; + chan->dcr = 0; + chan->ocr = 0; + chan->scr = 0; + chan->ccr = 0; + chan->csr = 0; + chan->active = false; + chan->pcl = false; + chan->cpr = 0; + chan->cer = 0; + + chan->niv = 0x0f; + chan->eiv = 0x0f; + } + + // イベントをすべて停止 + for (int i = 0; i < countof(event); i++) { + event[i].Stop(); + } +} + uint64 DMACDevice::Read(uint32 addr) { uint8 data; int ch; + gMPU->AddCycle(37); // InsideOut p.135 + ch = addr / 0x40; DMAC::Channel *chan = &dmac.chan[ch]; int n = addr % 0x40; @@ -77,7 +112,7 @@ DMACDevice::Read(uint32 addr) break; case DMAC::CCR: - data = chan->PeekCCR(); + data = chan->ccr; putlog(4, "#%d CCR -> $%02x", ch, data); break; @@ -203,12 +238,15 @@ DMACDevice::Write(uint32 addr, uint32 da { int ch; + gMPU->AddCycle(31); // InsideOut p.135 + ch = addr / 0x40; DMAC::Channel *chan = &dmac.chan[ch]; int n = addr % 0x40; switch (n) { case DMAC::CSR: chan->WriteCSR(data); + ChangeInterrupt(); break; case DMAC::CER: @@ -233,6 +271,7 @@ DMACDevice::Write(uint32 addr, uint32 da case DMAC::CCR: putlog(4, "#%d CCR <- $%02x", ch, data); chan->WriteCCR(data); + ChangeInterrupt(); break; case DMAC::MTC: @@ -378,7 +417,7 @@ DMACDevice::Peek(uint32 addr) break; case DMAC::CCR: - data = chan->PeekCCR(); + data = chan->ccr; break; case DMAC::MTC: @@ -634,7 +673,7 @@ DMACDevice::MonitorUpdate(TextScreen& mo monitor.Print(x, y++, "%d:%s", val, scr_xac[val]); // CCR - val = chan->PeekCCR(); + val = chan->ccr; monitor.Print(x, y++, "$%02x", val); static const char * const ccrnames[] = { "STR", "CNT", "HLT", "SAB", "INT", "---", "---", "---", @@ -740,6 +779,9 @@ DMAC::Channel::PeekCSR() const return val; } +// CSR への書き込み。 +// CSR への書き込みによって割り込み信号線が変化するかも知れないので +// この後呼び出し側が ChangeInterrupt() を呼ぶこと。 void DMAC::Channel::WriteCSR(uint32 data) { @@ -755,24 +797,15 @@ DMAC::Channel::WriteCSR(uint32 data) parent->putlog(4, "#%d CSR <- $%02x (CSR = $%02x)", ch, data, PeekCSR()); } -// Read と Peek から呼ばれるので副作用を持たせてはいけない。 -uint8 -DMAC::Channel::PeekCCR() const -{ - uint32 val; - - val = ccr & 0xf0; - if (int_enable) { - val |= DMAC::CCR_INT; - } - return val; -} - +// CCR への書き込み。 +// CCR への書き込みによって割り込み信号線が変化するかも知れないので +// この後呼び出し側が ChangeInterrupt() を呼ぶこと。 void DMAC::Channel::WriteCCR(uint32 data) { // 割り込み許可ビットは常に反映 - int_enable = (data & DMAC::CCR_INT); + ccr &= ~DMAC::CCR_INT; + ccr |= (data & DMAC::CCR_INT); // SAB (Software Abort) if ((data & DMAC::CCR_SAB)) { @@ -796,32 +829,7 @@ DMAC::Channel::WriteCCR(uint32 data) } } -// 割り込み要因ビットをセットし、必要なら割り込みを上げる。 -// PCL は割り込み条件を満たしている時だけ呼ぶこと。 -// ここはレジスタに対する書き込みではない。 -void -DMAC::Channel::SetINT(uint32 val) -{ - int vector; - - csr |= val; - - // ERR ビットが立つと SAB をクリアする - if ((csr & CSR_ERR)) { - ccr &= ~CCR_SAB; - } - - if (int_enable) { - if ((csr & CSR_ERR)) { - vector = eiv; - } else { - vector = niv; - } - gMPU680x0->Interrupt(parent, 3, vector); - } -} - -// 転送開始 +// 転送開始 (チャンネル側) void DMAC::Channel::StartTransfer() { @@ -859,7 +867,7 @@ DMAC::Channel::StartTransfer() case DMAC::OCR_REQG_AUTO_MAX: // 自発的に転送を開始する - parent->Transfer(ch); + parent->StartTransfer(this); break; case DMAC::OCR_REQG_EXTERNAL: @@ -872,11 +880,21 @@ DMAC::Channel::StartTransfer() } } +// 転送開始 (デバイス側) +// (チャンネルが転送開始する際にデバイス側の private 変数(event[])が必要に +// なるため、プロキシしている) +void +DMACDevice::StartTransfer(DMAC::Channel *chan) +{ + Transfer(event[chan->ch]); +} + // 転送 // (イベントを持つのでこれはデバイスクラスのメソッド) void -DMACDevice::Transfer(int ch) +DMACDevice::Transfer(Event& ev) { + int& ch = ev.code; DMAC::Channel *chan = &dmac.chan[ch]; int64 rv; @@ -933,13 +951,13 @@ DMACDevice::Transfer(int ch) chan->mtc--; if (chan->mtc == 0) { // 転送完了 - chan->active = false; - chan->SetINT(DMAC::CSR_COC); + chan->csr |= DMAC::CSR_COC; + ChangeInterrupt(); } else { // 次回の転送 // XXX とりあえず 1us で1回の転送にする - event[ch].time = 1_usec; - event[ch].Start(); + ev.time = 1_usec; + ev.Start(); } } @@ -991,7 +1009,46 @@ DMACDevice::AbortTransfer(DMAC::Channel // ここでは SAB ビットのセットを省略。 event[ch].Stop(); - chan->active = false; chan->cer = DMAC::CER_SOFT_ABORT; - chan->SetINT(DMAC::CSR_ERR); + // ERR ビットが立つと SAB をクリアする + chan->csr |= DMAC::CSR_ERR; + chan->ccr &= ~DMAC::CCR_SAB; + ChangeInterrupt(); +} + +// 割り込み信号線の状態を変える。 +void +DMACDevice::ChangeInterrupt() +{ + bool irq = false; + + for (int ch = 0; ch < countof(dmac.chan); ch++) { + DMAC::Channel *chan = &dmac.chan[ch]; + + if ((chan->ccr & DMAC::CCR_INT) && chan->IsINTR()) { + irq = true; + } + } + gInterrupt->ChangeINT(this, irq); +} + +// 割り込みアクノリッジ +int +DMACDevice::InterruptAcknowledge() +{ + // XXX CPR のプライオリティは未対応、とりあえず前から順 + for (int ch = 0; ch < countof(dmac.chan); ch++) { + DMAC::Channel *chan = &dmac.chan[ch]; + + if (chan->active) { + if ((chan->csr & DMAC::CSR_ERR)) { + return chan->eiv; + } else { + return chan->niv; + } + chan->active = false; + } + } + // XXX 誰もいなかったら? + PANIC("InterruptAcknowledge no active channels?"); }