em.i 52 KB

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