HDLBits练习汇总-06-组合逻辑设计测试--基础门设计
wire
实现下面的电路:
Module Declaration
module top_module (
input in,
output out
);
答案
module top_module (
input in,
output out);
assign out =in;
endmodule
GND
实现下面的电路:
Module Declaration
module top_module (
output out);
答案
module top_module (
output out);
assign out = 0 ;
endmodule
NOR
实现下面的电路:
Module Declaration
module top_module (
input in1,
input in2,
output out);
答案
module top_module (
input in1,
input in2,
output out);
assign out = !(in1|in2);
endmodule
另一个逻辑门练习
实现下面的电路:
Module Declaration
module top_module (
input in1,
input in2,
output out);
答案
module top_module (
input in1,
input in2,
output out);
assign out = in1 & (!in2);
endmodule
两个门
实现下面的电路:
Module Declaration
module top_module (
input in1,
input in2,
input in3,
output out);
答案
module top_module (
input in1,
input in2,
input in3,
output out);
assign out = !(in1^in2)^in3;
endmodule
许多逻辑门
让我们尝试同时构建几个逻辑门。构建一个具有两个输入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 Declaration
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
);
答案:
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芯片
7400 系列集成电路是一系列数字芯片,每个芯片都有几个门。7420 是具有两个 4 输入与非门的芯片。
创建一个与 7420 芯片功能相同的模块。它有8个输入和2个输出。
Module Declaration
module top_module (
input p1a, p1b, p1c, p1d,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
答案:
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
真值表1
在前面的练习中,我们使用了简单的逻辑门和几个逻辑门的组合。这些电路是组合电路的例子。组合意味着电路的输出只是其输入的函数(在数学意义上)。这意味着对于任何给定的输入值,只有一个可能的输出值。因此,描述组合函数行为的一种方法是明确列出输入的每个可能值的输出应该是什么。这是真值表。
对于 N 个输入的布尔函数,有 2N个可能的输入组合。真值表的每一行列出一个输入组合,所以总是有 2 N行。输出列显示每个输入值的输出应该是什么。
上面的真值表是针对三输入一输出的函数。对于 8 种可能的输入组合中的每一种,它都有 8 行,以及一个输出列。有四种输入组合,输出为 1,四种输入组合,输出为 0。
问题
创建一个实现上述真值表的组合电路。
Module Declaration
module top_module(
input x3,
input x2,
input x1, // three inputs
output f // one output
);
答案
module top_module(
input x3,
input x2,
input x1, // three inputs
output f // one output
);
assign f = (!x1&x2&!x3)|(x1&x2&!x3)|(x1&!x2&x3)|(x1&x2&x3);
endmodule
两位相等
创建一个具有两个 2 位输入A[1:0]和B[1:0] 的电路,并产生一个输出z。的值z应为1,如果A = B,否则z应该是0。
Module Declaration
module top_module (
input [1:0] A,
input [1:0] B,
output z );
答案
module top_module ( input [1:0] A, input [1:0] B, output z );
assign z = (A==B)?1:0;
endmodule
简单电路A
模块 A 应该实现函数z = (x^y) & x。实现这个模块。
Module Declaration
module top_module (input x, input y, output z);
答案
module top_module (input x, input y, output z);
assign z = (x^y) & x;
endmodule
简单电路B
电路B可以用下面的仿真波形来描述:
Module Declaration
module top_module ( input x, input y, output z );
答案
module top_module ( input x, input y, output z );
assign z=(x==y)?1:0;
endmodule
合并简单电路AB
有关此处使用的子模块,请参阅简单电路A和简单电路B。顶层设计包含两个实例,每个子电路 A 和 B,如下所示。
module top_module (input x, input y, output z);
wire z_from_ia1,z_from_ia2,z_from_ib1,z_from_ib2;
assign z = (z_from_ia1|z_from_ib1)^(z_from_ia2 & z_from_ib2);
A IA1(
.x(x),
.y(y),
.z(z_from_ia1)
);
A IA2(
.x(x),
.y(y),
.z(z_from_ia2)
);
B IB1(
.x(x),
.y(y),
.z(z_from_ib1)
);
B IB2(
.x(x),
.y(y),
.z(z_from_ib2)
);
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)?1:0;
endmodule
振铃和振动
假设您正在设计一个电路来控制手机的振铃器和振动电机。 每当电话需要在来电时振铃(输入振铃),您的电路必须打开振铃器(输出振铃器 = 1)或电机(输出电机 = 1),但不能同时打开两者。 如果手机处于振动模式(输入 vibrate_mode = 1),打开电机。 否则,打开振铃器。
尝试仅使用assign语句,看看是否可以将问题描述转换为逻辑门的集合。
Module Declaration
module top_module (
input ring,
input vibrate_mode,
output ringer, // Make sound
output motor // Vibrate
);
答案
module top_module (
input ring,
input vibrate_mode,
output ringer, // Make sound
output motor // Vibrate
);
assign ringer = (vibrate_mode==0)?ring:0;
assign motor = (vibrate_mode==1&ring==1)?1:0;
endmodule
恒温器
加热/冷却恒温器同时控制加热器(冬季)和空调(夏季)。实施一个电路,根据需要打开和关闭加热器、空调和鼓风机。
恒温器可以处于两种模式之一:加热 ( mode = 1) 和冷却 ( mode = 0)。在制热模式下,天气太冷时打开加热器(too_cold = 1),但不要使用空调。在制冷模式下,当温度过高时 ( too_hot = 1)打开空调,但不要打开加热器。当加热器或空调打开时,还要打开风扇使空气流通。此外,用户还可以要求风扇开启(fan_on = 1),即使加热器和空调关闭。
尝试仅使用assign语句,看看是否可以将问题描述转换为逻辑门的集合。
Module Declaration
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
答案
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
assign heater =(too_cold == 1 && mode ==1) ;
assign aircon =(too_hot == 1 && mode ==0) ;
assign fan = (heater==1)|(aircon==1)|(fan_on==1);
endmodule
人口计数
“人口计数”电路计算输入向量中“1”的数量。为 3 位输入向量构建人口计数电路。
Module Declaration
module top_module(
input [2:0] in,
output [1:0] out );
答案
module top_module(
input [2:0] in,
output [1:0] out );
int i;
always @(*)begin
out = 0;
for(i=0;i<=2;i=i+1)begin
if(in[i]==1)
out = out +1;
else
out = out;
end
end
endmodule
门和向量
您将获得一个四位输入向量。我们想知道每个位与其邻居之间的一些关系:
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 Declaration
module top_module(
input [3:0] in,
output [2:0] out_both,
output [3:1] out_any,
output [3:0] out_different );
答案
module top_module(
input [3:0] in,
output [2:0] out_both,
output [3:1] out_any,
output [3:0] out_different );
int i;
always @(*)begin
out_both = 0;
for(i=0;i<=2;i=i+1)begin
if(in[i]==1 && in[i+1]==1)
out_both[i] = 1;
else
out_both[i] = 0;
end
end
always @(*)begin
out_any = 0;
for(i=1;i<=3;i=i+1)begin
if(in[i]==1 || in[i-1]==1)
out_any[i] = 1;
else
out_any[i] = 0;
end
end
always @(*)begin
out_different = 0;
for(i=0;i<=3;i=i+1)begin
if(i==3)begin
if((in[i]^in[0])==1)
out_different[i] = 1;
else
out_different[i] = 0;
end
else if((in[i]^in[i+1])==1)
out_different[i] = 1;
else
out_different[i] = 0;
end
end
endmodule
100个门和向量
您将获得一个 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 Declaration
module top_module(
input [99:0] in,
output [98:0] out_both,
output [99:1] out_any,
output [99:0] out_different );
答案
module top_module(
input [99:0] in,
output [98:0] out_both,
output [99:1] out_any,
output [99:0] out_different );
int i;
always @(*)begin
out_both = 0;
for(i=0;i<=98;i=i+1)begin
if(in[i]==1 && in[i+1]==1)
out_both[i] = 1;
else
out_both[i] = 0;
end
end
always @(*)begin
out_any = 0;
for(i=1;i<=99;i=i+1)begin
if(in[i]==1 || in[i-1]==1)
out_any[i] = 1;
else
out_any[i] = 0;
end
end
always @(*)begin
out_different = 0;
for(i=0;i<=99;i=i+1)begin
if(i==99)begin
if((in[i]^in[0])==1)
out_different[i] = 1;
else
out_different[i] = 0;
end
else if((in[i]^in[i+1])==1)
out_different[i] = 1;
else
out_different[i] = 0;
end
end
endmodule
相关文章
- 安全测试笔记《二十四》——静态代码分析理论基础和主要技术
- python基础系列教程——Python的安装与测试:python解释器、PyDev编辑器、pycharm编译器
- 渗透测试基础- - -linux权限维持应急
- 渗透测试基础- - -windows入侵排查
- 【测试开发】python系列教程: 基础语法
- Web自动化测试-Protractor基础(二)
- 黑盒测试基础[通俗易懂]
- 量化合约对冲交易软件开发功能支持测试
- 接口01_精通Postman接口测试基础应用
- 1.零基础如何学习Web安全渗透测试?[通俗易懂]
- Python获取免费代理IP,并全部测试一遍,结果大失所望
- Spring基础(十七):Spring日志框架和测试支持
- Drozer-Android安全测试
- Fuzzable:一款基于静态分析实现的可模糊测试的自动化目标识别工具
- 码云WebHook使用(还未实际测试,但觉得可行)
- 自动化测试如此容易!多语言自动化测试框架 Selenium 编程(C#篇)
- 《花雕学AI》23:中文调教ChatGPT的秘诀:体验测试与通用案例,解锁无限有趣玩法!
- 渗透测试之攻破登录页面
- 检测Linux磁盘压力测试:改善系统性能(linux磁盘压力)
- Linux C编程实战:精准的测试方案(linuxc编程测试)
- 掌握linux:从基础命令测试开始(测试常用linux命令)
- 测试Linux系统硬盘IO性能(linux硬盘io测试)
- Linux基础测试:突破考题,助你轻松过关!(linux基础考题)
- 测试发现:三星980 Pro性能稍微好于PS5内置SSD
- 探索php 检验mysql连接功能(php测试mysql连接)
- Redis的通讯性能——测试小结(redis通讯)
- 利用redis进行性能测试一次有效的实验经验(性能测试数据库redis)
- 用存储过程、GetRows()、抽取10万条数据的速度测试
- javascript翻页测试页(动态创建标签并自动翻页)