1
0

UART simulation

This commit is contained in:
Aiden
2026-05-25 22:22:05 +10:00
parent 4b50d0e98f
commit c3eb09ddc8
8 changed files with 198 additions and 15 deletions

View File

@@ -1,7 +1,15 @@
import argparse
import unittest
from h8536.emulator.rx_probe import frame_checksum, frame_checksum_ok, parse_frame
from h8536.emulator import H8536Emulator, SCI1_RDR, SCI1_SSR, SCI_SSR_ORER, SCI_SSR_RDRF
from h8536.emulator.rx_probe import RunContext, UartTiming, _inject_frame_uart_timed, frame_checksum, frame_checksum_ok, parse_frame
def rom_with_reset(*, reset: int = 0x1000, size: int = 0x1020) -> bytearray:
rom = bytearray([0xFF] * size)
rom[0:2] = reset.to_bytes(2, "big")
rom[reset : reset + 2] = b"\x20\xFE" # BRA self
return rom
class EmulatorRxProbeTest(unittest.TestCase):
@@ -22,6 +30,23 @@ class EmulatorRxProbeTest(unittest.TestCase):
with self.assertRaises(argparse.ArgumentTypeError):
parse_frame("04 00 00 40")
def test_uart_timed_injection_does_not_wait_for_rdrf_consumption(self):
emulator = H8536Emulator(bytes(rom_with_reset()), clock_hz=10_000_000)
context = RunContext()
steps, reason = _inject_frame_uart_timed(
emulator,
b"\x11\x22",
timing=UartTiming(baud=38_400),
max_steps_per_gap=1000,
context=context,
)
self.assertEqual(reason, "frame_injected_uart_timing")
self.assertGreater(steps, 0)
self.assertEqual(emulator.memory.read8(SCI1_RDR), 0x11)
self.assertEqual(emulator.memory.read8(SCI1_SSR) & (SCI_SSR_RDRF | SCI_SSR_ORER), SCI_SSR_RDRF | SCI_SSR_ORER)
if __name__ == "__main__":
unittest.main()

View File

@@ -12,6 +12,7 @@ from h8536.emulator import (
SCI_SSR_RDRF,
SCI_SSR_TDRE,
SCI1,
UartTiming,
)
@@ -66,6 +67,22 @@ class SciTimingTest(unittest.TestCase):
self.assertEqual(sci.tx_frames, [HEARTBEAT_FRAME])
self.assertTrue(sci.saw_heartbeat())
def test_inject_rx_sets_overrun_if_rdrf_is_still_full(self):
sci = SCI1()
sci.inject_rx(0x11)
sci.inject_rx(0x22)
self.assertEqual(sci.read(SCI1_SSR) & (SCI_SSR_RDRF | SCI_SSR_ORER), SCI_SSR_RDRF | SCI_SSR_ORER)
self.assertEqual(sci.rdr, 0x11)
def test_uart_8n1_38400_byte_timing(self):
timing = UartTiming(baud=38_400)
self.assertEqual(timing.bits_per_character, 10)
self.assertAlmostEqual(timing.micros_per_character(), 260.416666, places=3)
self.assertEqual(timing.cycles_per_character(10_000_000), 2604)
if __name__ == "__main__":
unittest.main()