基於FPGA的4位減法器結構化設計


1. 設計要求:

  設計一個4位減法器,采用結構化設計。該設計名為sub_4bit,其頂層設計如下圖所示,有三個輸入量:被減數x[3:0],減數y[3:0],低位向本位的借位bin;有兩個輸出量本位差dif[3:0],本位向高位的借位bout。數學表達式為:(x + bout) – y – bin = diff

2. 設計實現

  4位減法器由四個全減器構成,1位全減器由兩個半減器和1個或門構成,如下圖所示。

(1) 半減器:

  半減器用於計算兩個二進制數x和y的劍法,輸出結果d和向高位的借位bo,沒有考慮低位向本位的借位。

(2) 全減器

  全加器有三個輸入量:被減數Ai,減數Bi,低位向本位的借位Ci-1;有兩個輸出量本位差Si,本位向高位的借位Ci。其真值表如下所示:

(3)4位減法器由四個1位全減器通過串行級聯構成,本機的借位作為下一級的借位輸入。按照數學知識,減法是從最低位開始,依次向高位計算,這樣做的原因是,高位不知道低位的2個數相減,有沒有借位。所以借法計算,只能是從低到高依次計算。

3. 設計實現

(1)4位減法器設計與實現

 1 module sub_4bit(
 2     input     wire        [3:0]        x,
 3     input     wire         [3:0]        y,
 4     input     wire                     bin,
 5     
 6     output     wire         [3:0]        dif,
 7     output     wire                     bout
 8 );
 9 
10     wire                         bout1;
11     wire                        bout2;
12     wire                        bout3;
13 
14     full_sub full_sub_inst0(
15         .x                    (x[0]),
16         .y                    (y[0]),
17         .bin                (bin),
18 
19         .dif                (dif[0]),
20         .bout                (bout1)
21     );
22     
23     full_sub full_sub_inst1(
24         .x                    (x[1]),
25         .y                    (y[1]),
26         .bin                (bout1),
27 
28         .dif                (dif[1]),
29         .bout                (bout2)
30     );
31     
32     full_sub full_sub_inst2(
33         .x                    (x[2]),
34         .y                    (y[2]),
35         .bin                (bout2),
36 
37         .dif                (dif[2]),
38         .bout                (bout3)
39     );
40     
41     full_sub full_sub_inst3(
42         .x                    (x[3]),
43         .y                    (y[3]),
44         .bin                (bout3),
45 
46         .dif                (dif[3]),
47         .bout                (bout)
48     );    
49         
50 endmodule 

(2)全減器設計與實現

 1 module full_sub(
 2     input     wire                    x,
 3     input     wire                     y,
 4     input     wire                     bin,
 5     
 6     output     wire                     dif,
 7     output     wire                     bout
 8 );
 9 
10     wire                     dif_xy;
11     wire                    bout_xy;
12     wire                     bout_xybin;
13     
14     half_sub half_sub_inst0(
15         .x                    (x),
16         .y                    (y),
17 
18         .dif                (dif_xy),
19         .bout                (bout_xy)
20     );
21     
22     half_sub half_sub_inst1(
23         .x                    (dif_xy),
24         .y                    (bin),
25 
26         .dif                (dif),
27         .bout                (bout_xybin)
28     );
29     
30     assign bout = bout_xy | bout_xybin;
31 
32 endmodule 

(3)半減器設計與實現

 1 module half_sub(
 2     input     wire                    x,
 3     input     wire                     y,
 4     
 5     output     wire                     dif,
 6     output     wire                     bout
 7 );
 8 
 9     assign dif     = x ^ y;
10     assign bout = (~x) & y;
11 
12 endmodule 

4. 仿真驗證

 1 `timescale 1ns/1ps
 2 
 3 module sub_4bit_tb();
 4 
 5     reg            [3:0]        x;
 6     reg             [3:0]        y;
 7     reg                         bin;
 8     
 9     wire             [3:0]        dif;
10     wire                         bout;
11 
12     sub_4bit sub_4bit_inst(
13         .x                    (x),
14         .y                    (y),
15         .bin                (bin),
16 
17         .dif                (dif),
18         .bout                (bout)
19     );
20     
21     initial begin
22         repeat(20)begin
23             x         = {$random}%16;
24             y         = {$random}%16;
25             bin     = 1'b0;
26             #20;
27         end
28     end
29     
30 endmodule 

5. 參考資料

(1)【HDL系列】半減器、全減器和減法器原理和設計 - 知乎 (zhihu.com)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM