-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBRAM_bytewrite.sv
34 lines (34 loc) · 1.03 KB
/
BRAM_bytewrite.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
`timescale 1ns/1ps
module BRAM_bytewrite #(
parameter DATA_WIDTH = 512,
ADDR_WIDTH = 6
)(
input clk, // Clock
input [ADDR_WIDTH-1:0] raddr, // read Address
input [ADDR_WIDTH-1:0] waddr, // write Address
input [DATA_WIDTH-1:0] din, // Data Input
input [DATA_WIDTH/8-1:0]we, // Write Enable
output [DATA_WIDTH-1:0] dout // Data Output
);
reg [ADDR_WIDTH-1:0] addr_r; // Address Register
reg [DATA_WIDTH-1:0] ram [0:(1 << ADDR_WIDTH)-1];
integer j;
initial begin
for (j = 0; j < (1 << ADDR_WIDTH); j = j + 1) begin
ram[j] = 0;
end
end
always @(posedge clk) begin
addr_r <= raddr == waddr ? waddr : raddr;
end
assign dout = ram[addr_r];
generate
genvar i;
for(i = 0; i < DATA_WIDTH/8; i = i+1) begin
always @(posedge clk) begin
if(we[i])
ram[waddr][(i+1)*8-1:(i*8)] <= din[(i+1)*8-1:(i*8)];
end
end
endgenerate
endmodule