psx_cdstream/engine.rs
1//! The transport state machine ([`Engine`]) and its configuration and counters.
2
3use crate::hw::CdHw;
4use crate::request::{
5 Completion, Failure, Outcome, Priority, Request, RequestState, SubmitError, Ticket,
6 SECTOR_WORDS,
7};
8use psx_hw::cd::{
9 irq as flag, CMD_PAUSE, CMD_READN, CMD_SEEKL, CMD_SETLOC, CMD_SETMODE, MODE_DOUBLE_SPEED,
10};
11
12/// Requests that can wait behind the active one.
13pub const QUEUE_DEPTH: usize = 4;
14/// Finished requests whose results [`Engine::state`] still remembers.
15pub const RECENT_RESULTS: usize = 8;
16
17/// Where the controller is in a transfer. The numbers are published in
18/// `PSX_CD_STATS` and match the earlier game-local transports.
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20#[repr(u32)]
21pub enum Phase {
22 /// Nothing has run since the engine was created.
23 Idle = 0,
24 /// Setloc sent, waiting for its acknowledge.
25 SettingTarget = 1,
26 /// SeekL sent, waiting for its acknowledge.
27 StartingSeek = 2,
28 /// Seek acknowledged, waiting for it to complete.
29 Seeking = 3,
30 /// Setmode sent, waiting for its acknowledge.
31 SettingMode = 4,
32 /// ReadN sent, waiting for its acknowledge. Sectors may already arrive.
33 StartingRead = 5,
34 /// Sectors are arriving.
35 Reading = 6,
36 /// Pause sent, waiting for its acknowledge.
37 Stopping = 7,
38 /// Pause acknowledged, waiting for the drive to stop.
39 WaitingForStop = 8,
40 /// The drive is stopped and the last transfer ended cleanly.
41 Done = 9,
42 /// A transfer failed; the drive's state is unknown. The next transfer
43 /// starts with a recovery Pause.
44 Failed = 10,
45}
46
47impl Phase {
48 const fn is_terminal(self) -> bool {
49 matches!(self, Phase::Idle | Phase::Done | Phase::Failed)
50 }
51}
52
53/// Who may program the controller.
54#[derive(Clone, Copy, Debug, PartialEq, Eq)]
55pub enum Owner {
56 /// Nobody has attached the transport yet, or it was detached: the
57 /// program uses the blocking `SectorReader` or drives the controller
58 /// itself, and no request starts.
59 Boot,
60 /// The transport reads. Requests run.
61 Data,
62 /// CD-DA or XA playback holds the drive. Requests queue but do not start.
63 Audio,
64}
65
66/// Where an audio lease request stands.
67#[derive(Clone, Copy, Debug, PartialEq, Eq)]
68pub enum LeaseState {
69 /// No lease is held or wanted.
70 None,
71 /// Asked for; the in-flight request is being stopped.
72 Pending,
73 /// The drive is the audio code's. The transport masks its interrupt
74 /// source and starts nothing until the lease is released.
75 Granted,
76}
77
78/// Whether the CD interrupt source stays open between transfers.
79#[derive(Clone, Copy, Debug, PartialEq, Eq)]
80pub enum IrqMaskPolicy {
81 /// Open from attach until detach or an audio lease.
82 AlwaysOn,
83 /// Open only while a request is queued or active, so polled code
84 /// (the SDK's `SectorReader`) never shares the controller with the
85 /// handler between transfers.
86 OnlyWhileBusy,
87}
88
89/// How the engine behaves.
90#[derive(Clone, Copy, Debug, PartialEq, Eq)]
91pub struct Config {
92 /// A transfer with no sector, response or phase change for this many
93 /// VBlanks fails with [`FailureKind::Watchdog`](crate::FailureKind).
94 pub timeout_vblanks: u32,
95 /// See [`IrqMaskPolicy`].
96 pub irq_mask_policy: IrqMaskPolicy,
97 /// Start the first transfer after an audio lease with a recovery Pause,
98 /// so a drive that was playing or parked at an audio position is stopped
99 /// before the seek. Costs one command; skip it only if the audio code
100 /// always ends with its own Pause.
101 pub pause_after_audio: bool,
102 /// Measure how long each handler call takes, with Timer 2, which the
103 /// transport then owns. The longest call is `max_irq_ticks`, in
104 /// system-clock / 8 ticks that wrap after about 15.5 ms.
105 pub time_handler: bool,
106 /// Read at double speed (150 sectors per second, the default and the
107 /// only rate a streamer wants). Clear it for single speed (75 sectors
108 /// per second), which halves the CPU the sector pops cost per second of
109 /// reading; it applies from the next transfer.
110 pub double_speed: bool,
111}
112
113impl Config {
114 /// The defaults: 600 VBlanks, interrupt source always on, recovery pause
115 /// after audio, no handler timing, double speed.
116 pub const DEFAULT: Config = Config {
117 timeout_vblanks: 600,
118 irq_mask_policy: IrqMaskPolicy::AlwaysOn,
119 pause_after_audio: true,
120 time_handler: false,
121 double_speed: true,
122 };
123}
124
125impl Default for Config {
126 fn default() -> Self {
127 Self::DEFAULT
128 }
129}
130
131/// Counters, published as one block (`PSX_CD_STATS`) for tools that read
132/// guest memory. All wrap at 2^32.
133#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
134#[repr(C)]
135pub struct StreamStats {
136 /// Interrupts handled.
137 pub irq_count: u32,
138 /// Sectors stored at a destination.
139 pub sectors: u32,
140 /// Sectors popped and dropped: after a cancel, after the last sector of
141 /// a request while the Pause was on its way, or with nothing to store to.
142 pub discarded_sectors: u32,
143 /// The longest handler call, in Timer 2 ticks (needs
144 /// [`Config::time_handler`]).
145 pub max_irq_ticks: u32,
146 /// The current [`Phase`], as its number.
147 pub phase: u32,
148 /// The code of the latest failure ([`Failure::code`]); 0 after a start.
149 pub error: u32,
150 /// Requests that ended [`Outcome::Done`].
151 pub requests_done: u32,
152 /// Requests that ended [`Outcome::Cancelled`].
153 pub requests_cancelled: u32,
154 /// Requests that ended [`Outcome::Failed`].
155 pub requests_failed: u32,
156 /// Requests started by chaining onto the previous one, without a seek.
157 pub chained: u32,
158 /// Requests put back in the queue to resume after an error.
159 pub resumed: u32,
160 /// Submissions refused because the queue was full.
161 pub rejected: u32,
162}
163
164#[derive(Clone, Copy)]
165struct Active {
166 ticket: Ticket,
167 request: Request,
168 /// Sectors a request resumed from an error had stored before this
169 /// activation; `request` is only the rest.
170 base: u32,
171 received: u32,
172 cancelled: bool,
173 resumes_left: u8,
174}
175
176#[derive(Clone, Copy)]
177struct Queued {
178 ticket: Ticket,
179 request: Request,
180 /// Sectors already stored by earlier activations (see `Active::base`).
181 base: u32,
182 /// Submission order; resumed requests carry 0 so they go first within
183 /// their priority.
184 seq: u32,
185 resumes_left: u8,
186}
187
188/// The CD transport.
189///
190/// One engine owns the drive. Foreground code submits [`Request`]s; the CD
191/// interrupt handler calls [`Engine::on_interrupt`] once per interrupt, and
192/// each call advances the machine by one controller event. Nothing in the
193/// machine waits for the drive: a command is written and the machine returns,
194/// and the response arrives as the next interrupt.
195///
196/// # A transfer
197///
198/// ```text
199/// Setloc ──INT3──▶ SeekL ──INT3──▶ (seek) ──INT2──▶ Setmode(double speed)
200/// ──INT3──▶ ReadN ──INT3──▶ INT1 × n (one sector each) ──▶ Pause
201/// ──INT3──▶ (stop) ──INT2──▶ stopped
202/// ```
203///
204/// The read is seek-first (Setloc, SeekL, then Setmode and ReadN), the form
205/// the BIOS uses and the only one proven to stream byte-perfect on a console:
206/// a bare Setloc plus ReadN starts delivering while the mechanism is still
207/// settling. Each INT1 pops one sector into the request's destination. After
208/// a failure the drive's state is unknown, so the next transfer opens with a
209/// recovery Pause.
210///
211/// # Chaining
212///
213/// When the last sector of a request lands and the best queued request starts
214/// at the next sector, the machine swaps the destination inside that same
215/// interrupt and keeps reading: no Pause, no seek. A request is `Done` the
216/// moment its last sector lands, even though the drive is still running on
217/// to the Pause; [`Engine::is_idle`] says when it has stopped.
218///
219/// # Abort
220///
221/// [`Engine::cancel`] only sets a flag. The handler drops the next sector it
222/// pops and issues Pause itself. Pausing from the foreground while sectors
223/// keep arriving lost the Pause acknowledge on silicon (no interrupt for 600
224/// VBlanks), so the foreground never does it.
225///
226/// # Generic over the drive
227///
228/// On the console the one instance lives in the crate's global and is reached
229/// through the free functions at the crate root; on the host a scripted drive
230/// stands in.
231pub struct Engine<H: CdHw> {
232 hw: H,
233 config: Config,
234 owner: Owner,
235 lease_pending: bool,
236 /// Whether the CPU-side CD source is currently open, as last written.
237 source_open: bool,
238 /// The first transfer starts with a recovery Pause.
239 recover_next: bool,
240 phase: Phase,
241 /// Set while the Pause that opens a transfer after a failure is running.
242 recovering: bool,
243 error: u32,
244 /// VBlank count at the last sign of life.
245 stamp: u32,
246 /// Absolute BCD Setloc parameters of the active request.
247 location: [u8; 3],
248 active: Option<Active>,
249 /// One spare slot beyond [`QUEUE_DEPTH`]: a failing request that resumes
250 /// goes back in the queue even when it is full.
251 queue: [Option<Queued>; QUEUE_DEPTH + 1],
252 results: [Option<Completion>; RECENT_RESULTS],
253 results_written: u32,
254 next_ticket: u32,
255 next_seq: u32,
256 stats: StreamStats,
257}
258
259impl<H: CdHw> Engine<H> {
260 /// An engine in the [`Owner::Boot`] state: nothing runs until
261 /// [`attach`](Self::attach).
262 pub const fn new(hw: H, config: Config) -> Self {
263 Engine {
264 hw,
265 config,
266 owner: Owner::Boot,
267 lease_pending: false,
268 source_open: false,
269 recover_next: false,
270 phase: Phase::Idle,
271 recovering: false,
272 error: 0,
273 stamp: 0,
274 location: [0; 3],
275 active: None,
276 queue: [None; QUEUE_DEPTH + 1],
277 results: [None; RECENT_RESULTS],
278 results_written: 0,
279 next_ticket: 1,
280 next_seq: 1,
281 stats: StreamStats {
282 irq_count: 0,
283 sectors: 0,
284 discarded_sectors: 0,
285 max_irq_ticks: 0,
286 phase: 0,
287 error: 0,
288 requests_done: 0,
289 requests_cancelled: 0,
290 requests_failed: 0,
291 chained: 0,
292 resumed: 0,
293 rejected: 0,
294 },
295 }
296 }
297
298 /// Replace the configuration. It applies from the next event.
299 pub fn configure(&mut self, config: Config) {
300 self.config = config;
301 }
302
303 /// The drive layer.
304 pub fn hw(&self) -> &H {
305 &self.hw
306 }
307
308 /// The drive layer, mutably.
309 pub fn hw_mut(&mut self) -> &mut H {
310 &mut self.hw
311 }
312
313 /// Who may program the controller.
314 pub fn owner(&self) -> Owner {
315 self.owner
316 }
317
318 /// The controller phase.
319 pub fn phase(&self) -> Phase {
320 self.phase
321 }
322
323 /// A copy of the counters, with the current phase and error filled in.
324 pub fn stats(&self) -> StreamStats {
325 StreamStats {
326 phase: self.phase as u32,
327 error: self.error,
328 ..self.stats
329 }
330 }
331
332 /// Start the longest-handler measurement over
333 /// ([`StreamStats::max_irq_ticks`] to 0), to attribute the peak to the
334 /// next stretch of work.
335 pub fn reset_max_irq_ticks(&mut self) {
336 self.stats.max_irq_ticks = 0;
337 }
338
339 /// Requests waiting behind the active one.
340 pub fn queued_count(&self) -> usize {
341 self.queue.iter().flatten().count()
342 }
343
344 /// Whether the drive is stopped and no request holds it. Queued requests
345 /// may still be waiting for their turn (while the owner is not `Data`).
346 pub fn is_idle(&self) -> bool {
347 self.phase.is_terminal() && self.active.is_none()
348 }
349
350 // ----------------------------------------------------------------- owner
351
352 /// Take the drive: [`Owner::Boot`] becomes [`Owner::Data`] and queued
353 /// requests start.
354 ///
355 /// The controller must be in a clean state: the polled reader released,
356 /// no command in flight. A no-op in any other state.
357 pub fn attach(&mut self) {
358 if self.owner != Owner::Boot {
359 return;
360 }
361 self.owner = Owner::Data;
362 self.recover_next = false;
363 self.hw.discard_response();
364 self.hw.acknowledge(flag::ACK_ALL);
365 self.settle();
366 self.sync_source();
367 }
368
369 /// Hand the drive back to polled code. Succeeds only when it is idle and
370 /// nothing is queued or wanted; [`cancel_all`](Self::cancel_all) and wait
371 /// for [`is_idle`](Self::is_idle) first.
372 pub fn detach(&mut self) -> bool {
373 if self.owner != Owner::Data
374 || !self.is_idle()
375 || self.queued_count() != 0
376 || self.lease_pending
377 {
378 return false;
379 }
380 self.owner = Owner::Boot;
381 self.sync_source();
382 true
383 }
384
385 /// Where an audio lease stands.
386 pub fn lease_state(&self) -> LeaseState {
387 if self.owner == Owner::Audio {
388 LeaseState::Granted
389 } else if self.lease_pending {
390 LeaseState::Pending
391 } else {
392 LeaseState::None
393 }
394 }
395
396 /// Ask for the drive on behalf of CD-DA or XA playback.
397 ///
398 /// New requests are still accepted but queue. A request in flight is
399 /// aborted at its next sector (it ends [`Outcome::Cancelled`] with what
400 /// landed; resume it with [`Request::remaining_after`] after the lease).
401 /// When the drive has stopped the lease is granted: the CD interrupt
402 /// source is masked, and the audio code may program the controller
403 /// directly. Poll [`lease_state`](Self::lease_state) until it reads
404 /// [`LeaseState::Granted`] if this returns `Pending`.
405 ///
406 /// Before [`attach`](Self::attach) the program already holds the drive
407 /// and this returns `Granted` without changing anything.
408 pub fn request_audio_lease(&mut self) -> LeaseState {
409 match self.owner {
410 Owner::Boot => return LeaseState::Granted,
411 Owner::Audio => return LeaseState::Granted,
412 Owner::Data => {}
413 }
414 self.lease_pending = true;
415 if let Some(active) = self.active.as_mut() {
416 active.cancelled = true;
417 }
418 if self.is_idle() {
419 self.grant_lease();
420 }
421 self.sync_source();
422 self.lease_state()
423 }
424
425 /// Take the drive for audio only if it can be had right now, without
426 /// waiting and without leaving anything behind.
427 ///
428 /// Returns `true` when the lease is granted (the drive was idle, or the
429 /// program still holds it before [`attach`](Self::attach)). Returns
430 /// `false` when a request is in flight: nothing changes, no lease is left
431 /// pending and the request is not aborted, so a read submitted afterwards
432 /// cannot end up queued behind a lease that is granted later. Ask again
433 /// when the drive is idle.
434 ///
435 /// [`request_audio_lease`](Self::request_audio_lease) is for audio that
436 /// must displace a read; this is for audio that only wants the drive
437 /// while nobody else is using it.
438 pub fn try_audio_lease(&mut self) -> bool {
439 match self.owner {
440 Owner::Boot | Owner::Audio => true,
441 Owner::Data => {
442 if self.lease_pending || !self.is_idle() {
443 return false;
444 }
445 self.grant_lease();
446 self.sync_source();
447 true
448 }
449 }
450 }
451
452 /// Withdraw a lease that was asked for and not yet granted. Returns
453 /// whether one was pending.
454 ///
455 /// Call it before submitting a read that must not wait for audio: a
456 /// pending lease is granted the moment the drive stops, which parks every
457 /// read queued behind it until the audio code gives the drive back. The
458 /// request that was aborted for the lease still ends
459 /// [`Outcome::Cancelled`] (resume it with [`Request::remaining_after`]).
460 /// A lease that is already granted is ended with
461 /// [`release_audio_lease`](Self::release_audio_lease).
462 pub fn withdraw_audio_lease(&mut self) -> bool {
463 if self.owner == Owner::Data && self.lease_pending {
464 self.lease_pending = false;
465 return true;
466 }
467 false
468 }
469
470 /// Give the drive back after audio playback, or withdraw a lease that is
471 /// still pending. Returns whether there was a lease to end.
472 ///
473 /// The audio code must have ended playback with Pause (never Stop: the
474 /// motor then spins down, reports status 0 for a second or two, and reads
475 /// started in that window failed on a console). The next transfer starts
476 /// with a recovery Pause if [`Config::pause_after_audio`] is set.
477 pub fn release_audio_lease(&mut self) -> bool {
478 if self.withdraw_audio_lease() {
479 return true;
480 }
481 if self.owner != Owner::Audio {
482 return false;
483 }
484 self.hw.discard_response();
485 self.hw.acknowledge(flag::ACK_ALL);
486 self.owner = Owner::Data;
487 self.recover_next = self.config.pause_after_audio;
488 self.settle();
489 self.sync_source();
490 true
491 }
492
493 fn grant_lease(&mut self) {
494 self.owner = Owner::Audio;
495 self.lease_pending = false;
496 }
497
498 // -------------------------------------------------------------- requests
499
500 /// Queue a request. It starts at once when the drive is free.
501 ///
502 /// Higher priority (lower number) goes first; equal priorities go in
503 /// submission order, except that one starting at the sector after the
504 /// active request's last is picked ahead of its peers so it chains
505 /// without a seek.
506 pub fn submit(&mut self, request: Request) -> Result<Ticket, SubmitError> {
507 if request.destination.is_null() {
508 return Err(SubmitError::NullDestination);
509 }
510 if request.destination as usize & 3 != 0 {
511 return Err(SubmitError::MisalignedDestination);
512 }
513 if request.sectors == 0 {
514 return Err(SubmitError::Empty);
515 }
516 if self.queued_count() >= QUEUE_DEPTH {
517 self.stats.rejected = self.stats.rejected.wrapping_add(1);
518 return Err(SubmitError::QueueFull);
519 }
520 let ticket = self.new_ticket();
521 let seq = self.next_seq;
522 self.next_seq = self.next_seq.wrapping_add(1).max(1);
523 self.enqueue(Queued {
524 ticket,
525 request,
526 base: 0,
527 seq,
528 resumes_left: request.resumes,
529 });
530 self.service();
531 // A free drive picks the request up now; a busy one keeps it queued
532 // and `settle` runs when it frees up.
533 self.settle();
534 self.sync_source();
535 Ok(ticket)
536 }
537
538 /// Stop a request. Returns whether `ticket` was queued or active.
539 ///
540 /// A queued request is dropped at once ([`Outcome::Cancelled`], 0
541 /// sectors). The active one is flagged: the handler drops the next
542 /// sector, pauses the drive, and the request ends `Cancelled` with the
543 /// sectors that had landed.
544 pub fn cancel(&mut self, ticket: Ticket) -> bool {
545 if self.active.is_some_and(|active| active.ticket == ticket) {
546 self.cancel_active();
547 return true;
548 }
549 for index in 0..self.queue.len() {
550 if self.queue[index].is_some_and(|queued| queued.ticket == ticket) {
551 self.drop_queued(index);
552 return true;
553 }
554 }
555 false
556 }
557
558 /// [`cancel`](Self::cancel) everything: the queue and the active request.
559 pub fn cancel_all(&mut self) {
560 for index in 0..self.queue.len() {
561 self.drop_queued(index);
562 }
563 if self.active.is_some() {
564 self.cancel_active();
565 }
566 }
567
568 fn cancel_active(&mut self) {
569 self.trace(2, 0);
570 if let Some(active) = self.active.as_mut() {
571 active.cancelled = true;
572 }
573 }
574
575 fn drop_queued(&mut self, index: usize) {
576 if let Some(queued) = self.queue[index].take() {
577 self.record(Completion {
578 ticket: queued.ticket,
579 outcome: Outcome::Cancelled,
580 received: queued.base,
581 });
582 }
583 }
584
585 /// Where `ticket` is. Also runs the no-progress check ([`service`](Self::service)).
586 ///
587 /// The CD source is reopened only after the lookup. The console wrapper
588 /// closes the source before it hands the engine to foreground code, so the
589 /// handler cannot run meanwhile; reopening it first lets a pending
590 /// interrupt run the handler between the checks below, and a request that
591 /// the handler moves from the queue to the active slot in that gap is
592 /// found in neither and reported as [`RequestState::Unknown`].
593 pub fn state(&mut self, ticket: Ticket) -> RequestState {
594 self.service();
595 let state = self.lookup(ticket);
596 self.sync_source();
597 state
598 }
599
600 fn lookup(&self, ticket: Ticket) -> RequestState {
601 if let Some(active) = &self.active {
602 if active.ticket == ticket {
603 return RequestState::Active {
604 received: active.base + active.received,
605 };
606 }
607 }
608 if self.queue.iter().flatten().any(|q| q.ticket == ticket) {
609 return RequestState::Queued;
610 }
611 if let Some(done) = self.results.iter().flatten().find(|c| c.ticket == ticket) {
612 return RequestState::Finished(*done);
613 }
614 RequestState::Unknown
615 }
616
617 /// Foreground upkeep: fail a transfer that has made no progress for
618 /// [`Config::timeout_vblanks`]. [`state`](Self::state) calls this; a main
619 /// loop that never asks about a ticket should call it once per frame.
620 pub fn service(&mut self) {
621 if self.owner != Owner::Data || self.phase.is_terminal() {
622 return;
623 }
624 let now = self.hw.vblank_count();
625 if now.wrapping_sub(self.stamp) > self.config.timeout_vblanks {
626 self.fail(Failure::watchdog(self.phase as u32));
627 self.sync_source();
628 }
629 }
630
631 // ------------------------------------------------------------- interrupt
632
633 /// Handle one CD interrupt. Called by the interrupt handler (the crate's
634 /// exception wrapper on the console, the test harness on the host).
635 pub fn on_interrupt(&mut self) {
636 let started = self.hw.clock();
637 self.stats.irq_count = self.stats.irq_count.wrapping_add(1);
638 if self.owner == Owner::Data {
639 let code = self.hw.interrupt_code();
640 self.trace(1, code);
641 self.dispatch(code);
642 self.sync_source();
643 }
644 let ticks = u32::from(self.hw.clock().wrapping_sub(started));
645 self.stats.max_irq_ticks = self.stats.max_irq_ticks.max(ticks);
646 }
647
648 fn dispatch(&mut self, code: u8) {
649 if code == flag::DATA_READY {
650 match self.take_sector() {
651 Ok(()) => {
652 self.hw.discard_response();
653 self.hw.acknowledge(code);
654 self.after_sector();
655 }
656 Err(failure) => {
657 self.hw.discard_response();
658 self.hw.acknowledge(flag::ACK_ALL);
659 self.fail(failure);
660 }
661 }
662 return;
663 }
664 if code == flag::ERROR {
665 let (status, detail) = self.hw.error_response();
666 self.hw.discard_response();
667 self.hw.acknowledge(flag::ACK_ALL);
668 self.fail(Failure::drive(status, self.phase as u32, detail));
669 return;
670 }
671 self.hw.discard_response();
672 self.hw.acknowledge(code);
673 match (self.phase, code) {
674 (Phase::SettingTarget, flag::ACKNOWLEDGE) => {
675 self.command(CMD_SEEKL, &[], Phase::StartingSeek)
676 }
677 (Phase::StartingSeek, flag::ACKNOWLEDGE) => self.change(Phase::Seeking),
678 (Phase::Seeking, flag::COMPLETE) => {
679 let mode = if self.config.double_speed {
680 MODE_DOUBLE_SPEED
681 } else {
682 0
683 };
684 self.command(CMD_SETMODE, &[mode], Phase::SettingMode)
685 }
686 (Phase::SettingMode, flag::ACKNOWLEDGE) => {
687 self.command(CMD_READN, &[], Phase::StartingRead)
688 }
689 (Phase::StartingRead, flag::ACKNOWLEDGE) => {
690 self.change(Phase::Reading);
691 if self.active.is_some_and(|a| a.cancelled) {
692 self.pause();
693 }
694 }
695 (Phase::Stopping, flag::ACKNOWLEDGE) => self.change(Phase::WaitingForStop),
696 (Phase::WaitingForStop, flag::COMPLETE) => self.pause_complete(),
697 (_, 0) => {}
698 _ => self.fail(Failure::unexpected(self.phase as u32, code)),
699 }
700 }
701
702 /// Pop the sector the interrupt announced; store it when a request wants
703 /// it. Fails when the data FIFO never filled.
704 fn take_sector(&mut self) -> Result<(), Failure> {
705 let reading = matches!(self.phase, Phase::Reading | Phase::StartingRead);
706 let (destination, words) = match self.active {
707 Some(a) if reading && !a.cancelled && a.received < a.request.sectors => {
708 // SAFETY: `received < sectors`, so this stays inside the
709 // `sectors * 2048` bytes `Request::new_raw` vouched for.
710 let at = unsafe {
711 a.request
712 .destination
713 .add(a.received as usize * SECTOR_WORDS as usize)
714 };
715 (at, a.request.store_words(a.received))
716 }
717 _ => (core::ptr::null_mut(), 0),
718 };
719 // SAFETY: `destination` is the sector's slot inside the active
720 // request's buffer, valid for `words` words by the contract of
721 // `Request::new_raw` (and unused when `words` is 0).
722 if !unsafe { self.hw.pop_sector(destination, words) } {
723 return Err(Failure::data_not_ready(self.phase as u32));
724 }
725 if words != 0 {
726 if let Some(a) = self.active.as_mut() {
727 a.received += 1;
728 }
729 self.stats.sectors = self.stats.sectors.wrapping_add(1);
730 } else {
731 self.stats.discarded_sectors = self.stats.discarded_sectors.wrapping_add(1);
732 }
733 self.stamp = self.hw.vblank_count();
734 Ok(())
735 }
736
737 /// After a sector was popped and acknowledged: finish the request, chain
738 /// onto the next, or pause for a cancel.
739 fn after_sector(&mut self) {
740 if !matches!(self.phase, Phase::Reading | Phase::StartingRead) {
741 return;
742 }
743 let Some(active) = self.active else {
744 return;
745 };
746 if active.cancelled {
747 self.pause();
748 return;
749 }
750 if active.received < active.request.sectors {
751 return;
752 }
753 self.active = None;
754 self.finish(active, Outcome::Done);
755 match self.take_chain(active.request.end_lba()) {
756 Some(next) => {
757 self.start_chained(next);
758 self.stats.chained = self.stats.chained.wrapping_add(1);
759 }
760 None => self.pause(),
761 }
762 }
763
764 fn pause_complete(&mut self) {
765 if self.recovering && self.active.is_some_and(|a| !a.cancelled) {
766 // The recovery Pause is done: start the seek it was clearing the
767 // way for.
768 self.recovering = false;
769 let location = self.location;
770 self.hw.drop_data_request();
771 self.command(CMD_SETLOC, &location, Phase::SettingTarget);
772 return;
773 }
774 self.recovering = false;
775 self.change(Phase::Done);
776 self.hw.silence_output();
777 if let Some(active) = self.active.take() {
778 self.finish(active, Outcome::Cancelled);
779 }
780 self.settle();
781 }
782
783 // -------------------------------------------------------------- stepping
784
785 fn change(&mut self, phase: Phase) {
786 self.phase = phase;
787 self.stamp = self.hw.vblank_count();
788 }
789
790 /// Send a command; its acknowledge and completion arrive as interrupts.
791 fn command(&mut self, command: u8, params: &[u8], next: Phase) {
792 self.trace(6, command);
793 if !self.hw.issue(command, params) {
794 self.fail(Failure::command_refused(command));
795 return;
796 }
797 self.change(next);
798 }
799
800 fn pause(&mut self) {
801 self.command(CMD_PAUSE, &[], Phase::Stopping);
802 }
803
804 /// End the transfer in the failed state. The active request resumes from
805 /// where it stopped if it has resumes left, else it ends `Failed`.
806 fn fail(&mut self, failure: Failure) {
807 self.trace(4, (failure.0 >> 24) as u8);
808 self.trace(5, (failure.0 >> 8) as u8);
809 self.error = failure.0;
810 self.recovering = false;
811 self.change(Phase::Failed);
812 self.hw.silence_output();
813 self.hw.acknowledge(0);
814 if let Some(active) = self.active.take() {
815 if active.cancelled {
816 self.finish(active, Outcome::Cancelled);
817 } else if active.resumes_left > 0 && active.received < active.request.sectors {
818 self.requeue_remainder(active);
819 } else {
820 self.finish(active, Outcome::Failed(failure));
821 }
822 }
823 self.settle();
824 }
825
826 fn requeue_remainder(&mut self, active: Active) {
827 // SAFETY: `received` is what this transfer stored, so the tail
828 // begins inside the request's destination (`Request::new_raw`).
829 let rest = unsafe { active.request.remaining_after(active.received) };
830 if let Some(request) = rest {
831 self.stats.resumed = self.stats.resumed.wrapping_add(1);
832 self.enqueue(Queued {
833 ticket: active.ticket,
834 request,
835 base: active.base + active.received,
836 seq: 0,
837 resumes_left: active.resumes_left - 1,
838 });
839 }
840 }
841
842 /// The drive has stopped (or is in an unknown state after a failure) and
843 /// no request holds it: grant a waiting lease, start the next request, or
844 /// stay idle.
845 fn settle(&mut self) {
846 if self.owner != Owner::Data || !self.is_ready_to_start() {
847 return;
848 }
849 if self.lease_pending {
850 self.grant_lease();
851 return;
852 }
853 if let Some(index) = self.select(None) {
854 if let Some(queued) = self.queue[index].take() {
855 self.begin(queued);
856 }
857 }
858 }
859
860 fn is_ready_to_start(&self) -> bool {
861 self.active.is_none() && matches!(self.phase, Phase::Idle | Phase::Done | Phase::Failed)
862 }
863
864 /// Start a transfer from a stopped drive.
865 fn begin(&mut self, queued: Queued) {
866 let recover = self.phase == Phase::Failed || self.recover_next;
867 self.recover_next = false;
868 self.error = 0;
869 self.active = Some(Active {
870 ticket: queued.ticket,
871 request: queued.request,
872 base: queued.base,
873 received: 0,
874 cancelled: false,
875 resumes_left: queued.resumes_left,
876 });
877 self.location =
878 psx_io::cd::lba_to_bcd_msf(psx_io::disc_base::shift_lba(queued.request.lba));
879 self.trace(3, 0);
880 if recover {
881 // The drive may still be reading or playing: stop it, then seek.
882 self.recovering = true;
883 self.pause();
884 } else {
885 self.recovering = false;
886 self.hw.drop_data_request();
887 let location = self.location;
888 self.command(CMD_SETLOC, &location, Phase::SettingTarget);
889 }
890 }
891
892 /// Continue reading into the next request with no command in between.
893 fn start_chained(&mut self, queued: Queued) {
894 self.active = Some(Active {
895 ticket: queued.ticket,
896 request: queued.request,
897 base: queued.base,
898 received: 0,
899 cancelled: false,
900 resumes_left: queued.resumes_left,
901 });
902 self.stamp = self.hw.vblank_count();
903 }
904
905 /// The queued request to chain onto a request that ended just before
906 /// `end_lba`: the best-ranked one, if it starts exactly there.
907 fn take_chain(&mut self, end_lba: u32) -> Option<Queued> {
908 // No owner or lease check: the handler only runs for the data owner,
909 // and a lease request aborts the active request before it can end.
910 let index = self.select(Some(end_lba))?;
911 if self.queue[index]?.request.lba != end_lba {
912 return None;
913 }
914 self.queue[index].take()
915 }
916
917 /// Index of the best queued request: most urgent priority first, then
918 /// one that continues from `contiguous_with`, then submission order.
919 fn select(&self, contiguous_with: Option<u32>) -> Option<usize> {
920 let rank = |q: &Queued| -> (Priority, bool, u32) {
921 (
922 q.request.priority,
923 contiguous_with != Some(q.request.lba),
924 q.seq,
925 )
926 };
927 let mut best: Option<usize> = None;
928 for (index, slot) in self.queue.iter().enumerate() {
929 let Some(candidate) = slot else {
930 continue;
931 };
932 let better = match best.and_then(|b| self.queue[b].as_ref()) {
933 None => true,
934 Some(current) => rank(candidate) < rank(current),
935 };
936 if better {
937 best = Some(index);
938 }
939 }
940 best
941 }
942
943 fn enqueue(&mut self, queued: Queued) {
944 if let Some(slot) = self.queue.iter_mut().find(|slot| slot.is_none()) {
945 *slot = Some(queued);
946 }
947 }
948
949 fn new_ticket(&mut self) -> Ticket {
950 let ticket = Ticket(self.next_ticket);
951 self.next_ticket = self.next_ticket.wrapping_add(1).max(1);
952 ticket
953 }
954
955 fn finish(&mut self, active: Active, outcome: Outcome) {
956 match outcome {
957 Outcome::Done => self.stats.requests_done = self.stats.requests_done.wrapping_add(1),
958 Outcome::Cancelled => {
959 self.stats.requests_cancelled = self.stats.requests_cancelled.wrapping_add(1)
960 }
961 Outcome::Failed(_) => {
962 self.stats.requests_failed = self.stats.requests_failed.wrapping_add(1)
963 }
964 }
965 self.record(Completion {
966 ticket: active.ticket,
967 outcome,
968 received: active.base + active.received,
969 });
970 }
971
972 fn record(&mut self, completion: Completion) {
973 let slot = self.results_written as usize % RECENT_RESULTS;
974 self.results[slot] = Some(completion);
975 self.results_written = self.results_written.wrapping_add(1);
976 }
977
978 // ------------------------------------------------------------ the source
979
980 /// Whether the CD interrupt source should reach the CPU right now.
981 pub fn source_wanted(&self) -> bool {
982 match self.owner {
983 Owner::Boot | Owner::Audio => false,
984 Owner::Data => match self.config.irq_mask_policy {
985 IrqMaskPolicy::AlwaysOn => true,
986 IrqMaskPolicy::OnlyWhileBusy => {
987 !self.is_idle() || self.queue.iter().any(Option::is_some)
988 }
989 },
990 }
991 }
992
993 /// Close the CPU-side CD source. The console wrapper calls this before it
994 /// touches the engine from the foreground, so the handler cannot run
995 /// meanwhile; every foreground method ends by opening it again if
996 /// [`source_wanted`](Self::source_wanted).
997 pub fn close_source(&mut self) {
998 self.hw.set_source_enabled(false);
999 self.source_open = false;
1000 }
1001
1002 /// Record that the source was closed behind the engine's back (by the
1003 /// console wrapper, through `I_MASK` directly), so the next
1004 /// [`sync_source`](Self::sync_source) reopens it.
1005 pub fn note_source_closed(&mut self) {
1006 self.source_open = false;
1007 }
1008
1009 /// Open or close the source to match [`source_wanted`](Self::source_wanted).
1010 pub fn sync_source(&mut self) {
1011 let wanted = self.source_wanted();
1012 if wanted != self.source_open {
1013 self.hw.set_source_enabled(wanted);
1014 self.source_open = wanted;
1015 }
1016 }
1017
1018 #[inline(always)]
1019 fn trace(&mut self, kind: u32, value: u8) {
1020 if H::TRACE {
1021 let received = self.active.map_or(0, |a| a.received);
1022 self.hw.trace(
1023 kind << 24 | (self.phase as u32) << 16 | u32::from(value) << 8 | received & 0xff,
1024 );
1025 }
1026 }
1027}