所謂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;}