Sei sulla pagina 1di 28

Final Report for Team K

1. Team Members

Team Leader: Srishti, ss878@msstate.edu

Team member: Krishna, kvt11@msstate.edu

2. Meetings (Oct 30 – December 1)

Name # of meetings # of attendance Contribution hours


Srishti 11 11 3-6hrs per meeting
Krishna 11 11 3-6hrs per meeting

3. Datapath with Control unit

a) Datapath without the new instructions:


b) Datapath with the new instructions:
4. Finite State Machine
5. ALU Control

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

6. Initial contents of the memory and register file at clock cycle 0

Memory (data + instructions):

0x10 0x0101
0x12 0x0110
0x14 0x0011
0x16 0x00F0
0x18 0xFFFF

Register File (data):

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

8. Output data from the simulation results:

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

1000 - Decode Opcode: 0010


ALUSrcB binaryDecimal: 0100
*************WRITEBACK********WriteData: 4; WriteReg: 0; RegDst: 0; Temp: 4
RegArray[0]: 4
1002 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0100
*************WRITEBACK********WriteData: 64; WriteReg: 0; RegDst: 0; Temp: 64
RegArray[0]: 64
1004 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 1; WriteReg: 1; RegDst: 0; Temp: 1
RegArray[1]: 1
1006 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 2; RegDst: 0; Temp: 7
RegArray[2]: 7
1008 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 2; RegDst: 0; Temp: 14
RegArray[2]: 14
1010 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 2; RegDst: 0; Temp: 15
RegArray[2]: 15
1012 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0000
*************WRITEBACK********WriteData: 15; WriteReg: 3; RegDst: 0; Temp: 15
RegArray[3]: 15
1014 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0100
*************WRITEBACK********WriteData: 240; WriteReg: 3; RegDst: 0; Temp: 240
RegArray[3]: 240
1016 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0000
*************WRITEBACK********WriteData: 0; WriteReg: 4; RegDst: 0; Temp: 0
RegArray[4]: 0
1018 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0100
*************WRITEBACK********WriteData: 4; WriteReg: 5; RegDst: 0; Temp: 4
RegArray[5]: 4
1020 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0010
*************WRITEBACK********WriteData: 16; WriteReg: 5; RegDst: 0; Temp: 16
RegArray[5]: 16
1022 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0101
*************WRITEBACK********WriteData: 5; WriteReg: 6; RegDst: 0; Temp: 5
RegArray[6]: 5
1024 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 1; WriteReg: 15; RegDst: 0; Temp: 1
RegArray[15]: 1
1026 - Decode Opcode: 1110
1028 - Decode Opcode: 1100
1032 - Decode Opcode: 0001
*************WRITEBACK********WriteData: 4; WriteReg: 6; RegDst: 1; Temp: 4
RegArray[6]: 4
1034 - Decode Opcode: 0011
ALUSrcB binaryDecimal: 0000
LW/SW: MuxA = 16 MuxB = 0 ALUResult = 16 Instruction: 0011010101000000
ALUResult: 16
LOAD WORD! regArray[4] = 0000000100000000
1036 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0010
*************WRITEBACK********WriteData: 18; WriteReg: 5; RegDst: 0; Temp: 18
RegArray[5]: 18
1038 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 9; RegDst: 0; Temp: 7
RegArray[9]: 7
1040 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 9; RegDst: 0; Temp: 14
RegArray[9]: 14
1042 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 9; RegDst: 0; Temp: 15
RegArray[9]: 15
1044 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 9; RegDst: 0; Temp: 120
RegArray[9]: 120
1046 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 9; RegDst: 0; Temp: 127
RegArray[9]: 127
1048 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 9; RegDst: 0; Temp: 254
RegArray[9]: 254
1050 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 9; RegDst: 0; Temp: 255
RegArray[9]: 255
1052 - Decode Opcode: 1110
1054 - Decode Opcode: 1100
1058 - Decode Opcode: 1011
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 32; WriteReg: 0; RegDst: 0; Temp: 32
RegArray[0]: 32
1060 - Decode Opcode: 0101
*************WRITEBACK********WriteData: 0; WriteReg: 11; RegDst: 1; Temp: 0
RegArray[11]: 0
1062 - Decode Opcode: 0110
*************WRITEBACK********WriteData: 257; WriteReg: 12; RegDst: 1; Temp: 257
RegArray[12]: 257
1064 - Decode Opcode: 0001
*************WRITEBACK********WriteData: 257; WriteReg: 1; RegDst: 1; Temp: 257
RegArray[1]: 257
1066 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 13; RegDst: 0; Temp: 7
RegArray[13]: 7
1068 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 13; RegDst: 0; Temp: 14
RegArray[13]: 14
1070 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 13; RegDst: 0; Temp: 15
RegArray[13]: 15
1072 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 13; RegDst: 0; Temp: 120
RegArray[13]: 120
1074 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 13; RegDst: 0; Temp: 127
RegArray[13]: 127
1076 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 13; RegDst: 0; Temp: 254
RegArray[13]: 254
1078 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 13; RegDst: 0; Temp: 255
RegArray[13]: 255
1080 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 1110
ALOHA
z = 13; regArray[13] = 255
1082 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 0000
ALOHA
c1: 255; c2: 65280; c3: 65535
Memory[16] = 65535; y = 16
1084 - Decode Opcode: 1111
jaddr: 010000000010; intJump: 1026
1026 - Decode Opcode: 1110
1028 - Decode Opcode: 1100
1032 - Decode Opcode: 0001
*************WRITEBACK********WriteData: 3; WriteReg: 6; RegDst: 1; Temp: 3
RegArray[6]: 3
1034 - Decode Opcode: 0011
ALUSrcB binaryDecimal: 0000
LW/SW: MuxA = 18 MuxB = 0 ALUResult = 18 Instruction: 0011010101000000
ALUResult: 18
LOAD WORD! regArray[4] = 0000000100010000
1036 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0010
*************WRITEBACK********WriteData: 20; WriteReg: 5; RegDst: 0; Temp: 20
RegArray[5]: 20
1038 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 9; RegDst: 0; Temp: 7
RegArray[9]: 7
1040 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 9; RegDst: 0; Temp: 14
RegArray[9]: 14
1042 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 9; RegDst: 0; Temp: 15
RegArray[9]: 15
1044 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 9; RegDst: 0; Temp: 120
RegArray[9]: 120
1046 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 9; RegDst: 0; Temp: 127
RegArray[9]: 127
1048 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 9; RegDst: 0; Temp: 254
RegArray[9]: 254
1050 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 9; RegDst: 0; Temp: 255
RegArray[9]: 255
1052 - Decode Opcode: 1110
1054 - Decode Opcode: 1100
1058 - Decode Opcode: 1011
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 16; WriteReg: 0; RegDst: 0; Temp: 16
RegArray[0]: 16
1060 - Decode Opcode: 0101
*************WRITEBACK********WriteData: 256; WriteReg: 11; RegDst: 1; Temp: 256
RegArray[11]: 256
1062 - Decode Opcode: 0110
*************WRITEBACK********WriteData: 273; WriteReg: 12; RegDst: 1; Temp: 273
RegArray[12]: 273
1064 - Decode Opcode: 0001
*************WRITEBACK********WriteData: 17; WriteReg: 1; RegDst: 1; Temp: 17
RegArray[1]: 17
1066 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 13; RegDst: 0; Temp: 7
RegArray[13]: 7
1068 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 13; RegDst: 0; Temp: 14
RegArray[13]: 14
1070 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 13; RegDst: 0; Temp: 15
RegArray[13]: 15
1072 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 13; RegDst: 0; Temp: 120
RegArray[13]: 120
1074 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 13; RegDst: 0; Temp: 127
RegArray[13]: 127
1076 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 13; RegDst: 0; Temp: 254
RegArray[13]: 254
1078 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 13; RegDst: 0; Temp: 255
RegArray[13]: 255
1080 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 1110
ALOHA
z = 13; regArray[13] = 255
1082 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 0000
ALOHA
c1: 255; c2: 65280; c3: 65535
Memory[18] = 65535; y = 18
1084 - Decode Opcode: 1111
jaddr: 010000000010; intJump: 1026
1026 - Decode Opcode: 1110
1028 - Decode Opcode: 1100
1032 - Decode Opcode: 0001
*************WRITEBACK********WriteData: 2; WriteReg: 6; RegDst: 1; Temp: 2
RegArray[6]: 2
1034 - Decode Opcode: 0011
ALUSrcB binaryDecimal: 0000
LW/SW: MuxA = 20 MuxB = 0 ALUResult = 20 Instruction: 0011010101000000
ALUResult: 20
LOAD WORD! regArray[4] = 0000000100010001
1036 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0010
*************WRITEBACK********WriteData: 22; WriteReg: 5; RegDst: 0; Temp: 22
RegArray[5]: 22
1038 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 9; RegDst: 0; Temp: 7
RegArray[9]: 7
1040 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 9; RegDst: 0; Temp: 14
RegArray[9]: 14
1042 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 9; RegDst: 0; Temp: 15
RegArray[9]: 15
1044 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 9; RegDst: 0; Temp: 120
RegArray[9]: 120
1046 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 9; RegDst: 0; Temp: 127
RegArray[9]: 127
1048 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 9; RegDst: 0; Temp: 254
RegArray[9]: 254
1050 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 9; RegDst: 0; Temp: 255
RegArray[9]: 255
1052 - Decode Opcode: 1110
1054 - Decode Opcode: 1100
1058 - Decode Opcode: 1011
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 8; WriteReg: 0; RegDst: 0; Temp: 8
RegArray[0]: 8
1060 - Decode Opcode: 0101
*************WRITEBACK********WriteData: 17; WriteReg: 11; RegDst: 1; Temp: 17
RegArray[11]: 17
1062 - Decode Opcode: 0110
*************WRITEBACK********WriteData: 273; WriteReg: 12; RegDst: 1; Temp: 273
RegArray[12]: 273
1064 - Decode Opcode: 0001
*************WRITEBACK********WriteData: 256; WriteReg: 1; RegDst: 1; Temp: 256
RegArray[1]: 256
1066 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 13; RegDst: 0; Temp: 7
RegArray[13]: 7
1068 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 13; RegDst: 0; Temp: 14
RegArray[13]: 14
1070 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 13; RegDst: 0; Temp: 15
RegArray[13]: 15
1072 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 13; RegDst: 0; Temp: 120
RegArray[13]: 120
1074 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 13; RegDst: 0; Temp: 127
RegArray[13]: 127
1076 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 13; RegDst: 0; Temp: 254
RegArray[13]: 254
1078 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 13; RegDst: 0; Temp: 255
RegArray[13]: 255
1080 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 1110
ALOHA
z = 13; regArray[13] = 255
1082 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 0000
ALOHA
c1: 255; c2: 65280; c3: 65535
Memory[20] = 65535; y = 20
1084 - Decode Opcode: 1111
jaddr: 010000000010; intJump: 1026
1026 - Decode Opcode: 1110
1028 - Decode Opcode: 1100
1032 - Decode Opcode: 0001
*************WRITEBACK********WriteData: 1; WriteReg: 6; RegDst: 1; Temp: 1
RegArray[6]: 1
1034 - Decode Opcode: 0011
ALUSrcB binaryDecimal: 0000
LW/SW: MuxA = 22 MuxB = 0 ALUResult = 22 Instruction: 0011010101000000
ALUResult: 22
LOAD WORD! regArray[4] = 0000000011110000
1036 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0010
*************WRITEBACK********WriteData: 24; WriteReg: 5; RegDst: 0; Temp: 24
RegArray[5]: 24
1038 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 9; RegDst: 0; Temp: 7
RegArray[9]: 7
1040 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 9; RegDst: 0; Temp: 14
RegArray[9]: 14
1042 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 9; RegDst: 0; Temp: 15
RegArray[9]: 15
1044 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 9; RegDst: 0; Temp: 120
RegArray[9]: 120
1046 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 9; RegDst: 0; Temp: 127
RegArray[9]: 127
1048 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 9; RegDst: 0; Temp: 254
RegArray[9]: 254
1050 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 9; RegDst: 0; Temp: 255
RegArray[9]: 255
1052 - Decode Opcode: 1110
1054 - Decode Opcode: 1100
1056 - Decode Opcode: 1111
jaddr: 010000111110; intJump: 1086
1086 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 30; WriteReg: 2; RegDst: 0; Temp: 30
RegArray[2]: 30
1088 - Decode Opcode: 0101
*************WRITEBACK********WriteData: 240; WriteReg: 3; RegDst: 1; Temp: 240
RegArray[3]: 240
1090 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 14; RegDst: 0; Temp: 7
RegArray[14]: 7
1092 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 14; RegDst: 0; Temp: 14
RegArray[14]: 14
1094 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 14; RegDst: 0; Temp: 15
RegArray[14]: 15
1096 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 14; RegDst: 0; Temp: 120
RegArray[14]: 120
1098 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 14; RegDst: 0; Temp: 127
RegArray[14]: 127
1100 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 14; RegDst: 0; Temp: 254
RegArray[14]: 254
1102 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 14; RegDst: 0; Temp: 255
RegArray[14]: 255
1104 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 0000
ALOHA
z = 14; regArray[14] = 255
1106 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 1110
ALOHA
c1: 255; c2: 0; c3: 255
Memory[22] = 255; y = 22
1108 - Decode Opcode: 1111
jaddr: 010000000010; intJump: 1026
1026 - Decode Opcode: 1110
1028 - Decode Opcode: 1100
1032 - Decode Opcode: 0001
*************WRITEBACK********WriteData: 0; WriteReg: 6; RegDst: 1; Temp: 0
RegArray[6]: 0
1034 - Decode Opcode: 0011
ALUSrcB binaryDecimal: 0000
LW/SW: MuxA = 24 MuxB = 0 ALUResult = 24 Instruction: 0011010101000000
ALUResult: 24
LOAD WORD! regArray[4] = 0000000011111111
1036 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0010
*************WRITEBACK********WriteData: 26; WriteReg: 5; RegDst: 0; Temp: 26
RegArray[5]: 26
1038 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 9; RegDst: 0; Temp: 7
RegArray[9]: 7
1040 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 9; RegDst: 0; Temp: 14
RegArray[9]: 14
1042 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 9; RegDst: 0; Temp: 15
RegArray[9]: 15
1044 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 9; RegDst: 0; Temp: 120
RegArray[9]: 120
1046 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 9; RegDst: 0; Temp: 127
RegArray[9]: 127
1048 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 9; RegDst: 0; Temp: 254
RegArray[9]: 254
1050 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 9; RegDst: 0; Temp: 255
RegArray[9]: 255
1052 - Decode Opcode: 1110
1054 - Decode Opcode: 1100
1056 - Decode Opcode: 1111
jaddr: 010000111110; intJump: 1086
1086 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 60; WriteReg: 2; RegDst: 0; Temp: 60
RegArray[2]: 60
1088 - Decode Opcode: 0101
*************WRITEBACK********WriteData: 240; WriteReg: 3; RegDst: 1; Temp: 240
RegArray[3]: 240
1090 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 7; WriteReg: 14; RegDst: 0; Temp: 7
RegArray[14]: 7
1092 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 14; WriteReg: 14; RegDst: 0; Temp: 14
RegArray[14]: 14
1094 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 15; WriteReg: 14; RegDst: 0; Temp: 15
RegArray[14]: 15
1096 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0011
*************WRITEBACK********WriteData: 120; WriteReg: 14; RegDst: 0; Temp: 120
RegArray[14]: 120
1098 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0111
*************WRITEBACK********WriteData: 127; WriteReg: 14; RegDst: 0; Temp: 127
RegArray[14]: 127
1100 - Decode Opcode: 1010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 254; WriteReg: 14; RegDst: 0; Temp: 254
RegArray[14]: 254
1102 - Decode Opcode: 0010
ALUSrcB binaryDecimal: 0001
*************WRITEBACK********WriteData: 255; WriteReg: 14; RegDst: 0; Temp: 255
RegArray[14]: 255
1104 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 0000
ALOHA
z = 14; regArray[14] = 255
1106 - Decode Opcode: 0100
ALUSrcB binaryDecimal: 1110
ALOHA
c1: 255; c2: 0; c3: 255
Memory[24] = 255; y = 24
1108 - Decode Opcode: 1111
jaddr: 010000000010; intJump: 1026
1026 - Decode Opcode: 1110
1028 - Decode Opcode: 1100
1030 - Decode Opcode: 1111
jaddr: 010001010110; intJump: 1110

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

