Bugs mux2 原本代碼的邏輯是反的,這不是坑人嗎。 module top_module ( input sel, input [7:0] a, input [7:0] b, output [7:0]out ); assign out = ({8{sel}} & a) | ({8{~sel}} ...
Bugs mux2
原本代碼的邏輯是反的,這不是坑人嗎。
module top_module ( input sel, input [7:0] a, input [7:0] b, output [7:0]out ); assign out = ({8{sel}} & a) | ({8{~sel}} & b); endmodule
Bugs nand3
五輸入的與門現在要實現三輸入的與非門,多餘的門可以輸入1並將輸出取反。
module top_module (input a, input b, input c, output out);// wire out_n; andgate inst1 ( out_n,a, b, c, 1'b1,1'b1 ); assign out = ~out_n; endmodule
Bugs mux4
bug1:mux0和mux1的位寬沒設置,預設是1;
bug2:選通引腳有問題,應該先通過mux[0]判斷是ac還是bd,再通過mux[1]進行判斷。
module top_module ( input [1:0] sel, input [7:0] a, input [7:0] b, input [7:0] c, input [7:0] d, output [7:0] out ); // wire [7:0]mux0, mux1; mux2 u_mux0 ( sel[0], a, b, mux0 ); mux2 u_mux1 ( sel[0], c, d, mux1 ); mux2 u_mux2 ( sel[1], mux0, mux1, out ); endmodule
Bugs addsubz
verilog中~是按位取反,!是邏輯取反。
同時需要補充out不為0的情況,否則輸出會預設保持,綜合出latch。
// synthesis verilog_input_version verilog_2001 module top_module ( input do_sub, input [7:0] a, input [7:0] b, output reg [7:0] out, output reg result_is_zero );// always @(*) begin case (do_sub) 0: out = a+b; 1: out = a-b; endcase if (!out) result_is_zero = 1; else result_is_zero = 0; end endmodule
Bugs case
這道題比較考驗眼力,一個是d要改成h,還有一個是6位改成8位。暈。
還有就是先給兩個輸出賦預設值,就不會綜合出latch了,而且代碼也更加簡潔。
module top_module ( input [7:0] code, output reg [3:0] out, output reg valid );// always @(*) begin out = 0; valid = 1'b1; case (code) 8'h45: out = 0; 8'h16: out = 1; 8'h1e: out = 2; 8'h26: out = 3; 8'h25: out = 4; 8'h2e: out = 5; 8'h36: out = 6; 8'h3d: out = 7; 8'h3e: out = 8; 8'h46: out = 9; default: valid = 0; endcase end endmodule