Skip to content

Commit 303b7d2

Browse files
committed
Add help and version command line flags
1 parent b36b761 commit 303b7d2

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

demo/emulator-run.ts

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,71 @@
1-
import * as fs from 'fs';
2-
import { GDBTCPServer } from '../src/gdb/gdb-tcp-server.js';
3-
import { Simulator } from '../src/simulator.js';
4-
import { bootromB1 } from './bootrom.js';
5-
import { loadHex } from './intelhex.js';
6-
import { loadUF2 } from './load-flash.js';
7-
import minimist from 'minimist';
1+
import * as fs from "fs";
2+
import minimist from "minimist";
3+
import packageJson from "../package.json" with { type: "json" };
4+
import { GDBTCPServer } from "../src/gdb/gdb-tcp-server.js";
5+
import { RP2040 } from "../src/index.js";
6+
import { Simulator } from "../src/simulator.js";
7+
import { bootromB1 } from "./bootrom.js";
8+
import { loadHex } from "./intelhex.js";
9+
import { loadUF2 } from "./load-flash.js";
10+
11+
const HELP_MESSAGE = `
12+
${packageJson.description}
13+
14+
Flags:
15+
--image <IMAGE>: The compiled image to load and run in the emulator (.uf2, .hex). If no arguement is provided, look for a file called 'hello_uart.hex' in the current directory.
16+
--help: Print this help message.
17+
--version: Print the current rp2040js version.
18+
`;
19+
20+
function loadImage(imageName = "hello_uart.hex", mcu: RP2040) {
21+
const extension = imageName.split(".").pop();
22+
23+
if (extension === "hex") {
24+
// Create an array with the compiled code of blink
25+
// Execute the instructions from this array, one by one.
26+
const hex = fs.readFileSync(imageName, "utf-8");
27+
console.log(`Loading hex image ${imageName}`);
28+
loadHex(hex, mcu.flash, 0x10000000);
29+
} else if (extension === "uf2") {
30+
console.log(`Loading uf2 image ${imageName}`);
31+
loadUF2(imageName, mcu);
32+
} else {
33+
console.log(`Unsupported file type: ${extension}`);
34+
process.exit(1);
35+
}
36+
}
837

938
const args = minimist(process.argv.slice(2), {
1039
string: [
11-
'image', // An image to load, hex and UF2 are supported
40+
"image", // An image to load, hex and UF2 are supported
41+
],
42+
boolean: [
43+
"help",
44+
"version",
1245
],
1346
});
1447

48+
if (args.help) {
49+
console.log(HELP_MESSAGE);
50+
process.exit(0);
51+
}
52+
53+
if (args.version) {
54+
console.log(`rp2040js version: ${packageJson.version}`);
55+
process.exit(0);
56+
}
57+
1558
const simulator = new Simulator();
1659
const mcu = simulator.rp2040;
1760
mcu.loadBootrom(bootromB1);
1861

19-
const imageName = args.image ?? 'hello_uart.hex';
20-
21-
// Check the extension of the file
22-
const extension = imageName.split('.').pop();
23-
if (extension === 'hex') {
24-
// Create an array with the compiled code of blink
25-
// Execute the instructions from this array, one by one.
26-
const hex = fs.readFileSync(imageName, 'utf-8');
27-
28-
console.log(`Loading hex image ${imageName}`);
29-
loadHex(hex, mcu.flash, 0x10000000);
30-
} else if (extension === 'uf2') {
31-
console.log(`Loading uf2 image ${imageName}`);
32-
loadUF2(imageName, mcu);
33-
} else {
34-
console.log(`Unsupported file type: ${extension}`);
62+
try {
63+
loadImage(args.image, mcu);
64+
} catch (err) {
65+
const message = err instanceof Error ? err.message : String(err);
66+
67+
console.log(`Failed to load image file: "${message}"`);
68+
console.log(HELP_MESSAGE);
3569
process.exit(1);
3670
}
3771

0 commit comments

Comments
 (0)