Professional Documents
Culture Documents
tphl => 7 ns, tplhe => 15 ns, tphle => 12 ns); END FOR; FOR ALL : and3 USE ENTITY work.and3(behaviour) GENERIC MAP(tplh => 8 ns, tphl => 5 ns, tplhe => 20 ns, tphle => 15 ns); END FOR; END FOR; END FOR; END FOR; END parts;
Generated Binary Up Counter The first design entity is a T-type flip-flop. The second is an scalable synchronous binary up counter illustrating the use of the generate statement to produce regular structures of components. library ieee; use ieee.std_logic_1164.all; entity tff is port(clk, t, clear : in std_logic; q : buffer std_logic); end tff; architecture v1 of tff is begin process(clear, clk) begin if clear = '1' then q <= '0'; elsif rising_edge(clk) then if t = '1' then q <= not q; else null; end if; end if; end process; end v1; library ieee; use ieee.std_logic_1164.all; entity bigcntr is generic(size : positive := 32); port(clk, clear : in std_logic; q : buffer std_logic_vector((size-1) downto 0)); end bigcntr; architecture v1 of bigcntr is component tff is port(clk, t, clear : in std_logic; q : buffer std_logic); end component; signal tin : std_logic_vector((size-1) downto 0); begin genttf : for i in (size-1) downto 0 generate ttype : tff port map (clk, tin(i), clear, q(i)); end generate; genand : for i in 0 to (size-1) generate t0 : if i = 0 generate
Counter using Multiple Wait Statements This example shows an inefficient way of describing a counter. --vhdl model of a 3-state counter illustrating the use --of the WAIT statement to suspend a process.At each wait --statement the simulation time is updated one cycle,transferring --the driver value to the output count. --This architecture shows that there is no difference between --WAIT UNTIL (clock'EVENT AND clock = '1') and WAIT UNTIL clock = '1' ENTITY cntr3 IS PORT(clock : IN BIT; count : OUT NATURAL); END cntr3; ARCHITECTURE using_wait OF cntr3 IS BEGIN PROCESS BEGIN --WAIT UNTIL (clock'EVENT AND clock = '1'); WAIT UNTIL clock = '1'; count <= 0; --WAIT UNTIL (clock'EVENT AND clock = '1'); WAIT UNTIL clock = '1'; count <= 1; --WAIT UNTIL (clock'EVENT AND clock = '1'); WAIT UNTIL clock = '1'; count <= 2; END PROCESS; END using_wait;
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 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;
Quad 2-input Nand Simple concurrent model of a TTL quad nand gate. --uses 1993 std VHDL library IEEE; use IEEE.Std_logic_1164.all; entity HCT00 is port(A1, B1, A2, B2, A3, B3, A4, B4 : in std_logic; Y1, Y2, Y3, Y4 : out std_logic); end HCT00; architecture VER1 of HCT00 is begin Y1 <= A1 nand B1 after 10 ns; Y2 <= A2 nand B2 after 10 ns; Y3 <= A3 nand B3 after 10 ns; Y4 <= A4 nand B4 after 10 ns; end VER1;
Dual 2-to-4 Decoder A set of conditional signal assignments model a dual 2-to-4 decoder --uses 1993 std VHDL library IEEE; use IEEE.Std_logic_1164.all; entity HCT139 is port(A2, B2, G2BAR, A1, B1, G1BAR : in std_logic; Y20, Y21, Y22, Y23, Y10, Y11, Y12, Y13 : out std_logic); end HCT139; architecture VER1 of HCT139 is begin Y10 <= '0' when (B1 = '0') and Y11 <= '0' when (B1 = '0') and Y12 <= '0' when (B1 = '1') and Y13 <= '0' when (B1 = '1') and Y20 <= '0' when (B2 = '0') and
= = = = =
= = = = =
Quad D-Type Flip-flop This example shows how a conditional signal assignment statement could be used to describe sequential logic (it is more common to use a process). The keyword 'unaffected' is equivalent to the 'null' statement in the sequential part of the language. The model would work exactly the same without the clause 'else unaffected' attached to the end of the statement. --uses 1993 std VHDL library IEEE; use IEEE.Std_logic_1164.all; entity HCT175 is port(D : in std_logic_vector(3 downto 0); Q : out std_logic_vector(3 downto 0); CLRBAR, CLK : in std_logic); end HCT175; architecture VER1 of HCT175 is begin Q <= (others => '0') when (CLRBAR = '0') else D when rising_edge(CLK) else unaffected; end VER1;
Octal Bus Transceiver This example shows the use of the high impedance literal 'Z' provided by std_logic. The aggregate '(others => 'Z')' means all of the bits of B must be forced to 'Z'. Ports A and B must be resolved for this model to work correctly (hence std_logic rather than std_ulogic). library IEEE; use IEEE.Std_logic_1164.all; entity HCT245 is port(A, B : inout std_logic_vector(7 downto 0); DIR, GBAR : in std_logic); end HCT245; architecture VER1 of HCT245 is begin A <= B when (GBAR = '0') and (DIR = '0') else (others => 'Z'); B <= A when (GBAR = '0') and (DIR = '1') else (others => 'Z'); end VER1;
Quad 2-input OR --uses 1993 std VHDL library IEEE; use IEEE.Std_logic_1164.all; entity HCT32 is port(A1, B1, A2, B2, A3, B3, A4, B4 : in std_logic; Y1, Y2, Y3, Y4 : out std_logic); end HCT32; architecture VER1 begin Y1 <= A1 or B1 Y2 <= A2 or B2 Y3 <= A3 or B3 Y4 <= A4 or B4 of HCT32 is after after after after 10 10 10 10 ns; ns; ns; ns;
8-bit Identity Comparator --uses 1993 std VHDL library IEEE; use IEEE.Std_logic_1164.all; entity HCT688 is port(Q, P : in std_logic_vector(7 downto 0); GBAR : in std_logic; PEQ : out std_logic); end HCT688; architecture VER1 of HCT688 is begin PEQ <= '0' when ((To_X01(P) = To_X01(Q)) and (GBAR = '0')) else '1'; end VER1;
Hamming Encoder A 4-bit Hamming Code encoder using concurrent assignments. The output vector is connected to the individual parity bits using an aggregate assignment. ENTITY hamenc IS PORT(datain : IN BIT_VECTOR(0 TO 3); hamout : OUT BIT_VECTOR(0 TO 7)); END hamenc; ARCHITECTURE ver2 OF hamenc IS SIGNAL p0, p1, p2, p4 : BIT; BEGIN --check bits
--d0 d1 d2 d3 --d0 d1 d2 d3 p0 p1 p2 p4
--generate check p0 <= (datain(0) p1 <= (datain(0) p2 <= (datain(0) p4 <= (datain(1)
--connect up outputs hamout(4 TO 7) <= (p0, p1, p2, p4); hamout(0 TO 3) <= datain(0 TO 3); END ver2;
Hamming Decoder This Hamming decoder accepts an 8-bit Hamming code (produced by the encoder above) and performs single error correction and double error detection. ENTITY hamdec IS PORT(hamin : IN BIT_VECTOR(0 TO 7); --d0 d1 d2 d3 p0 p1 p2 p4 dataout : OUT BIT_VECTOR(0 TO 3); --d0 d1 d2 d3 sec, ded, ne : OUT BIT); --diagnostic outputs END hamdec; ARCHITECTURE ver1 OF hamdec IS BEGIN PROCESS(hamin) VARIABLE syndrome : BIT_VECTOR(3 DOWNTO 0); BEGIN
-- parity errors
Synchronous Down Counter with Parallel Load This example shows the use of the package 'std_logic_unsigned' . The minus operator '-' is overloaded by this package, thereby allowing an integer to be subracted from a std_logic_vector. LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_unsigned.ALL; ENTITY pldcntr8 IS PORT (clk, load : IN Std_logic; datain : IN Std_logic_vector(7 DOWNTO 0); q : OUT Std_logic_vector(7 DOWNTO 0); tc : OUT Std_logic); END pldcntr8; ARCHITECTURE using_std_logic OF pldcntr8 IS SIGNAL count : Std_logic_vector(7 DOWNTO 0); BEGIN
Mod-16 Counter using JK Flip-flops Structural description of a 4-bit binary counter. The first two design entities describe a JK flip-flop and a 2-input AND gate respectively. These are then packaged together along with a signal named 'tied_high' into a package named 'jkpack'. The counter design uses the package 'jkpack', giving it access to the components and the signal declared within the package. The flip-flops and AND-gates are wired together to form a counter. Notice the use of the keyword OPEN to indicate an open-cct output port. ENTITY jkff IS PORT(clock, j, k : IN BIT; q, qbar : BUFFER BIT); END jkff; ARCHITECTURE using_process OF jkff IS BEGIN --sequential process to model JK flip-flop PROCESS --declare a local variable to hold ff state VARIABLE state : BIT := '0'; BEGIN --synchronise process to rising edge of clock WAIT UNTIL (clock'EVENT AND clock = '1'); IF (j = '1' AND k = '1') THEN state := NOT state; ELSIF (j = '0' AND k = '1') THEN state := '0'; ELSIF (j = '1' AND k = '0') THEN state := '1'; ELSE state := state; END IF; --assign values to output signals q <= state AFTER 5 ns; qbar <= NOT state AFTER 5 ns; END PROCESS; END using_process; ENTITY and_gate IS PORT(a, b : IN BIT; f : OUT BIT); END and_gate; ARCHITECTURE simple OF and_gate IS BEGIN f <= a AND b AFTER 2 ns; END simple; PACKAGE jkpack IS --toggle --reset --set --no change
Pseudo Random Bit Sequence Generator This design entity uses a single conditional signal assignment statement to describe a PRBSG register. The length of the register and the two tapping points are defined using generics. The '&' (aggregate) operator is used to form a vector comprising the shifted contents of the regsiter combined with the XOR feedback which is clocked into the register on the rising edge. --The following Design Entity defeines a parameterised Pseudo-random --bit sequence generator, it is useful for generating serial or parallel test waveforms --(for paralle waveforms you need to add an extra output port) --The generic 'length' is the length of the register minus one. --the generics 'tap1' and 'tap2' define the feedabck taps ENTITY prbsgen IS GENERIC(length : Positive := 8; tap1 : Positive := 8; tap2 : Positive := 4); PORT(clk, reset : IN Bit; prbs : OUT Bit); END prbsgen; ARCHITECTURE v2 OF prbsgen IS --create a shift register SIGNAL prreg : Bit_Vector(length DOWNTO 0); BEGIN --conditional signal assignment shifts register and feeds in xor value prreg <= (0 => '1', OTHERS => '0') WHEN reset = '1' ELSE --set all bits to '0' except lsb (prreg((length - 1) DOWNTO 0) & (prreg(tap1) XOR prreg(tap2))) --shift left with xor feedback WHEN clk'EVENT AND clk = '1' ELSE prreg; --connect msb of register to output prbs <= prreg(length); END v2;
Pelican Crossing Controller --Pelican Crossing Controller library ieee; use ieee.std_logic_1164.all; entity pelcross is port(clock, reset, pedestrian : in std_logic; red, amber, green : out std_logic); --traffic lights end pelcross; architecture v1 of pelcross is signal en, st, mt, lt, fr : std_logic; begin --timer for light sequence interval_timer : block constant stime : natural := 50; constant mtime : natural := 80; constant ltime : natural := 200; signal tcount : natural range 0 to ltime; begin process begin wait until rising_edge(clock); if (en = '0') or (tcount = ltime) then tcount <= 0; else tcount <= tcount + 1; end if; end process; st <= '1' when tcount = stime else '0'; mt <= '1' when tcount = mtime else '0'; lt <= '1' when tcount = ltime else '0'; end block; --free running timer for amber flashing free_run : block constant frtime : natural := 5; signal frcount : natural range 0 to frtime; begin process begin wait until rising_edge(clock); if frcount = frtime then frcount <= 0; else frcount <= frcount + 1; end if; end process; fr <= '1' when frcount = frtime else '0'; end block; --moore state machine to control light sequence controller : block type peltype is (res, stop, amb, amb_on, amb_off, grn, ped); signal pelstate : peltype; begin process(clock, reset) begin if reset = '1' then pelstate <= res; elsif rising_edge(clock) then case pelstate is when res => pelstate <= stop; when stop => if lt = '1' then pelstate <= amb;