Пример #1
0
    public synchronized void fail() throws Fail {
      int err = GetLastError();
      Memory buffer = new Memory(2048);
      int res =
          FormatMessageW(
              FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
              null,
              err,
              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
              buffer,
              (int) buffer.size(),
              null);

      log =
          log
              && log(
                  1,
                  "fail() %s, Windows GetLastError()= %d, %s\n",
                  lineno(1),
                  err,
                  buffer.getString(0, true));

      // FIXME here convert from Windows error code to 'posix' error code

      Fail f = new Fail();
      throw f;
    }
  RandomConfig(String[] args) {
    Limits fireBrigades = new Limits(10, 15, "fireBrigades", "fire brigades");
    Limits policeForces = new Limits(10, 15, "policeForces", "police forces");
    Limits ambulanceTeams = new Limits(5, 8, "ambulanceTeams", "ambulance teams");
    Limits fireStations = new Limits(1, 1, "fireStations", "fire stations");
    Limits policeStations = new Limits(1, 1, "policeStations", "police stations");
    Limits ambulanceCenters = new Limits(1, 1, "ambulanceCenters", "ambulance centers");
    Limits civilians = new Limits(70, 90, "civilians", "civilians");
    Limits refuges = new Limits(1, 5, "refuges", "refuges");
    Limits fires = new Limits(2, 8, "fires", "fires");
    Limits fireRadius = new Limits(0, 20000, "fireradius", "fire radius");
    Limits[] allLimits =
        new Limits[] {
          fireBrigades,
          policeForces,
          ambulanceTeams,
          fireStations,
          policeStations,
          ambulanceCenters,
          civilians,
          refuges,
          fires,
          fireRadius
        };
    boolean allowTrappedAgents = false;
    boolean bigFires = false;
    boolean trappedCivilians = true;

    String dir = "";

    for (int i = 0; i < args.length; ++i) {
      if (args[i].startsWith("-min-")) {
        for (int j = 0; j < allLimits.length; ++j) {
          if (args[i].equalsIgnoreCase("-min-" + allLimits[j].prefix))
            allLimits[j].min = Integer.parseInt(args[++i]);
        }
      } else if (args[i].startsWith("-max-")) {
        for (int j = 0; j < allLimits.length; ++j) {
          if (args[i].equalsIgnoreCase("-max-" + allLimits[j].prefix))
            allLimits[j].max = Integer.parseInt(args[++i]);
        }
      } else if (args[i].startsWith("-no-")) {
        for (int j = 0; j < allLimits.length; ++j) {
          if (args[i].equalsIgnoreCase("-no-" + allLimits[j].prefix)) {
            allLimits[j].min = 0;
            allLimits[j].max = 0;
          }
        }
      } else if (args[i].startsWith("-set-")) {
        int num = Integer.parseInt(args[i + 1]);
        for (int j = 0; j < allLimits.length; ++j) {
          if (args[i].equalsIgnoreCase("-set-" + allLimits[j].prefix)) {
            allLimits[j].min = num;
            allLimits[j].max = num;
          }
        }
        ++i;
      } else if (args[i].equalsIgnoreCase("-t")
          || args[i].equalsIgnoreCase("--allow-trapped-agents")) {
        allowTrappedAgents = true;
      } else if (args[i].equalsIgnoreCase("-c")
          || args[i].equalsIgnoreCase("--allow-untrapped-civilians")) {
        trappedCivilians = false;
      } else if (args[i].equalsIgnoreCase("-b") || args[i].equalsIgnoreCase("--big-fires")) {
        bigFires = true;
      } else if (args[i].equalsIgnoreCase("-d") || args[i].equalsIgnoreCase("--dir")) {
        dir = args[i + 1];
      } else if (args[i].equalsIgnoreCase("-h") || args[i].equalsIgnoreCase("--help")) {
        System.out.println("Usage: RandomConfig [options]");
        System.out.println(
            "This program will read from \"road.bin\", \"node.bin\" and \"building.bin\" and produce a randomised \"gisini.txt\"");
        System.out.println("Options");
        System.out.println("=======");
        for (int j = 0; j < allLimits.length; ++j) {
          System.out.println(
              "-min-"
                  + allLimits[j].prefix
                  + "\tSet the minimum number of "
                  + allLimits[j].name
                  + " (currently "
                  + allLimits[j].min
                  + ")");
          System.out.println(
              "-max-"
                  + allLimits[j].prefix
                  + "\tSet the maximum number of "
                  + allLimits[j].name
                  + " (currently "
                  + allLimits[j].max
                  + ")");
          System.out.println(
              "-no-"
                  + allLimits[j].prefix
                  + "\tSet the minimum and maximum of "
                  + allLimits[j].name
                  + " to zero");
        }
        System.out.println(
            "-t\t--allow-trapped-agents\tAllow rescue agents (fire brigades, police forces and ambulance teams) to be placed inside buildings (default OFF)");
        System.out.println(
            "-c\t--allow-untrapped-civilians\tAllow civilians to be placed outside buildings (default OFF)");
        System.out.println("-b\t--big-fires\tAllow big fires");
        System.out.println("-d\t--dir\tSet output directory (use full path)");
        System.out.println("-h\t--help\tPrint this message");
        return;
      }
    }
    try {

      File parentDir = new File(dir);
      File gisini = new File(parentDir, "gisini.txt");

      // Open the output

      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(gisini)));

      // Build the city data
      allNodes = MapFiles.loadNodes(parentDir);
      allRoads = MapFiles.loadRoads(parentDir);
      allBuildings = MapFiles.loadBuildings(parentDir);

      Memory memory = new HashMemory();
      for (int i = 0; i < allNodes.length; ++i) memory.add(allNodes[i], 0);
      for (int i = 0; i < allRoads.length; ++i) memory.add(allRoads[i], 0);
      for (int i = 0; i < allBuildings.length; ++i) memory.add(allBuildings[i], 0);
      // Place items
      int numFireBrigades = fireBrigades.getNumber();
      int numPoliceForces = policeForces.getNumber();
      int numAmbulanceTeams = ambulanceTeams.getNumber();
      int numFireStations = fireStations.getNumber();
      int numPoliceStations = policeStations.getNumber();
      int numAmbulanceCenters = ambulanceCenters.getNumber();
      int numRefuges = refuges.getNumber();
      int numCivilians = civilians.getNumber();
      int numFires = fires.getNumber();

      FireStation[] fireStationBuildings = new FireStation[numFireStations];
      PoliceOffice[] policeOfficeBuildings = new PoliceOffice[numPoliceStations];
      AmbulanceCenter[] ambulanceCenterBuildings = new AmbulanceCenter[numAmbulanceCenters];
      Refuge[] refugeBuildings = new Refuge[numRefuges];
      Building[] normalBuildings =
          placeMotionlessObjects(
              fireStationBuildings,
              policeOfficeBuildings,
              ambulanceCenterBuildings,
              refugeBuildings,
              allBuildings);
      MapFiles.writeGISMotionlessObjects(
          out,
          fireStationBuildings,
          policeOfficeBuildings,
          ambulanceCenterBuildings,
          refugeBuildings);

      FireBrigade[] fireBrigadeObjects = new FireBrigade[numFireBrigades];
      PoliceForce[] policeForceObjects = new PoliceForce[numPoliceForces];
      AmbulanceTeam[] ambulanceTeamObjects = new AmbulanceTeam[numAmbulanceTeams];
      Civilian[] civilianObjects = new Civilian[numCivilians];
      //
      //	placeMovingObjects(fireBrigadeObjects,policeForceObjects,ambulanceTeamObjects,civilianObjects,allBuildings,allRoads,allNodes,allowTrappedAgents,trappedCivilians);
      placeMovingObjects(
          fireBrigadeObjects,
          policeForceObjects,
          ambulanceTeamObjects,
          civilianObjects,
          allBuildings,
          new Road[0],
          allNodes,
          allowTrappedAgents,
          trappedCivilians);
      MapFiles.writeGISMovingObjects(
          out,
          fireBrigadeObjects,
          policeForceObjects,
          ambulanceTeamObjects,
          civilianObjects,
          memory);

      Building[] fireBuildings;
      if (bigFires) fireBuildings = placeBigFires(numFires, normalBuildings, fireRadius);
      else fireBuildings = placeNormalFires(numFires, normalBuildings);
      MapFiles.writeGISFires(out, fireBuildings);

      out.flush();
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #3
0
/**
 * Abstract class: it provides all methods and attributes for each instruction type
 *
 * @author Trubia Massimo, Russo Daniele
 */
