描述:現在有7個裁判對選手進行評判,裁判可以選擇通過和不通過兩個狀態,(用0表示通過,用1通過),當選擇通過的裁判數目大于一半時(4或4以上)就讓選手晉級,否則淘汰(用1表示選手晉級,0表示選手淘汰),
測驗樣例:
| 輸入電平樣例 | 輸出電平樣例 |
|---|---|
| 0000000 | 0 |
| 11111111 | 1 |
| 0000111 | 0 |
一、綜合代碼
module decision_circuit_7(a,out);
input[6:0] a;
output out;
wire[2:0] out1;
add_7 ad(a,out1);
assign out=(out1>=4)?1:0;
endmodule
module add_7(a,out);
input[6:0] a;
output[2:0] out;
assign out=((a[0]+a[1])+(a[2]+a[3]))+((a[4]+a[5])+a[6]);
endmodule
二、測驗代碼
`timescale 1 ps/ 1 ps
module decision_circuit_7_vlg_tst();
reg [6:0] a;
wire out;
decision_circuit_7 i1 (
.a(a),
.out(out)
);
initial
begin
$display("Running testbench");
#0 a[0] = 1; a[1] = 1; a[2] = 1; a[3] = 1; a[4] = 1; a[5] = 1; a[6] = 1;
#1 $display("input:%b -- out:%b", a, out);
// 這里的輸出之前一定要加延遲,否則輸出的是上一時刻哦,
#9 a[0] = 1; a[1] = 1; a[2] = 1; a[3] = 1; a[4] = 1; a[5] = 1; a[6] = 0;
#1 $display("input:%b -- out:%b", a, out);
#9 a[0] = 1; a[1] = 1; a[2] = 1; a[3] = 1; a[4] = 1; a[5] = 0; a[6] = 0;
#1 $display("input:%b -- out:%b", a, out);
#9 a[0] = 1; a[1] = 1; a[2] = 1; a[3] = 1; a[4] = 0; a[5] = 0; a[6] = 0;
#1 $display("input:%b -- out:%b", a, out);
#9 a[0] = 1; a[1] = 1; a[2] = 1; a[3] = 0; a[4] = 0; a[5] = 0; a[6] = 0;
#1 $display("input:%b -- out:%b", a, out);
#9 a[0] = 1; a[1] = 1; a[2] = 0; a[3] = 0; a[4] = 0; a[5] = 0; a[6] = 0;
#1 $display("input:%b -- out:%b", a, out);
#9 a[0] = 1; a[1] = 0; a[2] = 0; a[3] = 0; a[4] = 0; a[5] = 0; a[6] = 0;
#1 $display("input:%b -- out:%b", a, out);
#9 a[0] = 0; a[1] = 0; a[2] = 0; a[3] = 0; a[4] = 0; a[5] = 0; a[6] = 0;
#1 $display("input:%b -- out:%b", a, out);
#9;// 這里的#9是為了讓波形持續10的單位,否則代碼會立即結束,最后一個波形只有一個瞬間
end
endmodule
三、測驗結果
控制臺輸出

波形圖

$display()沒有加延時的錯誤輸出

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/179095.html
標籤:其他
