# source:src/ASM/AssemblyProof.ma@1037

Last change on this file since 1037 was 1037, checked in by sacerdot, 9 years ago

Main theorem: comments are working again.

File size: 99.1 KB
Line
1include "ASM/Assembly.ma".
2include "ASM/Interpret.ma".
3include "ASM/StatusProofs.ma".
4
5definition bit_elim_prop: ∀P: bool → Prop. Prop ≝
6  λP.
7    P true ∧ P false.
8
9let rec bitvector_elim_prop_internal
10  (n: nat) (P: BitVector n → Prop) (m: nat) on m: m ≤ n → BitVector (n - m) → Prop ≝
11  match m return λm. m ≤ n → BitVector (n - m) → Prop with
12  [ O    ⇒ λprf1. λprefix. P ?
13  | S n' ⇒ λprf2. λprefix. bit_elim_prop (λbit. bitvector_elim_prop_internal n P n' ? ?)
14  ].
15  [ applyS prefix
16  | letin res ≝ (bit ::: prefix)
17    < (minus_S_S ? ?)
18    > (minus_Sn_m ? ?)
19  [ @ res
20  | @ prf2
21  ]
22  | /2/
23  ].
24qed.
25
26definition bitvector_elim_prop ≝
27  λn: nat.
28  λP: BitVector n → Prop.
29    bitvector_elim_prop_internal n P n ? ?.
30  [ @ (le_n ?)
31  | < (minus_n_n ?)
32    @ [[ ]]
33  ]
34qed.
35
36lemma bool_eq_internal_eq:
37  ∀b, c.
38    (λb. λc. (if b then c else (if c then false else true))) b c = true → b = c.
39  #b #c
40  cases b
41  [ normalize //
42  | normalize
43    cases c
44    [ normalize //
45    | normalize //
46    ]
47  ]
48qed.
49
50definition bit_elim: ∀P: bool → bool. bool ≝
51  λP.
52    P true ∧ P false.
53
54let rec bitvector_elim_internal
55  (n: nat) (P: BitVector n → bool) (m: nat) on m: m ≤ n → BitVector (n - m) → bool ≝
56  match m return λm. m ≤ n → BitVector (n - m) → bool with
57  [ O    ⇒ λprf1. λprefix. P ?
58  | S n' ⇒ λprf2. λprefix. bit_elim (λbit. bitvector_elim_internal n P n' ? ?)
59  ].
60  [ applyS prefix
61  | letin res ≝ (bit ::: prefix)
62    < (minus_S_S ? ?)
63    > (minus_Sn_m ? ?)
64    [ @ res
65    | @ prf2
66    ]
67  | /2/
68  ].
69qed.
70
71definition bitvector_elim ≝
72  λn: nat.
73  λP: BitVector n → bool.
74    bitvector_elim_internal n P n ? ?.
75  [ @ (le_n ?)
76  | < (minus_n_n ?)
77    @ [[ ]]
78  ]
79qed.
80
81lemma super_rewrite2:
82 ∀A:Type[0].∀n,m.∀v1: Vector A n.∀v2: Vector A m.
83  ∀P: ∀m. Vector A m → Prop.
84   n=m → v1 ≃ v2 → P n v1 → P m v2.
85 #A #n #m #v1 #v2 #P #EQ <EQ in v2; #V #JMEQ >JMEQ //
86qed.
87
88lemma vector_cons_append:
89  ∀A: Type[0].
90  ∀n: nat.
91  ∀e: A.
92  ∀v: Vector A n.
93    e ::: v = [[ e ]] @@ v.
94  # A # N # E # V
95  elim V
96  [ normalize %
97  | # NN # AA # VV # IH
98    normalize
99    %
100qed.
101
102lemma vector_cons_append2:
103  ∀A: Type[0].
104  ∀n, m: nat.
105  ∀v: Vector A n.
106  ∀q: Vector A m.
107  ∀hd: A.
108    hd:::(v@@q) = (hd:::v)@@q.
109  #A #n #m #v #q
110  elim v
111  [ #hd %
112  | #n' #hd' #tl' #ih #hd' <ih %
113  ]
114qed.
115
116lemma jmeq_cons_vector_monotone:
117  ∀A: Type[0].
118  ∀m, n: nat.
119  ∀v: Vector A m.
120  ∀q: Vector A n.
121  ∀prf: m = n.
122  ∀hd: A.
123    v ≃ q → hd:::v ≃ hd:::q.
124  #A #m #n #v #q #prf #hd #E
125  @(super_rewrite2 A … E)
126  [ assumption | @jmrefl ]
127qed.
128
129lemma vector_associative_append:
130  ∀A: Type[0].
131  ∀n, m, o:  nat.
132  ∀v: Vector A n.
133  ∀q: Vector A m.
134  ∀r: Vector A o.
135    ((v @@ q) @@ r)
136    ≃
137    (v @@ (q @@ r)).
138  #A #n #m #o #v #q #r
139  elim v
140  [ %
141  | #n' #hd #tl #ih
142    <(vector_cons_append2 A … hd)
143    @jmeq_cons_vector_monotone
144    //
145  ]
146qed.
147
148lemma mem_middle_vector:
149  ∀A: Type[0].
150  ∀m, o: nat.
151  ∀eq: A → A → bool.
152  ∀reflex: ∀a. eq a a = true.
153  ∀p: Vector A m.
154  ∀a: A.
155  ∀r: Vector A o.
156    mem A eq ? (p@@(a:::r)) a = true.
157  # A # M # O # EQ # REFLEX # P # A
158  elim P
159  [ normalize
160    > (REFLEX A)
161    normalize
162    # H
163    %
164  | # NN # AA # PP # IH
165    normalize
166    cases (EQ A AA) //
167     @ IH
168  ]
169qed.
170
171lemma mem_monotonic_wrt_append:
172  ∀A: Type[0].
173  ∀m, o: nat.
174  ∀eq: A → A → bool.
175  ∀reflex: ∀a. eq a a = true.
176  ∀p: Vector A m.
177  ∀a: A.
178  ∀r: Vector A o.
179    mem A eq ? r a = true → mem A eq ? (p @@ r) a = true.
180  # A # M # O # EQ # REFLEX # P # A
181  elim P
182  [ #R #H @H
183  | #NN #AA # PP # IH #R #H
184    normalize
185    cases (EQ A AA)
186    [ normalize %
187    | @ IH @ H
188    ]
189  ]
190qed.
191
192lemma subvector_multiple_append:
193  ∀A: Type[0].
194  ∀o, n: nat.
195  ∀eq: A → A → bool.
196  ∀refl: ∀a. eq a a = true.
197  ∀h: Vector A o.
198  ∀v: Vector A n.
199  ∀m: nat.
200  ∀q: Vector A m.
201    bool_to_Prop (subvector_with A ? ? eq v (h @@ q @@ v)).
202  # A # O # N # EQ # REFLEX # H # V
203  elim V
204  [ normalize
205    # M # V %
206  | # NN # AA # VV # IH # MM # QQ
207    change with (bool_to_Prop (andb ??))
208    cut ((mem A EQ (O + (MM + S NN)) (H@@QQ@@AA:::VV) AA) = true)
209    [
210    | # HH > HH
211      > (vector_cons_append ? ? AA VV)
212      change with (bool_to_Prop (subvector_with ??????))
213      @(super_rewrite2 A ((MM + 1)+ NN) (MM+S NN) ??
214        (λSS.λVS.bool_to_Prop (subvector_with ?? (O+SS) ?? (H@@VS)))
215        ?
216        (vector_associative_append A ? ? ? QQ [[AA]] VV))
217      [ >associative_plus //
218      | @IH ]
219    ]
220    @(mem_monotonic_wrt_append)
221    [ @ REFLEX
222    | @(mem_monotonic_wrt_append)
223      [ @ REFLEX
224      | normalize
225        > REFLEX
226        normalize
227        %
228      ]
229    ]
230qed.
231
232lemma vector_cons_empty:
233  ∀A: Type[0].
234  ∀n: nat.
235  ∀v: Vector A n.
236    [[ ]] @@ v = v.
237  # A # N # V
238  elim V
239  [ normalize %
240  | # NN # HH # VV #H %
241  ]
242qed.
243
244corollary subvector_hd_tl:
245  ∀A: Type[0].
246  ∀o: nat.
247  ∀eq: A → A → bool.
248  ∀refl: ∀a. eq a a = true.
249  ∀h: A.
250  ∀v: Vector A o.
251    bool_to_Prop (subvector_with A ? ? eq v (h ::: v)).
252  # A # O # EQ # REFLEX # H # V
253  > (vector_cons_append A ? H V)
254  < (vector_cons_empty A ? ([[H]] @@ V))
255  @ (subvector_multiple_append A ? ? EQ REFLEX [[]] V ? [[ H ]])
256qed.
257
258lemma eq_a_reflexive:
259  ∀a. eq_a a a = true.
260  # A
261  cases A
262  %
263qed.
264
265lemma is_in_monotonic_wrt_append:
266  ∀m, n: nat.
270    bool_to_Prop (is_in ? p to_search) → bool_to_Prop (is_in ? (q @@ p) to_search).
271  # M # N # P # Q # TO_SEARCH
272  # H
273  elim Q
274  [ normalize
275    @ H
276  | # NN # PP # QQ # IH
277    normalize
278    cases (is_a PP TO_SEARCH)
279    [ normalize
280      %
281    | normalize
282      normalize in IH
283      @ IH
284    ]
285  ]
286qed.
287
288corollary is_in_hd_tl:
291  ∀n: nat.
293    bool_to_Prop (is_in ? v to_search) → bool_to_Prop (is_in ? (hd:::v) to_search).
294  # TO_SEARCH # HD # N # V
295  elim V
296  [ # H
297    normalize in H;
298    cases H
299  | # NN # HHD # VV # IH # HH
300    > vector_cons_append
301    > (vector_cons_append ? ? HHD VV)
302    @ (is_in_monotonic_wrt_append ? 1 ([[HHD]]@@VV) [[HD]] TO_SEARCH)
303    @ HH
304  ]
305qed.
306
308  (n: nat) (l: Vector addressing_mode_tag (S n)) on l: (l → bool) → bool ≝
309  match l return λx.match x with [O ⇒ λl: Vector … O. bool | S x' ⇒ λl: Vector addressing_mode_tag (S x').
310   (l → bool) → bool ] with
311  [ VEmpty      ⇒  true
312  | VCons len hd tl ⇒ λP.
313    let process_hd ≝
314      match hd return λhd. ∀P: hd:::tl → bool. bool with
315      [ direct ⇒ λP.bitvector_elim 8 (λx. P (DIRECT x))
316      | indirect ⇒ λP.bit_elim (λx. P (INDIRECT x))
317      | ext_indirect ⇒ λP.bit_elim (λx. P (EXT_INDIRECT x))
318      | registr ⇒ λP.bitvector_elim 3 (λx. P (REGISTER x))
319      | acc_a ⇒ λP.P ACC_A
320      | acc_b ⇒ λP.P ACC_B
321      | dptr ⇒ λP.P DPTR
322      | data ⇒ λP.bitvector_elim 8 (λx. P (DATA x))
323      | data16 ⇒ λP.bitvector_elim 16 (λx. P (DATA16 x))
324      | acc_dptr ⇒ λP.P ACC_DPTR
325      | acc_pc ⇒ λP.P ACC_PC
326      | ext_indirect_dptr ⇒ λP.P EXT_INDIRECT_DPTR
327      | indirect_dptr ⇒ λP.P INDIRECT_DPTR
328      | carry ⇒ λP.P CARRY
331      | relative ⇒ λP.bitvector_elim 8 (λx. P (RELATIVE x))
334      ]
335    in
336      andb (process_hd P)
337       (match len return λx. x = len → bool with
338         [ O ⇒ λprf. true
339         | S y ⇒ λprf. list_addressing_mode_tags_elim y ? P ] (refl ? len))
340  ].
341  try %
342  [ 2: cases (sym_eq ??? prf); @tl
343  | generalize in match H; generalize in match tl; cases prf;
344    (* cases prf in tl H; : ??? WAS WORKING BEFORE *)
345    #tl
346    normalize in ⊢ (∀_: %. ?)
347    # H
348    whd
349    normalize in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
350    cases (is_a hd (subaddressing_modeel y tl H)) whd // ]
351qed.
352
353definition product_elim ≝
354  λm, n: nat.
355  λv: Vector addressing_mode_tag (S m).
356  λq: Vector addressing_mode_tag (S n).
357  λP: (v × q) → bool.
358    list_addressing_mode_tags_elim ? v (λx. list_addressing_mode_tags_elim ? q (λy. P 〈x, y〉)).
359
360definition union_elim ≝
361  λA, B: Type[0].
362  λelimA: (A → bool) → bool.
363  λelimB: (B → bool) → bool.
364  λelimU: A ⊎ B → bool.
365    elimA (λa. elimB (λb. elimU (inl ? ? a) ∧ elimU (inr ? ? b))).
366
367(*
368definition preinstruction_elim: ∀P: preinstruction [[ relative ]] → bool. bool ≝
369  λP.
372    list_addressing_mode_tags_elim ? [[ registr ; direct ; indirect ; data ]] (λaddr. P (SUBB ? ACC_A addr)) ∧
373    list_addressing_mode_tags_elim ? [[ acc_a ; registr ; direct ; indirect ; dptr ]] (λaddr. P (INC ? addr)) ∧
374    list_addressing_mode_tags_elim ? [[ acc_a ; registr ; direct ; indirect ]] (λaddr. P (DEC ? addr)) ∧
377    list_addressing_mode_tags_elim ? [[ registr ; direct ]] (λaddr. bitvector_elim 8 (λr. P (DJNZ ? addr (RELATIVE r)))) ∧
380    P (DA ? ACC_A) ∧
381    bitvector_elim 8 (λr. P (JC ? (RELATIVE r))) ∧
382    bitvector_elim 8 (λr. P (JNC ? (RELATIVE r))) ∧
383    bitvector_elim 8 (λr. P (JZ ? (RELATIVE r))) ∧
384    bitvector_elim 8 (λr. P (JNZ ? (RELATIVE r))) ∧
385    bitvector_elim 8 (λr. (bitvector_elim 8 (λb: BitVector 8. P (JB ? (BIT_ADDR b) (RELATIVE r))))) ∧
386    bitvector_elim 8 (λr. (bitvector_elim 8 (λb: BitVector 8. P (JNB ? (BIT_ADDR b) (RELATIVE r))))) ∧
387    bitvector_elim 8 (λr. (bitvector_elim 8 (λb: BitVector 8. P (JBC ? (BIT_ADDR b) (RELATIVE r))))) ∧
388    list_addressing_mode_tags_elim ? [[ registr; direct ]] (λaddr. bitvector_elim 8 (λr. P (DJNZ ? addr (RELATIVE r)))) ∧
389    P (RL ? ACC_A) ∧
390    P (RLC ? ACC_A) ∧
391    P (RR ? ACC_A) ∧
392    P (RRC ? ACC_A) ∧
393    P (SWAP ? ACC_A) ∧
394    P (RET ?) ∧
395    P (RETI ?) ∧
396    P (NOP ?) ∧
397    bit_elim (λb. P (XCHD ? ACC_A (INDIRECT b))) ∧
401    union_elim ? ? (product_elim ? ? [[ acc_a ]] [[ direct; data ]])
402                   (product_elim ? ? [[ registr; indirect ]] [[ data ]])
403                   (λd. bitvector_elim 8 (λb. P (CJNE ? d (RELATIVE b)))) ∧
405    union_elim ? ? (product_elim ? ? [[acc_a]] [[ data ; registr ; direct ; indirect ]])
406                   (product_elim ? ? [[direct]] [[ acc_a ; data ]])
407                   (λd. P (XRL ? d)) ∧
408    union_elim ? ? (union_elim ? ? (product_elim ? ? [[acc_a]] [[ registr ; direct ; indirect ; data ]])
409                                   (product_elim ? ? [[direct]] [[ acc_a ; data ]]))
411                   (λd. P (ANL ? d)) ∧
412    union_elim ? ? (union_elim ? ? (product_elim ? ? [[acc_a]] [[ registr ; data ; direct ; indirect ]])
413                                   (product_elim ? ? [[direct]] [[ acc_a ; data ]]))
415                   (λd. P (ORL ? d)) ∧
416    union_elim ? ? (product_elim ? ? [[acc_a]] [[ ext_indirect ; ext_indirect_dptr ]])
417                   (product_elim ? ? [[ ext_indirect ; ext_indirect_dptr ]] [[acc_a]])
418                   (λd. P (MOVX ? d)) ∧
419    union_elim ? ? (
420      union_elim ? ? (
421        union_elim ? ? (
422          union_elim ? ? (
423            union_elim ? ?  (product_elim ? ? [[acc_a]] [[ registr ; direct ; indirect ; data ]])
424                            (product_elim ? ? [[ registr ; indirect ]] [[ acc_a ; direct ; data ]]))
425                            (product_elim ? ? [[direct]] [[ acc_a ; registr ; direct ; indirect ; data ]]))
426                            (product_elim ? ? [[dptr]] [[data16]]))
427                            (product_elim ? ? [[carry]] [[bit_addr]]))
428                            (product_elim ? ? [[bit_addr]] [[carry]])
429                            (λd. P (MOV ? d)).
430  %
431qed.
432
433definition instruction_elim: ∀P: instruction → bool. bool ≝
434  λP. (*
435    bitvector_elim 11 (λx. P (ACALL (ADDR11 x))) ∧
436    bitvector_elim 16 (λx. P (LCALL (ADDR16 x))) ∧
437    bitvector_elim 11 (λx. P (AJMP (ADDR11 x))) ∧
438    bitvector_elim 16 (λx. P (LJMP (ADDR16 x))) ∧ *)
439    bitvector_elim 8 (λx. P (SJMP (RELATIVE x))). (*  ∧
440    P (JMP INDIRECT_DPTR) ∧
441    list_addressing_mode_tags_elim ? [[ acc_dptr; acc_pc ]] (λa. P (MOVC ACC_A a)) ∧
442    preinstruction_elim (λp. P (RealInstruction p)). *)
443  %
444qed.
445
446
447axiom instruction_elim_complete:
448 ∀P. instruction_elim P = true → ∀i. P i = true.
449*)
450(*definition eq_instruction ≝
451  λi, j: instruction.
452    true.*)
453
456  match a with
457  [ DIRECT d ⇒
458    match b with
459    [ DIRECT e ⇒ eq_bv ? d e
460    | _ ⇒ false
461    ]
462  | INDIRECT b' ⇒
463    match b with
464    [ INDIRECT e ⇒ eq_b b' e
465    | _ ⇒ false
466    ]
467  | EXT_INDIRECT b' ⇒
468    match b with
469    [ EXT_INDIRECT e ⇒ eq_b b' e
470    | _ ⇒ false
471    ]
472  | REGISTER bv ⇒
473    match b with
474    [ REGISTER bv' ⇒ eq_bv ? bv bv'
475    | _ ⇒ false
476    ]
477  | ACC_A ⇒ match b with [ ACC_A ⇒ true | _ ⇒ false ]
478  | ACC_B ⇒ match b with [ ACC_B ⇒ true | _ ⇒ false ]
479  | DPTR ⇒ match b with [ DPTR ⇒ true | _ ⇒ false ]
480  | DATA b' ⇒
481    match b with
482    [ DATA e ⇒ eq_bv ? b' e
483    | _ ⇒ false
484    ]
485  | DATA16 w ⇒
486    match b with
487    [ DATA16 e ⇒ eq_bv ? w e
488    | _ ⇒ false
489    ]
490  | ACC_DPTR ⇒ match b with [ ACC_DPTR ⇒ true | _ ⇒ false ]
491  | ACC_PC ⇒ match b with [ ACC_PC ⇒ true | _ ⇒ false ]
492  | EXT_INDIRECT_DPTR ⇒ match b with [ EXT_INDIRECT_DPTR ⇒ true | _ ⇒ false ]
493  | INDIRECT_DPTR ⇒ match b with [ INDIRECT_DPTR ⇒ true | _ ⇒ false ]
494  | CARRY ⇒ match b with [ CARRY ⇒ true | _ ⇒ false ]
496    match b with
497    [ BIT_ADDR e ⇒ eq_bv ? b' e
498    | _ ⇒ false
499    ]
501    match b with
502    [ N_BIT_ADDR e ⇒ eq_bv ? b' e
503    | _ ⇒ false
504    ]
505  | RELATIVE n ⇒
506    match b with
507    [ RELATIVE e ⇒ eq_bv ? n e
508    | _ ⇒ false
509    ]
511    match b with
512    [ ADDR11 e ⇒ eq_bv ? w e
513    | _ ⇒ false
514    ]
516    match b with
517    [ ADDR16 e ⇒ eq_bv ? w e
518    | _ ⇒ false
519    ]
520  ].
521
522lemma eq_bv_refl:
523  ∀n, b.
524    eq_bv n b b = true.
525  #n #b cases b //
526qed.
527
528lemma eq_b_refl:
529  ∀b.
530    eq_b b b = true.
531  #b cases b //
532qed.
533
535  ∀a. eq_addressing_mode a a = true.
536  #a cases a try #arg1 try #arg2 try @eq_bv_refl try @eq_b_refl
537  try normalize %
538qed.
539
540definition eq_sum: ∀A, B. (A → A → bool) → (B → B → bool) → (A ⊎ B) → (A ⊎ B) → bool ≝
541  λlt, rt, leq, req, left, right.
542    match left with
543    [ inl l ⇒
544      match right with
545      [ inl l' ⇒ leq l l'
546      | _ ⇒ false
547      ]
548    | inr r ⇒
549      match right with
550      [ inr r' ⇒ req r r'
551      | _ ⇒ false
552      ]
553    ].
554
555definition eq_prod: ∀A, B. (A → A → bool) → (B → B → bool) → (A × B) → (A × B) → bool ≝
556  λlt, rt, leq, req, left, right.
557    let 〈l, r〉 ≝ left in
558    let 〈l', r'〉 ≝ right in
559      leq l l' ∧ req r r'.
560
561definition eq_preinstruction: preinstruction [[relative]] → preinstruction [[relative]] → bool ≝
562  λi, j.
563  match i with
564  [ ADD arg1 arg2 ⇒
565    match j with
567    | _ ⇒ false
568    ]
569  | ADDC arg1 arg2 ⇒
570    match j with
572    | _ ⇒ false
573    ]
574  | SUBB arg1 arg2 ⇒
575    match j with
576    [ SUBB arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
577    | _ ⇒ false
578    ]
579  | INC arg ⇒
580    match j with
581    [ INC arg' ⇒ eq_addressing_mode arg arg'
582    | _ ⇒ false
583    ]
584  | DEC arg ⇒
585    match j with
586    [ DEC arg' ⇒ eq_addressing_mode arg arg'
587    | _ ⇒ false
588    ]
589  | MUL arg1 arg2 ⇒
590    match j with
591    [ MUL arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
592    | _ ⇒ false
593    ]
594  | DIV arg1 arg2 ⇒
595    match j with
596    [ DIV arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
597    | _ ⇒ false
598    ]
599  | DA arg ⇒
600    match j with
601    [ DA arg' ⇒ eq_addressing_mode arg arg'
602    | _ ⇒ false
603    ]
604  | JC arg ⇒
605    match j with
606    [ JC arg' ⇒ eq_addressing_mode arg arg'
607    | _ ⇒ false
608    ]
609  | JNC arg ⇒
610    match j with
611    [ JNC arg' ⇒ eq_addressing_mode arg arg'
612    | _ ⇒ false
613    ]
614  | JB arg1 arg2 ⇒
615    match j with
616    [ JB arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
617    | _ ⇒ false
618    ]
619  | JNB arg1 arg2 ⇒
620    match j with
621    [ JNB arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
622    | _ ⇒ false
623    ]
624  | JBC arg1 arg2 ⇒
625    match j with
626    [ JBC arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
627    | _ ⇒ false
628    ]
629  | JZ arg ⇒
630    match j with
631    [ JZ arg' ⇒ eq_addressing_mode arg arg'
632    | _ ⇒ false
633    ]
634  | JNZ arg ⇒
635    match j with
636    [ JNZ arg' ⇒ eq_addressing_mode arg arg'
637    | _ ⇒ false
638    ]
639  | CJNE arg1 arg2 ⇒
640    match j with
641    [ CJNE arg1' arg2' ⇒
644      let arg1_eq ≝ eq_sum ? ? prod_eq_left prod_eq_right in
645        arg1_eq arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
646    | _ ⇒ false
647    ]
648  | DJNZ arg1 arg2 ⇒
649    match j with
650    [ DJNZ arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
651    | _ ⇒ false
652    ]
653  | CLR arg ⇒
654    match j with
655    [ CLR arg' ⇒ eq_addressing_mode arg arg'
656    | _ ⇒ false
657    ]
658  | CPL arg ⇒
659    match j with
660    [ CPL arg' ⇒ eq_addressing_mode arg arg'
661    | _ ⇒ false
662    ]
663  | RL arg ⇒
664    match j with
665    [ RL arg' ⇒ eq_addressing_mode arg arg'
666    | _ ⇒ false
667    ]
668  | RLC arg ⇒
669    match j with
670    [ RLC arg' ⇒ eq_addressing_mode arg arg'
671    | _ ⇒ false
672    ]
673  | RR arg ⇒
674    match j with
675    [ RR arg' ⇒ eq_addressing_mode arg arg'
676    | _ ⇒ false
677    ]
678  | RRC arg ⇒
679    match j with
680    [ RRC arg' ⇒ eq_addressing_mode arg arg'
681    | _ ⇒ false
682    ]
683  | SWAP arg ⇒
684    match j with
685    [ SWAP arg' ⇒ eq_addressing_mode arg arg'
686    | _ ⇒ false
687    ]
688  | SETB arg ⇒
689    match j with
690    [ SETB arg' ⇒ eq_addressing_mode arg arg'
691    | _ ⇒ false
692    ]
693  | PUSH arg ⇒
694    match j with
695    [ PUSH arg' ⇒ eq_addressing_mode arg arg'
696    | _ ⇒ false
697    ]
698  | POP arg ⇒
699    match j with
700    [ POP arg' ⇒ eq_addressing_mode arg arg'
701    | _ ⇒ false
702    ]
703  | XCH arg1 arg2 ⇒
704    match j with
705    [ XCH arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
706    | _ ⇒ false
707    ]
708  | XCHD arg1 arg2 ⇒
709    match j with
710    [ XCHD arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
711    | _ ⇒ false
712    ]
713  | RET ⇒ match j with [ RET ⇒ true | _ ⇒ false ]
714  | RETI ⇒ match j with [ RETI ⇒ true | _ ⇒ false ]
715  | NOP ⇒ match j with [ NOP ⇒ true | _ ⇒ false ]
716  | MOVX arg ⇒
717    match j with
718    [ MOVX arg' ⇒
721      let sum_eq ≝ eq_sum ? ? prod_eq_left prod_eq_right in
722        sum_eq arg arg'
723    | _ ⇒ false
724    ]
725  | XRL arg ⇒
726    match j with
727    [ XRL arg' ⇒
728      let prod_eq_left ≝ eq_prod [[acc_a]] [[ data ; registr ; direct ; indirect ]] eq_addressing_mode eq_addressing_mode in
729      let prod_eq_right ≝ eq_prod [[direct]] [[ acc_a ; data ]] eq_addressing_mode eq_addressing_mode in
730      let sum_eq ≝ eq_sum ? ? prod_eq_left prod_eq_right in
731        sum_eq arg arg'
732    | _ ⇒ false
733    ]
734  | ORL arg ⇒
735    match j with
736    [ ORL arg' ⇒
737      let prod_eq_left1 ≝ eq_prod [[acc_a]] [[ registr ; data ; direct ; indirect ]] eq_addressing_mode eq_addressing_mode in
738      let prod_eq_left2 ≝ eq_prod [[direct]] [[ acc_a; data ]] eq_addressing_mode eq_addressing_mode in
739      let prod_eq_left ≝ eq_sum ? ? prod_eq_left1 prod_eq_left2 in
741      let sum_eq ≝ eq_sum ? ? prod_eq_left prod_eq_right in
742        sum_eq arg arg'
743    | _ ⇒ false
744    ]
745  | ANL arg ⇒
746    match j with
747    [ ANL arg' ⇒
748      let prod_eq_left1 ≝ eq_prod [[acc_a]] [[ registr ; direct ; indirect ; data ]] eq_addressing_mode eq_addressing_mode in
749      let prod_eq_left2 ≝ eq_prod [[direct]] [[ acc_a; data ]] eq_addressing_mode eq_addressing_mode in
750      let prod_eq_left ≝ eq_sum ? ? prod_eq_left1 prod_eq_left2 in
752      let sum_eq ≝ eq_sum ? ? prod_eq_left prod_eq_right in
753        sum_eq arg arg'
754    | _ ⇒ false
755    ]
756  | MOV arg ⇒
757    match j with
758    [ MOV arg' ⇒
759      let prod_eq_6 ≝ eq_prod [[acc_a]] [[registr; direct; indirect; data]] eq_addressing_mode eq_addressing_mode in
760      let prod_eq_5 ≝ eq_prod [[registr; indirect]] [[acc_a; direct; data]] eq_addressing_mode eq_addressing_mode in
761      let prod_eq_4 ≝ eq_prod [[direct]] [[acc_a; registr; direct; indirect; data]] eq_addressing_mode eq_addressing_mode in
765      let sum_eq_1 ≝ eq_sum ? ? prod_eq_6 prod_eq_5 in
766      let sum_eq_2 ≝ eq_sum ? ? sum_eq_1 prod_eq_4 in
767      let sum_eq_3 ≝ eq_sum ? ? sum_eq_2 prod_eq_3 in
768      let sum_eq_4 ≝ eq_sum ? ? sum_eq_3 prod_eq_2 in
769      let sum_eq_5 ≝ eq_sum ? ? sum_eq_4 prod_eq_1 in
770        sum_eq_5 arg arg'
771    | _ ⇒ false
772    ]
773  ].
774
775lemma eq_sum_refl:
776  ∀A, B: Type[0].
777  ∀leq, req.
778  ∀s.
779  ∀leq_refl: (∀t. leq t t = true).
780  ∀req_refl: (∀u. req u u = true).
781    eq_sum A B leq req s s = true.
782  #A #B #leq #req #s #leq_refl #req_refl
783  cases s whd in ⊢ (? → ??%?) //
784qed.
785
786lemma eq_prod_refl:
787  ∀A, B: Type[0].
788  ∀leq, req.
789  ∀s.
790  ∀leq_refl: (∀t. leq t t = true).
791  ∀req_refl: (∀u. req u u = true).
792    eq_prod A B leq req s s = true.
793  #A #B #leq #req #s #leq_refl #req_refl
794  cases s whd in ⊢ (? → ? → ??%?) #l #r >leq_refl normalize @req_refl
795qed.
796
797lemma eq_preinstruction_refl:
798  ∀i.
799    eq_preinstruction i i = true.
800  #i cases i try #arg1 try #arg2
802  [1,2,3,4,5,6,7,8,10,16,17,18,19,20:
803    whd in ⊢ (??%?)
804    try %
807  |13,15:
808    whd in ⊢ (??%?)
809    cases arg1
810    [*:
811      #arg1_left normalize nodelta
812      >eq_prod_refl [*: try % #argr @eq_addressing_mode_refl]
813    ]
814  |11,12:
815    whd in ⊢ (??%?)
816    cases arg1
817    [1:
818      #arg1_left normalize nodelta
819      >(eq_sum_refl …)
820      [1: % | 2,3: #arg @eq_prod_refl ]
822    |2:
823      #arg1_left normalize nodelta
825    |3:
826      #arg1_left normalize nodelta
827      >(eq_sum_refl …) [1: %
828      |2,3: #arg @eq_prod_refl #arg @eq_addressing_mode_refl ]
829    |4:
830      #arg1_left normalize nodelta
831      @eq_prod_refl [*: #arg @eq_addressing_mode_refl ]
832    ]
833  |14:
834    whd in ⊢ (??%?)
835    cases arg1
836    [ #arg1_left normalize nodelta
837      @eq_sum_refl
838      [1: #arg @eq_sum_refl
839        [1: #arg @eq_sum_refl
840          [1: #arg @eq_sum_refl
841            [1: #arg @eq_prod_refl
843            |2: #arg @eq_prod_refl
845            ]
846          |2: #arg @eq_prod_refl
848          ]
849        |2: #arg @eq_prod_refl
851        ]
852      |2: #arg @eq_prod_refl
854      ]
855    |2: #arg1_right normalize nodelta
856        @eq_prod_refl [*: #arg @eq_addressing_mode_refl ]
857    ]
858  |*:
859    whd in ⊢ (??%?)
860    cases arg1
861    [*: #arg1 >eq_sum_refl
863      |2,3,5,6: #arg @eq_prod_refl
865      ]
866    ]
867  ]
868qed.
869
870definition eq_instruction: instruction → instruction → bool ≝
871  λi, j.
872  match i with
873  [ ACALL arg ⇒
874    match j with
875    [ ACALL arg' ⇒ eq_addressing_mode arg arg'
876    | _ ⇒ false
877    ]
878  | LCALL arg ⇒
879    match j with
880    [ LCALL arg' ⇒ eq_addressing_mode arg arg'
881    | _ ⇒ false
882    ]
883  | AJMP arg ⇒
884    match j with
885    [ AJMP arg' ⇒ eq_addressing_mode arg arg'
886    | _ ⇒ false
887    ]
888  | LJMP arg ⇒
889    match j with
890    [ LJMP arg' ⇒ eq_addressing_mode arg arg'
891    | _ ⇒ false
892    ]
893  | SJMP arg ⇒
894    match j with
895    [ SJMP arg' ⇒ eq_addressing_mode arg arg'
896    | _ ⇒ false
897    ]
898  | JMP arg ⇒
899    match j with
900    [ JMP arg' ⇒ eq_addressing_mode arg arg'
901    | _ ⇒ false
902    ]
903  | MOVC arg1 arg2 ⇒
904    match j with
905    [ MOVC arg1' arg2' ⇒ eq_addressing_mode arg1 arg1' ∧ eq_addressing_mode arg2 arg2'
906    | _ ⇒ false
907    ]
908  | RealInstruction instr ⇒
909    match j with
910    [ RealInstruction instr' ⇒ eq_preinstruction instr instr'
911    | _ ⇒ false
912    ]
913  ].
914
915lemma eq_instruction_refl:
916  ∀i. eq_instruction i i = true.
917  #i cases i
919  |7: #arg1 #arg2
920      whd in ⊢ (??%?)
923      %
924  |8: #arg @eq_preinstruction_refl
925  ]
926qed.
927
928let rec vect_member
929  (A: Type[0]) (n: nat) (eq: A → A → bool)
930  (v: Vector A n) (a: A) on v: bool ≝
931  match v with
932  [ VEmpty          ⇒ false
933  | VCons len hd tl ⇒
934    eq hd a ∨ (vect_member A ? eq tl a)
935  ].
936
938  (n: nat)
939  (l: Vector addressing_mode_tag (S n))
940  on l:
941  ∀P: l → Prop.
942  ∀direct_a. ∀indirect_a. ∀ext_indirect_a. ∀register_a. ∀acc_a_a.
943  ∀acc_b_a. ∀dptr_a. ∀data_a. ∀data16_a. ∀acc_dptr_a. ∀acc_pc_a.
946  ∀x: l. P x ≝
947  match l return
948    λy.
949      match y with
950      [ O    ⇒ λm: Vector addressing_mode_tag O. ∀prf: 0 = S n. True
951      | S y' ⇒ λl: Vector addressing_mode_tag (S y'). ∀prf: S y' = S n.∀P:l → Prop.
952               ∀direct_a: if vect_member … eq_a l direct then ∀x. P (DIRECT x) else True.
953               ∀indirect_a: if vect_member … eq_a l indirect then ∀x. P (INDIRECT x) else True.
954               ∀ext_indirect_a: if vect_member … eq_a l ext_indirect then ∀x. P (EXT_INDIRECT x) else True.
955               ∀register_a: if vect_member … eq_a l registr then ∀x. P (REGISTER x) else True.
956               ∀acc_a_a: if vect_member … eq_a l acc_a then P (ACC_A) else True.
957               ∀acc_b_a: if vect_member … eq_a l acc_b then P (ACC_B) else True.
958               ∀dptr_a: if vect_member … eq_a l dptr then P DPTR else True.
959               ∀data_a: if vect_member … eq_a l data then ∀x. P (DATA x) else True.
960               ∀data16_a: if vect_member … eq_a l data16 then ∀x. P (DATA16 x) else True.
961               ∀acc_dptr_a: if vect_member … eq_a l acc_dptr then P ACC_DPTR else True.
962               ∀acc_pc_a: if vect_member … eq_a l acc_pc then P ACC_PC else True.
963               ∀ext_indirect_dptr_a: if vect_member … eq_a l ext_indirect_dptr then P EXT_INDIRECT_DPTR else True.
964               ∀indirect_dptr_a: if vect_member … eq_a l indirect_dptr then P INDIRECT_DPTR else True.
965               ∀carry_a: if vect_member … eq_a l carry then P CARRY else True.
968               ∀relative_a: if vect_member … eq_a l relative then ∀x. P (RELATIVE x) else True.
971               ∀x:l. P x
972      ] with
973  [ VEmpty          ⇒ λAbsurd. ⊥
974  | VCons len hd tl ⇒ λProof. ?
975  ] (refl ? (S n)). cases daemon. qed. (*
976  [ destruct(Absurd)
977  | # A1 # A2 # A3 # A4 # A5 # A6 # A7
978    # A8 # A9 # A10 # A11 # A12 # A13 # A14
979    # A15 # A16 # A17 # A18 # A19 # X
980    cases X
981    # SUB cases daemon ] qed.
982    cases SUB
983    [ # BYTE
984    normalize
985  ].
986
987
988(*    let prepare_hd ≝
989      match hd with
990      [ direct ⇒ λdirect_prf. ?
991      | indirect ⇒ λindirect_prf. ?
992      | ext_indirect ⇒ λext_indirect_prf. ?
993      | registr ⇒ λregistr_prf. ?
994      | acc_a ⇒ λacc_a_prf. ?
995      | acc_b ⇒ λacc_b_prf. ?
996      | dptr ⇒ λdptr_prf. ?
997      | data ⇒ λdata_prf. ?
998      | data16 ⇒ λdata16_prf. ?
999      | acc_dptr ⇒ λacc_dptr_prf. ?
1000      | acc_pc ⇒ λacc_pc_prf. ?
1001      | ext_indirect_dptr ⇒ λext_indirect_prf. ?
1002      | indirect_dptr ⇒ λindirect_prf. ?
1003      | carry ⇒ λcarry_prf. ?
1006      | relative ⇒ λrelative_prf. ?
1009      ]
1010    in ? *)
1011  ].
1012  [ 1: destruct(absd)
1013  | 2: # A1 # A2 # A3 # A4 # A5 # A6
1014       # A7 # A8 # A9 # A10 # A11 # A12
1015       # A13 # A14 # A15 # A16 # A17 # A18
1016       # A19 *
1017  ].
1018
1019
1020  match l return λx.match x with [O ⇒ λl: Vector … O. bool | S x' ⇒ λl: Vector addressing_mode_tag (S x').
1021   (l → bool) → bool ] with
1022  [ VEmpty      ⇒  true
1023  | VCons len hd tl ⇒ λP.
1024    let process_hd ≝
1025      match hd return λhd. ∀P: hd:::tl → bool. bool with
1026      [ direct ⇒ λP.bitvector_elim 8 (λx. P (DIRECT x))
1027      | indirect ⇒ λP.bit_elim (λx. P (INDIRECT x))
1028      | ext_indirect ⇒ λP.bit_elim (λx. P (EXT_INDIRECT x))
1029      | registr ⇒ λP.bitvector_elim 3 (λx. P (REGISTER x))
1030      | acc_a ⇒ λP.P ACC_A
1031      | acc_b ⇒ λP.P ACC_B
1032      | dptr ⇒ λP.P DPTR
1033      | data ⇒ λP.bitvector_elim 8 (λx. P (DATA x))
1034      | data16 ⇒ λP.bitvector_elim 16 (λx. P (DATA16 x))
1035      | acc_dptr ⇒ λP.P ACC_DPTR
1036      | acc_pc ⇒ λP.P ACC_PC
1037      | ext_indirect_dptr ⇒ λP.P EXT_INDIRECT_DPTR
1038      | indirect_dptr ⇒ λP.P INDIRECT_DPTR
1039      | carry ⇒ λP.P CARRY
1042      | relative ⇒ λP.bitvector_elim 8 (λx. P (RELATIVE x))
1045      ]
1046    in
1047      andb (process_hd P)
1048       (match len return λx. x = len → bool with
1049         [ O ⇒ λprf. true
1050         | S y ⇒ λprf. list_addressing_mode_tags_elim y ? P ] (refl ? len))
1051  ].
1052  try %
1053  [ 2: cases (sym_eq ??? prf); @tl
1054  | generalize in match H; generalize in match tl; cases prf;
1055    (* cases prf in tl H; : ??? WAS WORKING BEFORE *)
1056    #tl
1057    normalize in ⊢ (∀_: %. ?)
1058    # H
1059    whd
1060    normalize in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
1061    cases (is_a hd (subaddressing_modeel y tl H)) whd // ]
1062qed.
1063*)
1064
1066 fold_left_i_aux … (
1067   λi, mem, v.
1068     insert … (bitvector_of_nat … i) v mem) (Stub Byte 16).
1069
1070axiom split_elim:
1071 ∀A,l,m,v.∀P: (Vector A l) × (Vector A m) → Prop.
1072  (∀vl,vm. v = vl@@vm → P 〈vl,vm〉) → P (split A l m v).
1073
1075 ∀pc.
1076 \snd (half_add 16 (bitvector_of_nat … pc) (bitvector_of_nat … 1)) = bitvector_of_nat … (S pc).
1077 cases daemon.
1078qed.
1079
1080(*
1081axiom not_eqvb_S:
1082 ∀pc.
1083 (¬eq_bv 16 (bitvector_of_nat 16 pc) (bitvector_of_nat 16 (S pc))).
1084
1085axiom not_eqvb_SS:
1086 ∀pc.
1087 (¬eq_bv 16 (bitvector_of_nat 16 pc) (bitvector_of_nat 16 (S (S pc)))).
1088
1089axiom bitvector_elim_complete:
1090 ∀n,P. bitvector_elim n P = true → ∀bv. P bv.
1091
1092lemma bitvector_elim_complete':
1093 ∀n,P. bitvector_elim n P = true → ∀bv. P bv = true.
1094 #n #P #H generalize in match (bitvector_elim_complete … H) #K #bv
1095 generalize in match (K bv) normalize cases (P bv) normalize // #abs @⊥ //
1096qed.
1097*)
1098
1099(*
1100lemma andb_elim':
1101 ∀b1,b2. (b1 = true) → (b2 = true) → (b1 ∧ b2) = true.
1102 #b1 #b2 #H1 #H2 @andb_elim cases b1 in H1; normalize //
1103qed.
1104*)
1105
1106let rec encoding_check (code_memory: BitVectorTrie Byte 16) (pc: Word) (final_pc: Word)
1107                       (encoding: list Byte) on encoding: Prop ≝
1108  match encoding with
1109  [ nil ⇒ final_pc = pc
1110  | cons hd tl ⇒
1111    let 〈new_pc, byte〉 ≝ next code_memory pc in
1112      hd = byte ∧ encoding_check code_memory new_pc final_pc tl
1113  ].
1114
1115lemma encoding_check_append: ∀code_memory,final_pc,l1,pc,l2.
1116 encoding_check code_memory (bitvector_of_nat … pc) (bitvector_of_nat … final_pc) (l1@l2) →
1117  let intermediate_pc ≝ pc + length … l1 in
1118   encoding_check code_memory (bitvector_of_nat … pc) (bitvector_of_nat … intermediate_pc) l1 ∧
1119    encoding_check code_memory (bitvector_of_nat … intermediate_pc) (bitvector_of_nat … final_pc) l2.
1120 #code_memory #final_pc #l1 elim l1
1121  [ #pc #l2 whd in ⊢ (????% → ?) #H <plus_n_O whd whd in ⊢ (?%?) /2/
1122  | #hd #tl #IH #pc #l2 * #H1 #H2 >half_add_SO in H2; #H2 cases (IH … H2) <plus_n_Sm
1123    #K1 #K2 % [2:@K2] whd % // >half_add_SO @K1 ]
1124qed.
1125
1126axiom bitvector_3_elim_prop:
1127 ∀P: BitVector 3 → Prop.
1128  P [[false;false;false]] → P [[false;false;true]] → P [[false;true;false]] →
1129  P [[false;true;true]] → P [[true;false;false]] → P [[true;false;true]] →
1130  P [[true;true;false]] → P [[true;true;true]] → ∀v. P v.
1131
1132definition ticks_of_instruction ≝
1133 λi.
1134  let trivial_code_memory ≝ assembly1 i in
1135  let trivial_status ≝ load_code_memory trivial_code_memory in
1136   \snd (fetch trivial_status (zero ?)).
1137
1138axiom fetch_assembly:
1139  ∀pc,i,code_memory,assembled.
1140    assembled = assembly1 i →
1141      let len ≝ length … assembled in
1142      encoding_check code_memory (bitvector_of_nat … pc) (bitvector_of_nat … (pc + len)) assembled →
1143      let fetched ≝ fetch code_memory (bitvector_of_nat … pc) in
1144      let 〈instr_pc, ticks〉 ≝ fetched in
1145      let 〈instr,pc'〉 ≝ instr_pc in
1146       (eq_instruction instr i ∧ eqb ticks (ticks_of_instruction instr) ∧ eq_bv … pc' (bitvector_of_nat … (pc + len))) = true.
1147(* #pc #i #code_memory #assembled cases i [8: *]
1148 [16,20,29: * * |18,19: * * [1,2,4,5: *] |28: * * [1,2: * [1,2: * [1,2: * [1,2: *]]]]]
1149 [47,48,49:
1150 |*: #arg @(list_addressing_mode_tags_elim_prop … arg) whd try % -arg
1151  [2,3,5,7,10,12,16,17,18,21,25,26,27,30,31,32,37,38,39,40,41,42,43,44,45,48,51,58,
1152   59,60,63,64,65,66,67: #ARG]]
1153 [4,5,6,7,8,9,10,11,12,13,22,23,24,27,28,39,40,41,42,43,44,45,46,47,48,49,50,51,52,
1154  56,57,69,70,72,73,75: #arg2 @(list_addressing_mode_tags_elim_prop … arg2) whd try % -arg2
1155  [1,2,4,7,9,10,12,13,15,16,17,18,20,22,23,24,25,26,27,28,29,30,31,32,33,36,37,38,
1156   39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,
1157   68,69,70,71: #ARG2]]
1158 [1,2,19,20: #arg3 @(list_addressing_mode_tags_elim_prop … arg3) whd try % -arg3 #ARG3]
1159 normalize in ⊢ (???% → ?)
1160 [92,94,42,93,95: @split_elim #vl #vm #E >E -E; [2,4: @(bitvector_3_elim_prop … vl)]
1161  normalize in ⊢ (???% → ?)]
1162 #H >H * #H1 try (change in ⊢ (% → ?) with (? ∧ ?) * #H2)
1163 try (change in ⊢ (% → ?) with (? ∧ ?) * #H3) whd in ⊢ (% → ?) #H4
1164 change in ⊢ (let fetched ≝ % in ?) with (fetch0 ??)
1165 whd in ⊢ (let fetched ≝ ??% in ?) <H1 whd in ⊢ (let fetched ≝ % in ?)
1166 [17,18,19,20,21,22,23,24,25,26,31,34,35,36,37,38: <H3]
1167 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
1168  30,31,32,33,34,35,36,37,38,39,40,43,45,48,49,52,53,54,55,56,57,60,61,62,65,66,
1169  69,70,73,74,78,80,81,84,85,95,98,101,102,103,104,105,106,107,108,109,110: <H2]
1170 whd >eq_instruction_refl >H4 @eq_bv_refl
1171qed. *)
1172
1173let rec fetch_many code_memory final_pc pc expected on expected: Prop ≝
1174 match expected with
1175  [ nil ⇒ eq_bv … pc final_pc = true
1176  | cons i tl ⇒
1177     let fetched ≝ fetch code_memory pc in
1178     let 〈instr_pc, ticks〉 ≝ fetched in
1179     let 〈instr,pc'〉 ≝ instr_pc in
1180      eq_instruction instr i = true ∧
1181      ticks = (ticks_of_instruction i) ∧
1182      fetch_many code_memory final_pc pc' tl].
1183
1184lemma option_destruct_Some: ∀A,a,b. Some A a = Some A b → a=b.
1185 #A #a #b #EQ destruct //
1186qed.
1187
1188axiom eq_instruction_to_eq:
1189  ∀i1,i2. eq_instruction i1 i2 = true → i1 = i2.
1190
1191
1192notation < "❰ x ❱" with precedence 90 for @{'dp \$x \$y }.
1193interpretation "dp" 'dp x y = (dp x y).
1194
1195lemma fetch_assembly_pseudo':
1196 ∀program.∀pol:policy program.∀ppc,lookup_labels,lookup_datalabels.
1197  ∀pi,code_memory,len,assembled,instructions,pc.
1198   let expansion ≝ pol ppc in
1199   Some ? instructions = expand_pseudo_instruction_safe lookup_labels lookup_datalabels (bitvector_of_nat ? pc) expansion pi →
1200    Some … 〈len,assembled〉 = assembly_1_pseudoinstruction_safe program pol ppc (bitvector_of_nat ? pc) lookup_labels lookup_datalabels pi →
1201     encoding_check code_memory (bitvector_of_nat … pc) (bitvector_of_nat … (pc + len)) assembled →
1202      fetch_many code_memory (bitvector_of_nat … (pc + len)) (bitvector_of_nat … pc) instructions.
1203 #program #pol #ppc #lookup_labels #lookup_datalabels #pi #code_memory #len #assembled #instructions #pc
1204 #EQ1 whd in ⊢ (???% → ?) <EQ1 whd in ⊢ (???% → ?) #EQ2
1205 cases (pair_destruct ?????? (option_destruct_Some … EQ2)) -EQ2; #EQ2a #EQ2b
1206 >EQ2a >EQ2b -EQ2a EQ2b;
1207  generalize in match (pc + |flatten … (map … assembly1 instructions)|); #final_pc
1208  generalize in match pc elim instructions
1209  [ #pc whd in ⊢ (% → %) #H >H @eq_bv_refl
1210  | #i #tl #IH #pc #H whd cases (encoding_check_append … H); -H; #H1 #H2 whd
1211    generalize in match (fetch_assembly pc i code_memory … (refl …) H1)
1212    cases (fetch code_memory (bitvector_of_nat … pc)) #newi_pc #ticks whd in ⊢ (% → %)
1213    cases newi_pc #newi #newpc whd in ⊢ (% → %) #K cases (conjunction_true … K) -K; #K1
1214    cases (conjunction_true … K1) -K1; #K1 #K2 #K3 % try %
1215    [ @K1 | @eqb_true_to_eq <(eq_instruction_to_eq … K1) @K2 | >(eq_bv_eq … K3) @IH @H2 ]
1216qed.
1217
1218axiom bitvector_of_nat_nat_of_bitvector:
1219  ∀n,v.
1220    bitvector_of_nat n (nat_of_bitvector n v) = v.
1221
1222(* CSC: soo long next one; can we merge with previous one now? *)
1223lemma fetch_assembly_pseudo:
1224 ∀program.∀pol:policy program.∀ppc.
1225  ∀code_memory.
1226   let lookup_labels ≝ ? in
1227   let lookup_datalabels ≝ ? in
1228   let pc ≝ ? in
1229   let pi ≝  \fst  (fetch_pseudo_instruction (\snd  program) ppc) in
1230   let instructions ≝ expand_pseudo_instruction program pol ppc lookup_labels lookup_datalabels pc (refl …) (refl …) (refl …) in
1231   let 〈len,assembled〉 ≝ assembly_1_pseudoinstruction program pol ppc lookup_labels lookup_datalabels pi (refl …) (refl …) (refl …) in
1232     encoding_check code_memory pc (bitvector_of_nat … (nat_of_bitvector ? pc + len)) assembled →
1233      fetch_many code_memory (bitvector_of_nat … (nat_of_bitvector ? pc + len)) pc instructions.
1234 #program #pol #ppc #code_memory
1235 letin lookup_labels ≝ (λx:Identifier
1236    .sigma program pol (address_of_word_labels_code_mem (\snd  program) x))
1237 letin lookup_datalabels ≝
1238  (λx:BitVector 16
1239    .lookup Identifier 16 x (construct_datalabels (\fst  program)) (zero 16))
1240 letin pc ≝ (sigma program pol ppc)
1241 letin pi ≝ (\fst  (fetch_pseudo_instruction (\snd  program) ppc))
1242 letin instructions ≝ (expand_pseudo_instruction program pol ppc lookup_labels lookup_datalabels pc (refl …) (refl …) (refl …))
1243 @pair_elim' #len #assembled #EQ1 #H
1244 generalize in match
1245  (fetch_assembly_pseudo' program pol ppc lookup_labels lookup_datalabels pi
1246  code_memory len assembled instructions (nat_of_bitvector ? pc) ???) in ⊢ ?;
1247 [ >bitvector_of_nat_nat_of_bitvector //
1248 | >bitvector_of_nat_nat_of_bitvector normalize nodelta >(sig2 ?? (expand_pseudo_instruction …)) %
1249 | >bitvector_of_nat_nat_of_bitvector <EQ1 @assembly_1_pseudoinstruction_ok1
1250 | //]
1251qed.
1252
1253axiom assembly_ok:
1254 ∀program,pol,assembled.
1255  let 〈labels,costs〉 ≝ build_maps program pol in
1256  〈assembled,costs〉 = assembly program pol →
1257  let code_memory ≝ load_code_memory assembled in
1258  let preamble ≝ \fst program in
1259  let datalabels ≝ construct_datalabels preamble in
1260  let lookup_labels ≝ λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x) in
1261  let lookup_datalabels ≝ λx. lookup ?? x datalabels (zero ?) in
1262  ∀ppc.
1263  let pi_newppc ≝ fetch_pseudo_instruction (\snd program) ppc in
1264   ∀len,assembledi.
1265     〈len,assembledi〉 = assembly_1_pseudoinstruction program pol ppc lookup_labels lookup_datalabels (\fst pi_newppc) (refl …) (refl …) (refl …) →
1266      encoding_check code_memory (sigma program pol ppc) (bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len)) assembledi ∧
1267       sigma program pol (\snd pi_newppc) = bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len).
1268
1269axiom assembly_ok_to_expand_pseudo_instruction_ok:
1270 ∀program,pol,assembled,costs.
1271  〈assembled,costs〉 = assembly program pol →
1272   ∀ppc.
1273    let code_memory ≝ load_code_memory assembled in
1274    let preamble ≝ \fst program in
1275    let data_labels ≝ construct_datalabels preamble in
1276    let lookup_labels ≝ λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x) in
1277    let lookup_datalabels ≝ λx. lookup ? ? x data_labels (zero ?) in
1278    let expansion ≝ pol ppc in
1279    let 〈pi,newppc〉 ≝ fetch_pseudo_instruction (\snd program) ppc in
1280     ∃instructions.
1281      Some ? instructions = expand_pseudo_instruction_safe lookup_labels lookup_datalabels (sigma program pol ppc) expansion pi.
1282
1283(*CSC: repair me!*)
1284axiom fetch_assembly_pseudo2:
1285 ∀program,pol,ppc.
1286(*  let 〈labels,costs〉 ≝ build_maps program pol in *)
1287  let assembled ≝ \fst (assembly program pol) in
1288  let code_memory ≝ load_code_memory assembled in
1289  let data_labels ≝ construct_datalabels (\fst program) in
1290  let lookup_labels ≝ λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x) in
1291  let lookup_datalabels ≝ λx. lookup ? ? x data_labels (zero ?) in
1292  let 〈pi,newppc〉 ≝ fetch_pseudo_instruction (\snd program) ppc in
1293  let instructions ≝ expand_pseudo_instruction program pol ppc lookup_labels lookup_datalabels (sigma program pol ppc) (refl …) (refl …) (refl …) in
1294   fetch_many code_memory (sigma program pol newppc) (sigma program pol ppc) instructions.
1295(*
1296 #program #pol #assembled generalize in match (assembly_ok program pol assembled)
1297 @pair_elim' #labels #costs #BUILD_MAPS
1298 #H #ASSEMBLY #ppc #pi #newppc
1299 letin code_memory ≝ (load_code_memory assembled)
1300 letin preamble ≝ (\fst program)
1301 letin data_labels ≝ (construct_datalabels preamble)
1302 letin lookup_labels ≝ (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x))
1303 letin lookup_datalabels ≝ (λx. lookup ? ? x data_labels (zero ?))
1304 normalize nodelta #EQ generalize in match (H ASSEMBLY ppc) -H;
1305 generalize in match (fetch_assembly_pseudo program pol ppc code_memory) in ⊢ ? normalize nodelta
1306 @pair_elim' #len #assembledi #ASSEMBLE1 #H1 #H2
1307 generalize in match (H1 ?) [2: cases (H2 len assembledi (refl …)) #H1 #_ @H1]
1308 >bitvector_of_nat_nat_of_bitvector
1309  #K
1310
1311
1312 cases
1313  (assembly_1_pseudoinstruction program pol ppc lookup_labels lookup_datalabels ????)
1314 @split_elim' #len #assembledi #ASSEMBLY1
1315
1316  @pair_elim'
1317
1318 normalize nodelta;
1319 whd in ⊢ (% → %) generalize in match (assembly_ok program pol assembled) >BUILD_MAPS #XX generalize in match (XX ASSEMBLY ppc) -XX;
1320 cases (fetch_pseudo_instruction (\snd program) ppc) #pi #newppc
1321 generalize in match (fetch_assembly_pseudo program pol ppc
1322  (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) (λx. lookup ?? x data_labels (zero ?)) pi
1324 whd in ⊢ ((∀_.∀_.∀_.∀_.%) → (∀_.∀_.%) → % → %) #H1 #H2 * #instructions #EXPAND
1325 whd in H1:(∀_.∀_.∀_.∀_.? → ???% → ?) H2:(∀_.∀_.???% → ?);
1326 normalize nodelta in EXPAND; (* HERE *)
1327 generalize in match (λlen, assembled.H1 len assembled instructions (nat_of_bitvector … (sigma program pol ppc))) -H1; #H1
1328 >bitvector_of_nat_nat_of_bitvector in H1; #H1
1329 <EXPAND in H1 H2; whd in ⊢ ((∀_.∀_.? → ???% → ?) → (∀_.∀_.???% → ?) → ?)
1330 #H1 #H2
1331 cases (H2 ?? (refl …)) -H2; #K1 #K2 >K2
1332 generalize in match (H1 ?? (refl …) (refl …) ?) -H1;
1333  [ #K3 % [2: % [% | @K3]] | @K1 ]
1334qed.
1335*)
1336
1337(*
1338lemma fetch_assembly_pseudo2:
1339 ∀program,pol,assembled.
1340  let 〈labels,costs〉 ≝ build_maps program pol in
1341  〈assembled,costs〉 = assembly program pol →
1342   ∀ppc.
1343    let code_memory ≝ load_code_memory assembled in
1344    let preamble ≝ \fst program in
1345    let data_labels ≝ construct_datalabels preamble in
1346    let lookup_labels ≝ λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x) in
1347    let lookup_datalabels ≝ λx. lookup ? ? x data_labels (zero ?) in
1348    let expansion ≝ pol ppc in
1349    let 〈pi,newppc〉 ≝ fetch_pseudo_instruction (\snd program) ppc in
1350     ∃instructions.
1351      Some ? instructions = expand_pseudo_instruction_safe lookup_labels lookup_datalabels (sigma program pol ppc) expansion pi ∧
1352       fetch_many code_memory (sigma program pol newppc) (sigma program pol ppc) instructions.
1353 #program #pol #assembled @pair_elim' #labels #costs #BUILD_MAPS #ASSEMBLY #ppc
1354 generalize in match (assembly_ok_to_expand_pseudo_instruction_ok program pol assembled costs ASSEMBLY ppc)
1355 letin code_memory ≝ (load_code_memory assembled)
1356 letin preamble ≝ (\fst program)
1357 letin data_labels ≝ (construct_datalabels preamble)
1358 letin lookup_labels ≝ (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x))
1359 letin lookup_datalabels ≝ (λx. lookup ? ? x data_labels (zero ?))
1360 whd in ⊢ (% → %) generalize in match (assembly_ok program pol assembled) >BUILD_MAPS #XX generalize in match (XX ASSEMBLY ppc) -XX;
1361 cases (fetch_pseudo_instruction (\snd program) ppc) #pi #newppc
1362 generalize in match (fetch_assembly_pseudo program pol ppc
1363  (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) (λx. lookup ?? x data_labels (zero ?))
1365 whd in ⊢ ((∀_.∀_.∀_.∀_.%) → (∀_.∀_.%) → % → %) #H1 #H2 * #instructions #EXPAND
1366 whd in H1:(∀_.∀_.∀_.∀_.? → ???% → ?) H2:(∀_.∀_.???% → ?);
1367 normalize nodelta in EXPAND; (* HERE *)
1368 generalize in match (λlen, assembled.H1 len assembled instructions (nat_of_bitvector … (sigma program pol ppc))) -H1; #H1
1369 >bitvector_of_nat_nat_of_bitvector in H1; #H1
1370 <EXPAND in H1 H2; whd in ⊢ ((∀_.∀_.? → ???% → ?) → (∀_.∀_.???% → ?) → ?)
1371 #H1 #H2
1372 cases (H2 ?? (refl …)) -H2; #K1 #K2 >K2
1373 generalize in match (H1 ?? (refl …) (refl …) ?) -H1;
1374  [ #K3 % [2: % [% | @K3]] | @K1 ]
1375qed.
1376*)
1377
1378(* OLD?
1379definition assembly_specification:
1380  ∀assembly_program: pseudo_assembly_program.
1381  ∀code_mem: BitVectorTrie Byte 16. Prop ≝
1382  λpseudo_assembly_program.
1383  λcode_mem.
1384    ∀pc: Word.
1385      let 〈preamble, instr_list〉 ≝ pseudo_assembly_program in
1386      let 〈pre_instr, pre_new_pc〉 ≝ fetch_pseudo_instruction instr_list pc in
1387      let labels ≝ λx. sigma' pseudo_assembly_program (address_of_word_labels_code_mem instr_list x) in
1388      let datalabels ≝ λx. sigma' pseudo_assembly_program (lookup ? ? x (construct_datalabels preamble) (zero ?)) in
1389      let pre_assembled ≝ assembly_1_pseudoinstruction pseudo_assembly_program
1390       (sigma' pseudo_assembly_program pc) labels datalabels pre_instr in
1391      match pre_assembled with
1392       [ None ⇒ True
1393       | Some pc_code ⇒
1394          let 〈new_pc,code〉 ≝ pc_code in
1395           encoding_check code_mem pc (sigma' pseudo_assembly_program pre_new_pc) code ].
1396
1397axiom assembly_meets_specification:
1398  ∀pseudo_assembly_program.
1399    match assembly pseudo_assembly_program with
1400    [ None ⇒ True
1401    | Some code_mem_cost ⇒
1402      let 〈code_mem, cost〉 ≝ code_mem_cost in
1404    ].
1405(*
1406  # PROGRAM
1407  [ cases PROGRAM
1408    # PREAMBLE
1409    # INSTR_LIST
1410    elim INSTR_LIST
1411    [ whd
1412      whd in ⊢ (∀_. %)
1413      # PC
1414      whd
1415    | # INSTR
1416      # INSTR_LIST_TL
1417      # H
1418      whd
1419      whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
1420    ]
1421  | cases not_implemented
1422  ] *)
1423*)
1424
1425definition internal_pseudo_address_map ≝ list (BitVector 8).
1426
1427axiom low_internal_ram_of_pseudo_low_internal_ram:
1428 ∀M:internal_pseudo_address_map.∀ram:BitVectorTrie Byte 7.BitVectorTrie Byte 7.
1429
1430axiom high_internal_ram_of_pseudo_high_internal_ram:
1431 ∀M:internal_pseudo_address_map.∀ram:BitVectorTrie Byte 7.BitVectorTrie Byte 7.
1432
1433axiom low_internal_ram_of_pseudo_internal_ram_hit:
1435  member ? (eq_bv 8) (false:::addr) M = true →
1436   let ram ≝ low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … s) in
1437   let pbl ≝ lookup ? 7 addr (low_internal_ram ? s) (zero 8) in
1438   let pbu ≝ lookup ? 7 (\snd (half_add ? addr (bitvector_of_nat 7 1))) (low_internal_ram ? s) (zero 8) in
1439   let bl ≝ lookup ? 7 addr ram (zero 8) in
1440   let bu ≝ lookup ? 7 (\snd (half_add ? addr (bitvector_of_nat 7 1))) ram (zero 8) in
1441    bu@@bl = sigma (code_memory … s) pol (pbu@@pbl).
1442
1443(* changed from add to sub *)
1444axiom low_internal_ram_of_pseudo_internal_ram_miss:
1446  let ram ≝ low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … s) in
1448  let carr ≝ get_index_v ? ? flags 1 ? in
1449  carr = false →
1450  member ? (eq_bv 8) (false:::Saddr) M = false →
1451   member ? (eq_bv 8) (false:::addr) M = false →
1452    lookup ? 7 addr ram (zero ?) = lookup ? 7 addr (low_internal_ram … s) (zero ?).
1453  //
1454qed.
1455
1460    [ DIRECT d ⇒
1461       ¬(member ? (eq_bv 8) d M) ∧
1462       ¬(member ? (eq_bv 8) (\fst (sub_8_with_carry d (bitvector_of_nat 8 1) false)) M)
1463    | INDIRECT i ⇒
1464       let d ≝ get_register ? s [[false;false;i]] in
1465       ¬(member ? (eq_bv 8) d M) ∧
1466       ¬(member ? (eq_bv 8) (\fst (sub_8_with_carry d (bitvector_of_nat 8 1) false)) M)
1467    | EXT_INDIRECT _ ⇒ true
1468    | REGISTER _ ⇒ true
1469    | ACC_A ⇒ true
1470    | ACC_B ⇒ true
1471    | DPTR ⇒ true
1472    | DATA _ ⇒ true
1473    | DATA16 _ ⇒ true
1474    | ACC_DPTR ⇒ true
1475    | ACC_PC ⇒ true
1476    | EXT_INDIRECT_DPTR ⇒ true
1477    | INDIRECT_DPTR ⇒ true
1478    | CARRY ⇒ true
1479    | BIT_ADDR _ ⇒ ¬true (* TO BE COMPLETED *)
1480    | N_BIT_ADDR _ ⇒ ¬true (* TO BE COMPLETED *)
1481    | RELATIVE _ ⇒ true
1482    | ADDR11 _ ⇒ true
1483    | ADDR16 _ ⇒ true ].
1484
1486  λT.
1487  λfetched.
1489  λs: PreStatus T.
1490   match fetched with
1491    [ Comment _ ⇒ Some ? M
1492    | Cost _ ⇒ Some … M
1493    | Jmp _ ⇒ Some … M
1494    | Call _ ⇒
1495       Some … (\snd (half_add ? (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1))::M)
1496    | Mov _ _ ⇒ Some … M
1497    | Instruction instr ⇒
1498       match instr with
1501            Some ? M
1502           else
1503            None ?
1506            Some ? M
1507           else
1508            None ?
1511            Some ? M
1512           else
1513            None ?
1514        | _ ⇒ (* TO BE COMPLETED *) Some ? M ]].
1515
1516
1519  λs:PseudoStatus.
1521     (\fst (fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s))) M s.
1522
1523definition status_of_pseudo_status:
1524 internal_pseudo_address_map → ∀ps:PseudoStatus. policy (code_memory … ps) → Status ≝
1525 λM,ps,pol.
1526  let pap ≝ code_memory … ps in
1527  let p ≝ assembly pap pol in
1528  let cm ≝ load_code_memory (\fst p) in
1529  let pc ≝ sigma pap pol (program_counter ? ps) in
1530  let lir ≝ low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … ps) in
1531  let hir ≝ high_internal_ram_of_pseudo_high_internal_ram M (high_internal_ram … ps) in
1532     mk_PreStatus (BitVectorTrie Byte 16)
1533      cm
1534      lir
1535      hir
1536      (external_ram … ps)
1537      pc
1538      (special_function_registers_8051 … ps)
1539      (special_function_registers_8052 … ps)
1540      (p1_latch … ps)
1541      (p3_latch … ps)
1542      (clock … ps).
1543
1544(*
1545definition write_at_stack_pointer':
1546 ∀M. ∀ps: PreStatus M. Byte → Σps':PreStatus M.(code_memory … ps = code_memory … ps') ≝
1547  λM: Type[0].
1548  λs: PreStatus M.
1549  λv: Byte.
1550    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in
1551    let bit_zero ≝ get_index_v… nu O ? in
1552    let bit_1 ≝ get_index_v… nu 1 ? in
1553    let bit_2 ≝ get_index_v… nu 2 ? in
1554    let bit_3 ≝ get_index_v… nu 3 ? in
1555      if bit_zero then
1556        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
1557                              v (low_internal_ram ? s) in
1558          set_low_internal_ram ? s memory
1559      else
1560        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
1561                              v (high_internal_ram ? s) in
1562          set_high_internal_ram ? s memory.
1563  [ cases l0 %
1564  |2,3,4,5: normalize repeat (@ le_S_S) @ le_O_n ]
1565qed.
1566
1567definition execute_1_pseudo_instruction': (Word → nat) → ∀ps:PseudoStatus.
1568 Σps':PseudoStatus.(code_memory … ps = code_memory … ps')
1569
1570  λticks_of.
1571  λs.
1572  let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s) in
1573  let ticks ≝ ticks_of (program_counter ? s) in
1574  let s ≝ set_clock ? s (clock ? s + ticks) in
1575  let s ≝ set_program_counter ? s pc in
1576    match instr with
1577    [ Instruction instr ⇒
1578       execute_1_preinstruction … (λx, y. address_of_word_labels y x) instr s
1579    | Comment cmt ⇒ s
1580    | Cost cst ⇒ s
1581    | Jmp jmp ⇒ set_program_counter ? s (address_of_word_labels s jmp)
1582    | Call call ⇒
1583      let a ≝ address_of_word_labels s call in
1584      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
1585      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
1586      let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter ? s) in
1587      let s ≝ write_at_stack_pointer' ? s pc_bl in
1588      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
1589      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
1590      let s ≝ write_at_stack_pointer' ? s pc_bu in
1591        set_program_counter ? s a
1592    | Mov dptr ident ⇒
1593       set_arg_16 ? s (get_arg_16 ? s (DATA16 (address_of_word_labels s ident))) dptr
1594    ].
1595 [
1596 |2,3,4: %
1597 | <(sig2 … l7) whd in ⊢ (??? (??%)) <(sig2 … l5) %
1598 |
1599 | %
1600 ]
1601 cases not_implemented
1602qed.
1603*)
1604
1605axiom execute_1_pseudo_instruction_preserves_code_memory:
1606 ∀ticks_of,ps.
1607  code_memory … (execute_1_pseudo_instruction ticks_of ps) = code_memory … ps.
1608
1609(*
1610lemma execute_code_memory_unchanged:
1611 ∀ticks_of,ps. code_memory ? ps = code_memory ? (execute_1_pseudo_instruction ticks_of ps).
1612 #ticks #ps whd in ⊢ (??? (??%))
1613 cases (fetch_pseudo_instruction (\snd (code_memory pseudo_assembly_program ps))
1614  (program_counter pseudo_assembly_program ps)) #instr #pc
1615 whd in ⊢ (??? (??%)) cases instr
1616  [ #pre cases pre
1617     [ #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1618       cases (split ????) #z1 #z2 %
1619     | #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1620       cases (split ????) #z1 #z2 %
1621     | #a1 #a2 whd in ⊢ (??? (??%)) cases (sub_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1622       cases (split ????) #z1 #z2 %
1623     | #a1 whd in ⊢ (??? (??%)) cases a1 #x #H whd in ⊢ (??? (??%)) cases x
1624       [ #x1 whd in ⊢ (??? (??%))
1625     | *: cases not_implemented
1626     ]
1627  | #comment %
1628  | #cost %
1629  | #label %
1630  | #label whd in ⊢ (??? (??%)) cases (half_add ???) #x1 #x2 whd in ⊢ (??? (??%))
1631    cases (split ????) #y1 #y2 whd in ⊢ (??? (??%)) cases (half_add ???) #z1 #z2
1632    whd in ⊢ (??? (??%)) whd in ⊢ (??? (??%)) cases (split ????) #w1 #w2
1633    whd in ⊢ (??? (??%)) cases (get_index_v bool ????) whd in ⊢ (??? (??%))
1634    (* CSC: ??? *)
1635  | #dptr #label (* CSC: ??? *)
1636  ]
1637  cases not_implemented
1638qed.
1639*)
1640
1642lemma status_of_pseudo_status_failure_depends_only_on_code_memory:
1644 ∀ps,ps': PseudoStatus.
1645 ∀pol.
1646  ∀prf:code_memory … ps = code_memory … ps'.
1647   let pol' ≝ ? in
1648   match status_of_pseudo_status M ps pol with
1649    [ None ⇒ status_of_pseudo_status M ps' pol' = None …
1650    | Some _ ⇒ ∃w. status_of_pseudo_status M ps' pol' = Some … w
1651    ].
1652 [2: <prf @pol]
1653 #M #ps #ps' #pol #H normalize nodelta; whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
1654 generalize in match (refl … (assembly (code_memory … ps) pol))
1655 cases (assembly ??) in ⊢ (???% → %)
1656  [ #K whd whd in ⊢ (??%?) <H >K %
1657  | #x #K whd whd in ⊢ (?? (λ_.??%?)) <H >K % [2: % ] ]
1658qed.
1659*)
1660
1661definition ticks_of0: ∀p:pseudo_assembly_program. policy p → Word → ? → nat × nat ≝
1662  λprogram: pseudo_assembly_program.λpol.
1663  λppc: Word.
1664  λfetched.
1665    match fetched with
1666    [ Instruction instr ⇒
1667      match instr with
1668      [ JC lbl ⇒
1669        match pol ppc with
1670        [ short_jump ⇒ 〈2, 2〉
1671        | medium_jump ⇒ ?
1672        | long_jump ⇒ 〈4, 4〉
1673        ]
1674      | JNC lbl ⇒
1675        match pol ppc with
1676        [ short_jump ⇒ 〈2, 2〉
1677        | medium_jump ⇒ ?
1678        | long_jump ⇒ 〈4, 4〉
1679        ]
1680      | JB bit lbl ⇒
1681        match pol ppc with
1682        [ short_jump ⇒ 〈2, 2〉
1683        | medium_jump ⇒ ?
1684        | long_jump ⇒ 〈4, 4〉
1685        ]
1686      | JNB bit lbl ⇒
1687        match pol ppc with
1688        [ short_jump ⇒ 〈2, 2〉
1689        | medium_jump ⇒ ?
1690        | long_jump ⇒ 〈4, 4〉
1691        ]
1692      | JBC bit lbl ⇒
1693        match pol ppc with
1694        [ short_jump ⇒ 〈2, 2〉
1695        | medium_jump ⇒ ?
1696        | long_jump ⇒ 〈4, 4〉
1697        ]
1698      | JZ lbl ⇒
1699        match pol ppc with
1700        [ short_jump ⇒ 〈2, 2〉
1701        | medium_jump ⇒ ?
1702        | long_jump ⇒ 〈4, 4〉
1703        ]
1704      | JNZ lbl ⇒
1705        match pol ppc with
1706        [ short_jump ⇒ 〈2, 2〉
1707        | medium_jump ⇒ ?
1708        | long_jump ⇒ 〈4, 4〉
1709        ]
1710      | CJNE arg lbl ⇒
1711        match pol ppc with
1712        [ short_jump ⇒ 〈2, 2〉
1713        | medium_jump ⇒ ?
1714        | long_jump ⇒ 〈4, 4〉
1715        ]
1716      | DJNZ arg lbl ⇒
1717        match pol ppc with
1718        [ short_jump ⇒ 〈2, 2〉
1719        | medium_jump ⇒ ?
1720        | long_jump ⇒ 〈4, 4〉
1721        ]
1722      | ADD arg1 arg2 ⇒
1723        let ticks ≝ ticks_of_instruction (ADD ? arg1 arg2) in
1724         〈ticks, ticks〉
1725      | ADDC arg1 arg2 ⇒
1726        let ticks ≝ ticks_of_instruction (ADDC ? arg1 arg2) in
1727         〈ticks, ticks〉
1728      | SUBB arg1 arg2 ⇒
1729        let ticks ≝ ticks_of_instruction (SUBB ? arg1 arg2) in
1730         〈ticks, ticks〉
1731      | INC arg ⇒
1732        let ticks ≝ ticks_of_instruction (INC ? arg) in
1733         〈ticks, ticks〉
1734      | DEC arg ⇒
1735        let ticks ≝ ticks_of_instruction (DEC ? arg) in
1736         〈ticks, ticks〉
1737      | MUL arg1 arg2 ⇒
1738        let ticks ≝ ticks_of_instruction (MUL ? arg1 arg2) in
1739         〈ticks, ticks〉
1740      | DIV arg1 arg2 ⇒
1741        let ticks ≝ ticks_of_instruction (DIV ? arg1 arg2) in
1742         〈ticks, ticks〉
1743      | DA arg ⇒
1744        let ticks ≝ ticks_of_instruction (DA ? arg) in
1745         〈ticks, ticks〉
1746      | ANL arg ⇒
1747        let ticks ≝ ticks_of_instruction (ANL ? arg) in
1748         〈ticks, ticks〉
1749      | ORL arg ⇒
1750        let ticks ≝ ticks_of_instruction (ORL ? arg) in
1751         〈ticks, ticks〉
1752      | XRL arg ⇒
1753        let ticks ≝ ticks_of_instruction (XRL ? arg) in
1754         〈ticks, ticks〉
1755      | CLR arg ⇒
1756        let ticks ≝ ticks_of_instruction (CLR ? arg) in
1757         〈ticks, ticks〉
1758      | CPL arg ⇒
1759        let ticks ≝ ticks_of_instruction (CPL ? arg) in
1760         〈ticks, ticks〉
1761      | RL arg ⇒
1762        let ticks ≝ ticks_of_instruction (RL ? arg) in
1763         〈ticks, ticks〉
1764      | RLC arg ⇒
1765        let ticks ≝ ticks_of_instruction (RLC ? arg) in
1766         〈ticks, ticks〉
1767      | RR arg ⇒
1768        let ticks ≝ ticks_of_instruction (RR ? arg) in
1769         〈ticks, ticks〉
1770      | RRC arg ⇒
1771        let ticks ≝ ticks_of_instruction (RRC ? arg) in
1772         〈ticks, ticks〉
1773      | SWAP arg ⇒
1774        let ticks ≝ ticks_of_instruction (SWAP ? arg) in
1775         〈ticks, ticks〉
1776      | MOV arg ⇒
1777        let ticks ≝ ticks_of_instruction (MOV ? arg) in
1778         〈ticks, ticks〉
1779      | MOVX arg ⇒
1780        let ticks ≝ ticks_of_instruction (MOVX ? arg) in
1781         〈ticks, ticks〉
1782      | SETB arg ⇒
1783        let ticks ≝ ticks_of_instruction (SETB ? arg) in
1784         〈ticks, ticks〉
1785      | PUSH arg ⇒
1786        let ticks ≝ ticks_of_instruction (PUSH ? arg) in
1787         〈ticks, ticks〉
1788      | POP arg ⇒
1789        let ticks ≝ ticks_of_instruction (POP ? arg) in
1790         〈ticks, ticks〉
1791      | XCH arg1 arg2 ⇒
1792        let ticks ≝ ticks_of_instruction (XCH ? arg1 arg2) in
1793         〈ticks, ticks〉
1794      | XCHD arg1 arg2 ⇒
1795        let ticks ≝ ticks_of_instruction (XCHD ? arg1 arg2) in
1796         〈ticks, ticks〉
1797      | RET ⇒
1798        let ticks ≝ ticks_of_instruction (RET ?) in
1799         〈ticks, ticks〉
1800      | RETI ⇒
1801        let ticks ≝ ticks_of_instruction (RETI ?) in
1802         〈ticks, ticks〉
1803      | NOP ⇒
1804        let ticks ≝ ticks_of_instruction (NOP ?) in
1805         〈ticks, ticks〉
1806      ]
1807    | Comment comment ⇒ 〈0, 0〉
1808    | Cost cost ⇒ 〈0, 0〉
1809    | Jmp jmp ⇒ 〈2, 2〉
1810    | Call call ⇒ 〈2, 2〉
1811    | Mov dptr tgt ⇒ 〈2, 2〉
1812    ].
1813  cases not_implemented (* policy returned medium_jump for conditional jumping = impossible *)
1814qed.
1815
1816definition ticks_of: ∀p:pseudo_assembly_program. policy p → Word → nat × nat ≝
1817  λprogram: pseudo_assembly_program.λpol.
1818  λppc: Word.
1819    let 〈preamble, pseudo〉 ≝ program in
1820    let 〈fetched, new_ppc〉 ≝ fetch_pseudo_instruction pseudo ppc in
1821     ticks_of0 program pol ppc fetched.
1822
1823lemma eq_rect_Type1_r:
1824  ∀A: Type[1].
1825  ∀a:A.
1826  ∀P: ∀x:A. eq ? x a → Type[1]. P a (refl A a) → ∀x: A.∀p:eq ? x a. P x p.
1827  #A #a #P #H #x #p
1828  generalize in match H
1829  generalize in match P
1830  cases p
1831  //
1832qed.
1833
1834axiom split_append:
1835  ∀A: Type[0].
1836  ∀m, n: nat.
1837  ∀v, v': Vector A m.
1838  ∀q, q': Vector A n.
1839    let 〈v', q'〉 ≝ split A m n (v@@q) in
1840      v = v' ∧ q = q'.
1841
1842axiom split_vector_singleton:
1843  ∀A: Type[0].
1844  ∀n: nat.
1845  ∀v: Vector A (S n).
1846  ∀rest: Vector A n.
1847  ∀s: Vector A 1.
1848  ∀prf.
1849    v = s @@ rest →
1850    ((get_index_v A ? v 0 prf) ::: rest) = v.
1851
1852example sub_minus_one_seven_eight:
1853  ∀v: BitVector 7.
1854  false ::: (\fst (sub_7_with_carry v (bitvector_of_nat ? 1) false)) =
1855  \fst (sub_8_with_carry (false ::: v) (bitvector_of_nat ? 1) false).
1856 cases daemon.
1857qed.
1858
1859(*
1860lemma blah:
1862  ∀s: PseudoStatus.
1863  ∀arg: Byte.
1864  ∀b: bool.
1865    addressing_mode_ok m s (DIRECT arg) = true →
1866      get_arg_8 ? (set_low_internal_ram ? s (low_internal_ram_of_pseudo_low_internal_ram m (low_internal_ram ? s))) b (DIRECT arg) =
1867      get_arg_8 ? s b (DIRECT arg).
1868  [2, 3: normalize % ]
1869  #m #s #arg #b #hyp
1870  whd in ⊢ (??%%)
1871  @split_elim''
1872  #nu' #nl' #arg_nu_nl_eq
1873  normalize nodelta
1874  generalize in match (refl ? (get_index_v bool 4 nu' ? ?))
1875  cases (get_index_v bool 4 nu' ? ?) in ⊢ (??%? → %)
1876  #get_index_v_eq
1877  normalize nodelta
1878  [2:
1879    normalize nodelta
1880    @split_elim''
1881    #bit_one' #three_bits' #bit_one_three_bit_eq
1882    generalize in match (low_internal_ram_of_pseudo_internal_ram_miss m s (three_bits'@@nl'))
1883    normalize nodelta
1884    generalize in match (refl ? (sub_7_with_carry ? ? ?))
1885    cases (sub_7_with_carry ? ? ?) in ⊢ (??%? → %)
1887    normalize nodelta
1888    #carr_hyp'
1889    @carr_hyp'
1890    [1:
1891    |2: whd in hyp:(??%?); generalize in match hyp; -hyp;
1892        generalize in match (refl ? (¬(member (BitVector 8) ? arg m)))
1893        cases (¬(member (BitVector 8) ? arg m)) in ⊢ (??%? → %)
1894        #member_eq
1895        normalize nodelta
1896        [2: #destr destruct(destr)
1897        |1: -carr_hyp';
1898            >arg_nu_nl_eq
1899            <(split_vector_singleton ? ? nu' ? ? ? bit_one_three_bit_eq)
1900            [1: >get_index_v_eq in ⊢ (??%? → ?)
1901            |2: @le_S @le_S @le_S @le_n
1902            ]
1903            cases (member (BitVector 8) ? (\fst ?) ?)
1904            [1: #destr normalize in destr; destruct(destr)
1905            |2:
1906            ]
1907        ]
1908    |3: >get_index_v_eq in ⊢ (??%?)
1909        change in ⊢ (??(???%?)?) with ((? ::: three_bits') @@ nl')
1910        >(split_vector_singleton … bit_one_three_bit_eq)
1911        <arg_nu_nl_eq
1912        whd in hyp:(??%?)
1913        cases (member (BitVector 8) (eq_bv 8) arg m) in hyp
1914        normalize nodelta [*: #ignore @sym_eq ]
1915    ]
1916  |
1917  ].
1918*)
1919(*
1920map_address0 ... (DIRECT arg) = Some .. →
1921  get_arg_8 (map_address0 ... (internal_ram ...) (DIRECT arg) =
1922  get_arg_8 (internal_ram ...) (DIRECT arg)
1923
1928*)
1929
1930axiom low_internal_ram_write_at_stack_pointer:
1931 ∀T1,T2,M,s1,s2,s3.∀pol.∀pbu,pbl,bu,bl,sp1,sp2:BitVector 8.
1932  get_8051_sfr ? s2 SFR_SP = get_8051_sfr ? s3 SFR_SP →
1933  low_internal_ram ? s2 = low_internal_ram T2 s3 →
1934  sp1 = \snd (half_add ? (get_8051_sfr ? s1 SFR_SP) (bitvector_of_nat 8 1)) →
1935  sp2 = \snd (half_add ? sp1 (bitvector_of_nat 8 1)) →
1936  bu@@bl = sigma (code_memory … s2) pol (pbu@@pbl) →
1937   low_internal_ram T1
1938     (write_at_stack_pointer ?
1939       (set_8051_sfr ?
1940         (write_at_stack_pointer ?
1941           (set_8051_sfr ?
1942             (set_low_internal_ram ? s1
1943               (low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram ? s2)))
1944             SFR_SP sp1)
1945          bl)
1946        SFR_SP sp2)
1947      bu)
1948   = low_internal_ram_of_pseudo_low_internal_ram (sp1::M)
1949      (low_internal_ram ?
1950       (write_at_stack_pointer ?
1951         (set_8051_sfr ?
1952           (write_at_stack_pointer ? (set_8051_sfr ? s3 SFR_SP sp1) pbl)
1953          SFR_SP sp2)
1954        pbu)).
1955
1956axiom high_internal_ram_write_at_stack_pointer:
1957 ∀T1,T2,M,s1,s2,s3,pol.∀pbu,pbl,bu,bl,sp1,sp2:BitVector 8.
1958  get_8051_sfr ? s2 SFR_SP = get_8051_sfr ? s3 SFR_SP →
1959  high_internal_ram ? s2 = high_internal_ram T2 s3 →
1960  sp1 = \snd (half_add ? (get_8051_sfr ? s1 SFR_SP) (bitvector_of_nat 8 1)) →
1961  sp2 = \snd (half_add ? sp1 (bitvector_of_nat 8 1)) →
1962  bu@@bl = sigma (code_memory … s2) pol (pbu@@pbl) →
1963   high_internal_ram T1
1964     (write_at_stack_pointer ?
1965       (set_8051_sfr ?
1966         (write_at_stack_pointer ?
1967           (set_8051_sfr ?
1968             (set_high_internal_ram ? s1
1969               (high_internal_ram_of_pseudo_high_internal_ram M (high_internal_ram ? s2)))
1970             SFR_SP sp1)
1971          bl)
1972        SFR_SP sp2)
1973      bu)
1974   = high_internal_ram_of_pseudo_high_internal_ram (sp1::M)
1975      (high_internal_ram ?
1976       (write_at_stack_pointer ?
1977         (set_8051_sfr ?
1978           (write_at_stack_pointer ? (set_8051_sfr ? s3 SFR_SP sp1) pbl)
1979          SFR_SP sp2)
1980        pbu)).
1981
1982lemma Some_Some_elim:
1983 ∀T:Type[0].∀x,y:T.∀P:Type[2]. (x=y → P) → Some T x = Some T y → P.
1984 #T #x #y #P #H #K @H @option_destruct_Some //
1985qed.
1986
1987theorem main_thm:
1988 ∀M,M',ps,pol.
1989  next_internal_pseudo_address_map M ps = Some … M' →
1990   ∃n.
1991      execute n (status_of_pseudo_status M ps pol)
1992    = status_of_pseudo_status M' (execute_1_pseudo_instruction (ticks_of (code_memory … ps) pol) ps) ?.
1993 [2: >execute_1_pseudo_instruction_preserves_code_memory @pol]
1994 #M #M' #ps #pol #SAFE
1995 cut
1996  (∀ps'.
1997    ∀prf:ps'=execute_1_pseudo_instruction (ticks_of (code_memory … ps) pol) ps.
1998    ∃n. execute n (status_of_pseudo_status M ps pol) = status_of_pseudo_status M' ps' ?)
1999 [ >prf >execute_1_pseudo_instruction_preserves_code_memory @pol |3: #K @(K ? (refl …))]
2000 #ps' #EQ
2001 whd in ⊢ (??(λ_.??(??%)?))
2002 change with
2003  (∃n.
2004    execute n
2005     (set_low_internal_ram ?
2006       (set_high_internal_ram ?
2007         (set_program_counter ?
2008           (set_code_memory ?? ps (load_code_memory ?))
2009          (sigma ? pol (program_counter ? ps)))
2010        (high_internal_ram_of_pseudo_high_internal_ram M ?))
2011      (low_internal_ram_of_pseudo_low_internal_ram M ?))
2012   = set_low_internal_ram ?
2013      (set_high_internal_ram ?
2014        (set_program_counter ?
2015          (set_code_memory ?? ? (load_code_memory ?))
2016         (sigma ???)) ?) ?)
2017 >EQ whd in match eq_rect_Type0_r normalize nodelta
2018 >execute_1_pseudo_instruction_preserves_code_memory normalize nodelta
2019 generalize in match EQ -EQ;
2020 generalize in match (refl … (code_memory pseudo_assembly_program ps))
2021 generalize in match pol -pol; generalize in ⊢ (∀_.??%? → ?)
2022 * #preamble #instr_list #pol #EQ1 generalize in match pol -pol <EQ1 #pol #EQps' <EQps'
2023 (* Dependent types madness ends here *)
2024 letin ppc ≝ (program_counter … ps)
2025 generalize in match (fetch_assembly_pseudo2 ? pol ppc)
2026 letin assembled ≝ (\fst (assembly ? pol))
2027 letin lookup_labels ≝ (λx. sigma ? pol (address_of_word_labels_code_mem instr_list x))
2028 letin lookup_datalabels ≝ (λx. lookup … x (construct_datalabels preamble) (zero 16))
2029 @pair_elim' #pi #newppc #EQ2
2030 letin instructions ≝
2031  (expand_pseudo_instruction ? pol ppc lookup_labels lookup_datalabels
2032    (sigma ? pol ppc) (refl …) (refl …) (refl …))
2033 change with (fetch_many ???? → ?) #H1
2034 change in EQ2 with (fetch_pseudo_instruction instr_list ppc = ?)
2035 change in SAFE with (next_internal_pseudo_address_map0 ???? = ?) <EQ1 in SAFE;
2036 >EQ2 whd in ⊢ (??(??%??)? → ?) #SAFE
2037 whd in EQps':(???%); <EQ1 in EQps'; >EQ2 normalize nodelta
2038 generalize in match H1; -H1; generalize in match instructions -instructions
2039 * #instructions >EQ2 change in match (\fst 〈pi,newppc〉) with pi
2040 whd in match ticks_of normalize nodelta <EQ1 >EQ2
2041 cases pi in SAFE
2042  [2,3: (* Comment, Cost *) #ARG whd in ⊢ (??%? → ???% → ?)
2043   @Some_Some_elim #MAP #EQ3 @(Some_Some_elim ????? EQ3) #EQ3'
2044   whd in match eject normalize nodelta >EQ3' in ⊢ (% → ?) whd in ⊢ (% → ?)
2045   #H2 #EQ %[1,3:@0]
2046   <MAP >(eq_bv_eq … H2) >EQ %
2047
2048lemma main_thm0:
2049 ∀M,M',ps,preamble,instr_list,pol.
2050 ∀EQ0 : (code_memory pseudo_assembly_program ps=〈preamble,instr_list〉).
2051 ∀assembled.
2052  ∀H:
2053   (∀ppc.
2054     let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc in
2055     let instructions ≝
2056     expand_pseudo_instruction
2057                〈preamble,instr_list〉 pol ppc
2058                (λx:Identifier
2059                 .sigma 〈preamble,instr_list〉 pol
2061                (λx:BitVector 16
2062                 .lookup Identifier 16 x (construct_datalabels preamble) (zero 16))
2063                (sigma 〈preamble,instr_list〉 pol ppc)
2064                (refl …) (refl …) (refl …)
2065     in
2067                (sigma 〈preamble,instr_list〉 pol newppc) (sigma 〈preamble,instr_list〉 pol ppc)
2068                instructions).
2070                    (\fst  (fetch_pseudo_instruction instr_list
2071                                 (program_counter pseudo_assembly_program ps))) M ps
2073 ∀ps'.
2074  (ps'=execute_1_pseudo_instruction (ticks_of 〈preamble,instr_list〉 pol) ps
2075  →∃n:ℕ
2076   .execute n
2077    (set_low_internal_ram (BitVectorTrie Byte 16)
2078     (set_high_internal_ram (BitVectorTrie Byte 16)
2079      (set_program_counter (BitVectorTrie Byte 16)
2080       (set_code_memory pseudo_assembly_program (BitVectorTrie Byte 16) ps
2082       (sigma 〈preamble,instr_list〉 pol (program_counter pseudo_assembly_program ps)))
2083      (high_internal_ram_of_pseudo_high_internal_ram M
2084       (high_internal_ram pseudo_assembly_program ps)))
2085     (low_internal_ram_of_pseudo_low_internal_ram M
2086      (low_internal_ram pseudo_assembly_program ps)))
2087    =set_low_internal_ram (BitVectorTrie Byte 16)
2088     (set_high_internal_ram (BitVectorTrie Byte 16)
2089      (set_program_counter (BitVectorTrie Byte 16)
2090       (set_code_memory pseudo_assembly_program (BitVectorTrie Byte 16) ps'
2092       (sigma 〈preamble,instr_list〉 pol
2093        (program_counter pseudo_assembly_program ps')))
2094      (high_internal_ram_of_pseudo_high_internal_ram M'
2095       (high_internal_ram pseudo_assembly_program ps')))
2096     (low_internal_ram_of_pseudo_low_internal_ram M'
2097      (low_internal_ram pseudo_assembly_program ps'))).
2098 #M #M' #ps #preamble #instr_list #pol #EQ0 #assembled #H #MAP #ps'
2099 whd in ⊢ (???(?%?) → ?) normalize nodelta
2100 generalize in match (H (program_counter … ps)) -H; >EQ0 normalize nodelta;
2101
2102 whd in match expand_pseudo_instruction  normalize nodelta;
2103 whd in match execute_1_pseudo_instruction (*!!! USARE 0 !!!*) normalize nodelta; >EQ0
2104 normalize in match (\snd 〈preamble,instr_list〉);
2105 cases (fetch_pseudo_instruction instr_list (program_counter pseudo_assembly_program ps)) in MAP ⊢ %
2106 #pi #newppc normalize nodelta; #MAP
2107 cases pi in MAP; normalize nodelta;
2108  [2,3: (* Comment, Cost *) #ARG #MAP whd in ⊢ (% → ?) #H2 #EQ %[1,3:@0]
2109    generalize in match (option_destruct_Some ??? MAP) -MAP; #MAP <MAP -MAP M';
2110    (*
2111    normalize in H1; generalize in match (option_destruct_Some ??? H1) #K1 >K1 i
2112n H2; whd in ⊢ (% → ?)
2113    #H2*) >(eq_bv_eq … H2) >EQ %
2114
2115
2116
2117lemma main_thm0:
2118 ∀M,M',ps,s,s'',pol.
2119  next_internal_pseudo_address_map M ps = Some … M' →
2120  status_of_pseudo_status M ps pol = s →
2121  status_of_pseudo_status M' (execute_1_pseudo_instruction (ticks_of (code_memory … ps) pol) ps) ? = s'' →
2122   ∃n. execute n s = s''.
2123 [2: >execute_1_pseudo_instruction_preserves_code_memory @pol]
2124 #M #M' #ps #s #s'' #pol
2125 generalize in match (fetch_assembly_pseudo2 (code_memory … ps) pol)
2126 >execute_1_pseudo_instruction_preserves_code_memory
2127 generalize in match (refl … (assembly (code_memory … ps) pol))
2128 generalize in match (assembly (code_memory … ps) pol) in ⊢ (??%? → ?) #ASS #K <K generalize in match K; -K;
2129
2130
2131 whd in ⊢ (? → ? → ??%? → ??%? → ?)
2132 >execute_1_pseudo_instruction_preserves_code_memory
2133 generalize in match (refl … (assembly (code_memory … ps) pol))
2134 generalize in match (assembly (code_memory … ps) pol) in ⊢ (??%? → ?) #ASS #K <K generalize in match K; -K;
2135 whd in ⊢ (???% → ?)
2136 cases (build_maps (code_memory … ps) pol)
2137 #labels #costs change in ⊢ (? → ? → ??%? → ?) with (next_internal_pseudo_address_map0 ? ? ? ?)
2138 cases (code_memory … ps) in pol ⊢ %;
2139 generalize in match pol; -pol;
2140 @(match code_memory … ps return λx. ∀e:code_memory … ps = x.? with [pair preamble instr_list ⇒ ?]) [%]
2141 #EQ0 #pol normalize nodelta;
2142
2143
2144
2145lemma main_thm0:
2146 ∀M,M',ps,ps',pol.
2147  next_internal_pseudo_address_map M ps = Some … M' →
2148  ∀H:ps' = execute_1_pseudo_instruction (ticks_of (code_memory … ps) pol) ps.
2149   ∃n.
2150      execute n (status_of_pseudo_status M ps pol)
2151    = status_of_pseudo_status M' ps' ?.
2152 [2: >H >execute_1_pseudo_instruction_preserves_code_memory @pol]
2153 #M #M' #ps #ps' #pol
2154 change with (next_internal_pseudo_address_map0 ???? = ? → ?)
2155 generalize in match (fetch_assembly_pseudo2 (code_memory … ps) pol (program_counter … ps))
2156 @pair_elim' #labels #costs #EQ1 normalize nodelta
2157 whd in match execute_1_pseudo_instruction
2158 @pair_elim' #pi #newppc #EQ2 normalize nodelta
2159 cases pi in EQ2; normalize nodelta
2160  [2: #ARG #MAP #H1 #H2 #EQ >MAP in EQ;
2161
2162
2163 cases (execute_1_pseudo_instruction_preserves_code_memory ??) XX
2164 cases (assembly ??) #assembled #costs normalize nodelta
2165 cases (build_maps (code_memory … ps) pol) * #labels #costs whd in match eject; normalize nodelta;
2166 cases ps in pol ⊢ %; #code_mem #lir #hir #er #pc #sfr1 #sfr2 #p1l #p3l #clock #pol
2167 #MAP
2168 cases (fetch_pseudo_instruction (\snd code_mem) ppc)
2169
2170 change with
2171  (? → ∃n.
2172    execute n
2173     (set_low_internal_ram ?
2174       (set_high_internal_ram ?
2175         (set_program_counter ?
2176           (set_code_memory ?? ps (load_code_memory ?))
2177          (sigma ? pol (program_counter ? ps)))
2178        (high_internal_ram_of_pseudo_high_internal_ram M ?))
2179      (low_internal_ram_of_pseudo_low_internal_ram M ?))
2180   = set_low_internal_ram ?
2181      (set_high_internal_ram ?
2182        (set_program_counter ?
2183          (set_code_memory ?? (execute_1_pseudo_instruction ? ps) (load_code_memory ?))
2184         (sigma ???))
2185       ?)
2186     ?)
2187 change with (next_internal_pseudo_address_map0 ???? = ? → ?)
2188 generalize in match (fetch_assembly_pseudo2 (code_memory … ps) pol (program_counter … ps))
2189 @pair_elim' #labels #costs #EQ1 normalize nodelta
2190 @pair_elim' #pi #newppc #EQ2
2191 >execute_1_pseudo_instruction_preserves_code_memory
2192 cases (assembly ??) #assembled #costs normalize nodelta
2193 cases (build_maps (code_memory … ps) pol) * #labels #costs whd in match eject; normalize nodelta;
2194 cases ps in pol ⊢ %; #code_mem #lir #hir #er #pc #sfr1 #sfr2 #p1l #p3l #clock #pol
2195 #MAP
2196 cases (fetch_pseudo_instruction (\snd code_mem) ppc)
2197
2198
2199 (code_memory pseudo_assembly_program ps)
2200 #MAP
2201
2202
2203
2204 whd in ⊢ (? → ? → ??%? → ??%? → ?)
2205 generalize in match (assembly (code_memory … ps) pol) in ⊢ (??%? → %) #ASS whd in ⊢ (???% → ?)
2206 cases (build_maps (code_memory … ps) pol)
2207 #labels #costs change in ⊢ (? → ? → ??%? → ?) with (next_internal_pseudo_address_map0 ? ? ? ?)
2208 generalize in match pol; -pol;
2209 @(match code_memory … ps return λx. ∀e:code_memory … ps = x.? with [pair preamble instr_list ⇒ ?]) [%]
2210 #EQ0 #pol normalize nodelta;
2211 generalize in ⊢ (???(match % with [_ ⇒ ? | _ ⇒ ?]) → ?) *; normalize nodelta;
2212  [ #EQ >EQ #_ #_ #abs @⊥ normalize in abs; destruct (abs) ]
2213 * #final_ppc * #final_pc #assembled #EQ >EQ -EQ ASS; normalize nodelta;
2214 #H generalize in match (H ? (refl …)) -H; #H;
2215 #MAP
2216 #H1 generalize in match (option_destruct_Some ??? H1) -H1; #H1 <H1 -H1;
2217 #H2 generalize in match (option_destruct_Some ??? H2) -H2; #H2 <H2 -H2;
2218 change with
2219  (∃n.
2220    execute n
2221     (set_low_internal_ram ?
2222       (set_high_internal_ram ?
2223         (set_program_counter ?
2224           (set_code_memory ?? ps (load_code_memory assembled))
2225          (sigma 〈preamble,instr_list〉 pol (program_counter ? ps)))
2226        (high_internal_ram_of_pseudo_high_internal_ram M ?))
2227      (low_internal_ram_of_pseudo_low_internal_ram M ?))
2228   = set_low_internal_ram ?
2229      (set_high_internal_ram ?
2230        (set_program_counter ?
2231          (set_code_memory ?? (execute_1_pseudo_instruction ? ps) (load_code_memory assembled))
2232         (sigma ???))
2233       ?)
2234     ?)
2235 whd in match (\snd 〈preamble,instr_list〉) in H;
2236 whd in match (\fst 〈preamble,instr_list〉) in H;
2237 whd in match (\snd 〈final_pc,assembled〉) in H;
2238 whd in match (\snd 〈preamble,instr_list〉) in MAP;
2239 -s s'' labels costs final_ppc final_pc;
2240 letin ps' ≝ (execute_1_pseudo_instruction (ticks_of 〈preamble,instr_list〉 pol) ps)
2241 (* NICE STATEMENT HERE *)
2242 generalize in match (refl … ps') generalize in ⊢ (??%? → ?) normalize nodelta; -ps'; #ps'
2243 #K <K generalize in match K; -K;
2244 (* STATEMENT WITH EQUALITY HERE *)
2245 whd in ⊢ (???(?%?) → ?)
2246 whd in ⊢ (???% → ?) generalize in match (H (program_counter … ps)) -H; >EQ0 normalize nodelta;
2247 cases (fetch_pseudo_instruction instr_list (program_counter … ps)) in MAP ⊢ %
2248 #pi #newppc normalize nodelta; #MAP * #instructions *;
2249 cases pi in MAP; normalize nodelta;
2250  [2,3: (* Comment, Cost *) #ARG #MAP #H1 #H2 #EQ %[1,3:@0]
2251    generalize in match (option_destruct_Some ??? MAP) -MAP; #MAP <MAP -MAP M';
2252    normalize in H1; generalize in match (option_destruct_Some ??? H1) #K1 >K1 in H2; whd in ⊢ (% → ?)
2253    #H2 >(eq_bv_eq … H2) >EQ %
2254(*  |6: (* Mov *) #arg1 #arg2
2255       #H1 #H2 #EQ %[@1]
2256       normalize in H1; generalize in match (option_destruct_Some ??? H1) #K1 >K1 in H2; whd in ⊢ (% → ?)
2257       change in ⊢ (? → ??%?) with (execute_1_0 ??)
2258       cases (fetch (load_code_memory assembled) (sigma 〈preamble,instr_list〉 (program_counter … ps))) * #instr #newppc' #ticks normalize nodelta;
2259       * * #H2a #H2b whd in ⊢ (% → ?) #H2c
2260       >H2b >(eq_instruction_to_eq … H2a)
2261       generalize in match EQ; -EQ; whd in ⊢ (???% → ??%?);
2262       @(list_addressing_mode_tags_elim_prop … arg1) whd try % -arg1; whd in ⊢ (???% → ??%?)
2263       @(list_addressing_mode_tags_elim_prop … arg2) whd try % -arg2; #ARG2
2264       normalize nodelta;
2265       [1,2,3,4,5,6,7,8: cases (add_8_with_carry ???) |*: cases (sub_8_with_carry ???)]
2266       #result #flags
2267       #EQ >EQ -EQ; normalize nodelta; >(eq_bv_eq … H2c) % *)
2268(*  |5: (* Call *) #label #MAP
2269      generalize in match (option_destruct_Some ??? MAP) -MAP; #MAP <MAP -MAP;
2270      whd in ⊢ (???% → ?) cases (pol ?) normalize nodelta;
2271       [ (* short *) #abs @⊥ destruct (abs)
2272       |3: (* long *) #H1 #H2 #EQ %[@1]
2273           (* normalize in H1; !!!*) generalize in match (option_destruct_Some ??? H1) #K1 >K1 in H2; whd in ⊢ (% → ?)
2274           change in ⊢ (? → ??%?) with (execute_1_0 ??)
2275           cases (fetch (load_code_memory assembled) (sigma 〈preamble,instr_list〉 pol (program_counter … ps))) * #instr #newppc' #ticks normalize nodelta;
2276           * * #H2a #H2b whd in ⊢ (% → ?) #H2c
2277           >H2b >(eq_instruction_to_eq … H2a)
2278           generalize in match EQ; -EQ;
2279           whd in ⊢ (???% → ??%?);
2280           generalize in match (refl … (half_add 8 (get_8051_sfr ? ps SFR_SP) (bitvector_of_nat ? 1))) cases (half_add ???) in ⊢ (??%? → %) #carry #new_sp #EQ1 normalize nodelta;
2281           >(eq_bv_eq … H2c)
2282           change with
2283            ((?=let 〈ppc_bu,ppc_bl〉 ≝ split bool 8 8 newppc in ?) →
2284                (let 〈pc_bu,pc_bl〉 ≝ split bool 8 8 (sigma 〈preamble,instr_list〉 pol newppc) in ?)=?)
2285           generalize in match (refl … (split … 8 8 newppc)) cases (split bool 8 8 newppc) in ⊢ (??%? → %) #ppc_bu #ppc_bl #EQppc
2286           generalize in match (refl … (split … 8 8 (sigma 〈preamble,instr_list〉 pol newppc))) cases (split bool 8 8 (sigma 〈preamble,instr_list〉 pol newppc)) in ⊢ (??%? → %) #pc_bu #pc_bl #EQpc normalize nodelta;
2287           >get_8051_sfr_write_at_stack_pointer >get_8051_sfr_write_at_stack_pointer
2288           >get_8051_sfr_set_8051_sfr >get_8051_sfr_set_8051_sfr
2289           generalize in match (refl … (half_add ? new_sp (bitvector_of_nat ? 1))) cases (half_add ???) in ⊢ (??%? → %) #carry' #new_sp' #EQ2 normalize nodelta;
2290           #EQ >EQ -EQ; normalize nodelta; >(eq_bv_eq … H2c)
2291           @split_eq_status;
2292            [ >code_memory_write_at_stack_pointer whd in ⊢ (??%?)
2293              >code_memory_write_at_stack_pointer %
2294            | >set_program_counter_set_low_internal_ram
2295              >set_clock_set_low_internal_ram
2296              @low_internal_ram_write_at_stack_pointer
2297               [ >EQ0 @pol | % | %
2298               | @(pair_destruct_2 … EQ1)
2299               | @(pair_destruct_2 … EQ2)
2300               | >(pair_destruct_1 ????? EQpc)
2301                 >(pair_destruct_2 ????? EQpc)
2302                 @split_elim #x #y #H <H -x y H;
2303                 >(pair_destruct_1 ????? EQppc)
2304                 >(pair_destruct_2 ????? EQppc)
2305                 @split_elim #x #y #H <H -x y H;
2306                 >EQ0 % ]
2307            | >set_low_internal_ram_set_high_internal_ram
2308              >set_program_counter_set_high_internal_ram
2309              >set_clock_set_high_internal_ram
2310              @high_internal_ram_write_at_stack_pointer
2311               [ >EQ0 @pol | % | %
2312               | @(pair_destruct_2 … EQ1)
2313               | @(pair_destruct_2 … EQ2)
2314               | >(pair_destruct_1 ????? EQpc)
2315                 >(pair_destruct_2 ????? EQpc)
2316                 @split_elim #x #y #H <H -x y H;
2317                 >(pair_destruct_1 ????? EQppc)
2318                 >(pair_destruct_2 ????? EQppc)
2319                 @split_elim #x #y #H <H -x y H;
2320                 >EQ0 % ]
2321            | >external_ram_write_at_stack_pointer whd in ⊢ (??%?)
2322              >external_ram_write_at_stack_pointer whd in ⊢ (???%)
2323              >external_ram_write_at_stack_pointer whd in ⊢ (???%)
2324              >external_ram_write_at_stack_pointer %
2325            | change with (? = sigma ?? (address_of_word_labels_code_mem (\snd (code_memory ? ps)) ?))
2326              >EQ0 %
2327            | >special_function_registers_8051_write_at_stack_pointer whd in ⊢ (??%?)
2328              >special_function_registers_8051_write_at_stack_pointer whd in ⊢ (???%)
2329              >special_function_registers_8051_write_at_stack_pointer whd in ⊢ (???%)
2330              >special_function_registers_8051_write_at_stack_pointer %
2331            | >special_function_registers_8052_write_at_stack_pointer whd in ⊢ (??%?)
2332              >special_function_registers_8052_write_at_stack_pointer whd in ⊢ (???%)
2333              >special_function_registers_8052_write_at_stack_pointer whd in ⊢ (???%)
2334              >special_function_registers_8052_write_at_stack_pointer %
2335            | >p1_latch_write_at_stack_pointer whd in ⊢ (??%?)
2336              >p1_latch_write_at_stack_pointer whd in ⊢ (???%)
2337              >p1_latch_write_at_stack_pointer whd in ⊢ (???%)
2338              >p1_latch_write_at_stack_pointer %
2339            | >p3_latch_write_at_stack_pointer whd in ⊢ (??%?)
2340              >p3_latch_write_at_stack_pointer whd in ⊢ (???%)
2341              >p3_latch_write_at_stack_pointer whd in ⊢ (???%)
2342              >p3_latch_write_at_stack_pointer %
2343            | >clock_write_at_stack_pointer whd in ⊢ (??%?)
2344              >clock_write_at_stack_pointer whd in ⊢ (???%)
2345              >clock_write_at_stack_pointer whd in ⊢ (???%)
2346              >clock_write_at_stack_pointer %]
2347       (*| (* medium *)  #H1 #H2 #EQ %[@1] generalize in match H1; -H1;
2349         @pair_elim' #fst_5_pc #rest_pc #EQ2
2350         generalize in match (refl … (eq_bv … fst_5_addr fst_5_pc))
2351         cases (eq_bv ???) in ⊢ (??%? → %) normalize nodelta; #EQ3 #TEQ [2: destruct (TEQ)]
2352         generalize in match (option_destruct_Some ??? TEQ) -TEQ; #K1 >K1 in H2; whd in ⊢ (% → ?)
2353         change in ⊢ (? →??%?) with (execute_1_0 ??)
2354         @pair_elim' * #instr #newppc' #ticks #EQn
2355          * * #H2a #H2b whd in ⊢ (% → ?) #H2c >H2b >(eq_instruction_to_eq … H2a) whd in ⊢ (??%?)
2356          generalize in match EQ; -EQ; normalize nodelta; >(eq_bv_eq … H2c)
2357          @pair_elim' #carry #new_sp change with (half_add ? (get_8051_sfr ? ps ?) ? = ? → ?) #EQ4
2358          @split_elim' #pc_bu #pc_bl >program_counter_set_8051_sfr XXX change with (newppc = ?) #EQ5
2359          @pair_elim' #carry' #new_sp' #EQ6 normalize nodelta; #EQx >EQx -EQx;
2360          change in ⊢ (??(match ????% with [_ ⇒ ?])?) with (sigma … newppc)
2361          @split_elim' #pc_bu' #pc_bl' #EQ7 change with (newppc' = ? → ?)
2362          >get_8051_sfr_set_8051_sfr
2363
2364          whd in EQ:(???%) ⊢ ? >EQ -EQ; normalize nodelta; >(eq_bv_eq … H2c) whd in ⊢ (??%?)
2365           change with ((let 〈pc_bu,pc_bl〉 ≝ split bool 8 8 (sigma 〈preamble,instr_list〉 newppc) in ?)=?)
2366           generalize in match (refl … (split bool 8 8 (sigma 〈preamble,instr_list〉 newppc)))
2367           cases (split ????) in ⊢ (??%? → %) #pc_bu #pc_bl normalize nodelta; #EQ4
2368           generalize in match (refl … (split bool 4 4 pc_bu))
2369           cases (split ????) in ⊢ (??%? → %) #nu #nl normalize nodelta; #EQ5
2370           generalize in match (refl … (split bool 3 8 rest_addr))
2371           cases (split ????) in ⊢ (??%? → %) #relevant1 #relevant2 normalize nodelta; #EQ6
2372           change with ((let 〈carry,new_pc〉 ≝ half_add ? (sigma … newppc) ? in ?) = ?)
2373           generalize in match
2374            (refl …
2375             (half_add 16 (sigma 〈preamble,instr_list〉 newppc)
2376             ((nu@@get_index' bool 0 3 nl:::relevant1)@@relevant2)))
2377           cases (half_add ???) in ⊢ (??%? → %) #carry #new_pc normalize nodelta; #EQ7
2378           @split_eq_status try %
2379            [ change with (? = sigma ? (address_of_word_labels ps label))
2380              (* ARITHMETICS, BUT THE GOAL SEEMS FALSE *)
2381            | whd in ⊢ (??%%) whd in ⊢ (??(?%?)?) whd in ⊢ (??(?(match ?(?%)? with [_ ⇒ ?])?)?)
2382              @(bitvector_3_elim_prop … (\fst (split bool 3 8 rest_addr))) %]] *)]
2383  |4: (* Jmp *) #label #MAP
2384      generalize in match (option_destruct_Some ??? MAP) -MAP; #MAP >MAP -MAP;
2385      whd in ⊢ (???% → ?) cases (pol ?) normalize nodelta;
2386       [3: (* long *) #H1 #H2 #EQ %[@1]
2387           (* normalize in H1; !!!*) generalize in match (option_destruct_Some ??? H1) #K1 >K1 in H2; whd in ⊢ (% → ?)
2388           change in ⊢ (? → ??%?) with (execute_1_0 ??)
2389           cases (fetch (load_code_memory assembled) (sigma 〈preamble,instr_list〉 pol (program_counter … ps))) * #instr #newppc' #ticks normalize nodelta;
2390           * * #H2a #H2b whd in ⊢ (% → ?) #H2c
2391           >H2b >(eq_instruction_to_eq … H2a)
2392           generalize in match EQ; -EQ;
2393           #EQ >EQ -EQ; normalize nodelta; >(eq_bv_eq … H2c)
2394           cases ps in EQ0 ⊢ %; #A1 #A2 #A3 #A4 #A5 #A6 #A7 #A8 #A9 #A10 #XXXX >XXXX %
2395       |1: (* short *) #H1 #H2 #EQ %[@1] generalize in match H1; -H1;
2396           generalize in match
2397            (refl ?
2398             (sub_16_with_carry
2399              (sigma 〈preamble,instr_list〉 pol (program_counter … ps))
2400              (sigma 〈preamble,instr_list〉 pol (address_of_word_labels_code_mem instr_list label))
2401              false))
2402           cases (sub_16_with_carry ???) in ⊢ (??%? → %); #results #flags normalize nodelta;
2403           generalize in match (refl … (split … 8 8 results)) cases (split ????) in ⊢ (??%? → %) #upper #lower normalize nodelta;
2404           generalize in match (refl … (eq_bv … upper (zero 8))) cases (eq_bv ???) in ⊢ (??%? → %) normalize nodelta;
2405           #EQ1 #EQ2 #EQ3 #H1 [2: @⊥ destruct (H1)]
2406           generalize in match (option_destruct_Some ??? H1) #K1 >K1 in H2; whd in ⊢ (% → ?)
2407           change in ⊢ (? → ??%?) with (execute_1_0 ??)
2408           cases (fetch (load_code_memory assembled) (sigma 〈preamble,instr_list〉 pol (program_counter … ps))) * #instr #newppc' #ticks normalize nodelta;
2409           * * #H2a #H2b whd in ⊢ (% → ?) #H2c
2410           >H2b >(eq_instruction_to_eq … H2a)
2411           generalize in match EQ; -EQ;
2412           whd in ⊢ (???% → ?);
2413           #EQ >EQ -EQ; normalize nodelta; >(eq_bv_eq … H2c)
2414           change with ((let 〈carry,new_pc〉 ≝ half_add ? (sigma ???) ? in ?) = ?)
2415           generalize in match (refl … (half_add 16 (sigma 〈preamble,instr_list〉 pol newppc) (sign_extension lower)))
2416           cases (half_add ???) in ⊢ (??%? → %) #carry #newpc normalize nodelta #EQ4
2417           @split_eq_status try % change with (newpc = sigma ?? (address_of_word_labels ps label))
2418           (* ARITHMETICS, BUT THE GOAL SEEMS FALSE *)
2419       | (* medium *)  #H1 #H2 #EQ %[@1] generalize in match H1; -H1;
2420         generalize in match
2421          (refl …
2422            (split … 5 11 (sigma 〈preamble,instr_list〉 pol (address_of_word_labels_code_mem instr_list label))))
2423         cases (split ????) in ⊢ (??%? → %) #fst_5_addr #rest_addr normalize nodelta; #EQ1
2424         generalize in match
2425          (refl …
2426            (split … 5 11 (sigma 〈preamble,instr_list〉 pol (program_counter … ps))))
2427         cases (split ????) in ⊢ (??%? → %) #fst_5_pc #rest_pc normalize nodelta; #EQ2
2428         generalize in match (refl … (eq_bv … fst_5_addr fst_5_pc))
2429         cases (eq_bv ???) in ⊢ (??%? → %) normalize nodelta; #EQ3 #TEQ [2: destruct (TEQ)]
2430         generalize in match (option_destruct_Some ??? TEQ) -TEQ; #K1 >K1 in H2; whd in ⊢ (% → ?)
2431         change in ⊢ (? →??%?) with (execute_1_0 ??)
2432           cases (fetch (load_code_memory assembled) (sigma 〈preamble,instr_list〉 pol (program_counter … ps))) * #instr #newppc' #ticks normalize nodelta;
2433           * * #H2a #H2b whd in ⊢ (% → ?) #H2c
2434           >H2b >(eq_instruction_to_eq … H2a)
2435           generalize in match EQ; -EQ;
2436           whd in ⊢ (???% → ?);
2437           #EQ >EQ -EQ; normalize nodelta; >(eq_bv_eq … H2c) whd in ⊢ (??%?)
2438           change with ((let 〈pc_bu,pc_bl〉 ≝ split bool 8 8 (sigma 〈preamble,instr_list〉 pol newppc) in ?)=?)
2439           generalize in match (refl … (split bool 8 8 (sigma 〈preamble,instr_list〉 pol newppc)))
2440           cases (split ????) in ⊢ (??%? → %) #pc_bu #pc_bl normalize nodelta; #EQ4
2441           generalize in match (refl … (split bool 4 4 pc_bu))
2442           cases (split ????) in ⊢ (??%? → %) #nu #nl normalize nodelta; #EQ5
2443           generalize in match (refl … (split bool 3 8 rest_addr))
2444           cases (split ????) in ⊢ (??%? → %) #relevant1 #relevant2 normalize nodelta; #EQ6
2445           change with ((let 〈carry,new_pc〉 ≝ half_add ? (sigma … newppc) ? in ?) = ?)
2446           generalize in match
2447            (refl …
2448             (half_add 16 (sigma 〈preamble,instr_list〉 pol newppc)
2449             ((nu@@get_index' bool 0 3 nl:::relevant1)@@relevant2)))
2450           cases (half_add ???) in ⊢ (??%? → %) #carry #new_pc normalize nodelta; #EQ7
2451           @split_eq_status try %
2452            [ change with (? = sigma ?? (address_of_word_labels ps label))
2453              (* ARITHMETICS, BUT THE GOAL SEEMS FALSE *)
2454            | whd in ⊢ (??%%) whd in ⊢ (??(?%?)?) whd in ⊢ (??(?(match ?(?%)? with [_ ⇒ ?])?)?)
2455              @(bitvector_3_elim_prop … (\fst (split bool 3 8 rest_addr))) %]]
2456*)  | (* Instruction *) -pi;  whd in ⊢ (? → ??%? → ?) *; normalize nodelta;
2457    [1,2,3: (* ADD, ADDC, SUBB *) #arg1 #arg2 #MAP #H1 #H2 #EQ %[1,3,5:@1]
2458       normalize in H1; generalize in match (option_destruct_Some ??? H1) #K1 >K1 in H2; whd in ⊢ (% → ?)
2459       change in ⊢ (? → ??%?) with (execute_1_0 ??)
2460       cases (fetch (load_code_memory assembled) (sigma 〈preamble,instr_list〉 pol (program_counter … ps))) * #instr #newppc' #ticks normalize nodelta;
2461       * * #H2a #H2b whd in ⊢ (% → ?) #H2c
2462       >H2b >(eq_instruction_to_eq … H2a)
2463       generalize in match EQ; -EQ; whd in ⊢ (???% → ??%?); generalize in match MAP; -MAP;
2464       @(list_addressing_mode_tags_elim_prop … arg1) whd try % -arg1;
2465       @(list_addressing_mode_tags_elim_prop … arg2) whd try % -arg2; #ARG2
2466       normalize nodelta; #MAP; (*
2467       [1: change in ⊢ (? → %) with
2468        ((let 〈result,flags〉 ≝
2470           (get_arg_8 ? ps false ACC_A)
2471           (get_arg_8 ?
2472             (set_low_internal_ram ? ps (low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … ps)))
2473             false (DIRECT ARG2))
2474           ? in ?) = ?)
2475        [2,3: %]
2476        change in ⊢ (???% → ?) with
2477         (let 〈result,flags〉 ≝ add_8_with_carry ?(*(get_arg_8 ? ps false ACC_A)*) ?? in ?)
2478        >get_arg_8_set_clock *)
2479       [1,2: cases (addressing_mode_ok ???? ∧ addressing_mode_ok ????) in MAP ⊢ ?
2480         [2,4: #abs @⊥ normalize in abs; destruct (abs)
2481         |*:whd in ⊢ (??%? → ?) #H <(option_destruct_Some ??? H)]
2482       [ change in ⊢ (? → %) with
2483        ((let 〈result,flags〉 ≝
2485           (get_arg_8 ? ps false ACC_A)
2486           (get_arg_8 ?
2487             (set_low_internal_ram ? ps (low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … ps)))
2488             false (DIRECT ARG2))
2489           ? in ?) = ?)
2490          >get_arg_8_set_low_internal_ram
2491
2493
2494        [1,2,3,4,5,6,7,8: cases (add_8_with_carry ???) |*: cases (sub_8_with_carry ???)]
2495       #result #flags
2496       #EQ >EQ -EQ; normalize nodelta; >(eq_bv_eq … H2c) %
2497    (*| (* INC *) #arg1 #H1 #H2 #EQ %[@1]
2498       normalize in H1; generalize in match (option_destruct_Some ??? H1) #K1 >K1 in H2; whd in ⊢ (% → ?)
2499       change in ⊢ (? → ??%?) with (execute_1_0 ??)
2500       cases (fetch (load_code_memory assembled) (sigma 〈preamble,instr_list〉 (program_counter … ps))) * #instr #newppc' #ticks normalize nodelta;
2501       * * #H2a #H2b whd in ⊢ (% → ?) #H2c
2502       >H2b >(eq_instruction_to_eq … H2a)
2503       generalize in match EQ; -EQ; whd in ⊢ (???% → ??%?);
2504       @(list_addressing_mode_tags_elim_prop … arg1) whd try % -arg1; normalize nodelta; [1,2,3: #ARG]
2505       [1,2,3,4: cases (half_add ???) #carry #result
2506       | cases (half_add ???) #carry #bl normalize nodelta;
2507         cases (full_add ????) #carry' #bu normalize nodelta ]
2508        #EQ >EQ -EQ; normalize nodelta; >(eq_bv_eq … H2c) -newppc';
2509        [5: %
2510        |1: <(set_arg_8_set_code_memory 0 [[direct]] ? ? ? (set_clock pseudo_assembly_program
2511      (set_program_counter pseudo_assembly_program ps newppc)
2512      (\fst  (ticks_of0 〈preamble,instr_list〉
2513                   (program_counter pseudo_assembly_program ps)
2514                   (Instruction (INC Identifier (DIRECT ARG))))
2515       +clock pseudo_assembly_program
2516        (set_program_counter pseudo_assembly_program ps newppc))) (load_code_memory assembled) result (DIRECT ARG))
2517        [2,3: // ]
2518            <(set_arg_8_set_program_counter 0 [[direct]] ? ? ? ? ?) [2://]
2519            whd in ⊢ (??%%)
2520            cases (split bool 4 4 ARG)
2521            #nu' #nl'
2522            normalize nodelta
2523            cases (split bool 1 3 nu')
2524            #bit_1' #ignore'
2525            normalize nodelta
2526            cases (get_index_v bool 4 nu' ? ?)
2527            [ normalize nodelta (* HERE *) whd in ⊢ (??%%) %
2528            |
2529            ] *)
2530
2531lemma main_thm:
2532 ∀M,M',ps,pol.
2533  next_internal_pseudo_address_map M ps = Some … M' →
2534   ∃n.
2535      execute n (status_of_pseudo_status M ps pol)
2536    = status_of_pseudo_status M' (execute_1_pseudo_instruction (ticks_of (code_memory … ps) pol) ps) ?.
2537 [2: >execute_1_pseudo_instruction_preserves_code_memory @pol]
Note: See TracBrowser for help on using the repository browser.