public abstract class Instruction {

  protected BitSet32 repr;
  protected List<Integer> params;
  protected int paramCount;
  protected String syntax;
  protected String name;
  protected String comment;
  protected static Memory memory = Memory.getInstance();
  protected Register[] TR; // is not static because each instruction has got its own registers
  protected String fullname;
  protected static boolean enableForwarding = (Boolean) Config.get("forwarding");
  protected String label;
  protected static final Logger logger = Logger.getLogger(Instruction.class.getName());

  /** Creates a new instance of Instruction */
  public Instruction() {
    params = new LinkedList<Integer>();
    TR = new Register[5];
    repr = new BitSet32();
    repr.reset(false);
    syntax = new String();
    // initialization of temporary registers
    for (int i = 0; i < TR.length; i++) {
      TR[i] = new Register();
    }
  }

  /** <pre>
   *  Returns a BitSet32 holding the binary representation of the Instruction
   *  @return the Bitset32 representing the instruction
   *  </pre>
   * */
  public BitSet32 getRepr() {
    return repr;
  }

  /**
   * Creates a new instance of an Instruction's subclass
   *
   * @param name string value to pass in order to instanciate an instruction object
   * @return the instruction object
   */
  public static Instruction buildInstruction(String name) {
    Instruction returnedObject = null;
    for (InstructionEnumerator op : InstructionEnumerator.values()) {
      if (op.name().equals(name) == true) {
        returnedObject = op.getObject();
        return returnedObject;
      }
    }
    return returnedObject;
  }

