Skip to content

Commit

Permalink
feat: custom chips API support
Browse files Browse the repository at this point in the history
  • Loading branch information
urish committed May 31, 2023
1 parent bfc9da6 commit b50389b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/APITypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export interface SerialMonitorDataPayload {
bytes: number[];
}

export interface ChipsLogPayload {
chip: string;
message: string;
}

export interface APIResultError {
code: number;
message: string;
Expand All @@ -46,4 +51,5 @@ export interface APISimStartParams {
firmware: string;
elf: string;
pause?: boolean;
chips?: string[];
}
6 changes: 6 additions & 0 deletions src/WokwiConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export interface WokwiTOMLChip {
name: string;
binary: string;
}

export interface WokwiTOML {
wokwi: {
version: number;
firmware: string;
elf: string;
};
chip?: WokwiTOMLChip[];
}
31 changes: 31 additions & 0 deletions src/loadChips.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { existsSync } from 'fs';
import { join, resolve } from 'path';
import type { WokwiTOMLChip } from './WokwiConfig';

function removeExtension(path: string) {
return path.replace(/\.[^.]+$/, '');
}

export function loadChips(chips: WokwiTOMLChip[], rootDir: string) {
const result = [];
for (const chip of chips ?? []) {
const wasmPath = join(rootDir, chip.binary);
if (!existsSync(wasmPath)) {
console.error(`Error: chip WASM file not found: ${resolve(wasmPath)}`);
process.exit(1);
}

const jsonPath = join(rootDir, removeExtension(chip.binary) + '.json');
if (!existsSync(jsonPath)) {
console.error(`Error: chip JSON file not found: ${resolve(jsonPath)}`);
process.exit(1);
}

result.push({
name: chip.name,
jsonPath,
wasmPath,
});
}
return result;
}
33 changes: 24 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import chalk from 'chalk';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import path, { join } from 'path';
import { APIClient } from './APIClient';
import type { APIEvent, SerialMonitorDataPayload } from './APITypes';
import type { APIEvent, ChipsLogPayload, SerialMonitorDataPayload } from './APITypes';
import { EventManager } from './EventManager';
import { ExpectEngine } from './ExpectEngine';
import { parseConfig } from './config';
import { cliHelp } from './help';
import { readVersion } from './readVersion';
import { loadChips } from './loadChips';

const millis = 1_000_000;

Expand Down Expand Up @@ -90,6 +91,8 @@ async function main() {
process.exit(1);
}

const chips = loadChips(config.chip ?? [], rootDir);

const expectEngine = new ExpectEngine();

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

for (const chip of chips) {
await client.fileUpload(`${chip.name}.chip.json`, readFileSync(chip.jsonPath, 'utf-8'));
await client.fileUpload(`${chip.name}.chip.wasm`, readFileSync(chip.wasmPath));
}

if (!quiet) {
console.log('Starting simulation...');
}
Expand All @@ -151,14 +159,6 @@ async function main() {

await client.serialMonitorListen();
const { timeToNextEvent } = eventManager;
await client.simStart({
elf: 'test.elf',
firmware: firmwareName,
pause: timeToNextEvent >= 0,
});
if (timeToNextEvent > 0) {
await client.simResume(timeToNextEvent);
}

client.onEvent = (event) => {
if (event.event === 'sim:pause') {
Expand All @@ -174,7 +174,22 @@ async function main() {
}
expectEngine.feed(bytes);
}
if (event.event === 'chips:log') {
const { message, chip } = (event as APIEvent<ChipsLogPayload>).payload;
console.log(chalk`[{magenta ${chip}}] ${message}`);
}
};

await client.simStart({
elf: 'test.elf',
firmware: firmwareName,
chips: chips.map((chip) => chip.name),
pause: timeToNextEvent >= 0,
});

if (timeToNextEvent > 0) {
await client.simResume(timeToNextEvent);
}
}

main().catch((err) => {
Expand Down

0 comments on commit b50389b

Please sign in to comment.