]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli-core.S
BMW-small -100 bytes / fix for help cmd
[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         brpl 1f
281         clr LEN_0
282         clr LEN_1
283 1:
284         movw r30, LST_0
285         lpm r24, Z+
286         lpm r25, Z+
287         movw ITM_0, r24
288         adiw r24, 0
289         breq 99f
290         movw r22, r24
291         movw r24, CMD_0
292         movw r20, LEN_0
293         adiw r28, CMDLIST_ENTRY_SIZE
294         call strncmp_P
295         adiw r24, 0
296         brne 1b
297         ldi r24, lo8(itemprefix_str)
298         ldi r25, hi8(itemprefix_str)
299         rcall cli_putstr_P
300         movw r24, ITM_0
301         rcall cli_putstr_P
302         rjmp 1b
303 99:
304         ldi r24, lo8(prompt_str)
305         ldi r25, hi8(prompt_str)
306         rcall cli_putstr_P
307         movw r24, CMD_0
308         rcall cli_putstr
309         pop_range 28, 29
310         pop_range 12, 17
311         ret
312
313 prompt_str:
314 .asciz "\r\n>"
315 itemprefix_str:
316 .asciz "\r\n   "
317
318 /******************************************************************************/
319 /* void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist)
320  *   param maxcmdlength:  r24:r25
321  *   param cmdlist:       r22:r23
322  */
323
324 TMP_0 = 14
325 TMP_1 = 15
326 LEN_0 = 16
327 LEN_1 = 17
328 LST_0 = 28
329 LST_1 = 29
330 cli_auto_help:
331         push_range 16, 17
332         push_range 28, 29
333         movw LEN_0, r24
334         movw r28, r22
335         ldi r24, lo8(ahelphead_str)
336         ldi r25, hi8(ahelphead_str)
337         rcall cli_putstr_P
338 1:
339         movw r30, r28
340         lpm r24, Z+
341         lpm r25, Z+
342         movw r28, r30
343         adiw r24, 0
344         breq 99f
345         movw TMP_0, r24
346         rcall cli_putstr_P
347         movw r24, TMP_0
348         call strlen_P
349         movw TMP_0, LEN_0
350         sub TMP_0, r24
351         sbc TMP_1, r25
352         breq 4f
353 2:      ldi r24, ' '
354         clr r25
355         rcall cli_putc
356         dec TMP_0
357         brne 2b
358 4:      ldi r24, lo8(sep_str)
359         ldi r25, hi8(sep_str)
360         rcall cli_putstr_P
361         ldi r22, lo8(none_str)
362         ldi r23, hi8(none_str)
363         movw r30, r28
364         lpm r24, Z+
365         lpm r25, Z+
366         movw r28, r30
367         adiw r24, 0
368         breq 5f
369         ldi r22, lo8(yes_str)
370         ldi r23, hi8(yes_str)
371 5:      movw r24, r22
372         rcall cli_putstr_P
373         ldi r24, lo8(sep2_str)
374         ldi r25, hi8(sep2_str)
375         rcall cli_putstr_P
376         movw r30, r28
377         adiw r30, 1
378         lpm r24, Z
379         rcall cli_hexdump_byte
380         movw r30, r28
381         lpm r24, Z
382         rcall cli_hexdump_byte
383         ldi r24, '\r'
384         clr r25
385         rcall cli_putc
386         ldi r24, '\n'
387         clr r25
388         rcall cli_putc
389         adiw r28, CMDLIST_ENTRY_SIZE-4
390         rjmp 1b
391 99:
392         pop_range 28, 29
393         pop_range 14, 17
394         ret
395 ahelphead_str:
396 .asciz "\r\n[auto help] available commands:\r\n <command> - <params> - <address>\r\n"
397 sep_str:
398 .asciz " - "
399 none_str:
400 .asciz "none"
401 yes_str:
402 .asciz "yes "
403 sep2_str:
404 .asciz" \t - 0x"
405 .byte 0
406
407
408
409