wire
實(shí)現(xiàn)下面的電路:
Module Declaration
module top_module (
input in,
output out
);
答案
module top_module (
input in,
output out);
assign out =in;
endmodule
GND
實(shí)現(xiàn)下面的電路:
Module Declaration
module top_module (
output out);
答案
module top_module (
output out);
assign out = 0 ;
endmodule
NOR
實(shí)現(xiàn)下面的電路:
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
另一個(gè)邏輯門(mén)練習(xí)
實(shí)現(xiàn)下面的電路:
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
兩個(gè)門(mén)
實(shí)現(xiàn)下面的電路:
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
許多邏輯門(mén)
讓我們嘗試同時(shí)構(gòu)建幾個(gè)邏輯門(mén)。構(gòu)建一個(gè)具有兩個(gè)輸入a和b的組合電路。 有 7 個(gè)輸出,每個(gè)輸出都有一個(gè)邏輯門(mén)驅(qū)動(dòng)它:
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 系列集成電路是一系列數(shù)字芯片,每個(gè)芯片都有幾個(gè)門(mén)。7420 是具有兩個(gè) 4 輸入與非門(mén)的芯片。 創(chuàng)建一個(gè)與 7420 芯片功能相同的模塊。它有8個(gè)輸入和2個(gè)輸出。
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
在前面的練習(xí)中,我們使用了簡(jiǎn)單的邏輯門(mén)和幾個(gè)邏輯門(mén)的組合。這些電路是組合電路的例子。組合意味著電路的輸出只是其輸入的函數(shù)(在數(shù)學(xué)意義上)。這意味著對(duì)于任何給定的輸入值,只有一個(gè)可能的輸出值。因此,描述組合函數(shù)行為的一種方法是明確列出輸入的每個(gè)可能值的輸出應(yīng)該是什么。這是真值表。
對(duì)于 N 個(gè)輸入的布爾函數(shù),有 2^N^個(gè)可能的輸入組合。真值表的每一行列出一個(gè)輸入組合,所以總是有 2 ^N^行。輸出列顯示每個(gè)輸入值的輸出應(yīng)該是什么。
上面的真值表是針對(duì)三輸入一輸出的函數(shù)。對(duì)于 8 種可能的輸入組合中的每一種,它都有 8 行,以及一個(gè)輸出列。有四種輸入組合,輸出為 1,四種輸入組合,輸出為 0。問(wèn)題創(chuàng)建一個(gè)實(shí)現(xiàn)上述真值表的組合電路。
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
兩位相等
創(chuàng)建一個(gè)具有兩個(gè) 2 位輸入A[1:0]和B[1:0] 的電路,并產(chǎn)生一個(gè)輸出z。的值z(mì)應(yīng)為1,如果A = B,否則z應(yīng)該是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
簡(jiǎn)單電路A
模塊 A 應(yīng)該實(shí)現(xiàn)函數(shù)z = (x^y) & x。實(shí)現(xiàn)這個(gè)模塊。
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
簡(jiǎn)單電路B
電路B可以用下面的仿真波形來(lái)描述:
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
合并簡(jiǎn)單電路AB
有關(guān)此處使用的子模塊,請(qǐng)參閱簡(jiǎn)單電路A和簡(jiǎn)單電路B。頂層設(shè)計(jì)包含兩個(gè)實(shí)例,每個(gè)子電路 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
振鈴和振動(dòng)
假設(shè)您正在設(shè)計(jì)一個(gè)電路來(lái)控制手機(jī)的振鈴器和振動(dòng)電機(jī)。 每當(dāng)電話(huà)需要在來(lái)電時(shí)振鈴(輸入振鈴),您的電路必須打開(kāi)振鈴器(輸出振鈴器 = 1)或電機(jī)(輸出電機(jī) = 1),但不能同時(shí)打開(kāi)兩者。 如果手機(jī)處于振動(dòng)模式(輸入 vibrate_mode = 1),打開(kāi)電機(jī)。 否則,打開(kāi)振鈴器。
嘗試僅使用assign語(yǔ)句,看看是否可以將問(wèn)題描述轉(zhuǎn)換為邏輯門(mén)的集合。
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
恒溫器
加熱/冷卻恒溫器同時(shí)控制加熱器(冬季)和空調(diào)(夏季)。實(shí)施一個(gè)電路,根據(jù)需要打開(kāi)和關(guān)閉加熱器、空調(diào)和鼓風(fēng)機(jī)。
恒溫器可以處于兩種模式之一:加熱 ( mode = 1) 和冷卻 ( mode = 0)。在制熱模式下,天氣太冷時(shí)打開(kāi)加熱器(too_cold = 1),但不要使用空調(diào)。在制冷模式下,當(dāng)溫度過(guò)高時(shí) ( too_hot = 1)打開(kāi)空調(diào),但不要打開(kāi)加熱器。當(dāng)加熱器或空調(diào)打開(kāi)時(shí),還要打開(kāi)風(fēng)扇使空氣流通。此外,用戶(hù)還可以要求風(fēng)扇開(kāi)啟(fan_on = 1),即使加熱器和空調(diào)關(guān)閉。
嘗試僅使用assign語(yǔ)句,看看是否可以將問(wèn)題描述轉(zhuǎn)換為邏輯門(mén)的集合。
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
人口計(jì)數(shù)
“人口計(jì)數(shù)”電路計(jì)算輸入向量中“1”的數(shù)量。為 3 位輸入向量構(gòu)建人口計(jì)數(shù)電路。
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
門(mén)和向量
您將獲得一個(gè)四位輸入向量。我們想知道每個(gè)位與其鄰居之間的一些關(guān)系:out_both: 本輸出向量的每一位應(yīng)該指示是否兩個(gè)相應(yīng)的輸入位和它的鄰居到左(較高的指數(shù))是“1”。例如,out_both[2]應(yīng)該表明in[2]和in[3]是否都為 1。由于in[3]左邊沒(méi)有鄰居,所以答案很明顯,所以我們不需要知道out_both[3]。out_any: 此輸出向量的每一位都應(yīng)指示是否有任何相應(yīng)的輸入位及其右側(cè)的鄰居為“1”。例如,out_any[2]應(yīng)該指示in[2]或in[1]是否為 1。由于in[0]右邊沒(méi)有鄰居,答案很明顯,所以我們不需要知道out_any[0 ]。out_different: 該輸出向量的每一位應(yīng)指示相應(yīng)的輸入位是否與其左側(cè)的相鄰位不同。例如,out_different[2]應(yīng)該指示in[2]是否與in[3] 不同。對(duì)于這部分,將向量視為環(huán)繞,因此in[3]左側(cè)的鄰居是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個(gè)門(mén)和向量
您將獲得一個(gè) 100 位的輸入向量。我們想知道每個(gè)位與其鄰居之間的一些關(guān)系:
out_both:本輸出向量的每一位應(yīng)該指示是否兩個(gè)相應(yīng)的輸入位和它的鄰居到左是“1”。例如,out_both[98]應(yīng)該表明in[98]和in[99]是否都為 1。由于in[99]左邊沒(méi)有鄰居,所以答案很明顯,所以我們不需要知道out_both[99 ]。out_any:此輸出向量的每一位都應(yīng)指示是否有任何相應(yīng)的輸入位及其右側(cè)的鄰居為“1”。例如,out_any[2]應(yīng)該指示in[2]或in[1]是否為 1。由于in[0]右邊沒(méi)有鄰居,答案很明顯,所以我們不需要知道out_any[0 ]。out_different:該輸出向量的每一位應(yīng)指示相應(yīng)的輸入位是否與其左側(cè)的相鄰位不同。例如,out_different[98]應(yīng)該指示in[98]是否與in[99] 不同。對(duì)于這部分,將向量視為環(huán)繞,因此in[99]左側(cè)的鄰居是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