DIRECTORY
AMBridge USING [SomeRefFromTV, TVForReferent],
AMTypes USING [TV],
Buttons USING [Create, ReLabel],
Convert USING [CardFromRope],
DebuggerDefs -- using lots -- ,
FS USING [Error, FileInfo, StreamOpen],
Interpreter USING [Evaluate],
IO,
Labels USING [Create, Set],
List USING [CompareProc, Sort],
Process USING [Detach, Milliseconds, PauseMsec],
Rope,
SoftcardOps,
SoftcardToolPrivate,
SymTab USING [Create, Ref, Store],
ViewerTools USING [GetContents, MakeNewTextViewer, SetSelection];
Word: TYPE = CARD32;
STREAM: TYPE = IO.STREAM;
ROPE: TYPE = Rope.ROPE;
Memory: TYPE = REF MemoryRec;
MemoryRec:
TYPE =
RECORD [
sym: SymTab.Ref, wds: REF MemWords, sortedSyms: REF SortedSyms,
lowestAddr: Word ← 0,
highestAddr: Word ← 0];
MemWords: TYPE = RECORD [SEQUENCE size: CARDINAL OF MemWord];
MemWord: TYPE = RECORD [addr, value: Word];
SortedSyms: TYPE = RECORD [SEQUENCE size: CARDINAL OF SymbolAddr];
SymbolAddr: TYPE = REF SymbolAddrRec;
SymbolAddrRec: TYPE = RECORD [sym: ROPE, addr: Word];
quadMemory: Memory;
quadFileNameText: Viewer;
interpretWhatText, showInterpretResult: Viewer;
debuggerWatcherButton, autoDumpDebuggerButton, autoRestartDebuggerButton: Viewer;
watcherWait: Process.Milliseconds ← 100;
watchingDebugger: BOOL ← FALSE;
autoDumpDebuggerState: BOOL ← FALSE;
autoRestartDebugger: BOOL ← FALSE;
debuggerWatchAddr: CARD32 = DD.debugBase + DD.debuggerProceed;
dumpBaseAddr: CARD32 = DD.debugBase + DD.debuggerOpcode;
numToDump:
CARD16 =
DD.euRegBase +
DD.EUStackSize +
DD.EUAuxRegs +
DD.EUConstants + 2*DD.IFUStackSize;
debuggerWatchSCAddr:
CARD32 =
SoftcardToolPrivate.DragonToSoftcardAddr[debuggerWatchAddr];
dumpBaseSCAddr: CARD32 = SoftcardToolPrivate.DragonToSoftcardAddr[dumpBaseAddr];
debuggerState: SoftcardOps.SeqLong;
BuildQuadIOButtons:
PUBLIC
PROC[topViewer, sibx: Viewer]
RETURNS[sib: Viewer] = {
sib ← sibx;
Load Quad file (dragon code)
sib ← Buttons.Create[
info: [ name: " LoadQuadFile ", parent: topViewer,
wx: leftEdge, wy: sib.wy+sib.wh+betweenHeight, wh: entryHeight,
border: TRUE, scrollable: FALSE],
font: activeFont, proc: LoadQuadFileProc ];
sib ← Buttons.Create[
info: [ name: " fileName: ", parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: FALSE, scrollable: FALSE],
font: selectFont, proc: QuadFileNameProc ];
sib ← quadFileNameText ← ViewerTools.MakeNewTextViewer[
info: [parent: topViewer, wx: sib.wx+sib.ww+xFudge+6, wy: sib.wy,
ww: 300, wh: entryHeight, border: FALSE, scrollable: FALSE]];
DebuggerWatcher for dragon code
watchingDebugger ← FALSE;
sib ← Labels.Create[
info: [ name: " Debugger ", parent: topViewer,
wx: leftEdge, wy: sib.wy+sib.wh+betweenHeight, wh: entryHeight,
border: FALSE, scrollable: FALSE],
font: labelFont ];
sib ← debuggerWatcherButton ← Buttons.Create[
info: [ name: " WatcherOff ", parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: TRUE, scrollable: FALSE],
font: selectFont, proc: DebuggerWatcherProc ];
autoDumpDebuggerState ← FALSE;
sib ← autoDumpDebuggerButton ← Buttons.Create[
info: [ name: " AutoDumpOff ", parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: TRUE, scrollable: FALSE],
font: selectFont, proc: AutoDumpDebuggerProc ];
autoRestartDebugger ← FALSE;
sib ← autoRestartDebuggerButton ← Buttons.Create[
info: [ name: " AutoRestartOff ", parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: TRUE, scrollable: FALSE],
font: selectFont, proc: AutoRestartDebuggerProc ];
sib ← Buttons.Create[
info: [ name: " DumpState ", parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: TRUE, scrollable: FALSE],
font: activeFont, proc: DumpDebuggerStateProc ];
sib ← Buttons.Create[
info: [ name: " Restart ", parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: TRUE, scrollable: FALSE],
font: activeFont, proc: RestartDebuggerProc ];
Interpret symbol for dragon code
sib ← Buttons.Create[
info: [ name: " InterpSym ", parent: topViewer,
wx: leftEdge, wy: sib.wy+sib.wh+betweenHeight, wh: entryHeight,
border: TRUE, scrollable: FALSE],
font: activeFont, proc: InterpSymbolProc ];
sib ← Buttons.Create[
info: [ name: " InterpAddr ", parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: TRUE, scrollable: FALSE],
font: activeFont, proc: InterpAddrProc ];
sib ← Buttons.Create[
info: [ name: " what: ", parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: FALSE, scrollable: FALSE],
font: selectFont, proc: InterpretWhatProc ];
sib ← interpretWhatText ← ViewerTools.MakeNewTextViewer[
info: [parent: topViewer, wx: sib.wx+sib.ww+xFudge, wy: sib.wy,
ww: 400, wh: entryHeight, border: FALSE, scrollable: FALSE]];
sib ← Labels.Create[
info: [ name: " result: ", parent: topViewer,
wx: leftEdge+40, wy: sib.wy+sib.wh+betweenHeight, wh: entryHeight,
border: FALSE, scrollable: FALSE],
font: labelFont ];
sib ← showInterpretResult ← Labels.Create[
info: [ name: " ",
parent: topViewer,
wx: sib.wx+sib.ww+xFudge, wy: sib.wy, wh: entryHeight,
border: FALSE, scrollable: FALSE]
];
RETURN[sib];
};
LoadQuadFileProc: ClickProc = {
fName: ROPE ← ViewerTools.GetContents[quadFileNameText];
fullName: ROPE;
numLeft: CARD16 ← 0;
indexThisRound, pushIndex: CARD16 ← 0;
PushAddrVal:
PROC
RETURNS[addr: SoftcardOps.Addr, value:
CARD32] = {
addr ← ByteAddrToSoftcardAddr[quadMemory.wds[pushIndex].addr];
value ← quadMemory.wds[pushIndex].value;
pushIndex ← pushIndex + 1;
};
IF fName.Length[] = 0
THEN {
TSOutPutRope["\n**** Please fill in the name of a quad file\n"];
RETURN
};
IF fName.Find["."] < 0 THEN fName ← fName.Concat[".quad"];
fullName ←
FS.FileInfo[name: fName, remoteCheck:
FALSE, wDir: regDir
!
FS.Error => {
TSOutPutRope[error.explanation];
TSOutPutChar['\n];
GOTO nogood } ].fullFName;
quadMemory ← ReadQuadFile[fullName];
IF quadMemory = NIL THEN RETURN; -- some error, already reported
numLeft ← quadMemory.wds.size;
WHILE numLeft > 256
DO
SoftcardOps.WriteMultipleLong[256, PushAddrVal];
numLeft ← numLeft - 256;
ENDLOOP;
IF numLeft > 0
THEN SoftcardOps.WriteMultipleLong[numLeft, PushAddrVal];
TSOutPutF["\n\tQuadFile %g (%g instructions) has been loaded\n\n",
[rope[fullName]], [cardinal[quadMemory.wds.size]] ];
};
QuadFileNameProc: ClickProc =
{ ViewerTools.SetSelection[quadFileNameText, NIL] };
DebuggerWatcherProc: ClickProc = {
IF DebuggerWatching[changeState:
TRUE]
THEN
Buttons.ReLabel[debuggerWatcherButton, " WatcherOff "]
ELSE Buttons.ReLabel[debuggerWatcherButton, " WatcherOn "]
};
DebuggerWatching:
ENTRY
PROC[changeState:
BOOL]
RETURNS[
BOOL] = {
IF ~changeState THEN RETURN[watchingDebugger];
watchingDebugger ← ~watchingDebugger;
RETURN[~watchingDebugger]; -- if changeState return previous value
};
AutoDumpDebuggerProc: ClickProc = {
IF AutoDumpDebuggerState[changeState:
TRUE]
THEN
Buttons.ReLabel[autoDumpDebuggerButton, " AutoDumpOff "]
ELSE Buttons.ReLabel[autoDumpDebuggerButton, " AutoDumpOn "]
};
AutoDumpDebuggerState:
ENTRY
PROC[changeState:
BOOL]
RETURNS[
BOOL] = {
IF ~changeState THEN RETURN[autoDumpDebuggerState];
autoDumpDebuggerState ← ~autoDumpDebuggerState;
RETURN[~autoDumpDebuggerState]; -- if changeState still return previous value
};
AutoRestartDebuggerProc: ClickProc = {
IF AutoRestart[changeState:
TRUE]
THEN
Buttons.ReLabel[autoRestartDebuggerButton, " AutoRestartOff "]
ELSE Buttons.ReLabel[autoRestartDebuggerButton, " AutoRestartOn "]
};
AutoRestart:
ENTRY
PROC[changeState:
BOOL]
RETURNS[
BOOL] = {
IF ~changeState THEN RETURN[autoRestartDebugger];
autoRestartDebugger ← ~autoRestartDebugger;
RETURN[~autoRestartDebugger]; -- if changeState still return previous value
};
TheWatcherProc:
PROC = {
alreadyReported: BOOL ← FALSE;
DO
BEGIN
ENABLE SoftcardOps.SCError =>
GOTO scError;
IF ~DebuggerWatching[changeState:
FALSE]
THEN {
alreadyReported ← FALSE;
Process.PauseMsec[watcherWait];
LOOP;
};
IF SoftcardOps.ReadLong[debuggerWatchSCAddr] = 0
THEN {
alreadyReported ← FALSE;
Process.PauseMsec[watcherWait];
LOOP;
};
IF ~alreadyReported
THEN {
TSOutPutRope["\n\n***** Debugger is in wait loop\n"];
alreadyReported ← TRUE;
debuggerState ← SoftcardOps.DumpLong[dumpBaseSCAddr, numToDump];
IF AutoDumpDebuggerState[changeState: FALSE] THEN DumpDebuggerState[];
IF AutoRestart[changeState:
FALSE]
THEN {
SoftcardOps.WriteLong[debuggerWatchSCAddr, 0];
alreadyReported ← FALSE;
};
};
Process.PauseMsec[watcherWait];
EXITS
scError => Process.PauseMsec[10*watcherWait];
END;
ENDLOOP;
};
DumpDebuggerStateProc: ClickProc = { DumpDebuggerState[] };
RestartDebuggerProc: ClickProc = {
SoftcardOps.WriteLong[debuggerWatchSCAddr, 0];
TSOutPutRope["\tRestarted the debugger\n"];
};
DumpDebuggerState:
PROC = {
count: CARD16 ← numToDump;
seq: SoftcardOps.SeqLong = debuggerState;
addr: CARD32 ← 0;
index: CARD16 ← 0;
format: ROPE = " %04x";
TSOutPutRope["\n*****Dumping the state with no interpretation as yet\n"];
WHILE count >= 8
DO
TSOutPutF["%xH:", [cardinal[addr+index]] ];
FOR i:
CARD16
IN [0..8)
DO
TSOutPutF[format, [cardinal[seq[index+i]]] ];
ENDLOOP;
TSOutPutChar['\n];
index ← index + 8;
count ← count - 8;
ENDLOOP;
IF count # 0
THEN {
TSOutPutF["%xH:", [cardinal[addr+index]] ];
FOR i:
CARD16
IN [0..count)
DO
TSOutPutF[format, [cardinal[seq[index+i]]] ];
ENDLOOP;
TSOutPutChar['\n];
};
};
InterpSymbolProc: ClickProc = {
what: ROPE = ViewerTools.GetContents[interpretWhatText];
rp: ROPE;
addr: CARD32;
ok: BOOL;
IF what.Length[] = 0
THEN {
TSOutPutRope["\n Please fill in the Interpret \"what\" field\n"];
RETURN
};
[ok, addr] ← InterpretSymAsAddr[what, quadMemory];
IF ok =
FALSE
THEN {
Labels.Set[showInterpretResult, " ??? "];
RETURN;
};
Labels.Set[showInterpretResult, rp ← IO.PutFR["0%xH", [cardinal[addr]]] ];
TSOutPutF["Expression \"%g\" has dragon byte address %g\n",
[rope[what]], [rope[rp]] ];
};
InterpAddrProc: ClickProc = {
what: ROPE = ViewerTools.GetContents[interpretWhatText];
ok: BOOL;
rp: ROPE;
addr: CARD32;
[ok, addr] ← CheckedGetAsCard[interpretWhatText, "Interpret \"what\"", LAST[CARD32]];
IF ~ok THEN RETURN;
[ok, rp] ← InterpretAddrAsSym[addr, quadMemory];
Labels.Set[showInterpretResult, rp];
IF ok
THEN
TSOutPutF["The symbol for dragon byte address \"%g\" is : %g\n",
[rope[what]], [rope[rp]] ];
};
InterpretWhatProc: ClickProc =
{ ViewerTools.SetSelection[interpretWhatText, NIL] };
ReadQuadFile:
PROC[fileName:
ROPE]
RETURNS[mem: Memory] = {
quadFile: STREAM ← FS.StreamOpen[fileName];
count: INT ← 0;
list: LIST OF MemWord ← NIL;
symList: LIST OF SymbolAddr ← NIL;
numSyms: CARD16 ← 0;
mem ← NEW[MemoryRec ← [sym: SymTab.Create[]]];
DO
Skip:
PROC [source:
STREAM, c:
CHAR]
RETURNS[
BOOL] ~ {
ch: CHAR;
[] ← source.SkipWhitespace[];
IF (ch ← source.GetChar[]) # c
THEN
{
TSOutPutF[" Bad char %g (expected %g) at pos %g in quadFile %g - quitting\n",
[character[c]], [character[ch]], [integer[source.GetIndex[]]], [rope[fileName]] ];
RETURN[FALSE];
};
RETURN[TRUE];
};
tokenKind: IO.TokenKind;
token: ROPE;
addr, value: Word;
[tokenKind, token] ← quadFile.GetCedarTokenRope[! IO.EndOfStream => EXIT];
SELECT tokenKind
FROM
tokenDECIMAL,
tokenOCTAL,
tokenHEX => {
addr ← Convert.CardFromRope[token];
value ← 0;
IF ~Skip[quadFile, '/]
THEN {
quadFile.Close[];
RETURN[NIL];
};
THROUGH [0..4) DO value ← value*100H + quadFile.GetCard[] ENDLOOP;
list ← CONS[[addr, value], list];
count ← count+1
};
tokenID => {
IF ~Skip[quadFile, '=]
THEN {
quadFile.Close[];
RETURN[NIL];
};
addr ← quadFile.GetCard[];
symList ← CONS[NEW[SymbolAddrRec ← [token, addr]], symList];
numSyms ← numSyms + 1;
[] ← SymTab.Store[mem.sym, token, TVFromRef[NEW[Word ← addr]]]
};
tokenEOF => EXIT;
ENDCASE => {
TSOutPutF["\n****Unknown tokenKind in quadFile %g at pos %g - quitting\n",
[rope[fileName]], [integer[quadFile.GetIndex[]]] ];
quadFile.Close[];
RETURN[NIL];
};
ENDLOOP;
quadFile.Close[];
mem.wds ← NEW[MemWords[count]];
FOR i:
INT
DECREASING
IN [0..count)
DO
mem.wds[i] ← list.first;
list ← list.rest
ENDLOOP;
mem.lowestAddr ← mem.wds[0].addr;
mem.highestAddr ← mem.wds[count-1].addr;
IF numSyms # 0
THEN {
MySort: List.CompareProc = {
sa1: SymbolAddr = NARROW[ref1];
sa2: SymbolAddr = NARROW[ref2];
IF sa1.addr < sa2.addr THEN RETURN[less];
IF sa1.addr = sa2.addr THEN RETURN[equal];
RETURN[greater];
};
TRUSTED { symList ← LOOPHOLE[List.Sort[LOOPHOLE[symList], MySort] ] };
mem.sortedSyms ← NEW[SortedSyms[numSyms]];
FOR i:
CARD16
IN [0..numSyms)
DO
mem.sortedSyms[i] ← symList.first;
symList ← symList.rest;
ENDLOOP;
};
};
InterpretSymAsAddr:
PROC[rp:
ROPE, mem: Memory]
RETURNS[ok:
BOOL, addr: Word] = {
result: AMTypes.TV;
errorRope: ROPE;
noResult: BOOL;
refCard: REF;
[result, errorRope, noResult] ← Interpreter.Evaluate[rp, NIL, LIST[mem.sym]];
IF noResult
THEN {
addr ← 0;
TSOutPutF["\n*****Could not interpret \"%g\" - quitting\n", [rope[rp]] ];
RETURN[FALSE, addr]};
refCard ← RefFromTV[result];
WITH refCard
SELECT
FROM
word: REF Word => {RETURN[TRUE, word^]};
card: REF LONG CARDINAL => {RETURN[TRUE, card^]};
int:
REF
INT => {
IF int^ < 0 THEN RETURN[FALSE, 0];
RETURN[TRUE, int^]
};
ENDCASE => {
TSOutPutF["\n*****Unknown variant for \"%g\" - quitting\n", [rope[rp]] ];
RETURN[FALSE, 0];
};
};
InterpretAddrAsSym:
PROC[addr:
CARD32, mem: Memory]
RETURNS[ok:
BOOL, rp:
ROPE] = {
sortedSyms: REF SortedSyms = mem.sortedSyms;
num: CARD16 = IF sortedSyms = NIL THEN 0 ELSE sortedSyms.size;
ok ← FALSE;
rp ← " ??? ";
IF addr < mem.lowestAddr
THEN {
TSOutPutF["***%xH is smaller than any address (%xH)\n",
[cardinal[addr]], [cardinal[mem.lowestAddr]] ];
RETURN
};
IF addr > mem.highestAddr
THEN {
TSOutPutF["***%xH is larger than any address (%xH)\n",
[cardinal[addr]], [cardinal[mem.highestAddr]] ];
RETURN
};
IF sortedSyms =
NIL
THEN {
TSOutPutRope["\t*****Loaded file had no symbols\n"];
RETURN;
};
IF addr < sortedSyms[0].addr
THEN {
diff: CARD32 = sortedSyms[0].addr - addr;
RETURN[TRUE, IO.PutFR["%g-%xH", [rope[sortedSyms[0].sym]], [cardinal[diff]]] ];
};
IF addr > sortedSyms[num-1].addr
THEN {
diff: CARD32 = addr - sortedSyms[num-1].addr;
RETURN[TRUE, IO.PutFR["%g+%xH", [rope[sortedSyms[num-1].sym]], [cardinal[diff]]] ];
};
FOR i:
CARD16
IN [0..num)
DO
diff: CARD32;
IF addr > sortedSyms[i].addr THEN LOOP;
IF addr = sortedSyms[i].addr THEN RETURN[TRUE, sortedSyms[i].sym];
diff ← addr - sortedSyms[i-1].addr;
RETURN[TRUE, IO.PutFR["%g+%xH", [rope[sortedSyms[i-1].sym]], [cardinal[diff]]] ];
ENDLOOP;
};