pcengine_rust/vdc/
dma.rs

1//! The VDC's two DMAs and burst mode: VRAM -> VRAM, and VRAM -> sprite attribute table
2//! (`vdc/dma.py`).
3//!
4//! [hw: pcetech 5.4; mfr: Hudson HuC6270 manual DCR/SOUR/DESR/LENR/DVSSR]
5//!
6//! VRAM -> VRAM: starts when LENR's MSB is written; one word per two dots (read, then write); SOUR
7//! and DESR step up or down by DCR D2/D3 (not by the CR increment), LENR counts down and ends at
8//! $FFFF; MAWR, MARR, the read buffer and the write latch are untouched. A transfer that is running
9//! when the display period starts is **paused and resumes once the VDC is free again** (C7:
10//! MacDonald, tier 2, and Mesen2 say so; Hudson's manual says the remainder is not transferred
11//! and Mednafen cancels it; `resume = false` selects that reading). Time: 2 dots per word is
12//! Mednafen's and Mesen2's.
13//!
14//! VRAM -> SAT: armed by a write to DVSSR, started when the display period ends (or every frame
15//! with DCR D4); the 256 words are copied at the start and the VDC is busy for 1024 dots, which
16//! is three lines at 5 MHz, "four lines after the last active line" as pcetech says (U2). The
17//! done interrupt (DCR D0) follows.
18//!
19//! Burst mode (CR D6 and D7 clear when the frame starts) is held by the sequencer; the SAT
20//! transfer and VRAM DMA are allowed whenever the display is not drawing.
21//!
22//! While either runs, CPU VRAM accesses wait (BSY).
23//!
24//! Differences forced by Rust: `DmaEngine` is the state; its methods are `impl Vdc` here, with
25//! the reference's names behind `dma_`.
26
27use serde::{Deserialize, Serialize};
28
29use crate::vdc::chip::Vdc;
30use crate::vdc::regs as r;
31use crate::vdc::sequencer::VPhase;
32
33/// [emu: Mednafen "1024 VDC cycles"; Mesen2 the same]
34pub const SAT_DOTS: i64 = 1024;
35pub const DMA_DOTS_PER_WORD: i64 = 2;
36
37#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
38pub struct DmaEngine {
39    pub resume: bool,
40    /// a VRAM -> VRAM transfer has been started and not finished
41    pub running: bool,
42    read_phase: bool,
43    buffer: u16,
44    /// master-clock time up to which the transfer has been run
45    t0: i64,
46    pub satb_pending: bool,
47    /// the VDC is busy with the SAT transfer until then
48    pub sat_end: Option<i64>,
49}
50
51impl DmaEngine {
52    pub fn new(resume: bool) -> DmaEngine {
53        DmaEngine {
54            resume,
55            running: false,
56            read_phase: true,
57            buffer: 0,
58            t0: 0,
59            satb_pending: false,
60            sat_end: None,
61        }
62    }
63
64    /// CPU VRAM accesses wait while a transfer runs, paused or not.
65    #[inline]
66    pub fn busy(&self) -> bool {
67        self.running || self.sat_end.is_some()
68    }
69}
70
71impl Vdc {
72    /// The VRAM is free of display fetches (blanking, or burst mode).
73    fn dma_allowed(&self) -> bool {
74        self.dma.sat_end.is_none() && (self.seq.vphase != VPhase::Vdw || self.seq.burst_mode)
75    }
76
77    pub(crate) fn dma_next_event(&self) -> Option<i64> {
78        let mut time = self.dma.sat_end;
79        if self.dma.running && self.dma_allowed() {
80            let mut halves = (self.regs.value[r::LENR] as i64 + 1) * DMA_DOTS_PER_WORD;
81            if !self.dma.read_phase {
82                halves -= 1; // the read of the current word is done
83            }
84            let at = self.dma.t0 + halves * self.seq.divider;
85            time = Some(time.map_or(at, |t| t.min(at)));
86        }
87        time
88    }
89
90    // -- commands ---------------------------------------------------------------------------------
91
92    /// LENR's MSB was written.
93    pub(crate) fn dma_start(&mut self, now: i64) {
94        self.dma_advance(now);
95        self.dma.running = true;
96        self.dma.read_phase = true;
97        self.dma.t0 = now;
98    }
99
100    pub(crate) fn dma_arm_satb(&mut self) {
101        self.dma.satb_pending = true;
102    }
103
104    pub(crate) fn dma_display_started(&mut self, now: i64) {
105        self.dma_advance(now);
106        if self.dma.running && !self.seq.burst_mode && !self.dma.resume {
107            self.dma.running = false; // the manual's reading: the remainder is lost, no done IRQ
108            self.commit_pending();
109        }
110        self.dma.t0 = now;
111    }
112
113    /// The SAT transfer test of Mednafen's Run loop: after the display period.
114    pub(crate) fn dma_display_ended(&mut self, now: i64) {
115        self.dma_advance(now);
116        if self.dma.satb_pending || self.regs.value[r::DCR] & 0x10 != 0 {
117            self.dma.satb_pending = false;
118            let start = self.regs.value[r::SATB] as usize;
119            for i in 0..256 {
120                self.sat[i] = if start + i < self.vram.len() {
121                    self.vram[start + i]
122                } else {
123                    0
124                };
125            }
126            self.dma.sat_end = Some(now + SAT_DOTS * self.seq.divider);
127        } else {
128            self.seq.burst_mode = true;
129        }
130        self.dma.t0 = now;
131    }
132
133    // -- running ----------------------------------------------------------------------------------
134
135    /// Run what is due up to master-clock time `t`.
136    pub(crate) fn dma_advance(&mut self, t: i64) {
137        if let Some(end) = self.dma.sat_end {
138            if end <= t {
139                self.dma.sat_end = None;
140                if self.regs.value[r::DCR] & 0x01 != 0 {
141                    self.status |= r::DS;
142                }
143                self.seq.burst_mode = true;
144                self.commit_pending();
145                self.dma.t0 = end;
146            }
147        }
148        if !self.dma.running || !self.dma_allowed() {
149            if self.dma.running {
150                self.dma.t0 = t; // paused: no progress, the clock keeps moving
151            }
152            return;
153        }
154        let half = self.seq.divider; // one dot: the read, then the write, of each word
155        while self.dma.running && self.dma.t0 + half <= t {
156            if self.dma.read_phase {
157                let src = self.regs.value[r::SOUR] as usize;
158                self.dma.buffer = if src < self.vram.len() {
159                    self.vram[src]
160                } else {
161                    0
162                };
163                self.dma.t0 += half;
164                self.dma.read_phase = false;
165                continue;
166            }
167            let dst = self.regs.value[r::DESR] as usize;
168            if dst < self.vram.len() {
169                self.vram[dst] = self.dma.buffer;
170            }
171            let dcr = self.regs.value[r::DCR];
172            let regs = &mut self.regs.value;
173            regs[r::SOUR] = if dcr & 0x04 != 0 {
174                regs[r::SOUR].wrapping_sub(1)
175            } else {
176                regs[r::SOUR].wrapping_add(1)
177            };
178            regs[r::DESR] = if dcr & 0x08 != 0 {
179                regs[r::DESR].wrapping_sub(1)
180            } else {
181                regs[r::DESR].wrapping_add(1)
182            };
183            regs[r::LENR] = regs[r::LENR].wrapping_sub(1);
184            self.dma.t0 += half;
185            self.dma.read_phase = true;
186            if self.regs.value[r::LENR] == 0xFFFF {
187                self.dma.running = false;
188                self.commit_pending();
189                if dcr & 0x02 != 0 {
190                    self.status |= r::DV;
191                }
192            }
193        }
194    }
195
196    /// A CPU VRAM access finds the VDC busy for an unknown time (paused DMA): finish it, as
197    /// Mednafen does when nothing can wait for it (its 'DMA completion forced').
198    pub(crate) fn dma_force_complete(&mut self, now: i64) {
199        if let Some(end) = self.dma.sat_end {
200            self.dma_advance(end);
201        }
202        let saved = self.seq.burst_mode;
203        self.seq.burst_mode = true;
204        self.dma.t0 = now - 2 * 65536 * self.seq.divider * DMA_DOTS_PER_WORD;
205        self.dma_advance(now);
206        self.seq.burst_mode = saved;
207    }
208}