04cde5b1bf0d3e6e099920fb1d74fd0e396d01b2
[fw/openocd] / contrib / loaders / erase_check / stm8_erase_check.s
1 /*
2 *   Copyright (C) 2017 Ake Rehnman
3 *   ake.rehnman(at)gmail.com
4 *
5 *   This program is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 ;;
19 ;; erase check memory code
20 ;;
21  .org 0x0
22 ;; start address
23  start_addr: .byte 0x00
24        .word 0x8000
25 ;; byte count
26  byte_cnt: .byte 0x00
27        .word 0x8000
28 ;
29 ; SP must point to start_addr on entry
30 ; first relocate start_addr to the location
31 ; we are running at
32 start:
33         ldw X,SP
34         ldw .cont+2,X
35         ldw X,(start_addr+1,SP) ;start addr
36         ldw Y,(byte_cnt+1,SP)   ;count
37         ld A,#0xff
38 ;
39 ; if count == 0 return
40 .L1:
41         tnzw Y
42         jrne .decrcnt   ;continue if low word != 0
43         tnz (byte_cnt,SP)       ;high byte
44         jreq .exit      ;goto exit
45 ;
46 ; decrement count (byte_cnt)
47 .decrcnt:
48         tnzw Y  ;low word count
49         jrne .decr1
50         dec (byte_cnt,SP)       ;high byte
51 .decr1:
52         decw Y; decr low word
53 ;
54 ; first check if [start_addr] is 0xff
55 .cont:
56         ldf A, [start_addr.e]
57         cp A,#0xff
58         jrne .exit ;exit if not 0xff
59 ;
60 ; increment start_addr (addr)
61         incw X
62         jrne .L1
63         inc (start_addr,SP)     ;increment high byte
64         jra .L1
65 ;
66 .exit:
67         ldw (start_addr+1,SP),X ;start addr
68         ldw (byte_cnt+1,SP),Y   ;count
69         break