HDLBits第六章练习及答案

1、线

实现以下电路:
在这里插入图片描述
代码实现:

module top_module (
    input in,
    output out);
	
    assign out = in;
    
endmodule

验证结果:
在这里插入图片描述

2、地

实现以下电路:
在这里插入图片描述
代码实现:

module top_module (
    output out);
	
    assign out =1'b0;
    
endmodule

验证结果:
在这里插入图片描述

3、或非门

实现以下电路:
在这里插入图片描述
代码实现:

module top_module (
    input in1,
    input in2,
    output out);
	
    assign out = ~ (in1 | in2);
        
endmodule

验证结果:
在这里插入图片描述

4、另一个门

实现以下电路:
在这里插入图片描述
代码实现:

module top_module (
    input in1,
    input in2,
    output out);
    
    assign out = in1 & (~in2);

endmodule

验证结果:
在这里插入图片描述

5、两门

实现以下电路:
在这里插入图片描述
代码实现:

module top_module (
    input in1,
    input in2,
    input in3,
    output out);
    
    assign out = ~(in1 ^ in2) ^ in3;

endmodule

验证结果:
在这里插入图片描述

6、更多逻辑门

让我们尝试同时构建几个逻辑门。构建一个具有两个输入a和b的组合电路。

有 7 个输出,每个输出都有一个逻辑门驱动它:

① out_and: a and b
② out_or: a or b
③ out_xor: a xor b
④ out_nand: a nand b
⑤ out_nor: a nor b
⑥ out_xnor: a xnor b
⑦ out_anotb: a and-not b

代码实现:

module top_module( 
    input a, b,
    output out_and,
    output out_or,
    output out_xor,
    output out_nand,
    output out_nor,
    output out_xnor,
    output out_anotb
);
    assign out_and = a & b;
    assign out_or = a | b;
    assign out_xor = a ^ b;
    assign out_nand = ~ ( a & b );
    assign out_nor = ~ ( a | b );
    assign out_xnor = ~(a ^ b);
    assign out_anotb = a & ( ~ b);

endmodule

验证结果:
在这里插入图片描述

7、7420芯片

7400 系列集成电路是一系列数字芯片,每个芯片都有几个门。7420 是具有两个 4 输入与非门的芯片。

创建一个与 7420 芯片功能相同的模块,它有8个输入和2个输出。
在这里插入图片描述
代码实现:

module top_module ( 
    input p1a, p1b, p1c, p1d,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y );

    assign p1y = ~ ( p1a & p1b & p1c & p1d );
    assign p2y = ~ ( p2a & p2b & p2c & p2d );
    
endmodule

验证结果:
在这里插入图片描述

8、真值表

在前面的练习中,我们使用了简单的逻辑门和几个逻辑门的组合。这些电路是组合电路的例子。组合意味着电路的输出只是其输入的一个函数(在数学意义上)。这意味着对于任何给定的输入值,只有一个可能的输出值。因此,描述组合函数行为的一种方法是显式列出每个输入值的输出应该是什么,这就是真值表。
对于一个有N个输入的布尔函数,有2N个可能的输入组合。真值表的每一行都列出了一个输入组合,所以总有2N行。输出列显示每个输入值的输出。

ROW numberinput x3input x2input x1output f
00000
10010
20101
30111
41000
51011
61100
71111

上面的真值表是针对三输入一输出的函数。对于 8 种可能的输入组合中的每一种,它都有 8 行,以及一个输出列。有四种输入组合,输出为 1;另四种输入组合,输出为 0。

根据真值表合成电路

假设我们想要构建上面的电路,但是我们被限制只使用标准逻辑门。如何构建任意的逻辑函数(表示为真值表)?

创建实现真值表功能的电路的一种简单方法是以乘积和形式表达该功能。乘积的总和(意为 OR)(意为 AND)是指真值表的每一行使用一个N 输入AND 门(以检测输入何时与每一行匹配),然后是一个 OR 门,只选择那些导致结果的行“1”输出。

对于上面的示例,如果输入匹配第 2行或第 3行或第 5行或第 7 行(这是一个 4 输入或门),则输出为“1” 。如果 x3=0且x2=1且x1=0(这是一个 3 输入与门),则输入匹配第 2 行。因此,这个真值表可以通过使用 4 个进行 OR 运算的 AND 门以规范形式实现。

