HDLBits_Verilog学习笔记Ⅰ——Verilog Language_Modules: Hierarchy
谢谢谢同学鸭
2021年05月07日 20:24
收录于文集
共5篇

本专栏集是基于HDLBits上的Verilog练习题的分析与总结,上篇地址:

  • 注:可以搭配上述视频食用,先做题再看答案,网址:https://hdlbits.01xz.net/

ps:B站还不支持Verilog代码,所以就随便选了一个,高亮部分应该是module input output wire reg assign等。

19. Modules

小知识点:本题介绍前面已反复使用但未深入的module(模块)。Verilog的模块即一块电路,它通过输入和输出端口与外部交互。更大、更复杂的电路是通过将较小的模块和其他组件(如assign语句和always块)连接在一起来构建的。

        在顶层模块中实例化(简称例化)下层模块时,只需关注下层模块的端口,而不必在乎它的内部代码。模块的层次化结构通过在一个模块中例化另一个模块来实现,只要这些模块都处于一个project(项目)中,编译器就能找到相对应的模块。注意,在一个模块中可以例化,但不允许定义和编写其他模块。具体可看下面这些栗子:

代码块
text/x-c++src
自动换行
复制代码
module mod_a ( input in, output out );		//定义了一个名为mod_a的模块
    ... // Module body
endmodule

module top_module ( input a, output out );	//定义了名为top_module的模块
	mod_a instance1 ( .in(a), .out(out));	//在top_module模块中例化mod_a模块
endmodule	
//mod_a是被例化模块的模块名,instance1是实例名,()内是两个模块的端口信号的连接

//以下写法属于非法模块嵌套,是不被允许的!
module top_module ( input a, output out );
	module mod_a ( input in, output out );
    	... // Module body
	endmodule
endmodule
复制成功

拓展:数字IC有自顶向下和自底向上两种设计方法。前者先定义顶层功能块,再对其分析并不断分解成一个个子模块,直至无法进一步分解的底层功能块;后者先分析现有的功能块,然后用它们搭建更大规模的模块,直至顶层功能块。Verilog就用module(模块)来表示这一个个的功能块(具有特定功能的一块块电路),它可大可小,大到一个微处理器,小到一个晶体管,原则上都可作为一个模块。

Practice: ... create one instance of module mod_a, then connect the module's three pins (in1, in2, and out) to your top-level module's three ports (wires a, b, and out) ...

大白话:根据下图,实例化一个下层模块mod_a,将其三个端口in1,in2,out按照图中的连接方式,分别连接到顶层模块的a,b,out端口上。mod_a模块已默认提供给你,不需要自己写,只需在顶层模块中例化它即可。模块的例化有两种方式:按端口位置和按端口名称,我将在下面两题中分别介绍,本题可先参照上面的例子来解答。

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module ( input a, input b, output out );
    mod_a instance2 ( .in1(a), .in2(b), .out(out));
endmodule
复制成功

BB两句:mod_a的in1和top_module的a,in2和b,out和out分别一一对应。

20. Connecting ports by position

小知识点:上面用的是通过端口名称例化模块,本题讲的是按端口位置例化,它类似C语言中的函数调用语法。例化格式:模块名 实例名 (外部信号1, 外部信号2, ,外部信号3); (注意,外部信号是指被例化模块外部的信号,也就是顶层模块的内部信号或端口信号,中间的空缺是由于此端口没有对应的外部信号)。按位置例化就是在实例化时,外部的信号需要按照该模块端口声明的位置顺序一一对应。这种方式的缺点是一旦端口列表发生改变,所有模块实例化中的端口连接都需要调整。

代码块
text/x-c++src
自动换行
复制代码
module mod_a ( input in, output out ); 		
	...
module top_module ( input a, output out );	
	mod_a instance1 (a, out);	//in连a,out连out,则此句的a和out
endmodule						//需按上面in和out的顺序写,即a在out前
复制成功

Practice: You are given a module named mod_a that has 2 outputs and 4 inputs, in that order. You must connect the 6 ports by position to your top-level module's ports ...

代码块
text/x-c++src
自动换行
复制代码
module mod_a ( output, output, input, input, input, input );
复制成功

