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/byte8.ma".
29 nrecord word16 : Type ≝
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 ].
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 ].
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 ].
47 ndefinition w16h ≝ λw:word16.match w with [ mk_word16 x _ ⇒ x ].
48 ndefinition w16l ≝ λw:word16.match w with [ mk_word16 _ x ⇒ x ].
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).
56 ndefinition eq_w16 ≝ λw1,w2.(eq_b8 (w16h w1) (w16h w2)) ⊗ (eq_b8 (w16l w1) (w16l w2)).
60 λw1,w2:word16.match lt_b8 (w16h w1) (w16h w2) with
62 | false ⇒ match gt_b8 (w16h w1) (w16h w2) with
64 | false ⇒ lt_b8 (w16l w1) (w16l w2) ]].
67 ndefinition le_w16 ≝ λw1,w2:word16.(eq_w16 w1 w2) ⊕ (lt_w16 w1 w2).
70 ndefinition gt_w16 ≝ λw1,w2:word16.⊖ (le_w16 w1 w2).
73 ndefinition ge_w16 ≝ λw1,w2:word16.⊖ (lt_w16 w1 w2).
77 λw1,w2:word16.mk_word16 (and_b8 (w16h w1) (w16h w2)) (and_b8 (w16l w1) (w16l w2)).
81 λw1,w2:word16.mk_word16 (or_b8 (w16h w1) (w16h w2)) (or_b8 (w16l w1) (w16l w2)).
85 λw1,w2:word16.mk_word16 (xor_b8 (w16h w1) (w16h w2)) (xor_b8 (w16l w1) (w16l w2)).
87 (* operatore rotazione destra con carry *)
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'' ]].
93 (* operatore shift destro *)
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'' ]].
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' ]]].
107 (* operatore rotazione destra n-volte *)
108 nlet rec ror_w16_n (w:word16) (n:nat) on n ≝
111 | S n' ⇒ ror_w16_n (ror_w16 w) n' ].
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'' ]].
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'' ]].
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' ]]].
133 (* operatore rotazione sinistra n-volte *)
134 nlet rec rol_w16_n (w:word16) (n:nat) on n ≝
137 | S n' ⇒ rol_w16_n (rol_w16 w) n' ].
139 (* operatore not/complemento a 1 *)
140 ndefinition not_w16 ≝
141 λw:word16.mk_word16 (not_b8 (w16h w)) (not_b8 (w16l w)).
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' ]].
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〉 ].
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).
161 (* operatore somma con data → data+carry *)
162 ndefinition plus_w16_d_dc ≝
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' ]].
168 (* operatore somma con data → data *)
169 ndefinition plus_w16_d_d ≝
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〉 ].
174 (* operatore somma con data → c *)
175 ndefinition plus_w16_d_c ≝
177 plus_b8_dc_c (w16h w1) (w16h w2) (plus_b8_d_c (w16l w1) (w16l w2)).
179 (* operatore Most Significant Bit *)
180 ndefinition MSB_w16 ≝ λw:word16.eq_ex x8 (and_ex x8 (b8h (w16h w))).
182 (* word → naturali *)
183 ndefinition nat_of_word16 ≝ λw:word16. 256 * (nat_of_byte8 (w16h w)) + (nat_of_byte8 (w16l w)).
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)) ].
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)) ].
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) ].
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)
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 ⇒
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〉〉
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
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' ]].
232 λw:word16.λb:byte8.match eq_b8 b 〈x0,x0〉 with
234 la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato
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 ]].
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).
251 (* iteratore sulle word *)
252 ndefinition forall_word16 ≝
256 P (mk_word16 bh bl ))).