em.i 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. { This is an interpreter for EM. It serves as the official machine
  2. definition. This interpreter must run on a machine which supports
  3. arithmetic with words and memory offsets.
  4. Certain aspects of the definition are over specified. In particular:
  5. 1. The representation of an address on the stack need not be the
  6. numerical value of the memory location.
  7. 2. The state of the stack is not defined after a trap has aborted
  8. an instruction in the middle. For example, it is officially un-
  9. defined whether the second operand of an ADD instruction has
  10. been popped or not if the first one is undefined ( -32768 or
  11. unsigned 32768).
  12. 3. The memory layout is implementation dependent. Only the most
  13. basic checks are performed whenever memory is accessed.
  14. 4. The representation of an integer or set on the stack is not fixed
  15. in bit order.
  16. 5. The format and existence of the procedure descriptors depends on
  17. the implementation.
  18. 6. The result of the compare operators CMI etc. are -1, 0 and 1
  19. here, but other negative and positive values will do and they
  20. need not be the same each time.
  21. 7. The shift count for SHL, SHR, ROL and ROR must be in the range 0
  22. to object size in bits - 1. The effect of a count not in this
  23. range is undefined.
  24. }
  25. .bp
  26. {$i256} {$d+}
  27. program em(tables,prog,input,output);
  28. label 8888,9999;
  29. const
  30. t15 = 32768; { 2**15 }
  31. t15m1 = 32767; { 2**15 -1 }
  32. t16 = 65536; { 2**16 }
  33. t16m1 = 65535; { 2**16 -1 }
  34. t31m1 = 2147483647; { 2**31 -1 }
  35. wsize = 2; { number of bytes in a word }
  36. asize = 2; { number of bytes in an address }
  37. fsize = 4; { number of bytes in a floating point number }
  38. maxret =4; { number of words in the return value area }
  39. signbit = t15; { the power of two indicating the sign bit }
  40. negoff = t16; { the next power of two }
  41. maxsint = t15m1; { the maximum signed integer }
  42. maxuint = t16m1; { the maximum unsigned integer }
  43. maxdbl = t31m1; { the maximum double signed integer }
  44. maxadr = t16m1; { the maximum address }
  45. maxoffs = t15m1; { the maximum offset from an address }
  46. maxbitnr= 15; { the number of the highest bit }
  47. lineadr = 0; { address of the line number }
  48. fileadr = 4; { address of the file name }
  49. maxcode = 8191; { highest byte in code address space }
  50. maxdata = 8191; { highest byte in data address space }
  51. { format of status save area }
  52. statd = 4; { how far is static link from lb }
  53. dynd = 2; { how far is dynamic link from lb }
  54. reta = 0; { how far is the return address from lb }
  55. savsize = 4; { size of save area in bytes }
  56. { procedure descriptor format }
  57. pdlocs = 0; { offset for size of local variables in bytes }
  58. pdbase = asize; { offset for the procedure base }
  59. pdsize = 4; { size of procedure descriptor in bytes = 2*asize }
  60. { header words }
  61. NTEXT = 1;
  62. NDATA = 2;
  63. NPROC = 3;
  64. ENTRY = 4;
  65. NLINE = 5;
  66. SZDATA = 6;
  67. escape1 = 254; { escape to secondary opcodes }
  68. escape2 = 255; { escape to tertiary opcodes }
  69. undef = signbit; { the range of integers is -32767 to +32767 }
  70. { error codes }
  71. EARRAY = 0; ERANGE = 1; ESET = 2; EIOVFL = 3; EFOVFL = 4;
  72. EFUNFL = 5; EIDIVZ = 6; EFDIVZ = 7; EIUND = 8; EFUND = 9;
  73. ECONV = 10; ESTACK = 16; EHEAP = 17; EILLINS = 18; EODDZ = 19;
  74. ECASE = 20; EMEMFLT = 21; EBADPTR = 22; EBADPC = 23; EBADLAE = 24;
  75. EBADMON = 25; EBADLIN = 26; EBADGTO = 27;
  76. .ne 20
  77. .bp
  78. {---------------------------------------------------------------------------}
  79. { Declarations }
  80. {---------------------------------------------------------------------------}
  81. type
  82. bitval= 0..1; { one bit }
  83. bitnr= 0..maxbitnr; { bits in machine words are numbered 0 to 15 }
  84. byte= 0..255; { memory is an array of bytes }
  85. adr= {0..maxadr} long; { the range of addresses }
  86. word= {0..maxuint} long;{ the range of unsigned integers }
  87. offs= -maxoffs..maxoffs; { the range of signed offsets from addresses }
  88. size= 0..maxoffs; { the range of sizes is the positive offsets }
  89. sword= {-signbit..maxsint} long; { the range of signed integers }
  90. full= {-maxuint..maxuint} long; { intermediate results need this range }
  91. double={-maxdbl..maxdbl} long; { double precision range }
  92. bftype= (andf,iorf,xorf); { tells which boolean operator needed }
  93. insclass=(prim,second,tert); { tells which opcode table is in use }
  94. instype=(implic,explic); { does opcode have implicit or explicit operand }
  95. iflags= (mini,short,sbit,wbit,zbit,ibit);
  96. ifset= set of iflags;
  97. mnem = ( NON,
  98. AAR, ADF, ADI, ADP, ADS, ADU,XAND, ASP, ASS, BEQ,
  99. BGE, BGT, BLE, BLM, BLS, BLT, BNE, BRA, CAI, CAL,
  100. CFF, CFI, CFU, CIF, CII, CIU, CMF, CMI, CMP, CMS,
  101. CMU, COM, CSA, CSB, CUF, CUI, CUU, DCH, DEC, DEE,
  102. DEL, DUP, DUS, DVF, DVI, DVU, EXG, FEF, FIF, FIL,
  103. GTO, INC, INE, INL, INN, IOR, LAE, LAL, LAR, LDC,
  104. LDE, LDF, LDL, LFR, LIL, LIM, LIN, LNI, LOC, LOE,
  105. LOF, LOI, LOL, LOR, LOS, LPB, LPI, LXA, LXL, MLF,
  106. MLI, MLU, MON, NGF, NGI, NOP, RCK, RET, RMI, RMU,
  107. ROL, ROR, RTT, SAR, SBF, SBI, SBS, SBU, SDE, SDF,
  108. SDL,XSET, SIG, SIL, SIM, SLI, SLU, SRI, SRU, STE,
  109. STF, STI, STL, STR, STS, TEQ, TGE, TGT, TLE, TLT,
  110. TNE, TRP, XOR, ZEQ, ZER, ZGE, ZGT, ZLE, ZLT, ZNE,
  111. ZRE, ZRF, ZRL);
  112. dispatch = record
  113. iflag: ifset;
  114. instr: mnem;
  115. case instype of
  116. implic: (implicit:sword);
  117. explic: (ilength:byte);
  118. end;
  119. var
  120. code: packed array[0..maxcode] of byte; { code space }
  121. data: packed array[0..maxdata] of byte; { data space }
  122. retarea: array[1..maxret ] of word; { return area }
  123. pc,lb,sp,hp,pd: adr; { internal machine registers }
  124. i: integer; { integer scratch variable }
  125. s,t :word; { scratch variables }
  126. sz:size; { scratch variables }
  127. ss,st: sword; { scratch variables }
  128. k :double; { scratch variables }
  129. j:size; { scratch variable used as index }
  130. a,b:adr; { scratch variable used for addresses }
  131. dt,ds:double; { scratch variables for double precision }
  132. rt,rs,x,y:real; { scratch variables for real }
  133. found:boolean; { scratch }
  134. opcode: byte; { holds the opcode during execution }
  135. iclass: insclass; { true for escaped opcodes }
  136. dispat: array[insclass,byte] of dispatch;
  137. retsize:size; { holds size of last LFR }
  138. insr: mnem; { holds the instruction number }
  139. halted: boolean; { normally false }
  140. exitstatus:word; { parameter of MON 1 }
  141. ignmask:word; { ignore mask for traps }
  142. uerrorproc:adr; { number of user defined error procedure }
  143. intrap:boolean; { Set when executing trap(), to catch recursive calls}
  144. trapval:byte; { Set to number of last trap }
  145. header: array[1..8] of adr;
  146. tables: text; { description of EM instructions }
  147. prog: file of byte; { program and initialized data }
  148. .ne 20
  149. .sp 2
  150. {---------------------------------------------------------------------------}
  151. { Various check routines }
  152. {---------------------------------------------------------------------------}
  153. { Only the most basic checks are performed. These routines are inherently
  154. implementation dependent. }
  155. procedure trap(n:byte); forward;
  156. procedure memadr(a:adr);
  157. begin if (a>maxdata) or ((a<sp) and (a>=hp)) then trap(EMEMFLT) end;
  158. procedure wordadr(a:adr);
  159. begin memadr(a); if (a mod wsize<>0) then trap(EBADPTR) end;
  160. procedure chkadr(a:adr; s:size);
  161. begin memadr(a); memadr(a+s-1); { assumption: size is ok }
  162. if s<wsize
  163. then begin if a mod s<>0 then trap(EBADPTR) end
  164. else if a mod wsize<>0 then trap(EBADPTR)
  165. end;
  166. procedure newpc(a:double);
  167. begin if (a<0) or (a>maxcode) then trap(EBADPC); pc:=a end;
  168. procedure newsp(a:adr);
  169. begin if (a>lb) or (a<hp) or (a mod wsize<>0) then trap(ESTACK); sp:=a end;
  170. procedure newlb(a:adr);
  171. begin if (a<sp) or (a mod wsize<>0) then trap(ESTACK); lb:=a end;
  172. procedure newhp(a:adr);
  173. begin if (a>sp) or (a>maxdata+1) or (a mod wsize<>0)
  174. then trap(EHEAP)
  175. else hp:=a
  176. end;
  177. function argc(a:double):sword;
  178. begin if (a<-signbit) or (a>maxsint) then trap(EILLINS); argc:=a end;
  179. function argd(a:double):double;
  180. begin if (a<-maxdbl) or (a>maxdbl) then trap(EILLINS); argd:=a end;
  181. function argl(a:double):offs;
  182. begin if (a<-maxoffs) or (a>maxoffs) then trap(EILLINS); argl:=a end;
  183. function argg(k:double):adr;
  184. begin if (k<0) or (k>maxadr) then trap(EILLINS); argg:=k end;
  185. function argf(a:double):offs;
  186. begin if (a<-maxoffs) or (a>maxoffs) then trap(EILLINS); argf:=a end;
  187. function argn(a:double):word;
  188. begin if (a<0) or (a>maxuint) then trap(EILLINS); argn:=a end;
  189. function args(a:double):size;
  190. begin if (a<=0) or (a>maxoffs)
  191. then trap(EODDZ)
  192. else if (a mod wsize)<>0 then trap(EODDZ);
  193. args:=a ;
  194. end;
  195. function argz(a:double):size;
  196. begin if (a<0) or (a>maxoffs)
  197. then trap(EODDZ)
  198. else if (a mod wsize)<>0 then trap(EODDZ);
  199. argz:=a ;
  200. end;
  201. function argo(a:double):size;
  202. begin if (a<=0) or (a>maxoffs)
  203. then trap(EODDZ)
  204. else if (a mod wsize<>0) and (wsize mod a<>0) then trap(EODDZ);
  205. argo:=a ;
  206. end;
  207. function argw(a:double):size;
  208. begin if (a<=0) or (a>maxoffs) or (a>maxuint)
  209. then trap(EODDZ)
  210. else if (a mod wsize)<>0 then trap(EODDZ);
  211. argw:=a ;
  212. end;
  213. function argp(a:double):size;
  214. begin if (a<0) or (a>=header[NPROC]) then trap(EILLINS); argp:=a end;
  215. function argr(a:double):word;
  216. begin if (a<0) or (a>2) then trap(EILLINS); argr:=a end;
  217. procedure argwf(s:double);
  218. begin if argw(s)<>fsize then trap(EILLINS) end;
  219. function szindex(s:double):integer;
  220. begin s:=argw(s); if (s mod wsize <> 0) or (s>2*wsize) then trap(EILLINS);
  221. szindex:=s div wsize
  222. end;
  223. function locadr(l:double):adr;
  224. begin l:=argl(l); if l<0 then locadr:=lb+l else locadr:=lb+l+savsize end;
  225. function signwd(w:word):sword;
  226. begin if w = undef then trap(EIUND);
  227. if w >= signbit then signwd:=w-negoff else signwd:=w
  228. end;
  229. function dosign(w:word):sword;
  230. begin if w >= signbit then dosign:=w-negoff else dosign:=w end;
  231. function unsign(w:sword):word;
  232. begin if w<0 then unsign:=w+negoff else unsign:=w end;
  233. function chopw(dw:double):word;
  234. begin chopw:=dw mod negoff end;
  235. function fitsw(w:full;trapno:byte):word;
  236. { checks whether value fits in signed word, returns unsigned representation}
  237. begin
  238. if (w>maxsint) or (w<-signbit) then
  239. begin trap(trapno);
  240. if w<0 then fitsw:=negoff- (-w)mod negoff
  241. else fitsw:=w mod negoff;
  242. end
  243. else fitsw:=unsign(w)
  244. end;
  245. function fitd(w:full):double;
  246. begin
  247. if abs(w) > maxdbl then trap(ECONV);
  248. fitd:=w
  249. end;
  250. .ne 20
  251. .sp 2
  252. {---------------------------------------------------------------------------}
  253. { Memory access routines }
  254. {---------------------------------------------------------------------------}
  255. { memw returns a machine word as an unsigned integer
  256. memb returns a single byte as a positive integer: 0 <= memb <= 255
  257. mems(a,s) fetches an object smaller than a word and returns a word
  258. store(a,v) stores the word v at machine address a
  259. storea(a,v) stores the address v at machine address a
  260. storeb(a,b) stores the byte b at machine address a
  261. stores(a,s,v) stores the s least significant bytes of a word at address a
  262. memi returns an offset from the instruction space
  263. Note that the procedure descriptors are part of instruction space.
  264. nextpc returns the next byte addressed by pc, incrementing pc
  265. lino changes the line number word.
  266. filna changes the pointer to the file name.
  267. All routines check to make sure the address is within range and valid for
  268. the size of the object. If an addressing error is found, a trap occurs.
  269. }
  270. function memw(a:adr):word;
  271. var b:word; i:integer;
  272. begin wordadr(a); b:=0;
  273. for i:=wsize-1 downto 0 do b:=256*b + data[a+i] ;
  274. memw:=b
  275. end;
  276. function memd(a:adr):double; { Always signed }
  277. var b:double; i:integer;
  278. begin wordadr(a); b:=data[a+2*wsize-1];
  279. if b>=128 then b:=b-256;
  280. for i:=2*wsize-2 downto 0 do b:=256*b + data[a+i] ;
  281. memd:=b
  282. end;
  283. function mema(a:adr):adr;
  284. var b:adr; i:integer;
  285. begin wordadr(a); b:=0;
  286. for i:=asize-1 downto 0 do b:=256*b + data[a+i] ;
  287. mema:=b
  288. end;
  289. function mems(a:adr;s:size):word;
  290. var i:integer; b:word;
  291. begin chkadr(a,s); b:=0; for i:=1 to s do b:=b*256+data[a+s-i]; mems:=b end;
  292. function memb(a:adr):byte;
  293. begin memadr(a); memb:=data[a] end;
  294. procedure store(a:adr; x:word);
  295. var i:integer;
  296. begin wordadr(a);
  297. for i:=0 to wsize-1 do
  298. begin data[a+i]:=x mod 256; x:=x div 256 end
  299. end;
  300. procedure storea(a:adr; x:adr);
  301. var i:integer;
  302. begin wordadr(a);
  303. for i:=0 to asize-1 do
  304. begin data[a+i]:=x mod 256; x:=x div 256 end
  305. end;
  306. procedure stores(a:adr;s:size;v:word);
  307. var i:integer;
  308. begin chkadr(a,s);
  309. for i:=0 to s-1 do begin data[a+i]:=v mod 256; v:=v div 256 end;
  310. end;
  311. procedure storeb(a:adr; b:byte);
  312. begin memadr(a); data[a]:=b end;
  313. function memi(a:adr):adr;
  314. var b:adr; i:integer;
  315. begin if (a mod wsize<>0) or (a+asize-1>maxcode) then trap(EBADPTR); b:=0;
  316. for i:=asize-1 downto 0 do b:=256*b + code[a+i] ;
  317. memi:=b
  318. end;
  319. function nextpc:byte;
  320. begin if pc>=pd then trap(EBADPC); nextpc:=code[pc]; newpc(pc+1) end;
  321. procedure lino(w:word);
  322. begin store(lineadr,w) end;
  323. procedure filna(a:adr);
  324. begin storea(fileadr,a) end;
  325. .ne 20
  326. .sp 2
  327. {---------------------------------------------------------------------------}
  328. { Stack Manipulation Routines }
  329. {---------------------------------------------------------------------------}
  330. { push puts a word on the stack
  331. pushsw takes a signed one word integer and pushes it on the stack
  332. pop removes a machine word from the stack and delivers it as a word
  333. popsw removes a machine word from the stack and delivers a signed integer
  334. pusha pushes an address on the stack
  335. popa removes a machine word from the stack and delivers it as an address
  336. pushd pushes a double precision number on the stack
  337. popd removes two machine words and returns a double precision integer
  338. pushr pushes a float (floating point) number on the stack
  339. popr removes several machine words and returns a float number
  340. pushx puts an object of arbitrary size on the stack
  341. popx removes an object of arbitrary size
  342. }
  343. procedure push(x:word);
  344. begin newsp(sp-wsize); store(sp,x) end;
  345. procedure pushsw(x:sword);
  346. begin newsp(sp-wsize); store(sp,unsign(x)) end;
  347. function pop:word;
  348. begin pop:=memw(sp); newsp(sp+wsize) end;
  349. function popsw:sword;
  350. begin popsw:=signwd(pop) end;
  351. procedure pusha(x:adr);
  352. begin newsp(sp-asize); storea(sp,x) end;
  353. function popa:adr;
  354. begin popa:=mema(sp); newsp(sp+asize) end;
  355. procedure pushd(y:double);
  356. begin { push double integer onto the stack } newsp(sp-2*wsize) end;
  357. function popd:double;
  358. begin { pop double integer from the stack } newsp(sp+2*wsize); popd:=0 end;
  359. procedure pushr(z:real);
  360. begin { Push a float onto the stack } newsp(sp-fsize) end;
  361. function popr:real;
  362. begin { pop float from the stack } newsp(sp+fsize); popr:=0.0 end;
  363. procedure pushx(objsize:size; a:adr);
  364. var i:integer;
  365. begin
  366. if objsize<wsize
  367. then push(mems(a,objsize))
  368. else for i:=1 to objsize div wsize do push(memw(a+objsize-wsize*i))
  369. end;
  370. procedure popx(objsize:size; a:adr);
  371. var i:integer;
  372. begin
  373. if objsize<wsize
  374. then stores(a,objsize,pop)
  375. else for i:=1 to objsize div wsize do store(a-wsize+wsize*i,pop)
  376. end;
  377. .ne 20
  378. .sp 2
  379. {---------------------------------------------------------------------------}
  380. { Bit manipulation routines (extract, shift, rotate) }
  381. {---------------------------------------------------------------------------}
  382. procedure sleft(var w:sword); { 1 bit left shift }
  383. begin w:= dosign(fitsw(2*w,EIOVFL)) end;
  384. procedure suleft(var w:word); { 1 bit left shift }
  385. begin w := chopw(2*w) end;
  386. procedure sdleft(var d:double); { 1 bit left shift }
  387. begin { shift two word signed integer } end;
  388. procedure sright(var w:sword); { 1 bit right shift with sign extension }
  389. begin if w >= 0 then w := w div 2 else w := (w-1) div 2 end;
  390. procedure suright(var w:word); { 1 bit right shift without sign extension }
  391. begin w := w div 2 end;
  392. procedure sdright(var d:double); { 1 bit right shift }
  393. begin { shift two word signed integer } end;
  394. procedure rleft(var w:word); { 1 bit left rotate }
  395. begin if w >= t15
  396. then w:=(w-t15)*2 + 1
  397. else w:=w*2
  398. end;
  399. procedure rright(var w:word); { 1 bit right rotate }
  400. begin if w mod 2 = 1
  401. then w:=w div 2 + t15
  402. else w:=w div 2
  403. end;
  404. function sextend(w:word;s:size):word;
  405. var i:size;
  406. begin
  407. for i:=1 to (wsize-s)*8 do rleft(w);
  408. for i:=1 to (wsize-s)*8 do sright(w);
  409. sextend:=w;
  410. end;
  411. function bit(b:bitnr; w:word):bitval; { return bit b of the word w }
  412. var i:bitnr;
  413. begin for i:= 1 to b do rright(w); bit:= w mod 2 end;
  414. function bf(ty:bftype; w1,w2:word):word; { return boolean fcn of 2 words }
  415. var i:bitnr; j:word;
  416. begin j:=0;
  417. for i:= maxbitnr downto 0 do
  418. begin j := 2*j;
  419. case ty of
  420. andf: if bit(i,w1)+bit(i,w2) = 2 then j:=j+1;
  421. iorf: if bit(i,w1)+bit(i,w2) > 0 then j:=j+1;
  422. xorf: if bit(i,w1)+bit(i,w2) = 1 then j:=j+1
  423. end
  424. end;
  425. bf:=j
  426. end;
  427. {---------------------------------------------------------------------------}
  428. { Array indexing }
  429. {---------------------------------------------------------------------------}
  430. function arraycalc(c:adr):adr; { subscript calculation }
  431. var j:full; objsize:size; a:adr;
  432. begin j:= popsw - signwd(memw(c));
  433. if (j<0) or (j>memw(c+wsize)) then trap(EARRAY);
  434. objsize := argo(memw(c+wsize+wsize));
  435. a := j*objsize+popa; chkadr(a,objsize);
  436. arraycalc:=a
  437. end;
  438. .ne 20
  439. .sp 2
  440. {---------------------------------------------------------------------------}
  441. { Double and Real Arithmetic }
  442. {---------------------------------------------------------------------------}
  443. { All routines for doubles and floats are dummy routines, since the format of
  444. doubles and floats is not defined in EM.
  445. }
  446. function doadi(ds,dt:double):double;
  447. begin { add two doubles } doadi:=0 end;
  448. function dosbi(ds,dt:double):double;
  449. begin { subtract two doubles } dosbi:=0 end;
  450. function domli(ds,dt:double):double;
  451. begin { multiply two doubles } domli:=0 end;
  452. function dodvi(ds,dt:double):double;
  453. begin { divide two doubles } dodvi:=0 end;
  454. function dormi(ds,dt:double):double;
  455. begin { modulo of two doubles } dormi:=0 end;
  456. function dongi(ds:double):double;
  457. begin { negative of a double } dongi:=0 end;
  458. function doadf(x,y:real):real;
  459. begin { add two floats } doadf:=0.0 end;
  460. function dosbf(x,y:real):real;
  461. begin { subtract two floats } dosbf:=0.0 end;
  462. function domlf(x,y:real):real;
  463. begin { multiply two floats } domlf:=0.0 end;
  464. function dodvf(x,y:real):real;
  465. begin { divide two floats } dodvf:=0.0 end;
  466. function dongf(x:real):real;
  467. begin { negate a float } dongf:=0.0 end;
  468. procedure dofif(x,y:real;var intpart,fraction:real);
  469. begin { dismember x*y into integer and fractional parts }
  470. intpart:=0.0; { integer part of x*y, same sign as x*y }
  471. fraction:=0.0;
  472. { fractional part of x*y, 0<=abs(fraction)<1 and same sign as x*y }
  473. end;
  474. procedure dofef(x:real;var mantissa:real;var exponent:sword);
  475. begin { dismember x into mantissa and exponent parts }
  476. mantissa:=0.0; { mantissa of x , >= 1/2 and <1 }
  477. exponent:=0; { base 2 exponent of x }
  478. end;
  479. .bp
  480. {---------------------------------------------------------------------------}
  481. { Trap and Call }
  482. {---------------------------------------------------------------------------}
  483. procedure call(p:adr); { Perform the call }
  484. begin
  485. pusha(lb);pusha(pc);
  486. newlb(sp);newsp(sp - memi(pd + pdsize*p + pdlocs));
  487. newpc(memi(pd + pdsize*p+ pdbase))
  488. end;
  489. procedure dotrap(n:byte);
  490. var i:size;
  491. begin
  492. if (uerrorproc=0) or intrap then
  493. begin
  494. if intrap then
  495. writeln('Recursive trap, first trap number was ', trapval:1);
  496. writeln('Error ', n:1);
  497. writeln('With',ord(insr):4,' arg ',k:1);
  498. goto 9999
  499. end;
  500. { Deposit all interpreter variables that need to be saved on
  501. the stack. This includes all scratch variables that can
  502. be in use at the moment and ( not possible in this interpreter )
  503. the internal address of the interpreter where the error occurred.
  504. This would make it possible to execute an RTT instruction totally
  505. transparent to the user program.
  506. It can, for example, occur within an ADD instruction that both
  507. operands are undefined and that the result overflows.
  508. Although this will generate 3 error traps it must be possible
  509. to ignore them all.
  510. }
  511. intrap:=true; trapval:=n;
  512. for i:=retsize div wsize downto 1 do push(retarea[i]);
  513. push(retsize); { saved return area }
  514. pusha(mema(fileadr)); { saved current file name pointer }
  515. push(memw(lineadr)); { saved line number }
  516. push(n); { push error number }
  517. a:=argp(uerrorproc);
  518. uerrorproc:=0; { reset signal }
  519. call(a); { call the routine }
  520. intrap:=false; { Don't catch recursive traps anymore }
  521. goto 8888; { reenter main loop }
  522. end;
  523. procedure trap;
  524. { This routine is invoked for overflow, and other run time errors.
  525. For non-fatal errors, trap returns to the calling routine
  526. }
  527. begin
  528. if n>=16 then dotrap(n) else if bit(n,ignmask)=0 then dotrap(n);
  529. end;
  530. procedure dortt;
  531. { The restoration of file address and line number is not essential.
  532. The restoration of the return save area is.
  533. }
  534. var i:size;
  535. n:word;
  536. begin
  537. newsp(lb); lb:=maxdata+1 ; { to circumvent ESTACK for the popa + pop }
  538. newpc(popa); newlb(popa); { So far a plain RET 0 }
  539. n:=pop; if (n>=16) and (n<64) then goto 9999 ;
  540. lino(pop); filna(popa); retsize:=pop;
  541. for i:=1 to retsize div wsize do retarea[i]:=pop ;
  542. end;
  543. .sp 2
  544. {---------------------------------------------------------------------------}
  545. { monitor calls }
  546. {---------------------------------------------------------------------------}
  547. procedure domon(entry:word);
  548. var index: 1..63;
  549. dummy: double;
  550. count,rwptr: adr;
  551. token: byte;
  552. i: integer;
  553. begin
  554. if (entry<=0) or (entry>63) then entry:=63 ;
  555. index:=entry;
  556. case index of
  557. 1: begin { exit } exitstatus:=pop; halted:=true end;
  558. 3: begin { read } dummy:=pop; { All input is from stdin }
  559. rwptr:=popa; count:=popa;
  560. i:=0 ;
  561. while (not eof(input)) and (i<count) do
  562. begin
  563. if eoln(input) then begin storeb(rwptr,10) ; count:=i end
  564. else storeb(rwptr,ord(input^)) ;
  565. get(input); rwptr:=rwptr+1 ; i:=i+1 ;
  566. end;
  567. pusha(i); push(0)
  568. end;
  569. 4: begin { write } dummy:=pop; { All output is to stdout }
  570. rwptr:=popa; count:=popa;
  571. for i:=1 to count do
  572. begin token:=memb(rwptr); rwptr:=rwptr+1 ;
  573. if token=10 then writeln else write(chr(token))
  574. end ;
  575. pusha(count);
  576. push(0)
  577. end;
  578. 54: begin { ioctl, faked } dummy:=popa;dummy:=popa;dummy:=pop;push(0) end ;
  579. 2, 5, 6, 7, 8, 9, 10,
  580. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  581. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  582. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  583. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  584. 51, 52, 53, 55, 56, 57, 58, 59, 60,
  585. 61, 62:
  586. begin push(22); push(22) end;
  587. 63: { exists only for the trap }
  588. trap(EBADMON)
  589. end
  590. end;
  591. .bp
  592. {---------------------------------------------------------------------------}
  593. { Initialization and debugging }
  594. {---------------------------------------------------------------------------}
  595. procedure doident; { print line number and file name }
  596. var a:adr; i,c:integer; found:boolean;
  597. begin
  598. write('at line ',memw(lineadr):1,' ');
  599. a:=mema(fileadr); if a<>0 then
  600. begin i:=20; found:=false;
  601. while (i<>0) and not found do
  602. begin c:=memb(a); a:=a+1; found:=true; i:=i-1;
  603. if (c>=48) and (c<=57) then
  604. begin found:=false; write(chr(ord('0')+c-48)) end;
  605. if (c>=65) and (c<=90) then
  606. begin found:=false; write(chr(ord('A')+c-65)) end;
  607. if (c>=97) and (c<=122) then
  608. begin found:=false; write(chr(ord('a')+c-97)) end;
  609. end;
  610. end;
  611. writeln;
  612. end;
  613. procedure initialize; { start the ball rolling }
  614. { This is not part of the machine definition }
  615. var cset:set of char;
  616. f:ifset;
  617. iclass:insclass;
  618. insno:byte;
  619. nops:integer;
  620. opcode:byte;
  621. i,j,n:integer;
  622. wtemp:sword;
  623. count:integer;
  624. repc:adr;
  625. nexta,firsta:adr;
  626. elem:byte;
  627. amount,ofst:size;
  628. c:char;
  629. function readb(n:integer):double;
  630. var b:byte;
  631. begin read(prog,b); if n>1 then readb:=readb(n-1)*256+b else readb:=b end;
  632. function readbyte:byte;
  633. begin readbyte:=readb(1) end;
  634. function readword:word;
  635. begin readword:=readb(wsize) end;
  636. function readadr:adr;
  637. begin readadr:=readb(asize) end;
  638. function ifind(ordinal:byte):mnem;
  639. var loopvar:mnem;
  640. found:boolean;
  641. begin ifind:=NON;
  642. loopvar:=insr; found:=false;
  643. repeat
  644. if ordinal=ord(loopvar) then
  645. begin found:=true; ifind:=loopvar end;
  646. if loopvar<>ZRL then loopvar:=succ(loopvar) else loopvar:=NON;
  647. until found or (loopvar=insr) ;
  648. end;
  649. procedure readhdr;
  650. type hdrw=0..32767 ; { 16 bit header words }
  651. var hdr: hdrw;
  652. i: integer;
  653. begin
  654. for i:=0 to 7 do
  655. begin hdr:=readb(2);
  656. case i of
  657. 0: if hdr<>3757 then { 07255 }
  658. begin writeln('Not an em load file'); halt end;
  659. 2: if hdr<>0 then
  660. begin writeln('Unsolved references'); halt end;
  661. 3: if hdr<>3 then
  662. begin writeln('Incorrect load file version'); halt end;
  663. 4: if hdr<>wsize then
  664. begin writeln('Incorrect word size'); halt end;
  665. 5: if hdr<>asize then
  666. begin writeln('Incorrect pointer size'); halt end;
  667. 1,6,7:;
  668. end
  669. end
  670. end;
  671. procedure noinit;
  672. begin writeln('Illegal initialization'); halt end;
  673. procedure readint(a:adr;s:size);
  674. var i:size;
  675. begin { construct integer out of byte sequence }
  676. for i:=1 to s do { construct the value and initialize at a }
  677. begin storeb(a,readbyte); a:=a+1 end
  678. end;
  679. procedure readuns(a:adr;s:size);
  680. begin { construct unsigned out of byte sequence }
  681. readint(a,s) { identical to readint }
  682. end;
  683. procedure readfloat(a:adr;s:size);
  684. var i:size; b:byte;
  685. begin { construct float out of string}
  686. if (s<>4) and (s<>8) then noinit; i:=0;
  687. repeat { eat the bytes, construct the value and intialize at a }
  688. b:=readbyte; i:=i+1;
  689. until b=0 ;
  690. end;
  691. begin
  692. halted:=false;
  693. exitstatus:=undef;
  694. uerrorproc:=0; intrap:=false;
  695. { initialize tables }
  696. for i:=0 to maxcode do code[i]:=0;
  697. for i:=0 to maxdata do data[i]:=0;
  698. for iclass:=prim to tert do
  699. for i:=0 to 255 do
  700. with dispat[iclass][i] do
  701. begin instr:=NON; iflag:=[zbit] end;
  702. { read instruction table file. see appendix B }
  703. { The table read here is a simple transformation of the table on page xx }
  704. { - instruction names were transformed to numbers }
  705. { - the '-' flag was transformed to an 'i' flag for 'w' type instructions }
  706. { - the 'S' flag was added for instructions having signed operands }
  707. reset(tables);
  708. insr:=NON;
  709. repeat
  710. read(tables,insno) ; cset:=[]; f:=[];
  711. insr:=ifind(insno);
  712. if insr=NON then begin writeln('Incorrect table'); halt end;
  713. repeat read(tables,c) until c<>' ' ;
  714. repeat
  715. cset:=cset+[c];
  716. read(tables,c)
  717. until c=' ' ;
  718. if 'm' in cset then f:=f+[mini];
  719. if 's' in cset then f:=f+[short];
  720. if '-' in cset then f:=f+[zbit];
  721. if 'i' in cset then f:=f+[ibit];
  722. if 'S' in cset then f:=f+[sbit];
  723. if 'w' in cset then f:=f+[wbit];
  724. if (mini in f) or (short in f) then read(tables,nops) else nops:=1 ;
  725. readln(tables,opcode);
  726. if ('4' in cset) or ('8' in cset) then
  727. begin iclass:=tert end
  728. else if 'e' in cset then
  729. begin iclass:=second end
  730. else iclass:=prim;
  731. for i:=0 to nops-1 do
  732. begin
  733. with dispat[iclass,opcode+i] do
  734. begin
  735. iflag:=f; instr:=insr;
  736. if '2' in cset then ilength:=2
  737. else if 'u' in cset then ilength:=2
  738. else if '4' in cset then ilength:=4
  739. else if '8' in cset then ilength:=8
  740. else if (mini in f) or (short in f) then
  741. begin
  742. if 'N' in cset then wtemp:=-1-i else wtemp:=i ;
  743. if 'o' in cset then wtemp:=wtemp+1 ;
  744. if short in f then wtemp:=wtemp*256 ;
  745. implicit:=wtemp
  746. end
  747. end
  748. end
  749. until eof(tables);
  750. { read in program text, data and procedure descriptors }
  751. reset(prog);
  752. readhdr; { verify first header }
  753. for i:=1 to 8 do header[i]:=readadr; { read second header }
  754. hp:=maxdata+1; sp:=maxdata+1; lino(0);
  755. { read program text }
  756. if header[NTEXT]+header[NPROC]*pdsize>maxcode then
  757. begin writeln('Text size too large'); halt end;
  758. if header[SZDATA]>maxdata then
  759. begin writeln('Data size too large'); halt end;
  760. for i:=0 to header[NTEXT]-1 do code[i]:=readbyte;
  761. { read data blocks }
  762. nexta:=0;
  763. for i:=1 to header[NDATA] do
  764. begin
  765. n:=readbyte;
  766. if n<>0 then
  767. begin
  768. elem:=readbyte; firsta:=nexta;
  769. case n of
  770. 1: { uninitialized words }
  771. for j:=1 to elem do
  772. begin store(nexta,undef); nexta:=nexta+wsize end;
  773. 2: { initialized bytes }
  774. for j:=1 to elem do
  775. begin storeb(nexta,readbyte); nexta:=nexta+1 end;
  776. 3: { initialized words }
  777. for j:=1 to elem do
  778. begin store(nexta,readword); nexta:=nexta+wsize end;
  779. 4,5: { instruction and data pointers }
  780. for j:=1 to elem do
  781. begin storea(nexta,readadr); nexta:=nexta+asize end;
  782. 6: { signed integers }
  783. begin readint(nexta,elem); nexta:=nexta+elem end;
  784. 7: { unsigned integers }
  785. begin readuns(nexta,elem); nexta:=nexta+elem end;
  786. 8: { floating point numbers }
  787. begin readfloat(nexta,elem); nexta:=nexta+elem end;
  788. end
  789. end
  790. else
  791. begin
  792. repc:=readadr; amount:=nexta-firsta;
  793. for count:=1 to repc do
  794. begin
  795. for ofst:=0 to amount-1 do data[nexta+ofst]:=data[firsta+ofst];
  796. nexta:=nexta+amount;
  797. end
  798. end
  799. end;
  800. if header[SZDATA]<>nexta then writeln('Data initialization error');
  801. hp:=nexta;
  802. { read descriptor table }
  803. pd:=header[NTEXT];
  804. for i:=1 to header[NPROC]*pdsize do code[pd+i-1]:=readbyte;
  805. { call the entry point routine }
  806. ignmask:=0; { catch all traps, higher numbered traps cannot be ignored}
  807. retsize:=0;
  808. lb:=maxdata; { illegal dynamic link }
  809. pc:=maxcode; { illegal return address }
  810. push(0); a:=sp; { No environment }
  811. push(0); b:=sp; { No args }
  812. pusha(a); { envp }
  813. pusha(b); { argv }
  814. push(0); { argc }
  815. call(argp(header[ENTRY]));
  816. end;
  817. .bp
  818. {---------------------------------------------------------------------------}
  819. { MAIN LOOP OF THE INTERPRETER }
  820. {---------------------------------------------------------------------------}
  821. { It should be noted that the interpreter (microprogram) for an EM
  822. machine can be written in two fundamentally different ways: (1) the
  823. instruction operands are fetched in the main loop, or (2) the in-
  824. struction operands are fetched after the 256 way branch, by the exe-
  825. cution routines themselves. In this interpreter, method (1) is used
  826. to simplify the description of execution routines. The dispatch
  827. table dispat is used to determine how the operand is encoded. There
  828. are 4 possibilities:
  829. 0. There is no operand
  830. 1. The operand and instruction are together in 1 byte (mini)
  831. 2. The operand is one byte long and follows the opcode byte(s)
  832. 3. The operand is two bytes long and follows the opcode byte(s)
  833. 4. The operand is four bytes long and follows the opcode byte(s)
  834. In this interpreter, the main loop determines the operand type,
  835. fetches it, and leaves it in the global variable k for the execution
  836. routines to use. Consequently, instructions such as LOL, which use
  837. three different formats, need only be described once in the body of
  838. the interpreter.
  839. However, for a production interpreter, or a hardware EM
  840. machine, it is probably better to use method (2), i.e. to let the
  841. execution routines themselves fetch their own operands. The reason
  842. for this is that each opcode uniquely determines the operand format,
  843. so no table lookup in the dispatch table is needed. The whole table
  844. is not needed. Method (2) therefore executes much faster.
  845. However, separate execution routines will be needed for LOL with
  846. a one byte offset, and LOL with a two byte offset. It is to avoid
  847. this additional clutter that method (1) is used here. In a produc-
  848. tion interpreter, it is envisioned that the main loop will fetch the
  849. next instruction byte, and use it as an index into a 256 word table
  850. to find the address of the interpreter routine to jump to. The
  851. routine jumped to will begin by fetching its operand, if any,
  852. without any table lookup, since it knows which format to expect.
  853. After doing the work, it returns to the main loop by jumping in-
  854. directly to a register that contains the address of the main loop.
  855. A slight variation on this idea is to have the register contain
  856. the address of the branch table, rather than the address of the main
  857. loop.
  858. Another issue is whether the execution routines for LOL 0, LOL
  859. 2, LOL 4, etc. should all be have distinct execution routines. Doing
  860. so provides for the maximum speed, since the operand is implicit in
  861. the routine itself. The disadvantage is that many nearly identical
  862. execution routines will then be needed. Another way of doing it is
  863. to keep the instruction byte fetched from memory (LOL 0, LOL 2, LOL
  864. 4, etc.) in some register, and have all the LOL mini format instruc-
  865. tions branch to a common routine. This routine can then determine
  866. the operand by subtracting the code for LOL 0 from the register,
  867. leaving the true operand in the register (as a word quantity of
  868. course). This method makes the interpreter smaller, but is a bit
  869. slower.
  870. .bp
  871. To make this important point a little clearer, consider how a
  872. production interpreter for the PDP-11 might appear. Let us assume the
  873. following opcodes have been assigned:
  874. 31: LOL -2 (2 bytes, i.e. next word)
  875. 32: LOL -4
  876. 33: LOL -6
  877. 34: LOL b (format with a one byte offset)
  878. 35: LOL w (format with a one word, i.e. two byte offset)
  879. Further assume that each of the 5 opcodes will have its own execution
  880. routine, i.e. we are making a tradeoff in favor of fast execution and
  881. a slightly larger interpreter.
  882. Register r5 is the em program counter.
  883. Register r4 is the em LB register
  884. Register r3 is the em SP register (the stack grows toward low core)
  885. Register r2 contains the interpreter address of the main loop
  886. The main loop looks like this:
  887. movb (r5)+,r0 /fetch the opcode into r0 and increment r5
  888. asl r0 /shift r0 left 1 bit. Now: -256<=r0<=+254
  889. jmp *table(r0) /jump to execution routine
  890. Notice that no operand fetching has been done. The execution routines for
  891. the 5 sample instructions given above might be as follows:
  892. lol2: mov -2(r4),-(sp) /push local -2 onto stack
  893. jmp (r2) /go back to main loop
  894. lol4: mov -4(r4),-(sp) /push local -4 onto stack
  895. jmp (r2) /go back to main loop
  896. lol6: mov -6(r4),-(sp) /push local -6 onto stack
  897. jmp (r2) /go back to main loop
  898. lolb: mov $177400,r0 /prepare to fetch the 1 byte operand
  899. bisb (r5)+,r0 /operand is now in r0
  900. asl r0 /r0 is now offset from LB in bytes, not words
  901. add r4,r0 /r0 is now address of the needed local
  902. mov (r0),-(sp) /push the local onto the stack
  903. jmp (r2)
  904. lolw: clr r0 /prepare to fetch the 2 byte operand
  905. bisb (r5)+,r0 /fetch high order byte first !!!
  906. swab r0 /insert high order byte in place
  907. bisb (r5)+,r0 /insert low order byte in place
  908. asl r0 /convert offset to bytes, from words
  909. add r4,r0 /r0 is now address of needed local
  910. mov (r0),-(sp) /stack the local
  911. jmp (r2) /done
  912. The important thing to notice is where and how the operand fetch occurred:
  913. lol2, lol4, and lol6, (the mini's) have implicit operands
  914. lolb knew it had to fetch one byte, and did so without any table lookup
  915. lolw knew it had to fetch a word, and did so, high order byte first }
  916. .bp
  917. .sp 4
  918. {---------------------------------------------------------------------------}
  919. { Routines for the individual instructions }
  920. {---------------------------------------------------------------------------}
  921. procedure loadops;
  922. var j:integer;
  923. begin
  924. case insr of
  925. { LOAD GROUP }
  926. LDC: pushd(argd(k));
  927. LOC: pushsw(argc(k));
  928. LOL: push(memw(locadr(k)));
  929. LOE: push(memw(argg(k)));
  930. LIL: push(memw(mema(locadr(k))));
  931. LOF: push(memw(popa+argf(k)));
  932. LAL: pusha(locadr(k));
  933. LAE: pusha(argg(k));
  934. LXL: begin a:=lb; for j:=1 to argn(k) do a:=mema(a+savsize); pusha(a) end;
  935. LXA: begin a:=lb;
  936. for j:=1 to argn(k) do a:= mema(a+savsize);
  937. pusha(a+savsize)
  938. end;
  939. LOI: pushx(argo(k),popa);
  940. LOS: begin k:=argw(k); if k<>wsize then trap(EILLINS);
  941. k:=pop; pushx(argo(k),popa)
  942. end;
  943. LDL: begin a:=locadr(k); push(memw(a+wsize)); push(memw(a)) end;
  944. LDE: begin k:=argg(k); push(memw(k+wsize)); push(memw(k)) end;
  945. LDF: begin k:=argf(k);
  946. a:=popa; push(memw(a+k+wsize)); push(memw(a+k))
  947. end;
  948. LPI: push(argp(k))
  949. end
  950. end;
  951. procedure storeops;
  952. begin
  953. case insr of
  954. { STORE GROUP }
  955. STL: store(locadr(k),pop);
  956. STE: store(argg(k),pop);
  957. SIL: store(mema(locadr(k)),pop);
  958. STF: begin a:=popa; store(a+argf(k),pop) end;
  959. STI: popx(argo(k),popa);
  960. STS: begin k:=argw(k); if k<>wsize then trap(EILLINS);
  961. k:=popa; popx(argo(k),popa)
  962. end;
  963. SDL: begin a:=locadr(k); store(a,pop); store(a+wsize,pop) end;
  964. SDE: begin k:=argg(k); store(k,pop); store(k+wsize,pop) end;
  965. SDF: begin k:=argf(k); a:=popa; store(a+k,pop); store(a+k+wsize,pop) end
  966. end
  967. end;
  968. procedure intarith;
  969. var i:integer;
  970. begin
  971. case insr of
  972. { SIGNED INTEGER ARITHMETIC }
  973. ADI: case szindex(argw(k)) of
  974. 1: begin st:=popsw; ss:=popsw; push(fitsw(ss+st,EIOVFL)) end;
  975. 2: begin dt:=popd; ds:=popd; pushd(doadi(ds,dt)) end;
  976. end ;
  977. SBI: case szindex(argw(k)) of
  978. 1: begin st:=popsw; ss:= popsw; push(fitsw(ss-st,EIOVFL)) end;
  979. 2: begin dt:=popd; ds:=popd; pushd(dosbi(ds,dt)) end;
  980. end ;
  981. MLI: case szindex(argw(k)) of
  982. 1: begin st:=popsw; ss:= popsw; push(fitsw(ss*st,EIOVFL)) end;
  983. 2: begin dt:=popd; ds:=popd; pushd(domli(ds,dt)) end;
  984. end ;
  985. DVI: case szindex(argw(k)) of
  986. 1: begin st:= popsw; ss:= popsw;
  987. if st=0 then trap(EIDIVZ) else pushsw(ss div st)
  988. end;
  989. 2: begin dt:=popd; ds:=popd; pushd(dodvi(ds,dt)) end;
  990. end;
  991. RMI: case szindex(argw(k)) of
  992. 1: begin st:= popsw; ss:=popsw;
  993. if st=0 then trap(EIDIVZ) else pushsw(ss - (ss div st)*st)
  994. end;
  995. 2: begin dt:=popd; ds:=popd; pushd(dormi(ds,dt)) end
  996. end;
  997. NGI: case szindex(argw(k)) of
  998. 1: begin st:=popsw; pushsw(-st) end;
  999. 2: begin ds:=popd; pushd(dongi(ds)) end
  1000. end;
  1001. SLI: begin t:=pop;
  1002. case szindex(argw(k)) of
  1003. 1: begin ss:=popsw;
  1004. for i:= 1 to t do sleft(ss); pushsw(ss)
  1005. end
  1006. end
  1007. end;
  1008. SRI: begin t:=pop;
  1009. case szindex(argw(k)) of
  1010. 1: begin ss:=popsw;
  1011. for i:= 1 to t do sright(ss); pushsw(ss)
  1012. end;
  1013. 2: begin ds:=popd;
  1014. for i:= 1 to t do sdright(ss); pushd(ss)
  1015. end
  1016. end
  1017. end
  1018. end
  1019. end;
  1020. procedure unsarith;
  1021. var i:integer;
  1022. begin
  1023. case insr of
  1024. { UNSIGNED INTEGER ARITHMETIC }
  1025. ADU: case szindex(argw(k)) of
  1026. 1: begin t:=pop; s:= pop; push(chopw(s+t)) end;
  1027. 2: trap(EILLINS);
  1028. end ;
  1029. SBU: case szindex(argw(k)) of
  1030. 1: begin t:=pop; s:= pop; push(chopw(s-t)) end;
  1031. 2: trap(EILLINS);
  1032. end ;
  1033. MLU: case szindex(argw(k)) of
  1034. 1: begin t:=pop; s:= pop; push(chopw(s*t)) end;
  1035. 2: trap(EILLINS);
  1036. end ;
  1037. DVU: case szindex(argw(k)) of
  1038. 1: begin t:= pop; s:= pop;
  1039. if t=0 then trap(EIDIVZ) else push(s div t)
  1040. end;
  1041. 2: trap(EILLINS);
  1042. end;
  1043. RMU: case szindex(argw(k)) of
  1044. 1: begin t:= pop; s:=pop;
  1045. if t=0 then trap(EIDIVZ) else push(s - (s div t)*t)
  1046. end;
  1047. 2: trap(EILLINS);
  1048. end;
  1049. SLU: case szindex(argw(k)) of
  1050. 1: begin t:=pop; s:=pop;
  1051. for i:= 1 to t do suleft(s); push(s)
  1052. end;
  1053. 2: trap(EILLINS);
  1054. end;
  1055. SRU: case szindex(argw(k)) of
  1056. 1: begin t:=pop; s:=pop;
  1057. for i:= 1 to t do suright(s); push(s)
  1058. end;
  1059. 2: trap(EILLINS);
  1060. end
  1061. end
  1062. end;
  1063. procedure fltarith;
  1064. begin
  1065. case insr of
  1066. { FLOATING POINT ARITHMETIC }
  1067. ADF: begin argwf(k); rt:=popr; rs:=popr; pushr(doadf(rs,rt)) end;
  1068. SBF: begin argwf(k); rt:=popr; rs:=popr; pushr(dosbf(rs,rt)) end;
  1069. MLF: begin argwf(k); rt:=popr; rs:=popr; pushr(domlf(rs,rt)) end;
  1070. DVF: begin argwf(k); rt:=popr; rs:=popr; pushr(dodvf(rs,rt)) end;
  1071. NGF: begin argwf(k); rt:=popr; pushr(dongf(rt)) end;
  1072. FIF: begin argwf(k); rt:=popr; rs:=popr;
  1073. dofif(rt,rs,x,y); pushr(y); pushr(x)
  1074. end;
  1075. FEF: begin argwf(k); rt:=popr; dofef(rt,x,ss); pushr(x); pushsw(ss) end
  1076. end
  1077. end;
  1078. procedure ptrarith;
  1079. begin
  1080. case insr of
  1081. { POINTER ARITHMETIC }
  1082. ADP: pusha(popa+argf(k));
  1083. ADS: case szindex(argw(k)) of
  1084. 1: begin st:=popsw; pusha(popa+st) end;
  1085. 2: begin dt:=popd; pusha(popa+dt) end;
  1086. end;
  1087. SBS: begin
  1088. a:=popa; b:=popa;
  1089. case szindex(argw(k)) of
  1090. 1: push(fitsw(b-a,EIOVFL));
  1091. 2: pushd(b-a)
  1092. end
  1093. end
  1094. end
  1095. end;
  1096. procedure incops;
  1097. var j:integer;
  1098. begin
  1099. case insr of
  1100. { INCREMENT/DECREMENT/ZERO }
  1101. INC: push(fitsw(popsw+1,EIOVFL));
  1102. INL: begin a:=locadr(k); store(a,fitsw(signwd(memw(a))+1,EIOVFL)) end;
  1103. INE: begin a:=argg(k); store(a,fitsw(signwd(memw(a))+1,EIOVFL)) end;
  1104. DEC: push(fitsw(popsw-1,EIOVFL));
  1105. DEL: begin a:=locadr(k); store(a,fitsw(signwd(memw(a))-1,EIOVFL)) end;
  1106. DEE: begin a:=argg(k); store(a,fitsw(signwd(memw(a))-1,EIOVFL)) end;
  1107. ZRL: store(locadr(k),0);
  1108. ZRE: store(argg(k),0);
  1109. ZER: for j:=1 to argw(k) div wsize do push(0);
  1110. ZRF: pushr(0);
  1111. end
  1112. end;
  1113. procedure convops;
  1114. begin
  1115. case insr of
  1116. { CONVERT GROUP }
  1117. CII: begin s:=pop; t:=pop;
  1118. if t<wsize then begin push(sextend(pop,t)); t:=wsize end;
  1119. case szindex(argw(t)) of
  1120. 1: if szindex(argw(s))=2 then pushd(popsw);
  1121. 2: if szindex(argw(s))=1 then push(fitsw(popd,ECONV))
  1122. end
  1123. end;
  1124. CIU: case szindex(argw(pop)) of
  1125. 1: if szindex(argw(pop))=2 then push(unsign(popd mod negoff));
  1126. 2: trap(EILLINS);
  1127. end;
  1128. CIF: begin argwf(pop);
  1129. case szindex(argw(pop)) of 1:pushr(popsw); 2:pushr(popd) end
  1130. end;
  1131. CUI: case szindex(argw(pop)) of
  1132. 1: case szindex(argw(pop)) of
  1133. 1: begin s:=pop; if s>maxsint then trap(ECONV); push(s) end;
  1134. 2: trap(EILLINS);
  1135. end;
  1136. 2: case szindex(argw(pop)) of
  1137. 1: pushd(pop);
  1138. 2: trap(EILLINS);
  1139. end;
  1140. end;
  1141. CUU: case szindex(argw(pop)) of
  1142. 1: if szindex(argw(pop))=2 then trap(EILLINS);
  1143. 2: trap(EILLINS);
  1144. end;
  1145. CUF: begin argwf(pop);
  1146. if szindex(argw(pop))=1 then pushr(pop) else trap(EILLINS)
  1147. end;
  1148. CFI: begin sz:=argw(pop); argwf(pop); rt:=popr;
  1149. case szindex(sz) of
  1150. 1: push(fitsw(trunc(rt),ECONV));
  1151. 2: pushd(fitd(trunc(rt)));
  1152. end
  1153. end;
  1154. CFU: begin sz:=argw(pop); argwf(pop); rt:=popr;
  1155. case szindex(sz) of
  1156. 1: push( chopw(trunc(abs(rt)-0.5)) );
  1157. 2: trap(EILLINS);
  1158. end
  1159. end;
  1160. CFF: begin argwf(pop); argwf(pop) end
  1161. end
  1162. end;
  1163. procedure logops;
  1164. var i,j:integer;
  1165. begin
  1166. case insr of
  1167. { LOGICAL GROUP }
  1168. XAND:
  1169. begin k:=argw(k);
  1170. for j:= 1 to k div wsize do
  1171. begin a:=sp+k; t:=pop; store(a,bf(andf,memw(a),t)) end;
  1172. end;
  1173. IOR:
  1174. begin k:=argw(k);
  1175. for j:= 1 to k div wsize do
  1176. begin a:=sp+k; t:=pop; store(a,bf(iorf,memw(a),t)) end;
  1177. end;
  1178. XOR:
  1179. begin k:=argw(k);
  1180. for j:= 1 to k div wsize do
  1181. begin a:=sp+k; t:=pop; store(a,bf(xorf,memw(a),t)) end;
  1182. end;
  1183. COM:
  1184. begin k:=argw(k);
  1185. for j:= 1 to k div wsize do
  1186. begin
  1187. store(sp+k-wsize*j, bf(xorf,memw(sp+k-wsize*j), negoff-1))
  1188. end
  1189. end;
  1190. ROL: begin k:=argw(k); if k<>wsize then trap(EILLINS);
  1191. t:=pop; s:=pop; for i:= 1 to t do rleft(s); push(s)
  1192. end;
  1193. ROR: begin k:=argw(k); if k<>wsize then trap(EILLINS);
  1194. t:=pop; s:=pop; for i:= 1 to t do rright(s); push(s)
  1195. end
  1196. end
  1197. end;
  1198. procedure setops;
  1199. var i,j:integer;
  1200. begin
  1201. case insr of
  1202. { SET GROUP }
  1203. INN:
  1204. begin k:=argw(k);
  1205. t:=pop;
  1206. i:= t mod 8; t:= t div 8;
  1207. if t>=k then
  1208. begin trap(ESET); s:=0 end
  1209. else
  1210. begin s:=memb(sp+t) end;
  1211. newsp(sp+k); push(bit(i,s));
  1212. end;
  1213. XSET:
  1214. begin k:=argw(k);
  1215. t:=pop;
  1216. i:= t mod 8; t:= t div 8;
  1217. for j:= 1 to k div wsize do push(0);
  1218. if t>=k then
  1219. trap(ESET)
  1220. else
  1221. begin s:=1; for j:= 1 to i do rleft(s); storeb(sp+t,s) end
  1222. end
  1223. end
  1224. end;
  1225. procedure arrops;
  1226. begin
  1227. case insr of
  1228. { ARRAY GROUP }
  1229. LAR:
  1230. begin k:=argw(k); if k<>wsize then trap(EILLINS); a:=popa;
  1231. pushx(argo(memw(a+2*k)),arraycalc(a))
  1232. end;
  1233. SAR:
  1234. begin k:=argw(k); if k<>wsize then trap(EILLINS); a:=popa;
  1235. popx(argo(memw(a+2*k)),arraycalc(a))
  1236. end;
  1237. AAR:
  1238. begin k:=argw(k); if k<>wsize then trap(EILLINS); a:=popa;
  1239. push(arraycalc(a))
  1240. end
  1241. end
  1242. end;
  1243. procedure cmpops;
  1244. begin
  1245. case insr of
  1246. { COMPARE GROUP }
  1247. CMI: case szindex(argw(k)) of
  1248. 1: begin st:=popsw; ss:=popsw;
  1249. if ss<st then pushsw(-1) else if ss=st then push(0) else push(1)
  1250. end;
  1251. 2: begin dt:=popd; ds:=popd;
  1252. if ds<dt then pushsw(-1) else if ds=dt then push(0) else push(1)
  1253. end;
  1254. end;
  1255. CMU: case szindex(argw(k)) of
  1256. 1: begin t:=pop; s:=pop;
  1257. if s<t then pushsw(-1) else if s=t then push(0) else push(1)
  1258. end;
  1259. 2: trap(EILLINS);
  1260. end;
  1261. CMP: begin a:=popa; b:=popa;
  1262. if b<a then pushsw(-1) else if b=a then push(0) else push(1)
  1263. end;
  1264. CMF: begin argwf(k); rt:=popr; rs:=popr;
  1265. if rs<rt then pushsw(-1) else if rs=rt then push(0) else push(1)
  1266. end;
  1267. CMS: begin k:=argw(k);
  1268. t:= 0; j:= 0;
  1269. while (j < k) and (t=0) do
  1270. begin if memw(sp+j) <> memw(sp+k+j) then t:=1;
  1271. j:=j+wsize
  1272. end;
  1273. newsp(sp+wsize*k); push(t);
  1274. end;
  1275. TLT: if popsw < 0 then push(1) else push(0);
  1276. TLE: if popsw <= 0 then push(1) else push(0);
  1277. TEQ: if pop = 0 then push(1) else push(0);
  1278. TNE: if pop <> 0 then push(1) else push(0);
  1279. TGE: if popsw >= 0 then push(1) else push(0);
  1280. TGT: if popsw > 0 then push(1) else push(0);
  1281. end
  1282. end;
  1283. procedure branchops;
  1284. begin
  1285. case insr of
  1286. { BRANCH GROUP }
  1287. BRA: newpc(pc+k);
  1288. BLT: begin st:=popsw; if popsw < st then newpc(pc+k) end;
  1289. BLE: begin st:=popsw; if popsw <= st then newpc(pc+k) end;
  1290. BEQ: begin t :=pop ; if pop = t then newpc(pc+k) end;
  1291. BNE: begin t :=pop ; if pop <> t then newpc(pc+k) end;
  1292. BGE: begin st:=popsw; if popsw >= st then newpc(pc+k) end;
  1293. BGT: begin st:=popsw; if popsw > st then newpc(pc+k) end;
  1294. ZLT: if popsw < 0 then newpc(pc+k);
  1295. ZLE: if popsw <= 0 then newpc(pc+k);
  1296. ZEQ: if pop = 0 then newpc(pc+k);
  1297. ZNE: if pop <> 0 then newpc(pc+k);
  1298. ZGE: if popsw >= 0 then newpc(pc+k);
  1299. ZGT: if popsw > 0 then newpc(pc+k)
  1300. end
  1301. end;
  1302. procedure callops;
  1303. var j:integer;
  1304. begin
  1305. case insr of
  1306. { PROCEDURE CALL GROUP }
  1307. CAL: call(argp(k));
  1308. CAI: begin call(argp(popa)) end;
  1309. RET: begin k:=argz(k); if k div wsize>maxret then trap(EILLINS);
  1310. for j:= 1 to k div wsize do retarea[j]:=pop; retsize:=k;
  1311. newsp(lb); lb:=maxdata+1; { To circumvent stack overflow error }
  1312. newpc(popa);
  1313. if pc=maxcode then
  1314. begin
  1315. halted:=true;
  1316. if retsize=wsize then exitstatus:=retarea[1]
  1317. else exitstatus:=undef
  1318. end
  1319. else
  1320. newlb(popa);
  1321. end;
  1322. LFR: begin k:=args(k); if k<>retsize then trap(EILLINS);
  1323. for j:=k div wsize downto 1 do push(retarea[j]);
  1324. end
  1325. end
  1326. end;
  1327. procedure miscops;
  1328. var i,j:integer;
  1329. begin
  1330. case insr of
  1331. { MISCELLANEOUS GROUP }
  1332. ASP,ASS:
  1333. begin if insr=ASS then
  1334. begin k:=argw(k); if k<>wsize then trap(EILLINS); k:=popsw end;
  1335. k:=argf(k);
  1336. if k<0
  1337. then for j:= 1 to -k div wsize do push(undef)
  1338. else newsp(sp+k);
  1339. end;
  1340. BLM,BLS:
  1341. begin if insr=BLS then
  1342. begin k:=argw(k); if k<>wsize then trap(EILLINS); k:=pop end;
  1343. k:=argz(k);
  1344. b:=popa; a:=popa;
  1345. for j := 1 to k div wsize do
  1346. store(b-wsize+wsize*j,memw(a-wsize+wsize*j))
  1347. end;
  1348. CSA: begin k:=argw(k); if k<>wsize then trap(EILLINS);
  1349. a:=popa;
  1350. st:= popsw - signwd(memw(a+asize));
  1351. if (st>=0) and (st<=memw(a+wsize+asize)) then
  1352. b:=mema(a+2*wsize+asize+asize*st) else b:=mema(a);
  1353. if b=0 then trap(ECASE) else newpc(b)
  1354. end;
  1355. CSB: begin k:=argw(k); if k<>wsize then trap(EILLINS); a:=popa;
  1356. t:=pop; i:=1; found:=false;
  1357. while (i<=memw(a+asize)) and not found do
  1358. if t=memw(a+(asize+wsize)*i) then found:=true else i:=i+1;
  1359. if found then b:=memw(a+(asize+wsize)*i+wsize) else b:=memw(a);
  1360. if b=0 then trap(ECASE) else newpc(b);
  1361. end;
  1362. DCH: begin pusha(mema(popa+dynd)) end;
  1363. DUP,DUS:
  1364. begin if insr=DUS then
  1365. begin k:=argw(k); if k<>wsize then trap(EILLINS); k:=pop end;
  1366. k:=args(k);
  1367. for i:=1 to k div wsize do push(memw(sp+k-wsize));
  1368. end;
  1369. EXG: begin
  1370. k:=argw(k);
  1371. for i:=1 to k div wsize do push(memw(sp+k-wsize));
  1372. for i:=0 to k div wsize - 1 do
  1373. store(sp+k+i*wsize,memw(sp+k+k+i*wsize));
  1374. for i:=1 to k div wsize do
  1375. begin t:=pop ; store(sp+k+k-wsize,t) end;
  1376. end;
  1377. FIL: filna(argg(k));
  1378. GTO: begin k:=argg(k);
  1379. newlb(mema(k+2*asize)); newsp(mema(k+asize)); newpc(mema(k))
  1380. end;
  1381. LIM: push(ignmask);
  1382. LIN: lino(argn(k));
  1383. LNI: lino(memw(0)+1);
  1384. LOR: begin i:=argr(k);
  1385. case i of 0:pusha(lb); 1:pusha(sp); 2:pusha(hp) end;
  1386. end;
  1387. LPB: pusha(popa+statd);
  1388. MON: domon(pop);
  1389. NOP: writeln('NOP at line ',memw(0):5) ;
  1390. RCK: begin a:=popa;
  1391. case szindex(argw(k)) of
  1392. 1: if (signwd(memw(sp))<signwd(memw(a))) or
  1393. (signwd(memw(sp))>signwd(memw(a+wsize))) then trap(ERANGE);
  1394. 2: if (memd(sp)<memd(a)) or
  1395. (memd(sp)>memd(a+2*wsize)) then trap(ERANGE);
  1396. end
  1397. end;
  1398. RTT: dortt;
  1399. SIG: begin a:=popa; pusha(uerrorproc); uerrorproc:=a end;
  1400. SIM: ignmask:=pop;
  1401. STR: begin i:=argr(k);
  1402. case i of 0: newlb(popa); 1: newsp(popa); 2: newhp(popa) end;
  1403. end;
  1404. TRP: trap(pop)
  1405. end
  1406. end;
  1407. .bp
  1408. {---------------------------------------------------------------------------}
  1409. { Main Loop }
  1410. {---------------------------------------------------------------------------}
  1411. begin initialize;
  1412. 8888:
  1413. repeat
  1414. opcode := nextpc; { fetch the first byte of the instruction }
  1415. if opcode=escape1 then iclass:=second
  1416. else if opcode=escape2 then iclass:=tert
  1417. else iclass:=prim;
  1418. if iclass<>prim then opcode := nextpc;
  1419. with dispat[iclass][opcode] do
  1420. begin insr:=instr;
  1421. if not (zbit in iflag) then
  1422. if ibit in iflag then k:=pop else
  1423. begin
  1424. if mini in iflag then k:=implicit else
  1425. begin
  1426. if short in iflag then k:=implicit+nextpc else
  1427. begin k:=nextpc;
  1428. if (sbit in iflag) and (k>=128) then k:=k-256;
  1429. for i:=2 to ilength do k:=256*k + nextpc
  1430. end
  1431. end;
  1432. if wbit in iflag then k:=k*wsize;
  1433. end
  1434. end;
  1435. case insr of
  1436. NON: trap(EILLINS);
  1437. { LOAD GROUP }
  1438. LDC,LOC,LOL,LOE,LIL,LOF,LAL,LAE,LXL,LXA,LOI,LOS,LDL,LDE,LDF,LPI:
  1439. loadops;
  1440. { STORE GROUP }
  1441. STL,STE,SIL,STF,STI,STS,SDL,SDE,SDF:
  1442. storeops;
  1443. { SIGNED INTEGER ARITHMETIC }
  1444. ADI,SBI,MLI,DVI,RMI,NGI,SLI,SRI:
  1445. intarith;
  1446. { UNSIGNED INTEGER ARITHMETIC }
  1447. ADU,SBU,MLU,DVU,RMU,SLU,SRU:
  1448. unsarith;
  1449. { FLOATING POINT ARITHMETIC }
  1450. ADF,SBF,MLF,DVF,NGF,FIF,FEF:
  1451. fltarith;
  1452. { POINTER ARITHMETIC }
  1453. ADP,ADS,SBS:
  1454. ptrarith;
  1455. { INCREMENT/DECREMENT/ZERO }
  1456. INC,INL,INE,DEC,DEL,DEE,ZRL,ZRE,ZER,ZRF:
  1457. incops;
  1458. { CONVERT GROUP }
  1459. CII,CIU,CIF,CUI,CUU,CUF,CFI,CFU,CFF:
  1460. convops;
  1461. { LOGICAL GROUP }
  1462. XAND,IOR,XOR,COM,ROL,ROR:
  1463. logops;
  1464. { SET GROUP }
  1465. INN,XSET:
  1466. setops;
  1467. { ARRAY GROUP }
  1468. LAR,SAR,AAR:
  1469. arrops;
  1470. { COMPARE GROUP }
  1471. CMI,CMU,CMP,CMF,CMS, TLT,TLE,TEQ,TNE,TGE,TGT:
  1472. cmpops;
  1473. { BRANCH GROUP }
  1474. BRA, BLT,BLE,BEQ,BNE,BGE,BGT, ZLT,ZLE,ZEQ,ZNE,ZGE,ZGT:
  1475. branchops;
  1476. { PROCEDURE CALL GROUP }
  1477. CAL,CAI,RET,LFR:
  1478. callops;
  1479. { MISCELLANEOUS GROUP }
  1480. ASP,ASS,BLM,BLS,CSA,CSB,DCH,DUP,DUS,EXG,FIL,GTO,LIM,
  1481. LIN,LNI,LOR,LPB,MON,NOP,RCK,RTT,SIG,SIM,STR,TRP:
  1482. miscops;
  1483. end; { end of case statement }
  1484. if not ( (insr=RET) or (insr=ASP) or (insr=BRA) or (insr=GTO) ) then
  1485. retsize:=0 ;
  1486. until halted;
  1487. 9999:
  1488. writeln('halt with exit status: ',exitstatus:1);
  1489. doident;
  1490. end.