大白话:给了一个模块mod_a(如上),根据下图的连接关系在top_module中例化,注意mod_a的output在input的前面。

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module ( 
    input a, 
    input b, 
    input c,
    input d,
    output out1,
    output out2
);
    mod_a u_mod_a (out1, out2, a, b, c, d);

endmodule
复制成功

BB两句:这题一定要注意给的那个mod_a声明时的output在input前面,而top_module恰恰相反,所以要按mod_a的端口顺序进行调整。

21. Connecting ports by name

小知识点:本题讲的是上上题栗子中用的例化方式,即按端口名称。例化格式:模块名 实例名 ( .端口名(外部信号), .端口名(), .端口名(外部信号)); (中间括号内的空缺也是因为该端口没有对应的外部信号)。按名称例化就是将模块实例外部的信号直接对应于该模块的端口名称,这种方式下的端口顺序可以是任意的。另外,要注意端口名前的“.”号。

Practice: You are given a module named mod_a that has 2 outputs and 4 inputs, in some order. You must connect the 6 ports by name to your top-level module's ports: ...

代码块
text/x-c++src
自动换行
复制代码
module mod_a(output out1,output out2, input in1,input in2,input in3,input in4);
复制成功

大白话:给定模块mod_a(如上),根据下图的连接关系在top_module中例化。

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module ( 
    input a, 
    input b, 
    input c,
    input d,
    output out1,
    output out2
);
    mod_a name ( 
      	.in1(a), 
      	.in2(b), 
      	.in3(c), 
      	.in4(d), 
      	.out1(out1), 
      	.out2(out2));

endmodule
复制成功

BB两句:模块mod_a的端口声明顺序是output在input前面,但是此处例化时把out写在了in后面,这跟上题的按位置例化不同。另外,这题的写法要比上上题的写法更美观直接,尤其是端口较多时,写成一行不便于阅读。

22. Three modules

小知识点:在实例化时,模块名后需加实例名,这是为了能够通过修改实例名,来实现在一个顶层模块中对同一个子模块的多次例化。

Practice: You are given a module my_dff with two inputs and one output (that implements a D flip-flop). Instantiate three of them, then ...

代码块
text/x-c++src
自动换行
复制代码
module my_dff ( input clk, input d, output q );
复制成功

大白话:给定一个可以实现D触发器功能的my_dff模块(如上),对它例化三次以构建一个3位的移位寄存器。clk时钟信号需要连接在所有实例上。注意,要建立内部连接,则需声明一些wire信号,在命名wire信号和模块实例名时要小心:名称必须唯一。

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module (
	input clk,
	input d,
	output q
);
	wire a, b;	

	my_dff dff1 ( .clk(clk), .d(d), .q(a));
	my_dff dff2 ( .clk(clk), .d(a), .q(b));
	my_dff dff3 ( .clk(clk), .d(b), .q(q));

endmodule
复制成功

BB两句:第一个DFF的q连第二个DFF的d,第二个DFF的q连第三个DFF的d。可以先声明两个wire类型的中间信号a和b,dff1的q连a,然后a再连dff2的d,dff2的q连b,然后再将b连dff3的d,这就实现了上述的连接。

23. Modules and vectors

小知识点:本题的模块端口不再是单一的引脚,而是多位宽的矢量。你将把wire矢量连接到这些模块上,而不是普通的wire。与Verilog中的其他地方一样,端口的向量长度不必与连接它的wire匹配,但这将导致向量的补零或截断。本题不使用向量长度不匹配的连接。

浅出:例化时的那些端口信号可以是多位宽的矢量,而且连接时的向量长度可以不匹配。

拓展:本题需要使用一个4选1的多路复用器(数据选择器),但是题干并没有提供,所以不能通过例化调用,而是需要自己在顶层模块中写一个。下面是一个2选1多路复用器的栗子(用always块加case语句,具体会在后面的题中讲解,可先仿照着写):

代码块
text/x-c++src
自动换行
复制代码
always@(*)				// always语句块,在断电或中断前,会一直执行
	case(sel)			// case语句,根据不同的sel值选择不同的输出值
		1'b0: q = d;	// 输入sel=0时,输出q=d
		1'b1: q = q0;	// 输入sel=1时,输出q=q0
	endcase
