1 \ *****************************************************************************
2 \ * Copyright (c) 2004, 2008 IBM Corporation
3 \ * All rights reserved.
4 \ * This program and the accompanying materials
5 \ * are made available under the terms of the BSD License
6 \ * which accompanies this distribution, and is available at
7 \ * http://www.opensource.org/licenses/bsd-license.php
10 \ * IBM Corporation - initial implementation
11 \ ****************************************************************************/
14 01 CONSTANT XM-SOH \ Start of header
15 04 CONSTANT XM-EOT \ End-of-transmission
16 06 CONSTANT XM-ACK \ Acknowledge
17 15 CONSTANT XM-NAK \ Neg. acknowledge
19 0 VALUE xm-retries \ Retry count
24 \ * Internal function:
25 \ * wait <timeout> seconds for a new character
27 : xmodem-get-byte ( timeout -- byte|-1 )
30 key? IF key UNLOOP EXIT THEN
38 \ * Internal function:
39 \ * Receive one XMODEM packet, check block number and check sum.
41 : xmodem-rx-packet ( address -- success? )
42 1 xmodem-get-byte \ Get block number
44 2drop false EXIT \ Timeout
46 1 xmodem-get-byte \ Get neg. block number
48 3drop false EXIT \ Timeout
50 rot 0 ( blk# ~blk# address chksum )
52 1 xmodem-get-byte dup 0 < IF ( blk# ~blk# address chksum byte )
53 3drop 2drop UNLOOP FALSE EXIT
55 dup 3 pick c! ( blk# ~blk# address chksum byte )
56 + swap 1+ swap ( blk# ~blk# address+1 chksum' )
58 ( blk# ~blk# address chksum )
61 1 xmodem-get-byte <> IF
66 \ finally check if block numbers are ok:
76 \ * Internal function:
77 \ * Load file to given address via XMODEM protocol
79 : (xmodem-load) ( address -- bytes )
84 d# 10 xmodem-get-byte dup >r
87 dup xmodem-rx-packet IF
88 \ A packet has been received successfully
90 80 + ( start-addr next-addr R: rx-byte )
91 0 to xm-retries \ Reset retry count
92 xm-block# 1+ ff and to xm-block# \ Increase current block#
94 \ Error while receiving packet
96 xm-retries 1+ to xm-retries \ Increase retry count
104 xm-retries 1+ to xm-retries \ Increase retry count
108 xm-retries d# 10 >= OR
109 UNTIL ( start-address end-address )
110 swap - ( bytes received )
115 \ * Load file to load-base via XMODEM protocol
117 : xmodem-load ( -- bytes )
118 cr ." Waiting for start of XMODEM upload..." cr
119 get-load-base (xmodem-load)