简体   繁体   中英

XOR signal in verilog

I have a signal like this for example:
wire [18:0] A;

I need to XOR it with itself with multiple XOR gates that have up to 4 inputs. So this signal would be broken down like this:

XOR the first 4 bits [3:0] 
XOR the next 4 bits [7:4] 
XOR the next 4 bits [11:8] 
XOR the next 4 bits [15:12] 
XOR the remaining 3 bits together [18:16]

How do I XOR a signal with itself like this? Also, what is the best way to do this?

There are a couple of ways of doing this. One way could be to build a 4-input XOR module, and then instantiate multiple copies.

module myXOR4 ( input a, input b, input c, input d, output f);
    assign f = a ^ b ^ c ^ d; // ^ is the XOR operator
endmodule
// ...

myXOR4 xor1 (A[0],A[1],A[2],A[3],out[0]);
myXOR4 xor2 (A[4],A[5],A[6],A[7],out[1]);
// etc

Another way would be to use a for-loop. This won't work with all the cases, since you don't have an evenly-divisible number of wires.

reg [4:0] out;
integer i;
always@(*) begin
    for (i=0; i<4; i=i+1) 
        out[i]=A[4*i] ^ A[4*i+1] ^ A[4*i+2] ^ A[4*i+3];
    out[4] = A[18] ^ A[17] ^ A[16];
end

There are some other tricks (like 0-padding the upper-bits of A to make it evenly divisible, which makes the loops simpler) but they'll all end up with the same result, so this should be enough to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM