// SPDX-License-Identifier: MIT //! Mikey's UART (ComLynx / REDEYE): transmitter, receiver of its own echo, interrupt //! (`mikey/uart.py`). //! //! Modelling level: cycle, in UART clocks (one Timer 4 borrow each; 8 per bit). //! //! Sources, DOC unless tagged otherwise: Spec ch. 11 "UART" (lynx8a) and Appx 2 //! SERCTL/SERDAT; the lynx-tests UART suites and their README (TEST ROM: uart, uart2, //! uart3, MIT, expectations taken on consoles). //! //! - DOC: baud = CLOCK4 / (TIMER4 + 1) / 8, so a bit is eight Timer 4 borrows. The //! divide-by-eight counter runs freely (HYPOTHESIS; TEST ROM uart 1-2's first //! iteration, written at a random phase after a long idle, accepts the spread a free //! divider gives, and the later ones, written just after TXEMPTY, the narrow one). //! - DOC: 11-bit frames: start (0), 8 data bits LSB first, the 9th bit, stop (1). The //! 9th bit is the parity when PAREN, else PAREVEN itself; parity counts the 9th bit //! ("the calculation for parity includes the parity bit itself"): even parity makes //! the nine bits' ones even. The receiver checks it always (PARERR), against PAREVEN //! as it stands when the frame completes (HYPOTHESIS for the moment of the check). //! - DOC: TXRDY = holding register free (1 after reset); TXEMPTY = holding and shift //! register both empty. TEST ROM uart2 3: PAREN/PAREVEN take effect for the next //! frame and, under TXBRK, after the break: so the 9th bit is fixed when the holding //! register is taken. TEST ROM uart2 4: a write to a full holding register replaces //! its byte. //! - DOC: TXBRK holds the line low as long as it is set; the receiver recognises a //! break after 24 bit times (RXBRK). HYPOTHESIS: a frame already in the shift register //! runs to its end (TEST ROM uart 6: TXRDY about 11 bits after the release when a //! frame was in flight) and the holding register is not taken while TXBRK is set. //! - DOC: errors (PARERR, OVERRUN, FRAMERR, RXBRK) stay set until a SERCTL write with //! RESETERR. The interrupt is level sensitive (TXINTEN and TXRDY, RXINTEN and RXRDY); //! TEST ROM uart2 5: the level sets INTSET bit 4, which only an INTRST write clears, //! and it sets again at once while the level holds. //! - TEST ROM uart3 (README): transmit and receive share one open-collector wire, so //! the receiver hears every frame this console sends (the echo). //! //! Timing inside a frame (when the holding register is taken, when TXEMPTY rises, when //! the echo is received, how many received bytes are held) has no document. The four //! constants below are FITTED: see the reference's docs/validation.md "M6 UART" for the //! tests, the values tried and the held-out results. //! //! Differences forced by Rust: the Python UART holds `pending`, a callable that ORs a bit //! into Mikey's INTSET; here each method that may raise the level takes INTSET as //! `pending: &mut u32`. The lists of tuples are `VecDeque`s. use std::collections::VecDeque; use serde::{Deserialize, Serialize}; pub const CLOCKS_PER_BIT: i64 = 8; // DOC: CLOCK4 / (TIMER4 + 1) / 8 pub const FRAME_CLOCKS: i64 = 11 * CLOCKS_PER_BIT; // DOC: 11-bit frames, back to back when chained pub const BREAK_CLOCKS: i64 = 24 * CLOCKS_PER_BIT; // DOC: a break is recognised after 24 bit times // FITTED (docs/validation.md "M6 UART"). IDLE_START: bit boundaries from a write to an // idle transmitter until the start bit. TAKE: UART clocks from the start bit until the // holding register is taken (TXRDY). EMPTY: UART clocks after the stop bit's end until // TXEMPTY. RECEIVED: UART clocks from the start bit until the echo is in the receiver. pub const IDLE_START: i64 = 1; pub const TAKE: i64 = 8; pub const EMPTY: i64 = 8; pub const RECEIVED: i64 = 84; // the middle of the stop bit (HYPOTHESIS: a receiver sampling mid-bit) /// FITTED: ticks (16 MHz) between the receiver's moment above and the byte being ready: /// a delay in time, not in bits. HYPOTHESIS for its cause: the open-collector wire's slow /// rise through its pull-up (DOC ch. 11: "open-collector with a pull-up resistor"). pub const ECHO_DELAY: i64 = 600; // passing window 560-640 at RECEIVED = 84 (480 and 720 fail uart3 3) pub const TXINTEN: u8 = 0x80; pub const RXINTEN: u8 = 0x40; pub const PAREN: u8 = 0x10; pub const RESETERR: u8 = 0x08; pub const TXBRK: u8 = 0x02; pub const PAREVEN: u8 = 0x01; fn ones(value: i64) -> i64 { value.count_ones() as i64 } #[derive(Clone, Serialize, Deserialize)] pub struct Uart { pub arriving: VecDeque<(i64, i64)>, // (tick it reaches the receiver, frame) pub break_clocks: i64, pub clocks: i64, pub ctl: u8, // DOC SERCTL reset 0 pub echoes: VecDeque<(i64, i64)>, // (UART clock received, 9-bit frame) pub frame_end: i64, // UART clock at which TXEMPTY may rise again pub frame_start: i64, pub framerr: u8, pub held: i64, // a finished 9-bit frame waiting in the receive shift register pub holding: i64, // -1 = empty (DOC: TXRDY is 1 after reset) pub idle_wait: i64, pub last_data: u8, pub overrun: u8, pub parerr: u8, pub received: VecDeque<(u8, u8)>, // the receive data register (0 or 1) pub rxbrk: u8, pub taken: bool, // the frame in the shift register has taken its byte } impl Default for Uart { fn default() -> Self { Self::new() } } impl Uart { pub fn new() -> Uart { Uart { clocks: 0, ctl: 0, holding: -1, echoes: VecDeque::new(), arriving: VecDeque::new(), taken: true, frame_start: -(1 << 30), frame_end: -(1 << 30), idle_wait: 0, received: VecDeque::new(), held: -1, last_data: 0, parerr: 0, overrun: 0, framerr: 0, rxbrk: 0, break_clocks: 0, } } // -- time --------------------------------------------------------------------------- /// One Timer 4 borrow. pub fn clock(&mut self, when: i64, pending: &mut u32) { self.clocks += 1; let n = self.clocks; if !self.taken && n - self.frame_start == TAKE { self.take(); } while let Some(&(at, _)) = self.echoes.front() { if at > n { break; } let frame = self.echoes.pop_front().unwrap().1; self.arriving.push_back((when + ECHO_DELAY, frame)); } #[allow(clippy::absurd_extreme_comparisons)] if !self.arriving.is_empty() && ECHO_DELAY == 0 { self.run_until(when, pending); } if self.ctl & TXBRK != 0 { self.break_clocks += 1; if self.break_clocks == BREAK_CLOCKS { self.rxbrk = 1; // DOC: a break of 24 bit times is recognised } } else { self.break_clocks = 0; } if n.rem_euclid(CLOCKS_PER_BIT) == 0 { self.bit_boundary(n); } self.set_level(pending); } /// Deliver the echoes whose delayed arrival is due by `tick` (Mikey calls this). pub fn run_until(&mut self, tick: i64, pending: &mut u32) { while let Some(&(at, _)) = self.arriving.front() { if at > tick { break; } let frame = self.arriving.pop_front().unwrap().1; if frame < 0 { self.start_bit(); } else { self.receive(frame); } } self.set_level(pending); } /// A bit boundary: start a frame if the holding register has a byte. fn bit_boundary(&mut self, n: i64) { if n - self.frame_start < FRAME_CLOCKS || self.holding < 0 || self.ctl & TXBRK != 0 { return; } let chained = self.frame_start + FRAME_CLOCKS == n; #[allow(clippy::absurd_extreme_comparisons)] if !chained && self.idle_wait < IDLE_START - 1 { self.idle_wait += 1; return; } self.idle_wait = 0; self.frame_start = n; self.frame_end = n + FRAME_CLOCKS + EMPTY; self.echoes.push_back((n, -1)); // the start bit reaches the receiver (-1: no frame) self.taken = false; #[allow(clippy::absurd_extreme_comparisons)] if TAKE == 0 { self.take(); } } /// The shift register takes the holding register: the 9th bit is fixed now. fn take(&mut self) { let data = self.holding.max(0); let ctl = self.ctl; let ninth = if ctl & PAREN != 0 { (ones(data) & 1) ^ if ctl & PAREVEN != 0 { 0 } else { 1 } } else { (ctl & PAREVEN) as i64 }; self.holding = -1; self.taken = true; // TEST ROM uart3: the frame comes back on the shared wire. self.echoes .push_back((self.frame_start + RECEIVED, data | (ninth << 8))); } /// The echo of a finished frame reaches the receiver. fn receive(&mut self, frame: i64) { let (data, ninth) = (frame & 0xFF, frame >> 8); let expect_even = self.ctl & PAREVEN != 0; if ((ones(data) + ninth) & 1) != if expect_even { 0 } else { 1 } { self.parerr = 1; } if self.received.is_empty() { self.received.push_back((data as u8, ninth as u8)); } else { // The receive data register is full: the frame stays in the shift register // (a second byte held) until it is read or the next start bit arrives. self.held = data | (ninth << 8); } } /// A frame starts on the wire: the receiver begins shifting it in. A finished /// frame still in the shift register is lost: the overrun (DOC: "a character has /// been received and the previously received character has not yet been read"). fn start_bit(&mut self) { if self.held >= 0 { self.held = -1; self.overrun = 1; } } // -- the interrupt (INTSET bit 4) ----------------------------------------------------- pub fn level(&self) -> bool { let ctl = self.ctl; (ctl & TXINTEN != 0 && self.holding < 0) || (ctl & RXINTEN != 0 && !self.received.is_empty()) } /// The reference's `_level`. pub fn set_level(&self, pending: &mut u32) { if self.level() { *pending |= 0x10; } } // -- registers --------------------------------------------------------------------- pub fn txempty(&self) -> bool { self.holding < 0 && self.taken && self.clocks >= self.frame_end } pub fn read_serctl(&self) -> u8 { let mut value = if self.holding < 0 { 0x80 } else { 0 }; if let Some(&(_, ninth)) = self.received.front() { value |= 0x40 | ninth; } if self.txempty() { value |= 0x20; } value |= (self.parerr << 4) | (self.overrun << 3) | (self.framerr << 2); value | (self.rxbrk << 1) } pub fn read_serdat(&mut self) -> u8 { if let Some((data, _)) = self.received.pop_front() { self.last_data = data; if self.held >= 0 { // the shift register's finished frame moves up self.received .push_back(((self.held & 0xFF) as u8, (self.held >> 8) as u8)); self.held = -1; } } self.last_data // HYPOTHESIS: an empty receiver reads its last byte again } pub fn write_serctl(&mut self, value: u8, _now: i64, pending: &mut u32) { self.ctl = value & 0xF7; if value & RESETERR != 0 { self.parerr = 0; self.overrun = 0; self.framerr = 0; self.rxbrk = 0; } self.set_level(pending); } pub fn write_serdat(&mut self, value: u8, _now: i64, pending: &mut u32) { self.holding = value as i64; // TEST ROM uart2 4: a full holding register is replaced self.set_level(pending); } }