`timescale 1ns/1ps

module tb_SPIm;

    // Parameters
    parameter CLK_PERIOD = 8.13; // Clock period in ns

    // Inputs
    reg Clk_i;
    reg Rst_i;
    reg Start_i;
    reg CPHA_i;
    reg [31:0] SPIdata;
	reg SpiDataVal_i;
    reg SelSt_i;
    reg [1:0] WidthSel_i;
    reg Lag_i;
    reg Lead_i;
    reg EndianSel_i;
    reg [5:0] Stop_i;
    reg PulsePol_i;

    // Outputs
    wire Mosi0_o;
    wire Mosi1_o;
    wire Mosi2_o;
    wire Mosi3_o;
    wire Sck_o;
    wire Ss_o;
    wire Val_o;

    SPIm SPIm_inst (
        .Clk_i(Clk_i), 
        .Rst_i(Rst_i), 
        .Start_i(Start_i), 
        .ClockPhase_i(CPHA_i), 
        .SpiData_i(SPIdata),
        .SelSt_i(SelSt_i),
        .WidthSel_i(WidthSel_i),
        .Lag_i(Lag_i),
        .Lead_i(Lead_i),
        .EndianSel_i(EndianSel_i),
        .Stop_i(Stop_i),
        .PulsePol_i(PulsePol_i),
        .Mosi0_o(Mosi0_o),
        .Sck_o(Sck_o),
        .Ss_o(Ss_o),
        .Val_o(Val_o)
    );


    SPIs SPIs_inst (
        .Clk_i(Clk_i), 
        .Rst_i(Rst_i), 
        .Sck_i(Sck_o), 
        .Ss_i(Ss_o), 
        .Mosi0_i(Mosi0_o), 
        .WidthSel_i(WidthSel_i), 
        .EndianSel_i(EndianSel_i),
        .SelSt_i(SelSt_i), 
        .Data_o(), 
        .Addr_o(), 
        .DataToRxFifo_o(), 
        .Val_o()
    );


    // QuadSPIm QuadSPIm_inst (
    //     .Clk_i(Clk_i),
    //     .Rst_i(Rst_i),
    //     .Start_i(Start_i),
    //     .ClockPhase_i(CPHA_i),
    //     .SpiData_i(SPIdata),
    //     .SpiDataVal_i(SpiDataVal_i),
    //     .SelSt_i(SelSt_i),
    //     .WidthSel_i(WidthSel_i),
    //     .Lag_i(Lag_i),
    //     .Lead_i(Lead_i),
    //     .EndianSel_i(EndianSel_i),
    //     .Stop_i(Stop_i),
    //     .PulsePol_i(PulsePol_i),
    //     .Mosi0_i(Mosi0_o),
    //     .Mosi1_i(Mosi1_o),
    //     .Mosi2_i(Mosi2_o),
    //     .Mosi3_i(Mosi3_o),
    //     .Sck_o(Sck_o),
    //     .Ss_o(Ss_o),
    //     .Val_o(Val_o)
    // );



    // QuadSPIs QuadSPIs_inst (
    //     .Clk_i(Clk_i),
    //     .Rst_i(Rst_i),
    //     .Sck_i(Sck_o),
    //     .Ss_i(Ss_o),
    //     .Mosi0_i(Mosi0_o),
    //     .Mosi1_i(Mosi1_o),
    //     .Mosi2_i(Mosi2_o),
    //     .Mosi3_i(Mosi3_o),
    //     .WidthSel_i(WidthSel_i),
    //     .EndianSel_i(EndianSel_i),
    //     .SelSt_i(SelSt_i),
    //     .Data_o(),
    //     .Addr_o(),
    //     .DataToRxFifo_o(),
    //     .Val_o()
    // );

    // Clock generation
    always #(CLK_PERIOD/2) Clk_i = ~Clk_i;

    // Initial setup and test sequence
    initial begin
        // Initialize Inputs
        Clk_i = 0;
        Rst_i = 1;
        Start_i = 0;
        CPHA_i = 0;
		SpiDataVal_i = 0;
        SPIdata = 32'h00000000;
        SelSt_i = 1;//0:High, 1:Low
        WidthSel_i = 3; // Full 32-bit width
        Lag_i = 0;
        Lead_i = 0;
        EndianSel_i = 0; // 0:MSB first, 1:lsb first
        Stop_i = 6'd0;
        PulsePol_i = 0;

        // Reset the system
        #(CLK_PERIOD*10) Rst_i = 0;
        #(CLK_PERIOD*2) Start_i = 1; // Start SPI transaction
        #(CLK_PERIOD*10)SPIdata =  {16'h2,16'h1};
        //    #(CLK_PERIOD*10)SPIdata =  32'haa;

    
        #(CLK_PERIOD*100);
           SPIdata = {1'h0, 7'h2a, 24'd10};

        // EndianSel_i = 1; // LSB first
        // SPIdata = {1'h0, 7'h2a, 8'haa,8'h00,8'haa}; 
        // #(CLK_PERIOD*2) Start_i = 0; 
        // #(CLK_PERIOD) Start_i = 1;

      
    end

endmodule
