fcdev 0.0.0-stage → 0.2.0
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.
- package/LICENSE +201 -0
- package/README.md +82 -2
- package/bin/fcdev.mjs +17 -0
- package/package.json +38 -4
- package/src/api/header-parser.mjs +78 -0
- package/src/api/index.mjs +128 -0
- package/src/build/bank-link-config.mjs +74 -0
- package/src/build/build-plan.mjs +92 -0
- package/src/build/dependency-file.mjs +24 -0
- package/src/build/disk-builder.mjs +36 -0
- package/src/build/index.mjs +32 -0
- package/src/build/program-builder.mjs +212 -0
- package/src/build/source-collector.mjs +54 -0
- package/src/build/tool-arguments.mjs +83 -0
- package/src/cli/args-parser.mjs +84 -0
- package/src/cli/commands/api-command.mjs +62 -0
- package/src/cli/commands/build-command.mjs +34 -0
- package/src/cli/commands/debug-command.mjs +70 -0
- package/src/cli/commands/disasm-command.mjs +65 -0
- package/src/cli/commands/disk-command.mjs +34 -0
- package/src/cli/commands/info-command.mjs +33 -0
- package/src/cli/commands/project-info.mjs +57 -0
- package/src/cli/commands/run-command.mjs +68 -0
- package/src/cli/commands/sync-command.mjs +85 -0
- package/src/cli/index.mjs +103 -0
- package/src/config/config-loader.mjs +132 -0
- package/src/config/index.mjs +9 -0
- package/src/disasm/analysis.mjs +97 -0
- package/src/disasm/cdl.mjs +35 -0
- package/src/disasm/disassembler.mjs +114 -0
- package/src/disasm/index.mjs +16 -0
- package/src/disasm/info-writer.mjs +72 -0
- package/src/disasm/loaders.mjs +69 -0
- package/src/disasm/opcode-table.mjs +268 -0
- package/src/disasm/roundtrip.mjs +69 -0
- package/src/disasm/symbols.mjs +30 -0
- package/src/disasm/trace.mjs +25 -0
- package/src/emulator/debug-client.mjs +125 -0
- package/src/emulator/emulator-launcher.mjs +80 -0
- package/src/emulator/emulator-locator.mjs +62 -0
- package/src/emulator/index.mjs +11 -0
- package/src/packagers/fat-floppy.mjs +22 -0
- package/src/packagers/fat12-image.mjs +245 -0
- package/src/packagers/flat-binary.mjs +53 -0
- package/src/packagers/index.mjs +54 -0
- package/src/packagers/ines.mjs +38 -0
- package/src/shared/errors.mjs +25 -0
- package/src/shared/fs-utils.mjs +66 -0
- package/src/shared/index.mjs +11 -0
- package/src/shared/process.mjs +50 -0
- package/src/targets/index.mjs +17 -0
- package/src/targets/name-rules.mjs +33 -0
- package/src/targets/target-registry.mjs +225 -0
- package/src/toolchain/index.mjs +16 -0
- package/src/toolchain/toolchain-locator.mjs +104 -0
- package/targets/nes/asminc/fcnes.inc +16 -0
- package/targets/nes/chr/ascii-8x8.chr +0 -0
- package/targets/nes/chr/font8x8_basic.h +152 -0
- package/targets/nes/include/fcnes.h +45 -0
- package/targets/nes/lib/fcnes.lib +0 -0
- package/targets/nes/lib/nes-font.o +0 -0
- package/targets/nes/lib-build.json +9 -0
- package/targets/nes/src/fcnes.s +91 -0
- package/targets/nes/src/nes-font.s +12 -0
- package/targets/nes/target.json +27 -0
- package/targets/sb2000/api-reference.md +3295 -0
- package/targets/sb2000/api-unwrapped.json +36 -0
- package/targets/sb2000/asminc/sb2000.inc +418 -0
- package/targets/sb2000/include/sb2000-bank.h +74 -0
- package/targets/sb2000/include/sb2000-clock.h +68 -0
- package/targets/sb2000/include/sb2000-disk.h +141 -0
- package/targets/sb2000/include/sb2000-display.h +352 -0
- package/targets/sb2000/include/sb2000-file.h +273 -0
- package/targets/sb2000/include/sb2000-ime.h +158 -0
- package/targets/sb2000/include/sb2000-input.h +192 -0
- package/targets/sb2000/include/sb2000-math.h +228 -0
- package/targets/sb2000/include/sb2000-memory.h +115 -0
- package/targets/sb2000/include/sb2000-printer.h +116 -0
- package/targets/sb2000/include/sb2000-sound.h +134 -0
- package/targets/sb2000/include/sb2000-symbols.h +776 -0
- package/targets/sb2000/include/sb2000-system.h +219 -0
- package/targets/sb2000/include/sb2000-text.h +99 -0
- package/targets/sb2000/include/sb2000.h +45 -0
- package/targets/sb2000/lib/crt0.o +0 -0
- package/targets/sb2000/lib/sb2000.lib +0 -0
- package/targets/sb2000/lib-build.json +8 -0
- package/targets/sb2000/link-banked.cfg +42 -0
- package/targets/sb2000/link.cfg +35 -0
- package/targets/sb2000/src/crt0.s +122 -0
- package/targets/sb2000/src/lib/bank/sb-bank-current.c +18 -0
- package/targets/sb2000/src/lib/bank/sb-bank-select.c +21 -0
- package/targets/sb2000/src/lib/bank/sbi-bank-trampoline.s +46 -0
- package/targets/sb2000/src/lib/clock/sb-clock-get-date.c +17 -0
- package/targets/sb2000/src/lib/clock/sb-clock-get-time.c +17 -0
- package/targets/sb2000/src/lib/clock/sb-clock-set-date.c +21 -0
- package/targets/sb2000/src/lib/clock/sb-clock-set-time.c +17 -0
- package/targets/sb2000/src/lib/clock/sbi-date-from-fat.c +20 -0
- package/targets/sb2000/src/lib/clock/sbi-date-to-fat.c +24 -0
- package/targets/sb2000/src/lib/clock/sbi-time-from-fat.c +20 -0
- package/targets/sb2000/src/lib/clock/sbi-time-to-fat.c +19 -0
- package/targets/sb2000/src/lib/disk/sb-disk-format-track.c +18 -0
- package/targets/sb2000/src/lib/disk/sb-disk-free-bytes.c +38 -0
- package/targets/sb2000/src/lib/disk/sb-disk-init.c +16 -0
- package/targets/sb2000/src/lib/disk/sb-disk-read-sector.c +24 -0
- package/targets/sb2000/src/lib/disk/sb-disk-read-sectors.c +17 -0
- package/targets/sb2000/src/lib/disk/sb-disk-scan-track.c +27 -0
- package/targets/sb2000/src/lib/disk/sb-disk-start.c +16 -0
- package/targets/sb2000/src/lib/disk/sb-disk-stop.c +16 -0
- package/targets/sb2000/src/lib/disk/sb-disk-write-sector.c +19 -0
- package/targets/sb2000/src/lib/disk/sb-disk-write-sectors.c +17 -0
- package/targets/sb2000/src/lib/disk/sbi-disk-batch.c +23 -0
- package/targets/sb2000/src/lib/disk/sbi-disk-call.c +17 -0
- package/targets/sb2000/src/lib/disk/sbi-disk-set-position.c +18 -0
- package/targets/sb2000/src/lib/display/sb-cell-address.c +20 -0
- package/targets/sb2000/src/lib/display/sb-cells-copy.c +27 -0
- package/targets/sb2000/src/lib/display/sb-cells-fill.c +25 -0
- package/targets/sb2000/src/lib/display/sb-cells-rect.c +20 -0
- package/targets/sb2000/src/lib/display/sb-display-mode.c +16 -0
- package/targets/sb2000/src/lib/display/sb-glyph-address.c +28 -0
- package/targets/sb2000/src/lib/display/sb-image-fill-rect.c +26 -0
- package/targets/sb2000/src/lib/display/sb-image-load-raw.c +22 -0
- package/targets/sb2000/src/lib/display/sb-image-show-full.c +20 -0
- package/targets/sb2000/src/lib/display/sb-image-show.c +25 -0
- package/targets/sb2000/src/lib/display/sb-image-unpack-map.c +28 -0
- package/targets/sb2000/src/lib/display/sb-image-unpack-tiles.c +20 -0
- package/targets/sb2000/src/lib/display/sb-image-unpack-to-memory.c +22 -0
- package/targets/sb2000/src/lib/display/sb-palette-write.c +20 -0
- package/targets/sb2000/src/lib/display/sb-screen-clear.c +19 -0
- package/targets/sb2000/src/lib/display/sb-screen-flush.c +16 -0
- package/targets/sb2000/src/lib/display/sb-text-draw.c +22 -0
- package/targets/sb2000/src/lib/display/sb-text-field-ascii.c +25 -0
- package/targets/sb2000/src/lib/display/sb-text-field.c +25 -0
- package/targets/sb2000/src/lib/display/sb-text-to-memory.c +27 -0
- package/targets/sb2000/src/lib/display/sb-vram-bank.c +17 -0
- package/targets/sb2000/src/lib/display/sb-vram-fill.c +21 -0
- package/targets/sb2000/src/lib/display/sb-vram-load.c +21 -0
- package/targets/sb2000/src/lib/display/sb-window-compact.c +16 -0
- package/targets/sb2000/src/lib/display/sb-window-init.c +16 -0
- package/targets/sb2000/src/lib/display/sb-window-restore.c +17 -0
- package/targets/sb2000/src/lib/display/sb-window-save.c +21 -0
- package/targets/sb2000/src/lib/file/sb-dir-change.c +21 -0
- package/targets/sb2000/src/lib/file/sb-dir-current.c +27 -0
- package/targets/sb2000/src/lib/file/sb-dir-make.c +21 -0
- package/targets/sb2000/src/lib/file/sb-dir-open.c +61 -0
- package/targets/sb2000/src/lib/file/sb-dir-read.c +87 -0
- package/targets/sb2000/src/lib/file/sb-dir-remove.c +21 -0
- package/targets/sb2000/src/lib/file/sb-file-close.c +21 -0
- package/targets/sb2000/src/lib/file/sb-file-delete.c +21 -0
- package/targets/sb2000/src/lib/file/sb-file-get-attr.c +25 -0
- package/targets/sb2000/src/lib/file/sb-file-get-date.c +25 -0
- package/targets/sb2000/src/lib/file/sb-file-get-time.c +25 -0
- package/targets/sb2000/src/lib/file/sb-file-load.c +50 -0
- package/targets/sb2000/src/lib/file/sb-file-open.c +26 -0
- package/targets/sb2000/src/lib/file/sb-file-read.c +52 -0
- package/targets/sb2000/src/lib/file/sb-file-rename.c +30 -0
- package/targets/sb2000/src/lib/file/sb-file-save.c +31 -0
- package/targets/sb2000/src/lib/file/sb-file-set-attr.c +22 -0
- package/targets/sb2000/src/lib/file/sb-file-set-date.c +25 -0
- package/targets/sb2000/src/lib/file/sb-file-set-time.c +22 -0
- package/targets/sb2000/src/lib/file/sb-file-size.c +27 -0
- package/targets/sb2000/src/lib/file/sb-file-write-all.c +32 -0
- package/targets/sb2000/src/lib/file/sb-file-write.c +60 -0
- package/targets/sb2000/src/lib/file/sbi-dir-state.c +15 -0
- package/targets/sb2000/src/lib/file/sbi-dos-call.c +17 -0
- package/targets/sb2000/src/lib/file/sbi-load-sector.c +18 -0
- package/targets/sb2000/src/lib/file/sbi-select-handle.c +20 -0
- package/targets/sb2000/src/lib/file/sbi-set-path.c +26 -0
- package/targets/sb2000/src/lib/ime/sb-ime-get-method.c +17 -0
- package/targets/sb2000/src/lib/ime/sb-ime-get-state.c +18 -0
- package/targets/sb2000/src/lib/ime/sb-ime-hex.c +20 -0
- package/targets/sb2000/src/lib/ime/sb-ime-init.c +26 -0
- package/targets/sb2000/src/lib/ime/sb-ime-key-wait.c +19 -0
- package/targets/sb2000/src/lib/ime/sb-ime-key.c +19 -0
- package/targets/sb2000/src/lib/ime/sb-ime-parse.c +23 -0
- package/targets/sb2000/src/lib/ime/sb-ime-resume.c +16 -0
- package/targets/sb2000/src/lib/ime/sb-ime-select.c +19 -0
- package/targets/sb2000/src/lib/ime/sb-ime-set-state.c +18 -0
- package/targets/sb2000/src/lib/ime/sb-ime-set-width.c +16 -0
- package/targets/sb2000/src/lib/ime/sb-ime-suspend.c +16 -0
- package/targets/sb2000/src/lib/input/sb-break-disable.c +16 -0
- package/targets/sb2000/src/lib/input/sb-break-enable.c +16 -0
- package/targets/sb2000/src/lib/input/sb-key-count.c +17 -0
- package/targets/sb2000/src/lib/input/sb-key-flush.c +16 -0
- package/targets/sb2000/src/lib/input/sb-key-init.c +16 -0
- package/targets/sb2000/src/lib/input/sb-key-modifiers.c +16 -0
- package/targets/sb2000/src/lib/input/sb-key-poll.c +17 -0
- package/targets/sb2000/src/lib/input/sb-key-wait.c +17 -0
- package/targets/sb2000/src/lib/input/sb-keyboard-reset.c +17 -0
- package/targets/sb2000/src/lib/input/sb-mouse-buttons.c +21 -0
- package/targets/sb2000/src/lib/input/sb-mouse-position.c +18 -0
- package/targets/sb2000/src/lib/input/sb-mouse-reset.c +17 -0
- package/targets/sb2000/src/lib/input/sb-mouse-set-double-click.c +17 -0
- package/targets/sb2000/src/lib/input/sb-mouse-set-limits.c +26 -0
- package/targets/sb2000/src/lib/input/sb-mouse-set-position.c +18 -0
- package/targets/sb2000/src/lib/input/sb-pad-init.c +17 -0
- package/targets/sb2000/src/lib/input/sb-pad-poll.c +17 -0
- package/targets/sb2000/src/lib/input/sb-pad-state.c +16 -0
- package/targets/sb2000/src/lib/input/sb-pad-wait.c +17 -0
- package/targets/sb2000/src/lib/math/sb-float-add.c +19 -0
- package/targets/sb2000/src/lib/math/sb-float-div.c +19 -0
- package/targets/sb2000/src/lib/math/sb-float-from-text.c +18 -0
- package/targets/sb2000/src/lib/math/sb-float-mul.c +19 -0
- package/targets/sb2000/src/lib/math/sb-float-sub.c +19 -0
- package/targets/sb2000/src/lib/math/sb-float-to-text.c +19 -0
- package/targets/sb2000/src/lib/math/sb-int24-add.c +21 -0
- package/targets/sb2000/src/lib/math/sb-int24-div.c +26 -0
- package/targets/sb2000/src/lib/math/sb-int24-mul.c +20 -0
- package/targets/sb2000/src/lib/math/sb-int24-sub.c +21 -0
- package/targets/sb2000/src/lib/math/sb-int40-from-text.c +19 -0
- package/targets/sb2000/src/lib/math/sb-int40-to-text.c +27 -0
- package/targets/sb2000/src/lib/math/sb-line-step.c +21 -0
- package/targets/sb2000/src/lib/math/sb-math-div16.c +27 -0
- package/targets/sb2000/src/lib/math/sb-math-time-text.c +21 -0
- package/targets/sb2000/src/lib/math/sb-random-next.c +20 -0
- package/targets/sb2000/src/lib/math/sb-random-seed.c +19 -0
- package/targets/sb2000/src/lib/math/sb-random-warm-up.c +16 -0
- package/targets/sb2000/src/lib/math/sbi-peek.c +20 -0
- package/targets/sb2000/src/lib/math/sbi-poke.c +20 -0
- package/targets/sb2000/src/lib/memory/sb-address-to-far.c +20 -0
- package/targets/sb2000/src/lib/memory/sb-far-read-byte.c +17 -0
- package/targets/sb2000/src/lib/memory/sb-far-read.c +36 -0
- package/targets/sb2000/src/lib/memory/sb-far-write-byte.c +17 -0
- package/targets/sb2000/src/lib/memory/sb-far-write.c +36 -0
- package/targets/sb2000/src/lib/memory/sb-page-alloc.c +19 -0
- package/targets/sb2000/src/lib/memory/sb-page-free.c +19 -0
- package/targets/sb2000/src/lib/memory/sb-page-largest-free.c +17 -0
- package/targets/sb2000/src/lib/memory/sb-window-map-group.c +17 -0
- package/targets/sb2000/src/lib/memory/sb-window-map.c +18 -0
- package/targets/sb2000/src/lib/memory/sbi-far-copy-in.s +17 -0
- package/targets/sb2000/src/lib/memory/sbi-far-copy-out.s +17 -0
- package/targets/sb2000/src/lib/memory/sbi-far-read-byte.s +19 -0
- package/targets/sb2000/src/lib/memory/sbi-far-write-byte.s +17 -0
- package/targets/sb2000/src/lib/printer/sb-printer-carriage-return.c +16 -0
- package/targets/sb2000/src/lib/printer/sb-printer-feed.c +17 -0
- package/targets/sb2000/src/lib/printer/sb-printer-graphics-mode.c +16 -0
- package/targets/sb2000/src/lib/printer/sb-printer-home.c +16 -0
- package/targets/sb2000/src/lib/printer/sb-printer-init-port.c +16 -0
- package/targets/sb2000/src/lib/printer/sb-printer-page-left.c +16 -0
- package/targets/sb2000/src/lib/printer/sb-printer-page-length.c +17 -0
- package/targets/sb2000/src/lib/printer/sb-printer-position.c +18 -0
- package/targets/sb2000/src/lib/printer/sb-printer-reset.c +16 -0
- package/targets/sb2000/src/lib/printer/sb-printer-send-byte.c +18 -0
- package/targets/sb2000/src/lib/printer/sb-printer-text.c +29 -0
- package/targets/sb2000/src/lib/sb-internal.h +111 -0
- package/targets/sb2000/src/lib/sound/sb-sound-effect-from.c +19 -0
- package/targets/sb2000/src/lib/sound/sb-sound-play-from.c +18 -0
- package/targets/sb2000/src/lib/sound/sb-sound-play-wav-from.c +18 -0
- package/targets/sb2000/src/lib/sound/sb-sound-play-wav.c +16 -0
- package/targets/sb2000/src/lib/sound/sb-sound-repeat-wav-from.c +19 -0
- package/targets/sb2000/src/lib/sound/sb-sound-status.c +17 -0
- package/targets/sb2000/src/lib/sound/sb-sound-stop.c +16 -0
- package/targets/sb2000/src/lib/sound/sb-sound-tempo.c +17 -0
- package/targets/sb2000/src/lib/sound/sb-sound-transpose.c +17 -0
- package/targets/sb2000/src/lib/sound/sb-speech-off.c +16 -0
- package/targets/sb2000/src/lib/sound/sb-speech-send-entry-from.c +21 -0
- package/targets/sb2000/src/lib/sound/sb-speech-send-from.c +19 -0
- package/targets/sb2000/src/lib/sound/sb-speech-send.c +16 -0
- package/targets/sb2000/src/lib/system/sb-call-banked.c +19 -0
- package/targets/sb2000/src/lib/system/sb-context-restore.c +16 -0
- package/targets/sb2000/src/lib/system/sb-context-save.c +19 -0
- package/targets/sb2000/src/lib/system/sb-display-off.c +16 -0
- package/targets/sb2000/src/lib/system/sb-display-on.c +16 -0
- package/targets/sb2000/src/lib/system/sb-display-option.c +18 -0
- package/targets/sb2000/src/lib/system/sb-exec.c +22 -0
- package/targets/sb2000/src/lib/system/sb-irq-enable.c +19 -0
- package/targets/sb2000/src/lib/system/sb-irq-hook-reset.c +20 -0
- package/targets/sb2000/src/lib/system/sb-irq-hook-set.c +39 -0
- package/targets/sb2000/src/lib/system/sb-irq-mask.c +19 -0
- package/targets/sb2000/src/lib/system/sb-jump-banked.s +25 -0
- package/targets/sb2000/src/lib/system/sb-nmi-disable.c +16 -0
- package/targets/sb2000/src/lib/system/sb-nmi-enable.c +16 -0
- package/targets/sb2000/src/lib/system/sb-nmi-hook-reset.c +20 -0
- package/targets/sb2000/src/lib/system/sb-nmi-hook-set.c +33 -0
- package/targets/sb2000/src/lib/system/sb-nmi-service.c +19 -0
- package/targets/sb2000/src/lib/system/sb-stack-pointer.s +18 -0
- package/targets/sb2000/src/lib/system/sb-system-init.c +16 -0
- package/targets/sb2000/src/lib/system/sb-system-state.c +20 -0
- package/targets/sb2000/src/lib/system/sb-wait-frames.s +19 -0
- package/targets/sb2000/src/lib/system/sbi-exec-run.s +43 -0
- package/targets/sb2000/src/lib/system/sbi-interrupts-off.s +19 -0
- package/targets/sb2000/src/lib/system/sbi-interrupts-restore.s +17 -0
- package/targets/sb2000/src/lib/text/sb-beep.c +16 -0
- package/targets/sb2000/src/lib/text/sb-cursor-column.c +16 -0
- package/targets/sb2000/src/lib/text/sb-cursor-left.c +16 -0
- package/targets/sb2000/src/lib/text/sb-cursor-right.c +16 -0
- package/targets/sb2000/src/lib/text/sb-cursor-row.c +16 -0
- package/targets/sb2000/src/lib/text/sb-cursor-set-column.c +18 -0
- package/targets/sb2000/src/lib/text/sb-cursor-up.c +16 -0
- package/targets/sb2000/src/lib/text/sb-print-hex.c +22 -0
- package/targets/sb2000/src/lib/text/sb-print-uint.c +25 -0
- package/targets/sb2000/src/lib/text/sb-print.c +35 -0
- package/targets/sb2000/src/lib/text/sb-putc.c +26 -0
- package/targets/sb2000/src/sb2000.s +64 -0
- package/targets/sb2000/target.json +42 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:api-command.mjs
|
|
3
|
+
// 说明:api 命令:查询目标的 C 接口文档(函数、模块、系统调用号)。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-11
|
|
7
|
+
// 备注:文档来自目标头文件里的文档注释,所以随库更新,不另存一份。没有查询词时列出全部模块;--json 输出机器可读格式。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import { formatFunction, formatSummary, groupByModule, loadTargetApi, queryApi } from '../../api/index.mjs';
|
|
10
|
+
import { loadProjectConfig } from '../../config/index.mjs';
|
|
11
|
+
import { FcdevError } from '../../shared/index.mjs';
|
|
12
|
+
import { loadTarget } from '../../targets/index.mjs';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 执行 api 命令。
|
|
16
|
+
* @param {{positionals: string[], options: {target?: string, json: boolean}, context: import('../index.mjs').CliContext}} input 参数、选项与运行环境
|
|
17
|
+
* @returns {Promise<number>} 退出码
|
|
18
|
+
*/
|
|
19
|
+
export async function runApiCommand({ positionals, options, context }) {
|
|
20
|
+
const config = loadProjectConfig(context.cwd, context.cwd);
|
|
21
|
+
const targetName = options.target ?? config.target;
|
|
22
|
+
if (!targetName) {
|
|
23
|
+
throw new FcdevError('没有指定目标', { code: 'TARGET_MISSING', hint: '在 fcdev.json 里写 "target",或用 --target <名> 指定' });
|
|
24
|
+
}
|
|
25
|
+
const target = loadTarget(targetName);
|
|
26
|
+
const functions = loadTargetApi(target);
|
|
27
|
+
if (functions.length === 0) {
|
|
28
|
+
throw new FcdevError(`目标 ${target.name} 没有 C 接口文档`, { code: 'API_MISSING', hint: '目前只有 sb2000 目标提供运行库函数的文档' });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const query = positionals.join(' ').trim();
|
|
32
|
+
if (query === '') {
|
|
33
|
+
const groups = groupByModule(functions);
|
|
34
|
+
if (options.json) {
|
|
35
|
+
const modules = [...groups].map(([name, items]) => ({ module: name, header: items[0].header, functions: items.map((fn) => fn.name) }));
|
|
36
|
+
context.stdout.write(`${JSON.stringify(modules, null, 2)}\n`);
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
const lines = [`${target.title} 的 C 接口:${functions.length} 个函数,${groups.size} 个模块`, ''];
|
|
40
|
+
for (const [name, items] of groups) lines.push(` ${name.padEnd(10)} ${String(items.length).padStart(3)} 个函数 ${items[0].header}`);
|
|
41
|
+
lines.push('', '查询:fcdev api <函数名>(完整文档)、fcdev api <模块名>(函数列表)、fcdev api <$功能号>(用到该系统调用的函数)、fcdev api <词>(按名称与说明搜索)');
|
|
42
|
+
context.stdout.write(`${lines.join('\n')}\n`);
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const { kind, matches } = queryApi(functions, query);
|
|
47
|
+
if (matches.length === 0) {
|
|
48
|
+
throw new FcdevError(`没有找到“${query}”`, { code: 'API_NOT_FOUND', hint: '不带查询词运行 fcdev api 可以看到全部模块' });
|
|
49
|
+
}
|
|
50
|
+
if (options.json) {
|
|
51
|
+
context.stdout.write(`${JSON.stringify(matches, null, 2)}\n`);
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
if (kind === 'function') {
|
|
55
|
+
context.stdout.write(`${matches.map(formatFunction).join('\n\n')}\n`);
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
const width = Math.max(...matches.map((fn) => fn.name.length));
|
|
59
|
+
const title = { module: `模块 ${query}`, syscall: `系统调用 ${query}`, text: `包含“${query}”` }[kind];
|
|
60
|
+
context.stdout.write(`${title}:${matches.length} 个函数\n${matches.map((fn) => formatSummary(fn, width)).join('\n')}\n`);
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:build-command.mjs
|
|
3
|
+
// 说明:build 命令:按路径构建一个程序,输出进度与结果。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:工具(cc65、ca65、ld65)的输出原样写到标准错误,格式保持不变,供问题匹配器使用。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { buildPath } from '../../build/index.mjs';
|
|
11
|
+
import { FcdevError } from '../../shared/index.mjs';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 执行 build 命令。
|
|
15
|
+
* @param {{positionals: string[], options: {target?: string, verbose: boolean}, context: import('../index.mjs').CliContext}} input 参数与运行环境
|
|
16
|
+
* @returns {Promise<number>} 退出码
|
|
17
|
+
* @throws {FcdevError} 位置参数多于一个,或构建失败
|
|
18
|
+
*/
|
|
19
|
+
export async function runBuildCommand({ positionals, options, context }) {
|
|
20
|
+
if (positionals.length > 1) {
|
|
21
|
+
throw new FcdevError('build 只接受一个路径参数', { code: 'ARGUMENT_INVALID', hint: '用法:fcdev build [目录或文件]' });
|
|
22
|
+
}
|
|
23
|
+
const reporter = {
|
|
24
|
+
log: (message) => context.stdout.write(`${message}\n`),
|
|
25
|
+
write: (text) => context.stderr.write(text),
|
|
26
|
+
};
|
|
27
|
+
const { plan, result } = await buildPath(
|
|
28
|
+
{ pathArg: positionals[0], cwd: context.cwd, targetOverride: options.target, env: context.env },
|
|
29
|
+
reporter,
|
|
30
|
+
);
|
|
31
|
+
const shown = path.relative(context.cwd, result.outputFile) || result.outputFile;
|
|
32
|
+
context.stdout.write(`已生成 ${shown}(${result.size} 字节,目标 ${plan.target.name},重新编译 ${result.compiled} 个文件,沿用 ${result.reused} 个)\n`);
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:debug-command.mjs
|
|
3
|
+
// 说明:debug 命令:把命令原样转发给模拟器的调试服务并打印应答。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:模拟器的工作目录与终端不同,命令里的相对路径(shot、disk、trace 等)先换成绝对路径;端口取环境变量 FCDEV_DEBUG_PORT,默认 5699。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { DEFAULT_DEBUG_PORT, DebugClient } from '../../emulator/index.mjs';
|
|
11
|
+
import { FcdevError } from '../../shared/index.mjs';
|
|
12
|
+
|
|
13
|
+
/** 耗时长的命令使用的应答超时,单位毫秒 */
|
|
14
|
+
const LONG_COMMAND_TIMEOUT_MS = 10 * 60 * 1000;
|
|
15
|
+
|
|
16
|
+
/** 耗时长的命令 */
|
|
17
|
+
const LONG_COMMANDS = new Set(['run', 'step']);
|
|
18
|
+
|
|
19
|
+
/** 命令里路径参数的位置(命令名之后的第几个词,从 1 起);disk 的路径可含空格,取剩余全部 */
|
|
20
|
+
const PATH_POSITIONS = {
|
|
21
|
+
shot: 1,
|
|
22
|
+
disk: 1,
|
|
23
|
+
trace: 1,
|
|
24
|
+
io: 1,
|
|
25
|
+
disklog: 1,
|
|
26
|
+
calllog: 1,
|
|
27
|
+
savestate: 1,
|
|
28
|
+
loadstate: 1,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 把命令里的相对路径换成绝对路径。
|
|
33
|
+
* @param {string[]} tokens 命令的各个词
|
|
34
|
+
* @param {string} cwd 终端当前目录
|
|
35
|
+
* @returns {string[]} 处理后的词
|
|
36
|
+
*/
|
|
37
|
+
export function resolveCommandPaths(tokens, cwd) {
|
|
38
|
+
const [name, ...rest] = tokens;
|
|
39
|
+
if (name === 'cdl' && rest[0] === 'save' && rest[1]) {
|
|
40
|
+
return [name, rest[0], path.resolve(cwd, rest[1]), ...rest.slice(2)];
|
|
41
|
+
}
|
|
42
|
+
const position = PATH_POSITIONS[name];
|
|
43
|
+
if (position === undefined || rest.length < position || rest[position - 1] === 'off') return tokens;
|
|
44
|
+
if (name === 'disk') return [name, path.resolve(cwd, rest.join(' '))];
|
|
45
|
+
const resolved = [...rest];
|
|
46
|
+
resolved[position - 1] = path.resolve(cwd, rest[position - 1]);
|
|
47
|
+
return [name, ...resolved];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 执行 debug 命令。
|
|
52
|
+
* @param {{positionals: string[], context: import('../index.mjs').CliContext}} input 调试命令的各个词与运行环境
|
|
53
|
+
* @returns {Promise<number>} 应答为 OK 时 0,否则 1
|
|
54
|
+
* @throws {FcdevError} 没有给命令,或无法连接调试服务
|
|
55
|
+
*/
|
|
56
|
+
export async function runDebugCommand({ positionals, context }) {
|
|
57
|
+
if (positionals.length === 0) {
|
|
58
|
+
throw new FcdevError('debug 需要一条调试命令', { code: 'ARGUMENT_INVALID', hint: '例如:fcdev debug status、fcdev debug run 60、fcdev debug shot out.png' });
|
|
59
|
+
}
|
|
60
|
+
const port = Number(context.env.FCDEV_DEBUG_PORT) || DEFAULT_DEBUG_PORT;
|
|
61
|
+
const tokens = resolveCommandPaths(positionals, context.cwd);
|
|
62
|
+
const client = await DebugClient.connect({ port });
|
|
63
|
+
try {
|
|
64
|
+
const reply = await client.send(tokens.join(' '), { timeoutMs: LONG_COMMANDS.has(tokens[0]) ? LONG_COMMAND_TIMEOUT_MS : undefined });
|
|
65
|
+
context.stdout.write(`${[reply.head, ...reply.lines].join('\n')}\n`);
|
|
66
|
+
return reply.ok ? 0 : 1;
|
|
67
|
+
} finally {
|
|
68
|
+
client.close();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:disasm-command.mjs
|
|
3
|
+
// 说明:disasm 命令:把 NES 程序或平铺二进制反汇编成 ca65 源码,并回读校验。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:iNES 文件读取 PRG 与中断向量,自动加入 NES 的 PPU 寄存器符号;平铺二进制必须指定加载地址与入口。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { disassemble } from '../../disasm/index.mjs';
|
|
11
|
+
import { loadProjectConfig } from '../../config/index.mjs';
|
|
12
|
+
import { FcdevError } from '../../shared/index.mjs';
|
|
13
|
+
import { BUILTIN_TARGETS_DIR } from '../../targets/index.mjs';
|
|
14
|
+
import { locateToolchain } from '../../toolchain/index.mjs';
|
|
15
|
+
|
|
16
|
+
/** 十六进制地址:可带 $ 或 0x 前缀,最多 4 位 */
|
|
17
|
+
const ADDRESS = /^(?:\$|0[xX])?([0-9A-Fa-f]{1,4})$/;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 解析命令行里的地址。
|
|
21
|
+
* @param {string} text 地址文本,如 8000、$8000、0x8000
|
|
22
|
+
* @param {string} option 选项名,用于出错提示
|
|
23
|
+
* @returns {number} 地址
|
|
24
|
+
* @throws {FcdevError} 不是合法的 16 位十六进制地址
|
|
25
|
+
*/
|
|
26
|
+
export function parseAddress(text, option) {
|
|
27
|
+
const match = ADDRESS.exec(text);
|
|
28
|
+
if (!match) throw new FcdevError(`${option} 需要 16 位十六进制地址,实际为“${text}”`, { code: 'ARGUMENT_INVALID' });
|
|
29
|
+
return Number.parseInt(match[1], 16);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 执行 disasm 命令。
|
|
34
|
+
* @param {{positionals: string[], options: {format?: string, base?: string, cdl?: string, trace?: string, symbols?: string, out?: string, entries: string[]}, context: import('../index.mjs').CliContext}} input 参数与运行环境
|
|
35
|
+
* @returns {Promise<number>} 退出码
|
|
36
|
+
* @throws {FcdevError} 参数不合法、找不到工具链,或回读校验不一致
|
|
37
|
+
*/
|
|
38
|
+
export async function runDisasmCommand({ positionals, options, context }) {
|
|
39
|
+
if (positionals.length !== 1) {
|
|
40
|
+
throw new FcdevError('disasm 需要一个输入文件', { code: 'ARGUMENT_INVALID', hint: '用法:fcdev disasm <文件> [--format ines|binary] [--base 地址] [--entry 地址] [--cdl 文件] [--symbols 文件] [--out 目录]' });
|
|
41
|
+
}
|
|
42
|
+
const inputFile = path.resolve(context.cwd, positionals[0]);
|
|
43
|
+
const config = loadProjectConfig(context.cwd, context.cwd);
|
|
44
|
+
const toolchain = locateToolchain({ configured: config.toolchain, env: context.env });
|
|
45
|
+
const nesRegisters = path.join(BUILTIN_TARGETS_DIR, 'nes', 'asminc', 'fcnes.inc');
|
|
46
|
+
|
|
47
|
+
const report = await disassemble({
|
|
48
|
+
inputFile,
|
|
49
|
+
format: options.format,
|
|
50
|
+
base: options.base === undefined ? undefined : parseAddress(options.base, '--base'),
|
|
51
|
+
entries: options.entries.map((entry) => parseAddress(entry, '--entry')),
|
|
52
|
+
cdlFile: options.cdl === undefined ? undefined : path.resolve(context.cwd, options.cdl),
|
|
53
|
+
traceFile: options.trace === undefined ? undefined : path.resolve(context.cwd, options.trace),
|
|
54
|
+
symbolsFile: options.symbols === undefined ? undefined : path.resolve(context.cwd, options.symbols),
|
|
55
|
+
outDir: path.resolve(context.cwd, options.out ?? path.join('build', 'disasm', path.basename(inputFile, path.extname(inputFile)))),
|
|
56
|
+
toolchain,
|
|
57
|
+
includes: [nesRegisters],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
for (const warning of report.warnings.slice(0, 10)) context.stderr.write(`警告:${warning}\n`);
|
|
61
|
+
if (report.warnings.length > 10) context.stderr.write(`警告:还有 ${report.warnings.length - 10} 条,省略\n`);
|
|
62
|
+
context.stdout.write(`${report.description}:${report.size} 字节,指令 ${report.instructions} 条(${report.codeBytes} 字节),其余 ${report.size - report.codeBytes} 字节按数据处理\n`);
|
|
63
|
+
context.stdout.write(`回读校验通过:重新汇编的结果与原映像逐字节一致\n已生成 ${path.relative(context.cwd, report.sourceFile) || report.sourceFile}\n`);
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:disk-command.mjs
|
|
3
|
+
// 说明:disk 命令:构建程序并把它放进磁盘镜像。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:只有声明了磁盘能力的目标可用;镜像生成在程序产物旁边。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { buildDiskImage, buildPath } from '../../build/index.mjs';
|
|
11
|
+
import { FcdevError } from '../../shared/index.mjs';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 执行 disk 命令。
|
|
15
|
+
* @param {{positionals: string[], options: {target?: string}, context: import('../index.mjs').CliContext}} input 参数与运行环境
|
|
16
|
+
* @returns {Promise<number>} 退出码
|
|
17
|
+
* @throws {FcdevError} 位置参数多于一个、目标不支持磁盘镜像,或构建失败
|
|
18
|
+
*/
|
|
19
|
+
export async function runDiskCommand({ positionals, options, context }) {
|
|
20
|
+
if (positionals.length > 1) {
|
|
21
|
+
throw new FcdevError('disk 只接受一个路径参数', { code: 'ARGUMENT_INVALID', hint: '用法:fcdev disk [目录或文件]' });
|
|
22
|
+
}
|
|
23
|
+
const reporter = {
|
|
24
|
+
log: (message) => context.stdout.write(`${message}\n`),
|
|
25
|
+
write: (text) => context.stderr.write(text),
|
|
26
|
+
};
|
|
27
|
+
const { plan, result } = await buildPath(
|
|
28
|
+
{ pathArg: positionals[0], cwd: context.cwd, targetOverride: options.target, env: context.env },
|
|
29
|
+
reporter,
|
|
30
|
+
);
|
|
31
|
+
const { imageFile, size } = buildDiskImage(plan, result);
|
|
32
|
+
context.stdout.write(`已生成 ${path.relative(context.cwd, imageFile) || imageFile}(${size} 字节,含 ${path.basename(result.outputFile)})\n`);
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:info-command.mjs
|
|
3
|
+
// 说明:info 命令:显示当前项目的目标、工具链与头文件目录,--json 时输出机器可读格式。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:无
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import { describeProject } from './project-info.mjs';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 执行 info 命令。
|
|
13
|
+
* @param {{options: {target?: string, json: boolean}, context: import('../index.mjs').CliContext}} input 选项与运行环境
|
|
14
|
+
* @returns {Promise<number>} 退出码
|
|
15
|
+
*/
|
|
16
|
+
export async function runInfoCommand({ options, context }) {
|
|
17
|
+
const info = await describeProject({ cwd: context.cwd, targetOverride: options.target, env: context.env });
|
|
18
|
+
if (options.json) {
|
|
19
|
+
context.stdout.write(`${JSON.stringify(info, null, 2)}\n`);
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
const lines = [
|
|
23
|
+
`项目根目录:${info.root}`,
|
|
24
|
+
`配置文件:${info.configFile ?? '(无)'}`,
|
|
25
|
+
`目标:${info.target.name}(${info.target.title})`,
|
|
26
|
+
`工具链:${info.toolchain.home}(${info.toolchain.source},${info.toolchain.version})`,
|
|
27
|
+
`输出目录:${info.outDir}`,
|
|
28
|
+
`头文件目录:\n ${info.includeDirs.join('\n ')}`,
|
|
29
|
+
`汇编引入目录:\n ${info.asmIncludeDirs.join('\n ')}`,
|
|
30
|
+
];
|
|
31
|
+
context.stdout.write(`${lines.join('\n')}\n`);
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:project-info.mjs
|
|
3
|
+
// 说明:汇总当前项目的目标、工具链与头文件目录,供 info 与 sync 命令使用。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:从终端当前目录向上查找 fcdev.json;没有配置时必须用 --target 指定目标。
|
|
8
|
+
// 预定义宏里带一组编辑器兼容宏:cc65 的头文件大量使用 __fastcall__ 这类关键字,
|
|
9
|
+
// C/C++ 扩展与 clangd 不认识,定义成空宏才不会报语法错误;只用于智能感知,不影响构建。
|
|
10
|
+
// ------------------------------------------------------------------------
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { loadProjectConfig } from '../../config/index.mjs';
|
|
13
|
+
import { FcdevError } from '../../shared/index.mjs';
|
|
14
|
+
import { loadTarget, resolveTargetPath } from '../../targets/index.mjs';
|
|
15
|
+
import { getToolchainVersion, locateToolchain } from '../../toolchain/index.mjs';
|
|
16
|
+
|
|
17
|
+
/** 只供编辑器使用的兼容宏:cc65 的调用约定关键字定义成空 */
|
|
18
|
+
const EDITOR_COMPATIBILITY_DEFINES = { __fastcall__: '', __cdecl__: '' };
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {object} ProjectInfo
|
|
22
|
+
* @property {string} root 项目根目录
|
|
23
|
+
* @property {string|null} configFile 配置文件路径
|
|
24
|
+
* @property {{name: string, title: string}} target 目标
|
|
25
|
+
* @property {{home: string, source: string, version: string}} toolchain 工具链
|
|
26
|
+
* @property {string[]} includeDirs C 头文件目录
|
|
27
|
+
* @property {string[]} asmIncludeDirs 汇编引入目录
|
|
28
|
+
* @property {Record<string, string|number>} defines 预定义宏(含 cc65 自带的目标宏与编辑器兼容宏)
|
|
29
|
+
* @property {string} outDir 输出目录
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 汇总项目信息。
|
|
34
|
+
* @param {{cwd: string, targetOverride?: string, env?: NodeJS.ProcessEnv}} input 终端目录、目标覆盖与环境变量
|
|
35
|
+
* @returns {Promise<ProjectInfo>} 项目信息
|
|
36
|
+
* @throws {FcdevError} 没有指定目标,或找不到工具链
|
|
37
|
+
*/
|
|
38
|
+
export async function describeProject({ cwd, targetOverride, env }) {
|
|
39
|
+
const config = loadProjectConfig(cwd, cwd);
|
|
40
|
+
const targetName = targetOverride ?? config.target;
|
|
41
|
+
if (!targetName) {
|
|
42
|
+
throw new FcdevError('没有指定目标', { code: 'TARGET_MISSING', hint: '在 fcdev.json 里写 "target",或用 --target <名> 指定' });
|
|
43
|
+
}
|
|
44
|
+
const target = loadTarget(targetName);
|
|
45
|
+
const toolchain = locateToolchain({ configured: config.toolchain, env });
|
|
46
|
+
const resolveAll = (values) => values.map((value) => resolveTargetPath(target, value, toolchain.home));
|
|
47
|
+
return {
|
|
48
|
+
root: config.root,
|
|
49
|
+
configFile: config.file,
|
|
50
|
+
target: { name: target.name, title: target.title },
|
|
51
|
+
toolchain: { home: toolchain.home, source: toolchain.source, version: await getToolchainVersion(toolchain) },
|
|
52
|
+
includeDirs: [...resolveAll(target.include), ...config.include, path.join(toolchain.home, 'include')],
|
|
53
|
+
asmIncludeDirs: [...resolveAll(target.asminclude), ...config.include, path.join(toolchain.home, 'asminc')],
|
|
54
|
+
defines: { ...EDITOR_COMPATIBILITY_DEFINES, __CC65__: 1, [`__${target.cc65Target.toUpperCase()}__`]: 1, ...target.defines, ...config.define },
|
|
55
|
+
outDir: config.outDir,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:run-command.mjs
|
|
3
|
+
// 说明:run 命令:构建程序后在模拟器里运行;--debug 时启动调试模式,由 debug 命令继续操作。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:运行方案由目标的 run 字段声明:rom 直接加载产物;firmware-disk 加载固件(fcdev.json 的 firmware),挂载磁盘镜像后复位。
|
|
8
|
+
// firmware-disk 方案需要调试服务挂载镜像,挂载完成后用 live 命令转为自由运行(加 --debug 则保持调试模式)。
|
|
9
|
+
// ------------------------------------------------------------------------
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { buildDiskImage, buildPath } from '../../build/index.mjs';
|
|
12
|
+
import { DEFAULT_DEBUG_PORT, launchEmulator, locateEmulator } from '../../emulator/index.mjs';
|
|
13
|
+
import { FcdevError } from '../../shared/index.mjs';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 执行 run 命令。
|
|
17
|
+
* @param {{positionals: string[], options: {target?: string, debug: boolean, port?: number}, context: import('../index.mjs').CliContext}} input 参数与运行环境
|
|
18
|
+
* @returns {Promise<number>} 退出码
|
|
19
|
+
* @throws {FcdevError} 目标没有运行方案、找不到模拟器或构建失败
|
|
20
|
+
*/
|
|
21
|
+
export async function runRunCommand({ positionals, options, context }) {
|
|
22
|
+
if (positionals.length > 1) {
|
|
23
|
+
throw new FcdevError('run 只接受一个路径参数', { code: 'ARGUMENT_INVALID', hint: '用法:fcdev run [目录或文件]' });
|
|
24
|
+
}
|
|
25
|
+
const reporter = {
|
|
26
|
+
log: (message) => context.stdout.write(`${message}\n`),
|
|
27
|
+
write: (text) => context.stderr.write(text),
|
|
28
|
+
};
|
|
29
|
+
const { plan, result } = await buildPath(
|
|
30
|
+
{ pathArg: positionals[0], cwd: context.cwd, targetOverride: options.target, env: context.env },
|
|
31
|
+
reporter,
|
|
32
|
+
);
|
|
33
|
+
const run = plan.target.run;
|
|
34
|
+
if (!run) {
|
|
35
|
+
throw new FcdevError(`目标 ${plan.target.name} 没有运行方案`, { code: 'TARGET_NO_RUN' });
|
|
36
|
+
}
|
|
37
|
+
const emulator = locateEmulator({ configured: plan.config.emulator, env: context.env });
|
|
38
|
+
const port = options.port ?? DEFAULT_DEBUG_PORT;
|
|
39
|
+
|
|
40
|
+
if (run.mode === 'rom') {
|
|
41
|
+
const { pid, client } = await launchEmulator({ executable: emulator.executable, romFile: result.outputFile, debug: options.debug, port });
|
|
42
|
+
client?.close();
|
|
43
|
+
context.stdout.write(`已启动模拟器(进程 ${pid}),加载 ${result.outputFile}\n`);
|
|
44
|
+
} else {
|
|
45
|
+
if (!plan.config.firmware) {
|
|
46
|
+
throw new FcdevError(`目标 ${plan.target.name} 的运行需要固件(BIOS)`, {
|
|
47
|
+
code: 'FIRMWARE_MISSING',
|
|
48
|
+
hint: '在 fcdev.json 里用 firmware 指定固件文件;fcdev 不提供任何 BIOS 或 ROM',
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const { imageFile } = buildDiskImage(plan, result);
|
|
52
|
+
const { pid, client } = await launchEmulator({ executable: emulator.executable, romFile: plan.config.firmware, debug: true, port });
|
|
53
|
+
try {
|
|
54
|
+
for (const command of [`disk ${imageFile}`, 'reset', ...(options.debug ? [] : ['live'])]) {
|
|
55
|
+
const reply = await client.send(command);
|
|
56
|
+
if (!reply.ok) throw new FcdevError(`模拟器拒绝命令“${command}”:${reply.head}`, { code: 'EMULATOR_COMMAND_FAILED' });
|
|
57
|
+
}
|
|
58
|
+
} finally {
|
|
59
|
+
client.close();
|
|
60
|
+
}
|
|
61
|
+
context.stdout.write(`已启动模拟器(进程 ${pid}),固件 ${plan.config.firmware},磁盘镜像 ${imageFile}\n`);
|
|
62
|
+
context.stdout.write(`等 DOS 启动后,在提示符下输入 ${path.basename(result.outputFile, path.extname(result.outputFile))} 运行程序\n`);
|
|
63
|
+
}
|
|
64
|
+
if (options.debug) {
|
|
65
|
+
context.stdout.write(`调试模式:端口 ${port},模拟器只在收到 run 命令时运行,例如 fcdev debug run 60\n`);
|
|
66
|
+
}
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:sync-command.mjs
|
|
3
|
+
// 说明:sync 命令:生成 IDE 的 C/C++ 配置,让智能提示找到 cc65 与目标的头文件。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:写 .vscode/c_cpp_properties.json 里名为 fcdev 的配置(保留其他配置)与项目根的 compile_flags.txt;
|
|
8
|
+
// compile_flags.txt 的第一行是标记 -DFCDEV_GENERATED,没有标记的同名文件视为用户自己的,不覆盖。
|
|
9
|
+
// ------------------------------------------------------------------------
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { FcdevError, readJsonFile } from '../../shared/index.mjs';
|
|
13
|
+
import { describeProject } from './project-info.mjs';
|
|
14
|
+
|
|
15
|
+
/** compile_flags.txt 里标记“由 fcdev 生成”的第一行 */
|
|
16
|
+
const GENERATED_MARKER = '-DFCDEV_GENERATED';
|
|
17
|
+
|
|
18
|
+
/** c_cpp_properties.json 里 fcdev 管理的配置名 */
|
|
19
|
+
const CONFIGURATION_NAME = 'fcdev';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 把预定义宏转成 NAME=VALUE 形式。
|
|
23
|
+
* @param {Record<string, string|number>} defines 宏表
|
|
24
|
+
* @returns {string[]} 宏列表
|
|
25
|
+
*/
|
|
26
|
+
function formatDefines(defines) {
|
|
27
|
+
return Object.entries(defines).map(([name, value]) => `${name}=${value}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 写入或更新 .vscode/c_cpp_properties.json 里 fcdev 的配置。
|
|
32
|
+
* @param {string} root 项目根目录
|
|
33
|
+
* @param {import('./project-info.mjs').ProjectInfo} info 项目信息
|
|
34
|
+
* @returns {string} 写入的文件
|
|
35
|
+
* @throws {FcdevError} 已有文件不是合法的配置
|
|
36
|
+
*/
|
|
37
|
+
function writeCppProperties(root, info) {
|
|
38
|
+
const file = path.join(root, '.vscode', 'c_cpp_properties.json');
|
|
39
|
+
const document = existsSync(file) ? readJsonFile(file) : { version: 4, configurations: [] };
|
|
40
|
+
if (!Array.isArray(document.configurations)) {
|
|
41
|
+
throw new FcdevError(`${file} 缺少 configurations 数组`, { code: 'SYNC_FILE_INVALID' });
|
|
42
|
+
}
|
|
43
|
+
const configuration = {
|
|
44
|
+
name: CONFIGURATION_NAME,
|
|
45
|
+
includePath: info.includeDirs,
|
|
46
|
+
defines: formatDefines(info.defines),
|
|
47
|
+
cStandard: 'c89',
|
|
48
|
+
intelliSenseMode: 'clang-x86',
|
|
49
|
+
};
|
|
50
|
+
const index = document.configurations.findIndex((item) => item.name === CONFIGURATION_NAME);
|
|
51
|
+
if (index >= 0) document.configurations[index] = configuration;
|
|
52
|
+
else document.configurations.push(configuration);
|
|
53
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
54
|
+
writeFileSync(file, `${JSON.stringify(document, null, 2)}\n`);
|
|
55
|
+
return file;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 写入 compile_flags.txt。
|
|
60
|
+
* @param {string} root 项目根目录
|
|
61
|
+
* @param {import('./project-info.mjs').ProjectInfo} info 项目信息
|
|
62
|
+
* @returns {string|null} 写入的文件;已有用户自己的同名文件时为 null
|
|
63
|
+
*/
|
|
64
|
+
function writeCompileFlags(root, info) {
|
|
65
|
+
const file = path.join(root, 'compile_flags.txt');
|
|
66
|
+
if (existsSync(file) && readFileSync(file, 'utf8').split(/\r?\n/)[0] !== GENERATED_MARKER) return null;
|
|
67
|
+
const lines = [GENERATED_MARKER, ...info.includeDirs.map((dir) => `-I${dir}`), ...formatDefines(info.defines).map((item) => `-D${item}`)];
|
|
68
|
+
writeFileSync(file, `${lines.join('\n')}\n`);
|
|
69
|
+
return file;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 执行 sync 命令。
|
|
74
|
+
* @param {{options: {target?: string}, context: import('../index.mjs').CliContext}} input 选项与运行环境
|
|
75
|
+
* @returns {Promise<number>} 退出码
|
|
76
|
+
*/
|
|
77
|
+
export async function runSyncCommand({ options, context }) {
|
|
78
|
+
const info = await describeProject({ cwd: context.cwd, targetOverride: options.target, env: context.env });
|
|
79
|
+
const written = [writeCppProperties(info.root, info)];
|
|
80
|
+
const flags = writeCompileFlags(info.root, info);
|
|
81
|
+
if (flags) written.push(flags);
|
|
82
|
+
else context.stderr.write('compile_flags.txt 已存在且不是 fcdev 生成的,没有覆盖\n');
|
|
83
|
+
context.stdout.write(`已生成:\n${written.map((file) => ` ${path.relative(context.cwd, file)}`).join('\n')}\n`);
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:index.mjs
|
|
3
|
+
// 说明:命令行入口:解析参数、分发命令、把错误翻译成用户能看懂的提示。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:runCli 不直接读写进程状态,终端目录、环境变量与输出流都由调用者传入,便于测试。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import { FcdevError, readJsonFile } from '../shared/index.mjs';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
11
|
+
import { parseArguments } from './args-parser.mjs';
|
|
12
|
+
import { runApiCommand } from './commands/api-command.mjs';
|
|
13
|
+
import { runBuildCommand } from './commands/build-command.mjs';
|
|
14
|
+
import { runDebugCommand } from './commands/debug-command.mjs';
|
|
15
|
+
import { runDiskCommand } from './commands/disk-command.mjs';
|
|
16
|
+
import { runDisasmCommand } from './commands/disasm-command.mjs';
|
|
17
|
+
import { runInfoCommand } from './commands/info-command.mjs';
|
|
18
|
+
import { runRunCommand } from './commands/run-command.mjs';
|
|
19
|
+
import { runSyncCommand } from './commands/sync-command.mjs';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {object} CliContext
|
|
23
|
+
* @property {string} cwd 终端当前目录
|
|
24
|
+
* @property {NodeJS.ProcessEnv} env 环境变量
|
|
25
|
+
* @property {{write: (text: string) => unknown}} stdout 标准输出
|
|
26
|
+
* @property {{write: (text: string) => unknown}} stderr 标准错误
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** 命令表:命令名到处理函数 */
|
|
30
|
+
const COMMANDS = {
|
|
31
|
+
api: runApiCommand,
|
|
32
|
+
build: runBuildCommand,
|
|
33
|
+
debug: runDebugCommand,
|
|
34
|
+
disasm: runDisasmCommand,
|
|
35
|
+
disk: runDiskCommand,
|
|
36
|
+
info: runInfoCommand,
|
|
37
|
+
run: runRunCommand,
|
|
38
|
+
sync: runSyncCommand,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** 帮助文字 */
|
|
42
|
+
const HELP_TEXT = `fcdev:6502 兼容机的开发工具
|
|
43
|
+
|
|
44
|
+
用法:fcdev <命令> [选项]
|
|
45
|
+
|
|
46
|
+
命令:
|
|
47
|
+
build [目录或文件] 构建:无参数为当前目录;目录整体构成一个程序;文件单独构成一个程序
|
|
48
|
+
run [目录或文件] 构建后在模拟器里运行(--debug 启动调试模式)
|
|
49
|
+
disk [目录或文件] 构建后把程序放进磁盘镜像(只有支持磁盘的目标可用)
|
|
50
|
+
disasm <文件> 反汇编 NES 程序或平铺二进制为 ca65 源码,并回读校验
|
|
51
|
+
debug <命令> 把命令转发给模拟器的调试服务,如 debug run 60、debug shot out.png
|
|
52
|
+
sync 生成 IDE 的 C/C++ 配置
|
|
53
|
+
info [--json] 显示目标、工具链与头文件目录
|
|
54
|
+
api [词] [--json] 查询目标的 C 接口文档:函数名、模块名、$功能号或关键词;不带词列出全部模块
|
|
55
|
+
|
|
56
|
+
选项:
|
|
57
|
+
--target <名> 指定目标,覆盖 fcdev.json
|
|
58
|
+
--debug run 以调试模式启动模拟器
|
|
59
|
+
--port <端口> run 的调试端口,默认 5699;debug 命令读环境变量 FCDEV_DEBUG_PORT,默认也是 5699
|
|
60
|
+
--json info 输出机器可读格式
|
|
61
|
+
|
|
62
|
+
disasm 的选项:
|
|
63
|
+
--format ines|binary 输入格式,缺省时按文件头判断
|
|
64
|
+
--base <地址> 平铺二进制的加载地址(十六进制)
|
|
65
|
+
--entry <地址> 追踪代码的入口,可重复;iNES 会自动使用中断向量
|
|
66
|
+
--cdl <文件> CDL(FCEUX、Mesen 格式),代码段的起点也当作入口
|
|
67
|
+
--trace <文件> 模拟器 trace 命令记录的指令轨迹,执行过的指令地址都当作入口
|
|
68
|
+
--symbols <文件> 符号文件,每行“名字 = $地址”
|
|
69
|
+
--out <目录> 输出目录,默认 build/disasm/<文件名>
|
|
70
|
+
-h, --help 显示帮助
|
|
71
|
+
-v, --version 显示版本
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 运行命令行。
|
|
76
|
+
* @param {string[]} argv 去掉 node 与脚本路径后的参数
|
|
77
|
+
* @param {CliContext} context 运行环境
|
|
78
|
+
* @returns {Promise<number>} 进程退出码
|
|
79
|
+
*/
|
|
80
|
+
export async function runCli(argv, context) {
|
|
81
|
+
try {
|
|
82
|
+
const { command, positionals, options } = parseArguments(argv);
|
|
83
|
+
if (options.version) {
|
|
84
|
+
const packageFile = fileURLToPath(new URL('../../package.json', import.meta.url));
|
|
85
|
+
context.stdout.write(`${readJsonFile(packageFile).version}\n`);
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
if (options.help || command === null || command === 'help') {
|
|
89
|
+
context.stdout.write(HELP_TEXT);
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
const handler = COMMANDS[command];
|
|
93
|
+
if (!handler) {
|
|
94
|
+
throw new FcdevError(`未知的命令:${command}`, { code: 'ARGUMENT_INVALID', hint: '用 fcdev --help 查看可用的命令' });
|
|
95
|
+
}
|
|
96
|
+
return await handler({ positionals, options, context });
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (!(error instanceof FcdevError)) throw error;
|
|
99
|
+
context.stderr.write(`错误:${error.message}\n`);
|
|
100
|
+
if (error.hint) context.stderr.write(`${error.hint}\n`);
|
|
101
|
+
return 1;
|
|
102
|
+
}
|
|
103
|
+
}
|