Z.debug,%(Z)debug,MTUNV,C51,5.1,04.03.98.0855,
IF{B'=#NN;
A^TITLE,
@1,
1^S,
@7,
DO{@19},
D(23,0)^#},
END;

[1]::OPEN.WINDOW
@2^BOT^F,
"A"^TOP^TF,
@3,
1^INPUT.ROW,
INPUT.ROW+2^PREFIX.ROW^MAIN.ROW,
26-MAIN.ROW/4:0D^HISTORY.HT*4^MAIN.HT^PREFIX.HT,
MAIN.ROW^LOCATION.ROW,
MAIN.ROW+4^SYMBOLS.ROW,
26-SYMBOLS.ROW^SYMBOLS.HT,
@5;

[2]::GET.ERROR.FRAME
D(E(^A)-IF{TITLE 1;2}+1);

[3]::GET.OPEN.PREFIXES
""^N,
DO{N@ZNP^N @4};

[4]::RECORD.PREFIX
N^O[N@ZPN],
NUM.PREFIXES+1^NUM.PREFIXES;

[5]::START
IF{TITLE D(23,4,26,92,8)_{"WJMBreakpoint: "_TITLE}^#;
D(23,4,26,92,0)_{"wjmDebugger"}^#},
D(1,15)_D(6,0,0,0,26,92,6,9,6,11)^#,
@6;

[6]::DISPLAY.WHAT.NEXT
D(1,15)_D(6,0,0,0,2,92,6,9,6,11)_D(1,2)^#,
P(0,0,"I,P,H,G,F,A,M,R,E,S,<,;,!,?,<F1>,[help],<exit>":92C)^#;

[7]::DO.HISTORY
IF{(PL="H") D(7)^#;
"H"^PL,
D(1,1)_P(MAIN.ROW-1,0,"History":92C)^#,
D(1,15)_D(6,0,MAIN.ROW,0,MAIN.HT,92,6,9,6,11)^#,
"H"^PL,
@8},
""^S;

[8]::DISPLAY.HISTORY
IF{IF{S;
'FRAME.POS;
@9,
@10} @11,
@13},
@18;

[9]::UNHIGHLIGHT.CURRENT.FRAME
D(16,FRAME.POS+MAIN.ROW,0,24,5,92)_D(16,FRAME.POS+MAIN.ROW+1,0,24,5,92)_D(16,FRAME.POS+MAIN.ROW+2,0,24,5,92)^#;

[10]::FRAME.NOT.ON.SCREEN
E(F)-E(TF)^DIST,
DIST*4^FRAME.POS,
(DIST<0!(DIST'<HISTORY.HT));

[11]::FIND.TOP.FRAME
HISTORY.HT^MF,
F^CF,
DO{F'=TOP&(MF>1) MF-1^MF,
@12^F},
F^TF;

[12]::PREV.FRAME
D(E(F)-1);

[13]::DISPLAY.HISTORY.FRAMES
1^MF,
D(1,15)_D(6,0,MAIN.ROW,0,MAIN.HT,92,6,9,6,11)_P(MAIN.ROW,0)^#,
DO{@14,
IF{F=CF MF-1*4^FRAME.POS},
MF<HISTORY.HT&(BOT'=F) @17^F,
MF+1^MF,
N("")N^#},
CF^F,
D(E(F),0,0,0)^T;

[14]::SHOW.LOCATION
@15,
I(@16^LTMP_":"_IF{LTMP'>70 F}:(5_"L")_R:92L)^#,
N(" ":5_BE)^#,N(" ":5_AE)^#;

[15]::GET.LOCATION
F@ZEL^{R,BE,AE,b};

[16]::FRAME.NO
E(F)-E(TOP);

[17]::NEXT.FRAME
D(E(F)+1);

[18]::HIGHLIGHT.CURRENT.FRAME
D(16,FRAME.POS+MAIN.ROW,0,24,4,92)_D(16,FRAME.POS+MAIN.ROW+1,0,24,4,92)_D(16,FRAME.POS+MAIN.ROW+2,0,24,4,92)^#;

[19]::PROCESS.INPUT
@20,
IF{I=E("P") @21;
I=E("R") @25("");
I=E("!") @26;
I=E("G") .,
@28;
IF{I=E("A");
I=E("M")} IF{(PL="P") D(7)^#;
@29,
#Y=27!'MS;
I=E("A") @30;
@31};
I=E("H") @7;
I=E("I") @32;
I=E("E") @38;
I=E("L") @40("");
I=E("V") @43("");
I=E("<") @44;
I=E("S") @46;
I=E(";") @47;
I=133 @49;
I=134 @51;
I=131 @52;
I=132 @56;
I=E("F") IF{(PL="P") D(7)^#;
@64,
#Y=27!'NF;
@50};
I=142!(I=194) @66;
I=143!(I=193) @67;
I=137!(I=E("?"))!(I=209) @68;
I'=27 D(7)^#},
I'=27;

[20]::READ
E(#S@LU)^I;

[21]::DO.PREFIXES
""^FRAME.POS,
IF{(PL="P") D(7)^#;
"P"^PL,
D(1,1)_P(PREFIX.ROW-1,0,"Prefixes":92C)^#,
@22};

[22]::DISPLAY.PREFIXES
+O[""]^P,
@23;

[23]::SHOW.PREFIXES
D(1,15)_D(6,0,PREFIX.ROW,0,PREFIX.HT,92,6,9,6,11)^#,
IF{P 0^PN,
P^TP,
DO{@24,
IF{PB "";
+O[P]^P&(PN<PREFIX.HT)}}};

[24]::SHOW.PREFIX
P(PN+PREFIX.ROW,0,P)_P(PN+PREFIX.ROW,L(P)+2!5)^#,
O([O[P]]):0X^PNM$(92-5)^#,
PN+1^PN,
PNM'$(92-5)^PNM,
DO{PN<PREFIX.HT&PNM N(PNM$92)^#,
PNM'$92^PNM,
PN+1^PN},
PNM^PB;

[25]::DO.REGISTERS
#mA^b,@ZER^x,x^#mW[b],
#M[3,0,4,"Registers",b,Q("Sure, but what does it all Mean?!")],
#mF[b];

[26]::DO.SAVE.INFO
"Save error log"^MSG,
@27,
%(MIS)ERROR..save.info("");

[27]::W.MSG
3^WTP,4^FNT,
#M[WTP,FNT,4,"Message",MSG,`&OK'];

[28]::DO.MAGNIFY
%(Z)g("");

[29]::ASK.SYMBOL
D(1,2)_P(0,0,"Which Symbol?":92C)^#,
#R[1,0,92]^MS,
@6;

[30]::DO.ASCII
%(Z)ascii([T,MS]);

[31]::DO.MEMBUF
%(Z)membuf(IF{MS?0N;[T,MS]});

[32]::DO.INSPECT.FRAME
""^FRAME.POS,
IF{(PL="I") D(7)^#;
"I"^PL,
D(1,1)^#,
P(LOCATION.ROW-1,0,"Frame":92C)_P(SYMBOLS.ROW-1,0,"Symbols":92C)^#,
D(1,15)_D(6,0,LOCATION.ROW,0,2,92,6,9,6,11)_D(6,0,SYMBOLS.ROW,0,SYMBOLS.HT,92,6,9,6,11)^#,
@33};

[33]::DISPLAY.FRAME
@34,
@35;

[34]::DISPLAY.LOCATION
D(1,15)_P(LOCATION.ROW,0)^#,
@14;

[35]::DISPLAY.SYMBOLS
D(E(F),0,0,0)^T,
+[T,""]^M,
@36;

[36]::SHOW.SYMBOLS
D(1,15)_D(6,0,SYMBOLS.ROW,0,SYMBOLS.HT,92,6,9,6,11)^#,
IF{M 0^NS,
[T,M^TM]^Z,
DO{@37,
IF{MB "";
+([T,M],Z)^M&(NS<SYMBOLS.HT)}}};

[37]::SHOW.SYMBOL
P(NS+SYMBOLS.ROW,0,M)_P(NS+SYMBOLS.ROW,L(M)+2!10)^#,
Z:0X^Z$(92-10)^#,
NS+1^NS,
Z'$(92-10)^Z,
DO{NS<SYMBOLS.HT&Z N(Z$92)^#,
Z'$92^Z,
NS+1^NS},
Z^MB;

[38]::DO.EDIT.PROGRAM
IF{O(/(.S)) @15,
@39,
O(/S,0P),%(MIS)EDIT..editor.in.its.own.window(PRG,PRG.TP,"OBJ",b),C(/U);
"Unavailable"^MSG,
@27};

[39]::GET.PROGRAM.NAME.AND.TYPE
R#"0,"^PRG,
%(MIS)EDIT..split.proc.name(PRG)^STUFF,
IF{STUFF|2 "NPR"^PRG.TP;"$T"^PRG.TP};

[40]::DO.MAGIC.LISTING
@ZER:0X^X,DO{X#"0 "@LU^Z,IF{Z="P" X#"1 "^PROG;Z="CS" X#"1 "^CODE},X%"0 "%0^X},
@41(PROG)-@41(CODE)^Z,
@42(Z)^ERRLOC,0:(8-L(ERRLOC))_ERRLOC^ERRLOC,
%(NPR)MAGIC.SRC..view.listing("VMAGIC","",ERRLOC);

[41]::HEX.TO.DEC
DO{A#0@LU^d,r*16+IF{d?1N;E(d)-55}^r,A%0^A},r;

[42]::DEC.TO.HEX
"0123456789ABCDEF"^x,DO{A\16^B,x#B_r^r,A/16^A>0},r;

[43]::DO.SHOW.VERSION
%(NPR)MAGIC.SRC..info("")^x,
#mA^b,
("Executable version",U(x),"linked",U(x)_";")^y,
y^#mW[b],
(" VMagic version",U(x),"entered",U(x),"assembled",@VN_";")^y,
y^#mW[b],
(" Term version",U(x),"entered",U(x),"assembled",#VN)^y,
y^#mW[b],
#M[3,4,4,"Version info",b,Q("That's Good to Know!")],
#mF[b];

[44]::DO.SCREEN
"To return to the debugger after viewing the screen, press any key."^MSG,
@27,
D(23,0)^#,
#S,
@45;

[45]::RESTORE
@5,
1^S,
""^PL,
@7;

[46]::DO.ZGSYM
%(Z)g(D(E(F),0,0,0));

[47]::DO.EVAL
O(/(.DBG)S,"P"),
@48,
IF{/(.DBG)EXP G([T],/(.DBG)TABLE),
%(Z)debug.eval.exp(""),
G(/(.DBG)TABLE,[T]),
IF{(PL="I") @35},
3^WTP,4^FNT,
#M[WTP,FNT,4,"VALUE","Value:  "_/(.DBG)VAL,`&OK']},
C(/(.DBG)U);

[48]::ASK.EXPRESSION
D(1,2)_P(0,0,"Expression?":92C)^#,
#R[1,0,92]^/(.DBG)EXP,
@6;

[49]::DO.UP
IF{(PL="P") D(7)^#;
F=TOP D(7)^#;
@12^NF,
@50};

[50]::DO.GOTO
IF{(PL="P") D(7)^#;
F=NF D(7)^#;
NF^F,
(PL="H") @8;
@33};

[51]::DO.DOWN
IF{(PL="P") D(7)^#;
F=BOT D(7)^#;
@17^NF,
@50};

[52]::DO.NEXT.PAGE
IF{(PL="P") @53;
(PL="H") @54;
@55};

[53]::PREFIXES.NEXT.PAGE
IF{'P D(7)^#;
@23};

[54]::HISTORY.NEXT.PAGE
TF^CF,
D(E(TF)+HISTORY.HT)^TMP,
IF{TMP>BOT D(7)^#;
TMP^TF^F^CF,
@13},
@18;

[55]::SYMBOLS.NEXT.PAGE
IF{'M D(7)^#;
@36};

[56]::DO.PREV.PAGE
IF{(PL="P") @57;
(PL="H") @60;
@61};

[57]::PREFIXES.PREV.PAGE
IF{-O[TP] TP^P,
@58,
@23;
D(7)^#};

[58]::FIND.PREV.TOP.PREFIX
0^PN,
DO{-O[P]^P&(PN+@59^PN<PREFIX.HT)},
IF{'P +O[""]^P};

[59]::PNAME.HT
IF{P (L(O([O[P]]):0X)-(92-5)+91/92:0D+1)};

[60]::HISTORY.PREV.PAGE
IF{TF=TOP D(7)^#;
TF^CF,
D(E(TF)-HISTORY.HT)!TOP^TF,
IF{@10 D(E(TF)+HISTORY.HT-1);
F}^CF,
TF^F,
@13},
@18;

[61]::SYMBOLS.PREV.PAGE
IF{-[T,TM] TM^M,
@62,
@36;
D(7)^#};

[62]::FIND.PREV.TOP.SYMBOL
0^NS,
DO{-([T,M],Z)^M&(NS+@63^NS<SYMBOLS.HT)},
IF{'M +[T,""]^M};

[63]::VALUE.HT
(L(Z:0X)-(92-10)+91/92:0D+1);

[64]::ASK.NEW.FRAME
D(1,2)_P(0,0,"Which Frame?":92C)^#,
""^NF^ATMP,
DO{ATMP^#R[1,0,3]@LU^ATMP,
IF{ATMP?0N'<0<256 D(E(TOP)+ATMP);ATMP}^NF,
NF<TOP!(NF>BOT)&(#Y'=27) "Frame out-of-bounds"^MSG,
@65},
@6;

[65]::W.ERR
D(7)^#,
3^WTP,4^FNT,
#M[WTP,FNT,3,"Warning",MSG,`&OK'];

[66]::DO.GOTO.TOP
IF{(PL="P") +O[""]^P,
IF{P=TP D(7)^#;
P @23;
D(7)^#};
(PL="H") TOP^NF,
@50;
+[T,""]^M,
M=TM D(7)^#;
M @36;
D(7)^#};

[67]::DO.GOTO.BOTTOM
IF{(PL="P") ""^TP,
@57;
(PL="H") BOT^NF,
@50;
""^TM,
@61};

[68]::DO.HELP
D(23,4,26,92,0)_{"Debugger Help"}^#,
T("The Following Commands are Available:  ([] indicates MEDITECH labeling)")^#,
N("I     Inspect the Current Frame")^#,
N("P     Display the Open Prefixes")^#,
N("H     Display the History (this is what is initially displayed)")^#,
N("G     Inspect Prefixes using %Z.g")^#,
N("F     Go to a Specific Frame (Prompts for Frame -- either letter or non-negative integer)")^#,
N("A/M   Show the ascii values in a symbol (A) or memory buffer (M) using %Z.ascii or")^#,
N("      %Z.membuf.  (Prompts for a symbol in the current frame.)")^#,
N("R     Show the contents of the registers")^#,
N("E     Invoke the program editor on the object code for the Current Frame")^#,
N("S     Invoke %Z.g to show the symbol table of the Current Frame")^#,
N("<     Display the screen as it was just before the error occured. (Press any key to")^#,
N("      return to the Debugger)")^#,
N(";     Evaluate an expression within the environment of the current frame.  Prompts for an")^#,
N("      expression.  WARNING:  *Any* error which occurs during the evaluation of the")^#,
N("      expression will terminate the debugging session.")^#,
N("!     Save error log.")^#,
NN("<F1> or ? or [help]         Displays this Message")^#,
N("<up> and <down>             Move up or down a Frame")^#,
N("<page down> and <page up>   Move through the information displayed")^#,
N("<cntl-home> or [begin]      Move to the top of the information displayed")^#,
N("<cntl-end> or [end]         Move to the bottom of the information displayed")^#,
N("<exit>                      Quit the Debugger")N^#,
N("Type any key to return to the Debugger")^#,
#S,
D(23,0)^#;

