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

VHDL/FPGA/Verilog

开发平台:

Visual C++

  1. module fifo(clock,reset,read,write,fifo_in,fifo_out,fifo_empty,fifo_half,fifo_full);
  2. input              clock,reset,read,write;
  3. input   [15:0]     fifo_in;
  4. output  [15:0]     fifo_out;
  5. output             fifo_empty,fifo_half,fifo_full;
  6. reg     [15:0]     fifo_out;          
  7. reg     [3:0]      read_ptr,write_ptr,counter;
  8. reg     [15:0]     ram    [15:0];
  9. wire               fifo_empty,fifo_half,fifo_full;
  10. always@(posedge clock)
  11.  if(reset)
  12.   begin
  13.   read_ptr     =0;
  14.   write_ptr    =0;
  15.   counter      =0;
  16.   fifo_out     =0;
  17.   end
  18.  else
  19.  case({read,write})
  20.      2'b00:counter=counter;
  21.      2'b01:begin
  22.            ram[write_ptr]=fifo_in;
  23.            counter=counter+1;
  24.            write_ptr=(write_ptr==15)?0:write_ptr+1;
  25.            end
  26.      2'b10:begin
  27.            fifo_out=ram[read_ptr];
  28.            counter=counter-1;
  29.            read_ptr=(read_ptr==15)?0:read_ptr+1;
  30.            end
  31.      2'b11:begin
  32.            if(counter==0)
  33.            fifo_out=fifo_in;
  34.            else
  35.            begin
  36.            ram[write_ptr]=fifo_in;
  37.            fifo_out=ram[read_ptr];
  38.            write_ptr=(write_ptr==15)?0:write_ptr+1;
  39.            read_ptr=(read_ptr==15)?0:read_ptr+1;
  40.            end
  41.            end
  42. endcase
  43. assign fifo_empty=(counter==0);
  44. assign fifo_full=(counter==15);
  45. assign fifo_half=(counter==8);
  46. endmodule