要求:给定五个 1 位信号(a、b、c、d 和 e),计算 25 位输出向量中的所有 25 个成对一比特比较。如果要比较的两个位相等,则输出应为 1。
我的代码
module top_module (
input a, b, c, d, e,
output [24:0] out );//
assign out[24:20] = ~({5{a}}^{a,b,c,d,e});
assign out[19:15] = ~({5{b}}^{a,b,c,d,e});
assign out[14:10] = ~({5{c}}^{a,b,c,d,e});
assign out[9:5] = ~({5{d}}^{a,b,c,d,e});
assign out[4:0] = ~({5{e}}^{a,b,c,d,e});
// The output is XNOR of two vectors created by
// concatenating and replicating the five inputs.
// assign out = ~{ ... } ^ { ... };
endmodule
示例代码:
module top_module (
input a, b, c, d, e,
output [24:0] out
);
wire [24:0] top, bottom;
assign top = { {5{a}}, {5{b}}, {5{c}}, {5{d}}, {5{e}} };
assign bottom = {5{a,b,c,d,e}};
assign out = ~top ^ bottom; // Bitwise XNOR
// This could be done on one line:
// assign out = ~{ {5{a}}, {5{b}}, {5{c}}, {5{d}}, {5{e}} } ^ {5{a,b,c,d,e}};
endmodule
总结:思路要活,不要怕产生中间变量。