Skip to content

Fix assembler parsing, script runner, disassembler and boot-drive bugs - #12

Merged
DNSGeek merged 17 commits into
mainfrom
claude/review-fixes-2
Oct 26, 2026
Merged

DNSGeek merged 17 commits into
mainfrom
claude/review-fixes-2

Conversation

@DNSGeek

@DNSGeek DNSGeek commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Second batch from the code review: the ten issues ranked "most worth fixing next" after #11. Most of them break things the manuals tell users to do. Every fix has a py65 regression test, and the script-runner fixes were also checked against the real BASIC ROM on py65.

Assembler (modasm)

  • Label then directive on one line (table: .byte 1,2,3, start: *= $C000) now works; it used to give BAD MNEMONIC.
  • @local labels are scoped to the preceding global label, as the manual's examples assume. They're stored as @ + scope number + the first 5 characters, so @loop1 and @loop12 are the same label (documented).
  • Mid-source .org pads the gap. The load address is the PC at the first emitted byte. A backward .org gives a new ORG BACKWARDS error. PHASE ERROR is now checked at every label, since a mid-source .org used to mask divergence.
  • < and > apply to the whole expression: #<(table+10) works, and #<msg+1 means <(msg+1) (documented).
  • Leftover text is an error instead of being ignored: STA1 $D020 no longer assembles as STA $01. A trailing ; comment is still fine, and LDA label + 2 now works as written.
  • Manual fixes: §13's example said SYS 2062 but main is at 2061. The error table now lists BRANCH RANGE, PHASE ERROR and ORG BACKWARDS.

Script runner

  • ONERR: it read LINNUM, which $B1AA never writes, so the target line was random. It now uses FRMNUM + GETADR, so the range is 0-65535 with type checking.
  • IF … THEN <extended keyword>: this gave ?SYNTAX ERROR, because the ROM's IF jumps straight to $A7ED and never reaches IGONE. That includes the manual's IF EX=1 THEN DELETE "DEMO". MODSCT now emits : between THEN and an extended token.

Disassembler

  • Oversized output now stops on a whole line and ends with ; *** DISASSEMBLY TRUNCATED ***. It used to cut silently mid-line.
  • A binary that ends partway through an instruction shows the leftover bytes as .BYTE, instead of reading operands out of the output text.
  • F8 → Disassemble now closes the gap first. Everything after the cursor used to be ignored, then lost.

Tokenizer and loader

  • Statement-only extended keywords: MODTOK tokenizes PETProject's keywords ($CC-$D8) only at a statement start and not as an assignment target. So DRIVE=8, PRINT STATUS and A=COPY+1 stay variables a stock C64 can run, while DRIVE 9 and RUNPROG "X", which RUNPROG-chained programs rely on, still tokenize.
  • Modules load from the boot drive (FA at startup) instead of always device 8. The data drive defaults to it too. The script runner's ASSEMBLE gets it through $022B.

Testing

  • tests/test_include.py and tests/test_modules.py both pass, and the C64 + C128 builds are clean.
  • Round-trip check: 60 random legal-opcode programs went through MODDIS and back through MODASM. 58 came back byte-identical. The other 2 use absolute,X with an address under $0100, which MODASM assembles in the shorter zero-page form, like most assemblers. That behaviour predates this PR.
  • Not run in VICE or on real hardware.

Reviewer notes

  • Size: modasm grew 186 bytes, and its code now ends around $B24C (guard at $BD00). Its new state is at $BF36-$BF3A.
  • RUNPROG-chained programs are tokenized by MODTOK, not MODSCT, so IF … THEN DELETE inside a chained program still hits the ROM limitation. Fixing that in MODTOK would add a visible : to the user's text on every save, so it's left for a decision.

🤖 Generated with Claude Code

