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