练习:

创建一个实现上述真值表的组合电路。
在这里插入图片描述
代码实现:

module top_module( 
    input x3,
    input x2,
    input x1,  // three inputs
    output f   // one output
);
    
    assign f = (x1 & x2) | (x1 & x3) | ( x2 & ~x3 );
     
endmodule

验证结果:
在这里插入图片描述

9、两位相等

创建一个电路,有两个2位输入A[1:0]和B[1:0],输出z,如果A = B, z的值应该是1,否则z应该是0。

代码实现:

(1)条件运算符

module top_module ( input [1:0] A, input [1:0] B, output z ); 

    assign z = ( A == B ) ? 1'b1 : 1'b0;
    
endmodule

(2)if-else 条件分支语句

module top_module ( input [1:0] A, input [1:0] B, output z ); 

    always@(*)
        begin
            if(A==B)
                z = 1'b1;
            else
                z = 1'b0;
        end
    
endmodule

验证结果:
在这里插入图片描述

10、简单电路A

模块A应该实现函数z = (x^y) & x。实现这个模块。

代码实现:

module top_module (input x, input y, output z);
    
    assign z = ( x ^ y ) & x;

endmodule

验证结果:
在这里插入图片描述

11、简单电路B

电路B可以用下面的仿真波形来描述:
在这里插入图片描述
实现这个电路。

代码实现:

module top_module ( input x, input y, output z );
    
    assign z = x ^~ y;//按位同或

endmodule

验证结果:
在这里插入图片描述

12、组合电路A和B

顶层设计包含两个实例,每个子电路 A 和 B,如下所示。
在这里插入图片描述
实现这个电路。

代码实现:

module top_module (input x, input y, output z);
    
    wire za,zb;
 	assign za = ( x ^ y ) & x;
    assign zb = x ^~ y;
    assign z = (za | zb) ^ (za & zb);
        
endmodule

验证结果:
在这里插入图片描述

13、响铃还是振动?

假设您正在设计一个电路来控制手机的响铃器和振动电机。每当电话因来电需要响铃 (input ring)时,您的电路必须打开响铃器 (output ringer = 1) 或电机 (output motor = 1),但不能同时打开两者。如果手机处于振动模式 (input vibrate_mode = 1),请打开电机。否则,打开振铃器。

尝试仅使用assign语句,看看是否可以将问题描述转换为逻辑门的集合。

设计提示:在设计电路时,人们通常必须“向后”思考问题,从输出开始,然后向输入反向工作。这通常与人们对(顺序的、命令式的)编程问题的看法相反,在这种问题中,人们会首先查看输入,然后决定一个动作(或输出)。对于顺序程序,人们经常会想“如果(输入是 ___ )那么(输出应该是 ___ )”。另一方面,硬件设计人员通常认为“当(输入为 ___ )时(输出应为 ___ )”。

上面的问题描述是以适合软件编程的命令式形式编写的(if ring then do this),因此您必须将其转换为更适合硬件实现的声明式形式(assign ringer = ___)。能够思考并在两种风格之间进行转换是硬件设计所需的最重要技能之一。
在这里插入图片描述
代码实现:

module top_module (
    input ring,
    input vibrate_mode,
    output ringer,       // Make sound
    output motor         // Vibrate
);
    
    assign ringer =  ~vibrate_mode & ring; 
    assign motor = vibrate_mode & ring;
    
endmodule

验证结果:
在这里插入图片描述

14、恒温器

加热/冷却恒温器同时控制加热器(冬季)和空调(夏季)。实施一个电路,根据需要打开和关闭加热器、空调和风扇。

恒温器可以处于两种模式之一:加热 ( mode = 1) 和冷却 ( mode = 0)。在制热模式下,天气太冷时打开加热器(too_cold = 1),但不要使用空调。在制冷模式下,当温度过高时 ( too_hot = 1)打开空调,但不要打开加热器。当加热器或空调打开时,还要打开风扇使空气流通。此外,即使加热器和空调关闭,用户还可以要求风扇开启(fan_on = 1)。

