// SPDX-License-Identifier: MIT //! The reference's quick tests of the board pieces that need no ROM (tests/test_board.py, //! milestones 1 and 2) and of the audio mix, the WAV writer and the UART //! (tests/test_audio_uart.py, milestone 6), transcribed where they test behaviour. //! (test_hot_objects_stay_under_pypys_attribute_limit is a PyPy rule, not behaviour.) use lynx::cart::port::CartPort; use lynx::sound::wav_bytes; use lynx::{Lynx, Machine, LYNX1, LYNX2}; fn blank(machine: Machine) -> Lynx { Lynx::new(&[], 1024, machine, None, &[], 0) // the reference's Lynx() } #[test] fn mapctl_readback_rows() { // TEST ROM (LT page-mode): Lynx II $03 -> $F3, $83 -> $73; Lynx I returns what was written. assert_eq!((LYNX2.mapctl_readback)(0x03), 0xF3); assert_eq!((LYNX2.mapctl_readback)(0x83), 0x73); assert_eq!((LYNX1.mapctl_readback)(0x83), 0x83); } #[test] fn cart_port_shift_and_counter() { let data: Vec = (0..256 * 1024).map(|i| (i % 256) as u8).collect(); // 256 KiB, 1024-byte pages let mut port = CartPort::new(&data, 1024, &[], 0); port.set_data(1); port.set_strobe(1); port.set_strobe(0); port.set_data(0); for _ in 0..7 { port.set_strobe(1); port.set_strobe(0); } assert_eq!(port.shift, 0x80); // MSB first let read: Vec = (0..3).map(|_| port.read(0)).collect(); let want: Vec = (0..3).map(|i| data[0x80 * 1024 + i]).collect(); assert_eq!(read, want); port.set_strobe(1); // the strobe high resets the counter assert_eq!(port.counter, 0); } #[test] fn timer_borrows_on_the_n_plus_first_clock() { let mut board = blank(LYNX2); let timers = &mut board.hw_mut().mikey.timers; timers.write(6, 2, 2, 0); // CNT = 2 timers.write(6, 1, 0x88, 0); // interrupt enable, count, 1 µs, no reload timers.run_until(16 * 2); assert!(timers.timers[6].count == 0 && timers.pending == 0); timers.run_until(16 * 3); assert!(timers.timers[6].done != 0 && timers.pending == 0x40); } #[test] fn ctlb_write_injects_one_clock() { // TEST ROM (LT timers "CTLB RD/WR"): CNT=$80, write $0A -> CTLB=$08, CNT=$7F. let mut board = blank(LYNX2); let timers = &mut board.hw_mut().mikey.timers; timers.write(0, 2, 0x80, 0); timers.write(0, 1, 0x18, 0); timers.write(0, 3, 0x0A, 0); // Read in the low half of the 1 us source clock (B2 is the clock's level, Timer.ctlb). assert!(timers.read(0, 3, 8) & 0x0F == 0x08 && timers.read(0, 2, 8) == 0x7F); assert_eq!(timers.read(0, 3, 16) & 0x0F, 0x0C); // just after a counting edge: B2 = 1 } fn outputs(board: &mut Lynx, values: [i32; 4]) { for (c, v) in values.iter().enumerate() { board.hw_mut().mikey.audio_regs[c * 8 + 2] = (v & 0xFF) as u8; } } fn mikey_write(board: &mut Lynx, address: u16, value: u8) { let hw = board.hw_mut(); hw.mikey.write(address, value, &mut hw.port, &hw.ram); } #[test] fn lynx1_is_mono_on_both_sides() { let mut board = blank(LYNX1); outputs(&mut board, [10, -3, 0, 1]); assert_eq!(board.hw().mikey.mix(), (8 * 64, 8 * 64)); } #[test] fn lynx2_mstereo_disables_per_side() { let mut board = blank(LYNX2); outputs(&mut board, [10, 20, 30, 40]); mikey_write(&mut board, 0xFD50, 0x81); // channel 0 off on the right, channel 3 off on the left assert_eq!( board.hw().mikey.mix(), ((10 + 20 + 30) * 64, (20 + 30 + 40) * 64) ); } #[test] fn lynx2_attenuation_applies_where_mpan_selects() { let mut board = blank(LYNX2); outputs(&mut board, [30, 0, 0, 0]); mikey_write(&mut board, 0xFD40, 0xF5); // left 15/15, right 5/15 mikey_write(&mut board, 0xFD44, 0x01); // attenuation selected on the right only assert_eq!(board.hw().mikey.mix(), (30 * 64, 10 * 64)); } #[test] fn attenuation_floors_negative_values() { // Python's // floors: -7 * 5 // 15 = -3 (not -2): the transcription's div_euclid. let mut board = blank(LYNX2); outputs(&mut board, [-7, 0, 0, 0]); mikey_write(&mut board, 0xFD40, 0xF5); mikey_write(&mut board, 0xFD44, 0x01); assert_eq!(board.hw().mikey.mix(), (-7 * 64, -3 * 64)); } #[test] fn wav_header_and_samples() { let image = wav_bytes(&[1, -2, 3, -4], 32000, 2); assert!(&image[..4] == b"RIFF" && &image[8..16] == b"WAVEfmt "); let channels = u16::from_le_bytes([image[22], image[23]]); let rate = u32::from_le_bytes([image[24], image[25], image[26], image[27]]); assert_eq!((channels, rate), (2, 32000)); let samples: Vec = (0..4) .map(|i| i16::from_le_bytes([image[44 + 2 * i], image[45 + 2 * i]])) .collect(); assert_eq!(samples, vec![1, -2, 3, -4]); } #[test] fn uart_frame_echo_and_status() { let mut board = blank(LYNX2); let mikey = &mut board.hw_mut().mikey; let (uart, pending) = (&mut mikey.uart, &mut mikey.timers.pending); assert_eq!(uart.read_serctl() & 0xA0, 0xA0); // DOC: TXRDY and TXEMPTY after reset uart.write_serctl(0x15, 0, pending); // PAREN, TXOPEN, PAREVEN (even) uart.write_serdat(0x01, 0, pending); assert_eq!(uart.read_serctl() & 0x80, 0); for _ in 0..16 { uart.clock(0, pending); } assert_eq!(uart.read_serctl() & 0xA0, 0x80); // holding taken, frame on the wire for _ in 0..120 { uart.clock(0, pending); } uart.run_until(1 << 40, pending); // the wire delay is in ticks let status = uart.read_serctl(); assert!(status & 0x40 != 0 && status & 0x01 != 0 && status & 0x10 == 0); // echo, 9th bit 1, no error assert_eq!(uart.read_serdat(), 0x01); } #[test] fn uart_interrupt_latch_cleared_only_by_intrst() { let mut board = blank(LYNX2); mikey_write(&mut board, 0xFD8C, 0x84); // TXINTEN with TXRDY = 1 assert!(board.hw().mikey.irq_bits() & 0x10 != 0); mikey_write(&mut board, 0xFD8D, 0x55); // TXRDY falls: the pending bit stays (TEST ROM uart2 5) assert!(board.hw().mikey.irq_bits() & 0x10 != 0); mikey_write(&mut board, 0xFD80, 0x10); assert_eq!(board.hw().mikey.irq_bits() & 0x10, 0); } #[test] fn save_state_round_trip_on_a_blank_board() { // The transcription's own save states (src/state.rs): a state loads into a fresh console. let mut board = blank(LYNX2); board.hw_mut().ram[0x1234] = 0x56; board.hw_mut().mikey.timers.write(0, 1, 0x18, 0); let state = board.save_state(); let mut fresh = blank(LYNX2); fresh.load_state(&state).unwrap(); assert_eq!(fresh.hw().ram[0x1234], 0x56); assert_eq!(fresh.hw().mikey.timers.timers[0].ctla, 0x18); assert_eq!(fresh.save_state(), state); }