Forum Discussion
Signal not assigned in init state
Hello,
I am working on a simple state machine but for some reason the output value is not assigned correctly. First it is supposed to go into state init and assign default values to my 4x8bit internal signals (key_int), then should go to state assign to assign and combine the internal signals into 1x32bit output signal (key). And that is where it fails. When I run my test bench it always shows that key="00000000000000000000000000000000". Although key_int(0..3) are set to x"61" during the init phase. I already placed a counter in both states so I don't get a timing problem. I also replace the array with 4x std_logic_vector signals and assiged those during init with the same result. Surpringly: when I assign key directly in init it takes it. What am I doing wrong here? Btw I am using Quartus II 15.0.0. if that makes any difference.use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity buttonToKey is port(
clk_50 : in std_logic;
button : in std_logic_vector(5 downto 1);
key: out std_logic_vector(31 downto 0)
);
end buttonToKey;
architecture behavior of buttonToKey is
type state_type is ( init, evaluate, assign );
signal state : state_type := init;
type my_array_type is array (0 to 3) of std_logic_vector(7 downto 0);
signal key_int : my_array_type;
signal index : integer := 0;
signal count : unsigned( 18 downto 0 ) := ( others => '0' );
begin
process( clk_50 )
begin
if rising_edge( clk_50 ) then
if state = init AND count = 100 then -- init and assign default char
state <= assign;
key_int( 3 ) <= x"61"; -- ASCII 'a'
key_int( 2 ) <= x"61";
key_int( 1 ) <= x"61";
key_int( 0 ) <= x"61";
count <= ( others => '0' );
elsif state = evaluate then -- check which button is pressed
if button = "10000" then -- button 5: increment ASCII character
state <= assign;
count <= ( others => '0' );
if unsigned( key_int( index ) ) = x"7A" then -- run circular between ASCII char 'a' and 'z'
key_int( index ) <= x"61";
else
key_int( index ) <= key_int( index ) + 1;
end if;
elsif button = "00001" then -- button 1: move to next position (start with 1)
state <= evaluate;
if index = 3 then
index <= 0; -- overflow protection
else
index <= index + 1;
end if;
else
state <= evaluate;
end if;
elsif state = assign AND count = 100 then -- assign any changed keys to output
count <= ( others => '0' );
state <= evaluate;
key <= key_int( 3 ) & key_int( 2 ) & key_int( 1 ) & key_int( 0 );
else
count <= count + 1;
end if;
end if;
end process;
end behavior;
Testbench: -- i took most of the test code out since the first transition from init to assign is not working
BEGIN
-- code that executes only once
button <= "00000";
wait for 80 ns;
button <= "00000";
wait for 80 ns;
button <= "00000";
wait for 80 ns;
button <= "00000";
wait for 80 ns;
WAIT;
END PROCESS init;
always : PROCESS
-- optional sensitivity list
-- ( )
-- variable declarations
BEGIN
-- code executes for every event on sensitivity list
WAIT;
END PROCESS always;
-- clock generation process
process
begin
clk_50 <= '1';
wait for 10 ns;
clk_50 <= '0';
wait for 10 ns;
end process;
END buttonToKey_arch; Cheers and thanks!16 Replies
- Altera_Forum
Honored Contributor
Without the testbench, we cannot see whats going wrong.
I also note you're using buttons in the design. Have you properly synchronised and debounced them? - Altera_Forum
Honored Contributor
--- Quote Start --- Without the testbench, we cannot see whats going wrong. --- Quote End --- Hello Tricky, that is the actual test bench that is in use at the moment. It should start the state machine, go into theinit state with the 101st rising edge and assign key_int( 3..1 ) <= x"61"; . Then it should go to assign state after another 101 rising edges to combine the key_int(3..1) into the output key. The problem I am seeing is that key is always 0. So either it does not go into the init state, or it does not assign key_int or it never reaches the assign state. And I have trouble determining whether it is my code, something Quartus II specific or something completely different. The reason why I wrote that "I took most of the code out" is that I reduced the test bench to what you see here to find the error. The test bench is pretty dumb at the moment, but should do a very simple thing..but it is not doing it. --- Quote Start --- I also note you're using buttons in the design. Have you properly synchronised and debounced them? --- Quote End --- Yes, those are debounced in a separate module that feeds this buttontokey module. But this should not matter for the test bench, does it? Thanks. ninja edit: changed wording - Altera_Forum
Honored Contributor
Are you running the test for long enough? it will take 200 clocks for output "key" to be assigned to anything.
- Altera_Forum
Honored Contributor
--- Quote Start --- Are you running the test for long enough? it will take 200 clocks for output "key" to be assigned to anything. --- Quote End --- I am running it for 4200 ns. The clock input is 50MHz or 20 ns periods. So 4200 ns should be sufficient. This is the output of ModelSim: http://www.alteraforum.com/forum/attachment.php?attachmentid=13398&stc=1 From your question I gather that the general code looks fine and should work? Thank you. - Altera_Forum
Honored Contributor
Have you check that the counter and state are actually changing?
- Altera_Forum
Honored Contributor
--- Quote Start --- Have you check that the counter and state are actually changing? --- Quote End --- Well, I have not checked the counter. I noticed however that when key is set in state init and not in state assign the value shows up in simulation. But when key is assigned in state init AND afterwards in state assign only the last value shows up in simulation..it is like it skipped the init state in that case. - Altera_Forum
Honored Contributor
Is there a way to monitor internal signals with ModelSim? I found this link which suggest it is possible (http://www.alteraforum.com/forum/showthread.php?t=47850&highlight=internal+signal+modelsim). But I could not find a description on how to do it.
Thanks. - Altera_Forum
Honored Contributor
If you're doing an RTL simulation, all internals can be put on the waveform. Click on the sim tab, select the module you want to look at then click on the objects tab - all internal signals should be listed.
- Altera_Forum
Honored Contributor
Found it. It shows as "Not Logged" in my case. And as "No Data" when added to the wave form.
- Altera_Forum
Honored Contributor
Yes, because it wasnt on the wave window when started, modelsim wont log it. Just restart the simulation and run it again. Easiest way is from modelsim console:
restart -f run 1 us