Forum Discussion
17 Replies
- Altera_Forum
Honored Contributor
Without the code - it is difficult to tell what to do to fix it.
- Altera_Forum
Honored Contributor
something like this, though i tried different combinations for the 'if' condition, as in in different places or using 'elseif', yet still same result...
process(clk, rst) variable cnt : unsigned(3 downto 0) begin if rst = '0' then cnt := (others => '1'); elsif rising_edge(clk) then if load = '1' then cnt := new_data; end if; if ena = '1' then cnt := cnt +1; end if; end if; cnt_out <= cnt; end process; - Altera_Forum
Honored Contributor
thats because your enable isnt acting as an enable. cnt_out is a combinatorial version of cnt+1 or cnt, using the enable bit to select. This is because you used a variable for cnt, and then assigned it to a signal after you've done +1
The fix : remove the variable complelely and use signals instead. Switch to vhdl 2008 and replace cnt with cnt_out. Variables can cause these issues as the ordering of the code can affect the circuit. If you used only signals, then you probably wouldnt have had this problem. - Altera_Forum
Honored Contributor
thanks tricky. i tried with signals too, still same issue. may be the vhdl version. i shall try again and post what happened. does it make much difference wheatear the FF ena is used or like in this case, going to a set of muxes? i'm kinda obsessed with making logic efficient and using least as possible, but some times not sure if it a good thing to do or not.
- Altera_Forum
Honored Contributor
--- Quote Start --- thanks tricky. i tried with signals too, still same issue. may be the vhdl version. i shall try again and post what happened. does it make much difference wheatear the FF ena is used or like in this case, going to a set of muxes? i'm kinda obsessed with making logic efficient and using least as possible, but some times not sure if it a good thing to do or not. --- Quote End --- one point: you statement cnt_out <= cnt; is outside clk edge but not in sensitivity list. simulation will not work - Altera_Forum
Honored Contributor
i'll have a look kaz. i just wrote the code snippet quickly as i don’t have quartus available at the moment. shall check tonight and post.... thanks for supporting too, great people
- Altera_Forum
Honored Contributor
I fixed it :). super. this is what made me realize :
--- Quote Start --- thats because your enable isnt acting as an enable. cnt_out is a combinatorial version of cnt+1 or cnt, using the enable bit to select. --- Quote End --- it works both with signals and variables, have a look :
awesome. thanks all. greatprocess(clk, rst) variable sig_pc : unsigned(n downto 0); begin if rst = '0' then sig_pc := (others => '1'); elsif falling_edge(clk) then if pc_en = '1' then sig_pc := sig_pc +1; if pc_ld = '1' then sig_pc := unsigned(pc_new); end if; end if; end if; pc_out <= std_logic_vector(sig_pc); end process; - Altera_Forum
Honored Contributor
--- Quote Start --- thats because your enable isnt acting as an enable. cnt_out is a combinatorial version of cnt+1 or cnt, using the enable bit to select. This is because you used a variable for cnt, and then assigned it to a signal after you've done +1 The fix : remove the variable complelely and use signals instead. Switch to vhdl 2008 and replace cnt with cnt_out. Variables can cause these issues as the ordering of the code can affect the circuit. If you used only signals, then you probably wouldnt have had this problem. --- Quote End --- Tricky, I am not sure you are right here. cnt is used before assigning it to itself so a register should be generated for the variable. so enable should work and takes priority over load. I know for sure such counters work with variables. Though I personally use variables very occasionally. I think the problem may be in the last statement outside clk edge. - Altera_Forum
Honored Contributor
--- Quote Start --- Tricky, I am not sure you are right here. cnt is used before assigning it to itself so a register should be generated for the variable. so enable should work and takes priority over load. I know for sure such counters work with variables. Though I personally use variables very occasionally. I think the problem may be in the last statement outside clk edge. --- Quote End --- But the enable overrides load, because they are two separate ifs. So actually its a 3:1 priority mux with cnt+1, new_data or cnt. So while I agree I was initially wrong, and it should work with the cnt variable, the fact it's two separate ifs and not an elsif will be the problem. - Altera_Forum
Honored Contributor
--- Quote Start --- But the enable overrides load, because they are two separate ifs. So actually its a 3:1 priority mux with cnt+1, new_data or cnt. So while I agree I was initially wrong, and it should work with the cnt variable, the fact it's two separate ifs and not an elsif will be the problem. --- Quote End --- Yes that is a good point. It does not follow standard design The way I view it is as follows: --- Quote Start --- load enable cnt 0 0 no change of cnt 0 1 count up, may not get initialised 1 0 load value to cnt 1 1 count up --- Quote End ---