create-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.
- package/LICENSE +201 -0
- package/README.md +26 -2
- package/bin/create-fcdev.mjs +19 -0
- package/package.json +30 -4
- package/src/args.mjs +57 -0
- package/src/errors.mjs +22 -0
- package/src/index.mjs +142 -0
- package/src/prompts.mjs +55 -0
- package/src/scaffold.mjs +102 -0
- package/src/templates.mjs +72 -0
- package/templates/_common/gitignore +6 -0
- package/templates/nes-asm/README.md +17 -0
- package/templates/nes-asm/src/hello.s +67 -0
- package/templates/nes-asm/template.json +8 -0
- package/templates/nes-c/README.md +17 -0
- package/templates/nes-c/src/hello.c +32 -0
- package/templates/nes-c/template.json +8 -0
- package/templates/sb2000-asm/README.md +33 -0
- package/templates/sb2000-asm/src/hello.s +35 -0
- package/templates/sb2000-asm/template.json +13 -0
- package/templates/sb2000-c/README.md +33 -0
- package/templates/sb2000-c/src/hello.c +23 -0
- package/templates/sb2000-c/template.json +13 -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,27 @@
|
|
|
1
|
-
#
|
|
1
|
+
# create-fcdev
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
创建 fcdev 项目:在空目录生成 6502 兼容机程序的开发结构与示例程序。
|
|
4
|
+
|
|
5
|
+
## 使用
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm create fcdev@latest my-game
|
|
9
|
+
npm create fcdev@latest . -- --target sb2000 --template c
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
| 选项 | 说明 |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| `[目录]` | 默认当前目录,必须为空(允许已有 `.git`);不存在时会创建 |
|
|
15
|
+
| `--target <名>` | 目标:`nes` 或 `sb2000`;缺省时询问 |
|
|
16
|
+
| `--template <语言>` | `c` 或 `asm`;缺省时询问 |
|
|
17
|
+
| `--no-install` | 不运行 `npm install` 与 `npm run sync` |
|
|
18
|
+
|
|
19
|
+
不能交互(例如在脚本里)时必须同时给出 `--target` 与 `--template`。
|
|
20
|
+
|
|
21
|
+
生成的项目包含 `src/hello.c`(或 `hello.s`)、`fcdev.json`、`package.json`、`README.md` 和 `.gitignore`,依赖与本包同版本的 [fcdev](https://www.npmjs.com/package/fcdev)。之后用 `npm run build` 构建,`npm start` 在模拟器里运行。
|
|
22
|
+
|
|
23
|
+
模板内置在本包里,不会从网络下载;已有文件不会被覆盖。
|
|
24
|
+
|
|
25
|
+
## 许可证
|
|
26
|
+
|
|
27
|
+
Apache-2.0
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// ------------------------------------------------------------------------
|
|
3
|
+
// 名称:create-fcdev.mjs
|
|
4
|
+
// 说明:npm create fcdev 的命令入口:把终端环境交给创建流程,并以其返回值作为退出码。
|
|
5
|
+
// 作者:sungbin
|
|
6
|
+
// 邮箱:sungbin@3578.cn
|
|
7
|
+
// 日期:2026-11-10
|
|
8
|
+
// 备注:只经 src/index.mjs 进入。
|
|
9
|
+
// ------------------------------------------------------------------------
|
|
10
|
+
import { runCommandInTerminal, runCreate } from '../src/index.mjs';
|
|
11
|
+
|
|
12
|
+
process.exitCode = await runCreate(process.argv.slice(2), {
|
|
13
|
+
cwd: process.cwd(),
|
|
14
|
+
stdin: process.stdin,
|
|
15
|
+
stdout: process.stdout,
|
|
16
|
+
stderr: process.stderr,
|
|
17
|
+
interactive: Boolean(process.stdin.isTTY && process.stdout.isTTY),
|
|
18
|
+
runCommand: runCommandInTerminal,
|
|
19
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-fcdev",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "创建 fcdev 项目:npm create fcdev,在空目录生成 6502 兼容机程序的开发结构与示例",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "sungbin <sungbin@3578.cn>",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"6502",
|
|
9
|
+
"nes",
|
|
10
|
+
"famicom",
|
|
11
|
+
"cc65",
|
|
12
|
+
"sb2000",
|
|
13
|
+
"create"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://gitee.com/sungbin/fcdev.git",
|
|
18
|
+
"directory": "packages/create-fcdev"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22"
|
|
23
|
+
},
|
|
24
|
+
"bin": {
|
|
25
|
+
"create-fcdev": "bin/create-fcdev.mjs"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"bin",
|
|
29
|
+
"src",
|
|
30
|
+
"templates"
|
|
31
|
+
]
|
|
32
|
+
}
|
package/src/args.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:args.mjs
|
|
3
|
+
// 说明:解析 create-fcdev 的命令行参数:目录、--target、--template、--no-install。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:无
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import { CreateError } from './errors.mjs';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} CreateArguments
|
|
13
|
+
* @property {string|undefined} directory 目录,缺省为当前目录
|
|
14
|
+
* @property {string|undefined} target 目标名
|
|
15
|
+
* @property {string|undefined} language 语言
|
|
16
|
+
* @property {boolean} install 是否安装依赖并生成 IDE 配置
|
|
17
|
+
* @property {boolean} help 是否显示帮助
|
|
18
|
+
* @property {boolean} version 是否显示版本
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 解析命令行参数。
|
|
23
|
+
* @param {string[]} argv 去掉 node 与脚本路径后的参数
|
|
24
|
+
* @returns {CreateArguments} 解析结果
|
|
25
|
+
* @throws {CreateError} 未知的选项、选项缺少值或位置参数多于一个
|
|
26
|
+
*/
|
|
27
|
+
export function parseCreateArguments(argv) {
|
|
28
|
+
const result = { directory: undefined, target: undefined, language: undefined, install: true, help: false, version: false };
|
|
29
|
+
const readValue = (token, name, index) => {
|
|
30
|
+
const value = token.includes('=') ? token.slice(token.indexOf('=') + 1) : argv[index + 1];
|
|
31
|
+
if (!value || value.startsWith('-')) throw new CreateError(`${name} 缺少值`, { code: 'ARGUMENT_INVALID' });
|
|
32
|
+
return value;
|
|
33
|
+
};
|
|
34
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
35
|
+
const token = argv[index];
|
|
36
|
+
if (token === '--help' || token === '-h') {
|
|
37
|
+
result.help = true;
|
|
38
|
+
} else if (token === '--version' || token === '-v') {
|
|
39
|
+
result.version = true;
|
|
40
|
+
} else if (token === '--no-install') {
|
|
41
|
+
result.install = false;
|
|
42
|
+
} else if (token === '--target' || token.startsWith('--target=')) {
|
|
43
|
+
result.target = readValue(token, '--target', index);
|
|
44
|
+
if (!token.includes('=')) index += 1;
|
|
45
|
+
} else if (token === '--template' || token.startsWith('--template=')) {
|
|
46
|
+
result.language = readValue(token, '--template', index);
|
|
47
|
+
if (!token.includes('=')) index += 1;
|
|
48
|
+
} else if (token.startsWith('-')) {
|
|
49
|
+
throw new CreateError(`未知的选项:${token}`, { code: 'ARGUMENT_INVALID', hint: '用 --help 查看可用的选项' });
|
|
50
|
+
} else if (result.directory === undefined) {
|
|
51
|
+
result.directory = token;
|
|
52
|
+
} else {
|
|
53
|
+
throw new CreateError('只能指定一个目录', { code: 'ARGUMENT_INVALID' });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
package/src/errors.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:errors.mjs
|
|
3
|
+
// 说明:create-fcdev 的统一错误类型:带错误码与下一步建议。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:创建包是独立发布的,不引用 fcdev 主包,所以有自己的错误类型。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
/** 可预期的使用错误(参数有误、目录不为空、没有这个模板等) */
|
|
11
|
+
export class CreateError extends Error {
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} message 说明发生了什么
|
|
14
|
+
* @param {{code?: string, hint?: string, cause?: unknown}} [options] code 为错误码;hint 为下一步怎么做;cause 为原始原因
|
|
15
|
+
*/
|
|
16
|
+
constructor(message, { code = 'CREATE_ERROR', hint = '', cause } = {}) {
|
|
17
|
+
super(message, cause === undefined ? undefined : { cause });
|
|
18
|
+
this.name = 'CreateError';
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.hint = hint;
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:index.mjs
|
|
3
|
+
// 说明:create-fcdev 的主流程:解析参数,选择目标与语言,生成项目,安装依赖并生成 IDE 配置,打印下一步。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:runCreate 不直接读写进程状态,终端目录、输入输出流与外部命令都由调用者传入,便于测试。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { parseCreateArguments } from './args.mjs';
|
|
12
|
+
import { CreateError } from './errors.mjs';
|
|
13
|
+
import { createPrompter } from './prompts.mjs';
|
|
14
|
+
import { readOwnVersion, scaffoldProject } from './scaffold.mjs';
|
|
15
|
+
import { LANGUAGES, findTemplate, listTemplates } from './templates.mjs';
|
|
16
|
+
|
|
17
|
+
export { CreateError } from './errors.mjs';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {object} CreateContext
|
|
21
|
+
* @property {string} cwd 终端当前目录
|
|
22
|
+
* @property {NodeJS.ReadableStream} stdin 标准输入
|
|
23
|
+
* @property {{write: (text: string) => unknown}} stdout 标准输出
|
|
24
|
+
* @property {{write: (text: string) => unknown}} stderr 标准错误
|
|
25
|
+
* @property {boolean} interactive 是否可以交互询问
|
|
26
|
+
* @property {(command: string, args: string[], cwd: string) => Promise<number>} runCommand 运行外部命令,返回退出码
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** 帮助文字 */
|
|
30
|
+
const HELP_TEXT = `create-fcdev:创建 6502 兼容机程序的项目
|
|
31
|
+
|
|
32
|
+
用法:npm create fcdev@latest [目录] [-- 选项]
|
|
33
|
+
|
|
34
|
+
目录 默认当前目录(必须为空);不存在时会创建
|
|
35
|
+
选项:
|
|
36
|
+
--target <名> 目标,如 nes、sb2000;缺省时询问
|
|
37
|
+
--template <语言> c 或 asm;缺省时询问
|
|
38
|
+
--no-install 不运行 npm install 与 npm run sync
|
|
39
|
+
-h, --help 显示帮助
|
|
40
|
+
-v, --version 显示版本
|
|
41
|
+
`;
|
|
42
|
+
|
|
43
|
+
/** 语言的显示名 */
|
|
44
|
+
const LANGUAGE_LABELS = { c: 'C 语言(cc65)', asm: '汇编(ca65)' };
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 运行外部命令并继承终端输出。
|
|
48
|
+
* @param {string} command 命令名
|
|
49
|
+
* @param {string[]} args 参数(固定值,不含用户输入)
|
|
50
|
+
* @param {string} cwd 工作目录
|
|
51
|
+
* @returns {Promise<number>} 退出码
|
|
52
|
+
*/
|
|
53
|
+
export function runCommandInTerminal(command, args, cwd) {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
// Windows 上 npm 是 .cmd 文件,必须经 shell 启动;参数都是固定值
|
|
56
|
+
const child = spawn(command, args, { cwd, stdio: 'inherit', shell: process.platform === 'win32' });
|
|
57
|
+
child.once('error', reject);
|
|
58
|
+
child.once('close', (code) => resolve(code ?? 1));
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 确定目标与语言:命令行给了就用,没给就询问。
|
|
64
|
+
* @param {import('./args.mjs').CreateArguments} args 命令行参数
|
|
65
|
+
* @param {CreateContext} context 运行环境
|
|
66
|
+
* @returns {Promise<{target: string, language: string}>} 目标与语言
|
|
67
|
+
* @throws {CreateError} 缺少选项又不能交互
|
|
68
|
+
*/
|
|
69
|
+
async function chooseTemplate(args, context) {
|
|
70
|
+
const templates = listTemplates();
|
|
71
|
+
let { target, language } = args;
|
|
72
|
+
if (target === undefined || language === undefined) {
|
|
73
|
+
if (!context.interactive) {
|
|
74
|
+
throw new CreateError('没有指定目标或语言,且当前不能交互', { code: 'ARGUMENT_MISSING', hint: '加上 --target <名> 和 --template <c|asm>' });
|
|
75
|
+
}
|
|
76
|
+
const prompter = createPrompter({ input: context.stdin, output: context.stdout });
|
|
77
|
+
try {
|
|
78
|
+
if (target === undefined) {
|
|
79
|
+
const seen = new Map(templates.map((item) => [item.target, item.title]));
|
|
80
|
+
target = await prompter.askChoice({
|
|
81
|
+
question: '选择目标:',
|
|
82
|
+
choices: [...seen].map(([value, title]) => ({ value, label: `${value} ${title}` })),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (language === undefined) {
|
|
86
|
+
language = await prompter.askChoice({
|
|
87
|
+
question: '选择语言:',
|
|
88
|
+
choices: LANGUAGES.map((value) => ({ value, label: `${value} ${LANGUAGE_LABELS[value]}` })),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
} finally {
|
|
92
|
+
prompter.close();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return { target, language };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 运行创建流程。
|
|
100
|
+
* @param {string[]} argv 去掉 node 与脚本路径后的参数
|
|
101
|
+
* @param {CreateContext} context 运行环境
|
|
102
|
+
* @returns {Promise<number>} 进程退出码
|
|
103
|
+
*/
|
|
104
|
+
export async function runCreate(argv, context) {
|
|
105
|
+
try {
|
|
106
|
+
const args = parseCreateArguments(argv);
|
|
107
|
+
if (args.version) {
|
|
108
|
+
context.stdout.write(`${readOwnVersion()}\n`);
|
|
109
|
+
return 0;
|
|
110
|
+
}
|
|
111
|
+
if (args.help) {
|
|
112
|
+
context.stdout.write(HELP_TEXT);
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const { target, language } = await chooseTemplate(args, context);
|
|
117
|
+
const template = findTemplate(target, language);
|
|
118
|
+
const directory = path.resolve(context.cwd, args.directory ?? '.');
|
|
119
|
+
const { name, files } = scaffoldProject({ directory, template });
|
|
120
|
+
context.stdout.write(`已在 ${directory} 创建项目 ${name}(模板 ${template.name}),共 ${files.length} 个文件\n`);
|
|
121
|
+
|
|
122
|
+
if (args.install) {
|
|
123
|
+
context.stdout.write('安装依赖……\n');
|
|
124
|
+
if ((await context.runCommand('npm', ['install'], directory)) !== 0) {
|
|
125
|
+
throw new CreateError('npm install 失败', { code: 'INSTALL_FAILED', hint: `项目文件已生成;解决网络或权限问题后,在 ${directory} 里重新运行 npm install` });
|
|
126
|
+
}
|
|
127
|
+
if ((await context.runCommand('npm', ['run', 'sync'], directory)) !== 0) {
|
|
128
|
+
throw new CreateError('生成 IDE 配置失败', { code: 'SYNC_FAILED', hint: `在 ${directory} 里运行 npm run sync 查看原因` });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const relative = path.relative(context.cwd, directory);
|
|
133
|
+
context.stdout.write(`\n下一步:\n${relative === '' ? '' : ` cd ${relative}\n`}${args.install ? '' : ' npm install\n'} npm run build\n`);
|
|
134
|
+
for (const note of template.notes) context.stdout.write(`${note}\n`);
|
|
135
|
+
return 0;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (!(error instanceof CreateError)) throw error;
|
|
138
|
+
context.stderr.write(`错误:${error.message}\n`);
|
|
139
|
+
if (error.hint) context.stderr.write(`${error.hint}\n`);
|
|
140
|
+
return 1;
|
|
141
|
+
}
|
|
142
|
+
}
|
package/src/prompts.mjs
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:prompts.mjs
|
|
3
|
+
// 说明:命令行交互:让用户在几个选项里选一个,输入序号或名称都可以。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:用 node:readline,不引入依赖;整个创建流程共用一个输入迭代器,避免多个问题争抢同一个输入流;输入不合法时重新询问,最多 3 次。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import { createInterface } from 'node:readline';
|
|
10
|
+
import { CreateError } from './errors.mjs';
|
|
11
|
+
|
|
12
|
+
/** 同一个问题最多询问的次数 */
|
|
13
|
+
const MAX_ATTEMPTS = 3;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 创建提问器:从输入流逐行读取回答。
|
|
17
|
+
* @param {{input: NodeJS.ReadableStream, output: {write: (text: string) => unknown}}} streams 输入流与输出流
|
|
18
|
+
* @returns {{askChoice: (request: {question: string, choices: {value: string, label: string}[]}) => Promise<string>, close: () => void}} 提问器;用完要 close
|
|
19
|
+
*/
|
|
20
|
+
export function createPrompter({ input, output }) {
|
|
21
|
+
const readline = createInterface({ input });
|
|
22
|
+
const lines = readline[Symbol.asyncIterator]();
|
|
23
|
+
|
|
24
|
+
/** 读下一行回答,输入结束时报错 */
|
|
25
|
+
async function readAnswer() {
|
|
26
|
+
output.write('请输入序号或名称:');
|
|
27
|
+
const { value, done } = await lines.next();
|
|
28
|
+
if (done) throw new CreateError('没有收到输入', { code: 'PROMPT_CLOSED' });
|
|
29
|
+
return value.trim();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
/**
|
|
34
|
+
* 询问用户在若干选项里选一个。
|
|
35
|
+
* @param {{question: string, choices: {value: string, label: string}[]}} request 问题与选项
|
|
36
|
+
* @returns {Promise<string>} 选中的 value
|
|
37
|
+
* @throws {CreateError} 连续输入不合法,或输入流结束
|
|
38
|
+
*/
|
|
39
|
+
async askChoice({ question, choices }) {
|
|
40
|
+
const list = choices.map((choice, index) => ` ${index + 1}) ${choice.label}`).join('\n');
|
|
41
|
+
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt += 1) {
|
|
42
|
+
output.write(`${question}\n${list}\n`);
|
|
43
|
+
const answer = await readAnswer();
|
|
44
|
+
const byNumber = Number(answer);
|
|
45
|
+
const chosen = Number.isInteger(byNumber) && byNumber >= 1 && byNumber <= choices.length ? choices[byNumber - 1] : choices.find((choice) => choice.value === answer);
|
|
46
|
+
if (chosen) return chosen.value;
|
|
47
|
+
output.write(`“${answer}”不是可选的项\n`);
|
|
48
|
+
}
|
|
49
|
+
throw new CreateError(`连续 ${MAX_ATTEMPTS} 次输入不合法`, { code: 'PROMPT_INVALID' });
|
|
50
|
+
},
|
|
51
|
+
close() {
|
|
52
|
+
readline.close();
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
package/src/scaffold.mjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:scaffold.mjs
|
|
3
|
+
// 说明:生成项目:检查目标目录为空,复制模板与公共文件,写 package.json 与 fcdev.json。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:模板在创建包内,不从网络下载;已有文件绝不覆盖,目录不为空时直接报错。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
import { CreateError } from './errors.mjs';
|
|
13
|
+
import { TEMPLATES_DIR } from './templates.mjs';
|
|
14
|
+
|
|
15
|
+
/** 目录里允许已经存在的内容 */
|
|
16
|
+
const ALLOWED_EXISTING = new Set(['.git']);
|
|
17
|
+
|
|
18
|
+
/** 模板里不复制到项目的文件 */
|
|
19
|
+
const TEMPLATE_ONLY_FILES = new Set(['template.json']);
|
|
20
|
+
|
|
21
|
+
/** 公共文件在模板目录里的名字与在项目里的名字:npm 发布时会丢掉 .gitignore,所以模板里不带点 */
|
|
22
|
+
const COMMON_FILES = [['gitignore', '.gitignore']];
|
|
23
|
+
|
|
24
|
+
/** 项目里 hello 程序的名字,同时是 fcdev.json 的 name,也是 DOS 里要输入的命令 */
|
|
25
|
+
export const PROGRAM_NAME = 'hello';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 由目录名得到 npm 包名:小写,非法字符换成连字符。
|
|
29
|
+
* @param {string} directoryName 目录名
|
|
30
|
+
* @returns {string} 包名;目录名里没有可用字符时为 fcdev-app
|
|
31
|
+
*/
|
|
32
|
+
export function toPackageName(directoryName) {
|
|
33
|
+
const cleaned = directoryName.toLowerCase().replace(/[^a-z0-9._-]+/g, '-').replace(/^[._-]+/, '').replace(/-+$/, '');
|
|
34
|
+
return cleaned === '' ? 'fcdev-app' : cleaned;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 读取创建包自己的版本号,生成的项目依赖同一个版本的 fcdev。
|
|
39
|
+
* @returns {string} 版本号
|
|
40
|
+
*/
|
|
41
|
+
export function readOwnVersion() {
|
|
42
|
+
return JSON.parse(readFileSync(fileURLToPath(new URL('../package.json', import.meta.url)), 'utf8')).version;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 确认目标目录可以用来创建项目:不存在,或为空(允许已有 .git)。
|
|
47
|
+
* @param {string} directory 目录的绝对路径
|
|
48
|
+
* @throws {CreateError} 目录里已有其他内容
|
|
49
|
+
*/
|
|
50
|
+
export function assertDirectoryUsable(directory) {
|
|
51
|
+
if (!existsSync(directory)) return;
|
|
52
|
+
const existing = readdirSync(directory).filter((name) => !ALLOWED_EXISTING.has(name));
|
|
53
|
+
if (existing.length > 0) {
|
|
54
|
+
throw new CreateError(`目录 ${directory} 不为空`, {
|
|
55
|
+
code: 'DIRECTORY_NOT_EMPTY',
|
|
56
|
+
hint: `已有:${existing.slice(0, 5).join('、')}${existing.length > 5 ? ' 等' : ''};请换一个空目录,或指定一个新目录名`,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 在目录里生成项目。
|
|
63
|
+
* @param {{directory: string, template: import('./templates.mjs').Template, fcdevVersion?: string, templatesDir?: string}} input 目标目录、模板与依赖的 fcdev 版本
|
|
64
|
+
* @returns {{directory: string, name: string, files: string[]}} 项目目录、包名与生成的文件(相对路径)
|
|
65
|
+
* @throws {CreateError} 目录不为空
|
|
66
|
+
*/
|
|
67
|
+
export function scaffoldProject({ directory, template, fcdevVersion = readOwnVersion(), templatesDir = TEMPLATES_DIR }) {
|
|
68
|
+
assertDirectoryUsable(directory);
|
|
69
|
+
mkdirSync(directory, { recursive: true });
|
|
70
|
+
const name = toPackageName(path.basename(directory));
|
|
71
|
+
|
|
72
|
+
cpSync(template.dir, directory, { recursive: true, filter: (source) => !TEMPLATE_ONLY_FILES.has(path.basename(source)) });
|
|
73
|
+
for (const [from, to] of COMMON_FILES) cpSync(path.join(templatesDir, '_common', from), path.join(directory, to));
|
|
74
|
+
|
|
75
|
+
const readme = path.join(directory, 'README.md');
|
|
76
|
+
if (existsSync(readme)) writeFileSync(readme, readFileSync(readme, 'utf8').replaceAll('{{name}}', name));
|
|
77
|
+
|
|
78
|
+
const packageJson = {
|
|
79
|
+
name,
|
|
80
|
+
version: '0.1.0',
|
|
81
|
+
private: true,
|
|
82
|
+
description: `${template.title} 程序`,
|
|
83
|
+
scripts: { build: 'fcdev build src', start: 'fcdev run src', sync: 'fcdev sync', ...template.scripts },
|
|
84
|
+
devDependencies: { fcdev: fcdevVersion },
|
|
85
|
+
};
|
|
86
|
+
writeFileSync(path.join(directory, 'package.json'), `${JSON.stringify(packageJson, null, 2)}\n`);
|
|
87
|
+
writeFileSync(path.join(directory, 'fcdev.json'), `${JSON.stringify({ target: template.target, name: PROGRAM_NAME }, null, 2)}\n`);
|
|
88
|
+
|
|
89
|
+
const files = [];
|
|
90
|
+
const walk = (current) => {
|
|
91
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
92
|
+
const full = path.join(current, entry.name);
|
|
93
|
+
if (entry.isDirectory()) {
|
|
94
|
+
if (entry.name !== '.git') walk(full);
|
|
95
|
+
} else {
|
|
96
|
+
files.push(path.relative(directory, full).split(path.sep).join('/'));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
walk(directory);
|
|
101
|
+
return { directory, name, files: files.sort() };
|
|
102
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// ------------------------------------------------------------------------
|
|
2
|
+
// 名称:templates.mjs
|
|
3
|
+
// 说明:读取项目模板:列出可用的模板,按目标与语言取模板信息。
|
|
4
|
+
// 作者:sungbin
|
|
5
|
+
// 邮箱:sungbin@3578.cn
|
|
6
|
+
// 日期:2026-11-10
|
|
7
|
+
// 备注:每个模板是 templates/<目标>-<语言>/ 目录,含 template.json(目标、语言、说明、额外的 npm 脚本);以 _ 开头的目录是公共文件,不是模板。
|
|
8
|
+
// ------------------------------------------------------------------------
|
|
9
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
import { CreateError } from './errors.mjs';
|
|
13
|
+
|
|
14
|
+
/** 模板所在目录 */
|
|
15
|
+
export const TEMPLATES_DIR = fileURLToPath(new URL('../templates/', import.meta.url));
|
|
16
|
+
|
|
17
|
+
/** 可选的语言 */
|
|
18
|
+
export const LANGUAGES = ['c', 'asm'];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {object} Template
|
|
22
|
+
* @property {string} name 模板名,如 nes-c
|
|
23
|
+
* @property {string} dir 模板目录
|
|
24
|
+
* @property {string} target 目标名
|
|
25
|
+
* @property {string} language 语言:c 或 asm
|
|
26
|
+
* @property {string} title 目标的显示名
|
|
27
|
+
* @property {Record<string, string>} scripts 额外的 npm 脚本
|
|
28
|
+
* @property {string[]} notes 创建完成后提示的补充说明
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 列出全部模板。
|
|
33
|
+
* @param {string} [templatesDir] 模板目录
|
|
34
|
+
* @returns {Template[]} 模板列表,按名称排序
|
|
35
|
+
* @throws {CreateError} 模板定义不合法
|
|
36
|
+
*/
|
|
37
|
+
export function listTemplates(templatesDir = TEMPLATES_DIR) {
|
|
38
|
+
return readdirSync(templatesDir, { withFileTypes: true })
|
|
39
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith('_'))
|
|
40
|
+
.map((entry) => {
|
|
41
|
+
const dir = path.join(templatesDir, entry.name);
|
|
42
|
+
const file = path.join(dir, 'template.json');
|
|
43
|
+
if (!existsSync(file)) throw new CreateError(`模板 ${entry.name} 缺少 template.json`, { code: 'TEMPLATE_INVALID' });
|
|
44
|
+
const raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
45
|
+
if (typeof raw.target !== 'string' || !LANGUAGES.includes(raw.language) || typeof raw.title !== 'string' || entry.name !== `${raw.target}-${raw.language}`) {
|
|
46
|
+
throw new CreateError(`模板 ${entry.name} 的 template.json 不合法`, { code: 'TEMPLATE_INVALID' });
|
|
47
|
+
}
|
|
48
|
+
return { name: entry.name, dir, target: raw.target, language: raw.language, title: raw.title, scripts: raw.scripts ?? {}, notes: raw.notes ?? [] };
|
|
49
|
+
})
|
|
50
|
+
.sort((left, right) => left.name.localeCompare(right.name));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 按目标与语言取模板。
|
|
55
|
+
* @param {string} target 目标名
|
|
56
|
+
* @param {string} language 语言
|
|
57
|
+
* @param {string} [templatesDir] 模板目录
|
|
58
|
+
* @returns {Template} 模板
|
|
59
|
+
* @throws {CreateError} 没有这个目标或语言的模板
|
|
60
|
+
*/
|
|
61
|
+
export function findTemplate(target, language, templatesDir = TEMPLATES_DIR) {
|
|
62
|
+
const templates = listTemplates(templatesDir);
|
|
63
|
+
const found = templates.find((item) => item.target === target && item.language === language);
|
|
64
|
+
if (!found) {
|
|
65
|
+
const targets = [...new Set(templates.map((item) => item.target))];
|
|
66
|
+
throw new CreateError(`没有目标 ${target}、语言 ${language} 的模板`, {
|
|
67
|
+
code: 'TEMPLATE_NOT_FOUND',
|
|
68
|
+
hint: `可用的目标:${targets.join('、')};可用的语言:${LANGUAGES.join('、')}`,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return found;
|
|
72
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# {{name}}
|
|
2
|
+
|
|
3
|
+
用 fcdev 开发的任天堂红白机(NES)程序,ca65 汇编。
|
|
4
|
+
|
|
5
|
+
## 构建与运行
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm run build # 生成 build/nes/hello.nes
|
|
9
|
+
npm start # 在模拟器里运行(只支持 Windows)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 文件
|
|
13
|
+
|
|
14
|
+
| 文件 | 说明 |
|
|
15
|
+
| --- | --- |
|
|
16
|
+
| `src/hello.s` | 示例程序:在屏幕上显示 HELLO, NES!,入口是导出的 `_main` |
|
|
17
|
+
| `fcdev.json` | fcdev 配置:目标 nes,程序名 hello |
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
; ------------------------------------------------------------------------
|
|
2
|
+
; 名称:hello.s
|
|
3
|
+
; 说明:NES 最小汇编程序:设置调色板,在屏幕中间显示 HELLO, NES!。
|
|
4
|
+
; 作者:sungbin
|
|
5
|
+
; 邮箱:sungbin@3578.cn
|
|
6
|
+
; 日期:2026-11-10
|
|
7
|
+
; 备注:入口是导出的 _main,由 cc65 的启动代码调用;PPU 寄存器的符号在 fcnes.inc 里;
|
|
8
|
+
; 文字用 fcdev 附带的 ASCII 字库,图块号等于字符码;写显存前画面要关闭。
|
|
9
|
+
; ------------------------------------------------------------------------
|
|
10
|
+
.include "fcnes.inc"
|
|
11
|
+
.export _main
|
|
12
|
+
|
|
13
|
+
.segment "RODATA"
|
|
14
|
+
|
|
15
|
+
; 32 字节调色板:每组 4 色,颜色 0 是背景(黑),颜色 1 是文字(白)
|
|
16
|
+
palette:
|
|
17
|
+
.byte $0F, $30, $10, $00, $0F, $30, $10, $00
|
|
18
|
+
.byte $0F, $30, $10, $00, $0F, $30, $10, $00
|
|
19
|
+
.byte $0F, $30, $10, $00, $0F, $30, $10, $00
|
|
20
|
+
.byte $0F, $30, $10, $00, $0F, $30, $10, $00
|
|
21
|
+
|
|
22
|
+
message:
|
|
23
|
+
.asciiz "HELLO, NES!"
|
|
24
|
+
|
|
25
|
+
.segment "CODE"
|
|
26
|
+
|
|
27
|
+
_main:
|
|
28
|
+
lda #$00
|
|
29
|
+
sta PPUMASK ; 关闭画面,才能写显存
|
|
30
|
+
|
|
31
|
+
lda PPUSTATUS ; 复位 PPUADDR 的写入顺序
|
|
32
|
+
lda #$3F
|
|
33
|
+
sta PPUADDR
|
|
34
|
+
lda #$00
|
|
35
|
+
sta PPUADDR ; 显存地址 $3F00:调色板
|
|
36
|
+
ldx #0
|
|
37
|
+
@palette:
|
|
38
|
+
lda palette,x
|
|
39
|
+
sta PPUDATA
|
|
40
|
+
inx
|
|
41
|
+
cpx #32
|
|
42
|
+
bne @palette
|
|
43
|
+
|
|
44
|
+
lda #$21
|
|
45
|
+
sta PPUADDR
|
|
46
|
+
lda #$CA
|
|
47
|
+
sta PPUADDR ; 名称表 (10, 14):$2000 + 14*32 + 10 = $21CA
|
|
48
|
+
ldx #0
|
|
49
|
+
@text: lda message,x
|
|
50
|
+
beq @done
|
|
51
|
+
sta PPUDATA
|
|
52
|
+
inx
|
|
53
|
+
bne @text
|
|
54
|
+
@done:
|
|
55
|
+
@vblank:
|
|
56
|
+
bit PPUSTATUS ; 等待垂直消隐,再打开画面
|
|
57
|
+
bpl @vblank
|
|
58
|
+
|
|
59
|
+
lda #$00
|
|
60
|
+
sta PPUSCROLL
|
|
61
|
+
sta PPUSCROLL ; 滚动复位到 (0, 0)
|
|
62
|
+
sta PPUCTRL
|
|
63
|
+
lda #$1E
|
|
64
|
+
sta PPUMASK ; 显示背景与精灵
|
|
65
|
+
|
|
66
|
+
@forever:
|
|
67
|
+
jmp @forever ; NES 程序没有“返回”,主循环一直运行
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# {{name}}
|
|
2
|
+
|
|
3
|
+
用 fcdev 开发的任天堂红白机(NES)程序,C 语言。
|
|
4
|
+
|
|
5
|
+
## 构建与运行
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm run build # 生成 build/nes/hello.nes
|
|
9
|
+
npm start # 在模拟器里运行(只支持 Windows)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 文件
|
|
13
|
+
|
|
14
|
+
| 文件 | 说明 |
|
|
15
|
+
| --- | --- |
|
|
16
|
+
| `src/hello.c` | 示例程序:在屏幕上显示 HELLO, NES! |
|
|
17
|
+
| `fcdev.json` | fcdev 配置:目标 nes,程序名 hello |
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* ------------------------------------------------------------------------
|
|
3
|
+
* 名称:hello.c
|
|
4
|
+
* 说明:NES 最小程序:设置调色板,在屏幕中间显示 HELLO, NES!。
|
|
5
|
+
* 作者:sungbin
|
|
6
|
+
* 邮箱:sungbin@3578.cn
|
|
7
|
+
* 日期:2026-11-10
|
|
8
|
+
* 备注:文字用 fcdev 附带的 ASCII 字库;写显存前要先关闭画面,写完再打开。
|
|
9
|
+
* ------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
#include <fcnes.h>
|
|
12
|
+
|
|
13
|
+
/* 32 字节调色板:每组 4 色,颜色 0 是背景(黑),颜色 1 是文字(白) */
|
|
14
|
+
static const unsigned char palette[32] = {
|
|
15
|
+
0x0F, 0x30, 0x10, 0x00, 0x0F, 0x30, 0x10, 0x00,
|
|
16
|
+
0x0F, 0x30, 0x10, 0x00, 0x0F, 0x30, 0x10, 0x00,
|
|
17
|
+
0x0F, 0x30, 0x10, 0x00, 0x0F, 0x30, 0x10, 0x00,
|
|
18
|
+
0x0F, 0x30, 0x10, 0x00, 0x0F, 0x30, 0x10, 0x00
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
int main(void)
|
|
22
|
+
{
|
|
23
|
+
nes_screen_off();
|
|
24
|
+
nes_set_palette(palette);
|
|
25
|
+
nes_print(10, 14, "HELLO, NES!");
|
|
26
|
+
nes_wait_vblank();
|
|
27
|
+
nes_screen_on();
|
|
28
|
+
|
|
29
|
+
/* NES 程序没有“返回”,主循环一直运行 */
|
|
30
|
+
for (;;) {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# {{name}}
|
|
2
|
+
|
|
3
|
+
用 fcdev 开发的小霸王学习机 SB2000(SB-DOS)程序,ca65 汇编。
|
|
4
|
+
|
|
5
|
+
## 构建
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm run build # 生成 build/sb2000/HELLO.EXE
|
|
9
|
+
npm run disk # 生成放有 HELLO.EXE 的软盘镜像 build/sb2000/HELLO.img
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
程序名必须符合 DOS 8.3 规则(主名不超过 8 个字符),在 `fcdev.json` 的 `name` 里修改。
|
|
13
|
+
|
|
14
|
+
## 在模拟器里运行(只支持 Windows)
|
|
15
|
+
|
|
16
|
+
fcdev 不提供 BIOS。先在 `fcdev.json` 里用 `firmware` 指定 SB2000 的 BIOS 文件:
|
|
17
|
+
|
|
18
|
+
```json
|
|
19
|
+
{ "target": "sb2000", "name": "hello", "firmware": "D:/rom/SB2K_BIOS.NES" }
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm start
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
DOS 启动后按两次回车跳过日期与时间提示,在提示符下输入 `hello`,显示后按任意键返回 DOS。
|
|
27
|
+
|
|
28
|
+
## 文件
|
|
29
|
+
|
|
30
|
+
| 文件 | 说明 |
|
|
31
|
+
| --- | --- |
|
|
32
|
+
| `src/hello.s` | 示例程序:显示一行英文和一行中文,等待按键后返回 DOS,入口是导出的 `_main` |
|
|
33
|
+
| `fcdev.json` | fcdev 配置:目标 sb2000,程序名 hello |
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
; ------------------------------------------------------------------------
|
|
2
|
+
; 名称:hello.s
|
|
3
|
+
; 说明:SB-DOS 最小汇编程序:显示一行英文与一行中文,等待按键后返回 DOS。
|
|
4
|
+
; 作者:sungbin
|
|
5
|
+
; 邮箱:sungbin@3578.cn
|
|
6
|
+
; 日期:2026-11-10
|
|
7
|
+
; 备注:入口是导出的 _main,由启动代码调用,返回后启动代码负责回到 DOS;
|
|
8
|
+
; _sb_puts、_sb_getkey 来自目标自带的 sb2000 库,参数用 cc65 的 fastcall:指针在 A(低)与 X(高);
|
|
9
|
+
; 中文用 GB2312 编码,字符码不小于 $A1 的两个字节显示一个汉字。
|
|
10
|
+
; ------------------------------------------------------------------------
|
|
11
|
+
.export _main
|
|
12
|
+
.import _sb_puts, _sb_getkey
|
|
13
|
+
|
|
14
|
+
.segment "RODATA"
|
|
15
|
+
|
|
16
|
+
message:
|
|
17
|
+
.asciiz "Hello, SB2000!"
|
|
18
|
+
|
|
19
|
+
; GB2312:你好,小霸王!
|
|
20
|
+
message_cn:
|
|
21
|
+
.byte $C4, $E3, $BA, $C3, $A3, $AC, $D0, $A1, $B0, $D4, $CD, $F5, $A3, $A1, $00
|
|
22
|
+
|
|
23
|
+
.segment "CODE"
|
|
24
|
+
|
|
25
|
+
_main:
|
|
26
|
+
lda #<message
|
|
27
|
+
ldx #>message
|
|
28
|
+
jsr _sb_puts ; 从行首显示并换行
|
|
29
|
+
|
|
30
|
+
lda #<message_cn
|
|
31
|
+
ldx #>message_cn
|
|
32
|
+
jsr _sb_puts
|
|
33
|
+
|
|
34
|
+
jsr _sb_getkey ; 等待按键,DOS 在程序返回后会清屏
|
|
35
|
+
rts
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"target": "sb2000",
|
|
3
|
+
"language": "asm",
|
|
4
|
+
"title": "小霸王学习机 SB2000(SB-DOS)",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"disk": "fcdev disk src"
|
|
7
|
+
},
|
|
8
|
+
"notes": [
|
|
9
|
+
" npm run disk 生成软盘镜像 build/sb2000/HELLO.img",
|
|
10
|
+
" npm start 在模拟器里运行(只支持 Windows):先在 fcdev.json 里用 firmware 指定 SB2000 的 BIOS 文件,",
|
|
11
|
+
" DOS 启动后按两次回车跳过日期与时间,再输入 hello"
|
|
12
|
+
]
|
|
13
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# {{name}}
|
|
2
|
+
|
|
3
|
+
用 fcdev 开发的小霸王学习机 SB2000(SB-DOS)程序,C 语言。
|
|
4
|
+
|
|
5
|
+
## 构建
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm run build # 生成 build/sb2000/HELLO.EXE
|
|
9
|
+
npm run disk # 生成放有 HELLO.EXE 的软盘镜像 build/sb2000/HELLO.img
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
程序名必须符合 DOS 8.3 规则(主名不超过 8 个字符),在 `fcdev.json` 的 `name` 里修改。
|
|
13
|
+
|
|
14
|
+
## 在模拟器里运行(只支持 Windows)
|
|
15
|
+
|
|
16
|
+
fcdev 不提供 BIOS。先在 `fcdev.json` 里用 `firmware` 指定 SB2000 的 BIOS 文件:
|
|
17
|
+
|
|
18
|
+
```json
|
|
19
|
+
{ "target": "sb2000", "name": "hello", "firmware": "D:/rom/SB2K_BIOS.NES" }
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm start
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
DOS 启动后按两次回车跳过日期与时间提示,在提示符下输入 `hello`,显示后按任意键返回 DOS。
|
|
27
|
+
|
|
28
|
+
## 文件
|
|
29
|
+
|
|
30
|
+
| 文件 | 说明 |
|
|
31
|
+
| --- | --- |
|
|
32
|
+
| `src/hello.c` | 示例程序:显示一行英文和一行中文,等待按键后返回 DOS |
|
|
33
|
+
| `fcdev.json` | fcdev 配置:目标 sb2000,程序名 hello |
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* ------------------------------------------------------------------------
|
|
3
|
+
* 名称:hello.c
|
|
4
|
+
* 说明:SB-DOS 最小程序:显示一行英文与一行中文,等待按键后返回 DOS。
|
|
5
|
+
* 作者:sungbin
|
|
6
|
+
* 邮箱:sungbin@3578.cn
|
|
7
|
+
* 日期:2026-11-10
|
|
8
|
+
* 备注:中文用 GB2312 编码,字符码不小于 0xA1 的两个字节显示一个汉字,这里写成十六进制转义;
|
|
9
|
+
* 返回 DOS 后 DOS 会清屏,所以显示后先等待按键。
|
|
10
|
+
* ------------------------------------------------------------------------
|
|
11
|
+
*/
|
|
12
|
+
#include <sb2000.h>
|
|
13
|
+
|
|
14
|
+
int main(void)
|
|
15
|
+
{
|
|
16
|
+
sb_puts("Hello, SB2000!");
|
|
17
|
+
|
|
18
|
+
/* GB2312:你好,小霸王! */
|
|
19
|
+
sb_puts("\xC4\xE3\xBA\xC3\xA3\xAC\xD0\xA1\xB0\xD4\xCD\xF5\xA3\xA1");
|
|
20
|
+
|
|
21
|
+
sb_getkey();
|
|
22
|
+
return 0;
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"target": "sb2000",
|
|
3
|
+
"language": "c",
|
|
4
|
+
"title": "小霸王学习机 SB2000(SB-DOS)",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"disk": "fcdev disk src"
|
|
7
|
+
},
|
|
8
|
+
"notes": [
|
|
9
|
+
" npm run disk 生成软盘镜像 build/sb2000/HELLO.img",
|
|
10
|
+
" npm start 在模拟器里运行(只支持 Windows):先在 fcdev.json 里用 firmware 指定 SB2000 的 BIOS 文件,",
|
|
11
|
+
" DOS 启动后按两次回车跳过日期与时间,再输入 hello"
|
|
12
|
+
]
|
|
13
|
+
}
|