  public enum InstructionEnumerator {

    // ALU R-Type 32-bits
    ADD {
      Instruction getObject() {
        ADD newObject = new ADD();
        return newObject;
      }
    },
    ADDU {
      Instruction getObject() {
        ADDU newObject = new ADDU();
        return newObject;
      }
    },
    SUB {
      Instruction getObject() {
        SUB newObject = new SUB();
        return newObject;
      }
    },
    SUBU {
      Instruction getObject() {
        SUBU newObject = new SUBU();
        return newObject;
      }
    },
    DIV {
      Instruction getObject() {
        DIV newObject = new DIV();
        return newObject;
      }
    },
    DIVU {
      Instruction getObject() {
        DIVU newObject = new DIVU();
        return newObject;
      }
    },
    MULT {
      Instruction getObject() {
        MULT newObject = new MULT();
        return newObject;
      }
    },
    MULTU {
      Instruction getObject() {
        MULTU newObject = new MULTU();
        return newObject;
      }
    },

    // ALU I-Type 32-bits
    ADDI {
      Instruction getObject() {
        ADDI newObject = new ADDI();
        return newObject;
      }
    },
    ADDIU {
      Instruction getObject() {
        ADDIU newObject = new ADDIU();
        return newObject;
      }
    },

    // ALU Shifting 32-bits
    SLL {
      Instruction getObject() {
        SLL newObject = new SLL();
        return newObject;
      }
    },
    SLLV {
      Instruction getObject() {
        SLLV newObject = new SLLV();
        return newObject;
      }
    },
    SRA {
      Instruction getObject() {
        SRA newObject = new SRA();
        return newObject;
      }
    },
    SRAV {
      Instruction getObject() {
        SRAV newObject = new SRAV();
        return newObject;
      }
    },
    SRL {
      Instruction getObject() {
        SRL newObject = new SRL();
        return newObject;
      }
    },
    SRLV {
      Instruction getObject() {
        SRLV newObject = new SRLV();
        return newObject;
      }
    },

