HDLBits个人刷题详解合集6-Circuits-Combinational Logic-Basic Gates-HDBits题目分析

Exams/m2014 q4h

代码如下:

module top_module (

    input in,

    output out);

assign out = in;

endmodule

Exams/m2014 q4i

代码如下:

module top_module (

    output out);

assign out = 1'b0;

endmodule

Exams/m2014 q4e

代码如下:

module top_module (

    input in1,

    input in2,

    output out);

    assign out = !(in1|in2);

endmodule

Exams/m2014 q4f

代码如下:

module top_module (

    input in1,

    input in2,

    output out);

    assign out = (in1 & (!in2));

endmodule

Exams/m2014 q4g

代码如下:

module top_module (

    input in1,

    input in2,

    input in3,

    output out);

    assign out = (!(in1 ^ in2)) ^ (in3);

endmodule

Gates

代码如下:

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

7420

代码如下:

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

Truthtable1

真值表

通用做法:把时序图做真值表,再根据真值表列出它的逻辑表达式。

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

对于 N 个输入的布尔函数,有 2 个N可能的输入组合。真值表的每一行都列出一个输入组合,因此始终有 2 个N行。输出列显示每个输入值的输出应是什么。

上面的真值表用于三输入一输出函数。对于 8 种可能的输入组合,每个组合都有 8 行,还有一列输出列。有四种输入组合,其中输出为 1,四种输入组合的输出为 0。

问题描述:对于上面的例子,选择输出为1的行,把4个输入用或门组合输出,比如如果 x3=0 且 x2=1  x1=0,则输入与第 2 行匹配(这是一个 3 输入 AND 门)。

代码如下:

module top_module(

    input x3,

    input x2,

    input x1,  // three inputs

    output f   // one output

);

    assign f = (!x3)&x2&(!x1) | (!x3)&x2&x1 | x3&(!x2)&x1 | x3&x2&x1;

endmodule

Mt2015 eq2

问题描述:创建一个有两个输入的电路A、B,要求输出Z的值,当A=B时为1否则为0;

代码如下:

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

    //assign z = (A == B);

    always @(*)

        begin

            if(A == B)

                z = 1;

            else begin

                z = 0;

            end

        end

endmodule

Mt2015 q4a

代码如下:

module top_module (input x, input y, output z);

    assign z = (x^y) & x;

endmodule

Mt2015 q4b

电路B可以用以下仿真波形来描述:

代码如下:

module top_module ( input x, input y, output z );

    //assign z = !(x^y);

    assign z = !x&!y | x&y;//同或

endmodule

Mt2015 q4

电路设计有子电路A和B实例化组成,代码如下:

module top_module (input x, input y, output z);

wire w1,w2,w3,w4;

    A IA1_inst(

        .x(x),

        .y(y),

        .z(w1));

    B IB1_inst(

        .x(x),

        .y(y),

        .z(w2));

    A IA2_inst(

        .x(x),

        .y(y),

        .z(w3));

    B IB2_inst(

        .x(x),

        .y(y),

        .z(w4));

    assign z = (w1|w2) ^ (w3&w4);

endmodule

module A (input x, input y, output z);

    assign z = (x^y) & x;

endmodule

module B ( input x, input y, output z );

    //assign z = !(x^y);

    assign z = !x&!y | x&y;

endmodule

Ringer

代码如下:

module top_module(

input ring,

input vibrate_mode,

output ringer,

output motor

);

// When should ringer be on? When (phone is ringing) and (phone is not in vibrate mode)

assign ringer = ring & ~vibrate_mode;

// When should motor be on? When (phone is ringing) and (phone is in vibrate mode)

assign motor = ring & vibrate_mode;

endmodule

Thermostat

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

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

代码如下:

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 = fan_on | heater | aircon;

endmodule

module top_module(

input too_cold,

input too_hot,

input mode,

input fan_on,

output heater,

output aircon,

output fan

);

// Reminder: The order in which you write assign statements doesn't matter.

// assign statements describe circuits, so you get the same circuit in the end

// regardless of which portion you describe first.

// Fan should be on when either heater or aircon is on, and also when requested to do so (fan_on = 1).

assign fan = heater | aircon | fan_on;

// Heater is on when it's too cold and mode is "heating".

assign heater = (mode & too_cold);

// Aircon is on when it's too hot and mode is not "heating".

assign aircon = (~mode & too_hot);

// * Unlike real thermostats, there is no "off" mode here.

endmodule

Popcount3

“人口计数”电路对输入向量中的“1”数进行计数。为 3 位输入向量构建人口计数电路。

参考答案:

module top_module (

input [2:0] in,

output [1:0] out

);

// This is a function of 3 inputs. One method is to use a 8-entry truth table:

// in[2:0] out[1:0]

// 000      00

// 001      01

// 010      01

// 011      10

// 100      01

// 101      10

// 110      10

// 111      11

assign out[0] = (~in[2] & ~in[1] & in[0]) | (~in[2] & in[1] & ~in[0]) | (in[2] & ~in[1] & ~in[0]) | (in[2] & in[1] & in[0]);

assign out[1] = (in[1] & in[0]) | (in[2] & in[0]) | (in[2] & in[1]);

// Using the addition operator works too:

// assign out = in[0]+in[1]+in[2];

// Yet another method uses behavioural code inside a procedure (combinational always block)

// to directly implement the truth table:

/*

always @(*) begin

case (in)

3'd0: out = 2'd0;

3'd1: out = 2'd1;

3'd2: out = 2'd1;

3'd3: out = 2'd2;

3'd4: out = 2'd1;

3'd5: out = 2'd2;

3'd6: out = 2'd2;

3'd7: out = 2'd3;

endcase

end

*/

endmodule

Gatesv

题目描述:给一个位宽为4 的输入,我们想知道相邻为之间的关系;

out_both:表示相应输入位及其左侧的邻居是否为1,如果都是1则相应的输出位为1,out_both[2] 应该指示 in[2] 和 in[3] 是否都是 1。

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

out_different:此输出向量的每个位都应指示相应的输入位是否与其左侧的邻居不同。例如,out_different[2] 应该指示 in[2] 是否与 in[3] 不同。对于这一部分,将向量视为环绕,因此在[3]中左侧的邻居在[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 = in[2:0] & in[3:1];

    assign out_any = in[3:1] | in[2:0];

    assign out_different = {in[0],in[3:1]} ^ in[3:0];

endmodule

Gatesv100

在[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] 应该指示[2]或[1]中是否为1。由于 in[0] 没有右边的邻居,答案很明显,所以我们不需要知道out_any[0]。

out_different:此输出向量的每个位都应指示相应的输入位是否与其左侧的邻居不同。例如,out_different[98]应该指出in[98]是否与in[99]不同。对于这一部分,将向量视为环绕,因此在[99]中左侧的邻居在[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[99:1] | in[98:0];

    assign out_different = {in[0],in[99:1]} ^ in[99:0];

endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值