#include #include #include "CAL16.mem.h" #define MAXBYTEADDR 65535 /* Memory state of the machine */ static unsigned char mem[MAXBYTEADDR]; int toInt (FILE *); int fromChar (char); /* Fill memory with the contents of a file */ void fillMem (FILE *in) { int n; int wordAddr; for (wordAddr=0; (n=toInt(in))>=0; wordAddr++) { toMem (wordAddr*2, n); } } /* * Return the word of memory at addresses byteAddr and byteAddr+1. */ unsigned int fromMem (unsigned int byteAddr) { byteAddr = byteAddr & 0xffff; if (byteAddr & 1) { fprintf (stderr, "Load alignment error: address = %x\n", byteAddr); exit (1); } return (mem[byteAddr] << 8) | mem[byteAddr+1]; } /* * Store the given value at the addresses byteAddr and byteAddr+1. */ void toMem (unsigned int byteAddr, unsigned int value) { byteAddr = byteAddr & 0xffff; if (byteAddr & 1) { fprintf (stderr, "Store alignment error: address = %x\n", byteAddr); exit (1); } mem[byteAddr] = value >> 8; mem[byteAddr+1] = value & 0xff; } /* * Return the integer representation of the four-character string of hex digits. */ int toInt (FILE *in) { char line[6]; int k, digitVal, result; char *linesRemain; linesRemain = (char *) fgets (line, 6, in); if (!linesRemain) { return -1; } for (k=0; k<4; k++) { if (line[k] == '\n') { fprintf (stderr, "Line '%s' not four characters long.\n", line); exit (1); } } if (line[4] != '\n') { fprintf (stderr, "Line '%s' not four characters long.\n", line); exit (1); } for (result=0, k=0; k<4; k++) { digitVal = fromChar (line[k]); result = result * 16 + digitVal; } return result; } /* * Return the binary value represented by the hex digit c. */ int fromChar (char c) { if (c >= 'A' && c <= 'F') { c = c - 'A' + 'a'; } switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return c - '0'; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': return 10 + c - 'a'; default: fprintf (stderr, "%c isn't a hex digit\n", c); exit (1); } }