huc6280/
block.rs

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