可以多次使用信号代替硬编码值吗?

问题描述 投票:1回答:1

我是一名学习VHDL的学生,并且有一个非常基本的问题。

我读过信号分配不会立即进行。 因此,以下操作将无法正常工作:

x <= y;
z <= not x;

因此,我知道分配不是立即进行的,也不是顺序进行的,但是我有一个关于将信号传递给实体的问题。 假设我有以下代码:

architecture struct of mips is
begin
    controller: entity work.controller port map(opD         => instrD(31 downto 26),          
                                                functD      => instrD(5 downto 0));   

    datapath:   entity work.dataPath port map(opD         => instrD(31 downto 26),                      
                                              functD      => instrD(5 downto 0));

end;

我习惯于避免代码重复和对其他语言进行硬编码,因此对上述代码中的opDfunctD值进行硬编码困扰着我。

我想知道的是是否可以将这些值分配给内部信号,如下所示:

architecture struct of mips is
    signal opD:    STD_LOGIC;
    signal functD: STD_LOGIC;
begin
    signal opD    <= instrD(31 downto 26);
    signal functD <= instrD(5 downto 0);

    controller: entity work.controller port map(opD         => opD,          
                                                functD      => functD);

    datapath:   entity work.dataPath port map(opD         => opD,                      
                                              functD      => functD);    
end;

那会按预期工作(即与上述代码块完全相同),还是会由于使用信号而导致某种“延迟”,从而使两个代码块的功能有所不同?

vhdl fpga modelsim quartus
1个回答
5
投票

我读过信号分配不会立即进行。

的确如此,但是我认为您错过了一个重要的要点,那就是要知道何时发生。 当生成信号的过程遇到等待语句或结束时(因为在过程结束时对过程敏感度列表进行隐式等待),将更新信号。

因此,如果将您的示例放在一个定时的过程中,它不会像您期望的那样工作,但是在具有正确灵敏度列表的组合过程中,它是完全有效的。

architecture rtl of example is
    signal y_r : std_logic;
    signal z_r : std_logic;
    signal y   : std_logic;
    signal z   : std_logic;
 begin
    y <= x; -- immediately updated when x changes
    z <= not y; -- immediately updated when y changes, equivalent to z <= not x

    process(clk)
    begin
        if rising_edge(clk) then
            y_r <= x; -- y is updated when the clock rise, once this process finishes
            z_r <= not y_r; -- y still have the value it had when the process started executing
         end if;
    end process;
end architecture rtl;

因此,除了语法错误之外,您的最后一个示例将按预期工作。 不过,有一种简洁的语法对此更好,恕我直言:

architecture struct of mips is
    alias opD is instrD(31 downto 26);
    alias functD is instrD(5 downto 0);
begin

    controller: entity work.controller port map(opD         => opD,          
                                                functD      => functD   

    datapath:   entity work.dataPath port map(opD         => opD,                      
                                              functD      => functD;     
end;
© www.soinside.com 2019 - 2024. All rights reserved.