现在的位置: 首页 > 综合 > 正文

4:2 compressor 的verliog代码

2013年08月18日 ⁄ 综合 ⁄ 共 1052字 ⁄ 字号 评论关闭

所谓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或1
		temp  = 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;
}

抱歉!评论已关闭.