提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
assign left = right; 在现实中就是用一根导线(wire)将两个引脚连起来,电信号从right流向left,所以Verilog里就把right和left定义为wire类型的信号(此外还有reg等),其中right是源或驱动,left是被驱动的sink。
一、Basics
1.Simple wire
Practice: Create a module with one input and one output that behaves like a wire.
翻译:创建一个一输入一输出的模块,输入in和输出out为wire信号。
Solution(不唯一,仅供参考):
module top_module( input in, output out );
assign out = in;
endmodule
Timing Diagram
2.Four wires
Practice:Create a module with 3 inputs and 4 outputs that behaves like wires that makes these connections.
翻译:创建一个三输入四输出的模块,用assign连线。
Solution(不唯一,仅供参考):
module top_module(
input a,b,c,
output w,x,y,z );
assign w = a;
assign x = b;
assign y = b;
assign z = c;
endmodule
Timing Diagram
3.Inverter
Practice:Create a module that implements a NOT gate.
翻译:创建一个非门(反相器)。
Solution(不唯一,仅供参考):
module top_module( input in, output out );
assign out = ~in;//或out = !in;
endmodule
Timing Diagram
4.AND gate
Practice:Create a module that implements an AND gate.
翻译:创建一个与门。
Solution(不唯一,仅供参考):
module top_module(
input a,
input b,
output out );
assign out = a&b;
endmodule
Timing Diagram
5.NOR gate
Practice:Create a module that implements a NOR gate. A NOR gate is an OR gate with its output inverted. A NOR function needs two operators when written in Verilog.
翻译:创建一个实现NOR门的模块。“非门”是输出反向的“非门”。
Solution(不唯一,仅供参考):
module top_module(
input a,
input b,
output out );
assign out = ~(a|b);
endmodule
Timing Diagram
6.XNOR gate
Practice:Create a module that implements an XNOR gate.
翻译:创建一个实现XNOR门的模块,即同或门,XNOR表示同或,XOR是异或。
Solution(不唯一,仅供参考):
module top_module(
input a,
input b,
output out );
assign out = ~(a^b);
//或out a~^b
endmodule
Timing Diagram
7.Declaring wires
Practice:Implement the following circuit. Create two intermediate wires (named anything you want) to connect the AND and OR gates together.
翻译:实现以下电路。创建两条中间导线(可以任意命名)将AND和OR门连接在一起。(可以在模块内部定义wire信号来描述比较复杂的电路)
Solution(不唯一,仅供参考):
`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );
wire and_ab = a&b; //也可以先定义,在赋值
wire and_cd = c&d;
assign out = and_ab | and_cd;
assign out_n = ~out;
endmodule
Timing Diagram
8.7458 chip
Practice:Create a module with the same functionality as the 7458 chip.
翻译:写一块7458芯片模块,如图所示。
Solution(不唯一,仅供参考):
module top_module (
input p1a, p1b, p1c, p1d, p1e, p1f,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
wire and2_ab, and2_cd, and1_abc, and1_def;
assign and2_ab = p2a&p2b;
assign and2_cd = p2c&p2d;
assign and1_abc = p1a&p1b&p1c;
assign and1_def = p1d&p1e&p1f;
assign p1y = and1_abc|and1_def;
assign p2y = and2_ab|and2_cd;
endmodule
也可以不用中间信号直接赋值
assign p1y = (p1a & p1b & p1c) | (p1d & p1e & p1f);
assign p2y = (p2a & p2b) | (p2d & p2c);
Timing Diagram
总结
1、Verilog语句中多个assign赋值是并行的。
2、Verilog在定义input和output时可以省略wire,即相当于(input wire in, output wire out) ,assign left = right; 在现实中就是用一根导线(wire)将两个引脚连起来,电信号从right流向left,所以Verilog里就把right和left定义为wire类型的信号(此外还有reg等),其中right是源source或驱动,left是被驱动的sink。
3、source通常只有一个,而sinks可以有多个 ,因为一个wire信号不能被多个信号同时驱动,假设x同时连了a和b,如果a来了个高电平,b来了个低电平,那x是高还是低呢。又如果一个wire信号没有其他信号驱动它,此时综合器会将其默认为0。
4、“~”是按位取反 ”!“是逻辑取反。
5、”&“按位与 ,”&&“逻辑与,(1011)&(0111)=0011,
(1011)&&(1101)=1。
6、wire信号定义
wire型数据常用来表示用以assign关键字指定的组合逻辑信号,Verilog程序模块中输入、输出信号类型默认时自动定义为wire型。wire型信号可以用做任何方程式的输入,也可以用作”assign“语句或实例元件的输出。