module rx_tb;
  parameter VCD_FILE = "rx_tb.vcd";
  parameter NUM_LANES = 1;
  parameter NUM_LINKS = 1;
  parameter OCTETS_PER_FRAME = 8;
  parameter FRAMES_PER_MULTIFRAME = 32;

  `include "tb_base.v"

  integer phy_reset_counter = 'h00;
  integer align_counter = 'h00;

  reg phy_ready = 1'b0;
  reg aligned = 1'b0;

  wire en_align;

  always @(posedge clk) begin
    if (reset == 1'b1) begin
      phy_reset_counter <= 'h00;
      phy_ready <= 1'b0;
    end else begin
      if (phy_reset_counter == 'h40) begin
        phy_ready <= 1'b1;
      end else begin
        phy_reset_counter <= phy_reset_counter + 1'b1;
      end
    end
  end

  always @(posedge clk) begin
    if (reset == 1'b1) begin
      aligned <= 1'b0;
      align_counter <= 'h00;
    end else if (phy_ready == 1'b1) begin
      if (en_align == 1'b1) begin
        if (align_counter == 'h20) begin
          aligned <= 1'b1;
        end else begin
          align_counter <= align_counter + 1;
        end
      end
    end
  end

  localparam KCHAR_ILAS_START = {3'd0,5'd28};
  localparam KCHAR_LANE_ALIGN = {3'd3,5'd28};
  localparam KCHAR_ILAS_CONFIG = {3'd4,5'd28};
  localparam KCHAR_CGS = {3'd5,5'd28};
  localparam KCHAR_FRAME_ALIGN = {3'd7,5'd28};

  reg [31:0] data = KCHAR_CGS;
  reg [3:0] charisk = 4'b1111;
  reg [3:0] disperr = 4'b0000;
  reg [3:0] notintable = 4'b0000;
  wire [NUM_LINKS-1:0] sync;

  integer counter = 'h00;
  wire [31:0] counter2 = (counter - 'h10) * 4;

  always @(posedge clk) begin
    if ( &sync == 1'b0 ) begin
      counter <= 'h00;
      charisk <= 4'b1111;
      data <= {KCHAR_CGS,KCHAR_CGS,KCHAR_CGS,KCHAR_CGS};
    end else begin
      counter <= counter + 1;
      if (counter >= 'h8 && counter < 'h28) begin
        if (counter % 'h8 == 'h0) begin
          if (counter != 'h10) begin
            data <= {24'h00,KCHAR_ILAS_START};
            charisk <= 4'b0001;
          end else begin
            data <= {16'hffaa,KCHAR_ILAS_CONFIG,KCHAR_ILAS_START};
            charisk <= 4'b0011;
          end
        end else if (counter % 'h8 == 'h7) begin
          data <= {KCHAR_LANE_ALIGN,24'h00};
          charisk <= 4'b1000;
        end else begin
          data <= {32'h00};
          charisk <= 4'b0000;
        end
      end else if (counter > 'h10) begin
        data <= (counter2 + 'h2) << 24 |
            (counter2 + 'h1) << 16 |
            (counter2 + 'h0) << 8 |
            (counter2 - 'h1);
        charisk <= 4'b0000;
      end
    end
  end

  wire [NUM_LANES-1:0] cfg_lanes_disable;
  wire [NUM_LINKS-1:0] cfg_links_disable;
  wire [7:0] cfg_beats_per_multiframe;
  wire [7:0] cfg_octets_per_frame;
  wire [7:0] cfg_lmfc_offset;
  wire cfg_sysref_oneshot;
  wire cfg_sysref_disable;
  wire cfg_disable_scrambler;
  wire cfg_buffer_early_release;

  always @(posedge clk) begin
    if ($urandom % 400 == 0)
      disperr <= 4'b1111;
    else if ($urandom % 400 == 1)
      disperr <= 4'b0001;
    else if ($urandom % 400 == 2)
      disperr <= 4'b0011;
    else if ($urandom % 400 == 3)
      disperr <= 4'b0111;
    else
      disperr <= 4'b0000;
  end
  wire [NUM_LANES*32-1:0] status_err_statistics_cnt;

  jesd204_rx_static_config #(
    .NUM_LANES(NUM_LANES),
    .NUM_LINKS(NUM_LINKS),
    .OCTETS_PER_FRAME(OCTETS_PER_FRAME),
    .FRAMES_PER_MULTIFRAME(FRAMES_PER_MULTIFRAME)
  ) i_cfg (
    .clk(clk),

    .cfg_lanes_disable(cfg_lanes_disable),
    .cfg_links_disable(cfg_links_disable),
    .cfg_beats_per_multiframe(cfg_beats_per_multiframe),
    .cfg_octets_per_frame(cfg_octets_per_frame),
    .cfg_lmfc_offset(cfg_lmfc_offset),
    .cfg_sysref_oneshot(cfg_sysref_oneshot),
    .cfg_sysref_disable(cfg_sysref_disable),
    .cfg_disable_scrambler(cfg_disable_scrambler),
    .cfg_buffer_early_release(cfg_buffer_early_release)
  );

  jesd204_rx #(
    .NUM_LANES(NUM_LANES),
    .NUM_LINKS(NUM_LINKS)
  ) i_rx (
    .clk(clk),
    .reset(reset),

    .cfg_lanes_disable(cfg_lanes_disable),
    .cfg_links_disable(cfg_links_disable),
    .cfg_beats_per_multiframe(cfg_beats_per_multiframe),
    .cfg_octets_per_frame(cfg_octets_per_frame),
    .cfg_lmfc_offset(cfg_lmfc_offset),
    .cfg_sysref_oneshot(cfg_sysref_oneshot),
    .cfg_sysref_disable(cfg_sysref_disable),
    .cfg_disable_scrambler(cfg_disable_scrambler),
    .cfg_buffer_early_release(cfg_buffer_early_release),

    .ctrl_err_statistics_reset(1'b0),
    .ctrl_err_statistics_mask(3'h7),

    .status_err_statistics_cnt(status_err_statistics_cnt),

    .phy_en_char_align(en_align),

    .phy_data({NUM_LANES{data}}),
    .phy_charisk({NUM_LANES{charisk}}),
    .phy_notintable({NUM_LANES{notintable}}),
    .phy_disperr({NUM_LANES{disperr}}),
    .sync(sync),
    .sysref(sysref)
  );

endmodule