source: src/ASM/AssemblyProof.ma @ 994

Last change on this file since 994 was 994, checked in by mulligan, 8 years ago

small changes

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