Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "Memory.h"
- #include "loadMemory.h"
- #include "Registers.h"
- // -------------------------------------------------------
- // constants used
- // -------------------------------------------------------
- const char storeCom[] = "store";
- const char fetchCom[] = "fetch";
- const char printCom[] = "print";
- const char quitCom[] = "quit";
- enum {maxComLen=200};
- enum {maxInfoLen = 4};
- // -------------------------------------------------------
- // functions used
- // -------------------------------------------------------
- void runMachine(int, int);
- void readln(FILE *f);
- int main(int argc, const char *argv[])
- {
- FILE *f;
- long numBytes;
- int trace, startLocation;
- if (argc != 2) {
- fprintf(stderr, "usage: a.out filename\n");
- exit(1);
- }
- if ((f=fopen(argv[1],"rb")) == 0) {
- fprintf(stderr, "cannot open [%s] for input\n", argv[1]);
- exit(1);
- }
- fprintf(stdout, "size of memory in bytes ?> ");
- fscanf(stdin, "%ld", &numBytes);
- if (numBytes < 1) {
- fprintf(stderr, "bad number of bytes [%ld]\n", numBytes);
- exit(1);
- }
- if (!allocateMemory(numBytes)) {
- fprintf(stderr, "memory allocation error\n");
- exit(1);
- }
- if (!loadMemory(f,0)) {
- fprintf(stderr, "error loading memory\n");
- exit(1);
- }
- fclose(f);
- fprintf(stdout, "starting address for program ?> ");
- fscanf(stdin, "%d", &startLocation);
- if ( startLocation < 0){
- fprintf(stderr, "bad start address loading memory\n");
- exit(1);
- }
- fprintf(stdout, "boolean (1/0) for trace (on/off) ?> ");
- fscanf(stdin, "%d", &trace);
- if (!(trace == 1 || trace == 0)){
- fprintf(stderr, "bad input for trace\n");
- exit(1);
- }
- runMachine(startLocation,trace);
- deallocateMemory();
- return 0;
- }
- // -------------------------------------------------------------
- /* this is the interactive portion of the program - it allows
- * a specified number of bytes of information to be stored into
- * memory, fetched from memory, or dumped.
- */
- void runMachine(int start, int trace)
- {
- int pc = start;
- while(fetch_exec(&pc));
- }
- // -------------------------------------------------------------
- /* this procedure inputs characters from the specified file until
- * end of line is reached
- */
- void readln(FILE *f)
- {
- while (fgetc(f) != '\n');
- }
Add Comment
Please, Sign In to add comment