Guest User

Untitled

a guest
Apr 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "Memory.h"
  5. #include "loadMemory.h"
  6. #include "Registers.h"
  7.  
  8. // -------------------------------------------------------
  9. // constants used
  10. // -------------------------------------------------------
  11.  
  12. const char storeCom[] = "store";
  13. const char fetchCom[] = "fetch";
  14. const char printCom[] = "print";
  15. const char quitCom[] = "quit";
  16.  
  17. enum {maxComLen=200};
  18. enum {maxInfoLen = 4};
  19.  
  20. // -------------------------------------------------------
  21. // functions used
  22. // -------------------------------------------------------
  23.  
  24. void runMachine(int, int);
  25. void readln(FILE *f);
  26.  
  27. int main(int argc, const char *argv[])
  28. {
  29. FILE *f;
  30. long numBytes;
  31. int trace, startLocation;
  32.  
  33. if (argc != 2) {
  34. fprintf(stderr, "usage: a.out filename\n");
  35. exit(1);
  36. }
  37.  
  38. if ((f=fopen(argv[1],"rb")) == 0) {
  39. fprintf(stderr, "cannot open [%s] for input\n", argv[1]);
  40. exit(1);
  41. }
  42.  
  43. fprintf(stdout, "size of memory in bytes ?> ");
  44. fscanf(stdin, "%ld", &numBytes);
  45.  
  46. if (numBytes < 1) {
  47. fprintf(stderr, "bad number of bytes [%ld]\n", numBytes);
  48. exit(1);
  49. }
  50.  
  51. if (!allocateMemory(numBytes)) {
  52. fprintf(stderr, "memory allocation error\n");
  53. exit(1);
  54. }
  55.  
  56. if (!loadMemory(f,0)) {
  57. fprintf(stderr, "error loading memory\n");
  58. exit(1);
  59. }
  60.  
  61. fclose(f);
  62.  
  63. fprintf(stdout, "starting address for program ?> ");
  64. fscanf(stdin, "%d", &startLocation);
  65.  
  66. if ( startLocation < 0){
  67. fprintf(stderr, "bad start address loading memory\n");
  68. exit(1);
  69. }
  70.  
  71. fprintf(stdout, "boolean (1/0) for trace (on/off) ?> ");
  72. fscanf(stdin, "%d", &trace);
  73.  
  74. if (!(trace == 1 || trace == 0)){
  75. fprintf(stderr, "bad input for trace\n");
  76. exit(1);
  77. }
  78.  
  79. runMachine(startLocation,trace);
  80.  
  81. deallocateMemory();
  82.  
  83. return 0;
  84. }
  85.  
  86. // -------------------------------------------------------------
  87. /* this is the interactive portion of the program - it allows
  88. * a specified number of bytes of information to be stored into
  89. * memory, fetched from memory, or dumped.
  90. */
  91.  
  92. void runMachine(int start, int trace)
  93. {
  94. int pc = start;
  95.  
  96. while(fetch_exec(&pc));
  97.  
  98. }
  99.  
  100. // -------------------------------------------------------------
  101. /* this procedure inputs characters from the specified file until
  102. * end of line is reached
  103. */
  104.  
  105. void readln(FILE *f)
  106. {
  107. while (fgetc(f) != '\n');
  108. }
Add Comment
Please, Sign In to add comment