Advertisement
vinocastro

C-based MIPS Simulator na di gumagana

Jun 19th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. int32_t signext(int32_t immediate) // sign-extend
  6. {
  7.     int32_t value = (0x0000FFFF & immediate);
  8.     if (0x00008000 & immediate)
  9.     {
  10.         value |= 0xFFFF0000;
  11.     }
  12.     return value;
  13. }
  14.  
  15. int main()
  16. {  
  17.     // components of Processor
  18.    int N;
  19.    scanf("%d",&N);
  20.    int32_t imem[N];
  21.    int32_t regfile[32] = {0};
  22.    int32_t dmem[1000] = {0};
  23.  
  24.     // get instructions put em into imem
  25.     for(int i = 0;i<N;i++)
  26.     {
  27.         scanf("%d",&imem[i]);
  28.     }
  29.  
  30.     int PC = 0;
  31.  
  32.     while(PC != N)
  33.     {  
  34.         printf("PC: %d\n",PC);
  35.         // fetch instruction
  36.         int32_t instr = imem[PC];
  37.         uint32_t opcode, rs, rt, rd, funct, jta;
  38.         int32_t immediate;
  39.         opcode = instr >> 26;
  40.         rs = (instr << 6) >> 27;
  41.         rt = (instr << 11) >> 27;
  42.         rd = (instr << 16) >> 27;
  43.         funct = (instr << 26) >> 26;
  44.         immediate = signext((instr << 16) >> 16);
  45.         jta = (instr << 6) >> 6;
  46.  
  47.         if(opcode == 0) // R-type
  48.         {
  49.             if(funct == 0x20) // add
  50.             {  
  51.                 if(rd != 0)
  52.                 {
  53.                     regfile[rd] = regfile[rs] + regfile[rt];
  54.                 }
  55.                 PC++;
  56.                 continue;
  57.             }
  58.             else if(funct == 0x22) // sub
  59.             {
  60.                 if(rd != 0)
  61.                 {
  62.                     regfile[rd] = regfile[rs] - regfile[rt];
  63.                    
  64.                 }
  65.                 PC++;
  66.                 continue;
  67.             }
  68.             else if(funct == 0x24) // and
  69.             {
  70.                 if(rd != 0)
  71.                 {
  72.                     regfile[rd] = regfile[rs] & regfile[rt];
  73.                 }
  74.                 PC++;
  75.                 continue;
  76.  
  77.             }
  78.             else if(funct == 0x25) // or
  79.             {
  80.                 if(rd != 0)
  81.                 {
  82.                     regfile[rd] = regfile[rs] | regfile[rt];
  83.                 }
  84.                 PC++;
  85.                 continue;
  86.             }
  87.             else if(funct == 0x2a) // slt
  88.             {
  89.                if(rd != 0)
  90.                 {
  91.                     regfile[rd] = (regfile[rs] < regfile[rt])? 1 : 0;
  92.                 }
  93.                 PC++;
  94.                 continue;
  95.  
  96.             }
  97.             else if(funct == 0xc) // syscall
  98.             {
  99.                 if(regfile[2] == 34)
  100.                 {
  101.                     printf("0x%8X\n",regfile[4]);
  102.                     PC++;
  103.                 }
  104.                 else if(regfile[2] == 10)
  105.                 {
  106.                     PC = N;
  107.                 }
  108.                 continue;
  109.             }
  110.  
  111.         }
  112.         else if(opcode == 0x9) // addiu
  113.         {  
  114.             if(rt != 0)
  115.             {
  116.                 regfile[rt] = regfile[rs] + immediate;
  117.             }
  118.             PC++;
  119.             continue;
  120.  
  121.         }
  122.         else if(opcode == 0x4) // beq
  123.         {
  124.             if(regfile[rs] == regfile[rt])
  125.             {
  126.                 PC = PC + 1 + immediate;
  127.             }
  128.             else
  129.             {
  130.                 PC++;
  131.             }
  132.             continue;
  133.         }
  134.         else if(opcode == 0x23) // lw
  135.         {
  136.             if(rt != 0)
  137.             {
  138.                 regfile[rt] = dmem[regfile[rs] + immediate];
  139.             }
  140.             PC++;
  141.             continue;
  142.         }
  143.         else if(opcode == 0x2b)
  144.         {
  145.             dmem[regfile[rs] + immediate] = regfile[rt];
  146.             PC++;
  147.             continue;
  148.         }
  149.         else if(opcode == 0x2)
  150.         {
  151.             PC = jta;
  152.             continue;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement