]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli-core.S
backporting uart_i and cli
[avr-crypto-lib.git] / test_src / cli-core.S
1 /* cli-core.S */
2 /*
3     This file is part of the AVR-Huffman.
4     Copyright (C) 2009  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "avr-asm-macros.S"
21
22 #define CMDLIST_ENTRY_SIZE 8
23
24 /******************************************************************************/
25 /* uint16_t max_cmd_length(PGM_VOID_P cmdlist){
26         uint16_t t,ret=0;
27         char* str;
28         for(;;){
29                 str = (char*)pgm_read_word(cmdlist);
30                 cmdlist = (uint8_t*)cmdlist + CMDLIST_ENTRY_SIZE;
31                 if(str==NULL)
32                         return ret;
33                 t = strlen_P(str);
34                 if(t>ret)
35                         ret=t;
36         }
37 }
38  *    param cmdlist:  r24:r25
39  */
40 TMP_CNT_0 = 26
41 TMP_CNT_1 = 27 
42 .global max_cmd_length
43 max_cmd_length:
44         movw r30, r24
45         clr r24
46         clr r25
47 1:
48         lpm r26, Z+
49         lpm r27, Z+
50         movw r20, r30
51         adiw r26, 0
52         brne 3f
53         ret
54 3:
55         movw r30, r26
56         clr TMP_CNT_0
57         clr TMP_CNT_1
58 4:
59         lpm r1, Z+
60         adiw TMP_CNT_0, 1
61         tst r1
62         brne 4b
63         sbiw TMP_CNT_0, 1
64         movw r30, r20
65         adiw r30, CMDLIST_ENTRY_SIZE-2
66         cp  r24, TMP_CNT_0
67         cpc r25, TMP_CNT_1
68         brge 1b
69         movw r24, TMP_CNT_0
70         rjmp 1b
71                  
72 /******************************************************************************/
73 .global echo_ctrl
74 echo_ctrl: 
75         call strstrip
76         tst r24
77         brne 3f
78         tst r25
79         breq print_echo_status
80 3:
81         call strlwr
82         movw r26, r24
83         ld r20, X+
84         tst r20
85         breq print_echo_status
86         cpi r20, '1'
87         breq exit_1
88         cpi r20, '0'
89         breq exit_0
90         cpi r20, 't'
91         breq exit_1
92         cpi r20, 'f'
93         breq exit_0
94         ld r20, X+
95         cpi r20, 'n'
96         breq exit_1
97         cpi r20, 'f'
98         breq exit_0     
99         ret
100 exit_1:         
101         ldi r20, 1
102         rjmp exit
103 exit_0:
104         ldi r20, 0
105 exit:   
106         sts cli_echo, r20
107         ret
108 print_echo_status:
109         ldi r24, lo8(pecho)
110         ldi r25, hi8(pecho)
111         rcall cli_putstr_P
112         lds r20, cli_echo
113         tst r20
114         breq 3f
115         clr r25
116         ldi r24, 'n'
117         rcall cli_putc
118         rjmp 4f
119 3:      clr r25
120         ldi r24, 'f'
121         rcall cli_putc
122         clr r25
123         ldi r24, 'f'
124         rcall cli_putc
125 4:      clr r25
126         ldi r24, '\r'
127         rcall cli_putc
128         clr r25
129         ldi r24, '\n'
130         rcall cli_putc
131         ret             
132 pecho:
133 .asciz "\r\necho is o"
134
135 /******************************************************************************/
136 /* int8_t search_and_call(char* cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist) 
137  *    param cmd:          r24:r25
138  *    param maxcmdlength: r22:r23
139  *    param cmdlist:      r20:r21
140  */
141 LEN_0 = 14
142 LEN_1 = 15
143 CMD_0 = 16
144 CMD_1 = 17
145
146 snc_auto_help:
147         movw r24, r22
148         movw r22, r20
149         rcall cli_auto_help     
150 snc_return_1:
151         ldi r24, 1
152         rjmp snc_return
153 snc_return_2:
154         ldi r24, 2
155 snc_return:
156         clr r25
157         ret      
158
159 .global search_and_call
160 search_and_call:
161         adiw r24, 0
162         breq snc_return_1
163 1:      movw r26, r24
164         ld r18, X+
165         cpi r18, 0
166         breq snc_return_1
167         cpi r18, '#'
168         breq snc_return_1
169         cpi r18, '?'
170         breq 2f
171         cpi r18, 'h'
172         brne 1f
173         ld r18, X+
174         cpi r18, 'e'
175         brne 1f
176         ld r18, X+
177         cpi r18, 'l'
178         brne 1f
179         ld r18, X+
180         cpi r18, 'p'
181         brne 1f
182         ld r18, X+
183         tst r18
184         brne 1f
185 2:      rjmp snc_auto_help
186 1:      
187         push r28
188         push r29
189         push CMD_0
190         push CMD_1
191         push LEN_0
192         push LEN_1
193         movw CMD_0, r24
194         movw r28, r20
195         rcall firstword_length
196         movw LEN_0, r24
197         movw r26, CMD_0
198         add r26, LEN_0
199         adc r27, LEN_1
200         st X, r1
201         movw r24, CMD_0
202         ldi r22, lo8(exit_str)
203         ldi r23, hi8(exit_str)
204         call strcmp_P
205         adiw r24, 0
206         breq snc_return_0
207 1:
208         movw r30, r28
209         lpm r24, Z+
210         lpm r25, Z+
211         adiw r24, 0
212         brne 3f
213         ldi r24, lo8(nocommand_str)
214         ldi r25, hi8(nocommand_str)
215         rcall cli_putstr_P
216         ldi r24, 1
217         rjmp 5f
218 3:      movw r22, r24
219         movw r24, CMD_0
220         call strcmp_P
221         adiw r24, 0
222         breq snc_found
223         adiw r28, CMDLIST_ENTRY_SIZE
224         rjmp 1b
225 snc_found:
226         movw r24, CMD_0
227         add r24, LEN_0
228         adc r25, LEN_1
229         adiw r24, 1
230         movw r30, r28
231         adiw r30, 4
232         lpm r22, Z+
233         lpm r23, Z+
234         movw r30, r22
235         adiw r30, 0
236         breq 4f ;snc_return_2
237         icall
238         ldi r24, 1
239         rjmp 5f
240 4:      ldi r24, 2      
241 snc_return_0:
242         clr r24
243 5:      pop LEN_1
244         pop LEN_0
245         pop CMD_1
246         pop CMD_0
247         pop r29
248         pop r28
249         rjmp snc_return
250
251 exit_str:
252 .asciz "exit"
253 help_str:
254 .asciz "help"
255 nocommand_str:
256 .asciz "\r\ncommand not found, try 'help'"
257
258 /******************************************************************************/
259 /* void cli_option_listing(char* buffer, PGM_VOID_P cmdlist)
260  *   param buffer:    r24:r25
261  *   param cmdlist:   r22:r23
262  */
263 ITM_0 = 12
264 ITM_1 = 13 
265 CMD_0 = 14
266 CMD_1 = 15
267 LEN_0 = 16
268 LEN_1 = 17
269 LST_0 = 28
270 LST_1 = 29
271 .global cli_option_listing
272 cli_option_listing:
273         push_range 12, 17
274         push_range 28, 29
275         movw CMD_0, r24
276         movw LST_0, r22
277         call strlen
278         sbiw r24, 1
279         movw LEN_0, r24
280 1:
281         movw r30, LST_0
282         lpm r24, Z+
283         lpm r25, Z+
284         movw ITM_0, r24
285         adiw r24, 0
286         breq 99f
287         movw r22, r24
288         movw r24, CMD_0
289         movw r20, LEN_0
290         adiw r28, CMDLIST_ENTRY_SIZE
291         call strncmp_P
292         adiw r24, 0
293         brne 1b
294         ldi r24, lo8(itemprefix_str)
295         ldi r25, hi8(itemprefix_str)
296         rcall cli_putstr_P
297         movw r24, ITM_0
298         rcall cli_putstr_P
299         rjmp 1b
300 99:     
301         ldi r24, lo8(prompt_str)
302         ldi r25, hi8(prompt_str)
303         rcall cli_putstr_P
304         movw r24, CMD_0
305         rcall cli_putstr
306         pop_range 28, 29
307         pop_range 12, 17
308         ret
309
310 prompt_str:     
311 .asciz "\r\n>"
312 itemprefix_str: 
313 .asciz "\r\n   "
314
315 /******************************************************************************/
316 /* void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist)
317  *   param maxcmdlength:  r24:r25
318  *   param cmdlist:       r22:r23
319  */
320
321 TMP_0 = 14
322 TMP_1 = 15 
323 LEN_0 = 16
324 LEN_1 = 17
325 LST_0 = 28
326 LST_1 = 29
327 cli_auto_help:
328         push_range 16, 17
329         push_range 28, 29
330         movw LEN_0, r24
331         movw r28, r22
332         ldi r24, lo8(ahelphead_str)
333         ldi r25, hi8(ahelphead_str)
334         rcall cli_putstr_P
335 1:
336         movw r30, r28
337         lpm r24, Z+
338         lpm r25, Z+
339         movw r28, r30
340         adiw r24, 0
341         breq 99f        
342         movw TMP_0, r24
343         rcall cli_putstr_P
344         movw r24, TMP_0
345         call strlen_P
346         movw TMP_0, LEN_0
347         sub TMP_0, r24
348         sbc TMP_1, r25
349         breq 4f
350 2:      ldi r24, ' '
351         clr r25
352         rcall cli_putc
353         dec TMP_0
354         brne 2b
355 4:      ldi r24, lo8(sep_str)
356         ldi r25, hi8(sep_str)
357         ldi r22, lo8(none_str)
358         ldi r23, hi8(none_str)
359         rcall cli_putstr_P
360         movw r30, r28
361         lpm r24, Z+
362         lpm r25, Z+
363         movw r28, r30
364         adiw r24, 0
365         breq 5f
366         ldi r22, lo8(yes_str)
367         ldi r23, hi8(yes_str)
368 5:      movw r24, r22
369         rcall cli_putstr_P      
370         ldi r24, lo8(sep2_str)
371         ldi r25, hi8(sep2_str)
372         rcall cli_putstr_P
373         movw r30, r28
374         adiw r30, 1
375         lpm r24, Z
376         rcall cli_hexdump_byte
377         movw r30, r28
378         lpm r24, Z
379         rcall cli_hexdump_byte
380         ldi r24, '\r'
381         clr r25
382         rcall cli_putc
383         ldi r24, '\n'
384         clr r25
385         rcall cli_putc
386         adiw r28, CMDLIST_ENTRY_SIZE-4
387         rjmp 1b
388 99:     
389         pop_range 28, 29
390         pop_range 14, 17
391         ret
392 ahelphead_str:
393 .asciz "\r\n[auto help] available commands:\r\n <command> - <params> - <address>\r\n"
394 sep_str:
395 .asciz " - "
396 none_str:
397 .asciz "none"
398 yes_str:
399 .asciz "yes "
400 sep2_str:
401 .asciz" \t - 0x"
402 .byte 0
403
404
405
406