// SPDX-License-Identifier: MIT //! The direct memory access controller (DMAC): two channels. //! //! sh2-python's `dmac.py`. HM chapter 9. A channel runs while CHCRn.DE = 1, DMAOR.DME = 1 //! and none of CHCRn.TE, DMAOR.NMIF, DMAOR.AE is set. Each transfer moves one unit //! (CHCR.TS: byte, word, longword, or a 16-byte unit of four longwords) from SARn to //! DARn, steps the addresses (CHCR.SM/DM: fixed, increment, decrement), and decrements //! TCRn; when TCRn reaches 0, CHCR.TE is set and, with CHCR.IE, the DEI interrupt //! requested (vector VCRDMAn). The request comes from the DREQ pin (CHCR.AR = 0; //! DRCRn.RS selects DREQ for the 32X's channel 0 FIFO) or is automatic (AR = 1). //! Cycle-steal mode (CHCR.TB = 0) gives the bus back after each transfer, burst mode (TB //! = 1) keeps it until the count ends (HM 9.3.4). //! //! DMA cycles reach the board with ``kind`` = ``DMA`` (``| DMA1`` for channel 1). DMA //! writes never update the cache (HM 7.11.2, 8.5.2: no snooping). //! //! Rungs 0-2 implement the registers and dual-address transfers between whole steps of //! the CPU: one transfer per CPU step in cycle-steal mode, the whole block in burst //! mode, with DREQ sampled as a level at the step boundary. A DMA address error (HM //! table 4.6's misaligned and wrong-size accesses) finishes its transfer, sets DMAOR.AE, //! stops both channels and raises the DMA address error exception, vector 10 (HM 9.2.7, //! 9.3.8; ``dmac_transfer``). Not modelled yet (rung 5 and the board's rung 6): the bus //! arbitration between CPU and DMAC inside a step, single-address mode and DACK //! (``on_dack``), DREQ edge detection (CHCR.DS) and DREQ1 acknowledge timing, the //! priority mode of DMAOR.PR. use crate::bus::{Bus, DMA, DMA1}; use crate::cpu::Sh7604; use crate::intc::Intc; use crate::module::{longword_shift, reg, value_of, ModuleState, Register}; pub const UNIT_BYTES: [u32; 4] = [1, 2, 4, 16]; // CHCR.TS1-0 (HM 9.2.4) pub const REGISTERS: [Register; 13] = [ reg(0xFFFFFF80, "SAR0", 32, None), reg(0xFFFFFF84, "DAR0", 32, None), reg(0xFFFFFF88, "TCR0", 32, None), reg(0xFFFFFF8C, "CHCR0", 32, Some(0x00000000)), reg(0xFFFFFF90, "SAR1", 32, None), reg(0xFFFFFF94, "DAR1", 32, None), reg(0xFFFFFF98, "TCR1", 32, None), reg(0xFFFFFF9C, "CHCR1", 32, Some(0x00000000)), reg(0xFFFFFFA0, "VCRDMA0", 32, None), reg(0xFFFFFFA8, "VCRDMA1", 32, None), reg(0xFFFFFFB0, "DMAOR", 32, Some(0x00000000)), reg(0xFFFFFE71, "DRCR0", 8, Some(0x00)), reg(0xFFFFFE72, "DRCR1", 8, Some(0x00)), ]; /// One channel's registers. #[derive(Clone, Debug, Default)] pub struct Channel { pub number: u32, pub sar: u32, pub dar: u32, pub tcr: u32, pub chcr: u32, pub vcr: u32, pub drcr: u32, } impl Channel { pub fn new(number: u32) -> Channel { Channel { number, ..Channel::default() } } } #[derive(Clone, Debug)] pub struct Dmac { pub channels: [Channel; 2], pub dmaor: u32, /// CHCR.TE as last read: it clears only after being read as 1. pub te_read: [u32; 2], pub ae_nmif_read: u32, /// True while a channel may transfer; the chip checks it once per step. pub active: bool, } impl Dmac { pub fn new() -> Dmac { Dmac { channels: [Channel::new(0), Channel::new(1)], dmaor: 0, te_read: [0, 0], ae_nmif_read: 0, active: false } } pub fn reset(&mut self) { for channel in self.channels.iter_mut() { channel.chcr = 0; channel.drcr = 0; } self.dmaor = 0; self.active = false; } pub fn enabled(&self, channel: &Channel) -> bool { // DE, DME, and none of TE, NMIF, AE (HM 9.3.1). channel.chcr & 1 != 0 && channel.chcr & 2 == 0 && self.dmaor & 7 == 1 } pub fn requests(&self, intc: &Intc, out: &mut Vec<(u32, u32)>) { // DEI0/DEI1: CHCR.TE with CHCR.IE (HM 9.4), level IPRA.DMACIP. let level = intc.level("dmac"); if level == 0 { return; } for channel in self.channels.iter() { if channel.chcr & 6 == 6 { // IE (bit 2) and TE (bit 1) out.push((level, channel.vcr & 0x7F)); } } } // -- registers ----------------------------------------------------------------- pub fn read(&mut self, address: u32, width: u32) -> u32 { if address == 0xFFFFFE71 || address == 0xFFFFFE72 { return if width == 8 { self.channels[(address - 0xFFFFFE71) as usize].drcr } else { 0 }; } // LongwordModule.read let base = address & !3; let value = self.read32(base); if width == 32 { return value; } let shift = longword_shift(address, width); (value >> shift) & ((1u32 << width) - 1) } pub fn read32(&mut self, address: u32) -> u32 { if address == 0xFFFFFFB0 { self.ae_nmif_read = self.dmaor; return self.dmaor; } if address == 0xFFFFFFA0 || address == 0xFFFFFFA8 { return self.channels[((address >> 3) & 1) as usize].vcr; } let channel = &self.channels[((address >> 4) & 1) as usize]; let register = address & 0xC; if register == 0x0 { return channel.sar; } if register == 0x4 { return channel.dar; } if register == 0x8 { return channel.tcr; } let (number, chcr) = (channel.number as usize, channel.chcr); self.te_read[number] = chcr & 2; chcr } /// The register half of ``write32`` (the reference's then calls ``_update``). fn write32_registers(&mut self, address: u32, value: u32, mask: u32) { if address == 0xFFFFFFB0 { // DMAOR: PR (3), AE (2), NMIF (1), DME (0); AE and NMIF clear only after // being read as 1 (HM 9.2.7). let merged = (self.dmaor & !mask) | (value & mask); let cleared = !merged & self.ae_nmif_read & 0x6; self.dmaor = (self.dmaor & 0x6 & !cleared) | (merged & 0x9); } else if address == 0xFFFFFFA0 || address == 0xFFFFFFA8 { let channel = &mut self.channels[((address >> 3) & 1) as usize]; channel.vcr = ((channel.vcr & !mask) | (value & mask)) & 0x7F; } else if (0xFFFFFF80..0xFFFFFFA0).contains(&address) { let te_read = self.te_read; let channel = &mut self.channels[((address >> 4) & 1) as usize]; let register = address & 0xC; if register == 0x0 { channel.sar = (channel.sar & !mask) | (value & mask); } else if register == 0x4 { channel.dar = (channel.dar & !mask) | (value & mask); } else if register == 0x8 { channel.tcr = ((channel.tcr & !mask) | (value & mask)) & 0xFFFFFF; } else { let merged = ((channel.chcr & !mask) | (value & mask)) & 0xFFFF; // TE (bit 1) clears only when written 0 after being read as 1. let mut te = channel.chcr & 2; if merged & 2 == 0 && te_read[channel.number as usize] != 0 { te = 0; } channel.chcr = (merged & !2) | te; } } } pub fn state(&self) -> ModuleState { let names: [[&'static str; 6]; 2] = [ ["SAR0", "DAR0", "TCR0", "CHCR0", "VCRDMA0", "DRCR0"], ["SAR1", "DAR1", "TCR1", "CHCR1", "VCRDMA1", "DRCR1"], ]; let mut out = Vec::new(); for channel in self.channels.iter() { let n = &names[channel.number as usize]; out.push((n[0], channel.sar as i64)); out.push((n[1], channel.dar as i64)); out.push((n[2], channel.tcr as i64)); out.push((n[3], channel.chcr as i64)); out.push((n[4], channel.vcr as i64)); out.push((n[5], channel.drcr as i64)); } out.push(("DMAOR", self.dmaor as i64)); out } pub fn load_state(&mut self, values: &ModuleState) { let names: [[&'static str; 6]; 2] = [ ["SAR0", "DAR0", "TCR0", "CHCR0", "VCRDMA0", "DRCR0"], ["SAR1", "DAR1", "TCR1", "CHCR1", "VCRDMA1", "DRCR1"], ]; for channel in self.channels.iter_mut() { let n = &names[channel.number as usize]; channel.sar = value_of(values, n[0]) as u32; channel.dar = value_of(values, n[1]) as u32; channel.tcr = value_of(values, n[2]) as u32; channel.chcr = value_of(values, n[3]) as u32; channel.vcr = value_of(values, n[4]) as u32; channel.drcr = value_of(values, n[5]) as u32; } self.dmaor = value_of(values, "DMAOR") as u32; self.active = self.channels.iter().any(|channel| self.enabled(channel)); } /// CHCR0.TE, CHCR1.TE and DMAOR as last read (they clear only after a read of 1). pub fn internal(&self) -> Vec { vec![self.te_read[0] as i64, self.te_read[1] as i64, self.ae_nmif_read as i64] } pub fn load_internal(&mut self, values: &[i64]) { let _ = values; self.ae_nmif_read = values[2] as u32; } } impl Default for Dmac { fn default() -> Self { Dmac::new() } } impl Sh7604 { pub(crate) fn dmac_write(&mut self, address: u32, width: u32, value: u32) { if address == 0xFFFFFE71 || address == 0xFFFFFE72 { if width == 8 { self.dmac.channels[(address - 0xFFFFFE71) as usize].drcr = value & 3; // RS1-0 } return; } // LongwordModule.write let base = address & !3; if width == 32 { self.dmac_write32(base, value, 0xFFFF_FFFF); return; } let shift = longword_shift(address, width); let mask = ((1u32 << width) - 1) << shift; self.dmac_write32(base, (value << shift) & mask, mask); } fn dmac_write32(&mut self, address: u32, value: u32, mask: u32) { self.dmac.write32_registers(address, value, mask); self.dmac_update(); } fn dmac_update(&mut self) { let dmac = &self.dmac; self.dmac.active = dmac.channels.iter().any(|channel| dmac.enabled(channel)); self.intc_update(); } // -- transfers ---------------------------------------------------------------------- /// Make the transfers due at this step boundary (see the module docstring). pub(crate) fn dmac_run(&mut self) { for number in 0..2 { if !self.dmac.enabled(&self.dmac.channels[number]) { continue; } let chcr = self.dmac.channels[number].chcr; // AR = 0: an external request, DREQ sampled as a level. let dreq = if number != 0 { self.dreq1 } else { self.dreq0 }; if chcr & 0x0200 == 0 && dreq == 0 { continue; } let burst = chcr & 0x0010; // TB loop { self.dmac_transfer(number); if self.dmac.channels[number].chcr & 2 != 0 || burst == 0 || self.dmac.dmaor & 4 != 0 { // TE, AE break; } } } self.dmac_update(); } fn dmac_transfer(&mut self, number: usize) { let channel = &self.dmac.channels[number]; let chcr = channel.chcr; let unit = UNIT_BYTES[((chcr >> 10) & 3) as usize]; let kind = DMA | if channel.number != 0 { DMA1 } else { 0 }; let (src, dst) = (channel.sar, channel.dar); // A DMA address error (HM table 4.6; chip.dma_address_fault). DOC, HM 9.3.8: "In // dual address mode, DMA transfer will be halted after the completion of the // following write cycle even when the address error occurs in the initial read // cycle. SAR, DAR and TCR are updated by the final transfer", so the transfer // finishes and is counted below. HYPOTHESIS (U15, as the CPU's misaligned // accesses in this core): the faulting access itself makes no bus cycle, and a // faulting read gives 0 to the write; a probe that DMAs from a misaligned // source into RAM and reads the destination, with the bus on a logic analyser, // would settle both. let width = if unit == 16 { 32 } else { unit * 8 }; let read_fault = Self::dma_address_fault(src, width); let write_fault = Self::dma_address_fault(dst, width); if unit == 16 { let words = if read_fault { vec![0, 0, 0, 0] } else { self.dma_read_unit(src, kind) }; if !write_fault { for (i, word) in words.iter().enumerate() { self.dma_write(dst.wrapping_add(4 * i as u32), 32, *word, kind); } } } else { let value = if read_fault { 0 } else { self.dma_read(src, width, kind) }; if !write_fault { self.dma_write(dst, width, value, kind); } } let (sm, dm) = ((chcr >> 12) & 3, (chcr >> 14) & 3); let channel = &mut self.dmac.channels[number]; // SM/DM: 00 fixed, 01 increment, 10 decrement (HM 9.2.4); "for a 16-byte // transfer, the [source] address is incremented by +16 regardless of the SM1 // and SM0 values". if unit == 16 || sm == 1 { channel.sar = src.wrapping_add(unit); } else if sm == 2 { channel.sar = src.wrapping_sub(unit); } if dm == 1 { channel.dar = dst.wrapping_add(unit); } else if dm == 2 { channel.dar = dst.wrapping_sub(unit); } // TCR counts transfers; "for 16-byte transfers, set the count to 4 times the // number of transfers" (HM 9.2.3); 0 means 2^24. let step = if unit == 16 { 4 } else { 1 }; channel.tcr = channel.tcr.wrapping_sub(step) & 0xFFFFFF; if channel.tcr == 0 { channel.chcr |= 2; // TE: also after a faulting final transfer (HM 9.3.8) } if read_fault || write_fault { // DOC: HM 9.2.7 (DMAOR.AE: "Operation is performed up to the DMAC transfer // being executed when the address error occurred"; no channel transfers // while AE = 1), HM 9.3.8 ("all channels stop their transfers"), HM 4.1 // (the DMA address error exception, vector 10, which the CPU takes at its // next instruction boundary: core.rs ``step``). TE stays 0 when TCR has not // reached 0 (HM 9.2.4, TE). self.dmac.dmaor |= 4; // AE self.dma_address_error = true; } } }