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
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 44 additions & 14 deletions c/core/netplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,45 @@ 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, 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))
#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 {
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
}

/* a failed NET command: ask the device why (desync / dropped) */
static void net_fail(void) {
net_status_t st;
Expand Down Expand Up @@ -135,8 +174,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;
Expand All @@ -161,12 +200,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 */
Expand Down Expand Up @@ -234,7 +268,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 */
Expand All @@ -255,6 +288,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();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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 */
Expand All @@ -275,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;
Expand All @@ -283,12 +318,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;
Expand Down
3 changes: 2 additions & 1 deletion c/platform/amiga/plat_amiga.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions c/platform/amiga/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
#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) */
extern uint8_t ntsc; /* 60 Hz (plat_amiga.c) */
#define PLAT_CLOCK16_10S (ntsc ? 600 : 500)
#endif
3 changes: 2 additions & 1 deletion c/platform/atari/plat_atari.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down
3 changes: 3 additions & 0 deletions c/platform/atari/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,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): ~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) */
extern uint8_t ntsc; /* 60 Hz (plat_atari.c) */
#define PLAT_CLOCK_QUARTER (ntsc ? 15 : 12)
#endif
3 changes: 3 additions & 0 deletions c/platform/atarist/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 "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) */
extern uint8_t hz60; /* 60 Hz (plat_st.c) */
#define PLAT_CLOCK16_10S (hz60 ? 600 : 500)
#endif
3 changes: 2 additions & 1 deletion c/platform/atarist/plat_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion c/platform/c64/plat_c64.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions c/platform/c64/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
#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) */
extern uint8_t ntsc; /* 60 Hz (plat_c64.c) */
#define PLAT_CLOCK_QUARTER (ntsc ? 15 : 12)
#endif
3 changes: 3 additions & 0 deletions c/platform/cpc/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions c/platform/mz/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
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
12 changes: 12 additions & 0 deletions c/platform/mz/plat_mz.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions c/platform/pc/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions c/platform/web/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions c/platform/web/plat_web.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions c/platform/zx/plat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion docs/net-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion docs/setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
12 changes: 12 additions & 0 deletions tools/lockstep_zx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)))
Expand Down Expand Up @@ -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']))
Expand Down Expand Up @@ -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)
11 changes: 7 additions & 4 deletions tools/zxemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading