-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrisc_v.v
382 lines (343 loc) · 9.4 KB
/
risc_v.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
module RISC_V_Processor(clk, rst);
input clk, rst;
wire [31:0]PCNext, PC, instr, PCPlus4, PCTarget, SrcA, RD2, ImmExt, SrcB, ALUResult, ReadData, Result;
wire PCSrc, ResultSrc, MemWrite, ALUSrc, RegWrite, Zero;
wire [2:0]ALUControl;
wire [1:0]ImmSrc;
Mux M1(.I1(PCPlus4), .I2(PCTarget), .s(PCSrc), .Out(PCNext));
Program_Counter P_C(.clk(clk), .PC_In(PCNext), .rst(rst), .PC_Out(PC));
Adder Add1(.a(PC), .b(32'd4), .out(PCPlus4));
instruction_memory IM(.inst_addr(PC), .instr(instr));
// Instr_Fetch IF(.clk(clk), .rst(rst), .instr());
registerFile RF(.WriteData(Result), .rs1(instr[19:15]), .rs2(instr[24:20]), .rd(instr[11:7]), .RegWrite(RegWrite), .clk(clk), .ReadData1(SrcA), .ReadData2(RD2));
Mux M2(.I1(RD2), .I2(ImmExt), .s(ALUSrc), .Out(SrcB));
ALU ALU1(.Result(ALUResult), .a(SrcA), .b(SrcB), .op(ALUControl), .zero(Zero));
Data_Memory DM(.clk(clk), .Mem_Addr(ALUResult), .Write_Data(RD2), .Mem_Write(MemWrite), .Read_Data(ReadData));
Mux M3(.I1(ALUResult), .I2(ReadData), .s(ResultSrc), .Out(Result));
imm_ext IDE(.instruction(instr), .immSrc(ImmSrc), .imm_data(ImmExt));
Adder Add2(.a(PC), .b(ImmExt), .out(PCTarget));
Control_Unit CU(.PCSrc(PCSrc), .Zero(Zero), .ResultSrc(ResultSrc), .MemWrite(MemWrite), .ALUSrc(ALUSrc), .ImmSrc(ImmSrc), .ALUControl(ALUControl), .RegWrite(RegWrite), .Opcode(instr[6:0]), .Funct3(instr[14:12]), .Funct7(instr[30]));
endmodule
// Multiplexer
module Mux(I1, I2, s, Out);
input [31:0]I1, I2;
input s;
output reg [31:0]Out;
always @(*)
begin
if (s == 0)
Out = I1;
else
Out = I2;
end
endmodule
// Instrution Fetch
/*module Instr_Fetch(clk, rst, instr);
input clk, rst;
output [31:0]instr;
wire [31:0]PC_in;
wire [31:0]PC_out;
Program_Counter P_C(.clk(clk), .PC_In(PC_in), .rst(rst), .PC_Out(PC_out));
Adder Add(.a(PC_out), .b(32'd4), .out(PC_in));
instruction_memory IM(.inst_addr(PC_out), .instr(instr));
endmodule */
// Program Counter
module Program_Counter(clk, PC_In, rst, PC_Out);
input clk, rst;
input [31:0]PC_In;
output reg [31:0]PC_Out;
always @(posedge clk)
begin
if (rst)
PC_Out <= 0;
else
PC_Out <= PC_In;
end
endmodule
// Adder
module Adder(a, b, out);
input [31:0]a, b;
output reg [31:0]out;
always @(*)
begin
out = a + b;
end
endmodule
// Instruction Memory
module instruction_memory(inst_addr,instr);
input [31:0] inst_addr;
output [31:0] instr;
reg [7:0] imem [15:0];
initial
begin
imem[0]= 8'b00000011; //03
imem[1]= 8'b10100011; //a3
imem[2]= 8'b11000100; //c4
imem[3]= 8'b11111111; //ff
imem[4]= 8'b00100011; //23
imem[5]= 8'b10100100; //a4
imem[6]= 8'b01100100; //64
imem[7]= 8'b00000000; //00
imem[8]= 8'b00110011; //33
imem[9]= 8'b11100010; //e2
imem[10]= 8'b00110010; //32
imem[11]= 8'b00000000; //00
imem[12]= 8'b11100011; //e3
imem[13]= 8'b00001010; //0a
imem[14]= 8'b01000010; //42
imem[15]= 8'b11111110; //fe
end
as#str= {imem[inst_addr+3],imem[inst_addr+2],
imem[inst_addr+1],imem[inst_addr]};
endmodule
// Register File
module registerFile(WriteData, rs1, rs2, rd, RegWrite, clk, ReadData1, ReadData2);
input [31:0] WriteData;
input [4:0] rs1, rs2, rd;
input RegWrite, clk;
output [31:0] ReadData1, ReadData2;
reg [31:0] Registers [31:0];
integer i;
assign ReadData1 = Registers[rs1];
assign ReadData2 = Registers[rs2];
always @(posedge clk)
begin
if (RegWrite)
Registers [rd] = WriteData;
end
initial
begin
for(i=0; i<32; i = i+1)
Registers[i] = i;
end
endmodule
// ALU
module ALU(Result, a, b, op, zero);
input [31:0]a, b;
input [2:0]op;
output reg [31:0]Result;
output reg zero;
always @(*)
begin
if (op == 3'b000)
Result = a + b;
else if (op == 3'b001)
Result = a - b;
else if (op == 3'b101)
Result = a < b;
else if (op == 3'b011)
Result = a | b;
else if (op == 3'b010)
Result = a & b;
end
always @(*)
begin
if (Result == 32'd0)
zero = 1;
else
zero = 0;
end
endmodule
// Data Memory
module Data_Memory(clk, Mem_Addr, Write_Data, Mem_Write, Read_Data);
input clk;
input [31:0]Mem_Addr;
input [31:0]Write_Data;
input Mem_Write;
output [31:0] Read_Data;
reg [7:0] Memory [31:0]; //[15:0]
integer i;
assign Read_Data = {Memory[Mem_Addr + 3], Memory[Mem_Addr + 2], Memory[Mem_Addr + 1], Memory[Mem_Addr + 0]};
//The below block will be changes to the values given in the manual.
initial
begin
for (i=0; i<32; i = i + 1)
Memory [i] = i;
end
always @ (posedge clk)
begin
if(Mem_Write)
Memory[Mem_Addr] = Write_Data[7:0];
Memory[Mem_Addr + 1] = Write_Data[15:8];
Memory[Mem_Addr + 2] = Write_Data[23:16];
Memory[Mem_Addr + 3] = Write_Data[31:24];
end
endmodule
//Immediate Extender
module imm_ext(instruction, immSrc, imm_data);
input [31:0]instruction;
input [1:0]immSrc;
output reg [31:0]imm_data;
always @(*)
begin
case(immSrc)
2'b00: imm_data = {{20{instruction[31]}}, instruction[31:20]};
2'b01: imm_data = {{20{instruction[31]}}, instruction[31:25], instruction[11:7]};
2'b10: imm_data = {{21{instruction[31]}}, instruction[7], instruction[30:25], instruction[11:8],1'b0};
default: imm_data = 32'b0;
endcase
end
endmodule
module Control_Unit(PCSrc, Zero, ResultSrc, MemWrite, ALUSrc, ImmSrc, ALUControl, RegWrite, Opcode, Funct3, Funct7);
input [2:0]Funct3;
input Funct7, Zero;
input [6:0]Opcode;
output ResultSrc, MemWrite, ALUSrc, RegWrite, PCSrc;
output [1:0]ImmSrc;
output [2:0]ALUControl;
wire Branch;
wire [1:0]ALUOp;
main_decoder CU1(.branch(Branch), .opcode(Opcode), .imm_src(ImmSrc), .result_src(ResultSrc), .alu_op(ALUOp), .mem_write(MemWrite), .alu_src(ALUSrc), .reg_write(RegWrite));
alu_decoder CU2(.alu_op(ALUOp), .func7(Funct7), .func3(Funct3), .alu_control(ALUControl));
PC_Src CU3(.branch(Branch), .zero(Zero), .pc_src(PCSrc));
endmodule
// Main Decoder
module main_decoder (
input [6:0]opcode,
input zero,
output reg branch,
output reg result_src,
output reg mem_write,
output reg alu_src,
output reg [1:0] imm_src,
output reg reg_write,
output reg [1:0] alu_op,
output pc_src
);
//reg clk;
always@(*) begin
case(opcode)
7'b0000011: begin //lw
reg_write <= 1;
imm_src <= 0;
alu_src <= 1;
mem_write <= 0;
result_src <= 1;
branch <= 0;
alu_op <= 0;
end
7'b0100011: begin //sw
reg_write <= 0;
imm_src <= 2'b01;
alu_src <= 1;
mem_write <= 1;
result_src <= 'x;
branch <= 0;
alu_op <= 0;
end
7'b0110011: begin //r_type
reg_write <= 1;
imm_src <= 'x;
alu_src <= 0;
mem_write <= 0;
result_src <= 0;
branch <= 0;
alu_op <= 2'b10;
end
7'b1100011: begin //beq
reg_write <= 0;
imm_src <= 2'b10;
alu_src <= 0;
mem_write <= 0;
result_src <= 'x;
branch <= 1;
alu_op <= 2'b01;
end
default: begin
reg_write <= 'x;
imm_src <= 'x;
alu_src <= 'x;
mem_write <= 'x;
result_src <= 'x;
branch <= 'x;
alu_op <= 'x;
end
endcase
end
endmodule
// ALU Decoder
module alu_decoder(
input [2:0] func3,
input func7,
input [1:0] alu_op,
output reg [2:0] alu_control
);
reg [6:0] conc;
initial begin
assign conc = {alu_op, func3, func7};
end
always @(*) begin
case(conc)
6'b000100: begin
alu_control <= 3'b000; //add
end
6'b000101: begin
alu_control <= 3'b000; //add
end
6'b010100: begin
alu_control <= 3'b001; //sub
end
6'b010101: begin
alu_control <= 3'b001; //sub
end
6'b100000: begin
alu_control <= 3'b000; //add
end
6'b100001: begin
alu_control <= 3'b001; //sub
end
6'b100100: begin
alu_control <= 3'b101; //set less than
end
6'b100101: begin
alu_control <= 3'b101; //set less than
end
6'b101100: begin
alu_control <= 3'b011; //or
end
6'b101101: begin
alu_control <= 3'b011; //or
end
6'b101110: begin
alu_control <= 3'b010; //and
end
6'b101111: begin
alu_control <= 3'b010; //and
end
endcase
end
endmodule
// PC Source
module PC_Src(pc_src, zero, branch);
input zero, branch;
output pc_src;
assign pc_src = zero & branch;
endmodule
// Test benches
//module Mux_tb();
// reg [31:0]I1, I2;
// reg s;
// wire [31:0]Out;
// Mux tb(I1, I2, s, Out);
// initial
// begin
// I1 = 320;
// I2 = 1;
// s = 0;
// #10 s = 1;
// end
//endmodule
module risc_v();
reg clk, rst;
RISC_V_Processor uut(clk, rst);
initial
clk = 1;
always
#10 clk = ~clk;
initial
begin
rst = 1;
#10 rst = 0;
// $display("instruction: %h, PC: : %h", instruction, PC1);
end
endmodule