# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
# 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010 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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

#
# Initialize the board on initial connection or after rebooting.
# Since the board autobauds, we have to be a bit aggressive about
# getting a valid prompt.
#
proc ${board}_init { dest } {
    global i960_try_count

    set prompt [board_info $dest shell_prompt]
    set done 0

    if {![info exists i960_try_count]} {
	set i960_try_count 1
    }

    remote_close $dest
    if { [remote_open $dest] != "" } {
	for { set tries 0 } { $tries < 7 && ! $done } { incr tries } {
	    remote_send $dest "\n"
	    remote_expect $dest 1 {
		-re "${prompt}" {
		    set done 1
		}
		-re ".+" { exp_continue }
		timeout { }
	    }
	}
    }

    remote_close $dest
    if { ! $done } {
	if { $i960_try_count == 3 } {
	    perror "Couldn't connect to board."
	} else {
	    incr i960_try_count
	    remote_close $dest
	    remote_reboot $dest
	}
    }
    if {[info exists i960_try_count]} {
	unset i960_try_count
    }
}

proc i960_ld { dest prog } {
    if {![file exists $prog]} {
	perror "$prog does not exist."
        return "untested"
    }
    set shell_prompt [board_info $dest shell_prompt]
    set strip [board_info $dest strip]
    set rprog [remote_download host $prog a.out]
    if { $strip != "" } {
	remote_exec host $strip $rprog
    }
    remote_upload host $rprog a.out

    set id [remote_open $dest]
    if { $id < 0 } {
	return -1
    }
    remote_binary $dest
    remote_send $dest "\n"
    remote_expect $dest 5 {
	-re $shell_prompt { }
    }
    remote_send $dest "do\n"
    remote_expect $dest 5 {
	-re "Downloading" { }
    }
    # Nasty.
    if { [board_info $dest connect] == "telnet" } {
	global board_info

	remote_close $dest
	set hp [split [board_info $dest netport] ":"]
	set host [lindex $hp 0]
	set port [lindex $hp 1]
	set status -1
	while { $status != 0 } {
	    set status [catch "socket $host $port" id2]
	    if { $status != 0 } {
		sleep 5
	    }
	}
    } else {
	set id2 [exp_open -leaveopen -i $id]
    }
    if {[catch "exec sx -bX a.out <@$id2 >@$id2 2>/dev/null" error]} {
	perror "exec sx failed: $error"
    }
    if { [board_info $dest connect] == "telnet" } {
	close $id2
	sleep 2
	remote_open $dest
	remote_binary $dest
    }
    set result 1
    remote_send $dest "\n"
    remote_expect $dest 1 {
	-re "$shell_prompt" {
	    set result 0
	    exp_continue
	}
	timeout { }
    }
    return $result
}

proc i960_spawn { dest prog args } {
    set shell_prompt [board_info $dest shell_prompt]

    for { set tries 0 } { $tries < 3 } { incr tries } {
	set result [remote_ld $dest $prog]
	if { $result == 0 } {
	    set comm "go [board_info $dest start_addr]"
	    remote_send $dest "$comm\n"
	    remote_expect $dest 10 {
		-re "$comm\[\r\n\]\[\r\n\]?" { }
		default { }
	    }
	    return [board_info $dest fileid]
	} else {
	    remote_reboot $dest
	}
    }
    return -1
}

proc i960_wait { dest timeout } {
    set output ""
    set shell_prompt [board_info $dest shell_prompt]
    set status -1

    remote_expect $dest $timeout {
	-re " fault at \[0-9a-h\]+, subtype \[0-9a-h\]+" {
	    set status -1
	    exp_continue
	}
	-re "(.*)(\[\r\n\]|^)Program Exit: (\[0-9\]+)\[\r\n\]" {
	    append output $expect_out(1,string)
	    set status $expect_out(3,string)
	    exp_continue
	}
	-re "(.*)$shell_prompt" {
	    append output $expect_out(1,string)
	    set bstatus [check_for_board_status output]
	    if { $bstatus >= 0 } {
		set status $bstatus
	    }
	}
	-re "\[\r\n\]+" {
	    # Sometimes the board goes wacky in the head, and we have
	    # to shoot it.
	    append output $expect_out(buffer)
	    if { [string length $output] < 512000 } {
		exp_continue
	    } else {
		set status -1
	    }
	}
	default {
	    set status -1
	}
    }
    return [list $status $output]
}

proc i960_load { dest prog args } {
    for { set x 0 } { $x < 3 } { incr x } {
	set id [eval remote_spawn \{$dest\} \{$prog\} $args]
	if { $id < 0 } {
	    return [list "fail" ""]
	}
	set result [remote_wait $dest 120]
	set status [lindex $result 0]
	set output [lindex $result 1]

	if { $status == 0 } {
	    return [list "pass" $output]
	} else {
	    global i960_retry

	    if { [board_info $dest exists unreliable] && ![info exists i960_retry] } {
		set i960_retry 1
		remote_reboot $dest
		set result [eval i960_load \{$dest\} \{$prog\} $args]
		unset i960_retry
		return $result
	    } else {
		if { $status < 0 } {
		    remote_reboot $dest
		}
		return [list "fail" $output]
	    }
	}
    }
}

set_board_info shell_prompt "=>"
set_board_info send_initial_cr 1
# We take care of getting a prompt in ${board}_init.
set_board_info dont_wait_for_prompt 1