The program, written using visual studio 2005 compiles successfully.


The executable generated runs successfully for the instructions related to the pseudo code provided.
The issue of keeping track of the clock cycles has not been dealt with. Thus the program runs and gives the
correct outputs but it does not mention the clock cycle of execution.
There has been confusion in the total number of registers. The program deals with using 8 registers; however,
implementation with 16 registers might have been better.

10. Final Version of Simulator Code (Attached)

#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

//Opening and storing the input file into memory


ifstream inFile;
inFile.open( "machinecode.txt" );
int tempInt;
for( int i = 0; i <= 108; i = i + 2 ) // < >
{
getline( inFile, memstring );

Memory[ 1000 + i ] = memstring;


}

//Closing the input file


inFile.close( );

//Printing the initial memory contents to the screen


cout << "MEMORY" << endl;
for( int k = 1000; k <= 1056; k = k + 2 ) // < >
cout << "Memory[" << k << "]: " << Memory[ k ] << endl;

//Printing the initial register contents to the screen


cout << endl << "REGISTERS" << endl;
for( int k = 0; k <= 15; k++ ) // < >
cout << "Register[" << k << "]: " << regArray[ k ] << endl;

programCounter = 1000;
while( programCounter < 1110 )
{
cout << programCounter << " - ";

Initialize( );
Fetch( );
Decode( );

//funcALU(ALUa, ALUb);

//Printing the final register contents to the screen


cout << endl << "REGISTERS" << endl;
for( int k = 0; k <= 15; k++ ) // < >
cout << "Register[" << k << "]: " << regArray[ k ] << endl;

//Printing the initial memory contents to the screen


cout << "MEMORY" << endl;
for( int k = 16; k <= 24; k = k + 2 ) // < >
cout << "Memory[" << k << "]: " << Memory[ k ] << endl;

//funcALU(ALUa,ALUb);

system( "pause" );
return 0;
}

