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