Guest User

Untitled

a guest
Jan 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define LIMIT 1000
  5.  
  6. #define READ 10
  7. #define SAVE 11
  8.  
  9. #define LOAD 20
  10. #define STORE 21
  11.  
  12. #define ADD 30
  13. #define SUBTRACT 31
  14. #define DIVIDE 32
  15. #define MULTIPLY 33
  16.  
  17. #define BRANCH 40
  18. #define BRANCHNEG 41
  19. #define BRANCHZERO 42
  20. #define HALT 43
  21.  
  22. #define VALID 0
  23.  
  24. //errors
  25. #define DIVIDEZERO 1
  26. #define INVALID 2
  27. #define INVALIDWORD 3
  28. #define INVALIDOPCODE 4
  29. #define ILLEGALINPUT 5
  30. #define OVERFLOW 6
  31. #define PRGTOOBIG 7
  32. #define UNDERFLOW 8
  33.  
  34.  
  35.  
  36. int init(int *memory, int *accumulator, int *instructionCounter,
  37.         int *instructionRegister, int *validInstructions, int *opCode,
  38.         int *operand);
  39.  
  40. int loadInstruction(int *memory, int *memPointer, int *maxMemory, int *machineOn);
  41.  
  42. int executeInstruction(int *instructionRegister, int *memory, int *opCode,
  43.                        int *operand, int *instructionCounter,
  44.                        int *accumulator, int *validInstructions,
  45.                        int *maxMemory, int *machineOn);
  46.  
  47. int memDump(int *memory, int *maxMemory);
  48.  
  49. int main(void){
  50.  
  51.     int *memory = (int*) malloc(1000 * (sizeof(int)));
  52.  
  53.     int x, accumulator, instructionCounter, instructionRegister;
  54.     int opCode, operand, validInstructions;
  55.  
  56.     int memPointer, machineOn, maxMemory = LIMIT;
  57.  
  58.     printf("\tSimpletron Virtual Machine\n");
  59.  
  60.     init(memory, &accumulator, &instructionCounter, &instructionRegister,
  61.         &validInstructions, &opCode, &operand);
  62.  
  63.     x = loadInstruction(memory, &memPointer, &maxMemory, &machineOn);
  64.  
  65.     if(x == PRGTOOBIG){
  66.         printf("\n.::ABEND: prg load: prg too big::.\n\n");
  67.         printf("\tRegisters\nAccumulator: %d\nInstruction Counter: %d\n"
  68.                "Instruction Register: %d\nOperation Code: %d\n"
  69.                "Operand: %d\nValid Instruction: %d\n\n", accumulator, instructionCounter,
  70.                instructionRegister, opCode, operand, validInstructions);
  71.     }
  72.     else if(x == INVALIDWORD){
  73.         printf("\n.::ABEND: prg load: invalid word::.\n\n");
  74.         printf("\tRegisters\nAccumulator: %d\nInstruction Counter: %d\n"
  75.                "Instruction Register: %d\nOperation Code: %d\n"
  76.                "Operand: %d\nValid Instruction: %d\n\n", accumulator, instructionCounter,
  77.                instructionRegister, opCode, operand, validInstructions);
  78.     }
  79.     else{
  80.         executeInstruction(&instructionRegister, &memory, &opCode, &operand,
  81.                        &instructionCounter, &accumulator, &validInstructions,
  82.                        &maxMemory, &machineOn);
  83.     }
  84.  
  85.     // only continue if machineOn = 1;
  86.  
  87.     return 0;
  88. }
  89.  
  90. int init(int *memory, int *accumulator, int *instructionCounter, int *instructionRegister,
  91.         int *validInstructions, int *opCode,
  92.         int *operand){
  93.  
  94.     int i;
  95.  
  96.     for(i = 0; i < LIMIT; i++){
  97.         memory[i] = 50505;
  98.     }
  99.  
  100.     *accumulator = 0, *instructionCounter = 0, *instructionRegister = 0;
  101.     *opCode = 0, *operand = 0, *validInstructions = 0;
  102.  
  103.     return VALID;
  104. }
  105.  
  106. int loadInstruction(int *memory, int *memPointer, int *maxMemory, int *machineOn){
  107.  
  108.     int word;
  109.     int i = 0;
  110.  
  111.     while(fscanf(stdin, "%d", &word) != EOF){
  112.         *maxMemory = *memPointer;
  113.         if(word == -999999){
  114.             *machineOn = 1;
  115.             return VALID;
  116.         }
  117.         else if(word < -99999 || word > 99999){
  118.             return INVALIDWORD;
  119.         }
  120.  
  121.         if(i >= LIMIT){
  122.             return PRGTOOBIG;
  123.         }
  124.  
  125.  
  126.         memory[i] = word;
  127.         i++;
  128.         *memPointer += 1;
  129.     }
  130. }
  131.  
  132. int executeInstruction(int *instructionRegister, int *memory, int *opCode, int *operand,
  133.                        int *instructionCounter, int *accumulator, int *validInstructions,
  134.                        int *maxMemory, int *machineOn){
  135.     int i;
  136.     int a;
  137.     int done = 0;
  138.     while (done != 1)
  139.     {
  140.         /* instruction fetch */
  141.         if(0 <= *instructionCounter && *instructionCounter <= 999){
  142.             instructionRegister = memory[*instructionCounter];
  143.             *opCode = *instructionRegister / 1000;
  144.             *operand = *instructionRegister % 1000;
  145.         }
  146.         else if(*instructionCounter < 0 || *instructionCounter > 999){
  147.             printf(".::ABEND: Addressability error::.\n");
  148.             done = 1;
  149.         }
  150.  
  151.         //instruction execute
  152.         switch(*opCode){
  153.             case READ:
  154.                 fscanf(stdin, "%d", &a);
  155.                 if(a < -99999 || a > 99999){
  156.                     return ILLEGALINPUT;
  157.                 }
  158.                 mem[*operand] = a;
  159.                 if(mem[*operand] < 0)
  160.  
  161.                 else
  162.  
  163.                 break;
  164.             case SAVE:
  165.                 break;
  166.             case LOAD:
  167.                 break;
  168.             case STORE:
  169.                 break;
  170.             case ADD:
  171.                 break;
  172.             case SUBTRACT:
  173.                 break;
  174.             case MULTIPLY:
  175.                 break;
  176.             case DIVIDE:
  177.                 break;
  178.             case BRANCH:
  179.                 break;
  180.             case BRANCHNEG:
  181.                 break;
  182.             case BRANCHZERO:
  183.                 break;
  184.             case HALT:
  185.                 done = 1;
  186.                 break;
  187.             default:
  188.                 return INVALIDOPCODE;
  189.                 break;
  190.         }
  191.  
  192.         if(done != 1){
  193.             instructionCounter++;
  194.         }
  195.         /*if (operationCode is not branching AND done != 1)
  196.             instructionCounter++;
  197.     }   end while */
  198.  
  199.     }
  200. }
  201. //"C:\Users\Omid\clanp\assign1\All\03_NORMAL_ADDITION.txt"
  202. int memDump(int *memory, int *maxMemory){
  203.  
  204. }
Add Comment
Please, Sign In to add comment