int binaryDecimal( string memstring )


{
int temp = 0;
int adder = 0;
int multiplier = 1;
int value = 0;

for( int i = memstring.size( ) - 1; i >= 0; i-- ) // < >


{
if( temp > 0 )
{
multiplier = 1;
for( int j = 0; j < temp; j++ ) // <>
{
multiplier = multiplier * 2;
}
}

if( memstring[ i ] == '1' )


{
adder = multiplier;
}

else
adder = 0;

value = value + adder;


temp++;
}

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;

instruction = Memory[ programCounter ];


programCounter = programCounter + 2;

void Decode( )
{
ALUSrcA = 0;
ALUSrcB = 3;
ALUOp = 0;

//Clearing the registers


rs = "";
rt = "";
rd = "";
imm = "";
jaddr = "";

//Opcode is first 4 bits of the instruction


opCode = instruction.substr( 0, 4 );

cout << "Decode Opcode: " << opCode << endl;

//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 );
}

if( opCode == "0000" ) // add


Execute( 2 );
if( opCode == "0001" ) // subtract
Execute( 3 );

if( opCode == "0010" ) // add immediate


Execute( 4 );

if( opCode == "0011" ) // load word


Execute( 5 );

if( opCode == "0100" ) // store word


Execute( 6 );

if( opCode == "0101" ) // and


Execute( 7 );

if( opCode == "0110" ) // or


Execute( 8 );

if( opCode == "0111" ) // lw immediate


Execute( 9 );

if( opCode == "1000" ) // mult and accumulate


Execute( 10 );

if( opCode == "1001" ) // or immediate


Execute( 11 );

if( opCode == "1010" ) // shift left logical


Execute( 12 );

if( opCode == "1011" ) // shift right logical


Execute( 13 );

if( opCode == "1100" ) // branch on equal


Execute( 14 );

if( opCode == "1101" ) // branch on not equal


Execute( 15 );

if( opCode == "1110" ) // set on less than


Execute( 16 );

if( opCode == "1111" ) // jump


Execute( 17 );

nextState = 0;
}

