#
# Nasty ugly stuff.
#
# We have to maintain two connections to the DOS box where the board
# is being held. One of them runs an I/O monitor process, while the other
# is used to actually start the program under test running on the board.
#

proc tic80_load { dest prog args } {
    set dos_box [board_info $dest dos_host];

    remote_spawn $dos_box [board_info $dest io_program];

    set status [remote_ld $dest $prog];

    if { $status != "pass" } {
	return $status;
    }

    set result [remote_wait $dos_box 300];
    set output [lindex $result 1];
    if { [lindex $result 0] < 0 } {
	if [board_info $dos_box exists fileid] {
	    dos_interrupt_job $dos_box;
	} else {
	    remote_close $dos_box;
	}
    }

    set status "fail";
    regsub "(\\*\\*\\* EXIT code \[0-9\]+\[\r\n]+).*$" "$output" "\\1" output;
    verbose "board out is $output";
    set bstatus [check_for_board_status output];
    if { [lindex $result 0] >= 0 } {
	if { $bstatus == 0 } {
	    set status "pass";
	}
    }
    remote_file build delete "a.fix";
    return [list $status $output];
}

proc tic80_ld { dest prog } {
    set dos_box [board_info $dest dos_host];
    set dopush 0;

    if { [remote_swap_conn $dos_box] == "fail" } {
	if { [remote_push_conn $dos_box] == "fail" } {
	    set dopush 1;
	}
	remote_open $dos_box;
    }
    set prog [remote_download $dos_box $prog "a.fix"];

    remote_spawn $dos_box "[board_info $dest start_program] $prog";
    remote_expect $dos_box 10 {
	-re "file\[(\]s\[)\] copied" {}
    }
    sleep 3;
    dos_interrupt_job $dos_box;
    if { $dopush } {
	remote_push_conn $dos_box;
    } else {
	remote_swap_conn $dos_box;
    }
    return "pass";
}
#
# Close the connection to the DOS box.
#
proc tic80_close { host } {
    set dos_box [board_info $host dos_host];

    return [remote_close $dos_box];
}

set_board_info protocol "tic80";
set_board_info gdb,use_standard_load 1;
set_board_info gdb,no_push_conn 1;
set_board_info gdb,do_reload_on_run 1;
set_board_info gdb,use_breakpoint_for_stub 1
