Implement a 4-bit barrel shifter using SystemVerilog.

Sure, here’s an example of a 4-bit barrel shifter implemented using SystemVerilog:

module barrel_shifter_4bit(
  input logic [3:0] in,
  input logic [1:0] shift_amount,
  input logic shift_direction,
  output logic [3:0] out
);

  always_comb begin
    case (shift_direction)
      1'b0: out = {in[3-shift_amount[1]:0], in[3:4-shift_amount]};
      1'b1: out = {in[shift_amount], in[3:shift_amount]};
      default: out = in;
    endcase
  end

endmodule

In this code, we define a module called `barrel_shifter_4bit` with three input ports `in`, `shift_amount`, and `shift_direction`, and one output port `out`. The `in` input is a 4-bit binary value representing the data to be shifted, the `shift_amount` input is a 2-bit binary value representing the amount of shift to perform, and the `shift_direction` input is a single-bit binary value representing the direction of the shift (0 for left shift, 1 for right shift). The `out` output is a 4-bit binary value representing the shifted data.

The `case` statement uses the `shift_direction` input to select the appropriate shift direction. If `shift_direction` is 0, the code performs a left shift by concatenating the `shift_amount` least significant bits of the input value with the `4-shift_amount` most significant bits of the input value. If `shift_direction` is 1, the code performs a right shift by concatenating the `4-shift_amount` most significant bits of the input value with the `shift_amount` least significant bits of the input value. If `shift_direction` is neither 0 nor 1, the code outputs the original input value.

For example, if the input signal `in` is `4’b1100`, the `shift_amount` signal is `2’b10`, and the `shift_direction` signal is 0, the output signal `out` will be `4’b0011`, which represents the result of a left shift of 2 bits (`1100` -> `0011`).

You can instantiate this module in your top-level design hierarchy and connect it to other modules or input/output signals as needed. Note that you can modify the code to implement a barrel shifter with a different number of bits by changing the bit width of the input and output signals, and modifying the shift logic accordingly.