微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > FPGA,CPLD和ASIC > 大家来看看这两段小程序有什么差别

大家来看看这两段小程序有什么差别

时间:10-02 整理:3721RD 点击:
1.
signal cnt : integer range 0 to 8;
elsif clk'event and clk = '1' then
   if cnt = 8 then
      cnt <= 0;
      led <= '1';
   else
      cnt <= cnt + 1;
      led <= '0';
   end if;
end if;
2.
signal cnt : integer range 0 to 7;
elsif clk'event and clk = '1' then
   if cnt = 7 then
      cnt <= cnt + 1;
      led <= '1';
   else
      cnt <= cnt + 1;
      led <= '0';
   end if;
end if;
我有两个问题:
1.程序2中我定义cnt的范围是0-7,当cnt=7时执行cnt=cnt+1代码时,会产生溢出,针对这个溢出,FPGA内部是如何操作的呢,是不理睬还是怎样呢?
2.时序仿真后,程序1比程序2多了一个时钟的空闲操作,如果要实现精确的计数,是不是移位寄存器的方法更适合呢?
希望大家踊跃发言。

自己顶一下!

你这是软件思维。硬件是2进制,只有0和1跳转,你想想7+1该怎么翻转?
计数器没必要用int,用std_logic_vector



   7+1=1000,我的疑问是我定义的是3位寄存器,那这个进位的‘1’是丢失了吗?FPGA内部对这个进位会是怎样的一种操作呢?
非常感谢你的参与。

LZ可以把整个程序都贴上来么?我想仿真一下,谢谢!

表面上你用的是integer,其实在芯片内部还是将其转换为二进制来实现。
建议你的程序这么改:
signal cnt : integer range 0 to 7;
elsif clk'event and clk = '1' then
   if cnt = 7 then
      cnt <= 0;
      led <= '1';
   else
      cnt <= cnt + 1;
      led <= '0';
   end if;
end if;



    好的。
1。
entity test is
port(
     clk : in std_logic;
     nrst : in std_logic;
     led : out std_logic
     );
end test;
architecture RTL of test is
signal cnt : integer range 0 to 7;
begin
process(clk,nrst)
begin
if nrst = '0' then
   cnt <= 0;
   led <= '0';
elsif clk'event and clk = '1' then
   if cnt = 7 then
      led <= '1';
      cnt <= cnt + 1;
   else
      cnt <= cnt + 1;
      led <= '0';
   end if;
end if;
end process;
end RTL;

2.
entity test is
port(
     clk : in std_logic;
     nrst : in std_logic;
     led : out std_logic
     );
end test;
architecture RTL of test is
signal cnt : integer range 0 to 8;
begin
process(clk,nrst)
begin
if nrst = '0' then
   cnt <= 0;
   led <= '0';
elsif clk'event and clk = '1' then
   if cnt = 8 then
      led <= '1';
      cnt <= 0;
   else
      cnt <= cnt + 1;
      led <= '0';
   end if;
end if;
end process;
end RTL;



    signal cnt : integer range 0 to 7;
elsif clk'event and clk = '1' then
   if cnt = 7 then
      cnt <= 0;
      led <= '1';
   else
      cnt <= cnt + 1;
      led <= '0';
   end if;
end if;
原来应该是这样,呵呵,仿真结果和
  if cnt = 7 then
      cnt <= cnt + 1;
      led <= '1';
是一样的,但是肯定是上面一种写法好,但是我还是想知道如果采用下面一种写法,FPGA内部逻辑实现是不是有可能出错呢?
呵呵,一点疑惑,万分感谢!



你用的是integer,怎么会是3位寄存器呢?理解上有偏差吧。如果你用integer定义的cnt,来执行这两个程序,第二个就是比第一个多一个时钟周期的计数;如果你是定义的4位寄存器,效果同前;如果是3位寄存器,那么第2段代码7过后就是0了,不会出现8的情况,而且可能也会报错,说cnt的位数不匹配,具体是不是能报错,你编译看看吧。

不会出错。

一般只会warning 吧,说丢了carry bit之类的,或者位数不匹配



    因为你定义的是cnt : integer range 0 to 7;
所以软件在综合的时候会用三位二进制来计数,从“000”到“111”。
所以用
if cnt=7 then
    cnt<=0;
不会产生溢出;
但如果用
if cnt=7 then
    cnt<=cnt+1;
那么cnt就是吧了,即“1000”。
这就超出了软件自己定义的三位二进制,从而会产生溢出,软件会给出警告或错误的提示吧。但低三位还是“000”。
我没有进行试验验证,但我感觉错误倒是不会有,也不会影响结果。建议按照规范来编程吧。

不是FPGA的问题,完全是EDA工具的原因。
强大的EDA...

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top