1 | include "ASM/StatusProofs.ma". |
---|
2 | include "common/StructuredTraces.ma". |
---|
3 | include "ASM/Fetch.ma". |
---|
4 | |
---|
5 | definition sign_extension: Byte → Word ≝ |
---|
6 | λc. |
---|
7 | let b ≝ get_index_v ? 8 c 1 ? in |
---|
8 | [[ b; b; b; b; b; b; b; b ]] @@ c. |
---|
9 | normalize; |
---|
10 | repeat (@ (le_S_S ?)); |
---|
11 | @ (le_O_n); |
---|
12 | qed. |
---|
13 | |
---|
14 | lemma eq_a_to_eq: ∀a,b. eq_a a b = true → a=b. |
---|
15 | # a |
---|
16 | # b |
---|
17 | cases a |
---|
18 | cases b |
---|
19 | normalize |
---|
20 | # K |
---|
21 | try % |
---|
22 | cases (eq_true_false K) |
---|
23 | qed. |
---|
24 | |
---|
25 | lemma is_a_to_mem_to_is_in: |
---|
26 | ∀he,a,m,q. is_a he … a = true → mem … eq_a (S m) q he = true → is_in … q a = true. |
---|
27 | # he |
---|
28 | # a |
---|
29 | # m |
---|
30 | # q |
---|
31 | elim q |
---|
32 | [ normalize |
---|
33 | # _ |
---|
34 | # K |
---|
35 | assumption |
---|
36 | | # m' |
---|
37 | # t |
---|
38 | # q' |
---|
39 | # II |
---|
40 | # H1 |
---|
41 | # H2 |
---|
42 | normalize |
---|
43 | change with (orb ??) in H2: (??%?); |
---|
44 | cases (inclusive_disjunction_true … H2) |
---|
45 | [ # K |
---|
46 | < (eq_a_to_eq … K) |
---|
47 | > H1 |
---|
48 | % |
---|
49 | | # K |
---|
50 | > II |
---|
51 | try assumption |
---|
52 | cases (is_a t a) |
---|
53 | normalize |
---|
54 | % |
---|
55 | ] |
---|
56 | ] |
---|
57 | qed. |
---|
58 | |
---|
59 | lemma execute_1_technical: |
---|
60 | ∀n, m: nat. |
---|
61 | ∀v: Vector addressing_mode_tag (S n). |
---|
62 | ∀q: Vector addressing_mode_tag (S m). |
---|
63 | ∀a: addressing_mode. |
---|
64 | bool_to_Prop (is_in ? v a) → |
---|
65 | bool_to_Prop (subvector_with ? ? ? eq_a v q) → |
---|
66 | bool_to_Prop (is_in ? q a). |
---|
67 | # n |
---|
68 | # m |
---|
69 | # v |
---|
70 | # q |
---|
71 | # a |
---|
72 | elim v |
---|
73 | [ normalize |
---|
74 | # K |
---|
75 | cases K |
---|
76 | | # n' |
---|
77 | # he |
---|
78 | # tl |
---|
79 | # II |
---|
80 | whd in ⊢ (% → ? → ?); |
---|
81 | lapply (refl … (is_in … (he:::tl) a)) |
---|
82 | cases (is_in … (he:::tl) a) in ⊢ (???% → %); |
---|
83 | [ # K |
---|
84 | # _ |
---|
85 | normalize in K; |
---|
86 | whd in ⊢ (% → ?); |
---|
87 | lapply (refl … (subvector_with … eq_a (he:::tl) q)); |
---|
88 | cases (subvector_with … eq_a (he:::tl) q) in ⊢ (???% → %); |
---|
89 | [ # K1 |
---|
90 | # _ |
---|
91 | change with ((andb ? (subvector_with …)) = true) in K1; |
---|
92 | cases (conjunction_true … K1) |
---|
93 | # K3 |
---|
94 | # K4 |
---|
95 | cases (inclusive_disjunction_true … K) |
---|
96 | # K2 |
---|
97 | [ > (is_a_to_mem_to_is_in … K2 K3) |
---|
98 | % |
---|
99 | | @ II |
---|
100 | [ > K2 |
---|
101 | % |
---|
102 | | > K4 |
---|
103 | % |
---|
104 | ] |
---|
105 | ] |
---|
106 | | # K1 |
---|
107 | # K2 |
---|
108 | normalize in K2; |
---|
109 | cases K2; |
---|
110 | ] |
---|
111 | | # K1 |
---|
112 | # K2 |
---|
113 | normalize in K2; |
---|
114 | cases K2 |
---|
115 | ] |
---|
116 | ] |
---|
117 | qed. |
---|
118 | |
---|
119 | include alias "arithmetics/nat.ma". |
---|
120 | include alias "ASM/BitVectorTrie.ma". |
---|
121 | |
---|
122 | definition ASM_classify00: ∀a. preinstruction a → status_class ≝ |
---|
123 | λa, pre. |
---|
124 | match pre with |
---|
125 | [ RET ⇒ cl_return |
---|
126 | | RETI ⇒ cl_return |
---|
127 | | JZ _ ⇒ cl_jump |
---|
128 | | JNZ _ ⇒ cl_jump |
---|
129 | | JC _ ⇒ cl_jump |
---|
130 | | JNC _ ⇒ cl_jump |
---|
131 | | JB _ _ ⇒ cl_jump |
---|
132 | | JNB _ _ ⇒ cl_jump |
---|
133 | | JBC _ _ ⇒ cl_jump |
---|
134 | | CJNE _ _ ⇒ cl_jump |
---|
135 | | DJNZ _ _ ⇒ cl_jump |
---|
136 | | _ ⇒ cl_other |
---|
137 | ]. |
---|
138 | |
---|
139 | definition ASM_classify0: instruction → status_class ≝ |
---|
140 | λi. |
---|
141 | match i with |
---|
142 | [ RealInstruction pre ⇒ ASM_classify00 [[relative]] pre |
---|
143 | | ACALL _ ⇒ cl_call |
---|
144 | | LCALL _ ⇒ cl_call |
---|
145 | | JMP _ ⇒ cl_call |
---|
146 | | AJMP _ ⇒ cl_jump |
---|
147 | | LJMP _ ⇒ cl_jump |
---|
148 | | SJMP _ ⇒ cl_jump |
---|
149 | | _ ⇒ cl_other |
---|
150 | ]. |
---|
151 | |
---|
152 | definition current_instruction0 ≝ |
---|
153 | λcode_memory: BitVectorTrie Byte 16. |
---|
154 | λprogram_counter: Word. |
---|
155 | \fst (\fst (fetch … code_memory program_counter)). |
---|
156 | |
---|
157 | definition current_instruction ≝ |
---|
158 | λcode_memory. |
---|
159 | λs: Status code_memory. |
---|
160 | current_instruction0 code_memory (program_counter … s). |
---|
161 | |
---|
162 | definition ASM_classify: ∀code_memory. Status code_memory → status_class ≝ |
---|
163 | λcode_memory. |
---|
164 | λs: Status code_memory. |
---|
165 | ASM_classify0 (current_instruction … s). |
---|
166 | |
---|
167 | definition execute_1_preinstruction': |
---|
168 | ∀ticks: nat × nat. |
---|
169 | ∀a, m: Type[0]. ∀cm. (a → PreStatus m cm → Word) → ∀instr: preinstruction a. |
---|
170 | ∀s: PreStatus m cm. |
---|
171 | Σs': PreStatus m cm. |
---|
172 | And (Or (clock ?? s' = \fst ticks + clock … s) (clock ?? s' = \snd ticks + clock … s)) |
---|
173 | (ASM_classify00 a instr = cl_other → program_counter ?? s' = program_counter … s) ≝ |
---|
174 | λticks: nat × nat. |
---|
175 | λa, m: Type[0]. λcm. |
---|
176 | λaddr_of: a → PreStatus m cm → Word. |
---|
177 | λinstr: preinstruction a. |
---|
178 | λs: PreStatus m cm. |
---|
179 | let add_ticks1 ≝ λs: PreStatus m cm. set_clock ?? s (\fst ticks + clock ?? s) in |
---|
180 | let add_ticks2 ≝ λs: PreStatus m cm. set_clock ?? s (\snd ticks + clock ?? s) in |
---|
181 | match instr in preinstruction return λx. x = instr → Σs': PreStatus m cm. |
---|
182 | And (Or (clock ?? s' = \fst ticks + clock … s) (clock ?? s' = \snd ticks + clock … s)) |
---|
183 | (ASM_classify00 a instr = cl_other → program_counter ?? s' = program_counter … s) with |
---|
184 | [ ADD addr1 addr2 ⇒ λinstr_refl. |
---|
185 | let s ≝ add_ticks1 s in |
---|
186 | let 〈result, flags〉 ≝ add_8_with_carry (get_arg_8 … s false addr1) |
---|
187 | (get_arg_8 … s false addr2) false in |
---|
188 | let cy_flag ≝ get_index' ? O ? flags in |
---|
189 | let ac_flag ≝ get_index' ? 1 ? flags in |
---|
190 | let ov_flag ≝ get_index' ? 2 ? flags in |
---|
191 | let s ≝ set_arg_8 … s ACC_A result in |
---|
192 | set_flags … s cy_flag (Some Bit ac_flag) ov_flag |
---|
193 | | ADDC addr1 addr2 ⇒ λinstr_refl. |
---|
194 | let s ≝ add_ticks1 s in |
---|
195 | let old_cy_flag ≝ get_cy_flag ?? s in |
---|
196 | let 〈result, flags〉 ≝ add_8_with_carry (get_arg_8 … s false addr1) |
---|
197 | (get_arg_8 … s false addr2) old_cy_flag in |
---|
198 | let cy_flag ≝ get_index' ? O ? flags in |
---|
199 | let ac_flag ≝ get_index' ? 1 ? flags in |
---|
200 | let ov_flag ≝ get_index' ? 2 ? flags in |
---|
201 | let s ≝ set_arg_8 … s ACC_A result in |
---|
202 | set_flags … s cy_flag (Some Bit ac_flag) ov_flag |
---|
203 | | SUBB addr1 addr2 ⇒ λinstr_refl. |
---|
204 | let s ≝ add_ticks1 s in |
---|
205 | let old_cy_flag ≝ get_cy_flag ?? s in |
---|
206 | let 〈result, flags〉 ≝ sub_8_with_carry (get_arg_8 … s false addr1) |
---|
207 | (get_arg_8 … s false addr2) old_cy_flag in |
---|
208 | let cy_flag ≝ get_index' ? O ? flags in |
---|
209 | let ac_flag ≝ get_index' ? 1 ? flags in |
---|
210 | let ov_flag ≝ get_index' ? 2 ? flags in |
---|
211 | let s ≝ set_arg_8 … s ACC_A result in |
---|
212 | set_flags … s cy_flag (Some Bit ac_flag) ov_flag |
---|
213 | | ANL addr ⇒ λinstr_refl. |
---|
214 | let s ≝ add_ticks1 s in |
---|
215 | match addr with |
---|
216 | [ inl l ⇒ |
---|
217 | match l with |
---|
218 | [ inl l' ⇒ |
---|
219 | let 〈addr1, addr2〉 ≝ l' in |
---|
220 | let and_val ≝ conjunction_bv ? (get_arg_8 … s true addr1) (get_arg_8 … s true addr2) in |
---|
221 | set_arg_8 … s addr1 and_val |
---|
222 | | inr r ⇒ |
---|
223 | let 〈addr1, addr2〉 ≝ r in |
---|
224 | let and_val ≝ conjunction_bv ? (get_arg_8 … s true addr1) (get_arg_8 … s true addr2) in |
---|
225 | set_arg_8 … s addr1 and_val |
---|
226 | ] |
---|
227 | | inr r ⇒ |
---|
228 | let 〈addr1, addr2〉 ≝ r in |
---|
229 | let and_val ≝ andb (get_cy_flag … s) (get_arg_1 … s addr2 true) in |
---|
230 | set_flags … s and_val (None ?) (get_ov_flag ?? s) |
---|
231 | ] |
---|
232 | | ORL addr ⇒ λinstr_refl. |
---|
233 | let s ≝ add_ticks1 s in |
---|
234 | match addr with |
---|
235 | [ inl l ⇒ |
---|
236 | match l with |
---|
237 | [ inl l' ⇒ |
---|
238 | let 〈addr1, addr2〉 ≝ l' in |
---|
239 | let or_val ≝ inclusive_disjunction_bv ? (get_arg_8 … s true addr1) (get_arg_8 … s true addr2) in |
---|
240 | set_arg_8 … s addr1 or_val |
---|
241 | | inr r ⇒ |
---|
242 | let 〈addr1, addr2〉 ≝ r in |
---|
243 | let or_val ≝ inclusive_disjunction_bv ? (get_arg_8 … s true addr1) (get_arg_8 … s true addr2) in |
---|
244 | set_arg_8 … s addr1 or_val |
---|
245 | ] |
---|
246 | | inr r ⇒ |
---|
247 | let 〈addr1, addr2〉 ≝ r in |
---|
248 | let or_val ≝ (get_cy_flag … s) ∨ (get_arg_1 … s addr2 true) in |
---|
249 | set_flags … s or_val (None ?) (get_ov_flag … s) |
---|
250 | ] |
---|
251 | | XRL addr ⇒ λinstr_refl. |
---|
252 | let s ≝ add_ticks1 s in |
---|
253 | match addr with |
---|
254 | [ inl l' ⇒ |
---|
255 | let 〈addr1, addr2〉 ≝ l' in |
---|
256 | let xor_val ≝ exclusive_disjunction_bv ? (get_arg_8 … s true addr1) (get_arg_8 … s true addr2) in |
---|
257 | set_arg_8 … s addr1 xor_val |
---|
258 | | inr r ⇒ |
---|
259 | let 〈addr1, addr2〉 ≝ r in |
---|
260 | let xor_val ≝ exclusive_disjunction_bv ? (get_arg_8 … s true addr1) (get_arg_8 … s true addr2) in |
---|
261 | set_arg_8 … s addr1 xor_val |
---|
262 | ] |
---|
263 | | INC addr ⇒ λinstr_refl. |
---|
264 | match addr return λx. bool_to_Prop (is_in … [[ acc_a;registr;direct;indirect;dptr ]] x) → Σs': PreStatus m cm. ? with |
---|
265 | [ ACC_A ⇒ λacc_a: True. |
---|
266 | let s' ≝ add_ticks1 s in |
---|
267 | let 〈 carry, result 〉 ≝ half_add ? (get_arg_8 … s' true ACC_A) (bitvector_of_nat 8 1) in |
---|
268 | set_arg_8 … s' ACC_A result |
---|
269 | | REGISTER r ⇒ λregister: True. |
---|
270 | let s' ≝ add_ticks1 s in |
---|
271 | let 〈 carry, result 〉 ≝ half_add ? (get_arg_8 … s' true (REGISTER r)) (bitvector_of_nat 8 1) in |
---|
272 | set_arg_8 … s' (REGISTER r) result |
---|
273 | | DIRECT d ⇒ λdirect: True. |
---|
274 | let s' ≝ add_ticks1 s in |
---|
275 | let 〈 carry, result 〉 ≝ half_add ? (get_arg_8 … s' true (DIRECT d)) (bitvector_of_nat 8 1) in |
---|
276 | set_arg_8 … s' (DIRECT d) result |
---|
277 | | INDIRECT i ⇒ λindirect: True. |
---|
278 | let s' ≝ add_ticks1 s in |
---|
279 | let 〈 carry, result 〉 ≝ half_add ? (get_arg_8 … s' true (INDIRECT i)) (bitvector_of_nat 8 1) in |
---|
280 | set_arg_8 … s' (INDIRECT i) result |
---|
281 | | DPTR ⇒ λdptr: True. |
---|
282 | let s' ≝ add_ticks1 s in |
---|
283 | let 〈 carry, bl 〉 ≝ half_add ? (get_8051_sfr … s' SFR_DPL) (bitvector_of_nat 8 1) in |
---|
284 | let 〈 carry, bu 〉 ≝ full_add ? (get_8051_sfr … s' SFR_DPH) (zero 8) carry in |
---|
285 | let s ≝ set_8051_sfr … s' SFR_DPL bl in |
---|
286 | set_8051_sfr … s' SFR_DPH bu |
---|
287 | | _ ⇒ λother: False. ⊥ |
---|
288 | ] (subaddressing_modein … addr) |
---|
289 | | NOP ⇒ λinstr_refl. |
---|
290 | let s ≝ add_ticks2 s in |
---|
291 | s |
---|
292 | | DEC addr ⇒ λinstr_refl. |
---|
293 | let s ≝ add_ticks1 s in |
---|
294 | let 〈 result, flags 〉 ≝ sub_8_with_carry (get_arg_8 … s true addr) (bitvector_of_nat 8 1) false in |
---|
295 | set_arg_8 … s addr result |
---|
296 | | MUL addr1 addr2 ⇒ λinstr_refl. |
---|
297 | let s ≝ add_ticks1 s in |
---|
298 | let acc_a_nat ≝ nat_of_bitvector 8 (get_8051_sfr … s SFR_ACC_A) in |
---|
299 | let acc_b_nat ≝ nat_of_bitvector 8 (get_8051_sfr … s SFR_ACC_B) in |
---|
300 | let product ≝ acc_a_nat * acc_b_nat in |
---|
301 | let ov_flag ≝ product ≥ 256 in |
---|
302 | let low ≝ bitvector_of_nat 8 (product mod 256) in |
---|
303 | let high ≝ bitvector_of_nat 8 (product ÷ 256) in |
---|
304 | let s ≝ set_8051_sfr … s SFR_ACC_A low in |
---|
305 | set_8051_sfr … s SFR_ACC_B high |
---|
306 | | DIV addr1 addr2 ⇒ λinstr_refl. |
---|
307 | let s ≝ add_ticks1 s in |
---|
308 | let acc_a_nat ≝ nat_of_bitvector 8 (get_8051_sfr … s SFR_ACC_A) in |
---|
309 | let acc_b_nat ≝ nat_of_bitvector 8 (get_8051_sfr … s SFR_ACC_B) in |
---|
310 | match acc_b_nat with |
---|
311 | [ O ⇒ set_flags … s false (None Bit) true |
---|
312 | | S o ⇒ |
---|
313 | let q ≝ bitvector_of_nat 8 (acc_a_nat ÷ (S o)) in |
---|
314 | let r ≝ bitvector_of_nat 8 (acc_a_nat mod 256) in |
---|
315 | let s ≝ set_8051_sfr … s SFR_ACC_A q in |
---|
316 | let s ≝ set_8051_sfr … s SFR_ACC_B r in |
---|
317 | set_flags … s false (None Bit) false |
---|
318 | ] |
---|
319 | | DA addr ⇒ λinstr_refl. |
---|
320 | let s ≝ add_ticks1 s in |
---|
321 | let 〈acc_nu, acc_nl〉 ≝ split ? 4 4 (get_8051_sfr … s SFR_ACC_A) in |
---|
322 | if (gtb (nat_of_bitvector ? acc_nl) 9) ∨ (get_ac_flag … s) then |
---|
323 | let 〈result, flags〉 ≝ add_8_with_carry (get_8051_sfr … s SFR_ACC_A) (bitvector_of_nat 8 6) false in |
---|
324 | let cy_flag ≝ get_index' ? O ? flags in |
---|
325 | let 〈acc_nu', acc_nl'〉 ≝ split ? 4 4 result in |
---|
326 | if (gtb (nat_of_bitvector ? acc_nu') 9) ∨ cy_flag then |
---|
327 | let 〈 carry, nu 〉 ≝ half_add ? acc_nu' (bitvector_of_nat 4 6) in |
---|
328 | let new_acc ≝ nu @@ acc_nl' in |
---|
329 | let s ≝ set_8051_sfr … s SFR_ACC_A new_acc in |
---|
330 | set_flags … s cy_flag (Some ? (get_ac_flag … s)) (get_ov_flag … s) |
---|
331 | else |
---|
332 | s |
---|
333 | else |
---|
334 | s |
---|
335 | | CLR addr ⇒ λinstr_refl. |
---|
336 | match addr return λx. bool_to_Prop (is_in … [[ acc_a; carry; bit_addr ]] x) → Σs': PreStatus m cm. ? with |
---|
337 | [ ACC_A ⇒ λacc_a: True. |
---|
338 | let s ≝ add_ticks1 s in |
---|
339 | set_arg_8 … s ACC_A (zero 8) |
---|
340 | | CARRY ⇒ λcarry: True. |
---|
341 | let s ≝ add_ticks1 s in |
---|
342 | set_arg_1 … s CARRY false |
---|
343 | | BIT_ADDR b ⇒ λbit_addr: True. |
---|
344 | let s ≝ add_ticks1 s in |
---|
345 | set_arg_1 … s (BIT_ADDR b) false |
---|
346 | | _ ⇒ λother: False. ⊥ |
---|
347 | ] (subaddressing_modein … addr) |
---|
348 | | CPL addr ⇒ λinstr_refl. |
---|
349 | match addr return λx. bool_to_Prop (is_in … [[ acc_a; carry; bit_addr ]] x) → Σs': PreStatus m cm. ? with |
---|
350 | [ ACC_A ⇒ λacc_a: True. |
---|
351 | let s ≝ add_ticks1 s in |
---|
352 | let old_acc ≝ get_8051_sfr … s SFR_ACC_A in |
---|
353 | let new_acc ≝ negation_bv ? old_acc in |
---|
354 | set_8051_sfr … s SFR_ACC_A new_acc |
---|
355 | | CARRY ⇒ λcarry: True. |
---|
356 | let s ≝ add_ticks1 s in |
---|
357 | let old_cy_flag ≝ get_arg_1 … s CARRY true in |
---|
358 | let new_cy_flag ≝ ¬old_cy_flag in |
---|
359 | set_arg_1 … s CARRY new_cy_flag |
---|
360 | | BIT_ADDR b ⇒ λbit_addr: True. |
---|
361 | let s ≝ add_ticks1 s in |
---|
362 | let old_bit ≝ get_arg_1 … s (BIT_ADDR b) true in |
---|
363 | let new_bit ≝ ¬old_bit in |
---|
364 | set_arg_1 … s (BIT_ADDR b) new_bit |
---|
365 | | _ ⇒ λother: False. ? |
---|
366 | ] (subaddressing_modein … addr) |
---|
367 | | SETB b ⇒ λinstr_refl. |
---|
368 | let s ≝ add_ticks1 s in |
---|
369 | set_arg_1 … s b false |
---|
370 | | RL _ ⇒ λinstr_refl. (* DPM: always ACC_A *) |
---|
371 | let s ≝ add_ticks1 s in |
---|
372 | let old_acc ≝ get_8051_sfr … s SFR_ACC_A in |
---|
373 | let new_acc ≝ rotate_left … 1 old_acc in |
---|
374 | set_8051_sfr … s SFR_ACC_A new_acc |
---|
375 | | RR _ ⇒ λinstr_refl. (* DPM: always ACC_A *) |
---|
376 | let s ≝ add_ticks1 s in |
---|
377 | let old_acc ≝ get_8051_sfr … s SFR_ACC_A in |
---|
378 | let new_acc ≝ rotate_right … 1 old_acc in |
---|
379 | set_8051_sfr … s SFR_ACC_A new_acc |
---|
380 | | RLC _ ⇒ λinstr_refl. (* DPM: always ACC_A *) |
---|
381 | let s ≝ add_ticks1 s in |
---|
382 | let old_cy_flag ≝ get_cy_flag ?? s in |
---|
383 | let old_acc ≝ get_8051_sfr … s SFR_ACC_A in |
---|
384 | let new_cy_flag ≝ get_index' ? O ? old_acc in |
---|
385 | let new_acc ≝ shift_left … 1 old_acc old_cy_flag in |
---|
386 | let s ≝ set_arg_1 … s CARRY new_cy_flag in |
---|
387 | set_8051_sfr … s SFR_ACC_A new_acc |
---|
388 | | RRC _ ⇒ λinstr_refl. (* DPM: always ACC_A *) |
---|
389 | let s ≝ add_ticks1 s in |
---|
390 | let old_cy_flag ≝ get_cy_flag ?? s in |
---|
391 | let old_acc ≝ get_8051_sfr … s SFR_ACC_A in |
---|
392 | let new_cy_flag ≝ get_index' ? 7 ? old_acc in |
---|
393 | let new_acc ≝ shift_right … 1 old_acc old_cy_flag in |
---|
394 | let s ≝ set_arg_1 … s CARRY new_cy_flag in |
---|
395 | set_8051_sfr … s SFR_ACC_A new_acc |
---|
396 | | SWAP _ ⇒ λinstr_refl. (* DPM: always ACC_A *) |
---|
397 | let s ≝ add_ticks1 s in |
---|
398 | let old_acc ≝ get_8051_sfr … s SFR_ACC_A in |
---|
399 | let 〈nu,nl〉 ≝ split ? 4 4 old_acc in |
---|
400 | let new_acc ≝ nl @@ nu in |
---|
401 | set_8051_sfr … s SFR_ACC_A new_acc |
---|
402 | | PUSH addr ⇒ λinstr_refl. |
---|
403 | match addr return λx. bool_to_Prop (is_in … [[ direct ]] x) → Σs': PreStatus m cm. ? with |
---|
404 | [ DIRECT d ⇒ λdirect: True. |
---|
405 | let s ≝ add_ticks1 s in |
---|
406 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) in |
---|
407 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
408 | write_at_stack_pointer … s d |
---|
409 | | _ ⇒ λother: False. ⊥ |
---|
410 | ] (subaddressing_modein … addr) |
---|
411 | | POP addr ⇒ λinstr_refl. |
---|
412 | let s ≝ add_ticks1 s in |
---|
413 | let contents ≝ read_at_stack_pointer ?? s in |
---|
414 | let 〈new_sp, flags〉 ≝ sub_8_with_carry (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) false in |
---|
415 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
416 | set_arg_8 … s addr contents |
---|
417 | | XCH addr1 addr2 ⇒ λinstr_refl. |
---|
418 | let s ≝ add_ticks1 s in |
---|
419 | let old_addr ≝ get_arg_8 … s false addr2 in |
---|
420 | let old_acc ≝ get_8051_sfr … s SFR_ACC_A in |
---|
421 | let s ≝ set_8051_sfr … s SFR_ACC_A old_addr in |
---|
422 | set_arg_8 … s addr2 old_acc |
---|
423 | | XCHD addr1 addr2 ⇒ λinstr_refl. |
---|
424 | let s ≝ add_ticks1 s in |
---|
425 | let 〈acc_nu, acc_nl〉 ≝ split … 4 4 (get_8051_sfr … s SFR_ACC_A) in |
---|
426 | let 〈arg_nu, arg_nl〉 ≝ split … 4 4 (get_arg_8 … s false addr2) in |
---|
427 | let new_acc ≝ acc_nu @@ arg_nl in |
---|
428 | let new_arg ≝ arg_nu @@ acc_nl in |
---|
429 | let s ≝ set_8051_sfr ?? s SFR_ACC_A new_acc in |
---|
430 | set_arg_8 … s addr2 new_arg |
---|
431 | | RET ⇒ λinstr_refl. |
---|
432 | let s ≝ add_ticks1 s in |
---|
433 | let high_bits ≝ read_at_stack_pointer ?? s in |
---|
434 | let 〈new_sp, flags〉 ≝ sub_8_with_carry (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) false in |
---|
435 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
436 | let low_bits ≝ read_at_stack_pointer ?? s in |
---|
437 | let 〈new_sp, flags〉 ≝ sub_8_with_carry (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) false in |
---|
438 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
439 | let new_pc ≝ high_bits @@ low_bits in |
---|
440 | set_program_counter … s new_pc |
---|
441 | | RETI ⇒ λinstr_refl. |
---|
442 | let s ≝ add_ticks1 s in |
---|
443 | let high_bits ≝ read_at_stack_pointer ?? s in |
---|
444 | let 〈new_sp, flags〉 ≝ sub_8_with_carry (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) false in |
---|
445 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
446 | let low_bits ≝ read_at_stack_pointer ?? s in |
---|
447 | let 〈new_sp, flags〉 ≝ sub_8_with_carry (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) false in |
---|
448 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
449 | let new_pc ≝ high_bits @@ low_bits in |
---|
450 | set_program_counter … s new_pc |
---|
451 | | MOVX addr ⇒ λinstr_refl. |
---|
452 | let s ≝ add_ticks1 s in |
---|
453 | (* DPM: only copies --- doesn't affect I/O *) |
---|
454 | match addr with |
---|
455 | [ inl l ⇒ |
---|
456 | let 〈addr1, addr2〉 ≝ l in |
---|
457 | set_arg_8 … s addr1 (get_arg_8 … s false addr2) |
---|
458 | | inr r ⇒ |
---|
459 | let 〈addr1, addr2〉 ≝ r in |
---|
460 | set_arg_8 … s addr1 (get_arg_8 … s false addr2) |
---|
461 | ] |
---|
462 | | MOV addr ⇒ λinstr_refl. |
---|
463 | let s ≝ add_ticks1 s in |
---|
464 | match addr with |
---|
465 | [ inl l ⇒ |
---|
466 | match l with |
---|
467 | [ inl l' ⇒ |
---|
468 | match l' with |
---|
469 | [ inl l'' ⇒ |
---|
470 | match l'' with |
---|
471 | [ inl l''' ⇒ |
---|
472 | match l''' with |
---|
473 | [ inl l'''' ⇒ |
---|
474 | let 〈addr1, addr2〉 ≝ l'''' in |
---|
475 | set_arg_8 … s addr1 (get_arg_8 … s false addr2) |
---|
476 | | inr r'''' ⇒ |
---|
477 | let 〈addr1, addr2〉 ≝ r'''' in |
---|
478 | set_arg_8 … s addr1 (get_arg_8 … s false addr2) |
---|
479 | ] |
---|
480 | | inr r''' ⇒ |
---|
481 | let 〈addr1, addr2〉 ≝ r''' in |
---|
482 | set_arg_8 … s addr1 (get_arg_8 … s false addr2) |
---|
483 | ] |
---|
484 | | inr r'' ⇒ |
---|
485 | let 〈addr1, addr2〉 ≝ r'' in |
---|
486 | set_arg_16 … s (get_arg_16 … s addr2) addr1 |
---|
487 | ] |
---|
488 | | inr r ⇒ |
---|
489 | let 〈addr1, addr2〉 ≝ r in |
---|
490 | set_arg_1 … s addr1 (get_arg_1 … s addr2 false) |
---|
491 | ] |
---|
492 | | inr r ⇒ |
---|
493 | let 〈addr1, addr2〉 ≝ r in |
---|
494 | set_arg_1 … s addr1 (get_arg_1 … s addr2 false) |
---|
495 | ] |
---|
496 | | JC addr ⇒ λinstr_refl. |
---|
497 | if get_cy_flag ?? s then |
---|
498 | let s ≝ add_ticks1 s in |
---|
499 | set_program_counter … s (addr_of addr s) |
---|
500 | else |
---|
501 | let s ≝ add_ticks2 s in |
---|
502 | s |
---|
503 | | JNC addr ⇒ λinstr_refl. |
---|
504 | if ¬(get_cy_flag ?? s) then |
---|
505 | let s ≝ add_ticks1 s in |
---|
506 | set_program_counter … s (addr_of addr s) |
---|
507 | else |
---|
508 | let s ≝ add_ticks2 s in |
---|
509 | s |
---|
510 | | JB addr1 addr2 ⇒ λinstr_refl. |
---|
511 | if get_arg_1 … s addr1 false then |
---|
512 | let s ≝ add_ticks1 s in |
---|
513 | set_program_counter … s (addr_of addr2 s) |
---|
514 | else |
---|
515 | let s ≝ add_ticks2 s in |
---|
516 | s |
---|
517 | | JNB addr1 addr2 ⇒ λinstr_refl. |
---|
518 | if ¬(get_arg_1 … s addr1 false) then |
---|
519 | let s ≝ add_ticks1 s in |
---|
520 | set_program_counter … s (addr_of addr2 s) |
---|
521 | else |
---|
522 | let s ≝ add_ticks2 s in |
---|
523 | s |
---|
524 | | JBC addr1 addr2 ⇒ λinstr_refl. |
---|
525 | let s ≝ set_arg_1 … s addr1 false in |
---|
526 | if get_arg_1 … s addr1 false then |
---|
527 | let s ≝ add_ticks1 s in |
---|
528 | set_program_counter … s (addr_of addr2 s) |
---|
529 | else |
---|
530 | let s ≝ add_ticks2 s in |
---|
531 | s |
---|
532 | | JZ addr ⇒ λinstr_refl. |
---|
533 | if eq_bv ? (get_8051_sfr … s SFR_ACC_A) (zero 8) then |
---|
534 | let s ≝ add_ticks1 s in |
---|
535 | set_program_counter … s (addr_of addr s) |
---|
536 | else |
---|
537 | let s ≝ add_ticks2 s in |
---|
538 | s |
---|
539 | | JNZ addr ⇒ λinstr_refl. |
---|
540 | if ¬(eq_bv ? (get_8051_sfr … s SFR_ACC_A) (zero 8)) then |
---|
541 | let s ≝ add_ticks1 s in |
---|
542 | set_program_counter … s (addr_of addr s) |
---|
543 | else |
---|
544 | let s ≝ add_ticks2 s in |
---|
545 | s |
---|
546 | | CJNE addr1 addr2 ⇒ λinstr_refl. |
---|
547 | match addr1 with |
---|
548 | [ inl l ⇒ |
---|
549 | let 〈addr1, addr2'〉 ≝ l in |
---|
550 | let new_cy ≝ ltb (nat_of_bitvector ? (get_arg_8 … s false addr1)) |
---|
551 | (nat_of_bitvector ? (get_arg_8 … s false addr2')) in |
---|
552 | if ¬(eq_bv ? (get_arg_8 … s false addr1) (get_arg_8 … s false addr2')) then |
---|
553 | let s ≝ add_ticks1 s in |
---|
554 | let s ≝ set_program_counter … s (addr_of addr2 s) in |
---|
555 | set_flags … s new_cy (None ?) (get_ov_flag ?? s) |
---|
556 | else |
---|
557 | let s ≝ add_ticks2 s in |
---|
558 | set_flags … s new_cy (None ?) (get_ov_flag ?? s) |
---|
559 | | inr r' ⇒ |
---|
560 | let 〈addr1, addr2'〉 ≝ r' in |
---|
561 | let new_cy ≝ ltb (nat_of_bitvector ? (get_arg_8 … s false addr1)) |
---|
562 | (nat_of_bitvector ? (get_arg_8 … s false addr2')) in |
---|
563 | if ¬(eq_bv ? (get_arg_8 … s false addr1) (get_arg_8 … s false addr2')) then |
---|
564 | let s ≝ add_ticks1 s in |
---|
565 | let s ≝ set_program_counter … s (addr_of addr2 s) in |
---|
566 | set_flags … s new_cy (None ?) (get_ov_flag ?? s) |
---|
567 | else |
---|
568 | let s ≝ add_ticks2 s in |
---|
569 | set_flags … s new_cy (None ?) (get_ov_flag ?? s) |
---|
570 | ] |
---|
571 | | DJNZ addr1 addr2 ⇒ λinstr_refl. |
---|
572 | let 〈result, flags〉 ≝ sub_8_with_carry (get_arg_8 … s true addr1) (bitvector_of_nat 8 1) false in |
---|
573 | let s ≝ set_arg_8 … s addr1 result in |
---|
574 | if ¬(eq_bv ? result (zero 8)) then |
---|
575 | let s ≝ add_ticks1 s in |
---|
576 | set_program_counter … s (addr_of addr2 s) |
---|
577 | else |
---|
578 | let s ≝ add_ticks2 s in |
---|
579 | s |
---|
580 | ] (refl … instr). |
---|
581 | try (cases(other)) |
---|
582 | try assumption (*20s*) |
---|
583 | try (% @False) (*6s*) (* Bug exploited here to implement solve :-*) |
---|
584 | try ( |
---|
585 | @(execute_1_technical … (subaddressing_modein …)) |
---|
586 | @I |
---|
587 | ) (*66s*) |
---|
588 | normalize nodelta % |
---|
589 | try (<instr_refl change with (cl_jump = cl_other → ?) #absurd destruct(absurd)) |
---|
590 | try (<instr_refl change with (cl_return = cl_other → ?) #absurd destruct(absurd)) |
---|
591 | try (@or_introl //) |
---|
592 | try (@or_intror //) |
---|
593 | #_ /demod/ % |
---|
594 | qed. |
---|
595 | |
---|
596 | definition execute_1_preinstruction: |
---|
597 | ∀ticks: nat × nat. |
---|
598 | ∀a, m: Type[0]. ∀cm. (a → PreStatus m cm → Word) → preinstruction a → |
---|
599 | PreStatus m cm → PreStatus m cm ≝ execute_1_preinstruction'. |
---|
600 | |
---|
601 | lemma execute_1_preinstruction_ok: |
---|
602 | ∀ticks,a,m,cm,f,i,s. |
---|
603 | (clock ?? (execute_1_preinstruction ticks a m cm f i s) = \fst ticks + clock … s ∨ |
---|
604 | clock ?? (execute_1_preinstruction ticks a m cm f i s) = \snd ticks + clock … s) ∧ |
---|
605 | (ASM_classify00 a i = cl_other → program_counter ?? (execute_1_preinstruction ticks a m cm f i s) = program_counter … s). |
---|
606 | #ticks #a #m #cm #f #i #s whd in match execute_1_preinstruction; normalize nodelta @pi2 |
---|
607 | qed. |
---|
608 | |
---|
609 | discriminator Prod. |
---|
610 | |
---|
611 | definition execute_1_0: ∀cm. ∀s: Status cm. ∀current:instruction × Word × nat. |
---|
612 | Σs': Status cm. |
---|
613 | And (clock ?? s' = \snd current + clock … s) |
---|
614 | (ASM_classify0 (\fst (\fst current)) = cl_other → |
---|
615 | program_counter ? ? s' = \snd (\fst current)) ≝ |
---|
616 | λcm,s0,instr_pc_ticks. |
---|
617 | let 〈instr_pc, ticks〉 as INSTR_PC_TICKS ≝ instr_pc_ticks in |
---|
618 | let 〈instr, pc〉 as INSTR_PC ≝ 〈fst … instr_pc, snd … instr_pc〉 in |
---|
619 | let s ≝ set_program_counter … s0 pc in |
---|
620 | match instr return λx. x = instr → Σs:?.? with |
---|
621 | [ RealInstruction instr' ⇒ λinstr_refl. execute_1_preinstruction 〈ticks, ticks〉 [[ relative ]] … |
---|
622 | (λx. λs. |
---|
623 | match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → Word with |
---|
624 | [ RELATIVE r ⇒ λ_. \snd (half_add ? (program_counter … s) (sign_extension r)) |
---|
625 | | _ ⇒ λabsd. ⊥ |
---|
626 | ] (subaddressing_modein … x)) instr' s |
---|
627 | | MOVC addr1 addr2 ⇒ λinstr_refl. |
---|
628 | let s ≝ set_clock ?? s (ticks + clock … s) in |
---|
629 | match addr2 return λx. bool_to_Prop (is_in … [[ acc_dptr; acc_pc ]] x) → Σs':?. ? with |
---|
630 | [ ACC_DPTR ⇒ λacc_dptr: True. |
---|
631 | let big_acc ≝ (zero 8) @@ (get_8051_sfr … s SFR_ACC_A) in |
---|
632 | let dptr ≝ (get_8051_sfr … s SFR_DPH) @@ (get_8051_sfr … s SFR_DPL) in |
---|
633 | let 〈carry, new_addr〉 ≝ half_add ? dptr big_acc in |
---|
634 | let result ≝ lookup ? ? new_addr cm (zero ?) in |
---|
635 | set_8051_sfr … s SFR_ACC_A result |
---|
636 | | ACC_PC ⇒ λacc_pc: True. |
---|
637 | let big_acc ≝ (zero 8) @@ (get_8051_sfr … s SFR_ACC_A) in |
---|
638 | (* DPM: Under specified: does the carry from PC incrementation affect the *) |
---|
639 | (* addition of the PC with the DPTR? At the moment, no. *) |
---|
640 | let 〈carry, new_addr〉 ≝ half_add ? (program_counter … s) big_acc in |
---|
641 | let result ≝ lookup ? ? new_addr cm (zero ?) in |
---|
642 | set_8051_sfr … s SFR_ACC_A result |
---|
643 | | _ ⇒ λother: False. ⊥ |
---|
644 | ] (subaddressing_modein … addr2) |
---|
645 | | JMP _ ⇒ λinstr_refl. (* DPM: always indirect_dptr *) |
---|
646 | let s ≝ set_clock ?? s (ticks + clock … s) in |
---|
647 | let dptr ≝ (get_8051_sfr … s SFR_DPH) @@ (get_8051_sfr … s SFR_DPL) in |
---|
648 | let big_acc ≝ (zero 8) @@ (get_8051_sfr … s SFR_ACC_A) in |
---|
649 | let 〈carry, jmp_addr〉 ≝ half_add ? big_acc dptr in |
---|
650 | let 〈carry, new_pc〉 ≝ half_add ? (program_counter … s) jmp_addr in |
---|
651 | set_program_counter … s new_pc |
---|
652 | | LJMP addr ⇒ λinstr_refl. |
---|
653 | let s ≝ set_clock ?? s (ticks + clock … s) in |
---|
654 | match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Σs': ?.? with |
---|
655 | [ ADDR16 a ⇒ λaddr16: True. |
---|
656 | set_program_counter … s a |
---|
657 | | _ ⇒ λother: False. ⊥ |
---|
658 | ] (subaddressing_modein … addr) |
---|
659 | | SJMP addr ⇒ λinstr_refl. |
---|
660 | let s ≝ set_clock ?? s (ticks + clock … s) in |
---|
661 | match addr return λx. bool_to_Prop (is_in … [[ relative ]] x) → Σs':?.? with |
---|
662 | [ RELATIVE r ⇒ λrelative: True. |
---|
663 | let 〈carry, new_pc〉 ≝ half_add ? (program_counter … s) (sign_extension r) in |
---|
664 | set_program_counter … s new_pc |
---|
665 | | _ ⇒ λother: False. ⊥ |
---|
666 | ] (subaddressing_modein … addr) |
---|
667 | | AJMP addr ⇒ λinstr_refl. |
---|
668 | let s ≝ set_clock ?? s (ticks + clock … s) in |
---|
669 | match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Σs':?. ? with |
---|
670 | [ ADDR11 a ⇒ λaddr11: True. |
---|
671 | let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter … s) in |
---|
672 | let 〈nu, nl〉 ≝ split ? 4 4 pc_bu in |
---|
673 | let bit ≝ get_index' ? O ? nl in |
---|
674 | let 〈relevant1, relevant2〉 ≝ split ? 3 8 a in |
---|
675 | let new_addr ≝ (nu @@ (bit ::: relevant1)) @@ relevant2 in |
---|
676 | let 〈carry, new_pc〉 ≝ half_add ? (program_counter … s) new_addr in |
---|
677 | set_program_counter … s new_pc |
---|
678 | | _ ⇒ λother: False. ⊥ |
---|
679 | ] (subaddressing_modein … addr) |
---|
680 | | ACALL addr ⇒ λinstr_refl. |
---|
681 | let s ≝ set_clock ?? s (ticks + clock … s) in |
---|
682 | match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Σs':?. ? with |
---|
683 | [ ADDR11 a ⇒ λaddr11: True. |
---|
684 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) in |
---|
685 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
686 | let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter … s) in |
---|
687 | let s ≝ write_at_stack_pointer … s pc_bl in |
---|
688 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) in |
---|
689 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
690 | let s ≝ write_at_stack_pointer … s pc_bu in |
---|
691 | let 〈thr, eig〉 ≝ split ? 3 8 a in |
---|
692 | let 〈fiv, thr'〉 ≝ split ? 5 3 pc_bu in |
---|
693 | let new_addr ≝ (fiv @@ thr) @@ pc_bl in |
---|
694 | set_program_counter … s new_addr |
---|
695 | | _ ⇒ λother: False. ⊥ |
---|
696 | ] (subaddressing_modein … addr) |
---|
697 | | LCALL addr ⇒ λinstr_refl. |
---|
698 | let s ≝ set_clock ?? s (ticks + clock … s) in |
---|
699 | match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Σs':?. ? with |
---|
700 | [ ADDR16 a ⇒ λaddr16: True. |
---|
701 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) in |
---|
702 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
703 | let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter … s) in |
---|
704 | let s ≝ write_at_stack_pointer … s pc_bl in |
---|
705 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) in |
---|
706 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
707 | let s ≝ write_at_stack_pointer … s pc_bu in |
---|
708 | set_program_counter … s a |
---|
709 | | _ ⇒ λother: False. ⊥ |
---|
710 | ] (subaddressing_modein … addr) |
---|
711 | ] (refl … instr). (*10s*) |
---|
712 | try assumption |
---|
713 | [1,2,3,4,5,6,7,8: |
---|
714 | normalize nodelta >clock_set_program_counter <INSTR_PC_TICKS % |
---|
715 | try // |
---|
716 | destruct(INSTR_PC) <instr_refl |
---|
717 | #absurd normalize in absurd; try destruct(absurd) try % |
---|
718 | |9: |
---|
719 | cases (execute_1_preinstruction_ok 〈ticks, ticks〉 [[ relative ]] ?? |
---|
720 | (λx. λs. |
---|
721 | match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → Word with |
---|
722 | [ RELATIVE r ⇒ λ_. \snd (half_add ? (program_counter … s) (sign_extension r)) |
---|
723 | | _ ⇒ λabsd. ⊥ |
---|
724 | ] (subaddressing_modein … x)) instr' s) try @absd |
---|
725 | #clock_proof #classify_proof % |
---|
726 | [1: |
---|
727 | cases clock_proof #clock_proof' >clock_proof' |
---|
728 | destruct(INSTR_PC_TICKS) % |
---|
729 | |2: |
---|
730 | #classify_assm -clock_proof >classify_proof -classify_proof |
---|
731 | [1: |
---|
732 | normalize nodelta normalize <INSTR_PC_TICKS |
---|
733 | destruct(INSTR_PC) % |
---|
734 | |2: |
---|
735 | <classify_assm <INSTR_PC_TICKS destruct <e0 % |
---|
736 | ] |
---|
737 | ] |
---|
738 | ] |
---|
739 | qed. |
---|
740 | |
---|
741 | definition current_instruction_cost ≝ |
---|
742 | λcm.λs: Status cm. |
---|
743 | \snd (fetch cm (program_counter … s)). |
---|
744 | |
---|
745 | definition execute_1': ∀cm.∀s:Status cm. |
---|
746 | Σs':Status cm. |
---|
747 | And (clock ?? s' = current_instruction_cost cm s + clock … s) |
---|
748 | (ASM_classify cm s = cl_other → \snd (\fst (fetch cm (program_counter … s))) = program_counter … s') ≝ |
---|
749 | λcm. λs: Status cm. |
---|
750 | let instr_pc_ticks ≝ fetch cm (program_counter … s) in |
---|
751 | pi1 ?? (execute_1_0 cm s instr_pc_ticks). |
---|
752 | % |
---|
753 | [1: |
---|
754 | cases(execute_1_0 cm s instr_pc_ticks) |
---|
755 | #the_status * #clock_assm #_ @clock_assm |
---|
756 | |2: |
---|
757 | cases(execute_1_0 cm s instr_pc_ticks) |
---|
758 | #the_status * #_ #classify_assm #classify_assm' |
---|
759 | lapply(classify_assm ?) |
---|
760 | [1: |
---|
761 | change with ( |
---|
762 | ASM_classify cm s = cl_other |
---|
763 | ) |
---|
764 | assumption |
---|
765 | |2: |
---|
766 | #program_counter_refl >program_counter_refl % |
---|
767 | ] |
---|
768 | ] |
---|
769 | qed. |
---|
770 | |
---|
771 | definition execute_1: ∀cm. Status cm → Status cm ≝ execute_1'. |
---|
772 | |
---|
773 | lemma execute_1_ok: ∀cm.∀s. |
---|
774 | (clock ?? (execute_1 cm s) = current_instruction_cost … s + clock … s) ∧ |
---|
775 | (ASM_classify cm s = cl_other → \snd (\fst (fetch cm (program_counter … s))) = program_counter … (execute_1 cm s)). |
---|
776 | #cm #s whd in match execute_1; normalize nodelta @pi2 |
---|
777 | qed-. (*x Andrea: indexing takes ages here *) |
---|
778 | |
---|
779 | definition execute_1_pseudo_instruction0: (nat × nat) → ∀cm. PseudoStatus cm → ? → ? → PseudoStatus cm ≝ |
---|
780 | λticks,cm,s,instr,pc. |
---|
781 | let s ≝ set_program_counter ?? s pc in |
---|
782 | let s ≝ |
---|
783 | match instr with |
---|
784 | [ Instruction instr ⇒ execute_1_preinstruction ticks … (λx, y. address_of_word_labels cm x) instr s |
---|
785 | | Comment cmt ⇒ set_clock … s (\fst ticks + clock … s) |
---|
786 | | Cost cst ⇒ s |
---|
787 | | Jmp jmp ⇒ |
---|
788 | let s ≝ set_clock … s (\fst ticks + clock … s) in |
---|
789 | set_program_counter … s (address_of_word_labels cm jmp) |
---|
790 | | Call call ⇒ |
---|
791 | let s ≝ set_clock ?? s (\fst ticks + clock … s) in |
---|
792 | let a ≝ address_of_word_labels cm call in |
---|
793 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) in |
---|
794 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
795 | let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter … s) in |
---|
796 | let s ≝ write_at_stack_pointer … s pc_bl in |
---|
797 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr … s SFR_SP) (bitvector_of_nat 8 1) in |
---|
798 | let s ≝ set_8051_sfr … s SFR_SP new_sp in |
---|
799 | let s ≝ write_at_stack_pointer … s pc_bu in |
---|
800 | set_program_counter … s a |
---|
801 | | Mov dptr ident ⇒ |
---|
802 | let s ≝ set_clock ?? s (\fst ticks + clock … s) in |
---|
803 | let the_preamble ≝ \fst cm in |
---|
804 | let data_labels ≝ construct_datalabels the_preamble in |
---|
805 | set_arg_16 … s (get_arg_16 … s (DATA16 (lookup_def ? ? data_labels ident (zero ?)))) dptr |
---|
806 | ] |
---|
807 | in |
---|
808 | s. |
---|
809 | normalize |
---|
810 | @I |
---|
811 | qed. |
---|
812 | |
---|
813 | definition execute_1_pseudo_instruction: (Word → nat × nat) → ∀cm. PseudoStatus cm → PseudoStatus cm ≝ |
---|
814 | λticks_of,cm,s. |
---|
815 | let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd cm) (program_counter … s) in |
---|
816 | let ticks ≝ ticks_of (program_counter … s) in |
---|
817 | execute_1_pseudo_instruction0 ticks cm s instr pc. |
---|
818 | |
---|
819 | let rec execute (n: nat) (cm:?) (s: Status cm) on n: Status cm ≝ |
---|
820 | match n with |
---|
821 | [ O ⇒ s |
---|
822 | | S o ⇒ execute o … (execute_1 … s) |
---|
823 | ]. |
---|
824 | |
---|
825 | let rec execute_pseudo_instruction (n: nat) (ticks_of: Word → nat × nat) (cm:?) (s: PseudoStatus cm) on n: PseudoStatus cm ≝ |
---|
826 | match n with |
---|
827 | [ O ⇒ s |
---|
828 | | S o ⇒ execute_pseudo_instruction o ticks_of … (execute_1_pseudo_instruction ticks_of … s) |
---|
829 | ]. |
---|