我对VHDL非常陌生,作为大学项目的一部分,我正在尝试创建具有四个状态的状态机。我写了一些VHDL代码和一个测试台,但是我还不能确认状态机是否可以正常工作,但是在解决了当前问题之后,我将对此进行验证:
状态机的一部分是一个无符号计数器,该计数器应确定是将数据提取到高字节还是低字节。在我的测试平台中,我只想确认此计数器是否全部正常工作。现在,在用0初始化之后,当计数过程第一次开始时,我收到一个未定义的值作为计数器值。
起初,我尝试将计数器实现为std_logic_vector,但我明白了,为什么这不起作用(没有强制转换)。因此,我切换到未签名状态,并期望计数器正常工作。 通过对单个进程进行注释,我发现,计数进程在进程“更改状态”和“ change_current_state”中产生了问题。但是我无法弄清楚为什么这些过程会相互干扰。 在我的测试台中,如果-语句“ change_current_state”从不到达“ RESET”,所以它无法为计数器分配值。另外,进程“ change_status”永远不会到达状态为“ prepareData”的elsif-语句。这是唯一为计数器分配值的语句。
VHDL代码:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity readFromADC is
Port
(
CLK_IN : in std_logic;
RESET_IN_N : in std_logic;
BUSY_IN : in std_logic;
DATA_IN : in std_logic_vector(7 downto 0);
ACK_ERROR_IN : in std_logic;
ENABLE_I2C : out std_logic;
ADDR_I2C : out std_logic_vector(6 downto 0);
RW_I2C : out std_logic;
DATA_WR_I2C : out std_logic_vector(7 downto 0);
DATA_OUT : out std_logic_vector(15 downto 0);
SHIFT_OUT_RDY : out std_logic;
SHOWSTATE : out std_logic_vector (1 downto 0); -- debugging
BYTECOUNTER: out unsigned (1 downto 0)
);
end readFromADC;
architecture readFromADC_arch of readFromADC is
constant const_data_wr : std_logic_vector(7 downto 0) := "00000000";
constant const_rw_I2C : std_logic := '1';
constant const_addr_I2C : std_logic_vector(6 downto 0) := "0101000";
constant const_da2_control : std_logic_vector (3 downto 0) := "0000";
type states is
(
idle,
read,
prepareData,
shiftOut
);
signal sign_clk : std_logic;
signal sign_HighByte : std_logic_vector(7 downto 0);
signal sign_LowByte : std_logic_vector(7 downto 0);
signal current_state : states;
signal next_state : states;
signal sign_data_out : std_logic_vector(15 downto 0);
signal sign_ena_i2c : std_logic := '0';
signal sign_count_bytes_uns : unsigned (1 downto 0) := "00";
signal prepare_data_rdy : std_logic := '0';
signal sign_shift_out_rdy : std_logic := '0';
signal debug_states : std_logic_vector (1 downto 0);
begin
--Zuweisungen
sign_clk <= CLK_IN;
ENABLE_I2C <= sign_ena_i2c;
ADDR_I2C <= const_addr_I2C;
RW_I2C <= const_rw_I2C;
DATA_WR_I2C <= const_data_wr;
DATA_OUT <= sign_data_out;
SHIFT_OUT_RDY <= sign_shift_out_rdy;
SHOWSTATE <= debug_states;
BYTECOUNTER <= sign_count_bytes_uns;
--nächsten Status zuweisen
change_current_state : process (sign_clk, RESET_IN_N)
begin
if rising_edge(sign_clk) then
if (RESET_IN_N = '0') then
current_state <= idle;
sign_shift_out_rdy <= '0';
sign_data_out <= (others => '0');
sign_count_bytes_uns <= "00";
else
current_state <= next_state;
end if;
end if;
end process change_current_state;
--Status ändern
change_status : process (current_state)
begin
if current_state = idle then
debug_states <= "00";
sign_shift_out_rdy <= '0';
prepare_data_rdy <= '0';
sign_ena_i2c <= '1';
elsif current_state = read then
debug_states <= "01";
-- sign_count_bytes <= sign_count_bytes + 1;
sign_shift_out_rdy <= '0';
elsif current_state = prepareData then
debug_states <= "10";
sign_ena_i2c <= '0';
-- sign_count_bytes <= "00";
elsif current_state = shiftOut then
sign_count_bytes_uns <= "00";
debug_states <= "11";
sign_shift_out_rdy <= '1';
end if;
end process change_status;
acquireData : process (sign_count_bytes_uns, current_state)
begin
if current_state = read then
if (sign_count_bytes_uns = 1) then
sign_HighByte <= DATA_IN;
elsif (sign_count_bytes_uns = 2) then
sign_LowByte <= DATA_IN;
end if;
end if;
end process acquireData;
gatherData : process (current_state)
begin
if current_state = prepareData then
sign_data_out <= const_da2_control & sign_HighByte(3 downto 0) & sign_LowByte;
prepare_data_rdy <= '1';
end if;
end process gatherData;
--Dekodiere den nächsten Status
next_state_decode: process (current_state, BUSY_IN, ACK_ERROR_IN, sign_count_bytes_uns, prepare_data_rdy, sign_shift_out_rdy)
begin
next_state <= current_state; --Default: behalte den aktuellen Status
case (current_state) is
when Idle =>
if (BUSY_IN = '0') AND (ACK_ERROR_IN = '0') then
next_state <= read;
end if;
when read =>
if sign_count_bytes_uns >= 2 then
-- if sign_count_bytes = "11" then
next_state <= prepareData;
end if;
when prepareData =>
if prepare_data_rdy = '1' then
next_state <= shiftOut;
end if;
when shiftOut =>
if sign_shift_out_rdy = '1' then
next_state <= Idle;
end if;
end case;
end process next_state_decode;
countBusy : process (BUSY_IN)
begin
if falling_edge(BUSY_IN) then
sign_count_bytes_uns <= sign_count_bytes_uns + 1;
-- if (sign_count_bytes_uns <= 2) then
-- sign_count_bytes <= std_logic_vector (sign_count_bytes_uns);
if (sign_count_bytes_uns > 2) then
sign_count_bytes_uns <= "00";
end if;
end if;
end process countBusy;
测试台
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity sim_read_adc is
-- Port ( );
end sim_read_adc;
architecture sim_read_adc_arch of sim_read_adc is
component readFromADC is
Port
(
CLK_IN : in std_logic;
RESET_IN_N : in std_logic;
BUSY_IN : in std_logic;
DATA_IN : in std_logic_vector(7 downto 0);
ACK_ERROR_IN : in std_logic;
ENABLE_I2C : out std_logic;
ADDR_I2C : out std_logic_vector(6 downto 0);
RW_I2C : out std_logic;
DATA_WR_I2C : out std_logic_vector(7 downto 0);
DATA_OUT : out std_logic_vector(15 downto 0);
SHIFT_OUT_RDY : out std_logic;
SHOWSTATE : out std_logic_vector (1 downto 0);
BYTECOUNTER: out unsigned (1 downto 0)
);
end component readFromADC;
signal sim_CLK_IN : std_logic;
signal sim_RESET_IN_N : std_logic := '1';
signal sim_BUSY_IN : std_logic := '1';
signal sim_DATA_IN : std_logic_vector(7 downto 0) := "10101010";
signal sim_ACK_ERROR_IN : std_logic := '0';
signal sim_ENABLE_I2C : std_logic;
signal sim_ADDR_I2C : std_logic_vector(6 downto 0);
signal sim_RW_I2C : std_logic;
signal sim_DATA_WR_I2C : std_logic_vector(7 downto 0);
signal sim_DATA_OUT : std_logic_vector(15 downto 0);
signal sim_SHIFT_OUT_RDY : std_logic;
signal sim_SHOWSTATE : std_logic_vector (1 downto 0);
signal sim_BYTECOUNTER : unsigned (1 downto 0);
begin
dut : readFromADC
port map
(
CLK_IN => sim_CLK_IN,
RESET_IN_N => sim_RESET_IN_N,
BUSY_IN => sim_BUSY_IN,
DATA_IN => sim_DATA_IN,
ACK_ERROR_IN => sim_ACK_ERROR_IN,
ENABLE_I2C => sim_ENABLE_I2C,
ADDR_I2C => sim_ADDR_I2C,
RW_I2C => sim_RW_I2C,
DATA_WR_I2C => sim_DATA_WR_I2C,
DATA_OUT => sim_DATA_OUT,
SHIFT_OUT_RDY => sim_SHIFT_OUT_RDY,
SHOWSTATE => sim_SHOWSTATE,
BYTECOUNTER => sim_BYTECOUNTER
);
clk_gen : process
begin
sim_CLK_IN <= '0';
wait for 0.5us;
sim_CLK_IN <= '1';
wait for 0.5us;
end process clk_gen;
generate_busy : process
begin
wait for 5us;
sim_BUSY_IN <= '0';
wait for 1us;
sim_BUSY_IN <= '1';
end process generate_busy;
end sim_read_adc_arch;
模拟(未注释任何过程): Simulation(no process is commented out)
*编辑:更新的VHDL-代码(不必更改测试台):
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity readFromADC is
Port
(
CLK_IN : in std_logic;
RESET_IN_N : in std_logic;
BUSY_IN : in std_logic;
DATA_IN : in std_logic_vector(7 downto 0);
ACK_ERROR_IN : in std_logic;
ENABLE_I2C : out std_logic;
ADDR_I2C : out std_logic_vector(6 downto 0);
RW_I2C : out std_logic;
DATA_WR_I2C : out std_logic_vector(7 downto 0);
DATA_OUT : out std_logic_vector(15 downto 0);
SHIFT_OUT_RDY : out std_logic;
SHOWSTATE : out std_logic_vector (1 downto 0); -- debugging
BYTECOUNTER: out unsigned (1 downto 0)
);
end readFromADC;
architecture readFromADC_arch of readFromADC is
constant const_data_wr : std_logic_vector(7 downto 0) := "00000000";
constant const_rw_I2C : std_logic := '1';
constant const_addr_I2C : std_logic_vector(6 downto 0) := "0101000";
constant const_da2_control : std_logic_vector (3 downto 0) := "0000";
type states is
(
idle,
read,
prepareData,
shiftOut
);
signal sign_clk : std_logic;
signal sign_HighByte : std_logic_vector(7 downto 0);
signal sign_LowByte : std_logic_vector(7 downto 0);
signal current_state : states;
signal next_state : states;
signal sign_data_out : std_logic_vector(15 downto 0);
signal sign_ena_i2c : std_logic := '0';
signal sign_count_bytes_uns : unsigned (1 downto 0) := "00";
signal prepare_data_rdy : std_logic := '0';
signal sign_shift_out_rdy : std_logic := '0';
signal debug_states : std_logic_vector (1 downto 0);
begin
--Zuweisungen
sign_clk <= CLK_IN;
ENABLE_I2C <= sign_ena_i2c;
ADDR_I2C <= const_addr_I2C;
RW_I2C <= const_rw_I2C;
DATA_WR_I2C <= const_data_wr;
DATA_OUT <= sign_data_out;
SHIFT_OUT_RDY <= sign_shift_out_rdy;
SHOWSTATE <= debug_states;
BYTECOUNTER <= sign_count_bytes_uns;
--nächsten Status zuweisen
change_current_state : process (sign_clk, RESET_IN_N)
begin
if rising_edge(sign_clk) then
-- if (RESET_IN_N = '0') then
-- current_state <= idle;
-- sign_shift_out_rdy <= '0';
-- sign_data_out <= (others => '0');
-- sign_count_bytes_uns <= "00";
-- else
current_state <= next_state;
-- end if;
end if;
end process change_current_state;
--Status ändern
change_status : process (current_state)
begin
if current_state = idle then
debug_states <= "00";
sign_shift_out_rdy <= '0';
prepare_data_rdy <= '0';
sign_ena_i2c <= '1';
elsif current_state = read then
debug_states <= "01";
sign_shift_out_rdy <= '0';
elsif current_state = prepareData then
debug_states <= "10";
sign_ena_i2c <= '0';
elsif current_state = shiftOut then
debug_states <= "11";
sign_shift_out_rdy <= '1';
end if;
end process change_status;
acquireData : process (sign_count_bytes_uns, current_state)
begin
if current_state = read then
if (sign_count_bytes_uns = 1) then
sign_HighByte <= DATA_IN;
elsif (sign_count_bytes_uns = 2) then
sign_LowByte <= DATA_IN;
end if;
end if;
end process acquireData;
gatherData : process (current_state)
begin
if current_state = prepareData then
sign_data_out <= const_da2_control & sign_HighByte(3 downto 0) & sign_LowByte;
prepare_data_rdy <= '1';
end if;
end process gatherData;
--Dekodiere den nächsten Status
next_state_decode: process (current_state, BUSY_IN, ACK_ERROR_IN, sign_count_bytes_uns, prepare_data_rdy, sign_shift_out_rdy)
begin
next_state <= current_state; --Default: behalte den aktuellen Status
case (current_state) is
when Idle =>
if (BUSY_IN = '0') AND (ACK_ERROR_IN = '0') then
next_state <= read;
end if;
when read =>
if sign_count_bytes_uns >= 2 then
-- if sign_count_bytes = "11" then
next_state <= prepareData;
end if;
when prepareData =>
if prepare_data_rdy = '1' then
next_state <= shiftOut;
end if;
when shiftOut =>
if sign_shift_out_rdy = '1' then
next_state <= Idle;
end if;
end case;
end process next_state_decode;
countBusy : process (BUSY_IN, RESET_IN_N)
begin
if falling_edge (RESET_IN_N) then
sign_count_bytes_uns <= "00";
elsif falling_edge(BUSY_IN) then
sign_count_bytes_uns <= sign_count_bytes_uns + 1;
if (sign_count_bytes_uns > 2) then
sign_count_bytes_uns <= "00";
end if;
end if;
end process countBusy;
end readFromADC_arch;
答案 0 :(得分:0)
首先,在仿真中得到一些“ X”通常意味着将信号同时分配给2个(或更多)值。这通常意味着您需要在多个并行过程中分配此信号。
在VHDL中,最好避免在2个不同的进程中分配信号(请注意,任何进程之外的行都是一个进程)。
在您的代码中,信号 sign_count_bytes_uns 在3个过程中受到影响。我认为您应该至少修改其中两个(通过合并)。
但是,在此过程中,“ X”的真正原因有些棘手:
change_status : process (current_state)
begin
if current_state = idle then
debug_states <= "00";
sign_shift_out_rdy <= '0';
prepare_data_rdy <= '0';
sign_ena_i2c <= '1';
elsif current_state = read then
debug_states <= "01";
sign_shift_out_rdy <= '0';
elsif current_state = prepareData then
debug_states <= "10";
sign_ena_i2c <= '0';
elsif current_state = shiftOut then
sign_count_bytes_uns <= "00";
debug_states <= "11";
sign_shift_out_rdy <= '1';
end if;
end process change_status;
您仅在1种情况下分配了 sign_count_bytes_uns ,而我假设您不在这种情况下,但是您给我们提供的模拟屏幕截图中, sign_count_bytes_uns 仍为“ XX”。
原因如下: 当您在过程中分配信号但并非在所有情况下都分配信号时,每次您都不在信号受影响的情况下,该信号将采用其先前的值(内存)。综合而言,将推断出一个锁存器。因此,当 countBusy 尝试更改它时,您的进程 change_status 会将 sign_count_bytes_uns 保持为“ 00”,这就是为什么您得到一些“ XX”的原因。