1 | include "ASM/Assembly.ma". |
---|
2 | include "ASM/Interpret.ma". |
---|
3 | |
---|
4 | let rec encoding_check (code_memory: BitVectorTrie Byte 16) (pc: Word) (final_pc: Word) |
---|
5 | (encoding: list Byte) on encoding: Prop ≝ |
---|
6 | match encoding with |
---|
7 | [ nil ⇒ final_pc = pc |
---|
8 | | cons hd tl ⇒ |
---|
9 | let 〈new_pc, byte〉 ≝ next code_memory pc in |
---|
10 | hd = byte ∧ encoding_check code_memory new_pc final_pc tl |
---|
11 | ]. |
---|
12 | |
---|
13 | (* |
---|
14 | definition assembly_specification: |
---|
15 | ∀sigma: Word → Word. ∀assembly_program: pseudo_assembly_program. |
---|
16 | ∀code_mem: BitVectorTrie Byte 16. Prop ≝ |
---|
17 | λsigma. |
---|
18 | λpseudo_assembly_program. |
---|
19 | λcode_mem. |
---|
20 | ∀pc: Word. |
---|
21 | let 〈preamble, instr_list〉 ≝ pseudo_assembly_program in |
---|
22 | let 〈pre_instr, pre_new_pc〉 ≝ fetch_pseudo_instruction instr_list pc in |
---|
23 | let address_of ≝ ? in |
---|
24 | let labels ≝ λx. sigma (address_of_word_labels_code_mem instr_list x) in |
---|
25 | let datalabels ≝ λx. sigma (lookup ? ? x (construct_datalabels preamble) (zero ?)) in |
---|
26 | let pre_assembled ≝ assembly_1_pseudoinstruction labels datalabels address_of 〈None ?, pre_instr〉 in |
---|
27 | encoding_check code_mem pc (sigma pre_new_pc) pre_assembled. |
---|
28 | cases not_implemented |
---|
29 | qed. |
---|
30 | |
---|
31 | axiom assembly_meets_specification: |
---|
32 | ∀pseudo_assembly_program. |
---|
33 | match assembly pseudo_assembly_program with |
---|
34 | [ None ⇒ True |
---|
35 | | Some code_mem_cost ⇒ |
---|
36 | let 〈code_mem, cost〉 ≝ code_mem_cost in |
---|
37 | assembly_specification ? pseudo_assembly_program (load_code_memory code_mem) |
---|
38 | ]. |
---|
39 | cases not_implemented (* |
---|
40 | # PROGRAM |
---|
41 | [ cases PROGRAM |
---|
42 | # PREAMBLE |
---|
43 | # INSTR_LIST |
---|
44 | elim INSTR_LIST |
---|
45 | [ whd |
---|
46 | whd in ⊢ (∀_. %) |
---|
47 | # PC |
---|
48 | whd |
---|
49 | | # INSTR |
---|
50 | # INSTR_LIST_TL |
---|
51 | # H |
---|
52 | whd |
---|
53 | whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?]) |
---|
54 | ] |
---|
55 | | cases not_implemented |
---|
56 | ] *) |
---|
57 | qed. |
---|
58 | *) |
---|
59 | |
---|
60 | axiom sigma: pseudo_assembly_program → Word → Word. |
---|
61 | |
---|
62 | (* |
---|
63 | definition status_of_pseudo_status: PseudoStatus → option Status ≝ |
---|
64 | λps. |
---|
65 | let pap ≝ code_memory … ps in |
---|
66 | match assembly pap with |
---|
67 | [ None ⇒ None … |
---|
68 | | Some p ⇒ |
---|
69 | let cm ≝ load_code_memory (\fst p) in |
---|
70 | let pc ≝ sigma pap (program_counter ? ps) in |
---|
71 | Some … |
---|
72 | (mk_PreStatus (BitVectorTrie Byte 16) |
---|
73 | cm |
---|
74 | (low_internal_ram … ps) |
---|
75 | (high_internal_ram … ps) |
---|
76 | (external_ram … ps) |
---|
77 | pc |
---|
78 | (special_function_registers_8051 … ps) |
---|
79 | (special_function_registers_8052 … ps) |
---|
80 | (p1_latch … ps) |
---|
81 | (p3_latch … ps) |
---|
82 | (clock … ps)) ]. |
---|
83 | *) |
---|
84 | |
---|
85 | (* RUSSEL **) |
---|
86 | |
---|
87 | include "basics/jmeq.ma". |
---|
88 | |
---|
89 | definition inject : ∀A.∀P:A → Prop.∀a.∀p:P a.Σx:A.P x ≝ λA,P,a,p. dp … a p. |
---|
90 | definition eject : ∀A.∀P: A → Prop.(Σx:A.P x) → A ≝ λA,P,c.match c with [ dp w p ⇒ w]. |
---|
91 | |
---|
92 | coercion inject : ∀A.∀P:A → Prop.∀a.∀p:P a.Σx:A.P x ≝ inject on a:? to Σx:?.?. |
---|
93 | coercion eject : ∀A.∀P:A → Prop.∀c:Σx:A.P x.A ≝ eject on _c:Σx:?.? to ?. |
---|
94 | |
---|
95 | lemma sig2: ∀A.∀P:A → Prop. ∀p:Σx:A.P x. P (eject … p). |
---|
96 | #A #P #p cases p #w #q @q |
---|
97 | qed. |
---|
98 | |
---|
99 | (* END RUSSELL **) |
---|
100 | |
---|
101 | definition write_at_stack_pointer: |
---|
102 | ∀M. ∀ps: PreStatus M. Byte → Σps':PreStatus M.(code_memory … ps = code_memory … ps') ≝ |
---|
103 | λM: Type[0]. |
---|
104 | λs: PreStatus M. |
---|
105 | λv: Byte. |
---|
106 | let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in |
---|
107 | let bit_zero ≝ get_index_v… nu O ? in |
---|
108 | let bit_1 ≝ get_index_v… nu 1 ? in |
---|
109 | let bit_2 ≝ get_index_v… nu 2 ? in |
---|
110 | let bit_3 ≝ get_index_v… nu 3 ? in |
---|
111 | if bit_zero then |
---|
112 | let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl) |
---|
113 | v (low_internal_ram ? s) in |
---|
114 | set_low_internal_ram ? s memory |
---|
115 | else |
---|
116 | let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl) |
---|
117 | v (high_internal_ram ? s) in |
---|
118 | set_high_internal_ram ? s memory. |
---|
119 | [ cases l0 % |
---|
120 | |2,3,4,5: cases not_implemented (* CSC: ???? normalize repeat (@ le_S_S) @ le_O_n *) |
---|
121 | ] |
---|
122 | qed. |
---|
123 | |
---|
124 | definition execute_1_pseudo_instruction': (Word → nat) → ∀ps:PseudoStatus. |
---|
125 | Σps':PseudoStatus.(code_memory … ps = code_memory … ps') |
---|
126 | ≝ |
---|
127 | λticks_of. |
---|
128 | λs. |
---|
129 | let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s) in |
---|
130 | let ticks ≝ ticks_of (program_counter ? s) in |
---|
131 | let s ≝ set_clock ? s (clock ? s + ticks) in |
---|
132 | let s ≝ set_program_counter ? s pc in |
---|
133 | match instr with |
---|
134 | [ Instruction instr ⇒ |
---|
135 | execute_1_preinstruction … (λx, y. address_of_word_labels y x) instr s |
---|
136 | | Comment cmt ⇒ s |
---|
137 | | Cost cst ⇒ s |
---|
138 | | Jmp jmp ⇒ set_program_counter ? s (address_of_word_labels s jmp) |
---|
139 | | Call call ⇒ |
---|
140 | let a ≝ address_of_word_labels s call in |
---|
141 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in |
---|
142 | let s ≝ set_8051_sfr ? s SFR_SP new_sp in |
---|
143 | let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter ? s) in |
---|
144 | let s ≝ write_at_stack_pointer ? s pc_bl in |
---|
145 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in |
---|
146 | let s ≝ set_8051_sfr ? s SFR_SP new_sp in |
---|
147 | let s ≝ write_at_stack_pointer ? s pc_bu in |
---|
148 | set_program_counter ? s a |
---|
149 | | Mov dptr ident ⇒ |
---|
150 | set_arg_16 ? s (get_arg_16 ? s (DATA16 (address_of_word_labels s ident))) dptr |
---|
151 | ]. |
---|
152 | [ |
---|
153 | |2,3,4: % |
---|
154 | | <(sig2 … l7) whd in ⊢ (??? (??%)) <(sig2 … l5) % |
---|
155 | | |
---|
156 | | (*CSC: ??? *) |
---|
157 | | (*CSC: ??? *) |
---|
158 | | % |
---|
159 | ] |
---|
160 | qed. |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | lemma execute_code_memory_unchanged: |
---|
166 | ∀ticks_of,ps. code_memory ? ps = code_memory ? (execute_1_pseudo_instruction ticks_of ps). |
---|
167 | #ticks #ps whd in ⊢ (??? (??%)) |
---|
168 | cases (fetch_pseudo_instruction (\snd (code_memory pseudo_assembly_program ps)) |
---|
169 | (program_counter pseudo_assembly_program ps)) #instr #pc |
---|
170 | whd in ⊢ (??? (??%)) cases instr |
---|
171 | [ #pre cases pre |
---|
172 | [ #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%)) |
---|
173 | cases (split ????) #z1 #z2 % |
---|
174 | | #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%)) |
---|
175 | cases (split ????) #z1 #z2 % |
---|
176 | | #a1 #a2 whd in ⊢ (??? (??%)) cases (sub_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%)) |
---|
177 | cases (split ????) #z1 #z2 % |
---|
178 | | #a1 whd in ⊢ (??? (??%)) cases a1 #x #H whd in ⊢ (??? (??%)) cases x |
---|
179 | [ #x1 whd in ⊢ (??? (??%)) |
---|
180 | | *: cases not_implemented |
---|
181 | ] |
---|
182 | | #comment % |
---|
183 | | #cost % |
---|
184 | | #label % |
---|
185 | | #label whd in ⊢ (??? (??%)) cases (half_add ???) #x1 #x2 whd in ⊢ (??? (??%)) |
---|
186 | cases (split ????) #y1 #y2 whd in ⊢ (??? (??%)) cases (half_add ???) #z1 #z2 |
---|
187 | whd in ⊢ (??? (??%)) whd in ⊢ (??? (??%)) cases (split ????) #w1 #w2 |
---|
188 | whd in ⊢ (??? (??%)) cases (get_index_v bool ????) whd in ⊢ (??? (??%)) |
---|
189 | (* CSC: ??? *) |
---|
190 | | #dptr #label (* CSC: ??? *) |
---|
191 | ] |
---|
192 | cases not_implemented |
---|
193 | qed. |
---|
194 | |
---|
195 | lemma foo: |
---|
196 | ∀ticks_of. |
---|
197 | ∀ps: PseudoStatus. |
---|
198 | match status_of_pseudo_status ps with [ None ⇒ True | Some s ⇒ |
---|
199 | let ps' ≝ execute_1_pseudo_instruction ticks_of ps in |
---|
200 | match status_of_pseudo_status ps' with [ None ⇒ True | Some s'' ⇒ |
---|
201 | let s' ≝ execute_1 s in |
---|
202 | s = s'']]. |
---|
203 | #ticks_of #ps |
---|
204 | whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ]) |
---|
205 | cases (assembly (code_memory pseudo_assembly_program ps)) [%] * #cm #costs whd |
---|
206 | whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ]) |
---|
207 | cases (status_of_pseudo_status (execute_1_pseudo_instruction ticks_of ps)) [%] #s'' whd |
---|