The loader, and the reason it is small: everything in a tune is an offset from wherever it was put, so taking one means adding the base to two tables and pointing four voices at their order lists. No sequence is walked. Nothing inside one is an address to be found and corrected, which is the difference between a malformed tune that plays wrongly and one that takes the loader with it. "SBTU", version, the tick in cycles, how many patches and sequences, offsets to the two tables, an order list each, and the patch each voice starts on. The magic is checked before anything else, because from there on the loader follows what the offsets name. break.sh shows what that guard is worth: without it, handing Play a PROGRAM runs the machine away until the cycle limit, rather than saying it is not a tune. PatchTable, SequenceTable and VoiceStart became pointers, so the engine does not care whether a tune came out of a file or was assembled in. Play's built-in tune now hands over the same three addresses a loaded one would, in eight lines - which is what keeps the two paths from drifting, and what made this rung change no scheduler code at all. Tests/maketune.py lays the fixture out byte by byte. IT IS NOT THE COMPILER: the sequences and the patches are literal bytes and the only thing computed is where each piece lands. That is the point - the loader is checked by something that does not share its idea of the format, which is the same reason SplitDisk and sbfs.asm share nothing but a specification. Both notes in the fixture are number 60, so the octave between them is a 0x80 command loading the second patch out of the file: the header, the tick, the relocation, an order list and a patch from a file, measured in one go. Play and the tune live on quiet.img rather than cosmos.img, so a fixture does not move ten recordings every time it changes size. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
87 lines
2.8 KiB
Python
87 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
# Lays out a .tune by hand, byte by byte, so that Play's loader has something to read before
|
|
# there is a compiler to write one.
|
|
#
|
|
# THIS IS NOT THE COMPILER. It is the hex editor: the sequences and the patches are written
|
|
# out as literal bytes and the only thing computed is where each piece lands, because counting
|
|
# offsets by hand is how you get a fixture that tests your arithmetic instead of the loader's.
|
|
#
|
|
# Written by Anachronaut
|
|
|
|
import sys
|
|
|
|
def patch(pairs):
|
|
out = [len(pairs)]
|
|
for parameter, value in pairs:
|
|
out += [parameter, value]
|
|
return out
|
|
|
|
# Two instruments differing in one parameter, the octave, so that the same note number sounds
|
|
# an octave apart depending on which is loaded.
|
|
def voicePatch(octave):
|
|
return patch([(0x00, 2), # saw
|
|
(0x01, 0xFF), # at full gain
|
|
(0x05, 1), # and on
|
|
(0x20, 0), # no attack
|
|
(0x21, 0), # no decay
|
|
(0x22, 0xFF), # held at full
|
|
(0x23, 5), # and gone quickly when the gate drops
|
|
(0x04, octave)])
|
|
|
|
patches = [voicePatch(128), voicePatch(129)]
|
|
|
|
# Note 60 for four ticks. The second sequence says it again, having changed instrument first -
|
|
# so the octave between them is the tune's doing and not the note's.
|
|
sequences = [[60, 4, 0xFF],
|
|
[0x80, 1, 60, 4, 0xFF]]
|
|
|
|
orders = [[0, 1, 0xFF], # voice 0 plays both, one after the other
|
|
[0xFF], # and the other three have no part
|
|
[0xFF],
|
|
[0xFF]]
|
|
|
|
starts = [0, 0, 0, 0]
|
|
tick = 125000 # a sixteenth note at 120 beats a minute
|
|
|
|
HEADER = 28
|
|
body, at = [], HEADER
|
|
|
|
def place(blob):
|
|
global at
|
|
where = at
|
|
body.extend(blob)
|
|
at += len(blob)
|
|
return where
|
|
|
|
# The tables come first so that their own offsets are known before anything they point at.
|
|
patchTableAt = at; at += 2 * len(patches)
|
|
sequenceTableAt = at; at += 2 * len(sequences)
|
|
|
|
patchAt = [place(p) for p in patches]
|
|
sequenceAt = [place(s) for s in sequences]
|
|
orderAt = [place(o) for o in orders]
|
|
|
|
def word(n):
|
|
return [(n >> 8) & 0xFF, n & 0xFF]
|
|
|
|
tables = []
|
|
for p in patchAt:
|
|
tables += word(p)
|
|
for q in sequenceAt:
|
|
tables += word(q)
|
|
body[0:0] = tables
|
|
|
|
header = [ord(c) for c in "SBTU"] + [1]
|
|
header += [(tick >> 16) & 0xFF, (tick >> 8) & 0xFF, tick & 0xFF]
|
|
header += [len(patches), len(sequences)]
|
|
header += word(patchTableAt) + word(sequenceTableAt)
|
|
for o in orderAt:
|
|
header += word(o)
|
|
header += starts
|
|
header += [0, 0]
|
|
assert len(header) == HEADER, "the header is %d bytes and the loader reads %d" % (len(header), HEADER)
|
|
|
|
open(sys.argv[1], "wb").write(bytes(header + body))
|
|
print("%s: %d bytes, %d patches, %d sequences" % (sys.argv[1], len(header) + len(body),
|
|
len(patches), len(sequences)))
|