fcdev 0.0.0-stage → 0.1.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.
Files changed (71) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +81 -2
  3. package/bin/fcdev.mjs +17 -0
  4. package/package.json +38 -4
  5. package/src/build/build-plan.mjs +92 -0
  6. package/src/build/dependency-file.mjs +24 -0
  7. package/src/build/disk-builder.mjs +36 -0
  8. package/src/build/index.mjs +31 -0
  9. package/src/build/program-builder.mjs +209 -0
  10. package/src/build/source-collector.mjs +54 -0
  11. package/src/build/tool-arguments.mjs +83 -0
  12. package/src/cli/args-parser.mjs +84 -0
  13. package/src/cli/commands/build-command.mjs +34 -0
  14. package/src/cli/commands/debug-command.mjs +70 -0
  15. package/src/cli/commands/disasm-command.mjs +65 -0
  16. package/src/cli/commands/disk-command.mjs +34 -0
  17. package/src/cli/commands/info-command.mjs +33 -0
  18. package/src/cli/commands/project-info.mjs +57 -0
  19. package/src/cli/commands/run-command.mjs +68 -0
  20. package/src/cli/commands/sync-command.mjs +85 -0
  21. package/src/cli/index.mjs +100 -0
  22. package/src/config/config-loader.mjs +132 -0
  23. package/src/config/index.mjs +9 -0
  24. package/src/disasm/analysis.mjs +97 -0
  25. package/src/disasm/cdl.mjs +35 -0
  26. package/src/disasm/disassembler.mjs +114 -0
  27. package/src/disasm/index.mjs +16 -0
  28. package/src/disasm/info-writer.mjs +72 -0
  29. package/src/disasm/loaders.mjs +69 -0
  30. package/src/disasm/opcode-table.mjs +268 -0
  31. package/src/disasm/roundtrip.mjs +69 -0
  32. package/src/disasm/symbols.mjs +30 -0
  33. package/src/disasm/trace.mjs +25 -0
  34. package/src/emulator/debug-client.mjs +125 -0
  35. package/src/emulator/emulator-launcher.mjs +80 -0
  36. package/src/emulator/emulator-locator.mjs +62 -0
  37. package/src/emulator/index.mjs +11 -0
  38. package/src/packagers/fat-floppy.mjs +22 -0
  39. package/src/packagers/fat12-image.mjs +245 -0
  40. package/src/packagers/flat-binary.mjs +53 -0
  41. package/src/packagers/index.mjs +54 -0
  42. package/src/packagers/ines.mjs +38 -0
  43. package/src/shared/errors.mjs +25 -0
  44. package/src/shared/fs-utils.mjs +66 -0
  45. package/src/shared/index.mjs +11 -0
  46. package/src/shared/process.mjs +50 -0
  47. package/src/targets/index.mjs +17 -0
  48. package/src/targets/name-rules.mjs +33 -0
  49. package/src/targets/target-registry.mjs +187 -0
  50. package/src/toolchain/index.mjs +16 -0
  51. package/src/toolchain/toolchain-locator.mjs +104 -0
  52. package/targets/nes/asminc/fcnes.inc +16 -0
  53. package/targets/nes/chr/ascii-8x8.chr +0 -0
  54. package/targets/nes/chr/font8x8_basic.h +152 -0
  55. package/targets/nes/include/fcnes.h +45 -0
  56. package/targets/nes/lib/fcnes.lib +0 -0
  57. package/targets/nes/lib/nes-font.o +0 -0
  58. package/targets/nes/lib-build.json +9 -0
  59. package/targets/nes/src/fcnes.s +91 -0
  60. package/targets/nes/src/nes-font.s +12 -0
  61. package/targets/nes/target.json +27 -0
  62. package/targets/sb2000/asminc/sb2000.inc +216 -0
  63. package/targets/sb2000/include/sb2000-symbols.h +380 -0
  64. package/targets/sb2000/include/sb2000.h +52 -0
  65. package/targets/sb2000/lib/crt0.o +0 -0
  66. package/targets/sb2000/lib/sb2000.lib +0 -0
  67. package/targets/sb2000/lib-build.json +8 -0
  68. package/targets/sb2000/link.cfg +35 -0
  69. package/targets/sb2000/src/crt0.s +37 -0
  70. package/targets/sb2000/src/sb2000.s +67 -0
  71. package/targets/sb2000/target.json +28 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,82 @@
