回复 45# 甲壳虫
基于双端口RAM, 模拟你的应用我写了个简单的代码,功能仿真是没有问题的。
- // top level to calulate the below loop with ram
- module calculation(
- input wire clk,
- input wire rst_n,
- output wire we,
- output wire [7:0] wr_addr,
- output wire [7:0] rd_addr,
- output wire [7:0] indata,
- output wire [7:0] outdata,
- output wire [7:0] avg);
-
- cal_expression U1(
- .clk(clk),
- .rst_n(rst_n),
- .we(we),
- .outdata(outdata),
- .rd_addr(rd_addr),
- .avg(avg));
- ram_init U2(
- .clk(clk),
- .rst_n(rst_n),
- .we(we),
- .indata(indata),
- .wr_addr(wr_addr));
-
- ram_dp U3(
- .clk(clk),
- .rst_n(rst_n),
- .rd_addr(rd_addr),
- .wr_addr(wr_addr),
- .indata(indata),
- .we(we),
- .outdata(outdata));
- endmodule
- //for (i = 10; i <20; i++)
- // avg += pd[i];
- module cal_expression(
- input wire clk,
- input wire rst_n,
- input wire we,
- input wire [7:0]outdata,
- output reg [7:0] rd_addr,
- output reg [7:0] avg);
- reg [7:0] cnt;
-
- always @ (posedge clk or negedge rst_n)
- if (!rst_n)
- rd_addr <= 8'd10;
- else if (we == 1'b0 && rd_addr < 19)
- rd_addr <= rd_addr + 8'b1;
-
- always @ (posedge clk or negedge rst_n)
- if (!rst_n)
- begin
- avg <= 8'b0;
- cnt <= 0;
- end
- else if (we == 1'b0 && cnt < 11)
- begin
- avg <= avg + outdata;
- cnt <= cnt + 8'b1;
- end
- endmodule
-
- //write initial values to ram.
- module ram_init(
- input wire clk,
- input wire rst_n,
- output reg we,
- output reg [7:0] indata,
- output reg [7:0] wr_addr);
-
- always @ (posedge clk or negedge rst_n)
- if (!rst_n)
- indata <= 8'b0;
- else if (we && indata < 255)
- indata <= indata + 8'b1;
-
- always @ (posedge clk or negedge rst_n)
- if (!rst_n)
- wr_addr <= 8'b0;
- else if (we && wr_addr < 255)
- wr_addr <= wr_addr + 8'b1;
- always @ (posedge clk or negedge rst_n)
- if (!rst_n)
- we <= 1'b1;
- else if (indata == 255 && wr_addr == 255)
- we <= 1'b0;
- endmodule
- //double port ram module
- module ram_dp(
- input wire clk,
- input wire rst_n,
- input wire we,
- input wire [7:0] wr_addr,
- input wire [7:0] indata,
- input wire [7:0] rd_addr,
- output reg [7:0] outdata);
- reg [7:0]mem[255:0];
-
- always @ (posedge clk or negedge rst_n)
- if (!rst_n)
- mem[wr_addr] <= 8'b0;
- else if (we == 1'b1)
- mem[wr_addr] <= indata;
- always @ (posedge clk or negedge rst_n)
- if (!rst_n)
- outdata <= 8'b0;
- else if (we == 1'b0)
- outdata <= mem[rd_addr];
- endmodule
复制代码 |