counter_conversion.txt

来自「UART 的VHDL源代码。可在ISE, Max-Plus II,等开发环境下实」· 文本 代码 · 共 51 行

TXT
51
字号
-- Counter using a Conversion Function
-- This counter uses a natural number to hold the count value and converts it into a bit_vector for output. Illustrates the use of a function.
-- 4-bit binary up counter with asynchronous reset 2/2/93
-- dowload from: www.fpga.com.cn & www.pld.com.cn

library ieee;
use ieee.std_logic_1164.all;

ENTITY cntr4bit IS
   PORT(reset,clock : IN BIT; count : OUT BIT_VECTOR(0 TO 3));
END cntr4bit;

ARCHITECTURE dataflow OF cntr4bit IS

   --interface function to generate output bit_vector from 
   --internal count value.
   FUNCTION nat_to_bv(input : NATURAL; highbit : POSITIVE) 
                                       RETURN BIT_VECTOR IS
      VARIABLE temp : NATURAL := 0;
      VARIABLE output : BIT_VECTOR(0 TO highbit);
   BEGIN
      temp := input;
      --check that input fits into (highbit+1) bits
      ASSERT (temp <= (2**(highbit + 1) - 1))
      REPORT "input no. is out of range" SEVERITY ERROR;
      --generate bit values
      FOR i IN highbit DOWNTO 0 LOOP
          IF temp >= (2**i)
          THEN output(i) := '1';
          temp := temp - (2**i);
          ELSE output(i) := '0';
          END IF;
      END LOOP;
      RETURN output;
   END nat_to_bv;

   --signal to hold current count value
   SIGNAL intcount : NATURAL := 0;

BEGIN
   --conditional natural signal assignment models counter
   intcount <= 0 WHEN (reset = '1') ELSE
            ((intcount + 1) MOD 16) WHEN (clock'EVENT AND clock = '1')
            ELSE intcount;
   --interface function converts natural count to bit_vector count
   count <= nat_to_bv(intcount,3);
END;



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?