forked from joaopmarinho/Risc-v-Pipeline
-
Notifications
You must be signed in to change notification settings - Fork 1
/
datamemory.sv
77 lines (71 loc) · 1.79 KB
/
datamemory.sv
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
`timescale 1ns / 1ps
module datamemory #(
parameter DM_ADDRESS = 9,
parameter DATA_W = 32
) (
input logic clk,
input logic MemRead, // comes from control unit
input logic MemWrite, // Comes from control unit
input logic [DM_ADDRESS - 1:0] a, // Read / Write address - 9 LSB bits of the ALU output
input logic [DATA_W - 1:0] wd, // Write Data
input logic [2:0] Funct3, // bits 12 to 14 of the instruction
output logic [DATA_W - 1:0] rd // Read Data
);
logic [31:0] raddress;
logic [31:0] waddress;
logic [31:0] Datain;
logic [31:0] Dataout;
logic [ 3:0] Wr;
Memoria32Data mem32 (
.raddress(raddress),
.waddress(waddress),
.Clk(~clk),
.Datain(Datain),
.Dataout(Dataout),
.Wr(Wr)
);
always_ff @(*) begin
raddress = {{22{1'b0}}, a};
waddress = {{22{1'b0}}, {a[8:2], {2{1'b0}}}};
Datain = wd;
Wr = 4'b0000;
if (MemRead) begin
case (Funct3)
3'b000: begin //LB
rd <= {Dataout[7]? {24{1'b1}} : {24{1'b0}}, Dataout[7:0]};
end
3'b010: begin //LW
rd <= Dataout;
end
3'b001: begin //LH
rd <= {Dataout[15]? {16{1'b1}} : {16{1'b0}}, Dataout[15:0]};
end
3'b100: begin //LBU
rd <= Dataout[7:0];
end
default: begin
rd <= Dataout;
end
endcase
end else if (MemWrite) begin
case (Funct3)
3'b000: begin //SB
Wr <= 4'b0001;
Datain <= wd;
end
3'b001: begin //SH
Wr <= 4'b0011;
Datain <= wd;
end
3'b010: begin //SW
Wr <= 4'b1111;
Datain <= wd;
end
default: begin
Wr <= 4'b1111;
Datain <= wd;
end
endcase
end
end
endmodule