Skip to content

Commit d129fd0

Browse files
project created and all the essential files added to it
0 parents  commit d129fd0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+952
-0
lines changed

Report/Report_persian_language.pdf

6.44 MB
Binary file not shown.

adder.sv

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module adder (input [31:0] a, b, output [31:0] y);
2+
3+
assign y = a+b;
4+
5+
endmodule

alu.sv

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module alu (input logic [31:0] src1, src2, input logic [3:0] aluc, output logic [31:0] out, output logic zero,
2+
output logic cout, overflow, sign);
3+
4+
always_comb begin
5+
case (aluc)
6+
4'b0000: begin
7+
{cout, out} = {1'b0, src1} + {1'b0, src2};
8+
end
9+
4'b0001: begin
10+
{cout, out} = {1'b0, src1} + {1'b0, ~src2} + 9'b1;
11+
end
12+
4'b0010: begin
13+
out = src1 & src2;
14+
cout = 0;
15+
end
16+
4'b0011: begin
17+
out = src1 | src2;
18+
cout = 0;
19+
end
20+
4'b0100: begin // sra
21+
out = $signed(src1) >>> $unsigned(src2[4:0]);
22+
cout = 0;
23+
end
24+
4'b0101: begin
25+
if ($signed(src1) < $signed(src2))
26+
out = 1;
27+
else
28+
out = 0;
29+
cout = 0;
30+
end
31+
4'b0110: begin // srl
32+
out = src1 >> src2[4:0];
33+
cout = 0;
34+
end
35+
4'b0111: begin // sll
36+
out = src1 << src2[4:0];
37+
cout = 0;
38+
end
39+
4'b1000: begin // sltu
40+
if($unsigned(src1) < $unsigned(src2))
41+
out = 1;
42+
else
43+
out = 0;
44+
cout = 0;
45+
end
46+
4'b1001: begin // xor
47+
out = src1 ^ src2;
48+
cout = 0;
49+
end
50+
default: begin
51+
out = 0;
52+
cout = 0;
53+
end
54+
endcase
55+
if (out==0)
56+
zero = 1;
57+
else
58+
zero = 0;
59+
sign = out[31];
60+
overflow = (~((src1[31] ^ src2[31]) ^ aluc[0])) & (src1[31] ^ sign) & ~aluc[1];
61+
end
62+
63+
endmodule

aluDec.sv

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module aluDec (input logic [1:0] aluop,
2+
input logic op5, funct7b5,
3+
input logic [2:0] funct3,
4+
output logic [3:0] alucontrol);
5+
6+
always_comb begin
7+
8+
case(aluop)
9+
2'b00: alucontrol = 4'b0000;
10+
2'b01: alucontrol = 4'b0001;
11+
2'b10: case (funct3)
12+
3'b000: if({op5, funct7b5} == 2'b11)
13+
alucontrol = 4'b0001;
14+
else
15+
alucontrol = 4'b0000;
16+
3'b001: // sll
17+
alucontrol = 4'b0111;
18+
3'b010: alucontrol = 4'b0101;
19+
3'b011:// sltu
20+
alucontrol = 4'b1000;
21+
3'b100:// xor
22+
alucontrol = 4'b1001;
23+
3'b101: if(funct7b5) // sra
24+
alucontrol = 4'b0100;
25+
else // srl
26+
alucontrol = 4'b0110;
27+
3'b110: alucontrol = 4'b0011;
28+
3'b111: alucontrol = 4'b0010;
29+
default: alucontrol = 4'bx;
30+
endcase
31+
default: alucontrol = 4'bx;
32+
endcase
33+
end
34+
35+
endmodule

auipcTb.sv

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module auipcTb();
2+
3+
logic clk;
4+
logic reset;
5+
logic [31:0] WriteData, DataAdr;
6+
logic MemWrite;
7+
8+
// instantiate device to be tested
9+
top dut(clk, reset, WriteData, DataAdr, MemWrite);
10+
11+
// initialize test
12+
initial
13+
begin
14+
reset <= 1; # 22; reset <= 0;
15+
end
16+
17+
// generate clock to sequence tests
18+
always
19+
begin
20+
clk <= 1; # 5; clk <= 0; # 5;
21+
end
22+
always @(negedge clk) begin
23+
if(MemWrite)
24+
begin
25+
if(DataAdr == 100 & WriteData == 4096)
26+
begin
27+
28+
$display("Simulation succeeded");
29+
$stop;
30+
end
31+
else
32+
begin
33+
$display("Simulation failed");
34+
$stop;
35+
end
36+
end
37+
end
38+
endmodule

