I am a beginner in FPGAs, and I am studying Verilog HDL. Could you please check the quality of my code (a shift register)? Any comments are welcome.
The shift register serializes parallel from wr_data_i
data to serial_data_o
and has two variations of first serialized bit. It may be most significant bit or least significant bit, which is defined in parameters as TRUE
or FALSE
.
`timescale 1ns / 1ps
module shift_reg #
(
parameter integer DATA_WIDTH = 16,
parameter integer DO_MSB_FIRST = "TRUE"
)
(
input wire clk_i,
input wire s_rst_n_i,
input wire enable,
input wire wr_enable,
input wire [DATA_WIDTH - 1 : 0] wr_data_i,
output wire serial_data_o
);
localparam integer MSB = DATA_WIDTH - 1;
localparam integer LSB = 0;
localparam integer XSB = ("TRUE" == DO_MSB_FIRST) ? MSB : LSB;
reg [DATA_WIDTH - 1 : 0] parallel_data;
assign serial_data_o = (1'h1 == enable) ? parallel_data[XSB] : 1'hz;
always @ (posedge clk_i)
begin
if (1'h0 == s_rst_n_i)
begin
parallel_data <= 'h0;
end
else if (1'h1 == wr_enable)
begin
parallel_data <= wr_data_i;
end
else if (1'h1 == enable)
begin
if ("TRUE" == DO_MSB_FIRST)
begin
parallel_data <= {parallel_data[MSB - 1 : 0], 1'h0};
end
else
begin
parallel_data <= {1'h0, parallel_data[MSB: 1]};
end
end
end
endmodule