HDLBits (9) — 线网声明
本题链接:
https://hdlbits.01xz.net/wiki/Wire_decl
到目前为止的电路都是输出是输入的简单函数。随着电路变得越来越复杂,您将需要线网将内部组件连接在一起。 当您需要使用线网( wire )时,您应该在模块的主体中,在首次使用之前的某处声明它。(将来,您会遇到更多类型的信号和变量,它们也以相同的方式声明,但现在,我们将从类型为 wire 的信号开始)。
例子

module top_module (
input in, // Declare an input wire named "in"
output out // Declare an output wire named "out"
);
wire not_in; // Declare a wire named "not_in"
assign out = ~not_in; // Assign a value to out (create a NOT gate).
assign not_in = ~in; // Assign a value to not_in (create another NOT gate).
endmodule // End of module "top_module"
在上面的模块中,有三条线网( in、out 和 not_in ),其中两条已经声明为模块的输入和输出端口的一部分(这就是为什么您不需要在前面的练习中声明任何线网的原因)。线网 not_in 需要在模块内声明,所以从模块外部看不到线网 not_in。 然后,使用两个赋值( assign )语句创建两个非门。 请注意,首先创建哪个非门并不重要:最终综合后的电路依旧相同。
练习
实现以下电路。 创建两条中间线网(命名随意)将与门和或门连接在一起。 注意输出线网也就是反馈到非门的线网,因此不一定需要在此处声明第三条线网。 注意线网是如何由一个源(门的输出)驱动的,但可以反馈为多个输入。
如果您遵循图中的电路结构,您应该得到四个赋值语句,因为有四个信号需要赋值。
(当然,可以在没有中间线网的情况下创建具有相同功能的电路。)

预期的解决方案长度:大约 5 行。
模块声明:
`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );

题目
`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );
endmodule

答案
`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );
wire g1,g2,g3;
assign g1 = a & b;
assign g2 = c & d;
assign g3 = g1 | g2;
assign out = g3;
assign out_n = ~g3;
endmodule

输出波形


以反引号 ` 开始的某些标识符是 Verilog 系统编译指令。
编译指令为 Verilog 代码的撰写、编译、调试等提供了极大的便利。
在编译阶段,`define 用于文本替换,类似于 C 语言中的 #define。
一旦 `define 指令被编译,其在整个编译过程中都会有效。
`undef 用来取消之前的宏定义。
连续赋值语句是 Verilog 数据流建模的基本语句,用于对 wire 型变量进行赋值。
LHS(left hand side) 指赋值操作的左侧,RHS(right hand side)指赋值操作的右侧。
assign 为关键词,任何已经声明 wire 变量的连续赋值语句都是以 assign 开头。
需要说明的是:
LHS_target 必须是一个标量或者线型向量,而不能是寄存器类型。
RHS_expression 的类型没有要求,可以是标量或线型或存器向量,也可以是函数调用。
只要 RHS_expression 表达式的操作数有事件发生(值的变化)时,RHS_expression 就会立刻重新计算,同时赋值给 LHS_target。
Verilog 还提供了另一种对 wire 型赋值的简单方法,即在 wire 型变量声明的时候同时对其赋值。wire 型变量只能被赋值一次,因此该种连续赋值方式也只能有一次。
参考内容:
2.5 Verilog 编译指令 | 菜鸟教程:https://www.runoob.com/w3cnote/verilog-compile-instruction.html
3.1 Verilog 连续赋值 | 菜鸟教程:
https://www.runoob.com/w3cnote/verilog-assign.html