auipctest.s

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#0 auipc x2, 1 //x2 = 00000000000000000001000000000000 = 0x1000 = 4096 | 00000000000000000001-00010-0010111 | 0x00001117
2+
#4 sw x2, 100(x0) //mem[100] = 0x1000 = 4096 | 0000011-00010-00000-010-00100-0100011 | 0x06202223

auipctest.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
00001117
2+
06202223

branchDec.sv

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module branchDec(input logic [6:0] op,
2+
input logic [2:0] funct3,
3+
input logic branch,
4+
output logic beq, bne, blt, bge, bltu, bgeu);
5+
6+
assign beq = (op == 7'b1100011) & (funct3 == 3'b000) & branch ;
7+
assign bne = (op == 7'b1100011) & (funct3 == 3'b001) & branch ;
8+
assign blt = (op == 7'b1100011) & (funct3 == 3'b100) & branch ;
9+
assign bge = (op == 7'b1100011) & (funct3 == 3'b101) & branch ;
10+
assign bltu = (op == 7'b1100011) & (funct3 == 3'b110) & branch ;
11+
assign bgeu = (op == 7'b1100011) & (funct3 == 3'b111) & branch ;
12+
13+
endmodule

controller.sv

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module controller(input logic clk,
2+
input logic reset,
3+
input logic [6:0] op,
4+
input logic [2:0] funct3,
5+
input logic funct7b5,
6+
input logic zero, cout, overflow, sign,
7+
output logic [2:0] immsrc,
8+
output logic [1:0] alusrca, alusrcb,
9+
output logic [1:0] resultsrc,
10+
output logic adrsrc,
11+
output logic [3:0] alucontrol,
12+
output logic irwrite, pcwrite,
13+
output logic regwrite, memwrite);
14+
15+
logic beq, bne, blt, bge, bltu, bgeu, branch, pcupdate;
16+
logic [1:0] aluop;
17+
18+
fsm MainFSM(clk, reset, op, branch, pcupdate, regwrite,
19+
memwrite, irwrite, resultsrc, alusrcb, alusrca, adrsrc, aluop);
20+
aluDec AluDecoder(aluop, op[5], funct7b5, funct3, alucontrol);
21+
instrDec InstrDecoder(op, immsrc);
22+
branchDec BranchDecoder(op, funct3, branch, beq, bne, blt, bge, bltu, bgeu);
23+
24+
assign pcwrite = (beq & zero) | (bne & ~zero) | (bgeu & cout) | (bltu & ~cout)
25+
| (bge & (sign == overflow)) | (blt & (sign != overflow)) | pcupdate;
26+
27+
28+
29+
endmodule

