From 0e7ad4a4996c10e4698ac2dcc10c59a3389e4512 Mon Sep 17 00:00:00 2001 From: Martin Matyas Date: Sun, 8 Nov 2026 16:03:10 +0100 Subject: [PATCH 1/2] A match whose link goes silent ends with CONNECTION LOST after 10 s A link that dies without a word (Wi-Fi lost under an ESP in passthrough, a cable pulled) left the waiting machine frozen on the playfield until BREAK: the device reports nothing, and the relay keeps the dead member's socket, so the others waited too. The lockstep and run-ahead waits now give up after 10 s with no frame from the others. Each platform names its clock in plat_config.h: PLAT_CLOCK16 (16 bits: the ZX's FRAMES, the Amiga's and ST's TV frame counts, the PC's and the browser's milliseconds), or PLAT_CLOCK, 8 bits counted in quarter seconds (CPC, MZ-800 by its 8253's counter 1, C64, Atari XL). The two waits share one check. tools/lockstep_zx.py CUT_AT=n silences A's link (zxemu's cut), in CI for the 48K and the 128K. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 4 +++ c/core/netplay.c | 54 +++++++++++++++++++++++--------- c/platform/amiga/plat_config.h | 3 ++ c/platform/atari/plat_config.h | 2 ++ c/platform/atarist/plat_config.h | 2 ++ c/platform/c64/plat_config.h | 3 ++ c/platform/cpc/plat_config.h | 3 ++ c/platform/mz/plat_config.h | 4 +++ c/platform/pc/plat_config.h | 3 ++ c/platform/web/plat_config.h | 3 ++ c/platform/web/plat_web.c | 2 ++ c/platform/zx/plat_config.h | 2 ++ docs/net-protocol.md | 5 ++- docs/setup/README.md | 2 +- tools/lockstep_zx.py | 12 +++++++ tools/zxemu.py | 11 ++++--- 16 files changed, 95 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c3bf022..c567c06 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -114,6 +114,10 @@ jobs: run: PLAYERS=4 LOCAL_A=2 LOCAL_B=2 KEYS=tests/recordings/dm4.bnr QUIET=1 STACK_MIN=150 build/venv/bin/python tools/lockstep_zx.py 600 - name: Spectrum 128K match, 4 players, stack watermark run: A_BUILD=build/zx128 B_BUILD=build/zx128 PLAYERS=4 LOCAL_A=2 LOCAL_B=2 KEYS=tests/recordings/dm4.bnr QUIET=1 STACK_MIN=100 build/venv/bin/python tools/lockstep_zx.py 600 + - name: Spectrum link gone silent mid-match, 48K and 128K (both end with CONNECTION LOST in about 10 s) + run: | + CUT_AT=200 QUIET=1 build/venv/bin/python tools/lockstep_zx.py 600 + CUT_AT=200 A_BUILD=build/zx128 B_BUILD=build/zx128 QUIET=1 build/venv/bin/python tools/lockstep_zx.py 600 - name: CPC match run: build/venv/bin/python tools/lockstep_cpc.py 300 - name: against BomberNet 0.2.0, both ways diff --git a/c/core/netplay.c b/c/core/netplay.c index b76e0c8..eb9bc91 100644 --- a/c/core/netplay.c +++ b/c/core/netplay.c @@ -103,6 +103,42 @@ static void adapt_window(const uint8_t *k) { } #endif +/* Every 16th poll that found the frame not there yet: the device asked why + * (desync, dropped, no link), BREAK, and the silence measured. A link that + * dies without a word (Wi-Fi gone under a modem in passthrough, a cable + * pulled) leaves the device and the relay waiting for good, every machine of + * the match with them: no frame for 10 s is the link lost. Nothing in a + * match waits that long otherwise (the messages between rounds run in + * lockstep too). The platform's clock (plat_config.h): PLAT_CLOCK16, 16 bits + * that do not wrap in 10 s, PLAT_CLOCK16_10S ticks in them; or PLAT_CLOCK, + * 8 bits, PLAT_CLOCK_QUARTER ticks a quarter second (under 128, so 16 polls + * never span a wrap), counted to NET_SILENT. Neither: no limit. */ +#if defined(PLAT_CLOCK16) +static uint16_t w_tick; +#define WAIT_START() (w_tick = (uint16_t)(PLAT_CLOCK16)) +#elif defined(PLAT_CLOCK) +#define NET_SILENT 40 +static uint8_t w_tick, w_quarters; +#define WAIT_START() (w_tick = (uint8_t)(PLAT_CLOCK), w_quarters = 0) +#else +#define WAIT_START() ((void)0) +#endif +static void wait_check(void) { + static net_status_t st; + if (net_status(&st) == 0 && + (st.state == NETST_DESYNC || st.state == NETST_DROPPED || st.state == NETST_NOLINK)) + net_abort = st.state ? st.state : 9; + else if (plat_key_char() == 0x1b) net_abort = NET_ABORT_BREAK; /* BREAK: give up waiting */ +#if defined(PLAT_CLOCK16) + else if ((uint16_t)((uint16_t)(PLAT_CLOCK16) - w_tick) >= PLAT_CLOCK16_10S) net_abort = 9; /* CONNECTION LOST */ +#elif defined(PLAT_CLOCK) + else if ((uint8_t)((uint8_t)(PLAT_CLOCK) - w_tick) >= PLAT_CLOCK_QUARTER) { + w_tick += PLAT_CLOCK_QUARTER; + if (++w_quarters == NET_SILENT) net_abort = 9; /* CONNECTION LOST */ + } +#endif +} + /* a failed NET command: ask the device why (desync / dropped) */ static void net_fail(void) { net_status_t st; @@ -135,8 +171,8 @@ void net_match_start(void) { void net_lockstep_poll(void) { static uint16_t avail; /* statics: smaller code and stack on the Z80 (never re-entered) */ static uint8_t keys[NET_SLOTS * NET_BYTES], local[NET_BYTES], i, tries; - static net_status_t st; tries = 0; + WAIT_START(); if (net_abort) { for (i = 0; i < MAX_PLAYERS; i++) players[i].keys = 0; return; } for (i = 0; i < ADAPT_BYTE; i++) local[i] = i < menu_local ? input_read(menu_inputs[i]) : 0; local[ADAPT_BYTE] = report; @@ -161,12 +197,7 @@ void net_lockstep_poll(void) { #endif if (++tries > 4) { net_waiting = 1; - if ((tries & 15) == 0) { - if (net_status(&st) == 0 && - (st.state == NETST_DESYNC || st.state == NETST_DROPPED || st.state == NETST_NOLINK)) - net_abort = st.state ? st.state : 9; - else if (plat_key_char() == 0x1b) net_abort = NET_ABORT_BREAK; /* BREAK: give up waiting */ - } + if ((tries & 15) == 0) wait_check(); } } { /* deal the vector out by the seat table */ @@ -234,7 +265,6 @@ uint8_t net_runahead_lead(void) { return (uint8_t)(shown + 1 - net_frame); } uint8_t net_runahead_step(void) { static uint8_t keys[NET_SLOTS * NET_BYTES], local[NET_BYTES]; - static net_status_t st; uint16_t avail, target, f; uint8_t i, s, tries, noted, cap; if (net_abort) { /* the match is over: the agreed state on the screen */ @@ -255,6 +285,7 @@ uint8_t net_runahead_step(void) { target = (uint16_t)(shown + 1); if (sim_guessed) { snap_restore(); sim_guessed = 0; } tries = 0; noted = 0; + WAIT_START(); cap = lead_max < net_runahead_cap ? lead_max : net_runahead_cap; if (cap > net_delay) cap = net_delay; while (!net_abort && net_frame <= target) { /* the agreed frames */ @@ -283,12 +314,7 @@ uint8_t net_runahead_step(void) { if ((uint16_t)(target - net_frame + 1) <= cap) break; /* the rest guessed */ if (++tries > 4) { /* waits, as the lockstep */ net_waiting = 1; - if ((tries & 15) == 0) { - if (net_status(&st) == 0 && - (st.state == NETST_DESYNC || st.state == NETST_DROPPED || st.state == NETST_NOLINK)) - net_abort = st.state ? st.state : 9; - else if (plat_key_char() == 0x1b) net_abort = NET_ABORT_BREAK; - } + if ((tries & 15) == 0) wait_check(); } } sim_quiet = 0; diff --git a/c/platform/amiga/plat_config.h b/c/platform/amiga/plat_config.h index 224fd88..a5bfc68 100644 --- a/c/platform/amiga/plat_config.h +++ b/c/platform/amiga/plat_config.h @@ -7,4 +7,7 @@ #define PLAT_RUNAHEAD 1 /* a frame ahead of the agreed one in a match (docs/rollback-plan.md) */ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1) */ #define NET_PLATFORM "amiga" /* the relay's statistics: &pf= (docs/net-protocol.md) */ +extern volatile uint32_t amiga_ticks; +#define PLAT_CLOCK16 amiga_ticks /* +1 every TV frame (plat_amiga.c): the match's silence (core/netplay.c) */ +#define PLAT_CLOCK16_10S 500 /* 50 Hz; 8.3 s at 60 */ #endif diff --git a/c/platform/atari/plat_config.h b/c/platform/atari/plat_config.h index b7371a3..0aeb52f 100644 --- a/c/platform/atari/plat_config.h +++ b/c/platform/atari/plat_config.h @@ -7,4 +7,6 @@ #define PLAT_ASM_COMPOSITE 1 /* cpu/6502/loops.s */ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1): ~7 bytes on FujiNet's bus, not ~50 */ #define NET_PLATFORM "atari-xl" /* the relay's statistics: &pf= (docs/net-protocol.md) */ +#define PLAT_CLOCK (*(volatile uint8_t *)0x14) /* RTCLOK, +1 every vertical blank: the match's silence (core/netplay.c) */ +#define PLAT_CLOCK_QUARTER 12 #endif diff --git a/c/platform/atarist/plat_config.h b/c/platform/atarist/plat_config.h index f471673..8277a54 100644 --- a/c/platform/atarist/plat_config.h +++ b/c/platform/atarist/plat_config.h @@ -7,4 +7,6 @@ #define PLAT_RUNAHEAD 1 /* a frame ahead of the agreed one in a match (docs/rollback-plan.md) */ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1) */ #define NET_PLATFORM "atari-st" /* the relay's statistics: &pf= (docs/net-protocol.md) */ +#define PLAT_CLOCK16 (*(volatile uint32_t *)0x466) /* frclock, TV frames: the match's silence (core/netplay.c) */ +#define PLAT_CLOCK16_10S 500 /* 50 Hz; 8.3 s at 60 */ #endif diff --git a/c/platform/c64/plat_config.h b/c/platform/c64/plat_config.h index 194f8cd..7104925 100644 --- a/c/platform/c64/plat_config.h +++ b/c/platform/c64/plat_config.h @@ -6,4 +6,7 @@ #define PLAT_ASM_COMPOSITE 1 /* cpu/6502/loops.s */ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1) */ #define NET_PLATFORM "c64" /* the relay's statistics: &pf= (docs/net-protocol.md) */ +extern volatile uint8_t c64_ticks; +#define PLAT_CLOCK c64_ticks /* +1 every TV frame (c64.s): the match's silence (core/netplay.c) */ +#define PLAT_CLOCK_QUARTER 12 #endif diff --git a/c/platform/cpc/plat_config.h b/c/platform/cpc/plat_config.h index a866ff5..b2ef5a4 100644 --- a/c/platform/cpc/plat_config.h +++ b/c/platform/cpc/plat_config.h @@ -11,4 +11,7 @@ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1): about 7 bytes, not 50 */ #define PLAT_ASM_TEXT 1 /* print_string, hud_text, title_text* in cpu/z80/loops.c */ #define NET_PLATFORM "amstrad-cpc" /* the relay's statistics: &pf= (docs/net-protocol.md) */ +extern volatile uint8_t cpc_ticks; +#define PLAT_CLOCK cpc_ticks /* 300 a second (plat_cpc.c): the match's silence (core/netplay.c) */ +#define PLAT_CLOCK_QUARTER 75 #endif diff --git a/c/platform/mz/plat_config.h b/c/platform/mz/plat_config.h index b451b07..37fd14f 100644 --- a/c/platform/mz/plat_config.h +++ b/c/platform/mz/plat_config.h @@ -8,4 +8,8 @@ #define PLAT_ASM_COMPOSITE 1 /* composite_map and hash_run are assembly (plat_mz.c) */ #define PLAT_ASM_HASH 1 #define PLAT_ASM_TEXT 1 /* print_string, hud_text, title_text* in cpu/z80/loops.c */ +#define PLAT_CLOCK (*(volatile uint8_t *)0xe007 = 0x40, (void)*(volatile uint8_t *)0xe005, \ + (uint8_t)~*(volatile uint8_t *)0xe005) /* the 8253's counter 1 (down, 15611 Hz): its high + byte, upwards, about 60 a second: the match's silence (core/netplay.c) */ +#define PLAT_CLOCK_QUARTER 15 #endif diff --git a/c/platform/pc/plat_config.h b/c/platform/pc/plat_config.h index 63bb5ee..648455d 100644 --- a/c/platform/pc/plat_config.h +++ b/c/platform/pc/plat_config.h @@ -7,4 +7,7 @@ #define PLAT_RUNAHEAD 1 /* a frame ahead of the agreed one in a match (docs/rollback-plan.md) */ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1) */ #define NET_PLATFORM "pc-dos" /* the relay's statistics: &pf= (docs/net-protocol.md) */ +uint32_t pc_ms(void); /* milliseconds (plat_pc.c): the match's silence (core/netplay.c) */ +#define PLAT_CLOCK16 pc_ms() +#define PLAT_CLOCK16_10S 10000 #endif diff --git a/c/platform/web/plat_config.h b/c/platform/web/plat_config.h index c3968fe..e81b759 100644 --- a/c/platform/web/plat_config.h +++ b/c/platform/web/plat_config.h @@ -8,4 +8,7 @@ #define PLAT_RUNAHEAD 1 /* a frame ahead of the agreed one in a match (docs/rollback-plan.md) */ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1) */ #define NET_PLATFORM "browser" /* the relay's statistics: &pf= (docs/net-protocol.md) */ +uint16_t web_clock(void); /* milliseconds (plat_web.c): the match's silence (core/netplay.c) */ +#define PLAT_CLOCK16 web_clock() +#define PLAT_CLOCK16_10S 10000 #endif diff --git a/c/platform/web/plat_web.c b/c/platform/web/plat_web.c index ff8fa40..8fe6a2c 100644 --- a/c/platform/web/plat_web.c +++ b/c/platform/web/plat_web.c @@ -224,6 +224,8 @@ uint8_t plat_break(void) { return keydown[K_ESC]; } static double next_frame; uint8_t plat_frame_idle; +uint16_t web_clock(void) { return (uint16_t)(uint32_t)emscripten_get_now(); } /* PLAT_CLOCK16 (plat_config.h) */ + void plat_frame_sync(void) { /* FRAME_MS of the browser's clock; the page is drawn meanwhile */ double now; #ifdef WEB_TEST diff --git a/c/platform/zx/plat_config.h b/c/platform/zx/plat_config.h index 9af9df4..225f4ac 100644 --- a/c/platform/zx/plat_config.h +++ b/c/platform/zx/plat_config.h @@ -18,4 +18,6 @@ #else #define NET_PLATFORM "zx-spectrum-48k" #endif +#define PLAT_CLOCK16 (*(volatile uint16_t *)0x5c78) /* FRAMES, the ROM's, 50 a second: the match's silence (core/netplay.c) */ +#define PLAT_CLOCK16_10S 500 #endif diff --git a/docs/net-protocol.md b/docs/net-protocol.md index 1e40d25..f701931 100644 --- a/docs/net-protocol.md +++ b/docs/net-protocol.md @@ -197,7 +197,10 @@ accesses, well under 0.1 ms. Input delay D (2-3 frames). Before running frame F: NETSEND(F+D, local input); NETPOLL(F) until avail >= F (stall with a "waiting" indicator -after a few polls); copy the S*N bytes into the game's per-frame input +after a few polls; give up on DESYNC, DROPPED or no link in NETSTATUS, on +BREAK, or after 10 s with nothing: a link that dies silently, Wi-Fi lost +under a modem in passthrough, leaves the device and the relay waiting +for good); copy the S*N bytes into the game's per-frame input vector; run the frame; every `hash_period` frames NETHASH(F, state_hash). Local players on one machine occupy consecutive slots and send N bytes each (one NETSEND per local slot; the relay keys inputs by socket + slot). diff --git a/docs/setup/README.md b/docs/setup/README.md index bb01e51..f9387a8 100644 --- a/docs/setup/README.md +++ b/docs/setup/README.md @@ -97,5 +97,5 @@ Then point each machine at that PC (say 192.168.1.10): | NETWORK ERROR nn | the device refused a command; nn is its code | | WAITING FOR THE OTHERS | you are ready; the match starts when everybody is | | DESYNC | the machines disagree about the game, which they check every sixteen frames; the match ends. It should not happen: please report it with the platforms involved | -| CONNECTION LOST | a machine stopped answering; the match ends | +| CONNECTION LOST | a machine stopped answering, or nothing came from the others for 10 seconds (a network link lost); the match ends | | MATCH LEFT | someone pressed BREAK | diff --git a/tools/lockstep_zx.py b/tools/lockstep_zx.py index 4b302fe..92e4331 100755 --- a/tools/lockstep_zx.py +++ b/tools/lockstep_zx.py @@ -17,6 +17,9 @@ return (calls they make included) BREAK_AT=40 ... A leaves with BREAK after 40 frames: A ends with MATCH LEFT, B with the other player gone + CUT_AT=200 ... A's link goes silent after 200 frames, its connection + kept open: both must end with CONNECTION LOST (9), about + 10 s (of A's emulated time) later """ import re, os, sys, time sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) @@ -137,10 +140,15 @@ def step_a(): # A's own quanta, to time its return 0 done_a = done_b = 0 BREAK_AT = int(os.environ.get('BREAK_AT', 0)) +CUT_AT = int(os.environ.get('CUT_AT', 0)) +cut_t = cut_after = None end = a.now() + 120 * 3500000 while (done_a < N or done_b < N) and a.now() < end: done_a += step_a() if BREAK_AT and done_a == BREAK_AT: a.press('CAPS SHIFT', 'SPACE') + if CUT_AT and done_a == CUT_AT and cut_t is None: a.cut = True; cut_t = a.now() + if cut_t is not None and cut_after is None and g8(a, NAB): # when A gave up (before it hangs up) + cut_after = (a.now() - cut_t) / 3500000 if b.step() == FB: on_flush(1, b); done_b += 1 print(f'frames: A {g16(a, FN)} B {g16(b, FNB)} abort {g8(a, NAB)}/{g8(b, NAB_B)}', flush=True) common = sorted(set(seen['A']) & set(seen['B'])) @@ -187,4 +195,8 @@ def step_a(): # A's own quanta, to time its again = gr(a, NC, 4) != before print('A hosts again: room', gr(a, NC, 4).decode() if again else 'none') sys.exit(0 if aborts[0] == 7 and aborts[1] and not mism and again else 1) +if CUT_AT: + print(f'link cut at frame {CUT_AT}: A abort {aborts[0]} after {cut_after if cut_after is None else round(cut_after, 1)} s, ' + f'B abort {aborts[1]} (9 = CONNECTION LOST)') + sys.exit(0 if aborts == (9, 9) and cut_after and 9 <= cut_after <= 12 and not mism else 1) sys.exit(1 if mism or any(aborts) or not common or stack_short else 0) diff --git a/tools/zxemu.py b/tools/zxemu.py index e29c055..bd4f22c 100644 --- a/tools/zxemu.py +++ b/tools/zxemu.py @@ -83,7 +83,7 @@ def say(self, text): def _deliver(self): now = self.z.now() - if self.transparent and self.sock and len(self.out) < 512: + if self.transparent and self.sock and len(self.out) < 512 and not self.z.cut: # cut: the link silent try: r, _, _ = select.select([self.sock], [], [], 0) if r: @@ -122,7 +122,7 @@ def write(self, b): self.plus += 1 # perhaps the escape: held back else: data = b'+' * self.plus + bytes([b]); self.plus = 0 - if self.sock: + if self.sock and not self.z.cut: try: self.sock.sendall(data) except OSError: self.sock = None self.last_tx = now @@ -190,6 +190,8 @@ def __init__(self, spectranet=False, relay=('127.0.0.1', 8765), bank6=None, esp0 self.ports = {} # low port byte -> value: joystick interfaces self.spectranet = spectranet self.relay = relay + self.cut = False # True: the network link goes silent, the connection kept open + # (a Wi-Fi lost under the modem): nothing in, nothing out self.socks = {} self.sn_calls = 0 self.esp = Esp01(self, relay) if esp01 else None @@ -252,14 +254,15 @@ def _sn_call(self): self.socks[a].setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self._sn_return(a=0) elif fn == 0x3e12: # SEND A = fd, DE = data, BC = length - self.socks[a].sendall(self.read(de, bc)); self._sn_return(bc=bc) + if not self.cut: self.socks[a].sendall(self.read(de, bc)) # cut: lost on the way + self._sn_return(bc=bc) elif fn == 0x3e24: # POLLFD A = fd -> Z = not ready, C = reason s = self.socks[a] # an emulated Spectrum runs far faster than real time; waiting # a moment when nothing is there keeps its timeouts (counted in # TV frames) meaningful against a real server r, _, _ = select.select([s], [], [], 0.003) - if not r: self._sn_return(zero=True, bc=bc & 0xff00) + if not r or self.cut: self._sn_return(zero=True, bc=bc & 0xff00) elif not s.recv(1, socket.MSG_PEEK): self._sn_return(bc=(bc & 0xff00) | 0x02) else: self._sn_return(bc=(bc & 0xff00) | 0x04) elif fn == 0x3e15: # RECV A = fd, DE = buffer, BC = size -> BC = got From 5f85612d55d33d858a2f72a01385850fd1047810 Mon Sep 17 00:00:00 2001 From: Martin Matyas Date: Sun, 8 Nov 2026 16:40:16 +0100 Subject: [PATCH 2/2] Silence timeout: review fixes; the MZ-800's clock read whole - The 8-bit clocks count every quarter second passed since the last check, not one: a slow device (FujiNet retrying, about 200 ms a poll) would have stretched 10 s to minutes. - 60 Hz machines wait 10 s too: the Amiga, ST, C64 and Atari XL take their limit from their video rate (600 frames, or quarters of 15). - The run-ahead starts the timer again with each agreed frame it takes. - MZ-800: the clock macro's first read of counter 1 was dropped by the compiler, so the low byte was read as the high one (a 0.3 s timeout), and the latch was left half read. mz_clock() reads both bytes, as the frame limiter does. Measured from a frozen relay: MZ-800 9.5 s, CPC 9.5 s. Co-Authored-By: Claude Opus 5.5 --- c/core/netplay.c | 14 +++++++++----- c/platform/amiga/plat_amiga.c | 3 ++- c/platform/amiga/plat_config.h | 3 ++- c/platform/atari/plat_atari.c | 3 ++- c/platform/atari/plat_config.h | 3 ++- c/platform/atarist/plat_config.h | 3 ++- c/platform/atarist/plat_st.c | 3 ++- c/platform/c64/plat_c64.c | 3 ++- c/platform/c64/plat_config.h | 3 ++- c/platform/mz/plat_config.h | 6 +++--- c/platform/mz/plat_mz.c | 12 ++++++++++++ 11 files changed, 40 insertions(+), 16 deletions(-) diff --git a/c/core/netplay.c b/c/core/netplay.c index eb9bc91..a202d8b 100644 --- a/c/core/netplay.c +++ b/c/core/netplay.c @@ -111,8 +111,11 @@ static void adapt_window(const uint8_t *k) { * match waits that long otherwise (the messages between rounds run in * lockstep too). The platform's clock (plat_config.h): PLAT_CLOCK16, 16 bits * that do not wrap in 10 s, PLAT_CLOCK16_10S ticks in them; or PLAT_CLOCK, - * 8 bits, PLAT_CLOCK_QUARTER ticks a quarter second (under 128, so 16 polls - * never span a wrap), counted to NET_SILENT. Neither: no limit. */ + * 8 bits, PLAT_CLOCK_QUARTER ticks a quarter second, counted to NET_SILENT: + * all the quarters passed since the last check (a slow device's 16 polls + * may take seconds; they must stay under the clock's wrap, 256 ticks). + * Neither: no limit. The timer starts with each wait, and again with each + * agreed frame the run-ahead takes. */ #if defined(PLAT_CLOCK16) static uint16_t w_tick; #define WAIT_START() (w_tick = (uint16_t)(PLAT_CLOCK16)) @@ -132,9 +135,9 @@ static void wait_check(void) { #if defined(PLAT_CLOCK16) else if ((uint16_t)((uint16_t)(PLAT_CLOCK16) - w_tick) >= PLAT_CLOCK16_10S) net_abort = 9; /* CONNECTION LOST */ #elif defined(PLAT_CLOCK) - else if ((uint8_t)((uint8_t)(PLAT_CLOCK) - w_tick) >= PLAT_CLOCK_QUARTER) { - w_tick += PLAT_CLOCK_QUARTER; - if (++w_quarters == NET_SILENT) net_abort = 9; /* CONNECTION LOST */ + else { + while ((uint8_t)((uint8_t)(PLAT_CLOCK) - w_tick) >= PLAT_CLOCK_QUARTER) { w_tick += PLAT_CLOCK_QUARTER; w_quarters++; } + if (w_quarters >= NET_SILENT) net_abort = 9; /* CONNECTION LOST */ } #endif } @@ -306,6 +309,7 @@ uint8_t net_runahead_step(void) { if (net_slot && i && i <= NET_DELAY_MAX) net_delay = i; if (((uint8_t)net_frame & (ADAPT_WINDOW - 1)) == ADAPT_WINDOW - 1) adapt_window(keys); net_frame++; + WAIT_START(); /* an agreed frame: the silence starts again */ if (frame_ends()) { target = (uint16_t)(net_frame - 1); break; } /* the round's end is this frame's: the lockstep machines stop here too, no frame past it */ continue; diff --git a/c/platform/amiga/plat_amiga.c b/c/platform/amiga/plat_amiga.c index aa53745..2deae62 100644 --- a/c/platform/amiga/plat_amiga.c +++ b/c/platform/amiga/plat_amiga.c @@ -255,7 +255,8 @@ uint8_t plat_break(void) { poll_keys(); return keydown[K_ESC]; } /* ---- time ---- */ static uint32_t last_tick; -static uint8_t ntsc, ntsc_step; +uint8_t ntsc; /* PLAT_CLOCK16_10S (plat_config.h) reads it too */ +static uint8_t ntsc_step; void plat_frame_sync(void) { /* 60 ms: 3 PAL frames, or 4,3,4,3,4 NTSC ones (3.6) */ uint8_t need = 3; diff --git a/c/platform/amiga/plat_config.h b/c/platform/amiga/plat_config.h index a5bfc68..c94f171 100644 --- a/c/platform/amiga/plat_config.h +++ b/c/platform/amiga/plat_config.h @@ -9,5 +9,6 @@ #define NET_PLATFORM "amiga" /* the relay's statistics: &pf= (docs/net-protocol.md) */ extern volatile uint32_t amiga_ticks; #define PLAT_CLOCK16 amiga_ticks /* +1 every TV frame (plat_amiga.c): the match's silence (core/netplay.c) */ -#define PLAT_CLOCK16_10S 500 /* 50 Hz; 8.3 s at 60 */ +extern uint8_t ntsc; /* 60 Hz (plat_amiga.c) */ +#define PLAT_CLOCK16_10S (ntsc ? 600 : 500) #endif diff --git a/c/platform/atari/plat_atari.c b/c/platform/atari/plat_atari.c index 283a036..b255c44 100644 --- a/c/platform/atari/plat_atari.c +++ b/c/platform/atari/plat_atari.c @@ -264,7 +264,8 @@ uint8_t plat_key_char(void) { uint8_t plat_break(void) { return irq_taken(0x80); } /* ---- time ---- */ -static uint8_t last_tick, ntsc, ntsc_step; +uint8_t ntsc; /* PLAT_CLOCK_QUARTER (plat_config.h) reads it too */ +static uint8_t last_tick, ntsc_step; static void tones_due(void); void plat_frame_sync(void) { /* 60 ms: 3 PAL frames, or 4,3,4,3,4 NTSC ones (3.6) */ diff --git a/c/platform/atari/plat_config.h b/c/platform/atari/plat_config.h index 0aeb52f..5f4da4f 100644 --- a/c/platform/atari/plat_config.h +++ b/c/platform/atari/plat_config.h @@ -8,5 +8,6 @@ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1): ~7 bytes on FujiNet's bus, not ~50 */ #define NET_PLATFORM "atari-xl" /* the relay's statistics: &pf= (docs/net-protocol.md) */ #define PLAT_CLOCK (*(volatile uint8_t *)0x14) /* RTCLOK, +1 every vertical blank: the match's silence (core/netplay.c) */ -#define PLAT_CLOCK_QUARTER 12 +extern uint8_t ntsc; /* 60 Hz (plat_atari.c) */ +#define PLAT_CLOCK_QUARTER (ntsc ? 15 : 12) #endif diff --git a/c/platform/atarist/plat_config.h b/c/platform/atarist/plat_config.h index 8277a54..0ee9723 100644 --- a/c/platform/atarist/plat_config.h +++ b/c/platform/atarist/plat_config.h @@ -8,5 +8,6 @@ #define NET_BINARY_INPUT 1 /* input lines as binary messages (the relay's bin=1) */ #define NET_PLATFORM "atari-st" /* the relay's statistics: &pf= (docs/net-protocol.md) */ #define PLAT_CLOCK16 (*(volatile uint32_t *)0x466) /* frclock, TV frames: the match's silence (core/netplay.c) */ -#define PLAT_CLOCK16_10S 500 /* 50 Hz; 8.3 s at 60 */ +extern uint8_t hz60; /* 60 Hz (plat_st.c) */ +#define PLAT_CLOCK16_10S (hz60 ? 600 : 500) #endif diff --git a/c/platform/atarist/plat_st.c b/c/platform/atarist/plat_st.c index 6ede25d..d3ad6e8 100644 --- a/c/platform/atarist/plat_st.c +++ b/c/platform/atarist/plat_st.c @@ -245,7 +245,8 @@ uint8_t plat_break(void) { return keydown[K_ESC]; } /* ---- time ---- */ static uint32_t last_tick; -static uint8_t hz60, hz60_step; +uint8_t hz60; /* PLAT_CLOCK16_10S (plat_config.h) reads it too */ +static uint8_t hz60_step; #ifdef ST_NETTEST static uint32_t sync_in, sync_out, work_t, wait_t; /* timer A's ticks: the frame's work, its wait at the sync */ diff --git a/c/platform/c64/plat_c64.c b/c/platform/c64/plat_c64.c index 02ec686..a07fb1b 100644 --- a/c/platform/c64/plat_c64.c +++ b/c/platform/c64/plat_c64.c @@ -169,7 +169,8 @@ uint8_t plat_break(void) { /* RUN/STOP, quickly: row 7 alon } /* ---- time ---- */ -static uint8_t last_tick, ntsc, ntsc_step; +uint8_t ntsc; /* PLAT_CLOCK_QUARTER (plat_config.h) reads it too */ +static uint8_t last_tick, ntsc_step; #ifdef C64_NETTIME /* frames in a match (tools/crossplay.py reads them): the work from one diff --git a/c/platform/c64/plat_config.h b/c/platform/c64/plat_config.h index 7104925..39dc106 100644 --- a/c/platform/c64/plat_config.h +++ b/c/platform/c64/plat_config.h @@ -8,5 +8,6 @@ #define NET_PLATFORM "c64" /* the relay's statistics: &pf= (docs/net-protocol.md) */ extern volatile uint8_t c64_ticks; #define PLAT_CLOCK c64_ticks /* +1 every TV frame (c64.s): the match's silence (core/netplay.c) */ -#define PLAT_CLOCK_QUARTER 12 +extern uint8_t ntsc; /* 60 Hz (plat_c64.c) */ +#define PLAT_CLOCK_QUARTER (ntsc ? 15 : 12) #endif diff --git a/c/platform/mz/plat_config.h b/c/platform/mz/plat_config.h index 37fd14f..14e786c 100644 --- a/c/platform/mz/plat_config.h +++ b/c/platform/mz/plat_config.h @@ -8,8 +8,8 @@ #define PLAT_ASM_COMPOSITE 1 /* composite_map and hash_run are assembly (plat_mz.c) */ #define PLAT_ASM_HASH 1 #define PLAT_ASM_TEXT 1 /* print_string, hud_text, title_text* in cpu/z80/loops.c */ -#define PLAT_CLOCK (*(volatile uint8_t *)0xe007 = 0x40, (void)*(volatile uint8_t *)0xe005, \ - (uint8_t)~*(volatile uint8_t *)0xe005) /* the 8253's counter 1 (down, 15611 Hz): its high - byte, upwards, about 60 a second: the match's silence (core/netplay.c) */ +uint8_t mz_clock(void); /* the 8253's counter 1 (down, 15611 Hz): its high byte, upwards, about 60 + a second (plat_mz.c): the match's silence (core/netplay.c) */ +#define PLAT_CLOCK mz_clock() #define PLAT_CLOCK_QUARTER 15 #endif diff --git a/c/platform/mz/plat_mz.c b/c/platform/mz/plat_mz.c index 6406c88..1d3383e 100644 --- a/c/platform/mz/plat_mz.c +++ b/c/platform/mz/plat_mz.c @@ -563,6 +563,18 @@ void plat_init(void) __naked { __endasm; } +/* PLAT_CLOCK (plat_config.h): counter 1 latched and read whole, both bytes + * (a byte left unread would leave the next reader's order swapped) */ +uint8_t mz_clock(void) __naked { + __asm + call tone_now ; DE = counter 1 + ld a,d + cpl + ld l,a + ret + __endasm; +} + /* mz_frame_sync(ticks): elapsed = (prev - now) & 0xffff (down counter); spin until >= ticks */ void mz_frame_sync(uint16_t ticks) __naked { __asm