perm filename IAUX1B.2[EAL,HE] blob sn#708940 filedate 1983-05-01 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00006 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	{$NOMAIN	More auxilliary routines for Interpreter }
C00023 00003	{ Externally defined routines: }
C00027 00004	(* aux io routines: prntSval, prntVec, prntTrans, prntStrng, prntPlist, onum, prntVar *)
C00033 00005	(* ppArmError: to display error messages from arms *)
C00036 00006	(* aux routines: addPdb, sleep, deClkQueue, msgDispatch, swap *)
C00057 ENDMK
C⊗;
{$NOMAIN	More auxilliary routines for Interpreter }

const

  (* Constants from EDIT *)

  maxLines = 28;
  maxPPLines = 18;
  maxBpts = 25;
  maxTBpts = 20;	(* max could be exceeded by huge case stmnt *)
  listinglength = 2000;	(* Length of Listingarray *)

(* Random type declarations for OMSI/SAIL compatibility *)

type
  byte = 0..255;	(* doesn't really belong here, but... *)
  ascii = char; 
  atext = text;

{ Define all the pointer types here }

vectorp = ↑vector;
transp = ↑trans;
strngp = ↑strng;
identp = ↑ident;
eventp = ↑event;
framep = ↑frame;
statementp = ↑statement;
varidefp = ↑varidef;
nodep = ↑node;
pdbp = ↑pdb;
envheaderp = ↑envheader;
enventryp = ↑enventry;
environp = ↑environment;
cmoncbp = ↑cmoncb;
messagep = ↑message;

(* This one is used whenever a pointer is needed for which the 	*)
(* definition is missing from this file; naturally, all 	*)
(* pointers use the same space 					*)

dump = ↑integer;
token = array[1..4] of integer;		{Uses same space as a token}
cursorp = array[1..4] of integer;	{Ditto, for cursorp}


(* datatype definitions *)

datatypes = (pconstype, varitype, svaltype, vectype, rottype, transtype,
	     frametype, eventtype, strngtype, labeltype, proctype, arraytype,
	     reftype, valtype, cmontype, nulltype, undeftype,
	     dimensiontype, mactype, macargtype, freevartype);

scalar = real;

u = (used,free);
vector = record case u of
	   used: (refcnt: integer; val: array [1..3] of real);
	   free: (next: vectorp);
	 end;

trans = record case u of
	   used: (refcnt: integer; val: array [1..3,1..4] of real);
	   free: (next: transp);
	end;

ident = record
	    next: identp;
	    length: integer;
	    name: strngp;
	    predefined: varidefp;
	  end;


cstring = packed array [1..10] of ascii;
c4str = packed array [1..4] of ascii;
c5str = packed array [1..5] of ascii;
c20str = packed array [1..20] of ascii;
linestr = packed array [1..130] of ascii;

strng = record
	  next: strngp;
	  ch: cstring;
	end;

event = record
	  next: eventp;		(* all events are on one big list *)
	  count: integer;
	  waitlist: pdbp;
	end;

frame = record
	  vari: varidefp;	(* back pointer to variable name & info *)
	  calcs: nodep;		(* affixment info *)
	  case ftype: boolean of	(* frame = true, device = false *)
  true:	    (valid: integer; val, fdepr: transp; dcntr: integer; dev: framep);
  false:    (mech: integer; case sdev: boolean of
		true: (sdest: real); false: (tdest,appr,depr: transp));
		(* sdev = true for scalar devices, false for frames *)
	end;



(* statement definitions *)

stmntypes = (progtype, blocktype, coblocktype, endtype, coendtype,
		fortype, iftype, whiletype, untiltype, casetype,
		calltype, returntype,
		printtype, prompttype, pausetype, aborttype, assigntype,
		signaltype, waittype, enabletype, disabletype, cmtype,
		affixtype, unfixtype,
		movetype,jtmovetype,operatetype,opentype,closetype,centertype,
		floattype, stoptype, retrytype,
		requiretype, definetype, macrotype, commenttype, dimdeftype,
		setbasetype, wristtype, saytype, declaretype, emptytype,
		evaltype, armmagictype);
		(* more??? *)

statement = packed record
		next, last: statementp;
		stlab: varidefp;
		exprs: nodep;	(* any expressions used by this statement *)
		nlines: integer;
		bpt,bad: boolean;
		case stype: stmntypes of
    commenttype:    (len: integer; str: strngp; cbody: statementp);
    dimdeftype:	    (dimname: varidefp; dimexpr: nodep);
    armmagictype:   (cmdnum,dev,iargs,oargs: nodep);
		end;



(* auxiliary definitions: variable, etc. *)

varidef = packed record
	    next,dnext: varidefp;
	    name: dump;
	    level: 0..255;	(* environment level *)
	    offset: 0..255;	(* environment offset *)
	    dtype: varidefp;	(* to hold the dimension info *)
	    tbits: 0..15;  (* special type bits: array = 1, proc = 2, ref = 4 & ? *)
	    dbits: 0..15;	(* for use by debugger/interpreter *)
	    case vtype: datatypes of
  arraytype:  (a: nodep);
  proctype:   (p: nodep);
  labeltype,
  cmontype:   (s: statementp);
  mactype:    (mdef: statementp);
  macargtype: (marg: dump);
  pconstype:  (c: nodep);
  dimensiontype: (dim: nodep);
	  end;



(* definition of the ubiquitous NODE record *)

nodetypes = (exprnode, leafnode, listnode, clistnode, colistnode, forvalnode,
		deprnode, viaptnode, apprnode, destnode, byptnode, durnode,
		sfacnode, wobblenode, swtnode, nullingnode, wristnode, cwnode,
		arrivalnode, departingnode,
		ffnode, forcenode, stiffnode, gathernode, cmonnode, errornode,
		calcnode, arraydefnode, bnddefnode, bndvalnode,
		waitlistnode, procdefnode, tlistnode, dimnode, commentnode,
		linearnode, elbownode, shouldernode, flipnode, wrtnode,
		loadnode,velocitynode);

exprtypes =  (	svalop,					(* scalar operators *)
		sltop, sleop, seqop, sgeop, sgtop, sneop,	(* relations *)
		notop, orop, xorop, andop, eqvop,		(* logical *)
		saddop, ssubop, smulop, sdivop, snegop, sabsop, (* scalar ops *)
		sexpop, maxop, minop, intop, idivop, modop,
		sqrtop, logop, expop, timeop,			(* functions *)
		sinop, cosop, tanop, asinop, acosop, atan2op,	(* trig *)
		vdotop, vmagnop, tmagnop,
		vecop,					(* vector operators *)
		vmakeop, unitvop, vaddop, vsubop, crossvop, vnegop,
		svmulop, vsmulop, vsdivop, tvmulop, wrtop,
		tposop, taxisop,
		transop,				(* trans operators *)
		tmakeop, torientop, ttmulop, tvaddop, tvsubop, tinvrtop,
		vsaxwrop, constrop, ftofop, deproachop, fmakeop, vmkfrcop,
		ioop,					(* i/o operators *)
		queryop, inscalarop,
		specop,					(* special operators *)
		arefop, callop, grinchop, macroop, vmop, adcop, dacop, jointop,
		badop,
		addop, subop, negop, mulop, divop, absop); (* for parsing *)

leaftypes = pconstype..strngtype;

reltypes = sltop..sgtop;
forcetypes = (force,absforce,torque,abstorque,angvelocity);

node = record
	next: nodep;
	case ntype: nodetypes of
    exprnode:	(op: exprtypes; arg1, arg2, arg3: nodep; elength: integer);
    leafnode:	(case ltype: leaftypes of
	varitype:  (vari: varidefp; vid: identp);
	pconstype: (cname: varidefp; pcval: nodep);
	svaltype:  (s: scalar; wid: integer);
	vectype:   (v: vectorp);
	transtype: (t: transp);
	strngtype: (length: integer; str: strngp) ); (* also used by commentnodes *)
    waitlistnode: (who: pdbp; when: integer);
    listnode:	(lval: nodep);
	end;



(* process descriptor blocks & environment record definitions *)

queuetypes = (nullqueue,nowrunning,runqueue,inputqueue,eventqueue,sleepqueue,
		forcewait,devicewait,joinwait,proccall);

pdb = packed record
	nextpdb,next: pdbp;	(* for list of all/active pdb's *)
	level: 0..255;		(* lexical level *)
	mode: 0..255;		(* expression/statement/sub-statement *)
	priority: 0..255;	(* probably never greater than 3? *)
	status: queuetypes;	(* what are we doing *)
	env: envheaderp;
	spc: statementp;	(* current statement *)
	epc: nodep;		(* current expression (if any) *)
	sp: nodep;		(* intermediate value stack *)
	cm: cmoncbp;		(* if we're a cmon point to our definition *)
	mech: framep;		(* current device being used *)
	linenum: integer;	(* used by editor/debugger *)
	 case procp: boolean of	(* true if we're a procedure *)
true:  (opdb: pdbp;		(* pdb to restore when procedure exits *)
	pdef: nodep);		(* procedure definition node *)
false: (evt: eventp;		(* event to signal when process goes away *)
	sdef: statementp);	(* first statement where process was defined *)
      end;


envheader = packed record
	      parent: envheaderp;
	      env: array [0..4] of environp;
	      varcnt: 0..255;		(* # of variables in use ??? *)
		case procp: boolean of  (* true if we're a procedure *)
	true: (proc: nodep);
	false:(block: statementp);
	    end;

enventry = record
	    case etype: datatypes of
  svaltype:  (s: scalar);
  vectype:   (v: vectorp);
  transtype: (t: transp);
  frametype: (f: framep);
  eventtype: (evt: eventp);
  strngtype: (length: integer; str: strngp);
  cmontype:  (c: cmoncbp);
  proctype:  (p: nodep; penv: envheaderp);
  reftype:   (r: enventryp);
  arraytype: (a: envheaderp; bnds: nodep);
	   end;


environment = record
		next: environp;
		vals: array [0..9] of enventryp;
	      end;


cmoncb = record
	   running, enabled: boolean;		(* cmon's status *)
	   cmon: statementp;
	   pdb: pdbp;
	   evt: eventp;
	   fbits: integer;			(* bits for force sensing *)
	   oldcmon: cmoncbp;			(* for debugger *)
	 end;


(* definition of AL-ARM messages *)

msgtypes = (initarmscmd,calibcmd,killarmscmd,wherecmd,
	    abortcmd,stopcmd,movehdrcmd,movesegcmd,
	    centercmd,operatecmd,movedonecmd,signalcmd,
	    setccmd,forcesigcmd,forceoffcmd,biasoncmd,biasoffcmd,setstiffcmd,
	    zerowristcmd,wristcmd,gathercmd,getgathercmd,readadccmd,writedaccmd,
	    errorcmd,floatcmd,setloadcmd,
	    armmagiccmd,realcmd,vectorcmd,transcmd);

errortypes = (noerror,noarmsol,timerr,durerr,toolong,featna,
	      unkmess,srvdead,adcdead,nozind,exjtfc,paslim,nopower,badpot,devbusy,
	      baddev,timout,panicb,nocart,cbound,badparm);

message = record
	   cmd: msgtypes;
	   ok: boolean;
	   case integer of
	1:   (dev, bits, n: integer;
(*	     (dev, bits, n, evt: integer;	(* for arm code version *)
	      evt: eventp;
	      dur: real;
	      case integer of
		1: (v1,v2,v3: real);
		2: (sfac,wobble,pos: real);
		3: (val,angle,mag: real);
		4: (max,min: real);
		5: (error: errortypes));
	2:   (fv1,fv2,fv3,mv1,mv2,mv3: real);	(* may never use these... *)
	3:   (t: array [1..6] of real);
	  end;

interr = record
         case integer of
           0: (i: integer);
	   1: (err,foo: errortypes);
	 end;

listingarray = packed array [0..listinglength] of ascii;


(* global variables *)

var
	(* from EDIT *)
    listing: listingarray;  (* first 150 chars are used by expression editor *)
			    (* next 40 by header & trailer lines *)
{*} cursorStack: array [1..15] of cursorp;	{These are BIG records! }
(*  lbuf: array [1..160] of ascii;
    ppBuf: array [1..100] of ascii; *)
    dum1: array[1..260] of ascii;
    lines: array [1..maxLines] of dump; 
    ppLines: array [1..maxPPLines] of dump;	
(*  marks: array [1..20] of integer;
    reswords: array [0..26] of reswordp;
    idents: array [0..26] of identp;
    macrostack: array [1..10] of tokenp;
    curmacstack: array [1..10] of varidefp;
    screenheight,dispHeight: integer;
    ppBufp,oppBufp,ppOffset,ppSize,nmarks: integer;
    lbufp,cursor,ocur,cursorLine,fieldnum,lineNum,findLine,pcLine: integer;
    firstDline,topDline,botDline,firstLine,lastLine,curLine: integer;
    freeLines,oldLines: linerecp;
    sysVars: varidefp;
    dProg: statementp;
    curBlock, newDeclarations, findStmnt: statementp;
    macrodepth: integer;
    filedepth, errCount, sCursor: integer;
    curChar, maxChar, curFLine, curPage: integer;
    nodim, distancedim, timedim, angledim,
      forcedim, torquedim, veldim, angveldim: varidefp;
    fvstiffdim, mvstiffdim: nodep;
    pnode: nodep;
*)  dum2: array[1..141] of dump;
(*  smartTerminal: boolean; 
    setUp,setExpr,setCursor,dontPrint,outFilep,collect,fParse,sParse,
      eofError,endOfLine,backup,expandmacros,flushcomments,checkDims,
      shownLine: boolean;
*)  dum3: array[1..16] of boolean;
    curtoken: token;
    file1,file2,file3,file4,file5,outFile: atext;

    bpts: array [1..maxBpts] of statementp;	(* debugging crap *)
    tbpts: array [1..maxTBpts] of statementp;
    debugPdbs: array [0..10] of pdbp;
(*  nbpts,ntbpts,debugLevel: integer;
    eCurInt: pdbp;
    STLevel: integer;
*)  dum4: array[1..5] of integer;
    singleThreadMode,tSingleThreadMode: boolean;

	(* from INTERP *)
    inputLine: array [1..20] of ascii;
    talk: text;			(* for using the speech synthesizer *)
    curInt, activeInts, readQueue, allPdbs: pdbp;
    sysEnv: envheaderp;
    clkQueue: nodep;
    allEvents: eventp;
    etime: integer;		(* used by eval *)
    curtime: integer; (* who knows where this will get updated - an ast? *)
    stime: integer;		(* used for clock queue on 10 *)
    msg: messagep;		(* for AL-ARM interaction *)
    inputp: integer;		(* current offset into inputLine array above *)
    resched, running, escapeI, iSingleThreadMode: boolean;
    msgp: boolean;		(* flag set if any messages pending *)
    inputReady: boolean;

(* various constant pointers *)
    xhat,yhat,zhat,nilvect: vectorp;
    niltrans: transp;
    gpark, rpark: transp;		(* arm park positions *)

(* various device & variable pointers *)
    speedfactor: enventryp;
    garm: framep;

{ Externally defined routines: }

	(* From ALLOC *)
function newVector: vectorp;					external;
procedure relVector(v: vectorp);				external;
function newTrans: transp;					external;
procedure relTrans(t: transp);					external;
function newNode: nodep;					external;
procedure relNode(n: nodep);					external;
procedure relEvent(n: eventp);					external;

	(* Display-related Routines *)
procedure ppLine; 						external;
procedure ppOutNow; 						external;
procedure ppChar(ch: ascii); 					external;
procedure pp5(ch: c5str; length: integer); 			external;
procedure pp10(ch: cstring; length: integer); 			external;
procedure pp10L(ch: cstring; length: integer);			external;
procedure pp20(ch: c20str; length: integer); 			external;
procedure pp20L(ch: c20str; length: integer); 			external;
procedure ppInt(i: integer); 					external;
procedure ppStrng(length: integer; s: strngp); 			external;

	(* In RSXRUT *)
procedure rcvmsg (var res,err: integer);			external;

	(* From IAUX1C.MAC *)
procedure getTime(var curTime: integer); 			external;
procedure clrTimeQueue; 					external;

	(* From IROOT *)
function i1bTaxis (t: transp): vectorp;				external;
function i1bTmagn (t: transp): scalar;				external;
procedure i1bSetVal (level, offset: byte); 			external;
function i1bGetVar (level, offset: byte): enventryp; 		external;

	(* From CALLER *)
procedure i1bPpReal(r: real); 					external;
procedure markTime(when: integer; var curTime,stime: integer);	external;

procedure iAux1bGet; external;
procedure iAux1bGet; begin end;	{ Used only to read in this overlay }


function pop: nodep;        (* Copied from IAUX1 - not externally available *)
 begin
 pop := curInt↑.sp;
 if curInt↑.sp = nil then
   begin                        (* **** error - stack underflow **** *)
   pp20L('Value Stack Underflo',20); ppChar('w'); ppLine;
   (* code to show where error occurred & to maybe recover??? *)
   end
  else curInt↑.sp := curInt↑.sp↑.next;
end;

(* aux io routines: prntSval, prntVec, prntTrans, prntStrng, prntPlist, onum, prntVar *)

procedure prntSval(s: real); external;
procedure prntSval;
 var si: real;
 begin
 if s < maxInt then
   begin
   si := trunc(s);
   s := si + round(1000*(s-si))/1000;
   end;
 i1bPpReal(s);
 end;

procedure prntVec(v: vectorp); external;
procedure prntVec;
 var i: integer;
 begin
 pp10('vector(   ',7);
 with v↑ do 
  for i := 1 to 3 do 
   begin
   prntSval(val[i]);
   if i = 3 then ppChar(')') else ppChar(',');
   end;
 ppOutNow;
 end;

procedure prntTrans(t: transp); external;
procedure prntTrans;
 var i: integer; v: vectorp;
 begin
 with t↑ do
  begin
  refcnt := refcnt + 1;
  pp10('trans(rot(',10);
  v := i1bTaxis(t); prntVec(v); relVector(v);
  ppChar(',');
  prntSval(i1bTmagn(t));
  pp10('),vector( ',9);
  for i := 1 to 3 do
   begin prntSval(val[i,4]); if i = 3 then ppChar(')') else ppChar(',') end;
  ppChar(')');
  refcnt := refcnt - 1;
  end;
 ppLine;
 end;

procedure prntStrng(length: integer; s: strngp); external; (* Also in EAUX3B *)
procedure prntStrng;
 begin
 ppStrng(length,s);
 ppOutNow;
 end;

procedure prntPlist(n: nodep); external;
procedure prntPlist;
 var np: nodep; b: boolean;

    (* The following is copied from IAUX1A *)
    function getNval(n: nodep; var b: boolean): nodep;
     begin
     b := false;
     with n↑ do
      if (ntype <> leafnode) or (ltype = varitype) then
	begin n := pop; b := true end;
     if n <> nil then
       if n↑.ltype = pconstype then
	 begin n := n↑.pcval; b := false end;
     getNval := n;
     end;

    (* The following is copied from IAUX1A *)
    procedure killNode(n: nodep);
     begin
     with n↑ do
      if ntype = leafnode then
	case ltype of
    vectype:   if v↑.refcnt <= 0 then relVector(v);
    transtype: if t↑.refcnt <= 0 then relTrans(t);
    otherwise {do nothing};
	end;
     relNode(n);
     end;

 begin
 while n <> nil do		(* print out the list *)
    begin
    np := getNval(n↑.lval,b);
    if np <> nil then
      begin
      with np↑ do
       case ltype of
svaltype:  begin prntSval(s); ppOutNow end;
vectype:   prntVec(v);
transtype: prntTrans(t);
strngtype: prntStrng(length,str);
otherwise {do nothing};
	end;
      if b then killNode(np);	(* flush used stack entry *)
      end;
    n := n↑.next;
    end;
 end;

procedure onum(s: integer); external;
procedure onum;

 procedure onum1(s: integer);
  var i,j: integer;
  begin
  i := s div 8;
  j := s mod 8;
  if i > 0 then onum1(i);
  ppInt(j);
  end;

 begin
 if s < 0 then begin ppChar('-'); s := -s end;
 onum1(s);
 ppOutNow;
 end;

procedure prntVar(v: nodep); external;
procedure prntVar;
 var i: integer; n,p: nodep;

 begin
  if v = nil then pp10('Noname    ',6)
  else if v↑.ntype = leafnode then
   with v↑.vid↑ do ppStrng(length,name)		(* print variable name *)
  else
   begin					(* array ref *)
   with v↑.arg1↑.vid↑ do ppStrng(length,name);	(* print variable name *)
   n := v↑.arg2;
   ppChar('[');
   while n <> nil do
    begin
    p := pop;					(* get this subscript's value *)
    i := round(p↑.s);
    ppInt(i);
    relNode(p);
    n := n↑.next;
    if n = nil then ppChar(']') else ppChar(',');
    end;
   end;
 ppLine;
 end;

procedure badJoints(angle: integer); external;
procedure badJoints;
 var i: integer;
 begin
 if angle <> 0 then
   begin			(* tell associated joint numbers *)
   pp20('   joint(s) =       ',14);
   i := 1;
   while angle <> 0 do		 (* decode them *)
    begin
    if odd(angle) then
      begin
      ppInt(i);
      if angle > 1 then ppChar(',');
      end;
    angle := angle div 2;
    i := i + 1;
    end;
   ppLine;
   end;
 end;

(* ppArmError: to display error messages from arms *)

procedure ppArmError(err: errortypes; angle: integer); external;
procedure ppArmError;
 begin
 if err = nopower then
   begin pp20('arm power not on    ',16); ppLine; end
  else if err = devbusy then
   begin pp20('device currently in ',20); pp5('use  ',4); ppLine end
  else
   begin
   case err of
srvdead:   pp10('servo dead',10);
adcdead:   pp10('a/d error ',9);
panicb:    pp20('panic button pushed ',19);
exjtfc:    begin pp20('excessive joint forc',20); ppChar('e'); end;
timout:    pp10('time out  ',8);
paslim:    pp20('joint out of range  ',18);
badpot:    pp20('bad pot on PUMA     ',15);
noarmsol:  pp20('No arm solution     ',16);
nocart:    begin pp20('No Cartesian path ex',20); pp20('ists between these p',20);
		 pp20('ath points.         ',11) end;
timerr:    begin pp20('Specified motion tim',20); pp20('e exceeds capabiliti',20);
		 pp5('es.  ',3) end;
durerr:    begin pp20('Motion overly constr',20); pp20('ained, will ignore g',20);
		 pp20('lobal time constrain',20); pp5('t.   ',2) end;
toolong:   begin pp20('Maximum segment time',20); pp20(' allowed is 32.2 sec',20);
		 pp5('onds.',5) end;
badparm:   pp20('Bad Magic Parameter ',19);
unkmess:   begin pp20('Unknown Message Type',20); pp20(' received from AL!  ',18) end;
nozind:    begin pp20('No Zero Index found ',20); pp20('( PUMA Encoder )    ',16) end;
baddev:    begin pp20('Device can''t perform',20); pp20(' commanded action   ',17) end;
cbound:    begin pp20('ARM Code compute bou',20); pp5('nd!  ',3) end;
featna:    begin pp20('Feature not availabl',20); pp10('e yet.    ',6) end;
otherwise  begin pp20('Unknown error! =    ',17); ppInt(ord(err)) end;
    end;
   badJoints(angle);     (* tell which joint(s) were bad, if any *)
   end;
end;

(* aux routines: addPdb, sleep, deClkQueue, msgDispatch, swap *)

procedure addPdb(var plist: pdbp; pn: pdbp); external;
procedure addPdb;
 var p,pp: pdbp; b: boolean;
 begin
 if plist = nil then
   begin				(* empty queue - we're it *)
   plist := pn;
   pn↑.next := nil;
   end
  else if plist↑.priority < pn↑.priority then
   begin				(* add us to start of queue *)
   pn↑.next := plist;
   plist := pn;
   end
  else
   begin				(* merge us into the queue *)
   p := plist;
   b := true;
   while (p↑.next <> nil) and b do
    if p↑.next↑.priority >= pn↑.priority then p := p↑.next else b := false;
   pn↑.next := p↑.next;
   p↑.next := pn;
   end;
 end;

procedure sleep(whenV: integer); external;
procedure sleep;
 var w,n,np: nodep; p,pp: pdbp; b: boolean; ti,oldtime: integer;
 begin
 curInt↑.next := nil;
 np := clkQueue;
 n := nil;
 b := true;
 if np <> nil then		(* ** Find correct value for "ti" *)
   begin
   oldTime := curTime;			(* need to save this value *)
   getTime(curTime);
   if stime <> 0 then
     begin
     ti := -((curtime - oldtime) MOD 18000); (* time since last scheduled *)
     repeat				(* about to be scheduled *)
      n := np;
      np := np↑.next;
      if np <> nil then ti := ti + np↑.when (* time til next entry is scheduled *)
       else
	begin
	ti := - ti;
	whenV := whenV + ti;	(* so it'll get scheduled at right time *)
	end;
     until ti >= 0;		(* point at first pending queue entry (if any) *)
     end
    else	(* time til first queue entry gets scheduled MOD 5 minutes *)
     ti := (oldTime - curTime) MOD 18000;
   curTime := oldTime;			(* restore it now *)
   end;
 while np <> nil do
  if ti = whenV then		(* add us to this wait node *)
    begin
    addPdb(np↑.who,curInt);
    np := nil;
    b := false;
    end
   else if ti < whenV then
    begin				(* move down list *)
    whenV := whenV - ti;		(* update relative wait time *)
    n := np;
    np := np↑.next;
    if np <> nil then ti := np↑.when;
    end
   else np := nil;
 if b then				(* need to make a new entry *)
   begin
   w := newNode;
   with w↑ do
    begin
    ntype := waitlistnode;
    who := curInt;
    when := whenV;
    next := nil;
    end;
  (* request a Marktime ast to have us made active *)
   if n = nil then
     begin
     w↑.next := clkQueue;
     clkQueue := w;				(* first entry in queue *)
     clrTimeQueue;				(* Cancel old MarkTime requests *)
     getTime(curTime);				(* Find out current time *)
     markTime(whenv,curTime,stime);		(* Make an AST for this one *)
     end
    else
     begin					(* add us to the queue *)
     w↑.next := n↑.next;
     n↑.next := w;
     end;
   if w↑.next <> nil then w↑.next↑.when := w↑.next↑.when - whenV;
   end;
 curInt↑.status := sleepqueue;
 curInt := nil;				(* swap in someone else *)
 resched := true;
 end;

procedure deClkQueue(po: pdbp); external;
procedure deClkQueue;
 var n,np: nodep; p,pp: pdbp; b: boolean;
 begin					(* remove pdb from clock queue *)
 n := clkQueue;
 np := nil;
 b := true;
 while (n <> nil) and b do
  begin
  p := n↑.who;
  pp := nil;
  while (p <> nil) and (p <> po) do begin pp := p; p := p↑.next end;
  if p <> nil then		(* found us, now splice us out of the list *)
    begin
    b := false;
    if pp = nil then
      begin				(* we were first entry in list *)
      n↑.who := p↑.next;
      if n↑.who = nil then		(* check if we were only entry *)
	begin				(* yup - remove this wait list node *)
	if np <> nil then np↑.next := n↑.next	(* splice out node *)
	 else
	  begin				(* we were first node *)
	  clrTimeQueue;			(* Cancel markTime request for this guy *)
	  clkQueue := n↑.next;
	  if n↑.next <> nil then 
	    markTime(clkQueue↑.when,curTime,stime);	(* Put next in queue *)
	  end;
	if n↑.next <> nil then n↑.next↑.when := n↑.when + n↑.next↑.when;
	relNode(n);			(* done with waitlist node now *)
	end
      end
     else pp↑.next := p↑.next;			(* splice us out of list *)
    end
   else begin np := n; n := n↑.next end;	(* try next node *)
  end;
 end;

procedure msgDispatch; external;	(* handles signals & movedone from ARM *)
procedure msgDispatch;
 const
	GARMDEV   = 1;
	GHANDDEV  = 2;
	RARMDEV   = 3;
	RHANDDEV  = 4;
	DRIVERDEV = 5;
	VISEDEV   = 6;
 var p,po: pdbp; nd,np: nodep; nvari: varidefp; i,j,k: integer; b: boolean;
     entry: enventryp;

    procedure freeEvent(e: eventp);	(* Copied from IAUX1 *)
     var eo: eventp; b: boolean;
     begin                                  (* remove event from list *)
     if allEvents = e then begin allEvents := e↑.next; b := true end
      else
       begin
       eo := allEvents;
       b := false;
       repeat                               (* find event in list *)
	if eo↑.next = e then b := true else eo := eo↑.next
       until b or (eo = nil);
       if b then eo↑.next := e↑.next;       (* splice us out of list *)
       end;
     if b then relEvent(e);         (* if not in list already released *)
     end;

    (* The following is copied from IAUX1A *)
    procedure killNode(n: nodep);
     begin
     with n↑ do
      if ntype = leafnode then
	case ltype of
    vectype:   if v↑.refcnt <= 0 then relVector(v);
    transtype: if t↑.refcnt <= 0 then relTrans(t);
    otherwise {do nothing};
	end;
     relNode(n);
     end;

    function GetArm: boolean;	(* Copied from RSXMSG *)
     var res,err:integer;
     begin
     rcvmsg(res,err);               { Go to Root portion to actually do it }
     if err<>0 then
       begin                        { Report the error }
       writeln('Error in getArm; err.flag=',err:2);
       break(output);
       end;
     GetArm := (res<>0);            { Set TRUE if nonzero }
     end;

    procedure push (n: nodep);		(* From in iaux1a *)
     begin                          (* no need to check for overflow *)
     n↑.next := curInt↑.sp;
     curInt↑.sp := n;
     end;

 begin {msgDispatch}
 with msg↑ do
  if cmd = errorcmd then
    begin
    if ok then pp20L('Fatal error:        ',13)
     else pp10L('Warning:  ',9);
    case dev of				(* tell which device *)
garmdev:   pp10('garm -    ',7);
ghanddev:  pp10('ghand -   ',8);
rarmdev:   pp10('rarm -    ',7);
rhanddev:  pp10('rhand -   ',8);
driverdev: pp10('driver -  ',9);
visedev:   pp10('vise -    ',7);
otherwise  pp20('unknown device -    ',18);
     end;
    ppArmError(error,bits);
    end
   else
    begin
    evt↑.count := evt↑.count + 1;
    p := evt↑.waitlist;		(* get pdb of process to schedule (if any) *)
    if p <> nil then
      begin
      evt↑.waitlist := p↑.next;		(* remove node from waitlist *)
      p↑.status := runqueue;
      addPdb(activeInts,p);		(* add it to active process list *)
      if curInt = nil then resched := true
       else if p↑.priority > curInt↑.priority then
	resched := true;		(* swap it in and swap us out *)
      if cmd = movedonecmd then
	begin				(* need to put error bits on stack *)
	nd := newNode;
	with nd↑ do
	 begin
	 ntype := leafnode;
	 ltype := svaltype;
	 if ok then s := 0 else s := 128 * ord(error) + bits;
	 next := p↑.sp;			(* push it *)
	 p↑.sp := nd;
	 end;
	freeEvent(evt);			(* also need to reclaim event *)
	end
       else if cmd = armmagiccmd then
	begin
	po := curInt;
	curInt := p;			(* swap process in temporarily *)
	j := n;			(* get number of args being passed back *)
	np := p↑.spc↑.oargs;
	for i := 1 to j do
	 begin			(* get the results of the arm magic cmd *)
	 repeat until getArm;		(* read next message *)
	 b := np <> nil;
	 if b then
	   with np↑.lval↑ do
	    if ntype = leafnode then nvari := vari else nvari := arg1↑.vari;
	 nd := newNode;
	 nd↑.ntype := leafnode;
	 nd↑.ltype := svaltype;		(* (so killnode will be happy) *)
	 if cmd = realcmd then
	   begin
	   nd↑.s := dur;		(* copy returned scalar *)
	   if b then b := nvari↑.vtype = svaltype;
	   end
	  else if cmd = vectorcmd then
	   begin
	   nd↑.ltype := vectype;
	   nd↑.v := newVector;		(* copy returned vector *)
	   with nd↑.v↑ do
	    begin val[1] := v1; val[2] := v2; val[3] := v3 end;
	   if b then b := nvari↑.vtype = vectype;
	   end
	  else if cmd = transcmd then
	   begin
	   nd↑.ltype := transtype;
	   nd↑.t := newTrans;		(* copy returned trans *)
	   with nd↑.t↑ do
	    begin
	    for k := 1 to 3 do begin t[k] := val[k,1]; t[k+3] := val[k,2] end;
	    repeat until getArm;	(* read second packet of trans *)
	(* ??? should probably check that it's a transcmd, but.... ??? *)
	    for k := 1 to 3 do begin t[k] := val[k,3]; t[k+3] := val[k,4] end;
	    end;
	   if b then b := nvari↑.vtype in [rottype,transtype,frametype];
	   end
	  else
	   begin		(* !!! this should never happen!!! *)
	   pp20L('Bad message received',20); pp20(' during ARM MAGIC - ',20);
	   pp10('Good Luck!',10); ppLine;
	   b := false;
	   end;
	 if np = nil then killNode(nd)	(* flush unwanted value *)
	  else
	   begin
	   if b then
	     begin			(* store value away in variable *)
	     push(nd);			(* first push value onto stack *)
	     with nvari↑ do
	      i1bSetVal(level,offset);	(* store value into variable *)
	     end
	    else
	     begin
	     pp20L('Datatype of value re',20); pp20('turned from ARM MAGI',20);
	     pp20('C does not match    ',16); ppLine;
	     killNode(nd);		(* flush unwanted value *)
	     with nvari↑ do		(* pop any subscripts off of stack *)
	      entry := i1bGetVar(level,offset);	(* look up env entry *)
	   (* ??? should we zero it instead of leaving it unchanged ??? *)
	     end;
	   np := np↑.next;
	   end;
	 end;
	if np <> nil then
	  begin
	  pp20L('Not enough values pa',20); pp20('ssed back from ARM M',20);
	  pp5('AGIC ',4); ppLine;
	  while np <> nil do
	   begin		(* clear any subscripts off of the stack *)
	   with np↑.lval↑ do
	    if ntype <> leafnode then 
	     with arg1↑.vari↑ do
	      entry := i1bGetVar(level,offset);	(* look up env entry *)
	(* ??? should we zero it instead of leaving it unchanged ??? *)
	   np := np↑.next;
	   end
	 end;
	curInt := po;			(* restore current process *)
	end
       else if cmd <> signalcmd then
	begin pp20('Unknown message of t',20); pp5('ype: ',5);
	      ppInt(ord(cmd)); ppLine end;
      end;
    end;
 end;

procedure swap(newp: pdbp); external;
procedure swap;
 var p,po: pdbp; b: boolean; e: eventp;
 begin
 if newp = nil then
   begin			(* swap in some active process *)
   curInt := activeInts;
   if activeInts <> nil then activeInts := activeInts↑.next;
   end
  else
   begin
   if newp↑.status = runqueue then
     begin			(* remove us from activeInts list *)
     if activeInts = newp then activeInts := newp↑.next;
     p := activeInts;
     while p↑.next <> nil do
      if p↑.next = newp then p↑.next := newp↑.next	(* remove us *)
       else p := p↑.next;
     end
    else if newp↑.status = sleepqueue then deClkQueue(newp)
    else if newp↑.status = eventqueue then
     begin	(* run through all events & remove us from event queue *)
     e := allEvents;
     b := true;
     while b and (e <> nil) do
      with e↑ do
       begin
       if waitlist = newp then
	 begin waitlist := newp↑.next; b := false end
	else
	 begin
	 p := waitlist;
	 while b and (p <> nil) do
	  if p↑.next = newp then
	    begin p↑.next := newp↑.next; b := false end
	   else p := p↑.next;
	 end;
       if b then e := next else count := count + 1;
       end;
     end;
   if (newp <> curInt) and (curInt <> nil) then
     begin
     curInt↑.status := runqueue;
     addPdb(activeInts,curInt);	(* swap current process out *)
     end;
   curInt := newp;		(* make new guy active *)
   newp↑.next := nil;
   end;
 if curInt <> nil then
   begin curInt↑.status := nowrunning; curInt↑.next := nil end;
 end;