]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli-core.S
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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
43 .global max_cmd_length
44 max_cmd_length:
45         movw r30, r24
46         clr r24
47         clr r25
48 1:
49         lpm r26, Z+
50         lpm r27, Z+
51         movw r20, r30
52         adiw r26, 0
53         brne 3f
54         ret
55 3:
56         movw r30, r26
57         clr TMP_CNT_0
58         clr TMP_CNT_1
59 4:
60         lpm r1, Z+
61         adiw TMP_CNT_0, 1
62         tst r1
63         brne 4b
64         sbiw TMP_CNT_0, 1
65         movw r30, r20
66         adiw r30, CMDLIST_ENTRY_SIZE-2
67         cp  r24, TMP_CNT_0
68         cpc r25, TMP_CNT_1
69         brge 1b
70         movw r24, TMP_CNT_0
71         rjmp 1b
72
73 /******************************************************************************/
74 .global echo_ctrl
75 echo_ctrl:
76         call strstrip
77         tst r24
78         brne 3f
79         tst r25
80         breq print_echo_status
81 3:
82         call strlwr
83         movw r26, r24
84         ld r20, X+
85         tst r20
86         breq print_echo_status
87         cpi r20, '1'
88         breq exit_1
89         cpi r20, '0'
90         breq exit_0
91         cpi r20, 't'
92         breq exit_1
93         cpi r20, 'f'
94         breq exit_0
95         ld r20, X+
96         cpi r20, 'n'
97         breq exit_1
98         cpi r20, 'f'
99         breq exit_0
100         ret
101 exit_1:
102         ldi r20, 1
103         rjmp exit
104 exit_0:
105         ldi r20, 0
106 exit:
107         sts cli_echo, r20
108         ret
109 print_echo_status:
110         ldi r24, lo8(pecho)
111         ldi r25, hi8(pecho)
112         rcall cli_putstr_P
113         lds r20, cli_echo
114         tst r20
115         breq 3f
116         clr r25
117         ldi r24, 'n'
118         rcall cli_putc
119         rjmp 4f
120 3:      clr r25
121         ldi r24, 'f'
122         rcall cli_putc
123         clr r25
124         ldi r24, 'f'
125         rcall cli_putc
126 4:      clr r25
127         ldi r24, '\r'
128         rcall cli_putc
129         clr r25
130         ldi r24, '\n'
131         rcall cli_putc
132         ret
133 pecho:
134 .asciz "\r\necho is o"
135
136 /******************************************************************************/
137 /* int8_t search_and_call(char *cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist)
138  *    param cmd:          r24:r25
139  *    param maxcmdlength: r22:r23
140  *    param cmdlist:      r20:r21
141  */
142 LEN_0 = 14
143 LEN_1 = 15
144 CMD_0 = 16
145 CMD_1 = 17
146
147 snc_auto_help:
148         movw r24, r22
149         movw r22, r20
150         rcall cli_auto_help
151 snc_return_1:
152         ldi r24, 1
153         rjmp snc_return
154 snc_return_2:
155         ldi r24, 2
156 snc_return:
157         clr r25
158         ret
159
160 .global search_and_call
161 search_and_call:
162         adiw r24, 0
163         breq snc_return_1
164 1:      movw r26, r24
165         ld r18, X+
166         cpi r18, 0
167         breq snc_return_1
168         cpi r18, '#'
169         breq snc_return_1
170         cpi r18, '?'
171         breq 2f
172         cpi r18, 'h'
173         brne 1f
174         ld r18, X+
175         cpi r18, 'e'
176         brne 1f
177         ld r18, X+
178         cpi r18, 'l'
179         brne 1f
180         ld r18, X+
181         cpi r18, 'p'
182         brne 1f
183         ld r18, X+
184         tst r18
185         brne 1f
186 2:      rjmp snc_auto_help
187 1:
188         push r28
189         push r29
190         push CMD_0
191         push CMD_1
192         push LEN_0
193         push LEN_1
194         movw CMD_0, r24
195         movw r28, r20
196         rcall firstword_length
197         movw LEN_0, r24
198         movw r26, CMD_0
199         add r26, LEN_0
200         adc r27, LEN_1
201         st X, r1
202         movw r24, CMD_0
203         ldi r22, lo8(exit_str)
204         ldi r23, hi8(exit_str)
205         call strcmp_P
206         adiw r24, 0
207         breq snc_return_0
208 1:
209         movw r30, r28
210         lpm r24, Z+
211         lpm r25, Z+
212         adiw r24, 0
213         brne 3f
214         ldi r24, lo8(nocommand_str)
215         ldi r25, hi8(nocommand_str)
216         rcall cli_putstr_P
217         ldi r24, 1
218         rjmp 5f
219 3:      movw r22, r24
220         movw r24, CMD_0
221         call strcmp_P
222         adiw r24, 0
223         breq snc_found
224         adiw r28, CMDLIST_ENTRY_SIZE
225         rjmp 1b
226 snc_found:
227         movw r24, CMD_0
228         add r24, LEN_0
229         adc r25, LEN_1
230         adiw r24, 1
231         movw r30, r28
232         adiw r30, 4
233         lpm r22, Z+
234         lpm r23, Z+
235         movw r30, r22
236         adiw r30, 0
237         breq 4f ;snc_return_2
238         icall
239         ldi r24, 1
240         rjmp 5f
241 4:      ldi r24, 2
242 snc_return_0:
243         clr r24
244 5:      pop LEN_1
245         pop LEN_0
246         pop CMD_1
247         pop CMD_0
248         pop r29
249         pop r28
250         rjmp snc_return
251
252 exit_str:
253 .asciz "exit"
254 help_str:
255 .asciz "help"
256 nocommand_str:
257 .asciz "\r\ncommand not found, try 'help'"
258
259 /******************************************************************************/
260 /* void cli_option_listing(char *buffer, PGM_VOID_P cmdlist)
261  *   param buffer:    r24:r25
262  *   param cmdlist:   r22:r23
263  */
264 ITM_0 = 12
265 ITM_1 = 13
266 CMD_0 = 14
267 CMD_1 = 15
268 LEN_0 = 16
269 LEN_1 = 17
270 LST_0 = 28
271 LST_1 = 29
272 .global cli_option_listing
273 cli_option_listing:
274         push_range 12, 17
275         push_range 28, 29
276         movw CMD_0, r24
277         movw LST_0, r22
278         call strlen
279         sbiw r24, 1
280         movw LEN_0, r24
281         brpl 1f
282         clr LEN_0
283         clr LEN_1
284 1:
285         movw r30, LST_0
286         lpm r24, Z+
287         lpm r25, Z+
288         movw ITM_0, r24
289         adiw r24, 0
290         breq 99f
291         movw r22, r24
292         movw r24, CMD_0
293         movw r20, LEN_0
294         adiw r28, CMDLIST_ENTRY_SIZE
295         call strncmp_P
296         adiw r24, 0
297         brne 1b
298         ldi r24, lo8(itemprefix_str)
299         ldi r25, hi8(itemprefix_str)
300         rcall cli_putstr_P
301         movw r24, ITM_0
302         rcall cli_putstr_P
303         rjmp 1b
304 99:
305         ldi r24, lo8(prompt_str)
306         ldi r25, hi8(prompt_str)
307         rcall cli_putstr_P
308         movw r24, CMD_0
309         rcall cli_putstr
310         pop_range 28, 29
311         pop_range 12, 17
312         ret
313
314 prompt_str:
315 .asciz "\r\n>"
316 itemprefix_str:
317 .asciz "\r\n   "
318
319 /******************************************************************************/
320 /* void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist)
321  *   param maxcmdlength:  r24:r25
322  *   param cmdlist:       r22:r23
323  */
324
325 TMP_0 = 14
326 TMP_1 = 15
327 LEN_0 = 16
328 LEN_1 = 17
329 LST_0 = 28
330 LST_1 = 29
331 cli_auto_help:
332         push_range 16, 17
333         push_range 28, 29
334         movw LEN_0, r24
335         movw r28, r22
336         ldi r24, lo8(ahelphead_str)
337         ldi r25, hi8(ahelphead_str)
338         rcall cli_putstr_P
339 1:
340         movw r30, r28
341         lpm r24, Z+
342         lpm r25, Z+
343         movw r28, r30
344         adiw r24, 0
345         breq 99f
346         movw TMP_0, r24
347         rcall cli_putstr_P
348         movw r24, TMP_0
349         call strlen_PF
350         movw TMP_0, LEN_0
351         sub TMP_0, r24
352         sbc TMP_1, r25
353         breq 4f
354 2:      ldi r24, ' '
355         clr r25
356         rcall cli_putc
357         dec TMP_0
358         brne 2b
359 4:      ldi r24, lo8(sep_str)
360         ldi r25, hi8(sep_str)
361         rcall cli_putstr_P
362         ldi r22, lo8(none_str)
363         ldi r23, hi8(none_str)
364         movw r30, r28
365         lpm r24, Z+
366         lpm r25, Z+
367         movw r28, r30
368         adiw r24, 0
369         breq 5f
370         ldi r22, lo8(yes_str)
371         ldi r23, hi8(yes_str)
372 5:      movw r24, r22
373         rcall cli_putstr_P
374         ldi r24, lo8(sep2_str)
375         ldi r25, hi8(sep2_str)
376         rcall cli_putstr_P
377         movw r30, r28
378         adiw r30, 1
379         lpm r24, Z
380         rcall cli_hexdump_byte
381         movw r30, r28
382         lpm r24, Z
383         rcall cli_hexdump_byte
384         ldi r24, '\r'
385         clr r25
386         rcall cli_putc
387         ldi r24, '\n'
388         clr r25
389         rcall cli_putc
390         adiw r28, CMDLIST_ENTRY_SIZE-4
391         rjmp 1b
392 99:
393         pop_range 28, 29
394         pop_range 14, 17
395         ori r24, 1
396         ret
397 ahelphead_str:
398 .asciz "\r\n[auto help] available commands:\r\n <command> - <params> - <address>\r\n"
399 sep_str:
400 .asciz " - "
401 none_str:
402 .asciz "none"
403 yes_str:
404 .asciz "yes "
405 sep2_str:
406 .asciz" \t - 0x"
407 .byte 0
408
409
410
411