HDLBits——Basics
Simple wire
要求:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xVLSIS1l-1637764502113)(https://hdlbits.01xz.net/mw/thumb.php?f=Wire.png&width=800)]
Solution:
module top_module( input in, output out );
assign out = in;
endmodule
Timing diagrams(时序图):
Four wires
要求:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2eAjRHd8-1637764502119)(https://hdlbits.01xz.net/mw/thumb.php?f=Wire4.png&width=800)]
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
改进:assign {w,x,y,z} = {a,b,b,c}; 将连接运算符与赋值结合。
Timing diagrams(时序图):
Inverter(逆变器,即非门)
要求:
Solution:
module top_module( input in, output out );
assign out = ~in;
endmodule
PS:按位非 ( ~) ,逻辑非 ( !)
Timing diagrams:
AND gate
要求:
Solution:
module top_module(
input a,
input b,
output out );
assign out = a & b;
endmodule
PS:按位与 ( &) ,逻辑与 ( &&)
Timing diagrams:
NOR gate
要求:
Solution:
module top_module(
input a,
input b,
output out );
assign out = ~(a | b);
endmodule
PS:按位或 ( |) ,逻辑或 ( ||)
Timing diagrams:
XNOR gate
要求:
Solution:
module top_module(
input a,
input b,
output out );
assign out = ~(a ^ b);
endmodule
PS:按位异或 ( ^) ,没有逻辑异或。
Timing diagrams:
Declaring wires
要求:
Solution:
`default_nettype none
module top_module(
input wire a,
input wire b,
input wire c,
input wire d,
output wire out,
output wire out_n );
wire x,y;
assign x = a & b;
assign y = c & d;
assign out = x | y;
assign out_n = ~out;
endmodule
PS:default_nettype 用于为隐式的线网变量指定为线网类型,即将没有被声明的连线定义为线网类型。default_nettype none 该实例定义后,将不再自动产生 wire 型变量。
Timing diagrams:
7458 chip
要求:
7458 是具有四个与门和两个或门的芯片。
创建一个与 7458 芯片功能相同的模块,它有 10 个输入和 2 个输出。用两个方法实现。
1、选择使用一条 assign 语句来驱动每条输出线。
2、选择声明 4 条线用作中间信号,其中每条内部线都由一个与门的输出驱动。
Solution:
方法一:
module top_module (
input p1a, p1b, p1c, p1d, p1e, p1f,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
assign p1y = (p1a & p1c & p1b)|(p1f & p1e & p1d);
assign p2y = (p2c & p2d)|(p2a & p2b);
endmodule
方法二:
module top_module (
input p1a, p1b, p1c, p1d, p1e, p1f,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
wire w1,w2,w3,w4;
assign w1 = p1a & p1c & p1b;
assign w2 = p1f & p1e & p1d;
assign w3 = p2c & p2d;
assign w4 = p2a & p2b;
assign p1y = (w1)|(w2);
assign p2y = (w3)|(w4);
endmodule
PS:命名不要随便命,最好是能看出来是什么的输出,比如 and1,and2 最起码能看出来是与门的输出。
Timing diagrams:
PS:
我发现我一个以前以为是好习惯,但现在觉得是坏习惯的问题,就是做什么都喜欢从零开始,争取全面的、彻底的掌握。就像开始做 HDLBits,把网站仔仔细细看一遍,翻译好,做笔记。但在查答案时却发现早就有前人做过类似的工作了,而且比我翻译得更好,总结的更全,于是乎,加入收藏夹,把自己总结的删掉,一晚上干了个寂寞。这样的事曾经干过很多,也吃了很多亏,但不长记性,做事抓不住重点,结果只是浪费时间。
我得明白,有的事情是必须自己做的,比如写 Verilog 代码,自己做一遍题别看答案。有的事情却得学会站在巨人的肩膀上,为自己节省时间,才能有精力做更多、学更多。
加油哇!