VHDL中Signal与Variable的FPGA硬件映射及验证问询
VHDL中Signal与Variable的硬件映射疑问与验证
课本中的Signal与Variable差异
variable :=在进程内立即更新signal <=经过delta周期后更新- variable为局部变量,signal为全局信号
认知冲突:软件特性 vs 硬件实现
主管工程师观点:
“硬件中variable和signal完全相同,都会因触发器存在延迟一个时钟周期,因此用variable做边沿检测是错误的。”
我的认知:是否生成寄存器取决于是否跨时钟周期保存状态,而非variable或signal类型。
背景:SPI主控制器中的边沿检测实现
process (CLK, RSTB) -- edge detection variables variable prev_sclk_v : std_logic := '0'; variable next_sclk_v : std_logic; variable sclk_rise_v : std_logic; variable sclk_fall_v : std_logic; begin if RSTB = '0' then sclk_r <= '0'; sclk_div_cnt <= 0; prev_sclk_v := '0'; ... elsif rising_edge(CLK) then done_r <= '0'; sclk_rise_v := '0'; sclk_fall_v := '0'; -- SCLK next value (divider logic, 40MHz -> 10MHz) next_sclk_v := sclk_r; if busy_r = '1' then if sclk_div_cnt = 1 then sclk_div_cnt <= 0; next_sclk_v := not sclk_r; else sclk_div_cnt <= sclk_div_cnt + 1; end if; else sclk_div_cnt <= 0; next_sclk_v := '0'; -- idle: SCLK low (mode 0) end if; -- edge detection using variables (same-cycle valid) if (prev_sclk_v = '0' and next_sclk_v = '1') then sclk_rise_v := '1'; elsif (prev_sclk_v = '1' and next_sclk_v = '0') then sclk_fall_v := '1'; end if; -- store for next cycle prev_sclk_v := next_sclk_v; sclk_r <= next_sclk_v; -- FSM example: use edge flags in the same clock case state_r is when send_header => if sclk_fall_v = '1' then -- shift MSB first, etc... end if; when receive_byte_a => if sclk_rise_v = '1' then -- sample SDIO on SCLK rising edge end if; ... end case; end if; end process;
我的硬件映射理解
sclk_r(时钟进程内赋值的signal)→ 触发器(FF)prev_sclk_v(跨周期保存状态的variable)→ 触发器(FF)next_sclk_v、sclk_rise_v、sclk_fall_v(仅单周期使用的variable)→ 纯组合逻辑
问题:FPGA综合实现视角的验证
1) 以下表述是否正确?
仅当variable需跨时钟周期保存状态时才会推断出触发器;若仅作为单周期中间值,则综合为纯组合逻辑,而非寄存器。
2) 上述代码中的硬件映射是否准确?
3) 以下总结是否准确?
“variable vs signal”并非硬件层面的核心区别,真正区别是组合逻辑与时序逻辑(状态)。variable仅在表示跨周期状态时成为寄存器,否则为纯组合逻辑;时钟进程内赋值的signal通常用于保存状态(触发器)。
4) 若我的理解正确,如何向持“硬件中variable和signal行为一致,均延迟一个时钟周期”观点的工程师清晰解释:为何此场景下用variable实现的边沿检测标志是组合逻辑,转为signal则会引入单周期延迟?若我的理解有误,也请予以纠正。
内容的提问来源于stack exchange,提问作者Hyung Jin Song
相关产品推荐
相关产品推荐