复制成功

Practice: You are given a module my_dff8 with two inputs and one output (that implements a set of 8 D flip-flops). Instantiate three of them, then chain them together to ...

代码块
JavaScript
自动换行
复制代码
module my_dff8 ( input clk, input [7:0] d, output [7:0] q );
复制成功

大白话:给定一个可以实现8位宽D触发器功能的my_dff8模块(如上),对它例化三次以构建一个输入为8位宽,长度为3的移位寄存器。此外,创建一个4选1多路复用器(未提供),根据sel[1:0]选择输出:输入d的值,或者在第一个、第二个或第三个D触发器之后的值。(本质上,sel选择的是输入延迟的周期,即0到3个时钟周期。)

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module ( 
    input clk, 
    input [7:0] d, 
    input [1:0] sel, 
    output [7:0] q 
);
    wire [7:0]q1,q2,q3;

    my_dff8 dff1( .clk(clk), .d(d), .q(q1));
    my_dff8 dff2( .clk(clk), .d(q1), .q(q2));
    my_dff8 dff3( .clk(clk), .d(q2), .q(q3));
    
    always@(*)
        case(sel)
            2'b00: q = d;
            2'b01: q = q1;
            2'b10: q = q2;
            2'b11: q = q3;
        endcase
endmodule
复制成功

24. Adder 1

Practice: You are given a module add16 that performs a 16-bit addition. Instantiate two of them to create a 32-bit adder. One add16 module computes the lower ...

代码块
text/x-c++src
自动换行
复制代码
module add16 (input[15:0]a,input[15:0]b,input cin, output[15:0]sum,output cout);
复制成功

