FIFO的verilog代码
生活随笔
收集整理的這篇文章主要介紹了
FIFO的verilog代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ??FIFO是英文First In First Out 的縮寫,是一種先進先出的數據緩存器,他與普通存儲器的區別是沒有外部讀寫地址線,這樣使用起來非常簡單,但缺點就是只能順序寫入數據,順序的讀出數據,其數據地址由內部讀寫指針自動加1完成,不能像普通存儲器那樣可以由地址線決定讀取或寫入某個指定的地址。 FIFO的一些重要參數 1、FIFO的寬度:也就是英文資料里常看到的THE WIDTH,它指的是FIFO一次讀寫操作的數據位,就像MCU有8位和16位,ARM 32位等等。 2、FIFO的深度:THE DEEPTH,它指的是FIFO可以存儲多少個N位的數據(如果寬度為N)。如一個8位的FIFO,若深度為8,它可以存儲8個8位的數據,深度為12 ,就可以存儲12個8位的數據。 3、滿標志:FIFO已滿或將要滿時由FIFO的狀態電路送出的一個信號,以阻止FIFO的寫操作繼續向FIFO中寫數據而造成溢出(overflow)。 4、空標志:FIFO已空或將要空時由FIFO的狀態電路送出的一個信號,以阻止FIFO的讀操作繼續從FIFO中讀出數據而造成無效數據的讀出(underflow)。 5、讀指針:指向下一個讀出地址。讀完后自動加1。
/************************************** * Module: fifo * Date:2014-08-10 * Author: hemmingway@163.com * * Description: FIFO存儲器設計 ***************************************/ module fifo(clk,rstp,din,writep,readp,dout,emptyp,fullp ); input clk; input rstp; // 復位信號 input[15:0] din; input readp; input writep; output[15:0] dout; output emptyp; output fullp; parameter DEPTH = 2,MAX_COUNT=2'b11; //定義地址最大值reg emptyp; reg fullp; reg[15:0] dout; reg[(DEPTH-1):0] tail; reg[(DEPTH-1):0] head; reg[(DEPTH-1):0] count; reg[15:0] fifomem[0:MAX_COUNT]; // 定義fifo存儲器,4個16位的存儲器// dout被賦給tail指針指向的數值 always @(posedge clk) beginif(rstp==1) begindout <= 16'h0000; // 復位信號有效置0endelse begindout <= fifomem[tail]; //將fifomem中第tail個單元給doutend end// 寫入數據 always @(posedge clk) beginif(rstp==1'b0 && writep == 1'b1 && fullp == 1'b0) beginfifomem[head]<=din; // 寫入end end// head指針遞增 always @(posedge clk) beginif(rstp==1'b1) beginhead<=2'b00;endelse beginif(writep==1'b1 && fullp==1'b0) beginhead<=head+1;endend end//tail指針遞增 always @(posedge clk) beginif(rstp==1'b1) begintail<=2'b00;endelse beginif(readp==1'b1 && emptyp==1'b0) begintail<=tail+1;endend end// 計數器 always @(posedge clk) beginif (rstp == 1'b1) begincount <= 2'b00;endelse begincase ({readp, writep})2'b00: count <= count;2'b01: if (count != MAX_COUNT) count <= count + 1; //為寫狀態時計數器進行加法計數2'b10: if (count != 2'b00)count <= count - 1; //為讀狀態計數器進行減法計數2'b11:count <= count;endcaseend end// empty指針 always @(count) beginif (count == 2'b00)emptyp <= 1'b1; //count為0時emptyp賦為1elseemptyp <= 1'b0; end// fullp指針 always @(count) beginif (count == MAX_COUNT)fullp <= 1'b1; //計數到最大時fullp賦為1elsefullp <= 1'b0; endendmodule
6、寫指針:指向下一個要寫入的地址的,寫完自動加1。
/************************************** * Module: fifo * Date:2014-08-10 * Author: hemmingway@163.com * * Description: FIFO存儲器設計 ***************************************/ module fifo(clk,rstp,din,writep,readp,dout,emptyp,fullp ); input clk; input rstp; // 復位信號 input[15:0] din; input readp; input writep; output[15:0] dout; output emptyp; output fullp; parameter DEPTH = 2,MAX_COUNT=2'b11; //定義地址最大值reg emptyp; reg fullp; reg[15:0] dout; reg[(DEPTH-1):0] tail; reg[(DEPTH-1):0] head; reg[(DEPTH-1):0] count; reg[15:0] fifomem[0:MAX_COUNT]; // 定義fifo存儲器,4個16位的存儲器// dout被賦給tail指針指向的數值 always @(posedge clk) beginif(rstp==1) begindout <= 16'h0000; // 復位信號有效置0endelse begindout <= fifomem[tail]; //將fifomem中第tail個單元給doutend end// 寫入數據 always @(posedge clk) beginif(rstp==1'b0 && writep == 1'b1 && fullp == 1'b0) beginfifomem[head]<=din; // 寫入end end// head指針遞增 always @(posedge clk) beginif(rstp==1'b1) beginhead<=2'b00;endelse beginif(writep==1'b1 && fullp==1'b0) beginhead<=head+1;endend end//tail指針遞增 always @(posedge clk) beginif(rstp==1'b1) begintail<=2'b00;endelse beginif(readp==1'b1 && emptyp==1'b0) begintail<=tail+1;endend end// 計數器 always @(posedge clk) beginif (rstp == 1'b1) begincount <= 2'b00;endelse begincase ({readp, writep})2'b00: count <= count;2'b01: if (count != MAX_COUNT) count <= count + 1; //為寫狀態時計數器進行加法計數2'b10: if (count != 2'b00)count <= count - 1; //為讀狀態計數器進行減法計數2'b11:count <= count;endcaseend end// empty指針 always @(count) beginif (count == 2'b00)emptyp <= 1'b1; //count為0時emptyp賦為1elseemptyp <= 1'b0; end// fullp指針 always @(count) beginif (count == MAX_COUNT)fullp <= 1'b1; //計數到最大時fullp賦為1elsefullp <= 1'b0; endendmodule
總結
以上是生活随笔為你收集整理的FIFO的verilog代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 暴雪网易彻底谈崩!暴雪中国称将于1月23
- 下一篇: 国产开放世界武侠游戏《燕云十六声》开发者