面试时提到一个关于大计数器的问题

2020-02-04 10:13发布

面试官问道针对一个大数分频器(比如1千分频1万分频)的计数器,有什么好方法可以节省一点资源?
当时想了很久,就说按实际分频用到多少位就分配几位,避免一些无用寄存器的使用。然后他摇了摇头,我向他请教,他说正确的是要把大计数器分拆成几个小计数器,溢出计数。他说这样子反转的位数就少,才可靠。
我当时纳闷了,你不是说要节省资源吗?怎么问题跑到这上面去了?
私底下思考了一下,请问大神:分拆成小计数器对节省资源来说有作用吗?按我理解用到几位就需要有几个寄存器,这是省不了的。
盼回复,谢谢!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
13条回答
pkuzhx
1楼-- · 2020-02-05 13:17
关注
drentsi
2楼-- · 2020-02-05 15:57
远离这种低水平学究型的单位,资源不是这么省的,等我有空写个超级省资源的计数器,128bit可能只需要十几个LUT
drentsi
3楼-- · 2020-02-05 16:46
 精彩回答 2  元偷偷看……
drentsi
4楼-- · 2020-02-05 22:33
代码贴上,没测试,不保证能用,能理解就理解,不教会,不解释
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date:    13:24:23 09/09/2016
-- Design Name:
-- Module Name:    WideCntr - 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 using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

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

entity WideCntr is
generic(A_SIZE:integer:=5); -- VEC=A_SIZE*2**A_SIZE,3=>24,4=>64,5=>160,6=>384
port(
        clk        :        in std_logic;
        rst        :        in std_logic;
        p                :        in std_logic;
        addr        :        in std_logic_vector(0 to A_SIZE-1);
        cntr        :  out std_logic_vector(0 to A_SIZE-1)
);
end WideCntr;

architecture Behavioral of WideCntr is
        type RAM_TYPE is array(0 to 2**A_SIZE-1) of std_logic_vector(0 to A_SIZE-1);
        signal ram:RAM_TYPE:=(others=>(others=>'0'));
       
        signal p_cntr:std_logic_vector(0 to A_SIZE);
        signal p_c_clr:std_logic;
        signal cy:std_logic;       
        signal ram_addr:std_logic_vector(0 to A_SIZE-1);
        signal ram_data:std_logic_vector(0 to A_SIZE);
       
begin

process(clk)
begin
if clk'event and clk='1' then
if rst='1' then
        p_cntr<=(others=>'0');
else
        if p='1' then
                if p_c_clr='1' then
                        p_cntr(0)<='0';
                        p_cntr(1 to A_SIZE)<=p_cntr(1 to A_SIZE)+1;
                else
                        p_cntr<=p_cntr +1;
                end if;
        end if;
end if;
end if;
end process;

process(clk)
begin
if clk'event and clk='1' then
        ram(conv_integer(ram_addr)) <=ram_data(1 to A_SIZE);
end if;
end process;

        ram_data<=(others=>'0') when rst='1' else ('0'&ram(conv_integer(ram_addr)))+cy;
       
process(clk)
begin
if clk'event and clk='1' then
if rst='1' then
        ram_addr<=ram_addr+1;
        p_c_clr<='0';
        cy<='0';
else
        p_c_clr<='0';
        cy<='0';
        ram_addr<=conv_std_logic_vector(1,A_SIZE);
        if p_cntr(0)='1' then
                cy<='1';
                p_c_clr<='1';
        elsif ram_data(0)='1' then
                cy<='1';
                ram_addr<=ram_addr+1;
        end if;
end if;
end if;
end process;


        cntr<=p_cntr(1 to A_SIZE) when addr=0 else ram(conv_integer(addr));

end Behavioral;

hqbenson
5楼-- · 2020-02-06 02:39
本帖最后由 hqbenson 于 2016-9-13 11:46 编辑
drentsi 发表于 2016-9-9 14:29
代码贴上,没测试,不保证能用,能理解就理解,不教会,不解释
----------------------------------------- ...

      谢谢!先感谢大神回复!我再仔细研究一下.VHDL忘得差不多了。。
      个人理解,您写应该就是把计数值存进RAM中是吗?只用几个RAM就能够实现多位计数,可以省下上百个LUT和REG。。
      确实很省资源!学习了!

hqbenson
6楼-- · 2020-02-06 03:31
kdurant 发表于 2016-9-2 15:38
纯属胡扯,以altera为例:

一个128bit的计数器,消耗资源

谢谢!先感谢大神回复!我再仔细研究一下.
        其实后来我也实际写过这两种情况下的代码,我也发现实际上分拆计数器的话消耗资源确实比一个大计数器要多出很多LUT和REG!
       我也很同意您的观点。实际上只是速度变快了,反转变少了。感觉其实跟流水线是一个原理。

一周热门 更多>