Forum Discussion
Altera_Forum
Honored Contributor
16 years agoNeed help creating a simple 32bit counter
I'm trying to learn VHDL so I thought I'd try converting the "my_first_fpga" example from verilog to VHDL (how complicated can adding one to a number be?), three hours later, lots of googling and searching through books has yielded no result. Sorry if this is the wrong place to ask (if so where is a better place?)
So I run this code through the simulator, apply a 50Mhz clock to CLK and get nothing from the counter bus. to make sure the process is running I output CLK to OUT, if I hard code a value ie X"FFFF0000" to counter it outputs that value but counter := counter +1 gets me no where. Also is it possible to step through code as with C? Here is my code:-- import std_logic from the IEEE library
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
-- this is the entity
entity counter_example is
port (
CLK : in std_logic;
OUT1: out std_logic;
Q : out std_logic_vector(31 downto 0));
end counter_example;
architecture counter_code of counter_example is
begin
process (CLK)
variable counter : std_logic_vector(31 downto 0);
begin
counter := counter + 1;
OUT1 <= CLK; -- Sanity Check that I am actually outputting something
Q <= counter;
end process;
end counter_code;Thanks.13 Replies
- Altera_Forum
Honored Contributor
Your process that generates the clock is also triggered by the clock, which won't work. It's a bit like the chicken-and-egg problem ;)
You should have two processes, one to generate the clock, and another one that triggers on it:
It is also a good idea to add a reset signal to give your counter (and clock) an initial value. If you don't do that counter_out will start with UUUUU (uninitialised) in the simulator and will become XXXXX (unknown) at the first clock cycle.Clockgen: process begin clock <= not clock after 10 ns; --will give 50MHz clock end process; Sim: process (clock) begin if rising_edge(clock) then counter_out <= counter_out + 1; end if; end process; - Altera_Forum
Honored Contributor
--- Quote Start --- Your process that generates the clock is also triggered by the clock, which won't work. It's a bit like the chicken-and-egg problem ;) You should have two processes, one to generate the clock, and another one that triggers on it:
--- Quote End --- This does not have to be inside a process, as it is a simple 1 line statement. Also, remember do not put this kind of code in your synthesisable code. Keep them separate as a testbench and a counter. If you put this code in and tried to synthesise it you'll some odd things getting generated.Clockgen: process begin clock <= not clock after 10 ns; --will give 50MHz clock end process; - Altera_Forum
Honored Contributor
Yes, I mixed up with the way I usually do a clock, which is either
orClockgen: process begin wait for 10ns; clock <= not clock; end process;
The latter is longer but doesn't require to initialize the clock valueClockgen: process begin clock <= '0'; wait for 10ns; clock <= '1'; wait for 10ns; end process;