source: src/ASM/Status.ma @ 1581

Last change on this file since 1581 was 1581, checked in by mulligan, 9 years ago

Dangling de Bruijn pointer when trying to propagate russell to set_arg_1

File size: 45.4 KB
Line 
1(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *)
2(* Interpret.ma: Operational semantics for the 8051/8052 processor.           *)
3(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *)
4
5include "ASM/ASM.ma".
6include "ASM/Arithmetic.ma".
7include "ASM/BitVectorTrie.ma".
8include "ASM/JMCoercions.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]): Type[0] ≝
89{
90  code_memory: M;
91  low_internal_ram: BitVectorTrie Byte 7;
92  high_internal_ram: BitVectorTrie Byte 7;
93  external_ram: BitVectorTrie Byte 16;
94 
95  program_counter: Word;
96 
97  special_function_registers_8051: Vector Byte 19;
98  special_function_registers_8052: Vector Byte 5;
99 
100  p1_latch: Byte;
101  p3_latch: Byte;
102 
103  clock: Time
104}.
105
106definition Status ≝ PreStatus (BitVectorTrie Byte 16).
107definition PseudoStatus ≝ PreStatus (pseudo_assembly_program).
108
109lemma sfr8051_index_19:
110  ∀i: SFR8051.
111    sfr_8051_index i < 19.
112 # i
113 cases i
114 normalize
115 repeat (@ le_S_S)
116 @ le_O_n
117qed.
118   
119lemma sfr8052_index_5:
120  ∀i: SFR8052.
121    sfr_8052_index i < 5.
122 # i
123 cases i
124 normalize
125 repeat (@ le_S_S)
126 @ le_O_n
127qed.
128   
129definition set_clock ≝
130  λM: Type[0].
131  λs: PreStatus M.
132  λt: Time.
133    let old_code_memory ≝ code_memory ? s in
134    let old_low_internal_ram ≝ low_internal_ram ? s in
135    let old_high_internal_ram ≝ high_internal_ram ? s in
136    let old_external_ram ≝ external_ram ? s in
137    let old_program_counter ≝ program_counter ? s in
138    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
139    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
140    let old_p1_latch ≝ p1_latch ? s in   
141    let old_p3_latch ≝ p3_latch ? s in
142      mk_PreStatus M old_code_memory
143                old_low_internal_ram
144                old_high_internal_ram
145                old_external_ram
146                old_program_counter
147                old_special_function_registers_8051
148                old_special_function_registers_8052
149                old_p1_latch
150                old_p3_latch
151                t.
152   
153definition set_p1_latch ≝
154  λM: Type[0].
155  λs: PreStatus M.
156  λb: Byte.
157    let old_code_memory ≝ code_memory ? s in
158    let old_low_internal_ram ≝ low_internal_ram ? s in
159    let old_high_internal_ram ≝ high_internal_ram ? s in
160    let old_external_ram ≝ external_ram ? s in
161    let old_program_counter ≝ program_counter ? s in
162    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
163    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
164    let old_p3_latch ≝ p3_latch ? s in
165    let old_clock ≝ clock ? s in
166      mk_PreStatus M old_code_memory
167                old_low_internal_ram
168                old_high_internal_ram
169                old_external_ram
170                old_program_counter
171                old_special_function_registers_8051
172                old_special_function_registers_8052
173                b
174                old_p3_latch
175                old_clock.
176
177definition set_p3_latch ≝
178  λM: Type[0].
179  λs: PreStatus M.
180  λb: Byte.
181    let old_code_memory ≝ code_memory ? s in
182    let old_low_internal_ram ≝ low_internal_ram ? s in
183    let old_high_internal_ram ≝ high_internal_ram ? s in
184    let old_external_ram ≝ external_ram ? s in
185    let old_program_counter ≝ program_counter ? s in
186    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
187    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
188    let old_p1_latch ≝ p1_latch ? s in
189    let old_clock ≝ clock ? s in
190      mk_PreStatus M old_code_memory
191                old_low_internal_ram
192                old_high_internal_ram
193                old_external_ram
194                old_program_counter
195                old_special_function_registers_8051
196                old_special_function_registers_8052
197                old_p1_latch
198                b
199                old_clock.
200
201definition get_8051_sfr ≝
202  λM: Type[0].
203  λs: PreStatus M.
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  λs: PreStatus M.
214  λi: SFR8052.
215    let sfr ≝ special_function_registers_8052 ? s in
216    let index ≝ sfr_8052_index i in
217      get_index_v … sfr index ?.
218    @ sfr8052_index_5
219qed.
220
221definition set_8051_sfr ≝
222  λM: Type[0].
223  λs: PreStatus M.
224  λi: SFR8051.
225  λb: Byte.
226    let index ≝ sfr_8051_index i in
227    let old_code_memory ≝ code_memory ? s in
228    let old_low_internal_ram ≝ low_internal_ram ? s in
229    let old_high_internal_ram ≝ high_internal_ram ? s in
230    let old_external_ram ≝ external_ram ? s in
231    let old_program_counter ≝ program_counter ? s in
232    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
233    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
234    let new_special_function_registers_8051 ≝
235      set_index Byte 19 old_special_function_registers_8051 index b ? in
236    let old_p1_latch ≝ p1_latch ? s in
237    let old_p3_latch ≝ p3_latch ? s in
238    let old_clock ≝ clock ? s in
239      mk_PreStatus M old_code_memory
240                old_low_internal_ram
241                old_high_internal_ram
242                old_external_ram
243                old_program_counter
244                new_special_function_registers_8051
245                old_special_function_registers_8052
246                old_p1_latch
247                old_p3_latch
248                old_clock.
249    @ (sfr8051_index_19 i)
250qed.
251
252definition set_8052_sfr ≝
253  λM: Type[0].
254  λs: PreStatus M.
255  λi: SFR8052.
256  λb: Byte.
257    let index ≝ sfr_8052_index i in
258    let old_code_memory ≝ code_memory ? s in
259    let old_low_internal_ram ≝ low_internal_ram ? s in
260    let old_high_internal_ram ≝ high_internal_ram ? s in
261    let old_external_ram ≝ external_ram ? s in
262    let old_program_counter ≝ program_counter ? s in
263    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
264    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
265    let new_special_function_registers_8052 ≝
266      set_index Byte 5 old_special_function_registers_8052 index b ? in
267    let old_p1_latch ≝ p1_latch ? s in
268    let old_p3_latch ≝ p3_latch ? s in
269    let old_clock ≝ clock ? s in
270      mk_PreStatus M old_code_memory
271                old_low_internal_ram
272                old_high_internal_ram
273                old_external_ram
274                old_program_counter
275                old_special_function_registers_8051
276                new_special_function_registers_8052
277                old_p1_latch
278                old_p3_latch
279                old_clock.
280    @ (sfr8052_index_5 i)
281qed.
282
283definition set_program_counter ≝
284  λM: Type[0].
285  λs: PreStatus M.
286  λw: Word.
287    let old_code_memory ≝ code_memory ? s in
288    let old_low_internal_ram ≝ low_internal_ram ? s in
289    let old_high_internal_ram ≝ high_internal_ram ? s in
290    let old_external_ram ≝ external_ram ? s in
291    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
292    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
293    let old_p1_latch ≝ p1_latch ? s in
294    let old_p3_latch ≝ p3_latch ? s in
295    let old_clock ≝ clock ? s in
296      mk_PreStatus M old_code_memory
297                old_low_internal_ram
298                old_high_internal_ram
299                old_external_ram
300                w
301                old_special_function_registers_8051
302                old_special_function_registers_8052
303                old_p1_latch
304                old_p3_latch
305                old_clock.
306               
307definition set_code_memory ≝
308  λM,M': Type[0].
309  λs: PreStatus M.
310  λr: M'.
311    let old_low_internal_ram ≝ low_internal_ram ? s in
312    let old_high_internal_ram ≝ high_internal_ram ? s in
313    let old_external_ram ≝ external_ram ? s in
314    let old_program_counter ≝ program_counter ? s in
315    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
316    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
317    let old_p1_latch ≝ p1_latch ? s in
318    let old_p3_latch ≝ p3_latch ? s in
319    let old_clock ≝ clock ? s in
320      mk_PreStatus M' r
321                old_low_internal_ram
322                old_high_internal_ram
323                old_external_ram
324                old_program_counter
325                old_special_function_registers_8051
326                old_special_function_registers_8052
327                old_p1_latch
328                old_p3_latch
329                old_clock.
330               
331definition set_low_internal_ram ≝
332  λM: Type[0].
333  λs: PreStatus M.
334  λr: BitVectorTrie Byte 7.
335    let old_code_memory ≝ code_memory ? s in
336    let old_high_internal_ram ≝ high_internal_ram ? s in
337    let old_external_ram ≝ external_ram ? s in
338    let old_program_counter ≝ program_counter ? s in
339    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
340    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
341    let old_p1_latch ≝ p1_latch ? s in
342    let old_p3_latch ≝ p3_latch ? s in
343    let old_clock ≝ clock ? s in
344      mk_PreStatus M old_code_memory
345                r
346                old_high_internal_ram
347                old_external_ram
348                old_program_counter
349                old_special_function_registers_8051
350                old_special_function_registers_8052
351                old_p1_latch
352                old_p3_latch
353                old_clock.
354               
355definition set_high_internal_ram ≝
356  λM: Type[0].
357  λs: PreStatus M.
358  λr: BitVectorTrie Byte 7.
359    let old_code_memory ≝ code_memory ? s in
360    let old_low_internal_ram ≝ low_internal_ram ? s in
361    let old_high_internal_ram ≝ high_internal_ram ? s in
362    let old_external_ram ≝ external_ram ? s in
363    let old_program_counter ≝ program_counter ? s in
364    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
365    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
366    let old_p1_latch ≝ p1_latch ? s in
367    let old_p3_latch ≝ p3_latch ? s in
368    let old_clock ≝ clock ? s in
369      mk_PreStatus M old_code_memory
370                old_low_internal_ram
371                r
372                old_external_ram
373                old_program_counter
374                old_special_function_registers_8051
375                old_special_function_registers_8052
376                old_p1_latch
377                old_p3_latch
378                old_clock.
379               
380definition set_external_ram ≝
381  λM: Type[0].
382  λs: PreStatus M.
383  λr: BitVectorTrie Byte 16.
384    let old_code_memory ≝ code_memory ? s in
385    let old_low_internal_ram ≝ low_internal_ram ? s in
386    let old_high_internal_ram ≝ high_internal_ram ? s in
387    let old_program_counter ≝ program_counter ? s in
388    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
389    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
390    let old_p1_latch ≝ p1_latch ? s in
391    let old_p3_latch ≝ p3_latch ? s in
392    let old_clock ≝ clock ? s in
393      mk_PreStatus M old_code_memory
394                old_low_internal_ram
395                old_high_internal_ram
396                r
397                old_program_counter
398                old_special_function_registers_8051
399                old_special_function_registers_8052
400                old_p1_latch
401                old_p3_latch
402                old_clock.
403               
404definition get_cy_flag ≝
405  λM: Type[0].
406  λs: PreStatus M.
407    let sfr ≝ special_function_registers_8051 ? s in
408    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
409      get_index_v bool 8 psw O ?.
410    normalize
411    @ (le_S_S ? ?)
412    [ @ le_O_n
413    | repeat (@ (le_S_S));
414      //
415    ]
416qed.
417
418definition get_ac_flag ≝
419  λM: Type[0].
420  λs: PreStatus M.
421    let sfr ≝ special_function_registers_8051 ? s in
422    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
423      get_index_v bool 8 psw 1 ?.
424    normalize
425    repeat (@ (le_S_S ? ?))
426    @ le_O_n
427qed.
428
429definition get_fo_flag ≝
430  λM: Type[0].
431  λs: PreStatus M.
432    let sfr ≝ special_function_registers_8051 ? s in
433    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
434      get_index_v bool 8 psw 2 ?.
435    normalize
436    repeat (@ (le_S_S ? ?))
437    @ le_O_n
438qed.
439
440definition get_rs1_flag ≝
441  λM: Type[0].
442  λs: PreStatus M.
443    let sfr ≝ special_function_registers_8051 ? s in
444    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
445      get_index_v bool 8 psw 3 ?.
446    normalize
447    repeat (@ (le_S_S ? ?))
448    @ le_O_n
449qed.
450
451definition get_rs0_flag ≝
452  λM: Type[0].
453  λs: PreStatus M.
454    let sfr ≝ special_function_registers_8051 ? s in
455    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
456      get_index_v bool 8 psw 4 ?.
457    normalize
458    repeat (@ (le_S_S ? ?))
459    @ le_O_n
460qed.
461
462definition get_ov_flag ≝
463  λM: Type[0].
464  λs: PreStatus M.
465    let sfr ≝ special_function_registers_8051 ? s in
466    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
467      get_index_v bool 8 psw 5 ?.
468    normalize
469    repeat (@ (le_S_S ? ?))
470    @ le_O_n
471qed.
472
473definition get_ud_flag ≝
474  λM: Type[0].
475  λs: PreStatus M.
476    let sfr ≝ special_function_registers_8051 ? s in
477    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
478      get_index_v bool 8 psw 6 ?.
479    normalize
480    repeat (@ (le_S_S ? ?))
481    @ le_O_n
482qed.
483
484definition get_p_flag ≝
485  λM: Type[0].
486  λs: PreStatus M.
487    let sfr ≝ special_function_registers_8051 ? s in
488    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
489      get_index_v bool 8 psw 7 ?.
490    normalize
491    repeat (@ (le_S_S ? ?))
492    @ le_O_n
493qed.
494
495definition set_flags ≝
496  λM: Type[0].
497  λs: PreStatus M.
498  λcy: Bit.
499  λac: option Bit.
500  λov: Bit.
501    let old_cy ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) O ? in
502    let old_ac ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 1 ? in
503    let old_fo ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 2 ? in
504    let old_rs1 ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 3 ? in
505    let old_rs0 ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 4 ? in
506    let old_ov ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 5 ? in
507    let old_ud ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 6 ? in
508    let old_p ≝ get_index_v… (get_8051_sfr ? s SFR_PSW) 7 ? in
509    let new_ac ≝ match ac with [ None ⇒ old_ac | Some j ⇒ j ] in
510    let new_psw ≝ [[ old_cy ; new_ac ; old_fo ; old_rs1 ;
511                     old_rs0 ; old_ov ; old_ud ; old_p ]] in
512      set_8051_sfr ? s SFR_PSW new_psw.
513    [1,2,3,4,5,6,7,8:
514       normalize
515       repeat (@ le_S_S)
516       @ le_O_n
517    ]
518qed.
519
520definition initialise_status ≝
521  λM: Type[0].
522  λcode_mem: M.
523  let status ≝ mk_PreStatus M code_mem                    (* Code mem. *)
524                         (Stub Byte 7)                      (* Low mem.  *)
525                         (Stub Byte 7)                      (* High mem. *)
526                         (Stub Byte 16)                    (* Ext mem.  *)
527                         (zero 16)                         (* PC.       *)
528                         (replicate Byte 19 (zero 8)) (* 8051 SFR. *)
529                         (replicate Byte 5 (zero 8))     (* 8052 SFR. *)
530                         (zero 8)                           (* P1 latch. *)
531                         (zero 8)                           (* P3 latch. *)
532                         O                                      (* Clock.    *)
533  in
534    set_8051_sfr ? status SFR_SP (bitvector_of_nat 8 7).
535 
536definition get_bit_addressable_sfr ≝
537  λM: Type[0].
538  λs: PreStatus M.
539  λn: nat.
540  λb: BitVector n.
541  λl: bool.
542    let address ≝ nat_of_bitvector … b in
543      if (eqb address 128) then
544        ?
545      else if (eqb address 144) then
546        if l then
547          (p1_latch ? s)
548        else
549          (get_8051_sfr ? s SFR_P1)
550      else if (eqb address 160) then
551        ?
552      else if (eqb address 176) then
553        if l then
554          (p3_latch ? s)
555        else
556          (get_8051_sfr ? s SFR_P3)
557      else if (eqb address 153) then
558        get_8051_sfr ? s SFR_SBUF
559      else if (eqb address 138) then
560        get_8051_sfr ? s SFR_TL0
561      else if (eqb address 139) then
562        get_8051_sfr ? s SFR_TL1
563      else if (eqb address 140) then
564        get_8051_sfr ? s SFR_TH0
565      else if (eqb address 141) then
566        get_8051_sfr ? s SFR_TH1
567      else if (eqb address 200) then
568        get_8052_sfr ? s SFR_T2CON
569      else if (eqb address 202) then
570        get_8052_sfr ? s SFR_RCAP2L
571      else if (eqb address 203) then
572        get_8052_sfr ? s SFR_RCAP2H
573      else if (eqb address 204) then
574        get_8052_sfr ? s SFR_TL2
575      else if (eqb address 205) then
576        get_8052_sfr ? s SFR_TH2
577      else if (eqb address 135) then
578        get_8051_sfr ? s SFR_PCON
579      else if (eqb address 136) then
580        get_8051_sfr ? s SFR_TCON
581      else if (eqb address 137) then
582        get_8051_sfr ? s SFR_TMOD
583      else if (eqb address 152) then
584        get_8051_sfr ? s SFR_SCON
585      else if (eqb address 168) then
586        get_8051_sfr ? s SFR_IE
587      else if (eqb address 184) then
588        get_8051_sfr ? s SFR_IP
589      else if (eqb address 129) then
590        get_8051_sfr ? s SFR_SP
591      else if (eqb address 130) then
592        get_8051_sfr ? s SFR_DPL
593      else if (eqb address 131) then
594        get_8051_sfr ? s SFR_DPH
595      else if (eqb address 208) then
596        get_8051_sfr ? s SFR_PSW
597      else if (eqb address 224) then
598        get_8051_sfr ? s SFR_ACC_A
599      else if (eqb address 240) then
600        get_8051_sfr ? s SFR_ACC_B
601      else
602        ?.
603      cases not_implemented
604qed.
605
606definition set_bit_addressable_sfr ≝
607  λM: Type[0].
608  λs: PreStatus M.
609  λb: Byte.
610  λv: Byte.
611    let address ≝ nat_of_bitvector … b in
612      if (eqb address 128) then
613        ?
614      else if (eqb address 144) then
615        let status_1 ≝ set_8051_sfr ? s SFR_P1 v in
616        let status_2 ≝ set_p1_latch ? s v in
617          status_2
618      else if (eqb address 160) then
619        ?
620      else if (eqb address 176) then
621        let status_1 ≝ set_8051_sfr ? s SFR_P3 v in
622        let status_2 ≝ set_p3_latch ? s v in
623          status_2
624      else if (eqb address 153) then
625        set_8051_sfr ? s SFR_SBUF v
626      else if (eqb address 138) then
627        set_8051_sfr ? s SFR_TL0 v
628      else if (eqb address 139) then
629        set_8051_sfr ? s SFR_TL1 v
630      else if (eqb address 140) then
631        set_8051_sfr ? s SFR_TH0 v
632      else if (eqb address 141) then
633        set_8051_sfr ? s SFR_TH1 v
634      else if (eqb address 200) then
635        set_8052_sfr ? s SFR_T2CON v
636      else if (eqb address 202) then
637        set_8052_sfr ? s SFR_RCAP2L v
638      else if (eqb address 203) then
639        set_8052_sfr ? s SFR_RCAP2H v
640      else if (eqb address 204) then
641        set_8052_sfr ? s SFR_TL2 v
642      else if (eqb address 205) then
643        set_8052_sfr ? s SFR_TH2 v
644      else if (eqb address 135) then
645        set_8051_sfr ? s SFR_PCON v
646      else if (eqb address 136) then
647        set_8051_sfr ? s SFR_TCON v
648      else if (eqb address 137) then
649        set_8051_sfr ? s SFR_TMOD v
650      else if (eqb address 152) then
651        set_8051_sfr ? s SFR_SCON v
652      else if (eqb address 168) then
653        set_8051_sfr ? s SFR_IE v
654      else if (eqb address 184) then
655        set_8051_sfr ? s SFR_IP v
656      else if (eqb address 129) then
657        set_8051_sfr ? s SFR_SP v
658      else if (eqb address 130) then
659        set_8051_sfr ? s SFR_DPL v
660      else if (eqb address 131) then
661        set_8051_sfr ? s SFR_DPH v
662      else if (eqb address 208) then
663        set_8051_sfr ? s SFR_PSW v
664      else if (eqb address 224) then
665        set_8051_sfr ? s SFR_ACC_A v
666      else if (eqb address 240) then
667        set_8051_sfr ? s SFR_ACC_B v
668      else
669        ?.
670      cases not_implemented
671qed.
672
673definition bit_address_of_register ≝
674  λM: Type[0].
675  λs: PreStatus M.
676  λr: BitVector 3.
677    let b ≝ get_index_v … r O ? in
678    let c ≝ get_index_v … r 1 ? in
679    let d ≝ get_index_v … r 2 ? in
680    let 〈 un, ln 〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in
681    let 〈 r1, r0 〉 ≝ 〈 get_index_v … 4 un 2 ?, get_index_v … 4 un 3 ? 〉 in
682    let offset ≝
683      if ¬r1 ∧ ¬r0 then
684        O
685      else if ¬r1 ∧ r0 then
686        8
687      else if r1 ∧ r0 then
688        24
689      else
690        16
691    in
692      bitvector_of_nat 7 (offset + (nat_of_bitvector ? [[ false ; b ; c ; d ]])).
693  [1,2,3,4,5:
694    normalize
695    repeat (@ le_S_S)
696    @ le_O_n;
697  ]
698qed.
699
700definition get_register ≝
701  λM: Type[0].
702  λs: PreStatus M.
703  λr: BitVector 3.
704    let address ≝ bit_address_of_register ? s r in
705      lookup ?? address (low_internal_ram ? s) (zero 8).
706     
707definition set_register ≝
708  λM: Type[0].
709  λs: PreStatus M.
710  λr: BitVector 3.
711  λv: Byte.
712    let address ≝ bit_address_of_register ? s r in
713    let old_low_internal_ram ≝ low_internal_ram ? s in
714    let new_low_internal_ram ≝ insert … address v old_low_internal_ram in
715      set_low_internal_ram ? s new_low_internal_ram.
716     
717definition read_at_stack_pointer ≝
718  λM: Type[0].
719  λs: PreStatus M.
720    let 〈 nu, nl 〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_SP) in
721    let m ≝ get_index_v … nu O ? in
722    let r1 ≝ get_index_v … nu 1 ? in
723    let r2 ≝ get_index_v … nu 2 ? in
724    let r3 ≝ get_index_v … nu 3 ? in
725    let address ≝ [[ r1 ; r2 ; r3 ]] @@ nl in
726    let memory ≝
727      if m then
728        (low_internal_ram ? s)
729      else
730        (high_internal_ram ? s)
731    in
732      lookup … address memory (zero 8).
733  [1,2,3,4:
734     normalize
735     repeat (@ le_S_S)
736     @ le_O_n
737  ]
738qed.
739
740definition write_at_stack_pointer ≝
741  λM: Type[0].
742  λs: PreStatus M.
743  λv: Byte.
744    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in
745    let bit_zero ≝ get_index_v… nu O ? in
746    let bit_1 ≝ get_index_v… nu 1 ? in
747    let bit_2 ≝ get_index_v… nu 2 ? in
748    let bit_3 ≝ get_index_v… nu 3 ? in
749      if bit_zero then
750        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
751                              v (low_internal_ram ? s) in
752          set_low_internal_ram ? s memory
753      else
754        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
755                              v (high_internal_ram ? s) in
756          set_high_internal_ram ? s memory.
757  [1,2,3,4:
758     normalize
759     repeat (@ le_S_S)
760     @ le_O_n
761  ]
762qed.
763
764definition set_arg_16: ∀M: Type[0]. PreStatus M → Word → [[ dptr ]] → PreStatus M ≝
765  λM.
766  λs.
767  λv.
768  λa.
769   match a return λx. bool_to_Prop (is_in ? [[ dptr ]] x) → ? with
770     [ DPTR ⇒ λ_:True.
771       let 〈 bu, bl 〉 ≝ split … 8 8 v in
772       let status ≝ set_8051_sfr ? s SFR_DPH bu in
773       let status ≝ set_8051_sfr ? status SFR_DPL bl in
774         status
775     | _ ⇒ λK.
776       match K in False with
777       [
778       ]
779     ] (subaddressing_modein … a).
780   
781definition get_arg_16: ∀M: Type[0]. PreStatus M → [[ data16 ]] → Word ≝
782  λm, s, a.
783    match a return λx. bool_to_Prop (is_in ? [[ data16 ]] x) → ? with
784      [ DATA16 d ⇒ λ_:True. d
785      | _ ⇒ λK.
786        match K in False with
787        [
788        ]
789      ] (subaddressing_modein … a).
790     
791definition get_arg_8: ∀M: Type[0]. PreStatus M → bool → [[ direct ; indirect ; registr ;
792                                          acc_a ; acc_b ; data ; acc_dptr ;
793                                          acc_pc ; ext_indirect ;
794                                          ext_indirect_dptr ]] → Byte ≝
795  λm, s, l, a.
796    match a return λx. bool_to_Prop (is_in ? [[ direct ; indirect ; registr ;
797                                                acc_a ; acc_b ; data ; acc_dptr ;
798                                                acc_pc ; ext_indirect ;
799                                                ext_indirect_dptr ]] x) → ? with
800      [ ACC_A ⇒ λacc_a: True. get_8051_sfr ? s SFR_ACC_A
801      | ACC_B ⇒ λacc_b: True. get_8051_sfr ? s SFR_ACC_B
802      | DATA d ⇒ λdata: True. d
803      | REGISTER r ⇒ λregister: True. get_register ? s r
804      | EXT_INDIRECT_DPTR ⇒
805        λext_indirect_dptr: True.
806          let address ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in
807            lookup ? 16 address (external_ram ? s) (zero 8)
808      | EXT_INDIRECT e ⇒
809        λext_indirect: True.
810          let address ≝ get_register ? s [[ false; false; e ]]  in
811          let padded_address ≝ pad 8 8 address in
812            lookup ? 16 padded_address (external_ram ? s) (zero 8)
813      | ACC_DPTR ⇒
814        λacc_dptr: True.
815          let dptr ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in
816          let padded_acc ≝ pad 8 8 (get_8051_sfr ? s SFR_ACC_A) in
817          let 〈 carry, address 〉 ≝ half_add 16 dptr padded_acc in
818            lookup ? 16 address (external_ram ? s) (zero 8)
819      | ACC_PC ⇒
820        λacc_pc: True.
821          let padded_acc ≝ pad 8 8 (get_8051_sfr ? s SFR_ACC_A) in
822          let 〈 carry, address 〉 ≝ half_add 16 (program_counter ? s) padded_acc in
823            lookup ? 16 address (external_ram ? s) (zero 8)
824      | DIRECT d ⇒
825        λdirect: True.
826          let 〈 nu, nl 〉 ≝ split ? 4 4 d in
827          let bit_one ≝ get_index_v ? ? nu 0 ? in
828          let 〈 ignore, three_bits 〉 ≝ split ? 1 3 nu in
829            match bit_one with
830              [ false ⇒
831                  let address ≝ three_bits @@ nl in
832                    lookup ? 7 address (low_internal_ram ? s) (zero 8)
833              | true ⇒ get_bit_addressable_sfr ? s 8 d l
834              ]
835      | INDIRECT i ⇒
836        λindirect: True.
837          let 〈 nu, nl 〉 ≝ split ? 4 4 (get_register ? s [[ false; false; i]]) in
838          let 〈 bit_one_v, three_bits 〉 ≝ split ? 1 3 nu in
839          let bit_1 ≝ get_index_v ?? bit_one_v O ? in
840          match  bit_1 with
841            [ false ⇒
842                lookup ? 7 (three_bits @@ nl) (low_internal_ram ? s) (zero 8)
843            | true ⇒
844                lookup ? 7 (three_bits @@ nl) (high_internal_ram ? s) (zero 8)
845            ]
846      | _ ⇒ λother.
847        match other in False with [ ]
848      ] (subaddressing_modein … a).
849  [1,2:
850     normalize
851     repeat (@ le_S_S)
852     @ le_O_n
853  ]
854qed.
855
856axiom set_bit_addressable_sfr_ignores_clock: ∀m,s,d,v. clock m s = clock … (set_bit_addressable_sfr … s d v).
857
858definition set_arg_8: ∀M: Type[0]. ∀s:PreStatus M. [[ direct ; indirect ; registr ;
859                                   acc_a ; acc_b ; ext_indirect ;
860                                   ext_indirect_dptr ]] → Byte → Σs':PreStatus M.
861                                   clock … s = clock … s' ≝
862  λm, s, a, v.
863    match a return λx. bool_to_Prop (is_in ? [[ direct ; indirect ; registr ;
864                                                acc_a ; acc_b ; ext_indirect ;
865                                                ext_indirect_dptr ]] x) →
866                   Σs':PreStatus m. ?(*clock … s*) = ?(*clock … s'*)
867                   (*CSC: bug here if one specified the two clock above*)
868            with
869      [ DIRECT d ⇒
870        λdirect: True.
871          let 〈 nu, nl 〉 ≝ split … 4 4 d in
872          let bit_one ≝ get_index_v ? ? nu 0 ? in
873          let 〈 ignore, three_bits 〉 ≝ split ? 1 3 nu in
874            match bit_one with
875              [ true ⇒ set_bit_addressable_sfr ? s d v
876              | false ⇒
877                let memory ≝ insert ? 7 (three_bits @@ nl) v (low_internal_ram ? s) in
878                  set_low_internal_ram ? s memory
879              ]
880      | INDIRECT i ⇒
881        λindirect: True.
882          let register ≝ get_register ? s [[ false; false; i ]] in
883          let 〈nu, nl〉 ≝ split ? 4 4 register in
884          let bit_1 ≝ get_index_v … nu 0 ? in
885          let 〈ignore, three_bits〉 ≝ split ? 1 3 nu in
886            match bit_1 with
887              [ false ⇒
888                let memory ≝ insert … (three_bits @@ nl) v (low_internal_ram ? s) in
889                  set_low_internal_ram ? s memory
890              | true ⇒
891                let memory ≝ insert … (three_bits @@ nl) v (high_internal_ram ? s) in
892                  set_high_internal_ram ? s memory
893              ]
894      | REGISTER r ⇒ λregister: True. set_register ? s r v
895      | ACC_A ⇒ λacc_a: True. set_8051_sfr ? s SFR_ACC_A v
896      | ACC_B ⇒ λacc_b: True. set_8051_sfr ? s SFR_ACC_B v
897      | EXT_INDIRECT e ⇒
898        λext_indirect: True.
899          let address ≝ get_register ? s [[ false; false; e ]] in
900          let padded_address ≝ pad 8 8 address in
901          let memory ≝ insert ? 16 padded_address v (external_ram ? s) in
902            set_external_ram ? s memory
903      | EXT_INDIRECT_DPTR ⇒
904        λext_indirect_dptr: True.
905          let address ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in
906          let memory ≝ insert ? 16 address v (external_ram ? s) in
907            set_external_ram ? s memory
908      | _ ⇒
909        λother: False.
910          match other in False with [ ]
911      ] (subaddressing_modein … a).
912// qed.
913
914theorem 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      @(le_inv_ind ?? 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 by/
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      @H
960      /3/
961    ]
962  ]
963qed.
964
965definition 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 0 ? 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 0 ? 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         @modulus_less_than
1019      ]
1020qed.
1021     
1022(* XXX: Russell typing: Σs': PreStatus M. clock … s = clock M s', causes dangling de Bruijn
1023        pointer assertion failure in nCicMetaSubst.ml, line 293.  *)
1024definition set_arg_1: ∀M: Type[0]. PreStatus M → [[bit_addr; carry]] → Bit → PreStatus M ≝
1025  λm: Type[0].
1026  λs: PreStatus m.
1027  λa: [[bit_addr; carry]].
1028  λv: Bit.
1029    match a return λx. bool_to_Prop (is_in ? [[bit_addr ; carry]] x) → ? with
1030      [ BIT_ADDR b ⇒ λbit_addr: True.
1031        let 〈nu, nl〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in
1032        let bit_1 ≝ get_index_v ?? nu 0 ? in
1033        let 〈ignore, three_bits〉 ≝ split ? 1 3 nu in
1034        match bit_1 return λ_. ? with
1035          [ true ⇒
1036            let address ≝ nat_of_bitvector … (three_bits @@ nl) in
1037            let d ≝ address ÷ 8 in
1038            let m ≝ address mod 8 in
1039            let t ≝ bitvector_of_nat 8 ((d * 8) + 128) in
1040            let sfr ≝ get_bit_addressable_sfr ? s ? t true in
1041            let new_sfr ≝ set_index … sfr m v ? in
1042              set_bit_addressable_sfr ? s new_sfr t
1043          | false ⇒
1044            let address ≝ nat_of_bitvector … (three_bits @@ nl) in
1045            let address' ≝ bitvector_of_nat 7 ((address ÷ 8) + 32) in
1046            let t ≝ lookup ? 7 address' (low_internal_ram ? s) (zero 8) in
1047            let n_bit ≝ set_index … t (modulus address 8) v ? in
1048            let memory ≝ insert ? 7 address' n_bit (low_internal_ram ? s) in
1049              set_low_internal_ram ? s memory
1050          ]
1051      | CARRY ⇒
1052        λcarry: True.
1053        let 〈nu, nl〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in
1054        let bit_1 ≝ get_index_v… nu 1 ? in
1055        let bit_2 ≝ get_index_v… nu 2 ? in
1056        let bit_3 ≝ get_index_v… nu 3 ? in
1057        let new_psw ≝ [[ v; bit_1 ; bit_2; bit_3 ]] @@ nl in
1058          set_8051_sfr ? s SFR_PSW new_psw
1059      | _ ⇒
1060        λother: False.
1061          match other in False with
1062            [ ]
1063      ] (subaddressing_modein … a).
1064      [1,2,3,6:
1065         normalize
1066         repeat (@ le_S_S)
1067         @ le_O_n
1068      |4,5:
1069         @ modulus_less_than
1070      ]
1071qed.
1072
1073definition load_code_memory ≝
1074 fold_left_i … (
1075   λi, mem, v.
1076     insert … (bitvector_of_nat … i) v mem) (Stub Byte 16).
1077
1078definition load ≝
1079 λl.
1080 λstatus.
1081   set_code_memory (BitVectorTrie Word 16) ? status (load_code_memory l).
1082
1083definition fetch_pseudo_instruction: list labelled_instruction → Word → (pseudo_instruction × Word) ≝
1084  λcode_mem.
1085  λpc: Word.
1086    let 〈lbl, instr〉 ≝ nth (nat_of_bitvector ? pc) … code_mem ? in
1087    let 〈flags, new_pc〉 ≝ half_add ? pc (bitvector_of_nat ? 1) in
1088      〈instr, new_pc〉.
1089    cases not_implemented.
1090qed.
1091
1092lemma snd_fetch_pseudo_instruction:
1093 ∀l,ppc. \snd (fetch_pseudo_instruction l ppc) = \snd (half_add ? ppc (bitvector_of_nat ? 1)).
1094 #l #ppc whd in ⊢ (??%?); whd in ⊢ (?? match % with [_ ⇒ ?]?); @pair_elim'
1095 #lft #rgt @pair_elim' #x #y #_ @pair_elim' #a #b #_ normalize #H
1096 generalize in match (pair_destruct_2 ????? H); normalize #K >K %
1097qed.
1098
1099definition instruction_matches_identifier ≝
1100  λy: Identifier.
1101  λx: labelled_instruction.
1102    match \fst x with
1103    [ None ⇒ false
1104    | Some x ⇒ eq_identifier ? x y
1105    ].
1106
1107let rec does_not_occur (id:Identifier) (l:list labelled_instruction) on l: bool ≝
1108 match l with
1109  [ nil ⇒ true
1110  | cons hd tl ⇒ notb (instruction_matches_identifier id hd) ∧ does_not_occur id tl].
1111
1112lemma does_not_occur_None:
1113 ∀id,i,list_instr.
1114  does_not_occur id (list_instr@[〈None …,i〉]) =
1115  does_not_occur id list_instr.
1116 #id #i #list_instr elim list_instr
1117  [ % | #hd #tl #IH whd in ⊢ (??%%); >IH %]
1118qed.
1119
1120lemma does_not_occur_Some:
1121 ∀id,id',i,list_instr.
1122  eq_identifier ? id' id = false →
1123   does_not_occur id (list_instr@[〈Some ? id',i〉]) =
1124    does_not_occur id list_instr.
1125 #id #id' #i #list_instr elim list_instr
1126  [ #H normalize in H ⊢ %; >H %
1127  | * #x #i' #tl #IH #H whd in ⊢ (??%%); >(IH H) %]
1128qed.
1129
1130lemma does_not_occur_absurd:
1131 ∀id,i,list_instr.
1132  does_not_occur id (list_instr@[〈Some ? id,i〉]) = false.
1133 #id #i #list_instr elim list_instr
1134  [ normalize change with (if (if eq_identifier ??? then ? else ?) then ? else ? = ?)
1135    >eq_identifier_refl %
1136  | * #x #i' #tl #IH whd in ⊢ (??%%); >IH cases (notb ?) %]
1137qed.
1138
1139
1140let rec occurs_exactly_once (id:Identifier) (l:list labelled_instruction) on l : bool ≝
1141 match l with
1142  [ nil ⇒ false
1143  | cons hd tl ⇒
1144     if instruction_matches_identifier id hd then
1145      does_not_occur id tl
1146     else
1147      occurs_exactly_once id tl ].
1148
1149lemma occurs_exactly_once_None:
1150 ∀id,i,list_instr.
1151  occurs_exactly_once id (list_instr@[〈None …,i〉]) =
1152  occurs_exactly_once id list_instr.
1153 #id #i #list_instr elim list_instr
1154  [ % | #hd #tl #IH whd in ⊢ (??%%); >IH >does_not_occur_None %]
1155qed.
1156
1157lemma occurs_exactly_once_Some:
1158 ∀id,id',i,prefix.
1159  occurs_exactly_once id (prefix@[〈Some ? id',i〉]) → eq_identifier ? id' id ∨ occurs_exactly_once id prefix.
1160 #id #id' #i #prefix elim prefix
1161  [ whd in ⊢ (?% → ?);
1162    change with (eq_identifier ? id' id) in ⊢ (?(match % with [_ ⇒ ? | _ ⇒ ?]) → ?);
1163    @eq_identifier_elim normalize nodelta; /2/
1164  | *; #he #i' #tl #IH whd in ⊢ (?% → ?); whd in ⊢ (?(match % with [_ ⇒ ? | _ ⇒ ?]) → ?);
1165    cases he; normalize nodelta
1166     [ #H @ (IH H)
1167     | #x whd in ⊢ (? → ?(??%)); change with (eq_identifier ? x id) in match (instruction_matches_identifier ??);
1168       @eq_identifier_elim #E normalize nodelta
1169       [ destruct @eq_identifier_elim normalize nodelta;
1170        /2/ #H >does_not_occur_Some /2/
1171       | #H @IH @H]]]
1172qed.
1173
1174lemma occurs_exactly_once_Some_stronger:
1175  ∀id,id',i,prefix.
1176    occurs_exactly_once id (prefix@[〈Some ? id',i〉]) →
1177    (eq_identifier ? id' id ∧ does_not_occur id prefix) ∨
1178    (¬eq_identifier ? id' id ∧ occurs_exactly_once id prefix).
1179 #id #id' #i #prefix elim prefix
1180 [ whd in ⊢ (?% → ?); change with (eq_identifier ???) in ⊢ (?(match % with [_ ⇒ ?| _ ⇒ ?]) → ?);
1181   @eq_identifier_elim #E
1182  [ normalize //
1183  | normalize #H @⊥ @H 
1184  ]
1185 | *; #he #i' #tl #IH whd in ⊢ (?% → ?); whd in ⊢ (?(match % with [_ ⇒ ? | _ ⇒ ?]) → ?);
1186   cases he; normalize nodelta
1187   [ #H @ (IH H)
1188   | #x @eq_identifier_elim #Heq
1189     [ @eq_identifier_elim normalize nodelta
1190       [ #H >H >does_not_occur_absurd #Hf @⊥ @Hf
1191       | #H >(does_not_occur_Some)
1192         [ #H2 whd in match (does_not_occur ??);
1193           change with (eq_identifier ???) in match (instruction_matches_identifier ??);
1194           >Heq >eq_identifier_refl normalize nodelta
1195           @orb_elim normalize nodelta whd in match (occurs_exactly_once ??);
1196           change with (eq_identifier ???) in match (instruction_matches_identifier ??);
1197           >eq_identifier_refl
1198           normalize nodelta @H2 
1199         | /2/
1200         ]
1201       ]
1202     | normalize nodelta #H lapply (IH H) -IH -H;
1203       @eq_identifier_elim #Heq2
1204       #Hor @orb_elim
1205       [ <Heq2 in Hor; #Hor normalize in Hor;
1206         whd in match (does_not_occur ??); change with (eq_identifier ???) in match (instruction_matches_identifier ??);
1207         >eq_identifier_false // normalize nodelta
1208         cases (does_not_occur id' tl) in Hor; normalize nodelta //
1209       | normalize nodelta whd in match (occurs_exactly_once ??);
1210         change with (eq_identifier ???) in match (instruction_matches_identifier ??);
1211         >eq_identifier_false //
1212       ]
1213     ] 
1214   ]
1215 ]
1216qed.   
1217
1218let rec index_of_internal (A: Type[0]) (pred: A → bool) (l: list A) (acc: nat) on l: nat ≝
1219  match l with
1220  [ nil ⇒ ?
1221  | cons hd tl ⇒
1222    if pred hd then
1223      acc
1224    else
1225      index_of_internal A pred tl (S acc)
1226  ].
1227  cases not_implemented.
1228qed.
1229
1230
1231definition index_of ≝
1232  λA.
1233  λeq.
1234  λl.
1235    index_of_internal A eq l 0.
1236
1237lemma index_of_internal_None: ∀i,id,instr_list,n.
1238 occurs_exactly_once id (instr_list@[〈None …,i〉]) →
1239  index_of_internal ? (instruction_matches_identifier id) instr_list n =
1240   index_of_internal ? (instruction_matches_identifier id) (instr_list@[〈None …,i〉]) n.
1241 #i #id #instr_list elim instr_list
1242  [ #n #abs whd in abs; cases abs
1243  | #hd #tl #IH #n whd in ⊢ (% → ??%%); whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ?);
1244    cases (instruction_matches_identifier id hd) whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ??%%);
1245    [ #H %
1246    | #H @IH whd in H; cases (occurs_exactly_once ??) in H ⊢ %;
1247      [ #_ % | #abs cases abs ]]]
1248qed.
1249
1250lemma index_of_internal_Some_miss: ∀i,id,id'.
1251 eq_identifier ? id' id = false →
1252 ∀instr_list,n.
1253 occurs_exactly_once id (instr_list@[〈Some ? id',i〉]) →
1254  index_of_internal ? (instruction_matches_identifier id) instr_list n =
1255   index_of_internal ? (instruction_matches_identifier id) (instr_list@[〈Some ? id',i〉]) n.
1256 #i #id #id' #EQ #instr_list #n #H generalize in match (occurs_exactly_once_Some … H) in ⊢ ?; >EQ
1257 change with (occurs_exactly_once ?? → ?) generalize in match n; -n -H; elim instr_list
1258  [ #n #abs cases abs
1259  | #hd #tl #IH #n whd in ⊢ (?% → ??%%); cases (instruction_matches_identifier id hd) normalize nodelta;
1260    [ // | #K @IH //]]
1261qed.
1262
1263lemma index_of_internal_Some_hit: ∀i,id.
1264 ∀instr_list,n.
1265  occurs_exactly_once id (instr_list@[〈Some ? id,i〉]) →
1266   index_of_internal ? (instruction_matches_identifier id) (instr_list@[〈Some ? id,i〉]) n
1267  = |instr_list| + n.
1268 #i #id #instr_list elim instr_list
1269  [ #n #_ whd in ⊢ (??%%); change with (if eq_identifier … id id then ? else ? = ?) >eq_identifier_refl %
1270  | #hd #tl #IH #n whd in ⊢ (?% → ??%%); cases (instruction_matches_identifier id hd) normalize nodelta;
1271    [ >does_not_occur_absurd #abs cases abs | #H >plus_n_Sm applyS (IH (S n)) //]]
1272qed.
1273
1274definition address_of_word_labels_code_mem ≝
1275  λcode_mem.
1276  λid: Identifier.
1277    bitvector_of_nat 16 (index_of ? (instruction_matches_identifier id) code_mem).
1278
1279lemma address_of_word_labels_code_mem_None: ∀i,id,instr_list.
1280 occurs_exactly_once id (instr_list@[〈None …,i〉]) →
1281  address_of_word_labels_code_mem instr_list id =
1282  address_of_word_labels_code_mem (instr_list@[〈None …,i〉]) id.
1283 #i #id #instr_list #H whd in ⊢ (??%%); whd in ⊢ (??(??%?)(??%?));
1284 >(index_of_internal_None … H) %
1285qed.
1286
1287lemma address_of_word_labels_code_mem_Some_miss: ∀i,id,id',instr_list.
1288 eq_identifier ? id' id = false →
1289  occurs_exactly_once id (instr_list@[〈Some ? id',i〉]) →
1290   address_of_word_labels_code_mem instr_list id =
1291   address_of_word_labels_code_mem (instr_list@[〈Some … id',i〉]) id.
1292 #i #id #id' #instr_list #EQ #H whd in ⊢ (??%%); whd in ⊢ (??(??%?)(??%?));
1293 >(index_of_internal_Some_miss … H) [ @refl | // ]
1294qed.
1295
1296lemma address_of_word_labels_code_mem_Some_hit: ∀i,id,instr_list.
1297  occurs_exactly_once id (instr_list@[〈Some ? id,i〉]) →
1298   address_of_word_labels_code_mem (instr_list@[〈Some … id,i〉]) id
1299   = bitvector_of_nat … (|instr_list|).
1300 #i #id #instr_list #H whd in ⊢ (??%%); whd in ⊢ (??(??%?)?);
1301 >(index_of_internal_Some_hit … H) <plus_n_O @refl
1302qed.
1303
1304definition address_of_word_labels ≝
1305  λps: PseudoStatus.
1306  λid: Identifier.
1307    address_of_word_labels_code_mem (\snd (code_memory ? ps)) id.
1308
1309definition construct_datalabels: preamble → ? ≝
1310  λthe_preamble: preamble.
1311    \fst (foldl ((identifier_map ASMTag Word) × Word) ? (
1312    λt. λpreamble.
1313      let 〈datalabels, addr〉 ≝ t in
1314      let 〈name, size〉 ≝ preamble in
1315      let 〈carry, sum〉 ≝ half_add … addr size in
1316        〈add ? ? datalabels name addr, sum〉)
1317          〈empty_map …, zero 16〉 (\snd the_preamble)).
Note: See TracBrowser for help on using the repository browser.