-
Notifications
You must be signed in to change notification settings - Fork 1
/
flash_boot.asm
260 lines (214 loc) · 4.44 KB
/
flash_boot.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
CAD U SPI_RESET_AND_ENABLE
;=========================================================
; Initialize the SPI flash memory for reading from address
; 0x0f0000 upwards.
;=========================================================
MVI 0 3
CAD U SPI_WRITE_BYTE
MVI 0 0x0f
CAD U SPI_WRITE_BYTE
MVI 0 0
CAD U SPI_WRITE_BYTE
CAD U SPI_WRITE_BYTE
;==================================================
; Read the program size into R2 (MSB) and R3 (LSB).
;==================================================
CAD U SPI_READ_BYTE
MVS 0
MVD 2
CAD U SPI_READ_BYTE
MVS 0
MVD 3
; get bootloader size
MVI 4 0
CAD U GET_BOOTLOADER_SIZE
MVS 0
MVD 5
;===================================
; R6 <= MSB of number of bytes read.
; R7 <= LSB of number of bytes read.
; R0 <= (R2 XOR R6) OR (R3 XOR R7)
; If R0 == 0, all bytes read.
;===================================
MVI 6 0 ; initialize R6
MVI 7 0 ; initialize R7
PSH 0 ; push anything
READ_USER_CODE_LOOP_START:
; R0 <= (R2 XOR R6)
MVS 2
XRA 6
MVD 0
; R0 <= (R3 XOR R7) OR R0
MVS 3
XRA 7
ORA 0
MVD 0
MVS 0
JPD Z READ_USER_CODE_LOOP_END
; read next byte
CAD U SPI_READ_BYTE
; store it
MVB 4
MVS 5
STR 0
; increment address
INC 5
ADI 4 0
; increment byte counter
INC 7
ADI 6 0
JPD U READ_USER_CODE_LOOP_START
READ_USER_CODE_LOOP_END:
JPD U BOOTLOADER_END
;==============================================
; Resets the SPI flash memory.
; This is done by driving CS high and then low.
; SPI_CLK is low after this.
;==============================================
SPI_RESET_AND_ENABLE:
PSH 0
PSH 1
MVI 0 0xff
MVB 0
MVI 0 0xe7
; reset SPI
MVI 1 8
MVS 0
STR 1
; enable SPI
MVI 1 0
MVS 0
STR 1
POP 1
POP 0
RET U
;========================================
; Reads a single byte from the SPI flash.
; Requires the SPI_CLK to be low.
; Returns the byte in R0.
;========================================
SPI_WRITE_BYTE:
PSH 1
PSH 2
PSH 3
PSH 4
MVI 1 0xff
MVB 1
MVI 1 0xe7
MVS 1
LOD 2
MVI 3 8
SPI_WRITE_BYTE.WRITE_LOOP_START:
MVS 3
JPD Z SPI_WRITE_BYTE.WRITE_LOOP_END
; extract MSB of data
MVS 0
MVD 4
RRC 4
RRC 4
RRC 4
RRC 4
RRC 4
RRC 4
RRC 4
ANI 4 1
ANI 2 0x0e
MVS 4
ORA 2
MVD 2
; write data to SPI register
MVS 1
STR 2
; give clock pulse
XRI 2 4
MVS 1
STR 2
XRI 2 4
MVS 1
STR 2
RLC 0
DCR 3
JPD U SPI_WRITE_BYTE.WRITE_LOOP_START
SPI_WRITE_BYTE.WRITE_LOOP_END:
POP 4
POP 3
POP 2
POP 1
RET U
;============================================
; Writes a single byte (R0) to the SPI flash.
; Requires the SPI_CLK to be low.
;============================================
SPI_READ_BYTE:
PSH 1
PSH 2
PSH 3
PSH 4
MVI 0 0
MVI 1 0xff
MVB 1
MVI 1 0xe7
MVS 1
LOD 2
MVI 3 8
SPI_READ_BYTE.READ_LOOP_START:
MVS 3
JPD Z SPI_READ_BYTE.READ_LOOP_END
RLC 0
; read data
RRC 2
ANI 2 1
MVS 2
ORA 0
MVD 0
; give clock pulse
MVS 1
LOD 2
XRI 2 4
MVS 1
STR 2
XRI 2 4
MVS 1
STR 2
MVS 1
LOD 2
DCR 3
JPD U SPI_READ_BYTE.READ_LOOP_START
SPI_READ_BYTE.READ_LOOP_END:
POP 4
POP 3
POP 2
POP 1
RET U
;=================================
; Disables the SPI flash chip.
; This is done by driving CS high.
;=================================
SPI_DISABLE:
PSH 0
PSH 1
MVI 0 0xff
MVB 0
MVI 0 0xe7
MVI 1 8
MVS 0
STR 1
POP 1
POP 0
RET U
;=========================================
; Returns the size of the bootloader code.
; Note: This should always be in the end.
; CAUTION: DO NOT MODIFY THE CODE BELOW!!
;=========================================
GET_BOOTLOADER_SIZE:
CAD U SYS_GET_BOOTLOADER_SIZE
RET U
SYS_GET_BOOTLOADER_SIZE:
POP 1
POP 0
PSH 0
PSH 1
ADI 0 8
RET U
BOOTLOADER_END: