Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. Team Members
Operation ALUOp
Add 0000
Sub 0001
Addi 0010
LW 0011
SW 0100
And 0101
Or 0110
Ori 1001
SLL 1010
BEQ 1100
BNE 1101
SLT 1110
J 1111
0x10 0x0101
0x12 0x0110
0x14 0x0011
0x16 0x00F0
0x18 0xFFFF
Register 0 ($v0) 0
Register 1 ($v1) 0
Register 2 ($v2) 0
Register 3 ($v3) 0
Register 4 ($a0) 0
Register 5 ($a1) 0
Register 6 ($t0) 0
Register 7 ($t1) 0
After the program is compiled and run the following output appears:
MEMORY
Memory[1000]: 0010011100000100
Memory[1002]: 1010000000000100
Memory[1004]: 0010011100010001
Memory[1006]: 0010011100100111
Memory[1008]: 1010001000100001
Memory[1010]: 0010001000100001
Memory[1012]: 0010001000110000
Memory[1014]: 1010001100110100
Memory[1016]: 0010011101000000
Memory[1018]: 0010011101010100
Memory[1020]: 1010010101010010
Memory[1022]: 0010011101100101
Memory[1024]: 0010011111110001
Memory[1026]: 1110011101101000
Memory[1028]: 1100111110000010
Memory[1030]: 1111010001010110
Memory[1032]: 0001011011110110
Memory[1034]: 0011010101000000
Memory[1036]: 0010010101010010
Memory[1038]: 0010011110010111
Memory[1040]: 1010100110010001
Memory[1042]: 0010100110010001
Memory[1044]: 1010100110010011
Memory[1046]: 0010100110010111
Memory[1048]: 1010100110010001
Memory[1050]: 0010100110010001
Memory[1052]: 1110100101001010
Memory[1054]:
Memory[1056]:
REGISTERS
Register[0]: 0
Register[1]: 0
Register[2]: 0
Register[3]: 0
Register[4]: 0
Register[5]: 0
Register[6]: 0
Register[7]: 0
REGISTERS
Register[0]: 8
Register[1]: 256
Register[2]: 60
Register[3]: 240
Register[4]: 255
Register[5]: 26
Register[6]: 0
Register[7]: 0
MEMORY
Memory[16]: 65535
Memory[18]: 65535
Memory[20]: 65535
Memory[22]: 255
Memory[24]: 255
9. Discussion
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <cmath>
#include <sstream>
using namespace std;
//Defining functions
void Initialize( );
void funcALU( int ALUa, int ALUb );
void Fetch( );
void Decode( );
void Execute( int state );
void MemAccess( int ALUResult );
void WriteBack( );
int binaryDecimal ( string );
//Variables
int y,
z,
c1,
c2,
c3;
string Memory[ 1200 ];
int regArray[ 16 ];
int ALUread = 0;
string memstring;
int programCounter = 0;
string instruction;
string opCode;
string rs,
rt,
rd,
imm,
jaddr;
int state,
nextState,
ALUa,
ALUb,
ALUResult;
int memData,
WriteData,
WriteReg,
intJump = 0;
//Control Signals
int PCWriteCond,
PCWrite,
IorD,
MemRead,
MemWrite,
MemtoReg,
IRWrite,
PCSource,
ALUOp,
ALUSrcB,
ALUSrcA,
RegWrite,
RegDst;
int main( )
{ //Initialize Memory contents
Memory[ 16 ] = "0000000100000001"; // 0101hex
Memory[ 18 ] = "0000000100010000"; // 0110hex
Memory[ 20 ] = "0000000000010001"; // 0011hex
Memory[ 22 ] = "0000000011110000"; // 00F0hex
Memory[ 24 ] = "0000000011111111"; // 00FFhex
programCounter = 1000;
while( programCounter < 1110 )
{
cout << programCounter << " - ";
Initialize( );
Fetch( );
Decode( );
//funcALU(ALUa, ALUb);
//funcALU(ALUa,ALUb);
system( "pause" );
return 0;
}
else
adder = 0;
return value;
}
void Initialize( )
{
PCWriteCond = 0;
PCWrite = 0;
IorD = 0;
MemRead = 0;
MemWrite = 0;
MemtoReg = 0;
IRWrite = 0;
PCSource = 0;
ALUOp = 0;
ALUSrcB = 0;
ALUSrcA = 0;
RegWrite = 0;
RegDst = 0;
void Fetch( )
{
MemRead = 1;
ALUSrcA = 0;
IorD = 0;
IRWrite = 1;
ALUSrcB = 1;
ALUOp = 0;
PCWrite = 1;
PCSource = 0;
void Decode( )
{
ALUSrcA = 0;
ALUSrcB = 3;
ALUOp = 0;
//R-type instructions
if( opCode == "0000" || opCode == "0001"
|| opCode == "0101" || opCode == "0110"
|| opCode == "1000" || opCode == "1110" )
{
rs = instruction.substr( 4, 4 );
rt = instruction.substr( 8, 4 );
rd = instruction.substr( 12, 4 );
ALUa = binaryDecimal( rs );
ALUb = binaryDecimal( rt );
//I-type instructions
if( opCode == "0010" || opCode == "0011"
|| opCode == "0100" || opCode == "0111"
|| opCode == "1001" || opCode == "1010"
|| opCode == "1011" || opCode == "1100"
|| opCode == "1101" || opCode == "1111"
)
{
rs = instruction.substr( 4, 4 );
rt = instruction.substr( 8, 4 );
imm = instruction.substr( 12, 4 );
ALUa = binaryDecimal( rs );
ALUb = binaryDecimal( rt );
//J-type instruction
if( opCode == "1111" )
{
jaddr = instruction.substr( 4, 12 );
}
nextState = 0;
}
if( state == 5 ) // lw
{
ALUSrcA = 1;
ALUSrcB = 2;
ALUOp = 2;
MemRead = 1;
MemWrite = 0;
IorD = 1;
RegDst = 0;
RegWrite = 1;
MemtoReg = 1;
funcALU( ALUa, ALUb );
MemAccess( ALUResult );
}
if( state == 6 ) // sw
{
ALUa = binaryDecimal( instruction.substr( 4, 4 ) );
ALUb = binaryDecimal( instruction.substr( 12, 4 ) );
ALUSrcA = 1;
ALUSrcB = 2;
ALUOp = 2;
MemWrite = 1;
MemRead = 0;
IorD = 1;
funcALU( ALUa, ALUb );
MemAccess( ALUResult );
}
if( state == 8 ) // or
{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 4;
RegDst = 1;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}
cout << "jaddr: " << jaddr << "; intJump: " << intJump << endl;
programCounter = intJump;
}
}
if( ALUOp == 2 )
{ // lw, sw
if( MemRead == 1 )
{
muxB = ( binaryDecimal( imm ) * 2 );
ALUResult = muxA + muxB;
cout << "LW/SW: MuxA = " << muxA << " MuxB = " << muxB << " ALUResult = "
<< ALUResult << " Instruction: " << instruction << endl;
}
}
if( ALUOp == 5 )
{ // lwi
if( MemRead == 1 )
{
muxB = ( binaryDecimal( imm ) + 4 );
ALUResult = muxA + muxB;
cout << "LW/SW: MuxA = " << muxA << " MuxB = " << muxB << " ALUResult = "
<< ALUResult << " Instruction: " << instruction << endl;
}
}
}
else
{
regArray[ rdi ] = 0;
}
}
while( ALUOp == 15 ) // mac
{
temp1 = muxA * muxB;
temp2 = temp2 + temp1;
ALUResult = temp2;
}
}
//load word
if( ( MemRead == 1 ) && ( IorD == 1 ) )
{
z = binaryDecimal( instruction.substr( 8, 4 ) );
cout << "ALUResult: " << ALUResult << endl;
cout << "LOAD WORD! regArray[" << z << "] = " << Memory[ ALUResult ] << endl;
regArray[ z ] = binaryDecimal( Memory[ ALUResult ] );
memData = atoi( Memory[ ALUResult ].c_str( ) );
}
//store word
if( ( MemWrite == 1 ) && ( IorD == 1 ) )
{
cout << "skm" << endl;
if( ALUread == 0 )
{
z = binaryDecimal( instruction.substr( 4, 4 ) );
c1 = regArray[ z ];
cout << "z = " << z << "; regArray[" << z << "] = " << regArray[ z ] << endl;
ALUread = 1;
}
y = binaryDecimal( instruction.substr( 8, 4 ) );
y = regArray[ y ];
y = y - 2;
string str;
stringstream out;
out << c3;
str = out.str( );
Memory[ y ] = str;
cout << "Memory[" << y << "] = " << Memory[ y ] << "; y = " << y << endl;
}
void WriteBack( )
{
cout << "*************WRITEBACK********";
char temp[ 16 ];
if( MemtoReg == 1 )
WriteData = memData;
if( MemtoReg == 0 )
WriteData = ALUResult;
if( RegDst == 1 )
WriteReg = binaryDecimal( rd.c_str( ) );
if( RegDst == 0 )
WriteReg = binaryDecimal( rt.c_str( ) );