58 lines
2.5 KiB
Python
58 lines
2.5 KiB
Python
"""Compare the real TLS frame payload against independent plain-cell evolution."""
|
|
from pathlib import Path
|
|
import json
|
|
import struct
|
|
import sys
|
|
|
|
from headless import connection, send, receive
|
|
from verify_rps_mvp_math import COLORS, init_word, scalar_step
|
|
|
|
cfg = json.loads(Path(sys.argv[2]).read_bytes())
|
|
width, height = 1024, 48
|
|
cfg["simulation"].update(width=width, height=height, seed="42", generations=4,
|
|
max_generations_per_second=60)
|
|
cells = bytearray()
|
|
for i in range(width * height // 32):
|
|
a, b = init_word(42, i)
|
|
cells.extend(((a >> j) & 1) | (((b >> j) & 1) << 1) for j in range(32))
|
|
initial = cells
|
|
for neighbors in (4, 8):
|
|
cells = initial
|
|
cfg["simulation"]["neighbors"] = neighbors
|
|
k = neighbors // 2
|
|
cfg["view"]["aggregation_side"] = k
|
|
roi = {"x": 992, "y": 16, "width": 32, "height": 32}
|
|
cfg["view"]["roi"] = roi
|
|
raw = json.dumps(cfg).encode()
|
|
gen = 0
|
|
with connection(sys.argv[1], int(sys.argv[3]) if len(sys.argv) > 3 else 11080) as tls:
|
|
send(tls, 1, raw)
|
|
assert receive(tls) == (2, raw)
|
|
while True:
|
|
kind, data = receive(tls)
|
|
assert kind == 3
|
|
frame, target, w, h, side, fmt = struct.unpack_from(">QQIIII", data)
|
|
assert (w, h, side, fmt) == (32//k, 32//k, k, 1)
|
|
while gen < target:
|
|
new = bytearray(len(cells))
|
|
for y in range(height):
|
|
for x in range(width):
|
|
present = {cells[((y+dy)%height)*width + (x+dx)%width]
|
|
for dy in (-1, 0, 1) for dx in (-1, 0, 1)
|
|
if (dx or dy) and (neighbors == 8 or not(dx and dy))}
|
|
i = y*width + x
|
|
new[i] = scalar_step(cells[i], 1 in present, 2 in present, 3 in present, 4)
|
|
cells = new
|
|
gen += 1
|
|
expected = bytearray()
|
|
for y in range(roi["y"], height, k):
|
|
for x in range(roi["x"], width, k):
|
|
colors = [COLORS[cells[(y+j)*width+x+i]] for j in range(k) for i in range(k)]
|
|
rgb = [(sum(channel)+k*k//2)//(k*k) for channel in zip(*colors)]
|
|
expected.extend([*reversed(rgb), 255])
|
|
assert expected == data[32:]
|
|
send(tls, 4, struct.pack(">Q", frame))
|
|
if gen == 4:
|
|
assert receive(tls) == (5, struct.pack(">QI", gen, 0))
|
|
break
|
|
print(f"PASS all transmitted BGRA pixels, neighbors={neighbors}")
|