-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_memory_interface.sv
58 lines (48 loc) · 1.66 KB
/
data_memory_interface.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
`include "config.sv"
`include "constants.sv"
module data_memory_interface (
input clock,
input read_enable,
input write_enable,
input [2:0] data_format,
input [31:0] address,
input [31:0] write_data,
output [31:0] read_data,
output [31:0] bus_address,
input [31:0] bus_read_data,
output [31:0] bus_write_data,
output logic [3:0] bus_byte_enable,
output bus_read_enable,
output bus_write_enable
);
logic [31:0] position_fix;
logic [31:0] sign_fix;
assign bus_address = address;
assign bus_write_enable = write_enable;
assign bus_read_enable = read_enable;
assign bus_write_data = write_data << (8*address[1:0]);
// calculate byte enable
always_comb begin
bus_byte_enable = 4'b0000;
case (data_format[1:0])
2'b00: bus_byte_enable = 4'b0001 << address[1:0];
2'b01: bus_byte_enable = 4'b0011 << address[1:0];
2'b10: bus_byte_enable = 4'b1111 << address[1:0];
default: bus_byte_enable = 4'b0000;
endcase
end
// correct for unaligned accesses
always_comb begin
position_fix = bus_read_data >> (8*address[1:0]);
end
// sign-extend if necessary
always_comb begin
case (data_format[1:0])
2'b00: sign_fix = {{24{~data_format[2] & position_fix[7]}}, position_fix[7:0]};
2'b01: sign_fix = {{16{~data_format[2] & position_fix[15]}}, position_fix[15:0]};
2'b10: sign_fix = position_fix[31:0];
default: sign_fix = 32'bx;
endcase
end
assign read_data = sign_fix;
endmodule