I have the following VHDL code - its for simulating an fsm, finite state machine. the…
- User
I have the following VHDL code - its for simulating an fsm, finite state machine. the code seems to behave fine, however when simulating i seem to have an issue with my output - the data out vector, which does not change value, and i want to be able to inspect its state at any given time. what could be a possible reason for that? LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.numeric_std.ALL; use ieee.std_logic_unsigned.all; use STD.textio.all;
entity key_fsm_c is port (clk: in std_logic; rst: in std_logic; left, right, up, down, center: in std_logic; data_out: out std_logic_vector(31 downto 0):= (others => '0'); cntr_en: out std_logic; cntr_rst: out std_logic; cntr_load: out std_logic; edit_en_out: out std_logic ); end entity;
architecture Behavioral of key_fsm_c is type state_type is (idle, start, stop, reset, load, edit, inc_val, dec_val, inc_pos, dec_pos); signal c_state, n_state: state_type;
-- Helper procedures for position and value increment and decrement operation procedure proc_dec_pos(old_pos: in integer; position: out integer; tmp: inout std_logic_vector; act_num: out integer; act_frag: out std_logic_vector) is begin if old_pos >= 1 then position := old_pos - 1; else position := 7; end if; act_frag := tmp(4*(position+1)-1 downto 4*position); act_num := to_integer(unsigned(act_frag)); end procedure; procedure proc_inc_pos(old_pos: in integer; position: out integer; tmp: inout std_logic_vector; act_num: out integer; act_frag: out std_logic_vector) is begin if old_pos <= 6 then position := old_pos + 1; else position := 0; end if; act_frag := tmp(4*(position+1)-1 downto 4*position); act_num := to_integer(unsigned(act_frag)); end procedure; procedure proc_dec_val(old_num: in integer; position: out integer; tmp: inout std_logic_vector; act_num: out integer; act_frag: out std_logic_vector) is begin if old_num >= 1 then act_num := old_num - 1; else act_num := 9; end if; act_frag := std_logic_vector(to_unsigned(act_num, 4)); tmp(4*(position+1)-1 downto 4*position) := act_frag; end procedure; procedure proc_inc_val(old_num: in integer; position: out integer; tmp: inout std_logic_vector; act_num: out integer; act_frag: out std_logic_vector) is begin if old_num <= 8 then act_num := old_num + 1; else act_num := 0; end if; act_frag := std_logic_vector(to_unsigned(act_num, 4)); tmp(4*(position+1)-1 downto 4*position) := act_frag; end procedure;begin -- FSM state transition process proc_state_transition: process (rst, clk) begin if rst = '1' then c_state <= idle; elsif rising_edge(clk) then c_state <= n_state; end if; end process;
-- FSM output logic process proc_output_logic: process (c_state, left, right, up, down, center) variable position: integer := 0; variable act_num: integer := 0; variable old_pos: integer := 0; variable old_num: integer := 0; variable is_begin: integer := 1; variable act_frag: std_logic_vector(3 downto 0); variable tmp: std_logic_vector(31 downto 0) := (others => '0'); variable edit_pending: boolean := false; begin -- Initializing control signals cntr_en <= '0'; cntr_rst <= '0'; cntr_load <= '0'; edit_en_out <= '0'; case c_state is when idle => n_state <= idle; if right='1' then n_state <= start; elsif left = '1' then n_state <= stop; elsif down = '1' then n_state <= reset; elsif up = '1' then n_state <= load; elsif center = '1' then n_state <= edit; edit_pending := true; end if; when start => cntr_en <= '1'; n_state <= idle; when stop => cntr_en <= '0'; n_state <= idle; when reset => cntr_rst <= '1'; n_state <= idle; when load => cntr_load <= '1'; edit_en_out <= '0'; n_state <= idle; when edit => n_state <= edit; edit_en_out <= '1'; if right='1' and not edit_pending then n_state <= dec_pos; elsif left='1' and not edit_pending then n_state <= inc_pos; elsif down='1' and not edit_pending then n_state <= dec_val; elsif up='1' and not edit_pending then n_state <= inc_val; elsif center='1' and not edit_pending then n_state <= load; end if; edit_pending := false; when dec_pos => proc_dec_pos(old_pos, position, tmp, act_num, act_frag); n_state <= edit; when inc_pos => proc_inc_pos(old_pos, position, tmp, act_num, act_frag); n_state <= edit; when dec_val => proc_dec_val(old_num, position, tmp, act_num, act_frag); n_state <= edit; when inc_val => proc_inc_val(old_num, position, tmp, act_num, act_frag); n_state <= edit; end case; data_out <= tmp; end process; - Assistant
Based on the code you provided, there are a few potential reasons for the
data_outsignal not changing value. Here are some areas you can investigate further:-
Initialization issue: Make sure that the
data_outsignal is properly initialized in your testbench. You can initialize it in the same way as you initialized thetmpvector in the FSM, using(others => '0'). -
Combination of control signals: Check if the conditions where
data_outis supposed to change meet the combination of input signals that you are simulating in your testbench. Make sure to account for the sensitivity list of theproc_output_logicprocess. -
Issue with blocking assignments: The
data_outsignal is assigned within theproc_output_logicprocess with a blocking assignment (). It might be worth trying to move the assignment outside the process, e.g., with a concurrent assignment, like so:
-
- User
i know my testbench works correct, because an earlier incarnation of my code worked fine. also, i cant just move the assignment outside the process due to the fact the tmp variable is not available. all other signals are fine
- Assistant
Since you mentioned that your testbench works correctly and the earlier version of your code was working fine, there might have been an issue introduced while updating the code. As you mentioned that you cannot move the assignment of
data_outout of the process, we need to focus on other possible reasons.Given that
tmpis the only signal drivingdata_out, you can narrow down the issue as follows:- Check if the
tmpsignal value is changing as you expect. You can add a report statement in your process_code to report the value oftmpduring simulation. Add the following code just before the assignment todata_out.
report "tmp = " & to_string(tmp); - Check if the
- User
i am entering states correctly, as well as my procedures, however old_pos seems to be not what its supposed to be. tmp is changing, but not to the values the testbench is expecting
- Assistant
Based on your observations, it seems the issue might be related to the
old_posvariable not being updated correctly. In your provided code, you use thepositionvariable to hold the new position and pass theold_posvariable to the procedures. However, you never update theold_posvariable after calling each procedure. Therefore, it retains its initial value of 0 during all the iterations. To fix this, you should update theold_posvariable after calling the procedures.Here's an example of how to update the
old_posvariable after calling theproc_inc_posprocedure:when inc_pos => proc_inc_pos(old_pos, position, tmp, act_num, act_frag); old_pos := position; -- Add this line to update old_pos n_state <= edit;Do the same for the
dec_posstate as well:when dec_pos => proc_dec_pos(old_pos, position, tmp, act_num, act_frag); old_pos := position; -- Add this line to update old_pos n_state <= edit;After updating the
old_posvariable in both states, re-run your simulation, and you should see the correct values being assigned to . - User
- Assistant
I have reviewed your original code and noticed a subtle difference between your original version and the updated one that you provided earlier. This might be causing the issue that you're facing in the updated version. Here is the main difference that could be affecting the functionality:
In the original code, the register is updated in an asynchronous manner due to the presence of the separate process:
- User
my original code was asynchronous. and in my new code, there is already separation
- Assistant
Apologies for the confusion earlier. Upon re-evaluation of your updated code, I observed that you have indeed separated the processes while maintaining a synchronous behavior. The
proc_state_transitionprocess updates the current state register,c_state, based on the clock and reset signals, and the output-related processes are based on the current state and input signals.The issue with the
data_outvector behavior might still be originating from the incorrect update of the position or other variables in the FSM process. So, to investigate further, I would recommend adding more report statements during the simulation to keep track of important variables and their values.To do this, add report statements inside the procedures and process:
- User
- Assistant
- User
- Assistant
Your modified code appears to be considerably improved from the original version. However, there is still one minor issue that needs to be corrected before it fully adheres to good VHDL practices.
In the
proc_fsmprocess, you have assignedcntr_en <= '0';outside of any condition. This means thatcntr_enwill always be assigned '0', which is not the intended behavior. To fix this, you should move thecntr_en <= '0';assignment under theidlestate or reset conditions to ensure it is properly initiated.For example, you can modify your code as follows:
-- FSM process proc_fsm: process (clk, rst) -- Variable declarations begin if rst = '1' then c_state <= idle; cntr_en <= '0'; -- Initialization for cntr_en moved to reset condition elsif rising_edge(clk) then n_state <= idle; -- Default next state -- FSM logic end if; end process;With this modification, your code should now be functionally correct and adhere to good VHDL practices. Make sure to test the design with a suitable testbench and verify its functionality to ensure everything works as expected.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.