    // ALU R-Type
    AND {
      Instruction getObject() {
        AND newObject = new AND();
        return newObject;
      }
    },
    DADD {
      Instruction getObject() {
        DADD newObject = new DADD();
        return newObject;
      }
    },
    DADDU {
      Instruction getObject() {
        DADDU newObject = new DADDU();
        return newObject;
      }
    },
    DSUB {
      Instruction getObject() {
        DSUB newObject = new DSUB();
        return newObject;
      }
    },
    DSUBU {
      Instruction getObject() {
        DSUBU newObject = new DSUBU();
        return newObject;
      }
    },
    OR {
      Instruction getObject() {
        OR newObject = new OR();
        return newObject;
      }
    },
    SLT {
      Instruction getObject() {
        SLT newObject = new SLT();
        return newObject;
      }
    },
    SLTU {
      Instruction getObject() {
        SLTU newObject = new SLTU();
        return newObject;
      }
    },
    XOR {
      Instruction getObject() {
        XOR newObject = new XOR();
        return newObject;
      }
    },
    MOVN {
      Instruction getObject() {
        MOVN newObject = new MOVN();
        return newObject;
      }
    },
    MOVZ {
      Instruction getObject() {
        MOVZ newObject = new MOVZ();
        return newObject;
      }
    },
    DDIV {
      Instruction getObject() {
        DDIV newObject = new DDIV();
        return newObject;
      }
    },
    DDIVU {
      Instruction getObject() {
        DDIVU newObject = new DDIVU();
        return newObject;
      }
    },
    DMULT {
      Instruction getObject() {
        DMULT newObject = new DMULT();
        return newObject;
      }
    },
    DMULTU {
      Instruction getObject() {
        DMULTU newObject = new DMULTU();
        return newObject;
      }
    },
    MFLO {
      Instruction getObject() {
        MFLO newObject = new MFLO();
        return newObject;
      }
    },
    MFHI {
      Instruction getObject() {
        MFHI newObject = new MFHI();
        return newObject;
      }
    },

