HDLBits习题讲解_Basics
线(wire)
- 在Verilog语法中,
wire
从功能上就好像现实中的导线一样,可以将一个信号持续不断的赋值给另一个信号;但与现实中的导线又是不一样的,Verilog中的wire
是__具有方向性__的(即定向的),这意味着信息仅从一个方向流定向赋值给另一个方向,即右侧的信号值被不断的驱动左侧的信号值。 - 模块上的端口也是具有方向性的,且默认端口信号类型为
wire
型;输入端口由模块外部的东西驱动,而输出端口则由模块内的信号对外驱动。 - wire型信号可以用作任何方程式的输入,也可以用作“assign”语句或实例元件的输出。
- 我们使用
assign
赋值语句,来实现对wire
信号的定向驱动,例如我们使用信号wire2
持续的驱动信号wire1
。代码格式如下:
assign wire1 = wire2;
Simple wire
- 题目要求使用输入的in信号持续驱动输出信号out。代码如下:
module top_module( input in, output out );
assign out = in;
endmodule
Four wires
- 题目要求创建一个具有3个输入和4个输出的模块,a -> w;b -> x;b -> y;c -> z。代码如下:
module top_module(
input a,b,c,
output w,x,y,z );
assign w = a;
assign y = b;
assign x = b;
assign z = c;
endmodule
tips: 在使用多个assign语句时,他们在代码书写时的__先后顺序并不重要__。
位/逻辑运算符
- 位运算符:按位取反()按位与(&)按位或(|)按位异或(^)按位同或(^)
- 逻辑运算符:逻辑与(&&)逻辑或(||)逻辑非(!)。
- 需要注意。当两个不同长度的数据进行位运算时,会自动地将两个操作数按右端对齐,位数少的操作数会在高位用0补齐。
Notgate
- 题目要求创建一个__非门__。代码如下:
module top_module( input in, output out );
assign out = ~in;
endmodule
Inverter
- 题目要求创建一个__与门__。代码如下:
module top_module(
input a,
input b,
output out );
assign out = a & b;
endmodule
Norgate
- 题目要求创建一个__或非门__。代码如下:
module top_module(
input a,
input b,
output out );
assign out = ~(a | b);
endmodule
Xnorgate
- 题目要求创建一个__异或非门__。代码如下:
module top_module(
input a,
input b,
output out );
assign out = ~(a ^ b);
endmodule
Wire decl
- 题目要求我们声明wire信号,用以表示中间变量,以实现更为复杂的组合逻辑描述。代码如下:
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );
wire and_ab,and_cd;
assign and_ab = a & b;
assign and_cd = c & d;
assign out = and_ab | and_cd;
assign out_n = ~out;
endmodule
##7458
- 题目以7458芯片为例,要求我们通过使用
wire
与·位运算符,对芯片进行描述。代码如下:
module top_module (
input p1a, p1b, p1c, p1d, p1e, p1f,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
assign p1y = (p1a & p1b & p1c) | (p1d & p1e & p1f);
assign p2y = (p2a & p2b) | (p2c & p2d);
endmodule