示例#1
0
 public String get_cache_mem() {
   return stage_mem.print();
 }
示例#2
0
  public boolean runSingleSycle() {
    if (currentLineOfInstructions < lineOfInstructions) {
      stage_if.action(modeBit);
      stage_id.action(modeBit);
      stage_exe.action(modeBit);
      if (programs != null) {
        for (PCB program : programs) {
          program.waitAction();
        }
      }
      if (stage_exe.isJump()) { // PC & 0xf0000000
        String pc4bit = "0000";
        String func_sign = "0111111111111111111";
        String func_first = pc4bit.concat(func_sign);

        try {
          String func_str =
              stage_exe
                  .getJ_pc()
                  .substring(stage_exe.getJ_pc().length() - 9, stage_exe.getJ_pc().length() - 2);
          func_str = String.valueOf(Integer.parseInt(func_str, 2));
          Method m = Computer.class.getMethod("func" + func_str);
          m.invoke(this);
        } catch (NoSuchMethodException e) {
          int old_pc = getPC();
          int pcbits = old_pc / (2 ^ 28);
          // not added pc to sign but it's ready for use then
          int offset = Integer.parseInt(stage_exe.getJ_pc(), 2) / 4;
          stage_if.setPC(offset);
          if (stage_exe.isRegwrite()) { // it's means have jal (our agreement)
            stage_exe.getExemem().setALU_result(old_pc); // this old_pc \
            // should not increment because it incremented in IF stage
            stage_exe.getExemem().setWrite_Register(31);
            // use mips structure to save new_pc in ra (not assign directly)
          }
        } catch (Exception e) {

        }
      }
      if (stage_exe.isJumpReg()) {
        int pc = stage_exe.getIdexe().getRS_DATA();
        stage_exe.getExemem().setControlBits("0000000000100");
        stage_if.setPC(pc);
      }
      if (stage_exe.isBranch()) {
        if (exemem.getALU_result() == 0 && !stage_exe.isNot()) {
          int offset;

          offset = Integer.parseInt(stage_exe.getIdexe().getSignExt(), 2);
          stage_if.setPC(stage_if.getPC() + offset);
        }
        if (exemem.getALU_result() != 0 && stage_exe.isNot()) {
          int offset = 0;
          String address = stage_exe.getIdexe().getSignExt();
          if (address.charAt(0) == '1') { // must use 2'scompliment method for negative numbers
            boolean flag = true;
            String newAddress = "";
            for (int i = address.length() - 1; i >= 0; i--) {
              if (flag) {
                char ch = address.charAt(i);
                newAddress = ch + newAddress;
                if (ch == '1') {
                  flag = false;
                }
              } else {
                char ch = address.charAt(i);

                if (ch == '1') {
                  newAddress = '0' + newAddress;
                }
                if (ch == '0') {
                  newAddress = '1' + newAddress;
                }
              }
              offset = Integer.parseInt(newAddress, 2);
              offset = -1 * offset;
            }
          } else {
            offset = Integer.parseInt(stage_exe.getIdexe().getSignExt(), 2);
          }

          stage_if.setPC(stage_if.getPC() + offset);
        }
      }
      if (stage_exe.isSyscall()) {
        System.out.println("here is Syscall");
        enableintrrupt = false;
        modeBit = true;
        interruptBit = true;
        interruptReason = 1; // reason 1 for syscall in program
      }
      stage_mem.action(modeBit, aa);
      stage_wb.action(modeBit);
      currentLineOfInstructions = stage_if.getPC();
      timer.action();
      if (timer.check_timer() && enableintrrupt) {
        interruptReason = 2;
        interruptBit = true;
        modeBit = true; // means in kernel Mode now
      }
      if (interruptBit) {
        int syscallReason = getRegisterFile().getRegfile(2);
        getRegisterFile().setReg(27, interruptReason);
        if (currentProgram != null) {
          currentProgram.setPC(stage_if.getPC());
        }
        switch (interruptReason) {
          case 1:
            switch (syscallReason) {
              case 10:
                System.out.println("exit");
                defaultRegisters.setReg(27, interruptReason);
                defaultRegisters.setReg(2, 10);
                break;
              case 20:
                defaultRegisters.setReg(27, interruptReason);
                defaultRegisters.setReg(2, 20);
                break;
              case 30:
                int loc = getRegisterFile().getRegfile(4);
                int ch = getRegisterFile().getRegfile(5);
                int col = getRegisterFile().getRegfile(6);
                me.changeMonitor(loc, ch, col);
            }
            ;
          case 2:
            // for timer intrupt
            break;

          case 3:
            // for exception
            break;
        }
        stage_if.setPC(0);
        currentLineOfInstructions = 0;
        lineOfInstructions = stage_if.getIns_mem().size();
        interruptBit = false;
        stage_id.regfile = defaultRegisters;
      }
      return true;
    } else {
      System.out.println("end of file ...");
      setRunable(true);
      return false;
    }
  }