final submission assignment-3

Co-authored-by: SriRam Mudragada <iam-msr@users.noreply.github.com>
This commit is contained in:
karthikmurakonda 2022-09-24 08:52:33 +05:30
parent 2b47f19f20
commit 7b33c16d10
6 changed files with 162 additions and 60 deletions

BIN
assignment-3/report.pdf Normal file

Binary file not shown.

View File

@ -2,6 +2,7 @@ package generic;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.DataInputStream;
import processor.Clock; import processor.Clock;
import processor.Processor; import processor.Processor;
@ -34,22 +35,18 @@ public class Simulator {
try ( try (
InputStream is = new FileInputStream(assemblyProgramFile); InputStream is = new FileInputStream(assemblyProgramFile);
){ ){
int i = 0; DataInputStream d_is = new DataInputStream(is);
byte[] line = new byte[4]; int address = -1;
boolean isFirstLine = true; while(d_is.available() > 0){
while(is.read(line) != -1) { int next = d_is.readInt();
int value = 0; System.out.println(next);
for(int j = 0; j < 4; j++) { if(address == -1){
value = (value << 8) | (line[j] & 0xff); processor.getRegisterFile().setProgramCounter(next);
} }
System.out.println(value); else{
if(isFirstLine) { processor.getMainMemory().setWord(address, next);
processor.getRegisterFile().setProgramCounter(value);
isFirstLine = false;
}else{
processor.getMainMemory().setWord(i, value);
i++;
} }
address += 1;
} }
processor.getRegisterFile().setValue(0, 0); processor.getRegisterFile().setValue(0, 0);
processor.getRegisterFile().setValue(1, 65535); processor.getRegisterFile().setValue(1, 65535);

View File

@ -1,5 +1,6 @@
package processor.pipeline; package processor.pipeline;
import processor.Processor; import processor.Processor;
import generic.Instruction; import generic.Instruction;
import generic.Instruction.OperationType; import generic.Instruction.OperationType;
import generic.Simulator; import generic.Simulator;
@ -21,16 +22,21 @@ public class Execute {
public void performEX() public void performEX()
{ {
//TODO // storing x31 here itself to not to complicate.
// TODO:remove this later in pipeline
if(OF_EX_Latch.isEX_enable()) if(OF_EX_Latch.isEX_enable())
{ {
int op1 = OF_EX_Latch.getOp1(); int op1 = OF_EX_Latch.getOp1();
int op2 = OF_EX_Latch.getOp2(); int op2 = OF_EX_Latch.getOp2();
int imm = OF_EX_Latch.getImm(); int imm = OF_EX_Latch.getImm();
System.out.println("op1: "+op1+" op2: "+op2+" imm: "+imm);
Instruction instruction = OF_EX_Latch.getInstruction(); Instruction instruction = OF_EX_Latch.getInstruction();
int cur_pc = containingProcessor.getRegisterFile().getProgramCounter(); int cur_pc = containingProcessor.getRegisterFile().getProgramCounter();
int alu_result = 0; int alu_result = 0;
System.out.println("EX: " + instruction);
OperationType alu_op = OF_EX_Latch.getInstruction().getOperationType(); OperationType alu_op = OF_EX_Latch.getInstruction().getOperationType();
System.out.println("ALU OP: " + alu_op);
boolean noma = false;
switch(alu_op) switch(alu_op)
{ {
case add: alu_result = op1 + op2; break; case add: alu_result = op1 + op2; break;
@ -39,8 +45,14 @@ public class Execute {
case subi: alu_result = op1 - imm; break; case subi: alu_result = op1 - imm; break;
case mul: alu_result = op1 * op2; break; case mul: alu_result = op1 * op2; break;
case muli: alu_result = op1 * imm; break; case muli: alu_result = op1 * imm; break;
case div: alu_result = op1 / op2; break; case div:
case divi: alu_result = op1 / imm; break; alu_result = op1 / op2;
containingProcessor.getRegisterFile().setValue(31, op1 % op2);
break;
case divi:
alu_result = op1 / imm;
containingProcessor.getRegisterFile().setValue(31, op1 % imm);
break;
case and: alu_result = op1 & op2; break; case and: alu_result = op1 & op2; break;
case andi: alu_result = op1 & imm; break; case andi: alu_result = op1 & imm; break;
case or: alu_result = op1 | op2; break; case or: alu_result = op1 | op2; break;
@ -49,25 +61,48 @@ public class Execute {
case xori: alu_result = op1 ^ imm; break; case xori: alu_result = op1 ^ imm; break;
case slt: alu_result= (op1 < op2) ? 1 : 0; break; case slt: alu_result= (op1 < op2) ? 1 : 0; break;
case slti: alu_result= (op1 < imm) ? 1 : 0; break; case slti: alu_result= (op1 < imm) ? 1 : 0; break;
case sll: alu_result = op1 << op2; break; case sll:
case slli: alu_result = op1 << imm; break; containingProcessor.getRegisterFile().setValue(31, (int) Math.pow(2, op2));
case srl: alu_result = op1 >>> op2; break; alu_result = op1 << op2;
case srli: alu_result = op1 >>> imm; break; break;
case sra: alu_result = op1 >> op2; break; case slli:
case srai: alu_result = op1 >> imm; break; containingProcessor.getRegisterFile().setValue(31, (int) Math.pow(2, imm));
alu_result = op1 << imm;
break;
case srl:
containingProcessor.getRegisterFile().setValue(31, op1 & (1 << (op2 - 1)));
alu_result = op1 >>> op2;
break;
case srli:
containingProcessor.getRegisterFile().setValue(31, op1 & (1 << (imm - 1)));
alu_result = op1 >>> imm;
break;
case sra:
containingProcessor.getRegisterFile().setValue(31, op1 & (1 << (op2 - 1)));
alu_result = op1 >> op2;
break;
case srai:
containingProcessor.getRegisterFile().setValue(31, op1 & (1 << (imm - 1)));
alu_result = op1 >> imm;
break;
case load: alu_result = op1 + imm; break;
case store: alu_result = op2 + imm; break;
case jmp: case jmp:
{ {
OperandType optype = instruction.getDestinationOperand().getOperandType(); OperandType optype = instruction.getSourceOperand1().getOperandType();
if (optype == OperandType.Register){ if (optype == OperandType.Register){
imm = containingProcessor.getRegisterFile().getValue( imm = containingProcessor.getRegisterFile().getValue(
instruction.getDestinationOperand().getValue()); instruction.getSourceOperand1().getValue());
} }
else{ else{
imm = OF_EX_Latch.getImm(); imm = OF_EX_Latch.getImm();
} }
alu_result = cur_pc + imm; alu_result = cur_pc + imm ;
EX_IF_Latch.setIF_enable(true); EX_IF_Latch.setIF_enable(true);
EX_IF_Latch.setPC(alu_result); EX_IF_Latch.setPC(alu_result);
noma = true;
} }
break; break;
case beq: case beq:
@ -77,6 +112,7 @@ public class Execute {
alu_result = cur_pc + imm; alu_result = cur_pc + imm;
EX_IF_Latch.setIF_enable(true); EX_IF_Latch.setIF_enable(true);
EX_IF_Latch.setPC(alu_result); EX_IF_Latch.setPC(alu_result);
noma = true;
} }
} }
break; break;
@ -87,17 +123,22 @@ public class Execute {
alu_result = cur_pc + imm; alu_result = cur_pc + imm;
EX_IF_Latch.setIF_enable(true); EX_IF_Latch.setIF_enable(true);
EX_IF_Latch.setPC(alu_result); EX_IF_Latch.setPC(alu_result);
noma = true;
} }
} }
break; break;
case blt: case blt:
{ {
if(op1 < op2) if(op1 < op2)
{ {
alu_result = cur_pc + imm; alu_result = cur_pc + imm;
EX_IF_Latch.setIF_enable(true); EX_IF_Latch.setIF_enable(true);
EX_IF_Latch.setPC(alu_result); EX_IF_Latch.setPC(alu_result);
noma = true;
System.out.println("hello world");
} }
System.out.println("hello world2");
} }
break; break;
case bgt: case bgt:
@ -107,6 +148,7 @@ public class Execute {
alu_result = cur_pc + imm; alu_result = cur_pc + imm;
EX_IF_Latch.setIF_enable(true); EX_IF_Latch.setIF_enable(true);
EX_IF_Latch.setPC(alu_result); EX_IF_Latch.setPC(alu_result);
noma = true;
} }
} }
break; break;
@ -118,9 +160,15 @@ public class Execute {
break; break;
} }
System.out.println("ALU RESULT: " + alu_result+"\n\n");
EX_MA_Latch.setALUResult(alu_result); EX_MA_Latch.setALUResult(alu_result);
EX_MA_Latch.setInstruction(OF_EX_Latch.getInstruction()); EX_MA_Latch.setInstruction(OF_EX_Latch.getInstruction());
if(!noma)
{
EX_MA_Latch.setMA_enable(true); EX_MA_Latch.setMA_enable(true);
}
OF_EX_Latch.setEX_enable(false); OF_EX_Latch.setEX_enable(false);
} }
} }

