4:2 compressor 的verliog代碼

來源:互聯網
上載者:User

所謂4:2 compressor,就是把4個數壓縮成兩個數,所示為一個4:2 compressor單元:

由圖可以看出把X1,X2,X3,X4這四個數壓縮成Sum與Carry這兩個數,其中,Cin,Cout是進位,且這些數滿足以下關係:

4:2 compressor的門級電路圖如所示:

對應的邏輯運算式為:

對應的verliog代碼實現:

module compressor #(parameter DATA_WIDTH = 8)(  input  [DATA_WIDTH - 1 : 0]  a,  input  [DATA_WIDTH - 1 : 0]  b,  input  [DATA_WIDTH - 1 : 0]  c,  input  [DATA_WIDTH - 1 : 0]  d,  input                        cin,  output [DATA_WIDTH - 1 : 0]  sum,  output [DATA_WIDTH - 1 : 0]  carry,  output                       cout  );    wire [DATA_WIDTH - 1 : 0] s_temp, cin_arry, cout_arry;    assign s_temp    = a ^ b ^ c;  assign cout_arry = (a ^ b) & c | a & b;  assign cin_arry  = {cout_arry[DATA_WIDTH - 2 : 0], cin};    assign sum   = s_temp ^ d ^ cin_arry;  assign carry = (s_temp ^ d) & cin_arry | s_temp & d;  assign cout  = cout_arry[DATA_WIDTH - 1];   endmodule

下面再貼一段對應的C語言代碼,可以驗證一下

#include <stdio.h>int main(){unsigned char x1 = 0, x2 = 0, x3 = 0, x4 = 0, cin = 0;unsigned char co_t, cin_t, temp;    unsigned char sum, carry, cout;    while (scanf("%d %d %d %d %d", &x1, &x2, &x3, &x4, &cin) != EOF){//x1, x2, x3, x4為0-255的無符號數, cin為0或1temp  = x1 ^ x2 ^ x3;    cout  = (x1^x2) & x3 | x1 & x2;co_t  = cout & 0x0080;co_t  = co_t << 1;cin_t = ( cout << 1 ) | cin;     sum   = temp ^ x4 ^ cin_t;        carry = (temp ^ x4) & cin_t | temp & x4;printf("cout = %d, carry = %d, sum = %d\n", cout >> 7, carry, sum);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.