大白话:给了一个实现16位加法器的模块add16,实例化两个add16以达到32位加法。一个add16模块计算结果的低16位,另一个add16模块在接收到前者的进位后再计算结果的高16位。此32位加法器不需要处理输入进位(假设为0)和输出进位(无需进位),但是内部模块仍需处理进位才能正常运行。(换句话说,add16模块执行16位的a+b+cin,而顶层模块执行32位的a+b)。根据下图连接模块。

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    wire cout1;
    add16 add_1 (.a(a[15:0]), .b(b[15:0]), .cin(1'b0), .sum(sum[15:0]), .cout(cout1));
    add16 add_2 (.a(a[31:16]), .b(b[31:16]), .cin(cout1), .sum(sum[31:16]), .cout());
endmodule
复制成功

25. Adder 2

小知识点:全加器的逻辑表达式 👉 sum = a ^ b ^ cin;  cout = a&b | a&cin | b&cin; 。

Practice: In this exercise, you will create a circuit with two levels of hierarchy. ... 

代码块
text/x-c++src
自动换行
复制代码
module add1 (input a,input b,input cin, output sum,output cout);//未提供,需编写
复制成功

大白话:本题需创建一个具有两级层次结构的电路。top_module中实例化两个add16(已提供),每个add16中实例化16个add1(需自己写),即写两个模块:top_module和add1。

       跟上面那题差不多,已提供了相同的add16,但是组成16位加法器的一位加法器需要自己写。也就是说,本题中一共有三个模块:① top_module:包含两个16bit加法器的顶级模块;② add16(已给):一个16bit加法器,由16个1bit全加器构成;③ add1(未给):1bit全加器模块。在每个add16中,实例化了16个全加器add1(未提供)来执行加法,需要编写的是全加器add1。提交的代码中如果缺少add1,将会出现error。

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module (
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
	wire cout1;
    add16 add_1(.a(a[15:0]), .b(b[15:0]), .cin(1'b0), .sum(sum[15:0]), .cout(cout1));
    add16 add_2(.a(a[31:16]), .b(b[31:16]), .cin(cout1), .sum(sum[31:16]), .cout());
endmodule

module add1 ( input a, input b, input cin, output sum, output cout );
	assign sum = a^b^cin;
	assign cout = a&b | a&cin | b&cin;
endmodule
复制成功

BB两句:这题噼里啪啦的一大堆,其实就是让你多写个一位的全加器。

26. Carry-select adder

小知识点:前两题实现的是行波进位加法器(也称串行进位或逐位进位加法器),这类加法器的缺点是较高位的相加结果必须等到低一位的进位产生之后才能开始计算,这使得加法器的计算延迟变大。选择进位是一种改进方法,如图所示,第一级加法器与之前相同,但第二级加法器有两个,一个假设进位为0,另一个假设进位为1,然后用2选1多路选择器来选择哪个是正确的。

浅出:串行进位的缺点就是低一位的结果出来,且将进位(0或1)给高一位的加法器后,高一位的才能开始计算,这样会很慢。所以选择进位就增加了一个高位加法器,一个进位预设为0,另一个预设为1,然后连上一个2选1多路选择器。所有位权的加法器都一起计算,如果低一位的加法器进位为0,就选择进位预设为0的高一位加法器的结果输出,反之,低一位的加法器进位若为1,则选择输出进位预设为1的高一位加法器的结果。

Practice: In this exercise, you are provided with the same module add16 as the previous exercise, which adds two 16-bit numbers with carry-in and ...

大白话:本题提供与上两题相同的add16模块。实例化三个add16来构建进位选择加法器,同时编写一个16位的2选1多路选择器,将模块如下图所示连在一起。

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
	wire sel;
    wire [31:16]sum0, sum1;
    
    add16 add1 (.a(a[15:0]), .b(b[15:0]), .cin(1'b0), .sum(sum[15:0]), .cout(sel));
    add16 add21 (.a(a[31:16]), .b(b[31:16]), .cin(1'b0), .sum(sum0), .cout());
    add16 add22 (.a(a[31:16]), .b(b[31:16]), .cin(1'b1), .sum(sum1), .cout());
    
    assign sum[31:16] = sel?sum1:sum0;	//这是条件操作符,效果与case语句一样
    
endmodule
复制成功

BB两句:虽然速度(性能)提升了,但是所需的逻辑器件(面积和功耗)也增加了。

27. Adder-subtractor

小知识点:加-减法运算器可由加法器来构建,等效于对输入数据取反后再加1。最终结果是一个可以执行以下两种操作的电路:a + b + 0 和 a + ~b + 1 。使用32位的异或门对b进行取反(sub为1时),这也可看作b[31:0]与复制32次后的sub相异或。同时sub信号连接到加法器的进位。

浅出:一个数取反(包括符号位)再加1就是在求它的相反数,即 a - b = a + (-b) = a + (~b + 1),要注意的是,这是补码运算,而不是原码。另外,一个二进制数与0异或就是它本身,与1异或就是取反,将b与sub异或,并将sub连到进位上,这样就实现了通过sub来控制是否对b取反以及加1。若sub=0,则b不变且进位为0,实现a+b+0,即a+b;若sub=1,则b变为~b且进位为1,实现a+~b+1,即a-b。

拓展:一个二进制数与1异或等价于对其取反,这也被用在某些ISA(指令集架构)中,比如RISC-V就用XOR来代替NOT以保持三操作数格式。

Practice: Build the adder-subtractor below. You are provided with a 16-bit adder ...

大白话:如下图所示构建加-减法运算器,实例化两次前几题已提供的16位加法器add16。

转自HDLBits

答案(先做再看哦,且不唯一,仅供参考):

代码块
text/x-c++src
自动换行
复制代码
module top_module(
    input [31:0] a,
    input [31:0] b,
    input sub,
    output [31:0] sum
);
    wire [31:0] b_sub;
    wire carry;
    
    assign b_sub = b ^ {32{sub}};
    
    add16 add1 (.a(a[15:0]), .b(b_sub[15:0]), .cin(sub), .sum(sum[15:0]), .cout(carry));
    add16 add2 (.a(a[31:16]), .b(b_sub[31:16]), .cin(carry), .sum(sum[31:16]), .cout());
    
endmodule
复制成功

重点:①模块的定义和例化;

          ②例化格式:模块名 实例名 (外部信号, ,外部信号);

                               模块名 实例名 ( .端口名(外部信号), .端口名());;

        ③全加器逻辑式:sum = a ^ b ^ cin; cout = a&b | a&cin | b&cin;;

          ④行波(串行或逐位)进位加法器和选择进位加法器;

          ⑤求一个数的相反数可将其取反(包括符号位)再加1。