# Trace schema (version 1) A trace is the observable behaviour of a 68000 core written down one processor boundary at a time. Two cores that produce equal traces for the same program and host are, as far as software can tell, the same CPU. This page is the contract for producing such a trace in any language so that `first_trace_divergence` can compare it against this core; everything here is implemented by `src/m68000_python/trace.py`, and if the two disagree the code is the specification and this page has a bug. `examples/reference_trace.jsonl` is a committed trace of a short program, regenerated by `examples/reference_trace.py` and checked fresh by the test suite. ## File format JSON Lines: UTF-8 text, one JSON object per line, `\n` terminated, blank lines ignored. Objects are written with sorted keys and no whitespace by this package; readers must not depend on either. Each line is one **record**, and records are aligned by line position when two traces are compared. The `sequence` field is carried for humans and is never used for alignment. ## Record | Key | Type | Meaning | | --- | --- | --- | | `version` | integer | Always `1` for this schema. A reader rejects any other value. | | `sequence` | integer >= 0 | Producer-local counter. Informational only. | | `kind` | string | What the boundary was; one of the five values below. | | `cycles` | integer > 0 | Clocks the boundary consumed, exactly as `step()` returns them. | | `instruction` | object or `null` | The instruction executed at this boundary; `null` for every other kind, and permitted to be `null` for an instruction boundary when the producer cannot disassemble. | | `before` | state object | Complete processor state at the boundary's start. | | `after` | state object | Complete processor state at its end. | | `accesses` | array, optional | Every bus access the boundary made, in order, as `[kind, address, value, size]`: kind `"r"` or `"w"`, a 24-bit address, the value read or written, size 1 or 2. Present only when the producer recorded accesses; absent means *not recorded*, not *none*. | No other keys are allowed. ### `kind` | Value | When | | --- | --- | | `instruction` | The instruction in IR ran, with any exception it raised itself (address error, TRAP, illegal, ...). | | `trace` | The trace exception left by the previous instruction was taken: 34 clocks. | | `interrupt` | An interrupt was accepted: 44 clocks plus the E-clock wait of an autovector. | | `stopped_idle` | The CPU was inside STOP and nothing could end it: a 4-clock idle step. | | `halted_idle` | The CPU was halted by a double bus fault: a 4-clock idle step. | The kind is decided from `before` in this order: `halted`, then `trace_pending`, then an `ipl` above the mask in `sr` (or 7 with `nmi_edge`), then `stopped`, else `instruction` (`next_boundary`). A producer whose `kind` disagrees with its own `before` state is malformed. ### `instruction` | Key | Type | Required | Meaning | | --- | --- | --- | --- | | `address` | integer | yes | The instruction's address (`before.pc`). | | `data` | lowercase hex string | yes | Every byte the instruction occupies, extension words included, e.g. `"33c000003000"`. | | `mnemonic` | string | optional | The mnemonic as this package's disassembler prints it, in MAME's spelling. | | `operands` | array of strings | optional | Operand texts as the disassembler prints them. | `mnemonic` and `operands` are present together or absent together. **A producer in another language should omit both.** The reader decodes `data` at `address` with this package's disassembler and fills them in, so a port never has to reproduce the reference formatting; it only has to get the bytes right. A record whose `data` is not exactly one instruction, or whose text does not match its bytes, is rejected. ### State object Every key below is required, none may be omitted, and no others are allowed. Integers are unsigned; booleans are JSON `true`/`false`. These are the fourteen fields of `CPUState` ([cpu-state](cpu-state.md)) and the definition of "processor state" for equivalence purposes. | Key | Type | Meaning | | --- | --- | --- | | `d` | array of 8 integers 0..0xFFFFFFFF | D0-D7 | | `a` | array of 7 integers 0..0xFFFFFFFF | A0-A6 | | `usp`, `ssp` | 0..0xFFFFFFFF | the two stack pointers; A7 is whichever `sr`'s S bit selects | | `sr` | 0..0xFFFF, bits 14, 12, 11, 7-5 zero | the status register | | `pc` | 0..0xFFFFFFFF | the address of the instruction in `ir` | | `ir`, `irc` | 0..0xFFFF | the prefetch queue: that instruction's first word and the word after it | | `ipl` | 0..7 | the interrupt level as the host last set it | | `nmi_edge` | boolean | a level 7 edge seen and not yet taken | | `trace_pending` | boolean | the next step is the trace exception | | `stopped`, `halted` | boolean | inside STOP; halted by a double bus fault | | `clock` | integer >= 0 | clocks run so far, the sum of every step | Memory and devices are host state and are not in the trace; equality of traces implies equality of every bus access the CPU made only when both cores ran the same host, which the `accesses` array lets a reader check. ## Example The first record of `examples/reference_trace.jsonl`: `moveq #5, D0` at `$1000` after a reset, with accesses tracked (the one access is the closing prefetch of the word at `$1004`): ```json {"accesses":[["r",4100,26364,2]],"after":{"a":[0,0,0,0,0,0,0],"clock":44,"d":[5,0,0,0,0,0,0,0],"halted":false,"ipl":0,"ir":21376,"irc":26364,"nmi_edge":false,"pc":4098,"sr":9984,"ssp":32768,"stopped":false,"trace_pending":false,"usp":0},"before":{"a":[0,0,0,0,0,0,0],"clock":40,"d":[0,0,0,0,0,0,0,0],"halted":false,"ipl":0,"ir":28677,"irc":21376,"nmi_edge":false,"pc":4096,"sr":9984,"ssp":32768,"stopped":false,"trace_pending":false,"usp":0},"cycles":4,"instruction":{"address":4096,"data":"7005","mnemonic":"moveq","operands":["#$5","D0"]},"kind":"instruction","sequence":0,"version":1} ``` ## Comparison rules `compare_step_records` reports every differing field by path: `kind`, `cycles`, `instruction` (when only one side has one), `instruction.address`, `instruction.data`, `before.` and `after.` for each state key, and `accesses` when both records carry them. When one trace ends before the other, the divergence path is `record` with values `"present"` and `null`. Comparison is lazy: the first divergence is found without reading either file to the end ([trace comparison](trace-comparison.md)). ## Versioning Any change to the set of keys, their types or their meaning is a new `version`, and readers reject versions they do not know. The optional `accesses` array and the optional instruction text are part of version 1 from the first release; a reader older than a future key would reject it, because it rejects unknown keys.