|
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 | +} |
8 | 37 |
|
9 | 38 | const args = minimist(process.argv.slice(2), {
|
10 | 39 | 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", |
12 | 45 | ],
|
13 | 46 | });
|
14 | 47 |
|
| 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 | + |
15 | 58 | const simulator = new Simulator();
|
16 | 59 | const mcu = simulator.rp2040;
|
17 | 60 | mcu.loadBootrom(bootromB1);
|
18 | 61 |
|
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); |
35 | 69 | process.exit(1);
|
36 | 70 | }
|
37 | 71 |
|
|
0 commit comments