Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

milestone 1

  • Loading branch information...
commit 7700d5c190c6a8f89e2b4a320cfd70db4c47f4d7 1 parent 229a0c6
blake-regalia authored
6 Driver.java
View
@@ -36,10 +36,10 @@ public Driver(String filename) {
int instructionInt;
// while there is an instruction
while((instructionInt = fetchUnit.nextInstruction()) != -1) {
- instructions.add(new InstructionDecoder(instructionInt));
-
-
+ InstructionDecoder inst = new InstructionDecoder(instructionInt);
+ instructions.add(inst);
}
+
Simulator sim = new Simulator(instructions);
sim.start();
}
19 FetchUnit.java
View
@@ -1,30 +1,23 @@
+import java.util.Scanner;
+
public class FetchUnit {
private String fileData;
private int position;
+ private Scanner in;
public FetchUnit(String _fileData) {
fileData = _fileData;
position = 0;
+ in = new Scanner(fileData);
}
-
public int nextInstruction() {
- String instruction = "";
-
- int nextIndex = fileData.indexOf('\n', position);
- if(nextIndex < 0) {
- instruction = fileData.substring(position);
- }
- else {
- instruction = fileData.substring(position, nextIndex);
- }
-
- if(instruction == null || instruction.length() == 0) {
+ if(!in.hasNextLine()) {
return -1;
}
- return Integer.valueOf(instruction);
+ return Integer.valueOf(in.nextLine());
}
}
148 InstructionDecoder.java
View
@@ -28,7 +28,7 @@
public static final int ALU_NOT = 0x04;
public static final int ALU_XOR = 0x05;
public static final int ALU_SLT = 0x06;
-
+
// Branch Types
public static final int BRANCH_NOT_EQUAL = 0x03;
public static final int BRANCH_GREATER_THAN = 0x02;
@@ -53,18 +53,19 @@
private int instruction;
private int format = 0;
-
+
private int op_fun;
private int rd;
private int rs;
private int rt;
+ private int imm;
-
- private int imm;
+ private int funct;
+ private int shamt;
private int alu_op;
-
+
/** controls bits **/
// MemWrite
@@ -96,7 +97,7 @@ private void decode() {
rs = (inst >> F1) & _5_BITS;
rt = (inst >> F2) & _5_BITS;
// NOTE: only jump instructions will not use these registers
-
+
// assume format is I-format, only change otherwise
format = FORMAT_I;
@@ -105,8 +106,7 @@ private void decode() {
// R-format
case OP_0:
format = FORMAT_R;
-
- int shamt, funct;
+
rd = (inst >> F3) & _5_BITS;
shamt = (inst >> F4) & _5_BITS;
funct = (inst >> F5) & _6_BITS;
@@ -129,17 +129,17 @@ private void decode() {
break;
case ADDI:
- imm = (inst >> F3) & _16_BITS;
+ imm = inst & _16_BITS;
alu_op = ALU_ADD;
-
+
asrc = 1; // use immediate value
rw = 1;
break;
case XORI:
- imm = (inst >> F3) & _16_BITS;
+ imm = inst & _16_BITS;
alu_op = ALU_XOR;
-
+
asrc = 1; // use immediate value
rw = 1;
break;
@@ -147,9 +147,9 @@ private void decode() {
// should we left-shift the imm value by 2 to reflect loading
// that many bytes after the memory address? eg: lw $1, 12($4)
case LW:
- imm = (inst >> F3) & _16_BITS;
+ imm = inst & _16_BITS;
alu_op = ALU_ADD;
-
+
asrc = 1; // use immediate value
mr = 1; // read from memory
mtr = 1; // load memory read to register
@@ -157,27 +157,27 @@ private void decode() {
break;
case SW:
- imm = (inst >> F3) & _16_BITS;
+ imm = inst & _16_BITS;
alu_op = ALU_ADD;
-
+
mw = 1;
asrc = 1;
break;
-
+
case BNE:
- imm = (inst >> F3) & _16_BITS;
+ imm = inst & _16_BITS;
alu_op = ALU_SUB;
-
+
bt = BRANCH_NOT_EQUAL;
break;
-
+
case BGE:
- imm = (inst >> F3) & _16_BITS;
+ imm = inst & _16_BITS;
alu_op = ALU_SUB;
-
+
bt = BRANCH_GREATER_THAN;
break;
-
+
case JAL:
format = FORMAT_J;
imm = (inst >> F1) & _26_BITS;
@@ -190,19 +190,23 @@ private void decode() {
}
}
-
+
private String instructionToString() {
- switch(instruction) {
- case ADD:
- return "add";
+ switch(op_fun) {
+ case OP_0:
+ switch(funct) {
+ case ADD:
+ return "add";
+ case SLT:
+ return "slt";
+ case SUB:
+ return "sub";
+ }
+ break;
case ADDI:
return "addi";
case XORI:
return "xori";
- case SLT:
- return "slt";
- case SUB:
- return "sub";
case LW:
return "lw";
case SW:
@@ -216,18 +220,19 @@ private String instructionToString() {
}
return "NON-INSTRUCTION";
}
-
+
@Override
public String toString() {
StringBuilder str = new StringBuilder();
- //str.append("Instruction #: "+instruction);
- str.append(instructionToString());
+ str.append(""+instruction);
+ str.append("\n");
+ str.append(instructionToString()+" ");
switch(format) {
case FORMAT_R:
- str.append("$"+rs+", $"+rt+", $"+rd);
+ str.append("$"+rt+", $"+rs+", $"+rd);
break;
case FORMAT_I:
- str.append("$"+rs+", $"+rt+", "+imm);
+ str.append("$"+rt+", $"+rs+", "+imm);
break;
case FORMAT_J:
str.append(""+imm);
@@ -239,7 +244,6 @@ public String toString() {
str.append("Control Bits:");
str.append("\n");
str.append("{alu: "+alu_op+", mw: "+mw+", mr: "+mr+", mtr: "+mtr+", asrc: "+asrc+", bt: "+bt+", rdst: "+rdst+", rw: "+rw+"}");
- str.append("\n");
return str.toString();
}
@@ -263,40 +267,40 @@ public boolean isArithmetic() {
return (op_fun == 0);
}
- public int getOp_fun() {
- return op_fun;
- }
-
- public int getAlu_op() {
- return alu_op;
- }
-
- public int getMw() {
- return mw;
- }
-
- public int getMr() {
- return mr;
- }
-
- public int getMtr() {
- return mtr;
- }
-
- public int getAsrc() {
- return asrc;
- }
-
- public int getBt() {
- return bt;
- }
-
- public int getRdst() {
- return rdst;
- }
-
- public int getRw() {
- return rw;
- }
+ public int getOp_fun() {
+ return op_fun;
+ }
+
+ public int getAlu_op() {
+ return alu_op;
+ }
+
+ public int getMw() {
+ return mw;
+ }
+
+ public int getMr() {
+ return mr;
+ }
+
+ public int getMtr() {
+ return mtr;
+ }
+
+ public int getAsrc() {
+ return asrc;
+ }
+
+ public int getBt() {
+ return bt;
+ }
+
+ public int getRdst() {
+ return rdst;
+ }
+
+ public int getRw() {
+ return rw;
+ }
}
10 Simulator.java
View
@@ -14,7 +14,7 @@ public static void main(String[] args) {
for(int i=0; i<args.length; i++) {
if(args[i].equals("-file")) {
- filename = args[i++];
+ filename = args[++i];
}
}
@@ -29,6 +29,8 @@ public static void main(String[] args) {
public Simulator(ArrayList<InstructionDecoder> in) {
instructions = in;
pc = 0;
+ regs= new int[32];
+ memory = new int[1024];
}
public void executeInstruction(InstructionDecoder instruction) {
@@ -82,14 +84,16 @@ public void executeInstruction(InstructionDecoder instruction) {
{
System.out.println("$"+i+": "+regs[i]+"\t$"+(i+8)+": "+regs[(i+8)]+"\t$"+(i+16)+": "+regs[(i+16)]+"\t$"+(i+24)+": "+regs[(i+24)]);
}
+ System.out.println("");
}
public void start() {
int index = 0;
- while((index=nextInstructionIndex())!=-1){
- System.out.print("Instruction "+index+":");
+ while(index < instructions.size()) {
+ System.out.print("Instruction "+index+": ");
executeInstruction(instructions.get(index));
+ index = nextInstructionIndex();
}
}
12 test.default.txt
View
@@ -0,0 +1,12 @@
+940179471
+940245017
+23750666
+1366163471
+21717004
+23754764
+21659660
+21727249
+23760913
+940179464
+491454464
+223543296
Please sign in to comment.
Something went wrong with that request. Please try again.