#include "data_recorder.h"
int main() {
    // Instantiate the class
    DataRecorder dr;

    printf("Test Reg Read - 0x%x\n", dr.read_reg(UTIL_REG_BASE + 0x0));

    // Setup Timing Engine and data generator to test
    float pri = 90e-6;
    uint32_t n_pulses = 128;
    float inter_cpi = 100e-6;
    uint32_t n_samples = 8192;

    float cpi_time = n_pulses * pri + inter_cpi;
    float cpi_num_bytes = n_pulses * n_samples * 16;

    float expected_data_rate = cpi_num_bytes / cpi_time / 1e6;
    // PCIe Gen3 x4 Therotecial Max is 4GBPS
    printf("Expected Data Rate - %.2f MBps Per Channel, Total %.2f\n", expected_data_rate, expected_data_rate * NUM_DMA_CH);

    dr.write_reg(TIMING_REG_BASE + 0x0, 1);
    dr.write_reg(TIMING_REG_BASE + 0x4, (uint32_t)(pri * CLK_RATE + 0.5) - 1);
    dr.write_reg(TIMING_REG_BASE + 0x8, n_pulses);
    dr.write_reg(TIMING_REG_BASE + 0x10, inter_cpi * CLK_RATE);

    for (int i = 0; i < 4; i++) {
        printf("Timing Reg 0x%x - 0x%x\n", i*4, dr.read_reg(TIMING_REG_BASE + i*4));
    }

    dr.write_reg(DATA_GEN_REG_BASE, n_samples - 1);

    dr.set_validate_cnt_data(true, dr.read_reg(TIMING_REG_BASE + 0x4) + 1, dr.read_reg(TIMING_REG_BASE + 0x10) + 1, n_pulses);

    // Start listening for data
    dr.start_recording("test.bin", 0);
    // Start the timing engine so data starts flowing
    dr.write_reg(TIMING_REG_BASE + 0x0, 0);
    // Wait a while
    std::this_thread::sleep_for(std::chrono::milliseconds(6000));
    
    dr.stop_recording();
    dr.write_reg(TIMING_REG_BASE + 0x0, 1);

    printf("Expected Data Rate - %.2f MBps Per Channel, Total %.2f\n", expected_data_rate, expected_data_rate * NUM_DMA_CH);


}