# Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
# Foundation, Inc.
#
# This file is part of DejaGnu.
#
# DejaGnu is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# DejaGnu is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DejaGnu; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#
# 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
