home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / version-1-0 / Beta / blitz.c < prev    next >
C/C++ Source or Header  |  2007-09-13  |  290KB  |  9,682 lines

  1. /* The BLITZ Machine Emulator
  2. **
  3. ** Copyright 2001-2007, Harry H. Porter III
  4. **
  5. ** This file may be freely copied, modified and compiled, on the sole
  6. ** conditions that if you modify it...
  7. **
  8. **   (1) Your name and the date of modification is added to this comment
  9. **       under "Modifications by", and
  10. **
  11. **   (2) Your name and the date of modification is added to the
  12. **       "commandLineHelp()" routine under "Modifications by".
  13. **
  14. ** Original Author:
  15. **   02/05/01 - Harry H. Porter III
  16. **
  17. ** Modifcations by:
  18. **   09/28/04 - Harry - Fix bug with randomSeed, disk errno
  19. **   11/26/04 - Harry - Fix bugs with serial input
  20. **   03/15/06 - Harry - Renamed "SPANK" to "BLITZ"
  21. **   04/27/07 - Harry - Support for little endian added
  22. **
  23. ** Please respect the coding and commenting style of this program.
  24. **
  25. */
  26.  
  27. #include <sys/time.h>
  28. #include <sys/types.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <stdarg.h>
  32. #include <string.h>
  33. #include <signal.h>
  34. #include <math.h>
  35. #include <errno.h>
  36.  
  37.  
  38.  
  39. /* SWAP_BYTES (int)  -->  int
  40. **
  41. ** This macro is used to swap the bytes in a 32-bit int from Big Endian order
  42. ** to Little Endian order, or vice-versa.
  43. **
  44. ** For example:
  45. **     i = SWAP_BYTES (i);
  46. **
  47. ** This program was originally written for a Big Endian architecture so swapping
  48. ** bytes was never necessary.  When compiled on a Big Endian computer, this macro
  49. ** is a no-op; when compiled on a Little Endian machine, it will swap the bytes.
  50. **
  51. */
  52. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  53. #define SWAP_BYTES(x) \
  54.     ((int)((((int)(x) & 0xff000000) >> 24) | \
  55.            (((int)(x) & 0x00ff0000) >>  8) | \
  56.            (((int)(x) & 0x0000ff00) <<  8) | \
  57.            (((int)(x) & 0x000000ff) << 24)))
  58. #else
  59. #define SWAP_BYTES(x) (x)
  60. #endif
  61.  
  62.  
  63.  
  64. typedef struct TableEntry TableEntry;
  65.  
  66.  
  67.  
  68. /*****  Global variables *****/
  69.  
  70. #define PAGE_SIZE 8192            /* Page Size (8K) */
  71. char * memory;                    /* Where machine memory is stored */
  72. int currentMemoryLock = -1;       /* Address of word holding lock, or -1 */
  73. int userRegisters [16];           /* User regs r0, r1, ... r15 */
  74. int systemRegisters [16];         /* System regs r0, r1, ... r15 */
  75. double floatRegisters [16];       /* Floating regs f0, f1, ... f15 */
  76. int pc;                           /* The current program counter */
  77. int ptbr;                         /* The page table base register */
  78. int ptlr;                         /* The page table length register */
  79. int statusN;                      /* Status register, N bit */
  80. int statusV;                      /* Status register, V bit */
  81. int statusZ;                      /* Status register, Z bit */
  82. int statusP;                      /* Status register, P bit */
  83. int statusS;                      /* Status register, S bit */
  84. int statusI;                      /* Status register, I bit */
  85. int instruction;                  /* The current instruction being executed */
  86. int commandOptionG = 0;           /* Automatically begin emulation, bypass interface */
  87. int commandOptionRaw = 0;         /* Prefer "raw" mode (default is "cooked") */
  88. int commandOptionWait = 0;        /* On 'wait' instruction, with for input */
  89. int commandOptionRand = 0;        /* Set if "-r" was on command line */
  90. int randSeedFromOption;           /* The value following "-r" */
  91. int randomSeed;                   /* The running pseudo-random number */
  92. char * executableFileName = NULL; /* The a.out filename */
  93. FILE * executableFile;            /* The a.out file */
  94. char * diskFileName = NULL;       /* The DISK filename */
  95. FILE * diskFile = NULL;           /* The DISK file, possibly NULL */
  96. char * termInputFileName = NULL;  /* The TERMINPUT filename */
  97. FILE * termInputFile;             /* The TERMINPUT file */
  98. char * termOutputFileName = NULL; /* The TERMOUTPUT filename */
  99. FILE * termOutputFile;            /* The TERMOUTPUT file */
  100. int textAddr;                     /* Starting addr of .text segment */
  101. int dataAddr;                     /* Starting addr of .data segment */
  102. int bssAddr;                      /* Starting addr of .bss segment */
  103. int textSize;                     /* Sum of all .text segment sizes */
  104. int dataSize;                     /* Sum of all .data segment sizes */
  105. int bssSize;                      /* Sum of all .bss segment sizes */
  106. char * pmPtr;
  107. int pmSize;
  108. int pmCount;
  109. int pmRow[16];
  110. int currentAddr;                  /* This is used in commandDis */
  111. int currentFrame;                 /* This is used in commandStackUp, commandStackDown */
  112. char inputBuffer [100];           /* Used for user input */
  113. int interruptsSignaled;           /* 0=none; otherwise one bit per type */
  114. int systemTrapNumber;             /* Set by the syscall exception */
  115. int pageInvalidOffendingAddress;  /* Used by Page Invalid exception */
  116. int pageReadonlyOffendingAddress; /* Used by Page Readonly exception */
  117. int translateCausedException;     /* Set by translate () */
  118. int currentTime;                  /* Measured in "clicks": 1,2,3,... */
  119. int timeSpentAsleep;              /* Incremented by "wait" instruction */
  120. int timeOfNextEvent;              /* Minimum of disk, serial, and timer */
  121. int timeOfNextTimerEvent;         /* Zero = now, MAX = never */
  122. int timeOfNextDiskEvent;          /* Zero = now, MAX = never */
  123. int timeOfNextSerialInEvent;      /* Zero = now, MAX = never */
  124. int timeOfNextSerialOutEvent;     /* Zero = now, MAX = never */
  125. int executionHalted;              /* 0=continue executing; 1=halted */
  126. int controlCPressed;              /* 0=not pressed; 1=pressed */
  127. int wantPrintingInSingleStep;     /* Used by singleStep to control verbosity. */
  128. int q;                            /* Used by the divide() routine */
  129. int r;                            /* Used by the divide() routine */
  130. double POSITIVE_INFINITY;         /* Effectively a constant */
  131. double NEGATIVE_INFINITY;         /* Effectively a constant */
  132. int terminalInRawMode;            /* 1=stdin is in "raw mode" for BLITZ input */
  133. int terminalWantRawProcessing;    /* 1=user wants raw, unbuffered input */
  134. int termInChar;                   /* The input character, zero if none */
  135. int termInCharAvail;              /* Set when key pressed, clr'd when queried */
  136. int termInCharWasUsed;            /* True after BLITZ program looks at char */
  137. int termOutputReady;              /* 1=busy, 0=not busy */
  138. #define TYPE_AHEAD_BUFFER_SIZE 100
  139.                                   /* Characters to be supplied to serial in */
  140. char typeAheadBuffer [TYPE_AHEAD_BUFFER_SIZE];
  141. int typeAheadBufferCount;         /* Number of characters in the buffer */
  142. int typeAheadBufferIn;            /* Next pos to put into */
  143. int typeAheadBufferOut;           /* Next pos to get from */
  144.  
  145.  
  146.  
  147.  
  148. /*****  Simulation Constants  *****
  149. **
  150. ** The following variables are "constants" in the sense that they remain fixed
  151. **   during instruction emulation.  They are set at startup time and when the
  152. **   "reset" command is executed.  If a file called ".blitzrc" exists, their
  153. **   values will be obtained from this; otherwise they will be given default
  154. **   values.  The default values are in the "setSimulationConstants()" routine.
  155. **
  156. ** All times are in terms of instruction "cycles."  Except for interrupts, there
  157. **   will be one instruction executed per cycle.  For a 1MHz machine, each
  158. **   cycle corresponds to 1 microsecond.
  159. **    
  160. ** To simulate human typing on the serial input, we might use
  161. **     KEYBOARD_WAIT_TIME = 1000    KEYBOARD_WAIT_TIME_VARIATION = 100000
  162. ** To simulate an output rate of 19 KBaud on the serial output, we might use
  163. **     TERM_OUT_DELAY = 500         TERM_OUT_DELAY_VARIATION = 10
  164. ** To switch processes about 200 times per second, we might use
  165. **     TIME_SLICE = 5000            TIME_SLICE_VARIATION = 10
  166. **   (To turn off Timer Interrupts, use TIME_SLICE = 0.)
  167. ** DISK_SEEK_TIME is the time to move the disk head one track.
  168. ** DISK_SETTLE_TIME is the time to wait after any head movement.
  169. ** DISK_ROTATIONAL_DELAY is the time for the disk to advance 1 sector.
  170. **   This value must be at least 1.  Transfer time is the rotational delay
  171. **   times the number of sectors.  (Note: we do not model the time for track
  172. **   seek and settle during a multi-sector transfer.)
  173. ** Disk access is the sum of seek, settle, rotational delay and transfer time.
  174. ** There can be a random variation added onto this, given by DISK_ACCESS_VARIATION.
  175. **
  176. ** DISK_READ_ERROR_PROBABILITY is the probability that an I/O error will occur
  177. **   on any one read operation.  It is given as a number like 500, which means
  178. **   1 error in every 500.  A value of 0 means errors never occur.
  179. ** DISK_WRITE_ERROR_PROBABILITY is the probability that an I/O error will occur
  180. **   on any one write operatiorn.
  181. **
  182. ** The MEMORY_SIZE is in bytes.  Typically, this would be something like 16Mbytes.
  183. **
  184. ** The "Memory Mapped Area" is a region of memory that behaves differently than
  185. **   normaly memory; when the program reads or writes bytes in this region, the exact
  186. **   behavior depends on the I/O device specification.  The MEMORY_MAPPED_AREA_LOW
  187. **   is the lowest address and the MEMORY_MAPPED_AREA_HIGH is the highest address
  188. **   in this region.  (E.g., 0x00ffff00, 0x00ffffff).
  189. **
  190. ** The various words in the emory Mapped Area have specific addresses given by
  191. **   SERIAL_STATUS_WORD_ADDRESS
  192. **   SERIAL_DATA_WORD_ADDRESS
  193. **   DISK_STATUS_WORD_ADDRESS
  194. **   DISK_COMMAND_WORD_ADDRESS
  195. **   DISK_MEMORY_ADDRESS_REGISTER
  196. **   DISK_SECTOR_NUMBER_REGISTER
  197. **   DISK_SECTOR_COUNT_REGISTER
  198. **  
  199. */
  200.  
  201. int KEYBOARD_WAIT_TIME = -1;
  202. int KEYBOARD_WAIT_TIME_VARIATION = -1;
  203. int TERM_OUT_DELAY = -1;
  204. int TERM_OUT_DELAY_VARIATION = -1;
  205. int TIME_SLICE = -1;
  206. int TIME_SLICE_VARIATION = -1;
  207. int DISK_SEEK_TIME = -1;
  208. int DISK_SETTLE_TIME = -1;
  209. int DISK_ROTATIONAL_DELAY = -1;
  210. int DISK_ACCESS_VARIATION = -1;
  211. int DISK_READ_ERROR_PROBABILITY = -1;
  212. int DISK_WRITE_ERROR_PROBABILITY = -1;
  213. int INIT_RANDOM_SEED = -1;
  214. int MEMORY_SIZE = -1;
  215. int MEMORY_MAPPED_AREA_LOW = -1;
  216. int MEMORY_MAPPED_AREA_HIGH = -1;
  217. int SERIAL_STATUS_WORD_ADDRESS = -1;
  218. int SERIAL_DATA_WORD_ADDRESS = -1;
  219. int DISK_STATUS_WORD_ADDRESS = -1;
  220. int DISK_COMMAND_WORD_ADDRESS = -1;
  221. int DISK_MEMORY_ADDRESS_REGISTER = -1;
  222. int DISK_SECTOR_NUMBER_REGISTER = -1;
  223. int DISK_SECTOR_COUNT_REGISTER = -1;
  224.  
  225.  
  226.  
  227. /*****  The disk simulation  *****
  228. **
  229. ** The disk is divided into sectors, whose size matches the
  230. ** "PAGE_SIZE" exaclty (i.e., 8Kbytes).  The disk is assumed to have
  231. ** one surface with a single disk head.  In other words, there
  232. ** is only 1 track per cylinder.  Hereafter, we speak only of
  233. ** tracks and ignore the concept of cylinders.  Each track is
  234. ** assumed  to have 16 sectors, giving 128Kbytes per track
  235. ** (= SECTORS_PER_TRACK * PAGE_SIZE).
  236. **
  237. ** The size of the disk is some multiple of tracks, > 0.  The
  238. ** actual size is determined by the size of the disk file.  When
  239. ** the file is opened, we determine the "diskTrackCount" and the
  240. ** "diskSectorCount" (which is 16 times greater).
  241. **
  242. ** At any time time, the disk has a current rotational position
  243. ** "currentDiskSector" which is a sector number between 0 and
  244. ** SECTORS_PER_TRACK-1.
  245. **
  246. ** The status of the disk is given by two variables:
  247. **    currentDiskStatus
  248. **    futureDiskStatus
  249. ** Whenever a disk operation is called for, it is done immediately
  250. ** during a single insrtuction cycle.  However, the simulation calls
  251. ** for the disk to reamin busy for a while.  Thus, the current status
  252. ** will be busy, and a future status (eg, completedOK or completedWithError)
  253. ** will be determined.  An event will be scheduled.  At that event,
  254. ** the status will be changed to the new status, and an interrupt will
  255. ** be signalled.
  256. **
  257. ** The possible values for these status variables are:
  258. **    DiskBusy                      0x00000000
  259. **    OperationCompletedOK          0x00000001
  260. **    OperationCompletedWithError1  0x00000002
  261. **    OperationCompletedWithError2  0x00000003
  262. **    OperationCompletedWithError3  0x00000004
  263. **    OperationCompletedWithError4  0x00000005
  264. **    OperationCompletedWithError5  0x00000006
  265. **
  266. ** All disk operations are in terms of a sector number, between
  267. ** 0 and diskSectorCount-1.  When a disk operation is requested, we
  268. ** will determine the delay, based on the current position of the
  269. ** head (i.e., which track it is on), and the current rotational
  270. ** angle of the disk platter (i.e., which sector within the track
  271. ** it is on).
  272. **
  273. ** The emulator will then go to the DISK file and perform the
  274. ** operation.  Finally, it will schedule a "disk event" to initiate
  275. ** the interrupt that must be signaled when the disk operation is
  276. ** supposed to complete.  In the case of a disk read operation, we
  277. ** also mark the words in memory so the memory manager can signal an
  278. ** error if the BLITZ program happens to read or write them while the
  279. ** disk operation is in progress.  These are given by "diskBufferLow"
  280. ** and "diskBufferHigh".
  281. **
  282. */
  283. #define SECTORS_PER_TRACK                           16
  284.  
  285. #define DISK_BUSY                           0x00000000
  286. #define OPERATION_COMPLETED_OK              0x00000001
  287. #define OPERATION_COMPLETED_WITH_ERROR_1    0x00000002
  288. #define OPERATION_COMPLETED_WITH_ERROR_2    0x00000003
  289. #define OPERATION_COMPLETED_WITH_ERROR_3    0x00000004
  290. #define OPERATION_COMPLETED_WITH_ERROR_4    0x00000005
  291. #define OPERATION_COMPLETED_WITH_ERROR_5    0x00000006
  292.  
  293. #define DISK_READ_COMMAND                   0x00000001
  294. #define DISK_WRITE_COMMAND                  0x00000002
  295.  
  296.  
  297. int diskTrackCount = 0;                          /* The size of the disk */
  298. int diskSectorCount = 0;                         /* The size of the disk */
  299. int currentDiskSector = 0;                       /* The current disk position */
  300. int currentDiskStatus = OPERATION_COMPLETED_OK;  /* Current status */
  301. int futureDiskStatus = OPERATION_COMPLETED_OK;   /* What to become upon next disk event */
  302. int diskBufferLow = 0;                           /* Used to error-check memory accesses */
  303. int diskBufferHigh = 0;                          /* Used to error-check memory accesses */
  304. int diskMemoryAddressRegister = 0x00000000;      /* What to read from memory-mapped area */
  305. int diskSectorNumberRegister = 0x00000000;       /* What to read from memory-mapped area */
  306. int diskSectorCountRegister = 0x00000000;        /* What to read from memory-mapped area */
  307. int numberOfDiskReads = 0;                       /* How many since last reset (not errors) */
  308. int numberOfDiskWrites = 0;                      /* How many since last reset (not errors) */
  309.  
  310.  
  311.  
  312. /*****  Memory-Mapped I/O Addresses  *****
  313. **
  314. ** A special region is physical memory is "memory-mapped I/O".
  315. ** Loads and stores to this region go directly to/from the various
  316. ** I/O devices, rather than the memory.  These are the locations of
  317. ** various special I/O registers that are mapped into the physical
  318. ** memory address space.
  319. **                                  default value
  320. **                                  =============
  321. **     MEMORY_MAPPED_AREA_LOW         0x00ffff00
  322. **     MEMORY_MAPPED_AREA_HIGH        0x00ffffff
  323. **
  324. **     SERIAL_STATUS_WORD_ADDRESS     0x00ffff00
  325. **     SERIAL_DATA_WORD_ADDRESS       0x00ffff04
  326. **
  327. **     DISK_STATUS_WORD_ADDRESS       0x00ffff08
  328. **     DISK_COMMAND_WORD_ADDRESS      0x00ffff08
  329. **     DISK_MEMORY_ADDRESS_REGISTER   0x00ffff0c
  330. **     DISK_SECTOR_NUMBER_REGISTER    0x00ffff10
  331. **     DISK_SECTOR_COUNT_REGISTER     0x00ffff14
  332. */
  333.  
  334.  
  335.  
  336. /*****  Interrupt Codes  *****
  337. **
  338. ** These codes are used to set and test bits in the
  339. ** interruptSignaled flag.  See also
  340. **    getNextInterrupt
  341. **    getVectorNumber
  342. */
  343. #define POWER_ON_RESET                      1
  344. #define TIMER_INTERRUPT                     2
  345. #define DISK_INTERRUPT                      4
  346. #define SERIAL_INTERRUPT                    8
  347. #define HARDWARE_FAULT                     16
  348. #define ILLEGAL_INSTRUCTION                32
  349. #define ARITHMETIC_EXCEPTION               64
  350. #define ADDRESS_EXCEPTION                 128
  351. #define PAGE_INVALID_EXCEPTION            256
  352. #define PAGE_READONLY_EXCEPTION           512
  353. #define PRIVILEGED_INSTRUCTION           1024
  354. #define ALIGNMENT_EXCEPTION              2048
  355. #define EXCEPTION_DURING_INTERRUPT       4096
  356. #define SYSCALL_TRAP                     8192
  357.  
  358.  
  359. #define MAX 2147483647
  360. int MIN;                 /* Initialized in main to -2147483648 */
  361.  
  362.  
  363. /*****  Label Table  *****
  364. **
  365. ** Each label is an identifier (consisting of letters, digits, underscores,
  366. ** and periods).  Each label has a value, which is a 32-bit integer.
  367. ** Label values are absolute, not relative, at run-time.  Each label-value
  368. ** pair is represented with a "TableEntry".  The TableEntries are indexed
  369. ** in two ways.  First, by name:  There is an array ("alphaIndex") which
  370. ** contains pointers to TableEntries.  Second, by value:  There is an array
  371. ** ("valueIndex") which contains pointers to TableEntries.  Both arrays will
  372. ** have the same number of elements, namely, "numberOfLabels", and both
  373. ** arrays are kept in sorted order.
  374. */
  375. struct TableEntry {
  376.   int   value;
  377.   char  string [0];       /* The characters, including \0 at end */
  378. };
  379.  
  380. #define MAX_NUMBER_OF_LABELS 500000
  381. TableEntry * alphaIndex [MAX_NUMBER_OF_LABELS];
  382. TableEntry * valueIndex [MAX_NUMBER_OF_LABELS];
  383. int numberOfLabels = 0;
  384.  
  385.  
  386.  
  387. /*****  Function prototypes  *****/
  388.  
  389. int main (int argc, char ** argv);
  390. void printFinalStats ();
  391. void checkHostCompatibility ();
  392. void checkArithmetic ();
  393. int isNegZero (double d);
  394. void processCommandLine (int argc, char ** argv);
  395. void badOption (char * msg);
  396. void errorExit ();
  397. void fatalError (char * msg);
  398. void commandLineHelp ();
  399. char * getToken ();
  400. char * trimToken (char * str);
  401. char * toLower (char * str);
  402. int readInteger (FILE * file);
  403. int readByte (FILE * file);
  404. int roundUpToMultipleOf (int i, int p);
  405. void printMemory (char * ptr, int n, int addr);
  406. void get16Bytes ();
  407. int getNextByte ();
  408. void putlong (int i);
  409. void puthalf (int i);
  410. void printline ();
  411. void printByte (int c);
  412. int bytesEqual (char * p, char * q, int lengthP, int lengthQ);
  413. void commandHelp ();
  414. void commandAllRegs ();
  415. void commandAllFloatRegs ();
  416. void printDouble (double d);
  417. void printDoubleVal (double d);
  418. void commandInfo ();
  419. void printPendingInterrupts ();
  420. void commandIO ();
  421. void commandSim ();
  422. void commandRaw ();
  423. void commandCooked ();
  424. void printSerialHelp ();
  425. void commandFormat ();
  426. void commandTest ();
  427. void printNumberNL2 (int i);
  428. void printNumberNL (int i);
  429. void commandDumpMemory ();
  430. void commandSetMemory ();
  431. void commandWriteWord ();
  432. void commandReadWord ();
  433. void commandInput ();
  434. void addToTypeAhead (int ch);
  435. void printTypeAheadBuffer ();
  436. void fancyPrintChar (int ch);
  437. int readHexInt ();
  438. int readDecimalInt ();
  439. double readDouble ();
  440. int readYesNo ();
  441. void commandSetI ();
  442. void commandSetS ();
  443. void commandSetP ();
  444. void commandSetZ ();
  445. void commandSetV ();
  446. void commandSetN ();
  447. void commandClearI ();
  448. void commandClearS ();
  449. void commandClearP ();
  450. void commandClearZ ();
  451. void commandClearV ();
  452. void commandClearN ();
  453. void commandSetPC ();
  454. void commandSetPTBR ();
  455. void commandSetPTLR ();
  456. void commandPrintPageTable ();
  457. void commandTranslate ();
  458. void commandCancel ();
  459. void commandLabels ();
  460. void printStringInWidth (char * string, int width);
  461. void commandInit ();
  462. void commandInit2 ();
  463. void commandSort ();
  464. void commandSort2 ();
  465. void quicksortAlpha (int m, int n);
  466. int partitionAlpha (int l, int r);
  467. void quicksortValue (int m, int n);
  468. int partitionValue (int l, int r);
  469. void commandFind ();
  470. void commandFind2 ();
  471. int findLabel (int value);
  472. int findLabel2 (int value, int low, int high);
  473. TableEntry * findLabelByAlpha (char * string, int low, int high);
  474. void commandAdd ();
  475. void insertLabel (TableEntry * p);
  476. void insertLabelUnsorted (TableEntry * p);
  477. void commandReset ();
  478. void resetState ();
  479. void printInfo ();
  480. void commandReg (int reg);
  481. void commandFloatReg (int reg);
  482. void commandFMem ();
  483. void commandDis ();
  484. void commandDis2 ();
  485. void disassemble (int addr);
  486. void printRa (int instr);
  487. void printRb (int instr);
  488. void printRc (int instr);
  489. void printFRa (int instr);
  490. void printFRb (int instr);
  491. void printFRc (int instr);
  492. void printData16 (int instr);
  493. void printData24 (int instr);
  494. void printComment (int i);
  495. void printComment2 (int i);
  496. void printAboutToExecute ();
  497. void commandStepN ();
  498. void commandGo (int count);
  499. void commandStep ();
  500. void commandStepHigh ();
  501. void commandStepHigh2 ();
  502. void singleStep ();
  503. int getRa (int instr);
  504. int getRb (int instr);
  505. int getRc (int instr);
  506. void putRc (int instr, int value);
  507. int getData16 (int instr);
  508. int getData24 (int instr);
  509. double getFRa (int instr);
  510. double getFRb (int instr);
  511. double getFRc (int instr);
  512. void putFRc (int instr, double value);
  513. int buildStatusWord ();
  514. void setStatusFromWord (int word);
  515. void setSR (int value, int overflow);
  516. int getNextInterrupt ();
  517. int getVectorNumber ();
  518. int physicalAddressOk (int addr);
  519. int isAligned (int addr);
  520. int getPhysicalWord (int addr);
  521. int getPhysicalWordAndLock (int addr);
  522. void putPhysicalWord (int addr, int value);
  523. void putPhysicalWordAndRelease (int addr, int value);
  524. void releaseMemoryLock (int physAddr);
  525. int inMemoryMappedArea (int addr);
  526. int translate (int addr, int reading, int wantPrinting, int doUpdates);
  527. int getMemoryMappedWord (int physAddr);
  528. void putMemoryMappedWord (int physAddr, int value);
  529. void pushOntoStack (int reg, int value);
  530. int popFromStack (int reg);
  531. void commandShowStack ();
  532. void commandFrame ();
  533. void commandStackUp ();
  534. void commandStackDown ();
  535. int printFrame (int frameNumber, int longPrint);
  536. int printAsciiDataInWidth (int ptr, int width);
  537. void commandHex ();
  538. void commandDecimal ();
  539. void commandAscii ();
  540. void printHexDecimalAscii (int i);
  541. void jumpIfTrueRaRb (int cond, int instr);
  542. void jumpIfTrueData24 (int cond, int instr);
  543. void controlC (int sig);
  544. int randomBetween (int lo, int high);
  545. int genRandom ();
  546. void doTimerEvent ();
  547. void doDiskEvent ();
  548. void doSerialInEvent (int waitForKeystroke);
  549. void doSerialOutEvent ();
  550. void updateTimeOfNextEvent ();
  551. void divide (int a, int b);
  552. void turnOnTerminal ();
  553. void turnOffTerminal ();
  554. char checkForInput (int waitForKeystroke);
  555. int characterAvailableOnStdin ();
  556. void initializeDisk ();
  557. void performDiskIO (int command);
  558. void checkDiskBufferError (int physAddr);
  559. void setSimulationConstants ();
  560. void defaultSimulationConstants ();
  561. void printKPLStmtCode ();
  562. void printCurrentFileLineAndFunction ();
  563.  
  564.  
  565. /* main()
  566. **
  567. ** Scan the command line arguments, then enter into the command loop.
  568. */
  569. main (int argc, char ** argv) {
  570.   int i;
  571.   char * command;
  572.  
  573.   checkHostCompatibility ();
  574.   checkArithmetic ();
  575.   MIN = -MAX - 1;
  576.   POSITIVE_INFINITY = 1.0 / 0.0;
  577.   NEGATIVE_INFINITY = -1.0 / 0.0;
  578.   processCommandLine (argc, argv);
  579.   terminalInRawMode = 0;
  580.   resetState ();
  581.   signal (SIGINT, controlC);
  582.  
  583.   /* If the "auto go" option (-g) was given, the just begin execution. */
  584.   if (commandOptionG) {
  585.     commandGo (MAX);
  586.     if (executionHalted) {
  587.       printFinalStats ();
  588.       exit (0);
  589.     } else {
  590.       fprintf (stderr, "\n\rEntering machine-level debugger...\n\r");
  591.     }
  592.   }
  593.  
  594.   printf (
  595. "======================================================\n"
  596. "=====                                            =====\n"
  597. "=====         The BLITZ Machine Emulator         =====\n"
  598. "=====                                            =====\n"
  599. "=====  Copyright 2001-2007, Harry H. Porter III  =====\n"
  600. "=====                                            =====\n"
  601. "======================================================\n"
  602. "\n"
  603. "Enter a command at the prompt.  Type 'quit' to exit or 'help' for\n"
  604. "info about commands.\n");
  605.  
  606.   /* Each execution of this loop will read and execute one command. */
  607.   while (1) {
  608.     controlCPressed = 0;  /* Forget about any control-C's */
  609.  
  610.     printf ("> ");
  611.     command = toLower (getToken ());
  612.  
  613.     if (!strcmp (command, "quit") || !strcmp (command, "q")) {
  614.       printFinalStats ();
  615.       exit (0);
  616.     } else if (!strcmp (command, "")) {
  617.       // printf ("BLITZ Emulator: You are in the command-line interface.\n");
  618.       /* ignore empty commands */
  619.     } else if (!strcmp (command, "help") || !strcmp (command, "h")) {
  620.       commandHelp ();
  621.     } else if (!strcmp (command, "info") || !strcmp (command, "i")) {
  622.       commandInfo ();
  623.     } else if (!strcmp (command, "io")) {
  624.       commandIO ();
  625.     } else if (!strcmp (command, "sim")) {
  626.       commandSim ();
  627.     } else if (!strcmp (command, "raw")) {
  628.       commandRaw ();
  629.     } else if (!strcmp (command, "cooked")) {
  630.       commandCooked ();
  631.     } else if (!strcmp (command, "format")) {
  632.       commandFormat ();
  633.     } else if (!strcmp (command, "go") || !strcmp (command, "g")) {
  634.       commandGo (MAX);
  635.     } else if (!strcmp (command, "step") || !strcmp (command, "s")) {
  636.       commandStep ();
  637.     } else if (!strcmp (command, "t")) {
  638.       commandStepHigh ();
  639.     } else if (!strcmp (command, "u")) {
  640.       commandStepHigh2 ();
  641.     } else if (!strcmp (command, "stepn")) {
  642.       commandStepN ();
  643.     } else if (!strcmp (command, "dumpmem") || !strcmp (command, "dm")) {
  644.       commandDumpMemory ();
  645.     } else if (!strcmp (command, "setmem")) {
  646.       commandSetMemory ();
  647.     } else if (!strcmp (command, "read")) {
  648.       commandReadWord ();
  649.     } else if (!strcmp (command, "write")) {
  650.       commandWriteWord ();
  651.     } else if (!strcmp (command, "input")) {
  652.       commandInput ();
  653.     } else if (!strcmp (command, "seti")) {
  654.       commandSetI ();
  655.     } else if (!strcmp (command, "sets")) {
  656.       commandSetS ();
  657.     } else if (!strcmp (command, "setp")) {
  658.       commandSetP ();
  659.     } else if (!strcmp (command, "setz")) {
  660.       commandSetZ ();
  661.     } else if (!strcmp (command, "setv")) {
  662.       commandSetV ();
  663.     } else if (!strcmp (command, "setn")) {
  664.       commandSetN ();
  665.     } else if (!strcmp (command, "cleari")) {
  666.       commandClearI ();
  667.     } else if (!strcmp (command, "clears")) {
  668.       commandClearS ();
  669.     } else if (!strcmp (command, "clearp")) {
  670.       commandClearP ();
  671.     } else if (!strcmp (command, "clearz")) {
  672.       commandClearZ ();
  673.     } else if (!strcmp (command, "clearv")) {
  674.       commandClearV ();
  675.     } else if (!strcmp (command, "clearn")) {
  676.       commandClearN ();
  677.     } else if (!strcmp (command, "setpc")) {
  678.       commandSetPC ();
  679.     } else if (!strcmp (command, "setptbr")) {
  680.       commandSetPTBR ();
  681.     } else if (!strcmp (command, "setptlr")) {
  682.       commandSetPTLR ();
  683.     } else if (!strcmp (command, "pt")) {
  684.       commandPrintPageTable ();
  685.     } else if (!strcmp (command, "trans")) {
  686.       commandTranslate ();
  687.     } else if (!strcmp (command, "cancel")) {
  688.       commandCancel ();
  689.     } else if (!strcmp (command, "add")) {
  690.       commandAdd ();
  691. /***
  692.     } else if (!strcmp (command, "init")) {
  693.       commandInit ();
  694.     } else if (!strcmp (command, "init2")) {
  695.       commandInit2 ();
  696.     } else if (!strcmp (command, "sort")) {
  697.       commandSort ();
  698.     } else if (!strcmp (command, "sort2")) {
  699.       commandSort2 ();
  700. ***/
  701.     } else if (!strcmp (command, "labels")) {
  702.       commandLabels ();
  703.     } else if (!strcmp (command, "find")) {
  704.       commandFind ();
  705.     } else if (!strcmp (command, "find2")) {
  706.       commandFind2 ();
  707.     } else if (!strcmp (command, "reset")) {
  708.       commandReset ();
  709.     } else if (!strcmp (command, "float") || !strcmp (command, "f")) {
  710.       commandAllFloatRegs (1);
  711.     } else if (!strcmp (command, "r")) {
  712.       commandAllRegs (1);
  713.     } else if (!strcmp (command, "r1")) {
  714.       commandReg (1);
  715.     } else if (!strcmp (command, "r2")) {
  716.       commandReg (2);
  717.     } else if (!strcmp (command, "r3")) {
  718.       commandReg (3);
  719.     } else if (!strcmp (command, "r4")) {
  720.       commandReg (4);
  721.     } else if (!strcmp (command, "r5")) {
  722.       commandReg (5);
  723.     } else if (!strcmp (command, "r6")) {
  724.       commandReg (6);
  725.     } else if (!strcmp (command, "r7")) {
  726.       commandReg (7);
  727.     } else if (!strcmp (command, "r8")) {
  728.       commandReg (8);
  729.     } else if (!strcmp (command, "r9")) {
  730.       commandReg (9);
  731.     } else if (!strcmp (command, "r10")) {
  732.       commandReg (10);
  733.     } else if (!strcmp (command, "r11")) {
  734.       commandReg (11);
  735.     } else if (!strcmp (command, "r12")) {
  736.       commandReg (12);
  737.     } else if (!strcmp (command, "r13")) {
  738.       commandReg (13);
  739.     } else if (!strcmp (command, "r14")) {
  740.       commandReg (14);
  741.     } else if (!strcmp (command, "r15")) {
  742.       commandReg (15);
  743.     } else if (!strcmp (command, "f0")) {
  744.       commandFloatReg (0);
  745.     } else if (!strcmp (command, "f1")) {
  746.       commandFloatReg (1);
  747.     } else if (!strcmp (command, "f2")) {
  748.       commandFloatReg (2);
  749.     } else if (!strcmp (command, "f3")) {
  750.       commandFloatReg (3);
  751.     } else if (!strcmp (command, "f4")) {
  752.       commandFloatReg (4);
  753.     } else if (!strcmp (command, "f5")) {
  754.       commandFloatReg (5);
  755.     } else if (!strcmp (command, "f6")) {
  756.       commandFloatReg (6);
  757.     } else if (!strcmp (command, "f7")) {
  758.       commandFloatReg (7);
  759.     } else if (!strcmp (command, "f8")) {
  760.       commandFloatReg (8);
  761.     } else if (!strcmp (command, "f9")) {
  762.       commandFloatReg (9);
  763.     } else if (!strcmp (command, "f10")) {
  764.       commandFloatReg (10);
  765.     } else if (!strcmp (command, "f11")) {
  766.       commandFloatReg (11);
  767.     } else if (!strcmp (command, "f12")) {
  768.       commandFloatReg (12);
  769.     } else if (!strcmp (command, "f13")) {
  770.       commandFloatReg (13);
  771.     } else if (!strcmp (command, "f14")) {
  772.       commandFloatReg (14);
  773.     } else if (!strcmp (command, "f15")) {
  774.       commandFloatReg (15);
  775.     } else if (!strcmp (command, "dis")) {
  776.       commandDis ();
  777.     } else if (!strcmp (command, "d")) {
  778.       commandDis2 ();
  779.     } else if (!strcmp (command, "dec")) {
  780.       commandDecimal ();
  781.     } else if (!strcmp (command, "ascii")) {
  782.       commandAscii ();
  783.     } else if (!strcmp (command, "hex")) {
  784.       commandHex ();
  785.     } else if (!strcmp (command, "fmem")) {
  786.       commandFMem ();
  787.     } else if (!strcmp (command, "stack") || !strcmp (command, "st")) {
  788.       commandShowStack ();
  789.     } else if (!strcmp (command, "frame") || !strcmp (command, "fr")) {
  790.       commandFrame ();
  791.     } else if (!strcmp (command, "up")) {
  792.       commandStackUp ();
  793.     } else if (!strcmp (command, "down")) {
  794.       commandStackDown ();
  795.     } else {
  796.       printf ("Unrecognized command.\n");
  797.       printf ("Enter a command at the prompt.  Type 'quit' to exit or 'help' for info about commands.\n");
  798.     }
  799.   }
  800. }
  801.  
  802.  
  803.  
  804. /* printFinalStats ()
  805. **
  806. ** This routine prints the final statistics.
  807. */
  808. void printFinalStats () {
  809.       printf ("Number of Disk Reads    = %d\n", numberOfDiskReads);
  810.       printf ("Number of Disk Writes   = %d\n", numberOfDiskWrites);
  811.       printf ("Instructions Executed   = %d\n", currentTime-timeSpentAsleep);
  812.       printf ("Time Spent Sleeping     = %d\n", timeSpentAsleep);
  813.       printf ("    Total Elapsed Time  = %d\n", currentTime);
  814. }
  815.  
  816.  
  817.  
  818. // checkHostCompatibility ()
  819. //
  820. // This routine checks that the host implementation of C++ meets certain
  821. // requirements.
  822. //
  823. // (1) This routine checks that integers are represented using exactly 4
  824. // bytes.
  825. //
  826. // (2) This routine checks that integers are stored in the expected
  827. // Big or Little Endian order.
  828. //
  829. // (3) This routine checks that integer overflow behavior is as expected
  830. // with two's complement arithmetic.
  831. //
  832. // (4) This routine checks that doubles are implemented using 8-bytes in
  833. // the IEEE standard, with the bytes in correct Big/Little Endian order.
  834. //
  835. // (5) This routine checks that the double->int conversion works as
  836. // expected.  If this is not the case, then truncateToInt() will need to
  837. // be changed.
  838. //
  839. void checkHostCompatibility () {
  840.   union fourBytes {
  841.     char chars [4];
  842.     unsigned int i;
  843.   } fourBytes;
  844.   double d;
  845.   char * p, * q;
  846.   int i, i1, i2, i3;
  847.  
  848.   // Check that ints are in the expected Big/Little Endian order.
  849.   fourBytes.chars[0] = 0x12;
  850.   fourBytes.chars[1] = 0x34;
  851.   fourBytes.chars[2] = 0x56;
  852.   fourBytes.chars[3] = 0x78;
  853.   if (SWAP_BYTES(fourBytes.i) != 0x12345678) {
  854.     fatalError ("There is a big/little endian byte ordering problem.");
  855.   }
  856.  
  857.   // Check that we have at least 4 bytes of precision.
  858.   i = 0x00000001;
  859.   i <<= 20;
  860.   i <<= 10;
  861.   i >>= 20;
  862.   i >>= 10;
  863.   if (i != 0x00000001) {
  864.     fatalError ("This program only runs on computers with 4 byte integers - 1");
  865.   }
  866.  
  867.   // Check that we have no more than 4 bytes of precision.
  868.   i = 0x00000001;
  869.   i <<= 20;
  870.   i <<= 13;   // Some compilers treat <<33 as a nop!
  871.   i >>= 20;
  872.   i >>= 13;
  873.   if (i != 0x00000000) {
  874.     fatalError ("This program only runs on computers with 4 byte integers - 2");
  875.   }
  876.  
  877.   // Check that we have the expected overflow behavior for ints.
  878.   i = -2147483647;
  879.   i = i - 2;
  880.   if (i != 2147483647) {
  881.     fatalError ("This program only runs on computers with 4 byte integers - 3");
  882.   }
  883.  
  884.   // Check that doubles are represented as we expect.
  885.   d = 123.456e37;
  886.   p = (char *) &d;
  887.   q = p;
  888.   // If doubles are stored in Big Endian byte order....
  889.   if ((*p++ == '\x48') &&
  890.       (*p++ == '\x0d') &&
  891.       (*p++ == '\x06') &&
  892.       (*p++ == '\x3c') &&
  893.       (*p++ == '\xdb') &&
  894.       (*p++ == '\x93') &&
  895.       (*p++ == '\x27') &&
  896.       (*p++ == '\xcf')) {
  897. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  898.     fatalError ("There is a big/little endian byte ordering problem with doubles - 1.");
  899. #endif
  900.  
  901.   // Else, if doubles are stored in Little Endian byte order...
  902.   } else if ((*q++ == '\xcf') &&
  903.              (*q++ == '\x27') &&
  904.              (*q++ == '\x93') &&
  905.              (*q++ == '\xdb') &&
  906.              (*q++ == '\x3c') &&
  907.              (*q++ == '\x06') &&
  908.              (*q++ == '\x0d') &&
  909.              (*q++ == '\x48')) {
  910. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  911.  
  912. #else
  913.     fatalError ("There is a big/little endian byte ordering problem with doubles - 2.");
  914. #endif
  915.  
  916.   // Else, if doubles are stored in some other way...
  917.   } else {
  918.     fatalError ("The host implementation of 'double' is not what I expect.");
  919.   }
  920.  
  921.   // There is variation in the way different hosts handle double->int conversion
  922.   // when the double is too large to represent as an integer.  When checking
  923.   // we must do the conversion in two steps, since some compilers perform the
  924.   // conversion at compile time, and will do the conversion differently than
  925.   // the host machine.  Truly appalling, isn't it!
  926.   //   On PPC,   (int) 9e99 is 0x7fffffff
  927.   //   On PPC,   (int) d    is 0x7fffffff
  928.   //   On Intel, (int) 9e99 is 0x7fffffff
  929.   //   On Intel, (int) d    is 0x80000000
  930.   //
  931.   i = (int) 9e99;
  932.   // printf ("(int) 9e99 is 0x%08x\n", i);
  933.   d = 9e99;
  934.   i = (int) d;  // Note: ((int) 9e99 == 0 while ((int) d) == 2147483647)!!!
  935.   // printf ("(int) d is 0x%08x\n", i);
  936.  
  937.   // Check that double->int conversion works as expected.
  938.   d = 4.9;
  939.   i1 = (int) d;
  940.   d = -4.9;
  941.   i2 = (int) d;
  942.   d = -9e99;
  943.   i3 = (int) d;
  944.   if ((i1 !=  4) ||
  945.       (i2 != -4) ||
  946.       (i3 != 0x80000000)) {
  947.     printf ("%d %d %d %d\n", i1, i2, i3);
  948.     fatalError ("The host implementation of double->int casting is not what I expect.");
  949.   }
  950.  
  951. }
  952.  
  953.  
  954.  
  955. /* checkArithmetic ()
  956. **
  957. ** This routine checks to make sure the machine running this
  958. ** programs implements "int" integers with 32 bit arithmetic and
  959. ** doubles with 64 bit floating-point numbers.
  960. */
  961. void checkArithmetic () {
  962.   int x;
  963.   x = 0x00005678;
  964.   x = x << 16;
  965.   x = x >> 16;
  966.   if (x != 0x00005678) {
  967.     fatalError ("This machine does not support 32 bit integer arithmetic");
  968.   }
  969.   if (sizeof(int) != 4) {
  970.     fatalError ("This machine does not support 32 bit integer arithmetic");
  971.   }
  972.   if (sizeof(double) != 8) {
  973.     fatalError ("This machine does not support 64 bit double precision");
  974.   }
  975. }
  976.  
  977.  
  978.  
  979. /* isNegZero (double)  -->  bool
  980. **
  981. ** This routine returns true if the double is negative-zero.
  982. */
  983. int isNegZero (double d) {
  984.   int * p;
  985.   p = (int *) &d;
  986. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  987.   if ((*p == 0x00000000) && (*(++p) == 0x80000000)) {
  988. #else
  989.   if ((*p == 0x80000000) && (*(++p) == 0x00000000)) {
  990. #endif
  991.     return 1;
  992.   } else {
  993.     return 0;
  994.   }
  995. }
  996.  
  997.  
  998.  
  999. /* processCommandLine (argc, argv)
  1000. **
  1001. ** This routine processes the command line options.
  1002. */
  1003. void processCommandLine (int argc, char ** argv) {
  1004.   int argCount;
  1005.   int len;
  1006.   int gotGOption = 0;
  1007.   int gotRawOption = 0;
  1008.   int gotWaitOption = 0;
  1009.  
  1010.   /* Each iteration of this loop looks at the next argument.  In some
  1011.      cases (like "-o a.out") one iteration will scan two tokens. */
  1012.   for (argc--, argv++; argc > 0; argc -= argCount, argv += argCount) {
  1013.     argCount = 1;
  1014.  
  1015.     /* Scan the -h option */
  1016.     if (!strcmp (*argv, "-h")) {
  1017.       commandLineHelp ();
  1018.       exit (1);
  1019.  
  1020.     /* Scan the -g option */
  1021.     } else if (!strcmp (*argv, "-g")) {
  1022.       commandOptionG = 1;
  1023.       if (gotGOption) {
  1024.         badOption ("Multiple occurences of the -g option");
  1025.       } else {
  1026.         gotGOption = 1;
  1027.       }
  1028.  
  1029.     /* Scan the -raw option */
  1030.     } else if (!strcmp (*argv, "-raw")) {
  1031.       commandOptionRaw = 1;
  1032.       if (gotRawOption) {
  1033.         badOption ("Multiple occurences of the -raw option");
  1034.       } else {
  1035.         gotRawOption = 1;
  1036.       }
  1037.  
  1038.     /* Scan the -wait option */
  1039.     } else if (!strcmp (*argv, "-wait")) {
  1040.       commandOptionWait = 1;
  1041.       if (gotWaitOption) {
  1042.         badOption ("Multiple occurences of the -wait option");
  1043.       } else {
  1044.         gotWaitOption = 1;
  1045.       }
  1046.  
  1047.     /* Scan the -r option */
  1048.     } else if (!strcmp (*argv, "-r")) {
  1049.       if (argc <= 1) {
  1050.         badOption ("Expecting integer after -r option");
  1051.       } else {
  1052.         argCount++;
  1053.         randSeedFromOption = atoi (*(argv+1));  /* Extra chars after int ignored */
  1054.         if (randSeedFromOption <= 0) {
  1055.           badOption ("Invalid integer after -r option");
  1056.         }
  1057.         if (commandOptionRand) {
  1058.           badOption ("Multiple occurences of the -r option");
  1059.         } else {
  1060.           commandOptionRand = 1;
  1061.         }
  1062.       }
  1063.  
  1064.     /* Scan the -d option, which should be followed by a file name */
  1065.     } else if (!strcmp (*argv, "-d")) {
  1066.       if (argc <= 1) {
  1067.         badOption ("Expecting filename after -d option");
  1068.       } else {
  1069.         argCount++;
  1070.         if (diskFileName == NULL) {
  1071.           diskFileName = *(argv+1);
  1072.         } else {
  1073.           badOption ("Invalid command line - multiple DISK files");
  1074.         }
  1075.       }
  1076.  
  1077.     /* Scan the -i option, which should be followed by a file name */
  1078.     } else if (!strcmp (*argv, "-i")) {
  1079.       if (argc <= 1) {
  1080.         badOption ("Expecting filename after -i option");
  1081.       } else {
  1082.         argCount++;
  1083.         if (termInputFileName == NULL) {
  1084.           termInputFileName = *(argv+1);
  1085.         } else {
  1086.           badOption ("Multiple occurences of the -i option");
  1087.         }
  1088.       }
  1089.  
  1090.     /* Scan the -o option, which should be followed by a file name */
  1091.     } else if (!strcmp (*argv, "-o")) {
  1092.       if (argc <= 1) {
  1093.         badOption ("Expecting filename after -o option");
  1094.       } else {
  1095.         argCount++;
  1096.         if (termOutputFileName == NULL) {
  1097.           termOutputFileName = *(argv+1);
  1098.         } else {
  1099.           badOption ("Multiple occurences of the -o option");
  1100.         }
  1101.       }
  1102.  
  1103.     /* Deal with any invalid options that begin with "-", such as "-qwerty" */
  1104.     } else if ((*argv)[0] == '-') {
  1105.       fprintf (
  1106.         stderr,
  1107.         "BLITZ Emulator Error: Invalid command line option (%s); Use -h for help display\n",
  1108.         *argv);
  1109.       exit (1);
  1110.     /* This token will be interpreted as the a.out file name. */
  1111.     } else {
  1112.       if (executableFileName == NULL) {
  1113.         executableFileName = *argv;
  1114.       } else {
  1115.         badOption ("Invalid command line - multiple input files");
  1116.       }
  1117.  
  1118.     }
  1119.   }
  1120.  
  1121.   /* Check that -raw and -i don't both occur. */
  1122.   if (commandOptionRaw && (termInputFileName != NULL)) {
  1123.     badOption ("Options -raw and -i are incompatible");
  1124.   }
  1125.  
  1126.   /* Figure out the name of the a.out file. */
  1127.   if (executableFileName == NULL) {
  1128.     executableFileName = "a.out";
  1129.   }
  1130.  
  1131.   /* Open the a.out file. */
  1132.   executableFile = fopen (executableFileName, "r");
  1133.   if (executableFile == NULL) {
  1134.     fprintf (stderr,
  1135.              "BLITZ Emulator Error: Input file \"%s\" could not be opened\n",
  1136.              executableFileName);
  1137.     exit (1);
  1138.   }
  1139.  
  1140.   /* Figure out the name of the DISK file. */
  1141.   if (diskFileName == NULL) {
  1142.     diskFileName = "DISK";
  1143.   }
  1144.  
  1145.   /* Open the TERM INPUT file. */
  1146.   if (termInputFileName == NULL) {
  1147.     termInputFile = stdin;
  1148.   } else {
  1149.     if (termOutputFileName != NULL) {
  1150.       if (strcmp(termInputFileName, termOutputFileName) == 0) {
  1151.         fprintf (stderr,
  1152.                  "BLITZ Emulator Error: Terminal input and output files must be different\n");
  1153.         exit (1);
  1154.       }
  1155.     }
  1156.     termInputFile = fopen (termInputFileName, "r");
  1157.     if (termInputFile == NULL) {
  1158.       fprintf (stderr,
  1159.                "BLITZ Emulator Error: Input file \"%s\" could not be opened for reading\n",
  1160.                termInputFileName);
  1161.       exit (1);
  1162.     }
  1163.   }
  1164.  
  1165.   /* Open the TERM OUTPUT file. */
  1166.   if (termOutputFileName == NULL) {
  1167.     termOutputFile = stdout;
  1168.   } else {
  1169.     termOutputFile = fopen (termOutputFileName, "w");
  1170.     if (termOutputFile == NULL) {
  1171.       fprintf (stderr,
  1172.                "BLITZ Emulator Error: Output file \"%s\" could not be opened for writing\n",
  1173.                termOutputFileName);
  1174.       exit (1);
  1175.     }
  1176.   }
  1177. }
  1178.  
  1179.  
  1180.  
  1181. /* badOption (msg)
  1182. **
  1183. ** This routine prints a message on stderr and then aborts this program.
  1184. */
  1185. void badOption (char * msg) {
  1186.   fprintf (stderr, "BLITZ Emulator Error: %s;  Use -h for help display\n", msg);
  1187.   exit (1);
  1188. }
  1189.  
  1190.  
  1191.  
  1192. /* fatalError (msg)
  1193. **
  1194. ** This routine prints the given error message on stderr and calls errorExit().
  1195. */
  1196. void fatalError (char * msg) {
  1197.   turnOffTerminal ();
  1198.   fprintf (stderr, "\nBLITZ Emulator Error: %s\n", msg);
  1199.   /* raise (SIGSEGV);  Produce a core dump. */
  1200.   errorExit ();
  1201. }
  1202.  
  1203.  
  1204.  
  1205. /* errorExit ()
  1206. **
  1207. ** This routine performs any final cleanup and calls exit(1).
  1208. */
  1209. void errorExit () {
  1210.   exit (1);
  1211. }
  1212.  
  1213.  
  1214.  
  1215. /* commandLineHelp ()
  1216. **
  1217. ** This routine prints some documentation.  It is invoked whenever
  1218. ** the -h option is used on the command line.
  1219. */
  1220. void commandLineHelp () {
  1221.   printf (
  1222. "========================================\n"
  1223. "=====                              =====\n"
  1224. "=====  The BLITZ Machine Emulator  =====\n"
  1225. "=====                              =====\n"
  1226. "========================================\n"
  1227. "\n"
  1228. "Copyright 2001-2007, Harry H. Porter III\n"
  1229. "========================================\n"
  1230. "  Original Author:\n"
  1231. "    02/05/01 - Harry H. Porter III\n"
  1232. /*
  1233.    "  Modifcations by:\n"
  1234.    "    09/28/04 - Harry - Fix bug with randomSeed, disk errno\n"
  1235.    "    11/26/04 - Harry - Fix bugs with serial input\n"
  1236.    "    03/15/06 - Harry - Renamed SPANK to BLITZ\n"
  1237.    "    04/27/07 - Harry - Support for little endian added\n"
  1238. */
  1239. "\n"
  1240. "Command Line Options\n"
  1241. "====================\n"
  1242. "  These command line options may be given in any order.\n"
  1243. "    filename\n"
  1244. "       The input executable file.  If missing, \"a.out\" will be used.\n"
  1245. "    -h\n"
  1246. "       Print this help info.  Ignore other options and exit.\n"
  1247. "    -d filename\n"
  1248. "       Disk file name.  If missing, \"DISK\" will be used.\n"
  1249. "    -g\n"
  1250. "       Automatically begin emulation of the a.out program, bypassing\n"
  1251. "       the command line interface.\n"
  1252. "    -i filename\n"
  1253. "       Terminal input file name.  If missing, \"stdin\" will be used.\n"
  1254. "    -o filename\n"
  1255. "       Terminal output file name.  If missing, \"stdout\" will be used.\n"
  1256. "    -r integer\n"
  1257. "       Set the random seed to the given integer, which must be > 0.\n"
  1258. "    -raw\n"
  1259. "       User input for BLITZ terminal I/O will be in \"raw\" mode; the\n"
  1260. "       default is \"cooked\", in which case the running BLITZ code\n"
  1261. "       is relieved from echoing keystrokes, processing backspaces, etc.\n"
  1262. "    -wait\n"
  1263. "       This option applies only when input is coming from an interactive\n"
  1264. "       terminal and a 'wait' instruction is executed with no other pending\n"
  1265. "       interrupts.  Without this option, execution will halt; with it the\n"
  1266. "       emulator will wait for input.\n"
  1267. );
  1268.  
  1269. }
  1270.  
  1271.  
  1272.  
  1273. /* getToken ()
  1274. **
  1275. ** This routine reads a single token from the input and returns it.
  1276. ** A token is any sequence of characters not containing whitespace.
  1277. ** Leading and trailing white space is removed.
  1278. */
  1279. char * getToken () {
  1280.   int i;
  1281.   fflush (stdout);
  1282.   fgets (inputBuffer, sizeof(inputBuffer), stdin);
  1283.  
  1284. /***
  1285.   printf ("inputBuffer = \n");
  1286.   for (i=0; inputBuffer[i] != '\0'; i++) {
  1287.     printf ("%02X ", (int) inputBuffer[i]);
  1288.   }
  1289.   printf ("\n");
  1290. ***/
  1291.  
  1292.   if (feof (stdin)) {
  1293.     printf ("EOF ignored: Type 'q' to exit.\n");
  1294.     fseek (stdin, 0l, SEEK_SET);
  1295.     inputBuffer [0] = '\0';
  1296.     return inputBuffer;
  1297.     /*  exit (0);  */
  1298.   }
  1299.   /* Overwrite the \n with \0 to remove it. */
  1300.   inputBuffer [strlen (inputBuffer)-1] = '\0';
  1301.   return trimToken (inputBuffer);
  1302. }
  1303.  
  1304.  
  1305.  
  1306. /* trimToken (str)
  1307. **
  1308. ** This routine is passed a pointer to a string.  It trims leading and
  1309. ** trailing whitespace off of the string and returns a pointer to the
  1310. ** new string.  The string is updated in place: a \0 character is written
  1311. ** into the string to trim trailing white space.  The string is assumed to
  1312. ** contain a single token, and an error is printed if there is more than one
  1313. ** token.
  1314. */
  1315. char * trimToken (char * str) {
  1316.   char * p, * end;
  1317.   char * start = str;
  1318.   /* Set start to point to the first non-whitespace character. */
  1319.   while (1) {
  1320.     if ((*start != ' ') && (*start != '\t')) {
  1321.       break;
  1322.     }
  1323.     start++;
  1324.   }
  1325.   /* Set p to point to the next whitespace or \0 character. */
  1326.   p = start;
  1327.   while (1) {
  1328.     if ((*p == ' ') || (*p == '\t') || (*p == '\0')) {
  1329.       break;
  1330.     }
  1331.     p++;
  1332.   }
  1333.   end = p;
  1334.   /* Make sure that there is nothing but whitespace until the \0 character. */
  1335.   while (*p != '\0') {
  1336.     if ((*p != ' ') && (*p != '\t')) {
  1337.       printf ("WARNING: Input characters (\"%s\") after the first word were ignored.\n", p);
  1338.       break;
  1339.     }
  1340.     p++;
  1341.   }
  1342.   /* Store \0 character after the last character of the token. */
  1343.   *end = '\0';
  1344.   return start;
  1345. }
  1346.  
  1347.  
  1348.  
  1349. /* toLower (str)
  1350. **
  1351. ** This routine is passed a string.  It runs through it, converting any
  1352. ** uppercase letters to lowercase.  It changes the string in place.  It
  1353. ** returns a pointer to the string.
  1354. */
  1355. char * toLower (char * str) {
  1356.   char * p = str;
  1357.   while (*p) {
  1358.     if (('A' <= *p) && (*p <= 'Z')) {
  1359.       *p = *p + ('a' - 'A');
  1360.     }
  1361.     p++;
  1362.   }
  1363.   return str;
  1364. }
  1365.  
  1366.  
  1367.  
  1368. /* readInteger (file)
  1369. **
  1370. ** Read an integer (4-bytes, binary) from the given file and return it.
  1371. */
  1372. int readInteger (FILE * file) {
  1373.   int i, numItemsRead;
  1374.   errno = 0;
  1375.   numItemsRead = fread (&i, 4, 1, file);
  1376.   if (numItemsRead != 1) {
  1377.     if (errno) perror ("Error when reading from binary file");
  1378.     fatalError ("Problem reading from file");
  1379.   }
  1380.   return SWAP_BYTES (i);
  1381. }
  1382.  
  1383.  
  1384.  
  1385. /* readByte (file)
  1386. **
  1387. ** Read 1 byte from the given file and return it as an integer.
  1388. */
  1389. int readByte (FILE * file) {
  1390.   int i, numBytesRead;
  1391.   char c;
  1392.   errno = 0;
  1393.   numBytesRead = fread (&c, 1, 1, file);
  1394.   if (numBytesRead != 1) {
  1395.     if (errno) perror ("Error when reading from binary file");
  1396.     fatalError ("Problem reading from file");
  1397.   }
  1398.   i = c;
  1399.   return i;
  1400. }
  1401.  
  1402.  
  1403.  
  1404. /* roundUpToMultipleOf (i, p)
  1405. **
  1406. ** This routine rounds i up to a multiple of p and returns the result.
  1407. */
  1408. int roundUpToMultipleOf (int i, int p) {
  1409.   fatalError ("This routine is not ever called");
  1410.   if ((i < 0) || (p <= 0)) {
  1411.     fatalError ("PROGRAM LOGIC ERROR: Bad arg in roundUpToMultipleOf()");
  1412.   }
  1413.   if (i % p > 0) {
  1414.     return (i / p + 1) * p;
  1415.   }
  1416.   return i;
  1417. }
  1418.  
  1419.  
  1420.  
  1421. /* printMemory (ptr, n, addr)
  1422. **
  1423. ** This routine dumps n bytes of memory (located at "ptr") in hex.  It
  1424. ** Labels the bytes starting with "addr".
  1425. */
  1426. void printMemory (char * ptr, int n, int addr) {
  1427.    pmCount = n;
  1428.    pmPtr = ptr;
  1429.  
  1430.    /* Each execution of this loop prints a single output line. */
  1431.    get16Bytes ();
  1432.    while (pmSize > 0) {
  1433.      /* The following test doesn't work well, since Unix uses huge output
  1434.         buffers and these tend to get filled quickly. */
  1435.      if (controlCPressed) {
  1436.        controlCPressed = 0;
  1437.        return;
  1438.      }
  1439.      putlong (addr);
  1440.      printf (":  ");
  1441.      printline ();
  1442.      addr = addr + 16;
  1443.      get16Bytes ();
  1444.    }
  1445.  
  1446. }
  1447.  
  1448.  
  1449.  
  1450. /* get16Bytes ()
  1451. **
  1452. ** This routine reads in the next 16 bytes from memory and
  1453. ** places them in the array named "pmRow", setting "pmSize" to
  1454. ** be the number of bytes moved.  "pmSize" will be less than
  1455. ** 16 if EOF was encountered, and may possibly be 0.
  1456. */
  1457. void get16Bytes () {
  1458.   int c;
  1459.   pmSize = 0;
  1460.   c = getNextByte ();
  1461.   while (c != -999) {
  1462.     pmRow [pmSize++] = c;
  1463.     if (pmSize >= 16) break;
  1464.     c = getNextByte ();
  1465.   }
  1466. }
  1467.  
  1468.  
  1469.  
  1470. /* getNextByte () 
  1471. **
  1472. ** Get the next byte from memory, from the location pointed to by
  1473. ** "pmPtr", incrementing "pmPtr" and decrementing the counter "pmCount."
  1474. ** This routine will return it as an integer, or return -999 if pmCount <= 0.
  1475. */
  1476. int getNextByte () {
  1477.   if (pmCount <= 0) return -999;
  1478.   pmCount--;
  1479.   return *(pmPtr++);
  1480. }
  1481.  
  1482.  
  1483.  
  1484. /* putlong (i)
  1485. **
  1486. ** This routine is passed an integer, which it prints as 8 hex digits.
  1487. */
  1488. void putlong (int i) {
  1489.   printByte ((i>>24) & 0x000000ff);
  1490.   printByte ((i>>16) & 0x000000ff);
  1491.   printByte ((i>>8) & 0x000000ff);
  1492.   printByte ((i>>0) & 0x000000ff);
  1493. }
  1494.  
  1495.  
  1496.  
  1497. /* puthalf (i)
  1498. **
  1499. ** This routine is passed an integer, which it prints as 4 hex digits.
  1500. */
  1501. void puthalf (int i) {
  1502.   fatalError ("Routine puthalf is never used");
  1503.   printByte ((i>>8) & 0x000000ff);
  1504.   printByte ((i>>0) & 0x000000ff);
  1505. }
  1506.  
  1507.  
  1508.  
  1509. /* printline ()
  1510. **
  1511. ** This routine prints the current 'pmRow'.
  1512. */
  1513. void printline () {
  1514.   int i, c;
  1515.   if (pmSize > 0) {
  1516.     i = 0;
  1517.     while (i<16) {
  1518.       if (i < pmSize) {
  1519.         printByte (pmRow[i]);
  1520.       } else {
  1521.         printf ("  ");
  1522.       }
  1523.       i++;
  1524.       if ((i%2) == 0) {
  1525.         putchar (' ');
  1526.       }
  1527.       if ((i%4) == 0) {
  1528.         putchar (' ');
  1529.       }
  1530.     }
  1531.     printf ("  ");
  1532.     for (i=0; i<pmSize; i++) {
  1533.       c = pmRow[i];
  1534.       if ((c>=' ') && (c <= '~')) {
  1535.         putchar (c);
  1536.       } else {
  1537.         putchar ('.');
  1538.       }
  1539.     }
  1540.     printf ("\n");
  1541.   }
  1542. }
  1543.  
  1544.  
  1545.  
  1546. /* printByte (c)
  1547. **
  1548. ** This routine is passed a byte (i.e., an integer -128..255) which
  1549. ** it prints as 2 hex characters.  If passed a number out of that
  1550. ** range, it outputs nothing.
  1551. */
  1552. void printByte (int c) {
  1553.   int i;
  1554.   if (c<0) c = c + 256;
  1555.   if ((c >= 0) && (c <= 255)) {
  1556.     i = (c & 0x000000f0) >> 4;
  1557.     if (i < 10) {
  1558.       putchar ('0' + i);
  1559.     } else {
  1560.       putchar ('A' + i - 10);
  1561.     }
  1562.     i = (c & 0x0000000f) >> 0;
  1563.     if (i < 10) {
  1564.       putchar ('0' + i);
  1565.     } else {
  1566.       putchar ('A' + i - 10);
  1567.     }
  1568.   }
  1569. }
  1570.  
  1571.  
  1572.  
  1573. /* bytesEqual (p, q, lengthP, lengthQ)
  1574. **
  1575. ** This function is passed two pointers to blocks of bytes, and a
  1576. ** length.  It compares the two sequences of bytes and returns true iff
  1577. ** they are both equal.
  1578. */
  1579. int bytesEqual (char * p, char * q, int lengthP, int lengthQ) {
  1580.   if (lengthP != lengthQ) return 0;
  1581.   for (; lengthP>0; lengthP--, p++, q++) {
  1582.     if (*p != *q) return 0;
  1583.   }
  1584.   return 1;
  1585. }
  1586.  
  1587.  
  1588.  
  1589. /* commandHelp ()
  1590. **
  1591. ** This routine prints the help command material.
  1592. */
  1593. void commandHelp () {
  1594.   printf (
  1595. "===========================================================================\n"
  1596. "This program accepts commands typed into the terminal.  Each command\n"
  1597. "should be typed without any arguments; the commands will prompt for\n"
  1598. "arguments when needed.  Case is not significant.  Some abbreviations\n"
  1599. "are allowed, as shown.  Typing control-C will halt execution.\n"
  1600. "\n"
  1601. "The available commands are:\n"
  1602. "\n"
  1603. "  quit    - Terminate this program\n"
  1604. "  q         \n"
  1605. "  help    - Produce this display\n"
  1606. "  h         \n"
  1607. "  info    - Display the current state of the machine\n"
  1608. "  i         \n"
  1609. "  dumpMem - Display the contents of memory\n"
  1610. "  dm        \n"
  1611. "  setmem  - Used to alter memory contents\n"
  1612. "  fmem    - Display floating point values from memory\n"
  1613. "  go      - Begin or resume BLITZ instruction execution\n"
  1614. "  g         \n"
  1615. "  step    - Single step; execute one machine-level instruction\n"
  1616. "  s         \n"
  1617. "  t       - Single step; execute one KPL statement\n"
  1618. "  u       - Execute continuously until next KPL call, send, or return statement\n"
  1619. "  stepn   - Execute N machine-level instructions\n"
  1620. "  r       - Display all the integer registers\n"
  1621. "  r1      - Change the value of register r1\n"
  1622. "   ...       \n"
  1623. "  r15     - Change the value of register r15\n"
  1624. "  float   - Display all the floating-point registers\n"
  1625. "  f         \n"
  1626. "  f0      - Change the value of floating-point register f0\n"
  1627. "   ...       \n"
  1628. "  f15     - Change the value of floating-point register f15\n"
  1629. "  dis     - Disassemble several instructions\n"
  1630. "  d       - Disassemble several instructions from the current location\n"
  1631. "  hex     - Convert a user-entered hex number into decimal and ascii\n"
  1632. "  dec     - Convert a user-entered decimal number into hex and ascii\n"
  1633. "  ascii   - Convert a user-entered ascii char into hex and decimal\n"
  1634. "  setI    - Set the I bit in the Status Register\n"
  1635. "  setS    - Set the S bit in the Status Register\n"
  1636. "  setP    - Set the P bit in the Status Register\n"
  1637. "  setZ    - Set the Z bit in the Status Register\n"
  1638. "  setV    - Set the V bit in the Status Register\n"
  1639. "  setN    - Set the N bit in the Status Register\n"
  1640. "  clearI  - Clear the I bit in the Status Register\n"
  1641. "  clearS  - Clear the S bit in the Status Register\n"
  1642. "  clearP  - Clear the P bit in the Status Register\n"
  1643. "  clearZ  - Clear the Z bit in the Status Register\n"
  1644. "  clearV  - Clear the V bit in the Status Register\n"
  1645. "  clearN  - Clear the N bit in the Status Register\n"
  1646. "  setPC   - Set the Program Counter (PC)\n"
  1647. "  setPTBR - Set the Page Table Base Register (PTBR)\n"
  1648. "  setPTLR - Set the Page Table Length Register (PTLR)\n"
  1649. "  pt      - Display the Page Table\n"
  1650. "  trans   - Perform page table translation on a single address\n"
  1651. "  cancel  - Cancel all pending interrupts\n"
  1652. "  labels  - Display the label table\n"
  1653. "  find    - Find a label by name\n"
  1654. "  find2   - Find a label by value\n"
  1655. "  add     - Add a new label, inserting it into the indexes\n"
  1656. "  reset   - Reset the machine state and re-read the a.out file\n"
  1657. "  io      - Display the state of the I/O devices\n"
  1658. "  read    - Read a word from memory-mapped I/O region\n"
  1659. "  write   - Write a word to memory-mapped I/O region\n"
  1660. "  raw     - Switch serial input to raw mode\n"
  1661. "  cooked  - Switch serial input to cooked mode\n"
  1662. "  input   - Enter input characters for future serial I/O input\n"
  1663. "  format  - Create and format a BLITZ disk file\n"
  1664. "  sim     - Display the current simulation constants\n"
  1665. "  stack   - Display the KPL calling stack\n"
  1666. "  st        \n"
  1667. "  frame   - Display the current activation frame\n"
  1668. "  fr        \n"
  1669. "  up      - Move up in the activation frame stack\n"
  1670. "  down    - Move down in the activation frame stack\n"
  1671. "\n"
  1672. "===========================================================================\n");
  1673.  
  1674. }
  1675.  
  1676.  
  1677.  
  1678. /* commandAllRegs ()
  1679. **
  1680. ** This routine prints the registers (either User or Sytstem, depending
  1681. ** on the current mode.
  1682. */
  1683. void commandAllRegs () {
  1684.   int i;
  1685.   if (!statusS) {
  1686.     printf ("=====  USER REGISTERS  =====\n");
  1687.     for (i=0; i<=9; i++) {
  1688.       printf ("  r%d  = ", i);
  1689.       printNumberNL (userRegisters [i]);
  1690.     }
  1691.     for (i=10; i<=15; i++) {
  1692.       printf ("  r%d = ", i);
  1693.       printNumberNL (userRegisters [i]);
  1694.     }
  1695.   } else {
  1696.     printf ("=====  SYSTEM REGISTERS  =====\n");
  1697.     for (i=0; i<=9; i++) {
  1698.       printf ("  r%d  = ", i);
  1699.       printNumberNL (systemRegisters [i]);
  1700.     }
  1701.     for (i=10; i<=15; i++) {
  1702.       printf ("  r%d = ", i);
  1703.       printNumberNL (systemRegisters [i]);
  1704.     }
  1705.   }
  1706.   printf ("==============================\n");
  1707. }
  1708.  
  1709.  
  1710.  
  1711. /* commandAllFloatRegs ()
  1712. **
  1713. ** This routine prints the floating-point registers.
  1714. */
  1715. void commandAllFloatRegs () {
  1716.   int i;
  1717.   double d;
  1718.   printf ("=====  FLOATING-POINT REGISTERS  =====\n");
  1719.   for (i=0; i<=9; i++) {
  1720.     d = floatRegisters [i];
  1721.     printf ("  f%d  = ", i);
  1722.     printDouble (d);
  1723.   }
  1724.   for (i=10; i<=15; i++) {
  1725.     d = floatRegisters [i];
  1726.     printf ("  f%d = ", i);
  1727.     printDouble (d);
  1728.   }
  1729.   printf ("======================================\n");
  1730. }
  1731.  
  1732.  
  1733.  
  1734. /* printDouble (d)
  1735. **
  1736. ** This rouinte prints out a floating-point number.  It checks for
  1737. ** NAN, POSITIVE-INFINITY, and NEGATIVE-INFINITY and prints them
  1738. ** appropriately.  Numbers are printed in the form:
  1739. **
  1740. **       4484ea0c eff1a9b4   ( value = 1.23456e+22 )
  1741. */
  1742. void printDouble (double d) {
  1743.   double value = d;
  1744.   int * p;
  1745.   p = (int *) (& value);
  1746. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  1747.   printf ("0x");
  1748.   putlong (*(p+1));
  1749.   printf (" ");
  1750.   putlong (*p);
  1751. #else
  1752.   printf ("0x");
  1753.   putlong (*p);
  1754.   printf (" ");
  1755.   putlong (*(p+1));
  1756. #endif
  1757.   printf ("   ( value = ");
  1758.   printDoubleVal (value);
  1759.   printf (" )\n");
  1760. }
  1761.  
  1762.  
  1763.  
  1764. /* printDoubleVal (d)
  1765. **
  1766. ** This routine is passed a floating-point number.  It checks for
  1767. ** NAN, POSITIVE-INFINITY, and NEGATIVE-INFINITY and prints them
  1768. ** appropriately.  Numbers are printed in the form:
  1769. **
  1770. **       1.23456e+22
  1771. **       Not-a-number
  1772. **       Positive-Infinity
  1773. **       Negative-Infinity
  1774. **       0
  1775. **       Negative-Zero
  1776. */
  1777. void printDoubleVal (double d) {
  1778.   if (isnan(d)) {
  1779.     printf ("Not-a-Number");
  1780.   } else if (d == POSITIVE_INFINITY) {
  1781.     printf ("Positive-Infinity");
  1782.   } else if (d == NEGATIVE_INFINITY) {
  1783.     printf ("Negative-Infinity");
  1784.   } else if (isNegZero (d)) {
  1785.     printf ("Negative-Zero");
  1786.   } else {
  1787.     printf ("%.15g", d);
  1788.   }
  1789. }
  1790.  
  1791.  
  1792.  
  1793.  
  1794. /* commandInfo ()
  1795. **
  1796. ** This routine prints the current state of the machine.
  1797. */
  1798. void commandInfo () {
  1799.   int i;
  1800.   printf ("============================\n");
  1801.   printInfo ();
  1802.   printf ("=====  USER REGISTERS  =====\n");
  1803.   for (i=0; i<=9; i++) {
  1804.     printf ("  r%d  = ", i);
  1805.     printNumberNL (userRegisters [i]);
  1806.   }
  1807.   for (i=10; i<=15; i++) {
  1808.     printf ("  r%d = ", i);
  1809.     printNumberNL (userRegisters [i]);
  1810.   }
  1811.   printf ("=====  SYSTEM REGISTERS  =====\n");
  1812.   for (i=0; i<=9; i++) {
  1813.     printf ("  r%d  = ", i);
  1814.     printNumberNL (systemRegisters [i]);
  1815.   }
  1816.   for (i=10; i<=15; i++) {
  1817.     printf ("  r%d = ", i);
  1818.     printNumberNL (systemRegisters [i]);
  1819.   }
  1820.   commandAllFloatRegs ();
  1821.   printf ("  PC   = ");
  1822.   printNumberNL (pc);
  1823.   printf ("  PTBR = ");
  1824.   printNumberNL (ptbr);
  1825.   printf ("  PTLR = ");
  1826.   printNumberNL (ptlr);
  1827.   printf ("                        ---- ----  ---- ----  ---- ----  --IS PZVN\n");
  1828.   printf ("  SR   = 0x%08X  =  0000 0000  0000 0000  0000 0000  00",
  1829.           buildStatusWord ());
  1830.   if (statusI) printf ("1");    else printf ("0");
  1831.   if (statusS) printf ("1 ");   else printf ("0 ");
  1832.   if (statusP) printf ("1");    else printf ("0");
  1833.   if (statusZ) printf ("1");    else printf ("0");
  1834.   if (statusV) printf ("1");    else printf ("0");
  1835.   if (statusN) printf ("1\n");  else printf ("0\n");
  1836.   if (statusI)
  1837.       printf ("           I = 1   Interrupts Enabled\n");
  1838.     else
  1839.       printf ("           I = 0   Interrupts Disabled\n");
  1840.   if (statusS)
  1841.       printf ("           S = 1   System Mode\n");
  1842.     else
  1843.       printf ("           S = 0   User Mode\n");
  1844.   if (statusP)
  1845.       printf ("           P = 1   Paging Enabled\n");
  1846.     else
  1847.       printf ("           P = 0   Paging Disabled\n");
  1848.   if (statusZ)
  1849.       printf ("           Z = 1   Zero\n");
  1850.     else
  1851.       printf ("           Z = 0   Not Zero\n");
  1852.   if (statusV)
  1853.       printf ("           V = 1   Overflow Occurred\n");
  1854.     else
  1855.       printf ("           V = 0   No Overflow\n");
  1856.   if (statusN)
  1857.       printf ("           N = 1   Negative\n");
  1858.     else
  1859.       printf ("           N = 0   Not Negative\n");
  1860.   printf ("==============================\n");
  1861.   printPendingInterrupts ();
  1862.   printf ("  System Trap Number                = 0x%08X\n", systemTrapNumber);
  1863.   printf ("  Page Invalid Offending Address    = 0x%08X\n", pageInvalidOffendingAddress);
  1864.   printf ("  Page Readonly Offending Address   = 0x%08X\n", pageReadonlyOffendingAddress);
  1865.   printf ("  Time of next timer event          = %d\n", timeOfNextTimerEvent);
  1866.   printf ("  Time of next disk event           = %d\n", timeOfNextDiskEvent);
  1867.   printf ("  Time of next serial in event      = %d\n", timeOfNextSerialInEvent);
  1868.   printf ("  Time of next serial out event     = %d\n", timeOfNextSerialOutEvent);
  1869.   printf ("    Current Time                    = %d\n", currentTime);
  1870.   printf ("    Time of next event              = %d\n", timeOfNextEvent);
  1871.   printf ("    Time Spent Sleeping             = %d\n", timeSpentAsleep);
  1872.   printf ("      Instructions Executed         = %d\n", currentTime-timeSpentAsleep);
  1873.   printf ("  Number of Disk Reads              = %d\n", numberOfDiskReads);
  1874.   printf ("  Number of Disk Writes             = %d\n", numberOfDiskWrites);
  1875.   printf ("==============================\n");
  1876.   printAboutToExecute ();
  1877.   printKPLStmtCode ();
  1878. }
  1879.  
  1880.  
  1881.  
  1882. /* printPendingInterrupts ()
  1883. **
  1884. ** This routine prints the current state of "interruptsSignaled".
  1885. */
  1886. void printPendingInterrupts () {
  1887.   printf ("  Pending Interrupts                = 0x%08X\n", interruptsSignaled);
  1888.   if (interruptsSignaled & POWER_ON_RESET)
  1889.     printf ("    POWER_ON_RESET\n");
  1890.   if (interruptsSignaled & TIMER_INTERRUPT)
  1891.     printf ("    TIMER_INTERRUPT\n");
  1892.   if (interruptsSignaled & DISK_INTERRUPT)
  1893.     printf ("    DISK_INTERRUPT\n");
  1894.   if (interruptsSignaled & SERIAL_INTERRUPT)
  1895.     printf ("    SERIAL_INTERRUPT\n");
  1896.   if (interruptsSignaled & HARDWARE_FAULT)
  1897.     printf ("    HARDWARE_FAULT\n");
  1898.   if (interruptsSignaled & ILLEGAL_INSTRUCTION)
  1899.     printf ("    ILLEGAL_INSTRUCTION\n");
  1900.   if (interruptsSignaled & ARITHMETIC_EXCEPTION)
  1901.     printf ("    ARITHMETIC_EXCEPTION\n");
  1902.   if (interruptsSignaled & ADDRESS_EXCEPTION)
  1903.     printf ("    ADDRESS_EXCEPTION\n");
  1904.   if (interruptsSignaled & PAGE_INVALID_EXCEPTION)
  1905.     printf ("    PAGE_INVALID_EXCEPTION\n");
  1906.   if (interruptsSignaled & PAGE_READONLY_EXCEPTION)
  1907.     printf ("    PAGE_READONLY_EXCEPTION\n");
  1908.   if (interruptsSignaled & PRIVILEGED_INSTRUCTION)
  1909.     printf ("    PRIVILEGED_INSTRUCTION\n");
  1910.   if (interruptsSignaled & ALIGNMENT_EXCEPTION)
  1911.     printf ("    ALIGNMENT_EXCEPTION\n");
  1912.   if (interruptsSignaled & EXCEPTION_DURING_INTERRUPT)
  1913.     printf ("    EXCEPTION_DURING_INTERRUPT\n");
  1914.   if (interruptsSignaled & SYSCALL_TRAP)
  1915.     printf ("    SYSCALL_TRAP\n");
  1916. }
  1917.  
  1918.  
  1919.  
  1920. /* commandIO ()
  1921. **
  1922. ** This routine prints the current state of the I/O devices.
  1923. */
  1924. void commandIO () {
  1925.   int i;
  1926.   printf ("==========  Serial I/O  ==========\n");
  1927.   if (termOutputReady) {
  1928.     printf ("  Output Status:       Ready\n");
  1929.   } else {
  1930.     printf ("  Output Status:       Not Ready\n");
  1931.   }
  1932.   if (termInCharAvail) {
  1933.     printf ("  Input Status:        Character Available\n");
  1934.   } else {
  1935.     printf ("  Input Status:        Character Not Available\n");
  1936.   }
  1937.   printf ("  Current Input Char:  \'");
  1938.   fancyPrintChar (termInChar);
  1939.   if (termInCharWasUsed) {
  1940.     printf ("\'    (already fetched by CPU)\n");
  1941.   } else {
  1942.     printf ("\'    (not yet fetched by CPU)\n");
  1943.   }
  1944.   printf ("    The following characters are currently in the type-ahead buffer:\n      ");
  1945.   printTypeAheadBuffer ();
  1946.   if (termInputFile == stdin) {
  1947.     printf ("  Input coming from: stdin\n");
  1948.     if (terminalWantRawProcessing) {
  1949.       printf ("    Input Mode: Raw\n");
  1950.     } else {
  1951.       printf ("    Input Mode: Cooked\n");
  1952.     }
  1953.   } else {
  1954.     printf ("  Input to the serial device is coming from file \"%s\".\n",
  1955.             termInputFileName);
  1956.   }
  1957.  
  1958.   printf ("==========  Disk I/O  ==========\n");
  1959.   printf ("  The file used for the disk: \"%s\"\n", diskFileName);
  1960.   if (diskFile == NULL) {
  1961.     printf ("    DISK File is currently closed.\n");
  1962.   } else {
  1963.     printf ("    DISK File is currently opened.\n");
  1964.   }
  1965.   printf ("  Disk size:\n");
  1966.   printf ("    Total Tracks = %d\n", diskTrackCount);
  1967.   printf ("    Total Sectors = %d\n", diskSectorCount);
  1968.   printf ("    Sectors per track = %d\n", SECTORS_PER_TRACK);
  1969.   printf ("  Current Status:\n");
  1970.   printf ("    Positioned at Sector = %d\n", currentDiskSector);
  1971.   if (currentDiskStatus == DISK_BUSY) {
  1972.     printf ("    Current Disk Status  = DISK_BUSY\n");
  1973.   } else if (currentDiskStatus == OPERATION_COMPLETED_OK) {
  1974.     printf ("    Current Disk Status  = OPERATION_COMPLETED_OK\n");
  1975.   } else if (currentDiskStatus == OPERATION_COMPLETED_WITH_ERROR_1) {
  1976.     printf ("    Current Disk Status  = OPERATION_COMPLETED_WITH_ERROR_1\n");
  1977.   } else if (currentDiskStatus == OPERATION_COMPLETED_WITH_ERROR_2) {
  1978.     printf ("    Current Disk Status  = OPERATION_COMPLETED_WITH_ERROR_2\n");
  1979.   } else if (currentDiskStatus == OPERATION_COMPLETED_WITH_ERROR_3) {
  1980.     printf ("    Current Disk Status  = OPERATION_COMPLETED_WITH_ERROR_3\n");
  1981.   } else if (currentDiskStatus == OPERATION_COMPLETED_WITH_ERROR_4) {
  1982.     printf ("    Current Disk Status  = OPERATION_COMPLETED_WITH_ERROR_4\n");
  1983.   } else if (currentDiskStatus == OPERATION_COMPLETED_WITH_ERROR_5) {
  1984.     printf ("    Current Disk Status  = OPERATION_COMPLETED_WITH_ERROR_5\n");
  1985.   } else {
  1986.     printf ("    Current Disk Status  = ***************  ERROR  ***************\n");
  1987.   }
  1988.   if (futureDiskStatus == DISK_BUSY) {
  1989.     printf ("    Future Disk Status   = DISK_BUSY\n");
  1990.   } else if (futureDiskStatus == OPERATION_COMPLETED_OK) {
  1991.     printf ("    Future Disk Status   = OPERATION_COMPLETED_OK\n");
  1992.   } else if (futureDiskStatus == OPERATION_COMPLETED_WITH_ERROR_1) {
  1993.     printf ("    Future Disk Status   = OPERATION_COMPLETED_WITH_ERROR_1\n");
  1994.   } else if (futureDiskStatus == OPERATION_COMPLETED_WITH_ERROR_2) {
  1995.     printf ("    Future Disk Status   = OPERATION_COMPLETED_WITH_ERROR_2\n");
  1996.   } else if (futureDiskStatus == OPERATION_COMPLETED_WITH_ERROR_3) {
  1997.     printf ("    Future Disk Status   = OPERATION_COMPLETED_WITH_ERROR_3\n");
  1998.   } else if (futureDiskStatus == OPERATION_COMPLETED_WITH_ERROR_4) {
  1999.     printf ("    Future Disk Status   = OPERATION_COMPLETED_WITH_ERROR_4\n");
  2000.   } else if (futureDiskStatus == OPERATION_COMPLETED_WITH_ERROR_5) {
  2001.     printf ("    Future Disk Status   = OPERATION_COMPLETED_WITH_ERROR_5\n");
  2002.   } else {
  2003.     printf ("    Future Disk Status   = ***************  ERROR  ***************\n");
  2004.   }
  2005.   printf ("  Area of memory being read from / written to:\n");
  2006.   printf ("    diskBufferLow  = 0x%08X\n", diskBufferLow);
  2007.   printf ("    diskBufferHigh = 0x%08X\n", diskBufferHigh);
  2008.   printf ("  Memory-Mapped Register Contents:\n");
  2009.   printf ("    DISK_MEMORY_ADDRESS_REGISTER = 0x%08X\n", diskMemoryAddressRegister);
  2010.   printf ("    DISK_SECTOR_NUMBER_REGISTER  = 0x%08X\n", diskSectorNumberRegister);
  2011.   printf ("    DISK_SECTOR_COUNT_REGISTER   = 0x%08X\n", diskSectorCountRegister);
  2012.   printf ("  Number of Disk Reads  = %d\n", numberOfDiskReads);
  2013.   printf ("  Number of Disk Writes = %d\n", numberOfDiskWrites);
  2014.  
  2015.   printf ("==============================\n");
  2016.   printf ("  CPU status:\n");
  2017.   if (statusI)
  2018.       printf ("    Interrupts:   Enabled\n");
  2019.     else
  2020.       printf ("    Interrupts:   Disabled\n");
  2021.   if (statusS)
  2022.       printf ("    Mode:         System\n");
  2023.     else
  2024.       printf ("    Mode:         User\n");
  2025.   printf ("    Pending Interrupts:\n");
  2026.   if (interruptsSignaled & POWER_ON_RESET)
  2027.     printf ("      POWER_ON_RESET\n");
  2028.   if (interruptsSignaled & TIMER_INTERRUPT)
  2029.     printf ("      TIMER_INTERRUPT\n");
  2030.   if (interruptsSignaled & DISK_INTERRUPT)
  2031.     printf ("      DISK_INTERRUPT\n");
  2032.   if (interruptsSignaled & SERIAL_INTERRUPT)
  2033.     printf ("      SERIAL_INTERRUPT\n");
  2034.   if (interruptsSignaled & HARDWARE_FAULT)
  2035.     printf ("      HARDWARE_FAULT\n");
  2036.   if (interruptsSignaled & ILLEGAL_INSTRUCTION)
  2037.     printf ("      ILLEGAL_INSTRUCTION\n");
  2038.   if (interruptsSignaled & ARITHMETIC_EXCEPTION)
  2039.     printf ("      ARITHMETIC_EXCEPTION\n");
  2040.   if (interruptsSignaled & ADDRESS_EXCEPTION)
  2041.     printf ("      ADDRESS_EXCEPTION\n");
  2042.   if (interruptsSignaled & PAGE_INVALID_EXCEPTION)
  2043.     printf ("      PAGE_INVALID_EXCEPTION\n");
  2044.   if (interruptsSignaled & PAGE_READONLY_EXCEPTION)
  2045.     printf ("      PAGE_READONLY_EXCEPTION\n");
  2046.   if (interruptsSignaled & PRIVILEGED_INSTRUCTION)
  2047.     printf ("      PRIVILEGED_INSTRUCTION\n");
  2048.   if (interruptsSignaled & ALIGNMENT_EXCEPTION)
  2049.     printf ("      ALIGNMENT_EXCEPTION\n");
  2050.   if (interruptsSignaled & EXCEPTION_DURING_INTERRUPT)
  2051.     printf ("      EXCEPTION_DURING_INTERRUPT\n");
  2052.   if (interruptsSignaled & SYSCALL_TRAP)
  2053.     printf ("      SYSCALL_TRAP\n");
  2054.   printf ("  Time of next timer event........ %d\n", timeOfNextTimerEvent);
  2055.   printf ("  Time of next disk event......... %d\n", timeOfNextDiskEvent);
  2056.   printf ("  Time of next serial in event.... %d\n", timeOfNextSerialInEvent);
  2057.   printf ("  Time of next serial out event... %d\n", timeOfNextSerialOutEvent);
  2058.   printf ("    Current Time.................. %d\n", currentTime);
  2059.   printf ("    Time of next event............ %d\n", timeOfNextEvent);
  2060.   printf ("==============================\n");
  2061. }
  2062.  
  2063.  
  2064.  
  2065. /* commandSim ()
  2066. **
  2067. ** This routine prints the current simulation constants.
  2068. */
  2069. void commandSim () {
  2070.   int ans;
  2071.   FILE * file;
  2072.  
  2073.   printf ("=========================  Simulation Constants  ==============================\n");
  2074.  
  2075.   printf ("  KEYBOARD_WAIT_TIME           %11 d\n", KEYBOARD_WAIT_TIME);
  2076.   printf ("  KEYBOARD_WAIT_TIME_VARIATION %11 d\n", KEYBOARD_WAIT_TIME_VARIATION);
  2077.  
  2078.   printf ("  TERM_OUT_DELAY               %11 d\n", TERM_OUT_DELAY);
  2079.   printf ("  TERM_OUT_DELAY_VARIATION     %11 d\n", TERM_OUT_DELAY_VARIATION);
  2080.  
  2081.   printf ("  TIME_SLICE                   %11 d (0=no timer interrutps)\n", TIME_SLICE);
  2082.   printf ("  TIME_SLICE_VARIATION         %11 d\n", TIME_SLICE_VARIATION);
  2083.  
  2084.   printf ("  DISK_SEEK_TIME               %11 d\n", DISK_SEEK_TIME);
  2085.   printf ("  DISK_SETTLE_TIME             %11 d\n", DISK_SETTLE_TIME);
  2086.   printf ("  DISK_ROTATIONAL_DELAY        %11 d\n", DISK_ROTATIONAL_DELAY);    /* Minimum is 1 */
  2087.   printf ("  DISK_ACCESS_VARIATION        %11 d\n", DISK_ACCESS_VARIATION);
  2088.  
  2089.   printf ("  DISK_READ_ERROR_PROBABILITY  %11 d (0=never, 1=always, n=\"about 1/n\")\n",
  2090.                                                     DISK_READ_ERROR_PROBABILITY);
  2091.   printf ("  DISK_WRITE_ERROR_PROBABILITY %11 d (0=never, 1=always, n=\"about 1/n\")\n",
  2092.                                                     DISK_WRITE_ERROR_PROBABILITY);
  2093.  
  2094.   printf ("  INIT_RANDOM_SEED             %11 d (between 1 and 2147483646)\n", INIT_RANDOM_SEED);
  2095.   if (commandOptionRand) {
  2096.     printf ("    (overridden with -r option %11 d)\n", randSeedFromOption);
  2097.   }
  2098.  
  2099.   printf ("  MEMORY_SIZE                   0x%08X (decimal: %d)\n", MEMORY_SIZE, MEMORY_SIZE);
  2100.  
  2101.   printf ("  MEMORY_MAPPED_AREA_LOW        0x%08X\n", MEMORY_MAPPED_AREA_LOW);
  2102.   printf ("  MEMORY_MAPPED_AREA_HIGH       0x%08X\n", MEMORY_MAPPED_AREA_HIGH);
  2103.  
  2104.   printf ("  SERIAL_STATUS_WORD_ADDRESS    0x%08X\n", SERIAL_STATUS_WORD_ADDRESS);
  2105.   printf ("  SERIAL_DATA_WORD_ADDRESS      0x%08X\n", SERIAL_DATA_WORD_ADDRESS);
  2106.  
  2107.   printf ("  DISK_STATUS_WORD_ADDRESS      0x%08X\n", DISK_STATUS_WORD_ADDRESS);
  2108.   printf ("  DISK_COMMAND_WORD_ADDRESS     0x%08X\n", DISK_COMMAND_WORD_ADDRESS);
  2109.   printf ("  DISK_MEMORY_ADDRESS_REGISTER  0x%08X\n", DISK_MEMORY_ADDRESS_REGISTER);
  2110.   printf ("  DISK_SECTOR_NUMBER_REGISTER   0x%08X\n", DISK_SECTOR_NUMBER_REGISTER);
  2111.   printf ("  DISK_SECTOR_COUNT_REGISTER    0x%08X\n", DISK_SECTOR_COUNT_REGISTER);
  2112.  
  2113.   printf ("===============================================================================\n");
  2114.  
  2115.   printf ("\n"
  2116.           "The simulation constants will be read in from the file \".blitzrc\" if it exists\n"
  2117.           "when the emulator starts up.  If the file does not exist at startup, defaults\n"
  2118.           "will be used.  You may edit the \".blitzrc\" file to change the values and then\n"
  2119.           "restart the emulator.\n"
  2120.           "\n"
  2121.           "Would you like me to write these values out to the file \".blitzrc\" now? ");
  2122.  
  2123.   ans = readYesNo ();
  2124.   if (ans) {
  2125.  
  2126.     // Open the file, creating it if it does not exist, overwriting it if it does...
  2127.     file = fopen (".blitzrc", "w");
  2128.     if (file == NULL) {
  2129.       printf ("The \".blitzrc\" file could not be opened for writing!\n");
  2130.       perror ("Host error");
  2131.       return;
  2132.     }
  2133.  
  2134.     // Write out the contents of the file...
  2135.     fprintf (file,
  2136.              "! BLITZ Simulation Constants\n"
  2137.              "!\n"
  2138.              "! This file is read by the BLITZ emulator when it starts up and after a\n"
  2139.              "! \"reset\" command.  This file is used to initialize various values that\n"
  2140.              "! will be used by the emulator.\n"
  2141.              "!\n"
  2142.              "! This file was produced by the emulator (with the \"sim\" command).  It may\n"
  2143.              "! be edited to change any or all values.\n"
  2144.              "!\n"
  2145.              "! Each line has variable name followed by an integer value.  A value may\n"
  2146.              "! be specified in either decimal (e.g., \"1234\") or hex (e.g., \"0x1234abcd\").\n"
  2147.              "! Values may be left out if desired, in which case a default will be used.\n"
  2148.              "! In the case of the random seed, any value specified here will override a\n"
  2149.              "! value given with a command line option (-r).\n"
  2150.              "!\n"
  2151.              "!\n"
  2152.       );
  2153.  
  2154.     fprintf (file, "KEYBOARD_WAIT_TIME           %11 d\n", KEYBOARD_WAIT_TIME);
  2155.     fprintf (file, "KEYBOARD_WAIT_TIME_VARIATION %11 d\n", KEYBOARD_WAIT_TIME_VARIATION);
  2156.  
  2157.     fprintf (file, "TERM_OUT_DELAY               %11 d\n", TERM_OUT_DELAY);
  2158.     fprintf (file, "TERM_OUT_DELAY_VARIATION     %11 d\n", TERM_OUT_DELAY_VARIATION);
  2159.  
  2160.     fprintf (file, "\n");
  2161.     fprintf (file, "TIME_SLICE                   %11 d\n", TIME_SLICE);
  2162.     fprintf (file, "TIME_SLICE_VARIATION         %11 d\n", TIME_SLICE_VARIATION);
  2163.  
  2164.     fprintf (file, "\n");
  2165.     fprintf (file, "DISK_SEEK_TIME               %11 d\n", DISK_SEEK_TIME);
  2166.     fprintf (file, "DISK_SETTLE_TIME             %11 d\n", DISK_SETTLE_TIME);
  2167.     fprintf (file, "DISK_ROTATIONAL_DELAY        %11 d\n", DISK_ROTATIONAL_DELAY);
  2168.     fprintf (file, "DISK_ACCESS_VARIATION        %11 d\n", DISK_ACCESS_VARIATION);
  2169.  
  2170.     fprintf (file, "DISK_READ_ERROR_PROBABILITY  %11 d\n", DISK_READ_ERROR_PROBABILITY);
  2171.     fprintf (file, "DISK_WRITE_ERROR_PROBABILITY %11 d\n", DISK_WRITE_ERROR_PROBABILITY);
  2172.  
  2173.     fprintf (file, "\n");
  2174.     fprintf (file, "INIT_RANDOM_SEED             %11 d\n", INIT_RANDOM_SEED);
  2175.  
  2176.     fprintf (file, "\n");
  2177.     fprintf (file, "MEMORY_SIZE                   0x%08X\n", MEMORY_SIZE);
  2178.  
  2179.     fprintf (file, "\n");
  2180.     fprintf (file, "MEMORY_MAPPED_AREA_LOW        0x%08X\n", MEMORY_MAPPED_AREA_LOW);
  2181.     fprintf (file, "MEMORY_MAPPED_AREA_HIGH       0x%08X\n", MEMORY_MAPPED_AREA_HIGH);
  2182.  
  2183.     fprintf (file, "\n");
  2184.     fprintf (file, "SERIAL_STATUS_WORD_ADDRESS    0x%08X\n", SERIAL_STATUS_WORD_ADDRESS);
  2185.     fprintf (file, "SERIAL_DATA_WORD_ADDRESS      0x%08X\n", SERIAL_DATA_WORD_ADDRESS);
  2186.  
  2187.     fprintf (file, "\n");
  2188.     fprintf (file, "DISK_STATUS_WORD_ADDRESS      0x%08X\n", DISK_STATUS_WORD_ADDRESS);
  2189.     fprintf (file, "DISK_COMMAND_WORD_ADDRESS     0x%08X\n", DISK_COMMAND_WORD_ADDRESS);
  2190.     fprintf (file, "DISK_MEMORY_ADDRESS_REGISTER  0x%08X\n", DISK_MEMORY_ADDRESS_REGISTER);
  2191.     fprintf (file, "DISK_SECTOR_NUMBER_REGISTER   0x%08X\n", DISK_SECTOR_NUMBER_REGISTER);
  2192.     fprintf (file, "DISK_SECTOR_COUNT_REGISTER    0x%08X\n", DISK_SECTOR_COUNT_REGISTER);
  2193.  
  2194.     fclose (file);
  2195.   }
  2196.  
  2197. }
  2198.  
  2199.  
  2200.  
  2201. /* commandRaw ()
  2202. **
  2203. ** This routine changes the raw/cooked mode.
  2204. */
  2205. void commandRaw () {
  2206.   if (termInputFileName != NULL) {
  2207.     printSerialHelp ();
  2208.     printf ("*****  Serial I/O is coming from the file \"%s\"  *****\n",
  2209.              termInputFileName);
  2210.   } else if (terminalWantRawProcessing) {
  2211.     printSerialHelp ();
  2212.     printf ("*****  The terminal is already in \"raw\" mode  *****\n");
  2213.   } else {
  2214.     printf ("Future terminal input will be \"raw\".\n");
  2215.     terminalWantRawProcessing = 1;
  2216.   }
  2217. }
  2218.  
  2219.  
  2220.  
  2221. /* commandCooked ()
  2222. **
  2223. ** This routine changes the raw/cooked mode.
  2224. */
  2225. void commandCooked () {
  2226.   if (termInputFileName != NULL) {
  2227.     printSerialHelp ();
  2228.     printf ("*****  Serial I/O is coming from the file \"%s\"  *****\n",
  2229.              termInputFileName);
  2230.   } else if (!terminalWantRawProcessing) {
  2231.     printSerialHelp ();
  2232.     printf ("*****  The terminal is already in \"cooked\" mode  *****\n");
  2233.   } else {
  2234.     printf ("Future terminal input will be \"cooked\".\n");
  2235.     terminalWantRawProcessing = 0;
  2236.   }
  2237. }
  2238.  
  2239.  
  2240.  
  2241. /* printSerialHelp ()
  2242. **
  2243. ** This routine prints a description of how the serial interface works.
  2244. */
  2245. void printSerialHelp () {
  2246.     printf (
  2247. "=============================================================================\n"
  2248. "From time to time a running BLITZ program may read characters from\n"
  2249. "  the \"Serial I/O\" device, which is intended to simulate an ASCII\n"
  2250. "  terminal.  The character data to be supplied to the running BLITZ\n"
  2251. "  program will come from either a file (which is specified using the\n"
  2252. "  \"-i filename\" command line option when the emulator is started) or\n"
  2253. "  from the interactive user-interface running here.\n"
  2254. "With this second option, you may enter characters on \"stdin\" at any\n"
  2255. "  time during the emulation of a running BLITZ program.  These\n"
  2256. "  characters will be supplied to the running BLITZ program (via the\n"
  2257. "  emulated Serial I/O device).  If the emulator seems to hang, it may\n"
  2258. "  be because the emulator is waiting for you to type additional\n"
  2259. "  characters.  (It may also be because the BLITZ program has gotten\n"
  2260. "  into an infinite loop.)\n"
  2261. "At any time you may always hit control-C to suspend instruction execution\n"
  2262. "  and re-enter the emulator command interface.\n"
  2263. "Normally a host OS like UNIX will process user input by echoing characters\n"
  2264. "  on the screen, buffering entire lines, and processing special characters\n"
  2265. "  like backspaces, etc.  This is called \"cooked\" input.  In \"raw\" mode\n"
  2266. "  each character is delivered as-is immediately after the key is pressed,\n"
  2267. "  with no buffering and without the normal echoing and processing of\n"
  2268. "  special characters.\n"
  2269. "The BLITZ emulator runs in either \"raw\" mode or \"cooked\" mode.\n"
  2270. "  The mode can be changed with the \"raw\" and \"cooked\" commands.\n"
  2271. "  This only affects typed input to be delivered to the running BLITZ\n"
  2272. "  program; typed input to the emulator itself is always in cooked mode.\n"
  2273. "In cooked mode, the host OS will suspend the emulator until you enter a\n"
  2274. "  complete line of data and hit ENTER.  Then, if the BLITZ program is\n"
  2275. "  echoing character data properly, you will see all the characters echoed,\n"
  2276. "  resulting in a second, identical line.  A good BLITZ program should echo\n"
  2277. "  all character data, so this duplication of input is normal in cooked\n"
  2278. "  mode.\n"
  2279. "In raw mode, the normal echoing of keystrokes by the host OS is turned\n"
  2280. "  off.  A good BLITZ program should echo all characters, so you *should*\n"
  2281. "  see each keystroke echoed properly.  But of course your BLITZ program\n"
  2282. "  may not be working properly.  It may fail to echo characters because\n"
  2283. "  it has a bug.  Also, the running BLITZ program may not handle\n"
  2284. "  backspaces, newlines, CRs, etc., exactly as you and your terminal\n"
  2285. "  expect.  It may be helpful to recall \\n=Control-J, \\r=Control-M, and\n"
  2286. "  Backspace=Control-H.  On some terminals, the ENTER key is \\r, while many\n"
  2287. "  programs expect to use \\n for END-OF-LINE.\n");
  2288.   if (termInputFileName != NULL) {
  2289.     printf ("Input for the Serial I/O device will come from file..... \"%s\"\n",
  2290.              termInputFileName);
  2291.   } else if (terminalWantRawProcessing) {
  2292.     printf ("Input for the Serial I/O device will come from..... \"stdin\"\n");
  2293.     printf ("The current input mode is.......................... \"raw\"\n");
  2294.   } else {
  2295.     printf ("Input for the Serial I/O device will come from...... \"stdin\"\n");
  2296.     printf ("The current input mode is........................... \"cooked\"\n");
  2297.   }
  2298.   printf (
  2299. "=============================================================================\n");
  2300. }
  2301.  
  2302.  
  2303.  
  2304. /* commandFormat ()
  2305. **
  2306. ** This routine is used to create a new DISK file or to change the size of an
  2307. ** existing DISK file.  When a DISK file is enlarged, its data is initialized.
  2308. */
  2309. void commandFormat () {
  2310.   int oldSectorCount, length, i, newByteLen, sec,
  2311.   newNumberOfSectors, newNumberOfTracks;
  2312.   long len;
  2313.   int magic = 0x424C5A64;   /* ASCII for "BLZd" */
  2314.   char sectorData [8192];
  2315.   char * charPtr;
  2316. #define BEGIN_MESS "<---BEGINNING OF SECTOR---------------"
  2317. #define END_MESS "-------------------------END OF SECTOR--->"
  2318.   char sectorMess [100];
  2319.  
  2320.   printf (
  2321.     "================================================================================\n"
  2322.     "This command is used to create or modify a file to be used by the BLITZ emulator\n"
  2323.     "for the disk.  By default, this file will be called \"DISK\".  The filename may\n"
  2324.     "be specified on the emulator command line with the \"-d filename\" option.  This\n"
  2325.     "command will create the file if it does not exist.  It will set the file to the\n"
  2326.     "desired size and initialize all newly allocated space.\n"
  2327.     "\n"
  2328.     "The size of the disk file is an integral number of tracks.  Each track will\n"
  2329.     "contain %d sectors.  The size of each sector is the same as the page size.\n"
  2330.     "Thus, the sector size is %d bytes.  The actual file size will be the number\n"
  2331.     "tracks times the number of sectors per track times the sector size, plus an\n"
  2332.     "additional 4 bytes, which will contain a \"magic number\".  The magic number\n"
  2333.     "is 0x%08X (decimal: %d, ASCII: \"%c%c%c%c\") and is used to identify this\n"
  2334.     "file as a BLITZ disk file.\n"
  2335.     "\n"
  2336.     "Initialization consists of writing the magic number in the first 4 bytes of the\n"
  2337.     "file and adjusting the file length.  Any data previously stored in the file will\n"
  2338.     "be preserved and any additional sectors created will be initialized.\n"
  2339.     "================================================================================\n\n",
  2340.     SECTORS_PER_TRACK , PAGE_SIZE, magic, magic,
  2341.     (magic>>24) & 0x000000ff,
  2342.     (magic>>16) & 0x000000ff,
  2343.     (magic>>8) & 0x000000ff,
  2344.     (magic>>0) & 0x000000ff
  2345. );
  2346.  
  2347.   // Display the file name...
  2348.   printf ("The name of the disk file is \"%s\".\n", diskFileName);
  2349.  
  2350.   // If open, close it...
  2351.   if (diskFile != NULL) {
  2352.     fclose (diskFile);
  2353.   }
  2354.  
  2355.   // Try to open the file as an existing file...
  2356.   diskFile = fopen (diskFileName, "r");
  2357.   if (diskFile == NULL) {
  2358.     errno = 0;
  2359.     printf ("The file \"%s\" did not previously exist.  (It could not be opened for reading.)\n", diskFileName);
  2360.     oldSectorCount = 0;
  2361.  
  2362.   // If the file already exists...
  2363.   } else {
  2364.     printf ("The file \"%s\" existed previously.\n", diskFileName);
  2365.     fseek (diskFile, 0l, SEEK_END);
  2366.     len = ftell (diskFile);
  2367.  
  2368.     // Check for length > MAX...
  2369.     if (len > ((long) MAX)) {
  2370.       printf ("ERROR: The  maximum integer is %d.\n", MAX);
  2371.       printf ("ERROR: The DISK file size exceeds the maximum; Please delete the file and try again.\n");
  2372.       initializeDisk ();
  2373.       return;
  2374.     }
  2375.  
  2376.     // Print statistics about the old file...
  2377.     length = (int) len;
  2378.     printf ("    Old File Length = %d bytes\n", length);
  2379.     if (length < (SECTORS_PER_TRACK * PAGE_SIZE + 4)) {
  2380.       printf ("      The existing DISK file is too small; it must be large enough for at least 1 track.\n");
  2381.       printf ("      The minimum DISK file size is 1 track + 4 bytes (where SectorSize = %d bytes and SectorsPerTrack = %d).\n", PAGE_SIZE, SECTORS_PER_TRACK);
  2382.     }
  2383.     oldSectorCount = (length - 4) / PAGE_SIZE;
  2384.     if (oldSectorCount * PAGE_SIZE + 4 != length) {
  2385.       printf ("      The existing DISK file size is not an even number of tracks plus 4 bytes.\n");
  2386.       printf ("      (SectorSize = %d bytes, SectorsPerTrack = %d, DISK file size = %d bytes)\n", PAGE_SIZE, SECTORS_PER_TRACK, length);
  2387.     }
  2388.     printf ("    Old Sector Count = %d\n", oldSectorCount);
  2389.     printf ("    Old Track Count = %d\n", oldSectorCount / SECTORS_PER_TRACK);
  2390.  
  2391.     // Close the old file...
  2392.     fclose (diskFile);
  2393.     diskFile = NULL;
  2394.   }
  2395.  
  2396.   // Ask for the new length...
  2397.   printf ("Enter the number of tracks (e.g., 1000; type 0 to abort):\n");
  2398.   newNumberOfTracks = readDecimalInt ();
  2399.  
  2400.   // If < 0 then abort this command...
  2401.   if (newNumberOfTracks <= 0) {
  2402.     printf ("Aborting; file not changed!\n");
  2403.     initializeDisk ();
  2404.     return;
  2405.  
  2406.   // Otherwise...
  2407.   } else {
  2408.  
  2409.     // Compute and print statistics about the new file...
  2410.     printf ("Desired number of tracks = %d\n", newNumberOfTracks);
  2411.     newNumberOfSectors = newNumberOfTracks * SECTORS_PER_TRACK;
  2412.     newByteLen = newNumberOfSectors * PAGE_SIZE;
  2413.     printf ("  New number of sectors = %d\n", newNumberOfSectors);
  2414.     printf ("  New number of tracks = %d\n", newNumberOfTracks);
  2415.     printf ("  New number of data bytes = %d\n", newByteLen);
  2416.  
  2417.     // Create the file if it does not exist...
  2418.     diskFile = fopen (diskFileName, "a+");
  2419.     if (diskFile == NULL) {
  2420.       printf ("The file could not be created!\n");
  2421.       initializeDisk ();
  2422.       return;
  2423.     }
  2424.     fclose (diskFile);
  2425.  
  2426.     // Alter the file's length, if necessary...
  2427.     errno = 0;
  2428.     i = truncate (diskFileName, (off_t) newByteLen+4);    // off_t is "long long" in sys/types.h
  2429.     if (i == 0) {
  2430.       printf ("    The magic number will consume 4 additional bytes.\n", newByteLen+4);
  2431.       printf ("File length changed to %d bytes.\n", newByteLen+4);
  2432.     } else {
  2433.       printf ("Problems during call to 'truncate'; Disk I/O has been disabled!\n");
  2434.       if (errno) perror ("Error");
  2435.       fclose (diskFile);
  2436.       diskFile = NULL;
  2437.       return;
  2438.     }
  2439.  
  2440.     // Open the file for updating...
  2441.     diskFile = fopen (diskFileName, "r+");
  2442.     if (diskFile == NULL) {
  2443.       printf ("The file could not be opened!\n");
  2444.       initializeDisk ();
  2445.       return;
  2446.     }
  2447.  
  2448.     // Write the magic number...
  2449.     errno = 0;
  2450.     if (fseek (diskFile, 0l, SEEK_SET)) {
  2451.       if (errno) perror ("Error on DISK file");
  2452.       fclose (diskFile);
  2453.       diskFile = NULL;
  2454.       return;
  2455.     }
  2456.     magic = SWAP_BYTES (magic);      // Write out in Big Endian order
  2457.     fwrite (&magic, 4, 1, diskFile);
  2458.     if (errno) perror ("Error writing to DISK file");
  2459.  
  2460.     // Print a message...
  2461.     if (newNumberOfSectors > oldSectorCount) {
  2462.       printf ("Initializing sectors %d through %d...\n", oldSectorCount, newNumberOfSectors-1);
  2463.     } else if (newNumberOfSectors < oldSectorCount) {
  2464.       printf ("The pre-existing data in sectors 0 through %d will be preserved...\n", newNumberOfSectors-1);
  2465.     }
  2466.  
  2467.     // In a loop, write each new sector data out...
  2468.     for (sec = oldSectorCount; sec < newNumberOfSectors; sec++) {
  2469.  
  2470.       // printf ("Initializing sector %d...\n", sec);
  2471.  
  2472.       // Initialize the default data for the sector...
  2473.       sprintf (sectorMess, "disk sector %06d ", sec);
  2474.       charPtr = sectorMess;
  2475.       for (i=0; i<8192; i++) {
  2476.         sectorData [i] = *(charPtr++);
  2477.         if (*charPtr == '\0') charPtr = sectorMess;
  2478.       }
  2479.       charPtr = BEGIN_MESS;
  2480.       for (i = 0; i < strlen (BEGIN_MESS); i++) {
  2481.         sectorData[i] = *(charPtr++);
  2482.       }
  2483.       charPtr = END_MESS;
  2484.       for (i = 0; i < strlen (END_MESS); i++) {
  2485.         sectorData[8192 - strlen(END_MESS) + i] = *(charPtr++);
  2486.       }
  2487.       sprintf (sectorMess, "disk sector %06d ", 123);
  2488.  
  2489.       // Seek to the proper location in the file...
  2490.       errno = 0;
  2491.       if (fseek (diskFile, ((long) ((sec * PAGE_SIZE) + 4)), SEEK_SET)) {
  2492.         printf ("Error from fseek for DISK file; Disk I/O has been disabled!\n");
  2493.         if (errno) perror ("Error");
  2494.         fclose (diskFile);
  2495.         diskFile = NULL;
  2496.         return;
  2497.       }
  2498.  
  2499.       // Write out a sector of data...
  2500.       errno = 0;
  2501.       fwrite (sectorData, 8192, 1, diskFile);
  2502.       if (errno) perror ("Error writing to DISK file");
  2503.     }
  2504.   }
  2505.  
  2506.   // Close the file.
  2507.   errno = 0;
  2508.   fclose (diskFile);
  2509.   if (errno) perror ("Error closing DISK file");
  2510.   diskFile = NULL;
  2511.  
  2512.   // Call "initializeDisk()" to set up the disk file...
  2513.   printf ("Successful completion.\n");
  2514.   initializeDisk ();
  2515.   return;
  2516. }
  2517.  
  2518.  
  2519.  
  2520. /* printNumberNL2 (i)
  2521. **
  2522. ** This routine prints a number in the form
  2523. **       0x1234abcd   ( decimal: 395441741 )
  2524. ** followed by a newline.
  2525. */
  2526. void printNumberNL2 (int i) {
  2527.   char str [100];
  2528.   char c;
  2529.   TableEntry * tableEntry;
  2530.   int index;
  2531.   printf ("0x");
  2532.   putlong (i);
  2533.   sprintf (str, "%d", i);
  2534.   printf ("     ( decimal: ");
  2535.   printStringInWidth (str, 11);
  2536.   printf (" )\n");
  2537. }
  2538.  
  2539.  
  2540.  
  2541. /* printNumberNL (i)
  2542. **
  2543. ** This routine prints a number in the form
  2544. **       0x1234abcd   ( decimal: 395441741     ascii: ".4.."   _my_label )
  2545. ** followed by a newline.
  2546. */
  2547. void printNumberNL (int i) {
  2548.   char str [100];
  2549.   char c;
  2550.   TableEntry * tableEntry;
  2551.   int index;
  2552.   if (i == 0) {
  2553.     printf ("0x00000000     ( decimal: 0 )\n");
  2554.     return;
  2555.   }
  2556.   printf ("0x");
  2557.   putlong (i);
  2558.   sprintf (str, "%d", i);
  2559.   printf ("     ( decimal: ");
  2560.   printStringInWidth (str, 11);
  2561.   if ((i >= ' ') && (i <= '~')) {
  2562.     printf (" ascii: \'");
  2563.     putchar (i & 0x000000ff);
  2564.     printf ("\'");
  2565.   }
  2566. /****
  2567.   printf (" ascii: \"");
  2568.   c = (i >> 24) & 0x000000ff;
  2569.   if ((c>=' ') && (c <= '~')) {
  2570.     putchar (c);
  2571.   } else {
  2572.     putchar ('.');
  2573.   }
  2574.   c = (i >> 16) & 0x000000ff;
  2575.   if ((c>=' ') && (c <= '~')) {
  2576.     putchar (c);
  2577.   } else {
  2578.     putchar ('.');
  2579.   }
  2580.   c = (i >> 8) & 0x000000ff;
  2581.   if ((c>=' ') && (c <= '~')) {
  2582.     putchar (c);
  2583.   } else {
  2584.     putchar ('.');
  2585.   }
  2586.   c = i & 0x000000ff;
  2587.   if ((c>=' ') && (c <= '~')) {
  2588.     putchar (c);
  2589.   } else {
  2590.     putchar ('.');
  2591.   }
  2592.   printf ("\"");
  2593. ****/
  2594.   index = findLabel (i);
  2595.   if (index != -1) {
  2596.     tableEntry = valueIndex [index];
  2597.     printf ("     %s", tableEntry->string);
  2598.   }
  2599.   printf (" )\n");
  2600. }
  2601.  
  2602.  
  2603.  
  2604. /* commandDumpMemory ()
  2605. **
  2606. ** This routine asks for a starting address and a length.  It then dumps
  2607. ** that many bytes of physical memory.
  2608. */
  2609. void commandDumpMemory () {
  2610.   int addr, len;
  2611.   printf ("Enter the starting (physical) memory address in hex: ");
  2612.   addr = readHexInt ();
  2613.   if (addr < 0 ) {
  2614.     printf ("Address must not be negative.\n");
  2615.     return;
  2616.   }
  2617.   if (addr >= MEMORY_SIZE) {
  2618.     printf ("This address is beyond the upper limit of physical memory.\n");
  2619.     printf ("The address of the upper-most byte is ");
  2620.     printNumberNL (MEMORY_SIZE - 1);
  2621.     return;
  2622.   }
  2623.   printf ("Enter the number of bytes in hex (or 0 to abort): ");
  2624.   len = readHexInt ();
  2625.   if (len <= 0 ) return;
  2626.   if (addr+len > MEMORY_SIZE) {
  2627.     printf ("This request extends beyond the upper limit of physical memory.\n");
  2628.     printf ("The maximum length from this address is ");
  2629.     printNumberNL (MEMORY_SIZE - addr);
  2630.     return;
  2631.   }
  2632.   printMemory (& memory [addr], len, addr);
  2633. }
  2634.  
  2635.  
  2636.  
  2637. /* commandSetMemory ()
  2638. **
  2639. ** This routine asks for a starting address and a value.  It then alters
  2640. ** a single word of physical memory.
  2641. */
  2642. void commandSetMemory () {
  2643.   int addr, val;
  2644.   printf ("Enter the (physical) memory address in hex of the word to be modified: ");
  2645.   addr = readHexInt ();
  2646.   if (addr < 0 ) {
  2647.     printf ("Address must not be negative.\n");
  2648.     return;
  2649.   }
  2650.   if (!physicalAddressOk (addr)) {
  2651.     printf ("This is not a legal physical address.\n");
  2652.     return;
  2653.   }
  2654.   if (!isAligned (addr)) {
  2655.     printf ("This is address is not word-aligned.\n");
  2656.     return;
  2657.   }
  2658.   printf ("The old value is:\n");
  2659.   printf ("0x%06X: 0x%08X\n", addr, getPhysicalWord (addr));
  2660.   printf ("Enter the new value (4 bytes in hex): ");
  2661.   val = readHexInt ();
  2662.   putPhysicalWord (addr, val);
  2663.   printf ("0x%06X: 0x%08X\n", addr, getPhysicalWord (addr));
  2664. }
  2665.  
  2666.  
  2667.  
  2668. /* commandWriteWord ()
  2669. **
  2670. ** This routine asks for an address and a value.  It then does a clean
  2671. ** write to a single word of physical memory.  This can be used to
  2672. ** test the memory-mapped I/O regions.
  2673. */
  2674. void commandWriteWord () {
  2675.   int addr, val;
  2676.   printf ("This command can be used to write to a word of memory that is in the\n");
  2677.   printf ("    memory-mapped I/O region, sending data or commands to the I/O device.\n");
  2678.   printf ("Enter the (physical) memory address in hex of the word to be written to: ");
  2679.   addr = readHexInt ();
  2680.   if (addr < 0 ) {
  2681.     printf ("Address must not be negative.\n");
  2682.     return;
  2683.   }
  2684.   if (!physicalAddressOk (addr)) {
  2685.     printf ("This is not a legal physical address.\n");
  2686.     return;
  2687.   }
  2688.   if (!isAligned (addr)) {
  2689.     printf ("This is address is not word-aligned.\n");
  2690.     return;
  2691.   }
  2692.   printf ("Enter the new value (4 bytes in hex): ");
  2693.   val = readHexInt ();
  2694.   putPhysicalWord (addr, val);
  2695.   printf ("Writing word...   address = 0x%06X    value = 0x%08X\n", addr, val);
  2696. }
  2697.  
  2698.  
  2699.  
  2700. /* commandReadWord ()
  2701. **
  2702. ** This routine asks for an address and a value.  It then does a clean
  2703. ** read from to a single word of physical memory.  This can be used to
  2704. ** test the memory-mapped I/O regions.
  2705. */
  2706. void commandReadWord () {
  2707.   int addr, val;
  2708.   printf ("This command can be used to read a word of memory that is in the\n");
  2709.   printf ("    memory-mapped I/O region, retrieving I/O device status or data.\n");
  2710.   printf ("Enter the (physical) memory address in hex of the word to be read from: ");
  2711.   addr = readHexInt ();
  2712.   if (addr < 0 ) {
  2713.     printf ("Address must not be negative.\n");
  2714.     return;
  2715.   }
  2716.   if (!physicalAddressOk (addr)) {
  2717.     printf ("This is not a legal physical address.\n");
  2718.     return;
  2719.   }
  2720.   if (!isAligned (addr)) {
  2721.     printf ("This is address is not word-aligned.\n");
  2722.     return;
  2723.   }
  2724.   val = getPhysicalWord (addr);
  2725.   printf ("Reading word...   address = 0x%06X    value = 0x%08X\n", addr, val);
  2726. }
  2727.  
  2728.  
  2729.  
  2730. /* commandInput ()
  2731. **
  2732. ** This command allows the user to type several characters, which will
  2733. ** be supplied to the serial input of the BLITZ machine as they are needed.
  2734. */
  2735. void commandInput () {
  2736.   int ch, i;
  2737.   char terminalBuffer [1];
  2738.  
  2739.   /* Make sure the serial input is not coming from a file. */
  2740.   if (termInputFile != stdin) {
  2741.     printf ("You cannot type in input since it comes from file \"%s\".\n",
  2742.             termInputFileName);
  2743.     return;
  2744.   }
  2745.  
  2746.   printf ("The following characters will be supplied as input to the BLITZ serial\n"
  2747.           "input before querying the terminal:\n  ");
  2748.   printTypeAheadBuffer ();
  2749.   printf ("You may add to this type-ahead buffer now.\n");
  2750.   if (terminalWantRawProcessing) {
  2751.     printf ("The terminal is now in \"raw\" mode.\nEnter characters followed by control-D...\n");
  2752.   } else {
  2753.     printf ("The terminal is now in \"cooked\" mode.\nEnter characters followed by control-D...\n");
  2754.   }
  2755.  
  2756.   /* Read in a bunch of characters. */
  2757.   fflush (stdout);
  2758.   turnOnTerminal ();
  2759.   while (1) {
  2760.     /* Get the next character. */
  2761.     i = fread (terminalBuffer, 1, 1, termInputFile);
  2762.     if (i == 1) {
  2763.       ch = terminalBuffer [0];
  2764.     } else {
  2765.       break;  /* Should not occur... */
  2766.     }
  2767.     /* Exit if EOF (i.e., control-D) was entered. */
  2768.     if (ch == 4) {
  2769.       break;
  2770.     }
  2771.     if (feof (termInputFile)) {
  2772.       break;
  2773.     }
  2774.     /* Echo the character. */
  2775.     if (terminalInRawMode) {
  2776.       fancyPrintChar (ch);
  2777.     }
  2778.     /* Add this character to the type-ahead buffer. */
  2779.     addToTypeAhead (ch);
  2780.   }
  2781.   turnOffTerminal ();
  2782.   printf ("\n");
  2783.   printf ("The following characters will be supplied as input to the BLITZ serial\n"
  2784.           "input before querying the terminal:\n  ");
  2785.   printTypeAheadBuffer ();
  2786. }
  2787.  
  2788.  
  2789.  
  2790. /* addToTypeAhead (ch)
  2791. **
  2792. ** This routine is passed a character.  It adds it to the type-ahead
  2793. ** buffer.  If the buffer is full, a fatal error occurs.
  2794. */
  2795. void addToTypeAhead (int ch) {
  2796.   if (typeAheadBufferCount < TYPE_AHEAD_BUFFER_SIZE) {
  2797.     typeAheadBufferCount++;
  2798.     typeAheadBuffer [typeAheadBufferIn++] = ch;
  2799.     if (typeAheadBufferIn >= TYPE_AHEAD_BUFFER_SIZE) {
  2800.       typeAheadBufferIn = 0;
  2801.     }
  2802.   } else {
  2803.     fatalError ("The type-ahead buffer has overflowed!");
  2804.   }
  2805. }
  2806.  
  2807.  
  2808.  
  2809. /* printTypeAheadBuffer ()
  2810. **
  2811. ** This routine prints out the type-ahead buffer.
  2812. */
  2813. void printTypeAheadBuffer () {
  2814.   int i, j, ch;
  2815.   printf ("\"");
  2816.   j = typeAheadBufferOut;
  2817.   for (i=typeAheadBufferCount; i>0; i--) {
  2818.     ch = typeAheadBuffer[j++];
  2819.     fancyPrintChar (ch);
  2820.     if (j >= TYPE_AHEAD_BUFFER_SIZE) {
  2821.       j = 0;
  2822.     }
  2823.   }
  2824.   printf ("\"\n");
  2825. }
  2826.  
  2827.  
  2828.  
  2829. /* fancyPrintChar (ch)
  2830. **
  2831. ** This routine is passed a character.  It prints it on stdout.  If
  2832. ** the character is a non-graphic, it is print using these escapes:
  2833. **     \"  \'  \\  \0  \a  \b  \t  \n  \v  \f  \r  \xFF
  2834. ** The last form is a variation on the \377 octal notation of "C", but
  2835. ** in hex instead.
  2836. */
  2837. void fancyPrintChar (int ch) {
  2838.  
  2839. /*****
  2840.   if (ch == 0) {
  2841.     printf ("\\0");
  2842.   } else if (ch == 8) {
  2843.     printf ("\\b");
  2844.   } else if (ch == 92) {
  2845.     printf ("\\\\");
  2846.   } else if (ch == 10) {
  2847.     printf ("\\n");
  2848.   } else if (ch == 9) {
  2849.     printf ("\\t");
  2850.   } else if (ch == 13) {
  2851.     printf ("\\r");
  2852.   } else if (ch == 34) {
  2853.     printf ("\\\"");
  2854.   } else if ((ch >= 32) && (ch < 127)) {
  2855.     printf ("%c", ch);
  2856.   } else {
  2857.     printf ("\\x%02X", ch);
  2858.   }
  2859. *****/
  2860.  
  2861.   if (ch == '\"') {
  2862.     printf ("\\\"");
  2863.   } else if (ch == '\'') {
  2864.     printf ("\\\'");
  2865.   } else if (ch == '\\') {
  2866.     printf ("\\\\");
  2867.   } else if ((ch >= 32) && (ch < 127)) {
  2868.     printf ("%c", ch);
  2869.   } else if (ch == '\0') {
  2870.     printf ("\\0");
  2871.   } else if (ch == '\a') {
  2872.     printf ("\\a");
  2873.   } else if (ch == '\b') {
  2874.     printf ("\\b");
  2875.   } else if (ch == '\t') {
  2876.     printf ("\\t");
  2877.   } else if (ch == '\n') {
  2878.     printf ("\\n");
  2879.   } else if (ch == '\v') {
  2880.     printf ("\\v");
  2881.   } else if (ch == '\f') {
  2882.     printf ("\\f");
  2883.   } else if (ch == '\r') {
  2884.     printf ("\\r");
  2885.   } else {
  2886.     printf ("\\x%02X", ch);
  2887.   }
  2888. }
  2889.  
  2890.  
  2891.  
  2892. /* readHexInt ()
  2893. **
  2894. ** This routine reads in an integer from the terminal and returns it.
  2895. ** This version is preliminary.  Eventually, we would like to be able to
  2896. ** read an arbitary expression, evaluate it, and then return the integer.
  2897. */
  2898. int readHexInt () {
  2899.   int i = 0;
  2900.   char * str = getToken ();
  2901.   sscanf (str, "%x", &i);
  2902.   return i;
  2903. }
  2904.  
  2905.  
  2906.  
  2907. /* readDecimalInt ()
  2908. **
  2909. ** This routine reads in an integer from the terminal and returns it.
  2910. ** This version is preliminary.  Eventually, we would like to be able to
  2911. ** read an arbitary expression, evaluate it, and then return the integer.
  2912. */
  2913. int readDecimalInt () {
  2914.   int i;
  2915.   char * str = getToken ();
  2916.   sscanf (str, "%d", &i);
  2917.   return i;
  2918. }
  2919.  
  2920.  
  2921.  
  2922. /* readDouble ()
  2923. **
  2924. ** Read a floating-point number from the user and return it as a double.
  2925. */
  2926. double readDouble () {
  2927.   double x = 0.0;
  2928.   char * str = toLower (getToken ());
  2929.   if (!strcmp (str, "nan")) {
  2930.     return 0.0 / 0.0;
  2931.   } else if (!strcmp (str, "inf")) {
  2932.     return 1.0 / 0.0;
  2933.   } else if (!strcmp (str, "-inf")) {
  2934.     return -1.0 / 0.0;
  2935.   }
  2936.   sscanf (str, "%lg", &x);
  2937.   return x;
  2938. }
  2939.  
  2940.  
  2941.  
  2942. /* readYesNo ()
  2943. **
  2944. ** This routine reads either "yes" or "no" from the user and returns
  2945. ** true if yes, false if no.
  2946. */
  2947. int readYesNo () {
  2948.   char * command;
  2949.   while (1) {
  2950.     command = toLower (getToken ());
  2951.     if (!strcmp (command, "yes")) return 1;
  2952.     if (!strcmp (command, "y")) return 1;
  2953.     if (!strcmp (command, "no")) return 0;
  2954.     if (!strcmp (command, "n")) return 0;
  2955.     printf ("INVLAID ENTRY:  Please enter \"y\" or \"n\"...");
  2956.   }
  2957. }
  2958.  
  2959.  
  2960.  
  2961. /* commandSetI ()
  2962. ** commandSetS ()
  2963. ** commandSetP ()
  2964. ** commandSetZ ()
  2965. ** commandSetV ()
  2966. ** commandSetN ()
  2967. **
  2968. ** commandClearI ()
  2969. ** commandClearS ()
  2970. ** commandClearP ()
  2971. ** commandClearZ ()
  2972. ** commandClearV ()
  2973. ** commandClearN ()
  2974. **
  2975. ** These commands set and clear the indicated bit in the status register.
  2976. */
  2977. void commandSetI () {
  2978.   statusI = 1;
  2979.   printf ("The I bit is now 1: Interrupts Enabled.\n");
  2980. }
  2981. void commandSetS () {
  2982.   statusS = 1;
  2983.   printf ("The S bit is now 1: System Mode.\n");
  2984. }
  2985. void commandSetP () {
  2986.   statusP = 1;
  2987.   printf ("The P bit is now 1: Paging Enabled.\n");
  2988.   printAboutToExecute ();
  2989. }
  2990. void commandSetZ () {
  2991.   statusZ = 1;
  2992.   printf ("The Z bit is now 1: Zero.\n");
  2993. }
  2994. void commandSetV () {
  2995.   statusV = 1;
  2996.   printf ("The V bit is now 1: Overflow Occurred.\n");
  2997. }
  2998. void commandSetN () {
  2999.   statusN = 1;
  3000.   printf ("The N bit is now 1: Negative.\n");
  3001. }
  3002.  
  3003. void commandClearI () {
  3004.   statusI = 0;
  3005.   printf ("The I bit is now 0: Interrupts Disabled.\n");
  3006. }
  3007. void commandClearS () {
  3008.   statusS = 0;
  3009.   printf ("The S bit is now 0: User Mode.\n");
  3010. }
  3011. void commandClearP () {
  3012.   statusP = 0;
  3013.   printf ("The P bit is now 0: Paging Disabled.\n");
  3014.   printAboutToExecute ();
  3015. }
  3016. void commandClearZ () {
  3017.   statusZ = 0;
  3018.   printf ("The Z bit is now 0: Not Zero.\n");
  3019. }
  3020. void commandClearV () {
  3021.   statusV = 0;
  3022.   printf ("The V bit is now 0: No Overflow.\n");
  3023. }
  3024. void commandClearN () {
  3025.   statusN = 0;
  3026.   printf ("The N bit is now 0: Not Negative.\n");
  3027. }
  3028.  
  3029.  
  3030.  
  3031. /* commandSetPC ()
  3032. **
  3033. ** This command is used to set the PC register.
  3034. */
  3035. void commandSetPC () {
  3036.   int i;
  3037.   printf ("Please enter the new value for the program counter (PC): ");
  3038.   i = readHexInt ();
  3039.   if (i%4 != 0) {
  3040.     printf ("ERROR: The PC must always be word aligned.  No change.\n");
  3041.     return;
  3042.   }
  3043.   pc = i;
  3044.   printf ("  PC   = ");
  3045.   printNumberNL (pc);
  3046.   printAboutToExecute ();
  3047. }
  3048.  
  3049.  
  3050.  
  3051. /* commandSetPTBR ()
  3052. **
  3053. ** This command is used to set the PTBR register.
  3054. */
  3055. void commandSetPTBR () {
  3056.   int i;
  3057.   printf ("Enter the new value for the Page Table Base Register (PTBR) in hex: ");
  3058.   i = readHexInt ();
  3059.   if (i%4 != 0) {
  3060.     printf ("ERROR: The PTBR must always be word aligned.  No change.\n");
  3061.     return;
  3062.   }
  3063.   ptbr = i;
  3064.   printf ("  PTBR = ");
  3065.   printNumberNL (ptbr);
  3066. }
  3067.  
  3068.  
  3069.  
  3070. /* commandSetPTLR ()
  3071. **
  3072. ** This command is used to set the PTLR register.
  3073. */
  3074. void commandSetPTLR () {
  3075.   int i;
  3076.   printf ("Enter the new value for the Page Table Length Register (PTLR) in hex: ");
  3077.   i = readHexInt ();
  3078.   if (i%4 != 0) {
  3079.     printf ("ERROR: The PTLR must always be word aligned.  No change.\n");
  3080.     return;
  3081.   }
  3082.   ptlr = i;
  3083.   printf ("  PTLR = ");
  3084.   printNumberNL (ptlr);
  3085. }
  3086.  
  3087.  
  3088.  
  3089. /* commandPrintPageTable ()
  3090. **
  3091. ** This command is used to print the page table.
  3092. */
  3093. void commandPrintPageTable () {
  3094.   int tableEntryAddr, tableEntry, frameNumber, i, addInfo;
  3095.  
  3096.   printf ("  Page Table:\n");
  3097.   printf ("    base   (PTBR) = 0x%08X\n", ptbr);
  3098.   printf ("    length (PTLR) = 0x%08X\n", ptlr);
  3099.   printf ("    This table describes a logical address space with 0x%08X (decimal %d) bytes (i.e., %d pages)\n", ptlr * PAGE_SIZE / 4,  ptlr * PAGE_SIZE / 4, ptlr / 4);
  3100.   printf ("\n");
  3101.  
  3102.   printf ("    addr      entry        Logical  Physical  Undefined Bits  Dirty  Referenced  Writeable  Valid\n");
  3103.   printf ("  ========   ========     ========  ========  ==============  =====  ==========  =========  =====\n");
  3104.  
  3105.   i = 0;
  3106.   for (tableEntryAddr = ptbr;
  3107.        tableEntryAddr < ptbr + ptlr;
  3108.        tableEntryAddr = tableEntryAddr + 4) {
  3109.  
  3110.     /* Check for an address exception.  If found, signal it and return. */
  3111.     if (!physicalAddressOk (tableEntryAddr)) {
  3112.       printf ("   This page table entry is not within physical memory!\n");
  3113.       break;
  3114.     }
  3115.  
  3116.     /* Get the page table entry. */
  3117.     /*   (tableEntryAddr must be aligned since ptbr is aligned.) */
  3118.     tableEntry = getPhysicalWord (tableEntryAddr);
  3119.     frameNumber = tableEntry & 0xffffe000;
  3120.     addInfo = (tableEntry & 0x00001ff0) >> 4;
  3121.  
  3122.     printf ("  %08X:  %08X     %08X", tableEntryAddr, tableEntry, i<<13);
  3123.     printf ("  %08X ", frameNumber);
  3124.     printf ("      %03x   ", addInfo);
  3125.     /* Print Dirty Bit */
  3126.     if (tableEntry & 0x00000008) {
  3127.       printf ("       1");
  3128.     } else {
  3129.       printf ("       0");
  3130.     }
  3131.     /* Print Referenced Bit */
  3132.     if (tableEntry & 0x00000004) {
  3133.       printf ("        1");
  3134.     } else {
  3135.       printf ("        0");
  3136.     }
  3137.     /* Print Writeable Bit */
  3138.     if (tableEntry & 0x00000002) {
  3139.       printf ("           1");
  3140.     } else {
  3141.       printf ("           0");
  3142.     }
  3143.     /* Print Valid Bit */
  3144.     if (tableEntry & 0x00000001) {
  3145.       printf ("        1");
  3146.     } else {
  3147.       printf ("        0");
  3148.     }
  3149.  
  3150.     if (!physicalAddressOk (frameNumber)) {
  3151.       printf ("    *** Bad physical address! ***\n");
  3152.     } else {
  3153.       printf ("\n");
  3154.     }
  3155.  
  3156.     i++;
  3157.     if (controlCPressed) {
  3158.       controlCPressed = 0;
  3159.       break;
  3160.     }
  3161.  
  3162.   }
  3163. }
  3164.  
  3165.  
  3166.  
  3167. /* commandTranslate ()
  3168. **
  3169. ** This command command asks for a logical address.  It the perform
  3170. ** address translation on it.
  3171. */
  3172. void commandTranslate () {
  3173.   int logicalAddr, physAddr, reading, doUpdates;
  3174.   char * str;
  3175.   printf ("Please enter a logical address: ");
  3176.   logicalAddr = readHexInt ();
  3177.   printf ("Will this be a read-only operation (y/n)? ");
  3178.   reading = readYesNo ();
  3179.   printf ("After figuring out the affect of this memory access, do you want me to update the\n"
  3180.           "page table and signal exceptions, if any, as if this operation were performed (y/n)? ");
  3181.   doUpdates = readYesNo ();
  3182.   /*   printf ("reading = %d   doUpdates=%d\n", reading, doUpdates);   */
  3183.   printf ("Calling:\n    translate (logicalAddr=0x%08X, reading=", logicalAddr);
  3184.   if (reading) {
  3185.     printf ("TRUE");
  3186.   } else {
  3187.     printf ("FALSE");
  3188.   }
  3189.   printf (", wantPrinting=TRUE, doUpdates=");
  3190.   if (doUpdates) {
  3191.     printf ("TRUE");
  3192.   } else {
  3193.     printf ("FALSE");
  3194.   }
  3195.   printf (")\n");
  3196.  
  3197.   physAddr = translate (logicalAddr, reading, 1, doUpdates);
  3198.  
  3199.   if (translateCausedException) {
  3200.     printf ("An exception occurred during translation.\n");
  3201.     if (!doUpdates) {
  3202.       printf ("  This exception will be ignored: The processor state has not been modified.\n");
  3203.     } else {
  3204.       printf ("  The processor state has been modified: this exception is now pending.\n");
  3205.     }
  3206.     return;
  3207.   }
  3208.   printf ("The value of the target word in physical memory was not changed.  It is...\n");
  3209.   printf ("  0x%06X: 0x%08X\n", physAddr, getPhysicalWord (physAddr));
  3210.   if (doUpdates && statusP) {
  3211.     printf ("The page table may have been modified by this command.\n");
  3212.   } else {
  3213.     printf ("The page table has not been modified by this command.\n");
  3214.   }
  3215. }
  3216.  
  3217.  
  3218.  
  3219. /* commandCancel ()
  3220. **
  3221. ** This routine cancels all pending interrupts.
  3222. */
  3223. void commandCancel () {
  3224.   interruptsSignaled = 0;
  3225.   systemTrapNumber = 0;
  3226.   pageInvalidOffendingAddress = 0;
  3227.   pageReadonlyOffendingAddress = 0;
  3228.   printf ("All pending interrupts have been cancelled.\n");
  3229. }
  3230.  
  3231.  
  3232.  
  3233. /* commandLabels ()
  3234. **
  3235. ** This routine prints the label tables, first by the alphaIndex, then
  3236. ** by the valueIndex.
  3237. */
  3238. void commandLabels () {
  3239.   int i, j;
  3240.   TableEntry * p;
  3241.   printf ("Ordered alphabetically:\n");
  3242.   printf ("    Label                          Hex Value  (in decimal)\n");
  3243.   printf ("    ============================== =========  ============\n");
  3244.   for (i=0; i<numberOfLabels; i++) {
  3245.     p = alphaIndex [i];
  3246.     printf ("    ");
  3247.     printStringInWidth (p->string, 30);
  3248.     printf ("  %08X   %11d\n", p->value, p->value);
  3249.   }
  3250.   printf ("Ordered by value:\n");
  3251.   printf ("    Label                          Hex Value  (in decimal)\n");
  3252.   printf ("    ============================== =========  ============\n");
  3253.   for (i=0; i<numberOfLabels; i++) {
  3254.     p = valueIndex [i];
  3255.     printf ("    ");
  3256.     printStringInWidth (p->string, 30);
  3257.     printf ("  %08X   %11d\n", p->value, p->value);
  3258.   }
  3259. }
  3260.  
  3261.  
  3262.  
  3263. /* printStringInWidth (string, width)
  3264. **
  3265. ** This routine prints this string.  It is passed a field width in
  3266. ** characters.  It will print exactly that many characters.  Shorter
  3267. ** strings will be padded with blanks; longer string will be truncated.
  3268. ** If the string length is larger than the available space, the string
  3269. ** will print like this:
  3270. **       aVeryLongAndBor...tring
  3271. **
  3272. */
  3273. void printStringInWidth (char * string, int width) {
  3274.   int i;
  3275.   int len = strlen (string);
  3276.   if (len <= width) {
  3277.     printf ("%s", string);
  3278.     for (i=width; i>len; i--) {
  3279.       printf (" ");
  3280.     }
  3281.   } else if (width < 6) {
  3282.     for (i=0; i<width; i++) {
  3283.       printf ("%c", string[i]);
  3284.     }
  3285.   } else if (width < 20) {
  3286.     for (i=0; i<width-3; i++) {
  3287.       printf ("%c", string[i]);
  3288.     }
  3289.     printf ("...");
  3290.   } else {
  3291.     for (i=0; i<width-8; i++) {
  3292.       printf ("%c", string[i]);
  3293.     }
  3294.     printf ("...");
  3295.     for (i=len-5; i<len; i++) {
  3296.       printf ("%c", string[i]);
  3297.     }
  3298.   }
  3299. }
  3300.  
  3301.  
  3302.  
  3303. /* commandInit ()
  3304. **
  3305. ** This command reinitializes the label table.  It then allows the user to
  3306. ** enter several new entries.
  3307. */
  3308. void commandInit () {
  3309.   int j;
  3310.   char * str;
  3311.   char c, * p;
  3312.   TableEntry * tableEntry;
  3313.   int value = 100;
  3314.  
  3315.   numberOfLabels = 0;
  3316.   while (1) {
  3317.     if (numberOfLabels >= MAX_NUMBER_OF_LABELS) {
  3318.       printf ("Implementation Restriction: Maximum number of labels would be exceeded; aborted.\n");
  3319.       return;
  3320.     }
  3321.     printf ("Enter the name of the next label (\"\" to terminate): ");
  3322.     str = getToken ();
  3323.     if (strlen (str) == 0) return;
  3324.     /* Check to make sure it contains legal characters only. */
  3325.     p = str;
  3326.     while ((c = *p) != 0) {
  3327.       if (c>='a' && c<='z') {
  3328.       } else if (c>='A' && c<='Z') {
  3329.       } else if (c>='0' && c<='9') {
  3330.       } else if (c=='_' || c=='.') {
  3331.       } else {
  3332.         printf ("Labels must contain letters, digits, '_' and '.' only; aborted.\n");
  3333.         return;
  3334.       }
  3335.       p++;
  3336.     }
  3337.   
  3338.     tableEntry = (TableEntry *)
  3339.                      calloc (1, sizeof (TableEntry) + strlen (str) + 1);
  3340.     if (tableEntry == 0) {
  3341.       fatalError ("Calloc failed - insufficient memory available");
  3342.     }
  3343.     tableEntry -> value = genRandom () % 100;
  3344.     strcpy (tableEntry->string, str);
  3345.     alphaIndex [numberOfLabels] = tableEntry;
  3346.     valueIndex [numberOfLabels++] = tableEntry;
  3347.   }
  3348. }
  3349.  
  3350.  
  3351.  
  3352. /* commandInit2 ()
  3353. **
  3354. ** This command reinitializes the label table.  It then allows the user to
  3355. ** enter several new entries.
  3356. */
  3357. void commandInit2 () {
  3358.   int j;
  3359.   char string [100];
  3360.   char c, * p;
  3361.   TableEntry * tableEntry;
  3362.   int value;
  3363.  
  3364.   numberOfLabels = 0;
  3365.   while (1) {
  3366.     if (numberOfLabels >= MAX_NUMBER_OF_LABELS) {
  3367.       printf ("Implementation Restriction: Maximum number of labels would be exceeded; aborted.\n");
  3368.       return;
  3369.     }
  3370.     printf ("Enter the value of the next label in decimal (-999 to terminate): ");
  3371.     value = readDecimalInt ();
  3372.     if (value == -999) return;
  3373.  
  3374.     sprintf (string, "sym_%d", value);
  3375.     tableEntry = (TableEntry *)
  3376.                      calloc (1, sizeof (TableEntry) + strlen (string) + 1);
  3377.     if (tableEntry == 0) {
  3378.       fatalError ("Calloc failed - insufficient memory available");
  3379.     }
  3380.     tableEntry -> value = value;
  3381.     strcpy (tableEntry->string, string);
  3382.     alphaIndex [numberOfLabels] = tableEntry;
  3383.     valueIndex [numberOfLabels++] = tableEntry;
  3384.   }
  3385. }
  3386.  
  3387.  
  3388.  
  3389. /* commandSort ()
  3390. **
  3391. ** Perform a quicksort on the label table, sorting by alpha index.
  3392. */
  3393. void commandSort () {
  3394.   quicksortAlpha (0, numberOfLabels-1);
  3395.   commandLabels ();
  3396. }
  3397.  
  3398.  
  3399.  
  3400. /* commandSort2 ()
  3401. **
  3402. ** Perform a quicksort on the label table, sorting by value index.
  3403. */
  3404. void commandSort2 () {
  3405.   quicksortValue (0, numberOfLabels-1);
  3406.   commandLabels ();
  3407. }
  3408.  
  3409.  
  3410.  
  3411. /* quicksortAlpha ()
  3412. **
  3413. ** This routine sorts the elements in the array from m through n (inclusive).
  3414. ** The sort is on the alphaIndex.
  3415. */
  3416. void quicksortAlpha (int m, int n) {
  3417.   int i;
  3418.   if (n>m) {
  3419.     i = partitionAlpha (m, n);
  3420.     quicksortAlpha (m, i-1);
  3421.     quicksortAlpha (i+1, n);
  3422.   }
  3423. }
  3424.  
  3425.  
  3426.  
  3427. /* partitionAlpha (l, r)
  3428. **
  3429. ** This function is used in quicksorting the array.  It is passed two
  3430. ** indexes ("l" and "r") which elements of the array are to be examined
  3431. ** and rearranged.  This routine will rearrange the elements in the
  3432. ** selected portion of the array, such that, after the partitioning,
  3433. ** all elements with indexes between "l" and "i" (inclusive) will be <=
  3434. ** a[i] and all elements with indexes between "i" and "r" (inclusive)
  3435. ** will be >= a[i].  This routine will compute and return an "i" that
  3436. ** satisfies the above conditions.
  3437. */
  3438. int partitionAlpha (int l, int r) {
  3439.   int i, j;
  3440.   TableEntry * v, * t;
  3441. /***
  3442.   printf ("==== Partition (%d, %d)\n", l, r);
  3443.   dump ();
  3444. ***/
  3445.   v = alphaIndex[r];
  3446.   i = l-1;
  3447.   j = r;
  3448.   while (1) {
  3449.     i++;
  3450.     while (strcmp (alphaIndex[i]->string , v->string) < 0) {
  3451.       i++;
  3452.     }
  3453.     j--;
  3454.     while ((j>=l) && (strcmp (alphaIndex[j]->string , v->string) > 0)) {
  3455.       j--;
  3456.     }
  3457.     t = alphaIndex[i];
  3458.     alphaIndex[i] = alphaIndex[j];
  3459.     alphaIndex[j] = t;
  3460.     if (j<=i) break;
  3461.   }
  3462.   alphaIndex[j] = alphaIndex[i];
  3463.   alphaIndex[i] = alphaIndex[r];
  3464.   alphaIndex[r] = t;
  3465. /***
  3466.   dump ();
  3467.   printf ("====  returns i=  %d\n", i);
  3468. ***/
  3469.   return i;
  3470. }
  3471.  
  3472.  
  3473.  
  3474. /* quicksortValue ()
  3475. **
  3476. ** This routine sorts the elements in the array from m through n (inclusive).
  3477. ** The sort is on the valueIndex.
  3478. */
  3479. void quicksortValue (int m, int n) {
  3480.   int i;
  3481.   if (n>m) {
  3482.     i = partitionValue (m, n);
  3483.     quicksortValue (m, i-1);
  3484.     quicksortValue (i+1, n);
  3485.   }
  3486. }
  3487.  
  3488.  
  3489.  
  3490. /* partitionValue (l, r)
  3491. **
  3492. ** This function is used in quicksorting the array.  It is passed two
  3493. ** indexes ("l" and "r") which elements of the array are to be examined
  3494. ** and rearranged.  This routine will rearrange the elements in the
  3495. ** selected portion of the array, such that, after the partitioning,
  3496. ** all elements with indexes between "l" and "i" (inclusive) will be <=
  3497. ** a[i] and all elements with indexes between "i" and "r" (inclusive)
  3498. ** will be >= a[i].  This routine will compute and return an "i" that
  3499. ** satisfies the above conditions.
  3500. */
  3501. int partitionValue (int l, int r) {
  3502.   int i, j;
  3503.   TableEntry * v, * t;
  3504. /***
  3505.   printf ("==== Partition (%d, %d)\n", l, r);
  3506.   dump ();
  3507. ***/
  3508.   v = valueIndex[r];
  3509.   i = l-1;
  3510.   j = r;
  3511.   while (1) {
  3512.     i++;
  3513.     while (valueIndex[i]->value < v->value) {
  3514.       i++;
  3515.     }
  3516.     j--;
  3517.     while ((j>=l) && (valueIndex[j]->value>v->value)) {
  3518.       j--;
  3519.     }
  3520.     t = valueIndex[i];
  3521.     valueIndex[i] = valueIndex[j];
  3522.     valueIndex[j] = t;
  3523.     if (j<=i) break;
  3524.   }
  3525.   valueIndex[j] = valueIndex[i];
  3526.   valueIndex[i] = valueIndex[r];
  3527.   valueIndex[r] = t;
  3528. /***
  3529.   dump ();
  3530.   printf ("====  returns i=  %d\n", i);
  3531. ***/
  3532.   return i;
  3533. }
  3534.  
  3535.  
  3536.  
  3537. /* commandFind ()
  3538. **
  3539. ** Find a label by name and print it.
  3540. */
  3541. void commandFind () {
  3542.   int i, prefixLength;
  3543.   char * str;
  3544.   TableEntry * tableEntry;
  3545.   printf ("Enter the first few characters of the label; all matching labels will be printed: ");
  3546.   str = getToken ();
  3547.   prefixLength = strlen (str);
  3548.   printf ("    Label                          Hex Value  (in decimal)\n");
  3549.   printf ("    ============================== =========  ============\n");
  3550.   for (i=0; i<numberOfLabels; i++) {
  3551.     tableEntry = alphaIndex [i];
  3552.     if (strlen (tableEntry->string) >= prefixLength) {
  3553.       if (bytesEqual (str, tableEntry->string, prefixLength, prefixLength)) {
  3554.         printf ("    ");
  3555.         printStringInWidth (tableEntry->string, 30);
  3556.         printf ("  %08X   %11d\n", tableEntry->value, tableEntry->value);
  3557.       }
  3558.     }
  3559.   }
  3560.  
  3561. /*****
  3562.   tableEntry = findLabelByAlpha (str, 0, numberOfLabels-1);
  3563.   if (tableEntry == NULL) {
  3564.     printf ("  Nothing found\n");
  3565.   } else {
  3566.     printf ("  %08X   (decimal: %d)   %s\n",
  3567.             tableEntry->value, tableEntry->value, tableEntry->string);
  3568.   }
  3569. *****/
  3570. }
  3571.  
  3572.  
  3573.  
  3574. /* commandFind2 ()
  3575. **
  3576. ** Find a label by value and print it.
  3577. */
  3578. void commandFind2 () {
  3579.   int value, j, index;
  3580.   TableEntry * tableEntry;
  3581.   printf ("Enter the value to find (in hex): ");
  3582.   value = readHexInt ();
  3583.   index = findLabel (value);
  3584.   index = findLabel (value);
  3585.   if (index == -1) {
  3586.     printf ("  Nothing found\n");
  3587.   } else {
  3588.     while (1) {
  3589.       if (index == -1) break;
  3590.       if (index >= numberOfLabels) break;
  3591.       tableEntry = valueIndex [index++];
  3592.       if (tableEntry->value != value) break;
  3593.       printf ("  %08X   (decimal: %d)   %s\n",
  3594.             tableEntry->value, tableEntry->value, tableEntry->string);
  3595.     }
  3596.   }
  3597. }
  3598.  
  3599.  
  3600.  
  3601. /* findLabel (value)
  3602. **
  3603. ** This routine is passed a value.  It searches the label table for a
  3604. ** label with that value.  If one is found, it returns the index within
  3605. ** valueIndex of the entry.  There may be several labels with the desired
  3606. ** value; this routine will return the one with the lowest index.  If the
  3607. ** table is empty or contains no entries equal to value, it returns
  3608. ** index = -1.
  3609. */
  3610. int findLabel (int value) {
  3611.   /* Do the binary search to find a matching label.  */
  3612.   int index = findLabel2 (value, 0, numberOfLabels-1);
  3613.   /* If none at all, return. */
  3614.   if (index == -1) return index;
  3615.   /* Find the lowest numbered index that matches. */
  3616.   while (1) {
  3617.     if (index == 0) return index;
  3618.     if (valueIndex[index-1]->value != value) return index;
  3619.     index--;
  3620.   }
  3621. }
  3622.  
  3623.  
  3624.  
  3625. /* findLabel2 (value, low, high)
  3626. **
  3627. ** This routine is passed a value.  It searches the label table for a
  3628. ** label with that value.  If one is found, it returns the index within
  3629. ** valueIndex of the entry.  There may be several labels with the desired
  3630. ** value; this routine will return an arbitrary on.  If the table is empty
  3631. ** or contains no entries equal to value, it returns index = -1.
  3632. **
  3633. ** This routine uses a binary search.  It is passed "low" and "high" which
  3634. ** indicate which portion of the valueIndex to search.
  3635. **
  3636. ** The initial call should look like this:
  3637. **      i = findLabel2 (value, 0, numberOfLabels-1);
  3638. */
  3639. int findLabel2 (int value, int low, int high) {
  3640.   int mid;
  3641.   if (high - low > 2) {
  3642.     mid = ((high - low) / 2) + low;
  3643.     if (valueIndex [mid]->value <= value) {
  3644.       return findLabel2 (value, mid, high);
  3645.     } else {
  3646.       return findLabel2 (value, low, mid-1);
  3647.     }
  3648.   } else if (high - low == 2) {
  3649.     if (valueIndex [high]->value == value) return high; 
  3650.     if (valueIndex [high-1]->value == value) return high-1;
  3651.     if (valueIndex [low]->value == value) return low;
  3652.     return -1;
  3653.   } else if (high - low == 1) {
  3654.     if (valueIndex [high]->value == value) return high;
  3655.     if (valueIndex [low]->value == value) return low;
  3656.     return -1;
  3657.   } else if (high == low) {
  3658.     if (valueIndex [high]->value == value) return high;
  3659.     return -1;
  3660.   } else {
  3661.     return -1;
  3662.   }
  3663. }
  3664.  
  3665.  
  3666.  
  3667. /* findLabelByAlpha (string, low, high)
  3668. **
  3669. ** This routine is passed a label string.  It searches the label table
  3670. ** and returns a ptr to the TableEntry of the label with that string.
  3671. ** If none matches exactly, it returns NULL.
  3672. **
  3673. ** This routine uses a binary search.  It is passed "low" and "high" which
  3674. ** indicate which portion of the alphaIndex to search.
  3675. **
  3676. ** The initial call should look like this:
  3677. **    p = findLabelByAlpha (string, 0, numberOfLabels-1);
  3678. */
  3679. TableEntry * findLabelByAlpha (char * string, int low, int high) {
  3680.   int mid;
  3681.   if (high - low > 2) {
  3682.     mid = ((high - low) / 2) + low;
  3683.     if (strcmp (alphaIndex [mid]->string , string) <= 0) {
  3684.       return findLabelByAlpha (string, mid, high);
  3685.     } else {
  3686.       return findLabelByAlpha (string, low, mid-1);
  3687.     }
  3688.   } else if (high - low == 2) {
  3689.     if (strcmp (alphaIndex [high]->string , string) == 0)
  3690.       return alphaIndex [high];
  3691.     if (strcmp (alphaIndex [high-1]->string , string) == 0)
  3692.       return alphaIndex [high-1];
  3693.     if (strcmp (alphaIndex [low]->string , string) == 0)
  3694.       return alphaIndex [low];
  3695.     return NULL;
  3696.   } else if (high - low == 1) {
  3697.     if (strcmp (alphaIndex [high]->string , string) == 0)
  3698.       return alphaIndex [high];
  3699.     if (strcmp (alphaIndex [low]->string , string) == 0)
  3700.       return alphaIndex [low];
  3701.     return NULL;
  3702.   } else if (high == low) {
  3703.     if (strcmp (alphaIndex [high]->string , string) == 0)
  3704.       return alphaIndex [high];
  3705.     return NULL;
  3706.   } else {
  3707.     return NULL;
  3708.   }
  3709. }
  3710.  
  3711.  
  3712.  
  3713. /* commandAdd ()
  3714. **
  3715. ** This command prompts for a new label and value.  Then it adds it to
  3716. ** the label table.
  3717. */
  3718. void commandAdd () {
  3719.   int j;
  3720.   char * str;
  3721.   char c, * p;
  3722.   TableEntry * tableEntry;
  3723.  
  3724.   printf ("Enter the name of the new label: ");
  3725.   str = getToken ();
  3726.  
  3727.   /* Check to make sure it contains legal characters only. */
  3728.   p = str;
  3729.   if (strlen (p) <= 0) {
  3730.     printf ("Null string -- command aborted\n");
  3731.     return;
  3732.   }
  3733.   while ((c = *p) != 0) {
  3734.     if (c>='a' && c<='z') {
  3735.     } else if (c>='A' && c<='Z') {
  3736.     } else if (c>='0' && c<='9') {
  3737.     } else if (c=='_' || c=='.') {
  3738.     } else {
  3739.       printf ("Labels must contain letters, digits, '_' and '.' only; aborted.\n");
  3740.       return;
  3741.     }
  3742.     p++;
  3743.   }
  3744.  
  3745.   /* See if this label already exists and abort if so. */
  3746.   tableEntry = findLabelByAlpha (str, 0, numberOfLabels-1);
  3747.   if (tableEntry != NULL) {
  3748.     printf ("  ERROR: Label already exists. You may not delete or change existing labels.\n");
  3749.     return;
  3750.   }
  3751.  
  3752.   /* Create a new TableEntry. */
  3753.   tableEntry = (TableEntry *)
  3754.                    calloc (1, sizeof (TableEntry) + strlen (str) + 1);
  3755.   if (tableEntry == 0) {
  3756.     fatalError ("Calloc failed - insufficient memory available");
  3757.   }
  3758.   strcpy (tableEntry->string, str);
  3759.  
  3760.   /* Get the new label's value. */
  3761.   printf ("Enter the value of the new label (in hex): ");
  3762.   tableEntry -> value = readHexInt ();
  3763.  
  3764.   /* Now insert this label into the alphaIndex and the valueIndex. */
  3765.   insertLabel (tableEntry);
  3766.  
  3767.   printf ("Label \"%s\" has been added.\n", tableEntry->string);
  3768. }
  3769.  
  3770.  
  3771.  
  3772. /* insertLabel (p)
  3773. **
  3774. ** This routine is passed a pointer to a new TableEntry.  It inserts
  3775. ** an index for it into the alphaIndex and into the valueIndex.  If there
  3776. ** is no room, this routine will call fatalError().
  3777. */
  3778. void insertLabel (TableEntry * p) {
  3779.   int i;
  3780.   if (numberOfLabels >= MAX_NUMBER_OF_LABELS) {
  3781.     fatalError ("Implementation restriction - the label table is full");
  3782.   }
  3783.   /* Shift all the labels in the valueIndex up to open up a slot. */
  3784.   i = numberOfLabels;
  3785.   while (i>0) {
  3786.     if (valueIndex[i-1]->value <= p->value) break;
  3787.     valueIndex [i] = valueIndex [i-1];
  3788.     i--;
  3789.   }
  3790.   /* Now i points to the vacant entry; put the new label there. */
  3791.   valueIndex [i] = p;
  3792.   /* Shift all the labels in the alphaIndex up to open up a slot. */
  3793.   i = numberOfLabels;
  3794.   while (i>0) {
  3795.     if (strcmp (alphaIndex[i-1]->string , p->string) <= 0) break;
  3796.     alphaIndex [i] = alphaIndex [i-1];
  3797.     i--;
  3798.   }
  3799.   /* Now i points to the vacant entry; put the new label there. */
  3800.   alphaIndex [i] = p;
  3801.   /* Increment the number of labels. */
  3802.   numberOfLabels++;
  3803. }
  3804.  
  3805.  
  3806.  
  3807. /* insertLabelUnsorted (p)
  3808. **
  3809. ** This routine is passed a pointer to a new TableEntry.  It adds
  3810. ** an index for it to the alphaIndex and to the valueIndex.  This label
  3811. ** is simply placed at the end of the array; there is no sorted insert.
  3812. ** If there is no room, this routine will call fatalError().
  3813. */
  3814. void insertLabelUnsorted (TableEntry * p) {
  3815.   if (numberOfLabels >= MAX_NUMBER_OF_LABELS) {
  3816.     fatalError ("Implementation restriction - the label table is full");
  3817.   }
  3818.   valueIndex [numberOfLabels] = p;
  3819.   alphaIndex [numberOfLabels] = p;
  3820.   numberOfLabels++;
  3821. }
  3822.  
  3823.  
  3824.  
  3825. /* commandReset ()
  3826. **
  3827. ** This command resets the entire machine state and re-reads the
  3828. ** a.out file into memory.
  3829. */
  3830. void commandReset () {
  3831.   printf ("Resetting all CPU registers and re-reading file \"%s\"...\n",
  3832.     executableFileName);
  3833.   resetState ();
  3834.   /* printInfo (); */
  3835. }
  3836.  
  3837.  
  3838.  
  3839. /* resetState ()
  3840. **
  3841. ** This routine resets the entire machine state and re-reads the a.out
  3842. ** file into memory.
  3843. */
  3844. void resetState () {
  3845.   int i, magic, len;
  3846.   char * targetAddr, * p;
  3847.   TableEntry * tableEntry;
  3848.   char buffer [2];
  3849.   FILE * blitzrc;
  3850.   char * first, * second;
  3851.  
  3852.   setSimulationConstants ();
  3853.  
  3854.   if (commandOptionRand) {
  3855.     randomSeed = randSeedFromOption;
  3856.   } else {
  3857.     randomSeed = INIT_RANDOM_SEED;
  3858.   }
  3859.  
  3860.   numberOfDiskReads = 0;
  3861.   numberOfDiskWrites = 0;
  3862.  
  3863.   /* Clear currentAddr, which is used in commandDis. */
  3864.   currentAddr = 0;
  3865.  
  3866.   /* Clear currentFrame, which is used in commandStackUp, commandStackDown. */
  3867.   currentFrame = 0;
  3868.  
  3869.   /* Clear out the label table. */
  3870.   numberOfLabels = 0;
  3871.  
  3872.   /* Reset misc. CPU registers. */
  3873.   pc = 0;
  3874.   ptbr = 0;
  3875.   ptlr = 0;
  3876.   statusN = 0;
  3877.   statusV = 0;
  3878.   statusZ = 0;
  3879.   statusP = 0;
  3880.   statusS = 1;
  3881.   statusI = 0;
  3882.  
  3883.   /* Reset any signalled interrupts. */
  3884.   interruptsSignaled = 0;
  3885.   systemTrapNumber = 0;
  3886.   pageInvalidOffendingAddress = 0;
  3887.   pageReadonlyOffendingAddress = 0;
  3888.  
  3889.   currentTime = 0;
  3890.   timeSpentAsleep = 0;
  3891.   timeOfNextTimerEvent = MAX;
  3892.   timeOfNextDiskEvent = MAX;
  3893.   timeOfNextSerialInEvent = 0;
  3894.   timeOfNextSerialOutEvent = MAX;
  3895.   doTimerEvent ();
  3896.   executionHalted = 0;
  3897.   controlCPressed = 0;
  3898.  
  3899.   /* Reset the disk */
  3900.   initializeDisk ();
  3901.  
  3902.   /* Clear out the typeAheadBuffer. */
  3903.   typeAheadBufferCount = 0;
  3904.   typeAheadBufferIn = 0;
  3905.   typeAheadBufferOut = 0;
  3906.  
  3907.   /* Position the terminal input file to the beginning. */
  3908.   if (termInputFile != stdin) {
  3909.     errno = 0;
  3910.     if (fseek (termInputFile, 0l, SEEK_SET)) {
  3911.       if (errno) perror ("Error on terminal input");
  3912.       fatalError ("Error from fseek for terminal input file");
  3913.     }
  3914.   }
  3915.  
  3916.   /* Reset the serial device. */
  3917.   terminalWantRawProcessing = commandOptionRaw;   /* Default is to have buffered input */
  3918.   termInChar = 0;                   /* Last keyboard key pressed */
  3919.   termInCharAvail = 0;              /* No input available at this time */
  3920.   termInCharWasUsed = 1;            /* Last character was absorbed ok */
  3921.   termOutputReady = 1;              /* Output device is not busy */
  3922.  
  3923.   updateTimeOfNextEvent ();
  3924.  
  3925.   /* If we have already allocated memory, free it. */
  3926.   if (memory != NULL) {
  3927.     free (memory);
  3928.   }
  3929.  
  3930.   /* Allocate a chunk of memory which will hold the BLITZ machine memory. */
  3931.   memory = (char *) calloc (1, MEMORY_SIZE);
  3932.   fflush (stdout);
  3933.   if (memory == 0) {
  3934.     fatalError ("Calloc failed - insufficient memory available");
  3935.   }
  3936.   currentMemoryLock = -1;
  3937.  
  3938.   /* Initialize all integer and floating-point registers to zero. */
  3939.   for (i=0; i<=15; i++) {
  3940.     userRegisters [i] = 0;
  3941.     systemRegisters [i] = 0;
  3942.     floatRegisters [i] = 0.0;
  3943.   }
  3944.  
  3945.   /* Reposition the a.out file to the beginning. */
  3946.   errno = 0;
  3947.   if (fseek (executableFile, 0l, SEEK_SET)) {
  3948.     if (errno) perror ("Error on a.out file");
  3949.     fatalError ("Error from fseek for a.out file");
  3950.   }
  3951.  
  3952.   /* Check the magic number. */
  3953.   magic = readInteger (executableFile);
  3954.   if (magic != 0x424C5A78) {
  3955.     fatalError ("Error in 'a.out' executable file: Magic number is not 'BLZx'");
  3956.   }
  3957.  
  3958.   /* Read in the header info. */
  3959.   textSize = readInteger (executableFile);
  3960.   dataSize = readInteger (executableFile);
  3961.   bssSize = readInteger (executableFile);
  3962.   textAddr = readInteger (executableFile);
  3963.   dataAddr = readInteger (executableFile);
  3964.   bssAddr = readInteger (executableFile);
  3965.  
  3966.   /* Compute the maximum address used and check that this will fit
  3967.      into available memory. */
  3968.   if (textAddr + textSize > MEMORY_SIZE) {
  3969.     fatalError ("The text segment will not fit into memory");
  3970.   }
  3971.   if (dataAddr + dataSize > MEMORY_SIZE) {
  3972.     fatalError ("The data segment will not fit into memory");
  3973.   }
  3974.   if (bssAddr + bssSize > MEMORY_SIZE) {
  3975.     fatalError ("The bss segment will not fit into memory");
  3976.   }
  3977.  
  3978.   /* Read in **** separator. */
  3979.   magic = readInteger (executableFile);
  3980.   if (magic != 0x2a2a2a2a) {
  3981.     fatalError ("Invalid file format - missing \"****\" separator");
  3982.   }
  3983.  
  3984.   /* Read in the text segment. */
  3985.   targetAddr = memory + textAddr;
  3986.   errno = 0;
  3987.   i = fread (targetAddr, 1, textSize, executableFile);
  3988.   if (i != textSize) {
  3989.     if (errno) perror ("Error reading from a.out file");
  3990.     fatalError ("Problems reading text segment from a.out file");
  3991.   }
  3992.  
  3993.   /* Read in **** separator. */
  3994.   magic = readInteger (executableFile);
  3995.   if (magic != 0x2a2a2a2a) {
  3996.     fatalError ("Invalid file format - missing \"****\" separator");
  3997.   }
  3998.  
  3999.   /* Read in the data segment. */
  4000.   targetAddr = memory + dataAddr;
  4001.   errno = 0;
  4002.   i = fread (targetAddr, 1, dataSize, executableFile);
  4003.   if (i != dataSize) {
  4004.     if (errno) perror ("Error reading from a.out file");
  4005.     fatalError ("Problems reading data segment from a.out file");
  4006.   }
  4007.  
  4008.   /* Read in **** separator. */
  4009.   magic = readInteger (executableFile);
  4010.   if (magic != 0x2a2a2a2a) {
  4011.     fatalError ("Invalid file format - missing \"****\" separator");
  4012.   }
  4013.  
  4014.   /* There is nothing to do for the bss segment, since the calloc will
  4015.      have cleared the memory to zero already. */
  4016.  
  4017.   /* Read in the labels and add to the end of the label table. */
  4018.   while (1) {
  4019.     len = readInteger (executableFile);
  4020.     if (len <= 0) break;
  4021.     tableEntry = (TableEntry *)
  4022.                      calloc (1, sizeof (TableEntry) + len + 1);
  4023.     if (tableEntry == 0) {
  4024.       fatalError ("Calloc failed - insufficient memory available");
  4025.     }
  4026.     tableEntry -> value = readInteger (executableFile);
  4027.     p = &( tableEntry->string[0] );
  4028.     for (; len>0; len--) {
  4029.       *p++ = readByte (executableFile);
  4030.     }
  4031.     p = 0;
  4032.     insertLabelUnsorted (tableEntry);
  4033.   }
  4034.  
  4035.   /* Sort the label table by alpha and value. */
  4036.   quicksortAlpha (0, numberOfLabels-1);
  4037.   quicksortValue (0, numberOfLabels-1);
  4038.  
  4039.   /* Read in **** separator. */
  4040.   magic = readInteger (executableFile);
  4041.   if (magic != 0x2a2a2a2a) {
  4042.     fatalError ("Invalid file format - missing \"****\" separator");
  4043.   }
  4044.  
  4045.   /* Finally, set the PC to the first address in the .text segment. */
  4046.   pc = textAddr;
  4047.  
  4048. }
  4049.  
  4050.  
  4051.  
  4052. /* printInfo ()
  4053. **
  4054. ** This routine prints out misc. info about the state of memory.
  4055. */
  4056. void printInfo () {
  4057.   printf ("Memory size = ");
  4058.   printNumberNL2 (MEMORY_SIZE);
  4059.   printf ("Page size   = ");
  4060.   printNumberNL2 (PAGE_SIZE);
  4061. /***
  4062.   printf ("Memory loc  = ");
  4063.   printNumberNL2 ((int) memory);
  4064. ***/
  4065.  
  4066.   printf (".text Segment\n");
  4067.   printf ("    addr    = ");
  4068.   printNumberNL2 (textAddr);
  4069.   printf ("    size    = ");
  4070.   printNumberNL2 (textSize);
  4071.  
  4072.   printf (".data Segment\n");
  4073.   printf ("    addr    = ");
  4074.   printNumberNL2 (dataAddr);
  4075.   printf ("    size    = ");
  4076.   printNumberNL2 (dataSize);
  4077.  
  4078.   printf (".bss Segment\n");
  4079.   printf ("    addr    = ");
  4080.   printNumberNL2 (bssAddr);
  4081.   printf ("    size    = ");
  4082.   printNumberNL2 (bssSize);
  4083. }
  4084.  
  4085.  
  4086.  
  4087. /* commandReg (reg)
  4088. **
  4089. ** This command allows the user to change the value of one of the
  4090. ** registers, r1..r15.  It looks at the status register mode bit
  4091. ** and either changes a system register or a user register.
  4092. */
  4093. void commandReg (int reg) {
  4094.   int value;
  4095.  
  4096.   /* Print out the old value. */
  4097.   if (statusS == 1) {
  4098.     printf ("  SYSTEM r%d = ", reg);
  4099.     printNumberNL (systemRegisters [reg]);
  4100.   } else {
  4101.     printf ("  USER r%d = ", reg);
  4102.     printNumberNL (userRegisters [reg]);
  4103.   }
  4104.  
  4105.   /* Read in the new value. */
  4106.   printf ("Enter the new value (in hex): ");
  4107.   value = readHexInt ();
  4108.  
  4109.   /* Change the register and print out the new value. */
  4110.   if (statusS == 1) {
  4111.     systemRegisters [reg] = value;
  4112.     printf ("  SYSTEM r%d = ", reg);
  4113.     printNumberNL (systemRegisters [reg]);
  4114.   } else {
  4115.     userRegisters [reg] = value;
  4116.     printf ("  USER r%d = ", reg);
  4117.     printNumberNL (userRegisters [reg]);
  4118.   }
  4119. }
  4120.  
  4121.  
  4122.  
  4123. /* commandFloatReg (reg)
  4124. **
  4125. ** This command allows the user to change the value of one of the
  4126. ** floating-point registers, f0..f15.
  4127. */
  4128. void commandFloatReg (int reg) {
  4129.   double value, d;
  4130.  
  4131.   /* Print out the old value. */
  4132.   d = floatRegisters [reg];
  4133.   printf ("  f%d = ", reg);
  4134.   printDouble (d);
  4135.  
  4136.   /* Read in the new value. */
  4137.   printf ("Enter the new value (e.g., 1.1, -123.456e-10, nan, inf, -inf): ");
  4138.   value = readDouble ();
  4139.  
  4140.   /* Change the register and print out the new value. */
  4141.   floatRegisters [reg] = value;
  4142.   printf ("  f%d = ", reg);
  4143.   printDouble (value);
  4144. }
  4145.  
  4146.  
  4147.  
  4148. /* commandFMem ()
  4149. **
  4150. ** This command allows the user to dump memory, interpreting the
  4151. ** contents as floating point numbers.
  4152. */
  4153. void commandFMem () {
  4154.   int i, count, index, implAddr, physAddr, from, to;
  4155.   double d;
  4156.   TableEntry * tableEntry;
  4157.  
  4158.   printf ("Enter the beginning phyical address (in hex): ");
  4159.   physAddr = readHexInt ();
  4160.   physAddr = physAddr & 0xfffffffc;
  4161.   if (!physicalAddressOk (physAddr)) {
  4162.     printf ("This is not a valid physical memory address.\n");
  4163.     return;
  4164.   }
  4165.   count = 32;  /* Display 32 floating point numbers. */
  4166.   printf ("Dumping 256 bytes as 32 double-precision floating-points...\n");
  4167.   physAddr = physAddr & 0xfffffffc;
  4168.   for (i=0; i<count; i++) {
  4169.  
  4170.     /* Check the address to make sure it is legal. */
  4171.     if (!physicalAddressOk (physAddr) || !isAligned (physAddr)) {
  4172.       printf ("Bad address - aborting.\n");
  4173.       return;
  4174.     }
  4175.  
  4176.     /* Fetch the data. */
  4177.     implAddr = ((int) memory) + physAddr;
  4178.     to = (int) & d;
  4179. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  4180.     * (int *) (to + 0) = SWAP_BYTES (* (int *) (implAddr+4));
  4181.     * (int *) (to + 4) = SWAP_BYTES (* (int *) (implAddr+0));
  4182. #else
  4183.     * (int *) (to + 0) = * (int *) (implAddr+0);
  4184.     * (int *) (to + 4) = * (int *) (implAddr+4);
  4185. #endif
  4186.     /* Print out the label(s) if any. */
  4187.     index = findLabel (physAddr);
  4188.     while (1) {
  4189.       if (index == -1) break;
  4190.       if (index >= numberOfLabels) break;
  4191.       tableEntry = valueIndex [index++];
  4192.       if (tableEntry->value != physAddr) break;
  4193.       printf ("%s:\n", tableEntry->string);
  4194.     }
  4195.  
  4196.     /* Print out the address and the value. */
  4197.     printf ("   %06X: %08X %08X   value = %.15g\n",
  4198.       physAddr,
  4199.       SWAP_BYTES (* (int *) implAddr),
  4200.       SWAP_BYTES (* (int *) (implAddr + 4)),
  4201.       d);
  4202.  
  4203.     physAddr = physAddr + 8;
  4204.   }
  4205.  
  4206. }
  4207.  
  4208.  
  4209.  
  4210. /* commandDis ()
  4211. **
  4212. ** This routine disassembles BLITZ instructions.  It asks the users
  4213. ** where to begin.
  4214. */
  4215. void commandDis () {
  4216.   printf ("Enter the beginning phyical address (in hex): ");
  4217.   currentAddr = readHexInt ();
  4218.   currentAddr = currentAddr & 0xfffffffc;
  4219.   if (!physicalAddressOk (currentAddr)) {
  4220.     printf ("This is not a valid physical memory address.\n");
  4221.     currentAddr = 0;
  4222.     return;
  4223.   }
  4224.   commandDis2 ();
  4225. }
  4226.  
  4227.  
  4228.  
  4229. /* commandDis2 ()
  4230. **
  4231. ** This routine disassembles several BLITZ instructions from the current
  4232. ** address.
  4233. */
  4234. void commandDis2 () {
  4235.   int i, count, index;
  4236.   TableEntry * tableEntry;
  4237.   count = 30;
  4238.   currentAddr = currentAddr & 0xfffffffc;
  4239.   for (i=0; i<count; i++) {
  4240.     if (!physicalAddressOk (currentAddr)) {
  4241.       printf ("Physical memory limit exceeded.\n");
  4242.       currentAddr = 0;
  4243.       return;
  4244.     }
  4245.     disassemble (currentAddr);
  4246.     currentAddr = currentAddr + 4;
  4247.   }
  4248. }
  4249.  
  4250.  
  4251.  
  4252. /* disassemble (physAddr)
  4253. **
  4254. ** This routine disassembles and prints a single BLITZ instruction.
  4255. ** This routine is passed the addr.  If this address is not a
  4256. ** a legal physical address in the BLITZ memory, it prints a message
  4257. ** and returns.
  4258. */
  4259. void disassemble (int physAddr) {
  4260.   int opcode, opcode2, cat, n, index, implAddr, instr, instr2, i1, i2;
  4261.   TableEntry * tableEntry;
  4262.   char c;
  4263.  
  4264.   char * opcodes [] = {
  4265.  
  4266. /* 0 .. 31 */
  4267. "nop", "wait", "debug", "cleari", "seti", "clearp", "setp", "clears",
  4268. "reti", "ret", "debug2", "---", "---", "---", "---", "---",
  4269. "---", "---", "---", "---", "---", "---", "---", "---",
  4270. "---", "---", "---", "---", "---", "---", "---", "---",
  4271.  
  4272. /* 32 .. 63 */
  4273. "ldptbr", "ldptlr", "---", "---", "---", "---", "---", "---",
  4274. "---", "---", "---", "---", "---", "---", "---", "---",
  4275. "---", "---", "---", "---", "---", "---", "---", "---",
  4276. "---", "---", "---", "---", "---", "---", "---", "---",
  4277.  
  4278. /* 64 .. 95 */
  4279. "call", "jmp", "be", "bne", "bl", "ble", "bg", "bge",
  4280. "---", "---", "bvs", "bvc", "bns", "bnc", "bss", "bsc",
  4281. "bis", "bic", "bps", "bpc", "push", "pop", "readu", "writeu",
  4282. "tset", "ftoi", "itof", "fcmp", "fsqrt", "fneg", "fabs", "---",
  4283.  
  4284. /* 96 .. 127 */
  4285. "add", "sub", "mul", "div", "sll", "srl", "sra", "or",
  4286. "and", "andn", "xor", "load", "loadb", "loadv", "loadbv", "store",
  4287. "storeb", "storev", "storebv", "rem", "fadd", "fsub", "fmul", "fdiv",
  4288. "fload", "fstore", "---", "---", "---", "---", "---", "---",
  4289.  
  4290. /* 128 .. 159 */
  4291. "add", "sub", "mul", "div", "sll", "srl", "sra", "or",
  4292. "and", "andn", "xor", "load", "loadb", "loadv", "loadbv", "store",
  4293. "storeb", "storev", "storebv", "readu", "writeu", "rem", "fload", "fstore",
  4294. "---", "---", "---", "---", "---", "---", "---", "---",
  4295.  
  4296. /* 160 .. 191 */
  4297. "call", "jmp", "be", "bne", "bl", "ble", "bg", "bge",
  4298. "---", "---", "bvs", "bvc", "bns", "bnc", "bss", "bsc",
  4299. "bis", "bic", "bps", "bpc", "---", "---", "---", "---",
  4300. "---", "---", "---", "---", "---", "---", "---", "---",
  4301.  
  4302. /* 192 .. 223 */
  4303. "sethi", "setlo", "ldaddr", "syscall", "---", "---", "---", "---",
  4304. "---", "---", "---", "---", "---", "---", "---", "---",
  4305. "---", "---", "---", "---", "---", "---", "---", "---",
  4306. "---", "---", "---", "---", "---", "---", "---", "---",
  4307.  
  4308. /* 224 .. 255 */
  4309. "---", "---", "---", "---", "---", "---", "---", "---",
  4310. "---", "---", "---", "---", "---", "---", "---", "---",
  4311. "---", "---", "---", "---", "---", "---", "---", "---",
  4312. "---", "---", "---", "---", "---", "---", "---", "---",
  4313.  
  4314. };
  4315.  
  4316.   int  category [] = {
  4317. /* 0 .. 31
  4318. nop, wait, debug, cleari, seti, clearp, setp, clears,
  4319. reti, ret, debug2, xxx, xxx, xxx, xxx, xxx,
  4320. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4321. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4322. */
  4323. 1, 1, 1, 1, 1, 1, 1, 1,
  4324. 1, 1, 1, 0, 0, 0, 0, 0,
  4325. 0, 0, 0, 0, 0, 0, 0, 0,
  4326. 0, 0, 0, 0, 0, 0, 0, 0,
  4327.  
  4328. /* 32 .. 63
  4329. ldptbr, ldptlr, xxx, xxx, xxx, xxx, xxx, xxx,
  4330. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4331. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4332. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4333. */
  4334. 17, 17, 0, 0, 0, 0, 0, 0,
  4335. 0, 0, 0, 0, 0, 0, 0, 0,
  4336. 0, 0, 0, 0, 0, 0, 0, 0,
  4337. 0, 0, 0, 0, 0, 0, 0, 0,
  4338.  
  4339. /* 64 .. 95
  4340. call, jmp, be, bne, bl, ble, bg, bge,
  4341. xxx, xxx, bvs, bvc, bns, bnc, bss, bsc,
  4342. bis, bic, bps, bpc, push, pop, readu, writeu,
  4343. tset, ftoi, itof, fcmp, fsqrt, fneg, fabs, xxx,
  4344. */
  4345. 8, 8, 8, 8, 8, 8, 8, 8,
  4346. 0, 0, 8, 8, 8, 8, 8, 8,
  4347. 8, 8, 8, 8, 10, 11, 15, 16,
  4348. 14, 19, 20, 22, 22, 22, 22, 0,
  4349.  
  4350. /* 96 .. 127
  4351. add, sub, mul, div, sll, srl, sra, or,
  4352. and, andn, xor, load, loadb, loadv, loadbv, store,
  4353. storeb, storev, storebv, rem, fadd, fsub, fmul, fdiv,
  4354. fload, fstore, xxx, xxx, xxx, xxx, xxx, xxx,
  4355. */
  4356. 2, 2, 2, 2, 2, 2, 2, 2,
  4357. 2, 2, 2, 4, 4, 4, 4, 6,
  4358. 6, 6, 6, 2, 21, 21, 21, 21,
  4359. 23, 25, 0, 0, 0, 0, 0, 0,
  4360.  
  4361. /* 128 .. 159
  4362. add, sub, mul, div, sll, srl, sra, or,
  4363. and, andn, xor, load, loadb, loadv, loadbv, store,
  4364. storeb, storev, storebv, readu, writeu, rem, fload, fstore,
  4365. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4366. */
  4367. 3, 3, 3, 3, 3, 3, 3, 3,
  4368. 3, 3, 3, 5, 5, 5, 5, 7,
  4369. 7, 7, 7, 7, 5, 3, 24, 26,
  4370. 0, 0, 0, 0, 0, 0, 0, 0,
  4371.  
  4372. /* 160 .. 191
  4373. call, jmp, be, bne, bl, ble, bg, bge,
  4374. xxx, xxx, bvs, bvc, bns, bnc, bss, bsc,
  4375. bis, bic, bps, bpc, xxx, xxx, xxx, xxx,
  4376. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4377. */
  4378. 9, 9, 9, 9, 9, 9, 9, 9,
  4379. 0, 0, 9, 9, 9, 9, 9, 9,
  4380. 9, 9, 9, 9, 0, 0, 0, 0,
  4381. 0, 0, 0, 0, 0, 0, 0, 0,
  4382.  
  4383. /* 192 .. 223
  4384. sethi, setlo, ldaddr, syscall, xxx, xxx, xxx, xxx,
  4385. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4386. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4387. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4388. */
  4389. 12, 12, 18, 13, 0, 0, 0, 0,
  4390. 0, 0, 0, 0, 0, 0, 0, 0,
  4391. 0, 0, 0, 0, 0, 0, 0, 0,
  4392. 0, 0, 0, 0, 0, 0, 0, 0,
  4393.  
  4394. /* 224 .. 255
  4395. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4396. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4397. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4398. xxx, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
  4399. */
  4400. 0, 0, 0, 0, 0, 0, 0, 0,
  4401. 0, 0, 0, 0, 0, 0, 0, 0,
  4402. 0, 0, 0, 0, 0, 0, 0, 0,
  4403. 0, 0, 0, 0, 0, 0, 0, 0,
  4404.  
  4405. };
  4406.  
  4407.   /* Check the address to make sure it is legal. */
  4408.   if (!physicalAddressOk (physAddr) || !isAligned (physAddr)) {
  4409.     printf ("Disassemble: Bad address.\n");
  4410.     return;
  4411.   }
  4412.  
  4413.   /* Fetch the instruction. */
  4414.   implAddr = ((int) memory) + physAddr;
  4415.   instr = SWAP_BYTES (* (int *) implAddr);
  4416.  
  4417.   /* Print out the label(s) if any. */
  4418.   index = findLabel (physAddr);
  4419.   while (1) {
  4420.     if (index == -1) break;
  4421.     if (index >= numberOfLabels) break;
  4422.     tableEntry = valueIndex [index++];
  4423.     if (tableEntry->value != physAddr) break;
  4424.     printf ("                   %s:\n", tableEntry->string);
  4425.   }
  4426.   printf ("%06X: %08X       ", physAddr, instr);
  4427.  
  4428.   opcode = (instr >> 24) & 0x000000ff;
  4429.   printStringInWidth (opcodes[opcode], 8);
  4430.  
  4431.   cat = category[opcode];
  4432.   switch (cat) {
  4433.     case 0:
  4434.       printf ("\t\t\t! Not an instruction\n");
  4435.       break;
  4436.     case 1:
  4437.       printf ("\n");
  4438.       break;
  4439.     case 2:
  4440.       printRa (instr);
  4441.       printf (",");
  4442.       printRb (instr);
  4443.       printf (",");
  4444.       printRc (instr);
  4445.       printf ("\n");
  4446.       break;
  4447.     case 3:
  4448.       printRa (instr);
  4449.       printf (",");
  4450.       printData16 (instr);
  4451.       printf (",");
  4452.       printRc (instr);
  4453.       printComment (instr & 0x0000ffff);
  4454.       break;
  4455.     case 4:
  4456.       printf ("[");
  4457.       printRa (instr);
  4458.       printf ("+");
  4459.       printRb (instr);
  4460.       printf ("],");
  4461.       printRc (instr);
  4462.       printf ("\n");
  4463.       break;
  4464.     case 5:
  4465.       printf ("[");
  4466.       printRa (instr);
  4467.       printf ("+");
  4468.       printData16 (instr);
  4469.       printf ("],");
  4470.       printRc (instr);
  4471.       printComment2 (instr & 0x0000ffff);
  4472.       break;
  4473.     case 6:
  4474.       printRc (instr);
  4475.       printf (",[");
  4476.       printRa (instr);
  4477.       printf ("+");
  4478.       printRb (instr);
  4479.       printf ("]");
  4480.       printf ("\n");
  4481.       break;
  4482.     case 7:
  4483.       printRc (instr);
  4484.       printf (",[");
  4485.       printRa (instr);
  4486.       printf ("+");
  4487.       printData16 (instr);
  4488.       printf ("]");
  4489.       printComment2 (instr & 0x0000ffff);
  4490.       break;
  4491.     case 8:
  4492.       printRa (instr);
  4493.       printf ("+");
  4494.       printRc (instr);
  4495.       printf ("\n");
  4496.       break;
  4497.     case 9:
  4498.       printData24 (instr);
  4499.       n = instr << 8;
  4500.       if (n<0) {
  4501.         n = instr | 0xff000000;
  4502.       } else {
  4503.         n = instr & 0x00ffffff;
  4504.       }
  4505.       index = findLabel (n+physAddr);
  4506.       if (index != -1) {
  4507.         printf ("\t\t! targetAddr = %s", valueIndex[index]->string);
  4508.       } else {
  4509.         printf ("\t\t! targetAddr = 0x%06X ", n+physAddr);
  4510.       }
  4511.       printf ("\n");
  4512.       break;
  4513.     case 10:
  4514.       printRc (instr);
  4515.       printf (",[--");
  4516.       printRa (instr);
  4517.       printf ("]");
  4518.       printf ("\n");
  4519.       break;
  4520.     case 11:
  4521.       printf ("[");
  4522.       printRa (instr);
  4523.       printf ("++],");
  4524.       printRc (instr);
  4525.       printf ("\n");
  4526.       break;
  4527.     case 12:      /* sethi, setlo */
  4528.       printData16 (instr);
  4529.       printf (",");
  4530.       printRc (instr);
  4531.       /* Fetch the next instruction. */
  4532.       if (physicalAddressOk (physAddr+4)) {
  4533.         implAddr = ((int) memory) + physAddr + 4;
  4534.         instr2 = SWAP_BYTES (* (int *) implAddr);
  4535.         opcode2 = (instr2 >> 24) & 0x000000ff;
  4536.       } else {
  4537.         opcode2 = 0;
  4538.       }
  4539.       /* If this instruction is a sethi and the next is a setlo, then... */
  4540.       if ((opcode == 192) && (opcode2 == 193)) {   /* SETHI == 192, SETLO=193 */
  4541.         i1 = instr & 0x0000ffff;
  4542.         i2 = instr2 & 0x0000ffff;
  4543.         printf ("\t! 0x%04X%04X = %d", i1, i2, (i1<< 16)|i2);
  4544.         index = findLabel ((i1<< 16)|i2);
  4545.         if (index != -1) {
  4546.           printf (" (%s)", valueIndex[index]->string);
  4547.         }
  4548.       }
  4549.       printf ("\n");
  4550.       break;
  4551.     case 13:
  4552.       printRc (instr);
  4553.       printf ("+");
  4554.       printData16 (instr);
  4555.       printComment2 (instr & 0x0000ffff);
  4556.       break;
  4557.     case 14:
  4558.       printf ("[");
  4559.       printRa (instr);
  4560.       printf ("],");
  4561.       printRc (instr);
  4562.       printf ("\n");
  4563.       break;
  4564.     case 15:
  4565.       printRc (instr);
  4566.       printf (",");
  4567.       printRa (instr);
  4568.       printf ("\n");
  4569.       break;
  4570.     case 16:
  4571.       printRa (instr);
  4572.       printf (",");
  4573.       printRc (instr);
  4574.       printf ("\n");
  4575.       break;
  4576.     case 17:
  4577.       printRc (instr);
  4578.       printf ("\n");
  4579.       break;
  4580.     case 18:            // i.e., the ldaddr instruction
  4581.       printData16 (instr);
  4582.       printf (",");
  4583.       printRc (instr);
  4584.       n = instr << 16;
  4585.       if (n<0) {
  4586.         n = instr | 0xffff0000;
  4587.       } else {
  4588.         n = instr & 0x0000ffff;
  4589.       }
  4590.       index = findLabel (n+physAddr);
  4591.       if (index != -1) {
  4592.         printf ("\t! targetAddr = %s", valueIndex[index]->string);
  4593.       } else {
  4594.         printf ("\t! targetAddr = 0x%06X ", n+physAddr);
  4595.       }
  4596.       printf ("\n");
  4597.       break;
  4598.     case 19:
  4599.       printFRa (instr);
  4600.       printf (",");
  4601.       printRc (instr);
  4602.       printf ("\n");
  4603.       break;
  4604.     case 20:
  4605.       printRa (instr);
  4606.       printf (",");
  4607.       printFRc (instr);
  4608.       printf ("\n");
  4609.       break;
  4610.     case 21:
  4611.       printFRa (instr);
  4612.       printf (",");
  4613.       printFRb (instr);
  4614.       printf (",");
  4615.       printFRc (instr);
  4616.       printf ("\n");
  4617.       break;
  4618.     case 22:
  4619.       printFRa (instr);
  4620.       printf (",");
  4621.       printFRc (instr);
  4622.       printf ("\n");
  4623.       break;
  4624.     case 23:
  4625.       printf ("[");
  4626.       printRa (instr);
  4627.       printf ("+");
  4628.       printRb (instr);
  4629.       printf ("],");
  4630.       printFRc (instr);
  4631.       printf ("\n");
  4632.       break;
  4633.     case 24:
  4634.       printf ("[");
  4635.       printRa (instr);
  4636.       printf ("+");
  4637.       printData16 (instr);
  4638.       printf ("],");
  4639.       printFRc (instr);
  4640.       printComment2 (instr & 0x0000ffff);
  4641.       break;
  4642.     case 25:
  4643.       printFRc (instr);
  4644.       printf (",[");
  4645.       printRa (instr);
  4646.       printf ("+");
  4647.       printRb (instr);
  4648.       printf ("]");
  4649.       printf ("\n");
  4650.       break;
  4651.     case 26:
  4652.       printFRc (instr);
  4653.       printf (",[");
  4654.       printRa (instr);
  4655.       printf ("+");
  4656.       printData16 (instr);
  4657.       printf ("]");
  4658.       printComment2 (instr & 0x0000ffff);
  4659.       break;
  4660.     default:
  4661.       fatalError ("PROGRAM LOGIC ERROR: within disassemble");
  4662.   }
  4663. }
  4664.  
  4665.  
  4666.  
  4667. /* printRa (instr)
  4668. ** printRb (instr)
  4669. ** printRc (instr)
  4670. ** printFRa (instr)
  4671. ** printFRb (instr)
  4672. ** printFRc (instr)
  4673. ** printData16 (instr)
  4674. ** printData24 (instr)
  4675. **
  4676. ** These routines print the selected field from the instruction.
  4677. */
  4678. void printRa (int instr) {
  4679.   printf ("r%d", (instr >> 16) & 0x0000000f);
  4680. }
  4681.  
  4682. void printRb (int instr) {
  4683.   printf ("r%d", (instr >> 12) & 0x0000000f);
  4684. }
  4685.  
  4686. void printRc (int instr) {
  4687.   printf ("r%d", (instr >> 20) & 0x0000000f);
  4688. }
  4689.  
  4690. void printFRa (int instr) {
  4691.   printf ("f%d", (instr >> 16) & 0x0000000f);
  4692. }
  4693.  
  4694. void printFRb (int instr) {
  4695.   printf ("f%d", (instr >> 12) & 0x0000000f);
  4696. }
  4697.  
  4698. void printFRc (int instr) {
  4699.   printf ("f%d", (instr >> 20) & 0x0000000f);
  4700. }
  4701.  
  4702. void printData16 (int instr) {
  4703.   printf ("0x%04X", instr & 0x0000ffff);
  4704. }
  4705.  
  4706. void printData24 (int instr) {
  4707.   printf ("0x%06X", instr & 0x00ffffff);
  4708. }
  4709.  
  4710.  
  4711.  
  4712. /* printComment (i)
  4713. **
  4714. ** This routine prints a number in the form
  4715. **       ! decimal = 32767  ".."  (label)
  4716. ** There is a leading tab printed and the output is followed by a newline.
  4717. */
  4718. void printComment (int i) {
  4719.   char c;
  4720.   int index;
  4721.  
  4722.   /* Sign extend the 16 bit number in i. */
  4723.   if ((i << 16) < 0) {
  4724.     i = i | 0xffff0000;
  4725.   } else {
  4726.     i = i & 0x0000ffff;
  4727.   }
  4728.  
  4729.   if (i != 0) {
  4730.     printf ("\t! decimal: %d, ascii: \"", i);
  4731.     c = (i >> 8) & 0x000000ff;
  4732.     if ((c>=' ') && (c <= '~')) {
  4733.       putchar (c);
  4734.     } else {
  4735.       putchar ('.');
  4736.     }
  4737.     c = i & 0x000000ff;
  4738.     if ((c>=' ') && (c <= '~')) {
  4739.       putchar (c);
  4740.     } else {
  4741.       putchar ('.');
  4742.     }
  4743.     printf ("\"  ");
  4744.     index = findLabel (i);
  4745.     if (index != -1) {
  4746.       printf ("(%s)", valueIndex [index]->string);
  4747.     }
  4748.   }
  4749.   printf ("\n");
  4750. }
  4751.  
  4752.  
  4753.  
  4754. /* printComment2 (i)
  4755. **
  4756. ** This routine prints a number in the form
  4757. **       ! decimal = -2147483648  (label)
  4758. ** There is a leading tab printed and the output is followed by a newline.
  4759. */
  4760. void printComment2 (int i) {
  4761.   char c;
  4762.   int index;
  4763.  
  4764.   /* Sign extend the 16 bit number in i. */
  4765.   if ((i << 16) < 0) {
  4766.     i = i | 0xffff0000;
  4767.   } else {
  4768.     i = i & 0x0000ffff;
  4769.   }
  4770.  
  4771.   /* Print the number. */
  4772.   printf ("\t! decimal: %d", i);
  4773.  
  4774.   /* If there is a label with that value, print it too. */
  4775.   index = findLabel (i);
  4776.   if (index != -1) {
  4777.     printf ("  (%s)", valueIndex [index]->string);
  4778.   }
  4779.   printf ("\n");
  4780. }
  4781.  
  4782.  
  4783.  
  4784. /* printAboutToExecute ()
  4785. **
  4786. ** This routine disassembles and prints the next instruction, which
  4787. ** is about to be executed.  It will not cause any exceptions, but will
  4788. ** notify the user if there would be problems.
  4789. */
  4790. void printAboutToExecute () {
  4791.   int physAddr;
  4792.   /* Call translate with doReading=1, wantPrinting=0, and doUpdates=0. */
  4793.   physAddr = translate (pc, 1, 0, 0);
  4794.   if (translateCausedException) {
  4795.     printf ("*****  WARNING: A PAGE_INVALID_EXCEPTION, ADDRESS_EXCEPTION, or ALIGNMENT_EXCEPTION will occur during the next instruction fetch  *****\n");
  4796.     return;
  4797.   }
  4798.   printf ("The next instruction to execute will be:\n");
  4799.   disassemble (physAddr);
  4800. }
  4801.  
  4802.  
  4803.  
  4804. /* commandStepN ()
  4805. **
  4806. ** Begin BLITZ execution, by repeatedly calling singleStep().
  4807. */
  4808. void commandStepN () {
  4809.   int count;
  4810.   printf ("Enter the number of instructions to execute: ");
  4811.   count = readDecimalInt ();
  4812.   commandGo (count);
  4813. }
  4814.  
  4815.  
  4816.  
  4817. /* commandGo (count)
  4818. **
  4819. ** Execute count BLITZ instructions, by calling singleStep() that
  4820. ** many times.  Count may be MAX, which will achieve infinite execution.
  4821. */
  4822. void commandGo (int count) {
  4823.   int i;
  4824.   printf ("Beginning execution...\n");
  4825.   wantPrintingInSingleStep = 0;
  4826.   executionHalted = 0;
  4827.   turnOnTerminal ();
  4828.   for (i=0; i<count; i++) {
  4829.     if (executionHalted) {
  4830.       break;
  4831.     }
  4832.     if (controlCPressed) {
  4833.       controlCPressed = 0;
  4834.       break;
  4835.     }
  4836.     singleStep ();
  4837.   }
  4838.   turnOffTerminal ();
  4839.   printf ("Done!  ");
  4840.   printAboutToExecute ();
  4841. }
  4842.  
  4843.  
  4844.  
  4845. /* commandStepHigh ()
  4846. **
  4847. ** Execute several instructions, by calling singleStep(), until we detect that
  4848. ** r10 has changed.  This signals the beginning of the next high-level
  4849. ** instruction.
  4850. */
  4851. void commandStepHigh () {
  4852.   int oldR10;
  4853.   // printf ("STEP HIGH...\n");
  4854.   wantPrintingInSingleStep = 1;
  4855.   executionHalted = 0;
  4856.   turnOnTerminal ();
  4857.   if (statusS) {
  4858.     oldR10 = systemRegisters [10];
  4859.   } else {
  4860.     oldR10 = userRegisters [10];
  4861.   }
  4862.   while (1) {
  4863.     if (executionHalted) {
  4864.       break;
  4865.     }
  4866.     if (controlCPressed) {
  4867.       controlCPressed = 0;
  4868.       break;
  4869.     }
  4870.  
  4871.     singleStep ();
  4872.     if (statusS) {
  4873.       if (oldR10 != systemRegisters [10]) break;
  4874.     } else {
  4875.       if (oldR10 != userRegisters [10]) break;
  4876.     }
  4877.   }
  4878.   turnOffTerminal ();
  4879.   printKPLStmtCode ();
  4880. }
  4881.  
  4882.  
  4883.  
  4884. /* commandStepHigh2 ()
  4885. **
  4886. ** Execute several instructions, by calling singleStep(), until we detect that
  4887. ** r10 has been set to "FU", "ME", or "RE".  This indicates that we are
  4888. ** entering or leaving some KPL function/method.
  4889. */
  4890. void commandStepHigh2 () {
  4891.   int newR10, oldR10;
  4892.   // printf ("STEP HIGH 2...\n");
  4893.   wantPrintingInSingleStep = 1;
  4894.   executionHalted = 0;
  4895.   turnOnTerminal ();
  4896.   while (1) {
  4897.     if (executionHalted) {
  4898.       break;
  4899.     }
  4900.     if (controlCPressed) {
  4901.       controlCPressed = 0;
  4902.       break;
  4903.     }
  4904.  
  4905.     if (statusS) {
  4906.       oldR10 = systemRegisters [10];
  4907.     } else {
  4908.       oldR10 = userRegisters [10];
  4909.     }
  4910.     singleStep ();
  4911.     if (statusS) {
  4912.       newR10 = systemRegisters [10];
  4913.     } else {
  4914.       newR10 = userRegisters [10];
  4915.     }
  4916.     if ((newR10 != oldR10) &&
  4917.         (newR10 == '\0\0FU' || newR10 == '\0\0ME' || newR10 == '\0\0RE')) {
  4918.       break;
  4919.     }
  4920.   }
  4921.   turnOffTerminal ();
  4922.   printKPLStmtCode ();
  4923. }
  4924.  
  4925.  
  4926.  
  4927. /* commandStep ()
  4928. **
  4929. ** Execute a single BLITZ instruction and print the next instruction to
  4930. ** be executed.
  4931. */
  4932. void commandStep () {
  4933.  
  4934.   wantPrintingInSingleStep = 1;
  4935.   turnOnTerminal ();
  4936.   singleStep ();
  4937.   turnOffTerminal ();
  4938.  
  4939.   /* Print the next instruction. */
  4940.   printf ("Done!  ");
  4941.   printAboutToExecute ();
  4942.  
  4943.   /*****
  4944.   if (getNextInterrupt ()) {
  4945.     printf ("Done!\n");
  4946.     // Don't print next instruction, since it will be pre-emted.
  4947.   } else {
  4948.     printf ("Done!  ");
  4949.     printAboutToExecute ();
  4950.   }
  4951.   *****/
  4952. }
  4953.  
  4954.  
  4955.  
  4956. /* singleStep ()
  4957. **
  4958. ** This routine will execute the next BLITZ instruction and return.
  4959. */
  4960. void singleStep () {
  4961.   int instr, opcode, x, y, z, thisInterrupt, sp, oldStatusReg, i, overflow;
  4962.   double d1, d2, d3;
  4963.   int physAddr, physAddr2, word, saveP, regNumber, regA, regC;
  4964.   int * p;
  4965.  
  4966.   // printf ("_");
  4967.  
  4968.   /* Increment the current time by 1 cycle.  This will overflow after
  4969.      perhaps 5 to 45 minutes of CPU-bound execution time, depending on the
  4970.      host speed. */
  4971.   if (++currentTime >= MAX) {
  4972.     fatalError ("Implementation Restriction: Current time overflow");
  4973.   }
  4974.  
  4975.   /* Check to see if we need to process an event. */
  4976.   i = 0;
  4977.   while (currentTime >= timeOfNextEvent) {
  4978.     if (currentTime >= timeOfNextTimerEvent) {
  4979.       // printf ("t");
  4980.       doTimerEvent ();
  4981.     }
  4982.     if (currentTime >= timeOfNextDiskEvent) {
  4983.       doDiskEvent ();
  4984.     }
  4985.     if (currentTime >= timeOfNextSerialInEvent) {
  4986.       // printf ("i");
  4987.       doSerialInEvent (0);
  4988.     }
  4989.     if (currentTime >= timeOfNextSerialOutEvent) {
  4990.       // printf ("o");
  4991.       doSerialOutEvent ();
  4992.     }
  4993.     updateTimeOfNextEvent ();
  4994.     if (++i > 100) {
  4995.       fatalError ("PROGRAM LOGIC ERROR: Too many events");
  4996.     }
  4997.   }
  4998.  
  4999.  
  5000.   /* Check for and process any and all interrupts that have been signaled.
  5001.      Each iteration of this loop looks at the next interrupt.  The call
  5002.      to getNextInterrupt will ignore maskable interrupts if interrupts
  5003.      are currently disabled (i.e., if statusI=0).  */
  5004.   if (thisInterrupt = getNextInterrupt ()) {
  5005.  
  5006.     // printf (".");
  5007.  
  5008.     if (wantPrintingInSingleStep) {
  5009.       if (interruptsSignaled & POWER_ON_RESET)
  5010.         printf ("*****  POWER_ON_RESET interrupt  *****\n");
  5011.       if (interruptsSignaled & TIMER_INTERRUPT)
  5012.         printf ("*****  TIMER_INTERRUPT interrupt  *****\n");
  5013.       if (interruptsSignaled & DISK_INTERRUPT)
  5014.         printf ("*****  DISK_INTERRUPT interrupt  *****\n");
  5015.       if (interruptsSignaled & SERIAL_INTERRUPT)
  5016.         printf ("*****  SERIAL_INTERRUPT interrupt  *****\n");
  5017.       if (interruptsSignaled & HARDWARE_FAULT)
  5018.         printf ("*****  HARDWARE_FAULT interrupt  *****\n");
  5019.       if (interruptsSignaled & ILLEGAL_INSTRUCTION)
  5020.         printf ("*****  ILLEGAL_INSTRUCTION interrupt  *****\n");
  5021.       if (interruptsSignaled & ARITHMETIC_EXCEPTION)
  5022.         printf ("*****  ARITHMETIC_EXCEPTION interrupt  *****\n");
  5023.       if (interruptsSignaled & ADDRESS_EXCEPTION)
  5024.         printf ("*****  ADDRESS_EXCEPTION interrupt  *****\n");
  5025.       if (interruptsSignaled & PAGE_INVALID_EXCEPTION)
  5026.         printf ("*****  PAGE_INVALID_EXCEPTION interrupt  *****\n");
  5027.       if (interruptsSignaled & PAGE_READONLY_EXCEPTION)
  5028.         printf ("*****  PAGE_READONLY_EXCEPTION interrupt  *****\n");
  5029.       if (interruptsSignaled & PRIVILEGED_INSTRUCTION)
  5030.         printf ("*****  PRIVILEGED_INSTRUCTION interrupt  *****\n");
  5031.       if (interruptsSignaled & ALIGNMENT_EXCEPTION)
  5032.         printf ("*****  ALIGNMENT_EXCEPTION interrupt  *****\n");
  5033.       if (interruptsSignaled & EXCEPTION_DURING_INTERRUPT)
  5034.         printf ("*****  EXCEPTION_DURING_INTERRUPT interrupt  *****\n");
  5035.       if (interruptsSignaled & SYSCALL_TRAP)
  5036.         printf ("*****  SYSCALL_TRAP interrupt  *****\n");
  5037.       printf ("Processing an interrupt and jumping into interrupt vector.\n");
  5038.     }
  5039.  
  5040.     /* Cancel this interrupt. */
  5041.     interruptsSignaled &= ~thisInterrupt;
  5042.  
  5043.     /* Save the old Status Register and change to System Mode, Paging
  5044.        Disabled, and Interrupts Disabled. */
  5045.     oldStatusReg = buildStatusWord ();
  5046.     statusI = 0;   
  5047.     statusS = 1;   
  5048.     statusP = 0;   
  5049.  
  5050.     /* For POWER_ON_RESET and HARDWARE_FAULT, cancel all other interrupts. */
  5051.     if ((thisInterrupt == POWER_ON_RESET) ||
  5052.         (thisInterrupt == HARDWARE_FAULT)) {
  5053.       interruptsSignaled = 0;
  5054.       pageInvalidOffendingAddress = 0;
  5055.       pageReadonlyOffendingAddress = 0;
  5056.       systemTrapNumber = 0;
  5057.  
  5058.     /* For all other interrupts, push exception info onto the stack. */
  5059.     } else {
  5060.  
  5061.       /* Check for any problems that might arise with the stack. */
  5062.       sp = systemRegisters [15];
  5063.       if (!physicalAddressOk (sp-4) || !physicalAddressOk (sp-12)
  5064.                || inMemoryMappedArea (sp-4) || inMemoryMappedArea (sp-12)
  5065.                || !isAligned (sp)) {
  5066.  
  5067.         /* Turn this into an EXCEPTION_DURING_INTERRUPT and cancel all
  5068.            other pending interrupts. */
  5069.         if (wantPrintingInSingleStep) {
  5070.           printf ("*****  An EXCEPTION_DURING_INTERRUPT interrupt has occurred  *****\n");
  5071.         }
  5072.         thisInterrupt = EXCEPTION_DURING_INTERRUPT;
  5073.         interruptsSignaled = 0;
  5074.   
  5075.       } else {
  5076.   
  5077.         /* Push the Exception info word onto the stack. */
  5078.         if (thisInterrupt == SYSCALL_TRAP) {
  5079.           putPhysicalWord (sp-4, systemTrapNumber);
  5080.           systemTrapNumber = 0;
  5081.         } else if (thisInterrupt == PAGE_INVALID_EXCEPTION) {
  5082.           putPhysicalWord (sp-4, pageInvalidOffendingAddress);
  5083.           pageInvalidOffendingAddress = 0;
  5084.         } else if (thisInterrupt == PAGE_READONLY_EXCEPTION) {
  5085.           putPhysicalWord (sp-4, pageReadonlyOffendingAddress);
  5086.           pageReadonlyOffendingAddress = 0;
  5087.         } else {
  5088.           putPhysicalWord (sp-4, 0);
  5089.         }
  5090.         /* Push the status register and the PC onto the stack. */
  5091.         putPhysicalWord (sp-8, oldStatusReg);
  5092.         putPhysicalWord (sp-12, pc);
  5093.         systemRegisters [15] = sp-12;
  5094.       }
  5095.     }
  5096.  
  5097.     /* Next, set the pc to the address in the low-memory interrupt vector. */
  5098.     pc = getVectorNumber (thisInterrupt);
  5099.  
  5100.     /* This is enough work for one machine cycle. */
  5101.     return;
  5102.  
  5103.   }
  5104.  
  5105.   /* Fetch the next instruction. */
  5106.   /*   Call translate with reading=TRUE, wantPrinting=FALSE, doUpdates=TRUE */
  5107.   i = translate (pc, 1, 0, 1);
  5108.   if (translateCausedException) {
  5109.     if (wantPrintingInSingleStep) {
  5110.       printf ("An exception has occurred during instruction fetch!\n");
  5111.     }
  5112.     return;
  5113.   }
  5114.   instr = getPhysicalWord (i);
  5115.  
  5116.   /* Print the instruction. */
  5117.   // printf ("Executing this instruction:\n");
  5118.   // printAboutToExecute ();
  5119.  
  5120.   /* Switch on the op-code. */
  5121.   opcode = (instr >> 24) & 0x000000ff;
  5122.   switch (opcode) {
  5123.  
  5124.     /***  add      Ra,Rb,Rc  ***/
  5125.     case 96:
  5126.       x = getRa (instr);
  5127.       y = getRb (instr);
  5128.       z = x + y;
  5129.       if ((x<0) && (y<0)) {
  5130.         overflow = (z>=0);
  5131.       } else if ((x>0) && (y>0)) {
  5132.         overflow = (z<=0);
  5133.       } else {
  5134.         overflow = 0;
  5135.       }
  5136.       setSR (z, overflow);
  5137.       putRc (instr, z);
  5138.       pc += 4;
  5139.       break;
  5140.  
  5141.     /***  add      Ra,data16,Rc  ***/
  5142.     case 128:
  5143.       x = getRa (instr);
  5144.       y = getData16 (instr);
  5145.       z = x + y;
  5146.       if ((x<0) && (y<0)) {
  5147.         overflow = (z>=0);
  5148.       } else if ((x>0) && (y>0)) {
  5149.         overflow = (z<=0);
  5150.       } else {
  5151.         overflow = 0;
  5152.       }
  5153.       setSR (z, overflow);
  5154.       putRc (instr, z);
  5155.       pc += 4;
  5156.       break;
  5157.  
  5158.     /***  sub      Ra,Rb,Rc  ***/
  5159.     case 97:
  5160.       x = getRa (instr);
  5161.       y = getRb (instr);
  5162.       z = x - y;
  5163.       if ((x>=0) && (y<0)) {
  5164.         overflow = (z<=0);
  5165.       } else if ((x<0) && (y>0)) {
  5166.         overflow = (z>=0);
  5167.       } else {
  5168.         overflow = 0;
  5169.       }
  5170.       setSR (z, overflow);
  5171.       putRc (instr, z);
  5172.       pc += 4;
  5173.       break;
  5174.  
  5175.     /***  sub      Ra,data16,Rc  ***/
  5176.     case 129:
  5177.       x = getRa (instr);
  5178.       y = getData16 (instr);
  5179.       z = x - y;
  5180.       if ((x>=0) && (y<0)) {
  5181.         overflow = (z<=0);
  5182.       } else if ((x<0) && (y>0)) {
  5183.         overflow = (z>=0);
  5184.       } else {
  5185.         overflow = 0;
  5186.       }
  5187.       setSR (z, overflow);
  5188.       putRc (instr, z);
  5189.       pc += 4;
  5190.       break;
  5191.  
  5192.     /***  mul      Ra,Rb,Rc  ***/
  5193.     case 98:
  5194.       x = getRa (instr);
  5195.       y = getRb (instr);
  5196.       d1 = ((double) x) * ((double) y);
  5197.       z = x * y;
  5198.       if (d1 != (double) z) {
  5199.         setSR (z, 1);
  5200.       } else {
  5201.         setSR (z, 0);
  5202.       }
  5203.       putRc (instr, z);
  5204.       pc += 4;
  5205.       break;
  5206.  
  5207.     /***  mul      Ra,data16,Rc  ***/
  5208.     case 130:
  5209.       x = getRa (instr);
  5210.       y = getData16 (instr);
  5211.       d1 = ((double) x) * ((double) y);
  5212.       z = x * y;
  5213.       if (d1 != (double) z) {
  5214.         setSR (z, 1);
  5215.       } else {
  5216.         setSR (z, 0);
  5217.       }
  5218.       putRc (instr, z);
  5219.       pc += 4;
  5220.       break;
  5221.  
  5222.     /***  div      Ra,Rb,Rc  ***/
  5223.     case 99:
  5224.       x = getRa (instr);
  5225.       y = getRb (instr);
  5226.       if (y == 0) {
  5227.         interruptsSignaled |= ARITHMETIC_EXCEPTION;
  5228.         break;
  5229.       }
  5230.       if ((x == MIN) && (y == -1)) {
  5231.         setSR (MIN, 1);
  5232.         putRc (instr, MIN);
  5233.         pc += 4;
  5234.         break;
  5235.       }
  5236.       divide (x, y);
  5237.       setSR (q, 0);
  5238.       putRc (instr, q);
  5239.       pc += 4;
  5240.       break;
  5241.  
  5242.     /***  div      Ra,data16,Rc  ***/
  5243.     case 131:
  5244.       x = getRa (instr);
  5245.       y = getData16 (instr);
  5246.       if (y == 0) {
  5247.         interruptsSignaled |= ARITHMETIC_EXCEPTION;
  5248.         break;
  5249.       }
  5250.       if ((x == MIN) && (y == -1)) {
  5251.         setSR (MIN, 1);
  5252.         putRc (instr, MIN);
  5253.         pc += 4;
  5254.         break;
  5255.       }
  5256.       divide (x, y);
  5257.       setSR (q, 0);
  5258.       putRc (instr, q);
  5259.       pc += 4;
  5260.       break;
  5261.  
  5262.     /***  sll      Ra,Rb,Rc  ***/
  5263.     case 100:
  5264.       x = getRa (instr);
  5265.       y = getRb (instr);
  5266.       y &= 0x0000001f;   /* Shift value must be 0..31 */
  5267.       z = x << y;
  5268.       setSR (z, 0);
  5269.       putRc (instr, z);
  5270.       pc += 4;
  5271.       break;
  5272.  
  5273.     /***  sll      Ra,data16,Rc  ***/
  5274.     case 132:
  5275.       x = getRa (instr);
  5276.       y = getData16 (instr);
  5277.       y &= 0x0000001f;   /* Shift value must be 0..31 */
  5278.       z = x << y;
  5279.       setSR (z, 0);
  5280.       putRc (instr, z);
  5281.       pc += 4;
  5282.       break;
  5283.  
  5284.     /***  srl      Ra,Rb,Rc  ***/
  5285.     case 101:
  5286.       x = getRa (instr);
  5287.       y = getRb (instr);
  5288.       y &= 0x0000001f;   /* Shift value must be 0..31 */
  5289.       z = (signed int) (((unsigned int) x) >> y);
  5290.       setSR (z, 0);
  5291.       putRc (instr, z);
  5292.       pc += 4;
  5293.       break;
  5294.  
  5295.     /***  srl      Ra,data16,Rc  ***/
  5296.     case 133:
  5297.       x = getRa (instr);
  5298.       y = getData16 (instr);
  5299.       y &= 0x0000001f;   /* Shift value must be 0..31 */
  5300.       z = (signed int) (((unsigned int) x) >> y);
  5301.       setSR (z, 0);
  5302.       putRc (instr, z);
  5303.       pc += 4;
  5304.       break;
  5305.  
  5306.     /***  sra      Ra,Rb,Rc  ***/
  5307.     case 102:
  5308.       x = getRa (instr);
  5309.       y = getRb (instr);
  5310.       y &= 0x0000001f;   /* Shift value must be 0..31 */
  5311.       z = x >> y;
  5312.       setSR (z, 0);
  5313.       putRc (instr, z);
  5314.       pc += 4;
  5315.       break;
  5316.  
  5317.     /***  sra      Ra,data16,Rc  ***/
  5318.     case 134:
  5319.       x = getRa (instr);
  5320.       y = getData16 (instr);
  5321.       y &= 0x0000001f;   /* Shift value must be 0..31 */
  5322.       z = x >> y;
  5323.       setSR (z, 0);
  5324.       putRc (instr, z);
  5325.       pc += 4;
  5326.       break;
  5327.  
  5328.     /***  or       Ra,Rb,Rc  ***/
  5329.     case 103:
  5330.       x = getRa (instr);
  5331.       y = getRb (instr);
  5332.       z = x | y;
  5333.       setSR (z, 0);
  5334.       putRc (instr, z);
  5335.       pc += 4;
  5336.       break;
  5337.  
  5338.     /***  or       Ra,data16,Rc  ***/
  5339.     case 135:
  5340.       x = getRa (instr);
  5341.       y = getData16 (instr);
  5342.       z = x | y;
  5343.       setSR (z, 0);
  5344.       putRc (instr, z);
  5345.       pc += 4;
  5346.       break;
  5347.  
  5348.     /***  and      Ra,Rb,Rc  ***/
  5349.     case 104:
  5350.       x = getRa (instr);
  5351.       y = getRb (instr);
  5352.       z = x & y;
  5353.       setSR (z, 0);
  5354.       putRc (instr, z);
  5355.       pc += 4;
  5356.       break;
  5357.  
  5358.     /***  and      Ra,data16,Rc  ***/
  5359.     case 136:
  5360.       x = getRa (instr);
  5361.       y = getData16 (instr);
  5362.       z = x & y;
  5363.       setSR (z, 0);
  5364.       putRc (instr, z);
  5365.       pc += 4;
  5366.       break;
  5367.  
  5368.     /***  andn     Ra,Rb,Rc  ***/
  5369.     case 105:
  5370.       x = getRa (instr);
  5371.       y = getRb (instr);
  5372.       z = x & ~y;
  5373.       setSR (z, 0);
  5374.       putRc (instr, z);
  5375.       pc += 4;
  5376.       break;
  5377.  
  5378.     /***  andn     Ra,data16,Rc  ***/
  5379.     case 137:
  5380.       x = getRa (instr);
  5381.       y = getData16 (instr);
  5382.       z = x & ~y;
  5383.       setSR (z, 0);
  5384.       putRc (instr, z);
  5385.       pc += 4;
  5386.       break;
  5387.  
  5388.     /***  xor      Ra,Rb,Rc  ***/
  5389.     case 106:
  5390.       x = getRa (instr);
  5391.       y = getRb (instr);
  5392.       z = x ^ y;
  5393.       setSR (z, 0);
  5394.       putRc (instr, z);
  5395.       pc += 4;
  5396.       break;
  5397.  
  5398.     /***  xor      Ra,data16,Rc  ***/
  5399.     case 138:
  5400.       x = getRa (instr);
  5401.       y = getData16 (instr);
  5402.       z = x ^ y;
  5403.       setSR (z, 0);
  5404.       putRc (instr, z);
  5405.       pc += 4;
  5406.       break;
  5407.  
  5408.     /***  rem      Ra,Rb,Rc  ***/
  5409.     case 115:
  5410.       x = getRa (instr);
  5411.       y = getRb (instr);
  5412.       if (y == 0) {
  5413.         interruptsSignaled |= ARITHMETIC_EXCEPTION;
  5414.         break;
  5415.       }
  5416.       if ((x == MIN) && (y == -1)) {
  5417.         setSR (0, 1);
  5418.         putRc (instr, 0);
  5419.         pc += 4;
  5420.         break;
  5421.       }
  5422.       divide (x, y);
  5423.       setSR (r, 0);
  5424.       putRc (instr, r);
  5425.       pc += 4;
  5426.       break;
  5427.  
  5428.     /***  rem      Ra,data16,Rc  ***/
  5429.     case 149:
  5430.       x = getRa (instr);
  5431.       y = getData16 (instr);
  5432.       if (y == 0) {
  5433.         interruptsSignaled |= ARITHMETIC_EXCEPTION;
  5434.         break;
  5435.       }
  5436.       if ((x == MIN) && (y == -1)) {
  5437.         setSR (0, 1);
  5438.         putRc (instr, 0);
  5439.         pc += 4;
  5440.         break;
  5441.       }
  5442.       divide (x, y);
  5443.       setSR (r, 0);
  5444.       putRc (instr, r);
  5445.       pc += 4;
  5446.       break;
  5447.  
  5448.     /***  load     [Ra+Rb],Rc  ***/
  5449.     case 107:
  5450.       x = getRa (instr);
  5451.       y = getRb (instr);
  5452.       z = x + y;
  5453.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  5454.       physAddr = translate (z, 1, 0, 1);
  5455.       if (translateCausedException) {
  5456.         break;
  5457.       }
  5458.       putRc (instr, getPhysicalWord (physAddr));
  5459.       pc += 4;
  5460.       break;
  5461.  
  5462.     /***  load     [Ra+data16],Rc  ***/
  5463.     case 139:
  5464.       x = getRa (instr);
  5465.       y = getData16 (instr);
  5466.       z = x + y;
  5467.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  5468.       physAddr = translate (z, 1, 0, 1);
  5469.       if (translateCausedException) {
  5470.         break;
  5471.       }
  5472.       putRc (instr, getPhysicalWord (physAddr));
  5473.       pc += 4;
  5474.       break;
  5475.  
  5476.     /***  loadb    [Ra+Rb],Rc  ***/
  5477.     case 108:
  5478.       x = getRa (instr);
  5479.       y = getRb (instr);
  5480.       z = x + y;
  5481.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  5482.       physAddr = translate (z&0xfffffffc, 1, 0, 1);
  5483.       if (translateCausedException) {
  5484.         break;
  5485.       }
  5486.       word = getPhysicalWord (physAddr);
  5487.       /* Isolate the byte in the word and shift to lower-order 8 bits. */
  5488.       word = word >> (24 - ((z & 0x00000003) << 3));
  5489.       word &= 0x000000ff;
  5490.       putRc (instr, word);
  5491.       pc += 4;
  5492.       break;
  5493.  
  5494.     /***  loadb    [Ra+data16],Rc  ***/
  5495.     case 140:
  5496.       x = getRa (instr);
  5497.       y = getData16 (instr);
  5498.       z = x + y;
  5499.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  5500.       physAddr = translate (z&0xfffffffc, 1, 0, 1);
  5501.       if (translateCausedException) {
  5502.         break;
  5503.       }
  5504.       word = getPhysicalWord (physAddr);
  5505.       /* Isolate the byte in the word and shift to lower-order 8 bits. */
  5506.       word = word >> (24 - ((z & 0x00000003) << 3));
  5507.       word &= 0x000000ff;
  5508.       putRc (instr, word);
  5509.       pc += 4;
  5510.       break;
  5511.  
  5512.     /***  loadv    [Ra+Rb],Rc  ***/
  5513.     case 109:
  5514.       if (!statusS) {
  5515.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  5516.         break;
  5517.       }
  5518.       x = getRa (instr);
  5519.       y = getRb (instr);
  5520.       z = x + y;
  5521.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  5522.       saveP = statusP;
  5523.       statusP = 1;
  5524.       physAddr = translate (z, 1, 0, 1);
  5525.       statusP = saveP;
  5526.       if (translateCausedException) {
  5527.         break;
  5528.       }
  5529.       putRc (instr, getPhysicalWord (physAddr));
  5530.       pc += 4;
  5531.       break;
  5532.  
  5533.     /***  loadv    [Ra+data16],Rc  ***/
  5534.     case 141:
  5535.       if (!statusS) {
  5536.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  5537.         break;
  5538.       }
  5539.       x = getRa (instr);
  5540.       y = getData16 (instr);
  5541.       z = x + y;
  5542.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  5543.       saveP = statusP;
  5544.       statusP = 1;
  5545.       physAddr = translate (z, 1, 0, 1);
  5546.       statusP = saveP;
  5547.       if (translateCausedException) {
  5548.         break;
  5549.       }
  5550.       putRc (instr, getPhysicalWord (physAddr));
  5551.       pc += 4;
  5552.       break;
  5553.  
  5554.     /***  loadbv   [Ra+Rb],Rc  ***/
  5555.     case 110:
  5556.       if (!statusS) {
  5557.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  5558.         break;
  5559.       }
  5560.       x = getRa (instr);
  5561.       y = getRb (instr);
  5562.       z = x + y;
  5563.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  5564.       saveP = statusP;
  5565.       statusP = 1;
  5566.       physAddr = translate (z&0xfffffffc, 1, 0, 1);
  5567.       statusP = saveP;
  5568.       if (translateCausedException) {
  5569.         break;
  5570.       }
  5571.       word = getPhysicalWord (physAddr);
  5572.       /* Isolate the byte in the word and shift to lower-order 8 bits. */
  5573.       word = word >> (24 - ((z & 0x00000003) << 3));
  5574.       word &= 0x000000ff;
  5575.       putRc (instr, word);
  5576.       pc += 4;
  5577.       break;
  5578.  
  5579.     /***  loadbv   [Ra+data16],Rc  ***/
  5580.     case 142:
  5581.       if (!statusS) {
  5582.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  5583.         break;
  5584.       }
  5585.       x = getRa (instr);
  5586.       y = getData16 (instr);
  5587.       z = x + y;
  5588.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  5589.       saveP = statusP;
  5590.       statusP = 1;
  5591.       physAddr = translate (z&0xfffffffc, 1, 0, 1);
  5592.       statusP = saveP;
  5593.       if (translateCausedException) {
  5594.         break;
  5595.       }
  5596.       word = getPhysicalWord (physAddr);
  5597.       /* Isolate the byte in the word and shift to lower-order 8 bits. */
  5598.       word = word >> (24 - ((z & 0x00000003) << 3));
  5599.       word &= 0x000000ff;
  5600.       putRc (instr, word);
  5601.       pc += 4;
  5602.       break;
  5603.  
  5604.     /***  store    Rc,[Ra+Rb]  ***/
  5605.     case 111:
  5606.       x = getRa (instr);
  5607.       y = getRb (instr);
  5608.       z = x + y;
  5609.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  5610.       physAddr = translate (z, 0, 0, 1);
  5611.       if (translateCausedException) {
  5612.         break;
  5613.       }
  5614.       putPhysicalWord (physAddr, getRc (instr));
  5615.       pc += 4;
  5616.       break;
  5617.  
  5618.     /***  store    Rc,[Ra+data16]  ***/
  5619.     case 143:
  5620.       x = getRa (instr);
  5621.       y = getData16 (instr);
  5622.       z = x + y;
  5623.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  5624.       physAddr = translate (z, 0, 0, 1);
  5625.       if (translateCausedException) {
  5626.         break;
  5627.       }
  5628.       putPhysicalWord (physAddr, getRc (instr));
  5629.       pc += 4;
  5630.       break;
  5631.  
  5632.     /***  storeb   Rc,[Ra+Rb]  ***/
  5633.     case 112:
  5634.       x = getRa (instr);
  5635.       y = getRb (instr);
  5636.       z = x + y;
  5637.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  5638.       physAddr = translate (z&0xfffffffc, 0, 0, 1);
  5639.       if (translateCausedException) {
  5640.         break;
  5641.       }
  5642.       word = getPhysicalWordAndLock (physAddr);
  5643.       /* Get the low-order byte from register C; then shift it to proper
  5644.          place in the word; then "or" it into the word. */
  5645.       i = getRc (instr);
  5646.       if (z%4 == 0) {
  5647.         word = (word & 0x00ffffff) | ((i & 0x000000ff) << 24);
  5648.       } else if (z%4 == 1) {
  5649.         word = (word & 0xff00ffff) | ((i & 0x000000ff) << 16);
  5650.       } else if (z%4 == 2) {
  5651.         word = (word & 0xffff00ff) | ((i & 0x000000ff) << 8);
  5652.       } else if (z%4 == 3) {
  5653.         word = (word & 0xffffff00) | (i & 0x000000ff);
  5654.       }
  5655.       putPhysicalWordAndRelease (physAddr, word);
  5656.       pc += 4;
  5657.       break;
  5658.  
  5659.     /***  storeb   Rc,[Ra+data16]  ***/
  5660.     case 144:
  5661.       x = getRa (instr);
  5662.       y = getData16 (instr);
  5663.       z = x + y;
  5664.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  5665.       physAddr = translate (z&0xfffffffc, 0, 0, 1);
  5666.       if (translateCausedException) {
  5667.         break;
  5668.       }
  5669.       word = getPhysicalWordAndLock (physAddr);
  5670.       /* Get the low-order byte from register C; then shift it to proper
  5671.          place in the word; then "or" it into the word. */
  5672.       i = getRc (instr);
  5673.       if (z%4 == 0) {
  5674.         word = (word & 0x00ffffff) | ((i & 0x000000ff) << 24);
  5675.       } else if (z%4 == 1) {
  5676.         word = (word & 0xff00ffff) | ((i & 0x000000ff) << 16);
  5677.       } else if (z%4 == 2) {
  5678.         word = (word & 0xffff00ff) | ((i & 0x000000ff) << 8);
  5679.       } else if (z%4 == 3) {
  5680.         word = (word & 0xffffff00) | (i & 0x000000ff);
  5681.       }
  5682.       putPhysicalWordAndRelease (physAddr, word);
  5683.       pc += 4;
  5684.       break;
  5685.  
  5686.     /***  storev   Rc,[Ra+Rb]  ***/
  5687.     case 113:
  5688.       if (!statusS) {
  5689.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  5690.         break;
  5691.       }
  5692.       x = getRa (instr);
  5693.       y = getRb (instr);
  5694.       z = x + y;
  5695.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  5696.       saveP = statusP;
  5697.       statusP = 1;
  5698.       physAddr = translate (z, 0, 0, 1);
  5699.       statusP = saveP;
  5700.       if (translateCausedException) {
  5701.         break;
  5702.       }
  5703.       putPhysicalWord (physAddr, getRc (instr));
  5704.       pc += 4;
  5705.       pc += 4;
  5706.       break;
  5707.  
  5708.     /***  storev   Rc,[Ra+data16]  ***/
  5709.     case 145:
  5710.       if (!statusS) {
  5711.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  5712.         break;
  5713.       }
  5714.       x = getRa (instr);
  5715.       y = getData16 (instr);
  5716.       z = x + y;
  5717.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  5718.       saveP = statusP;
  5719.       statusP = 1;
  5720.       physAddr = translate (z, 0, 0, 1);
  5721.       statusP = saveP;
  5722.       if (translateCausedException) {
  5723.         break;
  5724.       }
  5725.       putPhysicalWord (physAddr, getRc (instr));
  5726.       pc += 4;
  5727.       break;
  5728.  
  5729.     /***  storebv  Rc,[Ra+Rb]  ***/
  5730.     case 114:
  5731.       if (!statusS) {
  5732.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  5733.         break;
  5734.       }
  5735.       x = getRa (instr);
  5736.       y = getRb (instr);
  5737.       z = x + y;
  5738.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  5739.       saveP = statusP;
  5740.       statusP = 1;
  5741.       physAddr = translate (z&0xfffffffc, 0, 0, 1);
  5742.       statusP = saveP;
  5743.       if (translateCausedException) {
  5744.         break;
  5745.       }
  5746.       word = getPhysicalWordAndLock (physAddr);
  5747.       /* Get the low-order byte from register C; then shift it to proper
  5748.          place in the word; then "or" it into the word. */
  5749.       i = getRc (instr);
  5750.       if (z%4 == 0) {
  5751.         word = (word & 0x00ffffff) | ((i & 0x000000ff) << 24);
  5752.       } else if (z%4 == 1) {
  5753.         word = (word & 0xff00ffff) | ((i & 0x000000ff) << 16);
  5754.       } else if (z%4 == 2) {
  5755.         word = (word & 0xffff00ff) | ((i & 0x000000ff) << 8);
  5756.       } else if (z%4 == 3) {
  5757.         word = (word & 0xffffff00) | (i & 0x000000ff);
  5758.       }
  5759.       putPhysicalWordAndRelease (physAddr, word);
  5760.       pc += 4;
  5761.       break;
  5762.  
  5763.     /***  storebv  Rc,[Ra+data16]  ***/
  5764.     case 146:
  5765.       if (!statusS) {
  5766.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  5767.         break;
  5768.       }
  5769.       x = getRa (instr);
  5770.       y = getData16 (instr);
  5771.       z = x + y;
  5772.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  5773.       saveP = statusP;
  5774.       statusP = 1;
  5775.       physAddr = translate (z&0xfffffffc, 0, 0, 1);
  5776.       statusP = saveP;
  5777.       if (translateCausedException) {
  5778.         break;
  5779.       }
  5780.       word = getPhysicalWordAndLock (physAddr);
  5781.       /* Get the low-order byte from register C; then shift it to proper
  5782.          place in the word; then "or" it into the word. */
  5783.       i = getRc (instr);
  5784.       if (z%4 == 0) {
  5785.         word = (word & 0x00ffffff) | ((i & 0x000000ff) << 24);
  5786.       } else if (z%4 == 1) {
  5787.         word = (word & 0xff00ffff) | ((i & 0x000000ff) << 16);
  5788.       } else if (z%4 == 2) {
  5789.         word = (word & 0xffff00ff) | ((i & 0x000000ff) << 8);
  5790.       } else if (z%4 == 3) {
  5791.         word = (word & 0xffffff00) | (i & 0x000000ff);
  5792.       }
  5793.       putPhysicalWordAndRelease (physAddr, word);
  5794.       pc += 4;
  5795.       break;
  5796.  
  5797.     /***  call     Ra+Rc  ***/
  5798.     case 64:
  5799.       x = getRa (instr);
  5800.       y = getRc (instr);
  5801.       z = x + y;
  5802.       if (z % 4 != 0) {
  5803.         interruptsSignaled |= ALIGNMENT_EXCEPTION;
  5804.         break;
  5805.       }
  5806.       pushOntoStack (15, pc+4);
  5807.       if (translateCausedException) {
  5808.         break;
  5809.       }
  5810.       pc = z;
  5811.       break;
  5812.  
  5813.     /***  call     data24  ***/
  5814.     case 160:
  5815.       z = getData24 (instr);
  5816.       if (z % 4 != 0) {
  5817.         interruptsSignaled |= ALIGNMENT_EXCEPTION;
  5818.         break; 
  5819.       }
  5820.       pushOntoStack (15, pc+4);
  5821.       if (translateCausedException) {
  5822.         break;
  5823.       }
  5824.       pc += z;
  5825.       break;
  5826.  
  5827.     /***  jmp      Ra+Rc  ***/
  5828.     case 65:
  5829.       x = getRa (instr);
  5830.       y = getRc (instr);
  5831.       z = x + y;
  5832.       if (z % 4 != 0) {
  5833.         interruptsSignaled |= ALIGNMENT_EXCEPTION;
  5834.         break;
  5835.       }
  5836.       pc = z;
  5837.       break;
  5838.  
  5839.     /***  jmp      data24  ***/
  5840.     case 161:
  5841.       z = getData24 (instr);
  5842.       if (z % 4 != 0) {
  5843.         interruptsSignaled |= ALIGNMENT_EXCEPTION;
  5844.         break;
  5845.       }
  5846.       pc += z;
  5847.       break;
  5848.  
  5849.     /***  be       Ra+Rc  ***/
  5850.     case 66:
  5851.       jumpIfTrueRaRb (statusZ, instr);
  5852.       break;
  5853.  
  5854.     /***  be       data24  ***/
  5855.     case 162:
  5856.       jumpIfTrueData24 (statusZ, instr);
  5857.       break;
  5858.  
  5859.     /***  bne      Ra+Rc  ***/
  5860.     case 67:
  5861.       jumpIfTrueRaRb (!statusZ, instr);
  5862.       break;
  5863.  
  5864.     /***  bne      data24  ***/
  5865.     case 163:
  5866.       jumpIfTrueData24 (!statusZ, instr);
  5867.       break;
  5868.  
  5869.     /***  bl       Ra+Rc  ***/
  5870.     case 68:
  5871.       jumpIfTrueRaRb ((statusN ^ statusV), instr);
  5872.       break;
  5873.  
  5874.     /***  bl       data24  ***/
  5875.     case 164:
  5876.       jumpIfTrueData24 ((statusN ^ statusV), instr);
  5877.       break;
  5878.  
  5879.     /***  ble      Ra+Rc  ***/
  5880.     case 69:
  5881.       jumpIfTrueRaRb ((statusZ | (statusN ^ statusV)), instr);
  5882.       break;
  5883.  
  5884.     /***  ble      data24  ***/
  5885.     case 165:
  5886.       jumpIfTrueData24 ((statusZ | (statusN ^ statusV)), instr);
  5887.       break;
  5888.  
  5889.     /***  bg       Ra+Rc  ***/
  5890.     case 70:
  5891.       jumpIfTrueRaRb (!(statusZ | (statusN ^ statusV)), instr);
  5892.       break;
  5893.  
  5894.     /***  bg       data24  ***/
  5895.     case 166:
  5896.       jumpIfTrueData24 (!(statusZ | (statusN ^ statusV)), instr);
  5897.       break;
  5898.  
  5899.     /***  bge      Ra+Rc  ***/
  5900.     case 71:
  5901.       jumpIfTrueRaRb (!(statusN ^ statusV), instr);
  5902.       break;
  5903.  
  5904.     /***  bge      data24  ***/
  5905.     case 167:
  5906.       jumpIfTrueData24 (!(statusN ^ statusV), instr);
  5907.       break;
  5908.  
  5909. /***
  5910.     case 72:
  5911.     case 73:
  5912.     case 168:
  5913.     case 169:
  5914. ***/
  5915.  
  5916.     /***  bvs      Ra+Rc  ***/
  5917.     case 74:
  5918.       jumpIfTrueRaRb (statusV, instr);
  5919.       break;
  5920.  
  5921.     /***  bvs      data24  ***/
  5922.     case 170:
  5923.       jumpIfTrueData24 (statusV, instr);
  5924.       break;
  5925.  
  5926.     /***  bvc      Ra+Rc  ***/
  5927.     case 75:
  5928.       jumpIfTrueRaRb (!statusV, instr);
  5929.       break;
  5930.  
  5931.     /***  bvc      data24  ***/
  5932.     case 171:
  5933.       jumpIfTrueData24 (!statusV, instr);
  5934.       break;
  5935.  
  5936.     /***  bns      Ra+Rc  ***/
  5937.     case 76:
  5938.       jumpIfTrueRaRb (statusN, instr);
  5939.       break;
  5940.  
  5941.     /***  bns      data24  ***/
  5942.     case 172:
  5943.       jumpIfTrueData24 (statusN, instr);
  5944.       break;
  5945.  
  5946.     /***  bnc      Ra+Rc  ***/
  5947.     case 77:
  5948.       jumpIfTrueRaRb (!statusN, instr);
  5949.       break;
  5950.  
  5951.     /***  bnc      data24  ***/
  5952.     case 173:
  5953.       jumpIfTrueData24 (!statusN, instr);
  5954.       break;
  5955.  
  5956.     /***  bss      Ra+Rc  ***/
  5957.     case 78:
  5958.       jumpIfTrueRaRb (statusS, instr);
  5959.       break;
  5960.  
  5961.     /***  bss      data24  ***/
  5962.     case 174:
  5963.       jumpIfTrueData24 (statusS, instr);
  5964.       break;
  5965.  
  5966.     /***  bsc      Ra+Rc  ***/
  5967.     case 79:
  5968.       jumpIfTrueRaRb (!statusS, instr);
  5969.       break;
  5970.  
  5971.     /***  bsc      data24  ***/
  5972.     case 175:
  5973.       jumpIfTrueData24 (!statusS, instr);
  5974.       break;
  5975.  
  5976.     /***  bis      Ra+Rc  ***/
  5977.     case 80:
  5978.       jumpIfTrueRaRb (statusI, instr);
  5979.       break;
  5980.  
  5981.     /***  bis      data24  ***/
  5982.     case 176:
  5983.       jumpIfTrueData24 (statusI, instr);
  5984.       break;
  5985.  
  5986.     /***  bic      Ra+Rc  ***/
  5987.     case 81:
  5988.       jumpIfTrueRaRb (!statusI, instr);
  5989.       break;
  5990.  
  5991.     /***  bic      data24  ***/
  5992.     case 177:
  5993.       jumpIfTrueData24 (!statusI, instr);
  5994.       break;
  5995.  
  5996.     /***  bps      Ra+Rc  ***/
  5997.     case 82:
  5998.       jumpIfTrueRaRb (statusP, instr);
  5999.       break;
  6000.  
  6001.     /***  bps      data24  ***/
  6002.     case 178:
  6003.       jumpIfTrueData24 (statusP, instr);
  6004.       break;
  6005.  
  6006.     /***  bpc      Ra+Rc  ***/
  6007.     case 83:
  6008.       jumpIfTrueRaRb (!statusP, instr);
  6009.       break;
  6010.  
  6011.     /***  bpc      data24  ***/
  6012.     case 179:
  6013.       jumpIfTrueData24 (!statusP, instr);
  6014.       break;
  6015.  
  6016.     /***  push     Rc,[--Ra]  ***/
  6017.     case 84:
  6018.       regNumber = (instr >> 16) & 0x0000000f;
  6019.       z = getRc (instr);
  6020.       pushOntoStack (regNumber, z);
  6021.       if (translateCausedException) {
  6022.         break;
  6023.       }
  6024.       pc += 4;
  6025.       break;
  6026.  
  6027.     /***  pop      [Ra++],Rc  ***/
  6028.     case 85:
  6029.       regNumber = (instr >> 16) & 0x0000000f;
  6030.       z = popFromStack (regNumber);
  6031.       if (translateCausedException) {
  6032.         break;
  6033.       }
  6034.       putRc (instr, z);
  6035.       pc += 4;
  6036.       break;
  6037.  
  6038.     /***  sethi    data16,Rc  ***/
  6039.     case 192:
  6040.       z = getData16 (instr);
  6041.       regNumber = (instr >> 20) & 0x0000000f;
  6042.       if (regNumber != 0) {
  6043.         if (statusS) {
  6044.           systemRegisters [regNumber] =
  6045.                (systemRegisters [regNumber] & 0x0000ffff) | (z << 16);
  6046.         } else {
  6047.           userRegisters [regNumber] =
  6048.                (userRegisters [regNumber] & 0x0000ffff) | (z << 16);
  6049.         }
  6050.       }
  6051.       pc += 4;
  6052.       break;
  6053.  
  6054.     /***  setlo    data16,Rc  ***/
  6055.     case 193:
  6056.       z = getData16 (instr);
  6057.       regNumber = (instr >> 20) & 0x0000000f;
  6058.       if (regNumber != 0) {
  6059.         if (statusS) {
  6060.           systemRegisters [regNumber] =
  6061.                (systemRegisters [regNumber] & 0xffff0000) | (z & 0x0000ffff);
  6062.         } else {
  6063.           userRegisters [regNumber] =
  6064.                (userRegisters [regNumber] & 0xffff0000) | (z & 0x0000ffff);
  6065.         }
  6066.       }
  6067.       pc += 4;
  6068.       break;
  6069.  
  6070.     /***  ldaddr   data16,Rc  ***/
  6071.     case 194:
  6072.       x = getData16 (instr);
  6073.       y = pc + x;
  6074.       putRc (instr, y);
  6075.       pc += 4;
  6076.       break;
  6077.  
  6078.     /***  syscall  Rc+data16  ***/
  6079.     case 195:
  6080.       x = getRc (instr);
  6081.       y = getData16 (instr);
  6082.       interruptsSignaled |= SYSCALL_TRAP;
  6083.       systemTrapNumber = x+y;
  6084.       pc += 4;
  6085.       break;
  6086.  
  6087.     /***  nop  ***/
  6088.     case 0:
  6089.       printf ("WARNING: Executing a NOP instruction at address 0x%08X - Suspending execution!\r\n", pc);
  6090.       controlCPressed = 1;
  6091.       pc += 4;
  6092.       break;
  6093.  
  6094.     /***  wait  ***/
  6095.     case 1:
  6096.  
  6097.       // printf ("h");
  6098.       if (!statusS) {
  6099.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6100.         break;
  6101.       }
  6102.  
  6103.       /* Enable interrupts. */
  6104.       statusI = 1;
  6105.  
  6106.       /* Compute the time of the next event; we will ignore timer events... */
  6107.       i = timeOfNextDiskEvent;
  6108.  
  6109.       if (timeOfNextSerialOutEvent < i) {
  6110.         i = timeOfNextSerialOutEvent;
  6111.       }
  6112.  
  6113.       /* If there is something in the type-ahead buffer... */
  6114.       if (typeAheadBufferCount > 0) {
  6115.         doSerialInEvent (1);
  6116.         if (timeOfNextSerialInEvent < i) {
  6117.           i = timeOfNextSerialInEvent;
  6118.         }
  6119.  
  6120.       /* Else, if serial input comes from a file... */
  6121.       } else if (termInputFile != stdin) {
  6122.         /* Then get more input from the file, if any... */
  6123.         if (! feof (termInputFile)) {
  6124.           doSerialInEvent (1);
  6125.           if (timeOfNextSerialInEvent < i) {
  6126.             i = timeOfNextSerialInEvent;
  6127.           }
  6128.         }
  6129.  
  6130.       /* Else input comes from stdin... */
  6131.       } else {
  6132.         /* See if there is a character available on the input... */
  6133.         doSerialInEvent (0);
  6134.         if (termInCharAvail && timeOfNextSerialInEvent < i) {
  6135.           i = timeOfNextSerialInEvent;
  6136.         }
  6137.       }
  6138.  
  6139.       /* If we still haven't found anything to do
  6140.          and there are no pending interrupts
  6141.          and input is coming from the terminal
  6142.          and the user has asked us to wait instead of terminate...  */
  6143.       if ((i == MAX) &&
  6144.           (getNextInterrupt () == 0) &&
  6145.           (termInputFile == stdin) &&
  6146.           (commandOptionWait)) {
  6147.  
  6148.         /* We are about to wait on user input.  Print a message so the
  6149.            user will know we are waiting (unless we're in raw mode)...  */
  6150.         if (! terminalWantRawProcessing) {
  6151.           fprintf (stderr, "\n\r*****  Execution suspended on 'wait' instruction; waiting for additional user input  *****\n\r");
  6152.         }
  6153.  
  6154.         /* Wait for some input from stdin... */
  6155.         doSerialInEvent (1);
  6156.         if (timeOfNextSerialInEvent < i) {
  6157.           i = timeOfNextSerialInEvent;
  6158.         }
  6159.       }
  6160.  
  6161.       /* If we now have an interrupt that will be processed on the next cycle... */
  6162.       if (getNextInterrupt ()) {
  6163.         /* Do nothing thing; continue execution */
  6164.  
  6165.       /* Else if we have a disk or serial event in the future... */
  6166.       } else if (i != MAX) {
  6167.  
  6168.         /* Move currentTime up to the time of the next event. */
  6169.         timeSpentAsleep += (timeOfNextEvent-1) - currentTime;
  6170.         currentTime = timeOfNextEvent-1;
  6171.  
  6172.       /* Else halt execution. */
  6173.       } else {
  6174.      
  6175.         fprintf (stderr, "\n\r*****  A 'wait' instruction was executed and no more interrupts are scheduled... halting emulation!  *****\n\r\n\r");
  6176.         executionHalted = 1;
  6177.       }
  6178.  
  6179.       /* Increment PC; executing go after a wait will resume execution. */
  6180.       pc += 4;
  6181.       break;
  6182.  
  6183.     /***  debug  ***/
  6184.     case 2:
  6185.       /* Stop instruction emulation and print a message.  Advance the
  6186.          PC so that we can easily resume execution. */
  6187.       controlCPressed = 1;
  6188.       turnOffTerminal ();
  6189.       fprintf (stderr, "\n\r****  A 'debug' instruction was encountered  *****\n\r");
  6190.       turnOnTerminal ();
  6191.       pc += 4;
  6192.       break;
  6193.  
  6194.     /***  debug2  ***/
  6195.     case 10:
  6196.       /* Perform a special function.  The function code is in r1. */
  6197.       if (statusS) {
  6198.         x = systemRegisters [1];
  6199.       } else {
  6200.         x = userRegisters [1];
  6201.       }
  6202.       /* Get the value of register r2 (string ptr, integer, or boolean). */
  6203.       if (statusS) {
  6204.         y = systemRegisters [2];
  6205.       } else {
  6206.         y = userRegisters [2];
  6207.       }
  6208.  
  6209.       /* Function = "printInt" */
  6210.       if (x == 1) {
  6211.         printf ("%d", y);
  6212.  
  6213.       /* Function = "printString" */
  6214.       } else if (x == 2) {
  6215.         /* z = string length */
  6216.         if (statusS) {
  6217.           z = systemRegisters [3];
  6218.         } else {
  6219.           z = userRegisters [3];
  6220.         }
  6221.         if ((z < 0) | (z > 100000)) {
  6222.           controlCPressed = 1;
  6223.           fprintf (stderr, "\n\r****  An error occurred during a 'debug2' instruction - invalid string length in print  *****\n\r");
  6224.           z = -1;
  6225.         }
  6226.         while (z > 0) {
  6227.           z--;
  6228.           /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  6229.           physAddr = translate (y&0xfffffffc, 1, 0, 1);
  6230.           if (translateCausedException) {
  6231.             controlCPressed = 1;
  6232.             fprintf (stderr, "\n\r****  An error occurred during a 'debug2' instruction - address exception in print  *****\n\r");
  6233.             break;
  6234.           }
  6235.           word = getPhysicalWord (physAddr);
  6236.           /* Isolate the byte in the word and shift to lower-order 8 bits. */
  6237.           word = word >> (24 - ((y & 0x00000003) << 3));
  6238.           x = word & 0x000000ff;
  6239.           if ((x >= 32) && (x < 127)) {
  6240.             printf ("%c", x);
  6241.           } else if (x == '\t') {
  6242.             printf ("\t");
  6243.           } else if (x == '\n') {
  6244.             printf ("\r\n");
  6245.           } else {
  6246.             printf ("\\x%02X", x);
  6247.           }
  6248.           y++;
  6249.         }
  6250.  
  6251.       /* Function = "printChar" */
  6252.       } else if (x == 3) {
  6253.         if ((y >= 32) && (y < 127)) {
  6254.           printf ("%c", y);
  6255.         } else if (y == '\t') {
  6256.           printf ("\t");
  6257.         } else if (y == '\n') {
  6258.           printf ("\r\n");
  6259.         } else {
  6260.           printf ("\\x%02X", y);
  6261.         }
  6262.  
  6263.       /* Function = "printDouble" */
  6264.       } else if (x == 4) {
  6265.         printDoubleVal (floatRegisters[0]);
  6266.  
  6267.       /* Function = "printBool" */
  6268.       } else if (x == 5) {
  6269.         if (y == 0) {
  6270.           printf ("FALSE");
  6271.         } else if (y == 1) {
  6272.           printf ("TRUE");
  6273.         } else {
  6274.           controlCPressed = 1;
  6275.           fprintf (stderr, "\n\r****  An error occurred during a 'debug2' instruction - invalid boolean value in printBool  *****\n\r");
  6276.         }
  6277.  
  6278.       /* Function = "printHex" */
  6279.       } else if (x == 6) {
  6280.         printf ("0x");
  6281.         putlong (y);
  6282.  
  6283.       /* Function = ERROR */
  6284.       } else {
  6285.         controlCPressed = 1;
  6286.         fprintf (stderr, "\n\r****  An error occurred during a 'debug2' instruction - invalid function code  *****\n\r");
  6287.       }
  6288.  
  6289.       fflush (stdout);
  6290.       pc += 4;
  6291.       break;
  6292.  
  6293.     /***  cleari  ***/
  6294.     case 3:
  6295.       if (!statusS) {
  6296.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6297.         break;
  6298.       }
  6299.       statusI = 0;
  6300.       pc += 4;
  6301.       break;
  6302.  
  6303.     /***  seti  ***/
  6304.     case 4:
  6305.       if (!statusS) {
  6306.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6307.         break;
  6308.       }
  6309.       statusI = 1;
  6310.       pc += 4;
  6311.       break;
  6312.  
  6313.     /***  clearp  ***/
  6314.     case 5:
  6315.       if (!statusS) {
  6316.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6317.         break;
  6318.       }
  6319.       statusP = 0;
  6320.       pc += 4;
  6321.       break;
  6322.  
  6323.     /***  setp  ***/
  6324.     case 6:
  6325.       if (!statusS) {
  6326.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6327.         break;
  6328.       }
  6329.       statusP = 1;
  6330.       pc += 4;
  6331.       break;
  6332.  
  6333.     /***  clears  ***/
  6334.     case 7:
  6335.       if (!statusS) {
  6336.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6337.         break;
  6338.       }
  6339.       statusS = 0;
  6340.       pc += 4;
  6341.       break;
  6342.  
  6343.     /***  reti  ***/
  6344.     case 8:
  6345.       if (!statusS) {
  6346.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6347.         break;
  6348.       }
  6349.       x = popFromStack (15);
  6350.       if (translateCausedException) {
  6351.         break;
  6352.       }
  6353.       y = popFromStack (15);
  6354.       if (translateCausedException) {
  6355.         break;
  6356.       }
  6357.       z = popFromStack (15);
  6358.       if (translateCausedException) {
  6359.         break;
  6360.       }
  6361.       setStatusFromWord (y);
  6362.       pc = x;
  6363.       break;
  6364.  
  6365.     /***  ret  ***/
  6366.     case 9:
  6367.       z = popFromStack (15);
  6368.       if (translateCausedException) {
  6369.         break;
  6370.       }
  6371.       pc = z;
  6372.       break;
  6373.  
  6374.     /***  tset     [Ra],Rc  ***/
  6375.     case 88:
  6376.       x = getRa (instr);
  6377.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  6378.       physAddr = translate (x, 0, 0, 1);
  6379.       if (translateCausedException) {
  6380.         break;
  6381.       }
  6382.       putRc (instr, getPhysicalWordAndLock (physAddr));
  6383.       putPhysicalWordAndRelease (physAddr, 0x00000001);
  6384.       pc += 4;
  6385.       break;
  6386.  
  6387.     /***  readu    Rc,Ra  ***/
  6388.     case 86:
  6389.       if (!statusS) {
  6390.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6391.         break;
  6392.       }
  6393.       regC = (instr >> 20) & 0x0000000f;
  6394.       regA = (instr >> 16) & 0x0000000f;
  6395.       pc += 4;
  6396.       if (regA == 0) break;
  6397.       systemRegisters [regA] = userRegisters [regC];
  6398.       break;
  6399.  
  6400.     /***  readu    Rc,[Ra+data16]  ***/
  6401.     case 147:
  6402.       if (!statusS) {
  6403.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6404.         break;
  6405.       }
  6406.       x = getRa (instr);
  6407.       y = getData16 (instr);
  6408.       z = x + y;
  6409.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  6410.       physAddr = translate (z, 0, 0, 1);
  6411.       if (translateCausedException) {
  6412.         break;
  6413.       }
  6414.       regC = (instr >> 20) & 0x0000000f;
  6415.       putPhysicalWord (physAddr, userRegisters [regC]);
  6416.       pc += 4;
  6417.       break;
  6418.  
  6419.     /***  writeu   Ra,Rc  ***/
  6420.     case 87:
  6421.       if (!statusS) {
  6422.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6423.         break;
  6424.       }
  6425.       regC = (instr >> 20) & 0x0000000f;
  6426.       regA = (instr >> 16) & 0x0000000f;
  6427.       pc += 4;
  6428.       if (regC == 0) break;
  6429.       userRegisters [regC] = systemRegisters [regA];
  6430.       break;
  6431.  
  6432.     /***  writeu   [Ra+data16],Rc  ***/
  6433.     case 148:
  6434.       if (!statusS) {
  6435.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6436.         break;
  6437.       }
  6438.       x = getRa (instr);
  6439.       y = getData16 (instr);
  6440.       z = x + y;
  6441.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  6442.       physAddr = translate (z, 1, 0, 1);
  6443.       if (translateCausedException) {
  6444.         break;
  6445.       }
  6446.       regC = (instr >> 20) & 0x0000000f;
  6447.       pc += 4;
  6448.       if (regC == 0) break;
  6449.       userRegisters [regC] = getPhysicalWord (physAddr);
  6450.       break;
  6451.  
  6452.     /***  ldptbr   Rc  ***/
  6453.     case 32:
  6454.       if (!statusS) {
  6455.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6456.         break;
  6457.       }
  6458.       x = getRc (instr);
  6459.       if (x % 4 != 0) {
  6460.         interruptsSignaled |= ALIGNMENT_EXCEPTION;
  6461.         break;
  6462.       }
  6463.       ptbr = x;
  6464.       pc += 4;
  6465.       break;
  6466.  
  6467.     /***  ldptlr   Rc  ***/
  6468.     case 33:
  6469.       if (!statusS) {
  6470.         interruptsSignaled |= PRIVILEGED_INSTRUCTION;
  6471.         break;
  6472.       }
  6473.       x = getRc (instr);
  6474.       if (x % 4 != 0) {
  6475.         interruptsSignaled |= ALIGNMENT_EXCEPTION;
  6476.         break;
  6477.       }
  6478.       ptlr = x;
  6479.       pc += 4;
  6480.       break;
  6481.  
  6482.     /***  ftoi      FRa,Rc  ***/
  6483.     case 89:
  6484.       d1 = getFRa (instr);
  6485.       i = d1;
  6486.       putRc (instr, i);
  6487.       pc += 4;
  6488.       break;
  6489.  
  6490.     /***  itof      Ra,FRc  ***/
  6491.     case 90:
  6492.       i = getRa (instr);
  6493.       d1 = i;
  6494.       putFRc (instr, d1);
  6495.       pc += 4;
  6496.       break;
  6497.  
  6498.     /***  fadd      FRa,FRb,FRc  ***/
  6499.     case 116:
  6500.       d1 = getFRa (instr);
  6501.       d2 = getFRb (instr);
  6502.       d3 = d1 + d2;
  6503.       putFRc (instr, d3);
  6504.       pc += 4;
  6505.       break;
  6506.  
  6507.     /***  fsub      FRa,FRb,FRc  ***/
  6508.     case 117:
  6509.       d1 = getFRa (instr);
  6510.       d2 = getFRb (instr);
  6511.       d3 = d1 - d2;
  6512.       putFRc (instr, d3);
  6513.       pc += 4;
  6514.       break;
  6515.  
  6516.     /***  fmul      FRa,FRb,FRc  ***/
  6517.     case 118:
  6518.       d1 = getFRa (instr);
  6519.       d2 = getFRb (instr);
  6520.       d3 = d1 * d2;
  6521.       putFRc (instr, d3);
  6522.       pc += 4;
  6523.       break;
  6524.  
  6525.     /***  fdiv      FRa,FRb,FRc  ***/
  6526.     case 119:
  6527.       d1 = getFRa (instr);
  6528.       d2 = getFRb (instr);
  6529.       d3 = d1 / d2;
  6530.       putFRc (instr, d3);
  6531.       pc += 4;
  6532.       break;
  6533.  
  6534.     /***  fcmp      FRa,FRc  ***/
  6535.     case 91:
  6536.       d1 = getFRa (instr);
  6537.       d2 = getFRc (instr);
  6538.       statusZ = statusN = statusV = 0;
  6539.       if (d1 == d2) {
  6540.         statusZ = 1;
  6541.       }
  6542.       if (d1 < d2) {
  6543.         statusN = 1;
  6544.       }
  6545.       if (isnan(d1)) {
  6546.         statusV = 1;
  6547.       }
  6548.       if (isnan(d2)) {
  6549.         statusV = 1;
  6550.       }
  6551.       pc += 4;
  6552.       break;
  6553.  
  6554.     /***  fsqrt      FRa,FRc  ***/
  6555.     case 92:
  6556.       d1 = getFRa (instr);
  6557.       d3 = sqrt (d1);
  6558.       putFRc (instr, d3);
  6559.       pc += 4;
  6560.       break;
  6561.  
  6562.     /***  fneg      FRa,FRc  ***/
  6563.     case 93:
  6564.       d1 = getFRa (instr);
  6565.       d3 = -d1;
  6566.       putFRc (instr, d3);
  6567.       pc += 4;
  6568.       break;
  6569.  
  6570.     /***  fabs      FRa,FRc  ***/
  6571.     case 94:
  6572.       d1 = getFRa (instr);
  6573.       d3 = fabs (d1);
  6574.       putFRc (instr, d3);
  6575.       pc += 4;
  6576.       break;
  6577.  
  6578.     /***  fload      [Ra+Rb],FRc  ***/
  6579.     case 120:
  6580.       x = getRa (instr);
  6581.       y = getRb (instr);
  6582.       z = x + y;
  6583.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  6584.       physAddr = translate (z, 1, 0, 1);
  6585.       if (translateCausedException) {
  6586.         break;
  6587.       }
  6588.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  6589.       physAddr2 = translate (z+4, 1, 0, 1);
  6590.       if (translateCausedException) {
  6591.         break;
  6592.       }
  6593.       p = (int *) (& d3);
  6594. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  6595.       *p = getPhysicalWord (physAddr2);
  6596.       *(p+1) = getPhysicalWord (physAddr);
  6597. #else
  6598.       *p = getPhysicalWord (physAddr);
  6599.       *(p+1) = getPhysicalWord (physAddr2);
  6600. #endif
  6601.       putFRc (instr, d3);
  6602.       pc += 4;
  6603.       break;
  6604.  
  6605.     /***  fload      [Ra+data16],FRc  ***/
  6606.     case 150:
  6607.       x = getRa (instr);
  6608.       y = getData16 (instr);
  6609.       z = x + y;
  6610.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  6611.       physAddr = translate (z, 1, 0, 1);
  6612.       if (translateCausedException) {
  6613.         break;
  6614.       }
  6615.       /* Call translate with reading=true, wantPrinting=0, doUpdates=1 */
  6616.       physAddr2 = translate (z+4, 1, 0, 1);
  6617.       if (translateCausedException) {
  6618.         break;
  6619.       }
  6620.       p = (int *) (& d3);
  6621. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  6622.       *p = getPhysicalWord (physAddr2);
  6623.       *(p+1) = getPhysicalWord (physAddr);
  6624. #else
  6625.       *p = getPhysicalWord (physAddr);
  6626.       *(p+1) = getPhysicalWord (physAddr2);
  6627. #endif
  6628.       putFRc (instr, d3);
  6629.       pc += 4;
  6630.       break;
  6631.  
  6632.     /***  fstore      FRc,[Ra+Rb]  ***/
  6633.     case 121:
  6634.       x = getRa (instr);
  6635.       y = getRb (instr);
  6636.       z = x + y;
  6637.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  6638.       physAddr = translate (z, 0, 0, 1);
  6639.       if (translateCausedException) {
  6640.         break;
  6641.       }
  6642.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  6643.       physAddr2 = translate (z+4, 0, 0, 1);
  6644.       if (translateCausedException) {
  6645.         break;
  6646.       }
  6647.       p = (int *) (& d3);
  6648.       d3 = getFRc (instr);
  6649. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  6650.       putPhysicalWord (physAddr2, *p);
  6651.       putPhysicalWord (physAddr, *(p+1));
  6652. #else
  6653.       putPhysicalWord (physAddr, *p);
  6654.       putPhysicalWord (physAddr2, *(p+1));
  6655. #endif
  6656.       pc += 4;
  6657.       break;
  6658.  
  6659.     /***  fstore      FRc,[Ra+data16]  ***/
  6660.     case 151:
  6661.       x = getRa (instr);
  6662.       y = getData16 (instr);
  6663.       z = x + y;
  6664.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  6665.       physAddr = translate (z, 0, 0, 1);
  6666.       if (translateCausedException) {
  6667.         break;
  6668.       }
  6669.       /* Call translate with reading=false, wantPrinting=0, doUpdates=1 */
  6670.       physAddr2 = translate (z+4, 0, 0, 1);
  6671.       if (translateCausedException) {
  6672.         break;
  6673.       }
  6674.       p = (int *) (& d3);
  6675.       d3 = getFRc (instr);
  6676. #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN
  6677.       putPhysicalWord (physAddr2, *p);
  6678.       putPhysicalWord (physAddr, *(p+1));
  6679. #else
  6680.       putPhysicalWord (physAddr, *p);
  6681.       putPhysicalWord (physAddr2, *(p+1));
  6682. #endif
  6683.       pc += 4;
  6684.       break;
  6685.  
  6686.     /***  illegal instruction  ***/
  6687.     default:
  6688.       interruptsSignaled |= ILLEGAL_INSTRUCTION;
  6689.       break;
  6690.   }
  6691. }
  6692.  
  6693.  
  6694.  
  6695. /* getRa (instr)
  6696. ** getRb (instr)
  6697. ** getRc (instr)
  6698. ** putRc (instr, value)
  6699. ** getData16 (instr)
  6700. ** getData24 (instr)
  6701. **
  6702. ** These routine get the value of (put a value to) the indicated register.
  6703. ** The current SystemMode bit will be checked and the data will be
  6704. ** moved from/to either the System or User registers as appropriate.
  6705. ** The "getData16" and "getData24" routines return the literal values
  6706. ** from the instruction after sign-extending the value to 32 bits.
  6707. */
  6708. int getRa (int instr) {
  6709.   int reg = (instr >> 16) & 0x0000000f;
  6710.   if (statusS) {
  6711.     return systemRegisters [reg];
  6712.   } else {
  6713.     return userRegisters [reg];
  6714.   }
  6715. }
  6716.  
  6717. int getRb (int instr) {
  6718.   int reg = (instr >> 12) & 0x0000000f;
  6719.   if (statusS) {
  6720.     return systemRegisters [reg];
  6721.   } else {
  6722.     return userRegisters [reg];
  6723.   }
  6724. }
  6725.  
  6726. int getRc (int instr) {
  6727.   int reg = (instr >> 20) & 0x0000000f;
  6728.   if (statusS) {
  6729.     return systemRegisters [reg];
  6730.   } else {
  6731.     return userRegisters [reg];
  6732.   }
  6733. }
  6734.  
  6735. void putRc (int instr, int value) {
  6736.   int reg = (instr >> 20) & 0x0000000f;
  6737.   if (reg == 0) return;
  6738.   if (statusS) {
  6739.     systemRegisters [reg] = value;
  6740.   } else {
  6741.     userRegisters [reg] = value;
  6742.   }
  6743. }
  6744.  
  6745. int getData16 (int instr) {
  6746.   int i = instr << 16;
  6747.   if (i < 0) {
  6748.     return (instr | 0xffff0000);
  6749.   } else {
  6750.     return (instr & 0x0000ffff);
  6751.   }
  6752. }
  6753.  
  6754. int getData24 (int instr) {
  6755.   int i = instr << 8;
  6756.   if (i < 0) {
  6757.     return (instr | 0xff000000);
  6758.   } else {
  6759.     return (instr & 0x00ffffff);
  6760.   }
  6761. }
  6762.  
  6763.  
  6764.  
  6765. /* getFRa (instr)
  6766. ** getFRb (instr)
  6767. ** getFRc (instr)
  6768. ** putFRc (instr, value)
  6769. **
  6770. ** These routines get the value of (put a value to) the indicated register.
  6771. */
  6772. double getFRa (int instr) {
  6773.   int reg = (instr >> 16) & 0x0000000f;
  6774.   return floatRegisters [reg];
  6775. }
  6776.  
  6777. double getFRb (int instr) {
  6778.   int reg = (instr >> 12) & 0x0000000f;
  6779.   return floatRegisters [reg];
  6780. }
  6781.  
  6782. double getFRc (int instr) {
  6783.   int reg = (instr >> 20) & 0x0000000f;
  6784.   return floatRegisters [reg];
  6785. }
  6786.  
  6787. void putFRc (int instr, double value) {
  6788.   int reg = (instr >> 20) & 0x0000000f;
  6789.   floatRegisters [reg] = value;
  6790. }
  6791.  
  6792.  
  6793.  
  6794. /* buildStatusWord ()
  6795. **
  6796. ** This routine builds and returns a 32-bit word reflecting the status
  6797. ** bits, which are kept in individual variables by this program.
  6798. */
  6799. int buildStatusWord () {
  6800.   int word = 0;
  6801.   if (statusN) {
  6802.     word |= 0x00000001;
  6803.   }
  6804.   if (statusV) {
  6805.     word |= 0x00000002;
  6806.   }
  6807.   if (statusZ) {
  6808.     word |= 0x00000004;
  6809.   }
  6810.   if (statusP) {
  6811.     word |= 0x00000008;
  6812.   }
  6813.   if (statusS) {
  6814.     word |= 0x00000010;
  6815.   }
  6816.   if (statusI) {
  6817.     word |= 0x00000020;
  6818.   }
  6819.   return word;
  6820. }
  6821.  
  6822.  
  6823.  
  6824. /* setStatusFromWord (word)
  6825. **
  6826. ** This routine is passed a 32-bit word reflecting the status register.
  6827. ** It sets the individual status variables used by this program to
  6828. ** record the current status bits.
  6829. */
  6830. void setStatusFromWord (int word) {
  6831.   statusI = 0;
  6832.   statusS = 0;
  6833.   statusP = 0;
  6834.   statusZ = 0;
  6835.   statusV = 0;
  6836.   statusN = 0;
  6837.   if (word & 0x00000020) {
  6838.   }
  6839.   if (word & 0x00000020) {
  6840.     statusI = 1;
  6841.   }
  6842.   if (word & 0x00000010) {
  6843.     statusS = 1;
  6844.   }
  6845.   if (word & 0x00000008) {
  6846.     statusP = 1;
  6847.   }
  6848.   if (word & 0x00000004) {
  6849.     statusZ = 1;
  6850.   }
  6851.   if (word & 0x00000002) {
  6852.     statusV = 1;
  6853.   }
  6854.   if (word & 0x00000001) {
  6855.     statusN = 1;
  6856.   }
  6857.   /* ignore all other bits in the word */
  6858. }
  6859.  
  6860.  
  6861.  
  6862. /* setSR (value, overflow)
  6863. **
  6864. ** This routine sets the Z, V, and N bits of the status word.
  6865. ** The Zero (Z) and Negative (N) bits will be set according to the
  6866. ** "value".  The values of the overflow bit will be passed
  6867. ** in as an argument.
  6868. */
  6869. void setSR (int value, int overflow) {
  6870.   statusZ = (value == 0);
  6871.   statusV = overflow;
  6872.   statusN = (value < 0);
  6873. }
  6874.  
  6875.  
  6876.  
  6877. /* getNextInterrupt ()
  6878. **
  6879. ** This routine returns the type of the interrupt that is being
  6880. ** signalled.  It looks at the "interruptsSignaled" variable and finds
  6881. ** an interrupt to return by finding a bit that is set.  This routine
  6882. ** determines the order in which several simultaneous interrupts are
  6883. ** processed.  The first one returned will be processed first.  However,
  6884. ** on the next instruction cycle, the next interrupt will be chosen and
  6885. ** will be processed.  Thus, the first interrupt will be interrupted on
  6886. ** its first instruction and will not be serviced until all other
  6887. ** interrupts are serviced.
  6888. **
  6889. ** Thus, interrupts are actually processed in the reverse of whatever order
  6890. ** they are returned from this routine.
  6891. **
  6892. ** This routine also checks the "interrupts enabled" bit in the status
  6893. ** register.  If a signalled bit is maskable but interrupts are disabled,
  6894. ** this routine just ignores it.
  6895. **
  6896. ** Note that we return the exceptions and SYSCALL trap first, before TIMER,
  6897. ** DISK, and SERIAL interrupts.  The reason is that these last three exceptions
  6898. ** may perform a context switch.  It would be disastrous to have an exception
  6899. ** in one thread, process a TIMER Interrupt, do a context switch, then service
  6900. ** the exception or SYSCALL trap.  This would attribute the exception or trap
  6901. ** to the incorrect thread.
  6902. */
  6903. int getNextInterrupt () {
  6904.   if (interruptsSignaled & POWER_ON_RESET) {
  6905.     return POWER_ON_RESET;
  6906.   } else if (interruptsSignaled & HARDWARE_FAULT) {
  6907.     return HARDWARE_FAULT;
  6908.   } else if (interruptsSignaled & ILLEGAL_INSTRUCTION) {
  6909.     return ILLEGAL_INSTRUCTION;
  6910.   } else if ((interruptsSignaled & ARITHMETIC_EXCEPTION) && statusI) {
  6911.     return ARITHMETIC_EXCEPTION;
  6912.   } else if (interruptsSignaled & ADDRESS_EXCEPTION) {
  6913.     return ADDRESS_EXCEPTION;
  6914.   } else if (interruptsSignaled & PAGE_INVALID_EXCEPTION) {
  6915.     return PAGE_INVALID_EXCEPTION;
  6916.   } else if (interruptsSignaled & PAGE_READONLY_EXCEPTION) {
  6917.     return PAGE_READONLY_EXCEPTION;
  6918.   } else if (interruptsSignaled & PRIVILEGED_INSTRUCTION) {
  6919.     return PRIVILEGED_INSTRUCTION;
  6920.   } else if (interruptsSignaled & ALIGNMENT_EXCEPTION) {
  6921.     return ALIGNMENT_EXCEPTION;
  6922.   } else if (interruptsSignaled & EXCEPTION_DURING_INTERRUPT) {
  6923.     return EXCEPTION_DURING_INTERRUPT;
  6924.   } else if ((interruptsSignaled & SYSCALL_TRAP) && statusI) {
  6925.     return SYSCALL_TRAP;
  6926.   } else if ((interruptsSignaled & TIMER_INTERRUPT) && statusI) {
  6927.     return TIMER_INTERRUPT;
  6928.   } else if ((interruptsSignaled & DISK_INTERRUPT) && statusI) {
  6929.     return DISK_INTERRUPT;
  6930.   } else if ((interruptsSignaled & SERIAL_INTERRUPT) && statusI) {
  6931.     return SERIAL_INTERRUPT;
  6932.   } else {
  6933.     return 0;
  6934.   }
  6935. }
  6936.  
  6937.  
  6938.  
  6939. /* getVectorNumber (interruptType)
  6940. **
  6941. ** This routine returns the interrupt vector number for the
  6942. ** given interrupt type.
  6943. **
  6944. **     address description                 maskable
  6945. **     ======= =========================== ========
  6946. **      0000   Power On Reset                 No
  6947. **      0004   Timer Interrupt               Yes
  6948. **      0008   Disk Interrupt                Yes
  6949. **      000C   Serial Interrupt              Yes
  6950. **      0010   Hardware Fault                 No
  6951. **      0014   Illegal Instruction            No
  6952. **      0018   Arithmetic Exception          Yes
  6953. **      001C   Address Exception              No
  6954. **      0020   Page Invalid Exception         No
  6955. **      0024   Page Readonly Exception        No
  6956. **      0028   Privileged Instruction         No
  6957. **      002C   Alignment Exception            No
  6958. **      0030   Exception During Interrupt     No
  6959. **      0034   Syscall Trap                  Yes
  6960. */
  6961. int getVectorNumber (int interruptType) {
  6962.   if (interruptType == POWER_ON_RESET) {
  6963.     return 0x00000000;
  6964.   } else if (interruptType == TIMER_INTERRUPT) {
  6965.     return 0x00000004;
  6966.   } else if (interruptType == DISK_INTERRUPT) {
  6967.     return 0x00000008;
  6968.   } else if (interruptType == SERIAL_INTERRUPT) {
  6969.     return 0x0000000C;
  6970.   } else if (interruptType == HARDWARE_FAULT) {
  6971.     return 0x00000010;
  6972.   } else if (interruptType == ILLEGAL_INSTRUCTION) {
  6973.     return 0x00000014;
  6974.   } else if (interruptType == ARITHMETIC_EXCEPTION) {
  6975.     return 0x00000018;
  6976.   } else if (interruptType == ADDRESS_EXCEPTION) {
  6977.     return 0x0000001C;
  6978.   } else if (interruptType == PAGE_INVALID_EXCEPTION) {
  6979.     return 0x00000020;
  6980.   } else if (interruptType == PAGE_READONLY_EXCEPTION) {
  6981.     return 0x00000024;
  6982.   } else if (interruptType == PRIVILEGED_INSTRUCTION) {
  6983.     return 0x00000028;
  6984.   } else if (interruptType == ALIGNMENT_EXCEPTION) {
  6985.     return 0x0000002C;
  6986.   } else if (interruptType == EXCEPTION_DURING_INTERRUPT) {
  6987.     return 0x00000030;
  6988.   } else if (interruptType == SYSCALL_TRAP) {
  6989.     return 0x00000034;
  6990.   } else {
  6991.     fatalError ("PROGRAM LOGIC ERROR: Unknown exception vector");
  6992.   }
  6993. }
  6994.  
  6995.  
  6996.  
  6997.  
  6998.  
  6999. /**************************************************************************
  7000. **
  7001. ** MEMORY SUB-SYSTEM
  7002. **
  7003. ** These routines deal with access to the physical memory of the BLITZ
  7004. ** machine.
  7005. **
  7006. ** We make the distinction between "implementation memory", which is the
  7007. ** memory used by the program you are currently reading (i.e., the memory
  7008. ** addressed by direct "C" pointers, such as the "memory" variable, and
  7009. ** "BLITZ memory", which is the emulated memory.  We make a further
  7010. ** distinction between the "Physical memory", which is the total emulated
  7011. ** memory of the BLITZ machine, and "Virtual memory" (i.e., "logical
  7012. ** memory"), which is the memory of BLITZ user-level processes, before the
  7013. ** addresses have gone through page table translation.
  7014. **
  7015. ** The BLITZ machine is designed to support multi-processing, i.e., to
  7016. ** support multiple CPUs operating on a single shared physical memory
  7017. ** unit.  To this end, the CPU may lock portions of memory during critical
  7018. ** operations.  Examples of critical operations include: (1) the reading and
  7019. ** subsequent writing of a page table entry, and (2) the reading and
  7020. ** subsequent writing during the "tset" instruction.
  7021. **
  7022. ** The granularity of memory locks is not specified at this time.
  7023. ** It may be the case that each word has its own lock, or perhaps each lock
  7024. ** covers an entire page, or perhaps there is only a single lock covering all
  7025. ** of the physical memory.
  7026. **
  7027. ** In this implementation, we assume that individual words are locked.
  7028. ** Furthermore, we assume that, since this is a single processor implementation,
  7029. ** that only one lock will be held at a time.  Thus, locking is modelled with a
  7030. ** single variable (called "currentMemoryLock") which contains the address of
  7031. ** the word that is locked, or -1 if no word is currently locked.
  7032. ** 
  7033. ** Locking is somewhat unnecessary to the goal of this emulator, since only
  7034. ** a single CPU is emulated, yet it is included in order to model a
  7035. ** multiprocessor implementation more faithfully.  Toward this end, locks are
  7036. ** set and released at the correct times and we do some consistency checking
  7037. ** to make sure we don't (for example) try to release a lock that is not held,
  7038. ** but beyond that, there is no blocking, waiting, or scheduling associated
  7039. ** with locks.
  7040. **
  7041. ** The operations that a CPU or I/O device may issue to the memory unit are:
  7042. **
  7043. **   getPhysicalWord (physAddr)  --> int
  7044. **   getPhysicalWordAndLock (physAddr)  --> int
  7045. **   putPhysicalWord (physAddr, value)
  7046. **   putPhysicalWordAndRelease (physAddr, value)
  7047. **   releaseMemoryLock (physAddr)
  7048. **
  7049. ** In addition, there are a couple of support routines:
  7050. **
  7051. **   physicalAddressOk (physAddr)  -->  bool
  7052. **   isAligned (addr)  --> bool
  7053. **
  7054. ** All I/O is memory-mapped.  Occasionally, reads and writes to certain
  7055. ** addresses should be directed not to the memory, but to various devices.
  7056. ** Routines that support this are:
  7057. **
  7058. **   inMemoryMappedArea (physAddr)  --> bool
  7059. **   ... etc...
  7060. **
  7061. ** The CPU performs page-table translation.  All the details of the
  7062. ** page table translation are encapsulated into this routine.
  7063. **
  7064. **   translate (addr, reading, wantPrinting, doUpdates)  --> physAddr
  7065. **
  7066. ** A typical use is to first call translate to produce a physical address,
  7067. ** then check to see if an exception has occurred, then to either abort if
  7068. ** we had an exception or to call a routine (such as getPhysicalWord) to
  7069. ** fetch or store a word from/to memory.
  7070. **
  7071. **************************************************************************/
  7072.  
  7073.  
  7074.  
  7075.  
  7076. /* physicalAddressOk (addr)
  7077. **
  7078. ** This routine is passed a physical memory address.  It returns TRUE
  7079. ** if this is a legal physical memory address, i.e., if no "Address
  7080. ** Exception" should be thrown.  The address may be in the memory mapped
  7081. ** area; this is still considered OK.
  7082. */
  7083. int physicalAddressOk (int addr) {
  7084.   return ((0 <= addr ) && (addr < MEMORY_SIZE));
  7085. }
  7086.  
  7087.  
  7088.  
  7089. /* isAligned (addr)
  7090. **
  7091. ** This function returns TRUE if this address is word aligned.
  7092. */
  7093. int isAligned (int addr) {
  7094.   return addr%4 == 0;
  7095. }
  7096.  
  7097.  
  7098.  
  7099. /* getPhysicalWord (physAddr)
  7100. **
  7101. ** This routine is passed a physical memory address.  It returns the
  7102. ** word stored at that address.  The address is assumed to be OK, i.e.,
  7103. ** both aligned and within physical memory.  Therefore, no exceptions
  7104. ** are possible.  If the address is in the memory-mapped region, it calls
  7105. ** the I/O subsystem.
  7106. */
  7107. int getPhysicalWord (int physAddr) {
  7108.   int implAddr;
  7109.   if (!physicalAddressOk (physAddr)) {
  7110.     fatalError ("PROGRAM LOGIC ERROR: Invalid address in getPhysicalWord");
  7111.   }
  7112.   if (physAddr % 4 != 0) {
  7113.     fatalError ("PROGRAM LOGIC ERROR: Unaligned address in getPhysicalWord");
  7114.   }
  7115.   checkDiskBufferError (physAddr);
  7116.   if (inMemoryMappedArea (physAddr)) {
  7117.     return getMemoryMappedWord (physAddr);
  7118.   } else {
  7119.     implAddr = ((int) memory) + physAddr;
  7120.     return SWAP_BYTES (* (int *) implAddr);
  7121.   }
  7122. }
  7123.  
  7124.  
  7125.  
  7126. /* getPhysicalWordAndLock (physAddr)
  7127. **
  7128. ** Acquire a lock on this word and then get the word from memory.
  7129. */
  7130. int getPhysicalWordAndLock (int physAddr) {
  7131.   if (currentMemoryLock != -1) {
  7132.     fatalError ("PROGRAM LOGIC ERROR: In getPhysicalWordAndLock - lock is not clear");
  7133.   }
  7134.   currentMemoryLock = physAddr;
  7135.   return getPhysicalWord (physAddr);
  7136. }
  7137.  
  7138.  
  7139.  
  7140. /* putPhysicalWord (physAddr, value)
  7141. **
  7142. ** This routine is passed a physical memory address.  It stores the word
  7143. ** "value" at that address.  The address is assumed to be OK, i.e.,
  7144. ** both aligned and within physical memory.  Therefore, no exceptions
  7145. ** are possible.  If the address is in the memory-mapped region, it calls
  7146. ** the I/O subsystem.
  7147. */
  7148. void putPhysicalWord (int physAddr, int value) {
  7149.   int implAddr;
  7150.   if (!physicalAddressOk (physAddr)) {
  7151.     fatalError ("PROGRAM LOGIC ERROR: Invalid address in putPhysicalWord");
  7152.   }
  7153.   if (physAddr % 4 != 0) {
  7154.     fatalError ("PROGRAM LOGIC ERROR: Unaligned address in putPhysicalWord");
  7155.   }
  7156.   checkDiskBufferError (physAddr);
  7157.   if (inMemoryMappedArea (physAddr)) {
  7158.     putMemoryMappedWord (physAddr, value);
  7159.   } else {
  7160.     implAddr = ((int) memory) + physAddr;
  7161.     * (int *) implAddr = SWAP_BYTES (value);
  7162.   }
  7163. }
  7164.  
  7165.  
  7166.  
  7167. /* putPhysicalWordAndRelease (physAddr, value)
  7168. **
  7169. ** This routine writes a word to memory and then releases the lock.
  7170. */
  7171. void putPhysicalWordAndRelease (int physAddr, int value) {
  7172.   if (currentMemoryLock != physAddr) {
  7173.     fatalError ("PROGRAM LOGIC ERROR: In putPhysicalWordAndRelease - lock is not held for this address");
  7174.   }
  7175.   currentMemoryLock = -1;
  7176.   putPhysicalWord (physAddr, value);
  7177. }
  7178.  
  7179.  
  7180.  
  7181. /* releaseMemoryLock (physAddr)
  7182. **
  7183. ** This routine releases the lock on this memory location.
  7184. */
  7185. void releaseMemoryLock (int physAddr) {
  7186.   if (currentMemoryLock != physAddr) {
  7187.     fatalError ("PROGRAM LOGIC ERROR: In releaseMemoryLock - Lock not held on this address");
  7188.   }
  7189.   currentMemoryLock = -1;
  7190. }
  7191.  
  7192.  
  7193.  
  7194. /* inMemoryMappedArea (addr)
  7195. **
  7196. ** This routine is passed a physical address.  It returns true if
  7197. ** this address is the range of memory-mapped bytes.
  7198. */
  7199. int inMemoryMappedArea (int addr) {
  7200.   return (addr >= MEMORY_MAPPED_AREA_LOW) && (addr <= MEMORY_MAPPED_AREA_HIGH);
  7201. }
  7202.  
  7203.  
  7204.  
  7205. /* translate (virtAddr, reading, wantPrinting, doUpdates) --> physAddr
  7206. **
  7207. ** This routine is passed a logical address "virtAddr", which should be
  7208. ** word-aligned.
  7209. **
  7210. ** If paging is turned on, it will go through the page tables, compute the
  7211. ** physical address and return it.  If paging is off, it will return the
  7212. ** address, as is.  This routine will check for all exceptions that would
  7213. ** arise if memory were accessed using this address.  This routine will not
  7214. ** actually read or write the target memory word.
  7215. **
  7216. ** If "reading" is true, it indicates that the coming memory operation
  7217. ** will be a read, otherwise we intend to write to the address.  This is
  7218. ** needed since the page may be marked read-only.
  7219. **
  7220. ** If "wantPrinting" is TRUE, this routine will print out a trace of
  7221. ** how this logical address is processed, and which exception would
  7222. ** be signalled, if any.
  7223. **
  7224. ** If "doUpdates" is TRUE, this routine will update the page table
  7225. ** entry and will signal an interrupt (if one should occur).  If "doUpdates"
  7226. ** is FALSE, the page table entry will be unchanged and no exception will
  7227. ** be signalled.
  7228. **
  7229. ** This routine has the side-effect of setting the variable
  7230. ** "translateCausedException" to TRUE iff an exception should be
  7231. ** generated by this translation.  This variable is set regardless of
  7232. ** "doUpdates".
  7233. */
  7234. int translate (int virtAddr, int reading, int wantPrinting, int doUpdates) {
  7235.   int tableIndex, tableEntryAddr, tableEntry, frameNumber, offset, physAddr;
  7236.  
  7237.   translateCausedException = 0;
  7238.   if (wantPrinting) {
  7239.     printf ("*****  PAGE TRANSLATION BEGINNING  *****\n");
  7240.     printf ("   Logical address             = 0x%08X\n", virtAddr);
  7241.   }
  7242.  
  7243.   /* Check for an alignment exception.  If found, signal it and return. */
  7244.   if (!isAligned (virtAddr)) {
  7245.     if (wantPrinting) {
  7246.       printf ("   This address is not divisible by 4: ALIGNMENT_EXCEPTION\n");
  7247.     }
  7248.     translateCausedException = 1;
  7249.     if (doUpdates) {
  7250.       interruptsSignaled |= ALIGNMENT_EXCEPTION;
  7251.     }
  7252.     return 0;
  7253.   }
  7254.  
  7255.   /* If paging is turned off... */
  7256.   if (!statusP) {
  7257.     if (wantPrinting) {
  7258.       printf ("   Status[P] = 0, Paging is turned off\n");
  7259.       printf ("   Physical address            = 0x%08X\n", virtAddr);
  7260.     }
  7261.     /* Check for an address exception.  If found, signal it and return. */
  7262.     if (!physicalAddressOk (virtAddr)) {
  7263.       if (wantPrinting) {
  7264.         printf ("   Bad physical address: ADDRESS_EXCEPTION\n");
  7265.       }
  7266.       translateCausedException = 1;
  7267.       if (doUpdates) {
  7268.         interruptsSignaled |= ADDRESS_EXCEPTION;
  7269.       }
  7270.       return 0;
  7271.     }
  7272.     /* Return the virtual address as the physical address. */
  7273.     return virtAddr;
  7274.  
  7275.   /* If paging is turned on... */
  7276.   } else {
  7277.     tableIndex = virtAddr & 0x00ffe000;
  7278.     offset = virtAddr & 0x00001fff;
  7279.     if (wantPrinting) {
  7280.       printf ("     Page Number               = 0x%08X\n", tableIndex);
  7281.       printf ("     Offset into page          = 0x%08X\n", offset);
  7282.       printf ("   Status[P] = 1, Paging is turned on\n");
  7283.     }
  7284.  
  7285.     /* Check for an address exception.  If found, signal it and return. */
  7286.     if (virtAddr & 0xff000000) {
  7287.       if (wantPrinting) {
  7288.         printf ("   Logical address out of range: ADDRESS_EXCEPTION\n");
  7289.       }
  7290.       translateCausedException = 1;
  7291.       if (doUpdates) {
  7292.         interruptsSignaled |= ADDRESS_EXCEPTION;
  7293.       }
  7294.       return 0;
  7295.     }
  7296.  
  7297.     /* Determine which page table entry we will be accessing. */
  7298.     tableIndex = tableIndex >> 11;
  7299.     tableEntryAddr = ptbr + tableIndex;
  7300.     if (wantPrinting) {
  7301.       printf ("   Page Table:\n");
  7302.       printf ("            base               = 0x%08X\n", ptbr);
  7303.       printf ("            length             = 0x%08X\n", ptlr);
  7304.       printf ("            addr of last entry = 0x%08X\n", ptbr + ptlr - 4);
  7305.       printf ("   Page number (shifted)       = 0x%08X\n", tableIndex);
  7306.       printf ("   Address of page table entry = 0x%08X\n", tableEntryAddr);
  7307.     }
  7308.  
  7309.     /* Check for a PAGE_INVALID exception.  If found, signal it and return. */
  7310.     if (tableIndex >= ptlr) {
  7311.       if (wantPrinting) {
  7312.         printf ("   This entry is not within the page table bound: PAGE_INVALID_EXCEPTION\n");
  7313.       }
  7314.       translateCausedException = 1;
  7315.       if (doUpdates) {
  7316.         interruptsSignaled |= PAGE_INVALID_EXCEPTION;
  7317.         pageInvalidOffendingAddress = virtAddr;
  7318.       }
  7319.       return 0;
  7320.     }
  7321.  
  7322.     /* Check for an address exception.  If found, signal it and return. */
  7323.     if (!physicalAddressOk (tableEntryAddr)) {
  7324.       if (wantPrinting) {
  7325.         printf ("   This page table entry is not within physical memory: ADDRESS_EXCEPTION\n");
  7326.       }
  7327.       translateCausedException = 1;
  7328.       if (doUpdates) {
  7329.         interruptsSignaled |= ADDRESS_EXCEPTION;
  7330.       }
  7331.       return 0;
  7332.     }
  7333.  
  7334.     /* Get the page table entry. */
  7335.        /* (tableEntryAddr must be aligned since ptbr is aligned.) */
  7336.     tableEntry = getPhysicalWordAndLock (tableEntryAddr);
  7337.     frameNumber = tableEntry & 0xffffe000;
  7338.     if (wantPrinting) {
  7339.       printf ("   Page table entry            = 0x%08X\n", tableEntry);
  7340.       printf ("       Frame number = 0x%08X\n", frameNumber);
  7341.       if (tableEntry & 0x00000001) {
  7342.         printf ("       V=1 (Page is valid)\n");
  7343.       } else {
  7344.         printf ("       V=0 (Page not valid)\n");
  7345.       }
  7346.       if (tableEntry & 0x00000002) {
  7347.         printf ("       W=1 (Page is writable)\n");
  7348.       } else {
  7349.         printf ("       W=0 (Page not writable)\n");
  7350.       }
  7351.       if (tableEntry & 0x00000004) {
  7352.         printf ("       R=1 (Page has been referenced)\n");
  7353.       } else {
  7354.         printf ("       R=0 (Page has not been referenced)\n");
  7355.       }
  7356.       if (tableEntry & 0x00000008) {
  7357.         printf ("       D=1 (Page is dirty)\n");
  7358.       } else {
  7359.         printf ("       D=0 (Page not dirty)\n");
  7360.       }
  7361.     }
  7362.  
  7363.     /* Check for a PAGE_INVALID exception.  If found, signal it and return. */
  7364.     if (!(tableEntry & 0x00000001)) {   /* if V bit is clear... */
  7365.       if (wantPrinting) {
  7366.         printf ("   Valid bit is clear: PAGE_INVALID_EXCEPTION\n");
  7367.       }
  7368.       translateCausedException = 1;
  7369.       if (doUpdates) {
  7370.         interruptsSignaled |= PAGE_INVALID_EXCEPTION;
  7371.         pageInvalidOffendingAddress = virtAddr;
  7372.       }
  7373.       releaseMemoryLock (tableEntryAddr);
  7374.       return 0;
  7375.     }
  7376.  
  7377.     /* If we will be writing to memory...*/
  7378.     if (!reading) {
  7379.  
  7380.       /* Check for a PAGE_READONLY exception.  If found, signal and return. */
  7381.       if (!(tableEntry & 0x00000002)) {   /* if W bit is clear... */
  7382.         if (wantPrinting) {
  7383.           printf ("   Writable bit is clear: PAGE_READONLY_EXCEPTION\n");
  7384.         }
  7385.         translateCausedException = 1;
  7386.         if (doUpdates) {
  7387.           interruptsSignaled |= PAGE_READONLY_EXCEPTION;
  7388.           pageReadonlyOffendingAddress = virtAddr;
  7389.         }
  7390.         releaseMemoryLock (tableEntryAddr);
  7391.         return 0;
  7392.       }
  7393.  
  7394.       /* Set the dirty bit */
  7395.       if (wantPrinting) {
  7396.         printf ("   Setting the dirty bit\n");
  7397.       }
  7398.       tableEntry |= 0x00000008;
  7399.     }
  7400.  
  7401.     /* Set the referenced bit */
  7402.     if (wantPrinting) {
  7403.       printf ("   Setting the referenced bit\n");
  7404.     }
  7405.     tableEntry |= 0x00000004;
  7406.  
  7407.     /* Compute the physical address. */
  7408.     physAddr = frameNumber | offset;
  7409.     if (wantPrinting) {
  7410.       printf ("   Physical address            = 0x%08X\n", physAddr);
  7411.     }
  7412.  
  7413.     /* Check for an ADDRESS exception.  If found, signal it and return. */
  7414.     if (!physicalAddressOk (physAddr)) {
  7415.       if (wantPrinting) {
  7416.         printf ("   Bad physical address: ADDRESS_EXCEPTION\n");
  7417.       }
  7418.       translateCausedException = 1;
  7419.       if (doUpdates) {
  7420.         interruptsSignaled |= ADDRESS_EXCEPTION;
  7421.       }
  7422.       releaseMemoryLock (tableEntryAddr);
  7423.       return 0;
  7424.     }
  7425.  
  7426.     /* If we are supposed to update the page table, then update it. */
  7427.     if (doUpdates) {
  7428.       putPhysicalWordAndRelease (tableEntryAddr, tableEntry);
  7429.     } else {
  7430.       releaseMemoryLock (tableEntryAddr);
  7431.     }
  7432.     if (wantPrinting) {
  7433.       printf ("   Modified page table entry   = 0x%08X\n", tableEntry);
  7434.       if (doUpdates) {
  7435.         printf ("     (Page table entry was written back to memory)\n");
  7436.       } else {
  7437.         printf ("     (Page table entry was NOT modified)\n");
  7438.       }
  7439.       printf ("   Translation completed with no exceptions\n");
  7440.     }
  7441.  
  7442.     /* Return the physical address. */
  7443.     return physAddr;
  7444.   }
  7445. }
  7446.  
  7447.  
  7448.  
  7449. /* getMemoryMappedWord (physAddr)  ==> int
  7450. **
  7451. */
  7452. int getMemoryMappedWord (int physAddr) {
  7453.   int x;
  7454.  
  7455.   /* Terminal status word... */
  7456.   if (physAddr == SERIAL_STATUS_WORD_ADDRESS) {
  7457.     x = 0;
  7458.     if (termInCharAvail) {
  7459.       x = x | 0x00000001;
  7460.     }
  7461.     if (termOutputReady) {
  7462.       x = x | 0x00000002;
  7463.     }
  7464.     return x;
  7465.  
  7466.   /* Terminal input buffer... */
  7467.   } else if (physAddr == SERIAL_DATA_WORD_ADDRESS) {
  7468.     termInCharWasUsed = 1;
  7469.     termInCharAvail = 0;
  7470.     return termInChar;
  7471.  
  7472.   /* Disk status word... */
  7473.   } else if (physAddr == DISK_STATUS_WORD_ADDRESS) {
  7474.     return currentDiskStatus;
  7475.  
  7476.   /* DISK_MEMORY_ADDRESS_REGISTER... */
  7477.   } else if (physAddr == DISK_MEMORY_ADDRESS_REGISTER) {
  7478.     return diskMemoryAddressRegister;
  7479.  
  7480.   /* DISK_SECTOR_NUMBER_REGISTER... */
  7481.   } else if (physAddr == DISK_SECTOR_NUMBER_REGISTER) {
  7482.     return diskSectorNumberRegister;
  7483.  
  7484.   /* DISK_SECTOR_COUNT_REGISTER... */
  7485.   } else if (physAddr == DISK_SECTOR_COUNT_REGISTER) {
  7486.     return diskSectorCountRegister;
  7487.  
  7488.   /* All other words in the memory-mapped I/O region... */
  7489.   } else {
  7490.     fprintf (stderr, "\n\rERROR: Attempt to access undefined address in memory-mapped area\n\r");
  7491.     controlCPressed = 1;
  7492.   }
  7493. }
  7494.  
  7495.  
  7496.  
  7497. /* putMemoryMappedWord (physAddr, value)
  7498. **
  7499. */
  7500. void putMemoryMappedWord (int physAddr, int value) {
  7501.   int x;
  7502.  
  7503.   /* Terminal status word... */
  7504.   if (physAddr == SERIAL_STATUS_WORD_ADDRESS) {
  7505.     fprintf (stderr, "\n\rAttempt to write to the SERIAL_STATUS_WORD in the memory-mapped area\n\r");
  7506.     controlCPressed = 1;
  7507.     return;
  7508.  
  7509.   /* Terminal output buffer... */
  7510.   } else if (physAddr == SERIAL_DATA_WORD_ADDRESS) {
  7511.     x = value & 0x000000ff;
  7512.     if (!termOutputReady) {
  7513.       fprintf (stderr, "\n\rERROR: Serial device output overrun; char \"%c\" was lost\n\r",
  7514.                x);
  7515.       controlCPressed = 1;
  7516.       return;
  7517.     }
  7518.     /***  fprintf (termOutputFile, "OUTPUT >>>%c<<<\n\r", x);  ***/
  7519.     fprintf (termOutputFile, "%c", x);
  7520.     fflush (termOutputFile);
  7521.     termOutputReady = 0;
  7522.     timeOfNextSerialOutEvent =
  7523.          randomBetween (
  7524.             currentTime + TERM_OUT_DELAY,
  7525.             currentTime + TERM_OUT_DELAY + TERM_OUT_DELAY_VARIATION);
  7526.     if (timeOfNextSerialOutEvent <= currentTime) {
  7527.       timeOfNextSerialOutEvent = currentTime + 1;
  7528.     }
  7529.     updateTimeOfNextEvent ();
  7530.  
  7531.   /* DISK_COMMAND_WORD_ADDRESS... */
  7532.   } else if (physAddr == DISK_COMMAND_WORD_ADDRESS) {
  7533.     if (currentDiskStatus == DISK_BUSY) {
  7534.       fprintf (stderr, "\n\rERROR: Attempt to write to DISK_COMMAND_WORD while disk is busy!\n\r");
  7535.       controlCPressed = 1;
  7536.       return;
  7537.     } else {
  7538.       performDiskIO (value);
  7539.     }
  7540.  
  7541.   /* DISK_MEMORY_ADDRESS_REGISTER... */
  7542.   } else if (physAddr == DISK_MEMORY_ADDRESS_REGISTER) {
  7543.     if (currentDiskStatus == DISK_BUSY) {
  7544.       fprintf (stderr, "\n\rERROR: Attempt to write to DISK_MEMORY_ADDRESS_REGISTER while disk is busy!\n\r");
  7545.       controlCPressed = 1;
  7546.       return;
  7547.     } else {
  7548.       diskMemoryAddressRegister = value;
  7549.     }
  7550.  
  7551.   /* DISK_SECTOR_NUMBER_REGISTER... */
  7552.   } else if (physAddr == DISK_SECTOR_NUMBER_REGISTER) {
  7553.     if (currentDiskStatus == DISK_BUSY) {
  7554.       fprintf (stderr, "\n\rERROR: Attempt to write to DISK_SECTOR_NUMBER_REGISTER while disk is busy!\n\r");
  7555.       controlCPressed = 1;
  7556.       return;
  7557.     } else {
  7558.       diskSectorNumberRegister = value;
  7559.     }
  7560.  
  7561.   /* DISK_SECTOR_COUNT_REGISTER... */
  7562.   } else if (physAddr == DISK_SECTOR_COUNT_REGISTER) {
  7563.     if (currentDiskStatus == DISK_BUSY) {
  7564.       fprintf (stderr, "\n\rERROR: Attempt to write to DISK_SECTOR_COUNT_REGISTER while disk is busy!\n\r");
  7565.       controlCPressed = 1;
  7566.       return;
  7567.     } else {
  7568.       diskSectorCountRegister = value;
  7569.     }
  7570.  
  7571.   /* All other words in the memory-mapped I/O region... */
  7572.   } else {
  7573.     fprintf (stderr, "\n\rERROR: Attempt to access undefined address in memory-mapped area\n\r");
  7574.     controlCPressed = 1;
  7575.   }
  7576. }
  7577.  
  7578.  
  7579.  
  7580.  
  7581. /**************************************************************************
  7582. **
  7583. ** END OF MEMORY SUB-SYSTEM
  7584. **
  7585. **************************************************************************/
  7586.  
  7587.  
  7588.  
  7589. /* pushOntoStack (reg, value)
  7590. **
  7591. ** This routine is passed a 32-bit value.  It pushes it onto the stack.
  7592. ** It is passed the number of the register to use as the stack top
  7593. ** pointer.  If an exception occurs, it is made pending but no other
  7594. ** changes are made.
  7595. */
  7596. void pushOntoStack (int reg, int value) {
  7597.   int top, physAddr;
  7598.   if (statusS) {
  7599.     top = systemRegisters [reg];
  7600.   } else {
  7601.     top = userRegisters [reg];
  7602.   }
  7603.   top -= 4;
  7604.   /* Call translate with reading=false, wantPrinting=false, doUpdates=true */
  7605.   physAddr = translate (top, 0, 0, 1);
  7606.   if (translateCausedException) {
  7607.     return;
  7608.   }
  7609.   putPhysicalWord (physAddr, value);
  7610.   if (reg==0) {
  7611.     return;
  7612.   }
  7613.   if (reg==0) {
  7614.     return;
  7615.   }
  7616.   if (statusS) {
  7617.     systemRegisters [reg] = top;
  7618.   } else {
  7619.     userRegisters [reg] = top;
  7620.   }
  7621. }
  7622.  
  7623.  
  7624.  
  7625. /* popFromStack (reg)
  7626. **
  7627. ** This routine pops a word from the stack and returns it.  It is passed
  7628. ** the number of the register to use as the stack top pointer.  If an
  7629. ** exception occurs, it is made pending and zero is returned, but no other
  7630. ** changes are made.
  7631. */
  7632. int popFromStack (int reg) {
  7633.   int top, physAddr, value;
  7634.   if (statusS) {
  7635.     top = systemRegisters [reg];
  7636.   } else {
  7637.     top = userRegisters [reg];
  7638.   }
  7639.   /* Call translate with reading=true, wantPrinting=false, doUpdates=true */
  7640.   physAddr = translate (top, 1, 0, 1);
  7641.   if (translateCausedException) {
  7642.     return 0;
  7643.   }
  7644.   value = getPhysicalWord (physAddr);
  7645.   if (reg==0) {
  7646.     return value;
  7647.   }
  7648.   top += 4;
  7649.   if (statusS) {
  7650.     systemRegisters [reg] = top;
  7651.   } else {
  7652.     userRegisters [reg] = top;
  7653.   }
  7654.   return value;
  7655. }
  7656.  
  7657.  
  7658.  
  7659. /* commandShowStack ()
  7660. **
  7661. ** This routine prints out the KPL calling stack.
  7662. */
  7663. void commandShowStack () {
  7664.   int i = 0;
  7665.   printf ("   Function/Method            Frame Addr   Execution at...\n");
  7666.   printf ("   ========================   ==========   =====================\n");
  7667.   while (1) {
  7668.     if (!printFrame (i, 0)) break;
  7669.     i++;
  7670.     if (i % 50 == 0) {
  7671.       printf ("Want more (y/n)? ");
  7672.       if (readYesNo () != 1) return;
  7673.     }
  7674.   }
  7675. }
  7676.  
  7677.  
  7678.  
  7679. /* commandFrame ()
  7680. **
  7681. ** This routine prints out the current frame in the calling stack in
  7682. ** the format:
  7683. **   foo    testPack.c, line 123
  7684. */
  7685. void commandFrame () {
  7686.   printf ("=====  Frame number %d (where StackTop = 0)  =====\n",
  7687.           currentFrame);
  7688.   if (!printFrame (currentFrame, 1)) {
  7689.     printf ("Resetting current frame to top of stack!\n");
  7690.     currentFrame = 0;
  7691.   }
  7692. }
  7693.  
  7694.  
  7695.  
  7696. /* commandStackUp ()
  7697. **
  7698. ** This routine moves up one frame (toward the stack top) and prints
  7699. ** the frame.
  7700. */
  7701. void commandStackUp () {
  7702.   currentFrame--;
  7703.   if (currentFrame < 0) {
  7704.     printf ("Already at top of stack!\n");
  7705.     currentFrame = 0;
  7706.   }
  7707.   printf ("=====  Frame number %d (where StackTop = 0)  =====\n",
  7708.           currentFrame);
  7709.   if (!printFrame (currentFrame, 1)) {
  7710.     printf ("Resetting current frame to top of stack!\n");
  7711.     currentFrame = 0;
  7712.   }
  7713. }
  7714.  
  7715.  
  7716.  
  7717. /* commandStackDown ()
  7718. **
  7719. ** This routine moves down one frame (deeper into the stack) and prints
  7720. ** the frame.
  7721. */
  7722. void commandStackDown () {
  7723.   currentFrame++;
  7724.   printf ("=====  Frame number %d (where StackTop = 0)  =====\n",
  7725.           currentFrame);
  7726.   if (!printFrame (currentFrame, 1)) {
  7727.     printf ("Resetting current frame to top of stack!\n");
  7728.     currentFrame = 0;
  7729.   }
  7730. }
  7731.  
  7732.  
  7733.  
  7734. /* printFrame (frameNumber, longPrint)  -->  bool
  7735. **
  7736. ** This routine prints out frame number "frameNumber" in the calling stack.
  7737. ** Frame 0 is on the stack top.  If everything is ok, we return TRUE; if
  7738. ** problems (including end of stack) we return FALSE.
  7739. **
  7740. ** If longPrint is FALSE, we print one line; if TRUE, we print the full frame.
  7741. */
  7742. int printFrame (int frameNumber, int longPrint) {
  7743.   int r13, fp, sp, physAddr, addrOfRoutineDescriptor,
  7744.       filenamePtr, ptrToFunName, lineNum, oldFp, p, i, j, vdAddr, ptrToVarName,
  7745.       offset, offset2, code, totalParmSize, frameSize, index, sizeInBytes;
  7746.   TableEntry * tableEntry;
  7747.   int * ptr;
  7748.   double d; 
  7749.  
  7750.   // Get "r13" and "r14/fp"...
  7751.   if (statusS) {
  7752.     r13 = systemRegisters [13];
  7753.     fp = systemRegisters [14];
  7754.     sp = systemRegisters [15];
  7755.   } else {
  7756.     r13 = userRegisters [13];
  7757.     fp = userRegisters [14];
  7758.     sp = userRegisters [15];
  7759.   }
  7760.   lineNum = r13;
  7761.   oldFp = sp;
  7762.  
  7763.   while (frameNumber > 0) {
  7764.     frameNumber--;
  7765.  
  7766.     // Get "lineNum" from this frame...
  7767.     physAddr = translate (fp-4, 1, 0, 0);  // reading=1, wantPrinting=0, doUpdates=0
  7768.     if (translateCausedException) {
  7769.       printf ("Invalid activation frame stack!  The frame pointer is 0x%08X.\n", fp);
  7770.       return 0;
  7771.     }
  7772.     lineNum = getPhysicalWord (physAddr);
  7773.  
  7774.     // Get new frame pointer "fp" from this frame...
  7775.     physAddr = translate (fp, 1, 0, 0);  // reading=1, wantPrinting=0, doUpdates=0
  7776.     if (translateCausedException) {
  7777.       printf ("Invalid activation frame stack!  The frame pointer is 0x%08X.\n", fp);
  7778.       return 0;
  7779.     }
  7780.  
  7781.     oldFp = fp;
  7782.     fp = getPhysicalWord (physAddr);
  7783.  
  7784.     if (fp != 0 && fp <= oldFp) {
  7785.       printf ("Invalid activation frame stack!  The frame pointer is 0x%08X.\n", oldFp);
  7786.       return 0;
  7787.     }
  7788.   }
  7789.  
  7790.   if (fp == 0) {
  7791.     printf ("Bottom of activation frame stack!\n");
  7792.     return 0;
  7793.   }
  7794.  
  7795.   // printf ("frame pointer = ");
  7796.   // printNumberNL (fp);
  7797.  
  7798.   // Get ptr to routine descriptor
  7799.   physAddr = translate (fp-8, 1, 0, 0);  // reading=1, wantPrinting=0, doUpdates=0
  7800.   if (translateCausedException) {
  7801.     printf ("Invalid activation frame stack!  The frame pointer is 0x%08X.\n", fp);
  7802.     return 0;
  7803.   }
  7804.   addrOfRoutineDescriptor = getPhysicalWord (physAddr);
  7805.   // printf ("addrOfRoutineDescriptor = ");
  7806.   // printNumberNL (addrOfRoutineDescriptor);
  7807.  
  7808.   // Get ptr to filename...
  7809.   physAddr = translate (addrOfRoutineDescriptor, 1, 0, 0);
  7810.   if (translateCausedException) {
  7811.     printf ("Invalid activation frame stack!  The frame pointer is 0x%08X.\n", fp);
  7812.     return 0;
  7813.   }
  7814.   filenamePtr = getPhysicalWord (physAddr);
  7815.   // printf ("filenamePtr = ");
  7816.   // printNumberNL (filenamePtr);
  7817.  
  7818.   // Get ptr to function name...
  7819.   physAddr = translate (addrOfRoutineDescriptor+4, 1, 0, 0);
  7820.   if (translateCausedException) {
  7821.     printf ("Invalid activation frame stack!  The frame pointer is 0x%08X.\n", fp);
  7822.     return 0;
  7823.   }
  7824.   ptrToFunName = getPhysicalWord (physAddr);
  7825.   // printf ("ptrToFunName = ");
  7826.   // printNumberNL (ptrToFunName);
  7827.  
  7828.   // Get totalParmSize...
  7829.   physAddr = translate (addrOfRoutineDescriptor+8, 1, 0, 0);
  7830.   if (translateCausedException) {
  7831.     printf ("Invalid activation frame stack!  The frame pointer is 0x%08X.\n", fp);
  7832.     return 0;
  7833.   }
  7834.   totalParmSize = getPhysicalWord (physAddr);
  7835.  
  7836.   // Get frameSize...
  7837.   physAddr = translate (addrOfRoutineDescriptor+12, 1, 0, 0);
  7838.   if (translateCausedException) {
  7839.     printf ("Invalid activation frame stack!  The frame pointer is 0x%08X.\n", fp);
  7840.     return 0;
  7841.   }
  7842.   frameSize = getPhysicalWord (physAddr);
  7843.  
  7844.   if (longPrint == 0) {
  7845.  
  7846.     // Print the function name...
  7847.     printf ("   ");
  7848.     if (!printAsciiDataInWidth (ptrToFunName, 27)) return 0;
  7849.     printf (" ");
  7850.  
  7851.     // Print the frame address...
  7852.     putlong (fp);
  7853.     printf ("    ");
  7854.  
  7855.     // Print the Filename...
  7856.     if (!printAsciiDataInWidth (filenamePtr,0)) return 0;
  7857.  
  7858.     // Print the line number...
  7859.     printf (", line %d\n", lineNum);
  7860.  
  7861.     return 1;
  7862.   }
  7863.  
  7864.   // Perform the long printout of the frame...
  7865.  
  7866.   // Print the function name...
  7867.   printf ("Function Name:    ");
  7868.   printAsciiDataInWidth (ptrToFunName, 0);
  7869.   printf ("\n");
  7870.  
  7871.   // Print the Filename...
  7872.   printf ("Filename:         ");
  7873.   printAsciiDataInWidth (filenamePtr, 0);
  7874.   printf ("\n");
  7875.  
  7876.   // Print the line number...
  7877.   printf ("Execution now at: line %d\n", lineNum);
  7878.  
  7879.   // Print the frame address...
  7880.   printf ("Frame Addr:       ");
  7881.   putlong (fp);
  7882.   printf ("\n");
  7883.   printf ("frameSize:        %d\n", frameSize);
  7884.   printf ("totalParmSize:    %d\n", totalParmSize);
  7885.  
  7886.   printf ("                         ==========\n");
  7887.   if (oldFp != sp) {
  7888.     oldFp = oldFp+8;
  7889.   }
  7890.   for (p = oldFp; p <= fp+totalParmSize+4; p = p+4) {
  7891.     if (p == fp) {
  7892.       printf ("     fp:");
  7893.     } else if (p == fp-4) {
  7894.       printf ("    r13:");
  7895.     } else if (p == fp-8) {
  7896.       printf ("R.D.ptr:");
  7897.     } else if (p == fp+4) {
  7898.       printf ("RetAddr:");
  7899.     } else if (p == fp+8) {
  7900.       printf ("   Args:");
  7901.     } else if (p == sp) {
  7902.       printf ("   sp-->");
  7903.     } else {
  7904.       printf ("        ");
  7905.     }
  7906.     printf ("%4d   %08X:  ", p-fp, p);
  7907.     physAddr = translate (p, 1, 0, 0);
  7908.     if (translateCausedException) {
  7909.       printf ("*****  MEMORY EXCEPTION  *****\n");
  7910.     } else {
  7911.       i = getPhysicalWord (physAddr);
  7912.       printf ("%08X\n", i);
  7913.     }
  7914.     if (p == fp+4) {
  7915.       printf ("                         ==========\n");
  7916.     }
  7917.   }
  7918.  
  7919.  
  7920.   printf ("\nPARAMETERS AND LOCAL VARIABLES WITHIN THIS FRAME:\n");
  7921.   printf ("=================================================\n");
  7922.  
  7923.   // In a loop, print each var descriptor...
  7924.   vdAddr = addrOfRoutineDescriptor + 16;
  7925.   while (1) {
  7926.  
  7927.     // Get ptr to variable name...
  7928.     physAddr = translate (vdAddr, 1, 0, 0);
  7929.     if (translateCausedException) {
  7930.       printf ("Problems in variable descriptor information in memory (addr = 0x%08X - address exception when accessing ptr to var name)!\n",
  7931.               vdAddr);
  7932.       break;
  7933.     }
  7934.     ptrToVarName = getPhysicalWord (physAddr);
  7935.  
  7936.     // If the pointer is zero, we are done...
  7937.     if (ptrToVarName == 0) break;
  7938.  
  7939.     // Get offset...
  7940.     physAddr = translate (vdAddr+4, 1, 0, 0);
  7941.     if (translateCausedException) {
  7942.       printf ("Problems in variable descriptor information in memory (addr = 0x%08X - address exception when accessing offset)!\n",
  7943.               vdAddr+4);
  7944.       break;
  7945.     }
  7946.     offset = getPhysicalWord (physAddr);
  7947.  
  7948.     // Get sizeInBytes...
  7949.     physAddr = translate (vdAddr+8, 1, 0, 0);
  7950.     if (translateCausedException) {
  7951.       printf ("Problems in variable descriptor information in memory (addr = 0x%08X - bad sizeInBytes)!\n",
  7952.               vdAddr+8);
  7953.       break;
  7954.     }
  7955.     sizeInBytes = getPhysicalWord (physAddr);
  7956.  
  7957.     // Get the type code (e.g., 'D', 'I', etc.)
  7958.     physAddr = translate (ptrToVarName, 1, 0, 0);
  7959.     if (translateCausedException) {
  7960.       printf ("Problems in variable descriptor information in memory (addr = 0x%08X - address exception when accessing type code)!\n",
  7961.               ptrToVarName);
  7962.       break;
  7963.     }
  7964.     code = getPhysicalWord (physAddr);
  7965.     code = (code >> 24) & 0x000000ff;
  7966.  
  7967.     ptrToVarName++;
  7968.  
  7969.     // Print the variable name...
  7970.     printf ("  ");
  7971.     printAsciiDataInWidth (ptrToVarName, 0);
  7972.     if (code == 'I') {
  7973.       printf (": int\n");
  7974.       printf ("        %4d   %08X:  ", offset, fp+offset);
  7975.       physAddr = translate (fp+offset, 1, 0, 0);
  7976.       if (translateCausedException) {
  7977.         printf ("*****  MEMORY EXCEPTION  *****\n");
  7978.       } else {
  7979.         i = getPhysicalWord (physAddr);
  7980.         printf ("%08X    value = %d\n", i, i);
  7981.       }
  7982.     } else if (code == '?') {    // This would be for temporaries...
  7983.       printf ("\n");
  7984.       printf ("        %4d   %08X:  ", offset, fp+offset);
  7985.       physAddr = translate (fp+offset, 1, 0, 0);
  7986.       if (translateCausedException) {
  7987.         printf ("*****  MEMORY EXCEPTION  *****\n");
  7988.       } else {
  7989.         i = getPhysicalWord (physAddr);
  7990.         printf ("%08X\n", i);
  7991.         for (j = 4; j< sizeInBytes; j=j+4) {
  7992.           physAddr = translate (fp+offset+j, 1, 0, 0);
  7993.           if (translateCausedException) {
  7994.             printf ("*****  MEMORY EXCEPTION  *****\n");
  7995.           } else {
  7996.             i = getPhysicalWord (physAddr);
  7997.             printf ("        %4d   %08X:  %08X\n", offset+j, fp+offset+j, i);
  7998.           }
  7999.         }
  8000.       }
  8001.     } else if (code == 'D') {
  8002.       printf (": double\n");
  8003.       printf ("        %4d   %08X:  ", offset, fp+offset);
  8004.       physAddr = translate (fp+offset, 1, 0, 0);
  8005.       if (translateCausedException) {
  8006.         printf ("*****  MEMORY EXCEPTION  *****\n");
  8007.       } else {
  8008.         i = getPhysicalWord (physAddr);
  8009.         ptr = (int *) (& d);
  8010.         *ptr = i;
  8011.         ptr++;
  8012.         physAddr = translate (fp+offset+4, 1, 0, 0);
  8013.         if (translateCausedException) {
  8014.           printf ("*****  MEMORY EXCEPTION  *****\n");
  8015.         } else {
  8016.           j = getPhysicalWord (physAddr);
  8017.           *ptr = j;
  8018.           printf ("%08X    value = ", i);
  8019.           if (isnan(d)) {
  8020.             printf ("Not-a-Number\n");
  8021.           } else if (d == POSITIVE_INFINITY) {
  8022.             printf ("Positive-Infinity\n");
  8023.           } else if (d == NEGATIVE_INFINITY) {
  8024.             printf ("Negative-Infinity\n");
  8025.           } else if (isNegZero (d)) {
  8026.             printf ("Negative-Zero\n");
  8027.           } else {
  8028.             printf ("%.15g\n", d);        // This precision is a little low, to look nice
  8029.           }
  8030.           printf ("        %4d   %08X:  %08X\n", offset+4, fp+offset+4, j);
  8031.         }
  8032.       }
  8033.     } else if ((code == 'C') || (code == 'B')) {
  8034.       if (code == 'C') {
  8035.         printf (": char\n");
  8036.       } else {
  8037.         printf (": bool\n");
  8038.       }
  8039.       // printf ("offset = 0x%08X  %d\n", offset, offset);
  8040.       divide (offset, 4);
  8041.       offset2 = q * 4;
  8042.       // printf ("offset2 = 0x%08X  %d\n", offset2, offset2);
  8043.       printf ("        %4d   %08X:  ", offset, fp+offset2);
  8044.       physAddr = translate (fp+offset2, 1, 0, 0);
  8045.       if (translateCausedException) {
  8046.         printf ("*****  MEMORY EXCEPTION  *****\n");
  8047.       } else {
  8048.         i = getPhysicalWord (physAddr);
  8049.         if (r == 0) {
  8050.           i = (i >> 24) & 0x000000ff;
  8051.           printf ("%02X------", i);
  8052.         } else if (r == 1) {
  8053.           i = (i >> 16) & 0x000000ff;
  8054.           printf ("--%02X----", i);
  8055.         } else if (r == 2) {
  8056.           i = (i >> 8) & 0x000000ff;
  8057.           printf ("----%02X--", i);
  8058.         } else if (r == 3) {
  8059.           i = (i >> 0) & 0x000000ff;
  8060.           printf ("------%02X", i);
  8061.         }
  8062.         if (code == 'C') {
  8063.           printf ("    value = '");
  8064.           fancyPrintChar (i);
  8065.           printf ("'\n");
  8066.         } else {
  8067.           if (i == 0) {
  8068.             printf ("    value = FALSE\n");
  8069.           } else if (i == 1) {
  8070.             printf ("    value = TRUE\n");
  8071.           } else {
  8072.             printf ("    value = *****  ERROR IN BOOLEAN VALUE  *****\n");
  8073.           }
  8074.         }
  8075.       }
  8076.     } else if (code == 'P') {
  8077.       printf (": ptr\n");
  8078.       printf ("        %4d   %08X:  ", offset, fp+offset);
  8079.       physAddr = translate (fp+offset, 1, 0, 0);
  8080.       if (translateCausedException) {
  8081.         printf ("*****  MEMORY EXCEPTION  *****");
  8082.       } else {
  8083.         i = getPhysicalWord (physAddr);
  8084.         printf ("%08X    --> ", i);
  8085.         index = findLabel (i);
  8086.         if ((index != -1) && (i != 0)) {
  8087.           tableEntry = valueIndex [index];
  8088.           printf ("(%s) ", tableEntry->string);
  8089.         }
  8090.         if (i == 0) {
  8091.           printf ("NULL");
  8092.         } else {
  8093.           divide (i, 4);
  8094.           j = q * 4;
  8095.           printf ("%08X:  ", j);
  8096.           physAddr = translate (j, 1, 0, 0);
  8097.           if (translateCausedException) {
  8098.             printf ("*****  MEMORY EXCEPTION  *****");
  8099.           } else {
  8100.             i = getPhysicalWord (physAddr);
  8101.             if (r == 0) {
  8102.               printf ("%08X", i);
  8103.             } else if (r == 1) {
  8104.               printf ("--%06X", i & 0x00ffffff);
  8105.             } else if (r == 2) {
  8106.               printf ("----%04X", i & 0x0000ffff);
  8107.             } else {
  8108.               printf ("------%02X", i & 0x000000ff);
  8109.             }
  8110.           }
  8111.         }
  8112.       }
  8113.       printf ("\n");
  8114.     } else if (code == 'R') {
  8115.       printf (": record\n");
  8116.       printf ("        %4d   %08X:  ", offset, fp+offset);
  8117.       physAddr = translate (fp+offset, 1, 0, 0);
  8118.       if (translateCausedException) {
  8119.         printf ("*****  MEMORY EXCEPTION  *****\n");
  8120.       } else {
  8121.         i = getPhysicalWord (physAddr);
  8122.         printf ("%08X\n", i);
  8123.         for (j = 4; j< sizeInBytes; j=j+4) {
  8124.           physAddr = translate (fp+offset+j, 1, 0, 0);
  8125.           if (translateCausedException) {
  8126.             printf ("*****  MEMORY EXCEPTION  *****\n");
  8127.           } else {
  8128.             i = getPhysicalWord (physAddr);
  8129.             printf ("        %4d   %08X:  %08X\n", offset+j, fp+offset+j, i);
  8130.           }
  8131.         }
  8132.       }
  8133.     } else if (code == 'O') {
  8134.       printf (": object\n");
  8135.       printf ("        %4d   %08X:  ", offset, fp+offset);
  8136.       physAddr = translate (fp+offset, 1, 0, 0);
  8137.       if (translateCausedException) {
  8138.         printf ("*****  MEMORY EXCEPTION  *****\n");
  8139.       } else {
  8140.         i = getPhysicalWord (physAddr);
  8141.         printf ("%08X    (Dispatch Table is at 0x%08X)\n", i, i);
  8142.         for (j = 4; j< sizeInBytes; j=j+4) {
  8143.           physAddr = translate (fp+offset+j, 1, 0, 0);
  8144.           if (translateCausedException) {
  8145.             printf ("*****  MEMORY EXCEPTION  *****\n");
  8146.           } else {
  8147.             i = getPhysicalWord (physAddr);
  8148.             printf ("        %4d   %08X:  %08X\n", offset+j, fp+offset+j, i);
  8149.           }
  8150.         }
  8151.       }
  8152.     } else if (code == 'A') {
  8153.       printf (": array\n");
  8154.       printf ("        %4d   %08X:  ", offset, fp+offset);
  8155.       physAddr = translate (fp+offset, 1, 0, 0);
  8156.       if (translateCausedException) {
  8157.         printf ("*****  MEMORY EXCEPTION  *****\n");
  8158.       } else {
  8159.         i = getPhysicalWord (physAddr);
  8160.         printf ("%08X    (Number of elts = %d)\n", i, i);
  8161.         for (j = 4; j< sizeInBytes; j=j+4) {
  8162.           physAddr = translate (fp+offset+j, 1, 0, 0);
  8163.           if (translateCausedException) {
  8164.             printf ("*****  MEMORY EXCEPTION  *****\n");
  8165.           } else {
  8166.             i = getPhysicalWord (physAddr);
  8167.             printf ("        %4d   %08X:  %08X\n", offset+j, fp+offset+j, i);
  8168.           }
  8169.         }
  8170.       }
  8171.     } else {
  8172.       printf ("Problems in variable descriptor information in memory (addr = 0x%08X - address exception when accessing data type code)!\n",
  8173.               ptrToVarName);
  8174.       break;
  8175.     }
  8176.  
  8177.     // Increment to next var descriptor...
  8178.     vdAddr = vdAddr + 12;
  8179.     
  8180.   }
  8181.   printf ("=================================================\n");
  8182.  
  8183.   return 1;
  8184.  
  8185. }
  8186.  
  8187.  
  8188.  
  8189.  
  8190. /* printAsciiDataInWidth (ptr, width) --> bool
  8191. **
  8192. ** This routine is passed a pointer to a Null-terminated string of characters
  8193. ** in the BLITZ memory.  It attempts to print it.  The ptr could be anything, so
  8194. ** this routine checks for all conceivable errors.
  8195. **
  8196. ** If everything seemed ok, then return TRUE.  If problems, return FALSE.
  8197. **
  8198. ** If the string is shorter than 'width' characters, it will be padded to that
  8199. ** length with blanks.
  8200. */
  8201. int printAsciiDataInWidth (int ptr, int width) {
  8202.   int physAddr, word, i;
  8203.  
  8204.   while (1) {
  8205.     physAddr = translate (ptr & 0xfffffffc, 1, 0, 0);
  8206.                           // reading=1, wantPrinting=0, doUpdates=0
  8207.     if (translateCausedException) {
  8208.       printf ("\n*****  Problem with stack frame: A debugging string is in error  *****\n");
  8209.       return 0;
  8210.     }
  8211.     word = getPhysicalWord (physAddr);
  8212.     /* Isolate the byte in the word and shift to lower-order 8 bits. */
  8213.     word = word >> (24 - ((ptr & 0x00000003) << 3));
  8214.     word &= 0x000000ff;
  8215.     if (word == 0) {
  8216.       for (i = width; i>0; i--) {
  8217.         printf (" ");
  8218.       }
  8219.       return 1;
  8220.     }
  8221.     if (word >= ' ' && word < 0x7f) {
  8222.       printf ("%c", word);
  8223.       width--;
  8224.     } else {
  8225.       printf ("\n*****  Problem with stack frame: A debugging string is in error  *****\n");
  8226.       return 0;
  8227.     }
  8228.     ptr++;
  8229.   }
  8230. }
  8231.  
  8232.  
  8233.  
  8234. /* commandHex ()
  8235. **
  8236. ** This routine asks for a hex number.  It prints it out in hex, decimal,
  8237. ** and ascii.
  8238. */
  8239. void commandHex () {
  8240.   int value;
  8241.   /* Read in a value. */
  8242.   printf ("Enter a value in hex: ");
  8243.   value = readHexInt ();
  8244.  
  8245.   printHexDecimalAscii (value);
  8246. }
  8247.  
  8248.  
  8249.  
  8250. /* commandDecimal ()
  8251. **
  8252. ** This routine asks for a decimal number.  It prints it out in hex, decimal,
  8253. ** and ascii.
  8254. */
  8255. void commandDecimal () {
  8256.   int value;
  8257.   /* Read in a value. */
  8258.   printf ("Enter a value in decimal: ");
  8259.   value = readDecimalInt ();
  8260.  
  8261.   printHexDecimalAscii (value);
  8262. }
  8263.  
  8264.  
  8265.  
  8266. /* commandAscii ()
  8267. **
  8268. ** This routine asks for a character.  It prints it out in hex, decimal,
  8269. ** and ascii.
  8270. */
  8271. void commandAscii () {
  8272.   int value;
  8273.   /* Read in a value. */
  8274.   printf ("Enter a single character followed by a newline/return: ");
  8275.   fflush (stdout);
  8276.   fgets (inputBuffer, sizeof(inputBuffer), stdin);
  8277.   /* Overwrite the \n with \0 to remove it. */
  8278.   inputBuffer [strlen (inputBuffer)-1] = '\0';
  8279.   if (strlen (inputBuffer) != 1) {
  8280.     printf ("You entered %d characters - Aborted\n", strlen (inputBuffer));
  8281.     return;
  8282.   }
  8283.   printHexDecimalAscii (inputBuffer[0]);
  8284. }
  8285.  
  8286.  
  8287.  
  8288. /* printHexDecimalAscii (i)
  8289. **
  8290. ** This routine prints a number in the form
  8291. **       hex: 0x1234abcd   decimal: 395441741   ascii: "..a."
  8292. ** followed by a newline.
  8293. */
  8294. void printHexDecimalAscii (int i) {
  8295.   char str [100];
  8296.   char c;
  8297.   printf ("     hex: 0x");
  8298.   putlong (i);
  8299.   printf ("     decimal: %d", i);
  8300.   printf ("     ascii: \"");
  8301.   c = (i >> 24) & 0x000000ff;
  8302.   if ((c>=' ') && (c <= '~')) {
  8303.     putchar (c);
  8304.   } else {
  8305.     putchar ('.');
  8306.   }
  8307.   c = (i >> 16) & 0x000000ff;
  8308.   if ((c>=' ') && (c <= '~')) {
  8309.     putchar (c);
  8310.   } else {
  8311.     putchar ('.');
  8312.   }
  8313.   c = (i >> 8) & 0x000000ff;
  8314.   if ((c>=' ') && (c <= '~')) {
  8315.     putchar (c);
  8316.   } else {
  8317.     putchar ('.');
  8318.   }
  8319.   c = i & 0x000000ff;
  8320.   if ((c>=' ') && (c <= '~')) {
  8321.     putchar (c);
  8322.   } else {
  8323.     putchar ('.');
  8324.   }
  8325.   printf ("\"\n");
  8326. }
  8327.  
  8328.  
  8329.  
  8330. /* jumpIfTrueRaRb (cond, instr)
  8331. **
  8332. ** This routine is used in the implementation of the conditional jumping
  8333. ** instructions.  If the condition is true, it updates pc to effect the
  8334. ** jump.  Otherwise, it increments the pc.
  8335. */
  8336. void jumpIfTrueRaRb (int cond, int instr) {
  8337.   int x, y, z;
  8338.   if (cond) {
  8339.     x = getRa (instr);
  8340.     y = getRc (instr);
  8341.     z = x + y;
  8342.     if (z % 4 != 0) {
  8343.       interruptsSignaled |= ALIGNMENT_EXCEPTION;
  8344.       return;
  8345.     }
  8346.     pc = z;
  8347.   } else {
  8348.     pc += 4;
  8349.   }
  8350. }
  8351.  
  8352.  
  8353.  
  8354. /* jumpIfTrueData24 (cond, instr)
  8355. **
  8356. ** This routine is used in the implementation of the conditional jumping
  8357. ** instructions.  If the condition is true, it updates pc to effect the
  8358. ** jump.  Otherwise, it increments the pc.
  8359. */
  8360. void jumpIfTrueData24 (int cond, int instr) {
  8361.   int z;
  8362.   if (cond) {
  8363.     z = getData24 (instr);
  8364.     if (z % 4 != 0) {
  8365.       interruptsSignaled |= ALIGNMENT_EXCEPTION;
  8366.       return;
  8367.     }
  8368.     pc += z;
  8369.   } else {
  8370.     pc += 4;
  8371.   }
  8372. }
  8373.  
  8374.  
  8375.  
  8376. /* controlC (sig)
  8377. **
  8378. ** This routine is called when the user hits control-C.  It sets
  8379. ** "controlCPressed" to TRUE.  In the fetch-increment-execute loop, this
  8380. ** variable is checked before each instruction and execution of BLITZ
  8381. ** instructions is terminated if it is ever found to be true.  The variable
  8382. ** is also reset to false at that time.
  8383. **
  8384. ** If "controlCPressed" was already TRUE when this routine is called,
  8385. ** then it must be that control-C has just been pressed twice in a row, with
  8386. ** the first press not having been "absorbed".  This could be due to a
  8387. ** problem in the emulator itself; perhaps the emulator is stuck in an
  8388. ** infinite loop.  In this case, this routine will abort the emulator.
  8389. */
  8390. void controlC (int sig) {
  8391.   if (sig != SIGINT) {
  8392.     fatalError ("PROGRAM LOGIC ERROR: Expected SIGINT in signal handler");
  8393.   }
  8394.   signal (SIGINT, controlC);
  8395.   printf ("\n*****  Control-C  *****\n");
  8396.   if (controlCPressed) {
  8397.     turnOffTerminal ();
  8398.     fprintf (stderr, "\nBLITZ Emulator quiting\n");
  8399.     /* raise (SIGSEGV);  Produce a core dump. */
  8400.     errorExit ();
  8401.   } else {
  8402.     controlCPressed = 1;
  8403.   }
  8404. }
  8405.  
  8406.  
  8407.  
  8408. /* randomBetween (lo, high)
  8409. **
  8410. ** This routine returns the next random number between the given numbers,
  8411. ** inclusive.
  8412. */
  8413. int randomBetween (int lo, int high) {
  8414.   int gap = high - lo + 1;
  8415.   int i = genRandom () % gap;
  8416.   return lo + i;
  8417. }
  8418.  
  8419.  
  8420.  
  8421. /*
  8422. ** genRandom ()
  8423. **
  8424. ** This routine returns a random number within the range
  8425. ** 1,2,3,... 2147483646  (i.e., 2**31-2) and updates the random seed.
  8426. ** See "Random Number Generators: Good Ones are Hard to Find", by
  8427. ** Stephen K. Parks and Keith W. Miller, CACM 31:10, October 1988.
  8428. */
  8429. int genRandom () {
  8430.  
  8431. #define randomA 16807
  8432. #define randomM 2147483647
  8433. #define randomQ 127773
  8434. #define randomR 2836
  8435.  
  8436.   int lo,hi,test;
  8437.  
  8438.   hi = randomSeed / randomQ;
  8439.   lo = randomSeed % randomQ;
  8440.   test = (randomA * lo) - (randomR * hi);
  8441.   if (test > 0) {
  8442.     randomSeed = test;
  8443.   } else {
  8444.     randomSeed = test + randomM;
  8445.   }
  8446.   return (randomSeed);      /*  to normalize, use random/m */
  8447.  
  8448.  
  8449.  
  8450. /* doTimerEvent ()
  8451. **
  8452. ** This routine is called when a timer event is due.  It will signal and
  8453. ** interrupt and schedule another timer event in the future.
  8454. */
  8455. void doTimerEvent () {
  8456.  
  8457.   /* Set the timeOfNextTimerEvent. */
  8458.   if (TIME_SLICE <= 0) {
  8459.     timeOfNextTimerEvent = MAX;
  8460.   } else {
  8461.     timeOfNextTimerEvent =
  8462.          randomBetween (currentTime + TIME_SLICE,
  8463.                         currentTime + TIME_SLICE + TIME_SLICE_VARIATION);
  8464.     if (timeOfNextTimerEvent <= currentTime) {
  8465.       timeOfNextTimerEvent = currentTime + 1;
  8466.     }
  8467.     /* Signal a timer interrupt. */
  8468.     interruptsSignaled |= TIMER_INTERRUPT;
  8469.   }
  8470. }
  8471.  
  8472.  
  8473.  
  8474. /* doDiskEvent ()
  8475. **
  8476. ** This routine is called when a disk event is due.  It will:
  8477. **    Set the "currentDiskStatus" to "futureDiskStatus,"
  8478. **    Signal a DISK_INTERRUPT, and
  8479. **    Not schedule any future disk events.
  8480. */
  8481. void doDiskEvent () {
  8482.  
  8483.   /* Signal a disk interrupt. */
  8484.   interruptsSignaled |= DISK_INTERRUPT;
  8485.  
  8486.   /* Change the disk status to WAITING. */
  8487.   currentDiskStatus = futureDiskStatus;
  8488.  
  8489.   /* Do not schedule another disk event. */
  8490.   timeOfNextDiskEvent = MAX;
  8491. }
  8492.  
  8493.  
  8494.  
  8495. /* doSerialInEvent (waitForKeystroke)
  8496. **
  8497. ** This routine is called when a serial input event is due.  We need to check
  8498. ** the input and see if we have a new character on the input.  If so, we
  8499. ** need to signal a serial interrupt.  Normally, we do not wait for the user
  8500. ** to type something, but if "waitForKeystroke" is true, we will wait for
  8501. ** at least one keystroke.
  8502. */
  8503. void doSerialInEvent (int waitForKeystroke) {
  8504.   int ch;
  8505.  
  8506.   /* If the last character was never used, report it. */
  8507.   if (!termInCharWasUsed) {
  8508.     termInCharWasUsed = 1;
  8509.     if ((termInChar >= ' ') && (termInChar < 0x7f)) {
  8510.       fprintf (stderr, "\n\rERROR: The serial input character \"%c\" was not fetched in a timely way and has been lost!\n\r",
  8511.              termInChar);
  8512.     } else {
  8513.       fprintf (stderr, "\n\rERROR: The serial input character 0x%02X was not fetched in a timely way and has been lost!\n\r",
  8514.              termInChar);
  8515.     }
  8516.     // controlCPressed = 1;
  8517.     return;
  8518.   }
  8519.  
  8520.   /* See if we have a new key pressed.  If so, signal an interrupt. */
  8521.   ch = checkForInput (waitForKeystroke);
  8522.   if (ch != 0) {
  8523.     termInChar = ch;
  8524.     termInCharWasUsed = 0;
  8525.     termInCharAvail = 1;
  8526.     interruptsSignaled |= SERIAL_INTERRUPT;
  8527.   }
  8528.  
  8529.   /* Figure out when to check the keyboard next. */
  8530.   if (timeOfNextSerialInEvent != MAX) {
  8531.     timeOfNextSerialInEvent =
  8532.         randomBetween (
  8533.            currentTime + KEYBOARD_WAIT_TIME,
  8534.            currentTime + KEYBOARD_WAIT_TIME + KEYBOARD_WAIT_TIME_VARIATION);
  8535.     if (timeOfNextSerialInEvent <= currentTime) {
  8536.       timeOfNextSerialInEvent = currentTime + 1;
  8537.     }
  8538.   }
  8539.  
  8540. }
  8541.  
  8542.  
  8543.  
  8544. /* doSerialOutEvent ()
  8545. **
  8546. ** This routine is called when a serial outout event is due.  If we are
  8547. ** busy sending a character to the terminal, then at this event, we are
  8548. ** done and the terminal is once again ready.  Set the terminal output
  8549. ** status to "output ready" and signal an interrupt.
  8550. */
  8551. void doSerialOutEvent () {
  8552.   if (!termOutputReady) {
  8553.     termOutputReady = 1;
  8554.     interruptsSignaled |= SERIAL_INTERRUPT;
  8555.   }
  8556.   timeOfNextSerialOutEvent = MAX;
  8557. }
  8558.  
  8559.  
  8560.  
  8561. /* updateTimeOfNextEvent ()
  8562. **
  8563. ** This routine sets "timeOfNextEvent".  It looks at the times of all of
  8564. ** the next scheduled events and uses the minimum of all of these.
  8565. */
  8566. void updateTimeOfNextEvent () {
  8567.   timeOfNextEvent = MAX;
  8568.   if (timeOfNextEvent > timeOfNextTimerEvent) {
  8569.     timeOfNextEvent = timeOfNextTimerEvent;
  8570.   }
  8571.   if (timeOfNextEvent > timeOfNextDiskEvent) {
  8572.     timeOfNextEvent = timeOfNextDiskEvent;
  8573.   }
  8574.   if (timeOfNextEvent > timeOfNextSerialInEvent) {
  8575.     timeOfNextEvent = timeOfNextSerialInEvent;
  8576.   }
  8577.   if (timeOfNextEvent > timeOfNextSerialOutEvent) {
  8578.     timeOfNextEvent = timeOfNextSerialOutEvent;
  8579.   }
  8580. }
  8581.  
  8582.  
  8583.  
  8584. /* divide (a, b)
  8585. **
  8586. ** This routine is passed two integers ("a" and "b").  It divides a by b
  8587. ** to get a quotient ("q") and remainder ("r"), such that
  8588. **
  8589. **       a = b*q + r
  8590. **
  8591. ** Furthermore, the remainder follows the mathematical definition of the
  8592. ** "modulo" operator, namely that the remainder will have the same sign
  8593. ** as b and that
  8594. **
  8595. **       0 <= abs(r) < abs(b)
  8596. **
  8597. ** Another way to look at this is that the quotient is the real quotient,
  8598. ** rounded down to the nearest integer.
  8599. **
  8600. ** For example:
  8601. **
  8602. **       a   b     q   r     a =  b *  q +  r     a/b   rounded
  8603. **      ==  ==    ==  ==    =================    ====   =======
  8604. **       7   3     2   1     7 =  3 *  2 +  1     2.3      2
  8605. **      -7   3    -3   2    -7 =  3 * -3 +  2    -2.3     -3
  8606. **       7  -3    -3  -2     7 = -3 * -3 + -2    -2.3     -3
  8607. **      -7  -3     2  -1    -7 = -3 *  2 + -1     2.3      2
  8608. **
  8609. ** This routine modifies global variables "q" and "r".  If b=0 it
  8610. ** sets q and r to zero and returns immediately.
  8611. **
  8612. ** With this definition of "q" and "r", overflow can and will occur in only
  8613. ** one situation.  Assuming that we are using 32-bit signed integers, the
  8614. ** following inputs cause a problem...
  8615. **      a = -2147483648
  8616. **      b = -1
  8617. ** The mathematically correct answer is...
  8618. **      q = +2147483648
  8619. **      r = 0
  8620. ** Unfortunately, this value of q is not representable.  The underlying
  8621. ** implementation of the C operators / and % will normally fail, and will
  8622. ** quietly return the wrong answer...
  8623. **      q = -2147483648
  8624. **      r = 0
  8625. ** This routine will simply return these incorrect values.
  8626. **
  8627. ** The C language does not define the / and % operators precisely, but
  8628. ** only requires that a = b*q + r be true.  This routine is designed to
  8629. ** return consistent, "correct" answers, regardless of the underlying
  8630. ** implementation of / and %.
  8631. **
  8632. ** Typical variations in integer division are...
  8633. **
  8634. ** (1) "r" is always non-negative.  0 <= r < abs(b)
  8635. **     "q" will be negative when either a or b (but not both) are negative.
  8636. **         a   b     q   r     a =  b *  q +  r
  8637. **        ==  ==    ==  ==    =================
  8638. **         7   3     2   1     7 =  3 *  2 +  1
  8639. **        -7   3    -3   2    -7 =  3 * -3 +  2
  8640. **         7  -3    -2   1     7 = -3 * -2 +  1
  8641. **        -7  -3     3   2    -7 = -3 *  3 +  2
  8642. **
  8643. ** (2) Real division, rounded toward zero.
  8644. **     "q" = a/b, rounded toward zero.
  8645. **     "q" will be negative when either a or b (but not both) are negative.
  8646. **     The sign of "r" will be the same as the sign of "a".
  8647. **         a   b     q   r     a =  b *  q +  r     a/b   rounded
  8648. **        ==  ==    ==  ==    =================    ====   =======
  8649. **         7   3     2   1     7 =  3 *  2 +  1     2.3      2
  8650. **        -7   3    -2  -1    -7 =  3 * -2 + -1    -2.3     -2
  8651. **         7  -3    -2   1     7 = -3 * -2 +  1    -2.3     -2
  8652. **        -7  -3     2  -1    -7 = -3 *  2 + -1     2.3      2
  8653. **
  8654. ** (3) Real division, rounded toward negative infinity.
  8655. **     "q" = a/b, rounded toward negative infinity.
  8656. **     This results in "r" being the mathematically correct "modulo".
  8657. **     "q" will be negative when either a or b (but not both) are negative.
  8658. **     "r" will be negative whenever "b" is negative.
  8659. **
  8660. ** This routine implements option number (3).  It works assuming that
  8661. ** the underlying C implementation uses options (1), (2), or (3).
  8662. **
  8663. ** Overflow cannot occur in this routine, assuming 2's complement
  8664. ** representation of integers.
  8665. */
  8666. void divide (int a, int b) {
  8667.   if (b==0) {
  8668.     q = r = 0;
  8669.     return;
  8670.   }
  8671.   q = a/b;
  8672.   r = a%b;
  8673.   if (b>0) {
  8674.     if (r<0) {
  8675.       q--;        /* Overflow iff q=MIN; but then b=1 and r=0... can't be. */
  8676.       r = r + b;  /* r is neg, b is pos; cannot overflow. */
  8677.     }
  8678.   } else {
  8679.     if (r>0) {
  8680.       q--;        /* Overflow iff q=MIN; but then b=1 and r=0... can't be. */
  8681.       r = r + b;  /* r is pos, b is neg; cannot overflow. */
  8682.     }
  8683.   }
  8684. }
  8685.  
  8686.  
  8687.  
  8688. /* turnOnTerminal ()
  8689. **
  8690. ** This routine is called before BLITZ instruction execution begins.
  8691. ** Terminal input may come from "stdin", which is normally used by
  8692. ** the emulator itself.  If input is coming from stdin, this routine
  8693. ** sets up the terminal by turning off buffering and echoing, so that
  8694. ** it will behave like an "ideal BLITZ terminal".  If input is coming
  8695. ** from a file, then this routine does nothing.
  8696. **
  8697. ** NOTE: We assume that the Emulator is running under Unix and that the
  8698. ** terminal is normally running with "stty -raw echo".
  8699. */
  8700. void turnOnTerminal () {
  8701.   if (termInputFile == stdin) {
  8702.     clearerr (stdin);  /* clear previous control-D/EOFs */
  8703.     if (terminalWantRawProcessing) {
  8704.       terminalInRawMode = 1;
  8705.       system ("stty raw -echo");
  8706.     }
  8707.   }
  8708. }
  8709.  
  8710.  
  8711.  
  8712. /* turnOffTerminal ()
  8713. **
  8714. ** This routine is called when execution of BLITZ instructions is complete.
  8715. ** If the terminal was in raw mode, it restores it so that it will
  8716. ** behave normally.
  8717. */
  8718. void turnOffTerminal () {
  8719.   if (terminalInRawMode) {
  8720.     /* Change back to cooked mode */
  8721.     terminalInRawMode = 0;
  8722.     system ("stty -raw echo");
  8723.   }
  8724.   /* Clear any pending characters... */
  8725. // The following statement should disabled to support automated vm testing...
  8726.   fseek (stdin, 0l, SEEK_SET);
  8727.   clearerr (stdin);  /* clear previous control-D/EOFs */
  8728. }
  8729.  
  8730.  
  8731.  
  8732. /* checkForInput (waitForKeystroke) -> char
  8733. **
  8734. ** This routine checks to see if a character has appeared on the input.
  8735. ** If so, it returns the character.  If "waitForKeystroke" is true, this
  8736. ** routine will wait for a keystroke; otherwise it will return 0 without
  8737. ** waiting.  
  8738. **
  8739. ** If the input is coming from stdin, this routine checks to see if the
  8740. ** input character happens to be control-C; if so, it is processed
  8741. ** immediately (by setting a flag) and zero is returned.
  8742. **
  8743. ** If we have characters buffered in the type-ahead buffer, then these
  8744. ** are used before getting characters from stdin.  The characters may also
  8745. ** be coming from a file besides stdin.
  8746. */
  8747. char checkForInput (int waitForKeystroke) {
  8748.   int i, j;
  8749.   char ch, other;
  8750. #define TERM_BUFF_SIZE 1
  8751.   char terminalBuffer [TERM_BUFF_SIZE];
  8752.  
  8753.   /* Print the current typeAheadBuffer... */
  8754.   //  printf ("  typeAheadBuffer = ");
  8755.   //  printTypeAheadBuffer ();
  8756.  
  8757.   /* See if input comes from stdin and we have something in the type-ahead
  8758.      buffer... */
  8759.   if (typeAheadBufferCount > 0) {
  8760.     ch = typeAheadBuffer [typeAheadBufferOut++];
  8761.     if (typeAheadBufferOut >= TYPE_AHEAD_BUFFER_SIZE) {
  8762.       typeAheadBufferOut = 0;
  8763.     }
  8764.     typeAheadBufferCount--;
  8765.     return ch;
  8766.   }
  8767.  
  8768.   /* Return immediately if input=stdin & we don't want to wait & no char is avail. */
  8769.   if (termInputFile == stdin) {
  8770.     if (!waitForKeystroke)  {
  8771.       if (!characterAvailableOnStdin ()) {
  8772.         // printf ("  returning(stdin,noWait,nothingReady) \"\\0\"...\n");
  8773.         return 0;
  8774.       }
  8775.     }
  8776.   }
  8777.  
  8778.   if (feof (termInputFile)) {
  8779.     if (termInputFile == stdin) {
  8780.       printf ("\n\r*****  EOF on input ignored: Use Control-C to halt execution  *****\n\r");
  8781.       fseek (stdin, 0l, SEEK_SET);
  8782.       return 0;
  8783.     } else {
  8784.       if (waitForKeystroke) {
  8785.         printf ("\n\r*****  EOF encountered on input  *****\n\r");
  8786.         controlCPressed = 1;
  8787.         return 0;
  8788.       } else {
  8789.         /* No further input is available... */
  8790.         timeOfNextSerialInEvent = MAX;
  8791.         return 0;
  8792.       }
  8793.     }
  8794.   }
  8795.  
  8796.   i = fread (terminalBuffer, 1, 1 /*TERM_BUFF_SIZE*/, termInputFile);
  8797.  
  8798.   if (feof (termInputFile) && termInputFile == stdin) {
  8799.     printf ("\n\r*****  EOF on input ignored: Use Control-C to halt execution  *****\n\r");
  8800.     fseek (stdin, 0l, SEEK_SET);
  8801.     return 0;
  8802.   }
  8803.  
  8804.   if (i >= 1) {
  8805.     ch = terminalBuffer [0];
  8806.   } else {
  8807.     return 0;
  8808.   }
  8809.  
  8810.   /* If not in raw mode, then put the remaining characters on this line
  8811.      into the typeAheadBuffer. */
  8812.   if (!terminalInRawMode && ch != '\n') {
  8813.     while (1) {
  8814.       i = fread (terminalBuffer, 1, 1 /*TERM_BUFF_SIZE*/, termInputFile);
  8815.       if (i == 0) break;
  8816.       other = terminalBuffer [0];
  8817.       /* printf ("    Adding 0x%02X to buffer\n", other); */
  8818.       addToTypeAhead (other);
  8819.       if (other == '\n')  break;
  8820.     }
  8821.   }
  8822.  
  8823.   if (termInputFile == stdin) {
  8824.     if (ch == 3) {   /* if ch = Control-C... */
  8825.       printf ("\n\r*****  Control-C  *****\n\r");  /* raw mode: use \n\r... */
  8826.       controlCPressed = 1;
  8827.       // printf ("  returning (ch == contgrol-C): \"\\0\"...\n");
  8828.       return 0;
  8829.     }
  8830.   }
  8831.  
  8832.   return ch;
  8833. }
  8834.  
  8835.  
  8836.  
  8837. /* characterAvailableOnStdin ()
  8838. **
  8839. ** Return true if there is a character available for reading on stdin,
  8840. ** and return false otherwise.  Do not wait.  This function works by calling
  8841. ** the Unix function "select".  This function requires <fcntl.h>.
  8842. */
  8843. int characterAvailableOnStdin () {
  8844.   int numbits = 32;
  8845.   int rfd = 1;
  8846.   int wfd = 0;
  8847.   int xfd = 0;
  8848.   int retval;
  8849.   struct timeval pollTime;
  8850.   pollTime.tv_sec = 0;
  8851.   pollTime.tv_usec = 0;
  8852.   retval = select (numbits,
  8853.                    (fd_set *) &rfd,
  8854.                    (fd_set *) &wfd,
  8855.                    (fd_set *) &xfd,
  8856.                    &pollTime);
  8857.   if ((retval != 1) && (retval != 0)) {
  8858.     fatalError ("Problems with Unix function select");
  8859.   }
  8860.   return retval;
  8861. }
  8862.  
  8863.  
  8864.  
  8865. /* initializeDisk ()
  8866. **
  8867. ** This routine initializes the disk file and variables associated with the DISK.
  8868. */
  8869. void initializeDisk () {
  8870.   long len;
  8871.   int magic, length;
  8872.  
  8873.   currentDiskSector = 0;
  8874.   currentDiskStatus = OPERATION_COMPLETED_OK;
  8875.   futureDiskStatus = OPERATION_COMPLETED_OK;
  8876.   diskTrackCount = 0;
  8877.   diskSectorCount = 0;
  8878.   diskBufferLow = 0;
  8879.   diskBufferHigh = 0;
  8880.   diskMemoryAddressRegister = 0x00000000;
  8881.   diskSectorNumberRegister = 0x00000000;
  8882.   diskSectorCountRegister = 0x00000000;
  8883.  
  8884.   /* Close the DISK file if open... */
  8885.   if (diskFile != NULL) {
  8886.     fclose (diskFile);
  8887.   }
  8888.  
  8889.   /* Open the DISK file for updating... */
  8890.   diskFile = fopen (diskFileName, "r+");
  8891.   if (diskFile == NULL) {
  8892.     fprintf (stderr,
  8893.              "Error in DISK File: File \"%s\" could not be opened for updating.  Simulated disk I/O has been disabled!\n",
  8894.              diskFileName);
  8895.  
  8896.     return;
  8897.   }
  8898.  
  8899.   /* Get the size of the DISK file... */
  8900.   errno = 0;
  8901.   if (fseek (diskFile, 0l, SEEK_END)) {
  8902.     if (errno) perror ("Error on DISK file");
  8903.     fatalError ("Error during call to fseek");
  8904.   }
  8905.   len = ftell (diskFile);
  8906.  
  8907.   /* Check to see if the file is too big... */
  8908.   if (len > ((long) MAX)) {
  8909.     fprintf (stderr, "The  maximum integer is %d.\n", MAX);
  8910.     fprintf (stderr, "Error in DISK File: The DISK file size exceeds the maximum; You may use the 'format' command to change the file.\n");
  8911.     errno = 0;
  8912.     fclose (diskFile);
  8913.     if (errno) perror ("Error closing DISK file");
  8914.     diskFile = NULL;
  8915.     return;
  8916.   }
  8917.  
  8918.   length = (int) len;
  8919.   /* printf ("The length of the file is %d\n", length); */
  8920.  
  8921.   /* Check for bad file length and abort... */
  8922.   if (length < (SECTORS_PER_TRACK * PAGE_SIZE + 4)) {
  8923.     fprintf (stderr, "The minimum DISK file size is 1 track + 4 bytes (where PageSize = %d bytes and SectorsPerTrack = %d).\n", PAGE_SIZE, SECTORS_PER_TRACK);
  8924.     fprintf (stderr, "Error in DISK File: The DISK file must be large enough for at least 1 track.\n");
  8925.     errno = 0;
  8926.     fclose (diskFile);
  8927.     if (errno) perror ("Error closing DISK file");
  8928.     diskFile = NULL;
  8929.     return;
  8930.   }
  8931.  
  8932.   /* Check for bad file length and abort... */
  8933.   diskSectorCount = (length - 4) / PAGE_SIZE;
  8934.   if (diskSectorCount * PAGE_SIZE + 4 != length) {
  8935.     fprintf (stderr, "PageSize = %d bytes, SectorsPerTrack = %d, DISK file size = %d.\n", PAGE_SIZE, SECTORS_PER_TRACK, length);
  8936.     fprintf (stderr, "Error in DISK File: The DISK file size is not an even number of tracks plus 4 bytes\n");
  8937.     errno = 0;
  8938.     fclose (diskFile);
  8939.     if (errno) perror ("Error closing DISK file");
  8940.     diskFile = NULL;
  8941.     return;
  8942.   }
  8943.  
  8944.   diskTrackCount = diskSectorCount / SECTORS_PER_TRACK;
  8945.   /* printf ("diskSectorCount = %d\n", diskSectorCount); */
  8946.   /* printf ("diskTrackCount = %d\n", diskTrackCount); */
  8947.  
  8948.   /* Reposition the DISK file to the beginning. */
  8949.   errno = 0;
  8950.   if (fseek (diskFile, 0l, SEEK_SET)) {
  8951.     if (errno) perror ("Error on DISK file");
  8952.     fclose (diskFile);
  8953.     diskFile = NULL;
  8954.     return;
  8955.   }
  8956.  
  8957.   /* Check the magic number. */
  8958.   magic = readInteger (diskFile);
  8959.   if (magic != 0x424C5A64) {
  8960.     fprintf (stderr, "Error in DISK File: Magic number is not 'BLZd'\n");
  8961.     errno = 0;
  8962.     fclose (diskFile);
  8963.     if (errno) perror ("Error closing DISK file");
  8964.     diskFile = NULL;
  8965.     return;
  8966.   }
  8967.  
  8968. }
  8969.  
  8970.  
  8971.  
  8972. /* performDiskIO (command)
  8973. **
  8974. ** This routine is called whenever the program writes into the DISK_COMMAND_WORD.
  8975. ** It will determine the what operation is called for and perform all error
  8976. ** checking.  It will then perform the actual operation, reading from or writing to
  8977. ** the DISK file.  Finally, it will determine what outcome to simulate (e.g., 
  8978. ** OPERATION_COMPLETED_OK, OPERATION_COMPLETED_WITH_ERROR_3, etc).  It will
  8979. ** schedule an event in the future.  At that future event, an interrupt will occur
  8980. ** and the disk status will then change.
  8981. */
  8982. void performDiskIO (int command) {
  8983.   int seekTime, accessTime, futureTime, currentAngle, desiredAngle, angleChange, transferTime;
  8984.  
  8985.   /* Make sure the DISK is working... */
  8986.   if (diskFile == NULL) {
  8987.     fprintf (stderr, "\n\rERROR:  A store into the DISK_COMMAND_WORD has occurred, but the DISK is currently disabled.  (See the \"format\" command.)\n\r");
  8988.     controlCPressed = 1;
  8989.     return;
  8990.   }
  8991.  
  8992.   /* Make sure the command is legal... */
  8993.   if (command == DISK_READ_COMMAND) {
  8994.     // fprintf (stderr, "\n\rNOTE: A disk read command has been initiated...\n\r");
  8995.   } else if (command == DISK_WRITE_COMMAND) {
  8996.     // fprintf (stderr, "\n\rNOTE: A disk write command has been initiated...\n\r");
  8997.   }  else {
  8998.     currentDiskStatus = DISK_BUSY;
  8999.     futureDiskStatus = OPERATION_COMPLETED_WITH_ERROR_5;
  9000.     timeOfNextDiskEvent = currentTime+1;
  9001.     updateTimeOfNextEvent ();
  9002.     fprintf (stderr, "\n\rERROR: The program has stored an invalid command into the DISK_COMMAND_WORD.  An interrupt will occur when you proceed!\n\r");
  9003.     controlCPressed = 1;
  9004.     return;
  9005.   }
  9006.  
  9007.   currentDiskStatus = DISK_BUSY;
  9008.  
  9009.   /* Check for ERROR 1... */
  9010.   if (diskMemoryAddressRegister % PAGE_SIZE != 0) {
  9011.     fprintf (stderr, "\n\rDISK ERROR: The DISK_MEMORY_ADDRESS_REGISTER is not page-aligned.  An interrupt will occur when you proceed!\n\r");
  9012.     futureDiskStatus = OPERATION_COMPLETED_WITH_ERROR_1;
  9013.     timeOfNextDiskEvent = currentTime+1;
  9014.     updateTimeOfNextEvent ();
  9015.     controlCPressed = 1;
  9016.     return;
  9017.   }
  9018.  
  9019.   /* Make sure the sector count is positive... */
  9020.   if (diskSectorCountRegister <= 0) {
  9021.     fprintf (stderr, "\n\rDISK ERROR: The DISK_SECTOR_COUNT_REGISTER is not positive.  An interrupt will occur when you proceed!\n\r");
  9022.     futureDiskStatus = OPERATION_COMPLETED_WITH_ERROR_1;
  9023.     timeOfNextDiskEvent = currentTime+1;
  9024.     updateTimeOfNextEvent ();
  9025.     controlCPressed = 1;
  9026.     return;
  9027.   }
  9028.  
  9029.   /* Compute the area of memory affected... */
  9030.   diskBufferLow = diskMemoryAddressRegister;
  9031.   diskBufferHigh = diskBufferLow + (diskSectorCountRegister * PAGE_SIZE);
  9032.  
  9033.   /* Make sure the memory buffer is in physical memory... */
  9034.   if (!physicalAddressOk (diskBufferLow) ||
  9035.       !physicalAddressOk (diskBufferHigh-1) ||
  9036.       inMemoryMappedArea (diskBufferLow) ||
  9037.       inMemoryMappedArea (diskBufferHigh-1)) {
  9038.     fprintf (stderr, "\n\rDISK ERROR: The memory buffer is not all in physical memory or is in the memory-mapped region.  An interrupt will occur when you proceed!\n\r");
  9039.     futureDiskStatus = OPERATION_COMPLETED_WITH_ERROR_2;
  9040.     timeOfNextDiskEvent = currentTime+1;
  9041.     updateTimeOfNextEvent ();
  9042.     controlCPressed = 1;
  9043.     return;
  9044.   }
  9045.  
  9046.   /* Check that we are accessing legal disk sectors... */
  9047.   if ((diskSectorNumberRegister < 0) ||
  9048.       (diskSectorNumberRegister + diskSectorCountRegister > diskSectorCount)) {
  9049.     fprintf (stderr, "\n\rDISK ERROR: Attempting to read sectors that do not exist on the disk.  An interrupt will occur when you proceed!\n\r");
  9050.     futureDiskStatus = OPERATION_COMPLETED_WITH_ERROR_3;
  9051.     timeOfNextDiskEvent = currentTime+1;
  9052.     updateTimeOfNextEvent ();
  9053.     controlCPressed = 1;
  9054.     return;
  9055.   }
  9056.  
  9057.   /* See if we need to simulate a random disk I/O error... */
  9058.   if (command == DISK_READ_COMMAND) {
  9059.     if (DISK_READ_ERROR_PROBABILITY > 0) {
  9060.       if (1 == randomBetween (1, DISK_READ_ERROR_PROBABILITY)) {
  9061.         /***
  9062.         fprintf (stderr, "\n\rDISK ERROR: A random disk read error will be simulated at this time.  An interrupt will occur when you proceed!\n\r");
  9063.         controlCPressed = 1;
  9064.         ***/
  9065.         futureDiskStatus = OPERATION_COMPLETED_WITH_ERROR_4;
  9066.         timeOfNextDiskEvent = currentTime+1;
  9067.         updateTimeOfNextEvent ();
  9068.         return;
  9069.       }
  9070.     }
  9071.   } else if (command == DISK_WRITE_COMMAND) {
  9072.     if (DISK_WRITE_ERROR_PROBABILITY > 0) {
  9073.       if (1 == randomBetween (1, DISK_WRITE_ERROR_PROBABILITY)) {
  9074.         /***
  9075.         fprintf (stderr, "\n\rDISK ERROR: A random disk write error will be simulated at this time.  An interrupt will occur when you proceed!\n\r");
  9076.         controlCPressed = 1;
  9077.         ***/
  9078.         futureDiskStatus = OPERATION_COMPLETED_WITH_ERROR_4;
  9079.         timeOfNextDiskEvent = currentTime+1;
  9080.         updateTimeOfNextEvent ();
  9081.         return;
  9082.       }
  9083.     }
  9084.   }
  9085.  
  9086.   /* Everything looks good for a normal disk read/write... */
  9087.   futureDiskStatus = OPERATION_COMPLETED_OK;
  9088.  
  9089.   /* Compute a realistic delay... */
  9090.   // printf ("    Current sector = %d\n", currentDiskSector);
  9091.   // printf ("    Future sector = %d\n", diskSectorNumberRegister);
  9092.   // printf ("    Current track = %d\n", currentDiskSector / SECTORS_PER_TRACK);
  9093.   // printf ("    Future track = %d\n", diskSectorNumberRegister / SECTORS_PER_TRACK);
  9094.   accessTime = DISK_SEEK_TIME * abs (
  9095.                 diskSectorNumberRegister / SECTORS_PER_TRACK -
  9096.                 currentDiskSector / SECTORS_PER_TRACK);
  9097.   // printf ("    Seek time = %d\n", accessTime);
  9098.   if (accessTime) {
  9099.     accessTime = accessTime + DISK_SETTLE_TIME;
  9100.   }
  9101.   // printf ("    Access time = %d\n", accessTime);
  9102.   // printf ("    Current time before seek,settle = %d\n", currentTime);
  9103.   futureTime = currentTime + accessTime;
  9104.   // printf ("    Time after seek,settle = %d\n", futureTime);
  9105.   currentAngle = (futureTime / DISK_ROTATIONAL_DELAY) % SECTORS_PER_TRACK; 
  9106.   // printf ("    Rotational Angle (0..15) at that time = %d\n", currentAngle);
  9107.   desiredAngle = diskSectorNumberRegister % SECTORS_PER_TRACK;
  9108.   // printf ("    Desired Angle (0..15) = %d\n", desiredAngle);
  9109.   angleChange = desiredAngle - currentAngle;
  9110.   if (angleChange < 0) {
  9111.     angleChange = angleChange + SECTORS_PER_TRACK;
  9112.   }
  9113.   // printf ("    Angle Change (0..15) = %d\n", angleChange);
  9114.   futureTime = futureTime + angleChange * DISK_ROTATIONAL_DELAY;
  9115.   // printf ("    Time after seek,settle,rotate = %d\n", futureTime);
  9116.   // printf ("    Number of sectors transfered = %d\n", diskSectorCountRegister);
  9117.   transferTime = diskSectorCountRegister * DISK_ROTATIONAL_DELAY;
  9118.   // printf ("    Transfer time = %d\n", transferTime);
  9119.   futureTime = futureTime + transferTime;
  9120.   // printf ("    Time after seek,settle,rotate,transfer = %d\n", futureTime);
  9121.   futureTime = randomBetween (futureTime, futureTime + DISK_ACCESS_VARIATION);
  9122.   // printf ("    Time after seek,settle,rotate,transfer,random = %d\n", futureTime);
  9123.  
  9124.   // Set up the next disk event, based on the computed time of completion...
  9125.   if (futureTime <= currentTime) {
  9126.     timeOfNextDiskEvent = currentTime + 1;
  9127.   } else {
  9128.     timeOfNextDiskEvent = futureTime;
  9129.   }
  9130.   updateTimeOfNextEvent ();
  9131.  
  9132.   /* Set the new Current Disk Position... */
  9133.   currentDiskSector = diskSectorNumberRegister + diskSectorCountRegister;
  9134.  
  9135.   /* Seek to the proper location in the file... */
  9136.   errno = 0;
  9137.   if (fseek (diskFile, ((long) ((diskSectorNumberRegister * PAGE_SIZE) + 4)), SEEK_SET)) {
  9138.     fprintf (stderr, "\n\rError from host OS during DISK read/fseek; Disk I/O has been disabled!\n\r");
  9139.     if (errno) perror ("Host error");
  9140.     fclose (diskFile);
  9141.     diskFile = NULL;
  9142.     controlCPressed = 1;
  9143.     return;
  9144.   }
  9145.  
  9146.   if (command == DISK_READ_COMMAND) {
  9147.  
  9148.     numberOfDiskReads++;
  9149.  
  9150.     /* Read in N sectors of data... */
  9151.     errno = 0;
  9152.     fread ( (void *) (((int) memory) + diskBufferLow),
  9153.             PAGE_SIZE, diskSectorCountRegister, diskFile );
  9154.     if (errno) {
  9155.       fprintf (stderr, "\n\rError from host OS during DISK read; Disk I/O has been disabled!\n\r");
  9156.       if (errno) perror ("Host error");
  9157.       fclose (diskFile);
  9158.       diskFile = NULL;
  9159.       controlCPressed = 1;
  9160.       return;
  9161.     }
  9162.     return;
  9163.  
  9164.   } else if (command == DISK_WRITE_COMMAND) {
  9165.  
  9166.     numberOfDiskWrites++;
  9167.  
  9168.     /* Write in N sectors of data... */
  9169.     errno = 0;
  9170.     fwrite ( (void *) (((int) memory) + diskBufferLow),
  9171.             PAGE_SIZE, diskSectorCountRegister, diskFile );
  9172.     if (errno) {
  9173.       fprintf (stderr, "\n\rError from host OS during DISK write; Disk I/O has been disabled!\n\r");
  9174.       if (errno) perror ("Host error");
  9175.       fclose (diskFile);
  9176.       diskFile = NULL;
  9177.       controlCPressed = 1;
  9178.       return;
  9179.     }
  9180.     return;
  9181.   }
  9182. }
  9183.  
  9184.  
  9185.  
  9186. /* checkDiskBufferError (physAddr)
  9187. **
  9188. ** This routine checks the address to see if it is within the memory that is being used
  9189. ** for a disk operation.  If it is, then a message is printed and execution is halted.
  9190. */
  9191. void checkDiskBufferError (int physAddr) {
  9192.   if (currentDiskStatus == DISK_BUSY && physAddr >= diskBufferLow && physAddr < diskBufferHigh) {
  9193.     fprintf (stderr, "\n\rMEMORY ACCESS ERROR: The BLITZ program has attempted to read or write a memory word that is currently involved in a disk read or write operation!\n\r");
  9194.     controlCPressed = 1;
  9195.   }
  9196. }
  9197.  
  9198.  
  9199.  
  9200.  
  9201. /* setSimulationConstants ()
  9202. **
  9203. ** This routine initializes some important constants related to the simulation.
  9204. ** It gets values from the ".blitzrc" file if it exists, or uses defaults, if not.
  9205. */
  9206. void setSimulationConstants () {
  9207.   int i, errorInValue;
  9208.   FILE * blitzrc;
  9209.   char * first, * second, * p;
  9210.     errorInValue = 0;
  9211.  
  9212.     defaultSimulationConstants ();
  9213.  
  9214.     // Try to open the ".blitzrc" file...
  9215.     blitzrc = fopen (".blitzrc", "r");
  9216.     if (blitzrc != NULL) {
  9217.       // printf ("Reading simulation values from file \".blitzrc\"...\n");
  9218.  
  9219.       while (! feof (blitzrc)) {
  9220.         fgets (inputBuffer, sizeof(inputBuffer), blitzrc);
  9221.  
  9222.         if (feof (blitzrc)) break;
  9223.  
  9224.         /* Ignore comments... */
  9225.         if (inputBuffer[0] == '!') continue;
  9226.  
  9227.         /* Overwrite the \n with \0 to remove it. */
  9228.         inputBuffer [strlen (inputBuffer)-1] = '\0';
  9229.         // printf ("LINE >>>%s<<<\n", inputBuffer);
  9230.  
  9231.         /* Set "first" to point to the first non-whitespace character. */
  9232.         first = inputBuffer;
  9233.         while (1) {
  9234.           if ((*first != ' ') && (*first != '\t')) {
  9235.             break;
  9236.           }
  9237.           first++;
  9238.         }
  9239.  
  9240.         /* Ignore blank lines... */
  9241.         if (*first == '\0') continue;
  9242.  
  9243.         /* Set "p" to point to the next whitespace or \0 character. */
  9244.         p = first;
  9245.         while (1) {
  9246.           if ((*p == ' ') || (*p == '\t') || (*p == '\0')) {
  9247.             break;
  9248.           }
  9249.           p++;
  9250.         }
  9251.  
  9252.         // printf ("  First Token >>>%s<<<\n", first);
  9253.  
  9254.         if (*p == '\0') {
  9255.           fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  The line beginning \"%s\" has no value!\n", first);
  9256.           errorInValue = 1;
  9257.           continue;
  9258.         }
  9259.  
  9260.         *p = '\0';
  9261.         // printf ("  First Token >>>%s<<<\n", first);
  9262.  
  9263.         /* Set "second" to point to the next non-whitespace character. */
  9264.         second = p+1;
  9265.         while (1) {
  9266.           if ((* second != ' ') && (* second != '\t')) {
  9267.             break;
  9268.           }
  9269.           second ++;
  9270.         }
  9271.  
  9272.         /* Set "p" to point to the next whitespace or \0 character. */
  9273.         p = second;
  9274.         while (1) {
  9275.           if ((*p == ' ') || (*p == '\t') || (*p == '\0')) {
  9276.             break;
  9277.           }
  9278.           p++;
  9279.         }
  9280.  
  9281.         if (*p != '\0') {
  9282.           *p = '\0';
  9283.           /* Make sure that there is nothing but whitespace until the \0 character. */
  9284.           p++;
  9285.           while (*p != '\0') {
  9286.             if ((*p != ' ') && (*p != '\t')) {
  9287.               fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  Extraneous characters (\"%s\") on line beginning \"%s\"!\n", p, first);
  9288.               errorInValue = 1;
  9289.               break;
  9290.             }
  9291.             p++;
  9292.           }
  9293.         }
  9294.  
  9295.         if (*second == '\0') {
  9296.           fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  The line beginning \"%s\" has no value!\n", first);
  9297.           errorInValue = 1;
  9298.           continue;
  9299.         }
  9300.  
  9301.         // printf ("  PROCESSING: First Token >>>%s<<<    ", first);
  9302.         // printf ("  Second Token >>>%s<<<\n", second);
  9303.         if (second[0] == '0' &&
  9304.             second[1] == 'x') {
  9305.           second = second + 2;
  9306.           sscanf (second, "%x", &i);
  9307.         } else {
  9308.           sscanf (second, "%d", &i);
  9309.         }
  9310.         // printf ("------- i = 0x%08X (decimal: %d)\n", i, i);
  9311.  
  9312.         if (!strcmp (first, "KEYBOARD_WAIT_TIME")) {
  9313.           if (i < 0) {
  9314.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  KEYBOARD_WAIT_TIME is negative!\n");
  9315.             errorInValue = 1;
  9316.           } else {
  9317.             KEYBOARD_WAIT_TIME = i;
  9318.           }
  9319.         } else if (!strcmp (first, "KEYBOARD_WAIT_TIME_VARIATION")) {
  9320.           if (i < 0) {
  9321.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  KEYBOARD_WAIT_TIME_VARIATION is negative!\n");
  9322.             errorInValue = 1;
  9323.           } else {
  9324.             KEYBOARD_WAIT_TIME_VARIATION = i;
  9325.           }
  9326.         } else if (!strcmp (first, "TERM_OUT_DELAY")) {
  9327.           if (i < 0) {
  9328.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  TERM_OUT_DELAY is negative!\n");
  9329.             errorInValue = 1;
  9330.           } else {
  9331.             TERM_OUT_DELAY = i;
  9332.           }
  9333.         } else if (!strcmp (first, "TERM_OUT_DELAY_VARIATION")) {
  9334.           if (i < 0) {
  9335.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  TERM_OUT_DELAY_VARIATION is negative!\n");
  9336.             errorInValue = 1;
  9337.           } else {
  9338.             TERM_OUT_DELAY_VARIATION = i;
  9339.           }
  9340.         } else if (!strcmp (first, "TIME_SLICE")) {
  9341.           if (i < 0) {
  9342.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  TIME_SLICE is negative!\n");
  9343.             errorInValue = 1;
  9344.           } else {
  9345.             TIME_SLICE = i;
  9346.           }
  9347.         } else if (!strcmp (first, "TIME_SLICE_VARIATION")) {
  9348.           if (i < 0) {
  9349.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  TIME_SLICE_VARIATION is negative!\n");
  9350.             errorInValue = 1;
  9351.           } else {
  9352.             TIME_SLICE_VARIATION = i;
  9353.           }
  9354.         } else if (!strcmp (first, "DISK_SEEK_TIME")) {
  9355.           if (i < 0) {
  9356.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_SEEK_TIME is negative!\n");
  9357.             errorInValue = 1;
  9358.           } else {
  9359.             DISK_SEEK_TIME = i;
  9360.           }
  9361.         } else if (!strcmp (first, "DISK_SETTLE_TIME")) {
  9362.           if (i < 0) {
  9363.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_SETTLE_TIME is negative!\n");
  9364.             errorInValue = 1;
  9365.           } else {
  9366.             DISK_SETTLE_TIME = i;
  9367.           }
  9368.         } else if (!strcmp (first, "DISK_ROTATIONAL_DELAY")) {
  9369.           if (i < 1) {
  9370.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_ROTATIONAL_DELAY must be >= 1!\n");
  9371.             errorInValue = 1;
  9372.           } else {
  9373.             DISK_ROTATIONAL_DELAY = i;
  9374.           }
  9375.         } else if (!strcmp (first, "DISK_ACCESS_VARIATION")) {
  9376.           if (i < 0) {
  9377.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_ACCESS_VARIATION is negative!\n");
  9378.             errorInValue = 1;
  9379.           } else {
  9380.             DISK_ACCESS_VARIATION = i;
  9381.           }
  9382.         } else if (!strcmp (first, "DISK_READ_ERROR_PROBABILITY")) {
  9383.           if (i < 0) {
  9384.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_READ_ERROR_PROBABILITY is negative!\n");
  9385.             errorInValue = 1;
  9386.           } else {
  9387.             DISK_READ_ERROR_PROBABILITY = i;
  9388.           }
  9389.         } else if (!strcmp (first, "DISK_WRITE_ERROR_PROBABILITY")) {
  9390.           if (i < 0) {
  9391.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_WRITE_ERROR_PROBABILITY is negative!\n");
  9392.             errorInValue = 1;
  9393.           } else {
  9394.             DISK_WRITE_ERROR_PROBABILITY = i;
  9395.           }
  9396.         } else if (!strcmp (first, "INIT_RANDOM_SEED")) {
  9397.           if (i <= 0) {
  9398.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  INIT_RANDOM_SEED is <= 0!\n");
  9399.             errorInValue = 1;
  9400.           } else if (i > 2147483646) {
  9401.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  INIT_RANDOM_SEED is > 2147483646!\n");
  9402.             errorInValue = 1;
  9403.           } else {
  9404.             INIT_RANDOM_SEED = i;
  9405.           }
  9406.         } else if (!strcmp (first, "MEMORY_SIZE")) {
  9407.           /* Check that the MEMORY_SIZE is a multiple of PAGE_SIZE. */
  9408.           if (i % PAGE_SIZE != 0 || i <= 0) {
  9409.             fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  Memory size is not a multiple of page size!\n");
  9410.             errorInValue = 1;
  9411.           } else {
  9412.             MEMORY_SIZE = i;
  9413.           }
  9414.         } else if (!strcmp (first, "MEMORY_MAPPED_AREA_LOW")) {
  9415.           MEMORY_MAPPED_AREA_LOW = i;
  9416.         } else if (!strcmp (first, "MEMORY_MAPPED_AREA_HIGH")) {
  9417.           MEMORY_MAPPED_AREA_HIGH = i;
  9418.         } else if (!strcmp (first, "SERIAL_STATUS_WORD_ADDRESS")) {
  9419.           SERIAL_STATUS_WORD_ADDRESS = i;
  9420.         } else if (!strcmp (first, "SERIAL_DATA_WORD_ADDRESS")) {
  9421.           SERIAL_DATA_WORD_ADDRESS = i;
  9422.         } else if (!strcmp (first, "DISK_STATUS_WORD_ADDRESS")) {
  9423.           DISK_STATUS_WORD_ADDRESS = i;
  9424.         } else if (!strcmp (first, "DISK_COMMAND_WORD_ADDRESS")) {
  9425.           DISK_COMMAND_WORD_ADDRESS = i;
  9426.         } else if (!strcmp (first, "DISK_MEMORY_ADDRESS_REGISTER")) {
  9427.           DISK_MEMORY_ADDRESS_REGISTER = i;
  9428.         } else if (!strcmp (first, "DISK_SECTOR_NUMBER_REGISTER")) {
  9429.           DISK_SECTOR_NUMBER_REGISTER = i;
  9430.         } else if (!strcmp (first, "DISK_SECTOR_COUNT_REGISTER")) {
  9431.           DISK_SECTOR_COUNT_REGISTER = i;
  9432.         } else {
  9433.           fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  An attempt to set non-existent value \"%s\"!\n", first);
  9434.           errorInValue = 1;
  9435.         }
  9436.  
  9437.       }
  9438.  
  9439.       if (MEMORY_MAPPED_AREA_LOW < 0 ||
  9440.           MEMORY_MAPPED_AREA_HIGH >= MEMORY_SIZE ||
  9441.           MEMORY_MAPPED_AREA_LOW >= MEMORY_MAPPED_AREA_HIGH ||
  9442.           MEMORY_MAPPED_AREA_LOW % 4 != 0 ||
  9443.           (MEMORY_MAPPED_AREA_HIGH+1) % 4 != 0) {
  9444.         fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  MEMORY_MAPPED_AREA_HIGH or MEMORY_MAPPED_AREA_LOW are invalid!\n");
  9445.         errorInValue = 1;
  9446.       }
  9447.       if (SERIAL_STATUS_WORD_ADDRESS < MEMORY_MAPPED_AREA_LOW ||
  9448.           SERIAL_STATUS_WORD_ADDRESS > MEMORY_MAPPED_AREA_HIGH ||
  9449.           SERIAL_STATUS_WORD_ADDRESS % 4 != 0) {
  9450.         fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  SERIAL_STATUS_WORD_ADDRESS is not within the Memory Mapped Area or is not word-aligned!\n");
  9451.         errorInValue = 1;
  9452.       }
  9453.       if (SERIAL_DATA_WORD_ADDRESS < MEMORY_MAPPED_AREA_LOW ||
  9454.           SERIAL_DATA_WORD_ADDRESS > MEMORY_MAPPED_AREA_HIGH ||
  9455.           SERIAL_DATA_WORD_ADDRESS % 4 != 0) {
  9456.         fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  SERIAL_DATA_WORD_ADDRESS is not within the Memory Mapped Area or is not word-aligned!\n");
  9457.         errorInValue = 1;
  9458.       }
  9459.       if (DISK_STATUS_WORD_ADDRESS < MEMORY_MAPPED_AREA_LOW ||
  9460.           DISK_STATUS_WORD_ADDRESS > MEMORY_MAPPED_AREA_HIGH ||
  9461.           DISK_STATUS_WORD_ADDRESS % 4 != 0) {
  9462.         fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_STATUS_WORD_ADDRESS is not within the Memory Mapped Area or is not word-aligned!\n");
  9463.         errorInValue = 1;
  9464.       }
  9465.       if (DISK_COMMAND_WORD_ADDRESS < MEMORY_MAPPED_AREA_LOW ||
  9466.           DISK_COMMAND_WORD_ADDRESS > MEMORY_MAPPED_AREA_HIGH ||
  9467.           DISK_COMMAND_WORD_ADDRESS % 4 != 0) {
  9468.         fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_COMMAND_WORD_ADDRESS is not within the Memory Mapped Area or is not word-aligned!\n");
  9469.         errorInValue = 1;
  9470.       }
  9471.       if (DISK_MEMORY_ADDRESS_REGISTER < MEMORY_MAPPED_AREA_LOW ||
  9472.           DISK_MEMORY_ADDRESS_REGISTER > MEMORY_MAPPED_AREA_HIGH ||
  9473.           DISK_MEMORY_ADDRESS_REGISTER % 4 != 0) {
  9474.         fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_MEMORY_ADDRESS_REGISTER is not within the Memory Mapped Area or is not word-aligned!\n");
  9475.         errorInValue = 1;
  9476.       }
  9477.       if (DISK_SECTOR_NUMBER_REGISTER < MEMORY_MAPPED_AREA_LOW ||
  9478.           DISK_SECTOR_NUMBER_REGISTER > MEMORY_MAPPED_AREA_HIGH ||
  9479.           DISK_SECTOR_NUMBER_REGISTER % 4 != 0) {
  9480.         fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_SECTOR_NUMBER_REGISTER is not within the Memory Mapped Area or is not word-aligned!\n");
  9481.         errorInValue = 1;
  9482.       }
  9483.       if (DISK_SECTOR_COUNT_REGISTER < MEMORY_MAPPED_AREA_LOW ||
  9484.           DISK_SECTOR_COUNT_REGISTER > MEMORY_MAPPED_AREA_HIGH ||
  9485.           DISK_SECTOR_COUNT_REGISTER % 4 != 0) {
  9486.         fprintf (stderr, "\n*****  ERROR in \".blitzrc\" file:  DISK_SECTOR_COUNT_REGISTER is not within the Memory Mapped Area or is not word-aligned!\n");
  9487.         errorInValue = 1;
  9488.       }
  9489.  
  9490.       if (errorInValue) {
  9491.         fprintf (stderr, "*****  ERROR in \".blitzrc\" file:  All values in the file have been ignored.\n");
  9492.         defaultSimulationConstants ();
  9493.       }
  9494.  
  9495.       fclose (blitzrc);
  9496.     }
  9497. }
  9498.  
  9499.  
  9500.  
  9501. /* defaultSimulationConstants ()
  9502. **
  9503. ** This routine sets the simulation constants to their default values.
  9504. */
  9505. void defaultSimulationConstants () {
  9506.     KEYBOARD_WAIT_TIME =                 30000;
  9507.     KEYBOARD_WAIT_TIME_VARIATION =         100;
  9508.     TERM_OUT_DELAY =                       100;
  9509.     TERM_OUT_DELAY_VARIATION =              10;
  9510.     TIME_SLICE =                          5000;
  9511.     TIME_SLICE_VARIATION =                  30;
  9512.     DISK_SEEK_TIME =                     10000;
  9513.     DISK_SETTLE_TIME =                    1000;
  9514.     DISK_ROTATIONAL_DELAY =                100;
  9515.     DISK_ACCESS_VARIATION =                 10;
  9516.     DISK_READ_ERROR_PROBABILITY =          500;  // 0=never, 1=always, n="about 1/n"
  9517.     DISK_WRITE_ERROR_PROBABILITY =         500;  // 0=never, 1=always, n="about 1/n"
  9518.     INIT_RANDOM_SEED =              1829742401;  // Default random seed
  9519.     MEMORY_SIZE =                   0x01000000;  // 16 MBytes
  9520.     MEMORY_MAPPED_AREA_LOW =        0x00ffff00;
  9521.     MEMORY_MAPPED_AREA_HIGH =       0x00ffffff;
  9522.     SERIAL_STATUS_WORD_ADDRESS =    0x00ffff00;
  9523.     SERIAL_DATA_WORD_ADDRESS =      0x00ffff04;
  9524.     DISK_STATUS_WORD_ADDRESS =      0x00ffff08;
  9525.     DISK_COMMAND_WORD_ADDRESS =     0x00ffff08;
  9526.     DISK_MEMORY_ADDRESS_REGISTER =  0x00ffff0c;
  9527.     DISK_SECTOR_NUMBER_REGISTER =   0x00ffff10;
  9528.     DISK_SECTOR_COUNT_REGISTER =    0x00ffff14;
  9529. }
  9530.  
  9531.  
  9532.  
  9533. /* printKPLStmtCode ()
  9534. **
  9535. ** This routine looks at the value in r10 and uses to print the name of
  9536. ** some high-level KPL statement, such as "ASSIGN" or "RETURN".
  9537. */
  9538. void printKPLStmtCode () {
  9539.   int stmtCode;
  9540.   if (statusS) {
  9541.     stmtCode = systemRegisters [10];
  9542.   } else {
  9543.     stmtCode = userRegisters [10];
  9544.   }
  9545.   printf ("About to execute "); 
  9546.   switch (stmtCode) {
  9547. //qqqqqqqqqq
  9548.     case '\0\0AS':   printf ("ASSIGN statement                 ");
  9549.                      break;
  9550.     case '\0\0FU':   printf ("FUNCTION ENTRY                   ");
  9551.                      break;
  9552.     case '\0\0ME':   printf ("METHOD ENTRY                     ");
  9553.                      break;
  9554.     case '\0\0IF':   printf ("IF statement                     ");
  9555.                      break;
  9556.     case '\0\0TN':   printf ("THEN statement                   ");
  9557.                      break;
  9558.     case '\0\0EL':   printf ("ELSE statement                   ");
  9559.                      break;
  9560.     case '\0\0CA':   printf ("FUNCTION CALL                    ");
  9561.                      break;
  9562.     case '\0\0CF':   printf ("FUNCTION CALL (via pointer)      ");
  9563.                      break;
  9564.     case '\0\0CE':   printf ("FUNCTION CALL (external function)");
  9565.                      break;
  9566.     case '\0\0SE':   printf ("SEND                             ");
  9567.                      break;
  9568.     case '\0\0WH':   printf ("WHILE LOOP (expr evaluation)     ");
  9569.                      break;
  9570.     case '\0\0WB':   printf ("WHILE LOOP (body statements)     ");
  9571.                      break;
  9572.     case '\0\0DO':   printf ("DO-UNTIL (body statements)       ");
  9573.                      break;
  9574.     case '\0\0DU':   printf ("DO-UNTIL (expr evaluation)       ");
  9575.                      break;
  9576.     case '\0\0BR':   printf ("BREAK statement                  ");
  9577.                      break;
  9578.     case '\0\0CO':   printf ("CONTINUE statement               ");
  9579.                      break;
  9580.     case '\0\0RE':   printf ("RETURN statement                 ");
  9581.                      break;
  9582.     case '\0\0FO':   printf ("FOR statement                    ");
  9583.                      break;
  9584.     case '\0\0FB':   printf ("FOR (body statements)            ");
  9585.                      break;
  9586.     case '\0\0SW':   printf ("SWITCH                           ");
  9587.                      break;
  9588.     case '\0\0TR':   printf ("TRY statement                    ");
  9589.                      break;
  9590.     case '\0\0TH':   printf ("THROW statement                  ");
  9591.                      break;
  9592.     case '\0\0FR':   printf ("FREE statement                   ");
  9593.                      break;
  9594.     case '\0\0DE':   printf ("DEBUG statement                  ");
  9595.                      break;
  9596.     case '\0\0CC':   printf ("CATCH clause                     ");
  9597.                      break;
  9598.     default:         printf ("***INVLALID HIGH-LEVEL STATEMENT CODE IN REGISTER r10***\n");
  9599.                      return;
  9600.   }
  9601.   printCurrentFileLineAndFunction ();
  9602.   printf ("\n");
  9603. }
  9604.  
  9605.  
  9606.  
  9607. /* printCurrentFileLineAndFunction ()
  9608. **
  9609. ** This routine looks at the registers and the current frame and prints
  9610. ** the current file name, the current line number, and the function/method
  9611. ** name like this:
  9612. **        foo (Main.c, line 123)
  9613. ** If any problems, it simply gives up and returns.
  9614. */
  9615. void printCurrentFileLineAndFunction () {
  9616.   int lineNum, fp, physAddr, addrOfRoutineDescriptor, filenamePtr,
  9617.       ptrToFunName;
  9618.  
  9619.   // Get "r13" and "r14/fp"...
  9620.   if (statusS) {
  9621.     lineNum = systemRegisters [13];
  9622.     fp = systemRegisters [14];
  9623.   } else {
  9624.     lineNum = userRegisters [13];
  9625.     fp = userRegisters [14];
  9626.   }
  9627.  
  9628.   if (fp == 0) {
  9629.     printf ("***Invalid FP***\n");
  9630.     return;
  9631.   }
  9632.  
  9633.   // printf ("frame pointer = ");
  9634.   // printNumberNL (fp);
  9635.  
  9636.   // Get ptr to routine descriptor
  9637.   physAddr = translate (fp-8, 1, 0, 0);  // reading=1, wantPrinting=0, doUpdates=0
  9638.   if (translateCausedException) {
  9639.     printf ("***Invalid fp***\n");
  9640.     return;
  9641.   }
  9642.   addrOfRoutineDescriptor = getPhysicalWord (physAddr);
  9643.   // printf ("addrOfRoutineDescriptor = ");
  9644.   // printNumberNL (addrOfRoutineDescriptor);
  9645.  
  9646.   // Get ptr to filename...
  9647.   physAddr = translate (addrOfRoutineDescriptor, 1, 0, 0);
  9648.   if (translateCausedException) {
  9649.     printf ("***Invalid routine descriptor***");
  9650.     return;
  9651.   }
  9652.   filenamePtr = getPhysicalWord (physAddr);
  9653.   // printf ("filenamePtr = ");
  9654.   // printNumberNL (filenamePtr);
  9655.  
  9656.   // Get ptr to function name...
  9657.   physAddr = translate (addrOfRoutineDescriptor+4, 1, 0, 0);
  9658.   if (translateCausedException) {
  9659.     printf ("***Invalid routine descriptor***");
  9660.     return;
  9661.   }
  9662.   ptrToFunName = getPhysicalWord (physAddr);
  9663.   // printf ("ptrToFunName = ");
  9664.   // printNumberNL (ptrToFunName);
  9665.  
  9666.   // Print the function name...
  9667.   printf (" in ");
  9668.   if (!printAsciiDataInWidth (ptrToFunName, 0)) return;
  9669.  
  9670.   // Print the Filename...
  9671.   printf (" (");
  9672.   if (!printAsciiDataInWidth (filenamePtr, 0)) return;
  9673.  
  9674.   // Print the line number...
  9675.   printf (", line %d)", lineNum);
  9676.  
  9677.   // Print the cuurent time...
  9678.   printf ("  time = %d", currentTime);
  9679.  
  9680. }
  9681.