hmcc Users Guide

hmcc is a standalone Lynx Game Development SDK utility that compiles HandyMusic text scripts — instruments, sound effects and up to four music tracks — into the byte-coded binaries the HandyMusic driver replays on target. The scripts are the authoring format only; they are never parsed on the Lynx. hmcc does the parsing at build time and emits a fixed-layout .sfx or .mus blob (plus an .equ equates file in SFX mode). It is host-compiled and does not depend on the cc65 toolchain. hmcc is a faithful, portable port of Osman Celimli's original Win32 “HMCC” tool; see section 7 and the Licenses page for its provenance.

1. Overview

HandyMusic is a script-driven, macro-instrument sound driver. Instruments and sound effects are byte-code envelopes — waveform, volume and frequency scripts with loops and a note-off section — and music is up to four parallel track scripts that reference those instruments. hmcc is the tool that turns the text form of all this into the binary the driver reads. You write the material as plain .txt scripts and hmcc lowers it to the fixed-layout blob once, at build time, so the cartridge carries only the byte-coded result and the driver never has to parse ASCII.

Keeping the compiler off-target keeps the runtime free of a text tokenizer and lets the tool be a small host C program. hmcc lives under tools/hmcc/ and its binary installs into the root bin/ alongside the rest of the SDK utilities. Because HandyMusic bakes absolute address tables into its output, every hmcc invocation takes a $dest_addr base and the resulting data is only valid when loaded at exactly that address (see section 5). The script grammar and the driver it feeds are specified in full in design/LYNX_HANDYMUSIC_DESIGN.md.

inst.txt sfx.txt trk_1…4.txt HandyMusic text scripts bin/hmcc $dest_addr out.mus sfx.sfx sfx.equ ↓  ca65 .incbin at the reserved region  ↓ game.o + lynx-handymusic driver → game.lnx
hmcc compiles the text scripts once at build time; the game .incbins the blob at the base address it was built for and links it with the HandyMusic driver.

2. The two compile modes

hmcc picks its mode from the number of arguments. With a single script it runs in SFX mode: the script holds instrument/sound-effect definitions and hmcc writes a .sfx binary plus an .equ equates file mapping each name to its absolute address. With an output name, an instrument script and one to four track scripts it runs in Music mode: it assembles the tracks, appends the instrument/SFX table after a 16-byte header, and writes a single .mus binary.

ModeInvocationReadsWrites
SFXhmcc $addr sfx.txtone instrument/SFX scriptsfx.sfx + sfx.equ
Musichmcc $addr out.mus inst.txt trk1.txt…instrument script + 1–4 tracksout.mus

Both modes share the same instrument/SFX script language (section 3); Music mode adds the track language (section 4). In Music mode the SFX table is placed 16 bytes after the base ($addr + 16) to sit directly behind the .mus header.

3. Instrument & SFX scripts

3.1. Declaring an instrument