View File

@ -19,7 +19,12 @@ public class InstructionFetch {
public void performIF() public void performIF()
{ {
if(IF_EnableLatch.isIF_enable()) if(EX_IF_Latch.isIF_enable()){
containingProcessor.getRegisterFile().setProgramCounter(EX_IF_Latch.getPC()-1);
System.out.println("IF: PC set to " + EX_IF_Latch.getPC());
}
if(IF_EnableLatch.isIF_enable()|| EX_IF_Latch.isIF_enable())
{ {
int currentPC = containingProcessor.getRegisterFile().getProgramCounter(); int currentPC = containingProcessor.getRegisterFile().getProgramCounter();
int newInstruction = containingProcessor.getMainMemory().getWord(currentPC); int newInstruction = containingProcessor.getMainMemory().getWord(currentPC);
@ -28,6 +33,7 @@ public class InstructionFetch {
IF_EnableLatch.setIF_enable(false); IF_EnableLatch.setIF_enable(false);
IF_OF_Latch.setOF_enable(true); IF_OF_Latch.setOF_enable(true);
EX_IF_Latch.setIF_enable(false);
} }
} }

View File

@ -5,6 +5,7 @@ import java.util.Arrays;
import generic.Instruction; import generic.Instruction;
import processor.Processor; import processor.Processor;
import generic.Instruction.OperationType; import generic.Instruction.OperationType;
import generic.Operand.OperandType;
import generic.Operand; import generic.Operand;
public class OperandFetch { public class OperandFetch {
@ -20,6 +21,21 @@ public class OperandFetch {
this.OF_EX_Latch = oF_EX_Latch; this.OF_EX_Latch = oF_EX_Latch;
} }
public static int twoscompliment(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '0') {
chars[i] = '1';
} else {
chars[i] = '0';
}
}
String s1 = new String(chars);
int num = Integer.parseInt(s1, 2);
num = num + 1;
return num;
}
public void performOF() public void performOF()
{ {
if(IF_OF_Latch.isOF_enable()) if(IF_OF_Latch.isOF_enable())
@ -27,6 +43,15 @@ public class OperandFetch {
int instruction = IF_OF_Latch.getInstruction(); int instruction = IF_OF_Latch.getInstruction();
Instruction instr = new Instruction(); Instruction instr = new Instruction();
String bin_instr = Integer.toBinaryString(instruction); String bin_instr = Integer.toBinaryString(instruction);
if (bin_instr.length() < 32) { // TODO: check if this is correct
int diff = 32 - bin_instr.length();
String zeros = "";
for (int i = 0; i < diff; i++) {
zeros += "0";
}
bin_instr = zeros + bin_instr;
}
instr.setProgramCounter(containingProcessor.getRegisterFile().getProgramCounter());
int opcode = Integer.parseInt(bin_instr.substring(0, 5), 2); int opcode = Integer.parseInt(bin_instr.substring(0, 5), 2);
instr.setOperationType(opTypes[opcode]); instr.setOperationType(opTypes[opcode]);
@ -34,16 +59,8 @@ public class OperandFetch {
int R2I_type_operators[] = {1,3,5,7,9,11,13,15,17,19,21,22,23,25,26,27,28}; int R2I_type_operators[] = {1,3,5,7,9,11,13,15,17,19,21,22,23,25,26,27,28};
int R1I_type_operators[] = {24,29}; int R1I_type_operators[] = {24,29};
// if (bin_instr.length() < 32) { // TODO: check if this is correct // check if the instruction is of type R3
// int diff = 32 - bin_instr.length(); if (Arrays.stream(R3_type_operators).anyMatch(x -> x == opcode)) {
// String zeros = "";
// for (int i = 0; i < diff; i++) {
// zeros += "0";
// }
// bin_instr = zeros + bin_instr;
// }
if (Arrays.asList(R3_type_operators).contains(opcode)){
Operand rs1 = new Operand(); Operand rs1 = new Operand();
Operand rs2 = new Operand(); Operand rs2 = new Operand();
Operand rd = new Operand(); Operand rd = new Operand();
@ -52,8 +69,8 @@ public class OperandFetch {
rd.setOperandType(Operand.OperandType.Register); rd.setOperandType(Operand.OperandType.Register);
rs1.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2)); rs1.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
rs2.setValue(Integer.parseInt(bin_instr.substring(9, 14), 2)); rs2.setValue(Integer.parseInt(bin_instr.substring(10, 15), 2));
rd.setValue(Integer.parseInt(bin_instr.substring(14, 19), 2)); rd.setValue(Integer.parseInt(bin_instr.substring(15, 20), 2));
int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue()); int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue());
int op2 = containingProcessor.getRegisterFile().getValue(rs2.getValue()); int op2 = containingProcessor.getRegisterFile().getValue(rs2.getValue());
@ -65,27 +82,36 @@ public class OperandFetch {
instr.setSourceOperand1(rs1); instr.setSourceOperand1(rs1);
instr.setSourceOperand2(rs2); instr.setSourceOperand2(rs2);
} }
else if (Arrays.asList(R2I_type_operators).contains(opcode)){ else if (Arrays.stream(R2I_type_operators).anyMatch(x -> x == opcode)) {
Operand rs1 = new Operand(); Operand rs1 = new Operand();
Operand rd = new Operand(); Operand rd = new Operand();
rs1.setOperandType(Operand.OperandType.Register); rs1.setOperandType(Operand.OperandType.Register);
rd.setOperandType(Operand.OperandType.Register); rd.setOperandType(Operand.OperandType.Register);
rs1.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2)); rs1.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
rd.setValue(Integer.parseInt(bin_instr.substring(14, 19), 2)); rd.setValue(Integer.parseInt(bin_instr.substring(10, 15), 2));
// check 15th bit to see if it is negative
int imm = Integer.parseInt(bin_instr.substring(19, 32), 2); // TODO: 2's complement int imm = Integer.parseInt(bin_instr.substring(15, 32), 2); // TODO: 2's complement
if (bin_instr.charAt(15)=='1'){
imm = -1*twoscompliment(bin_instr.substring(15, 32));
System.out.println(bin_instr);
}
int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue()); int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue());
int op2 = containingProcessor.getRegisterFile().getValue(rd.getValue());
System.out.println("imm: " + imm);
OF_EX_Latch.setInstruction(instr); OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setImm(imm); OF_EX_Latch.setImm(imm);
OF_EX_Latch.setOp1(op1); OF_EX_Latch.setOp1(op1);
OF_EX_Latch.setOp2(op2);
System.out.println("op1: " + op1);
System.out.println("op2: " + rd);
instr.setDestinationOperand(rd); instr.setDestinationOperand(rd);
instr.setSourceOperand1(rs1); instr.setSourceOperand1(rs1);
} }
else if (Arrays.asList(R1I_type_operators).contains(opcode)){ else if (Arrays.stream(R1I_type_operators).anyMatch(x -> x == opcode)) {
if(opcode != 24){
Operand rd = new Operand(); Operand rd = new Operand();
rd.setOperandType(Operand.OperandType.Register); rd.setOperandType(Operand.OperandType.Register);
rd.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2)); rd.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
@ -93,10 +119,35 @@ public class OperandFetch {
instr.setDestinationOperand(rd); instr.setDestinationOperand(rd);
int imm = Integer.parseInt(bin_instr.substring(10, 32), 2); // TODO: 2's complement int imm = Integer.parseInt(bin_instr.substring(10, 32), 2); // TODO: 2's complement
if (bin_instr.charAt(10)=='1'){
imm = -1*twoscompliment(bin_instr.substring(10, 32));
System.out.println(bin_instr);
}
System.out.println("imm: " + imm);
OF_EX_Latch.setInstruction(instr); OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setImm(imm); OF_EX_Latch.setImm(imm);
} }
else{
Operand op = new Operand();
String imm = bin_instr.substring(10, 32);
int imm_val = Integer.parseInt(imm, 2);
if (imm.charAt(0) == '1'){
imm_val = -1*twoscompliment(imm);
}
if (imm_val != 0){
op.setOperandType(OperandType.Immediate);
op.setValue(imm_val);
instr.setSourceOperand1(op);
}
else{
op.setOperandType(OperandType.Register);
op.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
instr.setSourceOperand1(op);
}
OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setImm(imm_val);
}
}
IF_OF_Latch.setOF_enable(false); IF_OF_Latch.setOF_enable(false);
OF_EX_Latch.setEX_enable(true); OF_EX_Latch.setEX_enable(true);

View File

@ -1,9 +1,9 @@
package processor.pipeline; package processor.pipeline;
import generic.Simulator; import generic.Simulator;
import processor.Processor;
import generic.Instruction; import generic.Instruction;
import generic.Instruction.OperationType; import generic.Instruction.OperationType;
import processor.Processor;
public class RegisterWrite { public class RegisterWrite {
Processor containingProcessor; Processor containingProcessor;