HDLBits练习 Circuits;Combinational Logic;Basic Gates

0.常用逻辑门符号

在这里插入图片描述

1.Wire

例1:
在这里插入图片描述
解:

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

wire对应的电路模型就是这样的一条线

2.GND

例1:
在这里插入图片描述
解:

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

直接接地

3.NOR

例1:
在这里插入图片描述

解:

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

实现或非门
^是异或运算符:相异时值为真

ABP
000
011
101
110

4.Another gate

例1:
在这里插入图片描述
看图对照
解:

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

5.Two gates

例1:
在这里插入图片描述
注意图中的标号是异或和同或门
解:

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

6.More logic gates

例1:
在这里插入图片描述
写出这7种门。注意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;//a and not b:a与非b
endmodule

7.7420 chip

例1:
在这里插入图片描述

解:

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.Truth tables

例1:
在这里插入图片描述
在这里插入图片描述
输入 x1,x2,x3 根据真值标输出f

解:
先化简出最简表达式,然后按照表达式写出f
化简结果:
在这里插入图片描述

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

9.Tow-bits equality

例1:
在这里插入图片描述
解:
注意always 的使用结构

module top_module (
    input [1:0] A,
    input [1:0] B,
    output z 
); 
    always@(*)
        begin//always 中的begin必不可少
            if(A == B)
                z = 1;
            else
                z = 0;
        end
endmodule

10.Simple circuit A

例1:
在这里插入图片描述

解:

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

11.Simple circuit B

例1:
在这里插入图片描述
解:
时序图写程序,明显是同或 (-_-)

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

12.Combine circuits A and B

例1:
在这里插入图片描述
解:
分别用到了前两题的俩个模块,分别命名为 “IA”“IB”

module top_module (
    input x,
    input y,
    output z
);
    wire IA = (x ^ y) & x; 
    wire IB = !(x ^ y);
    assign z = (IA | IB) ^ (IA & IB);
endmodule

13.Ring or vibrate?

例1:
题目抽象为真值表如下,只有当来电且不在震动模式时会发出声音

ringvibrate_moderingmotor
1101
1010
0100
0000

在这里插入图片描述
本题实际上要求在确保输出的情况下再确保输入,且要转变思维,在两行语句之内实现
在这里插入图片描述
在这里插入图片描述
但抽象逻辑,真值表,卡诺图化简总归为最稳妥的方法

解:

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

14.Thermostat

例1:
在这里插入图片描述
在这里插入图片描述
thermostat(温度自动调节器)有两种模式heating 和 cooling,在heating下当too_cold =1时,打开 heater但是不使用air conditioner ,在cooling下当too_hot = 1时,打开 air conditioner但是不使用heater 此外无论打开 heater还是air conditioner都要打开 fan 最后当fan_on=1时同样打开fan
解:
与上一题类似,画真值表比较稳妥

modetoo_hottoo_coldheaterairconfan
000000
001000
010011
011011
100000
101101
110000
111101

然后卡诺图化简写出表达式(由于fan_on的情况较为简单,因此没有列在表中)

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

在这里插入图片描述
从上图可以看出没有考虑too_hot与too_cold同时存在的矛盾情况,不是很合理

15.3-bit population count

例1:
计算输入中“1”的个数
在这里插入图片描述

解:

module top_module( 
    input [2:0] in,
    output [1:0] out );
    integer i = 0;
    always @(*)
        begin
            out = 2'b00;
            for(i = 0;i < 3;i++)
                begin
                    if(in[i] == 1'b1 )
                        out = out + 1'b1;
                end
        end
endmodule

由于out赋了初值,因此不需要else

16.Gates and vectors

例1:
在这里插入图片描述

解:

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[3]&in[2]},{in[2]&in[1]},{in[1]&in[0]}};
    assign out_any        = {{in[3]|in[2]},{in[2]|in[1]},{in[1]|in[0]}};
    assign out_different  = {{in[0]^in[3]},{in[3]^in[2]},{in[2]^in[1]},{in[1]^in[0]}};
endmodule

17.Even longer vectors

例1:
题目与上题类似,但是从4位改成了100位
解:
有多种解法直接能想到的应该是generate下的always循环,这里用另一种方法:移位运算
out_both为

in[98]in[97]in[96]in[95]in[94]in[4]in[3]in[2]in[1]in[0]
in[99]in[98]in[97]in[96]in[95]in[5]in[4]in[3]in[2]in[1]

表格上下两位依次对比 写作: in[98:0] & in[99:1]. 略有不同的是 out_different 为

in[99]in[98]in[97]in[96]in[95]in[4]in[3]in[2]in[1]
in[98]in[97]in[96]in[95]in[94]in[3]in[2]in[1]

输出是将首尾连接

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[99:1] | in[98:0];
    assign out_different  = in[99:0] ^ {in[0],in[99:1]};
endmodule
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值