--- nono/vm/dmac.cpp 2026/04/29 17:04:32 1.1.1.2 +++ nono/vm/dmac.cpp 2026/04/29 17:04:58 1.1.1.9 @@ -1,22 +1,22 @@ // // nono -// Copyright (C) 2017 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // #include "dmac.h" -#include "mystring.h" +#include "bus.h" +#include "interrupt.h" +#include "mpu680x0.h" std::unique_ptr gDMAC; DMACDevice::DMACDevice() + : inherited("DMAC") { - logname = "dmac"; - devname = "DMAC"; devaddr = baseaddr; devlen = 0x2000; - monitor.Init(80, 30); - for (int i = 0; i < countof(dmac.chan); i++) { dmac.chan[i].ch = i; dmac.chan[i].parent = this; @@ -26,27 +26,65 @@ DMACDevice::DMACDevice() dmac.chan[2].desc = "User"; dmac.chan[3].desc = "ADPCM"; - for (int i = 0; i < countof(event); i++) { + for (int i = 0; i < event.size(); i++) { event[i].dev = this; event[i].func = (DeviceCallback_t)&DMACDevice::Transfer; event[i].SetName(string_format("DMAC #%d", i)); event[i].code = i; } + + monitor.func = (MonitorCallback_t)&DMACDevice::MonitorUpdate; + monitor.SetSize(80, 30); + monitor.Regist(ID_MONITOR_DMAC); } 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 (auto& ev : event) { + ev.Stop(); + } +} + uint64 -DMACDevice::Read8(uint32 addr) +DMACDevice::Read(uint32 offset) { uint8 data; int ch; - ch = (addr - baseaddr) / 0x40; + gMPU->AddCycle(37); // InsideOut p.135 + + ch = offset / 0x40; DMAC::Channel *chan = &dmac.chan[ch]; - int n = (addr - baseaddr) % 0x40; + int n = offset % 0x40; switch (n) { case DMAC::CSR: data = chan->PeekCSR(); @@ -74,7 +112,7 @@ DMACDevice::Read8(uint32 addr) break; case DMAC::CCR: - data = chan->PeekCCR(); + data = chan->ccr; putlog(4, "#%d CCR -> $%02x", ch, data); break; @@ -196,16 +234,19 @@ DMACDevice::Read8(uint32 addr) } uint64 -DMACDevice::Write8(uint32 addr, uint32 data) +DMACDevice::Write(uint32 offset, uint32 data) { int ch; - ch = (addr - baseaddr) / 0x40; + gMPU->AddCycle(31); // InsideOut p.135 + + ch = offset / 0x40; DMAC::Channel *chan = &dmac.chan[ch]; - int n = (addr - baseaddr) % 0x40; + int n = offset % 0x40; switch (n) { case DMAC::CSR: chan->WriteCSR(data); + ChangeInterrupt(); break; case DMAC::CER: @@ -230,6 +271,7 @@ DMACDevice::Write8(uint32 addr, uint32 d case DMAC::CCR: putlog(4, "#%d CCR <- $%02x", ch, data); chan->WriteCCR(data); + ChangeInterrupt(); break; case DMAC::MTC: @@ -345,14 +387,14 @@ DMACDevice::Write8(uint32 addr, uint32 d } uint64 -DMACDevice::Peek8(uint32 addr) +DMACDevice::Peek(uint32 offset) { uint8 data; int ch; - ch = (addr - baseaddr) / 0x40; + ch = offset / 0x40; const DMAC::Channel *chan = &dmac.chan[ch]; - int n = (addr - baseaddr) % 0x40; + int n = offset % 0x40; switch (n) { case DMAC::CSR: data = chan->PeekCSR(); @@ -375,7 +417,7 @@ DMACDevice::Peek8(uint32 addr) break; case DMAC::CCR: - data = chan->PeekCCR(); + data = chan->ccr; break; case DMAC::MTC: @@ -472,41 +514,41 @@ DMACDevice::Peek8(uint32 addr) return data; } -bool -DMACDevice::MonitorUpdate() +void +DMACDevice::MonitorUpdate(Monitor *, TextScreen& screen) { int x; int y; - monitor.Clear(); + screen.Clear(); y = 1; - monitor.Print(0, y++, "CSR"); + screen.Puts(0, y++, "CSR"); y += 2; - monitor.Print(0, y++, "CER"); - monitor.Print(0, y++, "DCR:XRM"); - monitor.Print(0, y++, "DCR:DTYP"); - monitor.Print(0, y++, "DCR:DPS"); - monitor.Print(0, y++, "DCR:PCL"); - monitor.Print(0, y++, "OCR:DIR"); - monitor.Print(0, y++, "OCR:SIZE"); - monitor.Print(0, y++, "OCR:CHAIN"); - monitor.Print(0, y++, "OCR:REQG"); - monitor.Print(0, y++, "SCR:MAC"); - monitor.Print(0, y++, "SCR:DAC"); - monitor.Print(0, y++, "CCR"); + screen.Puts(0, y++, "CER"); + screen.Puts(0, y++, "DCR:XRM"); + screen.Puts(0, y++, "DCR:DTYP"); + screen.Puts(0, y++, "DCR:DPS"); + screen.Puts(0, y++, "DCR:PCL"); + screen.Puts(0, y++, "OCR:DIR"); + screen.Puts(0, y++, "OCR:SIZE"); + screen.Puts(0, y++, "OCR:CHAIN"); + screen.Puts(0, y++, "OCR:REQG"); + screen.Puts(0, y++, "SCR:MAC"); + screen.Puts(0, y++, "SCR:DAC"); + screen.Puts(0, y++, "CCR"); y += 2; - monitor.Print(0, y++, "MTC"); - monitor.Print(0, y++, "MAR"); - monitor.Print(0, y++, "DAR"); - monitor.Print(0, y++, "BTC"); - monitor.Print(0, y++, "BAR"); - monitor.Print(0, y++, "NIV"); - monitor.Print(0, y++, "EIV"); - monitor.Print(0, y++, "MFC"); - monitor.Print(0, y++, "CPR"); - monitor.Print(0, y++, "DFC"); - monitor.Print(0, y++, "BFC"); + screen.Puts(0, y++, "MTC"); + screen.Puts(0, y++, "MAR"); + screen.Puts(0, y++, "DAR"); + screen.Puts(0, y++, "BTC"); + screen.Puts(0, y++, "BAR"); + screen.Puts(0, y++, "NIV"); + screen.Puts(0, y++, "EIV"); + screen.Puts(0, y++, "MFC"); + screen.Puts(0, y++, "CPR"); + screen.Puts(0, y++, "DFC"); + screen.Puts(0, y++, "BFC"); for (int ch = 0; ch < 4; ch++) { DMAC::Channel *chan = &dmac.chan[ch]; @@ -515,17 +557,17 @@ DMACDevice::MonitorUpdate() y = 0; // 地味だけど有効なチャンネルをハイライトしてみる - monitor.Print(x, y++, (chan->active ? TA::Em : TA::Off), + screen.Print(x, y++, (chan->active ? TA::Em : TA::Normal), "#%d (%s)", chan->ch, chan->desc); // CSR val = chan->PeekCSR(); - monitor.Print(x, y++, "$%02x", val); + screen.Print(x, y++, "$%02x", val); static const char * const csrname[] = { - "COC", "BTC", "NDT", "ERR", "ACT", "---", "PCT", "PCS", + "COC", "BTC", "NDT", "ERR", "ACT", "-", "PCT", "PCS", }; - MonitorReg4(x, y++, val >> 4, &csrname[0]); - MonitorReg4(x, y++, val & 0xff, &csrname[4]); + MonitorReg4(screen, x, y++, val >> 4, &csrname[0]); + MonitorReg4(screen, x, y++, val & 0xff, &csrname[4]); // CER val = chan->cer & 0x1f; @@ -535,9 +577,9 @@ DMACDevice::MonitorUpdate() } else { e = "?"; } - monitor.Print(x, y, "$%02x", chan->cer); + screen.Print(x, y, "$%02x", chan->cer); if (val > 0) { - monitor.Print(x + 3, y, ":%s", e); + screen.Print(x + 3, y, ":%s", e); } y++; @@ -550,7 +592,7 @@ DMACDevice::MonitorUpdate() "CycleWithHold", }; val = chan->dcr >> 6; - monitor.Print(x, y++, "%d:%s", val, dcr_xrm[val]); + screen.Print(x, y++, "%d:%s", val, dcr_xrm[val]); // DCR:DTYP static const char * const dcr_dtyp[] = { @@ -561,7 +603,7 @@ DMACDevice::MonitorUpdate() "ACK+READY", }; val = (chan->dcr >> 4) & 3; - monitor.Print(x, y++, "%d:%s", val, dcr_dtyp[val]); + screen.Print(x, y++, "%d:%s", val, dcr_dtyp[val]); // DCR:DPS static const char * const dcr_dps[] = { @@ -570,10 +612,10 @@ DMACDevice::MonitorUpdate() "16bit", }; val = (chan->dcr >> 3) & 1; - monitor.Print(x, y++, "%d:%s", val, dcr_dps[val]); + screen.Print(x, y++, "%d:%s", val, dcr_dps[val]); // DCR:PCL - monitor.Print(x, y++, "XXX PCL"); + screen.Print(x, y++, "XXX PCL"); // OCR:DIR static const char * const ocr_dir[] = { @@ -582,7 +624,7 @@ DMACDevice::MonitorUpdate() "DeviceToMemory", }; val = (chan->ocr >> 7); - monitor.Print(x, y++, "%d:%s", val, ocr_dir[val]); + screen.Print(x, y++, "%d:%s", val, ocr_dir[val]); // OCR:SIZE static const char * const ocr_size[] = { @@ -593,7 +635,7 @@ DMACDevice::MonitorUpdate() "8bit Unpacked", }; val = (chan->ocr >> 4) & 3; - monitor.Print(x, y++, "%d:%s", val, ocr_size[val]); + screen.Print(x, y++, "%d:%s", val, ocr_size[val]); // OCR:CHAIN static const char * const ocr_chain[] = { @@ -604,7 +646,7 @@ DMACDevice::MonitorUpdate() "LinkArrayChain", }; val = (chan->ocr >> 2) & 3; - monitor.Print(x, y++, "%d:%s", val, ocr_chain[val]); + screen.Print(x, y++, "%d:%s", val, ocr_chain[val]); // OCR:REQG static const char * const ocr_reqg[] = { @@ -615,7 +657,7 @@ DMACDevice::MonitorUpdate() "AutoThenExt", }; val = (chan->ocr & 3); - monitor.Print(x, y++, "%d:%s", val, ocr_reqg[val]); + screen.Print(x, y++, "%d:%s", val, ocr_reqg[val]); // SCR:MAC static const char * const scr_xac[] = { @@ -626,73 +668,75 @@ DMACDevice::MonitorUpdate() "undefined", }; val = (chan->scr >> 2) & 3; - monitor.Print(x, y++, "%d:%s", val, scr_xac[val]); + screen.Print(x, y++, "%d:%s", val, scr_xac[val]); val = chan->scr & 3; - monitor.Print(x, y++, "%d:%s", val, scr_xac[val]); + screen.Print(x, y++, "%d:%s", val, scr_xac[val]); // CCR - val = chan->PeekCCR(); - monitor.Print(x, y++, "$%02x", val); + val = chan->ccr; + screen.Print(x, y++, "$%02x", val); static const char * const ccrnames[] = { - "STR", "CNT", "HLT", "SAB", "INT", "---", "---", "---", + "STR", "CNT", "HLT", "SAB", "INT", "-", "-", "-", }; - MonitorReg4(x, y++, (val >> 4), &ccrnames[0]); - MonitorReg4(x, y++, (val & 0xff), &ccrnames[4]); + MonitorReg4(screen, x, y++, (val >> 4), &ccrnames[0]); + MonitorReg4(screen, x, y++, (val & 0xff), &ccrnames[4]); // MTC - monitor.Print(x, y++, "$%04x", chan->mtc); + screen.Print(x, y++, "$%04x", chan->mtc); // MAR // X680x0 がターゲットなので24bit超える設定は目立たせる if (chan->mar > 0xffffff) { - monitor.Print(x, y++, TA::On, "$%08x", chan->mar); + screen.Print(x, y++, TA::On, "$%08x", chan->mar); } else { - monitor.Print(x, y++, "$%06x", chan->mar); + screen.Print(x, y++, "$%06x", chan->mar); } // DAR if (chan->dar > 0xffffff) { - monitor.Print(x, y++, TA::On, "$%08x", chan->dar); + screen.Print(x, y++, TA::On, "$%08x", chan->dar); } else { - monitor.Print(x, y++, "$%06x", chan->dar); + screen.Print(x, y++, "$%06x", chan->dar); } // BTC - monitor.Print(x, y++, "$%04x", chan->btc); + screen.Print(x, y++, "$%04x", chan->btc); // BAR if (chan->bar > 0xffffff) { - monitor.Print(x, y++, TA::On, "$%08x", chan->bar); + screen.Print(x, y++, TA::On, "$%08x", chan->bar); } else { - monitor.Print(x, y++, "$%06x", chan->bar); + screen.Print(x, y++, "$%06x", chan->bar); } // NIV - monitor.Print(x, y++, "$%02x", chan->niv); + screen.Print(x, y++, "$%02x", chan->niv); // EIV - monitor.Print(x, y++, "$%02x", chan->eiv); + screen.Print(x, y++, "$%02x", chan->eiv); // MFC - monitor.Print(x, y++, "$%02x", chan->mfc); + screen.Print(x, y++, "$%02x", chan->mfc); // CPR - monitor.Print(x, y++, "$%02x", chan->cpr); + screen.Print(x, y++, "$%02x", chan->cpr); // DFC - monitor.Print(x, y++, "$%02x", chan->dfc); + screen.Print(x, y++, "$%02x", chan->dfc); // BFC - monitor.Print(x, y++, "$%02x", chan->bfc); + screen.Print(x, y++, "$%02x", chan->bfc); if (ch == 3) { - monitor.Print(x - 5, y++, "GCR: $%02x", dmac.gcr); + screen.Print(x - 5, y++, "GCR: $%02x", dmac.gcr); } } - - return true; } // 4ビット分を表示する。MonitorUpdate の下請け void -DMACDevice::MonitorReg4(int x, int y, uint32 reg, const char * const *names) +DMACDevice::MonitorReg4(TextScreen& screen, + int x, int y, uint32 reg, const char * const *names) { for (int i = 0; i < 4; i++) { - bool b = reg & (1 << (3 - i)); - uint attr = b ? TA::On : TA::Off; - monitor.Print(x + i * 4, y, attr, "%s", names[i]); + if (names[i][0] == '-') { + screen.Puts(x + i * 4, y, TA::Disable, "---"); + } else { + bool b = reg & (1 << (3 - i)); + screen.Puts(x + i * 4, y, TA::OnOff(b), names[i]); + } } } @@ -739,6 +783,9 @@ DMAC::Channel::PeekCSR() const return val; } +// CSR への書き込み。 +// CSR への書き込みによって割り込み信号線が変化するかも知れないので +// この後呼び出し側が ChangeInterrupt() を呼ぶこと。 void DMAC::Channel::WriteCSR(uint32 data) { @@ -751,27 +798,19 @@ DMAC::Channel::WriteCSR(uint32 data) prev_pcl = false; } - 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; + parent->putlogf(4, lstr("#%d CSR <- $%02x (CSR = $%02x)", + ch, data, PeekCSR())); } +// 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)) { @@ -781,12 +820,12 @@ DMAC::Channel::WriteCCR(uint32 data) // HLT (Halt Operation) if ((data & DMAC::CCR_HLT)) { - parent->putlog(0, "CCR HLT 未実装"); + parent->putlogf(0, lstr("CCR HLT 未実装")); } // CNT (Continue Operation) if ((data & DMAC::CCR_CNT)) { - parent->putlog(0, "CCR CNT 未実装"); + parent->putlogf(0, lstr("CCR CNT 未実装")); } // STR (Start Operation) @@ -795,32 +834,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(3, vector); - } -} - -// 転送開始 +// 転送開始 (チャンネル側) void DMAC::Channel::StartTransfer() { @@ -858,7 +872,7 @@ DMAC::Channel::StartTransfer() case DMAC::OCR_REQG_AUTO_MAX: // 自発的に転送を開始する - parent->Transfer(ch); + parent->StartTransfer(this); break; case DMAC::OCR_REQG_EXTERNAL: @@ -871,11 +885,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; @@ -932,13 +956,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(); } } @@ -990,7 +1014,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?"); }