尝试仅使用assign语句,看看是否可以将问题描述转换为逻辑门的集合。

代码实现:

module top_module (
    input too_cold,
    input too_hot,
    input mode,
    input fan_on,
    output heater,
    output aircon,
    output fan
); 
    
    assign heater = too_cold & mode ;
    assign aircon = too_hot & (~mode) ;
    assign fan =  aircon | heater | fan_on;
    
endmodule

验证结果:
在这里插入图片描述
在这里插入图片描述

15、三位计数器

“总体计数”电路计算输入向量中“1”的个数。为3位输入向量构建总体计数电路。

代码实现:

module top_module( 
    input [2:0] in,
    output [1:0] out );
    
    assign out = in[2] + in[1] + in[0];

endmodule

验证结果:
在这里插入图片描述

16、门和向量

在[3:0] 中,您将获得一个四位输入向量。我们想知道每个位与其邻居之间的一些关系:

① out_both:本输出向量的每一位应该指示是否两个相应的输入位和它的邻居到左(较高的指数)是“1”。例如,out_both[2]应该表明in[2]和in[3]是否都为 1。由于in[3]左边没有邻居,所以答案很明显,所以我们不需要知道out_both[3]。

② out_any:此输出向量的每一位都应指示是否有任何相应的输入位及其右侧的邻居为“1”。例如,out_any[2]应该表明in[2]或in[1]是否为 1。由于in[0]右边没有邻居,答案很明显,所以我们不需要知道out_any[0]。

③ out_different:该输出向量的每一位应指示相应的输入位是否与其左侧的相邻位不同。例如,out_different[2]应该指示in[2]是否与in[3] 不同。对于这部分,将向量视为环绕,因此in[3]左侧的邻居是in[0]。

代码实现:

方法一:

module top_module( 
    input [3:0] in,
    output [2:0] out_both,
    output [3:1] out_any,
    output [3:0] out_different );
    
    assign out_both[0] =  in[1] & in[0] ;
    assign out_both[1] =  in[2] & in[1] ;
    assign out_both[2] =  in[3] & in[2] ;
    
    assign out_any[1] = in[1] | in[0] ;
    assign out_any[2] = in[2] | in[1] ;
    assign out_any[3] = in[3] | in[2] ;
    
    assign out_different[0] = in[0] ^ in[1] ;
    assign out_different[1] = in[1] ^ in[2] ;
    assign out_different[2] = in[2] ^ in[3] ;
    assign out_different[3] = in[3] ^ in[0] ;
    
endmodule

方法二:

module top_module( 
    input [3:0] in,
    output [2:0] out_both,
    output [3:1] out_any,
    output [3:0] out_different );
    
    assign out_both = in[2:0] & in[3:1];
    assign out_any = in[2:0] | in[3:1];
    assign out_different = in[3:0] & {in[0],in[3:1]};

endmodule

验证结果:
在这里插入图片描述

17、更长的向量

在[99:0] 中,您将获得一个 100 位的输入向量。我们想知道每个位与其邻居之间的一些关系:

① out_both:本输出向量的每一位应该指示是否两个相应的输入位和它的邻居到左是“1”。例如,out_both[98]应该表明in[98]和in[99]是否都为 1。由于in[99]左边没有邻居,所以答案很明显,所以我们不需要知道out_both[99]。

② out_any:此输出向量的每一位都应指示是否有任何相应的输入位及其右侧的邻居为“1”。例如,out_any[2]应该表明in[2]或in[1]是否为 1。由于in[0]右边没有邻居,答案很明显,所以我们不需要知道out_any[0]。

③ out_different:该输出向量的每一位应指示相应的输入位是否与其左侧的相邻位不同。例如,out_different[98]应该指示in[98]是否与in[99] 不同。对于这部分,将向量视为环绕,因此in[99]左侧的邻居是in[0]。

代码实现:

module top_module( 
    input [99:0] in,
    output [98:0] out_both,
    output [99:1] out_any,
    output [99:0] out_different );
    
    assign out_both = in[98:0] & in[99:1];
    assign out_any = in[98:0] | in[99:1];
    assign out_different = in[99:0] ^ {in[0],in[99:1]};
    
endmodule

验证结果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值