A script is line-oriented. Whitespace separates tokens and a ; starts a comment that runs to end of line. Each instrument begins with a bare name line; in SFX mode the name is followed by a priority number (0–255). The lines up to an opening { are the header, which seeds the instrument's starting waveform, volume and frequency and its tuning. The lines between { and } are the body — the per-tick script the driver steps through. An optional noteoff marker splits the body into the part that plays while a note is held and the release that runs after note-off.

bass  10  ; name [priority] HEADER — seeds the 14-byte envelope head tuning 2.0 waveform / volume / frequency { BODY — per-tick play script volume… frequency… loop… noteoff — release section starts here volume… end }
An instrument: a name line (with priority in SFX mode), a header that seeds the 14-byte envelope head, and a braced body split by an optional noteoff.

3.2. Header and body commands

Every command has a long name and a short alias; both are accepted. Numbers may carry a fractional part (8.8 / 16.8 fixed point) where the adjust fields allow it. The same waveform, volume and frequency commands appear in both the header (setting the starting value) and the body (issuing a change over time); the loop, rest, note-off and end commands are body-only.

LongShortArgumentsWhereEffect
tuningtperiodheaderwaveform period used to turn notes into dividers (default 2.0)
waveformwfeedback [shift]bothLFSR feedback taps and shift-register seed (timbre)
volumevamplitude [adjust]bothset volume, with an optional per-tick 8.8 adjustment
frequencyfdivider [adjust]bothset the 10-bit divider, with an optional 16.8 per-tick slide
restrdelaybodywait delay ticks before the next command
looplcountbodyopen a loop; a negative count loops forever
endloopel—bodyclose the innermost loop
noteoffn—bodymark where the release section begins
ende—bodystop the envelope

3.3. Tuning and note frequencies

The compiled envelope stores a 10-bit Lynx divider, not a frequency. Music tracks name notes in a three-character nXo form — note letter, an optional b (flat) or s (sharp), then an octave digit — e.g. a.4, cs5, eb3 (a plain . means “natural”). hmcc converts the note to a frequency relative to A4 = 440 Hz, multiplies by the instrument's tuning period, and folds the product through the Lynx's piecewise divider table to get the value the hardware programs. The default tuning of 2.0 corresponds to a plain square pulse; a different waveform period needs a matching tuning so notes land in tune.

4. Music scripts

This section is the grammar of a track file. For a worked walk-through of the step before it — turning a piece of sheet music into these note tokens and tick counts, with staff examples and a rhythm-to-ticks table — see Encoding sheet music for HandyMusic.

4.1. Blocks, tracks and the main block

A track file is a set of named blocks. A block is a label followed by a body in braces:

intro {
    using bass
    pan 0
    a.3 r 4
    c.4 r 4
    ...
}

One block per track file carries a priority number after its name — that is the main block, the track's entry point, and there may be only one per file. Other blocks are reusable fragments a track jumps into with call and leaves with return. Each of the up to four track files becomes one of the four Mikey audio channels, in the order they are passed on the command line.

4.2. Track commands

Inside a block, bare tokens in nXo form (section 3.3) are notes; everything else is a command with a long name and short alias:

LongShortArgumentsEffect
usinguinstrumentselect the instrument this block plays with
priorityprvalueset the channel priority
panpattenuationset stereo attenuation for the channel
pitchptadjustmentapply a running pitch (divider) adjustment
restrtickssilence for ticks ticks
waitwtickshold the current note for ticks ticks
looplcountopen a loop (negative count loops forever)
endloopel—close the innermost loop
samplesmpnumbertrigger a PCM sample
sfxsticks [divider]fire a sound effect for ticks, optional divider override
callcblockLabeljump into another block, saving a return address
returnrt—return from a called block
breakb—break out of the current loop
ende—stop the track

5. Output files and the base address

The first argument to hmcc is a base address written as $dest_addr (a 16-bit hex value; a fractional part is rejected). HandyMusic bakes absolute pointers into its output — the .mus header holds absolute track and instrument-table pointers, and the SFX table holds absolute per-effect addresses — so the produced data is only valid when it is loaded at exactly that base. The intended model is a fixed data region reserved in the Lynx memory config: reserve the region, pass its address as $dest_addr, and .incbin the blob there.

In SFX mode the base is the SFX table itself; hmcc also writes an .equ file giving each effect a symbol at its absolute address so game code can name them. In Music mode the SFX table follows the header, so it is placed at base + 16, and the .mus file is a 16-byte header followed by the instrument/SFX table and the packed tracks.

$dest_addr +16 header instrument / SFX table track 1 … track 4 16 bytes absolute effect addrs packed note/command streams absolute pointers baked to $dest_addr — load the blob only at that address
The .mus layout: a 16-byte header of absolute pointers, the instrument/SFX table at base + 16, then the four packed tracks.

6. Usage

6.1. Command line

hmcc has no option flags — the argument count selects the mode and the first argument is always the base address:

hmcc  $dest_addr  sfx_file.txt
hmcc  $dest_addr  outfile.mus  inst_file.txt  trk_1.txt [trk_2.txt] [trk_3.txt] [trk_4.txt]

  $dest_addr     16-bit load address, e.g. $b000 (no fractional part)
  sfx_file.txt   SFX mode: instrument/SFX script -> sfx_file.sfx + sfx_file.equ
  outfile.mus    Music mode: output binary
  inst_file.txt  instrument/SFX definitions referenced by the tracks
  trk_N.txt      one to four track scripts, one per Mikey channel

The output names follow the inputs: SFX mode replaces the script's extension with .sfx and .equ; Music mode writes exactly the outfile.mus you name and prints its final size.

6.2. The build flow

hmcc is wired into a build like any other asset generator. Reserve the data region in the memory config, compile the scripts to a blob at that base, and .incbin the result:

HMCC := hmcc
MUS_BASE := $b000

assets/theme.mus: assets/inst.txt assets/t1.txt assets/t2.txt
	$(HMCC) $(MUS_BASE) $@ $^

theme.o: theme.s assets/theme.mus      ; theme.s does: .incbin "assets/theme.mus"

game.lnx: main.o theme.o
	cl65 -o $@ $^

Because the blob carries absolute pointers, the MUS_BASE passed to hmcc must match the address the region is linked at; changing one means changing the other. For the driver that replays these binaries and the C entry points that start and stop playback, see the HandyMusic design document and the audio material on the Sound page.

7. Copyright

hmcc is part of the lynxcc Lynx Game Development SDK. It is a derived port of Osman Celimli's HandyMusic “HMCC” tool: the Win32 file I/O and debug scaffolding were replaced with portable C, while the script-compiler logic itself is unchanged. It is distributed under the author's “free to use and modify” grant rather than the MPL that covers most of the fork. That grant, the attribution to Osman Celimli, and the list of derived files are reproduced verbatim, together with every other copyright notice in the distribution, on the consolidated Licenses and copyright notices page.