Exemple #1
0
  private void stage_issue() {
    System.out.println("station " + id + " in ISSUE.");

    // check operand 1
    Message msg;
    if (Qj != READY) {
      msg = bus.search_station(Qj);
      if (msg != null) { // found
        Qj = READY;
        Vj = msg.result;
      }
    } // endif

    // check operand 2
    if (Qk != READY) {
      msg = bus.search_station(Qk);
      if (msg != null) { // found
        Qk = READY;
        Vk = msg.result;
      }
    } // endif

    // if both operands ready, do execution
    if ((Qj == READY) && (Qk == READY)) {
      stage = "EX";
      stage_ex();
    }
  }
Exemple #2
0
  public void runCPUSimulator(ArrayList<ArrayList<String>> Op) {
    boolean busy_station = false; // are there any busy stations?
    bus.instruction_queue = Op;

    // for (int k = 0; k < 10; k++, clk_cycles++){
    for (;
        bus.PC < bus.instruction_queue.size() || busy_station;
        clk_cycles++) { // while more instruction coming or busy stations
      // System.out.println("PC: " + bus.PC + " === size: " + bus.instruction_queue.size());

      System.out.println("======= cycle " + (clk_cycles + 1) + " begin =======");

      if (bus.PC < bus.instruction_queue.size()) {
        bus.issued = false;
      } else {
        bus.issued = true;
      }

      // each station deals with this cycle
      for (int i = 0; i < n_stations; i++) {
        stations[i].one_cycle();
      }

      // modify busy_station;
      busy_station = false;
      for (int i = 0; i < n_stations; i++) {
        if (stations[i].is_busy()) {
          busy_station = true;
          break;
        }
      }

      bus.new_cycle();

      System.out.println("======== cycle " + (clk_cycles + 1) + " end ========");
    }

    clk_cycles--;
    CPI = (float) clk_cycles / bus.instruction_queue.size();

    bus.freg.printFreg();
    System.out.println("mem[150]: " + Float.parseFloat(String.valueOf(bus.mem[150])));
    System.out.println("average CPI: " + CPI);
  }
Exemple #3
0
  private void stage_wb() {
    System.out.println("station " + id + " in WB");

    // send message to bus
    Message msg = new Message(id, result, A, false);
    bus.receive_msg(msg);

    stage = "IDLE";
    Busy = false;
  }
Exemple #4
0
  private void issue(ArrayList<String> instruction) {
    System.out.println("station " + id + " being issued with one instruction.");
    bus.issued = true;
    bus.PC++;

    Busy = true;
    stage = "ISSUE";

    Op = instruction.get(0);

    if ((Op.equals("add.d"))
        || (Op.equals("sub.d"))
        || (Op.equals("mul.d"))
        || (Op.equals("div.d"))) {
      if ((Op.equals("add.d")) || Op.equals("sub.d")) {
        cycles_left = 3;
      } else cycles_left = 6;

      // check operand 1
      int index = Integer.parseInt(instruction.get(4));
      int depend = bus.freg.getFregFlag(index);
      if (depend == READY) { // operand 1 ready
        Qj = READY;
        Vj = bus.freg.getFreg(index);
      } else { // operand 1 depends on another station
        // check bus
        Message msg = bus.search_station(depend);
        if (msg != null) {
          Qj = READY;
          Vj = msg.result;
        } else Qj = depend; // wait on that station;
      }

      // check operand 2
      index = Integer.parseInt(instruction.get(6));
      depend = bus.freg.getFregFlag(index);
      if (depend == READY) { // operand 1 ready
        Qk = READY;
        Vk = bus.freg.getFreg(index);
      } else { // operand 1 depends on another station
        // check bus
        Message msg = bus.search_station(depend);
        if (msg != null) {
          Qk = READY;
          Vk = msg.result;
        } else Qk = depend; // wait on that station;
      }

      // if they are ready, enter stage EX
      if ((Qj == READY) && (Qk == READY)) { // both operands ready
        stage = "EX";
      }

      // set register dependency flag
      int reg_dst = Integer.parseInt(instruction.get(2));
      bus.freg.setFregFlag(reg_dst, id);
      System.out.println("station " + id + ": F" + reg_dst + " now depends on it.");

    } else if (Op.equals("daddi")) {
      cycles_left = 2; // how many?

      // check operand 1
      int index = Integer.parseInt(instruction.get(4));
      int depend = bus.reg.getRegFlag(index);
      if (depend == READY) { // operand 1 ready
        Qj = READY;
        Vj = bus.reg.getReg(index);
      } else { // operand 1 depends on another station
        // check bus
        Message msg = bus.search_station(depend);
        if (msg != null) {
          Qj = READY;
          Vj = msg.result;
        } else Qj = depend; // wait on that station;
      }

      // operand 2 always ready
      Qk = READY;
      Vk = Integer.parseInt(instruction.get(6));

      // if they are ready, enter stage EX
      if ((Qj == READY) && (Qk == READY)) { // both operands ready
        stage = "EX";
      }

      // set register dependency flag
      int reg_dst = Integer.parseInt(instruction.get(2));
      bus.reg.setRegFlag(reg_dst, id);
      System.out.println("station " + id + ": R" + reg_dst + " now depends on it.");

    } else if (Op.equals("s.d")) {
      cycles_left = 1;

      // check operand 1

      int index = Integer.parseInt(instruction.get(2));
      int depend = bus.freg.getFregFlag(index);
      if (depend == READY) { // operand ready
        Qj = READY;
        Vj = bus.freg.getFreg(index);
      } else { // operand depends on another station
        // check bus
        Message msg = bus.search_station(depend);
        if (msg != null) {
          Qj = READY;
          Vj = msg.result;
        } else Qj = depend; // wait on that station;
      }

      // check operand 2

      index = Integer.parseInt(instruction.get(6));
      depend = bus.reg.getRegFlag(index);
      if (depend == READY) { // operand 1 ready
        Qk = READY;
        Vk = bus.reg.getReg(index);
      } else { // operand 1 depends on another station
        // check bus
        Message msg = bus.search_station(depend);
        if (msg != null) {
          Qk = READY;
          Vk = msg.result;
        } else Qk = depend; // wait on that station;
      }

      // if they are ready, enter stage EX
      if ((Qj == READY) && (Qk == READY)) { // both operands ready
        stage = "EX";
      }

      // no registers' value depend on s.d instruction
      // but we have to calculation A, into which memory unit the bus stores result
      // second part of A comes from Rx, which will accumulated to A in stage EX
      A = Integer.parseInt(instruction.get(4));

    } else if (Op.equals("l.d")) {
      cycles_left = 1;

      // check operand 1
      Qj = READY;
      Vj = Integer.parseInt(instruction.get(4));

      // check operand 2
      int index = Integer.parseInt(instruction.get(6));
      int depend = bus.reg.getRegFlag(index);
      if (depend == READY) { // operand 2 ready
        Qk = READY;
        Vk = bus.reg.getReg(index);
      } else { // operand 2 depends on another station
        // check bus
        Message msg = bus.search_station(depend);
        if (msg != null) {
          Qk = READY;
          Vk = msg.result;
        } else Qk = depend; // wait on that station;
      }

      // if they are ready, enter stage EX
      if ((Qj == READY) && (Qk == READY)) { // both operands ready
        stage = "EX";
      }

      // set register dependency flag
      int reg_dst = Integer.parseInt(instruction.get(2));
      bus.freg.setFregFlag(reg_dst, id);
      System.out.println("station " + id + ": F" + reg_dst + " now depends on it.");

    } else {
      System.out.println("== Station.is_match == this should not happen");
    }
  }