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 ≝
36 notation "〈x,y〉" non associative with precedence 80
37 for @{ 'mk_byte8 $x $y }.
38 interpretation "mk_byte8" 'mk_byte8 x y = (mk_byte8 x y).
41 ndefinition eq_b8 ≝ λb1,b2:byte8.(eq_ex (b8h b1) (b8h b2)) ⊗ (eq_ex (b8l b1) (b8l b2)).
45 λb1,b2:byte8.match lt_ex (b8h b1) (b8h b2) with
47 | false ⇒ match gt_ex (b8h b1) (b8h b2) with
49 | false ⇒ lt_ex (b8l b1) (b8l b2) ]].
52 ndefinition le_b8 ≝ λb1,b2:byte8.(eq_b8 b1 b2) ⊕ (lt_b8 b1 b2).
55 ndefinition gt_b8 ≝ λb1,b2:byte8.⊖ (le_b8 b1 b2).
58 ndefinition ge_b8 ≝ λb1,b2:byte8.⊖ (lt_b8 b1 b2).
62 λb1,b2:byte8.mk_byte8 (and_ex (b8h b1) (b8h b2)) (and_ex (b8l b1) (b8l b2)).
66 λb1,b2:byte8.mk_byte8 (or_ex (b8h b1) (b8h b2)) (or_ex (b8l b1) (b8l b2)).
70 λb1,b2:byte8.mk_byte8 (xor_ex (b8h b1) (b8h b2)) (xor_ex (b8l b1) (b8l b2)).
72 (* operatore rotazione destra con carry *)
74 λb:byte8.λc:bool.match rcr_ex (b8h b) c with
75 [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
76 [ pair bl' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
78 (* operatore shift destro *)
80 λb:byte8.match rcr_ex (b8h b) false with
81 [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
82 [ pair bl' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
84 (* operatore rotazione destra *)
86 λb:byte8.match rcr_ex (b8h b) false with
87 [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
88 [ pair bl' c'' ⇒ match c'' with
89 [ true ⇒ mk_byte8 (or_ex x8 bh') bl'
90 | false ⇒ mk_byte8 bh' bl' ]]].
92 (* operatore rotazione destra n-volte *)
93 nlet rec ror_b8_n (b:byte8) (n:nat) on n ≝
96 | S n' ⇒ ror_b8_n (ror_b8 b) n' ].
98 (* operatore rotazione sinistra con carry *)
100 λb:byte8.λc:bool.match rcl_ex (b8l b) c with
101 [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
102 [ pair bh' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
104 (* operatore shift sinistro *)
106 λb:byte8.match rcl_ex (b8l b) false with
107 [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
108 [ pair bh' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
110 (* operatore rotazione sinistra *)
112 λb:byte8.match rcl_ex (b8l b) false with
113 [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
114 [ pair bh' c'' ⇒ match c'' with
115 [ true ⇒ mk_byte8 bh' (or_ex x1 bl')
116 | false ⇒ mk_byte8 bh' bl' ]]].
118 (* operatore rotazione sinistra n-volte *)
119 nlet rec rol_b8_n (b:byte8) (n:nat) on n ≝
122 | S n' ⇒ rol_b8_n (rol_b8 b) n' ].
124 (* operatore not/complemento a 1 *)
126 λb:byte8.mk_byte8 (not_ex (b8h b)) (not_ex (b8l b)).
128 (* operatore somma con data+carry → data+carry *)
129 ndefinition plus_b8_dc_dc ≝
130 λb1,b2:byte8.λc:bool.
131 match plus_ex_dc_dc (b8l b1) (b8l b2) c with
132 [ pair l c ⇒ match plus_ex_dc_dc (b8h b1) (b8h b2) c with
133 [ pair h c' ⇒ pair … 〈h,l〉 c' ]].
135 (* operatore somma con data+carry → data *)
136 ndefinition plus_b8_dc_d ≝
137 λb1,b2:byte8.λc:bool.
138 match plus_ex_dc_dc (b8l b1) (b8l b2) c with
139 [ pair l c ⇒ 〈plus_ex_dc_d (b8h b1) (b8h b2) c,l〉 ].
141 (* operatore somma con data+carry → c *)
142 ndefinition plus_b8_dc_c ≝
143 λb1,b2:byte8.λc:bool.
144 plus_ex_dc_c (b8h b1) (b8h b2) (plus_ex_dc_c (b8l b1) (b8l b2) c).
146 (* operatore somma con data → data+carry *)
147 ndefinition plus_b8_d_dc ≝
149 match plus_ex_d_dc (b8l b1) (b8l b2) with
150 [ pair l c ⇒ match plus_ex_dc_dc (b8h b1) (b8h b2) c with
151 [ pair h c' ⇒ pair … 〈h,l〉 c' ]].
153 (* operatore somma con data → data *)
154 ndefinition plus_b8_d_d ≝
156 match plus_ex_d_dc (b8l b1) (b8l b2) with
157 [ pair l c ⇒ 〈plus_ex_dc_d (b8h b1) (b8h b2) c,l〉 ].
159 (* operatore somma con data → c *)
160 ndefinition plus_b8_d_c ≝
162 plus_ex_dc_c (b8h b1) (b8h b2) (plus_ex_d_c (b8l b1) (b8l b2)).
164 (* operatore Most Significant Bit *)
165 ndefinition MSB_b8 ≝ λb:byte8.eq_ex x8 (and_ex x8 (b8h b)).
167 (* byte → naturali *)
168 ndefinition nat_of_byte8 ≝ λb:byte8.(16*(nat_of_exadecim (b8h b))) + (nat_of_exadecim (b8l b)).
170 (* operatore predecessore *)
171 ndefinition pred_b8 ≝
172 λb:byte8.match eq_ex (b8l b) x0 with
173 [ true ⇒ mk_byte8 (pred_ex (b8h b)) (pred_ex (b8l b))
174 | false ⇒ mk_byte8 (b8h b) (pred_ex (b8l b)) ].
176 (* operatore successore *)
177 ndefinition succ_b8 ≝
178 λb:byte8.match eq_ex (b8l b) xF with
179 [ true ⇒ mk_byte8 (succ_ex (b8h b)) (succ_ex (b8l b))
180 | false ⇒ mk_byte8 (b8h b) (succ_ex (b8l b)) ].
182 (* operatore neg/complemento a 2 *)
183 ndefinition compl_b8 ≝
184 λb:byte8.match MSB_b8 b with
185 [ true ⇒ succ_b8 (not_b8 b)
186 | false ⇒ not_b8 (pred_b8 b) ].
188 (* operatore moltiplicazione senza segno: e*e=[0x00,0xE1] *)
190 λe1,e2:exadecim.match e1 with
192 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x0〉 | x2 ⇒ 〈x0,x0〉 | x3 ⇒ 〈x0,x0〉
193 | x4 ⇒ 〈x0,x0〉 | x5 ⇒ 〈x0,x0〉 | x6 ⇒ 〈x0,x0〉 | x7 ⇒ 〈x0,x0〉
194 | x8 ⇒ 〈x0,x0〉 | x9 ⇒ 〈x0,x0〉 | xA ⇒ 〈x0,x0〉 | xB ⇒ 〈x0,x0〉
195 | xC ⇒ 〈x0,x0〉 | xD ⇒ 〈x0,x0〉 | xE ⇒ 〈x0,x0〉 | xF ⇒ 〈x0,x0〉 ]
197 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x1〉 | x2 ⇒ 〈x0,x2〉 | x3 ⇒ 〈x0,x3〉
198 | x4 ⇒ 〈x0,x4〉 | x5 ⇒ 〈x0,x5〉 | x6 ⇒ 〈x0,x6〉 | x7 ⇒ 〈x0,x7〉
199 | x8 ⇒ 〈x0,x8〉 | x9 ⇒ 〈x0,x9〉 | xA ⇒ 〈x0,xA〉 | xB ⇒ 〈x0,xB〉
200 | xC ⇒ 〈x0,xC〉 | xD ⇒ 〈x0,xD〉 | xE ⇒ 〈x0,xE〉 | xF ⇒ 〈x0,xF〉 ]
202 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x2〉 | x2 ⇒ 〈x0,x4〉 | x3 ⇒ 〈x0,x6〉
203 | x4 ⇒ 〈x0,x8〉 | x5 ⇒ 〈x0,xA〉 | x6 ⇒ 〈x0,xC〉 | x7 ⇒ 〈x0,xE〉
204 | x8 ⇒ 〈x1,x0〉 | x9 ⇒ 〈x1,x2〉 | xA ⇒ 〈x1,x4〉 | xB ⇒ 〈x1,x6〉
205 | xC ⇒ 〈x1,x8〉 | xD ⇒ 〈x1,xA〉 | xE ⇒ 〈x1,xC〉 | xF ⇒ 〈x1,xE〉 ]
207 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x3〉 | x2 ⇒ 〈x0,x6〉 | x3 ⇒ 〈x0,x9〉
208 | x4 ⇒ 〈x0,xC〉 | x5 ⇒ 〈x0,xF〉 | x6 ⇒ 〈x1,x2〉 | x7 ⇒ 〈x1,x5〉
209 | x8 ⇒ 〈x1,x8〉 | x9 ⇒ 〈x1,xB〉 | xA ⇒ 〈x1,xE〉 | xB ⇒ 〈x2,x1〉
210 | xC ⇒ 〈x2,x4〉 | xD ⇒ 〈x2,x7〉 | xE ⇒ 〈x2,xA〉 | xF ⇒ 〈x2,xD〉 ]
212 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x4〉 | x2 ⇒ 〈x0,x8〉 | x3 ⇒ 〈x0,xC〉
213 | x4 ⇒ 〈x1,x0〉 | x5 ⇒ 〈x1,x4〉 | x6 ⇒ 〈x1,x8〉 | x7 ⇒ 〈x1,xC〉
214 | x8 ⇒ 〈x2,x0〉 | x9 ⇒ 〈x2,x4〉 | xA ⇒ 〈x2,x8〉 | xB ⇒ 〈x2,xC〉
215 | xC ⇒ 〈x3,x0〉 | xD ⇒ 〈x3,x4〉 | xE ⇒ 〈x3,x8〉 | xF ⇒ 〈x3,xC〉 ]
217 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x5〉 | x2 ⇒ 〈x0,xA〉 | x3 ⇒ 〈x0,xF〉
218 | x4 ⇒ 〈x1,x4〉 | x5 ⇒ 〈x1,x9〉 | x6 ⇒ 〈x1,xE〉 | x7 ⇒ 〈x2,x3〉
219 | x8 ⇒ 〈x2,x8〉 | x9 ⇒ 〈x2,xD〉 | xA ⇒ 〈x3,x2〉 | xB ⇒ 〈x3,x7〉
220 | xC ⇒ 〈x3,xC〉 | xD ⇒ 〈x4,x1〉 | xE ⇒ 〈x4,x6〉 | xF ⇒ 〈x4,xB〉 ]
222 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x6〉 | x2 ⇒ 〈x0,xC〉 | x3 ⇒ 〈x1,x2〉
223 | x4 ⇒ 〈x1,x8〉 | x5 ⇒ 〈x1,xE〉 | x6 ⇒ 〈x2,x4〉 | x7 ⇒ 〈x2,xA〉
224 | x8 ⇒ 〈x3,x0〉 | x9 ⇒ 〈x3,x6〉 | xA ⇒ 〈x3,xC〉 | xB ⇒ 〈x4,x2〉
225 | xC ⇒ 〈x4,x8〉 | xD ⇒ 〈x4,xE〉 | xE ⇒ 〈x5,x4〉 | xF ⇒ 〈x5,xA〉 ]
227 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x7〉 | x2 ⇒ 〈x0,xE〉 | x3 ⇒ 〈x1,x5〉
228 | x4 ⇒ 〈x1,xC〉 | x5 ⇒ 〈x2,x3〉 | x6 ⇒ 〈x2,xA〉 | x7 ⇒ 〈x3,x1〉
229 | x8 ⇒ 〈x3,x8〉 | x9 ⇒ 〈x3,xF〉 | xA ⇒ 〈x4,x6〉 | xB ⇒ 〈x4,xD〉
230 | xC ⇒ 〈x5,x4〉 | xD ⇒ 〈x5,xB〉 | xE ⇒ 〈x6,x2〉 | xF ⇒ 〈x6,x9〉 ]
232 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x8〉 | x2 ⇒ 〈x1,x0〉 | x3 ⇒ 〈x1,x8〉
233 | x4 ⇒ 〈x2,x0〉 | x5 ⇒ 〈x2,x8〉 | x6 ⇒ 〈x3,x0〉 | x7 ⇒ 〈x3,x8〉
234 | x8 ⇒ 〈x4,x0〉 | x9 ⇒ 〈x4,x8〉 | xA ⇒ 〈x5,x0〉 | xB ⇒ 〈x5,x8〉
235 | xC ⇒ 〈x6,x0〉 | xD ⇒ 〈x6,x8〉 | xE ⇒ 〈x7,x0〉 | xF ⇒ 〈x7,x8〉 ]
237 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,x9〉 | x2 ⇒ 〈x1,x2〉 | x3 ⇒ 〈x1,xB〉
238 | x4 ⇒ 〈x2,x4〉 | x5 ⇒ 〈x2,xD〉 | x6 ⇒ 〈x3,x6〉 | x7 ⇒ 〈x3,xF〉
239 | x8 ⇒ 〈x4,x8〉 | x9 ⇒ 〈x5,x1〉 | xA ⇒ 〈x5,xA〉 | xB ⇒ 〈x6,x3〉
240 | xC ⇒ 〈x6,xC〉 | xD ⇒ 〈x7,x5〉 | xE ⇒ 〈x7,xE〉 | xF ⇒ 〈x8,x7〉 ]
242 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xA〉 | x2 ⇒ 〈x1,x4〉 | x3 ⇒ 〈x1,xE〉
243 | x4 ⇒ 〈x2,x8〉 | x5 ⇒ 〈x3,x2〉 | x6 ⇒ 〈x3,xC〉 | x7 ⇒ 〈x4,x6〉
244 | x8 ⇒ 〈x5,x0〉 | x9 ⇒ 〈x5,xA〉 | xA ⇒ 〈x6,x4〉 | xB ⇒ 〈x6,xE〉
245 | xC ⇒ 〈x7,x8〉 | xD ⇒ 〈x8,x2〉 | xE ⇒ 〈x8,xC〉 | xF ⇒ 〈x9,x6〉 ]
247 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xB〉 | x2 ⇒ 〈x1,x6〉 | x3 ⇒ 〈x2,x1〉
248 | x4 ⇒ 〈x2,xC〉 | x5 ⇒ 〈x3,x7〉 | x6 ⇒ 〈x4,x2〉 | x7 ⇒ 〈x4,xD〉
249 | x8 ⇒ 〈x5,x8〉 | x9 ⇒ 〈x6,x3〉 | xA ⇒ 〈x6,xE〉 | xB ⇒ 〈x7,x9〉
250 | xC ⇒ 〈x8,x4〉 | xD ⇒ 〈x8,xF〉 | xE ⇒ 〈x9,xA〉 | xF ⇒ 〈xA,x5〉 ]
252 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xC〉 | x2 ⇒ 〈x1,x8〉 | x3 ⇒ 〈x2,x4〉
253 | x4 ⇒ 〈x3,x0〉 | x5 ⇒ 〈x3,xC〉 | x6 ⇒ 〈x4,x8〉 | x7 ⇒ 〈x5,x4〉
254 | x8 ⇒ 〈x6,x0〉 | x9 ⇒ 〈x6,xC〉 | xA ⇒ 〈x7,x8〉 | xB ⇒ 〈x8,x4〉
255 | xC ⇒ 〈x9,x0〉 | xD ⇒ 〈x9,xC〉 | xE ⇒ 〈xA,x8〉 | xF ⇒ 〈xB,x4〉 ]
257 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xD〉 | x2 ⇒ 〈x1,xA〉 | x3 ⇒ 〈x2,x7〉
258 | x4 ⇒ 〈x3,x4〉 | x5 ⇒ 〈x4,x1〉 | x6 ⇒ 〈x4,xE〉 | x7 ⇒ 〈x5,xB〉
259 | x8 ⇒ 〈x6,x8〉 | x9 ⇒ 〈x7,x5〉 | xA ⇒ 〈x8,x2〉 | xB ⇒ 〈x8,xF〉
260 | xC ⇒ 〈x9,xC〉 | xD ⇒ 〈xA,x9〉 | xE ⇒ 〈xB,x6〉 | xF ⇒ 〈xC,x3〉 ]
262 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xE〉 | x2 ⇒ 〈x1,xC〉 | x3 ⇒ 〈x2,xA〉
263 | x4 ⇒ 〈x3,x8〉 | x5 ⇒ 〈x4,x6〉 | x6 ⇒ 〈x5,x4〉 | x7 ⇒ 〈x6,x2〉
264 | x8 ⇒ 〈x7,x0〉 | x9 ⇒ 〈x7,xE〉 | xA ⇒ 〈x8,xC〉 | xB ⇒ 〈x9,xA〉
265 | xC ⇒ 〈xA,x8〉 | xD ⇒ 〈xB,x6〉 | xE ⇒ 〈xC,x4〉 | xF ⇒ 〈xD,x2〉 ]
267 [ x0 ⇒ 〈x0,x0〉 | x1 ⇒ 〈x0,xF〉 | x2 ⇒ 〈x1,xE〉 | x3 ⇒ 〈x2,xD〉
268 | x4 ⇒ 〈x3,xC〉 | x5 ⇒ 〈x4,xB〉 | x6 ⇒ 〈x5,xA〉 | x7 ⇒ 〈x6,x9〉
269 | x8 ⇒ 〈x7,x8〉 | x9 ⇒ 〈x8,x7〉 | xA ⇒ 〈x9,x6〉 | xB ⇒ 〈xA,x5〉
270 | xC ⇒ 〈xB,x4〉 | xD ⇒ 〈xC,x3〉 | xE ⇒ 〈xD,x2〉 | xF ⇒ 〈xE,x1〉 ]
273 (* correzione per somma su BCD *)
274 (* input: halfcarry,carry,X(BCD+BCD) *)
275 (* output: X',carry' *)
278 match lt_b8 X 〈x9,xA〉 with
281 (* X' = [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + [c=1 ? 0x60 : 0x00]
282 [(b16l X):0xA-0xF] X + 0x06 + [c=1 ? 0x60 : 0x00] *)
284 let X' ≝ match (lt_ex (b8l X) xA) ⊗ (⊖h) with
286 | false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
287 let X'' ≝ match c with
288 [ true ⇒ plus_b8_d_d X' 〈x6,x0〉
293 (* X' = [X:0x9A-0xFF]
294 [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + 0x60
295 [(b16l X):0xA-0xF] X + 0x6 + 0x60 *)
297 let X' ≝ match (lt_ex (b8l X) xA) ⊗ (⊖h) with
299 | false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
300 let X'' ≝ plus_b8_d_d X' 〈x6,x0〉 in
304 (* iteratore sui byte *)
305 ndefinition forall_byte8 ≝
307 forall_exadecim (λbh.
308 forall_exadecim (λbl.
309 P (mk_byte8 bh bl))).