diff --git a/src/common/baseplatform.ts b/src/common/baseplatform.ts index b464d3e0..a0a44ae9 100644 --- a/src/common/baseplatform.ts +++ b/src/common/baseplatform.ts @@ -1270,8 +1270,9 @@ export class WASMMachine implements Machine { getCPUState() { this.exports.machine_save_cpu_state(this.sys, this.cpustateptr); var s = this.cpustatearr; + var pc = s[2] + (s[3]<<8); return { - PC:s[2] + (s[3]<<8), + PC:pc, SP:s[9], A:s[6], X:s[7], @@ -1282,6 +1283,7 @@ export class WASMMachine implements Machine { D:s[10] & 8, V:s[10] & 64, N:s[10] & 128, + o:this.readConst(pc), } } saveState() { diff --git a/src/common/workertypes.ts b/src/common/workertypes.ts index ab5aee95..628ff3b0 100644 --- a/src/common/workertypes.ts +++ b/src/common/workertypes.ts @@ -28,13 +28,13 @@ export class SourceFile { } } } - findLineForOffset(PC:number, lookbehind:number):number { + // TODO: smarter about looking for source lines between two addresses + findLineForOffset(PC:number, lookbehind:number) { if (this.offset2line) { for (var i=0; i<=lookbehind; i++) { var line = this.offset2line[PC]; if (line >= 0) { - //console.log(this.lines.length, PC.toString(16), line); - return line; + return {line:line, offset:PC}; } PC--; } diff --git a/src/ide/ui.ts b/src/ide/ui.ts index 053a3007..d9627144 100644 --- a/src/ide/ui.ts +++ b/src/ide/ui.ts @@ -54,6 +54,7 @@ var lastDebugInfo; // last debug info (CPU text) var debugCategory; // current debug category var debugTickPaused = false; var recorderActive = false; +var lastViewClicked = null; var lastBreakExpr = "c.PC == 0x6000"; @@ -205,6 +206,7 @@ function refreshWindowList() { projectWindows.setShowFunc(id, onopen); $(a).click( (e) => { projectWindows.createOrShow(id); + lastViewClicked = id; }); } } @@ -1096,36 +1098,38 @@ function checkRunReady() { } function openRelevantListing(state: EmuState) { - // if already on disassembly window, retain it - if (projectWindows.getActive() instanceof Views.DisassemblerView) return; + // if we clicked on disassembly window, retain it + if (lastViewClicked == '#disasm' && projectWindows.getActive() instanceof Views.DisassemblerView) return; // search through listings var listings = current_project.getListings(); + var bestid = "#disasm"; + var bestscore = 32; if (listings) { var pc = state.c ? (state.c.EPC || state.c.PC) : 0; for (var lstfn in listings) { + //var wndid = current_project.filename2path[lstfn] || lstfn; var wndid = projectWindows.findWindowWithFilePrefix(lstfn); - console.log(lstfn,wndid); + //console.log(lstfn,wndid); if (projectWindows.isWindow(wndid)) { var lst = listings[lstfn]; - var file = lst.assemblyfile || lst.sourcefile; - // TODO: look for closest match, not just first match - var lineno = file && file.findLineForOffset(pc, 16); // TODO: const - console.log(hex(pc,4), wndid, lstfn, lineno); - if (lineno !== null) { - projectWindows.createOrShow(wndid, true); - return; + var file = lst.sourcefile || lst.assemblyfile; + var res = file && file.findLineForOffset(pc, 32); // TODO: const + if (res && pc-res.offset < bestscore) { + bestid = wndid; + bestscore = pc-res.offset; } + console.log(hex(pc,4), wndid, lstfn, bestid, bestscore); } } } // if no appropriate listing found, use disassembly view - projectWindows.createOrShow("#disasm", true); + projectWindows.createOrShow(bestid, true); } function uiDebugCallback(state: EmuState) { lastDebugState = state; showDebugInfo(state); - //openRelevantListing(state); + openRelevantListing(state); projectWindows.refresh(true); // move cursor debugTickPaused = true; } @@ -1175,6 +1179,7 @@ function resume() { } _resume(); userPaused = false; + lastViewClicked = null; } function togglePause() { diff --git a/src/ide/views.ts b/src/ide/views.ts index f31945dd..d3814f3d 100644 --- a/src/ide/views.ts +++ b/src/ide/views.ts @@ -322,8 +322,8 @@ export class SourceEditor implements ProjectView { var state = lastDebugState; if (state && state.c && this.sourcefile) { var EPC = state.c.EPC || state.c.PC; - var line = this.sourcefile.findLineForOffset(EPC, 15); - return line; + var res = this.sourcefile.findLineForOffset(EPC, 15); + return res && res.line; } else return -1; } @@ -509,13 +509,13 @@ export class ListingView extends DisassemblerView implements ProjectView { var disasmview = this.getDisasmView(); disasmview.setValue(asmtext); if (pc >= 0 && this.assemblyfile) { - var lineno = this.assemblyfile.findLineForOffset(pc, 15); - if (lineno) { + var res = this.assemblyfile.findLineForOffset(pc, 15); + if (res) { // set cursor while debugging if (moveCursor) { - disasmview.setCursor(lineno-1, 0); + disasmview.setCursor(res.line-1, 0); } - jumpToLine(disasmview, lineno-1); + jumpToLine(disasmview, res.line-1); } } } diff --git a/src/ide/windows.ts b/src/ide/windows.ts index 02361e29..005acfa6 100644 --- a/src/ide/windows.ts +++ b/src/ide/windows.ts @@ -160,7 +160,7 @@ export class ProjectWindows { filename = getFilenameForPath(getFilenamePrefix(filename)); for (var fileid in this.id2createfn) { // ignore include files (TODO) - if (fileid.toLowerCase().endsWith('.h') || fileid.toLowerCase().endsWith('.inc')) + if (fileid.toLowerCase().endsWith('.h') || fileid.toLowerCase().endsWith('.inc') || fileid.toLowerCase().endsWith('.bas')) continue; if (getFilenameForPath(getFilenamePrefix(fileid)) == filename) return fileid; } diff --git a/test/cli/teststore.js b/test/cli/teststore.js index 051166de..c81267b3 100644 --- a/test/cli/teststore.js +++ b/test/cli/teststore.js @@ -116,7 +116,7 @@ describe('Store', function() { assert.deepEqual([false], msgs); var lst = buildresult.listings.test; console.log(lst); - assert.equal(3, lst.sourcefile.findLineForOffset(61440+15, 15)); + assert.deepEqual({line:3,offset:61440}, lst.sourcefile.findLineForOffset(61440+15, 15)); assert.equal(null, lst.sourcefile.findLineForOffset(61440+16, 15)); assert.equal(null, lst.sourcefile.findLineForOffset(61440+1, 0)); assert.equal(null, lst.sourcefile.findLineForOffset(61440-1, 16));