source: src/ASM/Status.ma @ 3066

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