source: src/ASM/Status.ma @ 924

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

Type of set_code_memory generalized.

File size: 36.1 KB
Line 
1(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *)
2(* Interpret.ma: Operational semantics for the 8051/8052 processor.           *)
3(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *)
4
5include "ASM/ASM.ma".
6include "ASM/Arithmetic.ma".
7include "ASM/BitVectorTrie.ma".
8
9definition Time ≝ nat.
10
11inductive SerialBufferType: Type[0] ≝
12  Eight: Byte → SerialBufferType
13| Nine: Bit → Byte → SerialBufferType.
14
15inductive LineType: Type[0] ≝
16  P1: Byte → LineType
17| P3: Byte → LineType
18| SerialBuffer: SerialBufferType → LineType.
19
20(* What is a continuation, now? *)
21
22inductive SFR8051: Type[0] ≝
23  SFR_SP: SFR8051
24| SFR_DPL: SFR8051
25| SFR_DPH: SFR8051
26| SFR_PCON: SFR8051
27| SFR_TCON: SFR8051
28| SFR_TMOD: SFR8051
29| SFR_TL0: SFR8051
30| SFR_TL1: SFR8051
31| SFR_TH0: SFR8051
32| SFR_TH1: SFR8051
33| SFR_P1: SFR8051
34| SFR_SCON: SFR8051
35| SFR_SBUF: SFR8051
36| SFR_IE: SFR8051
37| SFR_P3: SFR8051
38| SFR_IP: SFR8051
39| SFR_PSW: SFR8051
40| SFR_ACC_A: SFR8051
41| SFR_ACC_B: SFR8051.
42
43definition sfr_8051_index ≝
44  λs: SFR8051.
45    match s with
46      [ SFR_SP ⇒ O
47      | SFR_DPL ⇒ 1
48      | SFR_DPH ⇒ 2
49      | SFR_PCON ⇒ 3
50      | SFR_TCON ⇒ 4
51      | SFR_TMOD ⇒ 5
52      | SFR_TL0 ⇒ 6
53      | SFR_TL1 ⇒ 7
54      | SFR_TH0 ⇒ 8
55      | SFR_TH1 ⇒ 9
56      | SFR_P1 ⇒ 10
57      | SFR_SCON ⇒ 11
58      | SFR_SBUF ⇒ 12
59      | SFR_IE ⇒ 13
60      | SFR_P3 ⇒ 14
61      | SFR_IP ⇒ 15
62      | SFR_PSW ⇒ 16
63      | SFR_ACC_A ⇒ 17
64      | SFR_ACC_B ⇒ 18
65      ].
66     
67inductive SFR8052: Type[0] ≝
68   SFR_T2CON: SFR8052
69|  SFR_RCAP2L: SFR8052
70|  SFR_RCAP2H: SFR8052
71|  SFR_TL2: SFR8052
72|  SFR_TH2: SFR8052.
73
74definition sfr_8052_index ≝
75  λs: SFR8052.
76    match s with
77      [ SFR_T2CON ⇒ O
78      | SFR_RCAP2L ⇒ 1
79      | SFR_RCAP2H ⇒ 2
80      | SFR_TL2 ⇒ 3
81      | SFR_TH2 ⇒ 4
82      ].
83
84(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *)
85(* Processor status.                                                          *)
86(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *)
87record PreStatus (M: Type[0]): Type[0] ≝
88{
89  code_memory: M;
90  low_internal_ram: BitVectorTrie Byte 7;
91  high_internal_ram: BitVectorTrie Byte 7;
92  external_ram: BitVectorTrie Byte 16;
93 
94  program_counter: Word;
95 
96  special_function_registers_8051: Vector Byte 19;
97  special_function_registers_8052: Vector Byte 5;
98 
99  p1_latch: Byte;
100  p3_latch: Byte;
101 
102  clock: Time
103}.
104
105definition Status ≝ PreStatus (BitVectorTrie Byte 16).
106definition PseudoStatus ≝ PreStatus (pseudo_assembly_program).
107
108lemma sfr8051_index_19:
109  ∀i: SFR8051.
110    sfr_8051_index i < 19.
111 # i
112 cases i
113 normalize
114 repeat (@ le_S_S)
115 @ le_O_n
116qed.
117   
118lemma sfr8052_index_5:
119  ∀i: SFR8052.
120    sfr_8052_index i < 5.
121 # i
122 cases i
123 normalize
124 repeat (@ le_S_S)
125 @ le_O_n
126qed.
127   
128definition set_clock ≝
129  λM: Type[0].
130  λs: PreStatus M.
131  λt: Time.
132    let old_code_memory ≝ code_memory ? s in
133    let old_low_internal_ram ≝ low_internal_ram ? s in
134    let old_high_internal_ram ≝ high_internal_ram ? s in
135    let old_external_ram ≝ external_ram ? s in
136    let old_program_counter ≝ program_counter ? s in
137    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
138    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
139    let old_p1_latch ≝ p1_latch ? s in   
140    let old_p3_latch ≝ p3_latch ? s in
141      mk_PreStatus M old_code_memory
142                old_low_internal_ram
143                old_high_internal_ram
144                old_external_ram
145                old_program_counter
146                old_special_function_registers_8051
147                old_special_function_registers_8052
148                old_p1_latch
149                old_p3_latch
150                t.
151   
152definition set_p1_latch ≝
153  λM: Type[0].
154  λs: PreStatus M.
155  λb: Byte.
156    let old_code_memory ≝ code_memory ? s in
157    let old_low_internal_ram ≝ low_internal_ram ? s in
158    let old_high_internal_ram ≝ high_internal_ram ? s in
159    let old_external_ram ≝ external_ram ? s in
160    let old_program_counter ≝ program_counter ? s in
161    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
162    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
163    let old_p3_latch ≝ p3_latch ? s in
164    let old_clock ≝ clock ? s in
165      mk_PreStatus M old_code_memory
166                old_low_internal_ram
167                old_high_internal_ram
168                old_external_ram
169                old_program_counter
170                old_special_function_registers_8051
171                old_special_function_registers_8052
172                b
173                old_p3_latch
174                old_clock.
175
176definition set_p3_latch ≝
177  λM: Type[0].
178  λs: PreStatus M.
179  λb: Byte.
180    let old_code_memory ≝ code_memory ? s in
181    let old_low_internal_ram ≝ low_internal_ram ? s in
182    let old_high_internal_ram ≝ high_internal_ram ? s in
183    let old_external_ram ≝ external_ram ? s in
184    let old_program_counter ≝ program_counter ? s in
185    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
186    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
187    let old_p1_latch ≝ p1_latch ? s in
188    let old_clock ≝ clock ? s in
189      mk_PreStatus M old_code_memory
190                old_low_internal_ram
191                old_high_internal_ram
192                old_external_ram
193                old_program_counter
194                old_special_function_registers_8051
195                old_special_function_registers_8052
196                old_p1_latch
197                b
198                old_clock.
199
200definition get_8051_sfr ≝
201  λM: Type[0].
202  λs: PreStatus M.
203  λi: SFR8051.
204    let sfr ≝ special_function_registers_8051 ? s in
205    let index ≝ sfr_8051_index i in
206      get_index_v … sfr index ?.
207    @ sfr8051_index_19
208qed.
209
210definition get_8052_sfr ≝
211  λM: Type[0].
212  λs: PreStatus M.
213  λi: SFR8052.
214    let sfr ≝ special_function_registers_8052 ? s in
215    let index ≝ sfr_8052_index i in
216      get_index_v … sfr index ?.
217    @ sfr8052_index_5
218qed.
219
220definition set_8051_sfr ≝
221  λM: Type[0].
222  λs: PreStatus M.
223  λi: SFR8051.
224  λb: Byte.
225    let index ≝ sfr_8051_index i in
226    let old_code_memory ≝ code_memory ? s in
227    let old_low_internal_ram ≝ low_internal_ram ? s in
228    let old_high_internal_ram ≝ high_internal_ram ? s in
229    let old_external_ram ≝ external_ram ? s in
230    let old_program_counter ≝ program_counter ? s in
231    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
232    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
233    let new_special_function_registers_8051 ≝
234      set_index Byte 19 old_special_function_registers_8051 index b ? in
235    let old_p1_latch ≝ p1_latch ? s in
236    let old_p3_latch ≝ p3_latch ? s in
237    let old_clock ≝ clock ? s in
238      mk_PreStatus M old_code_memory
239                old_low_internal_ram
240                old_high_internal_ram
241                old_external_ram
242                old_program_counter
243                new_special_function_registers_8051
244                old_special_function_registers_8052
245                old_p1_latch
246                old_p3_latch
247                old_clock.
248    @ (sfr8051_index_19 i)
249qed.
250
251definition set_8052_sfr ≝
252  λM: Type[0].
253  λs: PreStatus M.
254  λi: SFR8052.
255  λb: Byte.
256    let index ≝ sfr_8052_index i in
257    let old_code_memory ≝ code_memory ? s in
258    let old_low_internal_ram ≝ low_internal_ram ? s in
259    let old_high_internal_ram ≝ high_internal_ram ? s in
260    let old_external_ram ≝ external_ram ? s in
261    let old_program_counter ≝ program_counter ? s in
262    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
263    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
264    let new_special_function_registers_8052 ≝
265      set_index Byte 5 old_special_function_registers_8052 index b ? in
266    let old_p1_latch ≝ p1_latch ? s in
267    let old_p3_latch ≝ p3_latch ? s in
268    let old_clock ≝ clock ? s in
269      mk_PreStatus M old_code_memory
270                old_low_internal_ram
271                old_high_internal_ram
272                old_external_ram
273                old_program_counter
274                old_special_function_registers_8051
275                new_special_function_registers_8052
276                old_p1_latch
277                old_p3_latch
278                old_clock.
279    @ (sfr8052_index_5 i)
280qed.
281
282definition set_program_counter ≝
283  λM: Type[0].
284  λs: PreStatus M.
285  λw: Word.
286    let old_code_memory ≝ code_memory ? s in
287    let old_low_internal_ram ≝ low_internal_ram ? s in
288    let old_high_internal_ram ≝ high_internal_ram ? s in
289    let old_external_ram ≝ external_ram ? s in
290    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
291    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
292    let old_p1_latch ≝ p1_latch ? s in
293    let old_p3_latch ≝ p3_latch ? s in
294    let old_clock ≝ clock ? s in
295      mk_PreStatus M old_code_memory
296                old_low_internal_ram
297                old_high_internal_ram
298                old_external_ram
299                w
300                old_special_function_registers_8051
301                old_special_function_registers_8052
302                old_p1_latch
303                old_p3_latch
304                old_clock.
305               
306definition set_code_memory ≝
307  λM,M': Type[0].
308  λs: PreStatus M.
309  λr: M'.
310    let old_low_internal_ram ≝ low_internal_ram ? s in
311    let old_high_internal_ram ≝ high_internal_ram ? s in
312    let old_external_ram ≝ external_ram ? s in
313    let old_program_counter ≝ program_counter ? s in
314    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
315    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
316    let old_p1_latch ≝ p1_latch ? s in
317    let old_p3_latch ≝ p3_latch ? s in
318    let old_clock ≝ clock ? s in
319      mk_PreStatus M' r
320                old_low_internal_ram
321                old_high_internal_ram
322                old_external_ram
323                old_program_counter
324                old_special_function_registers_8051
325                old_special_function_registers_8052
326                old_p1_latch
327                old_p3_latch
328                old_clock.
329               
330definition set_low_internal_ram ≝
331  λM: Type[0].
332  λs: PreStatus M.
333  λr: BitVectorTrie Byte 7.
334    let old_code_memory ≝ code_memory ? s in
335    let old_high_internal_ram ≝ high_internal_ram ? s in
336    let old_external_ram ≝ external_ram ? s in
337    let old_program_counter ≝ program_counter ? s in
338    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
339    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
340    let old_p1_latch ≝ p1_latch ? s in
341    let old_p3_latch ≝ p3_latch ? s in
342    let old_clock ≝ clock ? s in
343      mk_PreStatus M old_code_memory
344                r
345                old_high_internal_ram
346                old_external_ram
347                old_program_counter
348                old_special_function_registers_8051
349                old_special_function_registers_8052
350                old_p1_latch
351                old_p3_latch
352                old_clock.
353               
354definition set_high_internal_ram ≝
355  λM: Type[0].
356  λs: PreStatus M.
357  λr: BitVectorTrie Byte 7.
358    let old_code_memory ≝ code_memory ? s in
359    let old_low_internal_ram ≝ low_internal_ram ? s in
360    let old_high_internal_ram ≝ high_internal_ram ? s in
361    let old_external_ram ≝ external_ram ? s in
362    let old_program_counter ≝ program_counter ? s in
363    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
364    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
365    let old_p1_latch ≝ p1_latch ? s in
366    let old_p3_latch ≝ p3_latch ? s in
367    let old_clock ≝ clock ? s in
368      mk_PreStatus M old_code_memory
369                old_low_internal_ram
370                r
371                old_external_ram
372                old_program_counter
373                old_special_function_registers_8051
374                old_special_function_registers_8052
375                old_p1_latch
376                old_p3_latch
377                old_clock.
378               
379definition set_external_ram ≝
380  λM: Type[0].
381  λs: PreStatus M.
382  λr: BitVectorTrie Byte 16.
383    let old_code_memory ≝ code_memory ? s in
384    let old_low_internal_ram ≝ low_internal_ram ? s in
385    let old_high_internal_ram ≝ high_internal_ram ? s in
386    let old_program_counter ≝ program_counter ? s in
387    let old_special_function_registers_8051 ≝ special_function_registers_8051 ? s in
388    let old_special_function_registers_8052 ≝ special_function_registers_8052 ? s in
389    let old_p1_latch ≝ p1_latch ? s in
390    let old_p3_latch ≝ p3_latch ? s in
391    let old_clock ≝ clock ? s in
392      mk_PreStatus M old_code_memory
393                old_low_internal_ram
394                old_high_internal_ram
395                r
396                old_program_counter
397                old_special_function_registers_8051
398                old_special_function_registers_8052
399                old_p1_latch
400                old_p3_latch
401                old_clock.
402               
403definition get_cy_flag ≝
404  λM: Type[0].
405  λs: PreStatus M.
406    let sfr ≝ special_function_registers_8051 ? s in
407    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
408      get_index_v bool 8 psw O ?.
409    normalize
410    @ (le_S_S ? ?)
411    [ @ le_O_n
412    | repeat (@ (le_S_S));
413      //
414    ]
415qed.
416
417definition get_ac_flag ≝
418  λM: Type[0].
419  λs: PreStatus M.
420    let sfr ≝ special_function_registers_8051 ? s in
421    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
422      get_index_v bool 8 psw (S O) ?.
423    normalize
424    repeat (@ (le_S_S ? ?))
425    @ le_O_n
426qed.
427
428definition get_fo_flag ≝
429  λM: Type[0].
430  λs: PreStatus M.
431    let sfr ≝ special_function_registers_8051 ? s in
432    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
433      get_index_v bool 8 psw 2 ?.
434    normalize
435    repeat (@ (le_S_S ? ?))
436    @ le_O_n
437qed.
438
439definition get_rs1_flag ≝
440  λM: Type[0].
441  λs: PreStatus M.
442    let sfr ≝ special_function_registers_8051 ? s in
443    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
444      get_index_v bool 8 psw 3 ?.
445    normalize
446    repeat (@ (le_S_S ? ?))
447    @ le_O_n
448qed.
449
450definition get_rs0_flag ≝
451  λM: Type[0].
452  λs: PreStatus M.
453    let sfr ≝ special_function_registers_8051 ? s in
454    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
455      get_index_v bool 8 psw 4 ?.
456    normalize
457    repeat (@ (le_S_S ? ?))
458    @ le_O_n
459qed.
460
461definition get_ov_flag ≝
462  λM: Type[0].
463  λs: PreStatus M.
464    let sfr ≝ special_function_registers_8051 ? s in
465    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
466      get_index_v bool 8 psw 5 ?.
467    normalize
468    repeat (@ (le_S_S ? ?))
469    @ le_O_n
470qed.
471
472definition get_ud_flag ≝
473  λM: Type[0].
474  λs: PreStatus M.
475    let sfr ≝ special_function_registers_8051 ? s in
476    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
477      get_index_v bool 8 psw 6 ?.
478    normalize
479    repeat (@ (le_S_S ? ?))
480    @ le_O_n
481qed.
482
483definition get_p_flag ≝
484  λM: Type[0].
485  λs: PreStatus M.
486    let sfr ≝ special_function_registers_8051 ? s in
487    let psw ≝ get_index_v Byte 19 sfr (sfr_8051_index SFR_PSW) ? in
488      get_index_v bool 8 psw 7 ?.
489    normalize
490    repeat (@ (le_S_S ? ?))
491    @ le_O_n
492qed.
493
494definition set_flags ≝
495  λM: Type[0].
496  λs: PreStatus M.
497  λcy: Bit.
498  λac: option Bit.
499  λov: Bit.
500    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_PSW) in
501    let old_cy ≝ get_index_v… nu O ? in
502    let old_ac ≝ get_index_v… nu 1 ? in
503    let old_fo ≝ get_index_v… nu 2 ? in
504    let old_rs1 ≝ get_index_v… nu 3 ? in
505    let old_rs0 ≝ get_index_v… nl O ? in
506    let old_ov ≝ get_index_v… nl 1 ? in
507    let old_ud ≝ get_index_v… nl 2 ? in
508    let old_p ≝ get_index_v… nl 3 ? in
509    let new_ac ≝ match ac with [ None ⇒ old_ac | Some j ⇒ j ] in
510    let new_psw ≝ [[ old_cy ; new_ac ; old_fo ; old_rs1 ;
511                     old_rs0 ; old_ov ; old_ud ; old_p ]] in
512      set_8051_sfr ? s SFR_PSW new_psw.
513    [1,2,3,4,5,6,7,8:
514       normalize
515       repeat (@ le_S_S)
516       @ le_O_n
517    ]
518qed.
519
520definition initialise_status ≝
521  λM: Type[0].
522  λcode_mem: M.
523  let status ≝ mk_PreStatus M code_mem                    (* Code mem. *)
524                         (Stub Byte 7)                      (* Low mem.  *)
525                         (Stub Byte 7)                      (* High mem. *)
526                         (Stub Byte 16)                    (* Ext mem.  *)
527                         (zero 16)                         (* PC.       *)
528                         (replicate Byte 19 (zero 8)) (* 8051 SFR. *)
529                         (replicate Byte 5 (zero 8))     (* 8052 SFR. *)
530                         (zero 8)                           (* P1 latch. *)
531                         (zero 8)                           (* P3 latch. *)
532                         O                                      (* Clock.    *)
533  in
534    set_8051_sfr ? status SFR_SP (bitvector_of_nat 8 7).
535 
536axiom not_implemented: False.
537 
538definition get_bit_addressable_sfr ≝
539  λM: Type[0].
540  λs: PreStatus M.
541  λn: nat.
542  λb: BitVector n.
543  λl: bool.
544    let address ≝ nat_of_bitvector … b in
545      if (eqb address 128) then
546        ?
547      else if (eqb address 144) then
548        if l then
549          (p1_latch ? s)
550        else
551          (get_8051_sfr ? s SFR_P1)
552      else if (eqb address 160) then
553        ?
554      else if (eqb address 176) then
555        if l then
556          (p3_latch ? s)
557        else
558          (get_8051_sfr ? s SFR_P3)
559      else if (eqb address 153) then
560        get_8051_sfr ? s SFR_SBUF
561      else if (eqb address 138) then
562        get_8051_sfr ? s SFR_TL0
563      else if (eqb address 139) then
564        get_8051_sfr ? s SFR_TL1
565      else if (eqb address 140) then
566        get_8051_sfr ? s SFR_TH0
567      else if (eqb address 141) then
568        get_8051_sfr ? s SFR_TH1
569      else if (eqb address 200) then
570        get_8052_sfr ? s SFR_T2CON
571      else if (eqb address 202) then
572        get_8052_sfr ? s SFR_RCAP2L
573      else if (eqb address 203) then
574        get_8052_sfr ? s SFR_RCAP2H
575      else if (eqb address 204) then
576        get_8052_sfr ? s SFR_TL2
577      else if (eqb address 205) then
578        get_8052_sfr ? s SFR_TH2
579      else if (eqb address 135) then
580        get_8051_sfr ? s SFR_PCON
581      else if (eqb address 136) then
582        get_8051_sfr ? s SFR_TCON
583      else if (eqb address 137) then
584        get_8051_sfr ? s SFR_TMOD
585      else if (eqb address 152) then
586        get_8051_sfr ? s SFR_SCON
587      else if (eqb address 168) then
588        get_8051_sfr ? s SFR_IE
589      else if (eqb address 184) then
590        get_8051_sfr ? s SFR_IP
591      else if (eqb address 129) then
592        get_8051_sfr ? s SFR_SP
593      else if (eqb address 130) then
594        get_8051_sfr ? s SFR_DPL
595      else if (eqb address 131) then
596        get_8051_sfr ? s SFR_DPH
597      else if (eqb address 208) then
598        get_8051_sfr ? s SFR_PSW
599      else if (eqb address 224) then
600        get_8051_sfr ? s SFR_ACC_A
601      else if (eqb address 240) then
602        get_8051_sfr ? s SFR_ACC_B
603      else
604        ?.
605      cases not_implemented
606qed.
607
608definition set_bit_addressable_sfr ≝
609  λM: Type[0].
610  λs: PreStatus M.
611  λb: Byte.
612  λv: Byte.
613    let address ≝ nat_of_bitvector … b in
614      if (eqb address 128) then
615        ?
616      else if (eqb address 144) then
617        let status_1 ≝ set_8051_sfr ? s SFR_P1 v in
618        let status_2 ≝ set_p1_latch ? s v in
619          status_2
620      else if (eqb address 160) then
621        ?
622      else if (eqb address 176) then
623        let status_1 ≝ set_8051_sfr ? s SFR_P3 v in
624        let status_2 ≝ set_p3_latch ? s v in
625          status_2
626      else if (eqb address 153) then
627        set_8051_sfr ? s SFR_SBUF v
628      else if (eqb address 138) then
629        set_8051_sfr ? s SFR_TL0 v
630      else if (eqb address 139) then
631        set_8051_sfr ? s SFR_TL1 v
632      else if (eqb address 140) then
633        set_8051_sfr ? s SFR_TH0 v
634      else if (eqb address 141) then
635        set_8051_sfr ? s SFR_TH1 v
636      else if (eqb address 200) then
637        set_8052_sfr ? s SFR_T2CON v
638      else if (eqb address 202) then
639        set_8052_sfr ? s SFR_RCAP2L v
640      else if (eqb address 203) then
641        set_8052_sfr ? s SFR_RCAP2H v
642      else if (eqb address 204) then
643        set_8052_sfr ? s SFR_TL2 v
644      else if (eqb address 205) then
645        set_8052_sfr ? s SFR_TH2 v
646      else if (eqb address 135) then
647        set_8051_sfr ? s SFR_PCON v
648      else if (eqb address 136) then
649        set_8051_sfr ? s SFR_TCON v
650      else if (eqb address 137) then
651        set_8051_sfr ? s SFR_TMOD v
652      else if (eqb address 152) then
653        set_8051_sfr ? s SFR_SCON v
654      else if (eqb address 168) then
655        set_8051_sfr ? s SFR_IE v
656      else if (eqb address 184) then
657        set_8051_sfr ? s SFR_IP v
658      else if (eqb address 129) then
659        set_8051_sfr ? s SFR_SP v
660      else if (eqb address 130) then
661        set_8051_sfr ? s SFR_DPL v
662      else if (eqb address 131) then
663        set_8051_sfr ? s SFR_DPH v
664      else if (eqb address 208) then
665        set_8051_sfr ? s SFR_PSW v
666      else if (eqb address 224) then
667        set_8051_sfr ? s SFR_ACC_A v
668      else if (eqb address 240) then
669        set_8051_sfr ? s SFR_ACC_B v
670      else
671        ?.
672      cases not_implemented
673qed.
674
675definition bit_address_of_register ≝
676  λM: Type[0].
677  λs: PreStatus M.
678  λr: BitVector 3.
679    let b ≝ get_index_v … r O ? in
680    let c ≝ get_index_v … r 1 ? in
681    let d ≝ get_index_v … r 2 ? in
682    let 〈 un, ln 〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in
683    let 〈 r1, r0 〉 ≝ 〈 get_index_v … 4 un 2 ?, get_index_v … 4 un 3 ? 〉 in
684    let offset ≝
685      if ¬r1 ∧ ¬r0 then
686        O
687      else if ¬r1 ∧ r0 then
688        8
689      else if r1 ∧ r0 then
690        24
691      else
692        16
693    in
694      bitvector_of_nat 7 (offset + (nat_of_bitvector ? [[ false ; b ; c ; d ]])).
695  [1,2,3,4,5:
696    normalize
697    repeat (@ le_S_S)
698    @ le_O_n;
699  ]
700qed.
701
702definition get_register ≝
703  λM: Type[0].
704  λs: PreStatus M.
705  λr: BitVector 3.
706    let address ≝ bit_address_of_register ? s r in
707      lookup … address (low_internal_ram ? s) (zero 8).
708     
709definition set_register ≝
710  λM: Type[0].
711  λs: PreStatus M.
712  λr: BitVector 3.
713  λv: Byte.
714    let address ≝ bit_address_of_register ? s r in
715    let old_low_internal_ram ≝ low_internal_ram ? s in
716    let new_low_internal_ram ≝ insert … address v old_low_internal_ram in
717      set_low_internal_ram ? s new_low_internal_ram.
718     
719definition read_at_stack_pointer ≝
720  λM: Type[0].
721  λs: PreStatus M.
722    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in
723    let m ≝ get_index_v … nu O ? in
724    let r1 ≝ get_index_v … nu 1 ? in
725    let r2 ≝ get_index_v … nu 2 ? in
726    let r3 ≝ get_index_v … nu 3 ? in
727    let address ≝ [[ r1 ; r2 ; r3 ]] @@ nl in
728    let memory ≝
729      if m then
730        (low_internal_ram ? s)
731      else
732        (high_internal_ram ? s)
733    in
734      lookup … address memory (zero 8).
735  [1,2,3,4:
736     normalize
737     repeat (@ le_S_S)
738     @ le_O_n
739  ]
740qed.
741
742definition write_at_stack_pointer ≝
743  λM: Type[0].
744  λs: PreStatus M.
745  λv: Byte.
746    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in
747    let bit_zero ≝ get_index_v… nu O ? in
748    let bit_1 ≝ get_index_v… nu 1 ? in
749    let bit_2 ≝ get_index_v… nu 2 ? in
750    let bit_3 ≝ get_index_v… nu 3 ? in
751      if bit_zero then
752        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
753                              v (low_internal_ram ? s) in
754          set_low_internal_ram ? s memory
755      else
756        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
757                              v (high_internal_ram ? s) in
758          set_high_internal_ram ? s memory.
759  [1,2,3,4:
760     normalize
761     repeat (@ le_S_S)
762     @ le_O_n
763  ]
764qed.
765
766definition set_arg_16: ∀M: Type[0]. PreStatus M → Word → [[ dptr ]] → PreStatus M ≝
767  λM.
768  λs.
769  λv.
770  λa.
771   match a return λx. bool_to_Prop (is_in ? [[ dptr ]] x) → ? with
772     [ DPTR ⇒ λ_:True.
773       let 〈 bu, bl 〉 ≝ split … 8 8 v in
774       let status ≝ set_8051_sfr ? s SFR_DPH bu in
775       let status ≝ set_8051_sfr ? status SFR_DPL bl in
776         status
777     | _ ⇒ λK.
778       match K in False with
779       [
780       ]
781     ] (subaddressing_modein … a).
782   
783definition get_arg_16: ∀M: Type[0]. PreStatus M → [[ data16 ]] → Word ≝
784  λm, s, a.
785    match a return λx. bool_to_Prop (is_in ? [[ data16 ]] x) → ? with
786      [ DATA16 d ⇒ λ_:True. d
787      | _ ⇒ λK.
788        match K in False with
789        [
790        ]
791      ] (subaddressing_modein … a).
792     
793definition get_arg_8: ∀M: Type[0]. PreStatus M → bool → [[ direct ; indirect ; registr ;
794                                          acc_a ; acc_b ; data ; acc_dptr ;
795                                          acc_pc ; ext_indirect ;
796                                          ext_indirect_dptr ]] → Byte ≝
797  λm, s, l, a.
798    match a return λx. bool_to_Prop (is_in ? [[ direct ; indirect ; registr ;
799                                                acc_a ; acc_b ; data ; acc_dptr ;
800                                                acc_pc ; ext_indirect ;
801                                                ext_indirect_dptr ]] x) → ? with
802      [ ACC_A ⇒ λacc_a: True. get_8051_sfr ? s SFR_ACC_A
803      | ACC_B ⇒ λacc_b: True. get_8051_sfr ? s SFR_ACC_B
804      | DATA d ⇒ λdata: True. d
805      | REGISTER r ⇒ λregister: True. get_register ? s r
806      | EXT_INDIRECT_DPTR ⇒
807        λext_indirect_dptr: True.
808          let address ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in
809            lookup … 16 address (external_ram ? s) (zero 8)
810      | EXT_INDIRECT e ⇒
811        λext_indirect: True.
812          let address ≝ get_register ? s [[ false; false; e ]]  in
813          let padded_address ≝ pad 8 8 address in
814            lookup … 16 padded_address (external_ram ? s) (zero 8)
815      | ACC_DPTR ⇒
816        λacc_dptr: True.
817          let dptr ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in
818          let padded_acc ≝ pad 8 8 (get_8051_sfr ? s SFR_ACC_A) in
819          let 〈 carry, address 〉 ≝ half_add 16 dptr padded_acc in
820            lookup … 16 address (external_ram ? s) (zero 8)
821      | ACC_PC ⇒
822        λacc_pc: True.
823          let padded_acc ≝ pad 8 8 (get_8051_sfr ? s SFR_ACC_A) in
824          let 〈 carry, address 〉 ≝ half_add 16 (program_counter ? s) padded_acc in
825            lookup … 16 address (external_ram ? s) (zero 8)
826      | DIRECT d ⇒
827        λdirect: True.
828          let 〈 nu, nl 〉 ≝ split … 4 4 d in
829          let bit_1 ≝ get_index_v … nu 1 ? in
830            match bit_1 with
831              [ true ⇒
832                  let 〈 bit_one, three_bits 〉 ≝ split ? 1 3 nu in
833                  let address ≝ three_bits @@ nl in
834                    lookup ? 7 address (low_internal_ram ? s) (zero 8)
835              | false ⇒ get_bit_addressable_sfr ? s 8 d l
836              ]
837      | INDIRECT i ⇒
838        λindirect: True.
839          let 〈 nu, nl 〉 ≝ split ? 4 4 (get_register ? s [[ false; false; i]]) in
840          let 〈 bit_one_v, three_bits 〉 ≝ split ? 1 3 nu in
841          let bit_1 ≝ get_index_v … bit_one_v O ? in
842          match bit_1 with
843            [ true ⇒
844                lookup ? 7 (three_bits @@ nl) (low_internal_ram ? s) (zero 8)
845            | false ⇒
846                lookup ? 7 (three_bits @@ nl) (high_internal_ram ? s) (zero 8)
847            ]
848      | _ ⇒ λother.
849        match other in False with [ ]
850      ] (subaddressing_modein … a).
851  [1,2:
852     normalize
853     repeat (@ le_S_S)
854     @ le_O_n
855  ]
856qed.
857
858definition set_arg_8: ∀M: Type[0]. PreStatus M → [[ direct ; indirect ; registr ;
859                                   acc_a ; acc_b ; ext_indirect ;
860                                   ext_indirect_dptr ]] → Byte → PreStatus M ≝
861  λm, s, a, v.
862    match a return λx. bool_to_Prop (is_in ? [[ direct ; indirect ; registr ;
863                                                acc_a ; acc_b ; ext_indirect ;
864                                                ext_indirect_dptr ]] x) → ? with
865      [ DIRECT d ⇒
866        λdirect: True.
867          let 〈 nu, nl 〉 ≝ split … 4 4 d in
868          let bit_1 ≝ get_index_v … nu 1 ? in
869          let 〈 ignore, three_bits 〉 ≝ split ? 1 3 nu in
870            match bit_1 with
871              [ true ⇒ set_bit_addressable_sfr ? s d v
872              | false ⇒
873                let memory ≝ insert ? 7 (three_bits @@ nl) v (low_internal_ram ? s) in
874                  set_low_internal_ram ? s memory
875              ]
876      | INDIRECT i ⇒
877        λindirect: True.
878          let register ≝ get_register ? s [[ false; false; i ]] in
879          let 〈nu, nl〉 ≝ split ? 4 4 register in
880          let bit_1 ≝ get_index_v … nu 1 ? in
881          let 〈ignore, three_bits〉 ≝ split ? 1 3 nu in
882            match bit_1 with
883              [ true ⇒
884                let memory ≝ insert … (three_bits @@ nl) v (low_internal_ram ? s) in
885                  set_low_internal_ram ? s memory
886              | false ⇒
887                let memory ≝ insert … (three_bits @@ nl) v (high_internal_ram ? s) in
888                  set_high_internal_ram ? s memory
889              ]
890      | REGISTER r ⇒ λregister: True. set_register ? s r v
891      | ACC_A ⇒ λacc_a: True. set_8051_sfr ? s SFR_ACC_A v
892      | ACC_B ⇒ λacc_b: True. set_8051_sfr ? s SFR_ACC_B v
893      | EXT_INDIRECT e ⇒
894        λext_indirect: True.
895          let address ≝ get_register ? s [[ false; false; e ]] in
896          let padded_address ≝ pad 8 8 address in
897          let memory ≝ insert ? 16 padded_address v (external_ram ? s) in
898            set_external_ram ? s memory
899      | EXT_INDIRECT_DPTR ⇒
900        λext_indirect_dptr: True.
901          let address ≝ (get_8051_sfr ? s SFR_DPH) @@ (get_8051_sfr ? s SFR_DPL) in
902          let memory ≝ insert ? 16 address v (external_ram ? s) in
903            set_external_ram ? s memory
904      | _ ⇒
905        λother: False.
906          match other in False with [ ]
907      ] (subaddressing_modein … a).
908  [1,2,3,4:
909     normalize
910     repeat (@ le_S_S)
911     @ le_O_n
912  ]
913qed.
914
915theorem modulus_less_than:
916  ∀m,n: nat. (m mod (S n)) < S n.
917  #n #m
918  normalize
919  @ le_S_S
920  lapply (le_n n)
921  generalize in ⊢ (?%? → ?(??%?)?)
922  elim n in ⊢ (∀_:?. ??% → ?(?%??)?)
923  [ normalize
924    #n
925    @ (less_than_or_equal_b_elim n m)
926    normalize
927    [ //
928    | #H #K
929      inversion K
930      [ # H1
931        < H1
932        //
933      | #x #H1 #H2 #H3
934        destruct
935      ]
936    ]
937  | normalize
938    # y # H1 # n # H2
939    @ (less_than_or_equal_b_elim n m)
940    normalize
941    [ //
942    | # K
943      @ H1
944      cut (n ≤ S y → n - S m ≤ y)
945      /2/
946      cases n
947      normalize
948      //
949      # x # K1
950      lapply (le_S_S_to_le … K1)
951      generalize in match m
952      elim x
953      normalize
954      //
955      # w1 # H # m
956      cases m
957      normalize
958      //
959      # q # K2
960      apply H
961      /3/
962    ]
963  ]
964qed.
965
966definition get_arg_1: ∀M: Type[0]. PreStatus M → [[ bit_addr ; n_bit_addr ; carry ]] →
967                       bool → bool ≝
968  λm, s, a, l.
969    match a return λx. bool_to_Prop (is_in ? [[ bit_addr ;
970                                                 n_bit_addr ;
971                                                 carry ]] x) → ? with
972      [ BIT_ADDR b ⇒
973        λbit_addr: True.
974          let 〈 nu, nl 〉 ≝ split … 4 4 b in
975          let bit_1 ≝ get_index_v … nu 1 ? in
976          let 〈 bit_one_v, three_bits 〉 ≝ split ? 1 3 nu in
977            match bit_1 with
978              [ true ⇒
979                let address ≝ nat_of_bitvector … (three_bits @@ nl) in
980                let d ≝ address ÷ 8 in
981                let m ≝ address mod 8 in
982                let trans ≝ bitvector_of_nat 8 ((d * 8) + 128) in
983                let sfr ≝ get_bit_addressable_sfr ? s ? trans l in
984                  get_index_v … sfr m ?
985              | false ⇒
986                let address ≝ nat_of_bitvector … (three_bits @@ nl) in
987                let address' ≝ bitvector_of_nat 7 ((address ÷ 8) + 32) in
988                let t ≝ lookup … 7 address' (low_internal_ram ? s) (zero 8) in
989                  get_index_v … t (modulus address 8) ?
990              ]
991      | N_BIT_ADDR n ⇒
992        λn_bit_addr: True.
993          let 〈 nu, nl 〉 ≝ split … 4 4 n in
994          let bit_1 ≝ get_index_v … nu 1 ? in
995          let 〈 bit_one_v, three_bits 〉 ≝ split ? 1 3 nu in
996            match bit_1 with
997              [ true ⇒
998                let address ≝ nat_of_bitvector … (three_bits @@ nl) in
999                let d ≝ address ÷ 8 in
1000                let m ≝ address mod 8 in
1001                let trans ≝ bitvector_of_nat 8 ((d * 8) + 128) in
1002                let sfr ≝ get_bit_addressable_sfr ? s ? trans l in
1003                  ¬(get_index_v … sfr m ?)
1004              | false ⇒
1005                let address ≝ nat_of_bitvector … (three_bits @@ nl) in
1006                let address' ≝ bitvector_of_nat 7 ((address ÷ 8) + 32) in
1007                let trans ≝ lookup … 7 address' (low_internal_ram ? s) (zero 8) in
1008                  ¬(get_index_v … trans (modulus address 8) ?)
1009              ]
1010      | CARRY ⇒ λcarry: True. get_cy_flag ? s
1011      | _ ⇒ λother.
1012        match other in False with [ ]
1013      ] (subaddressing_modein … a).
1014      [3,6:
1015         normalize
1016         repeat (@ le_S_S)
1017         @ le_O_n
1018      |1,2,4,5:
1019         apply modulus_less_than
1020      ]
1021qed.
1022     
1023definition set_arg_1: ∀M: Type[0]. PreStatus M → [[ bit_addr ; carry ]] →
1024                       Bit → PreStatus M ≝
1025  λm, s, a, v.
1026    match a return λx. bool_to_Prop (is_in ? [[ bit_addr ; carry ]] x) → ? with
1027      [ BIT_ADDR b ⇒ λbit_addr: True.
1028          let 〈 nu, nl 〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in
1029          let bit_1 ≝ get_index_v … nu 1 ? in
1030          let 〈 ignore, three_bits 〉 ≝ split ? 1 3 nu in
1031          match bit_1 with
1032            [ true ⇒
1033                let address ≝ nat_of_bitvector … (three_bits @@ nl) in
1034                let d ≝ address ÷ 8 in
1035                let m ≝ address mod 8 in
1036                let t ≝ bitvector_of_nat 8 ((d * 8) + 128) in
1037                let sfr ≝ get_bit_addressable_sfr ? s ? t true in
1038                let new_sfr ≝ set_index … sfr m v ? in
1039                  set_bit_addressable_sfr ? s new_sfr t
1040            | false ⇒
1041                let address ≝ nat_of_bitvector … (three_bits @@ nl) in
1042                let address' ≝ bitvector_of_nat 7 ((address ÷ 8) + 32) in
1043                let t ≝ lookup … 7 address' (low_internal_ram ? s) (zero 8) in
1044                let n_bit ≝ set_index … t (modulus address 8) v ? in
1045                let memory ≝ insert ? 7 address' n_bit (low_internal_ram ? s) in
1046                  set_low_internal_ram ? s memory
1047            ]
1048      | CARRY ⇒
1049        λcarry: True.
1050          let 〈 nu, nl 〉 ≝ split ? 4 4 (get_8051_sfr ? s SFR_PSW) in
1051          let bit_1 ≝ get_index_v… nu 1 ? in
1052          let bit_2 ≝ get_index_v… nu 2 ? in
1053          let bit_3 ≝ get_index_v… nu 3 ? in
1054          let new_psw ≝ [[ v; bit_1 ; bit_2; bit_3 ]] @@ nl in
1055            set_8051_sfr ? s SFR_PSW new_psw
1056      | _ ⇒
1057        λother: False.
1058          match other in False with
1059            [ ]
1060      ] (subaddressing_modein … a).
1061      [1,2,3,6:
1062         normalize
1063         repeat (@ le_S_S)
1064         @ le_O_n
1065      |4,5:
1066         @ modulus_less_than
1067      ]
1068qed.
1069
1070definition load_code_memory ≝
1071 fold_left_i … (
1072   λi, mem, v.
1073     insert … (bitvector_of_nat … i) v mem) (Stub Byte 16).
1074
1075definition load ≝
1076 λl.
1077 λstatus.
1078   set_code_memory (BitVectorTrie Word 16) ? status (load_code_memory l).
1079
1080definition fetch_pseudo_instruction: list labelled_instruction → Word → (pseudo_instruction × Word) ≝
1081  λcode_mem.
1082  λpc: Word.
1083    let 〈lbl, instr〉 ≝ nth (nat_of_bitvector ? pc) … code_mem ? in
1084    let 〈flags, new_pc〉 ≝ half_add ? pc (bitvector_of_nat ? 1) in
1085      〈instr, new_pc〉.
1086    cases not_implemented.
1087qed.
Note: See TracBrowser for help on using the repository browser.