Professional Documents
Culture Documents
/*##########################################################################
#########
Note: Please don’t upload the assignments, template file/solution and lab. manual on GitHub
or others public repository.
It violates the BITS’s Intellectual Property Rights (IPR).
************************************************************************************/
#include <stdio.h>
#include <cstdint>
int32_t JAddr;
uint8_t Shamt; // for shifting
uint8_t Funct, ALUOP;
uint8_t RS, RT, RD;
int16_t Immediate;
bool ZeroFlag; //
int32_t RegFile[32];
uint8_t Imem[256], Dmem[256]; //2^8 locations, byte addressable Instruction and Data
memory
uint8_t run = 1; //
if (RegWrite==true){
if (in_reg == 0 )
printf("Don't try to modify R0\n");
else
RegFile[in_reg] = *out_reg;}
break;
//used for BNE //
case 1: result = in1 - in2;
break;
//for rest of the instruction types
case 2:
switch(Funct) {
break;
}
break;
}
if (result == 0)
ZeroFlag = true;
else
ZeroFlag = false;
return result;
}
uint32_t readIMM(uint32_t addr) {
//Big-endian format - lower byte - higher address
uint8_t byte0, byte1, byte2, byte3;
uint32_t instr;
byte3 = Imem[addr+0];
byte2 = Imem[addr+1];
byte1 = Imem[addr+2];
byte0 = Imem[addr+3];
instr = (byte3 << 24) + (byte2 << 16) + (byte1 << 8) + byte0;
return instr;
}
int readDMM(int addr, bool MemRead) {
//Big-endian format - lower byte - higher address
uint8_t byte0, byte1, byte2, byte3;
int data;
if (MemRead) {
byte3 = Dmem[addr+0];
byte2 = Dmem[addr+1];
byte1 = Dmem[addr+2];
byte0 = Dmem[addr+3];
data = (byte3 << 24) + (byte2 << 16) + (byte1 << 8) + byte0;
}
else
printf("Error from Data memory's read module\n");
return data;
}
if (MemWrite) {
byte3 = data >> 24;
byte2 = (data & 0b00000000111111110000000000000000) >> 16;
byte1 = (data & 0b00000000000000001111111100000000) >> 8;
byte0 = (data & 0b00000000000000000000000011111111);
Dmem[addr+0] = byte3;
Dmem[addr+1] = byte2;
Dmem[addr+2] = byte1;
Dmem[addr+3] = byte0;
}
else
printf("Error from Data memory's write module\n");
uint8_t fetchDecodeFSM(void) {
uint8_t Opcode; // Other Global variable can be used.
//Write your code here
// incremement PC
PC = incrementPC(PC);
return Opcode;
void lwFSM(void) {
void swFSM(void) {
int32_t out_reg1, out_reg2, out_reg;
//Write your code here
void addFSM(void) {
void addiFSM(void) {
// compute addi
out_reg = ALU(out_reg1, signExtn(Immediate));
// write data to target reg
writeRegfile(RT, true, &out_reg);
void bneFSM(void) {
int32_t out_reg1, out_reg2, out_reg;
//Write your code here
void stpFSM(void) {
run=0;
}
void load_program(void);
int main(void) {
RegFile[0] = 0;
load_program();
PC = 0;
while (run) {
uint8_t opcode = fetchDecodeFSM();
switch (opcode)
{
case R_OP:
ALUOP = 2;
switch (Funct) {
case ADD_Func: addFSM();
break;
}
break;
case ADDI_OP: ALUOP = 0; addiFSM(); break;
case LW_OP: ALUOP = 0; lwFSM(); break;
case SW_OP: ALUOP = 0; swFSM(); break;
case BNE_OP: ALUOP = 1; bneFSM(); break;
case STP_OP: stpFSM(); break;
}
}
printf("The Fibonacci numbers are:\n");
for (int i = 0; i < 7; i++) {
uint8_t byte0, byte1, byte2, byte3;
int data;
}
void load_program(void) {
//Big-endian format lower-order addresses are used for the most significant byte
Dmem[255] = 0b00000001;
Dmem[254] = 0b00000000;
Dmem[253] = 0b00000000;
Dmem[252] = 0b00000000;
Dmem[251] = 0b00000001;
Dmem[250] = 0b00000000;
Dmem[249] = 0b00000000;
Dmem[248] = 0b00000000;
//Instructions