1 |
2 |
alfik |
/*
|
2 |
|
|
* This file is subject to the terms and conditions of the BSD License. See
|
3 |
|
|
* the file "LICENSE" in the main directory of this archive for more details.
|
4 |
|
|
*
|
5 |
|
|
* Copyright (C) 2014 Aleksander Osman
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
`include "defines.v"
|
9 |
|
|
|
10 |
|
|
module memory_avalon(
|
11 |
|
|
input clk,
|
12 |
|
|
input rst_n,
|
13 |
|
|
|
14 |
|
|
//{ [66] 1'b is_write, [65:36] 30'b address, [35:4] 32'b value, [3:0] 4'b byteena (4'b0000 - can burst 4 words) }
|
15 |
|
|
input [66:0] ram_fifo_q,
|
16 |
|
|
input ram_fifo_empty,
|
17 |
|
|
output ram_fifo_rdreq,
|
18 |
|
|
|
19 |
|
|
//address and req must be held till ack; on ack address can change
|
20 |
|
|
input [31:0] ram_instr_address,
|
21 |
|
|
input ram_instr_req,
|
22 |
|
|
output reg ram_instr_ack,
|
23 |
|
|
|
24 |
|
|
output reg [31:0] ram_result_address,
|
25 |
|
|
output reg ram_result_valid,
|
26 |
|
|
output reg ram_result_is_read_instr,
|
27 |
|
|
output reg [2:0] ram_result_burstcount,
|
28 |
|
|
output reg [31:0] ram_result,
|
29 |
|
|
|
30 |
|
|
//Avalon master interface
|
31 |
|
|
output reg [31:0] avm_address,
|
32 |
|
|
output reg [31:0] avm_writedata,
|
33 |
|
|
output reg [3:0] avm_byteenable,
|
34 |
|
|
output reg [2:0] avm_burstcount,
|
35 |
|
|
output reg avm_write,
|
36 |
|
|
output reg avm_read,
|
37 |
|
|
|
38 |
|
|
input avm_waitrequest,
|
39 |
|
|
input avm_readdatavalid,
|
40 |
|
|
input [31:0] avm_readdata
|
41 |
|
|
);
|
42 |
|
|
|
43 |
|
|
//------------------------------------------------------------------------------ state machine
|
44 |
|
|
|
45 |
|
|
localparam [1:0] STATE_IDLE = 2'd0;
|
46 |
|
|
localparam [1:0] STATE_WRITE = 2'd1;
|
47 |
|
|
localparam [1:0] STATE_READ = 2'd2;
|
48 |
|
|
|
49 |
|
|
wire start_write = (state == STATE_IDLE || ~(avm_waitrequest)) && ~(ram_fifo_empty) && ram_fifo_q[66] == 1'b1;
|
50 |
|
|
wire start_read = (state == STATE_IDLE || ~(avm_waitrequest)) && ~(ram_fifo_empty) && ram_fifo_q[66] == 1'b0 && readp_possible;
|
51 |
|
|
wire start_instr_read = (state == STATE_IDLE || ~(avm_waitrequest)) && ~(start_write) && ~(start_read) && ram_instr_req && readp_possible;
|
52 |
|
|
|
53 |
|
|
assign ram_fifo_rdreq = start_read || start_write;
|
54 |
|
|
|
55 |
|
|
reg [1:0] state;
|
56 |
|
|
always @(posedge clk or negedge rst_n) begin
|
57 |
|
|
if(rst_n == 1'b0) state <= STATE_IDLE;
|
58 |
|
|
else if(start_write) state <= STATE_WRITE;
|
59 |
|
|
else if(start_read || start_instr_read) state <= STATE_READ;
|
60 |
|
|
else if(~(avm_waitrequest)) state <= STATE_IDLE;
|
61 |
|
|
end
|
62 |
|
|
|
63 |
|
|
//------------------------------------------------------------------------------ pipeline read
|
64 |
|
|
|
65 |
|
|
//[33] is_read_instr [32:30] burstcount [29:0] address
|
66 |
|
|
|
67 |
|
|
wire readp_0_update = (start_read || start_instr_read) && readp_2[32:30] == 3'd0 && readp_1[32:30] == 3'd0 && (readp_0[32:30] == 3'd0 || readp_chain);
|
68 |
|
|
wire readp_1_update = ~(readp_0_update) && (start_read || start_instr_read) && readp_2[32:30] == 3'd0 && (readp_1[32:30] == 3'd0 || readp_chain);
|
69 |
|
|
wire readp_2_update = ~(readp_1_update) && ~(readp_0_update) && (start_read || start_instr_read) && (readp_2[32:30] == 3'd0 || readp_chain);
|
70 |
|
|
|
71 |
|
|
wire readp_chain = readp_0[32:30] == 3'd1 && avm_readdatavalid;
|
72 |
|
|
|
73 |
|
|
wire [2:0] readp_0_burstcount = readp_0[32:30] - 3'd1;
|
74 |
|
|
wire [29:0] readp_0_address = readp_0[29:0] + 30'd1;
|
75 |
|
|
|
76 |
|
|
wire [2:0] read_burstcount = (ram_fifo_q[3:0] == 4'h0)? 3'd4 : 3'd1;
|
77 |
|
|
|
78 |
|
|
wire [33:0] readp_value = (start_read)? { 1'b0, read_burstcount, ram_fifo_q[65:36] } : { 1'b1, 3'd4, ram_instr_address[31:2] };
|
79 |
|
|
|
80 |
|
|
reg [33:0] readp_0;
|
81 |
|
|
always @(posedge clk or negedge rst_n) begin
|
82 |
|
|
if(rst_n == 1'b0) readp_0 <= 34'd0;
|
83 |
|
|
else if(readp_0_update) readp_0 <= readp_value;
|
84 |
|
|
else if(readp_chain) readp_0 <= readp_1;
|
85 |
|
|
else if(readp_0[32:30] > 3'd0 && avm_readdatavalid) readp_0 <= { readp_0[33], readp_0_burstcount, readp_0_address };
|
86 |
|
|
end
|
87 |
|
|
|
88 |
|
|
reg [33:0] readp_1;
|
89 |
|
|
always @(posedge clk or negedge rst_n) begin
|
90 |
|
|
if(rst_n == 1'b0) readp_1 <= 34'd0;
|
91 |
|
|
else if(readp_1_update) readp_1 <= readp_value;
|
92 |
|
|
else if(readp_chain) readp_1 <= readp_2;
|
93 |
|
|
end
|
94 |
|
|
|
95 |
|
|
reg [33:0] readp_2;
|
96 |
|
|
always @(posedge clk or negedge rst_n) begin
|
97 |
|
|
if(rst_n == 1'b0) readp_2 <= 34'd0;
|
98 |
|
|
else if(readp_2_update) readp_2 <= readp_value;
|
99 |
|
|
else if(readp_chain) readp_2 <= 34'd0;
|
100 |
|
|
end
|
101 |
|
|
|
102 |
|
|
wire readp_possible = readp_2[32:30] == 3'd0 || readp_1[32:30] == 3'd0 || readp_0[32:30] == 3'd0 || (readp_0[32:30] == 3'd1 && avm_readdatavalid);
|
103 |
|
|
|
104 |
|
|
//------------------------------------------------------------------------------ avalon bus control
|
105 |
|
|
|
106 |
|
|
always @(posedge clk or negedge rst_n) begin
|
107 |
|
|
if(rst_n == 1'b0) avm_address <= 32'd0;
|
108 |
|
|
else if(start_write || start_read) avm_address <= { ram_fifo_q[65:36], 2'b00 };
|
109 |
|
|
else if(start_instr_read) avm_address <= { ram_instr_address[31:2], 2'b00 };
|
110 |
|
|
end
|
111 |
|
|
|
112 |
|
|
always @(posedge clk or negedge rst_n) begin
|
113 |
|
|
if(rst_n == 1'b0) avm_writedata <= 32'd0;
|
114 |
|
|
else if(start_write) avm_writedata <= ram_fifo_q[35:4];
|
115 |
|
|
end
|
116 |
|
|
|
117 |
|
|
always @(posedge clk or negedge rst_n) begin
|
118 |
|
|
if(rst_n == 1'b0) avm_byteenable <= 4'd0;
|
119 |
|
|
else if(start_write || start_read) avm_byteenable <= (ram_fifo_q[3:0] == 4'h0)? 4'hF : ram_fifo_q[3:0];
|
120 |
|
|
else if(start_instr_read) avm_byteenable <= 4'hF;
|
121 |
|
|
end
|
122 |
|
|
|
123 |
|
|
always @(posedge clk or negedge rst_n) begin
|
124 |
|
|
if(rst_n == 1'b0) avm_burstcount <= 3'd0;
|
125 |
|
|
else if(start_write) avm_burstcount <= 3'd1;
|
126 |
|
|
else if(start_read) avm_burstcount <= read_burstcount;
|
127 |
|
|
else if(start_instr_read) avm_burstcount <= 3'd4;
|
128 |
|
|
end
|
129 |
|
|
|
130 |
|
|
always @(posedge clk or negedge rst_n) begin
|
131 |
|
|
if(rst_n == 1'b0) avm_read <= 1'b0;
|
132 |
|
|
else if(start_read || start_instr_read) avm_read <= 1'b1;
|
133 |
|
|
else if(~(avm_waitrequest)) avm_read <= 1'b0;
|
134 |
|
|
end
|
135 |
|
|
|
136 |
|
|
always @(posedge clk or negedge rst_n) begin
|
137 |
|
|
if(rst_n == 1'b0) avm_write <= 1'b0;
|
138 |
|
|
else if(start_write) avm_write <= 1'b1;
|
139 |
|
|
else if(~(avm_waitrequest)) avm_write <= 1'b0;
|
140 |
|
|
end
|
141 |
|
|
|
142 |
|
|
//------------------------------------------------------------------------------ results
|
143 |
|
|
|
144 |
|
|
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) ram_instr_ack <= `FALSE; else ram_instr_ack <= start_instr_read; end
|
145 |
|
|
|
146 |
|
|
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) ram_result_address <= 32'd0; else ram_result_address <= { readp_0[29:0], 2'b00 }; end
|
147 |
|
|
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) ram_result_valid <= `FALSE; else ram_result_valid <= avm_readdatavalid; end
|
148 |
|
|
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) ram_result_burstcount <= 3'd0; else ram_result_burstcount <= readp_0[32:30]; end
|
149 |
|
|
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) ram_result_is_read_instr <= 1'b0; else ram_result_is_read_instr <= readp_0[33]; end
|
150 |
|
|
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) ram_result <= 32'd0; else ram_result <= avm_readdata; end
|
151 |
|
|
|
152 |
|
|
//------------------------------------------------------------------------------
|
153 |
|
|
// synthesis translate_off
|
154 |
|
|
wire _unused_ok = &{ 1'b0, ram_instr_address[1:0], 1'b0 };
|
155 |
|
|
// synthesis translate_on
|
156 |
|
|
//------------------------------------------------------------------------------
|
157 |
|
|
|
158 |
|
|
endmodule
|