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/exadecim.ma".
29 nrecord byte8 : Type ≝
35 ndefinition byte8_ind : ΠP:byte8 → Prop.(Πe:exadecim.Πe1:exadecim.P (mk_byte8 e e1)) → Πb:byte8.P b ≝
36 λP:byte8 → Prop.λf:Πe:exadecim.Πe1:exadecim.P (mk_byte8 e e1).λb:byte8.
37 match b with [ mk_byte8 (e:exadecim) (e1:exadecim) ⇒ f e e1 ].
39 ndefinition byte8_rec : ΠP:byte8 → Set.(Πe:exadecim.Πe1:exadecim.P (mk_byte8 e e1)) → Πb:byte8.P b ≝
40 λP:byte8 → Set.λf:Πe:exadecim.Πe1:exadecim.P (mk_byte8 e e1).λb:byte8.
41 match b with [ mk_byte8 (e:exadecim) (e1:exadecim) ⇒ f e e1 ].
43 ndefinition byte8_rect : ΠP:byte8 → Type.(Πe:exadecim.Πe1:exadecim.P (mk_byte8 e e1)) → Πb:byte8.P b ≝
44 λP:byte8 → Type.λf:Πe:exadecim.Πe1:exadecim.P (mk_byte8 e e1).λb:byte8.
45 match b with [ mk_byte8 (e:exadecim) (e1:exadecim) ⇒ f e e1 ].
47 ndefinition b8h ≝ λb:byte8.match b with [ mk_byte8 x _ ⇒ x ].
48 ndefinition b8l ≝ λb:byte8.match b with [ mk_byte8 _ x ⇒ x ].
51 notation "〈x,y〉" non associative with precedence 80
52 for @{ 'mk_byte8 $x $y }.
53 interpretation "mk_byte8" 'mk_byte8 x y = (mk_byte8 x y).
56 ndefinition eq_b8 ≝ λb1,b2:byte8.(eq_ex (b8h b1) (b8h b2)) ⊗ (eq_ex (b8l b1) (b8l b2)).
60 λb1,b2:byte8.match lt_ex (b8h b1) (b8h b2) with
62 | false ⇒ match gt_ex (b8h b1) (b8h b2) with
64 | false ⇒ lt_ex (b8l b1) (b8l b2) ]].
67 ndefinition le_b8 ≝ λb1,b2:byte8.(eq_b8 b1 b2) ⊕ (lt_b8 b1 b2).
70 ndefinition gt_b8 ≝ λb1,b2:byte8.⊖ (le_b8 b1 b2).
73 ndefinition ge_b8 ≝ λb1,b2:byte8.⊖ (lt_b8 b1 b2).
77 λb1,b2:byte8.mk_byte8 (and_ex (b8h b1) (b8h b2)) (and_ex (b8l b1) (b8l b2)).
81 λb1,b2:byte8.mk_byte8 (or_ex (b8h b1) (b8h b2)) (or_ex (b8l b1) (b8l b2)).
85 λb1,b2:byte8.mk_byte8 (xor_ex (b8h b1) (b8h b2)) (xor_ex (b8l b1) (b8l b2)).
87 (* operatore rotazione destra con carry *)
89 λb:byte8.λc:bool.match rcr_ex (b8h b) c with
90 [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
91 [ pair bl' c'' ⇒ pair ?? (mk_byte8 bh' bl') c'' ]].
93 (* operatore shift destro *)
95 λb:byte8.match rcr_ex (b8h b) false with
96 [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
97 [ pair bl' c'' ⇒ pair ?? (mk_byte8 bh' bl') c'' ]].
99 (* operatore rotazione destra *)
101 λb:byte8.match rcr_ex (b8h b) false with
102 [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
103 [ pair bl' c'' ⇒ match c'' with
104 [ true ⇒ mk_byte8 (or_ex x8 bh') bl'
105 | false ⇒ mk_byte8 bh' bl' ]]].
107 (* operatore rotazione destra n-volte *)
108 nlet rec ror_b8_n (b:byte8) (n:nat) on n ≝
111 | S n' ⇒ ror_b8_n (ror_b8 b) n' ].
113 (* operatore rotazione sinistra con carry *)
115 λb:byte8.λc:bool.match rcl_ex (b8l b) c with
116 [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
117 [ pair bh' c'' ⇒ pair ?? (mk_byte8 bh' bl') c'' ]].
119 (* operatore shift sinistro *)
121 λb:byte8.match rcl_ex (b8l b) false with
122 [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
123 [ pair bh' c'' ⇒ pair ?? (mk_byte8 bh' bl') c'' ]].
125 (* operatore rotazione sinistra *)
127 λb:byte8.match rcl_ex (b8l b) false with
128 [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
129 [ pair bh' c'' ⇒ match c'' with
130 [ true ⇒ mk_byte8 bh' (or_ex x1 bl')
131 | false ⇒ mk_byte8 bh' bl' ]]].
133 (* operatore rotazione sinistra n-volte *)
134 nlet rec rol_b8_n (b:byte8) (n:nat) on n ≝
137 | S n' ⇒ rol_b8_n (rol_b8 b) n' ].
139 (* operatore not/complemento a 1 *)
141 λb:byte8.mk_byte8 (not_ex (b8h b)) (not_ex (b8l b)).
143 (* operatore somma con data+carry → data+carry *)
144 ndefinition plus_b8_dc_dc ≝
145 λb1,b2:byte8.λc:bool.
146 match plus_ex_dc_dc (b8l b1) (b8l b2) c with
147 [ pair l c ⇒ match plus_ex_dc_dc (b8h b1) (b8h b2) c with
148 [ pair h c' ⇒ pair ?? 〈h,l〉 c' ]].
150 (* operatore somma con data+carry → data *)
151 ndefinition plus_b8_dc_d ≝
152 λb1,b2:byte8.λc:bool.
153 match plus_ex_dc_dc (b8l b1) (b8l b2) c with
154 [ pair l c ⇒ 〈plus_ex_dc_d (b8h b1) (b8h b2) c,l〉 ].
156 (* operatore somma con data+carry → c *)
157 ndefinition plus_b8_dc_c ≝
158 λb1,b2:byte8.λc:bool.
159 plus_ex_dc_c (b8h b1) (b8h b2) (plus_ex_dc_c (b8l b1) (b8l b2) c).
161 (* operatore somma con data → data+carry *)
162 ndefinition plus_b8_d_dc ≝
164 match plus_ex_d_dc (b8l b1) (b8l b2) with
165 [ pair l c ⇒ match plus_ex_dc_dc (b8h b1) (b8h b2) c with
166 [ pair h c' ⇒ pair ?? 〈h,l〉 c' ]].
168 (* operatore somma con data → data *)
169 ndefinition plus_b8_d_d ≝
171 match plus_ex_d_dc (b8l b1) (b8l b2) with
172 [ pair l c ⇒ 〈plus_ex_dc_d (b8h b1) (b8h b2) c,l〉 ].
174 (* operatore somma con data → c *)
175 ndefinition plus_b8_d_c ≝
177 plus_ex_dc_c (b8h b1) (b8h b2) (plus_ex_d_c (b8l b1) (b8l b2)).
179 (* operatore Most Significant Bit *)
180 ndefinition MSB_b8 ≝ λb:byte8.eq_ex x8 (and_ex x8 (b8h b)).
182 (* byte → naturali *)
183 ndefinition nat_of_byte8 ≝ λb:byte8.(16*(nat_of_exadecim (b8h b))) + (nat_of_exadecim (b8l b)).
185 (* operatore predecessore *)
186 ndefinition pred_b8 ≝
187 λb:byte8.match eq_ex (b8l b) x0 with
188 [ true ⇒ mk_byte8 (pred_ex (b8h b)) (pred_ex (b8l b))
189 | false ⇒ mk_byte8 (b8h b) (pred_ex (b8l b)) ].
191 (* operatore successore *)
192 ndefinition succ_b8 ≝
193 λb:byte8.match eq_ex (b8l b) xF with
194 [ true ⇒ mk_byte8 (succ_ex (b8h b)) (succ_ex (b8l b))
195 | false ⇒ mk_byte8 (b8h b) (succ_ex (b8l b)) ].
197 (* operatore neg/complemento a 2 *)
198 ndefinition compl_b8 ≝
199 λb:byte8.match MSB_b8 b with
200 [ true ⇒ succ_b8 (not_b8 b)
201 | false ⇒ not_b8 (pred_b8 b) ].
203 (* operatore moltiplicazione senza segno: e*e=[0x00,0xE1] *)
205 λe1,e2:exadecim.match e1 with
207 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x0〉 | x2 ⇒ 〈x0,x0〉 | x3 ⇒ 〈x0,x0〉
208 | x4 ⇒ 〈x0,x0〉 | x5 ⇒ 〈x0,x0〉 | x6 ⇒ 〈x0,x0〉 | x7 ⇒ 〈x0,x0〉
209 | x8 ⇒ 〈x0,x0〉 | x9 ⇒ 〈x0,x0〉 | xA ⇒ 〈x0,x0〉 | xB ⇒ 〈x0,x0〉
210 | xC ⇒ 〈x0,x0〉 | xD ⇒ 〈x0,x0〉 | xE ⇒ 〈x0,x0〉 | xF ⇒ 〈x0,x0〉 ]
212 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x1〉 | x2 ⇒ 〈x0,x2〉 | x3 ⇒ 〈x0,x3〉
213 | x4 ⇒ 〈x0,x4〉 | x5 ⇒ 〈x0,x5〉 | x6 ⇒ 〈x0,x6〉 | x7 ⇒ 〈x0,x7〉
214 | x8 ⇒ 〈x0,x8〉 | x9 ⇒ 〈x0,x9〉 | xA ⇒ 〈x0,xA〉 | xB ⇒ 〈x0,xB〉
215 | xC ⇒ 〈x0,xC〉 | xD ⇒ 〈x0,xD〉 | xE ⇒ 〈x0,xE〉 | xF ⇒ 〈x0,xF〉 ]
217 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x2〉 | x2 ⇒ 〈x0,x4〉 | x3 ⇒ 〈x0,x6〉
218 | x4 ⇒ 〈x0,x8〉 | x5 ⇒ 〈x0,xA〉 | x6 ⇒ 〈x0,xC〉 | x7 ⇒ 〈x0,xE〉
219 | x8 ⇒ 〈x1,x0〉 | x9 ⇒ 〈x1,x2〉 | xA ⇒ 〈x1,x4〉 | xB ⇒ 〈x1,x6〉
220 | xC ⇒ 〈x1,x8〉 | xD ⇒ 〈x1,xA〉 | xE ⇒ 〈x1,xC〉 | xF ⇒ 〈x1,xE〉 ]
222 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x3〉 | x2 ⇒ 〈x0,x6〉 | x3 ⇒ 〈x0,x9〉
223 | x4 ⇒ 〈x0,xC〉 | x5 ⇒ 〈x0,xF〉 | x6 ⇒ 〈x1,x2〉 | x7 ⇒ 〈x1,x5〉
224 | x8 ⇒ 〈x1,x8〉 | x9 ⇒ 〈x1,xB〉 | xA ⇒ 〈x1,xE〉 | xB ⇒ 〈x2,x1〉
225 | xC ⇒ 〈x2,x4〉 | xD ⇒ 〈x2,x7〉 | xE ⇒ 〈x2,xA〉 | xF ⇒ 〈x2,xD〉 ]
227 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x4〉 | x2 ⇒ 〈x0,x8〉 | x3 ⇒ 〈x0,xC〉
228 | x4 ⇒ 〈x1,x0〉 | x5 ⇒ 〈x1,x4〉 | x6 ⇒ 〈x1,x8〉 | x7 ⇒ 〈x1,xC〉
229 | x8 ⇒ 〈x2,x0〉 | x9 ⇒ 〈x2,x4〉 | xA ⇒ 〈x2,x8〉 | xB ⇒ 〈x2,xC〉
230 | xC ⇒ 〈x3,x0〉 | xD ⇒ 〈x3,x4〉 | xE ⇒ 〈x3,x8〉 | xF ⇒ 〈x3,xC〉 ]
232 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x5〉 | x2 ⇒ 〈x0,xA〉 | x3 ⇒ 〈x0,xF〉
233 | x4 ⇒ 〈x1,x4〉 | x5 ⇒ 〈x1,x9〉 | x6 ⇒ 〈x1,xE〉 | x7 ⇒ 〈x2,x3〉
234 | x8 ⇒ 〈x2,x8〉 | x9 ⇒ 〈x2,xD〉 | xA ⇒ 〈x3,x2〉 | xB ⇒ 〈x3,x7〉
235 | xC ⇒ 〈x3,xC〉 | xD ⇒ 〈x4,x1〉 | xE ⇒ 〈x4,x6〉 | xF ⇒ 〈x4,xB〉 ]
237 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x6〉 | x2 ⇒ 〈x0,xC〉 | x3 ⇒ 〈x1,x2〉
238 | x4 ⇒ 〈x1,x8〉 | x5 ⇒ 〈x1,xE〉 | x6 ⇒ 〈x2,x4〉 | x7 ⇒ 〈x2,xA〉
239 | x8 ⇒ 〈x3,x0〉 | x9 ⇒ 〈x3,x6〉 | xA ⇒ 〈x3,xC〉 | xB ⇒ 〈x4,x2〉
240 | xC ⇒ 〈x4,x8〉 | xD ⇒ 〈x4,xE〉 | xE ⇒ 〈x5,x4〉 | xF ⇒ 〈x5,xA〉 ]
242 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x7〉 | x2 ⇒ 〈x0,xE〉 | x3 ⇒ 〈x1,x5〉
243 | x4 ⇒ 〈x1,xC〉 | x5 ⇒ 〈x2,x3〉 | x6 ⇒ 〈x2,xA〉 | x7 ⇒ 〈x3,x1〉
244 | x8 ⇒ 〈x3,x8〉 | x9 ⇒ 〈x3,xF〉 | xA ⇒ 〈x4,x6〉 | xB ⇒ 〈x4,xD〉
245 | xC ⇒ 〈x5,x4〉 | xD ⇒ 〈x5,xB〉 | xE ⇒ 〈x6,x2〉 | xF ⇒ 〈x6,x9〉 ]
247 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x8〉 | x2 ⇒ 〈x1,x0〉 | x3 ⇒ 〈x1,x8〉
248 | x4 ⇒ 〈x2,x0〉 | x5 ⇒ 〈x2,x8〉 | x6 ⇒ 〈x3,x0〉 | x7 ⇒ 〈x3,x8〉
249 | x8 ⇒ 〈x4,x0〉 | x9 ⇒ 〈x4,x8〉 | xA ⇒ 〈x5,x0〉 | xB ⇒ 〈x5,x8〉
250 | xC ⇒ 〈x6,x0〉 | xD ⇒ 〈x6,x8〉 | xE ⇒ 〈x7,x0〉 | xF ⇒ 〈x7,x8〉 ]
252 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x9〉 | x2 ⇒ 〈x1,x2〉 | x3 ⇒ 〈x1,xB〉
253 | x4 ⇒ 〈x2,x4〉 | x5 ⇒ 〈x2,xD〉 | x6 ⇒ 〈x3,x6〉 | x7 ⇒ 〈x3,xF〉
254 | x8 ⇒ 〈x4,x8〉 | x9 ⇒ 〈x5,x1〉 | xA ⇒ 〈x5,xA〉 | xB ⇒ 〈x6,x3〉
255 | xC ⇒ 〈x6,xC〉 | xD ⇒ 〈x7,x5〉 | xE ⇒ 〈x7,xE〉 | xF ⇒ 〈x8,x7〉 ]
257 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xA〉 | x2 ⇒ 〈x1,x4〉 | x3 ⇒ 〈x1,xE〉
258 | x4 ⇒ 〈x2,x8〉 | x5 ⇒ 〈x3,x2〉 | x6 ⇒ 〈x3,xC〉 | x7 ⇒ 〈x4,x6〉
259 | x8 ⇒ 〈x5,x0〉 | x9 ⇒ 〈x5,xA〉 | xA ⇒ 〈x6,x4〉 | xB ⇒ 〈x6,xE〉
260 | xC ⇒ 〈x7,x8〉 | xD ⇒ 〈x8,x2〉 | xE ⇒ 〈x8,xC〉 | xF ⇒ 〈x9,x6〉 ]
262 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xB〉 | x2 ⇒ 〈x1,x6〉 | x3 ⇒ 〈x2,x1〉
263 | x4 ⇒ 〈x2,xC〉 | x5 ⇒ 〈x3,x7〉 | x6 ⇒ 〈x4,x2〉 | x7 ⇒ 〈x4,xD〉
264 | x8 ⇒ 〈x5,x8〉 | x9 ⇒ 〈x6,x3〉 | xA ⇒ 〈x6,xE〉 | xB ⇒ 〈x7,x9〉
265 | xC ⇒ 〈x8,x4〉 | xD ⇒ 〈x8,xF〉 | xE ⇒ 〈x9,xA〉 | xF ⇒ 〈xA,x5〉 ]
267 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xC〉 | x2 ⇒ 〈x1,x8〉 | x3 ⇒ 〈x2,x4〉
268 | x4 ⇒ 〈x3,x0〉 | x5 ⇒ 〈x3,xC〉 | x6 ⇒ 〈x4,x8〉 | x7 ⇒ 〈x5,x4〉
269 | x8 ⇒ 〈x6,x0〉 | x9 ⇒ 〈x6,xC〉 | xA ⇒ 〈x7,x8〉 | xB ⇒ 〈x8,x4〉
270 | xC ⇒ 〈x9,x0〉 | xD ⇒ 〈x9,xC〉 | xE ⇒ 〈xA,x8〉 | xF ⇒ 〈xB,x4〉 ]
272 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xD〉 | x2 ⇒ 〈x1,xA〉 | x3 ⇒ 〈x2,x7〉
273 | x4 ⇒ 〈x3,x4〉 | x5 ⇒ 〈x4,x1〉 | x6 ⇒ 〈x4,xE〉 | x7 ⇒ 〈x5,xB〉
274 | x8 ⇒ 〈x6,x8〉 | x9 ⇒ 〈x7,x5〉 | xA ⇒ 〈x8,x2〉 | xB ⇒ 〈x8,xF〉
275 | xC ⇒ 〈x9,xC〉 | xD ⇒ 〈xA,x9〉 | xE ⇒ 〈xB,x6〉 | xF ⇒ 〈xC,x3〉 ]
277 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xE〉 | x2 ⇒ 〈x1,xC〉 | x3 ⇒ 〈x2,xA〉
278 | x4 ⇒ 〈x3,x8〉 | x5 ⇒ 〈x4,x6〉 | x6 ⇒ 〈x5,x4〉 | x7 ⇒ 〈x6,x2〉
279 | x8 ⇒ 〈x7,x0〉 | x9 ⇒ 〈x7,xE〉 | xA ⇒ 〈x8,xC〉 | xB ⇒ 〈x9,xA〉
280 | xC ⇒ 〈xA,x8〉 | xD ⇒ 〈xB,x6〉 | xE ⇒ 〈xC,x4〉 | xF ⇒ 〈xD,x2〉 ]
282 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xF〉 | x2 ⇒ 〈x1,xE〉 | x3 ⇒ 〈x2,xD〉
283 | x4 ⇒ 〈x3,xC〉 | x5 ⇒ 〈x4,xB〉 | x6 ⇒ 〈x5,xA〉 | x7 ⇒ 〈x6,x9〉
284 | x8 ⇒ 〈x7,x8〉 | x9 ⇒ 〈x8,x7〉 | xA ⇒ 〈x9,x6〉 | xB ⇒ 〈xA,x5〉
285 | xC ⇒ 〈xB,x4〉 | xD ⇒ 〈xC,x3〉 | xE ⇒ 〈xD,x2〉 | xF ⇒ 〈xE,x1〉 ]
288 (* correzione per somma su BCD *)
289 (* input: halfcarry,carry,X(BCD+BCD) *)
290 (* output: X',carry' *)
293 match lt_b8 X 〈x9,xA〉 with
296 (* X' = [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + [c=1 ? 0x60 : 0x00]
297 [(b16l X):0xA-0xF] X + 0x06 + [c=1 ? 0x60 : 0x00] *)
299 let X' ≝ match (lt_ex (b8l X) xA) ⊗ (⊖h) with
301 | false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
302 let X'' ≝ match c with
303 [ true ⇒ plus_b8_d_d X' 〈x6,x0〉
308 (* X' = [X:0x9A-0xFF]
309 [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + 0x60
310 [(b16l X):0xA-0xF] X + 0x6 + 0x60 *)
312 let X' ≝ match (lt_ex (b8l X) xA) ⊗ (⊖h) with
314 | false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
315 let X'' ≝ plus_b8_d_d X' 〈x6,x0〉 in
319 (* iteratore sui byte *)
320 ndefinition forall_byte8 ≝
322 forall_exadecim (λbh.
323 forall_exadecim (λbl.
324 P (mk_byte8 bh bl))).