1
0

more serial improvements

This commit is contained in:
Aiden
2026-05-25 16:32:58 +10:00
parent 6ceed81765
commit 56829b6e0b
10 changed files with 16843 additions and 122 deletions

View File

@@ -2,6 +2,7 @@ import json
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from h8536.serial_pseudocode import (
SerialPseudocodeOptions,
@@ -13,6 +14,27 @@ from h8536.serial_pseudocode import (
def candidate_payload() -> dict:
return {
"instructions": [],
"dtc_vectors": [],
"sci": {
"channels": {
"SCI1": {
"configurations": [
{
"mode": "async",
"mode_summary": "async 8-bit even parity 1 stop",
"smr": 0x24,
"smr_hex": "H'24",
"brr": 0x07,
"brr_hex": "H'07",
"scr": 0x3C,
"scr_hex": "H'3C",
"clock_source": "internal",
"baud_bps": None,
},
],
},
},
},
"sci_protocol": {
"manual_references": [
"Manual/0900766b802125d0.md:15794 RDR receive data register",
@@ -87,8 +109,20 @@ def candidate_payload() -> dict:
"confidence_reason": "RX count, copy, and checksum-validation evidence were observed",
"caveat": "candidate frame means six consecutive bytes, not a proven delimited packet",
"comment": "candidate/evidence-supported SCI1 6-byte RX frame hypothesis",
"rx_error_handling": {
"error_latch_address": 0xFAA4,
"error_latch_address_hex": "H'FAA4",
"error_latch_bit": 7,
"fallthrough_to_rx_byte_path": True,
"rdrf_clear_before_rdr_read": True,
"summary": "SCI1 ERI marks a physical receive error and continues into RXI byte capture.",
"manual_caveat": "The ROM clears RDRF before reading RDR; preserve that observed order.",
"evidence_addresses_hex": ["H'BB57", "H'BB69", "H'BB6D"],
},
"evidence_addresses_hex": {
"rx_rdr_read": ["H'BB6D"],
"rx_rdrf_clear_before_rdr_read": ["H'BB69", "H'BB6D"],
"rx_eri_falls_through_to_rxi": ["H'BB57", "H'BB5B", "H'BB5F", "H'BB63", "H'BB69", "H'BB6D"],
"rx_xor_checksum_validation": ["H'BBD6", "H'BBEC"],
},
},
@@ -121,6 +155,9 @@ class SerialPseudocodeTest(unittest.TestCase):
self.assertIn("focused SCI RX/TX pseudocode from rom.json", text)
self.assertIn("SCI1 TX 6-byte frame at H'F858-H'F85D", text)
self.assertIn("SCI1 RX 6-byte frame captured at H'F868-H'F86D", text)
self.assertIn("8E1 SCI characters carry the 6 protocol bytes", text)
self.assertIn("SMR=H'24, BRR=H'07, SCR=H'3C", text)
self.assertIn("No SCI1 RXI/TXI DTC vector entries are present", text)
self.assertIn("MAX202 pin 11 traces to H8 pin 66", text)
self.assertIn("Manual/0900766b802125d0.md:15823 TDR transmit data register", text)
self.assertIn("#define TX_FRAME(n) MEM8[(u16)(0xF858u + (n))]", text)
@@ -132,6 +169,9 @@ class SerialPseudocodeTest(unittest.TestCase):
self.assertIn("SCI1_SSR &= (u8)~SCI_SSR_RDRF;\n byte = SCI1_RDR;", text)
self.assertIn("RX_CAPTURE(RX_INDEX) = byte;", text)
self.assertIn("return sci1_process_rx_candidate_frame();", text)
self.assertIn("RX_ERROR_LATCH |= RX_ERROR_LATCH_PHYSICAL_ERROR;", text)
self.assertIn("sci1_rx_byte_received_candidate_isr();", text)
self.assertIn("RX error handling: SCI1 ERI marks a physical receive error", text)
self.assertIn("rx_xor_checksum_validation: H'BBD6, H'BBEC", text)
def test_generates_candidate_protocol_semantics_switch(self):
@@ -145,6 +185,110 @@ class SerialPseudocodeTest(unittest.TestCase):
self.assertIn("case 0x01u:", text)
self.assertIn("candidate_read_value(logical_index, value);", text)
def test_surfaces_refined_semantic_candidates(self):
analysis = {
"protocol_semantics": [
{
"confidence": "medium",
"confidence_score": 0.7,
"commands": [
{
"command_value": 0,
"command_value_hex": "0x00",
"name_candidate": "set_value_acked",
"summary": "candidate acknowledged set",
"effects": [
{
"kind": "table_write_candidate",
"target_candidate": "primary_value_table_candidate",
"source_candidate": "RX[3:4] value bytes",
"table_base_hex": "H'E000",
}
],
}
],
"command_effects": [
{
"command_value": 0,
"command_value_hex": "0x00",
"name_candidate": "set_value_acked",
"summary": "Candidate acknowledged set writes value bytes.",
"effects": [
{
"kind": "table_write_candidate",
"target_candidate": "primary_value_table_candidate",
"source_candidate": "RX[3:4] value bytes",
"table_base_hex": "H'E000",
"evidence_addresses_hex": ["H'C010"],
}
],
"evidence_addresses_hex": ["H'C000"],
}
],
"response_schemas": [
{
"response_id": "response_at_C030",
"bytes": [
{"byte": "byte0", "source_expression": "0x04"},
{"byte": "byte1", "source_expression": "RX[1]"},
],
"evidence_addresses_hex": ["H'C01C", "H'C024"],
}
],
"logical_table_map_candidates": [
{
"name_candidate": "primary_value_table_candidate",
"logical_base_address_hex": "H'E000",
"element_candidate": "word_value",
"observed_accesses": ["write"],
"evidence_addresses_hex": ["H'C010"],
}
],
"state_variable_candidates": [
{
"name_candidate": "serial_session_flags_candidate",
"address": 0xFAA2,
"address_hex": "H'FAA2",
"read_count": 1,
"write_count": 2,
"bit_candidates": [7],
"evidence_addresses_hex": ["H'C018"],
}
],
"retry_error_model": {
"checksum_failure_path": {
"condition_candidate": "0x5A-seeded XOR over RX[0..4] differs from RX[5]",
"error_target": "loc_BE29",
},
"retry_path": {
"counter_address_hex": "H'FAA6",
"threshold_candidate": 2,
"summary": "Candidate retry path stages a command 0x07 response.",
},
"command_0x07_path": {
"summary": "Candidate explicit command 0x07 path copies previous TX frame bytes.",
},
"evidence_addresses_hex": ["H'BBD6", "H'BE29"],
},
}
]
}
with patch("h8536.serial_pseudocode.analyze_serial_semantics", return_value=analysis):
text = generate_serial_pseudocode(candidate_payload())
self.assertIn("command effects:", text)
self.assertIn("effect: table_write_candidate; target primary_value_table_candidate", text)
self.assertIn("response schemas:", text)
self.assertIn("response_at_C030: byte0=0x04; byte1=RX[1]", text)
self.assertIn("table map candidates:", text)
self.assertIn("primary_value_table_candidate at H'E000", text)
self.assertIn("state variable candidates:", text)
self.assertIn("serial_session_flags_candidate H'FAA2: reads 1, writes 2; bits 7", text)
self.assertIn("retry/error model candidate:", text)
self.assertIn("checksum path: 0x5A-seeded XOR over RX[0..4] differs from RX[5] -> loc_BE29", text)
self.assertIn("candidate effect: table_write_candidate; target primary_value_table_candidate", text)
def test_tx_only_option_omits_rx_functions(self):
text = generate_serial_pseudocode(
candidate_payload(),