-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmodem.asm
211 lines (199 loc) · 3.8 KB
/
xmodem.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;;
; Copyright Jacques Deschênes 2019,2020
; This file is part of PABasic
;
; PABasic 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.
;
; PABasic 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 PABasic. If not, see <http://www.gnu.org/licenses/>.
;;
;--------------------------------------
; Implementation of xmodem transfert protocl
; REF: https://en.wikipedia.org/wiki/XMODEM
; DATE: 2020-07-17
;
;--------------------------------------------------
.if SEPARATE
.module XMODEM
.include "config.inc"
.area CODE
.endif
PACKET_SIZE=128
DEBUG=0
;-----------------------
; wait SOH or EOT
; drain all other char
; input:
; Y time out msec
; output:
; A SOH start of header
; EOT end of transmission
; 0 time out
;-----------------------
wait_soh:
ldw y,#1000
ldw timer,y
1$: clr a
ldw y,timer
jrne 2$
ret
2$: call uart_qgetc
jreq 1$
call uart_getc
cp a,#SOH
jreq 4$
cp a,#EOT
jrne 1$
4$: clrw y
ldw timer,y
ret
;-----------------------
; get next character
; wait 1 second maximum
;------------------------
get_next:
ldw y,#1000
;-------------------------------
; getc with timeout
; input:
; Y timeout delay in msec.
; output:
; A 0|char received
;-------------------------------
getc_to::
ldw timer,y
clr a
1$: ldw y,timer
jrne 2$
ret
2$: call uart_qgetc
jreq 1$
call uart_getc
clrw y
ldw timer,y
ret
;-----------------------------------
; XMODEM receive 128 bytes block
; input:
; X receive buffer address
; output:
; A ACK packet received ok
; CAN all tries failed
; EOT end of file
;-----------------------------------
BCOUNT=1
CHKSUM=2
SERIAL=3
TRIES=4
BUFFER=5
VAR_SIZE=6
xrcv_block::
_vars VAR_SIZE
ldw (BUFFER,sp),x
ld a,#10 ; number of tries
ld (TRIES,sp),a
try_again:
ldw x,(BUFFER,sp)
call wait_soh
cp a,#SOH
jreq 1$
cp a,#EOT
jrne 2$
ld a,#ACK
call uart_putc
ld a,#EOT
jra 7$
1$: ;start of header received
ld a,#PACKET_SIZE
ld (BCOUNT,sp),a
clr (CHKSUM,sp)
call get_next
ld (SERIAL,sp),a
call get_next
add a,(SERIAL,sp)
inc a
jreq 4$
2$:
ld a,#NAK
call uart_putc
dec (TRIES,sp)
jrne try_again
jra 5$
4$: ; receive data
call get_next
ld (x),a
incw x
add a,(CHKSUM,sp)
ld (CHKSUM,sp),a
dec (BCOUNT,sp)
jrne 4$
call get_next
cp a,(CHKSUM,sp)
jrne 2$
; packet received ok
ld a,(SERIAL,sp)
call print_hex
ld a,#ACK
jra 6$
5$: ; all tries failed
ld a,#CAN
6$: call uart_putc
7$: _drop VAR_SIZE
ret
;-------------------------
; XMODEM transmit 128 bytes block
; input:
; A packet number
; X buffer address
;-------------------------
; local variables
CHKSUM=1 ; byte
TRIES=2 ; byte 10 retries
SERIAL=3 ; byte packet number
PLEN=4 ; byte packet length 128 bytes
BUFF=5 ; word buffer address
VAR_SIZE=6
xtrmt_block::
_vars VAR_SIZE
ldw (BUFF,sp),x
ld (SERIAL,sp),a
ld a,#10
ld (TRIES,sp),a
tx_retries:
ld a,#SOH
call uart_putc
ld a,(SERIAL,sp)
call uart_putc
ld a,(SERIAL,sp)
cpl a
call uart_putc
clr (CHKSUM,sp)
ld a,#PACKET_SIZE
ld (PLEN,sp),a
ldw x,(BUFF,sp)
1$: ld a,(x)
incw x
call uart_putc
add a,(CHKSUM,sp)
ld (CHKSUM,sp),a
dec (PLEN,sp)
jrne 1$
ld a,(CHKSUM,sp)
call uart_putc
call get_next
cp a,#ACK
jreq 2$
dec (TRIES,sp)
jrne tx_retries
ld a,#NAK
2$:
_drop VAR_SIZE
ret