mips simulator na nawawalan ng 0x9

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.1 kB
5
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

void array_check(int *arr, int n){
  printf("\nREGISTER FILE:\n");
  for (int i = 0; i < n; i++){
  printf("reg[%d] = %X\n",i, arr[i]);
}
}
void addiu(int *regs, int instr){
  int rt,rs,imm, mask;
  imm = instr&0xFFFF;
  mask = 0x00008000;
  if (mask & imm)
  {
    imm |= 0xFFFF0000;
  }
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >>27;
  int value = regs[rs] + imm;
  if (rt == 0){
    return;
  }
  regs[rt] = value;
}
void or(int *regs, int instr){

  int rs, rt, rd;
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >> 27;
  rd = instr<<16; rd = rd >> 27;
  int value = regs[rs] | regs[rt];
  if (rd == 0){
    return;
  }
  regs[rd] = value;
}
void and(int *regs, int instr){

  int rs, rt, rd;
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >> 27;
  rd = instr<<16; rd = rd >> 27;
  int value = regs[rs] & regs[rt];
  if (rd == 0){
    return;
  }
  regs[rd] = value;
}
void add(int *regs, int instr){

  int rs, rt, rd;
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >> 27;
  rd = instr<<16; rd = rd >> 27;
  int value = regs[rs] + regs[rt];
  if (rd == 0){
    return;
  }
  regs[rd] = value;
}
int beq(int *regs, int instr, int i){
  int rt,rs,target;
  target = abs(instr&0xFFFF);
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >>27;
  if (regs[rs] == regs[rt]){
    return (i+1+target);
  }
  else{
    return i;
  }
}
void slt(int *regs, int instr){

  int rs, rt, rd;
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >> 27;
  rd = instr<<16; rd = rd >> 27;
  int value = rs < rt ? 1 : 0;
  if (rd == 0){
    return;
  }
  if (rs < rt){
    regs[rd] = 1;
  }
  else{
    regs[rd] = 0;
  }
  //regs[rd] = value;
}
void sub(int *regs, int instr){

  int rs, rt, rd;
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >> 27;
  rd = instr<<16; rd = rd >> 27;
  if (rd == 0){
    return;
  }
  int value = regs[rs] - regs[rt];
  regs[rd] = value;
}
void sw(int *regs, int* mem, int instr){

  int rt,rs,offset;
  offset = instr&0xFFFF;
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >>27;
  mem[rs+offset] = regs[rt];
}
void lw(int *regs, int* mem, int instr){
  int rt,rs,offset;
  offset = instr&0xFFFF;
  rs = instr<<6;rs = rs >> 27;
  rt = instr<<11; rt = rt >>27;
  if (rt == 0){
    return;
  }
  regs[rt] = mem[rs+offset];
}
int j(int instr, int i){

  int address;
  address = (instr&0x03FFFFFF);
  address = address <<2;
  int mips_i = (i + 1)*4 + 0x10000000;
  int target = mips_i | address;
  return (target-0x10000000)/4;
}
int main(){
  int n;
  scanf("%d", &n);
  int instr[n];
  int registers[32] = {0};
  int memory[0x3E8] = {0};
  for (int i = 0; i < n; i++){
    scanf("%d", &instr[i]);
  }
  for (int i = 0; i < n; i ++){
    //int opcode = (instr[i]>>26)&0x3F;
    int opcode = (instr[i]&0b11111100000000000000000000000000)>>26;
    //R-TYPE
    if (opcode == 0){
      int funct = instr[i] & 0x3F;
      if (funct == 12){
        if (registers[2] == 34){
          printf("0x%08X\n", registers[4]);

        }
        else if (registers[2] == 10){
          break;
        }
      }
      else if (funct == 0x20){
        add(registers, instr[i]);

      }
      else if (funct == 0x22){
        sub(registers, instr[i]);

      }
      else if (funct == 0x24){
        and(registers, instr[i]);

      }
      else if (funct == 0x25){
        or(registers, instr[i]);

      }
      else if (funct == 0x2A){
        slt(registers, instr[i]);

      }
    }
    if (opcode == 0x23){
      lw (registers, memory, instr[i]);

    }
    else if (opcode == 0x2B){
      sw (registers, memory, instr[i]);

    }
    else if (opcode == 4){
      i = beq(registers, instr[i], i);


    }
    else if (opcode == 9){
      addiu(registers, instr[i]);

    }
    else if (opcode == 2){
      i = j(instr[i],i);


    }
  }
  //array_check(registers, 32);
  //array_check(memory,100);
  return 0;
}