1pub mod audio;
16pub mod pause;
17pub mod reader;
18pub mod xa;
19pub mod xa_clock;
20
21use crate::periph::Cd;
22use crate::{irq, read_u8, write_u8};
23
24use psx_hw::cd::{
25 index_status as status, irq as code, reg, request, volume_apply, CMD_DEMUTE, CMD_GETLOCP,
26 CMD_GETSTAT, CMD_MUTE, CMD_PAUSE, CMD_PLAY, CMD_READN, CMD_SETLOC, CMD_SETMODE, CMD_STOP,
27 STAT_PLAYING, STAT_SEEKING,
28};
29
30const SECTOR_POLL_DRAIN_LIMIT: u32 = 256;
33
34#[derive(Copy, Clone, Debug, PartialEq, Eq)]
36pub struct Response {
37 bytes: [u8; 16],
38 len: usize,
39}
40
41impl Response {
42 pub const fn empty() -> Self {
45 Response {
46 bytes: [0; 16],
47 len: 0,
48 }
49 }
50
51 pub const fn len(&self) -> usize {
53 self.len
54 }
55
56 pub const fn is_empty(&self) -> bool {
58 self.len == 0
59 }
60
61 pub fn bytes(&self) -> &[u8] {
63 &self.bytes[..self.len]
64 }
65}
66
67#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69pub enum CdError {
70 Timeout,
73 DriveError,
75}
76
77pub const DEFAULT_COMMAND_SPINS: u32 = 131_072;
82
83impl Cd {
84 pub fn command(&mut self, command: u8, params: &[u8]) -> Result<Response, CdError> {
91 self.command_within(command, params, DEFAULT_COMMAND_SPINS)
92 }
93
94 pub fn command_within(
96 &mut self,
97 command: u8,
98 params: &[u8],
99 spin_limit: u32,
100 ) -> Result<Response, CdError> {
101 run_command(&mut Mmio, command, params, spin_limit)
102 }
103
104 pub fn try_command(&mut self, command: u8, params: &[u8], spin_limit: u32) -> Option<Response> {
113 self.command_within(command, params, spin_limit).ok()
114 }
115
116 pub fn irq_flag_value(&mut self) -> u8 {
124 irq_flag()
125 }
126
127 pub fn acknowledge_irq(&mut self, bits: u8) {
129 ack_irq(bits);
130 }
131
132 pub fn discard_response(&mut self) {
134 drain_response_fifo();
135 }
136
137 pub fn dispatch_command(&mut self, command: u8, params: &[u8], spin_limit: u32) -> Option<u8> {
146 let irq_enable = begin_polled_command();
147 select_index(0);
148 for ¶m in params {
149 if !wait_param_room_bounded(spin_limit) {
150 finish_failed_polled_command(irq_enable);
151 return None;
152 }
153 write_byte(reg::PARAMETER, param);
154 }
155 crate::diag::cd_command(command);
156 write_byte(reg::COMMAND_RESPONSE, command);
157 Some(irq_enable)
158 }
159
160 pub fn restore_irq_output(&mut self, saved: u8) {
162 drain_response_fifo();
163 ack_irq(code::ACK_ALL);
164 restore_irq_enable(saved);
165 select_index(0);
166 }
167
168 pub fn poll_data_sector(&mut self) -> Result<bool, SectorPollError> {
173 poll_sector(&mut SectorPollMmio)
174 }
175
176 pub fn try_wait_data_sector(&mut self, spin_limit: u32) -> bool {
183 let mut spins = spin_limit;
184 loop {
185 let flag = irq_flag();
186 if flag == code::DATA_READY {
187 ack_irq(flag);
188 return true;
189 }
190 if flag == code::ERROR {
191 let _ = read_response_fifo();
192 ack_irq(flag);
193 return false;
194 }
195 if flag != 0 {
196 let _ = read_response_fifo();
197 ack_irq(flag);
198 }
199 if spins == 0 {
200 return false;
201 }
202 spins -= 1;
203 core::hint::spin_loop();
204 }
205 }
206
207 #[doc(alias = "Getstat")]
209 #[doc(alias = "CdlNop")]
210 pub fn status(&mut self) -> Result<Response, CdError> {
211 self.command(CMD_GETSTAT, &[])
212 }
213
214 #[doc(alias = "Getstat")]
216 pub fn try_status(&mut self, spin_limit: u32) -> Option<Response> {
217 self.try_command(CMD_GETSTAT, &[], spin_limit)
218 }
219
220 pub fn set_mode(&mut self, mode: u8) -> Result<Response, CdError> {
222 self.command(CMD_SETMODE, &[mode])
223 }
224
225 pub fn try_set_mode(&mut self, mode: u8, spin_limit: u32) -> Option<Response> {
227 self.try_command(CMD_SETMODE, &[mode], spin_limit)
228 }
229
230 #[doc(alias = "Setloc")]
232 #[doc(alias = "CdlSetloc")]
233 pub fn try_set_target_lba(&mut self, lba: u32, spin_limit: u32) -> Option<Response> {
234 let [minute, second, frame] = lba_to_bcd_msf(lba);
235 self.try_command(CMD_SETLOC, &[minute, second, frame], spin_limit)
236 }
237
238 #[doc(alias = "ReadN")]
240 #[doc(alias = "CdlReadN")]
241 pub fn try_start_reading(&mut self, spin_limit: u32) -> Option<Response> {
242 self.try_command(CMD_READN, &[], spin_limit)
243 }
244
245 #[doc(alias = "Demute")]
247 #[doc(alias = "CdlDemute")]
248 pub fn unmute(&mut self) -> Result<Response, CdError> {
249 self.command(CMD_DEMUTE, &[])
250 }
251
252 #[doc(alias = "Demute")]
254 pub fn try_unmute(&mut self, spin_limit: u32) -> Option<Response> {
255 self.try_command(CMD_DEMUTE, &[], spin_limit)
256 }
257
258 pub fn mute(&mut self) -> Result<Response, CdError> {
260 self.command(CMD_MUTE, &[])
261 }
262
263 pub fn try_mute(&mut self, spin_limit: u32) -> Option<Response> {
265 self.try_command(CMD_MUTE, &[], spin_limit)
266 }
267
268 pub fn play_track(&mut self, track: u8) -> Result<Response, CdError> {
273 self.command(
274 CMD_PLAY,
275 &[bin_to_bcd(crate::disc_base::shift_track(track))],
276 )
277 }
278
279 pub fn try_play_track(&mut self, track: u8, spin_limit: u32) -> Option<Response> {
282 self.try_command(
283 CMD_PLAY,
284 &[bin_to_bcd(crate::disc_base::shift_track(track))],
285 spin_limit,
286 )
287 }
288
289 pub fn pause(&mut self) -> Result<Response, CdError> {
291 self.command(CMD_PAUSE, &[])
292 }
293
294 pub fn try_pause(&mut self, spin_limit: u32) -> Option<Response> {
296 self.try_command(CMD_PAUSE, &[], spin_limit)
297 }
298
299 pub fn try_pause_until_complete(&mut self, spin_limit: u32) -> bool {
312 try_command_until_complete_inner(CMD_PAUSE, &[], spin_limit)
313 }
314
315 pub fn stop(&mut self) -> Result<Response, CdError> {
317 self.command(CMD_STOP, &[])
318 }
319
320 pub fn try_stop(&mut self, spin_limit: u32) -> Option<Response> {
330 self.try_command(CMD_STOP, &[], spin_limit)
331 }
332
333 pub fn stop_and_settle(&mut self, spin_limit: u32, max_polls: u32) -> bool {
342 let _ = self.try_stop(spin_limit);
343 let mut settled = 0u8;
344 for _ in 0..max_polls {
345 for _ in 0..20_000u32 {
349 unsafe { core::ptr::read_volatile(reg::INDEX as *const u8) };
352 }
353 let quiet = match self.try_status(spin_limit) {
354 Some(r) => r
355 .bytes()
356 .first()
357 .is_some_and(|s| s & (STAT_PLAYING | STAT_SEEKING) == 0),
358 None => false,
359 };
360 settled = if quiet { settled + 1 } else { 0 };
361 if settled >= 2 {
362 return true;
363 }
364 }
365 false
366 }
367
368 #[doc(alias = "GetlocP")]
372 #[doc(alias = "CdlGetlocP")]
373 pub fn play_position(&mut self) -> Result<Response, CdError> {
374 self.command(CMD_GETLOCP, &[])
375 }
376
377 #[doc(alias = "GetlocP")]
379 pub fn try_play_position(&mut self, spin_limit: u32) -> Option<Response> {
380 self.try_command(CMD_GETLOCP, &[], spin_limit)
381 }
382
383 pub fn try_command_until_complete(
391 &mut self,
392 command: u8,
393 params: &[u8],
394 spin_limit: u32,
395 ) -> bool {
396 try_command_until_complete_inner(command, params, spin_limit)
397 }
398
399 pub fn set_audio_mixer(
404 &mut self,
405 left_to_left: u8,
406 left_to_right: u8,
407 right_to_right: u8,
408 right_to_left: u8,
409 ) {
410 select_index(2);
411 write_byte(reg::PARAMETER, left_to_left);
412 write_byte(reg::REQUEST_IRQ, left_to_right);
413 select_index(3);
414 write_byte(reg::COMMAND_RESPONSE, right_to_right);
415 write_byte(reg::PARAMETER, right_to_left);
416 write_byte(reg::REQUEST_IRQ, volume_apply::APPLY);
418 select_index(0);
419 }
420}
421
422impl Cd {
429 pub fn acknowledge_all_and_reset_parameters(&mut self) {
432 ack_all_and_reset_parameters();
433 }
434
435 pub fn irq_enable_mask(&mut self) -> u8 {
437 irq_enable()
438 }
439
440 pub fn set_irq_enable_mask(&mut self, mask: u8) {
442 set_irq_enable(mask);
443 }
444
445 pub fn drain_response_limited(&mut self, limit: u32) {
448 drain_response_limited(limit);
449 }
450
451 pub fn is_data_fifo_ready(&mut self) -> bool {
453 data_fifo_ready()
454 }
455
456 pub fn wait_parameter_room(&mut self, spins: u32) -> bool {
459 wait_param_room_bounded(spins)
460 }
461
462 pub fn reset_parameter_fifo(&mut self) {
464 clear_parameter_fifo();
465 }
466
467 pub fn send_parameter_byte(&mut self, byte: u8) {
469 write_byte(reg::PARAMETER, byte);
470 }
471
472 pub fn send_command_byte(&mut self, byte: u8) {
474 crate::diag::cd_command(byte);
475 write_byte(reg::COMMAND_RESPONSE, byte);
476 }
477
478 pub fn read_response_byte(&mut self) -> u8 {
480 read_byte(reg::COMMAND_RESPONSE)
481 }
482
483 pub fn status_register(&mut self) -> u8 {
485 read_status()
486 }
487
488 #[doc(alias = "BFRD")]
492 pub fn request_data(&mut self) {
493 write_byte(reg::REQUEST_IRQ, request::WANT_DATA);
494 }
495
496 pub fn clear_data_request(&mut self) {
498 write_byte(reg::REQUEST_IRQ, 0);
499 }
500
501 pub fn read_data_byte(&mut self) -> u8 {
503 read_byte(reg::PARAMETER)
504 }
505}
506
507trait CommandIo {
510 fn begin(&mut self) -> u8;
511 fn wait_param_room(&mut self, spins: u32) -> bool;
512 fn write_param(&mut self, value: u8);
513 fn write_command(&mut self, command: u8);
514 fn wait_irq(&mut self, expected: u8, spins: u32) -> Result<u8, CdError>;
515 fn finish(&mut self, irq_enable: u8, irq: u8) -> Response;
516 fn recover(&mut self, irq_enable: u8);
519 fn restore_enable(&mut self, irq_enable: u8);
521}
522
523struct Mmio;
524
525impl CommandIo for Mmio {
526 fn begin(&mut self) -> u8 {
527 begin_polled_command()
528 }
529 fn wait_param_room(&mut self, spins: u32) -> bool {
530 wait_param_room_bounded(spins)
531 }
532 fn write_param(&mut self, value: u8) {
533 write_byte(reg::PARAMETER, value);
534 }
535 fn write_command(&mut self, command: u8) {
536 crate::diag::cd_command(command);
537 write_byte(reg::COMMAND_RESPONSE, command);
538 }
539 fn wait_irq(&mut self, expected: u8, spins: u32) -> Result<u8, CdError> {
540 wait_irq_flag(expected, spins, irq_flag, |flag| {
541 let _ = read_response_fifo();
542 ack_irq(flag);
543 })
544 }
545 fn finish(&mut self, irq_enable: u8, irq: u8) -> Response {
546 finish_polled_command(irq_enable, irq)
547 }
548 fn recover(&mut self, irq_enable: u8) {
549 finish_failed_polled_command(irq_enable);
550 }
551 fn restore_enable(&mut self, irq_enable: u8) {
552 restore_irq_enable(irq_enable);
553 select_index(0);
554 }
555}
556
557fn run_command(
558 io: &mut impl CommandIo,
559 command: u8,
560 params: &[u8],
561 spin_limit: u32,
562) -> Result<Response, CdError> {
563 let irq_enable = io.begin();
564 for ¶m in params {
565 if !io.wait_param_room(spin_limit) {
566 io.restore_enable(irq_enable);
568 return Err(CdError::Timeout);
569 }
570 io.write_param(param);
571 }
572 io.write_command(command);
573 match io.wait_irq(code::ACKNOWLEDGE, spin_limit) {
574 Ok(irq) => Ok(io.finish(irq_enable, irq)),
575 Err(CdError::DriveError) => {
576 io.recover(irq_enable);
577 Err(CdError::DriveError)
578 }
579 Err(CdError::Timeout) => Err(CdError::Timeout),
582 }
583}
584
585#[derive(Clone, Copy, Debug, PartialEq, Eq)]
587pub struct SectorPollError;
588
589trait SectorPollIo {
590 fn flag(&mut self) -> u8;
591 fn drain(&mut self);
592 fn acknowledge(&mut self, flag: u8, reset: bool);
593 fn data_ready(&mut self) -> bool;
594}
595struct SectorPollMmio;
596impl SectorPollIo for SectorPollMmio {
597 #[inline]
598 fn flag(&mut self) -> u8 {
599 irq_flag()
600 }
601 #[inline]
602 fn drain(&mut self) {
603 drain_response_limited(SECTOR_POLL_DRAIN_LIMIT);
604 }
605 #[inline]
606 fn acknowledge(&mut self, flag: u8, reset: bool) {
607 if reset {
608 ack_all_and_reset_parameters();
609 } else {
610 ack_irq(flag);
611 }
612 }
613 #[inline]
614 fn data_ready(&mut self) -> bool {
615 data_fifo_ready()
616 }
617}
618#[inline]
619fn poll_sector(io: &mut impl SectorPollIo) -> Result<bool, SectorPollError> {
620 match io.flag() {
621 code::DATA_READY => Ok(true),
622 code::ERROR => {
623 io.drain();
624 io.acknowledge(code::ACK_ALL, true);
625 Err(SectorPollError)
626 }
627 flag @ (code::COMPLETE | code::ACKNOWLEDGE | code::DATA_END) => {
628 io.drain();
629 io.acknowledge(flag, false);
630 Ok(false)
631 }
632 _ => Ok(io.data_ready()),
633 }
634}
635
636pub const fn lba_to_bcd_msf(lba: u32) -> [u8; 3] {
640 let absolute = lba.saturating_add(150);
641 let raw_minute = absolute / (60 * 75);
642 let minute = if raw_minute > 99 {
643 99
644 } else {
645 raw_minute as u8
646 };
647 let second = ((absolute / 75) % 60) as u8;
648 let frame = (absolute % 75) as u8;
649 [bin_to_bcd(minute), bin_to_bcd(second), bin_to_bcd(frame)]
650}
651
652pub const fn bin_to_bcd(v: u8) -> u8 {
654 let v = if v > 99 { 99 } else { v };
655 ((v / 10) << 4) | (v % 10)
656}
657
658pub const fn bcd_to_bin(v: u8) -> u8 {
661 (v >> 4) * 10 + (v & 0x0f)
662}
663
664#[derive(Clone, Copy, PartialEq, Eq, Debug)]
669pub struct PlayPosition {
670 pub track: u8,
672 pub index: u8,
674 pub relative_min: u8,
676 pub relative_sec: u8,
678 pub relative_frame: u8,
680 pub absolute_min: u8,
682 pub absolute_sec: u8,
684 pub absolute_frame: u8,
686}
687
688impl PlayPosition {
689 pub fn parse(resp: &Response) -> Option<Self> {
693 let b = resp.bytes();
694 if b.len() < 8 {
695 return None;
696 }
697 Some(PlayPosition {
698 track: bcd_to_bin(b[0]),
699 index: b[1],
700 relative_min: bcd_to_bin(b[2]),
701 relative_sec: bcd_to_bin(b[3]),
702 relative_frame: bcd_to_bin(b[4]),
703 absolute_min: bcd_to_bin(b[5]),
704 absolute_sec: bcd_to_bin(b[6]),
705 absolute_frame: bcd_to_bin(b[7]),
706 })
707 }
708
709 pub fn relative_sectors(&self) -> u32 {
711 (self.relative_min as u32 * 60 + self.relative_sec as u32) * 75 + self.relative_frame as u32
712 }
713
714 pub fn relative_millis(&self) -> u32 {
716 self.relative_sectors() * 1000 / 75
717 }
718}
719
720fn begin_polled_command() -> u8 {
721 let irq_enable = irq_enable();
722 set_irq_enable(0);
723 ack_irq(code::ACK_ALL);
724 select_index(0);
725 drain_response_fifo();
726 clear_parameter_fifo();
727 irq_enable
728}
729
730fn finish_polled_command(irq_enable: u8, irq: u8) -> Response {
731 let response = read_response_fifo();
732 ack_irq(irq);
733 restore_irq_enable(irq_enable);
734 select_index(0);
735 response
736}
737
738fn wait_irq_flag(
742 expected: u8,
743 mut spins: u32,
744 mut flag: impl FnMut() -> u8,
745 mut discard: impl FnMut(u8),
746) -> Result<u8, CdError> {
747 loop {
748 let irq = flag();
749 if irq == expected {
750 return Ok(irq);
751 }
752 if irq == code::ERROR {
753 return Err(CdError::DriveError);
754 }
755 if irq != 0 {
756 discard(irq);
757 }
758 if spins == 0 {
759 return Err(CdError::Timeout);
760 }
761 spins -= 1;
762 core::hint::spin_loop();
763 }
764}
765
766fn wait_irq_bounded(expected: u8, spins: u32) -> Option<u8> {
767 Mmio.wait_irq(expected, spins).ok()
768}
769
770fn try_command_until_complete_inner(command: u8, params: &[u8], spin_limit: u32) -> bool {
771 let irq_enable = begin_polled_command();
772 select_index(0);
773 for ¶m in params {
774 if !wait_param_room_bounded(spin_limit) {
775 finish_failed_polled_command(irq_enable);
776 return false;
777 }
778 write_byte(reg::PARAMETER, param);
779 }
780 crate::diag::cd_command(command);
781 write_byte(reg::COMMAND_RESPONSE, command);
782
783 let ok = wait_ack_then_complete(spin_limit);
784 drain_response_fifo();
785 ack_irq(code::ACK_ALL);
786 restore_irq_enable(irq_enable);
787 select_index(0);
788 ok
789}
790
791fn wait_ack_then_complete(spin_limit: u32) -> bool {
792 if wait_irq_bounded(code::ACKNOWLEDGE, spin_limit).is_none() {
793 return false;
794 }
795 drain_response_fifo();
796 ack_irq(code::ACKNOWLEDGE);
797
798 if wait_irq_bounded(code::COMPLETE, spin_limit).is_none() {
799 return false;
800 }
801 drain_response_fifo();
802 ack_irq(code::COMPLETE);
803 true
804}
805
806fn finish_failed_polled_command(irq_enable: u8) {
807 drain_response_fifo();
808 ack_irq(code::ACK_ALL);
809 restore_irq_enable(irq_enable);
810 select_index(0);
811}
812
813fn read_response_fifo() -> Response {
814 select_index(0);
815
816 let mut bytes = [0u8; 16];
817 let mut len = 0;
818 while read_status() & status::RESPONSE_NOT_EMPTY != 0 && len < bytes.len() {
819 bytes[len] = read_byte(reg::COMMAND_RESPONSE);
820 len += 1;
821 }
822 Response { bytes, len }
823}
824
825fn drain_response_fifo() {
826 let _ = read_response_fifo();
827}
828
829fn wait_param_room_bounded(mut spins: u32) -> bool {
830 while read_status() & status::PARAM_NOT_FULL == 0 {
831 if spins == 0 {
832 return false;
833 }
834 spins -= 1;
835 core::hint::spin_loop();
836 }
837 true
838}
839
840fn drain_response_limited(limit: u32) {
842 select_index(0);
843 let mut drained = 0;
844 while read_status() & status::RESPONSE_NOT_EMPTY != 0 && drained < limit {
845 let _ = read_byte(reg::COMMAND_RESPONSE);
846 drained += 1;
847 }
848}
849
850fn ack_all_and_reset_parameters() {
853 select_index(1);
854 write_byte(reg::REQUEST_IRQ, code::ACK_ALL | code::CLEAR_PARAMETER_FIFO);
855 irq::acknowledge(1 << psx_hw::irq::source::CDROM);
856 select_index(0);
857}
858
859fn data_fifo_ready() -> bool {
860 select_index(0);
861 read_status() & status::DATA_FIFO_NOT_EMPTY != 0
862}
863
864fn clear_parameter_fifo() {
865 select_index(1);
866 write_byte(reg::REQUEST_IRQ, code::CLEAR_PARAMETER_FIFO);
867 select_index(0);
868}
869
870fn ack_irq(bits: u8) {
871 crate::diag::cd_irq_acked(bits);
872 select_index(1);
873 write_byte(reg::REQUEST_IRQ, bits & code::ACK_ALL);
874 irq::acknowledge(1 << psx_hw::irq::source::CDROM);
875 select_index(0);
876}
877
878fn irq_flag() -> u8 {
879 select_index(1);
880 let flag = read_byte(reg::REQUEST_IRQ) & code::ACK_ALL;
881 select_index(0);
882 crate::diag::cd_irq_seen(flag);
883 flag
884}
885
886fn irq_enable() -> u8 {
887 select_index(0);
888 let enable = read_byte(reg::REQUEST_IRQ) & code::ACK_ALL;
889 select_index(0);
890 enable
891}
892
893fn restore_irq_enable(enable: u8) {
894 set_irq_enable(enable);
895}
896
897fn set_irq_enable(enable: u8) {
898 select_index(1);
899 write_byte(reg::PARAMETER, enable & code::ACK_ALL);
900 select_index(0);
901}
902
903fn read_status() -> u8 {
904 read_byte(reg::INDEX)
905}
906
907fn select_index(index: u8) {
908 write_byte(reg::INDEX, index & 0x03);
909}
910
911fn read_byte(addr: u32) -> u8 {
912 unsafe { read_u8(addr) }
914}
915
916fn write_byte(addr: u32, value: u8) {
917 unsafe { write_u8(addr, value) }
919}
920
921fn steal() -> Cd {
923 unsafe { Cd::steal() }
926}
927
928#[deprecated(note = "use `Cd::try_command` with the `Cd` token")]
930#[inline(always)]
931pub fn try_command(command: u8, params: &[u8], spin_limit: u32) -> Option<Response> {
932 steal().try_command(command, params, spin_limit)
933}
934
935#[deprecated(note = "use `Cd::irq_flag_value` with the `Cd` token")]
937#[inline(always)]
938pub fn irq_flag_value() -> u8 {
939 steal().irq_flag_value()
940}
941
942#[deprecated(note = "use `Cd::acknowledge_irq` with the `Cd` token")]
944#[inline(always)]
945pub fn acknowledge_irq(bits: u8) {
946 steal().acknowledge_irq(bits)
947}
948
949#[deprecated(note = "use `Cd::discard_response` with the `Cd` token")]
951#[inline(always)]
952pub fn discard_response() {
953 steal().discard_response()
954}
955
956#[deprecated(note = "use `Cd::dispatch_command` with the `Cd` token")]
958#[inline(always)]
959pub fn dispatch_command(command: u8, params: &[u8], spin_limit: u32) -> Option<u8> {
960 steal().dispatch_command(command, params, spin_limit)
961}
962
963#[deprecated(note = "use `Cd::restore_irq_output` with the `Cd` token")]
965#[inline(always)]
966pub fn restore_irq_output(saved: u8) {
967 steal().restore_irq_output(saved)
968}
969
970#[deprecated(note = "use `Cd::poll_data_sector` with the `Cd` token")]
972#[inline(always)]
973pub fn poll_data_sector() -> Result<bool, SectorPollError> {
974 steal().poll_data_sector()
975}
976
977#[deprecated(note = "use `Cd::try_status` with the `Cd` token")]
979#[inline(always)]
980pub fn try_status(spin_limit: u32) -> Option<Response> {
981 steal().try_status(spin_limit)
982}
983
984#[deprecated(note = "use `Cd::try_set_mode` with the `Cd` token")]
986#[inline(always)]
987pub fn try_set_mode(mode: u8, spin_limit: u32) -> Option<Response> {
988 steal().try_set_mode(mode, spin_limit)
989}
990
991#[deprecated(note = "use `Cd::try_set_target_lba` with the `Cd` token")]
993#[inline(always)]
994pub fn try_set_target_lba(lba: u32, spin_limit: u32) -> Option<Response> {
995 steal().try_set_target_lba(lba, spin_limit)
996}
997
998#[deprecated(note = "use `Cd::try_start_reading` with the `Cd` token")]
1000#[inline(always)]
1001pub fn try_start_reading(spin_limit: u32) -> Option<Response> {
1002 steal().try_start_reading(spin_limit)
1003}
1004
1005#[deprecated(note = "use `Cd::try_unmute` with the `Cd` token")]
1007#[inline(always)]
1008pub fn try_unmute(spin_limit: u32) -> Option<Response> {
1009 steal().try_unmute(spin_limit)
1010}
1011
1012#[deprecated(note = "use `Cd::try_mute` with the `Cd` token")]
1014#[inline(always)]
1015pub fn try_mute(spin_limit: u32) -> Option<Response> {
1016 steal().try_mute(spin_limit)
1017}
1018
1019#[deprecated(note = "use `Cd::try_play_track` with the `Cd` token")]
1021#[inline(always)]
1022pub fn try_play_track(track: u8, spin_limit: u32) -> Option<Response> {
1023 steal().try_play_track(track, spin_limit)
1024}
1025
1026#[deprecated(note = "use `Cd::try_pause` with the `Cd` token")]
1028#[inline(always)]
1029pub fn try_pause(spin_limit: u32) -> Option<Response> {
1030 steal().try_pause(spin_limit)
1031}
1032
1033#[deprecated(note = "use `Cd::try_pause_until_complete` with the `Cd` token")]
1035#[inline(always)]
1036pub fn try_pause_until_complete(spin_limit: u32) -> bool {
1037 steal().try_pause_until_complete(spin_limit)
1038}
1039
1040#[deprecated(note = "use `Cd::try_play_position` with the `Cd` token")]
1042#[inline(always)]
1043pub fn try_play_position(spin_limit: u32) -> Option<Response> {
1044 steal().try_play_position(spin_limit)
1045}
1046
1047#[cfg(test)]
1048mod tests {
1049 use super::*;
1050
1051 #[test]
1052 fn sector_probe_leaves_int1_and_orders_other_acknowledgements() {
1053 struct Fake {
1054 flag: u8,
1055 ready: bool,
1056 events: [u8; 4],
1057 len: usize,
1058 }
1059 impl Fake {
1060 fn event(&mut self, e: u8) {
1061 self.events[self.len] = e;
1062 self.len += 1;
1063 }
1064 }
1065 impl SectorPollIo for Fake {
1066 fn flag(&mut self) -> u8 {
1067 self.event(1);
1068 self.flag
1069 }
1070 fn drain(&mut self) {
1071 self.event(2);
1072 }
1073 fn acknowledge(&mut self, flag: u8, reset: bool) {
1074 assert_eq!(flag, if reset { 31 } else { self.flag });
1075 self.event(if reset { 4 } else { 3 });
1076 }
1077 fn data_ready(&mut self) -> bool {
1078 self.event(5);
1079 self.ready
1080 }
1081 }
1082 for flag in 0..=5 {
1083 for ready in [false, true] {
1084 let mut io = Fake {
1085 flag,
1086 ready,
1087 events: [0; 4],
1088 len: 0,
1089 };
1090 let result = poll_sector(&mut io);
1091 let expected = match flag {
1092 1 => Ok(true),
1093 5 => Err(SectorPollError),
1094 2..=4 => Ok(false),
1095 _ => Ok(ready),
1096 };
1097 assert_eq!(result, expected);
1098 let events: &[u8] = match flag {
1099 1 => &[1],
1100 5 => &[1, 2, 4],
1101 2..=4 => &[1, 2, 3],
1102 _ => &[1, 5],
1103 };
1104 assert_eq!(&io.events[..io.len], events);
1105 }
1106 }
1107 }
1108
1109 struct FakeIo {
1112 room: bool,
1113 irq: Result<u8, CdError>,
1114 events: [u8; 12],
1115 len: usize,
1116 spins_seen: [u32; 4],
1117 spins_len: usize,
1118 }
1119
1120 impl FakeIo {
1121 fn new(room: bool, irq: Result<u8, CdError>) -> Self {
1122 FakeIo {
1123 room,
1124 irq,
1125 events: [0; 12],
1126 len: 0,
1127 spins_seen: [0; 4],
1128 spins_len: 0,
1129 }
1130 }
1131 fn event(&mut self, e: u8) {
1132 self.events[self.len] = e;
1133 self.len += 1;
1134 }
1135 fn events(&self) -> &[u8] {
1136 &self.events[..self.len]
1137 }
1138 fn spins(&mut self, spins: u32) {
1139 self.spins_seen[self.spins_len] = spins;
1140 self.spins_len += 1;
1141 }
1142 }
1143
1144 const BEGIN: u8 = 1;
1147 const ROOM: u8 = 2;
1148 const PARAM: u8 = 3;
1149 const COMMAND: u8 = 4;
1150 const WAIT: u8 = 5;
1151 const FINISH: u8 = 6;
1152 const RECOVER: u8 = 7;
1153 const RESTORE: u8 = 8;
1154
1155 impl CommandIo for FakeIo {
1156 fn begin(&mut self) -> u8 {
1157 self.event(BEGIN);
1158 0x1f
1159 }
1160 fn wait_param_room(&mut self, spins: u32) -> bool {
1161 self.event(ROOM);
1162 self.spins(spins);
1163 self.room
1164 }
1165 fn write_param(&mut self, _value: u8) {
1166 self.event(PARAM);
1167 }
1168 fn write_command(&mut self, _command: u8) {
1169 self.event(COMMAND);
1170 }
1171 fn wait_irq(&mut self, _expected: u8, spins: u32) -> Result<u8, CdError> {
1172 self.event(WAIT);
1173 self.spins(spins);
1174 self.irq
1175 }
1176 fn finish(&mut self, _irq_enable: u8, _irq: u8) -> Response {
1177 self.event(FINISH);
1178 response_from(&[0x02])
1179 }
1180 fn recover(&mut self, _irq_enable: u8) {
1181 self.event(RECOVER);
1182 }
1183 fn restore_enable(&mut self, _irq_enable: u8) {
1184 self.event(RESTORE);
1185 }
1186 }
1187
1188 #[test]
1189 fn a_full_parameter_fifo_times_out_without_writing_the_parameter() {
1190 let mut io = FakeIo::new(false, Ok(code::ACKNOWLEDGE));
1192 let result = run_command(&mut io, CMD_SETLOC, &[0x00, 0x02, 0x00], 77);
1193 assert_eq!(result, Err(CdError::Timeout));
1194 assert_eq!(io.events(), &[BEGIN, ROOM, RESTORE]);
1195 }
1196
1197 #[test]
1198 fn a_command_nobody_acknowledges_times_out_and_leaves_irq_output_masked() {
1199 let mut io = FakeIo::new(true, Err(CdError::Timeout));
1200 let result = run_command(&mut io, CMD_GETSTAT, &[], DEFAULT_COMMAND_SPINS);
1201 assert_eq!(result, Err(CdError::Timeout));
1202 assert_eq!(io.events(), &[BEGIN, COMMAND, WAIT]);
1204 assert_eq!(&io.spins_seen[..io.spins_len], &[DEFAULT_COMMAND_SPINS]);
1205 }
1206
1207 #[test]
1208 fn a_drive_error_is_reported_and_the_controller_cleaned_up() {
1209 let mut io = FakeIo::new(true, Err(CdError::DriveError));
1210 let result = run_command(&mut io, CMD_PLAY, &[0x01], 10);
1211 assert_eq!(result, Err(CdError::DriveError));
1212 assert_eq!(io.events(), &[BEGIN, ROOM, PARAM, COMMAND, WAIT, RECOVER]);
1213 }
1214
1215 #[test]
1216 fn an_acknowledged_command_returns_its_response() {
1217 let mut io = FakeIo::new(true, Ok(code::ACKNOWLEDGE));
1218 let result = run_command(&mut io, CMD_SETLOC, &[1, 2, 3], 10).unwrap();
1219 assert_eq!(result.bytes(), &[0x02]);
1220 assert_eq!(
1221 io.events(),
1222 &[BEGIN, ROOM, PARAM, ROOM, PARAM, ROOM, PARAM, COMMAND, WAIT, FINISH]
1223 );
1224 }
1225
1226 #[test]
1227 fn the_flag_wait_discards_stale_responses_and_stops_at_its_budget() {
1228 let script = [0u8, 2, 3];
1230 let mut next = 0;
1231 let mut discarded = [0u8; 4];
1232 let mut dropped = 0;
1233 let got = wait_irq_flag(
1234 code::ACKNOWLEDGE,
1235 10,
1236 || {
1237 let flag = script[next];
1238 next += 1;
1239 flag
1240 },
1241 |flag| {
1242 discarded[dropped] = flag;
1243 dropped += 1;
1244 },
1245 );
1246 assert_eq!(got, Ok(code::ACKNOWLEDGE));
1247 assert_eq!(&discarded[..dropped], &[2]);
1248
1249 let mut reads = 0u32;
1251 let got = wait_irq_flag(
1252 code::ACKNOWLEDGE,
1253 5,
1254 || {
1255 reads += 1;
1256 0
1257 },
1258 |_| {},
1259 );
1260 assert_eq!(got, Err(CdError::Timeout));
1261 assert_eq!(reads, 6);
1262
1263 let got = wait_irq_flag(code::ACKNOWLEDGE, 1000, || code::ERROR, |_| {});
1265 assert_eq!(got, Err(CdError::DriveError));
1266 }
1267
1268 #[test]
1269 fn bcd_clamps_to_two_digits() {
1270 assert_eq!(bin_to_bcd(2), 0x02);
1271 assert_eq!(bin_to_bcd(42), 0x42);
1272 assert_eq!(bin_to_bcd(100), 0x99);
1273 }
1274
1275 #[test]
1276 fn bcd_to_bin_roundtrips() {
1277 for v in 0u8..=99 {
1278 assert_eq!(bcd_to_bin(bin_to_bcd(v)), v);
1279 }
1280 }
1281
1282 #[test]
1283 fn lba_to_msf_includes_the_lead_in() {
1284 assert_eq!(lba_to_bcd_msf(0), [0x00, 0x02, 0x00]);
1285 assert_eq!(lba_to_bcd_msf(424), [0x00, 0x07, 0x49]);
1286 assert_eq!(lba_to_bcd_msf(992), [0x00, 0x15, 0x17]);
1287 }
1288
1289 fn response_from(bytes: &[u8]) -> Response {
1290 let mut buf = [0u8; 16];
1291 buf[..bytes.len()].copy_from_slice(bytes);
1292 Response {
1293 bytes: buf,
1294 len: bytes.len(),
1295 }
1296 }
1297
1298 #[test]
1299 fn play_position_parses_and_converts() {
1300 let resp = response_from(&[
1302 bin_to_bcd(2),
1303 0x01,
1304 bin_to_bcd(1),
1305 bin_to_bcd(23),
1306 bin_to_bcd(45),
1307 bin_to_bcd(4),
1308 bin_to_bcd(56),
1309 bin_to_bcd(0),
1310 ]);
1311 let p = PlayPosition::parse(&resp).unwrap();
1312 assert_eq!(p.track, 2);
1313 assert_eq!(p.index, 1);
1314 assert_eq!(
1315 (p.relative_min, p.relative_sec, p.relative_frame),
1316 (1, 23, 45)
1317 );
1318 assert_eq!(p.relative_sectors(), 6270);
1320 assert_eq!(p.relative_millis(), 83_600);
1322 }
1323
1324 #[test]
1325 fn play_position_rejects_short_response() {
1326 assert!(PlayPosition::parse(&response_from(&[0x02, 0x01, 0x00])).is_none());
1327 }
1328}