void Execute( int state )


{
if( state == 2 ) // add
{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 0;
RegDst = 1;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}

if( state == 3 ) // subtract


{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 1;
RegDst = 1;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}

if( state == 4 ) // addi


{
ALUSrcA = 1;
ALUSrcB = 2;
ALUOp = 0;
RegDst = 0;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}

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 == 7 ) // and


{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 3;
RegDst = 1;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}

if( state == 8 ) // or
{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 4;
RegDst = 1;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}

if( state == 9 ) // lwi


{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 5;
RegDst = 0;
RegWrite = 1;
MemtoReg = 1;
funcALU( ALUa, ALUb );
WriteBack( );
}

if( state == 10 ) // mac


{
ALUSrcA = 1;
ALUSrcB = 2;
ALUOp = 8;
RegDst = 1;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}

if( state == 11 ) // ori


{
ALUSrcA = 1;
ALUSrcB = 2;
ALUOp = 7;
RegDst = 0;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}

if( state == 12 ) // shift left logical


{
ALUSrcA = 1;
ALUSrcB = 2;
ALUOp = 8;
RegDst = 0;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );

if( state == 13 ) // shift right logical


{
ALUSrcA = 1;
ALUSrcB = 2;
ALUOp = 9;
RegDst = 0;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
WriteBack( );
}

