HDLBits答案(23)_找BUG
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                HDLBits答案(23)_找BUG
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                Finding bugs in code
HDLBits鏈接
前言
今天更新HDLBits習題部分找BUG部分,比較簡單,大家看一下即可。
題庫
8bit_2_1_Mux
原Code:
module top_module (input sel,input [7:0] a,input [7:0] b,output out );assign out = (~sel & a) | (sel & b);endmodule從上面的代碼中我們可以看到,輸出的out信號位寬不對,其次多路選擇的表達式有誤,按上圖代碼中的取法只能取出1bit。
Solution:
module top_module (input sel,input [7:0] a,input [7:0] b,output [7:0] out);assign out = sel ? a : b;endmoduleNAND
用5與非來實現3與非
可供調用的5與模塊:
module andgate ( output out, input a, input b, input c, input d, input e );原Code:
module top_module (input a, input b, input c, output out);//andgate inst1 ( a, b, c, out );endmodule由上面可供調用的模塊可見,輸入輸出的對應關系不對,且參數數量也不對,代碼改正如下:
Solution:
module top_module (input a, input b, input c, output out);//wire and_out;andgate inst1 ( and_out, a, b, c, 1, 1);assign out = ~and_out;endmodule8bit_4_1_Mux
提供了一個無BUG的2_1_Mux
module mux2 (input sel,input [7:0] a,input [7:0] b,output [7:0] out );待改代碼:
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 mux0, mux1;mux2 mux0 ( sel[0], a, b, mux0 );mux2 mux1 ( sel[1], c, d, mux1 );mux2 mux2 ( sel[1], mux0, mux1, out );endmodule分析:sel[1]區分不了c和d,此處應該還是sel[0]。此外例化名與變量名不能重復;且wire信號的位寬也不對。
Solution:
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] mux00, mux11;mux2 mux0 ( sel[0], a, b, mux00 );mux2 mux1 ( sel[0], c, d, mux11 );mux2 mux2 ( sel[1], mux00, mux11, out );endmoduleAdd/Sub
原代碼:
// 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 @(*) begincase (do_sub)0: out = a+b;1: out = a-b;endcaseif (~out)result_is_zero = 1;endendmodule因為result_is_zero為reg型,當其為1后一直為1,因為沒有其他狀態能使其改變,且需鎖存狀態,因為if未遍歷所有狀態。
Solution:
// 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 @(*) begincase (do_sub)0: out = a+b;1: out = a-b;endcaseif (out == 8'd0) beginresult_is_zero = 1;endelse beginresult_is_zero = 0;endendendmoduleCase statement
原代碼:
module top_module (input [7:0] code,output reg [3:0] out,output reg valid=1 );//always @(*)case (code)8'h45: out = 0;8'h16: out = 1;8'h1e: out = 2;8'd26: out = 3;8'h25: out = 4;8'h2e: out = 5;8'h36: out = 6;8'h3d: out = 7;8'h3e: out = 8;6'h46: out = 9;default: valid = 0;endcaseendmodule分析:默認輸出valid=1不能按上圖所示的編寫代碼,默認的輸入可以;其次進制8'd26需改成8進制。
Solution:
module top_module (input [7:0] code,output reg [3:0] out,output reg valid);//always @(*) begincase (code)8'h45: out = 4'd0;8'h16: out = 4'd1;8'h1e: out = 4'd2;8'h26: out = 4'd3;8'h25: out = 4'd4;8'h2e: out = 4'd5;8'h36: out = 4'd6;8'h3d: out = 4'd7;8'h3e: out = 4'd8;8'h46: out = 4'd9;default: beginout = 4'd0;endendcaseif(out == 4'd0 && code!= 8'h45) beginvalid = 1'b0;endelse beginvalid = 1'b1;endendendmodule結語
該小結就算更新結束了,找BUG部分的習題還挺簡單的,有誤的地方歡迎大家指正。
總結
以上是生活随笔為你收集整理的HDLBits答案(23)_找BUG的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: HDLBits答案(22)_基于有限状态
 - 下一篇: HDLBits答案(24)_由波形图描述