// SPDX-License-Identifier: MIT //! The bus state controller (BSC): areas, port widths, cycle shapes, the refresh timer. //! //! sh2-python's `bsc.py`. HM chapter 7. The external space is four areas selected by //! A26-A25 (HM table 7.3): CS0 H'00000000-, CS1 H'02000000-, CS2 H'04000000-, CS3 //! H'06000000- (the same in the cache-through alias at +H'20000000). Each area's port //! width comes from BCR2 (areas 1-3) or the MD3/MD4 pins (area 0), its waits from WCR //! and BCR1, its interface (ordinary, burst ROM, synchronous DRAM, DRAM, pseudo-SRAM) //! from BCR1 and MCR. //! //! The registers are 16 bits wide at H'FFFFFFE0-H'FFFFFFFB, one per longword: "If read //! as 32 bits, the upper 16 bits are 0 ... 32-bit writes are accepted only when the //! value of the upper 16 bits of the write data is H'A55A; no other writes are //! performed", and a 16-bit access (at the longword's address + 2) "is for read only" //! (HM 7.1.4, table 7.2). //! //! **Ordinary space** (every area at reset): the chip splits accesses into port-width //! cycles (HM 7.4.1) of ``cycle_states[area]`` = 2 states + programmed waits, plus the //! board's ``chip.wait`` (HM 7.2.3, 7.4.1). //! //! **Synchronous DRAM** (BCR1 DRAM2-DRAM0 = 001, 100 or 101: area 3, area 2 or both; HM //! 7.2.1, 7.5). The port is MCR.SZ's 16 or 32 bits ("the data bus width of BCR2 is //! ignored", HM 7.2.4), WCR's Wn1/Wn0 are the CAS latency (1-4), and "with synchronous //! DRAM, external wait input is ignored regardless of any setting" (HM 7.2.3), so //! ``waits`` is 0 and ``wait_pin`` False there. Every read is one burst of 16 bytes, 8 //! data cycles on a 16-bit port and 4 on a 32-bit one (HM 7.5.1); a cache-through, ID/OD //! or DMA read of less than 16 bytes still makes the whole burst and takes its data //! from the first cycles (HM 7.5.4, "8-word-burst-read-fixed"). Writes are single (HM //! 7.5.5). The states, with RCD, TRP, TRWL (1 or 2 cycles) from MCR and CL from WCR: //! //! ==================================== ================================== ========= //! cycle states (HM) 32X //! ==================================== ================================== ========= //! read, auto-precharge (RASD = 0) Tr [Trw] Tc Tw x (CL - 1) Td x n figs 7.15-7.17 //! read, bank active, bank idle the same (figure 7.19) 3 + 8 = 11 //! read, bank active, same row [Tnop if CL = 1] Tc Tw x (CL - 1) 2 + 8 = 10 //! Td x n (figure 7.20) //! read, bank active, other row Tp [Tnop] Tr [Trw] Tc Tw x (CL - 1) 4 + 8 = 12 //! Td x n (figure 7.21) //! write, auto-precharge Tr [Trw] Tc (figure 7.18) - //! write, bank active, bank idle Tr [Trw] Tc (figure 7.22) 2 //! write, bank active, same row Tc (figure 7.23) 1 //! write, bank active, other row Tp [Tnop] Tr [Trw] Tc (figure 7.24) 3 //! auto-refresh Tp [Tnop] Trr, TRAS + 2 (fig. 7.25) 7 //! ==================================== ================================== ========= //! //! (The 32X column: its boot ROMs' BCR1 = H'0001, WCR = H'0055, MCR = H'0AB8, i.e. CS3 //! synchronous DRAM, 16 bits, CAS latency 2, bank active, TRP = RCD = TRWL = 1, TRAS = //! 3.) The tail of an auto-precharge cycle is the bank's, not the bus's: Tap (TRP cycles //! after a read with CAS latency 1, TRP - 1 with 2 or more; HM 7.5.3) and Trwl + Tap //! after a write (HM 7.5.5) only hold back the next command to the same bank, which is //! why HM 7.5.6 says they become "invisible" when the next access comes later. In //! bank-active mode TRWL holds back a PRE after a write instead (HM 7.2.4). Bank-active //! mode is area 3 only; area 2 is "always auto-precharge" (HM 7.2.4, RASD). Each area //! has two banks (HM 7.5.1), each with one open row; the bank bit and the row's lowest //! bit come from MCR's SZ and AMX (HM table 7.4: the bank is the row address bit table //! 7.4's note 2 marks; the row starts at the address bit on the device's A0). Banks //! close at a refresh and "after the bus is released by bus arbitration" (HM 7.5.6), //! which only the board sees: it calls ``bus_released``. //! //! The callbacks still see one call per port cycle: a burst's first call carries its //! command states (Tr ... Tw), every call one data cycle, and the last call the burst's //! remaining data cycles when the access wanted fewer than the burst holds (Td2-Td8 of //! HM figure 7.17). Inside a callback ``cycle_states[area]`` is that call's own states, //! so a board that computes a cycle's end as ``chip.clock + cycle_states[area] + //! chip.wait`` keeps working. ``chip.wait`` still lengthens an SDRAM cycle: the WAIT pin //! is ignored there (a board must not add WAIT states to it), but ``chip.wait`` is also //! how a board says the cycle could not start yet (bus arbitration, HM 7.10), and that //! delay is real for SDRAM cycles too (docs/timing.md, "Synchronous DRAM"). //! //! **The refresh timer** (HM 7.2.5-7.2.7, 7.5.7). RTCNT counts CKIO / 4 ... / 4096 //! (RTCSR.CKS2-CKS0, 000 stops it) on multiples of the divider, like the WDT; when it //! reaches RTCOR it is cleared, RTCSR.CMF is set (the CMI interrupt with CMIE: IPRA's //! WDT level, VCRWDT's BCMV vector, HM table 5.4) and, with MCR.RFSH = 1, RMD = 0 and a //! synchronous DRAM area, a refresh is requested. The chip makes it "after the access //! cycle is completed" (HM 7.5.7): at the request, or when the external access under //! way ends, as one ``read(address, width, REFRESH)`` call at the first SDRAM area's //! base address (value ignored); it holds the bus for its states and closes every bank. //! A request made while one waits replaces it (HM 7.2.7). In slave mode RFSH "is always //! handled as 0" (HM 7.2.4), so only the master refreshes. RTCNT and RTCSR are read //! live. //! //! Not modelled (each changes timing only): DRAM, pseudo-SRAM and burst-ROM cycle //! shapes (HM 7.6-7.8) and their refresh (those areas stay ordinary cycles), //! self-refresh (RMD = 1: no refresh is made), the SDRAM mode-register writes //! (H'FFFF8000-, HM 7.5.8), idle cycles (WCR.IW, HM 7.9), arbitration itself (HM 7.10: //! the board's). The write buffer (HM 7.11.2) is the chip's (``core.rs``). //! //! What Rust forces: the reference's ``regs`` dict (address -> value) is an array over //! the seven longword addresses, and the methods that reach the chip (the refresh //! timer's interrupt, the refresh schedule) are chip methods named ``bsc_*``. use crate::bus::Bus; use crate::cpu::Sh7604; use crate::intc::Intc; use crate::module::{reg, value_of, ModuleState, Register}; pub const KEY: u32 = 0xA55A0000; /// Long-wait counts selected by BCR1's AHLW/A1LW/A0LW fields (HM 7.2.1): 3, 4, 5, 6. pub const LONG_WAITS: [i64; 4] = [3, 4, 5, 6]; /// BCR2's AnSZ field -> port width in bits (00 is reserved; HM 7.2.2). pub const PORT_WIDTHS: [u32; 4] = [32, 8, 16, 32]; /// BCR1's DRAM2-DRAM0 -> (area 2, area 3) synchronous DRAM (HM 7.2.1; 110/111 reserved). pub fn sdram_areas(dram: u32) -> (bool, bool) { match dram { 1 => (false, true), 4 => (true, false), 5 => (true, true), _ => (false, false), } } /// MCR (SZ, AMX2-AMX0) -> (the row's lowest address bit, the bank address bit) (HM table /// 7.4). With SZ = 0 the device's A0 is the chip's A1, with SZ = 1 its A2 (HM 7.5.2), so /// the row starts at the address bit the row phase puts on that pin; the bank bit is the /// one note 2 marks. Reserved settings fall back to AMX = 000. pub fn sdram_geometry(sz: u32, amx: u32) -> Option<(u32, u32)> { match (sz, amx) { (0, 0b000) => Some((9, 20)), // 16 Mbit, 1M x 16 (0, 0b011) => Some((9, 18)), // 4 Mbit, 256k x 16 (0, 0b111) => Some((9, 17)), // 2 Mbit, 128k x 16 (the 32X's) (1, 0b000) => Some((10, 21)), (1, 0b001) => Some((11, 22)), (1, 0b010) => Some((12, 23)), (1, 0b011) => Some((10, 19)), (1, 0b111) => Some((10, 18)), _ => None, } } /// RTCSR.CKS2-CKS0 -> CKIO divider; 0 = count-up disabled (HM 7.2.5). pub const REFRESH_DIVIDERS: [i64; 8] = [0, 4, 16, 64, 256, 1024, 2048, 4096]; pub const NEVER: i64 = 1 << 62; pub const BCR1: u32 = 0xFFFFFFE0; pub const BCR2: u32 = 0xFFFFFFE4; pub const WCR: u32 = 0xFFFFFFE8; pub const MCR: u32 = 0xFFFFFFEC; pub const RTCSR: u32 = 0xFFFFFFF0; pub const RTCNT: u32 = 0xFFFFFFF4; pub const RTCOR: u32 = 0xFFFFFFF8; pub const REGISTERS: [Register; 7] = [ reg(BCR1, "BCR1", 16, Some(0x03F0)), reg(BCR2, "BCR2", 16, Some(0x00FC)), reg(WCR, "WCR", 16, Some(0xAAFF)), reg(MCR, "MCR", 16, Some(0x0000)), reg(RTCSR, "RTCSR", 16, Some(0x0000)), reg(RTCNT, "RTCNT", 16, Some(0x0000)), reg(RTCOR, "RTCOR", 16, Some(0x0000)), ]; /// Where ``address`` is in the register array, or None (the reference's ``not in /// self.regs``). fn slot(address: u32) -> Option { if (BCR1..=RTCOR).contains(&address) && address & 3 == 0 { Some(((address - BCR1) >> 2) as usize) } else { None } } #[derive(Clone, Debug)] pub struct Bsc { pub slave: bool, pub area0_width: u32, pub regs: [u32; 7], /// Port width of each area in bits, recomputed when BCR2 is written. pub widths: [u32; 4], /// Programmed waits of each area (ordinary space), and whether WAIT is sampled. pub waits: [i64; 4], pub wait_pin: [bool; 4], /// States of one ordinary-space cycle in each area before the board's waits; for a /// synchronous DRAM area, inside a callback, the states of the call being made. pub cycle_states: [i64; 4], /// Whether each area is synchronous DRAM (BCR1 DRAM2-DRAM0). pub sdram: [bool; 4], /// Data cycles of one burst read in each synchronous DRAM area (HM 7.5.1). pub burst: [i64; 4], pub cas: [i64; 4], // CAS latency (WCR, HM 7.2.3) pub trp: i64, pub rcd: i64, pub trwl: i64, pub tras: i64, pub row_low: u32, pub bank_bit: u32, pub bank_active: bool, // RASD (area 3 only) // Per area and bank: the open row (-1: none), the first state the bank takes a // command (after an auto-precharge's Trwl/Tap), the first state it takes a PRE. pub rows: [[i64; 2]; 4], pub ready: [[i64; 2]; 4], pub pre_ok: [[i64; 2]; 4], // The refresh timer: RTCNT is ``count`` at state ``count_at``. pub count: i64, pub count_at: i64, /// The state of the next refresh request not yet made (NEVER: none). pub refresh_at: i64, pub cmf_read: bool, } impl Bsc { /// ``BSC(chip, slave=..., area0_width=...)``: the constructor runs ``reset`` with the /// chip not yet able to schedule (``hasattr(chip, "_bsc_due")`` is false then). pub fn new(slave: bool, area0_width: u32, clock: i64) -> Bsc { let mut bsc = Bsc { slave, area0_width, regs: [0; 7], widths: [area0_width, 32, 32, 32], waits: [0; 4], wait_pin: [true; 4], cycle_states: [2; 4], sdram: [false; 4], burst: [0; 4], cas: [1; 4], trp: 1, rcd: 1, trwl: 1, tras: 2, row_low: 9, bank_bit: 20, bank_active: false, rows: [[-1, -1]; 4], ready: [[0, 0]; 4], pre_ok: [[0, 0]; 4], count: 0, count_at: 0, refresh_at: NEVER, cmf_read: false, }; bsc.reset_registers(clock); bsc.refresh_at = bsc.schedule_value(); bsc } /// ``self.regs[address]``. pub fn reg(&self, address: u32) -> u32 { self.regs[slot(address).unwrap()] } fn set_reg(&mut self, address: u32, value: u32) { self.regs[slot(address).unwrap()] = value; } /// The first half of ``reset``: everything before ``_schedule_refresh``. fn reset_registers(&mut self, clock: i64) { for register in REGISTERS.iter() { self.set_reg(register.address, register.initial.unwrap_or(0)); } self.count = 0; self.count_at = clock; self.cmf_read = false; self.recompute(); } pub fn recompute(&mut self) { let (bcr1, bcr2, wcr) = (self.reg(BCR1), self.reg(BCR2), self.reg(WCR)); let mcr = self.reg(MCR); self.widths = [ self.area0_width, PORT_WIDTHS[((bcr2 >> 2) & 3) as usize], PORT_WIDTHS[((bcr2 >> 4) & 3) as usize], PORT_WIDTHS[((bcr2 >> 6) & 3) as usize], ]; let long_waits = [ LONG_WAITS[((bcr1 >> 4) & 3) as usize], // area 0: A0LW LONG_WAITS[((bcr1 >> 6) & 3) as usize], // area 1: A1LW LONG_WAITS[((bcr1 >> 8) & 3) as usize], // areas 2 and 3: AHLW LONG_WAITS[((bcr1 >> 8) & 3) as usize], ]; let (area2, area3) = sdram_areas(bcr1 & 7); self.sdram = [false, false, area2, area3]; let sz = (mcr >> 6) & 1; for area in 0..4 { let w = (wcr >> (2 * area)) & 3; if self.sdram[area] { // Wn1/Wn0 are the CAS latency; WAIT is ignored; MCR.SZ is the port // (HM 7.2.3, 7.2.4). self.cas[area] = w as i64 + 1; self.waits[area] = 0; self.wait_pin[area] = false; self.widths[area] = if sz != 0 { 32 } else { 16 }; self.burst[area] = if sz != 0 { 4 } else { 8 }; self.cycle_states[area] = 1; continue; } // WCR Wn1/Wn0: 00 no wait and no WAIT pin; 01 one wait, 10 two, 11 BCR1's // long wait; every setting but 00 samples the WAIT pin (HM 7.2.3). self.waits[area] = if w == 3 { long_waits[area] } else { w as i64 }; self.wait_pin[area] = w != 0; self.cycle_states[area] = 2 + self.waits[area]; self.burst[area] = 0; } self.configure_sdram(); } fn configure_sdram(&mut self) { let mcr = self.reg(MCR); let sz = (mcr >> 6) & 1; self.trp = if mcr & 0x8000 != 0 { 2 } else { 1 }; self.rcd = if mcr & 0x4000 != 0 { 2 } else { 1 }; self.trwl = if mcr & 0x2000 != 0 { 2 } else { 1 }; self.tras = 2 + (((mcr >> 11) & 3) as i64).min(2); // 11 is reserved self.bank_active = mcr & 0x0200 != 0; let amx = ((mcr >> 5) & 4) | ((mcr >> 4) & 3); let (row_low, bank_bit) = sdram_geometry(sz, amx).unwrap_or_else(|| sdram_geometry(sz, 0).unwrap()); self.row_low = row_low; self.bank_bit = bank_bit; self.close_banks(); } // -- synchronous DRAM cycles (HM 7.5) --------------------------------------------- /// Every bank precharged: no open row (a refresh, a bus release, a new setting). pub fn close_banks(&mut self) { for rows in self.rows.iter_mut() { rows[0] = -1; rows[1] = -1; } } /// For the board: this chip gave the bus away (HM 7.10). "Both banks will become /// inactive even in the bank active mode ... after the bus is released by bus /// arbitration" (HM 7.5.6); the precharge's own states are not modelled. pub fn bus_released(&mut self) { self.close_banks(); } /// A burst read of ``address`` wanted at ``clock``: (its first state, its command /// states before Td1). Opens the row in bank-active mode. pub fn read_start(&mut self, area: usize, address: u32, clock: i64) -> (i64, i64) { let bank = ((address >> self.bank_bit) & 1) as usize; let row = ((address & ((1u32 << self.bank_bit) - 1)) >> self.row_low) as i64; let mut start = clock.max(self.ready[area][bank]); let cas = self.cas[area]; // Tc, then CAS latency - 1 Tw before the first data cycle (HM 7.5.3). let mut states = cas; if self.bank_active && area == 3 { if self.rows[area][bank] == row { if cas == 1 { states += 1; // Tnop for DQM's two-cycle latency (HM 7.5.6, figure 7.20) } } else { if self.rows[area][bank] >= 0 { // Tp, TRP - 1 Tnop (figure 7.21) start = start.max(self.pre_ok[area][bank]); states += self.trp; } states += self.rcd; // Tr, RCD - 1 Trw (figure 7.19) self.rows[area][bank] = row; } } else { states += self.rcd; // Tr [Trw] (figures 7.15, 7.16) } (start, states) } /// A burst read ended at ``end``: an auto-precharge read's Tap holds the bank. pub fn read_end(&mut self, area: usize, address: u32, end: i64) { if !(self.bank_active && area == 3) { let bank = ((address >> self.bank_bit) & 1) as usize; let tap = if self.cas[area] == 1 { self.trp } else { self.trp - 1 }; // HM 7.5.3 self.ready[area][bank] = end + tap; } } /// A single write of ``address`` wanted at ``clock``: (its first state, its states). /// Opens the row in bank-active mode. pub fn write_cycle(&mut self, area: usize, address: u32, clock: i64) -> (i64, i64) { let bank = ((address >> self.bank_bit) & 1) as usize; let mut start = clock.max(self.ready[area][bank]); let states; if self.bank_active && area == 3 { let row = ((address & ((1u32 << self.bank_bit) - 1)) >> self.row_low) as i64; if self.rows[area][bank] == row { states = 1; // Tc (figure 7.23) } else if self.rows[area][bank] < 0 { states = self.rcd + 1; // Tr [Trw] Tc (figure 7.22) } else { start = start.max(self.pre_ok[area][bank]); states = self.trp + self.rcd + 1; // Tp [Tnop] Tr [Trw] Tc (figure 7.24) } self.rows[area][bank] = row; } else { states = self.rcd + 1; // Tr [Trw] Tc (figure 7.18) } (start, states) } /// A single write ended at ``end``: what it holds back, the bank's next command /// (auto-precharge: Trwl then Tap) or its next PRE (bank active: TRWL), HM 7.2.4, /// 7.5.5-7.5.6. pub fn write_end(&mut self, area: usize, address: u32, end: i64) { let bank = ((address >> self.bank_bit) & 1) as usize; if self.bank_active && area == 3 { self.pre_ok[area][bank] = end + self.trwl; } else { self.ready[area][bank] = end + self.trwl + self.trp; } } /// An auto-refresh wanted at ``clock``: (its first state, its states). Tp (PALL), /// TRP - 1 Tnop, Trr (REF), then TRAS + 2 with no command (HM 7.5.7, figure 7.25); /// every bank closes. pub fn refresh_cycle(&mut self, clock: i64) -> (i64, i64) { let mut start = clock; for area in [2, 3] { if self.sdram[area] { start = start.max(self.pre_ok[area][0]).max(self.pre_ok[area][1]); } } self.close_banks(); (start, self.trp + 1 + self.tras + 2) } /// The refresh cycle ended at ``end``: the banks take commands from then on. pub fn refresh_end(&mut self, end: i64) { for area in [2, 3] { let ready = &mut self.ready[area]; ready[0] = ready[0].max(end); ready[1] = ready[1].max(end); } } /// Where a refresh cycle is reported: the first synchronous DRAM area's base. pub fn refresh_address(&self) -> u32 { if self.sdram[2] { 0x04000000 } else { 0x06000000 } } // -- the refresh timer (HM 7.2.5-7.2.7) ------------------------------------------ /// The state at which RTCNT next reaches RTCOR (NEVER when it does not count). pub fn next_match(&self) -> i64 { let div = REFRESH_DIVIDERS[((self.reg(RTCSR) >> 3) & 7) as usize]; if div == 0 { return NEVER; } let need = match (self.reg(RTCOR) as i64 - self.count) & 0xFF { 0 => 0x100, n => n, }; (self.count_at.div_euclid(div) + need) * div } /// ``_schedule_refresh``'s value for ``refresh_at``. fn schedule_value(&self) -> i64 { let mcr = self.reg(MCR); if self.slave || mcr & 0x000C != 0x0008 || !(self.sdram[2] || self.sdram[3]) { NEVER // RFSH = 0 (always, in slave mode), or RMD = 1 } else { self.next_match() } } /// A refresh was made at ``start``: requests up to then are answered (a request made /// while one waited replaced it, HM 7.2.7). pub fn refresh_served(&mut self, start: i64) { let rtcor = self.reg(RTCOR) as i64; let period = (if rtcor != 0 { rtcor } else { 0x100 }) * REFRESH_DIVIDERS[((self.reg(RTCSR) >> 3) & 7) as usize]; let mut at = self.refresh_at; if period <= 0 { self.refresh_at = NEVER; return; } while at <= start { at += period; } self.refresh_at = at; } /// The next state the chip must look at the BSC: a refresh, or CMI's match. pub fn next_event(&self) -> i64 { if self.reg(RTCSR) & 0x40 != 0 && self.reg(RTCSR) & 0x80 == 0 { return self.refresh_at.min(self.next_match()); } self.refresh_at } pub fn requests(&self, intc: &Intc, out: &mut Vec<(u32, u32)>) { // CMI: compare match with CMIE (HM 7.2.5), at IPRA's WDT level, vector // VCRWDT.BCMV (HM 5.3.1, 5.3.6). let level = intc.level("wdt"); if level != 0 && self.reg(RTCSR) & 0xC0 == 0xC0 { out.push((level, intc.vector(0xFFFFFEE5))); } } } impl Sh7604 { pub(crate) fn bsc_reset(&mut self) { let clock = self.clock; self.bsc.reset_registers(clock); self.bsc_schedule_refresh(); } /// RTCNT and CMF brought to ``clock``. pub(crate) fn bsc_run_timer(&mut self, clock: i64) { let last = self.bsc.count_at; if clock <= last { return; } self.bsc.count_at = clock; let div = REFRESH_DIVIDERS[((self.bsc.reg(RTCSR) >> 3) & 7) as usize]; if div == 0 { return; } let ticks = clock.div_euclid(div) - last.div_euclid(div); if ticks == 0 { return; } let rtcor = self.bsc.reg(RTCOR) as i64; let need = match (rtcor - self.bsc.count) & 0xFF { 0 => 0x100, n => n, }; if ticks < need { self.bsc.count = (self.bsc.count + ticks) & 0xFF; return; } let period = if rtcor != 0 { rtcor } else { 0x100 }; self.bsc.count = (ticks - need).rem_euclid(period); let rtcsr = self.bsc.reg(RTCSR); self.bsc.set_reg(RTCSR, rtcsr | 0x80); // CMF self.intc_update(); } pub(crate) fn bsc_schedule_refresh(&mut self) { self.bsc.refresh_at = self.bsc.schedule_value(); if self.bsc_due_exists { self.bsc_due = self.bsc_due.min(self.bsc.refresh_at); self.reschedule(); } } pub(crate) fn bsc_run_until(&mut self, clock: i64) { self.bsc_run_timer(clock); } // -- registers ------------------------------------------------------------- pub(crate) fn bsc_read32(&mut self, address: u32) -> u32 { if address == RTCSR || address == RTCNT { let clock = self.clock; self.bsc_run_timer(clock); if address == RTCNT { return self.bsc.count as u32; } self.bsc.cmf_read = self.bsc.reg(RTCSR) & 0x80 != 0; // for CMF's clear } let mut value = match slot(address) { Some(i) => self.bsc.regs[i], None => 0, }; if address == BCR1 { // BCR1.MASTER (bit 15) reads the MD5 pin: 1 in slave mode (HM 7.2.1). value = (value & 0x7FFF) | if self.bsc.slave { 0x8000 } else { 0 }; } value } pub(crate) fn bsc_write(&mut self, address: u32, width: u32, value: u32) { // Only a longword write with H'A55A in the upper half is performed (HM 7.1.4). if width == 32 && value & 0xFFFF0000 == KEY { self.bsc_write32(address & !3, value, 0xFFFF); } } pub(crate) fn bsc_write32(&mut self, address: u32, value: u32, _mask: u32) { if slot(address).is_none() { return; } let clock = self.clock; if self.bsc.refresh_at <= clock { self.refresh_idle(); // requests made before this write are kept } self.bsc_run_timer(clock); let mut value = value & 0xFFFF; if address == BCR1 { value &= 0x1FF7; // MASTER is read-only; bits 14, 13 and 3 read 0 } else if address == BCR2 { value &= 0x00FC; } else if address == MCR { value &= 0xFEFC; } else if address == RTCSR { // CMF can only be cleared: written 0 after it was read as 1 (HM 7.2.5). let mut cmf = self.bsc.reg(RTCSR) & 0x80; if value & 0x80 == 0 && self.bsc.cmf_read { cmf = 0; } self.bsc.cmf_read = false; value = (value & 0x78) | cmf; } else if address == RTCNT { value &= 0x00FF; self.bsc.count = value as i64; } else if address == RTCOR { value &= 0x00FF; } self.bsc.set_reg(address, value); if address < RTCSR { self.bsc.recompute(); } self.bsc_schedule_refresh(); if address == RTCSR { self.intc_update(); } } pub(crate) fn bsc_state(&mut self) -> ModuleState { let clock = self.clock; self.bsc_run_timer(clock); let count = self.bsc.count as u32; self.bsc.set_reg(RTCNT, count); REGISTERS.iter().map(|r| (r.name, self.bsc.reg(r.address) as i64)).collect() } pub(crate) fn bsc_load_state(&mut self, values: &ModuleState) { for register in REGISTERS.iter() { self.bsc.set_reg(register.address, value_of(values, register.name) as u32); } self.bsc.count = self.bsc.reg(RTCNT) as i64; self.bsc.count_at = self.clock; self.bsc.recompute(); self.bsc_schedule_refresh(); } /// Each area's two banks (open row, ready, precharge allowed), CMF as last read, /// then the chip's refresh fields (``refresh_end``, ``bsc_due``, core.rs). pub(crate) fn bsc_internal(&self) -> Vec { let mut out: Vec = Vec::new(); for area in 0..4 { out.extend_from_slice(&self.bsc.rows[area]); out.extend_from_slice(&self.bsc.ready[area]); out.extend_from_slice(&self.bsc.pre_ok[area]); } out.push(self.bsc.cmf_read as i64); out.push(self.refresh_end); out.push(self.bsc_due); out } pub(crate) fn bsc_load_internal(&mut self, values: &[i64]) { for area in 0..4 { let row = &values[6 * area..6 * area + 6]; self.bsc.rows[area] = [row[0], row[1]]; self.bsc.ready[area] = [row[2], row[3]]; self.bsc.pre_ok[area] = [row[4], row[5]]; } self.bsc.cmf_read = values[24] != 0; self.refresh_end = values[25]; self.bsc_due = values[26]; } }