Advertisement
Guest User

C4C 1 Virtual Machine

a guest
Jun 11th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. unsigned int ROM[256] = {0x3FC02000, //Contents of ROM - 30 bits used
  4.                          0x3FA00020,
  5.                          0x3FE00000,
  6.                          0x3EC0A000,
  7.                          0x3EA02000,
  8.                          0x3E000000};  
  9. unsigned int RAM[256];  //Contents of RAM - 30 bits used
  10. unsigned char REG[16];  //Registers AX - PX - 8 bits used
  11. unsigned char INST = 0; //Instruction pointer - 8 bits used
  12. unsigned int currInst;  //Current instruction loaded
  13. unsigned int state = 1; //State of the computer, on or off
  14. unsigned char OPERANDA;  //Operand A
  15. unsigned char OPERANDB;  //Operand B
  16. unsigned char getData = '\0'; //Character received from input
  17.  
  18. void main() {
  19.  //Need to understand file descriptors for STDIN and normal file to read stuff
  20.  
  21.     //Walking through each instruction and looping on overflow
  22.     while (INST <= 256 && state == 1) {
  23.         //printf("%d\n", INST); //Debug
  24.         currInst = ROM[INST];       //Load instruction from ROM and increment instruction pointer
  25.         ++INST;
  26.        
  27.         if ((0x3E000000 & currInst) == 0x3E000000) {    //If this is an instruction, identify instruction and execute it
  28.             OPERANDA = (currInst << 11) >> 24;          //Get operand A value - from INST OPERANDA, OPERANDB
  29.             OPERANDB = (currInst << 19) >> 24;          //Get operand B value
  30.             //printf("Cell: %d Inst: %x Operand A: %x Operand B: %x\n", INST - 1, ((currInst << 7) >> 28), OPERANDA, OPERANDB); //Debug
  31.  
  32.             switch ((currInst << 7) >> 28) {
  33.                 case 0:         //HALT
  34.                     state = 0;
  35.                     break;
  36.                 case 1:         //MOV
  37.                     REG[OPERANDA] = REG[OPERANDB];
  38.                     break;
  39.                 case 2:         //STORE
  40.                     RAM[OPERANDA] = REG[OPERANDB];
  41.                     break;
  42.                 case 3:         //LOADRAM
  43.                     REG[OPERANDA] = RAM[OPERANDB];
  44.                     break;
  45.                 case 4:         //LOADROM
  46.                     REG[OPERANDA] = ROM[OPERANDB];
  47.                     break;
  48.                 case 5:         //JMP
  49.                     INST = OPERANDA;
  50.                     break;
  51.                 case 6:         //JZ
  52.                     if (REG[OPERANDB] == 0) {
  53.                         INST = OPERANDA;
  54.                     }
  55.                     break;
  56.                 case 7:         //JNZ
  57.                     if (REG[OPERANDB] != 0) {
  58.                         INST = OPERANDA;
  59.                     }
  60.                     break;
  61.                 case 8:         //OR
  62.                     REG[OPERANDA] |= REG[OPERANDB];
  63.                     break;
  64.                 case 9:         //AND
  65.                     REG[OPERANDA] &= REG[OPERANDB];
  66.                     break;
  67.                 case 10:            //NOT
  68.                     ~REG[OPERANDA];
  69.                     break;
  70.                 case 11:            //SHL
  71.                     REG[OPERANDA] << 1;
  72.                     break;
  73.                 case 12:            //SHR
  74.                     REG[OPERANDA] >> 1;
  75.                     break;
  76.                 case 13:            //ADD
  77.                     REG[OPERANDA] += REG[OPERANDB];
  78.                     break;
  79.                 case 14:            //RECV
  80.                     while (!(getData >= 'a' && getData <= 'f') && !(getData >= 'A' && getData <= 'F') && !(getData >= '0' && getData <= '9')) {
  81.                         getData = getchar();   
  82.                     }
  83.                     if (getData >= 'a' && getData <= 'f') {
  84.                         getData -= ('a' - 0xA);
  85.                     }
  86.                     if (getData >= 'A' && getData <= 'F') {
  87.                         getData -= ('A' - 0xA);
  88.                     }
  89.                     if (getData >= '0' && getData <= '9') {
  90.                         getData -= '0';
  91.                     }
  92.                     REG[OPERANDA] = getData;
  93.                     getData = '\0';
  94.                     rewind(stdin);
  95.                     break;
  96.                 case 15:            //SEND
  97.                     printf("%x\n", REG[OPERANDA]);
  98.                     break;
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement