Rework the megademo to fit the 80-column source line limit
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m29s
akbasic CI Build / coverage (push) Failing after 3m58s
akbasic CI Build / sanitizers (push) Failing after 4m36s
akbasic CI Build / mutation_test (push) Failing after 3m52s
akbasic CI Build / akgl_build (push) Failing after 7m29s
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m29s
akbasic CI Build / coverage (push) Failing after 3m58s
akbasic CI Build / sanitizers (push) Failing after 4m36s
akbasic CI Build / mutation_test (push) Failing after 3m52s
akbasic CI Build / akgl_build (push) Failing after 7m29s
AKBASIC_MAX_LINE_LENGTH's cut from 256 to 80 left seventeen lines of examples/megademo unloadable: the sixteen IM$() picture strings (up to 252 characters) and TUNEA/TUNEB's four-bar PLAY strings (174 and 175). The real ceiling is 78 characters, not 80 -- stdio_readline() refuses a read that fills the 80-byte buffer without a newline, so content plus its terminator must fit in 79. The picture: vaporwave.py's PAYLOAD drops from 240 to 64, so every emitted IM$(NN) = "..." line fits under the ceiling. chop() no longer slices blind; it walks the stream a record at a time -- two characters for a run, three for an R row record -- and never cuts inside one, because the decoder reads a record's tail with MID on the string it is walking and a record straddling two IM$ entries decodes as garbage. The old blind slice at 240 only happened to be safe. verify() now simulates the CHOPPED strings with the cursor threaded across the boundaries exactly the way DRAWSTREAM executes them, so a bad cut is an assertion failure instead of a corrupted screen, and emit_block() asserts every emitted line fits. The picture is 56 strings where it was 16; the decoder needed no changes at all, since it already carries X#/Y# from one IM$ entry to the next. The music: TUNEA and TUNEB each become four PLAY statements, one bar apiece. play.c keeps voice, envelope, level and duration state on the runtime across statements and every PLAY appends to the same queue, so four bars queue exactly as one long string did. Each bar restates the V1T3U9S prefix so a bar dropped by QFULL cannot leave the next batch playing on the drum kit's envelope. Everything still clears the shrunken pools with room to spare: 1625 source lines of 2048, ~704 array slots of 2048, identifiers within the 24-character symtab key. Verified end to end against this branch's build: the demo loads, the offscreen host renders every scene, and the scene-5 still is pixel-identical to vaporwave.py's own preview. The test suite fails the same seventeen cases with and without this commit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ACffnV6F7sxQuG3Y8a1L3s
This commit is contained in:
@@ -58,7 +58,14 @@ SKIP = "Q"
|
||||
ROWREC = "R"
|
||||
LENCH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
MAXRUN = len(LENCH)
|
||||
PAYLOAD = 240
|
||||
|
||||
# The interpreter reads source through an 80-byte line buffer and refuses
|
||||
# any line that fills it (AKBASIC_MAX_LINE_LENGTH, sink_stdio.c), so a
|
||||
# stored line is at most 78 characters plus its newline. 'IM$(NN) = "' and
|
||||
# the closing quote spend 12 of those; 64 keeps the emitted lines under
|
||||
# the ceiling with margin to spare while the index stays two digits.
|
||||
PAYLOAD = 64
|
||||
MAXLINE = 78
|
||||
|
||||
SUN_CX, SUN_CY, SUN_R = 80, 50, 30
|
||||
HORIZON = 74
|
||||
@@ -206,13 +213,32 @@ def encode_delta(prev, cur):
|
||||
|
||||
|
||||
def chop(blob):
|
||||
return [blob[i:i + PAYLOAD] for i in range(0, len(blob), PAYLOAD)]
|
||||
"""Split a stream into strings of at most PAYLOAD characters, cutting
|
||||
only between records. The decoder reads a record's tail characters
|
||||
with MID on the string it is walking, so a run (two characters) or a
|
||||
row record (three) that straddled two IM$ entries would decode as
|
||||
garbage; DRAWSTREAM only carries the cursor, never a partial record."""
|
||||
out, cur = [], ""
|
||||
p = 0
|
||||
while p < len(blob):
|
||||
n = 3 if blob[p] == ROWREC else 2
|
||||
if len(cur) + n > PAYLOAD:
|
||||
out.append(cur)
|
||||
cur = ""
|
||||
cur += blob[p:p + n]
|
||||
p += n
|
||||
if cur:
|
||||
out.append(cur)
|
||||
return out
|
||||
|
||||
|
||||
def simulate(raster, blob):
|
||||
def simulate(raster, blob, x=0, y=0):
|
||||
"""Apply one encoded stream to a raster exactly the way the BASIC
|
||||
decoder does, skips-draw-nothing and all."""
|
||||
x = y = p = 0
|
||||
decoder does, skips-draw-nothing and all. The cursor comes in and
|
||||
goes back out because DRAWSTREAM carries it from one IM$ entry to
|
||||
the next -- decoding the chopped strings one at a time with the
|
||||
cursor threaded through is exactly what the demo will execute."""
|
||||
p = 0
|
||||
while p < len(blob):
|
||||
c = blob[p]
|
||||
if c == ROWREC:
|
||||
@@ -230,20 +256,31 @@ def simulate(raster, blob):
|
||||
x = 0
|
||||
y += 1
|
||||
p += 2
|
||||
return raster, x, y
|
||||
|
||||
|
||||
def simulate_lines(raster, lines):
|
||||
"""One stream as its chopped strings, cursor carried across the
|
||||
boundaries the way DRAWSTREAM carries X# and Y#."""
|
||||
x = y = 0
|
||||
for line in lines:
|
||||
raster, x, y = simulate(raster, line, x, y)
|
||||
return raster
|
||||
|
||||
|
||||
def verify(frames, base_blob, delta_blobs):
|
||||
def verify(frames, base_lines, delta_line_groups):
|
||||
"""The base must reproduce frame 0 exactly, and each delta must
|
||||
carry the raster exactly to the next frame. A skip leaves the cell
|
||||
the encoder promised was already right, so equality is total and
|
||||
any difference at all is an encoder bug."""
|
||||
any difference at all is an encoder bug. This decodes the CHOPPED
|
||||
strings, not the blobs, so a chop that split a record would fail
|
||||
here instead of corrupting the screen."""
|
||||
raster = [[1] * W for _ in range(H)]
|
||||
raster = simulate(raster, base_blob)
|
||||
raster = simulate_lines(raster, base_lines)
|
||||
assert raster == frames[0], "base stream does not reproduce frame 0"
|
||||
for i, blob in enumerate(delta_blobs):
|
||||
for i, lines in enumerate(delta_line_groups):
|
||||
want = frames[(i + 1) % PHASES]
|
||||
raster = simulate(raster, blob)
|
||||
raster = simulate_lines(raster, lines)
|
||||
assert raster == want, "delta %d does not reproduce its frame" % i
|
||||
|
||||
|
||||
@@ -261,6 +298,9 @@ def emit_block(base_lines, delta_ranges, all_lines):
|
||||
for i, s in enumerate(all_lines):
|
||||
out.append('IM$(%d) = "%s"' % (i, s))
|
||||
out.append("REM ---- PICTURE-END")
|
||||
for line in out:
|
||||
assert len(line) <= MAXLINE, "emitted line over %d chars: %r" % (
|
||||
MAXLINE, line)
|
||||
return out
|
||||
|
||||
|
||||
@@ -303,15 +343,15 @@ def main():
|
||||
base_lines = chop(base_blob)
|
||||
all_lines = list(base_lines)
|
||||
delta_ranges = []
|
||||
delta_blobs = []
|
||||
delta_line_groups = []
|
||||
for i in range(PHASES):
|
||||
blob = encode_delta(frames[i], frames[(i + 1) % PHASES])
|
||||
delta_blobs.append(blob)
|
||||
lines = chop(blob)
|
||||
delta_line_groups.append(lines)
|
||||
delta_ranges.append((len(all_lines), len(all_lines) + len(lines) - 1))
|
||||
all_lines.extend(lines)
|
||||
verify(frames, base_blob, delta_blobs)
|
||||
dbytes = sum(len(b) for b in delta_blobs)
|
||||
verify(frames, base_lines, delta_line_groups)
|
||||
dbytes = sum(len(l) for g in delta_line_groups for l in g)
|
||||
print("base %d bytes in %d strings; video %d bytes in %d strings; "
|
||||
"%d strings total" %
|
||||
(sum(len(s) for s in base_lines), len(base_lines), dbytes,
|
||||
|
||||
Reference in New Issue
Block a user