[]
,拼接使用大括号{}
,例如:reg [7:0] vect;
wire a;
wire [3:0] b,
wire [5:0] c;
assign a = vect[1]; //取其中1Bit
assign b[3:0] = vect[7:4];//截取4Bit
assing c[5:0] = {a, b[3:0], 1'b1}; //拼接
reg [7:0] vect;
reg [1:0] cnt;
wire [4:0] out;
assign out = vect[cnt+4:cnt];
** Error: test.v(10): Range must be bounded by constant expressions.
提示vect的范围必须为常量表达式。也就是必须为,vect[6:2]
或vect[7:4]
,不能是vect[a:0]
,vect[4:b]
,或vect[a:b]
。额,这该怎么办呢?
既然有这个使用场景,那Verilog在设计之初就应该会考虑到这个应用吧!于是就去翻IEEE的Verilog标准文档,在5.2.1章节发现了一个用法可以实现我这个需求,那就是+:
和-:
符号,这个用法很少,在大部分关于FPGA和Verilog书籍中都没有提到。
大致意思就是,可以实现动态截取固定长度的数据,基本语法为:
vect[base+:width]或[base-:width]
base
可以为变量,width
必须为常量。reg [7:0] vect_1;
reg [0:7] vect_2;
wire [2:0] out;
vect_1[4+:3];
vect_1[4-:3];
vect_2[4+:3];
vect_2[4-:3];
vect_1[4+:3]表示,起始位为4,宽度为3,**升序**,则vect_1[4+:3] = vect_1[6:4]
vect_1[4-:3]表示,起始位为4,宽度为3,**降序**,则vect_1[4-:3] = vect_1[4:2]
vect_2[4+:3]表示,起始位为4,宽度为3,升序,则vect_2[4+:3] = vect_2[4:6]
vect_2[4-:3]表示,起始位为4,宽度为3,降序,则vect_2[4-:3] = vect_2[2:4]
module test;
reg [7:0] vect_1;
reg [0:7] vect_2;
initial
begin
vect_1 = 'b0101_1010;
vect_2 = 'b0101_1010;
$display("vect_1[7:0] = %b, vect_2[0:7] = %b", vect_1, vect_2);
$display("vect_1[4+:3] = %b, vect_1[4-:3] = %b", vect_1[4+:3], vect_1[4-:3]);
$display("vect_2[4+:3] = %b, vect_2[4-:3] = %b", vect_2[4+:3], vect_2[4-:3]);
$stop;
end
endmodule
//进入到源文件所在文件夹
cd c:/users/whik/desktop/verilog
//编译
vlog test.v
//仿真
vsim work.test
//运行
run -all
//运行结果
# vect_1[7:0] = 01011010, vect_2[0:7] = 01011010
# vect_1[4+:3] = 101, vect_1[4-:3] = 110
# vect_2[4+:3] = 101, vect_2[4-:3] = 011
# ** Note: $stop : test.v(15)
# Time: 0 ps Iteration: 0 Instance: /test
# Break in Module test at test.v line 15
END
→点关注,不迷路←