    // ALU I-Type
    ANDI {
      Instruction getObject() {
        ANDI newObject = new ANDI();
        return newObject;
      }
    },
    DADDI {
      Instruction getObject() {
        DADDI newObject = new DADDI();
        return newObject;
      }
    },
    DADDUI {
      Instruction getObject() {
        DADDUI newObject = new DADDUI();
        return newObject;
      }
    },
    DADDIU {
      Instruction getObject() {
        DADDIU newObject = new DADDIU();
        return newObject;
      }
    },
    LUI {
      Instruction getObject() {
        LUI newObject = new LUI();
        return newObject;
      }
    },
    ORI {
      Instruction getObject() {
        ORI newObject = new ORI();
        return newObject;
      }
    },
    SLTI {
      Instruction getObject() {
        SLTI newObject = new SLTI();
        return newObject;
      }
    },
    SLTIU {
      Instruction getObject() {
        SLTIU newObject = new SLTIU();
        return newObject;
      }
    },
    XORI {
      Instruction getObject() {
        XORI newObject = new XORI();
        return newObject;
      }
    },
    // ALU Shifting
    DSLL {
      Instruction getObject() {
        DSLL newObject = new DSLL();
        return newObject;
      }
    },
    DSLLV {
      Instruction getObject() {
        DSLLV newObject = new DSLLV();
        return newObject;
      }
    },
    DSRA {
      Instruction getObject() {
        DSRA newObject = new DSRA();
        return newObject;
      }
    },
    DSRAV {
      Instruction getObject() {
        DSRAV newObject = new DSRAV();
        return newObject;
      }
    },
    DSRL {
      Instruction getObject() {
        DSRL newObject = new DSRL();
        return newObject;
      }
    },
    DSRLV {
      Instruction getObject() {
        DSRLV newObject = new DSRLV();
        return newObject;
      }
    },
    // Load-Signed
    LB {
      Instruction getObject() {
        LB newObject = new LB();
        return newObject;
      }
    },
    LH {
      Instruction getObject() {
        LH newObject = new LH();
        return newObject;
      }
    },
    LW {
      Instruction getObject() {
        LW newObject = new LW();
        return newObject;
      }
    },
    LD {
      Instruction getObject() {
        LD newObject = new LD();
        return newObject;
      }
    },
    // Load-Unsigned
    LBU {
      Instruction getObject() {
        LBU newObject = new LBU();
        return newObject;
      }
    },
    LHU {
      Instruction getObject() {
        LHU newObject = new LHU();
        return newObject;
      }
    },
    LWU {
      Instruction getObject() {
        LWU newObject = new LWU();
        return newObject;
      }
    },
    // Store
    SB {
      Instruction getObject() {
        SB newObject = new SB();
        return newObject;
      }
    },
    SH {
      Instruction getObject() {
        SH newObject = new SH();
        return newObject;
      }
    },
    SW {
      Instruction getObject() {
        SW newObject = new SW();
        return newObject;
      }
    },
    SD {
      Instruction getObject() {
        SD newObject = new SD();
        return newObject;
      }
    },
    // Unconditional branches
    J {
      Instruction getObject() {
        J newObject = new J();
        return newObject;
      }
    },
    JAL {
      Instruction getObject() {
        JAL newObject = new JAL();
        return newObject;
      }
    },
    JALR {
      Instruction getObject() {
        JALR newObject = new JALR();
        return newObject;
      }
    },
    JR {
      Instruction getObject() {
        JR newObject = new JR();
        return newObject;
      }
    },
    BNE {
      Instruction getObject() {
        BNE newObject = new BNE();
        return newObject;
      }
    },
    B {
      Instruction getObject() {
        B newObject = new B();
        return newObject;
      }
    },
    // Conditional branches
    BEQ {
      Instruction getObject() {
        BEQ newObject = new BEQ();
        return newObject;
      }
    },
    BNEZ {
      Instruction getObject() {
        BNEZ newObject = new BNEZ();
        return newObject;
      }
    },
    BEQZ {
      Instruction getObject() {
        BEQZ newObject = new BEQZ();
        return newObject;
      }
    },
    BGEZ {
      Instruction getObject() {
        BGEZ newObject = new BGEZ();
        return newObject;
      }
    },
    // Special instructions
    NOP {
      Instruction getObject() {
        NOP newObject = new NOP();
        return newObject;
      }
    },
    BUBBLE {
      Instruction getObject() {
        BUBBLE newObject = new BUBBLE();
        return newObject;
      }
    },
    HALT {
      Instruction getObject() {
        HALT newObject = new HALT();
        return newObject;
      }
    },
    TRAP {
      Instruction getObject() {
        TRAP newObject = new TRAP();
        return newObject;
      }
    },
    SYSCALL {
      Instruction getObject() {
        SYSCALL newObject = new SYSCALL();
        return newObject;
      }
    },
    BREAK {
      Instruction getObject() {
        BREAK newObject = new BREAK();
        return newObject;
      }
    };

    abstract Instruction getObject();
  }
  /**
   *
   *
   * <pre>
   * Instruction fetch.
   * Now it is used in order to generate the Dinero trace-file
   * </pre>
   */
  public void IF() throws BreakException {}

  /**
   *
   *
   * <pre>
   * Decode stage of the Pipeline
   * In this method all instructions that modify GPRs lock the involved register
   * </pre>
   */
  public abstract void ID()
      throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException,
          TwosComplementSumException, HaltException, JumpException, BreakException;

  /**
   *
   *
   * <pre>
   * Execute stage of the Pipeline
   * In this stage all Alu Instructions perform their computations and save results in temporary registers
   * </pre>
   */
  public abstract void EX()
      throws HaltException, IrregularStringOfBitsException, IntegerOverflowException,
          TwosComplementSumException, IrregularWriteOperationException,
          IrregularWriteOperationException, DivisionByZeroException;

  /**
   *
   *
   * <pre>
   * Memory stage of the Pipeline
   * In this stage all Load and Store instructions access memory for getting or putting data
   * </pre>
   */
  public abstract void MEM()
      throws HaltException, IrregularStringOfBitsException, MemoryElementNotFoundException,
          AddressErrorException, IrregularWriteOperationException;

  /**
   *
   *
   * <pre>
   * Write Back stage of the Pipeline
   * In this stage all instructions that modify registers write and unlock them
   * </pre>
   */
  public abstract void WB() throws HaltException, IrregularStringOfBitsException;