if( state == 14 ) // branch on equal


{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 12;
PCWriteCond = 1;
PCSource = 1;
funcALU( ALUa, ALUb );
}

if( state == 15 ) // branch on not equal


{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 13;
PCWriteCond = 1;
PCSource = 1;
funcALU( ALUa, ALUb );
}

if( state == 16 ) // set on less than


{
ALUSrcA = 1;
ALUSrcB = 0;
ALUOp = 14;
RegDst = 1;
RegWrite = 1;
MemtoReg = 0;
funcALU( ALUa, ALUb );
}

if( state == 17 ) // jump


{
PCWrite = 1;
PCSource = 2;
jaddr = instruction.substr( 4, 12 );
intJump = binaryDecimal( jaddr );

cout << "jaddr: " << jaddr << "; intJump: " << intJump << endl;
programCounter = intJump;
}
}

void funcALU( int ALUa, int ALUb )


{
int muxA = 0;
int muxB = 0;
int temp1 = 0;
int temp2 = 0;
if( ALUSrcA == 0 )
muxA = PCSource;
if( ALUSrcA == 1 )
muxA = regArray[ ALUa ];
if( ALUSrcB == 0 )
muxB = regArray[ ALUb ];
if( ALUSrcB == 1 )
muxB = 2;
if( ALUSrcB == 2 )
{
muxB = binaryDecimal( imm );
cout << "ALUSrcB binaryDecimal: " << imm << endl;
}
if( ALUSrcB == 3 )
muxB = ( binaryDecimal( imm ) * 2 );
if( ALUOp == 0 ) // add, addi
ALUResult = muxA + muxB;

if( ALUOp == 1 ) // sub


ALUResult = muxA - muxB;

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 == 3 || ALUOp == 6 ) // and


