esp32emu / tools /dbg2.mjs
HelloSun's picture
ESP32 emulator update
d8afd6f verified
Raw History Blame Contribute Delete
2.38 kB
import fs from 'fs';
import { ESP32 } from '../web/js/soc.js';
import { disasm } from './dis.mjs';
const romDir = new URL('../web/rom/', import.meta.url).pathname;
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(process.argv[2]));
soc.enablePsram(process.argv.includes('--psram'));
soc.onUart = (n, b) => process.stdout.write(String.fromCharCode(b));
soc.reset('power');
const c = soc.cpus[0];
const stopAt = parseInt(process.argv[3], 16);
const oe = c.exception.bind(c); let hit=null; c.exception = function(pc, cause, va){ if(!hit) hit={pc,cause,va}; throw new Error('EXC'); };
let n = 0;
c.trace = null;
try { while (n < 3e8) { soc.run(1000); n += 1000; const p = c.pc >>> 0; if (p === stopAt) break; } } catch(e) { if (e.message!=='EXC') throw e; console.log('EXC', hit.pc.toString(16), hit.cause, (hit.va>>>0).toString(16)); c.pc = hit.pc; }
console.log('\nstopped pc', (c.pc>>>0).toString(16), 'n', n);
const regs=[]; for (let i=0;i<16;i++) regs.push((c.ar[(c.wb4+i)&63]>>>0).toString(16));
console.log('a0-a15', regs.join(' '), 'wb', c.wb, 'ws', c.ws.toString(16), 'ps', c.ps.toString(16));
console.log(disasm(soc, c, (c.pc >>> 0) - 24, 64));
{ const a=(c.pc>>>0); const d=c.rd[a>>>12]; const b=[]; for(let i=-6;i<12;i++) b.push(soc.h8[((a+i)+d)|0].toString(16).padStart(2,'0')); console.log('bytes around pc:', b.join(' ')); }
if (process.argv.includes('--trace')) {
c.trace = []; soc.reset('power');
const c2 = soc.cpus[0]; c2.trace = []; c2.exception = function(pc, cause, va){ throw new Error('EXC'); };
try { for (let k=0;k<4000000;k++) soc.run(100); } catch(e) {}
const tr = c2.trace.slice(-60);
const lo = Math.min(...tr)-16, hi = Math.max(...tr)+16;
console.log('trace range', lo.toString(16), hi.toString(16));
const text = disasm(soc, c2, lo >>> 0, (hi-lo)>>>0);
const map = new Map(); for (const l of text.split('\n')) { const m = l.match(/^\s*([0-9a-f]+):\s*(.*)$/); if (m) map.set(parseInt(m[1],16), m[2]); }
console.log(tr.map(p => (p>>>0).toString(16)+': '+(map.get(p>>>0)||'?')).join('\n'));
}
if (process.argv.includes('--dump')) { const a = parseInt(process.argv[process.argv.indexOf('--dump')+1],16); const n = parseInt(process.argv[process.argv.indexOf('--dump')+2]||'96'); console.log(disasm(soc, c, a, n)); }