controller.tv

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// controller.tv
2+
3+
// Place in same computer directory as controller.sv to test your multicycle controller.
4+
// Checks all supported instructions (add,sub,and,or,slt,lw,addi,sw,beq,jal).
5+
6+
7+
// ========== Test Vector Format ==========
8+
// Op[6:0] Funct3[2:0] Funct7b5 Zero _ ImmSrc[1:0] ALUSrcA[1:0] ALUSrcB[1:0] ResultSrc[1:0] AdrSrc ALUControl[2:0] IRWrite PCWrite RegWrite MemWrite
9+
10+
11+
// ========== Test R-type instructions (add,sub,or,and,slt) ==========
12+
// Each instruction takes four cycles to execute
13+
// Op[6:0] Funct3[2:0] Funct7b5 Zero _ ImmSrc[1:0] ALUSrcA[1:0] ALUSrcB[1:0] ResultSrc[1:0] AdrSrc ALUControl[2:0] IRWrite PCWrite RegWrite MemWrite
14+
//
15+
// add
16+
0110011_000_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 000)
17+
0110011_000_0_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 001)
18+
0110011_000_0_0__XX_10_00_XX_X_000_0_0_0_0 // ExecuteR (Vect 002)
19+
0110011_000_0_0__XX_XX_XX_00_X_XXX_0_0_1_0 // ALUWB (Vect 003)
20+
//
21+
// sub
22+
0110011_000_1_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 004)
23+
0110011_000_1_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 005)
24+
0110011_000_1_0__XX_10_00_XX_X_001_0_0_0_0 // ExecuteR (Vect 006)
25+
0110011_000_1_0__XX_XX_XX_00_X_XXX_0_0_1_0 // ALUWB (Vect 007)
26+
//
27+
// or
28+
0110011_110_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 008)
29+
0110011_110_0_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 009)
30+
0110011_110_0_0__XX_10_00_XX_X_011_0_0_0_0 // ExecuteR (Vect 010)
31+
0110011_110_0_0__XX_XX_XX_00_X_XXX_0_0_1_0 // ALUWB (Vect 011)
32+
//
33+
// and
34+
0110011_111_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 012)
35+
0110011_111_0_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 013)
36+
0110011_111_0_0__XX_10_00_XX_X_010_0_0_0_0 // ExecuteR (Vect 014)
37+
0110011_111_0_0__XX_XX_XX_00_X_XXX_0_0_1_0 // ALUWB (Vect 015)
38+
//
39+
// slt
40+
0110011_010_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 016)
41+
0110011_010_0_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 017)
42+
0110011_010_0_0__XX_10_00_XX_X_101_0_0_0_0 // ExecuteR (Vect 018)
43+
0110011_010_0_0__XX_XX_XX_00_X_XXX_0_0_1_0 // ALUWB (Vect 019)
44+
45+
46+
// ========== Test I-type instructions (lw, addi) ==========
47+
// lw takes five cycles to execute, addi takes four cycles
48+
// Op[6:0] Funct3[2:0] Funct7b5 Zero _ ImmSrc[1:0] ALUSrcA[1:0] ALUSrcB[1:0] ResultSrc[1:0] AdrSrc ALUControl[2:0] IRWrite PCWrite RegWrite MemWrite
49+
//
50+
// lw
51+
0000011_010_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 020)
52+
0000011_010_0_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 021)
53+
0000011_010_0_0__00_10_01_XX_X_000_0_0_0_0 // MemAdr (Vect 022)
54+
0000011_010_0_0__XX_XX_XX_00_1_XXX_0_0_0_0 // MemRead (Vect 023)
55+
0000011_010_0_0__XX_XX_XX_01_X_XXX_0_0_1_0 // MemWB (Vect 024)
56+
//
57+
// addi
58+
0010011_000_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 025)
59+
0010011_000_0_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 026)
60+
0010011_000_0_0__00_10_01_XX_X_000_0_0_0_0 // ExecuteI (Vect 027)
61+
0010011_000_0_0__XX_XX_XX_00_X_XXX_0_0_1_0 // ALUWB (Vect 028)
62+
//
63+
// addi (funct7b5 is now just data and shouldn't affect execution)
64+
0010011_000_1_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 029)
65+
0010011_000_1_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 030)
66+
0010011_000_1_0__00_10_01_XX_X_000_0_0_0_0 // ExecuteI (Vect 031)
67+
0010011_000_1_0__XX_XX_XX_00_X_XXX_0_0_1_0 // ALUWB (Vect 032)
68+
69+
70+
// ========== Test S-type instructions (sw) ==========
71+
// sw takes four cycles to execute
72+
// Op[6:0] Funct3[2:0] Funct7b5 Zero _ ImmSrc[1:0] ALUSrcA[1:0] ALUSrcB[1:0] ResultSrc[1:0] AdrSrc ALUControl[2:0] IRWrite PCWrite RegWrite MemWrite
73+
//
74+
// sw
75+
0100011_010_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 033)
76+
0100011_010_0_0__XX_XX_XX_XX_X_XXX_0_0_0_0 // Decode (Vect 034)
77+
0100011_010_0_0__01_10_01_XX_X_000_0_0_0_0 // MemAdr (Vect 035)
78+
0100011_010_0_0__XX_XX_XX_00_1_XXX_0_0_0_1 // MemWrite (Vect 036)
79+
80+
81+
// ========== Test B-type instructions (beq) ==========
82+
// beq takes three cycles to execute
83+
// Op[6:0] Funct3[2:0] Funct7b5 Zero _ ImmSrc[1:0] ALUSrcA[1:0] ALUSrcB[1:0] ResultSrc[1:0] AdrSrc ALUControl[2:0] IRWrite PCWrite RegWrite MemWrite
84+
//
85+
// beq (branch not taken because ALU returns Zero=0 indicating inequality)
86+
1100011_000_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 037)
87+
1100011_000_0_0__10_01_01_XX_X_000_0_0_0_0 // Decode (Vect 038)
88+
1100011_000_0_0__XX_10_00_00_X_001_0_0_0_0 // BEQ (Vect 039)
89+
//
90+
// beq (branch taken because ALU returns Zero=1 indicating equality)
91+
1100011_000_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 040)
92+
1100011_000_0_0__10_01_01_XX_X_000_0_0_0_0 // Decode (Vect 041)
93+
1100011_000_0_1__XX_10_00_00_X_001_0_1_0_0 // BEQ (Vect 042)
94+
95+
96+
// ========== Test J-type instructions (jal) ==========
97+
// jal takes four cycles to execute
98+
// Op[6:0] Funct3[2:0] Funct7b5 Zero _ ImmSrc[1:0] ALUSrcA[1:0] ALUSrcB[1:0] ResultSrc[1:0] AdrSrc ALUControl[2:0] IRWrite PCWrite RegWrite MemWrite
99+
//
100+
// jal
101+
1101111_000_0_0__XX_00_10_10_0_000_1_1_0_0 // Fetch (Vect 043)
102+
1101111_000_0_0__11_01_01_XX_X_000_0_0_0_0 // Decode (Vect 044)
103+
1101111_000_0_0__XX_01_10_00_X_000_0_1_0_0 // JAL (Vect 045)
104+
1101111_000_0_0__XX_XX_XX_00_X_XXX_0_0_1_0 // ALUWB (Vect 046)

