There are both a simple byte endian (little and big) order swapper and its testbench. A data stream inputs to the module and is converted to the other endianness by computational logic.
byte_order_swap.v
`timescale 1ns / 1ps
module byte_order_swap #
(
parameter integer DATA_WIDTH = 32
)
(
input wire [DATA_WIDTH - 1 : 0] data_i,
input wire [DATA_WIDTH - 1 : 0] data_o
);
localparam integer DATA_BYTE_NUMBER = DATA_WIDTH / 8;
localparam integer DEC_DATA_WIDTH = DATA_WIDTH - 1;
generate
genvar i;
if (0 == (DATA_WIDTH % 4)) begin
for(i = 0; i < DATA_BYTE_NUMBER; i = i + 1) begin
assign data_o[(i * 8) +: 8] = data_i[(DEC_DATA_WIDTH - i * 8) -: 8];
end
end
else begin
assign data_o = {DATA_WIDTH{1'h0}};
end
endgenerate
endmodule
byte_order_swap_tb.v
`timescale 1ns / 1ps
module byte_order_swap_tb;
localparam integer DATA_WIDTH = 32;
localparam integer CLOCK_PERIOD = 100;
localparam integer ITERATION_NUMBER = 1000;
localparam [DATA_WIDTH - 1 : 0] COUNTER_START_VALUE = 32'hAABB1122;
wire [DATA_WIDTH - 1 : 0] counter_swap_value;
reg clk;
reg [DATA_WIDTH - 1 : 0] counter_dir_value;
byte_order_swap #
(
.DATA_WIDTH (DATA_WIDTH)
)
byte_order_swap_dut
(
.data_i (counter_dir_value),
.data_o (counter_swap_value)
);
initial begin
clk = 1'h0;
forever begin
#( CLOCK_PERIOD / 2 ) clk = !clk;
end
end
initial begin
counter_dir_value <= COUNTER_START_VALUE;
repeat(ITERATION_NUMBER) begin
@(posedge clk);
counter_dir_value <= counter_dir_value + 1'h1;
end
end
task check_swap;
begin
repeat(ITERATION_NUMBER) begin
@(posedge clk);
$display("A direction value: %h -> the swap value: %h",counter_dir_value, counter_swap_value, $time);
end
end
endtask
initial begin
check_swap;
$stop();
end
endmodule