module upack2_underflow_tb;
  parameter VCD_FILE = {`__FILE__,"cd"};
  parameter NUM_OF_CHANNELS = 8;
  parameter SAMPLES_PER_CHANNEL = 4;

  `include "tb_base.v"

  initial
  begin
    #1500000
    if (failed == 1'b0)
      $display("SUCCESS");
    else
      $display("FAILED");
    $finish;
  end

  localparam NUM_OF_PORTS = SAMPLES_PER_CHANNEL * NUM_OF_CHANNELS;

  reg fifo_rd_en = 1'b1;
  wire [NUM_OF_PORTS*8-1:0] fifo_rd_data;
  wire fifo_rd_valid;
  wire fifo_rd_underflow;

  reg s_axis_valid = 1'b1;
  wire s_axis_ready;
  reg [NUM_OF_PORTS*8-1:0] s_axis_data = {NUM_OF_PORTS*8{1'b1}};

  reg [NUM_OF_CHANNELS-1:0] enable = {NUM_OF_CHANNELS{1'b1}};
  integer counter = 0;

  always @(posedge clk) begin
    if (fifo_rd_underflow == 1'b1 && fifo_rd_data != 'h00) begin
      failed <= 1'b1;
    end
    if (fifo_rd_valid == 1'b1 && fifo_rd_data != {NUM_OF_PORTS*8{1'b1}}) begin
      failed <= 1'b1;
    end
  end

  always @(posedge clk) begin
    if (reset == 1'b1) begin
      counter <= 0;
      s_axis_valid <= 1'b1;
    end else begin
      if (s_axis_valid == 1'b0) begin
        if (counter == 8) begin
          s_axis_valid <= 1'b1;
        end
        counter <= counter + 1;
      end else if (s_axis_ready == 1'b1) begin
        s_axis_valid <= 1'b0;
        counter <= 0;
      end
    end
  end

  always @(posedge clk) begin
    fifo_rd_en <= $random & 1;
  end

  util_upack2_impl #(
    .NUM_OF_CHANNELS(NUM_OF_CHANNELS),
    .SAMPLES_PER_CHANNEL(SAMPLES_PER_CHANNEL),
    .SAMPLE_DATA_WIDTH(8)
  ) i_unpack (
    .clk(clk),
    .reset(reset),

    .enable(enable),

    .fifo_rd_en({NUM_OF_CHANNELS{fifo_rd_en}}),
    .fifo_rd_data(fifo_rd_data),
    .fifo_rd_valid(fifo_rd_valid),
    .fifo_rd_underflow(fifo_rd_underflow),

    .s_axis_valid(s_axis_valid),
    .s_axis_ready(s_axis_ready),
    .s_axis_data(s_axis_data)
  );

endmodule