if { $argc != 1} {
    puts "# ERROR: Missing parameter: <path_to_config_file> !"   
    return $argc
} 

set cfg_file [lindex $argv 0]

set software_file_path "./test_files/code_files/hello_world/neorv32_application_image.vhd"
set input_file_path "./test_files/input_data.csv"
set output_file_path "./test_files/output_data.csv"
set simulation_time "all"
set simulation_unit ""
set PWM_filter_en 0

#  Slurp up the data file
if { [catch {set fp [open $cfg_file r]}] } {
    # Oppening failed
    puts "# ERROR: File $cfg_file couldn't be open !"
    return $cfg_file
}
set file_data [read $fp]
close $fp

#set parameters { software_file_i input_file_i output_file_i simulation_time_i PWM_filter_i }

# Parsing config file
set data [split $file_data "\n"]
set i 0
foreach line $data {
    switch $i {
        0 {
            set software_file_path [lindex $data 0]
            if {[file exists $software_file_path] == 0} {
                puts "# ERROR: File [lindex $argv 0] couldn't be open !"
                return $software_file_path
            }
            puts [lindex $data 0]
        }      
        1 {
            set input_file_path [lindex $data 1]
            if {[file exists $input_file_path] == 0} {
                puts "# ERROR: File [lindex $argv 1] couldn't be open !"
                return $input_file_path
            }
            puts [lindex $data 1]
        }
        2 {
            set output_file_path [lindex $data 2]
            puts $output_file_path
        }

        3 {   
            set sim_time_data [lindex $data 3]    
            set sim_time_data [split $sim_time_data " "]
            set simulation_time [lindex $sim_time_data 0]
            set simulation_unit [lindex $sim_time_data 1]
            switch $simulation_unit {
                us {
                    set simulation_time "${simulation_time}000"
                }  
                ms {
                    set simulation_time "${simulation_time}000000"
                }
                s {
                    set simulation_time "${simulation_time}000000000"
                }  
                default {
                }
            }
        }
            
        4 {
            set PWM_filter_en [lindex $data 4] 
            puts $PWM_filter_en
        }
        default {
           puts "# WARNIGN: Too many parameters in config file, some of them won't be used !" 
        }
    }
     incr i
}

if { $i < 2} {
    puts "# ERROR: Missing parameters in config file, testing might not work correctly !"   
    return $i
} 

if {$software_file_path != ""} {
    set software_file_actual ./../neorv32/rtl/core/neorv32_application_image.vhd
    if {[file size $software_file_path] != [file size $software_file_actual]} {
        # Set instruction code file 
        file copy -force $software_file_path ./../neorv32/rtl/core
    } else {
        puts "# INFO: Software files are identical !"
    }
}

#set runlist [get_runs synth*]
# Run synthesis
#if {[regexp -- synth_1 $runlist]}
#{
#    launch_runs synth_1 -jobs 4
#    wait_on_run synth_1 
#}


# Set in/out files 
set_property generic [list input_file=$input_file_path output_file=$output_file_path PWM_filter=$PWM_filter_en sim_time=$simulation_time] [get_filesets sim_1]

# Run simulation
## TODO: if is simulation already running -> close it
#launch_simulation -simset [get_filesets sim_1]
if { [catch {launch_simulation -simset [get_filesets sim_1]}] } {
    # Launching simulation failed, already launched
    puts "# WARNIGN: Launching simulation failed, already running"
    close_sim -force
    launch_simulation -simset [get_filesets sim_1]
}
run all


