日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDLBits答案(23)_找BUG

發布時間:2023/12/19 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;endmodule

NAND

用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;endmodule

8bit_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 );endmodule

Add/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;endendendmodule

Case 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。