Skip to content

Commit b50389b

Browse files
committed
1 parent bfc9da6 commit b50389b

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

src/APITypes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export interface SerialMonitorDataPayload {
3737
bytes: number[];
3838
}
3939

40+
export interface ChipsLogPayload {
41+
chip: string;
42+
message: string;
43+
}
44+
4045
export interface APIResultError {
4146
code: number;
4247
message: string;
@@ -46,4 +51,5 @@ export interface APISimStartParams {
4651
firmware: string;
4752
elf: string;
4853
pause?: boolean;
54+
chips?: string[];
4955
}

src/WokwiConfig.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
export interface WokwiTOMLChip {
2+
name: string;
3+
binary: string;
4+
}
5+
16
export interface WokwiTOML {
27
wokwi: {
38
version: number;
49
firmware: string;
510
elf: string;
611
};
12+
chip?: WokwiTOMLChip[];
713
}

src/loadChips.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { existsSync } from 'fs';
2+
import { join, resolve } from 'path';
3+
import type { WokwiTOMLChip } from './WokwiConfig';
4+
5+
function removeExtension(path: string) {
6+
return path.replace(/\.[^.]+$/, '');
7+
}
8+
9+
export function loadChips(chips: WokwiTOMLChip[], rootDir: string) {
10+
const result = [];
11+
for (const chip of chips ?? []) {
12+
const wasmPath = join(rootDir, chip.binary);
13+
if (!existsSync(wasmPath)) {
14+
console.error(`Error: chip WASM file not found: ${resolve(wasmPath)}`);
15+
process.exit(1);
16+
}
17+
18+
const jsonPath = join(rootDir, removeExtension(chip.binary) + '.json');
19+
if (!existsSync(jsonPath)) {
20+
console.error(`Error: chip JSON file not found: ${resolve(jsonPath)}`);
21+
process.exit(1);
22+
}
23+
24+
result.push({
25+
name: chip.name,
26+
jsonPath,
27+
wasmPath,
28+
});
29+
}
30+
return result;
31+
}

src/main.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import chalk from 'chalk';
33
import { existsSync, readFileSync, writeFileSync } from 'fs';
44
import path, { join } from 'path';
55
import { APIClient } from './APIClient';
6-
import type { APIEvent, SerialMonitorDataPayload } from './APITypes';
6+
import type { APIEvent, ChipsLogPayload, SerialMonitorDataPayload } from './APITypes';
77
import { EventManager } from './EventManager';
88
import { ExpectEngine } from './ExpectEngine';
99
import { parseConfig } from './config';
1010
import { cliHelp } from './help';
1111
import { readVersion } from './readVersion';
12+
import { loadChips } from './loadChips';
1213

1314
const millis = 1_000_000;
1415

@@ -90,6 +91,8 @@ async function main() {
9091
process.exit(1);
9192
}
9293

94+
const chips = loadChips(config.chip ?? [], rootDir);
95+
9396
const expectEngine = new ExpectEngine();
9497

9598
if (expectText) {
@@ -126,6 +129,11 @@ async function main() {
126129
await client.fileUpload(firmwareName, readFileSync(firmwarePath));
127130
await client.fileUpload('firmware.elf', readFileSync(elfPath));
128131

132+
for (const chip of chips) {
133+
await client.fileUpload(`${chip.name}.chip.json`, readFileSync(chip.jsonPath, 'utf-8'));
134+
await client.fileUpload(`${chip.name}.chip.wasm`, readFileSync(chip.wasmPath));
135+
}
136+
129137
if (!quiet) {
130138
console.log('Starting simulation...');
131139
}
@@ -151,14 +159,6 @@ async function main() {
151159

152160
await client.serialMonitorListen();
153161
const { timeToNextEvent } = eventManager;
154-
await client.simStart({
155-
elf: 'test.elf',
156-
firmware: firmwareName,
157-
pause: timeToNextEvent >= 0,
158-
});
159-
if (timeToNextEvent > 0) {
160-
await client.simResume(timeToNextEvent);
161-
}
162162

163163
client.onEvent = (event) => {
164164
if (event.event === 'sim:pause') {
@@ -174,7 +174,22 @@ async function main() {
174174
}
175175
expectEngine.feed(bytes);
176176
}
177+
if (event.event === 'chips:log') {
178+
const { message, chip } = (event as APIEvent<ChipsLogPayload>).payload;
179+
console.log(chalk`[{magenta ${chip}}] ${message}`);
180+
}
177181
};
182+
183+
await client.simStart({
184+
elf: 'test.elf',
185+
firmware: firmwareName,
186+
chips: chips.map((chip) => chip.name),
187+
pause: timeToNextEvent >= 0,
188+
});
189+
190+
if (timeToNextEvent > 0) {
191+
await client.simResume(timeToNextEvent);
192+
}
178193
}
179194

180195
main().catch((err) => {

0 commit comments

Comments
 (0)