FPGA|CPLD|ASIC论坛
登录
直播中
h1654155275.5916
7年用户
992经验值
私信
关注
[经验]
FPGA中for循环的理解与运用
FPGA
for循环
HDL
For循环是任何HDL语言中最让人容易误解的代码之一。For循环可以以可综合与不可综合两种版本实现。不过C语言中的For循环与HDL众的For循环不太一样,要想熟练运用的话就得先理解它。
将软件编程风格的For循环代码改成HDL代码
For循环是新手硬件开发人员常常困扰的领域。您可能已经在C里运行了无数次,因此您认为它们在Verilog和VHDL中是相同的。此处要明确指明:For循环在硬件语言与软件语言中是不同的。
在可综合代码中For循环可以用来拓展复制逻辑。在你完全理解复制逻辑是如何运作之前,千万不要轻易使用for循环。以下是软件语言对HDL语言的转换。
// 软件语言例程:
For (int i=0; i<10; i++)
data[i] = data[i] + 1;
VHDL的等效代码
P_INCREMENT : process (clock)
begin
if rising_edge(clock) then
if index < 10 then
data(index) <= data(index) + 1;
index <= index + 1;
end if;
end if;
end process P_INCREMENT;
还有Verilog的版本
always @(posedge clock)
begin
if (index < 10)
begin
data[index] <= data[index] + 1;
index <= index + 1;
end
end
在可综合代码中使用For循环
For循环可以用于综合。For循环在可综合语句中的用途主要是扩展复制逻辑。以下是两个语言的代码与
仿真
。
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
en
ti
ty Example_For_Loop is
port (
i_Clock : std_logic
);
end Example_For_Loop;
architecture behave of Example_For_Loop is
signal r_Shift_With_For : std_logic_vector(3 downto 0) := X"1";
signal r_Shift_Regular : std_logic_vector(3 downto 0) := X"1";
begin
p_Shift_With_For : process (i_Clock)
begin
if rising_edge(i_Clock) then
for ii in 0 to 2 loop
r_Shift_With_For(ii+1) <= r_Shift_With_For(ii);
end loop; -- ii
end if;
end process;
p_Shift_Without_For : process (i_Clock)
begin
if rising_edge(i_Clock) then
r_Shift_Regular(1) <= r_Shift_Regular(0);
r_Shift_Regular(2) <= r_Shift_Regular(1);
r_Shift_Regular(3) <= r_Shift_Regular(2);
end if;
end process;
end behave;
Verilog
module for_loop_synthesis (i_Clock);
input i_Clock;
integer ii=0;
reg [3:0] r_Shift_With_For = 4'h1;
reg [3:0] r_Shift_Regular = 4'h1;
always @(posedge i_Clock)
begin
for(ii=0; ii<3; ii=ii+1)
r_Shift_With_For[ii+1] <= r_Shift_With_For[ii];
end
always @(posedge i_Clock)
begin
r_Shift_Regular[1] <= r_Shift_Regular[0];
r_Shift_Regular[2] <= r_Shift_Regular[1];
r_Shift_Regular[3] <= r_Shift_Regular[2];
end
endmodule
module for_loop_synthesis_tb (); // Testbench
reg r_Clock = 1'b0;
for_loop_synthesis UUT (.i_Clock(r_Clock));
always
#10 r_Clock = !r_Clock;
endmodule
仿真结果
回帖
(1)
徐思龙
2019-8-7 08:14:56
不错,很好的经验分享,辛苦麻烦了,欠缺这方面的资料,非常感谢
不错,很好的经验分享,辛苦麻烦了,欠缺这方面的资料,非常感谢
举报
更多回帖
rotate(-90deg);
回复
相关帖子
FPGA
for循环
HDL
理解
和
运用
Maxim iButton产品中的
循环
冗余校验
996
深入
理解
GPIO原理和
运用
1391
运用
FPGA
解决DSP设计难题
2325
反熔丝
FPGA
在密码芯片设计
中
的
运用
1983
基于
FPGA
边沿检测的
理解
问题?
2965
从原理到代码
理解
CRC
循环
冗余校验
1523
在
FPGA
中
,同步信号、异步信号和亚稳态的
理解
533
理解
FPGA
中的单粒子翻转
1443
数字设计
FPGA
应用:
循环
语句for
2575
理解
FPGA
的亚稳定性
0
发帖
登录/注册
20万+
工程师都在用,
免费
PCB检查工具
无需安装、支持浏览器和手机在线查看、实时共享
查看
点击登录
登录更多精彩功能!
首页
论坛版块
小组
免费开发板试用
ebook
直播
搜索
登录
×
20
完善资料,
赚取积分