1
- # Temporary Holding Version
1
+ # fcdev
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 6502 兼容机(NES、小霸王学习机等)的开发工具:用 cc65 把 C 或 ca65 汇编构建成机型对应的程序,带调试用模拟器,并能把已有程序反汇编回 ca65 源码。首批支持 NES 与小霸王学习机 SB2000,更多机型按“目标”扩展。
4
+
5
+ 通常不需要手动安装:用 `npm create fcdev@latest` 创建项目时会自动装好。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm install --save-dev fcdev
11
+ ```
12
+
13
+ 要求 Node.js 22 及以上。cc65 编译器与模拟器是分平台的依赖包,npm 会按系统自动安装;目前 cc65 只提供 Windows x64 的包,其他系统可用环境变量 `FCDEV_CC65_HOME` 指向自备的 cc65 目录。模拟器只提供 Windows x64 的包,其他系统可用环境变量 `FCDEV_EMULATOR` 指向自备的模拟器,但调试命令需要该模拟器支持同样的调试服务。
14
+
15
+ ## 使用
16
+
17
+ ```bash
18
+ fcdev build # 构建当前目录;目录里的全部 .c、.s、.asm 链接成一个程序
19
+ fcdev build src # 构建目录 src
20
+ fcdev build src/a.c # 只构建这个文件,它自己是一个程序
21
+ fcdev run src # 构建后在模拟器里运行
22
+ fcdev disk src # 构建后把程序放进磁盘镜像(只有声明了磁盘能力的目标)
23
+ fcdev disasm game.nes # 反汇编成 ca65 源码,并回读校验
24
+ fcdev sync # 生成 IDE 的 C/C++ 配置
25
+ fcdev info --json # 显示目标、工具链与头文件目录
26
+ ```
27
+
28
+ 入口是 `main`(汇编程序写 `.export _main`)。产物放在项目根的 `build/<目标>/`,中间文件在 `build/obj/`。
29
+
30
+ ### 调试
31
+
32
+ ```bash
33
+ fcdev run --debug --port 5688 src # 以调试模式启动模拟器,只在收到 run 命令时运行
34
+ FCDEV_DEBUG_PORT=5688 fcdev debug run 60
35
+ FCDEV_DEBUG_PORT=5688 fcdev debug shot screen.png
36
+ ```
37
+
38
+ `debug` 把命令原样转发给模拟器的调试服务,命令表见模拟器的说明文档。调试端口默认 5699。
39
+
40
+ ### 反汇编
41
+
42
+ ```bash
43
+ fcdev disasm game.nes # NES(NROM),入口取自中断向量
44
+ fcdev disasm prog.bin --base 8000 --entry 8008 # 平铺二进制:加载地址与入口
45
+ fcdev disasm game.nes --cdl game.cdl --symbols sym.txt # 加 CDL 与符号文件
46
+ ```
47
+
48
+ 用 `da65` 生成源码,再用 `ca65`、`ld65` 重新汇编,与原映像逐字节比较,不一致就报错。只追得到的代码才会按指令输出,其余按数据输出;用 `--cdl`(FCEUX、Mesen 格式)或 `--trace`(模拟器 `trace` 命令的日志)补充运行时才执行到的代码。
49
+
50
+ ## 配置
51
+
52
+ 项目根的 `fcdev.json`,未知的配置项会报错:
53
+
54
+ | 项 | 默认值 | 说明 |
55
+ | --- | --- | --- |
56
+ | `target` | 无,必填 | 目标名:`nes` 或 `sb2000` |
57
+ | `name` | 目录名 | 目录构建时的程序名;SB2000 要符合 DOS 8.3 规则 |
58
+ | `out` | `build` | 输出目录 |
59
+ | `optimize` | `true` | 给 cc65 加优化选项 |
60
+ | `define` | 空 | 预定义宏,值为字符串或数字 |
61
+ | `include` | 空 | 额外的头文件目录 |
62
+ | `toolchain` | 空 | 自备 cc65 的根目录,优先于平台包 |
63
+ | `firmware` | 无 | 固件(BIOS)文件;SB2000 运行时必填,fcdev 不提供任何 BIOS 或 ROM |
64
+ | `emulator` | 空 | 自备模拟器的路径,优先于平台包 |
65
+ | `targetOptions` | 空 | 目标专用选项,如 NES 的 `font`(是否链接 ASCII 字库,默认 `true`) |
66
+
67
+ ## 目标
68
+
69
+ | 目标 | 产物 | 头文件 | 说明 |
70
+ | --- | --- | --- | --- |
71
+ | `nes` | `.nes` | `nes.h`(cc65)、`fcnes.h` | NROM,16K PRG 加 8K CHR;`fcnes.h` 提供调色板、文字显示与等待垂直消隐 |
72
+ | `sb2000` | `.EXE` | `sb2000.h` | 程序从 `$8000` 起装入,最大 16K;C 栈在窗口顶端;`sb_puts`、`sb_getkey` 等见头文件 |
73
+
74
+ ## 限制
75
+
76
+ - 反汇编目前只支持 NROM 与平铺二进制;静态追踪追不到 RTS 跳转表等间接调用的代码,这部分按数据输出,回读仍然一致。
77
+ - SB2000 程序的退出码被忽略,`main` 返回后总是回到 DOS。
78
+ - `run` 在非调试模式下沿用模拟器的单实例规则:已有普通的模拟器窗口时,会把程序交给它打开。
79
+
80
+ ## 许可证
81
+
82
+ Apache-2.0。cc65 部分为 zlib 许可,模拟器为 GPL-3.0,各自随平台包附带许可文件。
package/bin/fcdev.mjs ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+ // ------------------------------------------------------------------------
3
+ // 名称:fcdev.mjs
4
+ // 说明:fcdev 命令入口:把终端环境交给 cli,并以其返回值作为退出码。
5
+ // 作者:sungbin
6
+ // 邮箱:sungbin@3578.cn
7
+ // 日期:2026-11-10
8
+ // 备注:只经 src/cli/index.mjs 进入,不直接引用其他目录。
9
+ // ------------------------------------------------------------------------
10
+ import { runCli } from '../src/cli/index.mjs';
11
+
12
+ process.exitCode = await runCli(process.argv.slice(2), {
13
+ cwd: process.cwd(),
14
+ env: process.env,
15
+ stdout: process.stdout,
16
+ stderr: process.stderr,
17
+ });
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "fcdev",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "6502 兼容机(NES、学习机等)的开发工具:用 cc65 构建 C 与汇编程序",
5
+ "license": "Apache-2.0",
6
+ "author": "sungbin <sungbin@3578.cn>",
7
+ "keywords": [
8
+ "6502",
9
+ "nes",
10
+ "famicom",
11
+ "cc65",
12
+ "ca65",
13
+ "sb2000",
14
+ "disassembler"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://gitee.com/sungbin/fcdev.git",
19
+ "directory": "packages/fcdev"
20
+ },
21
+ "type": "module",
22
+ "engines": {
23
+ "node": ">=22"
24
+ },
25
+ "bin": {
26
+ "fcdev": "bin/fcdev.mjs"
27
+ },
28
+ "scripts": {
29
+ "prepack": "node ../../tools/build-libs.mjs"
30
+ },
31
+ "files": [
32
+ "bin",
33
+ "src",
34
+ "targets"
35
+ ],
36
+ "optionalDependencies": {
37
+ "@rujianjs/fcdev-cc65-win32-x64": "0.1.0",
38
+ "@rujianjs/fcdev-emulator-win32-x64": "0.1.0"
39
+ }
40
+ }
@@ -0,0 +1,92 @@
1
+ // ------------------------------------------------------------------------
2
+ // 名称:build-plan.mjs
3
+ // 说明:把命令行的构建路径解析成构建计划:确定项目配置、目标、源文件、程序名与输出位置。
4
+ // 作者:sungbin
5
+ // 邮箱:sungbin@3578.cn
6
+ // 日期:2026-11-10
7
+ // 备注:规则见 docs/02-命令行与构建.md 第 2 节:无参数为当前目录;目录整体构成一个程序;文件单独构成一个程序。
8
+ // ------------------------------------------------------------------------
9
+ import { statSync } from 'node:fs';
10
+ import path from 'node:path';
11
+ import { loadProjectConfig } from '../config/index.mjs';
12
+ import { FcdevError } from '../shared/index.mjs';
13
+ import { applyNameRule, loadTarget, resolveTargetOptions } from '../targets/index.mjs';
14
+ import { collectSources, getSourceLanguage } from './source-collector.mjs';
15
+
16
+ /**
17
+ * @typedef {object} BuildPlan
18
+ * @property {'file'|'directory'} mode 构建单位
19
+ * @property {string} sourceBase 源文件的公共根目录,用于生成中间文件名
20
+ * @property {string[]} sources 源文件的绝对路径
21
+ * @property {string} programName 程序名(中间文件目录用)
22
+ * @property {string} outputFile 最终产物的绝对路径
23
+ * @property {string} objectDir 中间文件目录
24
+ * @property {import('../targets/index.mjs').Target} target 目标
25
+ * @property {Record<string, unknown>} targetOptions 合并后的目标专用选项
26
+ * @property {import('../config/index.mjs').ProjectConfig} config 项目配置
27
+ */
28
+
29
+ /**
30
+ * 读取路径信息,不存在时给出明确的错误。
31
+ * @param {string} target 绝对路径
32
+ * @returns {import('node:fs').Stats} 路径信息
33
+ */
34
+ function statOrFail(target) {
35
+ try {
36
+ return statSync(target);
37
+ } catch (error) {
38
+ if (error.code === 'ENOENT') throw new FcdevError(`找不到 ${target}`, { code: 'PATH_NOT_FOUND' });
39
+ throw error;
40
+ }
41
+ }
42
+
43
+ /**
44
+ * 解析构建路径,生成构建计划。
45
+ * @param {{pathArg?: string, cwd: string, targetOverride?: string, targetsDir?: string}} input pathArg 为命令行路径,缺省为当前目录;cwd 为终端当前目录
46
+ * @returns {BuildPlan} 构建计划
47
+ * @throws {FcdevError} 路径不存在、没有指定目标、文件不是源文件等
48
+ */
49
+ export function createBuildPlan({ pathArg = '.', cwd, targetOverride, targetsDir }) {
50
+ const absolute = path.resolve(cwd, pathArg);
51
+ const info = statOrFail(absolute);
52
+ const config = loadProjectConfig(info.isDirectory() ? absolute : path.dirname(absolute), cwd);
53
+
54
+ const targetName = targetOverride ?? config.target;
55
+ if (!targetName) {
56
+ throw new FcdevError('没有指定目标', { code: 'TARGET_MISSING', hint: '在 fcdev.json 里写 "target",或用 --target <名> 指定' });
57
+ }
58
+ const target = loadTarget(targetName, targetsDir);
59
+ const targetOptions = resolveTargetOptions(target, config.targetOptions);
60
+
61
+ let mode;
62
+ let sourceBase;
63
+ let sources;
64
+ let programName;
65
+ if (info.isDirectory()) {
66
+ mode = 'directory';
67
+ sourceBase = absolute;
68
+ sources = collectSources(absolute, [config.outDir]);
69
+ programName = config.name ?? path.basename(absolute);
70
+ } else {
71
+ if (!getSourceLanguage(absolute)) {
72
+ throw new FcdevError(`${absolute} 不是 .c、.s 或 .asm 源文件`, { code: 'NOT_A_SOURCE' });
73
+ }
74
+ mode = 'file';
75
+ sourceBase = path.dirname(absolute);
76
+ sources = [absolute];
77
+ programName = path.basename(absolute, path.extname(absolute));
78
+ }
79
+
80
+ const outputName = applyNameRule(programName, target.nameRule);
81
+ return {
82
+ mode,
83
+ sourceBase,
84
+ sources,
85
+ programName,
86
+ outputFile: path.join(config.outDir, target.name, `${outputName}${target.extension}`),
87
+ objectDir: path.join(config.outDir, 'obj', target.name, programName),
88
+ target,
89
+ targetOptions,
90
+ config,
91
+ };
92
+ }
@@ -0,0 +1,24 @@
1
+ // ------------------------------------------------------------------------
2
+ // 名称:dependency-file.mjs
3
+ // 说明:解析 cc65、ca65 用 --create-dep 生成的依赖文件(make 格式)。
4
+ // 作者:sungbin
5
+ // 邮箱:sungbin@3578.cn
6
+ // 日期:2026-11-10
7
+ // 备注:路径里的空格用反斜杠转义;Windows 路径里的反斜杠分隔符保持原样。
8
+ // ------------------------------------------------------------------------
9
+
10
+ /**
11
+ * 取出依赖文件里第一条规则的依赖项。
12
+ * @param {string} text 依赖文件内容
13
+ * @returns {string[]} 依赖文件路径;没有规则时为空数组
14
+ */
15
+ export function parseDependencyFile(text) {
16
+ const joined = text.replace(/\\\r?\n/g, ' ');
17
+ const firstRule = joined.split(/\r?\n/).find((line) => line.trim() !== '');
18
+ if (!firstRule) return [];
19
+ // 规则的冒号后面一定是空白;盘符的冒号后面是路径分隔符,不会被误判
20
+ const separator = firstRule.search(/:(\s|$)/);
21
+ if (separator < 0) return [];
22
+ const dependencies = firstRule.slice(separator + 1).match(/(?:\\ |\S)+/g) ?? [];
23
+ return dependencies.map((item) => item.replaceAll('\\ ', ' '));
24
+ }
@@ -0,0 +1,36 @@
1
+ // ------------------------------------------------------------------------
2
+ // 名称:disk-builder.mjs
3
+ // 说明:把构建出的程序放进磁盘镜像:按目标声明的磁盘打包器生成镜像文件。
4
+ // 作者:sungbin
5
+ // 邮箱:sungbin@3578.cn
6
+ // 日期:2026-11-10
7
+ // 备注:镜像里只放这一次构建的程序;镜像放在程序产物旁边,文件名与程序名相同。
8
+ // ------------------------------------------------------------------------
9
+ import { readFileSync, statSync } from 'node:fs';
10
+ import path from 'node:path';
11
+ import { getDiskPackager } from '../packagers/index.mjs';
12
+ import { FcdevError } from '../shared/index.mjs';
13
+
14
+ /** 镜像卷标 */
15
+ const DISK_LABEL = 'FCDEV';
16
+
17
+ /**
18
+ * 生成包含这次构建的程序的磁盘镜像。
19
+ * @param {import('./build-plan.mjs').BuildPlan} plan 构建计划
20
+ * @param {import('./program-builder.mjs').BuildResult} result 构建结果
21
+ * @returns {{imageFile: string, size: number}} 镜像路径与大小
22
+ * @throws {FcdevError} 目标不支持磁盘镜像,或程序名不符合镜像文件名规则
23
+ */
24
+ export function buildDiskImage(plan, result) {
25
+ if (!plan.target.disk) {
26
+ throw new FcdevError(`目标 ${plan.target.name} 不支持磁盘镜像`, { code: 'TARGET_NO_DISK' });
27
+ }
28
+ const outputName = path.basename(result.outputFile);
29
+ const imageFile = path.join(path.dirname(result.outputFile), `${path.basename(outputName, path.extname(outputName))}.img`);
30
+ const packaged = getDiskPackager(plan.target.disk.packager)({
31
+ files: [{ name: outputName, data: readFileSync(result.outputFile), mtime: statSync(result.outputFile).mtime }],
32
+ outputFile: imageFile,
33
+ options: { label: DISK_LABEL },
34
+ });
35
+ return { imageFile: packaged.outputFile, size: packaged.size };
36
+ }
@@ -0,0 +1,31 @@
1
+ // ------------------------------------------------------------------------
2
+ // 名称:index.mjs
3
+ // 说明:build 目录的对外入口:解析构建路径并构建程序。
4
+ // 作者:sungbin
5
+ // 邮箱:sungbin@3578.cn
6
+ // 日期:2026-11-10
7
+ // 备注:无
8
+ // ------------------------------------------------------------------------
9
+ import { locateToolchain } from '../toolchain/index.mjs';
10
+ import { createBuildPlan } from './build-plan.mjs';
11
+ import { buildProgram } from './program-builder.mjs';
12
+
13
+ export { createBuildPlan } from './build-plan.mjs';
14
+ export { parseDependencyFile } from './dependency-file.mjs';
15
+ export { buildDiskImage } from './disk-builder.mjs';
16
+ export { buildProgram } from './program-builder.mjs';
17
+ export { collectSources, getSourceLanguage } from './source-collector.mjs';
18
+ export { buildAssemblerArguments, buildCompilerArguments, buildLinkerArguments } from './tool-arguments.mjs';
19
+
20
+ /**
21
+ * 解析路径并构建。
22
+ * @param {{pathArg?: string, cwd: string, targetOverride?: string, targetsDir?: string, env?: NodeJS.ProcessEnv}} input 构建路径、终端目录与目标覆盖
23
+ * @param {import('./program-builder.mjs').Reporter} [reporter] 汇报器
24
+ * @returns {Promise<{plan: import('./build-plan.mjs').BuildPlan, result: import('./program-builder.mjs').BuildResult}>} 计划与结果
25
+ */
26
+ export async function buildPath(input, reporter) {
27
+ const plan = createBuildPlan(input);
28
+ const toolchain = locateToolchain({ configured: plan.config.toolchain, env: input.env });
29
+ const result = await buildProgram(plan, toolchain, reporter);
30
+ return { plan, result };
31
+ }