# SPDX-License-Identifier: MIT """Quick tests of the audio mix, the WAV writer and the UART (milestone 6), no ROM.""" from __future__ import annotations import struct from lynx_python.board import Lynx from lynx_python.machine import LYNX1, LYNX2 from lynx_python.sound import wav_bytes def _outputs(board: Lynx, values: tuple[int, int, int, int]) -> None: for c, v in enumerate(values): board.mikey.audio_regs[c * 8 + 2] = v & 0xFF def test_lynx1_is_mono_on_both_sides(): board = Lynx(machine=LYNX1) _outputs(board, (10, -3, 0, 1)) assert board.mikey.mix() == (8 * 64, 8 * 64) def test_lynx2_mstereo_disables_per_side(): board = Lynx(machine=LYNX2) _outputs(board, (10, 20, 30, 40)) board.mikey.write(0xFD50, 0x81) # channel 0 off on the right, channel 3 off on the left assert board.mikey.mix() == ((10 + 20 + 30) * 64, (20 + 30 + 40) * 64) def test_lynx2_attenuation_applies_where_mpan_selects(): board = Lynx(machine=LYNX2) _outputs(board, (30, 0, 0, 0)) board.mikey.write(0xFD40, 0xF5) # left 15/15, right 5/15 board.mikey.write(0xFD44, 0x01) # attenuation selected on the right only assert board.mikey.mix() == (30 * 64, 10 * 64) def test_wav_header_and_samples(): image = wav_bytes([1, -2, 3, -4], 32000) assert image[:4] == b"RIFF" and image[8:16] == b"WAVEfmt " channels, rate = struct.unpack_from(" None: uart = board.mikey.uart for _ in range(borrows): uart.clock(0) def test_uart_frame_echo_and_status(): board = Lynx() uart = board.mikey.uart assert uart.read_serctl() & 0xA0 == 0xA0 # DOC: TXRDY and TXEMPTY after reset uart.write_serctl(0x15, 0) # PAREN, TXOPEN, PAREVEN (even) uart.write_serdat(0x01, 0) assert not uart.read_serctl() & 0x80 _run_uart(board, 16) assert uart.read_serctl() & 0xA0 == 0x80 # holding taken, frame on the wire _run_uart(board, 120) uart.run_until(1 << 40) # the wire delay is in ticks status = uart.read_serctl() assert status & 0x40 and status & 0x01 and not status & 0x10 # echo, 9th bit 1, no error assert uart.read_serdat() == 0x01 def test_uart_interrupt_latch_cleared_only_by_intrst(): board = Lynx() mikey = board.mikey mikey.write(0xFD8C, 0x84) # TXINTEN with TXRDY = 1 assert mikey.irq_bits() & 0x10 mikey.write(0xFD8D, 0x55) # TXRDY falls: the pending bit stays (TEST ROM uart2 5) assert mikey.irq_bits() & 0x10 mikey.write(0xFD80, 0x10) assert not mikey.irq_bits() & 0x10