Put reusable rules in a local import
As a program grows, put calculations in a separate .zp64 file and import it by filename. The compiler includes the functions used by the caller in the same PRG. @import("c64") and @import("ZeroPage") instead name compiler-provided modules. The DLL chapters introduce a separate runtime loading path.
Call from the application
const math = @import("06_module_math.zp64");
// In main:
var total: u8 = math.add8(2, 5);
Define the rule
fn add8(a: u8, b: u8) u8 {
return a + b;
}
fn isEqual8(value: u8, expected: u8) bool {
return value == expected;
}
Expect IMPORT CALL OK and TOTAL=07. Keep both files in the same directory when moving the example. Try changing 5 to 6 and update the expected comparison from 7 to 8.
Choose a type for an event and for a missing result
The survey can dispatch a few keys directly. When different keys carry different kinds of data, a narrow tagged union gives each event its own payload. The selection holds a Mark enum, confirmation carries the PETSCII byte, and an ignored key needs no payload. switch exposes only the payload of the selected branch.
const c64 = @import("c64");
const Mark = enum(u8) { surveyed = 1, revisit = 2 };
const Event = union(enum) { ignore, select: Mark, confirm: u8 };
fn eventFromKey(key: u8) Event {
if (key == 0x31) {
return Event{ .select = .surveyed };
}
if (key == 0x32) {
return Event{ .select = .revisit };
}
if (key == c64.key.return_) {
return Event{ .confirm = key };
}
return Event{ .ignore = {} };
}
fn selectedMark(event: Event) ?Mark {
switch (event) {
.select => |mark| {
return mark;
},
else => {
return null;
},
}
}
export fn main() void {
c64.console.clear();
c64.console.print("1 SURVEY 2 REVISIT RETURN CONFIRM\n", .{});
const event = eventFromKey(c64.keyboard.waitPetscii());
const maybe_mark = selectedMark(event);
if (maybe_mark) |mark| {
c64.console.print("MARK {x}\n", .{mark});
} else {
switch (event) {
.confirm => |key| {
c64.console.print("CONFIRM {x}\n", .{key});
},
else => {
c64.console.print("NO MARK\n", .{});
},
}
}
}
Press 1 for MARK 01 or 2 for MARK 02. Reboot between inputs. Return follows the empty optional path but still carries a key byte, so expect CONFIRM 0D. Press A for NO MARK. The numeric key comparisons use PETSCII bytes from the keyboard. The next chapter distinguishes those from screen codes.
Know what carries over from Zig
Functions, local imports, records, and these captures use Zig syntax. The compiler parses the ordinary source with Zig's parser, then lowers the supported subset for the 6510. Choose fixed arrays, scalar results, and explicit optional captures for the patterns used here. General slices, generic functions, error unions, and std are outside the current subset. The language guide gives the supported forms and the target-specific arithmetic and layout rules.