`resetall
`timescale 1ns / 1ps
`default_nettype none

module digital_rx_chain #
(
    parameter CTRL_REG_ADDR = 32'h00000000,
    parameter NUM_SAMPLES_REG_ADDR = 32'h00000004,
    parameter START_SAMPLE_REG_ADDR = 32'h00000008,
    parameter DEC_SELECT_REG_ADDR = 32'h0000000C,
    
    parameter integer AXI_ADDR_WIDTH	= 32,
    parameter integer AXI_DATA_WIDTH	= 32,
    parameter integer JESD_DATA_WIDTH	= 64
//    parameter integer JESD_DATA_WIDTH	= 32       
)
(
    input  wire       clk,

    // AXI4L Config Interface
    axi4l_intf.slave ctrl_if,
    
    input  wire        start_of_pulse,
    
    // Input Data
    input  wire        in_tvalid,
    input  wire [JESD_DATA_WIDTH-1:0] in_tdata_i,
    input  wire [JESD_DATA_WIDTH-1:0] in_tdata_q,
    
    // Output Data
    axi4s_intf.master    rx_out

);

reg        out_tvalid_r;
wire       out_tlast_r;
wire       out_tstart_r;
reg [63:0] out_tdata_i_r;
reg [63:0] out_tdata_q_r;

// ------------------------------
// Register Inputs for timing
// ------------------------------
reg        in_tstart_reg;
reg        in_tvalid_reg;
reg [JESD_DATA_WIDTH-1:0] in_tdata_i_reg;
reg [JESD_DATA_WIDTH-1:0] in_tdata_q_reg;
always @ (posedge clk) begin
    in_tstart_reg <= start_of_pulse;
    in_tvalid_reg <= in_tvalid;
//    in_tdata_i_reg <= in_tdata_i;
//    in_tdata_q_reg <= in_tdata_q;

    in_tdata_i_reg[15:0]     = in_tdata_i[63:48];
    in_tdata_i_reg[31:16]    = in_tdata_i[47:32];
    in_tdata_i_reg[47:32]    = in_tdata_i[31:16];
    in_tdata_i_reg[63:48]    = in_tdata_i[15:0];
    
    in_tdata_q_reg[15:0]     = in_tdata_q[63:48];
    in_tdata_q_reg[31:16]    = in_tdata_q[47:32];
    in_tdata_q_reg[47:32]    = in_tdata_q[31:16];
    in_tdata_q_reg[63:48]    = in_tdata_q[15:0];   
    
end


// ------------------------------
// AXIL Decode
// ------------------------------
wire [AXI_ADDR_WIDTH-1 : 0]		raddr;
wire [AXI_ADDR_WIDTH-1 : 0]		waddr;
wire 						    rden;
wire 						    wren;
wire [AXI_DATA_WIDTH-1 : 0]		wdata;
reg [AXI_DATA_WIDTH-1 : 0]		rdata;


axil_slave 
# (
	.DATA_WIDTH(AXI_DATA_WIDTH),
	.ADDR_WIDTH(AXI_ADDR_WIDTH)
) axil_slave_i 
(
	// AXIL Slave
	.S_AXI_ACLK(ctrl_if.clk),
	.S_AXI_ARESETN(ctrl_if.resetn),
	.S_AXI_AWADDR(ctrl_if.awaddr),
	.S_AXI_AWPROT(ctrl_if.awprot),
	.S_AXI_AWVALID(ctrl_if.awvalid),
	.S_AXI_AWREADY(ctrl_if.awready),
	.S_AXI_WDATA(ctrl_if.wdata),
	.S_AXI_WSTRB(ctrl_if.wstrb),
	.S_AXI_WVALID(ctrl_if.wvalid),
	.S_AXI_WREADY(ctrl_if.wready),
	.S_AXI_BRESP(ctrl_if.bresp),
	.S_AXI_BVALID(ctrl_if.bvalid),
	.S_AXI_BREADY(ctrl_if.bready),
	.S_AXI_ARADDR(ctrl_if.araddr),
	.S_AXI_ARPROT(ctrl_if.arprot),
	.S_AXI_ARVALID(ctrl_if.arvalid),
	.S_AXI_ARREADY(ctrl_if.arready),
	.S_AXI_RDATA(ctrl_if.rdata),
	.S_AXI_RRESP(ctrl_if.rresp),
	.S_AXI_RVALID(ctrl_if.rvalid),
	.S_AXI_RREADY(ctrl_if.rready),	
	
	.raddr(raddr),
	.waddr(waddr),
	.wren(wren),
    .rden(rden),
	.wdata(wdata),
	.rdata(rdata)
);

// ------------------------------
// Config Registers
// ------------------------------
wire reset;
reg [31:0]  reg_ctrl;
reg [15:0]  reg_num_samples;
reg [27:0]  reg_start_sample;
reg [3:0]   reg_dec_select;

always @ (posedge ctrl_if.clk) begin
    if (~ctrl_if.resetn) begin
        reg_ctrl <= 0;
    end else if (wren && waddr[11:0] == CTRL_REG_ADDR) begin
        reg_ctrl <= wdata[15:0];
    end
end

always @ (posedge ctrl_if.clk) begin
    if (~ctrl_if.resetn) begin
        reg_num_samples <= 0;
    end else if (wren && waddr[11:0] == NUM_SAMPLES_REG_ADDR) begin
        reg_num_samples <= wdata[15:0];
    end
end

always @ (posedge ctrl_if.clk) begin
    if (~ctrl_if.resetn) begin
        reg_start_sample <= 0;
    end else if (wren && waddr[11:0] == START_SAMPLE_REG_ADDR) begin
        reg_start_sample <= wdata;
    end
end

always @ (posedge ctrl_if.clk) begin
    if (~ctrl_if.resetn) begin
        reg_dec_select <= 0;
    end else if (wren && waddr[11:0] == DEC_SELECT_REG_ADDR) begin
        reg_dec_select <= wdata;
    end
end

always @ (posedge ctrl_if.clk) begin
    if (rden) begin
        if ( raddr[11:0] == CTRL_REG_ADDR ) 
            rdata <= reg_ctrl;       
        if ( raddr[11:0] == NUM_SAMPLES_REG_ADDR ) 
            rdata <= reg_num_samples;   
        if ( raddr[11:0] == START_SAMPLE_REG_ADDR ) 
            rdata <= reg_start_sample;   
        if ( raddr[11:0] == DEC_SELECT_REG_ADDR ) 
            rdata <= reg_dec_select;               
    end      
end

assign reset = reg_ctrl[0];


// ------------------------------
// Event Counter
// ------------------------------
reg [15:0] event_cnt;

always @ (posedge clk) begin
   if (reset == 1'b1) begin
       event_cnt <= 0;
   end else begin
      if (in_tstart_reg) begin
         event_cnt <= event_cnt + 1;
      end
   end
end

// ------------------------------
// Sample gating
// ------------------------------
reg [16:0] sample_cnt;
reg pulse_active;
reg pulse_active_q;
reg pulse_active_fed;
reg pulse_active_fed_q;
reg pulse_active_fed_stretch;

// Delay event pulse by start sample
reg [27:0] start_sample_cnt;
reg [27:0] start_sample_this_pulse;
reg delay_active;
reg delay_active_q;
reg delay_active_fed;

always @ (posedge clk) begin
    if (reset == 1'b1) begin
        start_sample_cnt <= 0;
        delay_active <= 0;
    end else begin

        delay_active_q <= delay_active;
        delay_active_fed <= ~delay_active && delay_active_q;

        if (in_tstart_reg && in_tvalid_reg) begin
            start_sample_cnt <= reg_start_sample;
            start_sample_this_pulse <= reg_start_sample;
            delay_active <= 1;
        end

        if (delay_active) begin
            start_sample_cnt <= start_sample_cnt - 1;
            if (start_sample_cnt == 0) begin
                delay_active <= 0;
            end
        end

    end
end

// Reset for filters after pulse
assign pulse_active_fed_stretch = pulse_active_fed || pulse_active_fed_q;

always @ (posedge clk) begin
    if (reset == 1'b1) begin
        sample_cnt <= 0;
        pulse_active <= 0;
        pulse_active_q <= 0;
        pulse_active_fed <= 1;
        pulse_active_fed_q <= 1;
        
    end else begin
       
        pulse_active_q <= pulse_active;
        pulse_active_fed <= ~pulse_active && pulse_active_q;
        pulse_active_fed_q <= pulse_active_fed;

        if (delay_active_fed && in_tvalid_reg) begin
            sample_cnt <= 0;
            pulse_active <= 1;
        end;
            
        if (out_tvalid_r) begin
            sample_cnt <= sample_cnt + 1;
            if (sample_cnt == reg_num_samples-1) begin
                sample_cnt <= 0;
                pulse_active <= 0;
            end
        end
    end
end

assign out_tlast_r = ((sample_cnt == reg_num_samples-1) && out_tvalid_r) ? 1'b1 : 1'b0;
assign out_tstart_r = ((sample_cnt == 0) && out_tvalid_r) ? 1'b1 : 1'b0;
//assign out_tvalid_r = in_tvalid_reg && (pulse_active || delay_active_fed);


// ------------------------------
// Decimation
// ------------------------------
wire         dec_by_2_tvalid_i;
wire         dec_by_2_tvalid_q;
wire  [95:0] dec_by_2_tdata_i;
wire  [95:0] dec_by_2_tdata_q;
wire  [63:0] dec_by_2_tdata_i_slice;
wire  [63:0] dec_by_2_tdata_q_slice;

wire         dec_by_4_tvalid_i;
wire         dec_by_4_tvalid_q;
wire  [95:0] dec_by_4_tdata_i;
wire  [95:0] dec_by_4_tdata_q;
wire  [63:0] dec_by_4_tdata_i_slice;
wire  [63:0] dec_by_4_tdata_q_slice;

wire         dec_by_8_tvalid_i;
wire         dec_by_8_tvalid_q;
wire  [95:0] dec_by_8_tdata_i;
wire  [95:0] dec_by_8_tdata_q;
wire  [63:0] dec_by_8_tdata_i_slice;
wire  [63:0] dec_by_8_tdata_q_slice;

wire         dec_by_16_tvalid_i;
wire         dec_by_16_tvalid_q;
wire  [95:0] dec_by_16_tdata_i;
wire  [95:0] dec_by_16_tdata_q;
wire  [63:0] dec_by_16_tdata_i_slice;
wire  [63:0] dec_by_16_tdata_q_slice;

wire dec_in_valid;


assign dec_in_valid = in_tvalid_reg && (pulse_active || delay_active_fed);


decimation_bd decimation_bd_i
   (.rstn(~pulse_active_fed_stretch),
    .clk(clk),
    .in_i_tdata(in_tdata_i_reg),
    .in_i_tready(),
    .in_i_tvalid(dec_in_valid),
    .dec_by_2_tdata(dec_by_2_tdata_i),
    .dec_by_2_tvalid(dec_by_2_tvalid_i),
    .dec_by_2_tready(1'b1),
    .dec_by_4_tdata(dec_by_4_tdata_i),
    .dec_by_4_tvalid(dec_by_4_tvalid_i),
    .dec_by_4_tready(1'b1),
    .dec_by_8_tdata(dec_by_8_tdata_i),
    .dec_by_8_tvalid(dec_by_8_tvalid_i),
    .dec_by_8_tready(1'b1),
    .dec_by_16_tdata(dec_by_16_tdata_i),
    .dec_by_16_tvalid(dec_by_16_tvalid_i),
    .dec_by_16_tready(1'b1)        
    );

decimation_bd decimation_bd_q
   (.rstn(~pulse_active_fed_stretch),
    .clk(clk),
    .in_i_tdata(in_tdata_q_reg),
    .in_i_tready(),
    .in_i_tvalid(dec_in_valid),
    .dec_by_2_tdata(dec_by_2_tdata_q),
    .dec_by_2_tvalid(dec_by_2_tvalid_q),
    .dec_by_2_tready(1'b1),
    .dec_by_4_tdata(dec_by_4_tdata_q),
    .dec_by_4_tvalid(dec_by_4_tvalid_q),
    .dec_by_4_tready(1'b1),
    .dec_by_8_tdata(dec_by_8_tdata_q),
    .dec_by_8_tvalid(dec_by_8_tvalid_q),
    .dec_by_8_tready(1'b1),
    .dec_by_16_tdata(dec_by_16_tdata_q),
    .dec_by_16_tvalid(dec_by_16_tvalid_q),
    .dec_by_16_tready(1'b1)
    );    

assign dec_by_2_tdata_i_slice[15:0]  = dec_by_2_tdata_i[15:0];
assign dec_by_2_tdata_i_slice[31:16] = dec_by_2_tdata_i[39:24];
assign dec_by_2_tdata_i_slice[47:32] = dec_by_2_tdata_i[63:48];
assign dec_by_2_tdata_i_slice[63:48] = dec_by_2_tdata_i[87:72];
assign dec_by_2_tdata_q_slice[15:0]  = dec_by_2_tdata_q[15:0];
assign dec_by_2_tdata_q_slice[31:16] = dec_by_2_tdata_q[39:24];
assign dec_by_2_tdata_q_slice[47:32] = dec_by_2_tdata_q[63:48];
assign dec_by_2_tdata_q_slice[63:48] = dec_by_2_tdata_q[87:72];

assign dec_by_4_tdata_i_slice[15:0]  = dec_by_4_tdata_i[15:0];
assign dec_by_4_tdata_i_slice[31:16] = dec_by_4_tdata_i[39:24];
assign dec_by_4_tdata_i_slice[47:32] = dec_by_4_tdata_i[63:48];
assign dec_by_4_tdata_i_slice[63:48] = dec_by_4_tdata_i[87:72];
assign dec_by_4_tdata_q_slice[15:0]  = dec_by_4_tdata_q[15:0];
assign dec_by_4_tdata_q_slice[31:16] = dec_by_4_tdata_q[39:24];
assign dec_by_4_tdata_q_slice[47:32] = dec_by_4_tdata_q[63:48];
assign dec_by_4_tdata_q_slice[63:48] = dec_by_4_tdata_q[87:72];

assign dec_by_8_tdata_i_slice[15:0]  = dec_by_8_tdata_i[15:0];
assign dec_by_8_tdata_i_slice[31:16] = dec_by_8_tdata_i[39:24];
assign dec_by_8_tdata_i_slice[47:32] = dec_by_8_tdata_i[63:48];
assign dec_by_8_tdata_i_slice[63:48] = dec_by_8_tdata_i[87:72];
assign dec_by_8_tdata_q_slice[15:0]  = dec_by_8_tdata_q[15:0];
assign dec_by_8_tdata_q_slice[31:16] = dec_by_8_tdata_q[39:24];
assign dec_by_8_tdata_q_slice[47:32] = dec_by_8_tdata_q[63:48];
assign dec_by_8_tdata_q_slice[63:48] = dec_by_8_tdata_q[87:72];

assign dec_by_16_tdata_i_slice[15:0]  = dec_by_16_tdata_i[15:0];
assign dec_by_16_tdata_i_slice[31:16] = dec_by_16_tdata_i[39:24];
assign dec_by_16_tdata_i_slice[47:32] = dec_by_16_tdata_i[63:48];
assign dec_by_16_tdata_i_slice[63:48] = dec_by_16_tdata_i[87:72];
assign dec_by_16_tdata_q_slice[15:0]  = dec_by_16_tdata_q[15:0];
assign dec_by_16_tdata_q_slice[31:16] = dec_by_16_tdata_q[39:24];
assign dec_by_16_tdata_q_slice[47:32] = dec_by_16_tdata_q[63:48];
assign dec_by_16_tdata_q_slice[63:48] = dec_by_16_tdata_q[87:72];

// ------------------------------
// Decimation Selection
// ------------------------------
always @ * begin
    case(reg_dec_select)
        4'b000  : out_tvalid_r = in_tvalid_reg && (pulse_active || delay_active_fed);
        4'b001  : out_tvalid_r = dec_by_2_tvalid_i;
        4'b010  : out_tvalid_r = dec_by_4_tvalid_i;
        4'b011  : out_tvalid_r = dec_by_8_tvalid_i;
        4'b100  : out_tvalid_r = dec_by_16_tvalid_i;
        default : out_tvalid_r = 1'b0;
    endcase
end

always @ * begin
    case(reg_dec_select)
        4'b000  : out_tdata_i_r = in_tdata_i_reg;
        4'b001  : out_tdata_i_r = dec_by_2_tdata_i_slice;
        4'b010  : out_tdata_i_r = dec_by_4_tdata_i_slice;
        4'b011  : out_tdata_i_r = dec_by_8_tdata_i_slice;
        4'b100  : out_tdata_i_r = dec_by_16_tdata_i_slice;
        default : out_tdata_i_r = 1'b0;
    endcase
end

always @ * begin
    case(reg_dec_select)
        4'b000  : out_tdata_q_r = in_tdata_q_reg;
        4'b001  : out_tdata_q_r = dec_by_2_tdata_q_slice;
        4'b010  : out_tdata_q_r = dec_by_4_tdata_q_slice;
        4'b011  : out_tdata_q_r = dec_by_8_tdata_q_slice;
        4'b100  : out_tdata_q_r = dec_by_16_tdata_q_slice;
        default : out_tdata_q_r = 1'b0;
    endcase
end

// ------------------------------
// Buffer
// ------------------------------
axi4s_intf # (
    .AXI_DATA_WIDTH(JESD_DATA_WIDTH * 2),
    .AXI_USER_WIDTH(1)     
 ) 
 axis_odec_out (
    .clk(clk),
    .resetn(~reset)
 ); 

 axi4s_intf # (
    .AXI_DATA_WIDTH(JESD_DATA_WIDTH * 2),
    .AXI_USER_WIDTH(1)     
 ) 
 axis_pulse_buffer_out (
    .clk(clk),
    .resetn(~reset)
 );   

 axi4s_intf # (
    .AXI_DATA_WIDTH(64),
    .AXI_USER_WIDTH(8)     
 ) 
 axis_dwidth_conv_out (
    .clk(clk),
    .resetn(~reset)
 );      
   
assign axis_odec_out.tvalid = out_tvalid_r && (pulse_active || delay_active_fed);
assign axis_odec_out.tlast = out_tlast_r;
assign axis_odec_out.tuser = out_tstart_r;

assign axis_odec_out.tdata[15:0]     = out_tdata_i_r[15:0];
assign axis_odec_out.tdata[31:16]    = out_tdata_q_r[15:0];
assign axis_odec_out.tdata[47:32]    = out_tdata_i_r[31:16];
assign axis_odec_out.tdata[63:48]    = out_tdata_q_r[31:16];
assign axis_odec_out.tdata[79:64]    = out_tdata_i_r[47:32];
assign axis_odec_out.tdata[95:80]    = out_tdata_q_r[47:32];
assign axis_odec_out.tdata[111:96]   = out_tdata_i_r[63:48];
assign axis_odec_out.tdata[127:112]  = out_tdata_q_r[63:48];         

//assign axis_odec_out.tdata[15:0]     = in_tdata_i_reg[63:48];
//assign axis_odec_out.tdata[31:16]    = in_tdata_q_reg[63:48];
//assign axis_odec_out.tdata[47:32]    = in_tdata_i_reg[47:32];
//assign axis_odec_out.tdata[63:48]    = in_tdata_q_reg[47:32];
//assign axis_odec_out.tdata[79:64]    = in_tdata_i_reg[31:16];
//assign axis_odec_out.tdata[95:80]    = in_tdata_q_reg[31:16];
//assign axis_odec_out.tdata[111:96]   = in_tdata_i_reg[15:0];
//assign axis_odec_out.tdata[127:112]  = in_tdata_q_reg[15:0];     

//204C

pulse_buffer_fifo pulse_buffer_fifo_i (
   .s_axis_aresetn(~reset), 
   .s_axis_aclk(clk),     

   .s_axis_tvalid(axis_odec_out.tvalid),
   .s_axis_tready(),
   .s_axis_tdata(axis_odec_out.tdata),  
   .s_axis_tlast(axis_odec_out.tlast),
   .s_axis_tuser(axis_odec_out.tuser),

   .m_axis_tvalid(axis_pulse_buffer_out.tvalid),
   .m_axis_tready(axis_pulse_buffer_out.tready),
   .m_axis_tdata(axis_pulse_buffer_out.tdata), 
   .m_axis_tlast(axis_pulse_buffer_out.tlast),
   .m_axis_tuser(axis_pulse_buffer_out.tuser) 
);

wire [15:0] dwidth_conv_tuser_in;
assign dwidth_conv_tuser_in[15:1] = '0;
assign dwidth_conv_tuser_in[0] = axis_pulse_buffer_out.tuser;

// This is going from 128 bits to 64 bits
dig_rx_dwidth_converter dig_rx_dwidth_converter_i (
    .aresetn(~reset),
    .aclk(clk),      
   
   .s_axis_tvalid(axis_pulse_buffer_out.tvalid),
   .s_axis_tready(axis_pulse_buffer_out.tready),
   .s_axis_tdata(axis_pulse_buffer_out.tdata),
   .s_axis_tlast(axis_pulse_buffer_out.tlast),
   .s_axis_tuser(dwidth_conv_tuser_in),

   .m_axis_tvalid(axis_dwidth_conv_out.tvalid),
   .m_axis_tready(axis_dwidth_conv_out.tready),
   .m_axis_tdata(axis_dwidth_conv_out.tdata),
   .m_axis_tlast(axis_dwidth_conv_out.tlast),
   .m_axis_tuser(axis_dwidth_conv_out.tuser)
);

// Now need a clock converter to go to output which is connected directly to eth udp
dig_rx_clock_converter dig_rx_clock_converter_i (
  .s_axis_aresetn(axis_dwidth_conv_out.resetn),
  .s_axis_aclk(axis_dwidth_conv_out.clk),      
  .s_axis_tvalid(axis_dwidth_conv_out.tvalid),  
  .s_axis_tready(axis_dwidth_conv_out.tready),  
  .s_axis_tdata(axis_dwidth_conv_out.tdata),    
  .s_axis_tlast(axis_dwidth_conv_out.tlast),    
  .s_axis_tuser(axis_dwidth_conv_out.tuser),    
  
  .m_axis_aresetn(rx_out.resetn),
  .m_axis_aclk(rx_out.clk),      
  .m_axis_tvalid(rx_out.tvalid),  
  .m_axis_tready(rx_out.tready),  
  .m_axis_tdata(rx_out.tdata),    
  .m_axis_tlast(rx_out.tlast),    
  .m_axis_tuser(rx_out.tuser)     
);


//204B
//assign axis_odec_out.tdata[15:0]     = in_tdata_i_reg[31:16];
//assign axis_odec_out.tdata[31:16]    = in_tdata_q_reg[31:16];
//assign axis_odec_out.tdata[47:32]    = in_tdata_i_reg[15:0];
//assign axis_odec_out.tdata[63:48]    = in_tdata_q_reg[15:0];


//pulse_buffer_204B_fifo pulse_buffer_fifo_i (
//    .s_axis_aresetn(~reset), 
//    .s_axis_aclk(clk),     
    
//    .s_axis_tvalid(axis_odec_out.tvalid),
//    .s_axis_tready(),
//    .s_axis_tdata(axis_odec_out.tdata),  
//    .s_axis_tlast(axis_odec_out.tlast),
//    .s_axis_tuser(axis_odec_out.tuser),
    
//    .m_axis_aclk(rx_out.clk),      
//    .m_axis_tvalid(rx_out.tvalid),  
//    .m_axis_tready(rx_out.tready),  
//    .m_axis_tdata(rx_out.tdata),    
//    .m_axis_tlast(rx_out.tlast),    
//    .m_axis_tuser(rx_out.tuser)     
//);

assign rx_out.tkeep = '1;
assign rx_out.tdest = 1;

endmodule


`resetall