  /**
   *
   *
   * <pre>
   * Builds the binary encoding of instructions.
   * Every instruction is represented by a 32 bit field
   * </pre>
   */
  public abstract void pack() throws IrregularStringOfBitsException;

  /**
   * Gets the number of params to pass at the instruction object
   *
   * @return number of params
   */
  public int getNParams() {
    return paramCount;
  };

  /**
   *
   *
   * <pre>
   * Gets the syntax of any instruction as string composed by the following simbols
   * %R   Register
   * %I   Immediate
   * %U   Unsigned Immediate
   * %L   Memory Label
   * %E   Program Label used for Jump Instructions
   * %B   Program Label used for Brench Instructions
   *
   * examples:
   *   Instruction -----> Syntax
   * DADD  R1,R2,R3   |   %R,%R,%R
   * DADDI R1,R2,-3   |   %R,%R,%I
   * DSLL  R1,R2,15   |   %R,%R,%U
   * LD    R1,vet(R0) |   %R,%L(%R)
   * J     loop       |   %E
   * BNE   R1,R2,loop |   %R,%R,%B
   * </pre>
   */
  public String getSyntax() {
    return syntax;
  }

  /**
   * Returns the name of the instruction as string.
   *
   * @return the instruction name(e.g. "DADD")
   */
  public String getName() {
    return name;
  }

  /**
   *
   *
   * <pre>
   * Returns a list with the instruction parameters
   * e.g. DADD R1,R2,R3 --> params= { 1, 2, 3}
   *      LD R1, var(R0)--> params= { 1, address memory corresponding with var, 0}
   * </pre>
   *
   * @return the list of parameters
   */
  public List<Integer> getParams() {
    return params;
  }

  /**
   *<pre>
   * Sets the instruction with a list of parameters
   *          Passed list                                      | Instruction to set
   * e.g. list= { 1, 2, 3}                                     |   DADD R1,R2,R3
   *      list= { 1, address memory corresponding with var, 0} |   LD R1, var(R0)
   *@param params The list of parameters
   **/
  public void setParams(List<Integer> params) {
    this.params = params;
  }

  /**
   * Sets the full name of the instruction as string
   *
   * @param value full name of the instruction (e.g. "DADD R1,R2,R3")
   */
  public void setFullName(String value) {
    fullname = value;
  }

  /**
   * Sets the comment of the instruction as string. The comment is the text after every semicolon in
   * the file .s
   *
   * @params value the full name of the instruction (e.g. "DADD R1,R2,R3")
   */
  public void setComment(String comment) {
    this.comment = comment;
  }

  /**
   * Gets the comment of the instruction as string.The comment is the text after every semicolon in
   * the file .s
   *
   * @return the comment
   */
  public String getComment() {
    return comment;
  }

  /**
   * Gets the comment of the instruction as string. The comment is the text after every semicolon in
   * the file .s
   *
   * @return the full name of the instruction (e.g. "DADD R1,R2,R3")
   */
  public String getFullName() {
    return fullname;
  }

  public String toString() {
    return fullname;
  }

  /**
   * Enable forwarding mode
   *
   * @param value This variable enable the forwarding modality if it is true
   */
  public static void setEnableForwarding(boolean value) {
    enableForwarding = value;
  }

  /**
   * Gets the state of EnableForwarding. This modality anticipates writing on registers at EX stage
   * for Alu instructions or at MEM stage for Load-Store instructions
   *
   * @return The forwarding state
   */
  public static boolean getEnableForwarding() {
    return enableForwarding;
  }

  /**
   *
   *
   * <pre>
   * Gets the label of the instruction. Labels may be assigned to instructions
   * when they are inserted in the symbol table
   * </pre>
   *
   * @return label of the instruction
   */
  public String getLabel() {
    return label;
  }

  /**
   *
   *
   * <pre>
   * Sets the label of the instruction. Labels may be assigned to instructions
   * when they are inserted in the symbol table
   * </pre>
   *
   * @value label of the instruction
   */
  public void setLabel(String value) {
    label = value;
  }
}