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

View File

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

View File

@ -19,7 +19,12 @@ public class InstructionFetch {
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 newInstruction = containingProcessor.getMainMemory().getWord(currentPC);
@ -28,6 +33,7 @@ public class InstructionFetch {
IF_EnableLatch.setIF_enable(false);
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 processor.Processor;
import generic.Instruction.OperationType;
import generic.Operand.OperandType;
import generic.Operand;
public class OperandFetch {
@ -20,6 +21,21 @@ public class OperandFetch {
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()
{
if(IF_OF_Latch.isOF_enable())
@ -27,6 +43,15 @@ public class OperandFetch {
int instruction = IF_OF_Latch.getInstruction();
Instruction instr = new 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);
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 R1I_type_operators[] = {24,29};
// 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;
// }
if (Arrays.asList(R3_type_operators).contains(opcode)){
// check if the instruction is of type R3
if (Arrays.stream(R3_type_operators).anyMatch(x -> x == opcode)) {
Operand rs1 = new Operand();
Operand rs2 = new Operand();
Operand rd = new Operand();
@ -52,8 +69,8 @@ public class OperandFetch {
rd.setOperandType(Operand.OperandType.Register);
rs1.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
rs2.setValue(Integer.parseInt(bin_instr.substring(9, 14), 2));
rd.setValue(Integer.parseInt(bin_instr.substring(14, 19), 2));
rs2.setValue(Integer.parseInt(bin_instr.substring(10, 15), 2));
rd.setValue(Integer.parseInt(bin_instr.substring(15, 20), 2));
int op1 = containingProcessor.getRegisterFile().getValue(rs1.getValue());
int op2 = containingProcessor.getRegisterFile().getValue(rs2.getValue());
@ -65,37 +82,71 @@ public class OperandFetch {
instr.setSourceOperand1(rs1);
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 rd = new Operand();
rs1.setOperandType(Operand.OperandType.Register);
rd.setOperandType(Operand.OperandType.Register);
rs1.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
rd.setValue(Integer.parseInt(bin_instr.substring(14, 19), 2));
int imm = Integer.parseInt(bin_instr.substring(19, 32), 2); // TODO: 2's complement
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(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 op2 = containingProcessor.getRegisterFile().getValue(rd.getValue());
System.out.println("imm: " + imm);
OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setImm(imm);
OF_EX_Latch.setOp1(op1);
OF_EX_Latch.setOp2(op2);
System.out.println("op1: " + op1);
System.out.println("op2: " + rd);
instr.setDestinationOperand(rd);
instr.setSourceOperand1(rs1);
}
else if (Arrays.asList(R1I_type_operators).contains(opcode)){
Operand rd = new Operand();
rd.setOperandType(Operand.OperandType.Register);
rd.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
else if (Arrays.stream(R1I_type_operators).anyMatch(x -> x == opcode)) {
if(opcode != 24){
Operand rd = new Operand();
rd.setOperandType(Operand.OperandType.Register);
rd.setValue(Integer.parseInt(bin_instr.substring(5, 10), 2));
instr.setDestinationOperand(rd);
instr.setDestinationOperand(rd);
int imm = Integer.parseInt(bin_instr.substring(10, 32), 2); // TODO: 2's complement
OF_EX_Latch.setInstruction(instr);
OF_EX_Latch.setImm(imm);
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.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);

View File

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