Add DSP mapping probe and ROM DTE startup handling

This commit is contained in:
Cryan209 committed 2026-09-04 22:35:40 +12:00
1 parent 1c11116568
commit b013186a88
5 files changed
+79735 -10

No files matched your search

+120 -3
View File
@@ -16,8 +16,11 @@ import struct
ORIGIN = 0x8000
BUFFER = 0x0300
SAMPLE_WORDS = 32
MAPPING_SAMPLE_WORDS = 16
CONTROL = (0x1357, 0x2468, 0xA55A, 0x5AA5, 0x0000, 0xFFFF, 0x8001, 0x7FFE)
HEADER = (0xC051, 1, 0, SAMPLE_WORDS, 0, 8, 0, 0)
MAPPING_HEADER = (0xC052, 2, 0, MAPPING_SAMPLE_WORDS, 0, 8,
MAPPING_SAMPLE_WORDS, 0)
COMPLETE = 0xD00E
@@ -69,6 +72,53 @@ def build_probe(*, mailbox: bool = False) -> RomProbe:
return RomProbe(tuple(words), control_address, halt)
def build_mapping_probe(*, mailbox: bool = False) -> RomProbe:
"""Compare low external program memory with the mapped C52 ROM window.
The program executes at 8000, so changing PMST.MP/MC cannot unmap the
executing probe. PMST is deliberately reduced to just MP/MC while the
external sample is taken, then cleared to map ROM for the internal sample.
This is a standalone takeover diagnostic and does not return to firmware.
"""
words = [0xBE41, 0xBC06, 0x8B89] # SETC INTM; LDP #6; MAR *,AR1
for index, value in enumerate(MAPPING_HEADER):
words.extend((0xAE00 | index, value))
words.extend((0xBF09, BUFFER + len(MAPPING_HEADER)))
control_references = []
# Known program-RAM control before changing the mapping.
words.extend((0xBF80, 0))
control_references.append(len(words) - 1)
words.extend((0xBB00 | (len(CONTROL) - 1), 0xA6A0))
# PMST.MP/MC=1: low program addresses are external.
words.extend((0xAE07, 0x0008, 0xBF80, 0x0000,
0xBB00 | (MAPPING_SAMPLE_WORDS - 1), 0xA6A0))
# PMST.MP/MC=0: the C52's 0000..0fff ROM is mapped.
words.extend((0xAE07, 0x0000, 0xBF80, 0x0000,
0xBB00 | (MAPPING_SAMPLE_WORDS - 1), 0xA6A0))
words.extend((0xBF80, 0))
control_references.append(len(words) - 1)
words.extend((0xBB00 | (len(CONTROL) - 1), 0xA6A0, 0xAE04, COMPLETE))
if mailbox:
words.extend((0xAE7C, 0x5200, 0xBF09, BUFFER))
poll = ORIGIN + len(words)
words.extend((0xBF0A, 0xFF57, 0x8B8A, 0x1080, 0x0880, 0x8B89,
0x907D, 0x4E7D, 0xE200, poll))
words.extend((0x0C7C, 0x005E, 0x0CA0, 0x005F, 0xB902, 0x8857,
0x697C, 0xB801, 0x907C, 0xBFA0, 0x5238, 0xE308, poll))
halt = ORIGIN + len(words)
words.extend((0x7980, halt))
control_address = ORIGIN + len(words)
for index in control_references:
words[index] = control_address
words.extend(CONTROL)
while len(words) % 8:
words.append(0x8B00)
return RomProbe(tuple(words), control_address, halt)
def inspect_buffer(words: list[int]) -> dict:
expected_length = len(HEADER) + len(CONTROL) * 2 + SAMPLE_WORDS
if len(words) != expected_length:
@@ -85,6 +135,63 @@ def inspect_buffer(words: list[int]) -> dict:
"interpretation": "A valid buffer proves execution and program-RAM reads. A ROM sample still needs mapping/protection checks; all-zero/all-one data is inconclusive."}
def inspect_mapping_buffer(words: list[int]) -> dict:
if len(words) != 56:
raise ValueError("expected 56 words")
before = words[8:16]
external = words[16:32]
internal = words[32:48]
after = words[48:56]
valid = words[:4] == list(MAPPING_HEADER[:4])
valid = valid and words[5:8] == list(MAPPING_HEADER[5:8])
valid = valid and words[4] == COMPLETE
valid = valid and before == after == list(CONTROL)
return {
"status": "mapping-samples-captured" if valid else "invalid-or-incomplete",
"control_before": before,
"external_sample": external,
"internal_sample": internal,
"control_after": after,
"complete": words[4] == COMPLETE,
"samples_differ": external != internal,
"rom_readable": valid and external != internal,
"interpretation": (
"Distinct stable samples prove that enabling the internal window changes "
"the low program mapping. Equal, uniform, or bus-like samples are inconclusive "
"without repeated captures and external-bus observation."
),
}
def simulate_mapping_probe(probe: RomProbe) -> dict:
from .dsp import NativeC5x
rom = tuple((0x1234 + i * 0x0193) & 0xFFFF for i in range(0x1000))
external = tuple((0xA55A ^ i * 0x0101) & 0xFFFF
for i in range(MAPPING_SAMPLE_WORDS))
core = NativeC5x(probe)
try:
core.load_rom(struct.pack("<4096H", *rom))
core.load_program(struct.pack(f"<{len(external)}H", *external), 0)
core.set_pc(ORIGIN)
for _ in range(300):
if core.state()["pc"] == probe.halt_address:
break
core.step(1)
else:
raise RuntimeError("mapping probe did not complete")
result = inspect_mapping_buffer(
[core.data(BUFFER + i) for i in range(56)])
result.update(
external_matches_fixture=result["external_sample"] == list(external),
internal_matches_fixture=result["internal_sample"] == list(rom[:MAPPING_SAMPLE_WORDS]),
protection_modeled=False,
io_events=core.io_events(),
)
return result
finally:
core.close()
def simulate_probe(probe: RomProbe, *, rom_mapped: bool) -> dict:
from .dsp import NativeC5x
rom = tuple((0x1234 + i * 0x0193) & 0xFFFF for i in range(0x1000))
@@ -183,24 +290,34 @@ def main() -> int:
parser.add_argument("--reference", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True,
help="new output directory; must not already exist")
parser.add_argument("--mapping-test", action="store_true",
help="compare MP/MC external and internal low mappings")
args = parser.parse_args()
if args.output.exists():
parser.error("output directory already exists; choose a new directory")
probe = build_probe()
probe = build_mapping_probe() if args.mapping_test else build_probe()
# Verify before writing artifacts. mkdir without exist_ok prevents source
# collisions, accidental overwrites and ambiguous stale manifests.
result = {"hardware_tested": False, "uploadable_sdl_image": False,
"program_origin": ORIGIN, "result_buffer": BUFFER, "result_words": 56,
"kernel_sha256": sha256(probe.payload).hexdigest(),
"download": verify_download(args.reference, probe),
"simulations": [simulate_probe(probe, rom_mapped=m) for m in (True, False)],
"simulations": ([simulate_mapping_probe(probe)] if args.mapping_test else
[simulate_probe(probe, rom_mapped=m) for m in (True, False)]),
"limitations": ["Raw C5x kernel only; do not send this file as an SDL update.",
"Actual modem bootstrap entry and serial readback are not connected.",
"C5x core models an unprotected C52 ROM; C51 size and mask-ROM protection are not tested.",
"Reference IDSDL302 is a modified release, not a verified stock image."]}
if not result["download"]["matches_kernel"] or not result["download"]["checksum_matches"]:
raise RuntimeError("download verification failed")
if any(s["status"] != "sample-captured" or not s["sample_matches_fixture"] for s in result["simulations"]):
if args.mapping_test:
sample = result["simulations"][0]
if (sample["status"] != "mapping-samples-captured" or
not sample["external_matches_fixture"] or
not sample["internal_matches_fixture"]):
raise RuntimeError("mapping kernel verification failed")
elif any(s["status"] != "sample-captured" or not s["sample_matches_fixture"]
for s in result["simulations"]):
raise RuntimeError("kernel verification failed")
args.output.mkdir(parents=True)
(args.output / "probe-c5x.bin").write_bytes(probe.payload)
+48 -5
View File
@@ -305,6 +305,7 @@ class CourierMachine:
self._rx_started_at = 0
self._rx_edge_at = 0
self._rom_rx_bit = 0
self._rom_dte_opened = False
self._previous_address: int | None = None
self.executed: Counter[int] = Counter()
self.last_addresses: deque[int] = deque(maxlen=64)
@@ -961,6 +962,34 @@ class CourierMachine:
# iret so the two never nest.
self._tick_owed = True
self.instructions += 1
if (
self._rom_tick
and not self._rom_dte_opened
and self._terminal_attached
and self.instructions >= DTE_READY_INSTRUCTIONS
and bytes(_uc.mem_read(0x0695, 1))[0] & 1
):
# The ROM's board/DSP startup normally posts the event whose
# handler at 0x8246b changes the serial state from startup to
# command mode and opens the buffered DTE channel. A ROM run
# has no executable C52 attached, so complete that hardware-
# facing half once the firmware has accepted its EEPROM
# settings and a DTE exists. Command parsing and result-code
# generation remain in the ROM.
_uc.mem_write(0x033E, b"\x02")
flags = bytes(_uc.mem_read(0x0B88, 1))[0]
_uc.mem_write(0x0B88, bytes((flags | 0x40,)))
_uc.mem_write(0x0B7E, b"\x00")
_uc.mem_write(0x0B7F, (0x9B0A).to_bytes(2, "little"))
output = bytes(_uc.mem_read(0x0EA7, 1))[0]
_uc.mem_write(0x0EA7, bytes((output | 0x01,)))
_uc.mem_write(0x3424, b"\x01")
_uc.mem_write(0x3CA4, (0x3800).to_bytes(2, "little"))
_uc.mem_write(0x0050, b"\x23\x0f\x00\x80")
self.uart.control = 0x21
self._rom_dte_opened = True
if self._rom_dte_opened and address == 0x815F0:
self._capture_serial(_uc.reg_read(UC_X86_REG_AX) & 0xFF)
if self.console is not None and not self.instructions % self.console.poll_instructions:
typed = self.console.poll()
if typed:
@@ -1275,6 +1304,10 @@ class CourierMachine:
and self.serial_rx
and interrupts_on
and self.instructions >= DTE_TYPING_INSTRUCTIONS
and (
not self._rom_tick
or int.from_bytes(_uc.mem_read(0x026A, 2), "little") == 0x9A06
)
):
# Put the character on the wire before handing it over.
# The ROM's callback chain watches the raw line for the
@@ -1288,6 +1321,13 @@ class CourierMachine:
self._rom_rx_bit = 0
elif self.instructions - self._rx_started_at >= START_BIT_INSTRUCTIONS:
self.uart.holding = False
if self._rom_dte_opened:
# Temporary autobaud handlers reuse type 0x14.
# Once the board-side open event has completed,
# the received character belongs at the ROM's
# final integrated-UART ISR.
_uc.mem_write(0x0050, b"\x23\x0f\x00\x80")
self.uart.control = 0x21
self.uart.deliver(self.serial_rx.popleft())
_uc.emu_stop()
if (
@@ -1462,7 +1502,7 @@ class CourierMachine:
# 0x12. Bit 6 is the active-low DTE DTR input: an
# attached terminal pulls it low, which opens the normal
# receive path instead of installing the discard callback.
if self._dte_asserted():
if self._terminal_attached:
value &= ~0x40
else:
value |= 0x40
@@ -1474,9 +1514,12 @@ class CourierMachine:
else:
value &= ~STRAP_SENSE_BIT
if self.uart is not None and port == 0x14 and size == 1:
# The raw receive line, which the ROM polls as well as
# reading the buffer. Bit 0 is the mark/space level.
if not self._dte_asserted():
# DTR is high while an ordinary terminal remains
# attached. The serial-PnP state machine deliberately
# looks for a high-to-low strobe followed by RX-ready in
# a narrow counter window; do not manufacture that Win95
# enumeration sequence for plain queued AT input.
if self._terminal_attached:
value |= 0x01
else:
value &= ~0x01
@@ -1967,7 +2010,7 @@ class CourierMachine:
0x1C77, 0x0283, 0x0285, 0x026A, 0x0313,
0x031A, 0x0607, 0x0695, 0x0EA7, 0x0B7C,
0x0B7E, 0x0B88, 0x0A96, 0x033E, 0x3424,
0x3CA4, 0x0298)
0x3CA4, 0x0298, 0x026E, 0x0B8E)
},
panel=self.panel.status(),
nvram=nvram_result,
File diff suppressed because it is too large. Load diff
+10 -1
View File
@@ -73,7 +73,16 @@ it to 0 and capture the internal window, with known reads from `8000` bracketing
both. A stable, nonuniform internal sample distinct from the external control
proves readable ROM. Uniform or bus-like data is not by itself proof of
protection; it must be repeated and compared with bus traces and the external
control. The current blocker is physical delivery/control of the probe or a
control. Generate this raw takeover kernel with:
```sh
python -m courier_emu.dsp_probe --mapping-test \
--reference IDSDL302.ROM --output artifacts/dsp-rom-mapping-v1
```
Its 56-word result is header, known control, 16 external words from address
zero, 16 internal-window words from address zero, and the repeated control.
The current blocker is physical delivery/control of the probe or a
C5x-compatible JTAG connection, not the `TBLR` read primitive.
## Reproducible offline probe
+22 -1
View File
@@ -3,7 +3,8 @@ from pathlib import Path
import pytest
from courier_emu.dsp_probe import (
COMPLETE, CONTROL, HEADER, build_probe, inspect_buffer, simulate_probe, verify_download,
COMPLETE, CONTROL, HEADER, build_mapping_probe, build_probe, inspect_buffer,
inspect_mapping_buffer, simulate_mapping_probe, simulate_probe, verify_download,
)
REFERENCE = Path(__file__).resolve().parents[1] / "IDSDL302.ROM"
@@ -23,6 +24,26 @@ def test_probe_reads_rom_and_distinguishes_external_mapping():
assert not result["rom_access_proven"]
def test_mapping_probe_switches_between_external_and_internal_program_zero():
probe = build_mapping_probe()
result = simulate_mapping_probe(probe)
assert result["status"] == "mapping-samples-captured"
assert result["control_before"] == result["control_after"] == list(CONTROL)
assert result["external_matches_fixture"]
assert result["internal_matches_fixture"]
assert result["samples_differ"] and result["rom_readable"]
assert not result["protection_modeled"]
assert result["io_events"] == []
def test_mapping_probe_rejects_equal_samples_as_inconclusive():
words = list((0xC052, 2, 0, 16, COMPLETE, 8, 16, 0))
words += list(CONTROL) + [0xAAAA] * 16 + [0xAAAA] * 16 + list(CONTROL)
result = inspect_mapping_buffer(words)
assert result["status"] == "mapping-samples-captured"
assert not result["samples_differ"] and not result["rom_readable"]
def test_reference_cpu_transfers_every_probe_byte_and_checksum():
pytest.importorskip("unicorn")
if not REFERENCE.exists():