Files
courier-emu/tools/executed_addresses.py
T
Cryan209andClaude Opus 5 4772fbe1a9 AT&T1 reaches the datapump through the test flag, not the call gate
Tools first: tools/x86_disasm.py wraps capstone over CourierRom.at so an
80186 routine can be read at the same physical addresses the traces
print, and tools/executed_addresses.py dumps the whole track_executed set
and diffs two runs as contiguous spans. hot_addresses is a top-20 profile
and cannot say whether a branch ran at all; the diff can, and it is what
found everything below. AT&T0 executed 2,885 addresses, AT&T1 3,745, and
&T1 against &T8 differs by 10.

The &T dispatch is a table of far-call thunks at 875d8, four bytes apart.
Slot 0 -> 875f8 and slot 1 -> 876b1 are confirmed executed by AT&T0 and
AT&T1, slot 7 -> 87738 by AT&T8. The middle slots are not named here
because nothing ran them.

[0x685] is the test-mode flag byte. Every access to it in the image - 51
sites - agrees on the bits:

  0x01  analogue loopback     876c5 (&T1), 87752 (&T8)
  0x02  a second loopback     876eb
  0x04  a third               8770e, 8825c
  0x08  self test             87752 as |9, 8772c as |0c
  0x40  already-online form   876bd, 8774b
  0x80  test armed            87764
  0x07  any loopback, a gate  833bb, 88236, a4fea, a6abd

So the modes differ by bit, and the test pattern is bit 3, not bit 0.
[0x686] is the self-test error counter: &T8 zeroes it, and &T0's handler
does `test [0x685],8 / jz / call 8776f`, which reads [0x686] and prints
it. &T1 sets bit 0 alone - no internal pattern and no counter.

The part that corrects earlier work here: the gate at 8b863 that
datapump-dispatch-gate.md documents returns carry *clear* under &T1.
8bbc2, the carry-clear target, is in the &T1-only set and 8bbaa's carry
branch is not taken. The datapump comes in through 8b84f instead:

  8bbc2  call 8b84f
  8bbc5  jne  8bbca          taken
  8b84f  test [0xa96],2      jnz ret
  8b856  test [0x5a5],1      jnz ret
  8b85d  test [0x685],1      the loopback bit leaves ZF clear

8b85d is in the &T1-only set. The test flag is what routes to 8bbca and
the 344-address block at 8bbe9..8bd40 that selects the datapump, loads
overlay 6 and publishes 0010:5001. &T0 confirms the shape by absence: it
runs slot 0 and 875fb and never reaches 87438, 8b85d or 8bbc2.

Two sites ran under &T1 that gate against the test rather than for it,
and are where to look next for why the DTE data path never opens:

  a4d4c  test [0x685],0x80 / and [0x685],0x7f / stc / ret
         the armed bit is consumed and cleared on first pass, one shot
  a6abd  test [0x685],7 / jnz -> stc; ret
         refuses the jump through cs:[bx+0x1dbe] while any loopback is up

No behaviour changes in this commit; both tools are smoke-tested against
the runs above and reproduce them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-16 12:07:18 +12:00

91 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Every 80186 address one run executed, and the diff between two runs.
`hot_addresses` is a top-20 profile, which cannot answer "did this branch run
at all". The machine already counts every address under `track_executed`; this
exposes the whole set and diffs it, which is the cheapest way to find the code
one AT command takes and another does not.
PYTHONPATH=. python3 tools/executed_addresses.py IDSDL302.ROM \
--at 'AT&T1' --out t1.json
PYTHONPATH=. python3 tools/executed_addresses.py --diff t0.json t1.json
The diff prints contiguous runs rather than bare addresses, because a routine
that ran shows up as a span and a branch that did not shows up as a gap. Feed
the interesting spans to `x86_disasm.py`.
Per-address counting disables the native batches, so a run takes minutes where
the same limit through `./courier run` takes seconds. Ask for the smallest
instruction limit that still reaches what you are looking for.
"""
from __future__ import annotations
import argparse
import json
from courier_emu.cli import DIP_PRESETS, load_image
from courier_emu.machine import CourierMachine
def executed(image: str, command: str, limit: int, preset: str) -> dict[str, int]:
"""Run one AT command and return {address: times executed}, hex keys."""
machine = CourierMachine(
load_image(image),
track_executed=True,
with_dsp=True,
serial_input=command.encode("ascii") + b"\r" if command else b"",
dip_closed=frozenset(DIP_PRESETS[preset]),
)
machine.run(limit)
return {f"{address:05x}": count for address, count in machine.executed.items()}
def runs(addresses, gap: int = 16):
"""Group sorted addresses into spans, breaking on a gap of more than `gap`."""
addresses = sorted(addresses)
if not addresses:
return
first = previous = addresses[0]
for address in addresses[1:]:
if address - previous > gap:
yield first, previous
first = address
previous = address
yield first, previous
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("image", nargs="?")
parser.add_argument("--at", default="", metavar="COMMAND",
help="AT command to send after boot")
parser.add_argument("--instructions", type=int, default=40_000_000)
parser.add_argument("--dip-preset", default="dedicated-line",
choices=sorted(DIP_PRESETS))
parser.add_argument("--out", metavar="PATH", help="write the counts as JSON")
parser.add_argument("--diff", nargs=2, metavar=("BASE", "OTHER"),
help="report addresses in OTHER that BASE never ran")
args = parser.parse_args()
if args.diff:
base = set(json.load(open(args.diff[0])))
other = set(json.load(open(args.diff[1])))
only = [int(address, 16) for address in other - base]
spans = list(runs(only))
print(f"{len(only)} addresses in {len(spans)} runs")
for low, high in spans:
print(" %05x-%05x (%d)" % (low, high, high - low + 1))
return
if not args.image:
parser.error("an image is required unless --diff is given")
counts = executed(args.image, args.at, args.instructions, args.dip_preset)
if args.out:
with open(args.out, "w") as handle:
json.dump(counts, handle)
print(f"{args.at or 'no command'}: {len(counts)} addresses")
if __name__ == "__main__":
main()