-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.asm
283 lines (267 loc) · 5.61 KB
/
files.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Copyright Jacques Deschênes 2019,2022
; This file is part of stm8_tbi
;
; stm8_tbi 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.
;
; stm8_tbi 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 stm8_tbi. If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------
; file system
; file header:
; sign field: 2 bytes .ascii "TB"
; program size: 1 word
; data: n*BLOCK_SIZE
; BLOCK_SIZE is 128 bytes depend
; on MCU block erase size.
;----------------------------------
.if SEPARATE
.module FILES
.include "config.inc"
.area CODE
.endif
NO_LABEL: .asciz "Can't save, program not labeled."
SIGNATURE: .ascii "TB"
ERASED: .ascii "XX" ; erased file, replace signature.
;-------------------------------
; search a program in flash
; memory.
; The file name is identified
; by a label on first line
; of program.
; input:
; x *name
; output:
; X prog_data_addr|0
;-------------------------------
WLKPTR=1
PNAME=3
LIMIT=5
YSAVE=7
VSIZE=6
search_program:
pushw y
_vars VSIZE
ldw (PNAME,sp),x
ldw x,#0xFFFF
ldw (LIMIT,sp),x ; search end address
ldw x,#app_space ; search start address
1$: ldw (WLKPTR,sp),x
ldw x,(x)
jreq 8$ ; end of search
ldw x,(WLKPTR,sp)
_qsign ; query signature
jrne 4$
ld a,(FILE_DATA+LINE_HEADER_SIZE,x)
cp a,#LABEL_IDX
jrne 4$
addw x,#FILE_DATA+LINE_HEADER_SIZE+2 ; label string
ldw y,(PNAME,sp)
call strcmp
jreq 6$
ldw x,(WLKPTR,sp)
4$: ; erased file ignore it.
call skip_to_next
cpw x,(LIMIT,sp)
jrult 1$
clrw x
jra 8$
6$: ; found label
ldw x,(WLKPTR,sp)
8$:
_drop VSIZE
popw y
ret
;-----------------------------------
; erase program file
; keep signature and size fields.
; signature replaced by "XX"
; input:
; X *address, point to signature
;-----------------------------------
ADDR=1 ; program address
PRG_SIZE=3 ; program size
BLOCKS=5 ; blocks to erase
VSIZE=6
erase_file:
call move_erase_to_ram
_clrz farptr
_vars VSIZE
1$:
ldw (ADDR,sp),x
ldw x,(FILE_SIZE_FIELD,x)
ldw (PRG_SIZE,sp),x
ld a,#BLOCK_SIZE
div x,a
tnz a ; test for remainder
jreq 2$
incw x ; 1 more block to erase
2$: ldw (BLOCKS,sp),x
ldw x,(ADDR,sp)
ldw ptr16,x
3$: ld a,#'E
call putc
call block_erase
ldw x,#BLOCK_SIZE
call incr_farptr
ldw x,(BLOCKS,sp)
decw x
ldw (BLOCKS,sp),x
jrne 3$
; write XX and size at addr
ldw x,(ADDR,sp)
ldw ptr16,x
ld a,#'X
clrw x
call write_byte
call write_byte
ld a,(PRG_SIZE,sp)
call write_byte
ld a,(PRG_SIZE+1,sp)
call write_byte
_drop VSIZE
9$:
ret
;--------------------------------------
; fill write buffer
; input:
; y point to output buffer
; x point to source
; a bytes to write in buffer
; output:
; y += A
; X += A
; A 0
;---------------------------------------
fill_write_buffer:
push a
tnz a
jreq 9$
1$: ld a,(x)
incw x
ld (y),a
incw y
dec (1,sp)
jrne 1$
9$: pop a
ret
;--------------------------------------
; fill pad buffer with zero
; input:
; none
; output:
; y buffer address
;--------------------------------------
clear_block_buffer:
push a
ldw y,#block_buffer
pushw y
ld a,#BLOCK_SIZE
1$: clr (y)
incw y
dec a
jrne 1$
9$: popw y
pop a
ret
;----------------------------------
; search a free space space that
; fit program size
; input:
; X program data size
; output:
; X address | 0
;------------------------------------
PG_SIZE=1
VSIZE=2
search_fit:
addw x,#FILE_HEADER_SIZE
pushw x; PG_SIZE
ldw x,#app_space
1$: _is_erased
jreq 4$
ld a,(x)
or a,(1,x)
jreq 5$ ; free space, end of search
2$: call skip_to_next
tnzw x ;
jreq 9$ ; end of memory
jra 1$
4$: ; erased program
; does it fit?
ldw acc16,x
ldw x,(FILE_SIZE_FIELD,x) ; size erased program
; top multiple of BLOCK_SIZE
addw x,#FILE_HEADER_SIZE
addw x,#BLOCK_SIZE-1
ld a,xl
and a,#BLOCK_SIZE
ld xl,a
cpw x,(PG_SIZE,sp) ; size program to save
jruge 8$ ; fit
_ldxz acc16
jra 2$
; is free space large enough?
5$: ldw acc16,x
ldw x,#0xFFFF
subw x,acc16
incw x ; because limit is 0x10000 not 0xffff
cpw x,(PG_SIZE,sp)
jruge 8$
clrw x
jra 9$
8$: _ldxz acc16 ; fit in this one
9$: _drop VSIZE
ret
;-------------------------
; erase header and
; size fields
; input:
; X program address
; output:
; X unchanged
;-------------------------
COUNT=1
erase_header:
pushw x
push #FILE_HEADER_SIZE ; COUNT
_clrz farptr
ldw ptr16,x
clr a
clrw x
1$: call write_byte
dec (COUNT,sp)
jrne 1$
_drop 1
popw x
ldw ptr16,x
ret
;----------------------------
; skip to next program
; block
; input:
; X actual file program addr
; output:
; X next block
; after program
;----------------------------
skip_to_next:
ldw acc16, x
ldw x,(FILE_SIZE_FIELD,x) ; data size
addw x,#FILE_HEADER_SIZE
addw x,acc16
; align to next block
addw x,#BLOCK_SIZE-1
ld a,xl
and a,#BLOCK_SIZE
ld xl,a
ret