Verilog 作业
将值放在网络和变量上称为赋值。有三种基本形式:
- 程序
- 连续
- 程序连续
法律 LHS 值
赋值有两个部分 - 右侧 (RHS) 和左侧 (LHS),中间有一个等号 (=) 或一个小于号 (<=)。
作业类型 | 左侧 |
---|---|
程序 |
|
连续 |
|
程序连续 |
|
RHS 可以包含任何计算结果为最终值的表达式,而 LHS 表示将 RHS 中的值分配给的网络或变量。
module tb;
reg clk;
wire a, b, c, d, e, f;
reg z, y;
// clk is on the LHS and the not of clk forms RHS
always #10 clk = ~clk;
// y is the LHS and the constant 1 is RHS
assign y = 1;
// f is the LHS, and the expression of a,b,d,e forms the RHS
assign f = (a | b) ^ (d & e);
always @ (posedge clk) begin
// z is the LHS, and the expression of a,b,c,d forms the RHS
z <= a + b + c + d;
end
initial begin
// Variable names on the left form LHS while 0 is RHS
a <= 0; b <= 0; c <= 0; d <= 0; e <= 0;
clk <= 0;
end
endmodule
程序分配
过程赋值发生在过程中,例如always、initial、task 和functions,用于将值赋给变量。该变量将保持该值,直到下一次分配给同一变量。
当仿真在仿真期间的某个时间点执行此语句时,该值将被放置到变量上。这可以通过使用 if-else-if、case 语句和循环机制等控制流语句按照我们想要的方式进行控制和修改。
reg [7:0] data;
integer count;
real period;
initial begin
data = 8'h3e;
period = 4.23;
count = 0;
end
always @ (posedge clk)
count++;
变量声明赋值
初始值可以在声明时放置到变量上,如下所示。赋值没有持续时间,并保持该值,直到对同一变量进行下一次赋值。请注意,不允许将变量声明分配给数组。
module my_block;
reg [31:0] data = 32'hdead_cafe;
initial begin
#20 data = 32'h1234_5678; // data will have dead_cafe from time 0 to time 20
// At time 20, data will get 12345678
end
endmodule
reg [3:0] a = 4'b4;
// is equivalent to
reg [3:0] a;
initial a = 4'b4;
如果变量在声明期间和初始块中的时间 0 初始化,如下所示,则无法保证评估顺序,因此可以有 8'h05 或 8'hee。
module my_block;
reg [7:0] addr = 8'h05;
initial
addr = 8'hee;
endmodule
reg [3:0] array [3:0] = 0; // illegal
integer i = 0, j; // declares two integers i,j and i is assigned 0
real r2 = 4.5, r3 = 8; // declares two real numbers r2,r3 and are assigned 4.5, 8 resp.
time startTime = 40; // declares time variable with initial value 40
程序块和分配将在后面的部分中更详细地介绍。
连续赋值
点击这里查看逐步模拟示例!
这用于将值分配给标量和矢量网络,并在 RHS 发生变化时发生。它提供了一种在不指定门互连的情况下对组合逻辑进行建模的方法,并使使用逻辑表达式驱动网络变得更加容易。
// Example model of an AND gate
wire a, b, c;
assign a = b & c;
每当 b 或 c 改变它的值时,RHS 中的整个表达式都会被计算并且 a 将使用新值进行更新。
网络声明赋值
这允许我们在声明 net 的同一语句上放置连续赋值。请注意,由于网络只能声明一次,因此网络只能声明一次。
wire penable = 1;
程序连续赋值
这些是允许将表达式连续分配给网络或变量的过程语句,有两种类型。
assign
...deassign
force
...release
分配解除分配
这将覆盖对变量的所有程序分配,并通过使用与 deassign
相同的信号来停用 .变量的值将保持不变,直到变量通过程序或程序连续赋值获得新值。 assign
的 LHS statement 不能是位选择、部分选择或数组引用,但可以是变量或变量的串联。
reg q;
initial begin
assign q = 0;
#10 deassign q;
end
强制释放
这些类似于 assign - deassign
语句,但也可以应用于网络和变量。 LHS 可以是网络的位选择、网络的部分选择、变量或网络,但不能是对数组的引用和变量的位/部分选择。 force
语句将覆盖对变量所做的所有其他分配,直到使用 release
释放它 关键字。
reg o, a, b;
initial begin
force o = a & b;
...
release o;
end
Verilog