# VIXEN-32 NETWORK PONG Two people, two machines, one ball. The MEGA65 plays a PC — or another MEGA65 — through a small server that owns the ball and pairs whoever turns up. risc/pong/ the machine's client -> pong_bNN.rvk tools/pongserv/ the match server + a bot (build under WSL) tools/pongpc/ the PC client, Win32/GDI (build with MinGW) tools/pongmac/ the macOS client, Cocoa (build with Apple's clang) shared/pong_net.h the wire format shared/pong_sim.h THE RULES -- one copy, used by all of them ## Why there is a server Neither player hosts. Both clients connect **out** to a server, so nobody needs a forwarded port, a firewall exception or a fixed address, and two people behind two different routers can play. The server owns the ball and scores; the clients send where their paddle is and draw what they are told. Two machines simulating the same ball and correcting each other is the classic way to lose a fortnight, and there is no need for it here: a paddle is one number and the ball is four. ## Running a match Server, in WSL or on any Linux box (it needs a LAN address of its own — WSL in NAT mode cannot be reached by the MEGA65; bridged or mirrored networking can): cd tools/pongserv && make && ./pongserv An opponent, either a bot on the same box: ./pongtest -b -1 -n BOT # -1 makes it miss sometimes, so you can win or a person at a PC: tools\pongpc\pongpc.exe -s -n LEIF and the machine itself: HELP -> the folder holding `PONG_BNN.RVK` -> pick it, RUN, then press **2**. The first two to ask are put in a match. First to 9. `pongpc.exe -1` plays single player against the machine with no server and no network at all — the same rules, since both run `shared/pong_sim.h`. ## On the machine 1 one player, against the machine. No network needed. 2 two player. The network must already be UP: SHIFT+POUND at the BASIC prompt, because VIXEN-32 boots with it down. RUN/STOP leave a game, back to the menu **The mouse moves your paddle** — the machine has no paddle port, and the mouse counters are the analogue input it does have. Your own paddle is drawn from your own mouse and only the opponent's comes off the wire: waiting for the round trip to see your own hand move is the one place where the network would be felt as the machine being unresponsive. The menu shows the server it will use and whether the network is up. While joining, "WAITING FOR AN OPPONENT" means the server answered and nobody else has arrived; "NO ANSWER FROM ..." means nothing is at that address — a server that is not running, or a digit wrong in the config. UDP reports neither failure on its own, which is why the screen has to. ## Where the server address comes from `/VIXEN-32/TFTP/PONG.TXT` on the SD card, beside the .rvk: server: pong.example.com ; a name, or server: 192.168.5.102 ; an address port: 7777 **A name is looked up when you press 2**, not at the menu — the network may still be down when the menu is drawn — and the result is kept for the rest of the session. It needs DNS to work, which DHCP provides, or the `dns:` line in `NETWORK.TXT` for a static setup. A name that will not resolve says so separately from an address that will not answer: one is a DNS problem, the other a server problem, and sending a player to check the wrong one wastes their evening. A literal address needs no DNS at all, which is what you want on a LAN. Anything else in the file is ignored. If the file is missing the compiled-in default is used, and the menu says which of the two it took. Send a new one over TFTP whenever the server moves: tftp -v -m binary -c put PONG.TXT tftp/PONG.TXT ## The protocol, and the two rules that shaped it `shared/pong_net.h` has the whole of it. Little-endian, one packet per message, and: * **Every packet is padded to 32 bytes of payload.** Ethernet will not carry a frame under 60 bytes and a short one is dropped by the adapter before any counter in our code can see it — the bug that once made the machine able to answer a ping and unable to send one. A game's packets are exactly the small ones that fall foul of it. * **Every field is read and written a byte at a time.** A UDP payload starts 42 bytes into the frame, so it is never 4-byte aligned, and a struct laid over it would fault the RISC-V on the first 32-bit load. `STATE` is a **full snapshot** of the court every tick, never a delta, and paddles are sent as **absolute positions** rather than up/down. The Ethernet receiver holds one frame, so a client busy drawing will miss packets; with snapshots a lost one costs a single frame of smoothness and nothing else — there is no resend, no sequence repair and no divergence to detect. That is what lets the server be as small as it is. ## Two traps worth remembering **A networked .rvk must re-arm the module's clock.** `kmodnet` holds the `ticks_ms` function pointer it was given when the network came up, and that points into `basic_test.c` — code an app is loaded *over*. Calling it jumps into the game. `net_start()` therefore reads the current addressing back out of the module and the MAC registers and calls `knet_up()` again with the app's own clock, keeping the machine exactly where it was on the network. Any future networked app needs to do the same. **A player waiting for an opponent sends nothing but HELLO.** There is no INPUT to send before a match exists, so the server must count a HELLO as proof of life or it drops anyone who waits more than three seconds. It showed up only between two real players — pair someone with a bot and both join within a second, which is inside the window; walking from the PC to the MEGA65 is not. ## Builds Every `pong_bNN.rvk` is kept. `BUILD_NO` in the Makefile names the file and is compiled in as the number on the menu, so the two can never disagree. b1 first build b2 25-row screen (the hint moved below the play area), black court b3 config read from /VIXEN-32/TFTP/PONG.TXT b4 tells "no answer from " apart from "waiting for an opponent" b5 server: may be a DOMAIN NAME, resolved when a game starts