|
楼主 |
发表于 2011-5-17 18:48:54
|
显示全部楼层
本帖最后由 yuwu131 于 2011-5-17 18:55 编辑
回复 2# bobzeng
我写了一个不知道有没有问题
module fifo_i (input [15:0]data_in,input clk_in,clk_o,rst,rd,wr,fifo_i,reset,
output empty,full,output reg [4:0]fifo_cnt,output reg [15:0]data_out);
reg [15:0] fifo_ram[0:15] ;
reg [2:0] rd_ptr,wr_ptr; //read and write point
assign empty=(fifo_cnt==0);
assign full= (fifo_cnt==15);
always @( posedge clk_i ) begin:write
if(fifo_i)begin
if(wr &&!full)
fifo_ram[wr_ptr] <= data_in;
/* else if(wr && rd)
fifo_ram[wr_ptr] <= data_in;*/
end
end
always @ (posedge clk_o )begin:read
if(fifo_i)begin
if (rd&&!empty)
data_out <= fifo_ram[rd_ptr];
else if (wr&&rd) // always deal with the data read out fifo first
data_out <= fifo_ram[rd_ptr];
end
end
always @ (posedge clk_i )begin:pointer1
if (fifo_i)begin
if (rst)begin
wr_ptr<=0;
end
else begin
wr_ptr<=((fifo_i&&(wr&&!full)/*||(wr&&rd)*/))?wr_ptr+1:wr_ptr;
end
end
end
always @ (posedge clk_o )begin:pointer2
if (fifo_i)begin
if (rst)begin
rd_ptr<=0;
end
else begin
rd_ptr<=((fifo_i&&(rd&&!empty))||(fifo_i&&(wr&&rd)))?rd_ptr+1:rd_ptr;
end
end
end
always @ (posedge clk_i )begin:count1
if (fifo_i)begin
if (rst)
fifo_cnt <=4'b0000;
else begin
case({wr,rd})
2'b00:fifo_cnt<=fifo_cnt;
/* 2'b01:fifo_cnt<=(fifo_cnt==0)?0:fifo_cnt-1; */
2'b10:fifo_cnt<=(fifo_cnt==15)?15:fifo_cnt+1;
/* 2'b11:fifo_cnt<=fifo_cnt-1; */
default:fifo_cnt<=fifo_cnt;
endcase
end
end
end
always @ (posedge clk_o)begin:count2
if (fifo_i)begin
if (rst)
fifo_cnt <=4'b0000;
else begin
case({wr,rd})
2'b00:fifo_cnt<=fifo_cnt;
2'b01:fifo_cnt<=(fifo_cnt==0)?0:fifo_cnt-1;
/* 2'b01:fifo_cnt<=(fifo_cnt==15)?15:fifo_cnt+1; */
2'b11:fifo_cnt<=fifo_cnt-1;
default:fifo_cnt<=fifo_cnt;
endcase
end
end
end
endmodule |
|