Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,11 @@ can catch a regression here — the guard is `tools/uci/boot_check.py`.

### Firmware quirk — FPGA register timing (delay-loop fence)

The UCI FPGA needs a minimum gap between register accesses regardless of
CPU clock. `uci_fence` (`uci_regs.inc`, OUTER=5, INNER=217, ~85 us at
64 MHz) follows every access to `$DF1C-$DF1F`. INNER=217 covers both the
The UCI FPGA needs a minimum gap after register WRITES regardless of CPU
clock. `uci_fence` (`uci_regs.inc`, OUTER=5, INNER=217, ~85 us at 64 MHz)
follows every write to `$DF1C`/`$DF1D`; reads are unfenced on both UCI
backends (`uci_read_fence`; `UCI_READ_FENCE=1` / `M3_READ_FENCE=1` restore
the old images byte for byte; the trust store's `uci_dos.s` still fences). INNER=217 covers both the
U64E (~38 us floor) and the C64 Ultimate (floor bracketed 51.6 FAIL /
62.9 PASS us at 64 MHz; the symptom below the floor is a silently lost
TCP_CONNECT push → `NO_SOCKET`). 48 MHz (U64E) and 64 MHz (C64U) verified.
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ else
$(foreach k,M3_READ_FENCE M3_TRACE,$(if $(filter-out 0,$($(k))),$(error $(k)=$($(k)) applies only to BACKEND=uci-m3)))
endif

# UCI_READ_FENCE=1 (BACKEND=uci): a uci_fence after every UCI register READ
# in src/net/uci too, as before (src/net/uci/uci_regs.inc, uci_read_fence):
# the A/B arm and the fallback. Refused on the other backends; uci-m3 has
# its own M3_READ_FENCE.
ifneq ($(filter-out 0 1,$(UCI_READ_FENCE)),)
$(error UCI_READ_FENCE must be 1 or 0/unset)
endif
ifneq ($(BACKEND),uci)
ifeq ($(UCI_READ_FENCE),1)
$(error UCI_READ_FENCE=1 applies only to BACKEND=uci)
endif
endif

# X25519 is always the libs/x25519 sibling (issue #245 retired the in-tree
# src/crypto/{x25519,fe25519}.s). The old opt-in knob is refused rather than
# ignored, so a stale command line cannot believe it selected something.
Expand Down Expand Up @@ -120,6 +133,9 @@ CA65FLAGS += -D BACKEND_UCI=1
# patience), so the check has the least to do exactly where it does not fit.
# Documented in README and tracked on #135 rather than left to be discovered.
CA65FLAGS += -D X509_VERIFY_NAME=1
ifeq ($(UCI_READ_FENCE),1)
CA65FLAGS += -D UCI_READ_FENCE=1
endif
else ifeq ($(BACKEND),uci-m3)
# A UCI build (the typed-target prompt, the REU body sink and the viewer
# come with BACKEND_UCI) plus the M3 switch. -I src/net/uci after the
Expand Down
11 changes: 6 additions & 5 deletions src/net/uci/net.s
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ net_init:
php

lda UCI_ID
uci_fence ; settle before comparing ID
uci_read_fence ; settle before comparing ID
cmp #UCI_ID_VALUE
beq @present

Expand Down Expand Up @@ -273,17 +273,18 @@ net_poll:
; SMC dst. Loop style matches uci_read_resp_bytes — tight-poll
; DATA_AV and read UCI_RESP_DATA; the firmware FIFO auto-advances
; on read (Phase 2 finding), so NO per-byte DATA_ACC.
uci_poll_stage_fence: ; kept: the one fence after a read that is
uci_fence ; give firmware time to stage response
ldy #$00
@hdr_loop:
lda UCI_STATUS
uci_fence ; settle before testing DATA_AV
uci_read_fence ; settle before testing DATA_AV
and #UCI_STAT_DATA_AV
bne @hdr_got ; branch past trampoline
jmp @hdr_done_short ; long branch: fence too wide for BEQ
@hdr_got:
lda UCI_RESP_DATA
uci_fence ; settle before storing header byte
uci_read_fence ; settle before storing header byte
sta uci_read_hdr,y
iny
cpy #2
Expand Down Expand Up @@ -415,7 +416,7 @@ net_poll:
; FIFO drained mid-record we bail (shouldn't happen if firmware
; honored actual_len but we defend anyway).
lda UCI_STATUS
uci_fence ; settle before testing DATA_AV
uci_read_fence ; settle before testing DATA_AV
and #UCI_STAT_DATA_AV
bne @have_byte
jmp @done_data
Expand All @@ -431,7 +432,7 @@ net_poll:
sta @rb_store+2

lda UCI_RESP_DATA
uci_fence ; settle before storing data byte
uci_read_fence ; settle before storing data byte
@rb_store:
sta $ffff ; SMC: patched each byte

Expand Down
20 changes: 15 additions & 5 deletions src/net/uci/uci_cmd.s
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ uci_wait_idle:
sta @wi_elapsed
@wi_loop:
lda UCI_STATUS
uci_fence ; settle read before testing bits
uci_read_fence ; settle read before testing bits
and #(UCI_STAT_STATE | UCI_STAT_ABORT_PENDING | UCI_STAT_CMD_BUSY) ; $35
beq @idle_done

Expand Down Expand Up @@ -269,7 +269,7 @@ uci_wait_not_busy:
sta @wnb_elapsed
@wnb_loop:
lda UCI_STATUS
uci_fence ; settle read before testing bits
uci_read_fence ; settle read before testing bits
and #UCI_STAT_CMD_BUSY
beq @wnb_done

Expand Down Expand Up @@ -393,7 +393,7 @@ uci_wait_reply:
sta @wr_elapsed
@wr_loop:
lda UCI_STATUS
uci_fence ; settle read before testing bits
uci_read_fence ; settle read before testing bits
and #(UCI_STAT_REPLY_VALID | UCI_STAT_ERROR) ; $28
bne @wr_done

Expand Down Expand Up @@ -425,7 +425,9 @@ uci_wait_reply:
; =============================================================================
uci_check_err:
lda UCI_STATUS
.ifdef UCI_READ_FENCE
jsr uci_settle ; settle before testing error bit
.endif
and #UCI_STAT_ERROR
bne @has_err
clc
Expand Down Expand Up @@ -537,11 +539,11 @@ uci_read_resp_bytes:
cpy uci_resp_max
bcs @rd_done
lda UCI_STATUS
uci_fence ; settle before testing DATA_AV
uci_read_fence ; settle before testing DATA_AV
and #UCI_STAT_DATA_AV
beq @rd_done ; reply shorter than max: it is final
lda UCI_RESP_DATA
uci_fence ; settle before storing/looping
uci_read_fence ; settle before storing/looping
@rd_store:
sta $FFFF,y ; SMC: dst low/high patched above
iny
Expand Down Expand Up @@ -589,14 +591,18 @@ uci_drain_resp:
sta @drn_elapsed
@drn_loop:
lda UCI_STATUS
.ifdef UCI_READ_FENCE
jsr uci_settle ; settle before testing DATA_AV
.endif
and #UCI_STAT_DATA_AV
bne @drn_have
clc
rts
@drn_have:
lda UCI_RESP_DATA
.ifdef UCI_READ_FENCE
jsr uci_settle
.endif
; NO PER-BYTE ACK — see the header. The read advanced the FIFO.

; Check TOD for elapsed tenths. Latch (HOUR) then read TENTHS.
Expand Down Expand Up @@ -671,15 +677,19 @@ uci_drain_status:
sta @dst_elapsed
@dst_loop:
lda UCI_STATUS
.ifdef UCI_READ_FENCE
jsr uci_settle ; settle before testing STAT_AV
.endif
and #UCI_STAT_STAT_AV
bne @dst_have
jsr @dst_commit
clc
rts
@dst_have:
lda UCI_STATUS_DATA
.ifdef UCI_READ_FENCE
jsr uci_settle
.endif
; Last two bytes seen, non-sticky: net_poll reads the errno of a
; "02,NO DATA: n" line off them (#253).
ldx uci_status_tail+1
Expand Down
5 changes: 4 additions & 1 deletion src/net/uci/uci_dos.s
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ dos_finish:
jsr uci_check_err
bcs dos_reject ; push rejected (interface not idle)
dos_part:
lda UCI_STATUS
uci_dos_rd_part: ; uci_dos.s's own reads keep their fence on
lda UCI_STATUS ; every image (check_uci_read_pacing.py)
jsr uci_settle
bpl dos_status ; DATA_AV clear
lda dos_cap ; not a read (dos_hdr zeroed the cap):
Expand All @@ -126,6 +127,7 @@ dos_part:
cmp dos_cap+1
beq dos_reject ; one byte more than asked for
@store:
uci_dos_rd_data:
lda UCI_RESP_DATA
jsr uci_settle
dos_store:
Expand All @@ -140,6 +142,7 @@ dos_store:
dos_status:
jsr uci_drain_status
bcs dos_rts
uci_dos_rd_state:
lda UCI_STATUS
jsr uci_settle
and #UCI_STAT_STATE
Expand Down
13 changes: 13 additions & 0 deletions src/net/uci/uci_regs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ UCI_FENCE_INNER = 217 ; inner loop iterations
pla ; restore A
.endmacro

; uci_read_fence — the fence after a READ of $DF1C-$DF1F in src/net/uci.
; The FPGA's gap is needed after a WRITE (above); reads are unfenced: the
; queues advance on the read itself and DATA_AV / STAT_AV are polled before
; every byte. `make BACKEND=uci UCI_READ_FENCE=1` puts the fence back and
; assembles the previous products byte for byte (the A/B arm and the
; fallback). Sites that fence through `jsr uci_settle` use .ifdef directly.
; uci-m3 has its own knob (M3_READ_FENCE, src/net/uci-m3/m3.inc).
.macro uci_read_fence
.ifdef UCI_READ_FENCE
uci_fence
.endif
.endmacro

; =============================================================================
; Command IDs (issued as the first command byte after selecting the target)
; Phase 2+ will use these; Phase 1b keeps them here purely as equates.
Expand Down
Loading