DNSGeek and others added 17 commits October 25, 2026 17:46
Modules always loaded from device 8, so booting from drive 9 (C128
BOOT U9, or LOAD"PETPROJECT",9 on a C64) left every module "not found",
including MODDET, and BASIC files then loaded as raw tokens. boot128
already loads the editor from FA. init_settings now records FA as
PROG_DRIVE (8 if FA isn't a disk device), module loads use it, and the
data drive defaults to it when Settings can show it (8-11). It's also
published at $022B for the script runner, which loads MODASM itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
modtok's table includes the script runner's keywords ($CC-$D8), so a
plain BASIC save turned DRIVE=8, PRINT STATUS or A=COPY+1 into tokens a
stock C64 rejects with ?SYNTAX ERROR. They can't simply be dropped, since
programs chained with RUNPROG are documented to use them. But the runner
dispatches them only as statements (through IGONE), so tokenize them
only at a statement start (line start, after an unquoted ':' or THEN)
and not as an assignment target. Elsewhere the letters stay a variable
name, as on a stock C64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MODDIS takes the binary from work_buf up to GAP_START. With the cursor
mid-buffer, everything after it was ignored, and then lost, because the
disassembly replaces the buffer. Compact the gap first, as save does,
so the whole buffer is disassembled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hnd_onerr called FRMEVL ($AD9E) and AYINT ($B1AA), then read the result
from $14/$15. AYINT returns the integer in A/Y (LDA $64 / LDY $65 at
$B1AD) and never writes LINNUM, so ONERR registered whatever stale line
number $14/$15 held from earlier parsing. FRMEVL also skips the type
check (a string argument was accepted), and AYINT caps the value at
32767 (?ILLEGAL QUANTITY above that).

Use FRMNUM ($AD8A: JSR FRMEVL, then CHKNUM) and GETADR ($B7F7), which
converts FAC1 to an unsigned 16-bit value and stores it in LINNUM
(STY $14 / STA $15 at $B808: $14 = lo, $15 = hi), range 0-65535 -- the
same pair the ROM uses for POKE/WAIT addresses. Store $14 as lo and $15
as hi.

Checked by running the ROM bytes from Resources/basic.asm on py65: for
"1000" the old pair leaves $14/$15 untouched (A/Y = $03/$E8), the new
one leaves $E8/$03; 40000 and 65535 now convert, 65536 and -1 raise
?ILLEGAL QUANTITY, a string raises ?TYPE MISMATCH.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After THEN, BASIC's IF handler executes the statement with JMP $A7ED
($A940-$A948) -- GONE's execute-statement entry, not the IGONE vector
at $0308 -- so the resident handler never sees the token, and $A7ED
raises ?SYNTAX ERROR for $CC-$D8. The manual's own example
IF EX=1 THEN DELETE "DEMO" failed this way.

When an extended token directly follows THEN (spaces ignored), the
tokenizer now emits ':' in front of it. $A7ED returns on ':' (BEQ
$A82B), NEWSTT sees the ':' and dispatches the next statement through
IGONE, where the handler runs the keyword. A false IF skips to the end
of the line (REMN, $A93B -> $A909) regardless, so the ':' changes
nothing else. The ':' goes through emit_byte + inc_basic_addr like any
other output byte, so link words and the overflow check count it.
THEN <line number>, IF ... GOTO and stock keywords after THEN are
unchanged; REM, DATA and string contents never reach the keyword path.
The check lives in a subroutine (tok_then_colon) to keep the token
loop's branches in range.

Tests: run_module can now emulate REU DMA on writes to $DF01, and a new
test drives MODSCT and checks the REU output (the ':' after THEN, the
link chain, and the forms that must stay untouched). Separately, running
the tokenized script under the ROM bytes from Resources/basic.asm on
py65 with a counting IGONE hook: the true IF runs DELETE once, the false
IF skips both DELETEs on its line, and the same program without the ':'
stops with ?SYNTAX ERROR.

The manual documents this and ONERR's 0-65535 range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
parse_line only checked for '*=' and '.' before looking for a label;
once a label was consumed it went straight to parse_mnemonic, so
"table: .byte 1,2,3", "msg: .text "HI"" and "start: *= $C000" all
failed with BAD MNEMONIC even though the manual documents labels on
directive lines.  The label is now read first and the statement after
it goes through the same '*' / '.' / mnemonic dispatch as an unlabelled
line.

The origin handling of '*=' and '.org' is shared in set_org.  A label
on an origin line names the new address (pass 1 re-points the symbol it
just appended), which is what "start: *= $C000" is written to mean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASSEMBLE loaded MODASM from a hard-coded device 8, so a script run from
an IDE booted on another drive could not find the assembler. The editor
now writes its boot drive into MOD_PROG_DRIVE ($022B) every time it runs
a module, MODSCRH included, but page 2 is overwritten once BASIC runs the
script. So the execute stub at $C000 now jumps to hnd_install, which
copies $022B into hnd_home_drive (falling back to 8 when the byte is
outside 8-30) before reporting success; the MODASM load uses that
instead of #8.

The stub stays 7 bytes (JMP + NOP padding, with an assert) so the fixed
entries at $C007 and the state block at $C010-$C016 that MODSCR writes
do not move. Checked on py65: $022B = 8/9/11/30 is kept, 0/7/31/$FF
give 8, MOD_STATUS is $00 in every case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Output runs ~15x the binary size, so anything past ~1.5 KB of PRG
overflows the 24 K buffer. The overflow check compared only the page of
DST_PTR before each line, and emit_dst dropped every byte in BUF_END's
page, so the listing ended mid-line with no CR, emit_trunc_notice was
never called, and the user got no hint anything was missing. Now each
line is preceded by a 16-bit check that DST + longest line (37) +
notice (32) stays below BUF_END, the listing ends with the comment line
"; *** DISASSEMBLY TRUNCATED ***" (a ';' line, so it still assembles),
and at least one byte of gap is always left (MOD_NEW_END < BUF_END).
emit_dst's page guard is gone: check_room now covers every write, and the
guard wasted up to 255 bytes when BUF_END was not page-aligned.

A final instruction with fewer bytes left than its size took its operands
from past DIS_SRC_END - where the output text starts - so a PRG ending
in a lone $20 showed as "JSR $4F2E". The opcode and the bytes after it
now come out as ".BYTE $XX" lines. emit_illegal pads to the same 37
columns as an instruction line so the hint column lines up.

Output is unchanged for every input that hits neither case (old-vs-new
py65 comparison, C64 and C128 builds). Tests cover both cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The manual's addressing-mode section and its complete example use
"@loop:" / "BPL @loop", but '@' was not a label character, so both the
definition and the reference failed.  The conventional meaning of an
@name - and the only one that lets every routine have its own @loop -
is a label local to the most recent global label, so that is what is
implemented.

read_name (now shared by check_for_label and parse_label_ref) stores a
local as '@', a 16-bit scope number, then the first 5 name characters,
inside the existing 8-byte symbol name.  The scope number counts the
global labels seen so far in the pass, identically in both passes, so
forward references resolve.  Globals cannot start with '@' and locals
in different scopes differ in the scope bytes, so mangled names never
collide.  The manual documents the scoping and the 5-character limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	tests/test_modules.py
'<' and '>' only wrapped parse_expr_base, so the offset parse ran only
on the non-lohi path: "LDA #<(table+10)" - the manual's own example -
failed with BAD OPERAND (parse_label_ref saw '('), and "LDA #<msg+1"
silently assembled "#<msg" with the "+1" left over and ignored.

parse_expr now parses a full sum (parse_sum: base followed by any
number of +N / -N numeric offsets) and applies '<' / '>' to the
result, with optional parentheses directly after the operator.  The
manual is silent on "<msg+1", so it follows the usual 6502-assembler
reading, <(msg+1), and now says so.  Spaces around '+' / '-' are
accepted (and documented), so "LDA label + 2" is no longer cut short
at the space.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Nothing checked that a statement ended where its parse did, so text
after it was silently dropped: "LDA #1 2" assembled as "LDA #1",
".byte 1 2" emitted just 1, and the typo "STA1 $D020" - parse_mnemonic
read three letters and stopped - assembled as "STA $01" followed by
nothing, a wrong store with no error.

After an instruction's operand, after .org / *= values, after the last
.byte / .word value and after a .text string, only spaces and an
optional ';' comment may now follow (at_eol), else SYNTAX ERROR.  A
mnemonic and a directive name must be followed by a space or the end of
the statement (at_delim), else BAD MNEMONIC / SYNTAX ERROR.  .include
checks the rest of its line before opening the file, so
'.include "x" ; comment' still works; the manual's "anything after the
closing quote is ignored" is replaced by the new rule.

The directive name is also read as letters only and zero-padded: the
old loop ran until a space or CR, so a directive at the very end of
the source (no CR) scanned on past the end of the buffer, and a short name could match on
stale bytes of an earlier directive's name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
emit_byte wrote the output stream contiguously and the PRG header used
the first .org seen, so the file only matched the labels when there was
a single .org before any code:
  - ".org $C000 / RTS / .org $C100 / data: .byte 1" loaded the byte at
    $C001 while "data" was $C100;
  - "NOP / .org $C000" loaded the NOP at $C000 while its labels said
    $0801.

The output is now one contiguous image starting at the load address,
which is the PC at the first emitted byte (mark_emit, pass 1; $0801 if
nothing is emitted), so code before a leading .org loads where its
labels say.  After that an origin may only move forward: pass 2 pads
the gap with $00 bytes, and a backward move is the new ORG BACKWARDS
error, raised in pass 1 so no file is written.  emit_byte now runs in
both passes (pass 1 only notes the position), which removes the pass
checks from the .byte/.word/.text loops; their output errors now report
I/O ERROR instead of SYNTAX ERROR.

An .org re-synchronises the two passes' PCs, which could hide a phase
divergence (e.g. a forward reference sized ABS in pass 1 that resolves
to zero page) from the end-of-pass PHASE ERROR check.  Pass 2 now also
checks every label against the PC (or, on an origin line, the new PC),
reporting PHASE ERROR at the first misplaced label.  The end-of-pass
check stays for unlabelled trailing code.

The manual documents the load-address rule, the padding, and the
BRANCH RANGE / ORG BACKWARDS / PHASE ERROR messages (the error table
listed neither PHASE ERROR nor BRANCH RANGE, and claimed an
out-of-range branch is BAD ADDR MODE).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The section 13 example could not assemble before the label/@Local
fixes; it now does, and a test assembles it verbatim from the manual so
the two stay in sync.  Checking its bytes showed the example itself was
wrong: the stub is 12 bytes (a 10-byte BASIC line plus the 2-byte
end-of-program marker), so main is at $080D = 2061, not $080E = 2062 -
"SYS 2062" would have entered main one byte in, in the middle of
"LDX #39".  Its header comment also said it fills the row with '@'
(screen code $00) while the code writes spaces ($20).

Also brings modasm.asm's header summary of the source syntax up to
date with the parser changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DNSGeek
DNSGeek merged commit 11a16b6 into main Oct 26, 2026
4 checks passed
@DNSGeek
DNSGeek deleted the claude/review-fixes-2 branch October 26, 2026 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant