Skip to content
Merged
5 changes: 4 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ Variables:
(M3 firmware; M3-SPEC v1 + errata v1.1/v1.2), `src/net/uci-m3/` +
`cfg/c64-https-uci-m3.cfg`, no 6510 TLS/crypto linked, every crypto knob
`$(error)`s, not in `PACKAGE_VARIANTS`. `HTTPS_PIN_SPKI_SHA256` = trust
PIN, `M3_ALLOW_TLS12=1` offers hardened 1.2. Model + mutants:
PIN, `M3_ALLOW_TLS12=1` offers hardened 1.2. UCI reads are unfenced
(writes keep `uci_fence`); `M3_READ_FENCE=1` restores the old client
byte for byte, `M3_TRACE=1` = per-READ timing records (measurement only;
`rig_https_m3.py timed`). Model + mutants:
`tools/check_m3_client.py`, `tools/mutate_m3_client.py` (standalone,
need an m3 build); hardware: `rig_https_banner.py`, `rig_https_m3.py`.
- `USE_NISTCURVES_ONCHIP=1` — libs/nistcurves FP_ONCHIP_MUL profile: no
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ $(foreach k,$(M3_REFUSED_KNOBS),$(if $(strip $($(k))),$(error $(k)=$($(k)) does
ifneq ($(filter-out 0 1,$(M3_ALLOW_TLS12)),)
$(error M3_ALLOW_TLS12 must be 1 (offer TLS 1.3 and hardened 1.2) or 0/unset (TLS 1.3 only))
endif
# M3_READ_FENCE=1: a uci_fence after every UCI register READ too, as before
# (src/net/uci-m3/m3.inc, "Read pacing"): the A/B arm and the fallback.
# M3_TRACE=1: per-net_poll timing records, a measurement build only.
$(foreach k,M3_READ_FENCE M3_TRACE,$(if $(filter-out 0 1,$($(k))),$(error $(k) must be 1 or 0/unset)))
else
$(foreach k,M3_READ_FENCE M3_TRACE,$(if $(filter-out 0,$($(k))),$(error $(k)=$($(k)) applies only to BACKEND=uci-m3)))
endif

# X25519 is always the libs/x25519 sibling (issue #245 retired the in-tree
Expand Down Expand Up @@ -116,6 +122,12 @@ CA65FLAGS += -D BACKEND_UCI=1 -D BACKEND_UCI_M3=1 -I src/net/uci
ifeq ($(M3_ALLOW_TLS12),1)
CA65FLAGS += -D M3_ALLOW_TLS12=1
endif
ifeq ($(M3_READ_FENCE),1)
CA65FLAGS += -D M3_READ_FENCE=1
endif
ifeq ($(M3_TRACE),1)
CA65FLAGS += -D M3_TRACE=1
endif
else
CA65FLAGS += -D BACKEND_IP65=1
endif
Expand Down
28 changes: 28 additions & 0 deletions src/net/uci-m3/m3.inc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ M3_B_READY = 3000 ; ER-10 ready-bit polling budget: 30 s
M3_B_READY_POLL = 100 ; ...polled once a second
M3_B_HTTP_IDLE = 6000 ; http_recv_body: 60 s with no byte arrived

; --- Read pacing (M3_READ_FENCE) ------------------------------------------
; uci_fence's gap stays after every WRITE to $DF1C/$DF1D. READS of $DF1C-$DF1F
; are unfenced: the 1541ultimate firmware side says back-to-back reads need
; no gap, only correct state polling (DATA_AV is still tested before every
; byte, ER-12), and the U64E A/B that came with this change is the evidence.
; Two fences per body byte were most of a large fetch's time.
; `make BACKEND=uci-m3 M3_READ_FENCE=1` puts a fence back after every read
; and assembles the pre-change client byte for byte: the A/B arm, and the
; fallback if some device turns out to need it.

; --- Timing trace (M3_TRACE, a measurement build only) ---------------------
; net_poll appends one M3_TR_REC-byte record per call made while CONNECTED
; to M3_TR_BASE (the MAIN tail, link-asserted free), up to M3_TR_CAP;
; m3_tr_count keeps counting past it. Each delta is (dTB, dTA): CIA2 timer B units and timer
; A cycles, ticks = dTB * M3_UNIT_CYCLES + (signed) dTA. Record layout:
; +0 rtt PUSH of the READ to REPLY_VALID seen (0/0: no READ was run)
; +4 total the whole net_poll call
; +8 bytes put into the ring (16-bit)
; +10 m3_poll_result
; +11 $DF1C read right after that READ's PUSH and its fence
M3_TR_BASE = $6000
M3_TR_REC = 12
M3_TR_CAP = ($A000 - M3_TR_BASE) / M3_TR_REC
M3_TS_PUSH = 0 ; m3_tr_t slots
M3_TS_REPLY = 4
M3_TS_START = 8
M3_TS_END = 12

; Deadline slots (offsets into m3_dl).
M3_DL_CMD = 0
M3_DL_APP = 4
Expand Down
115 changes: 114 additions & 1 deletion src/net/uci-m3/m3_cmd.s
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@
.export m3_code
.export m3_wedged
.export m3_dl
.ifdef M3_TRACE
.export m3_tr_sample
.export m3_tr_delta
.export m3_tr_t
.export m3_tr_d
.export m3_tr_got
.export m3_tr_post
.export m3_tr_pushes
.export m3_tr_bit3
.export m3_tr_or
.endif

CIA2_TA_LO = $DD04
CIA2_TA_HI = $DD05
Expand Down Expand Up @@ -139,10 +150,14 @@ m3_settle:
uci_fence
rts

; m3_get_status — A = $DF1C, read and settled.
; m3_get_status — A = $DF1C. Unfenced unless M3_READ_FENCE (m3.inc).
m3_get_status:
lda UCI_STATUS
.ifdef M3_READ_FENCE
jmp m3_settle
.else
rts
.endif

; =============================================================================
; m3_wait_clear / m3_wait_set — A = mask. Spin until ($DF1C & mask) is zero
Expand Down Expand Up @@ -282,14 +297,27 @@ m3_exec:
jsr m3_settle
lda #UCI_CTRL_PUSH_CMD
sta UCI_CONTROL
.ifdef M3_TRACE
ldy #M3_TS_PUSH
jsr m3_tr_sample
.endif
jsr m3_settle
.ifdef M3_TRACE
jsr m3_tr_post_push
.endif
lda m3_tmp2
ldx m3_tmp2+1
ldy #M3_DL_CMD
jsr m3_dl_arm ; PUSH + B
lda #(UCI_STAT_REPLY_VALID | UCI_STAT_ERROR)
jsr m3_wait_set
bcs @x_timeout
.ifdef M3_TRACE
ldy #M3_TS_REPLY
jsr m3_tr_sample
lda #1
sta m3_tr_got
.endif
jsr m3_get_status
and #UCI_STAT_ERROR
beq @x_reply
Expand Down Expand Up @@ -337,7 +365,9 @@ m3_read_data:
and #UCI_STAT_DATA_AV
beq @rd_done
lda UCI_RESP_DATA
.ifdef M3_READ_FENCE
jsr m3_settle
.endif
@rd_store:
sta $FFFF,y ; SMC: m3_rd_dst
iny
Expand All @@ -361,7 +391,9 @@ m3_discard_data:
and #UCI_STAT_DATA_AV
beq @dd_done
lda UCI_RESP_DATA
.ifdef M3_READ_FENCE
jsr m3_settle
.endif
lda #1
sta m3_discarded
dey
Expand All @@ -387,7 +419,9 @@ m3_read_status:
and #UCI_STAT_STAT_AV
beq @rs_done
lda UCI_STATUS_DATA
.ifdef M3_READ_FENCE
jsr m3_settle
.endif
cpy #M3_STATUS_MAX
bcs @rs_skip
sta m3_status,y
Expand Down Expand Up @@ -438,6 +472,76 @@ m3_finish:
jsr m3_read_status
jmp m3_accept

.ifdef M3_TRACE
; =============================================================================
; M3_TRACE only (m3.inc, "Timing trace"): never in a default build.
; m3_tr_sample — Y = slot (M3_TS_*): CIA2 timer B (2) then timer A (2) into
; m3_tr_t+Y, a consistent set (read again until neither timer moved between
; its bytes). Clobbers: A. Preserves X, Y.
; =============================================================================
m3_tr_sample:
lda CIA2_TB_HI
sta m3_tr_t+1,y
lda CIA2_TB_LO
sta m3_tr_t+0,y
lda CIA2_TA_HI
sta m3_tr_t+3,y
lda CIA2_TA_LO
sta m3_tr_t+2,y
lda CIA2_TA_HI
cmp m3_tr_t+3,y
bne m3_tr_sample
lda CIA2_TB_LO
cmp m3_tr_t+0,y
bne m3_tr_sample
lda CIA2_TB_HI
cmp m3_tr_t+1,y
bne m3_tr_sample
rts

; m3_tr_delta — X = earlier slot, Y = later slot: m3_tr_d = (dTB, dTA), each
; 16-bit little-endian; both timers count down, so earlier - later.
; Clobbers: A.
m3_tr_delta:
sec
lda m3_tr_t+0,x
sbc m3_tr_t+0,y
sta m3_tr_d+0
lda m3_tr_t+1,x
sbc m3_tr_t+1,y
sta m3_tr_d+1
sec
lda m3_tr_t+2,x
sbc m3_tr_t+2,y
sta m3_tr_d+2
lda m3_tr_t+3,x
sbc m3_tr_t+3,y
sta m3_tr_d+3
rts

; m3_tr_post_push — $DF1C as the PUSH left it (after its fence): kept for
; the READ record, OR-ed into m3_tr_or, and bit 3 (the FPGA's error_busy,
; a refused push) counted. Clobbers: A.
m3_tr_post_push:
lda UCI_STATUS
.ifdef M3_READ_FENCE
jsr m3_settle
.endif
sta m3_tr_post
ora m3_tr_or
sta m3_tr_or
inc m3_tr_pushes
bne :+
inc m3_tr_pushes+1
: lda m3_tr_post
and #UCI_STAT_ERROR
beq :+
inc m3_tr_bit3
bne :+
inc m3_tr_bit3+1
: rts
.endif

.segment "UCI_BSS"

m3_dl: .res 8 ; two deadline slots: start(2), length(2)
Expand All @@ -453,3 +557,12 @@ m3_status: .res M3_STATUS_MAX
m3_status_len: .res 1
m3_status_seen: .res 1
m3_code: .res 1
.ifdef M3_TRACE
m3_tr_t: .res 16 ; M3_TS_* slots: TB (2), TA (2)
m3_tr_d: .res 4
m3_tr_got: .res 1 ; 1: the last m3_exec saw its reply (rtt valid)
m3_tr_post: .res 1
m3_tr_pushes: .res 2
m3_tr_bit3: .res 2
m3_tr_or: .res 1
.endif
Loading