# SPDX-License-Identifier: MIT """Mikey's UART (ComLynx / REDEYE): transmitter, receiver of its own echo, interrupt. 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 docs/validation.md "M6 UART" for the tests, the values tried and the held-out results. """ from __future__ import annotations CLOCKS_PER_BIT = 8 # DOC: CLOCK4 / (TIMER4 + 1) / 8 FRAME_CLOCKS = 11 * CLOCKS_PER_BIT # DOC: 11-bit frames, back to back when chained BREAK_CLOCKS = 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. IDLE_START = 1 TAKE = 8 EMPTY = 8 RECEIVED = 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"). ECHO_DELAY = 600 # passing window 560-640 at RECEIVED = 84 (480 and 720 fail uart3 3) TXINTEN, RXINTEN, PAREN, RESETERR, TXBRK, PAREVEN = 0x80, 0x40, 0x10, 0x08, 0x02, 0x01 def _ones(value: int) -> int: return value.bit_count() class Uart: __slots__ = ( "arriving", "break_clocks", "clocks", "ctl", "echoes", "frame_end", "frame_start", "framerr", "held", "holding", "idle_wait", "last_data", "overrun", "parerr", "pending", "received", "rxbrk", "taken", ) # fmt: skip def __init__(self, pending) -> None: self.pending = pending # sets and reads INTSET bit 4 (Mikey's TimerBank) self.clocks = 0 self.ctl = 0 # DOC SERCTL reset 0 self.holding = -1 # -1 = empty (DOC: TXRDY is 1 after reset) self.echoes: list[tuple[int, int]] = [] # (UART clock received, 9-bit frame) self.arriving: list[tuple[int, int]] = [] # (tick it reaches the receiver, frame) self.taken = True # the frame in the shift register has taken its byte self.frame_start = -(1 << 30) self.frame_end = -(1 << 30) # UART clock at which TXEMPTY may rise again self.idle_wait = 0 self.received: list[tuple[int, int]] = [] # the receive data register (0 or 1) self.held = -1 # a finished 9-bit frame waiting in the receive shift register self.last_data = 0 self.parerr = self.overrun = self.framerr = self.rxbrk = 0 self.break_clocks = 0 # -- time --------------------------------------------------------------------------- def clock(self, when: int) -> None: """One Timer 4 borrow.""" self.clocks += 1 n = self.clocks if not self.taken and n - self.frame_start == TAKE: self._take() echoes = self.echoes while echoes and echoes[0][0] <= n: self.arriving.append((when + ECHO_DELAY, echoes.pop(0)[1])) if self.arriving and not ECHO_DELAY: self.run_until(when) if self.ctl & TXBRK: 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 % CLOCKS_PER_BIT == 0: self._bit_boundary(n) self._level() def run_until(self, tick: int) -> None: """Deliver the echoes whose delayed arrival is due by ``tick`` (Mikey calls this).""" arriving = self.arriving while arriving and arriving[0][0] <= tick: frame = arriving.pop(0)[1] if frame < 0: self._start_bit() else: self._receive(frame) self._level() def _bit_boundary(self, n: int) -> None: """A bit boundary: start a frame if the holding register has a byte.""" if n - self.frame_start < FRAME_CLOCKS or self.holding < 0 or self.ctl & TXBRK: return chained = self.frame_start + FRAME_CLOCKS == n if not chained and 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.append((n, -1)) # the start bit reaches the receiver (-1: no frame) self.taken = False if TAKE == 0: self._take() def _take(self) -> None: """The shift register takes the holding register: the 9th bit is fixed now.""" data = max(self.holding, 0) ctl = self.ctl if ctl & PAREN: ninth = (_ones(data) & 1) ^ (0 if ctl & PAREVEN else 1) else: ninth = ctl & PAREVEN self.holding = -1 self.taken = True # TEST ROM uart3: the frame comes back on the shared wire. self.echoes.append((self.frame_start + RECEIVED, data | (ninth << 8))) def _receive(self, frame: int) -> None: """The echo of a finished frame reaches the receiver.""" data, ninth = frame & 0xFF, frame >> 8 expect_even = self.ctl & PAREVEN if ((_ones(data) + ninth) & 1) != (0 if expect_even else 1): self.parerr = 1 if not self.received: self.received.append((data, ninth)) 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) def _start_bit(self) -> None: """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").""" if self.held >= 0: self.held = -1 self.overrun = 1 # -- the interrupt (INTSET bit 4) ----------------------------------------------------- def level(self) -> bool: ctl = self.ctl return bool((ctl & TXINTEN and self.holding < 0) or (ctl & RXINTEN and self.received)) def _level(self) -> None: if self.level(): self.pending(0x10) # -- registers --------------------------------------------------------------------- def txempty(self) -> bool: return self.holding < 0 and self.taken and self.clocks >= self.frame_end def read_serctl(self) -> int: value = 0x80 if self.holding < 0 else 0 if self.received: value |= 0x40 | self.received[0][1] if self.txempty(): value |= 0x20 value |= (self.parerr << 4) | (self.overrun << 3) | (self.framerr << 2) return value | (self.rxbrk << 1) def read_serdat(self) -> int: if self.received: self.last_data = self.received.pop(0)[0] if self.held >= 0: # the shift register's finished frame moves up self.received.append((self.held & 0xFF, self.held >> 8)) self.held = -1 return self.last_data # HYPOTHESIS: an empty receiver reads its last byte again def write_serctl(self, value: int, now: int) -> None: self.ctl = value & 0xF7 if value & RESETERR: self.parerr = self.overrun = self.framerr = self.rxbrk = 0 self._level() def write_serdat(self, value: int, now: int) -> None: self.holding = value # TEST ROM uart2 4: a full holding register is replaced self._level()