huc6280/
block.rs

1//! Block transfers: TII TDD TIN TIA TAI (SW SB-88 to SB-100; HW HB-6;
2//! pcetech 2.0) (`_block.py`).
3//!
4//! All five push Y, A, X, load the source, destination and length from the
5//! three operand words, move one byte per six cycles, pop X, A, Y, for
6//! 17 + 6n cycles (n = 0 means 65,536). The whole transfer is **one step**
7//! and cannot be interrupted: the interrupt is taken when it ends (U6,
8//! pcetech). The shape is Mesen's: 13 cycles of entry, `i R i W i i` a byte,
9//! 4 of exit (the reference's docs/timing.md).
10
11use crate::core::{Bus, HuC6280};
12
13// U7: a block transfer's reads of the hardware page's $0800-$17FF (PSG,
14// timer, port, interrupt registers) return 0 without touching the device,
15// whatever is there; writes work (pcetech 2.0, hw-verified). The end is the
16// parenthesis's reading, not the stated "$1400" (C9, issue 0001).
17pub const HARDWARE_ZERO_START: u32 = 0x1FE800;
18pub const HARDWARE_ZERO_END: u32 = 0x1FF7FF;
19
20impl<B: Bus> HuC6280<B> {
21    fn block_read(&mut self, address: u16) -> u8 {
22        if self.block_reads_hardware_page_as_zero
23            && (HARDWARE_ZERO_START..=HARDWARE_ZERO_END).contains(&self.physical(address))
24        {
25            self.dummy(address); // the cycle happens; the device is not asked
26            return 0;
27        }
28        self.cycle_read(address)
29    }
30
31    /// Run one transfer; each step slice is the per-byte increment, cycled
32    /// (1, -1 alternates).
33    fn transfer(&mut self, source_steps: &[i16], destination_steps: &[i16]) {
34        self.implied();
35        self.io();
36        // HW HB-6: Y, then A, then X go on the stack; A, X and Y hold the low
37        // bytes while it runs and come back at the end.
38        self.push(self.y);
39        self.push(self.a);
40        self.push(self.x);
41        let mut source = self.fetch_word();
42        let mut destination = self.fetch_word();
43        let length = match self.fetch_word() {
44            0 => 0x10000,
45            length => length as usize,
46        };
47        self.io();
48        for index in 0..length {
49            self.io();
50            let value = self.block_read(source);
51            self.io();
52            self.cycle_write(destination, value);
53            self.io();
54            self.io();
55            source = source.wrapping_add(source_steps[index % source_steps.len()] as u16);
56            destination =
57                destination.wrapping_add(destination_steps[index % destination_steps.len()] as u16);
58        }
59        self.io();
60        self.x = self.pull();
61        self.a = self.pull();
62        self.y = self.pull();
63    }
64
65    /// TII -- transfer, source +1, destination +1 (SW SB-97).
66    pub(crate) fn op_tii(&mut self) {
67        self.transfer(&[1], &[1]);
68    }
69
70    /// TDD -- transfer, source -1, destination -1 (SW SB-93).
71    pub(crate) fn op_tdd(&mut self) {
72        self.transfer(&[-1], &[-1]);
73    }
74
75    /// TIN -- transfer, source +1, destination fixed (SW SB-99).
76    pub(crate) fn op_tin(&mut self) {
77        self.transfer(&[1], &[0]);
78    }
79
80    /// TIA -- transfer, source +1, destination alternating +1, -1 (SW SB-95).
81    pub(crate) fn op_tia(&mut self) {
82        self.transfer(&[1], &[1, -1]);
83    }
84
85    /// TAI -- transfer, source alternating +1, -1, destination +1 (SW SB-88).
86    pub(crate) fn op_tai(&mut self) {
87        self.transfer(&[1, -1], &[1]);
88    }
89}