ALUResult = muxA & muxB;

if( ALUOp == 4 || ALUOp == 7 ) // or, ori


ALUResult = muxA | muxB;

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;
}
}

if( ALUOp == 8 ) // sll


ALUResult = muxA << muxB;

if( ALUOp == 9 ) // srl


ALUResult = muxA >> muxB;

if( ALUOp == 12 ) // beq


{

if( muxA == muxB )


programCounter = programCounter + binaryDecimal( imm );
}

if( ALUOp == 13 ) // bne


{
if( muxA != muxB )
programCounter = programCounter + binaryDecimal( imm );
}

if( ALUOp == 14 ) // slt


{
int rdi = binaryDecimal( rd );

if( ( muxA - muxB ) < 0 )


{
regArray[ rdi ] = 1;

}
else
{
regArray[ rdi ] = 0;

}
}
while( ALUOp == 15 ) // mac
{
temp1 = muxA * muxB;
temp2 = temp2 + temp1;
ALUResult = temp2;
}
}

void MemAccess( int ALU_B )


{
char temp[ 16 ];
int z;

//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;
}

else if( ALUread == 1 )


{
ALUread = 0;
z = binaryDecimal( instruction.substr( 4, 4 ) );
c2 = regArray[ z ];
c2 = c2 * 256;
c3 = c1 + c2;
cout << "c1: " << c1 << "; c2: " << c2 << "; c3: " << c3 << endl;

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( ) );

itoa( WriteData, temp, 10 );

cout << "WriteData: " << WriteData;


cout << "; WriteReg: " << WriteReg << "; RegDst: " << RegDst << "; Temp: " << temp << endl;
regArray[ WriteReg ] = WriteData;
cout << "RegArray[" << WriteReg << "]: " << regArray[ WriteReg ] << endl;
}

Potrebbero piacerti anche