Skip to content
This repository was archived by the owner on Jun 29, 2023. It is now read-only.

Commit 8e355b6

Browse files
committed
First Commit
0 parents  commit 8e355b6

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

README.md

26 Bytes
Binary file not shown.

memfile.dat

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
20040005
2+
20050009
3+
00A43020
4+
00A43822
5+
00A44024
6+
00A44825
7+
0085502A
8+
AC040000
9+
8C0B0000
10+
0C00000D
11+
00850019
12+
00006810
13+
00007012
14+
200C0007
15+
03E00008

mips.v

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module mips
2+
(input clk, rst,
3+
input [31:0] instr, rd_dm,
4+
output we_dm,
5+
output [31:0] pc_current, alu_out, wd_dm);
6+
wire pc_src, jump, link, jump_reg ,reg_dst, we_reg, alu_src, we_hi, we_lo, hi2reg, lo2reg, zero, dm2reg;
7+
wire [2:0] alu_ctrl;
8+
datapath DP (clk, rst, pc_src, jump, link, jump_reg ,reg_dst, we_reg, alu_src, we_hi, we_lo, hi2reg, lo2reg, dm2reg, alu_ctrl, instr[25:0], rd_dm, zero, pc_current, alu_out, wd_dm);
9+
controlunit CU (zero, instr[31:26], instr[5:0], pc_src, jump, link, jump_reg ,reg_dst, we_reg, alu_src, we_hi, we_lo, hi2reg, lo2reg, we_dm, dm2reg, alu_ctrl);
10+
endmodule
11+
12+
module datapath
13+
(input clk, rst, pc_src, jump, link, jump_reg, reg_dst, we_reg, alu_src, we_hi, we_lo, hi2reg, lo2reg, dm2reg,
14+
input [2:0] alu_ctrl,
15+
input [25:0] instr,
16+
input [31:0] rd_dm,
17+
output zero,
18+
output [31:0] pc_current, alu_out, wd_dm);
19+
wire [4:0] wa_rf, jal_rf;
20+
wire [31:0] pc_pre, pc_next, pc_plus4, jra, jta, sext_imm, ba, bta, alu_pa, alu_pb, hi_dat, lo_dat, hi_res, lo_res, wd_rf, wd_rf_res;
21+
wire [63:0] product;
22+
assign jta = {pc_plus4[31:28], instr[25:0], 2'b00};
23+
assign ba = {sext_imm[29:0], 2'b00};
24+
// Next PC Logic
25+
mux2 #(32) pc_jr_mux (jump_reg, pc_plus4, alu_pa, jra);
26+
mux2 #(32) pc_src_mux (pc_src, jra, bta, pc_pre);
27+
mux2 #(32) pc_jmp_mux (jump, pc_pre, jta, pc_next);
28+
dreg #(32) pc_reg (clk, rst, 1, pc_next, pc_current);
29+
adder #(32) pc_add4 (pc_current, 4, pc_plus4);
30+
adder #(32) pc_add_bra (pc_plus4, ba, bta);
31+
signext se (instr[15:0], sext_imm);
32+
// RF Logic
33+
mux2 #(5) rf_wa_mux (reg_dst, instr[20:16], instr[15:11], wa_rf);
34+
mux2 #(5) rf_jal_mux (link, wa_rf, 31, jal_rf);
35+
mux2 #(32) rf_wd_mux (link, lo_res, pc_plus4, wd_rf_res);
36+
regfile #(32) rf (clk, we_reg, jal_rf, instr[25:21], instr[20:16], wd_rf_res, alu_pa, wd_dm);
37+
// ALU Logic
38+
mux2 #(32) alu_pb_mux (alu_src, wd_dm, sext_imm, alu_pb);
39+
alu #(32) alu (alu_ctrl, alu_pa, alu_pb, zero, alu_out);
40+
multi #(32) multi (alu_pa, alu_pb, product[63:32], product[31:0]);
41+
dreg #(32) high (clk, rst, we_hi, product[63:32], hi_dat);
42+
dreg #(32) low (clk, rst, we_lo, product[31:0], lo_dat);
43+
mux2 #(32) wd_rf_mux (dm2reg, alu_out, rd_dm, wd_rf);
44+
mux2 #(32) hi_mux (hi2reg, wd_rf, hi_dat, hi_res);
45+
mux2 #(32) lo_mux (lo2reg, hi_res, lo_dat, lo_res);
46+
endmodule
47+
48+
module controlunit
49+
(input zero,
50+
input [5:0] op, funct,
51+
output pc_src, jump, link, jump_reg, reg_dst, we_reg, alu_src, we_hi, we_lo, hi2reg, lo2reg, we_dm, dm2reg,
52+
output [2:0] alu_ctrl);
53+
wire branch;
54+
wire [1:0] alu_op;
55+
assign pc_src = branch & zero;
56+
maindec MD (op, branch, jump, link, reg_dst, we_reg, alu_src, we_dm, dm2reg, alu_op);
57+
auxdec AD (alu_op, funct, jump_reg ,we_hi, we_lo, hi2reg, lo2reg, alu_ctrl);
58+
endmodule

mips_top.v

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module mips_top
2+
(input clk, rst, output [31:0] pc_current, instr, alu_out, wd_dm, rd_dm);
3+
wire we_dm;
4+
mips MIPS (clk, rst, instr, rd_dm, we_dm, pc_current, alu_out, wd_dm);
5+
imem #(32)IMEM (pc_current[7:2], instr);
6+
dmem #(32)DMEM (clk, we_dm, alu_out[5:0], wd_dm, rd_dm);
7+
endmodule

parts.v

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
module mux2 #(parameter wide = 8)
2+
(input sel, [wide-1:0] a, b, output [wide-1:0] y);
3+
assign y = sel ? b : a;
4+
endmodule
5+
6+
module adder #(parameter wide = 8)
7+
(input [wide-1:0] a, b, output [wide-1:0] y);
8+
assign y = a + b;
9+
endmodule
10+
11+
module multi #(parameter wide = 8)
12+
(input [wide-1:0] a, b, output [wide-1:0] h, l);
13+
assign {h, l} = a * b;
14+
endmodule
15+
16+
module signext
17+
(input [15:0] a, output [31:0] y);
18+
assign y = {{16{a[15]}}, a};
19+
endmodule
20+
21+
module alu #(parameter wide = 8)
22+
(input [2:0] op, [wide-1:0] a, b, output zero, reg [wide-1:0] y);
23+
assign zero = (y == 'h0);
24+
always @ (op, a, b)
25+
case (op)
26+
3'b000: y = a & b;
27+
3'b001: y = a | b;
28+
3'b010: y = a + b;
29+
3'b110: y = a - b;
30+
3'b111: y = (a < b) ? 1 : 0;
31+
default: y = 'hx;
32+
endcase
33+
endmodule
34+
35+
module dreg #(parameter wide = 8)
36+
(input clk, rst, en, [wide-1:0] d, output reg [wide-1:0] q);
37+
always @ (posedge clk, posedge rst)
38+
begin
39+
if (rst) q <= 0;
40+
else q <= en ? d : q;
41+
end
42+
endmodule
43+
44+
module regfile #(parameter wide = 8)
45+
(input clk, we, [4:0] wa, ra1, ra2, [wide-1:0] wd, output [wide-1:0] rd1, rd2);
46+
reg [wide-1:0] rf [0:31];
47+
always @ (posedge clk) if (we) rf[wa] <= wd;
48+
assign rd1 = (ra1) ? rf[ra1] : 0;
49+
assign rd2 = (ra2) ? rf[ra2] : 0;
50+
endmodule
51+
52+
module imem #(parameter wide = 8)
53+
(input [5:0] a, output [wide-1:0] y);
54+
reg [wide-1:0] rom [0:63];
55+
initial $readmemh ("memfile.dat", rom);
56+
assign y = rom[a];
57+
endmodule
58+
59+
module dmem #(parameter wide = 8)
60+
(input clk, we, [5:0] a, [wide-1:0] d, output [wide-1:0] q);
61+
reg [wide-1:0] ram [0:63];
62+
always @ (posedge clk) if (we) ram[a] <= d;
63+
assign q = ram[a];
64+
endmodule
65+
66+
module maindec
67+
(input [5:0] op, output reg branch, jump, link, reg_dst, we_reg, alu_src, we_dm, dm2reg, reg [1:0] alu_op);
68+
reg [9:0] ctrl;
69+
always @ (ctrl) {branch, jump, link, reg_dst, we_reg, alu_src, we_dm, dm2reg, alu_op} = ctrl;
70+
always @ (op)
71+
case (op)
72+
6'b000000: ctrl = 10'b0_0_0_1_1_0_0_0_10; // R-Type
73+
6'b100011: ctrl = 10'b0_0_0_0_1_1_0_1_00; // LW
74+
6'b101011: ctrl = 10'b0_0_0_0_0_1_1_0_00; // SW
75+
6'b000100: ctrl = 10'b1_0_0_0_0_0_0_0_01; // BEQ
76+
6'b001000: ctrl = 10'b0_0_0_0_1_1_0_0_00; // ADDI
77+
6'b000010: ctrl = 10'b0_1_0_0_0_0_0_0_00; // J
78+
6'b000011: ctrl = 10'b0_1_1_0_1_0_0_0_00; // JAL
79+
default: ctrl = 10'bx;
80+
endcase
81+
endmodule
82+
83+
module auxdec
84+
(input [1:0] alu_op, [5:0] funct, output reg jump_reg, we_hi, we_lo, hi2reg, lo2reg, reg [2:0] alu_ctrl);
85+
reg [7:0] ctrl;
86+
always @ (ctrl) {jump_reg, we_hi, we_lo, hi2reg, lo2reg, alu_ctrl} = ctrl;
87+
always @ (alu_op, funct)
88+
case (alu_op)
89+
2'b00: ctrl = 8'b0_00_00_010; // add
90+
2'b01: ctrl = 8'b0_00_00_110; // sub
91+
default: case (funct)
92+
6'b100000: ctrl = 8'b0_00_00_010; // ADD
93+
6'b100010: ctrl = 8'b0_00_00_110; // SUB
94+
6'b100100: ctrl = 8'b0_00_00_000; // AND
95+
6'b100101: ctrl = 8'b0_00_00_001; // OR
96+
6'b101010: ctrl = 8'b0_00_00_111; // SLT
97+
6'b011001: ctrl = 8'b0_11_00_000; // MULTU
98+
6'b010000: ctrl = 8'b0_00_10_000; // MFHI
99+
6'b010010: ctrl = 8'b0_00_01_000; // MFLO
100+
6'b001000: ctrl = 8'b1_00_00_000; // JR
101+
default: ctrl = 8'bx;
102+
endcase
103+
endcase
104+
endmodule

0 commit comments

Comments
 (0)