Files
TeslaRel410/dpl3-revive/patha/vrserver.py
T
CydandClaude Fable 5 afc3fd839e Vendor dpl3-revive: the Division/DPL3 renderer, now ours
Bring the graphics-dev collaborator's dpl3-revive into the repo as first-class
project code (they've handed it off; it's ours now). This is the proven
Division renderer that our in-process rt_draw has been trying to be.

What's here:
- parser/  B2Z/V2Z/SVT/SCN/SPL/BGF/BMF/BSL decoders (pure Python).
- spec/    reverse-engineered format + the definitive VelociRender wire
           protocol (from the original DIVISION source, matches our live
           VPX node/action tables exactly).
- source-ref/  read-only copies of the original DIVISION C (BIZREAD.C,
           DPLTYPES.H, DPL.H) that define the formats.
- patha/   the "virtual VelociRender board": vrboard.py (24-action protocol
           server), vrview.py (numpy software rasterizer, the reference),
           vrview_gl.py (moderngl GPU backend, 832x512@60Hz), plus the
           run/replay/regress tooling and evidence renders. Drives FLYK/BLADE/
           Star Trek demos AND our btl4opt/rpl4opt game binaries.
- viewer/  WebGL archive generators (.py); prebuilt HTML/data regeneratable.
- samples/ small test models/textures.
- bt*.raw.bin  real BTL4OPT arena wire captures (kept for offline renderer
           testing/regression against OUR game).

.gitignore keeps the multi-hundred-MB demo capture dumps + debug logs +
regeneratable viewer bundles out of history (they stay on disk).

Phase 0 of the integration is validated: their board decodes our bt8 capture
with zero errors (3748 nodes, 507 instances, 4 mechs) and renders our arena
(terrain/dome/sky, correct Division DAC gamma). Plan + status in memory;
integration continues in emulator/RENDERER-COLLAB.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-05 22:06:25 -05:00

124 lines
4.9 KiB
Python

#!/usr/bin/env python3
"""
vrserver.py -- the board behind a socket. This is the endpoint the DOSBox-X C012
I/O handler connects to (see CAPTURE.md). It speaks RAW link bytes:
host -> board : the exact byte stream FLYK writes to port 0x151 (0x150 base)
board -> host : reply bytes FLYK reads from port 0x150
It logs every reassembled message and reply to a capture file, so a real FLYK boot
produces an annotated transcript we can diff against the source-derived spec and use
to pin the open items (readpixels layout, bit30 receive semantics).
Boot handling: on a link RESET marker (control byte from the handler), the board
re-arms and, per the source handshake, queues 3 iserver transactions so FLYK's
startup_handshake() proceeds into the framed HSP/860/init traffic.
python vrserver.py # listen on 127.0.0.1:8620, log to capture.log
python vrserver.py --selftest # loopback test: replay a synthetic boot+scene
"""
import socket, struct, sys, threading, time
from vrboard import VirtualBoard, Assembler, pack_iserver, A
HOST, PORT = '127.0.0.1', 8620
RESET_MARK = b'\x00RSET' # control frame the handler sends on a link reset strobe
class BoardSession:
def __init__(self, log):
self.board = VirtualBoard()
self.asm = Assembler()
self.out = bytearray() # board->host bytes waiting to be read
self.log = log
self.seq = 0
def on_reset(self):
self._emit("RESET (link re-armed; queuing 3 iserver handshake txns)")
self.out.clear()
# source: C-runtime does 3 iserver transactions before the host proceeds
for _ in range(3):
self.out += pack_iserver(2, b'\x20\x00\x00') # getenv-shaped stub
self.board.log.clear()
def feed(self, data):
"""Consume host->board bytes; return board->host bytes to send back."""
if not data:
return b''
self.asm.feed(data)
for msg in self.asm:
self.board.log.clear()
reply = self.board.handle(msg)
for line in self.board.log:
self._emit(" " + line)
self._emit(f"MSG {msg!r}")
if reply:
self.out += reply
act = struct.unpack_from('<II', reply, 0)[1]
self._emit(f" reply {A(act).name} ({len(reply)}B on wire)")
take = bytes(self.out); self.out.clear()
return take
def _emit(self, s):
self.seq += 1
line = f"[{self.seq:5}] {s}"
self.log.write(line + "\n"); self.log.flush()
def serve(logpath="capture.log"):
log = open(logpath, "w")
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind((HOST, PORT)); srv.listen(1)
print(f"vrserver listening on {HOST}:{PORT}, logging to {logpath}")
while True:
conn, addr = srv.accept()
sess = BoardSession(log)
with conn:
buf = b''
while True:
data = conn.recv(4096)
if not data:
break
# split off any RESET control frames (handler-injected)
while RESET_MARK in data:
i = data.index(RESET_MARK)
pre, data = data[:i], data[i+len(RESET_MARK):]
if pre: conn.sendall(sess.feed(pre))
sess.on_reset()
conn.sendall(bytes(sess.out)); sess.out.clear()
out = sess.feed(data)
if out: conn.sendall(out)
# ---- loopback self-test: a synthetic 'FLYK' that boots + builds a scene ----
def _selftest():
log = open("selftest_capture.log", "w")
sess = BoardSession(log)
got = bytearray()
# simulate: reset -> (board queues 3 iserver) -> host would service them ->
# then framed boot/init/scene traffic
sess.on_reset()
assert len(sess.out) > 0, "reset should queue handshake bytes"
sess.out.clear()
from vrboard import pack_vr
stream = pack_vr(A.code860, b'\x00'*40)
stream += pack_vr(A.init, b'video=svga|\n\x00')
stream += pack_vr(A.create, struct.pack('<I', 6))
stream += pack_vr(A.draw_scene, struct.pack('<I', 1))
# feed in two arbitrary chunks to exercise the partial-message reassembly
got += sess.feed(stream[:7])
got += sess.feed(stream[7:])
# board should have replied to init, create, draw_scene (3 replies)
asm = Assembler(); asm.feed(bytes(got))
replies = [m for m in asm]
names = [A(m.action).name for m in replies if not m.iserver]
print("replies decoded from board->host stream:", names)
assert names == ['init', 'create', 'draw_scene'], names
assert sess.board.frames == 1
log.close()
print("OK: socket board reassembles chunked input, boots, and replies correctly.")
print(" (see selftest_capture.log for the annotated transcript)")
if __name__ == '__main__':
if '--selftest' in sys.argv:
_selftest()
else:
serve()