-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregfile.v.bak
61 lines (49 loc) · 1.51 KB
/
regfile.v.bak
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
`include "defines.v"
module regfile(
input wire clk,
input wire rst,
// writing
input wire we,
input wire[`RegAddrBus] waddr,
input wire[`RegBus] wdata,
// reading 1
input wire re2,
input wire[`RegAddrBus] raddr1,
output reg[`RegBus] rdata1,
// reading 2
input wire re2,
input wire[`RegAddrBus] raddr2,
output reg[`RegBus] rdata2
);
/*
stage 1. define a 32 length 32 bits
*/
reg[`RegBus] regs[0: `RegNum - 1];
/*
stage 2. WRITING operation
*/
always @(posedge clk) begin
// $0 can only be 0
if(rst == `RstDisable && we == `WriteEnable && waddr != 0)
regs[waddr] <= wdata;
end
/*
stage 3. READING operation 1
*/
always @(*) begin
if(rst == `RstEnable || re1 == `ReadDisable) rdata1 <= `ZeroWord;
else if(raddr1 == 0) rdata1 <= `ZeroWord;
// if writing / reading address are the same, connect it
else if(raddr1 == waddr && we == `WriteEnable) rdata1 <= wdata;
else rdata1 <= regs[raddr1];
end
/*
stage 4. READING operation 2
*/
always @(*) begin
if(rst == `RstEnable || re2 == `ReadDisable) rdata2 <= `ZeroWord;
else if(raddr2 == 0) rdata2 <= `ZeroWord;
else if(raddr2 == waddr && we == `WriteEnable) rdata2 <= wdata;
else rdata2 <= regs[raddr2];
end
endmodule