-
Notifications
You must be signed in to change notification settings - Fork 2
/
magic_memory.sv
59 lines (49 loc) · 1.04 KB
/
magic_memory.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
/*
* Magic memory (provided)
*/
module magic_memory
(
input clk,
/* Port A */
input read,
input write,
input [1:0] wmask,
input [15:0] address,
input [15:0] wdata,
output logic resp,
output logic [15:0] rdata
);
timeunit 1ns;
timeprecision 1ns;
logic [7:0] mem [0:2**($bits(address))-1];
logic [15:0] internal_address;
/* Initialize memory contents from memory.lst file */
initial
begin
$readmemh("memory.lst", mem);
end
/* Calculate internal address */
assign internal_address = {address[15:1], 1'b0};
/* Read */
always_comb
begin : mem_read
rdata = {mem[internal_address+1], mem[internal_address]};
end : mem_read
/* Write */
always @(posedge clk)
begin : mem_write
if (write)
begin
if (wmask[1])
begin
mem[internal_address+1] = wdata[15:8];
end
if (wmask[0])
begin
mem[internal_address] = wdata[7:0];
end
end
end : mem_write
/* Magic memory responds immediately */
assign resp = read | write;
endmodule : magic_memory