Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / SLOF / slof / fs / xmodem.fs
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
8 \ *
9 \ * Contributors:
10 \ *     IBM Corporation - initial implementation
11 \ ****************************************************************************/
12
13
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
18
19 0 VALUE xm-retries   \ Retry count
20 0 VALUE xm-block#
21
22
23 \ *
24 \ * Internal function:
25 \ * wait <timeout> seconds for a new character
26 \ *
27 : xmodem-get-byte  ( timeout -- byte|-1 )
28    d# 1000 *
29    0 DO
30       key? IF key UNLOOP EXIT THEN
31       1 ms
32    LOOP
33    -1
34 ;
35
36
37 \ *
38 \ * Internal function:
39 \ * Receive one XMODEM packet, check block number and check sum.
40 \ *
41 : xmodem-rx-packet  ( address -- success? )
42    1 xmodem-get-byte    \ Get block number
43    dup 0 < IF
44       2drop false EXIT  \ Timeout
45    THEN
46    1 xmodem-get-byte    \ Get neg. block number
47    dup 0 < IF
48       3drop false EXIT  \ Timeout
49    THEN
50    rot 0                ( blk# ~blk# address chksum )
51    80 0 DO
52       1 xmodem-get-byte dup 0 < IF     ( blk# ~blk# address chksum byte )
53          3drop 2drop UNLOOP FALSE EXIT
54       THEN
55       dup 3 pick c!            ( blk# ~blk# address chksum byte )
56       + swap 1+ swap           ( blk# ~blk# address+1 chksum' )
57    LOOP
58    ( blk# ~blk# address chksum )
59    \ Check sum:
60    0ff and
61    1 xmodem-get-byte <> IF
62       \ CRC failed!
63       3drop FALSE EXIT
64    THEN
65    drop                        ( blk# ~blk# )
66    \ finally check if block numbers are ok:
67    over xm-block# <> IF
68       \ Wrong block number!
69       2drop FALSE EXIT
70    THEN                        ( blk# ~blk# )
71    ff xor =
72 ;
73
74
75 \ *
76 \ * Internal function:
77 \ * Load file to given address via XMODEM protocol
78 \ *
79 : (xmodem-load)  ( address -- bytes )
80    1 to xm-block#
81    0 to xm-retries
82    dup
83    BEGIN
84       d# 10 xmodem-get-byte dup >r
85       CASE
86          XM-SOH OF
87             dup xmodem-rx-packet IF
88                \ A packet has been received successfully
89                XM-ACK emit
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#
93             ELSE
94                \ Error while receiving packet
95                XM-NAK emit
96                xm-retries 1+ to xm-retries  \ Increase retry count
97             THEN
98          ENDOF
99          XM-EOT OF
100             XM-ACK emit
101          ENDOF
102          dup OF
103             XM-NAK emit
104             xm-retries 1+ to xm-retries  \ Increase retry count
105          ENDOF
106       ENDCASE
107       r> XM-EOT =
108       xm-retries d# 10 >= OR
109    UNTIL                         ( start-address end-address )
110    swap -                        ( bytes received )
111 ;
112
113
114 \ *
115 \ * Load file to load-base via XMODEM protocol
116 \ *
117 : xmodem-load  ( -- bytes )
118    cr ." Waiting for start of XMODEM upload..." cr
119    get-load-base (xmodem-load)
120 ;