dataPath.sv

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module dataPath (input logic clk, reset,
2+
input logic [2:0] ImmSrc,
3+
input logic [3:0] ALUControl,
4+
input logic [1:0] ResultSrc,
5+
input logic IRWrite,
6+
input logic RegWrite,
7+
input logic [1:0] ALUSrcA, ALUSrcB,
8+
input logic AdrSrc,
9+
input logic PCWrite,
10+
input logic [31:0] ReadData,
11+
output logic Zero, cout, overflow, sign,
12+
output logic [31:0] Adr,
13+
output logic [31:0] WriteData,
14+
output logic [31:0] instr);
15+
16+
17+
logic [31:0] Result , ALUOut, ALUResult;
18+
logic [31:0] RD1, RD2, A , SrcA, SrcB, Data;
19+
logic [31:0] ImmExt;
20+
logic [31:0] PC, OldPC;
21+
22+
23+
//pc
24+
flopenr #(32) pcFlop(clk, reset, PCWrite, Result, PC);
25+
26+
27+
//regFile
28+
regFile rf(clk, RegWrite, instr[19:15], instr[24:20], instr[11:7], Result, RD1, RD2);
29+
extend ext(instr[31:7], ImmSrc, ImmExt);
30+
flopr #(32) regF( clk, reset, RD1, A);
31+
flopr #(32) regF_2( clk, reset, RD2, WriteData);
32+
33+
34+
//alu
35+
mux3 #(32) srcAmux(PC, OldPC, A, ALUSrcA, SrcA);
36+
mux3 #(32) srcBmux(WriteData, ImmExt, 32'd4, ALUSrcB, SrcB);
37+
alu alu(SrcA, SrcB, ALUControl, ALUResult, Zero, cout, overflow, sign);
38+
flopr #(32) aluReg (clk, reset, ALUResult, ALUOut);
39+
mux4 #(32) resultMux(ALUOut, Data, ALUResult, ImmExt, ResultSrc, Result );
40+
41+
//mem
42+
mux2 #(32) adrMux(PC, Result, AdrSrc, Adr);
43+
flopenr #(32) memFlop1(clk, reset, IRWrite, PC, OldPC);
44+
flopenr #(32) memFlop2(clk, reset, IRWrite, ReadData, instr);
45+
flopr #(32) memDataFlop(clk, reset, ReadData, Data);
46+
47+
endmodule

experimenting.sv

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module experiment(input logic [2:0] a, b);
2+
3+
always_comb begin
4+
5+
if ($signed(a) < $signed(b))
6+
$display("use $signed for signed values , signed a < signed b");
7+
if ($signed(a) > $signed(b))
8+
$display("use $signed for signed values , signed a > signed b");
9+
if($unsigned(a) > $unsigned(b))
10+
$display("use $unsigned for unsigned values, unsigned a > unsigned b");
11+
if($unsigned(a) < $unsigned(b))
12+
$display("use $unsigned for unsigned values, unsigned a < unsigned b");
13+
if(a > b)
14+
$display("a>b");
15+
else
16+
$display("a<b");
17+
$display("go fuck yourself");
18+
19+
end
20+
21+
22+
endmodule

extend.sv

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module extend (input logic [31:7] instr, input logic [2:0] immsrc, output logic [31:0] immext);
2+
3+
always_comb
4+
case(immsrc) //controller produces immsrc signal
5+
//I
6+
3'b000: immext = {{20{instr[31]}}, instr[31:20]};
7+
//S
8+
3'b001: immext = {{20{instr[31]}}, instr[31:25], instr[11:7]};
9+
//B
10+
3'b010: immext = {{20{instr[31]}}, instr[7], instr[30:25], instr[11:8], 1'b0};
11+
//J
12+
3'b011: immext = {{12{instr[31]}}, instr[19:12], instr[20], instr[30:21], 1'b0};
13+
//U
14+
3'b100: immext = {instr[31:12], 12'b0};
15+
default: immext = 32'bx; // undefined ? ?
16+
endcase
17+
endmodule

0 commit comments

Comments
 (0)