搜索
bottom↓
回复: 12
打印 上一主题 下一主题

刚出锅的FIFO代码(VHDL),欢迎大家拍砖。

[复制链接]

出0入0汤圆

跳转到指定楼层
1
发表于 2010-3-16 09:47:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
FIFO是英文First In First Out 的缩写,是一种先进先出的数据缓存器,没有地址线。

FIFO一般用于不同时钟域之间的数据传输,比如FIFO的一端时AD数据采集,另一端时计算机的PCI总线,假设其AD采集的速率为16位 100K SPS,那么每秒的数据量为100K×16bit=1.6Mbps,而PCI总线的速度为33MHz,总线宽度32bit,在两个不同的时钟域间就可以采用FIFO来作为数据缓冲。另外对于不同宽度的数据接口也可以用FIFO,例如单片机位8位数据输出,而DSP可能是16位数据输入,在单片机与DSP连接时就可以使用FIFO来达到数据匹配的目的。


下面是我写的一个FIFO代码,是深度和宽度都可以改变的,也只是刚刚前仿通过,欢迎大家拍砖。


端口的输入/输出解释:
----------------------------------------------------------------------------------------
---rst为复位信号。
---rd为读允许位,wr为写允许位
---data_in为数据输入,data_out 为数据输出;
---clk_wr,clk_rd  分别是写数据 和 读 数据的时钟。
---wr_reset,rd_reset 分别是 写的指针复位 读的指针复位
---wr_yichu,rd_ok 分别是写满溢出位和“读完毕位”

-----------------------------------------------------------------------


----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date:    19:02:19 03/15/2010
-- Design Name:
-- Module Name:    fifo - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity fifo is
generic (bits : integer :=8;
         words: integer :=5);
port(rst,clk_rd,clk_wr,rd,wr,rd_reset,wr_reset:in std_logic;
          data_in:in std_logic_vector(bits-1 downto 0);
          wr_yichu,rd_ok:out std_logic;
          data_out:out std_logic_vector(bits-1 downto 0));
end fifo;

architecture Behavioral of fifo is
type fifo_array is array(0 to words-1) of
     std_logic_vector(bits-1 downto 0);
signal vector_array:fifo_array;
signal addr_rd:integer range 0 to words;
signal addr_wr:integer range 0 to words;

begin
reset_andwr:process(rst,clk_wr)
      begin
                if (rst='1') then
                for i in vector_array'range loop
                vector_array(i)<=(others =>'0');
                end loop;
                elsif(clk_wr'event and clk_wr='1') then
                if(wr='1' and  (addr_wr/=words)) then
                vector_array(addr_wr)<=data_in;
                end if;
                end if;
           end process;
---------------------------------------------------
read: process(rst,clk_rd)
      begin
      if(rst='1') then
                data_out<=(others=>'Z');
                elsif(clk_rd'event and clk_rd='1') then
                if(rd='1') then
                data_out<=vector_array(addr_rd);
                end if;
                end if;
                end process;
-----------------------------------------
wr_count:process(rst,clk_wr)
      begin
                if (rst='1') then
                addr_wr<=0;
                elsif(clk_wr'event and clk_wr='1') then
                if(wr_reset='1') then
                addr_wr<=0;
                elsif (wr='1') then addr_wr<=addr_wr+1;
               
                end if;
                end if;
                 if(addr_wr=words) then wr_yichu<='1';
                          else wr_yichu<='0';
                          end if;   
                end process;
-------------------------------------------------
rd_count:process(rst,clk_rd)
      begin
                if (rst='1') then
                addr_rd<=0;
                elsif(clk_rd'event and clk_rd='1') then
               
                if (rd='1') then addr_rd<=addr_rd+1;
          
                end if;
                end if;
                 if(addr_rd=words)then rd_ok<='1';
                          else rd_ok<='0';
                          end if;
      if(rd_reset='1') then
                addr_rd<=0;               
                end if;
                end process;
------------------------------------               
end Behavioral;




--------------------------------------------------
wr_count为写到哪里的标志,如果写满,则会停止写。并且wr_yichu置‘1’,然后等待wr_reset;
rd_count为读到哪里的标志 ,如果读不允许,则输出高阻态。读完时,rd_ok置‘1’,等待rd_reset.
------------------------------------------------------------------------------------



结束语:等到下到板子里后肯定会修改的,到时候再说,大家先看看。

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

2
发表于 2010-3-16 10:47:58 | 只看该作者
回复【楼主位】dongzhiqing
-----------------------------------------------------------------------

期待

出0入0汤圆

3
发表于 2010-3-16 10:57:52 | 只看该作者
期待,好,想要一个最全的,参考

出0入0汤圆

4
发表于 2010-3-16 12:19:20 | 只看该作者
这样会被综合在逻辑资源区,你应当用IP核的块RAM做FIFO

出0入0汤圆

5
 楼主| 发表于 2010-3-16 12:51:48 | 只看该作者
回复【3楼】888888888888
-----------------------------------------------------------------------

我用的是xilinx的spartan ii,有20万门。还不知道他的ram多大。一般fifo都用ram来做吗?
我主要是刚入门,希望说的明白点。

出0入0汤圆

6
发表于 2010-3-16 12:51:54 | 只看该作者
我也觉得,fifo不是有ip核么,用着也挺方便的。为什么大家都想自己编呢?高手别拍我。。。。

出0入0汤圆

7
发表于 2010-3-16 12:54:17 | 只看该作者
回复【4楼】dongzhiqing
回复【3楼】888888888888  
-----------------------------------------------------------------------
我用的是xilinx的spartan ii,有20万门。还不知道他的ram多大。一般fifo都用ram来做吗?
我主要是刚入门,希望说的明白点。
-----------------------------------------------------------------------

XC2S100 10个块RAM,每个4096位,我试过16位宽,2048深度能通过

出0入0汤圆

8
发表于 2010-3-16 17:47:17 | 只看该作者
楼主想法很好,但是实际异步时钟的FIFO需要考虑一些问题.
需要有同步采样电路,必然同时要用格雷码作为内部地址计数器的编码~
给楼主一分资料参考一下吧~
异步FIFO的设计ourdev_538703.pdf(文件大小:434K) (原文件名:异步FIFO的设计.pdf)

出0入0汤圆

9
 楼主| 发表于 2010-3-17 08:55:14 | 只看该作者
回复【7楼】jerrychenglei
-----------------------------------------------------------------------

非常感谢啊。

出0入0汤圆

10
发表于 2010-3-17 12:57:53 | 只看该作者
好东西

出0入0汤圆

11
发表于 2010-3-17 20:12:11 | 只看该作者
thanks

出0入0汤圆

12
发表于 2010-3-19 15:42:05 | 只看该作者
mark

出0入0汤圆

13
发表于 2010-10-15 21:42:10 | 只看该作者
MARK
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 17:20

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表