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