esp32emu / tools /exc.mjs
HelloSun's picture
ESP32 emulator update
d8afd6f verified
Raw History Blame Contribute Delete
1.73 kB
import fs from 'fs';
import { ESP32 } from '../web/js/soc.js';
import { Syms } from './sym.mjs';
const romDir = new URL('../web/rom/', import.meta.url).pathname;
const args = process.argv.slice(2);
const symFiles = (args.find(a => a.startsWith('--syms=')) || '--syms=/tmp/opencode/hello.syms').slice(7).split(',').concat(['/tmp/opencode/rom.syms']);
const syms = new Syms(symFiles);
const soc = new ESP32({});
soc.loadRoms(fs.readFileSync(romDir + 'esp32-v3-rom.bin'), fs.readFileSync(romDir + 'esp32-v3-rom-app.bin'));
soc.setFlash(fs.readFileSync(args[0])); soc.enablePsram(args.includes('--psram'));
soc.onUart = (n, b) => process.stdout.write(String.fromCharCode(b));
soc.reset('power');
let nexc = 0;
for (const c of soc.cpus) {
const oe = c.exception.bind(c);
c.exception = function (pc, cause, va) {
if (cause !== 4 && nexc++ < 12) { const regs = []; for (let i = 0; i < 16; i++) regs.push((c.ar[(c.wb4 + i) & 63] >>> 0).toString(16)); console.log(`EXC cpu${c.id} pc=${(pc>>>0).toString(16)} ${syms.name(pc)} cause=${cause} va=${((va||0)>>>0).toString(16)} ps=${c.ps.toString(16)} a0=${syms.name(c.ar[c.wb4&63])}\n regs ${regs.join(' ')}`); }
return oe(pc, cause, va);
};
}
soc.cpus[0].enableTrace();
const oe0 = soc.cpus[0].exception;
soc.cpus[0].exception = function(pc, cause, va){ if (cause===0 && !globalThis.shown) { globalThis.shown=1; const tr = soc.cpus[0].getTrace(65000); let last=''; const seq=[]; for (const p of tr) { const n = syms.name(p).replace(/\+.*/,''); if (n!==last) { seq.push(n); last=n; } } console.log('FUNC SEQ:', seq.slice(-400).join(' > ')); } return oe0.call(this, pc, cause, va); };
const limit = parseFloat(args[1] || '2') * 1e9;
while (soc.timeNs < limit) soc.run(10000);