1 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
2 | (* Interpret.ma: Operational semantics for the 8051/8052 processor. *) |
---|
3 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
4 | |
---|
5 | include "ASM/ASM.ma". |
---|
6 | include "ASM/Arithmetic.ma". |
---|
7 | include "ASM/BitVectorTrie.ma". |
---|
8 | |
---|
9 | definition Time ≝ nat. |
---|
10 | |
---|
11 | inductive SerialBufferType: Type[0] ≝ |
---|
12 | Eight: Byte → SerialBufferType |
---|
13 | | Nine: Bit → Byte → SerialBufferType. |
---|
14 | |
---|
15 | inductive LineType: Type[0] ≝ |
---|
16 | P1: Byte → LineType |
---|
17 | | P3: Byte → LineType |
---|
18 | | SerialBuffer: SerialBufferType → LineType. |
---|
19 | |
---|
20 | (* What is a continuation, now? *) |
---|
21 | |
---|
22 | inductive SFR8051: Type[0] ≝ |
---|
23 | SFR_SP: SFR8051 |
---|
24 | | SFR_DPL: SFR8051 |
---|
25 | | SFR_DPH: SFR8051 |
---|
26 | | SFR_PCON: SFR8051 |
---|
27 | | SFR_TCON: SFR8051 |
---|
28 | | SFR_TMOD: SFR8051 |
---|
29 | | SFR_TL0: SFR8051 |
---|
30 | | SFR_TL1: SFR8051 |
---|
31 | | SFR_TH0: SFR8051 |
---|
32 | | SFR_TH1: SFR8051 |
---|
33 | | SFR_P1: SFR8051 |
---|
34 | | SFR_SCON: SFR8051 |
---|
35 | | SFR_SBUF: SFR8051 |
---|
36 | | SFR_IE: SFR8051 |
---|
37 | | SFR_P3: SFR8051 |
---|
38 | | SFR_IP: SFR8051 |
---|
39 | | SFR_PSW: SFR8051 |
---|
40 | | SFR_ACC_A: SFR8051 |
---|
41 | | SFR_ACC_B: SFR8051. |
---|
42 | |
---|
43 | definition sfr_8051_index ≝ |
---|
44 | λs: SFR8051. |
---|
45 | match s with |
---|
46 | [ SFR_SP ⇒ O |
---|
47 | | SFR_DPL ⇒ 1 |
---|
48 | | SFR_DPH ⇒ 2 |
---|
49 | | SFR_PCON ⇒ 3 |
---|
50 | | SFR_TCON ⇒ 4 |
---|
51 | | SFR_TMOD ⇒ 5 |
---|
52 | | SFR_TL0 ⇒ 6 |
---|
53 | | SFR_TL1 ⇒ 7 |
---|
54 | | SFR_TH0 ⇒ 8 |
---|
55 | | SFR_TH1 ⇒ 9 |
---|
56 | | SFR_P1 ⇒ 10 |
---|
57 | | SFR_SCON ⇒ 11 |
---|
58 | | SFR_SBUF ⇒ 12 |
---|
59 | | SFR_IE ⇒ 13 |
---|
60 | | SFR_P3 ⇒ 14 |
---|
61 | | SFR_IP ⇒ 15 |
---|
62 | | SFR_PSW ⇒ 16 |
---|
63 | | SFR_ACC_A ⇒ 17 |
---|
64 | | SFR_ACC_B ⇒ 18 |
---|
65 | ]. |
---|
66 | |
---|
67 | inductive SFR8052: Type[0] ≝ |
---|
68 | SFR_T2CON: SFR8052 |
---|
69 | | SFR_RCAP2L: SFR8052 |
---|
70 | | SFR_RCAP2H: SFR8052 |
---|
71 | | SFR_TL2: SFR8052 |
---|
72 | | SFR_TH2: SFR8052. |
---|
73 | |
---|
74 | definition sfr_8052_index ≝ |
---|
75 | λs: SFR8052. |
---|
76 | match s with |
---|
77 | [ SFR_T2CON ⇒ O |
---|
78 | | SFR_RCAP2L ⇒ 1 |
---|
79 | | SFR_RCAP2H ⇒ 2 |
---|
80 | | SFR_TL2 ⇒ 3 |
---|
81 | | SFR_TH2 ⇒ 4 |
---|
82 | ]. |
---|
83 | |
---|
84 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
85 | (* Processor status. *) |
---|
86 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
87 | record PreStatus (M: Type[0]): Type[0] ≝ |
---|
88 | { |
---|
89 | code_memory: M; |
---|
90 | low_internal_ram: BitVectorTrie Byte 7; |
---|
91 | high_internal_ram: BitVectorTrie Byte 7; |
---|
92 | external_ram: BitVectorTrie Byte 16; |
---|
93 | |
---|
94 | program_counter: Word; |
---|
95 | |
---|
96 | special_function_registers_8051: Vector Byte 19; |
---|
97 | special_function_registers_8052: Vector Byte 5; |
---|
98 | |
---|
99 | p1_latch: Byte; |
---|
100 | p3_latch: Byte; |
---|
101 | |
---|
102 | clock: Time |
---|
103 | }. |
---|
104 | |
---|
105 | definition Status ≝ PreStatus (BitVectorTrie Byte 16). |
---|
106 | definition PseudoStatus ≝ PreStatus (pseudo_assembly_program). |
---|
107 | |
---|
108 | lemma sfr8051_index_19: |
---|
109 | ∀i: SFR8051. |
---|
110 | sfr_8051_index i < 19. |
---|
111 | # i |
---|
112 | cases i |
---|
113 | normalize |
---|
114 | repeat (@ le_S_S) |
---|
115 | @ le_O_n |
---|
116 | qed. |
---|
117 | |
---|
118 | lemma sfr8052_index_5: |
---|
119 | ∀i: SFR8052. |
---|
120 | sfr_8052_index i < 5. |
---|
121 | # i |
---|
122 | cases i |
---|
123 | normalize |
---|
124 | repeat (@ le_S_S) |
---|
125 | @ le_O_n |
---|
126 | qed. |
---|
127 | |
---|
128 | definition set_clock ≝ |
---|
129 | λM: Type[0]. |
---|
130 | λs: PreStatus M. |
---|
131 | λt: Time. |
---|
132 | let old_code_memory ≝ code_memory ? s in |
---|
133 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
134 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
135 | let old_external_ram ≝ external_ram ? s in |
---|
136 | let old_program_counter ≝ program_counter ? s in |
---|
137 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
138 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
139 | let old_p1_latch ≝ p1_latch ? s in |
---|
140 | let old_p3_latch ≝ p3_latch ? s in |
---|
141 | mk_PreStatus M old_code_memory |
---|
142 | old_low_internal_ram |
---|
143 | old_high_internal_ram |
---|
144 | old_external_ram |
---|
145 | old_program_counter |
---|
146 | old_special_function_registers_8051 |
---|
147 | old_special_function_registers_8052 |
---|
148 | old_p1_latch |
---|
149 | old_p3_latch |
---|
150 | t. |
---|
151 | |
---|
152 | definition set_p1_latch ≝ |
---|
153 | λM: Type[0]. |
---|
154 | λs: PreStatus M. |
---|
155 | λb: Byte. |
---|
156 | let old_code_memory ≝ code_memory ? s in |
---|
157 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
158 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
159 | let old_external_ram ≝ external_ram ? s in |
---|
160 | let old_program_counter ≝ program_counter ? s in |
---|
161 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
162 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
163 | let old_p3_latch ≝ p3_latch ? s in |
---|
164 | let old_clock ≝ clock ? s in |
---|
165 | mk_PreStatus M old_code_memory |
---|
166 | old_low_internal_ram |
---|
167 | old_high_internal_ram |
---|
168 | old_external_ram |
---|
169 | old_program_counter |
---|
170 | old_special_function_registers_8051 |
---|
171 | old_special_function_registers_8052 |
---|
172 | b |
---|
173 | old_p3_latch |
---|
174 | old_clock. |
---|
175 | |
---|
176 | definition set_p3_latch ≝ |
---|
177 | λM: Type[0]. |
---|
178 | λs: PreStatus M. |
---|
179 | λb: Byte. |
---|
180 | let old_code_memory ≝ code_memory ? s in |
---|
181 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
182 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
183 | let old_external_ram ≝ external_ram ? s in |
---|
184 | let old_program_counter ≝ program_counter ? s in |
---|
185 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
186 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
187 | let old_p1_latch ≝ p1_latch ? s in |
---|
188 | let old_clock ≝ clock ? s in |
---|
189 | mk_PreStatus M old_code_memory |
---|
190 | old_low_internal_ram |
---|
191 | old_high_internal_ram |
---|
192 | old_external_ram |
---|
193 | old_program_counter |
---|
194 | old_special_function_registers_8051 |
---|
195 | old_special_function_registers_8052 |
---|
196 | old_p1_latch |
---|
197 | b |
---|
198 | old_clock. |
---|
199 | |
---|
200 | definition get_8051_sfr ≝ |
---|
201 | λM: Type[0]. |
---|
202 | λs: PreStatus M. |
---|
203 | λi: SFR8051. |
---|
204 | let sfr ≝ special_function_registers_8051 ? s in |
---|
205 | let index ≝ sfr_8051_index i in |
---|
206 | get_index_v … sfr index ?. |
---|
207 | @ sfr8051_index_19 |
---|
208 | qed. |
---|
209 | |
---|
210 | definition get_8052_sfr ≝ |
---|
211 | λM: Type[0]. |
---|
212 | λs: PreStatus M. |
---|
213 | λi: SFR8052. |
---|
214 | let sfr ≝ special_function_registers_8052 ? s in |
---|
215 | let index ≝ sfr_8052_index i in |
---|
216 | get_index_v … sfr index ?. |
---|
217 | @ sfr8052_index_5 |
---|
218 | qed. |
---|
219 | |
---|
220 | definition set_8051_sfr ≝ |
---|
221 | λM: Type[0]. |
---|
222 | λs: PreStatus M. |
---|
223 | λi: SFR8051. |
---|
224 | λb: Byte. |
---|
225 | let index ≝ sfr_8051_index i in |
---|
226 | let old_code_memory ≝ code_memory ? s in |
---|
227 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
228 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
229 | let old_external_ram ≝ external_ram ? s in |
---|
230 | let old_program_counter ≝ program_counter ? s in |
---|
231 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
232 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
233 | let new_special_function_registers_8051 ≝ |
---|
234 | set_index Byte 19 old_special_function_registers_8051 index b ? in |
---|
235 | let old_p1_latch ≝ p1_latch ? s in |
---|
236 | let old_p3_latch ≝ p3_latch ? s in |
---|
237 | let old_clock ≝ clock ? s in |
---|
238 | mk_PreStatus M old_code_memory |
---|
239 | old_low_internal_ram |
---|
240 | old_high_internal_ram |
---|
241 | old_external_ram |
---|
242 | old_program_counter |
---|
243 | new_special_function_registers_8051 |
---|
244 | old_special_function_registers_8052 |
---|
245 | old_p1_latch |
---|
246 | old_p3_latch |
---|
247 | old_clock. |
---|
248 | @ (sfr8051_index_19 i) |
---|
249 | qed. |
---|
250 | |
---|
251 | definition set_8052_sfr ≝ |
---|
252 | λM: Type[0]. |
---|
253 | λs: PreStatus M. |
---|
254 | λi: SFR8052. |
---|
255 | λb: Byte. |
---|
256 | let index ≝ sfr_8052_index i in |
---|
257 | let old_code_memory ≝ code_memory ? s in |
---|
258 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
259 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
260 | let old_external_ram ≝ external_ram ? s in |
---|
261 | let old_program_counter ≝ program_counter ? s in |
---|
262 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
263 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
264 | let new_special_function_registers_8052 ≝ |
---|
265 | set_index Byte 5 old_special_function_registers_8052 index b ? in |
---|
266 | let old_p1_latch ≝ p1_latch ? s in |
---|
267 | let old_p3_latch ≝ p3_latch ? s in |
---|
268 | let old_clock ≝ clock ? s in |
---|
269 | mk_PreStatus M old_code_memory |
---|
270 | old_low_internal_ram |
---|
271 | old_high_internal_ram |
---|
272 | old_external_ram |
---|
273 | old_program_counter |
---|
274 | old_special_function_registers_8051 |
---|
275 | new_special_function_registers_8052 |
---|
276 | old_p1_latch |
---|
277 | old_p3_latch |
---|
278 | old_clock. |
---|
279 | @ (sfr8052_index_5 i) |
---|
280 | qed. |
---|
281 | |
---|
282 | definition set_program_counter ≝ |
---|
283 | λM: Type[0]. |
---|
284 | λs: PreStatus M. |
---|
285 | λw: Word. |
---|
286 | let old_code_memory ≝ code_memory ? s in |
---|
287 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
288 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
289 | let old_external_ram ≝ external_ram ? s in |
---|
290 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
291 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
292 | let old_p1_latch ≝ p1_latch ? s in |
---|
293 | let old_p3_latch ≝ p3_latch ? s in |
---|
294 | let old_clock ≝ clock ? s in |
---|
295 | mk_PreStatus M old_code_memory |
---|
296 | old_low_internal_ram |
---|
297 | old_high_internal_ram |
---|
298 | old_external_ram |
---|
299 | w |
---|
300 | old_special_function_registers_8051 |
---|
301 | old_special_function_registers_8052 |
---|
302 | old_p1_latch |
---|
303 | old_p3_latch |
---|
304 | old_clock. |
---|
305 | |
---|
306 | definition set_code_memory ≝ |
---|
307 | λM,M': Type[0]. |
---|
308 | λs: PreStatus M. |
---|
309 | λr: M'. |
---|
310 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
311 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
312 | let old_external_ram ≝ external_ram ? s in |
---|
313 | let old_program_counter ≝ program_counter ? s in |
---|
314 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
315 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
316 | let old_p1_latch ≝ p1_latch ? s in |
---|
317 | let old_p3_latch ≝ p3_latch ? s in |
---|
318 | let old_clock ≝ clock ? s in |
---|
319 | mk_PreStatus M' r |
---|
320 | old_low_internal_ram |
---|
321 | old_high_internal_ram |
---|
322 | old_external_ram |
---|
323 | old_program_counter |
---|
324 | old_special_function_registers_8051 |
---|
325 | old_special_function_registers_8052 |
---|
326 | old_p1_latch |
---|
327 | old_p3_latch |
---|
328 | old_clock. |
---|
329 | |
---|
330 | definition set_low_internal_ram ≝ |
---|
331 | λM: Type[0]. |
---|
332 | λs: PreStatus M. |
---|
333 | λr: BitVectorTrie Byte 7. |
---|
334 | let old_code_memory ≝ code_memory ? s in |
---|
335 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
336 | let old_external_ram ≝ external_ram ? s in |
---|
337 | let old_program_counter ≝ program_counter ? s in |
---|
338 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
339 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
340 | let old_p1_latch ≝ p1_latch ? s in |
---|
341 | let old_p3_latch ≝ p3_latch ? s in |
---|
342 | let old_clock ≝ clock ? s in |
---|
343 | mk_PreStatus M old_code_memory |
---|
344 | r |
---|
345 | old_high_internal_ram |
---|
346 | old_external_ram |
---|
347 | old_program_counter |
---|
348 | old_special_function_registers_8051 |
---|
349 | old_special_function_registers_8052 |
---|
350 | old_p1_latch |
---|
351 | old_p3_latch |
---|
352 | old_clock. |
---|
353 | |
---|
354 | definition set_high_internal_ram ≝ |
---|
355 | λM: Type[0]. |
---|
356 | λs: PreStatus M. |
---|
357 | λr: BitVectorTrie Byte 7. |
---|
358 | let old_code_memory ≝ code_memory ? s in |
---|
359 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
360 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
361 | let old_external_ram ≝ external_ram ? s in |
---|
362 | let old_program_counter ≝ program_counter ? s in |
---|
363 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
364 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
365 | let old_p1_latch ≝ p1_latch ? s in |
---|
366 | let old_p3_latch ≝ p3_latch ? s in |
---|
367 | let old_clock ≝ clock ? s in |
---|
368 | mk_PreStatus M old_code_memory |
---|
369 | old_low_internal_ram |
---|
370 | r |
---|
371 | old_external_ram |
---|
372 | old_program_counter |
---|
373 | old_special_function_registers_8051 |
---|
374 | old_special_function_registers_8052 |
---|
375 | old_p1_latch |
---|
376 | old_p3_latch |
---|
377 | old_clock. |
---|
378 | |
---|
379 | definition set_external_ram ≝ |
---|
380 | λM: Type[0]. |
---|
381 | λs: PreStatus M. |
---|
382 | λr: BitVectorTrie Byte 16. |
---|
383 | let old_code_memory ≝ code_memory ? s in |
---|
384 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
385 | let old_high_internal_ram ≝ high_internal_ram ? s in |
---|
386 | let old_program_counter ≝ program_counter ? s in |
---|
387 | let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in |
---|
388 | let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in |
---|
389 | let old_p1_latch ≝ p1_latch ? s in |
---|
390 | let old_p3_latch ≝ p3_latch ? s in |
---|
391 | let old_clock ≝ clock ? s in |
---|
392 | mk_PreStatus M old_code_memory |
---|
393 | old_low_internal_ram |
---|
394 | old_high_internal_ram |
---|
395 | r |
---|
396 | old_program_counter |
---|
397 | old_special_function_registers_8051 |
---|
398 | old_special_function_registers_8052 |
---|
399 | old_p1_latch |
---|
400 | old_p3_latch |
---|
401 | old_clock. |
---|
402 | |
---|
403 | definition get_cy_flag ≝ |
---|
404 | λM: Type[0]. |
---|
405 | λs: PreStatus M. |
---|
406 | let sfr ≝ special_function_registers_8051 ? s in |
---|
407 | let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in |
---|
408 | get_index_v bool 8 psw O ?. |
---|
409 | normalize |
---|
410 | @ (le_S_S ? ?) |
---|
411 | [ @ le_O_n |
---|
412 | | repeat (@ (le_S_S)); |
---|
413 | // |
---|
414 | ] |
---|
415 | qed. |
---|
416 | |
---|
417 | definition get_ac_flag ≝ |
---|
418 | λM: Type[0]. |
---|
419 | λs: PreStatus M. |
---|
420 | let sfr ≝ special_function_registers_8051 ? s in |
---|
421 | let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in |
---|
422 | get_index_v bool 8 psw (S O) ?. |
---|
423 | normalize |
---|
424 | repeat (@ (le_S_S ? ?)) |
---|
425 | @ le_O_n |
---|
426 | qed. |
---|
427 | |
---|
428 | definition get_fo_flag ≝ |
---|
429 | λM: Type[0]. |
---|
430 | λs: PreStatus M. |
---|
431 | let sfr ≝ special_function_registers_8051 ? s in |
---|
432 | let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in |
---|
433 | get_index_v bool 8 psw 2 ?. |
---|
434 | normalize |
---|
435 | repeat (@ (le_S_S ? ?)) |
---|
436 | @ le_O_n |
---|
437 | qed. |
---|
438 | |
---|
439 | definition get_rs1_flag ≝ |
---|
440 | λM: Type[0]. |
---|
441 | λs: PreStatus M. |
---|
442 | let sfr ≝ special_function_registers_8051 ? s in |
---|
443 | let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in |
---|
444 | get_index_v bool 8 psw 3 ?. |
---|
445 | normalize |
---|
446 | repeat (@ (le_S_S ? ?)) |
---|
447 | @ le_O_n |
---|
448 | qed. |
---|
449 | |
---|
450 | definition get_rs0_flag ≝ |
---|
451 | λM: Type[0]. |
---|
452 | λs: PreStatus M. |
---|
453 | let sfr ≝ special_function_registers_8051 ? s in |
---|
454 | let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in |
---|
455 | get_index_v bool 8 psw 4 ?. |
---|
456 | normalize |
---|
457 | repeat (@ (le_S_S ? ?)) |
---|
458 | @ le_O_n |
---|
459 | qed. |
---|
460 | |
---|
461 | definition get_ov_flag ≝ |
---|
462 | λM: Type[0]. |
---|
463 | λs: PreStatus M. |
---|
464 | let sfr ≝ special_function_registers_8051 ? s in |
---|
465 | let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in |
---|
466 | get_index_v bool 8 psw 5 ?. |
---|
467 | normalize |
---|
468 | repeat (@ (le_S_S ? ?)) |
---|
469 | @ le_O_n |
---|
470 | qed. |
---|
471 | |
---|
472 | definition get_ud_flag ≝ |
---|
473 | λM: Type[0]. |
---|
474 | λs: PreStatus M. |
---|
475 | let sfr ≝ special_function_registers_8051 ? s in |
---|
476 | let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in |
---|
477 | get_index_v bool 8 psw 6 ?. |
---|
478 | normalize |
---|
479 | repeat (@ (le_S_S ? ?)) |
---|
480 | @ le_O_n |
---|
481 | qed. |
---|
482 | |
---|
483 | definition get_p_flag ≝ |
---|
484 | λM: Type[0]. |
---|
485 | λs: PreStatus M. |
---|
486 | let sfr ≝ special_function_registers_8051 ? s in |
---|
487 | let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in |
---|
488 | get_index_v bool 8 psw 7 ?. |
---|
489 | normalize |
---|
490 | repeat (@ (le_S_S ? ?)) |
---|
491 | @ le_O_n |
---|
492 | qed. |
---|
493 | |
---|
494 | definition set_flags ≝ |
---|
495 | λM: Type[0]. |
---|
496 | λs: PreStatus M. |
---|
497 | λcy: Bit. |
---|
498 | λac: option Bit. |
---|
499 | λov: Bit. |
---|
500 | let old_cy ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) O ? in |
---|
501 | let old_ac ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 1 ? in |
---|
502 | let old_fo ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 2 ? in |
---|
503 | let old_rs1 ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 3 ? in |
---|
504 | let old_rs0 ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 4 ? in |
---|
505 | let old_ov ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 5 ? in |
---|
506 | let old_ud ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 6 ? in |
---|
507 | let old_p ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 7 ? in |
---|
508 | let new_ac ≝ match ac with [ None ⇒ old_ac | Some j ⇒ j ] in |
---|
509 | let new_psw ≝ [[ old_cy ; new_ac ; old_fo ; old_rs1 ; |
---|
510 | old_rs0 ; old_ov ; old_ud ; old_p ]] in |
---|
511 | set_8051_sfr ? s SFR_PSW new_psw. |
---|
512 | [1,2,3,4,5,6,7,8: |
---|
513 | normalize |
---|
514 | repeat (@ le_S_S) |
---|
515 | @ le_O_n |
---|
516 | ] |
---|
517 | qed. |
---|
518 | |
---|
519 | definition initialise_status ≝ |
---|
520 | λM: Type[0]. |
---|
521 | λcode_mem: M. |
---|
522 | let status ≝ mk_PreStatus M code_mem (* Code mem. *) |
---|
523 | (Stub Byte 7) (* Low mem. *) |
---|
524 | (Stub Byte 7) (* High mem. *) |
---|
525 | (Stub Byte 16) (* Ext mem. *) |
---|
526 | (zero 16) (* PC. *) |
---|
527 | (replicate Byte 19 (zero 8)) (* 8051 SFR. *) |
---|
528 | (replicate Byte 5 (zero 8)) (* 8052 SFR. *) |
---|
529 | (zero 8) (* P1 latch. *) |
---|
530 | (zero 8) (* P3 latch. *) |
---|
531 | O (* Clock. *) |
---|
532 | in |
---|
533 | set_8051_sfr ? status SFR_SP (bitvector_of_nat 8 7). |
---|
534 | |
---|
535 | axiom not_implemented: False. |
---|
536 | |
---|
537 | definition get_bit_addressable_sfr ≝ |
---|
538 | λM: Type[0]. |
---|
539 | λs: PreStatus M. |
---|
540 | λn: nat. |
---|
541 | λb: BitVector n. |
---|
542 | λl: bool. |
---|
543 | let address ≝ nat_of_bitvector … b in |
---|
544 | if (eqb address 128) then |
---|
545 | ? |
---|
546 | else if (eqb address 144) then |
---|
547 | if l then |
---|
548 | (p1_latch ? s) |
---|
549 | else |
---|
550 | (get_8051_sfr ? s SFR_P1) |
---|
551 | else if (eqb address 160) then |
---|
552 | ? |
---|
553 | else if (eqb address 176) then |
---|
554 | if l then |
---|
555 | (p3_latch ? s) |
---|
556 | else |
---|
557 | (get_8051_sfr ? s SFR_P3) |
---|
558 | else if (eqb address 153) then |
---|
559 | get_8051_sfr ? s SFR_SBUF |
---|
560 | else if (eqb address 138) then |
---|
561 | get_8051_sfr ? s SFR_TL0 |
---|
562 | else if (eqb address 139) then |
---|
563 | get_8051_sfr ? s SFR_TL1 |
---|
564 | else if (eqb address 140) then |
---|
565 | get_8051_sfr ? s SFR_TH0 |
---|
566 | else if (eqb address 141) then |
---|
567 | get_8051_sfr ? s SFR_TH1 |
---|
568 | else if (eqb address 200) then |
---|
569 | get_8052_sfr ? s SFR_T2CON |
---|
570 | else if (eqb address 202) then |
---|
571 | get_8052_sfr ? s SFR_RCAP2L |
---|
572 | else if (eqb address 203) then |
---|
573 | get_8052_sfr ? s SFR_RCAP2H |
---|
574 | else if (eqb address 204) then |
---|
575 | get_8052_sfr ? s SFR_TL2 |
---|
576 | else if (eqb address 205) then |
---|
577 | get_8052_sfr ? s SFR_TH2 |
---|
578 | else if (eqb address 135) then |
---|
579 | get_8051_sfr ? s SFR_PCON |
---|
580 | else if (eqb address 136) then |
---|
581 | get_8051_sfr ? s SFR_TCON |
---|
582 | else if (eqb address 137) then |
---|
583 | get_8051_sfr ? s SFR_TMOD |
---|
584 | else if (eqb address 152) then |
---|
585 | get_8051_sfr ? s SFR_SCON |
---|
586 | else if (eqb address 168) then |
---|
587 | get_8051_sfr ? s SFR_IE |
---|
588 | else if (eqb address 184) then |
---|
589 | get_8051_sfr ? s SFR_IP |
---|
590 | else if (eqb address 129) then |
---|
591 | get_8051_sfr ? s SFR_SP |
---|
592 | else if (eqb address 130) then |
---|
593 | get_8051_sfr ? s SFR_DPL |
---|
594 | else if (eqb address 131) then |
---|
595 | get_8051_sfr ? s SFR_DPH |
---|
596 | else if (eqb address 208) then |
---|
597 | get_8051_sfr ? s SFR_PSW |
---|
598 | else if (eqb address 224) then |
---|
599 | get_8051_sfr ? s SFR_ACC_A |
---|
600 | else if (eqb address 240) then |
---|
601 | get_8051_sfr ? s SFR_ACC_B |
---|
602 | else |
---|
603 | ?. |
---|
604 | cases not_implemented |
---|
605 | qed. |
---|
606 | |
---|
607 | definition set_bit_addressable_sfr ≝ |
---|
608 | λM: Type[0]. |
---|
609 | λs: PreStatus M. |
---|
610 | λb: Byte. |
---|
611 | λv: Byte. |
---|
612 | let address ≝ nat_of_bitvector … b in |
---|
613 | if (eqb address 128) then |
---|
614 | ? |
---|
615 | else if (eqb address 144) then |
---|
616 | let status_1 ≝ set_8051_sfr ? s SFR_P1 v in |
---|
617 | let status_2 ≝ set_p1_latch ? s v in |
---|
618 | status_2 |
---|
619 | else if (eqb address 160) then |
---|
620 | ? |
---|
621 | else if (eqb address 176) then |
---|
622 | let status_1 ≝ set_8051_sfr ? s SFR_P3 v in |
---|
623 | let status_2 ≝ set_p3_latch ? s v in |
---|
624 | status_2 |
---|
625 | else if (eqb address 153) then |
---|
626 | set_8051_sfr ? s SFR_SBUF v |
---|
627 | else if (eqb address 138) then |
---|
628 | set_8051_sfr ? s SFR_TL0 v |
---|
629 | else if (eqb address 139) then |
---|
630 | set_8051_sfr ? s SFR_TL1 v |
---|
631 | else if (eqb address 140) then |
---|
632 | set_8051_sfr ? s SFR_TH0 v |
---|
633 | else if (eqb address 141) then |
---|
634 | set_8051_sfr ? s SFR_TH1 v |
---|
635 | else if (eqb address 200) then |
---|
636 | set_8052_sfr ? s SFR_T2CON v |
---|
637 | else if (eqb address 202) then |
---|
638 | set_8052_sfr ? s SFR_RCAP2L v |
---|
639 | else if (eqb address 203) then |
---|
640 | set_8052_sfr ? s SFR_RCAP2H v |
---|
641 | else if (eqb address 204) then |
---|
642 | set_8052_sfr ? s SFR_TL2 v |
---|
643 | else if (eqb address 205) then |
---|
644 | set_8052_sfr ? s SFR_TH2 v |
---|
645 | else if (eqb address 135) then |
---|
646 | set_8051_sfr ? s SFR_PCON v |
---|
647 | else if (eqb address 136) then |
---|
648 | set_8051_sfr ? s SFR_TCON v |
---|
649 | else if (eqb address 137) then |
---|
650 | set_8051_sfr ? s SFR_TMOD v |
---|
651 | else if (eqb address 152) then |
---|
652 | set_8051_sfr ? s SFR_SCON v |
---|
653 | else if (eqb address 168) then |
---|
654 | set_8051_sfr ? s SFR_IE v |
---|
655 | else if (eqb address 184) then |
---|
656 | set_8051_sfr ? s SFR_IP v |
---|
657 | else if (eqb address 129) then |
---|
658 | set_8051_sfr ? s SFR_SP v |
---|
659 | else if (eqb address 130) then |
---|
660 | set_8051_sfr ? s SFR_DPL v |
---|
661 | else if (eqb address 131) then |
---|
662 | set_8051_sfr ? s SFR_DPH v |
---|
663 | else if (eqb address 208) then |
---|
664 | set_8051_sfr ? s SFR_PSW v |
---|
665 | else if (eqb address 224) then |
---|
666 | set_8051_sfr ? s SFR_ACC_A v |
---|
667 | else if (eqb address 240) then |
---|
668 | set_8051_sfr ? s SFR_ACC_B v |
---|
669 | else |
---|
670 | ?. |
---|
671 | cases not_implemented |
---|
672 | qed. |
---|
673 | |
---|
674 | definition bit_address_of_register ≝ |
---|
675 | λM: Type[0]. |
---|
676 | λs: PreStatus M. |
---|
677 | λr: BitVector 3. |
---|
678 | let b ≝ get_index_v … r O ? in |
---|
679 | let c ≝ get_index_v … r 1 ? in |
---|
680 | let d ≝ get_index_v … r 2 ? in |
---|
681 | let 〈 un, ln 〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in |
---|
682 | let 〈 r1, r0 〉 ≝ 〈 get_index_v … 4 un 2 ?, get_index_v … 4 un 3 ? 〉 in |
---|
683 | let offset ≝ |
---|
684 | if ¬r1 ∧ ¬r0 then |
---|
685 | O |
---|
686 | else if ¬r1 ∧ r0 then |
---|
687 | 8 |
---|
688 | else if r1 ∧ r0 then |
---|
689 | 24 |
---|
690 | else |
---|
691 | 16 |
---|
692 | in |
---|
693 | bitvector_of_nat 7 (offset + (nat_of_bitvector ? [[ false ; b ; c ; d ]])). |
---|
694 | [1,2,3,4,5: |
---|
695 | normalize |
---|
696 | repeat (@ le_S_S) |
---|
697 | @ le_O_n; |
---|
698 | ] |
---|
699 | qed. |
---|
700 | |
---|
701 | definition get_register ≝ |
---|
702 | λM: Type[0]. |
---|
703 | λs: PreStatus M. |
---|
704 | λr: BitVector 3. |
---|
705 | let address ≝ bit_address_of_register ? s r in |
---|
706 | lookup … address (low_internal_ram ? s) (zero 8). |
---|
707 | |
---|
708 | definition set_register ≝ |
---|
709 | λM: Type[0]. |
---|
710 | λs: PreStatus M. |
---|
711 | λr: BitVector 3. |
---|
712 | λv: Byte. |
---|
713 | let address ≝ bit_address_of_register ? s r in |
---|
714 | let old_low_internal_ram ≝ low_internal_ram ? s in |
---|
715 | let new_low_internal_ram ≝ insert … address v old_low_internal_ram in |
---|
716 | set_low_internal_ram ? s new_low_internal_ram. |
---|
717 | |
---|
718 | definition read_at_stack_pointer ≝ |
---|
719 | λM: Type[0]. |
---|
720 | λs: PreStatus M. |
---|
721 | let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in |
---|
722 | let m ≝ get_index_v … nu O ? in |
---|
723 | let r1 ≝ get_index_v … nu 1 ? in |
---|
724 | let r2 ≝ get_index_v … nu 2 ? in |
---|
725 | let r3 ≝ get_index_v … nu 3 ? in |
---|
726 | let address ≝ [[ r1 ; r2 ; r3 ]] @@ nl in |
---|
727 | let memory ≝ |
---|
728 | if m then |
---|
729 | (low_internal_ram ? s) |
---|
730 | else |
---|
731 | (high_internal_ram ? s) |
---|
732 | in |
---|
733 | lookup … address memory (zero 8). |
---|
734 | [1,2,3,4: |
---|
735 | normalize |
---|
736 | repeat (@ le_S_S) |
---|
737 | @ le_O_n |
---|
738 | ] |
---|
739 | qed. |
---|
740 | |
---|
741 | definition write_at_stack_pointer ≝ |
---|
742 | λM: Type[0]. |
---|
743 | λs: PreStatus M. |
---|
744 | λv: Byte. |
---|
745 | let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in |
---|
746 | let bit_zero ≝ get_index_v… nu O ? in |
---|
747 | let bit_1 ≝ get_index_v… nu 1 ? in |
---|
748 | let bit_2 ≝ get_index_v… nu 2 ? in |
---|
749 | let bit_3 ≝ get_index_v… nu 3 ? in |
---|
750 | if bit_zero then |
---|
751 | let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl) |
---|
752 | v (low_internal_ram ? s) in |
---|
753 | set_low_internal_ram ? s memory |
---|
754 | else |
---|
755 | let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl) |
---|
756 | v (high_internal_ram ? s) in |
---|
757 | set_high_internal_ram ? s memory. |
---|
758 | [1,2,3,4: |
---|
759 | normalize |
---|
760 | repeat (@ le_S_S) |
---|
761 | @ le_O_n |
---|
762 | ] |
---|
763 | qed. |
---|
764 | |
---|
765 | definition set_arg_16: ∀M: Type[0]. PreStatus M → Word → [[ dptr ]] → PreStatus M ≝ |
---|
766 | λM. |
---|
767 | λs. |
---|
768 | λv. |
---|
769 | λa. |
---|
770 | match a return λx. bool_to_Prop (is_in ? [[ dptr ]] x) → ? with |
---|
771 | [ DPTR ⇒ λ_:True. |
---|
772 | let 〈 bu, bl 〉 ≝ split … 8 8 v in |
---|
773 | let status ≝ set_8051_sfr ? s SFR_DPH bu in |
---|
774 | let status ≝ set_8051_sfr ? status SFR_DPL bl in |
---|
775 | status |
---|
776 | | _ ⇒ λK. |
---|
777 | match K in False with |
---|
778 | [ |
---|
779 | ] |
---|
780 | ] (subaddressing_modein … a). |
---|
781 | |
---|
782 | definition get_arg_16: ∀M: Type[0]. PreStatus M → [[ data16 ]] → Word ≝ |
---|
783 | λm, s, a. |
---|
784 | match a return λx. bool_to_Prop (is_in ? [[ data16 ]] x) → ? with |
---|
785 | [ DATA16 d ⇒ λ_:True. d |
---|
786 | | _ ⇒ λK. |
---|
787 | match K in False with |
---|
788 | [ |
---|
789 | ] |
---|
790 | ] (subaddressing_modein … a). |
---|
791 | |
---|
792 | definition get_arg_8: ∀M: Type[0]. PreStatus M → bool → [[ direct ; indirect ; registr ; |
---|
793 | acc_a ; acc_b ; data ; acc_dptr ; |
---|
794 | acc_pc ; ext_indirect ; |
---|
795 | ext_indirect_dptr ]] → Byte ≝ |
---|
796 | λm, s, l, a. |
---|
797 | match a return λx. bool_to_Prop (is_in ? [[ direct ; indirect ; registr ; |
---|
798 | acc_a ; acc_b ; data ; acc_dptr ; |
---|
799 | acc_pc ; ext_indirect ; |
---|
800 | ext_indirect_dptr ]] x) → ? with |
---|
801 | [ ACC_A ⇒ λacc_a: True. get_8051_sfr ? s SFR_ACC_A |
---|
802 | | ACC_B ⇒ λacc_b: True. get_8051_sfr ? s SFR_ACC_B |
---|
803 | | DATA d ⇒ λdata: True. d |
---|
804 | | REGISTER r ⇒ λregister: True. get_register ? s r |
---|
805 | | EXT_INDIRECT_DPTR ⇒ |
---|
806 | λext_indirect_dptr: True. |
---|
807 | let address ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in |
---|
808 | lookup … 16 address (external_ram ? s) (zero 8) |
---|
809 | | EXT_INDIRECT e ⇒ |
---|
810 | λext_indirect: True. |
---|
811 | let address ≝ get_register ? s [[ false; false; e ]] in |
---|
812 | let padded_address ≝ pad 8 8 address in |
---|
813 | lookup … 16 padded_address (external_ram ? s) (zero 8) |
---|
814 | | ACC_DPTR ⇒ |
---|
815 | λacc_dptr: True. |
---|
816 | let dptr ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in |
---|
817 | let padded_acc ≝ pad 8 8 (get_8051_sfr ? s SFR_ACC_A) in |
---|
818 | let 〈 carry, address 〉 ≝ half_add 16 dptr padded_acc in |
---|
819 | lookup … 16 address (external_ram ? s) (zero 8) |
---|
820 | | ACC_PC ⇒ |
---|
821 | λacc_pc: True. |
---|
822 | let padded_acc ≝ pad 8 8 (get_8051_sfr ? s SFR_ACC_A) in |
---|
823 | let 〈 carry, address 〉 ≝ half_add 16 (program_counter ? s) padded_acc in |
---|
824 | lookup … 16 address (external_ram ? s) (zero 8) |
---|
825 | | DIRECT d ⇒ |
---|
826 | λdirect: True. |
---|
827 | let 〈 nu, nl 〉 ≝ split … 4 4 d in |
---|
828 | let bit_1 ≝ get_index_v … nu 1 ? in |
---|
829 | match bit_1 with |
---|
830 | [ true ⇒ |
---|
831 | let 〈 bit_one, three_bits 〉 ≝ split ? 1 3 nu in |
---|
832 | let address ≝ three_bits @@ nl in |
---|
833 | lookup ? 7 address (low_internal_ram ? s) (zero 8) |
---|
834 | | false ⇒ get_bit_addressable_sfr ? s 8 d l |
---|
835 | ] |
---|
836 | | INDIRECT i ⇒ |
---|
837 | λindirect: True. |
---|
838 | let 〈 nu, nl 〉 ≝ split ? 4 4 (get_register ? s [[ false; false; i]]) in |
---|
839 | let 〈 bit_one_v, three_bits 〉 ≝ split ? 1 3 nu in |
---|
840 | let bit_1 ≝ get_index_v … bit_one_v O ? in |
---|
841 | match bit_1 with |
---|
842 | [ true ⇒ |
---|
843 | lookup ? 7 (three_bits @@ nl) (low_internal_ram ? s) (zero 8) |
---|
844 | | false ⇒ |
---|
845 | lookup ? 7 (three_bits @@ nl) (high_internal_ram ? s) (zero 8) |
---|
846 | ] |
---|
847 | | _ ⇒ λother. |
---|
848 | match other in False with [ ] |
---|
849 | ] (subaddressing_modein … a). |
---|
850 | [1,2: |
---|
851 | normalize |
---|
852 | repeat (@ le_S_S) |
---|
853 | @ le_O_n |
---|
854 | ] |
---|
855 | qed. |
---|
856 | |
---|
857 | definition set_arg_8: ∀M: Type[0]. PreStatus M → [[ direct ; indirect ; registr ; |
---|
858 | acc_a ; acc_b ; ext_indirect ; |
---|
859 | ext_indirect_dptr ]] → Byte → PreStatus M ≝ |
---|
860 | λm, s, a, v. |
---|
861 | match a return λx. bool_to_Prop (is_in ? [[ direct ; indirect ; registr ; |
---|
862 | acc_a ; acc_b ; ext_indirect ; |
---|
863 | ext_indirect_dptr ]] x) → ? with |
---|
864 | [ DIRECT d ⇒ |
---|
865 | λdirect: True. |
---|
866 | let 〈 nu, nl 〉 ≝ split … 4 4 d in |
---|
867 | let bit_1 ≝ get_index_v … nu 1 ? in |
---|
868 | let 〈 ignore, three_bits 〉 ≝ split ? 1 3 nu in |
---|
869 | match bit_1 with |
---|
870 | [ true ⇒ set_bit_addressable_sfr ? s d v |
---|
871 | | false ⇒ |
---|
872 | let memory ≝ insert ? 7 (three_bits @@ nl) v (low_internal_ram ? s) in |
---|
873 | set_low_internal_ram ? s memory |
---|
874 | ] |
---|
875 | | INDIRECT i ⇒ |
---|
876 | λindirect: True. |
---|
877 | let register ≝ get_register ? s [[ false; false; i ]] in |
---|
878 | let 〈nu, nl〉 ≝ split ? 4 4 register in |
---|
879 | let bit_1 ≝ get_index_v … nu 1 ? in |
---|
880 | let 〈ignore, three_bits〉 ≝ split ? 1 3 nu in |
---|
881 | match bit_1 with |
---|
882 | [ true ⇒ |
---|
883 | let memory ≝ insert … (three_bits @@ nl) v (low_internal_ram ? s) in |
---|
884 | set_low_internal_ram ? s memory |
---|
885 | | false ⇒ |
---|
886 | let memory ≝ insert … (three_bits @@ nl) v (high_internal_ram ? s) in |
---|
887 | set_high_internal_ram ? s memory |
---|
888 | ] |
---|
889 | | REGISTER r ⇒ λregister: True. set_register ? s r v |
---|
890 | | ACC_A ⇒ λacc_a: True. set_8051_sfr ? s SFR_ACC_A v |
---|
891 | | ACC_B ⇒ λacc_b: True. set_8051_sfr ? s SFR_ACC_B v |
---|
892 | | EXT_INDIRECT e ⇒ |
---|
893 | λext_indirect: True. |
---|
894 | let address ≝ get_register ? s [[ false; false; e ]] in |
---|
895 | let padded_address ≝ pad 8 8 address in |
---|
896 | let memory ≝ insert ? 16 padded_address v (external_ram ? s) in |
---|
897 | set_external_ram ? s memory |
---|
898 | | EXT_INDIRECT_DPTR ⇒ |
---|
899 | λext_indirect_dptr: True. |
---|
900 | let address ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in |
---|
901 | let memory ≝ insert ? 16 address v (external_ram ? s) in |
---|
902 | set_external_ram ? s memory |
---|
903 | | _ ⇒ |
---|
904 | λother: False. |
---|
905 | match other in False with [ ] |
---|
906 | ] (subaddressing_modein … a). |
---|
907 | [1,2,3,4: |
---|
908 | normalize |
---|
909 | repeat (@ le_S_S) |
---|
910 | @ le_O_n |
---|
911 | ] |
---|
912 | qed. |
---|
913 | |
---|
914 | theorem modulus_less_than: |
---|
915 | ∀m,n: nat. (m mod (S n)) < S n. |
---|
916 | #n #m |
---|
917 | normalize |
---|
918 | @ le_S_S |
---|
919 | lapply (le_n n) |
---|
920 | generalize in ⊢ (?%? → ?(??%?)?) |
---|
921 | elim n in ⊢ (∀_:?. ??% → ?(?%??)?) |
---|
922 | [ normalize |
---|
923 | #n |
---|
924 | @ (less_than_or_equal_b_elim n m) |
---|
925 | normalize |
---|
926 | [ // |
---|
927 | | #H #K |
---|
928 | inversion K |
---|
929 | [ # H1 |
---|
930 | < H1 |
---|
931 | // |
---|
932 | | #x #H1 #H2 #H3 |
---|
933 | destruct |
---|
934 | ] |
---|
935 | ] |
---|
936 | | normalize |
---|
937 | # y # H1 # n # H2 |
---|
938 | @ (less_than_or_equal_b_elim n m) |
---|
939 | normalize |
---|
940 | [ // |
---|
941 | | # K |
---|
942 | @ H1 |
---|
943 | cut (n ≤ S y → n - S m ≤ y) |
---|
944 | /2/ |
---|
945 | cases n |
---|
946 | normalize |
---|
947 | // |
---|
948 | # x # K1 |
---|
949 | lapply (le_S_S_to_le … K1) |
---|
950 | generalize in match m |
---|
951 | elim x |
---|
952 | normalize |
---|
953 | // |
---|
954 | # w1 # H # m |
---|
955 | cases m |
---|
956 | normalize |
---|
957 | // |
---|
958 | # q # K2 |
---|
959 | apply H |
---|
960 | /3/ |
---|
961 | ] |
---|
962 | ] |
---|
963 | qed. |
---|
964 | |
---|
965 | definition get_arg_1: ∀M: Type[0]. PreStatus M → [[ bit_addr ; n_bit_addr ; carry ]] → |
---|
966 | bool → bool ≝ |
---|
967 | λm, s, a, l. |
---|
968 | match a return λx. bool_to_Prop (is_in ? [[ bit_addr ; |
---|
969 | n_bit_addr ; |
---|
970 | carry ]] x) → ? with |
---|
971 | [ BIT_ADDR b ⇒ |
---|
972 | λbit_addr: True. |
---|
973 | let 〈 nu, nl 〉 ≝ split … 4 4 b in |
---|
974 | let bit_1 ≝ get_index_v … nu 1 ? in |
---|
975 | let 〈 bit_one_v, three_bits 〉 ≝ split ? 1 3 nu in |
---|
976 | match bit_1 with |
---|
977 | [ true ⇒ |
---|
978 | let address ≝ nat_of_bitvector … (three_bits @@ nl) in |
---|
979 | let d ≝ address ÷ 8 in |
---|
980 | let m ≝ address mod 8 in |
---|
981 | let trans ≝ bitvector_of_nat 8 ((d * 8) + 128) in |
---|
982 | let sfr ≝ get_bit_addressable_sfr ? s ? trans l in |
---|
983 | get_index_v … sfr m ? |
---|
984 | | false ⇒ |
---|
985 | let address ≝ nat_of_bitvector … (three_bits @@ nl) in |
---|
986 | let address' ≝ bitvector_of_nat 7 ((address ÷ 8) + 32) in |
---|
987 | let t ≝ lookup … 7 address' (low_internal_ram ? s) (zero 8) in |
---|
988 | get_index_v … t (modulus address 8) ? |
---|
989 | ] |
---|
990 | | N_BIT_ADDR n ⇒ |
---|
991 | λn_bit_addr: True. |
---|
992 | let 〈 nu, nl 〉 ≝ split … 4 4 n in |
---|
993 | let bit_1 ≝ get_index_v … nu 1 ? in |
---|
994 | let 〈 bit_one_v, three_bits 〉 ≝ split ? 1 3 nu in |
---|
995 | match bit_1 with |
---|
996 | [ true ⇒ |
---|
997 | let address ≝ nat_of_bitvector … (three_bits @@ nl) in |
---|
998 | let d ≝ address ÷ 8 in |
---|
999 | let m ≝ address mod 8 in |
---|
1000 | let trans ≝ bitvector_of_nat 8 ((d * 8) + 128) in |
---|
1001 | let sfr ≝ get_bit_addressable_sfr ? s ? trans l in |
---|
1002 | ¬(get_index_v … sfr m ?) |
---|
1003 | | false ⇒ |
---|
1004 | let address ≝ nat_of_bitvector … (three_bits @@ nl) in |
---|
1005 | let address' ≝ bitvector_of_nat 7 ((address ÷ 8) + 32) in |
---|
1006 | let trans ≝ lookup … 7 address' (low_internal_ram ? s) (zero 8) in |
---|
1007 | ¬(get_index_v … trans (modulus address 8) ?) |
---|
1008 | ] |
---|
1009 | | CARRY ⇒ λcarry: True. get_cy_flag ? s |
---|
1010 | | _ ⇒ λother. |
---|
1011 | match other in False with [ ] |
---|
1012 | ] (subaddressing_modein … a). |
---|
1013 | [3,6: |
---|
1014 | normalize |
---|
1015 | repeat (@ le_S_S) |
---|
1016 | @ le_O_n |
---|
1017 | |1,2,4,5: |
---|
1018 | apply modulus_less_than |
---|
1019 | ] |
---|
1020 | qed. |
---|
1021 | |
---|
1022 | definition set_arg_1: ∀M: Type[0]. PreStatus M → [[ bit_addr ; carry ]] → |
---|
1023 | Bit → PreStatus M ≝ |
---|
1024 | λm, s, a, v. |
---|
1025 | match a return λx. bool_to_Prop (is_in ? [[ bit_addr ; carry ]] x) → ? with |
---|
1026 | [ BIT_ADDR b ⇒ λbit_addr: True. |
---|
1027 | let 〈 nu, nl 〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in |
---|
1028 | let bit_1 ≝ get_index_v … nu 1 ? in |
---|
1029 | let 〈 ignore, three_bits 〉 ≝ split ? 1 3 nu in |
---|
1030 | match bit_1 with |
---|
1031 | [ true ⇒ |
---|
1032 | let address ≝ nat_of_bitvector … (three_bits @@ nl) in |
---|
1033 | let d ≝ address ÷ 8 in |
---|
1034 | let m ≝ address mod 8 in |
---|
1035 | let t ≝ bitvector_of_nat 8 ((d * 8) + 128) in |
---|
1036 | let sfr ≝ get_bit_addressable_sfr ? s ? t true in |
---|
1037 | let new_sfr ≝ set_index … sfr m v ? in |
---|
1038 | set_bit_addressable_sfr ? s new_sfr t |
---|
1039 | | false ⇒ |
---|
1040 | let address ≝ nat_of_bitvector … (three_bits @@ nl) in |
---|
1041 | let address' ≝ bitvector_of_nat 7 ((address ÷ 8) + 32) in |
---|
1042 | let t ≝ lookup … 7 address' (low_internal_ram ? s) (zero 8) in |
---|
1043 | let n_bit ≝ set_index … t (modulus address 8) v ? in |
---|
1044 | let memory ≝ insert ? 7 address' n_bit (low_internal_ram ? s) in |
---|
1045 | set_low_internal_ram ? s memory |
---|
1046 | ] |
---|
1047 | | CARRY ⇒ |
---|
1048 | λcarry: True. |
---|
1049 | let 〈 nu, nl 〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in |
---|
1050 | let bit_1 ≝ get_index_v… nu 1 ? in |
---|
1051 | let bit_2 ≝ get_index_v… nu 2 ? in |
---|
1052 | let bit_3 ≝ get_index_v… nu 3 ? in |
---|
1053 | let new_psw ≝ [[ v; bit_1 ; bit_2; bit_3 ]] @@ nl in |
---|
1054 | set_8051_sfr ? s SFR_PSW new_psw |
---|
1055 | | _ ⇒ |
---|
1056 | λother: False. |
---|
1057 | match other in False with |
---|
1058 | [ ] |
---|
1059 | ] (subaddressing_modein … a). |
---|
1060 | [1,2,3,6: |
---|
1061 | normalize |
---|
1062 | repeat (@ le_S_S) |
---|
1063 | @ le_O_n |
---|
1064 | |4,5: |
---|
1065 | @ modulus_less_than |
---|
1066 | ] |
---|
1067 | qed. |
---|
1068 | |
---|
1069 | definition load_code_memory ≝ |
---|
1070 | fold_left_i … ( |
---|
1071 | λi, mem, v. |
---|
1072 | insert … (bitvector_of_nat … i) v mem) (Stub Byte 16). |
---|
1073 | |
---|
1074 | definition load ≝ |
---|
1075 | λl. |
---|
1076 | λstatus. |
---|
1077 | set_code_memory (BitVectorTrie Word 16) ? status (load_code_memory l). |
---|
1078 | |
---|
1079 | definition fetch_pseudo_instruction: list labelled_instruction → Word → (pseudo_instruction × Word) ≝ |
---|
1080 | λcode_mem. |
---|
1081 | λpc: Word. |
---|
1082 | let 〈lbl, instr〉 ≝ nth (nat_of_bitvector ? pc) … code_mem ? in |
---|
1083 | let 〈flags, new_pc〉 ≝ half_add ? pc (bitvector_of_nat ? 1) in |
---|
1084 | 〈instr, new_pc〉. |
---|
1085 | cases not_implemented. |
---|
1086 | qed. |
---|