HDLBits – Gates4

Build a combinational circuit with four inputs, in[3:0].

There are 3 outputs:

out_and: output of a 4-input AND gate.
out_or: output of a 4-input OR gate.
out_xor: output of a 4-input XOR gate.

https://hdlbits.01xz.net/wiki/Gates4

The point of this exercise is to demonstrate the use of the reduction operators, which can be used to prevent having to make logic assignments such as the following:

assign out_and = in[3] & in[2] & in[1] & in[0];

The following table summarizes the available reduction operators. Note that unlike with the bitwise operators, ~| and ~& can be used for NOR and NAND, respectively.

OperationReduction Operator
AND&
NAND~&
OR|
NOR~|
XOR^
XNOR~|
Table 1 : Available Reduction Operators

Using these operators, a simple solution is as follows:

module top_module (
  input  logic [3:0] in,
  output logic out_and,
  output logic out_or,
  output logic out_xor );
  // Reduction gate output assignments
  assign out_and = ∈
  assign out_or  = |in;
  assign out_xor = ^in;
endmodule : top_module

Leave a Reply

Your email address will not be published. Required fields are marked *