1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 (* ********************************************************************** *)
16 (* Progetto FreeScale *)
18 (* Sviluppato da: Cosimo Oliboni, oliboni@cs.unibo.it *)
19 (* Cosimo Oliboni, oliboni@cs.unibo.it *)
21 (* ********************************************************************** *)
23 include "freescale/word16.ma".
29 nrecord word32 : Type ≝
35 (*ndefinition word32_ind : ΠP:word32 → Prop.(Πw:word16.Πw1:word16.P (mk_word32 w w1)) → Πdw:word32.P dw ≝
36 λP:word32 → Prop.λf:Πw:word16.Πw1:word16.P (mk_word32 w w1).λdw:word32.
37 match dw with [ mk_word32 (w:word16) (w1:word16) ⇒ f w w1 ].
39 ndefinition word32_rec : ΠP:word32 → Set.(Πw:word16.Πw1:word16.P (mk_word32 w w1)) → Πdw:word32.P dw ≝
40 λP:word32 → Set.λf:Πw:word16.Πw1:word16.P (mk_word32 w w1).λdw:word32.
41 match dw with [ mk_word32 (w:word16) (w1:word16) ⇒ f w w1 ].
43 ndefinition word32_rect : ΠP:word32 → Type.(Πw:word16.Πw1:word16.P (mk_word32 w w1)) → Πdw:word32.P dw ≝
44 λP:word32 → Type.λf:Πw:word16.Πw1:word16.P (mk_word32 w w1).λdw:word32.
45 match dw with [ mk_word32 (w:word16) (w1:word16) ⇒ f w w1 ].
47 ndefinition w32h ≝ λdw:word32.match dw with [ mk_word32 x _ ⇒ x ].
48 ndefinition w32l ≝ λdw:word32.match dw with [ mk_word32 _ x ⇒ x ].*)
51 notation "〈x.y〉" non associative with precedence 80
52 for @{ 'mk_word32 $x $y }.
53 interpretation "mk_word32" 'mk_word32 x y = (mk_word32 x y).
56 ndefinition eq_w32 ≝ λdw1,dw2.(eq_w16 (w32h dw1) (w32h dw2)) ⊗ (eq_w16 (w32l dw1) (w32l dw2)).
60 λdw1,dw2:word32.match lt_w16 (w32h dw1) (w32h dw2) with
62 | false ⇒ match gt_w16 (w32h dw1) (w32h dw2) with
64 | false ⇒ lt_w16 (w32l dw1) (w32l dw2) ]].
67 ndefinition le_w32 ≝ λdw1,dw2:word32.(eq_w32 dw1 dw2) ⊕ (lt_w32 dw1 dw2).
70 ndefinition gt_w32 ≝ λdw1,dw2:word32.⊖ (le_w32 dw1 dw2).
73 ndefinition ge_w32 ≝ λdw1,dw2:word32.⊖ (lt_w32 dw1 dw2).
77 λdw1,dw2:word32.mk_word32 (and_w16 (w32h dw1) (w32h dw2)) (and_w16 (w32l dw1) (w32l dw2)).
81 λdw1,dw2:word32.mk_word32 (or_w16 (w32h dw1) (w32h dw2)) (or_w16 (w32l dw1) (w32l dw2)).
85 λdw1,dw2:word32.mk_word32 (xor_w16 (w32h dw1) (w32h dw2)) (xor_w16 (w32l dw1) (w32l dw2)).
87 (* operatore rotazione destra con carry *)
89 λdw:word32.λc:bool.match rcr_w16 (w32h dw) c with
90 [ pair wh' c' ⇒ match rcr_w16 (w32l dw) c' with
91 [ pair wl' c'' ⇒ pair ?? (mk_word32 wh' wl') c'' ]].
93 (* operatore shift destro *)
95 λdw:word32.match rcr_w16 (w32h dw) false with
96 [ pair wh' c' ⇒ match rcr_w16 (w32l dw) c' with
97 [ pair wl' c'' ⇒ pair ?? (mk_word32 wh' wl') c'' ]].
99 (* operatore rotazione destra *)
100 ndefinition ror_w32 ≝
101 λdw:word32.match rcr_w16 (w32h dw) false with
102 [ pair wh' c' ⇒ match rcr_w16 (w32l dw) c' with
103 [ pair wl' c'' ⇒ match c'' with
104 [ true ⇒ mk_word32 (or_w16 (mk_word16 (mk_byte8 x8 x0) (mk_byte8 x0 x0)) wh') wl'
105 | false ⇒ mk_word32 wh' wl' ]]].
107 (* operatore rotazione destra n-volte *)
108 nlet rec ror_w32_n (dw:word32) (n:nat) on n ≝
111 | S n' ⇒ ror_w32_n (ror_w32 dw) n' ].
113 (* operatore rotazione sinistra con carry *)
114 ndefinition rcl_w32 ≝
115 λdw:word32.λc:bool.match rcl_w16 (w32l dw) c with
116 [ pair wl' c' ⇒ match rcl_w16 (w32h dw) c' with
117 [ pair wh' c'' ⇒ pair ?? (mk_word32 wh' wl') c'' ]].
119 (* operatore shift sinistro *)
120 ndefinition shl_w32 ≝
121 λdw:word32.match rcl_w16 (w32l dw) false with
122 [ pair wl' c' ⇒ match rcl_w16 (w32h dw) c' with
123 [ pair wh' c'' ⇒ pair ?? (mk_word32 wh' wl') c'' ]].
125 (* operatore rotazione sinistra *)
126 ndefinition rol_w32 ≝
127 λdw:word32.match rcl_w16 (w32l dw) false with
128 [ pair wl' c' ⇒ match rcl_w16 (w32h dw) c' with
129 [ pair wh' c'' ⇒ match c'' with
130 [ true ⇒ mk_word32 wh' (or_w16 (mk_word16 (mk_byte8 x0 x0) (mk_byte8 x0 x1)) wl')
131 | false ⇒ mk_word32 wh' wl' ]]].
133 (* operatore rotazione sinistra n-volte *)
134 nlet rec rol_w32_n (dw:word32) (n:nat) on n ≝
137 | S n' ⇒ rol_w32_n (rol_w32 dw) n' ].
139 (* operatore not/complemento a 1 *)
140 ndefinition not_w32 ≝
141 λdw:word32.mk_word32 (not_w16 (w32h dw)) (not_w16 (w32l dw)).
143 (* operatore somma con data+carry → data+carry *)
144 ndefinition plus_w32_dc_dc ≝
145 λdw1,dw2:word32.λc:bool.
146 match plus_w16_dc_dc (w32l dw1) (w32l dw2) c with
147 [ pair l c ⇒ match plus_w16_dc_dc (w32h dw1) (w32h dw2) c with
148 [ pair h c' ⇒ pair ?? 〈h.l〉 c' ]].
150 (* operatore somma con data+carry → data *)
151 ndefinition plus_w32_dc_d ≝
152 λdw1,dw2:word32.λc:bool.
153 match plus_w16_dc_dc (w32l dw1) (w32l dw2) c with
154 [ pair l c ⇒ 〈plus_w16_dc_d (w32h dw1) (w32h dw2) c.l〉 ].
156 (* operatore somma con data+carry → c *)
157 ndefinition plus_w32_dc_c ≝
158 λdw1,dw2:word32.λc:bool.
159 plus_w16_dc_c (w32h dw1) (w32h dw2) (plus_w16_dc_c (w32l dw1) (w32l dw2) c).
161 (* operatore somma con data → data+carry *)
162 ndefinition plus_w32_d_dc ≝
164 match plus_w16_d_dc (w32l dw1) (w32l dw2) with
165 [ pair l c ⇒ match plus_w16_dc_dc (w32h dw1) (w32h dw2) c with
166 [ pair h c' ⇒ pair ?? 〈h.l〉 c' ]].
168 (* operatore somma con data → data *)
169 ndefinition plus_w32_d_d ≝
171 match plus_w16_d_dc (w32l dw1) (w32l dw2) with
172 [ pair l c ⇒ 〈plus_w16_dc_d (w32h dw1) (w32h dw2) c.l〉 ].
174 (* operatore somma con data → c *)
175 ndefinition plus_w32_d_c ≝
177 plus_w16_dc_c (w32h dw1) (w32h dw2) (plus_w16_d_c (w32l dw1) (w32l dw2)).
179 (* operatore Most Significant Bit *)
180 ndefinition MSB_w32 ≝ λdw:word32.eq_ex x8 (and_ex x8 (b8h (w16h (w32h dw)))).
182 (* word → naturali *)
183 ndefinition nat_of_word32 ≝ λdw:word32. (256 * 256 * (nat_of_word16 (w32h dw))) + (nat_of_word16 (w32l dw)).
185 (* operatore predecessore *)
186 ndefinition pred_w32 ≝
187 λdw:word32.match eq_w16 (w32l dw) (mk_word16 (mk_byte8 x0 x0) (mk_byte8 x0 x0)) with
188 [ true ⇒ mk_word32 (pred_w16 (w32h dw)) (pred_w16 (w32l dw))
189 | false ⇒ mk_word32 (w32h dw) (pred_w16 (w32l dw)) ].
191 (* operatore successore *)
192 ndefinition succ_w32 ≝
193 λdw:word32.match eq_w16 (w32l dw) (mk_word16 (mk_byte8 xF xF) (mk_byte8 xF xF)) with
194 [ true ⇒ mk_word32 (succ_w16 (w32h dw)) (succ_w16 (w32l dw))
195 | false ⇒ mk_word32 (w32h dw) (succ_w16 (w32l dw)) ].
197 (* operatore neg/complemento a 2 *)
198 ndefinition compl_w32 ≝
199 λdw:word32.match MSB_w32 dw with
200 [ true ⇒ succ_w32 (not_w32 dw)
201 | false ⇒ not_w32 (pred_w32 dw) ].
204 operatore moltiplicazione senza segno: b*b=[0x00000000,0xFFFE0001]
205 ... in pratica (〈a:b〉*〈c:d〉) = (a*c)<<16+(a*d)<<8+(b*c)<<8+(b*d)
207 ndefinition mul_w16 ≝
208 λw1,w2:word16.match w1 with
209 [ mk_word16 b1h b1l ⇒ match w2 with
210 [ mk_word16 b2h b2l ⇒ match mul_b8 b1l b2l with
211 [ mk_word16 t1_h t1_l ⇒ match mul_b8 b1h b2l with
212 [ mk_word16 t2_h t2_l ⇒ match mul_b8 b2h b1l with
213 [ mk_word16 t3_h t3_l ⇒ match mul_b8 b1h b2h with
214 [ mk_word16 t4_h t4_l ⇒
217 (plus_w32_d_d 〈〈〈x0,x0〉:t3_h〉.〈t3_l:〈x0,x0〉〉〉 〈〈〈x0,x0〉:t2_h〉.〈t2_l:〈x0,x0〉〉〉) 〈〈t4_h:t4_l〉.〈〈x0,x0〉:〈x0,x0〉〉〉)〈〈〈x0,x0〉:〈x0,x0〉〉.〈t1_h:t1_l〉〉
220 (* divisione senza segno (secondo la logica delle ALU): (quoziente resto) overflow *)
221 nlet rec div_w16_aux (divd:word32) (divs:word32) (molt:word16) (q:word16) (c:nat) on c ≝
222 let w' ≝ plus_w32_d_d divd (compl_w32 divs) in
224 [ O ⇒ match le_w32 divs divd with
225 [ true ⇒ triple ??? (or_w16 molt q) (w32l w') (⊖ (eq_w16 (w32h w') 〈〈x0,x0〉:〈x0,x0〉〉))
226 | false ⇒ triple ??? q (w32l divd) (⊖ (eq_w16 (w32h divd) 〈〈x0,x0〉:〈x0,x0〉〉)) ]
227 | S c' ⇒ match le_w32 divs divd with
228 [ true ⇒ div_w16_aux w' (ror_w32 divs) (ror_w16 molt) (or_w16 molt q) c'
229 | false ⇒ div_w16_aux divd (ror_w32 divs) (ror_w16 molt) q c' ]].
231 ndefinition div_w16 ≝
232 λw:word32.λb:word16.match eq_w16 b 〈〈x0,x0〉:〈x0,x0〉〉 with
234 la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato
236 [ true ⇒ triple ??? 〈〈xF,xF〉:〈xF,xF〉〉 (w32l w) true
237 | false ⇒ match eq_w32 w 〈〈〈x0,x0〉:〈x0,x0〉〉.〈〈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〉〉 〈〈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_w16_aux w (rol_w32_n 〈〈〈x0,x0〉:〈x0,x0〉〉.b〉 15) 〈〈x8,x0〉:〈x0,x0〉〉 〈〈x0,x0〉:〈x0,x0〉〉 15 ]].
247 (* operatore x in [inf,sup] *)
248 ndefinition in_range ≝
249 λx,inf,sup:word32.(le_w32 inf sup) ⊗ (ge_w32 x inf) ⊗ (le_w32 x sup).
251 (* iteratore sulle word *)
252 ndefinition forall_word32 ≝
256 P (mk_word32 bh bl ))).