]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/ng_assembly/freescale/word16.ma
(no commit message)
[helm.git] / helm / software / matita / contribs / ng_assembly / freescale / word16.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 (* ********************************************************************** *)
16 (*                           Progetto FreeScale                           *)
17 (*                                                                        *)
18 (* Sviluppato da:                                                         *)
19 (*   Cosimo Oliboni, oliboni@cs.unibo.it                                  *)
20 (*                                                                        *)
21 (* Questo materiale fa parte della tesi:                                  *)
22 (*   "Formalizzazione Interattiva dei Microcontroller a 8bit FreeScale"   *)
23 (*                                                                        *)
24 (*                    data ultima modifica 15/11/2007                     *)
25 (* ********************************************************************** *)
26
27 include "freescale/byte8.ma".
28
29 (* **** *)
30 (* WORD *)
31 (* **** *)
32
33 nrecord word16 : Type ≝
34  {
35  w16h: byte8;
36  w16l: byte8
37  }.
38
39 ndefinition word16_ind : ΠP:word16 → Prop.(Πb:byte8.Πb1:byte8.P (mk_word16 b b1)) → Πw:word16.P w ≝
40 λP:word16 → Prop.λf:Πb:byte8.Πb1:byte8.P (mk_word16 b b1).λw:word16.
41  match w with [ mk_word16 (b:byte8) (b1:byte8) ⇒ f b b1 ].
42
43 ndefinition word16_rec : ΠP:word16 → Set.(Πb:byte8.Πb1:byte8.P (mk_word16 b b1)) → Πw:word16.P w ≝
44 λP:word16 → Set.λf:Πb:byte8.Πb1:byte8.P (mk_word16 b b1).λw:word16.
45  match w with [ mk_word16 (b:byte8) (b1:byte8) ⇒ f b b1 ].
46
47 ndefinition word16_rect : ΠP:word16 → Type.(Πb:byte8.Πb1:byte8.P (mk_word16 b b1)) → Πw:word16.P w ≝
48 λP:word16 → Type.λf:Πb:byte8.Πb1:byte8.P (mk_word16 b b1).λw:word16.
49  match w with [ mk_word16 (b:byte8) (b1:byte8) ⇒ f b b1 ].
50
51 ndefinition w16h ≝ λw:word16.match w with [ mk_word16 x _ ⇒ x ].
52 ndefinition w16l ≝ λw:word16.match w with [ mk_word16 _ x ⇒ x ].
53
54 (* \langle \rangle *)
55 notation "〈x:y〉" non associative with precedence 80
56  for @{ 'mk_word16 $x $y }.
57 interpretation "mk_word16" 'mk_word16 x y = (mk_word16 x y).
58
59 (* operatore = *)
60 ndefinition eq_w16 ≝ λw1,w2.(eq_b8 (w16h w1) (w16h w2)) ⊗ (eq_b8 (w16l w1) (w16l w2)).
61
62 (* operatore < *)
63 ndefinition lt_w16 ≝
64 λw1,w2:word16.match lt_b8 (w16h w1) (w16h w2) with
65  [ true ⇒ true
66  | false ⇒ match gt_b8 (w16h w1) (w16h w2) with
67   [ true ⇒ false
68   | false ⇒ lt_b8 (w16l w1) (w16l w2) ]].
69
70 (* operatore ≤ *)
71 ndefinition le_w16 ≝ λw1,w2:word16.(eq_w16 w1 w2) ⊕ (lt_w16 w1 w2).
72
73 (* operatore > *)
74 ndefinition gt_w16 ≝ λw1,w2:word16.⊖ (le_w16 w1 w2).
75
76 (* operatore ≥ *)
77 ndefinition ge_w16 ≝ λw1,w2:word16.⊖ (lt_w16 w1 w2).
78
79 (* operatore and *)
80 ndefinition and_w16 ≝
81 λw1,w2:word16.mk_word16 (and_b8 (w16h w1) (w16h w2)) (and_b8 (w16l w1) (w16l w2)).
82
83 (* operatore or *)
84 ndefinition or_w16 ≝
85 λw1,w2:word16.mk_word16 (or_b8 (w16h w1) (w16h w2)) (or_b8 (w16l w1) (w16l w2)).
86
87 (* operatore xor *)
88 ndefinition xor_w16 ≝
89 λw1,w2:word16.mk_word16 (xor_b8 (w16h w1) (w16h w2)) (xor_b8 (w16l w1) (w16l w2)).
90
91 (* operatore rotazione destra con carry *)
92 ndefinition rcr_w16 ≝
93 λw:word16.λc:bool.match rcr_b8 (w16h w) c with
94  [ pair wh' c' ⇒ match rcr_b8 (w16l w) c' with
95   [ pair wl' c'' ⇒ pair ?? (mk_word16 wh' wl') c'' ]]. 
96
97 (* operatore shift destro *)
98 ndefinition shr_w16 ≝
99 λw:word16.match rcr_b8 (w16h w) false with
100  [ pair wh' c' ⇒ match rcr_b8 (w16l w) c' with
101   [ pair wl' c'' ⇒ pair ?? (mk_word16 wh' wl') c'' ]].
102
103 (* operatore rotazione destra *)
104 ndefinition ror_w16 ≝
105 λw:word16.match rcr_b8 (w16h w) false with
106  [ pair wh' c' ⇒ match rcr_b8 (w16l w) c' with
107   [ pair wl' c'' ⇒ match c'' with
108    [ true ⇒ mk_word16 (or_b8 (mk_byte8 x8 x0) wh') wl'
109    | false ⇒ mk_word16 wh' wl' ]]].
110
111 (* operatore rotazione destra n-volte *)
112 nlet rec ror_w16_n (w:word16) (n:nat) on n ≝
113  match n with
114   [ O ⇒ w
115   | S n' ⇒ ror_w16_n (ror_w16 w) n' ].
116
117 (* operatore rotazione sinistra con carry *)
118 ndefinition rcl_w16 ≝
119 λw:word16.λc:bool.match rcl_b8 (w16l w) c with
120  [ pair wl' c' ⇒ match rcl_b8 (w16h w) c' with
121   [ pair wh' c'' ⇒ pair ?? (mk_word16 wh' wl') c'' ]]. 
122
123 (* operatore shift sinistro *)
124 ndefinition shl_w16 ≝
125 λw:word16.match rcl_b8 (w16l w) false with
126  [ pair wl' c' ⇒ match rcl_b8 (w16h w) c' with
127   [ pair wh' c'' ⇒ pair ?? (mk_word16 wh' wl') c'' ]].
128
129 (* operatore rotazione sinistra *)
130 ndefinition rol_w16 ≝
131 λw:word16.match rcl_b8 (w16l w) false with
132  [ pair wl' c' ⇒ match rcl_b8 (w16h w) c' with
133   [ pair wh' c'' ⇒ match c'' with
134    [ true ⇒ mk_word16 wh' (or_b8 (mk_byte8 x0 x1) wl')
135    | false ⇒ mk_word16 wh' wl' ]]].
136
137 (* operatore rotazione sinistra n-volte *)
138 nlet rec rol_w16_n (w:word16) (n:nat) on n ≝
139  match n with
140   [ O ⇒ w
141   | S n' ⇒ rol_w16_n (rol_w16 w) n' ].
142
143 (* operatore not/complemento a 1 *)
144 ndefinition not_w16 ≝
145 λw:word16.mk_word16 (not_b8 (w16h w)) (not_b8 (w16l w)).
146
147 (* operatore somma con data+carry → data+carry *)
148 ndefinition plus_w16_dc_dc ≝
149 λw1,w2:word16.λc:bool.
150  match plus_b8_dc_dc (w16l w1) (w16l w2) c with
151   [ pair l c ⇒ match plus_b8_dc_dc (w16h w1) (w16h w2) c with
152    [ pair h c' ⇒ pair ?? 〈h:l〉 c' ]].
153
154 (* operatore somma con data+carry → data *)
155 ndefinition plus_w16_dc_d ≝
156 λw1,w2:word16.λc:bool.
157  match plus_b8_dc_dc (w16l w1) (w16l w2) c with
158   [ pair l c ⇒ 〈plus_b8_dc_d (w16h w1) (w16h w2) c:l〉 ].
159
160 (* operatore somma con data+carry → c *)
161 ndefinition plus_w16_dc_c ≝
162 λw1,w2:word16.λc:bool.
163  plus_b8_dc_c (w16h w1) (w16h w2) (plus_b8_dc_c (w16l w1) (w16l w2) c).
164
165 (* operatore somma con data → data+carry *)
166 ndefinition plus_w16_d_dc ≝
167 λw1,w2:word16.
168  match plus_b8_d_dc (w16l w1) (w16l w2) with
169   [ pair l c ⇒ match plus_b8_dc_dc (w16h w1) (w16h w2) c with
170    [ pair h c' ⇒ pair ?? 〈h:l〉 c' ]].
171
172 (* operatore somma con data → data *)
173 ndefinition plus_w16_d_d ≝
174 λw1,w2:word16.
175  match plus_b8_d_dc (w16l w1) (w16l w2) with
176   [ pair l c ⇒ 〈plus_b8_dc_d (w16h w1) (w16h w2) c:l〉 ].
177
178 (* operatore somma con data → c *)
179 ndefinition plus_w16_d_c ≝
180 λw1,w2:word16.
181  plus_b8_dc_c (w16h w1) (w16h w2) (plus_b8_d_c (w16l w1) (w16l w2)).
182
183 (* operatore Most Significant Bit *)
184 ndefinition MSB_w16 ≝ λw:word16.eq_ex x8 (and_ex x8 (b8h (w16h w))).
185
186 (* word → naturali *)
187 ndefinition nat_of_word16 ≝ λw:word16. 256 * (nat_of_byte8 (w16h w)) + (nat_of_byte8 (w16l w)).
188
189 (* operatore predecessore *)
190 ndefinition pred_w16 ≝
191 λw:word16.match eq_b8 (w16l w) (mk_byte8 x0 x0) with
192  [ true ⇒ mk_word16 (pred_b8 (w16h w)) (pred_b8 (w16l w))
193  | false ⇒ mk_word16 (w16h w) (pred_b8 (w16l w)) ].
194
195 (* operatore successore *)
196 ndefinition succ_w16 ≝
197 λw:word16.match eq_b8 (w16l w) (mk_byte8 xF xF) with
198  [ true ⇒ mk_word16 (succ_b8 (w16h w)) (succ_b8 (w16l w))
199  | false ⇒ mk_word16 (w16h w) (succ_b8 (w16l w)) ].
200
201 (* operatore neg/complemento a 2 *)
202 ndefinition compl_w16 ≝
203 λw:word16.match MSB_w16 w with
204  [ true ⇒ succ_w16 (not_w16 w)
205  | false ⇒ not_w16 (pred_w16 w) ].
206
207 (* 
208    operatore moltiplicazione senza segno: b*b=[0x0000,0xFE01]
209    ... in pratica (〈a,b〉*〈c,d〉) = (a*c)<<8+(a*d)<<4+(b*c)<<4+(b*d)
210 *)
211 ndefinition mul_b8 ≝
212 λb1,b2:byte8.match b1 with
213 [ mk_byte8 b1h b1l ⇒ match b2 with
214 [ mk_byte8 b2h b2l ⇒ match mul_ex b1l b2l with
215 [ mk_byte8 t1_h t1_l ⇒ match mul_ex b1h b2l with
216 [ mk_byte8 t2_h t2_l ⇒ match mul_ex b2h b1l with
217 [ mk_byte8 t3_h t3_l ⇒ match mul_ex b1h b2h with
218 [ mk_byte8 t4_h t4_l ⇒
219  plus_w16_d_d
220   (plus_w16_d_d
221    (plus_w16_d_d 〈〈x0,t3_h〉:〈t3_l,x0〉〉 〈〈x0,t2_h〉:〈t2_l,x0〉〉) 〈〈t4_h,t4_l〉:〈x0,x0〉〉)〈〈x0,x0〉:〈t1_h,t1_l〉〉
222 ]]]]]].
223
224 (* divisione senza segno (secondo la logica delle ALU): (quoziente resto) overflow *)
225 nlet rec div_b8_aux (divd:word16) (divs:word16) (molt:byte8) (q:byte8) (c:nat) on c ≝
226  let w' ≝ plus_w16_d_d divd (compl_w16 divs) in
227   match c with
228   [ O ⇒ match le_w16 divs divd with
229    [ true ⇒ triple ??? (or_b8 molt q) (w16l w') (⊖ (eq_b8 (w16h w') 〈x0,x0〉))
230    | false ⇒ triple ??? q (w16l divd) (⊖ (eq_b8 (w16h divd) 〈x0,x0〉)) ]
231   | S c' ⇒ match le_w16 divs divd with
232    [ true ⇒ div_b8_aux w' (ror_w16 divs) (ror_b8 molt) (or_b8 molt q) c'
233    | false ⇒ div_b8_aux divd (ror_w16 divs) (ror_b8 molt) q c' ]].
234
235 ndefinition div_b8 ≝
236 λw:word16.λb:byte8.match eq_b8 b 〈x0,x0〉 with
237 (* 
238    la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato
239 *)
240  [ true ⇒ triple ??? 〈xF,xF〉 (w16l w) true
241  | false ⇒ match eq_w16 w 〈〈x0,x0〉:〈x0,x0〉〉 with
242 (* 0 diviso qualsiasi cosa diverso da 0 da' q=0 r=0 o=false *)
243   [ true ⇒ triple ??? 〈x0,x0〉 〈x0,x0〉 false
244 (* 1) e' una divisione sensata che produrra' overflow/risultato *)
245 (* 2) parametri: dividendo, divisore, moltiplicatore, quoziente, contatore *)
246 (* 3) ad ogni ciclo il divisore e il moltiplicatore vengono scalati di 1 a dx *)
247 (* 4) il moltiplicatore e' la quantita' aggiunta al quoziente se il divisore *)
248 (*    puo' essere sottratto al dividendo *) 
249   | false ⇒ div_b8_aux w (rol_w16_n 〈〈x0,x0〉:b〉 7) 〈x8,x0〉 〈x0,x0〉 7 ]].
250
251 (* operatore x in [inf,sup] *)
252 ndefinition in_range ≝
253 λx,inf,sup:word16.(le_w16 inf sup) ⊗ (ge_w16 x inf) ⊗ (le_w16 x sup).
254
255 (* iteratore sulle word *)
256 ndefinition forall_word16 ≝
257  λP.
258   forall_byte8 (λbh.
259   forall_byte8 (λbl.
260    P (mk_word16 bh bl ))).