romdevtools 0.70.0 → 0.71.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/examples/dreamcast/platformer/main.c +31 -0
  3. package/examples/dreamcast/puzzle/main.c +44 -0
  4. package/examples/dreamcast/racing/main.c +39 -0
  5. package/examples/dreamcast/shmup/main.c +50 -0
  6. package/examples/dreamcast/sports/main.c +39 -0
  7. package/package.json +3 -3
  8. package/src/cores/capabilities.js +13 -9
  9. package/src/host/LibretroHost.js +242 -23
  10. package/src/host/callbacks.js +68 -1
  11. package/src/host/cpu-state.js +32 -0
  12. package/src/host/dc-aica-state.js +67 -0
  13. package/src/mcp/tools/audio.js +1 -1
  14. package/src/mcp/tools/disasm.js +1 -1
  15. package/src/mcp/tools/index.js +1 -1
  16. package/src/mcp/tools/platform-docs.js +1 -1
  17. package/src/mcp/tools/platform-tools.js +9 -1
  18. package/src/mcp/tools/platforms.js +46 -10
  19. package/src/mcp/tools/toolchain.js +140 -10
  20. package/src/platforms/dreamcast/MENTAL_MODEL.md +87 -0
  21. package/src/platforms/dreamcast/TROUBLESHOOTING.md +55 -0
  22. package/src/platforms/dreamcast/UPSTREAM_SOURCES.md +57 -0
  23. package/src/platforms/gba/MENTAL_MODEL.md +22 -1
  24. package/src/platforms/n64/MENTAL_MODEL.md +84 -0
  25. package/src/platforms/n64/TROUBLESHOOTING.md +60 -0
  26. package/src/platforms/n64/UPSTREAM_SOURCES.md +52 -0
  27. package/src/platforms/n64/lib/c/n64.c +181 -80
  28. package/src/platforms/ps1/MENTAL_MODEL.md +85 -0
  29. package/src/platforms/ps1/TROUBLESHOOTING.md +55 -0
  30. package/src/platforms/ps1/UPSTREAM_SOURCES.md +54 -0
  31. package/src/platforms/snes/TROUBLESHOOTING.md +10 -0
  32. package/src/toolchains/asar/asar.js +84 -14
  33. package/src/toolchains/mips-c/mips-c.js +35 -1
  34. package/src/toolchains/sh-c/lib/dc.h +65 -15
  35. package/src/toolchains/sh-c/sh-c.js +6 -3
@@ -42,6 +42,7 @@ import { decodeLynxMikey, decodeLynxPalette } from "../../host/lynx-mikey-state.
42
42
  import { getPcePsgState } from "../../host/pce-psg-state.js";
43
43
  import { decodePs1Spu } from "../../host/ps1-spu-state.js";
44
44
  import { decodeN64Ai } from "../../host/n64-ai-state.js";
45
+ import { decodeAica } from "../../host/dc-aica-state.js";
45
46
  import { getMsxAyState } from "../../host/msx-ay-state.js";
46
47
  import { decodeGbaSprites, decodeGbaPalette } from "../../host/gba-video-state.js";
47
48
 
@@ -440,7 +441,14 @@ export function registerPlatformTools(server, z, sessionKey) {
440
441
  if (!regs) throw new Error("getAudioState chip:'ai' — no AI region (load an N64 ROM into the rebuilt parallel_n64 core).");
441
442
  return { platform: "n64", ...decodeN64Ai(regs) };
442
443
  }
443
- throw new Error(`getAudioState: unknown chip '${chip}'. Use 'nes' (NES 2A03), 'gb' (Game Boy/GBC), 'gba' (GBA), 'dsp' (SNES), 'psg' (Genesis/SMS/GG SN76489), 'ym2612' (Genesis FM), 'sid' (C64), 'mikey' (Lynx), 'pce', 'ay8910' (MSX), or 'spu' (PS1).`);
444
+ if (chip === "aica") {
445
+ // Dreamcast AICA — 64 PCM/ADPCM channels (key-on/volume/pitch/loop) + master
446
+ // volume, from the rebuilt flycast core's romdev_aica_get register window.
447
+ const regs = host.getAicaRegs?.();
448
+ if (!regs) throw new Error("getAudioState chip:'aica' — no AICA region (load a Dreamcast program into the rebuilt flycast core).");
449
+ return { platform: "dreamcast", ...decodeAica(regs) };
450
+ }
451
+ throw new Error(`getAudioState: unknown chip '${chip}'. Use 'nes' (NES 2A03), 'gb' (Game Boy/GBC), 'gba' (GBA), 'dsp' (SNES), 'psg' (Genesis/SMS/GG SN76489), 'ym2612' (Genesis FM), 'sid' (C64), 'mikey' (Lynx), 'pce', 'ay8910' (MSX), 'spu' (PS1), or 'aica' (Dreamcast).`);
444
452
  }
445
453
 
446
454
  getAudioStateCore = async ({ chip }, callerSessionKey) => jsonContent(readAudioChip(chip, callerSessionKey));
@@ -93,9 +93,17 @@ const PLATFORM_QUIRKS = {
93
93
  };
94
94
 
95
95
  /** op:'list' — every platform with core/toolchains/languages/quirks. */
