FIFO.V
上传用户:saul_905
上传日期:2013-11-27
资源大小:184k
文件大小:3k
源码类别:

VHDL/FPGA/Verilog

开发平台:

Visual C++

  1. module fifo(data_out,fifo_full,fifo_he,fifo_hf,fifo_empty,clk,reset,write,read,data_in);
  2. parameter FIFO_WIDTH=8;
  3. parameter FIFO_DEPTH=8;
  4. parameter FIFO_PTR_WDTH=3;
  5. output    [FIFO_WIDTH-1:0]      data_out;//The output data
  6. output                          fifo_full;//The fifo full flag
  7. output                          fifo_he;//The fifo half empty flag
  8. output                          fifo_hf;//The fifo half full flag
  9. output                          fifo_empty;//The fifo empty flag
  10. input                           clk;//The input flag
  11. input                           reset;//The fifo reset
  12. input                           write;//The syncronous write strobe
  13. input                           read;//The syncronous read strobe
  14. input     [FIFO_WIDTH-1:0]      data_in;//The input data
  15. reg       [FIFO_WIDTH-1:0]      fifo_ram       [0:FIFO_DEPTH-1];
  16. reg       [FIFO_PTR_WDTH-1:0]   wr_ptr,rd_ptr;
  17. reg       [FIFO_PTR_WDTH-1:0]   fifo_count;
  18. wire                            fifo_full,fifo_enpty;
  19. wire                            fifo_he,fifo_hf;
  20. reg       [FIFO_WIDTH-1:0]      data_out;
  21. /***************************************************************************
  22. If this is a read get the data that is in the location pointed to by the
  23.  tead pointer,and put it onto the output bus
  24. ****************************************************************************/
  25. always@(posedge clk)
  26.    if(read)
  27.       data_out<=fifo_ram[rd_ptr];
  28.    else if(write)
  29.       fifo_ram[wr_ptr]=data_in;
  30. /****************************************************************************
  31. Increment the write pointer on every write and the read pointer on every read
  32. *****************************************************************************/
  33.         
  34. always@(posedge clk)
  35.    if(reset)
  36.       wr_ptr<=0;
  37.    else
  38.       wr_ptr<=(write)?wr_ptr+1:wr_ptr;
  39.    
  40. always@(posedge clk)
  41.    if(reset)
  42.       rd_ptr<=0;
  43.    else
  44.       rd_ptr<=(read)?rd_ptr+1:rd_ptr;
  45.       
  46. /*****************************************************************************
  47. The fifo counter increment on every write and decerment on every read .those 
  48. code is used to provide flags to the other module,the other module check 
  49. those flags,then know the state of fifo and decide whether write or read fifo
  50. *****************************************************************************/
  51. always@(posedge clk)
  52. begin
  53.      if(reset)
  54.            begin
  55.                fifo_count<=0;
  56.            end
  57.       else begin
  58.             case({write,read}) 
  59.             2'b00:fifo_count<=fifo_count; 
  60.             2'b01:fifo_count<=(fifo_count==0)?FIFO_DEPTH:fifo_count-1;
  61.             2'b10:fifo_count<=(fifo_count==FIFO_DEPTH)?0:fifo_count+1;
  62.             2'b11:fifo_count<=fifo_count;
  63.             endcase
  64.             end
  65. end
  66. assign fifo_hf=(fifo_count>=4);
  67. assign fifo_he=(fifo_count<=4);
  68. assign fifo_empty=(fifo_count==0);
  69. assign fifo_full=(fifo_count>=FIFO_DEPTH);
  70. endmodule