96
- export function listPlatformsCore() {
96
+ export function listPlatformsCore({ platform, slim } = {}) {
97
97
  const available = new Set(listAvailableCores());
98
- const platforms = Object.entries(CORES).map(([id, info]) => {
98
+ let ids = Object.keys(CORES);
99
+ if (platform) {
100
+ if (!CORES[platform]) {
101
+ throw new Error(`platform({op:'list'}): unknown platform '${platform}'. Known: ${ids.join(", ")}.`);
102
+ }
103
+ ids = [platform]; // per-platform filter — the big token-sink fix (v0.71.0 fb)
104
+ }
105
+ const platforms = ids.map((id) => {
106
+ const info = CORES[id];
99
107
  const toolchains = Object.values(TOOLCHAINS)
100
108
  .filter((t) => t.platforms.includes(id))
101
109
  .map((t) => ({ id: t.id, displayName: t.displayName, tier: t.tier }));
@@ -107,18 +115,45 @@ export function listPlatformsCore() {
107
115
  toolchains,
108
116
  };
109
117
  const langs = getLanguageOptions(id);
110
- if (langs) entry.languages = langs;
111
- if (PLATFORM_QUIRKS[id]) entry.quirks = PLATFORM_QUIRKS[id];
118
+ if (langs) {
119
+ // `slim` drops the heavy per-language `note` + `quirks` prose (the big
120
+ // token sink when you only need "which platforms/toolchains/languages
121
+ // exist"). Detail stays behind platform({op:'doc'}) + op:'capabilities'.
122
+ entry.languages = slim
123
+ ? { defaultLanguage: langs.defaultLanguage,
124
+ languages: (langs.languages || []).map((l) => ({ language: l.language, toolchain: l.toolchain, available: l.available })) }
125
+ : langs;
126
+ }
127
+ if (!slim && PLATFORM_QUIRKS[id]) entry.quirks = PLATFORM_QUIRKS[id];
112
128
  return entry;
113
129
  });
114
- return { platforms };
130
+ // A single-platform query returns just that row (not wrapped in `platforms[]`).
131
+ return platform ? platforms[0] : { platforms };
115
132
  }
116
133
 
117
- /** op:'resolve' — resolved core paths for a platform (debugging aid). */
134
+ /** op:'resolve' — resolved core paths + the toolchain summary for a platform. */
118
135
  export function resolvePlatformCore({ platform }) {
119
136
  const r = resolveCore(platform);
120
137
  if (!r) throw new Error(`no core available for platform '${platform}'`);
121
- return r;
138
+ // Also surface the toolchain(s) — resolve used to report only the emulator
139
+ // core, so agents had to spelunk node_modules to learn the build path (v0.71.0
140
+ // fb #3). We do NOT hand out the WASM/.mjs artifact paths: those tools run ONLY
141
+ // inside romdev's `build` worker harness (virtual FS), so a node_modules path
142
+ // invites the wrong mental model (shimming them into an external Makefile —
143
+ // which does NOT work). The `note` states that plainly (fb #4/#5).
144
+ const toolchains = Object.values(TOOLCHAINS)
145
+ .filter((t) => t.platforms.includes(platform))
146
+ .map((t) => ({ id: t.id, displayName: t.displayName, tier: t.tier }));
147
+ return {
148
+ ...r,
149
+ toolchains,
150
+ toolchainNote:
151
+ "Build via the `build` tool (it compiles a source set into one ROM). The toolchain " +
152
+ "binaries are WASM, run ONLY inside romdev's build worker (virtual FS) — they are NOT " +
153
+ "host-callable and CANNOT back an external project's Makefile. For an existing decomp/" +
154
+ "romhack that needs its own legacy compiler (e.g. agbcc) + Makefile, build it on the host " +
155
+ "and use romdev to run/inspect/debug the resulting ROM.",
156
+ };
122
157
  }
123
158
 
124
159
  export function registerPlatformTools(server, z) {
@@ -142,14 +177,15 @@ export function registerPlatformTools(server, z) {
142
177
  "troubleshooting / upstream_sources; `platform:'romhacking'` + `name:'playbook'` for the RE decision tree). " +
143
178
  "Read MENTAL_MODEL before writing code, and the romhacking playbook before a hack.",
144
179
  {
145
- op: z.enum(["list", "capabilities", "resolve", "toolchains", "docs", "doc"]).describe("list=platforms; capabilities=per-platform op support matrix; resolve=core paths; toolchains; docs=a platform's doc names; doc=read one doc."),
146
- platform: z.string().optional().describe("op=resolve/docs/doc: platform id (e.g. nes, gb, genesis; 'romhacking' for the RE playbook)."),
180
+ op: z.enum(["list", "capabilities", "resolve", "toolchains", "docs", "doc"]).describe("list=platforms (pass `platform` to get just ONE, `slim:true` to drop the verbose notes); capabilities=per-platform op support matrix; resolve=core + toolchain artifact paths; toolchains; docs=a platform's doc names; doc=read one doc."),
181
+ platform: z.string().optional().describe("op=list/resolve/docs/doc/capabilities: platform id (e.g. nes, gb, genesis; 'romhacking' for the RE playbook). On op=list it filters to that ONE platform's row instead of the whole matrix (big token saver)."),
182
+ slim: z.boolean().optional().describe("op=list: drop the heavy per-language `note` + `quirks` prose; return just {platform, toolchains[], languages{defaultLanguage,…}}. Detail stays behind op:'doc' / op:'capabilities'."),
147
183
  id: z.string().optional().describe("op=toolchains: a specific toolchain's install status (e.g. 'cc65')."),
148
184
  name: z.string().optional().describe("op=doc: which doc — mental_model | troubleshooting | upstream_sources | playbook."),
149
185
  },
150
186
  safeTool(async (args) => {
151
187
  switch (args.op) {
152
- case "list": return jsonContent(listPlatformsCore());
188
+ case "list": return jsonContent(listPlatformsCore({ platform: args.platform, slim: args.slim }));
153
189
  case "capabilities": {
154
190
  if (args.platform) {
155
191
  const cap = capabilitiesFor(args.platform);
@@ -470,8 +470,16 @@ export function registerToolchainTools(server, z, sessionKey) {
470
470
  return jsonContent(payload);
471
471
  }
472
472
 
473
- async function runSourceImpl({ platform, language, source, sourcePath, sources, sourcesPaths, includes, binaryIncludes, binaryIncludePaths, includePaths, runtime, maxmod, rebuildSdk, crt0, crt0Path, codeLoc, dataLoc, linkerConfig, linkerConfigPath, inesHeader, path: projPath, frames = 60, holdInputs, screenshotPath, projectName }) {
473
+ async function runSourceImpl({ platform, language, source, sourcePath, sources, sourcesPaths, includes, binaryIncludes, binaryIncludePaths, includePaths, runtime, maxmod, rebuildSdk, crt0, crt0Path, codeLoc, dataLoc, linkerConfig, linkerConfigPath, inesHeader, options, defines, entry, path: projPath, frames = 60, holdInputs, screenshotPath, projectName }) {
474
474
  const { buildForPlatform } = await import("../../toolchains/index.js");
475
+ // Merge `defines` ({_VER:1}) into `options` (--define) just like the project/rom paths,
476
+ // so a project-dir RUN honors them too.
477
+ const mergedOptions = [
478
+ ...(Array.isArray(options) ? options : []),
479
+ ...(defines && typeof defines === "object"
480
+ ? Object.entries(defines).flatMap(([k, v]) => ["--define", `${k}=${v}`])
481
+ : []),
482
+ ];
475
483
  const resolved = resolveCore(platform);
476
484
  if (!resolved) throw new Error(`no core available for platform '${platform}'`);
477
485
 
@@ -481,7 +489,7 @@ export function registerToolchainTools(server, z, sessionKey) {
481
489
  // happy path; without it, output:'run' + path errored ("requires source").
482
490
  const noExplicitSources = source == null && sourcePath == null && sources == null && sourcesPaths == null;
483
491
  if (projPath && noExplicitSources) {
484
- const r = await readProjectDir(projPath, platform);
492
+ const r = await readProjectDir(projPath, platform, { entry });
485
493
  includes = { ...(includes ?? {}), ...r.includes };
486
494
  binaryIncludes = { ...(binaryIncludes ?? {}), ...r.binaryIncludes };
487
495
  if (r.crt0 != null) crt0 = r.crt0;
@@ -586,6 +594,7 @@ export function registerToolchainTools(server, z, sessionKey) {
586
594
  crt0: crt0Rel2,
587
595
  codeLoc,
588
596
  dataLoc,
597
+ options: mergedOptions.length ? mergedOptions : undefined,
589
598
  });
590
599
  logBuildResult("build:run", platform, build);
591
600
  if (!build.ok || !build.binary) {
@@ -703,7 +712,7 @@ export function registerToolchainTools(server, z, sessionKey) {
703
712
  "• output:'rom' (default) — assemble or compile `source` (single) / `sources` ({name:contents}) / `sourcePath` / `sourcesPaths`. Returns the ROM (path by default; `inline:true` for binaryBase64) + build log. **`binaryIncludes`/`binaryIncludePaths` (base64/path CHR-ROM, music blobs for `.incbin`) — WITHOUT them no game with external assets builds.** `includes`/`includePaths` for `.include`d text. `linkerConfig` (cc65; NES preset 'chr-ram-runtime' RECOMMENDED). `crt0`/`crt0Path`/`codeLoc`/`dataLoc` (SDCC). `runtime`/`maxmod`/`rebuildSdk` (GBA/Genesis SDK). **`lint:'strict'` fails the build (stage:'lint', no binary) if the pre-flight SDCC crash-pattern scan flags anything (e.g. the uint8 loop-bound trap); 'advisory' (default) just lists hits in issues[].** **`includeSymbols:true` returns the .map text inline on a PLAIN rom build — distinct from output:'romWithDebug' which writes .dbg/.map FILES.** Language is inferred from extension/content — usually OMIT `language`.\n" +
704
713
  "• output:'romWithDebug' — like 'rom' but also emits linker debug info for the `symbols` tool: cc65 → `.dbg`, SDCC → sdld `.map`, Genesis m68k → GNU ld map (find where a RAM var landed). DEFAULT writes ROM + debug file + log to disk (`outputPath` required unless `inline:true`). **`resolveSymbols:['grid','score']` folds those names' addresses ({resolvedSymbols:{grid:{address,hex,region?,ramOffset?}}}) straight into the result — the cheap way to a WRAM variable's address without loading the whole map (or round-tripping it through `symbols`).**\n" +
705
714
  "• output:'run' — BUILD + LOAD + RUN + SCREENSHOT in one round trip — the fastest iteration loop. Same build args; runs `frames` frames and returns the screenshot INLINE. `holdInputs` holds controller state; `screenshotPath` writes the PNG to disk instead; `projectName` titles the playtest window.\n" +
706
- "• output:'project' — build a project DIRECTORY (`path`) without re-passing the file manifest each call. Entry point is `main.c` (C/SGDK Genesis, GBA, cc65/SDCC C) OR `main.s`/`main.asm` (asm). Every `.c`/`.s`/`.asm` in the dir is a translation unit (linked together), every `.h`/`.inc` an include, and `.bin/.chr/.pcm/.brr/.vgm/...` become binaryIncludes (for `.incbin`). Iterate an on-disk project by re-calling with just `{path, platform}`. **This is the no-boilerplate path for an examples({op:'fork'}) dir: the per-platform recipe auto-supplies the crt0 + load address — GB/GBC default `gb_crt0.s` + `codeLoc:0x150` (don't hand-pass them!), MSX routes `msx_crt0.s` + `codeLoc:0x4010`, SMS/GG auto-inject their bundled crt0, NES applies the chr-ram-runtime preset. PREFER this over re-passing `crt0Path`/`codeLoc` to output:'rom' for a forked project.**",
715
+ "• output:'project' — build a project DIRECTORY (`path`) without re-passing the file manifest each call. Entry point auto-detects `main.c` (C/SGDK Genesis, GBA, cc65/SDCC C) OR `main.s`/`main.asm` (asm); pass `entry:'smw.asm'` to point at a differently-named top-level file (e.g. an existing disassembly). Every `.c`/`.s`/`.asm` in the dir is a translation unit (linked together), every `.h`/`.inc` an include, and `.bin/.chr/.pcm/.brr/.vgm/...` (recursively, including subdirectories) become binaryIncludes (for `.incbin`). `options` (e.g. asar `--define _VER=1`) and `defines` are honored here too. Iterate an on-disk project by re-calling with just `{path, platform}`. **This is the no-boilerplate path for an examples({op:'fork'}) dir: the per-platform recipe auto-supplies the crt0 + load address — GB/GBC default `gb_crt0.s` + `codeLoc:0x150` (don't hand-pass them!), MSX routes `msx_crt0.s` + `codeLoc:0x4010`, SMS/GG auto-inject their bundled crt0, NES applies the chr-ram-runtime preset. PREFER this over re-passing `crt0Path`/`codeLoc` to output:'rom' for a forked project.**",
707
716
  {
708
717
  output: z.enum(["rom", "romWithDebug", "run", "project"])
709
718
  .describe("rom=produce a ROM (default); romWithDebug=ROM + .dbg/.map debug files; run=build+load+run+screenshot; project=build a project directory."),
@@ -722,7 +731,8 @@ export function registerToolchainTools(server, z, sessionKey) {
722
731
  crt0Path: z.string().optional().describe("Path-based `crt0`. NOT read by output:'romWithDebug' — pass `crt0` there."),
723
732
  codeLoc: z.coerce.number().int().optional().describe("SDCC — _CODE load address (default $0000; GB/GBC bundled crt0 wants 0x150)."),
724
733
  dataLoc: z.coerce.number().int().optional().describe("SDCC — _DATA (WRAM) load address (default $C000 on Z80). NOT read by output:'romWithDebug'."),
725
- options: z.array(z.string()).optional().describe("output:'rom' extra toolchain CLI options."),
734
+ options: z.array(z.string()).optional().describe("Extra toolchain CLI options. Honored by output:'rom' AND output:'project' (e.g. asar `--define _VER=1`, `--fix-checksum=off`, `-wno…`)."),
735
+ defines: z.record(z.string(), z.union([z.string(), z.number()])).optional().describe("Assembler defines as a map ({_VER:1}) — convenience for asar's `--define NAME=VALUE`. Merged into `options` for output:'rom'/'project'. (Equivalent to passing `--define _VER=1` yourself.)"),
726
736
  linkerConfig: z.string().optional().describe("ld65 linker config (cc65). NES presets: 'chr-ram-runtime' (RECOMMENDED for homebrew C — full crt0 + iNES header + NMI w/ OAM DMA + `_shadow_oam` at $0200), 'chr-ram' (bare nmi:rti stub), 'chr-rom' (cc65-C with FIXED CHR-ROM art — segment split + CHARS segment; supply CHR via binaryIncludePaths into a CHARS source + the header via `inesHeader`). Or full .cfg contents. Preset NAMES only resolve on output:'rom'/'run'; output:'romWithDebug' takes raw .cfg contents only. **For rebuilding a commercial NROM game from its disassembly, prefer `inesHeader` over a raw .cfg.**"),
727
737
  linkerConfigPath: z.string().optional().describe("Path-based `linkerConfig`: absolute path to a .cfg file on disk (the server reads it — the cfg never enters your context; e.g. the multi-bank cfg a banked-NES disasm project ships). Ignored when `linkerConfig` is passed inline."),
728
738
  inesHeader: z.object({
@@ -746,6 +756,7 @@ export function registerToolchainTools(server, z, sessionKey) {
746
756
  projectName: z.string().optional().describe("output:'run' — playtest window title (no effect on the ROM)."),
747
757
  // project-only
748
758
  path: z.string().optional().describe("output:'project' — absolute path to the project directory."),
759
+ entry: z.string().optional().describe("output:'project' — name of the top-level source file when it isn't main.c/main.s/main.asm (e.g. 'smw.asm' for an existing disassembly). Project-relative or a bare filename. Default: auto-detect main.c / main.s / main.asm."),
749
760
  // shared output
750
761
  outputPath: z.string().optional().describe("output:'rom'/'romWithDebug'/'project' — absolute path to write the ROM (romWithDebug writes .dbg/.map/.log alongside; REQUIRED for romWithDebug unless inline:true). output:'rom' omitted → temp-file path returned (or inline:true for base64)."),
751
762
  inline: z.boolean().default(false).describe("output:'rom'/'romWithDebug' — return binaryBase64 (+ debug text for romWithDebug) in the response instead of writing to disk."),
@@ -905,6 +916,45 @@ export function projectBuildRecipe(platform, names) {
905
916
  return r;
906
917
  }
907
918
 
919
+ /**
920
+ * Recursively collect files in SUBDIRECTORIES of a project (top-level files are
921
+ * handled by the recipe). Returns {rel, abs} with `rel` POSIX-relative to root
922
+ * (e.g. "col/misc/x.pal"), so the asar/incbin mount resolves the same path the
923
+ * source references. Skips dot-dirs and common build/VCS dirs to avoid hauling
924
+ * in junk. Caps total files to keep a pathological tree from blowing up.
925
+ * @param {string} root
926
+ * @returns {Promise<Array<{rel:string, abs:string}>>}
927
+ */
928
+ async function walkSubdirAssets(root) {
929
+ /** @type {Array<{rel:string, abs:string}>} */
930
+ const out = [];
931
+ const SKIP_DIR = new Set([".git", ".svn", "node_modules", "out", "build", "obj", ".vscode"]);
932
+ const MAX = 5000;
933
+ async function walk(dir, rel) {
934
+ let ents;
935
+ try { ents = await readdir(dir, { withFileTypes: true }); } catch { return; }
936
+ for (const e of ents) {
937
+ if (out.length >= MAX) return;
938
+ const childRel = rel ? `${rel}/${e.name}` : e.name;
939
+ if (e.isDirectory()) {
940
+ if (e.name.startsWith(".") || SKIP_DIR.has(e.name)) continue;
941
+ await walk(path.join(dir, e.name), childRel);
942
+ } else if (e.isFile()) {
943
+ out.push({ rel: childRel, abs: path.join(dir, e.name) });
944
+ }
945
+ }
946
+ }
947
+ // Only descend into subdirectories (top-level handled by the flat loop/recipe).
948
+ let top;
949
+ try { top = await readdir(root, { withFileTypes: true }); } catch { return out; }
950
+ for (const e of top) {
951
+ if (e.isDirectory() && !e.name.startsWith(".") && !SKIP_DIR.has(e.name)) {
952
+ await walk(path.join(root, e.name), e.name);
953
+ }
954
+ }
955
+ return out;
956
+ }
957
+
908
958
  /**
909
959
  * Read a scaffolded project DIRECTORY into the build inputs, applying the
910
960
  * per-platform recipe (crt0 routing, linker preset, runtime, skip-list) and
@@ -913,17 +963,57 @@ export function projectBuildRecipe(platform, names) {
913
963
  * can never drift. Returns crt0 as RAW source text (callers assemble it).
914
964
  * @param {string} projPath
915
965
  * @param {string} platform
966
+ * @param {{entry?: string}} [opts] entry: name of the top-level source when it isn't main.*
916
967
  */
917
- export async function readProjectDir(projPath, platform) {
968
+ export async function readProjectDir(projPath, platform, opts = {}) {
918
969
  const entries = await readdir(projPath, { withFileTypes: true });
919
970
  const files = entries.filter((e) => e.isFile());
920
971
 
972
+ // Subdirectory assets — a real disassembly keeps palettes/gfx in nested dirs
973
+ // (e.g. col/misc/back_area.pal) and `.incbin`s them by relative path. The old
974
+ // flat readdir never saw them → "file not found". Walk recursively and add any
975
+ // binary asset (regardless of extension/double-extension) keyed by its path
976
+ // RELATIVE to the project root, so the asar/incbin mount resolves it.
977
+ const subAssets = await walkSubdirAssets(projPath);
978
+
979
+ // Entry override: an existing project whose top file isn't main.* (e.g.
980
+ // smw.asm) OR is nested (e.g. src/main.c — common for decomps/SDK projects).
981
+ // Accept a project-relative path or a bare name; it becomes the single entry
982
+ // source. Resolve against BOTH the top-level files AND the recursively staged
983
+ // subdir set, with POSIX slashes (`path.normalize` may emit `\` on Windows).
984
+ const entryName = opts.entry
985
+ ? path.normalize(opts.entry).replace(/\\/g, "/").replace(/^[./]+/, "")
986
+ : null;
987
+ // The resolved {rel, abs} for the entry when it's nested in a subdirectory.
988
+ let entrySubAsset = null;
989
+ if (entryName) {
990
+ const topMatch = files.some((f) => f.name === entryName);
991
+ entrySubAsset = subAssets.find((a) => a.rel === entryName) || null;
992
+ // Bare-filename fallback: `entry:'main.c'` matches `src/main.c` if unique.
993
+ if (!topMatch && !entrySubAsset && !entryName.includes("/")) {
994
+ const byBase = subAssets.filter((a) => a.rel.split("/").pop() === entryName);
995
+ if (byBase.length === 1) entrySubAsset = byBase[0];
996
+ }
997
+ if (!topMatch && !entrySubAsset) {
998
+ const nearby = subAssets
999
+ .filter((a) => a.rel.split("/").pop() === entryName.split("/").pop())
1000
+ .map((a) => a.rel).slice(0, 8);
1001
+ throw new Error(
1002
+ `entry '${entryName}' not found in ${projPath}. ` +
1003
+ (nearby.length
1004
+ ? `Did you mean: ${nearby.join(", ")}? (entry is project-relative — e.g. 'src/main.c'.)`
1005
+ : `Top-level: ${files.map((f) => f.name).join(", ") || "(none)"}. ` +
1006
+ `entry is project-relative — pass the path under the repo root (e.g. 'src/main.c').`)
1007
+ );
1008
+ }
1009
+ }
1010
+
921
1011
  const hasC = files.some((f) => f.name === "main.c");
922
1012
  const hasAsm = files.some((f) => f.name === "main.s" || f.name === "main.asm");
923
- if (!hasC && !hasAsm) {
1013
+ if (!entryName && !hasC && !hasAsm) {
924
1014
  throw new Error(
925
- `no entry point in ${projPath}: expected main.c (C/SGDK/GBA/cc65 project) or main.s / main.asm (asm project). ` +
926
- `Found: ${files.map((f) => f.name).join(", ") || "(empty)"}.`
1015
+ `no entry point in ${projPath}: expected main.c (C/SGDK/GBA/cc65 project) or main.s / main.asm (asm project), ` +
1016
+ `or pass entry:'<top-file>'. Found: ${files.map((f) => f.name).join(", ") || "(empty)"}.`
927
1017
  );
928
1018
  }
929
1019
 
@@ -933,6 +1023,15 @@ export async function readProjectDir(projPath, platform) {
933
1023
  // the dir build matches the hand-written build({output:'run'}) call.
934
1024
  const recipe = projectBuildRecipe(platform, files.map((f) => f.name));
935
1025
 
1026
+ // With a custom ASM entry, the entry is the ONE source; every OTHER top-level
1027
+ // .asm/.s/.c routes as an include (asar/wla resolve `.include`/`#include`
1028
+ // from the includes mount), matching the single-source asm model.
1029
+ if (entryName && /\.(asm|s)$/i.test(entryName)) {
1030
+ for (const f of files) {
1031
+ if (/\.(c|s|asm)$/i.test(f.name) && f.name !== entryName) recipe.includeAsC.add(f.name);
1032
+ }
1033
+ }
1034
+
936
1035
  /** @type {Record<string,string>} */ const sources = {};
937
1036
  /** @type {Record<string,string>} */ const includes = {};
938
1037
  /** @type {Record<string,string>} */ const binaryIncludes = {};
@@ -953,6 +1052,27 @@ export async function readProjectDir(projPath, platform) {
953
1052
  }
954
1053
  }
955
1054
 
1055
+ // Stage subdirectory assets (any extension) keyed by their path relative to the
1056
+ // project root, so `incbin "col/misc/X.pal"` / `.incbin "gfx/a.bin"` resolve in
1057
+ // the mount. Text-ish includes (.asm/.s/.h/.inc) found in subdirs go to includes
1058
+ // (so `incsrc "lib/foo.asm"` works); everything else is a binary asset.
1059
+ for (const a of subAssets) {
1060
+ if (recipe.skip.has(a.rel) || a.rel === entrySubAsset?.rel) continue;
1061
+ if (/\.(h|inc|asm|s)$/i.test(a.rel)) {
1062
+ includes[a.rel] = (await readFile(a.abs)).toString("utf-8");
1063
+ } else {
1064
+ binaryIncludes[a.rel] = (await readFile(a.abs)).toString("base64");
1065
+ }
1066
+ }
1067
+
1068
+ // A NESTED entry (e.g. src/main.c) is read here as the entry SOURCE — the
1069
+ // top-level loop above only sees root files, so a subdir entry would otherwise
1070
+ // never compile. Keyed by its relative path so #include/.include resolution
1071
+ // from the same subdir works.
1072
+ if (entrySubAsset) {
1073
+ sources[entrySubAsset.rel] = (await readFile(entrySubAsset.abs)).toString("utf-8");
1074
+ }
1075
+
956
1076
  // SMS/GG with no crt0 file in the dir → fall back to the bundled crt0,
957
1077
  // exactly like the output:'rom'/'run' handlers do. Without this the link
958
1078
  // silently uses SDCC's stock z80 crt0, which never calls main() (black
@@ -972,8 +1092,17 @@ export async function readProjectDir(projPath, platform) {
972
1092
  return { sources, includes, binaryIncludes, crt0, codeLoc: recipe.codeLoc, dataLoc: recipe.dataLoc, linkerConfig: recipe.linkerConfig, runtime, maxmod: recipe.maxmod };
973
1093
  }
974
1094
 
975
- export async function buildProjectCore({ path: projPath, platform, outputPath }) {
976
- const { sources, includes, binaryIncludes, crt0, codeLoc, dataLoc, linkerConfig, runtime, maxmod } = await readProjectDir(projPath, platform);
1095
+ export async function buildProjectCore({ path: projPath, platform, outputPath, options, defines, entry }) {
1096
+ const { sources, includes, binaryIncludes, crt0, codeLoc, dataLoc, linkerConfig, runtime, maxmod } = await readProjectDir(projPath, platform, { entry });
1097
+
1098
+ // Thread `options` (e.g. asar `--define _VER=1`, `--fix-checksum=off`) + the `defines`
1099
+ // convenience map through to the toolchain — project mode used to silently drop them.
1100
+ const mergedOptions = [
1101
+ ...(Array.isArray(options) ? options : []),
1102
+ ...(defines && typeof defines === "object"
1103
+ ? Object.entries(defines).flatMap(([k, v]) => ["--define", `${k}=${v}`])
1104
+ : []),
1105
+ ];
977
1106
 
978
1107
  // Linker preset: the recipe names it (e.g. NES 'chr-ram-runtime', which ships
979
1108
  // the OAM/CHARS segments + its own crt0). resolveLinkerConfig also returns any
@@ -1012,6 +1141,7 @@ export async function buildProjectCore({ path: projPath, platform, outputPath })
1012
1141
  crt0: crt0Rel,
1013
1142
  codeLoc,
1014
1143
  dataLoc,
1144
+ options: mergedOptions.length ? mergedOptions : undefined,
1015
1145
  });
1016
1146
  if (outputPath && result.binary) {
1017
1147
  await mkdir(path.dirname(outputPath), { recursive: true });
@@ -0,0 +1,87 @@
1
+ # Sega Dreamcast — mental model
2
+
3
+ The Dreamcast is a **3D machine**: a 200 MHz Hitachi SH-4 CPU (with an on-die FPU/
4
+ vector unit) + the PowerVR2 (CLX2) **tile-based deferred renderer** drawing into
5
+ 8 MB VRAM, plus the AICA sound system (an ARM7 CPU + DSP). romdev runs it on
6
+ **Flycast** built to WASM, presenting through the **native-gles / WebGL2 hardware
7
+ GPU**, with the **reios HLE BIOS** so you don't ship a real `dc_boot.bin`.
8
+
9
+ ## The one thing to know about rendering
10
+
11
+ Flycast is **GPU-first**: the PowerVR2 is a Tile Accelerator (TA) — homebrew normally
12
+ submits TA display lists (polygon/vertex lists) that the GPU rasterizes. romdev runs
13
+ Flycast `hwRender: true` (the real GPU via WebGL2). **BUT** the core is configured
14
+ with `flycast_emulate_framebuffer: enabled`, which gives a working **direct-framebuffer
15
+ scanout** path — a program that writes the DC framebuffer displays (verified: a
16
+ framebuffer-writing program captured ~727k pixels). So both routes work:
17
+
18
+ - **Direct framebuffer** (simplest for 2D / first pixels): write VRAM and let the
19
+ emulated-framebuffer path scan it out. The starting point for "just show something."
20
+ - **TA / GPU** (the real path for 3D): submit PowerVR2 TA lists. The native SDK,
21
+ **KallistiOS (KOS)**, wraps the TA and ships an OpenGL port — lean on KOS rather than
22
+ hand-rolling the TA command encoding.
23
+
24
+ Decide which your homebrew uses; the bundled helper lib targets the framebuffer path
25
+ first (like a software front end) and grows toward TA.
26
+
27
+ ## CPU / memory map (SH-4, 32-bit, little-endian)
28
+
29
+ | Region | Address | Notes |
30
+ |--------|---------|-------|
31
+ | System RAM | `0x8C00_0000`+ | 16 MB main RAM (KOS links code here) |
32
+ | VRAM | `0xA500_0000`+ | 8 MB texture/framebuffer RAM |
33
+ | Hardware regs | `0xA05F_xxxx` | PowerVR2 (TA/CORE), Holly, GD-ROM, AICA, Maple |
34
+ | AICA RAM | `0x00800000` (AICA bus) | ARM7 sound RAM |
35
+
36
+ Addresses are **little-endian**. SH-4 uses **PC-relative loads** for constants/
37
+ addresses (`mov.l @(disp,PC)`) heavily — relevant for RE base-address alignment.
38
+ `memory({op:'read', region:'system_ram'})` reads main RAM.
39
+
40
+ ## Booting — KOS ELF + reios HLE (no disc needed for the dev loop)
41
+
42
+ A real Dreamcast boots from a GD-ROM with an `IP.BIN` bootstrap (a CDI/CHD disc image).
43
+ For homebrew, that's heavy. romdev uses Flycast's **reios HLE BIOS** (`flycast_hle_bios:
44
+ enabled`), and KallistiOS produces a ready-to-boot **ELF** — Flycast loads the raw KOS
45
+ ELF directly, so you skip the disc-image dance. (Build a disc image only if a specific
46
+ title demands it.) `build({platform:'dreamcast'})` produces the loadable image.
47
+
48
+ ## Input
49
+
50
+ Homebrew reads controllers over the **Maple bus** (`0xA05F_6C00`+) — the DC's
51
+ peripheral protocol. KOS abstracts this (`maple_enum_*`, controller state). The host's
52
+ `setInput`/`run` holdInputs drive the emulated controller; the game polls Maple.
53
+
54
+ ## Sound
55
+
56
+ The **AICA** = an ARM7 CPU + DSP with its own RAM. The SH-4 uploads a sound program +
57
+ samples to AICA RAM and triggers channels. KOS ships an AICA sound driver
58
+ (`snd_stream`). `audioChips` lists `aica`.
59
+
60
+ ## MCP debug & inspection tooling — current state
61
+
62
+ - **`run` / `screenshot` / `frame({op:'verify'})`** — boots + presents + render-health.
63
+ ✅ (reios HLE; the framebuffer path captures pixels.)
64
+ - **`memory({op:'read', region:'system_ram'})`** — SH-4 main RAM. ✅
65
+ - **`disasm` / `decompile`** — SH-4 via rizin (`sh`) + Ghidra (SuperH4 SLEIGH). ✅
66
+ (Good decompile quality; SH-4 is a clean 32-bit RISC.)
67
+ - **`build`** — sh-elf-gcc → WASM toolchain. ✅
68
+ - **`cpu({op:'read'})`** — live SH-4 register file (`romdev_sh4_regs_get`: r0..r15, pc,
69
+ pr, gbr, vbr, sr, mac, fpul + decoded SR flags). ✅
70
+ - **`audioDebug({op:'inspect', chip:'aica'})`** — the AICA's 64 PCM/ADPCM channels
71
+ (key-on / volume / pitch / loop / format) + master volume (`romdev_aica_get`). ✅
72
+ - **`breakpoint` / `watch`** — not yet (need interpreter-step + memory-path hooks in
73
+ flycast; cpuState/audioDebug are plain reads and ARE wired).
74
+ - **`renderingContext`** is **N/A** — 3D TA machine, no 2D tile VDP to decode.
75
+
76
+ ## Build pipeline
77
+
78
+ `build({platform:'dreamcast'})` cross-compiles with an **sh-elf-gcc → WASM** toolchain
79
+ (adapted from KOS's `dc-chain`; SH-4 is single-endian little, so no be/el split). Link
80
+ against KallistiOS for libc + the TA/AICA/Maple drivers. `#include` the bundled
81
+ `dc.h` helper for the framebuffer front end + input/audio helpers.
82
+
83
+ ## What's NOT bundled / hardware limits
84
+
85
+ - No GD-ROM/CDDA streaming or VMU save emulation in the dev loop.
86
+ - `breakpoint`/`watch` not yet (need interpreter-step hooks; cpuState/audioDebug ARE wired).
87
+ - `renderingContext` is N/A (3D TA, no tile VDP).
@@ -0,0 +1,55 @@
1
+ # Sega Dreamcast — troubleshooting
2
+
3
+ Read `platform({op:'doc', platform:'dreamcast', name:'mental_model'})` first — DC runs
4
+ on Flycast (hardware GPU + reios HLE BIOS), with a working emulated-framebuffer path.
5
+
6
+ ## "ROM/ELF builds but won't boot"
7
+
8
+ 1. **reios HLE must be on.** Flycast only boots a raw homebrew ELF via the **reios HLE
9
+ BIOS** (`flycast_hle_bios: enabled`) — it parses the ELF + jumps. With it off,
10
+ Flycast wants a real `dc_boot.bin` you don't ship. romdev's DC core config already
11
+ enables reios; if you swapped the core/config, re-enable it.
12
+ 2. **You built a flat binary, not a KOS ELF.** Flycast loads the KallistiOS ELF
13
+ (linked at `0x8C00_0000`). Use `build({platform:'dreamcast'})` — it links against
14
+ KOS and emits the loadable image. A bare `.bin` with no ELF/entry won't boot.
15
+
16
+ ## "Boots but the screen is BLACK"
17
+
18
+ 1. **Nothing was drawn to the framebuffer OR submitted to the TA.** DC has two render
19
+ routes (see mental model). For the simple path, write the DC framebuffer (the
20
+ `flycast_emulate_framebuffer: enabled` path scans it out — a framebuffer-writing
21
+ program shows pixels). For 3D, submit PowerVR2 **TA** lists via KOS. A blank screen
22
+ = neither happened. Confirm with `frame({op:'verify'})` (nearlyBlank).
23
+ 2. **No display/render init.** The PowerVR2 needs its video mode + render target set
24
+ up (KOS `vid_set_mode` / `pvr_init`). Without it there's no scanout. Call the KOS
25
+ init (or the helper lib's init) before drawing.
26
+ 3. **You expected raw VRAM writes to a TA-only setup to show.** With the framebuffer-
27
+ emulation path enabled they do; if you disabled it and use pure TA, only submitted
28
+ TA geometry renders.
29
+
30
+ ## "Geometry/3D is wrong or missing (TA path)"
31
+
32
+ PowerVR2 is a **tile-based DEFERRED** renderer — opaque, punch-through, and translucent
33
+ lists are submitted + sorted per tile. Submitting polys to the wrong list type, or not
34
+ finalizing the list, drops them. Use KOS's `pvr_*` list API (it orders this correctly)
35
+ rather than hand-encoding TA commands.
36
+
37
+ ## "disasm/decompile returns junk addresses on a multi-function program"
38
+
39
+ SH-4 decompiles well, but it uses **PC-relative loads** (`mov.l @(disp,PC)`) heavily,
40
+ so the analysis buffer's flat offsets must line up with the binary's load VA or
41
+ constant/address resolution breaks. Build a **multi-function** test program to verify
42
+ `functions` returns real VAs that round-trip — a single-instruction smoke test hides
43
+ base-address misalignment. (Same class of trap as the PS1 rebase issue.)
44
+
45
+ ## "breakpoint / watch return N/A"
46
+
47
+ `cpu({op:'read'})` and `audioDebug({op:'inspect', chip:'aica'})` ARE wired on DC (the
48
+ flycast core exports `romdev_sh4_regs_get` + `romdev_aica_get`). `breakpoint`/`watch`
49
+ are not yet — those need interpreter-step + memory-path hooks in flycast (cpuState/
50
+ audioDebug are plain register reads). Use `cpu`/`audioDebug` + `memory` +
51
+ `disasm`/`decompile` meanwhile.
52
+
53
+ ## "renderingContext returns N/A"
54
+
55
+ Correct — DC is a 3D TA machine with no 2D tile/sprite VDP for that op. Not a bug.
@@ -0,0 +1,57 @@
1
+ # Sega Dreamcast — source you can read
2
+
3
+ Trust hierarchy (try in order before filing a feedback round):
4
+
5
+ 1. **Bundled examples** (`examples/dreamcast/{hello,shmup,platformer,puzzle,racing,sports}/main.c`)
6
+ — verified building + rendering on the GPU (full 480-line frame). Start here.
7
+ 2. **Bundled helper lib source** (`src/toolchains/sh-c/lib/dc.h`) — the PowerVR2
8
+ framebuffer bring-up + drawing + input. Read this when something doesn't render or input
9
+ doesn't read: it shows `dc_video_init` (FB_R_CTRL/SIZE/SOF1 + SPG, **480i interlace** —
10
+ 240p only shows the top 240 lines), `dc_clear`/`dc_rect`, and the Maple-DMA `dc_pad()`.
11
+ 3. **The core source** (NOT bundled — fetch on demand):
12
+
13
+ | What | Upstream |
14
+ |---|---|
15
+ | Flycast (libretro core, PowerVR2 + reios HLE BIOS) | https://github.com/flyinghead/flycast |
16
+ | SH-4 / PowerVR2 / AICA / Maple internals | (in the flycast tree: `core/hw/sh4/`, `core/hw/pvr/`, `core/hw/aica/`, `core/hw/maple/`) |
17
+
18
+ The cpuState/audioDebug exports we patch in (`romdev_sh4_regs_get`, `romdev_aica_get`)
19
+ live in `scripts/patches/romdev-snippets/flycast-debug.c`.
20
+
21
+ 4. **The toolchain** — a from-scratch `sh-elf-gcc` cross-compiler (little-endian SH-4,
22
+ m4-single-only FP) built to WASM (`scripts/build-sh-toolchain.sh` +
23
+ `build-sh-wasm-tools.sh`). NOTE: cc1 defaults to **-O1** (the sh-elf cc1.wasm has an
24
+ -O2-only crash on common control flow):
25
+
26
+ | Component | Upstream |
27
+ |---|---|
28
+ | binutils | https://ftp.gnu.org/gnu/binutils/ |
29
+ | gcc | https://ftp.gnu.org/gnu/gcc/ |
30
+ | newlib | https://sourceware.org/pub/newlib/ |
31
+
32
+ 5. **Reverse engineering** — Rizin + Ghidra SH-4 (SuperH4 SLEIGH):
33
+
34
+ | What | Upstream |
35
+ |---|---|
36
+ | Rizin | https://github.com/rizinorg/rizin |
37
+ | rz-ghidra | https://github.com/rizinorg/rz-ghidra |
38
+
39
+ ## Dreamcast hardware docs
40
+
41
+ - **KallistiOS (KOS)** — the canonical open DC SDK; read its PowerVR2 TA, AICA, and Maple
42
+ drivers for the standard register sequences: https://github.com/KallistiOS/KallistiOS
43
+ - **Mc Spankled / DCEmulation docs + the Sega Dreamcast Hardware Specification** (PVR2/Holly
44
+ register maps): cross-check against flycast's `core/hw/pvr/pvr_regs.h` enum.
45
+ - **SH-4 ISA / SH7750 manual** (Renesas) for the CPU.
46
+
47
+ ## When to use what
48
+
49
+ - "Boots but the screen is BLACK / only the top half draws" → MENTAL_MODEL +
50
+ TROUBLESHOOTING (it's the framebuffer path + the 480i-interlace requirement).
51
+ - "How does the helper set up the PowerVR2 framebuffer?" → `dc.h` `dc_video_init` +
52
+ flycast `core/hw/pvr/pvr_regs.h` (SPG_CONTROL interlace bit).
53
+ - "dc_pad doesn't reflect button presses" → `dc.h` `dc_pad()` (Maple Get-Condition DMA) +
54
+ flycast `core/hw/maple/maple_devs.cpp` (the controller response framing). Reads the
55
+ resting state; press mapping is a known follow-up.
56
+ - "Which AICA register holds channel key-on/volume?" → `dc-aica-state.js` decode +
57
+ flycast `core/hw/aica/`.
@@ -248,6 +248,11 @@ Loadable via mGBA (`loadMedia`).
248
248
 
249
249
  ## What's NOT bundled
250
250
 
251
+ - **`agbcc` (the legacy GBA compiler).** romdev's GBA C path is **modern
252
+ `arm-none-eabi-gcc` 14.2.0** only. The byte-exact decompilations + romhacks
253
+ (pokeruby / pokeemerald / pokefirered, etc.) build with agbcc + a custom
254
+ `ld_script.txt`, so romdev **cannot reproduce a matching retail ROM** for those.
255
+ That's a hard limit, not a missing feature — see "romdev's build model" below.
251
256
  - **libgba's `console.c`** (iprintf-style stdio output). Pulls in
252
257
  devkitPro's libsysbase header chain — not yet ported. See
253
258
  TROUBLESHOOTING.md for the trade-off rationale and workarounds.
@@ -256,7 +261,23 @@ Loadable via mGBA (`loadMedia`).
256
261
  - **devkitARM's `bin2s`** (binary → assembly converter for asset
257
262
  pipelines). Not bundled; ship binary assets as C arrays for now.
258
263
 
259
- Everything else from a stock devkitARM install works.
264
+ Everything else from a stock devkitARM install (homebrew-style) works.
265
+
266
+ ## romdev's build model (read this before driving an existing project)
267
+
268
+ `build` is a **single-shot "compile these sources → one ROM" tool**, NOT an arbitrary
269
+ build-system backend. The toolchain binaries are **WASM, run only inside romdev's build
270
+ worker (virtual FS)** — they are **not host-callable** and **cannot back an external
271
+ project's `Makefile`** as `$(TOOLCHAIN)/bin`. (The `.mjs` wrappers under `node_modules`
272
+ export a worker factory, not a CLI `main` — invoking one directly exits 0 and writes
273
+ nothing.)
274
+
275
+ So for an **existing decomp/romhack** (agbcc-era or any project with its own Makefile):
276
+ build it **on the host** with its own toolchain, then point romdev at the resulting
277
+ `.gba` for the run / inspect / debug / decompile loop. Confirmed host recipe for the
278
+ Pokémon Gen-III decomps: `brew install arm-none-eabi-gcc arm-none-eabi-binutils`, clone
279
+ `pret/agbcc` → `./build.sh` → `./install.sh <repo>`, then `make <target>` (yields a
280
+ byte-matching ROM). romdev's value here is everything AFTER the build, not the build.
260
281
 
261
282
  ## Horizontal scrolling (for side-scrollers)
262
283