]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/ng_assembly/num/byte8.ma
freescale porting
[helm.git] / helm / software / matita / contribs / ng_assembly / num / byte8.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: Ing. Cosimo Oliboni, oliboni@cs.unibo.it              *)
19 (*   Sviluppo: 2008-2010                                                  *)
20 (*                                                                        *)
21 (* ********************************************************************** *)
22
23 include "num/exadecim.ma".
24 include "num/comp_num.ma".
25 include "num/bitrigesim.ma".
26 include "common/nat.ma".
27
28 (* **** *)
29 (* BYTE *)
30 (* **** *)
31
32 ndefinition byte8 ≝ comp_num exadecim.
33 ndefinition mk_byte8 ≝ λe1,e2.mk_comp_num exadecim e1 e2.
34
35 (* \langle \rangle *)
36 notation "〈x,y〉" non associative with precedence 80
37  for @{ mk_comp_num exadecim $x $y }.
38
39 (* iteratore sui byte *)
40 ndefinition forall_b8 ≝ forall_cn ? forall_ex.
41
42 (* operatore = *)
43 ndefinition eq_b8 ≝ eq2_cn ? eq_ex.
44
45 (* operatore < *)
46 ndefinition lt_b8 ≝ ltgt_cn ? eq_ex lt_ex.
47
48 (* operatore ≤ *)
49 ndefinition le_b8 ≝ lege_cn ? eq_ex lt_ex le_ex.
50
51 (* operatore > *)
52 ndefinition gt_b8 ≝ ltgt_cn ? eq_ex gt_ex.
53
54 (* operatore ≥ *)
55 ndefinition ge_b8 ≝ lege_cn ? eq_ex gt_ex ge_ex.
56
57 (* operatore and *)
58 ndefinition and_b8 ≝ fop2_cn ? and_ex.
59
60 (* operatore or *)
61 ndefinition or_b8 ≝ fop2_cn ? or_ex.
62
63 (* operatore xor *)
64 ndefinition xor_b8 ≝ fop2_cn ? xor_ex.
65
66 (* operatore Most Significant Bit *)
67 ndefinition getMSB_b8 ≝ getOPH_cn ? getMSB_ex.
68 ndefinition setMSB_b8 ≝ setOPH_cn ? setMSB_ex.
69 ndefinition clrMSB_b8 ≝ setOPH_cn ? clrMSB_ex.
70
71 (* operatore Least Significant Bit *)
72 ndefinition getLSB_b8 ≝ getOPL_cn ? getLSB_ex.
73 ndefinition setLSB_b8 ≝ setOPL_cn ? setLSB_ex.
74 ndefinition clrLSB_b8 ≝ setOPL_cn ? clrLSB_ex.
75
76 (* operatore estensione unsigned *)
77 ndefinition extu_b8 ≝ λe2.〈x0,e2〉.
78
79 (* operatore estensione signed *)
80 ndefinition exts_b8 ≝
81 λe2.〈(match getMSB_ex e2 with 
82       [ true ⇒ xF | false ⇒ x0 ]),e2〉.
83
84 (* operatore rotazione destra con carry *)
85 ndefinition rcr_b8 ≝ opcr_cn ? rcr_ex.
86
87 (* operatore shift destro *)
88 ndefinition shr_b8 ≝ opcr_cn ? rcr_ex false.
89
90 (* operatore rotazione destra *)
91 ndefinition ror_b8 ≝
92 λb.match shr_b8 b with
93  [ pair c b' ⇒ match c with
94   [ true ⇒ setMSB_b8 b' | false ⇒ b' ]].
95
96 (* operatore rotazione sinistra con carry *)
97 ndefinition rcl_b8 ≝ opcl_cn ? rcl_ex.
98
99 (* operatore shift sinistro *)
100 ndefinition shl_b8 ≝ opcl_cn ? rcl_ex false.
101
102 (* operatore rotazione sinistra *)
103 ndefinition rol_b8 ≝
104 λb.match shl_b8 b with
105  [ pair c b' ⇒ match c with
106   [ true ⇒ setLSB_b8 b' | false ⇒ b' ]].
107
108 (* operatore not/complemento a 1 *)
109 ndefinition not_b8 ≝ fop_cn ? not_ex.
110
111 (* operatore somma con data+carry → data+carry *)
112 ndefinition plus_b8_dc_dc ≝ opcl2_cn ? plus_ex_dc_dc.
113
114 (* operatore somma con data+carry → data *)
115 ndefinition plus_b8_dc_d ≝ λc,b1,b2.snd … (plus_b8_dc_dc c b1 b2).
116
117 (* operatore somma con data+carry → c *)
118 ndefinition plus_b8_dc_c ≝ λc,b1,b2.fst … (plus_b8_dc_dc c b1 b2).
119
120 (* operatore somma con data → data+carry *)
121 ndefinition plus_b8_d_dc ≝ opcl2_cn ? plus_ex_dc_dc false.
122
123 (* operatore somma con data → data *)
124 ndefinition plus_b8_d_d ≝ λb1,b2.snd … (plus_b8_d_dc b1 b2).
125
126 (* operatore somma con data → c *)
127 ndefinition plus_b8_d_c ≝ λb1,b2.fst … (plus_b8_d_dc b1 b2).
128
129 (* operatore predecessore *)
130 ndefinition pred_b8 ≝ predsucc_cn ? (eq_ex x0) pred_ex.
131
132 (* operatore successore *)
133 ndefinition succ_b8 ≝ predsucc_cn ? (eq_ex xF) succ_ex.
134
135 (* operatore neg/complemento a 2 *)
136 ndefinition compl_b8 ≝
137 λb:byte8.match getMSB_b8 b with
138  [ true ⇒ succ_b8 (not_b8 b)
139  | false ⇒ not_b8 (pred_b8 b) ].
140
141 (* operatore x in [inf,sup] o in sup],[inf *)
142 ndefinition inrange_b8 ≝
143 λx,inf,sup:byte8.
144  match le_b8 inf sup with
145   [ true ⇒ and_bool | false ⇒ or_bool ]
146  (le_b8 inf x) (le_b8 x sup).
147
148 (* operatore moltiplicazione senza segno: e*e=[0x00,0xE1] *)
149 ndefinition mul_ex ≝
150 λe1,e2:exadecim.match e1 with
151  [ x0 ⇒ match e2 with
152   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x0〉   | x2 ⇒ 〈x0,x0〉   | x3 ⇒ 〈x0,x0〉
153   | x4 ⇒ 〈x0,x0〉   | x5 ⇒ 〈x0,x0〉   | x6 ⇒ 〈x0,x0〉   | x7 ⇒ 〈x0,x0〉
154   | x8 ⇒ 〈x0,x0〉   | x9 ⇒ 〈x0,x0〉   | xA ⇒ 〈x0,x0〉   | xB ⇒ 〈x0,x0〉
155   | xC ⇒ 〈x0,x0〉   | xD ⇒ 〈x0,x0〉   | xE ⇒ 〈x0,x0〉   | xF ⇒ 〈x0,x0〉 ]
156  | x1 ⇒ match e2 with
157   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x1〉   | x2 ⇒ 〈x0,x2〉   | x3 ⇒ 〈x0,x3〉
158   | x4 ⇒ 〈x0,x4〉   | x5 ⇒ 〈x0,x5〉   | x6 ⇒ 〈x0,x6〉   | x7 ⇒ 〈x0,x7〉
159   | x8 ⇒ 〈x0,x8〉   | x9 ⇒ 〈x0,x9〉   | xA ⇒ 〈x0,xA〉   | xB ⇒ 〈x0,xB〉
160   | xC ⇒ 〈x0,xC〉   | xD ⇒ 〈x0,xD〉   | xE ⇒ 〈x0,xE〉   | xF ⇒ 〈x0,xF〉 ]
161  | x2 ⇒ match e2 with
162   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x2〉   | x2 ⇒ 〈x0,x4〉   | x3 ⇒ 〈x0,x6〉
163   | x4 ⇒ 〈x0,x8〉   | x5 ⇒ 〈x0,xA〉   | x6 ⇒ 〈x0,xC〉   | x7 ⇒ 〈x0,xE〉
164   | x8 ⇒ 〈x1,x0〉   | x9 ⇒ 〈x1,x2〉   | xA ⇒ 〈x1,x4〉   | xB ⇒ 〈x1,x6〉
165   | xC ⇒ 〈x1,x8〉   | xD ⇒ 〈x1,xA〉   | xE ⇒ 〈x1,xC〉   | xF ⇒ 〈x1,xE〉 ]
166  | x3 ⇒ match e2 with
167   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x3〉   | x2 ⇒ 〈x0,x6〉   | x3 ⇒ 〈x0,x9〉
168   | x4 ⇒ 〈x0,xC〉   | x5 ⇒ 〈x0,xF〉   | x6 ⇒ 〈x1,x2〉   | x7 ⇒ 〈x1,x5〉
169   | x8 ⇒ 〈x1,x8〉   | x9 ⇒ 〈x1,xB〉   | xA ⇒ 〈x1,xE〉   | xB ⇒ 〈x2,x1〉
170   | xC ⇒ 〈x2,x4〉   | xD ⇒ 〈x2,x7〉   | xE ⇒ 〈x2,xA〉   | xF ⇒ 〈x2,xD〉 ]
171  | x4 ⇒ match e2 with
172   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x4〉   | x2 ⇒ 〈x0,x8〉   | x3 ⇒ 〈x0,xC〉
173   | x4 ⇒ 〈x1,x0〉   | x5 ⇒ 〈x1,x4〉   | x6 ⇒ 〈x1,x8〉   | x7 ⇒ 〈x1,xC〉
174   | x8 ⇒ 〈x2,x0〉   | x9 ⇒ 〈x2,x4〉   | xA ⇒ 〈x2,x8〉   | xB ⇒ 〈x2,xC〉
175   | xC ⇒ 〈x3,x0〉   | xD ⇒ 〈x3,x4〉   | xE ⇒ 〈x3,x8〉   | xF ⇒ 〈x3,xC〉 ]
176  | x5 ⇒ match e2 with
177   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x5〉   | x2 ⇒ 〈x0,xA〉   | x3 ⇒ 〈x0,xF〉
178   | x4 ⇒ 〈x1,x4〉   | x5 ⇒ 〈x1,x9〉   | x6 ⇒ 〈x1,xE〉   | x7 ⇒ 〈x2,x3〉
179   | x8 ⇒ 〈x2,x8〉   | x9 ⇒ 〈x2,xD〉   | xA ⇒ 〈x3,x2〉   | xB ⇒ 〈x3,x7〉
180   | xC ⇒ 〈x3,xC〉   | xD ⇒ 〈x4,x1〉   | xE ⇒ 〈x4,x6〉   | xF ⇒ 〈x4,xB〉 ]
181  | x6 ⇒ match e2 with
182   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x6〉   | x2 ⇒ 〈x0,xC〉   | x3 ⇒ 〈x1,x2〉
183   | x4 ⇒ 〈x1,x8〉   | x5 ⇒ 〈x1,xE〉   | x6 ⇒ 〈x2,x4〉   | x7 ⇒ 〈x2,xA〉
184   | x8 ⇒ 〈x3,x0〉   | x9 ⇒ 〈x3,x6〉   | xA ⇒ 〈x3,xC〉   | xB ⇒ 〈x4,x2〉
185   | xC ⇒ 〈x4,x8〉   | xD ⇒ 〈x4,xE〉   | xE ⇒ 〈x5,x4〉   | xF ⇒ 〈x5,xA〉 ]
186  | x7 ⇒ match e2 with
187   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x7〉   | x2 ⇒ 〈x0,xE〉   | x3 ⇒ 〈x1,x5〉
188   | x4 ⇒ 〈x1,xC〉   | x5 ⇒ 〈x2,x3〉   | x6 ⇒ 〈x2,xA〉   | x7 ⇒ 〈x3,x1〉
189   | x8 ⇒ 〈x3,x8〉   | x9 ⇒ 〈x3,xF〉   | xA ⇒ 〈x4,x6〉   | xB ⇒ 〈x4,xD〉
190   | xC ⇒ 〈x5,x4〉   | xD ⇒ 〈x5,xB〉   | xE ⇒ 〈x6,x2〉   | xF ⇒ 〈x6,x9〉 ]
191  | x8 ⇒ match e2 with
192   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x8〉   | x2 ⇒ 〈x1,x0〉   | x3 ⇒ 〈x1,x8〉
193   | x4 ⇒ 〈x2,x0〉   | x5 ⇒ 〈x2,x8〉   | x6 ⇒ 〈x3,x0〉   | x7 ⇒ 〈x3,x8〉
194   | x8 ⇒ 〈x4,x0〉   | x9 ⇒ 〈x4,x8〉   | xA ⇒ 〈x5,x0〉   | xB ⇒ 〈x5,x8〉
195   | xC ⇒ 〈x6,x0〉   | xD ⇒ 〈x6,x8〉   | xE ⇒ 〈x7,x0〉   | xF ⇒ 〈x7,x8〉 ]
196  | x9 ⇒ match e2 with
197   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x9〉   | x2 ⇒ 〈x1,x2〉   | x3 ⇒ 〈x1,xB〉
198   | x4 ⇒ 〈x2,x4〉   | x5 ⇒ 〈x2,xD〉   | x6 ⇒ 〈x3,x6〉   | x7 ⇒ 〈x3,xF〉
199   | x8 ⇒ 〈x4,x8〉   | x9 ⇒ 〈x5,x1〉   | xA ⇒ 〈x5,xA〉   | xB ⇒ 〈x6,x3〉
200   | xC ⇒ 〈x6,xC〉   | xD ⇒ 〈x7,x5〉   | xE ⇒ 〈x7,xE〉   | xF ⇒ 〈x8,x7〉 ]
201  | xA ⇒ match e2 with
202   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xA〉   | x2 ⇒ 〈x1,x4〉   | x3 ⇒ 〈x1,xE〉
203   | x4 ⇒ 〈x2,x8〉   | x5 ⇒ 〈x3,x2〉   | x6 ⇒ 〈x3,xC〉   | x7 ⇒ 〈x4,x6〉
204   | x8 ⇒ 〈x5,x0〉   | x9 ⇒ 〈x5,xA〉   | xA ⇒ 〈x6,x4〉   | xB ⇒ 〈x6,xE〉
205   | xC ⇒ 〈x7,x8〉   | xD ⇒ 〈x8,x2〉   | xE ⇒ 〈x8,xC〉   | xF ⇒ 〈x9,x6〉 ]
206  | xB ⇒ match e2 with
207   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xB〉   | x2 ⇒ 〈x1,x6〉   | x3 ⇒ 〈x2,x1〉
208   | x4 ⇒ 〈x2,xC〉   | x5 ⇒ 〈x3,x7〉   | x6 ⇒ 〈x4,x2〉   | x7 ⇒ 〈x4,xD〉
209   | x8 ⇒ 〈x5,x8〉   | x9 ⇒ 〈x6,x3〉   | xA ⇒ 〈x6,xE〉   | xB ⇒ 〈x7,x9〉
210   | xC ⇒ 〈x8,x4〉   | xD ⇒ 〈x8,xF〉   | xE ⇒ 〈x9,xA〉   | xF ⇒ 〈xA,x5〉 ]
211  | xC ⇒ match e2 with
212   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xC〉   | x2 ⇒ 〈x1,x8〉   | x3 ⇒ 〈x2,x4〉
213   | x4 ⇒ 〈x3,x0〉   | x5 ⇒ 〈x3,xC〉   | x6 ⇒ 〈x4,x8〉   | x7 ⇒ 〈x5,x4〉
214   | x8 ⇒ 〈x6,x0〉   | x9 ⇒ 〈x6,xC〉   | xA ⇒ 〈x7,x8〉   | xB ⇒ 〈x8,x4〉
215   | xC ⇒ 〈x9,x0〉   | xD ⇒ 〈x9,xC〉   | xE ⇒ 〈xA,x8〉   | xF ⇒ 〈xB,x4〉 ]
216  | xD ⇒ match e2 with
217   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xD〉   | x2 ⇒ 〈x1,xA〉   | x3 ⇒ 〈x2,x7〉
218   | x4 ⇒ 〈x3,x4〉   | x5 ⇒ 〈x4,x1〉   | x6 ⇒ 〈x4,xE〉   | x7 ⇒ 〈x5,xB〉
219   | x8 ⇒ 〈x6,x8〉   | x9 ⇒ 〈x7,x5〉   | xA ⇒ 〈x8,x2〉   | xB ⇒ 〈x8,xF〉
220   | xC ⇒ 〈x9,xC〉   | xD ⇒ 〈xA,x9〉   | xE ⇒ 〈xB,x6〉   | xF ⇒ 〈xC,x3〉 ]
221  | xE ⇒ match e2 with
222   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xE〉   | x2 ⇒ 〈x1,xC〉   | x3 ⇒ 〈x2,xA〉
223   | x4 ⇒ 〈x3,x8〉   | x5 ⇒ 〈x4,x6〉   | x6 ⇒ 〈x5,x4〉   | x7 ⇒ 〈x6,x2〉
224   | x8 ⇒ 〈x7,x0〉   | x9 ⇒ 〈x7,xE〉   | xA ⇒ 〈x8,xC〉   | xB ⇒ 〈x9,xA〉
225   | xC ⇒ 〈xA,x8〉   | xD ⇒ 〈xB,x6〉   | xE ⇒ 〈xC,x4〉   | xF ⇒ 〈xD,x2〉 ]
226  | xF ⇒ match e2 with
227   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xF〉   | x2 ⇒ 〈x1,xE〉   | x3 ⇒ 〈x2,xD〉
228   | x4 ⇒ 〈x3,xC〉   | x5 ⇒ 〈x4,xB〉   | x6 ⇒ 〈x5,xA〉   | x7 ⇒ 〈x6,x9〉
229   | x8 ⇒ 〈x7,x8〉   | x9 ⇒ 〈x8,x7〉   | xA ⇒ 〈x9,x6〉   | xB ⇒ 〈xA,x5〉
230   | xC ⇒ 〈xB,x4〉   | xD ⇒ 〈xC,x3〉   | xE ⇒ 〈xD,x2〉   | xF ⇒ 〈xE,x1〉 ]
231  ].
232
233 (* correzione per somma su BCD *)
234 (* input: halfcarry,carry,X(BCD+BCD) *)
235 (* output: X',carry' *)
236 ndefinition daa_b8 ≝
237 λh,c:bool.λX:byte8.
238  match lt_b8 X 〈x9,xA〉 with
239   (* [X:0x00-0x99] *)
240   (* c' = c *)
241   (* X' = [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + [c=1 ? 0x60 : 0x00]
242           [(b16l X):0xA-0xF] X + 0x06 + [c=1 ? 0x60 : 0x00] *)
243   [ true ⇒
244    let X' ≝ match (lt_ex (cnL ? X) xA) ⊗ (⊖h) with
245     [ true ⇒ X
246     | false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
247    let X'' ≝ match c with
248     [ true ⇒ plus_b8_d_d X' 〈x6,x0〉
249     | false ⇒ X' ] in
250    pair … X'' c
251   (* [X:0x9A-0xFF] *)
252   (* c' = 1 *)
253   (* X' = [X:0x9A-0xFF]
254           [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + 0x60
255           [(b16l X):0xA-0xF] X + 0x6 + 0x60 *) 
256   | false ⇒
257    let X' ≝ match (lt_ex (cnL ? X) xA) ⊗ (⊖h) with
258     [ true ⇒ X
259     | false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
260    let X'' ≝ plus_b8_d_d X' 〈x6,x0〉 in
261    pair … X'' true
262   ].
263
264 (* divisione senza segno (secondo la logica delle ALU): (quoziente resto) overflow *)
265 nlet rec div_b8_ex_aux (divd:byte8) (divs:byte8) (molt:exadecim) (q:exadecim) (n:nat) on n ≝
266  let w' ≝ plus_b8_d_d divd (compl_b8 divs) in
267   match n with
268   [ O ⇒ match le_b8 divs divd with
269    [ true ⇒ triple … (or_ex molt q) (cnL ? w') (⊖ (eq_ex (cnH ? w') x0))
270    | false ⇒ triple … q (cnL ? divd) (⊖ (eq_ex (cnH ? divd) x0)) ]
271   | S n' ⇒ match le_b8 divs divd with
272    [ true ⇒ div_b8_ex_aux w' (ror_b8 divs) (ror_ex molt) (or_ex molt q) n'
273    | false ⇒ div_b8_ex_aux divd (ror_b8 divs) (ror_ex molt) q n' ]].
274
275 ndefinition div_b8_ex ≝
276 λb:byte8.λe:exadecim.match eq_ex e x0 with
277 (* la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato *)
278  [ true ⇒ triple … xF (cnL ? b) true
279  | false ⇒ match eq_b8 b 〈x0,x0〉 with
280 (* 0 diviso qualsiasi cosa diverso da 0 da' q=0 r=0 o=false *)
281   [ true ⇒ triple … x0 x0 false
282 (* 1) e' una divisione sensata che produrra' overflow/risultato *)
283 (* 2) parametri: dividendo, divisore, moltiplicatore, quoziente, contatore *)
284 (* 3) ad ogni ciclo il divisore e il moltiplicatore vengono scalati di 1 a dx *)
285 (* 4) il moltiplicatore e' la quantita' aggiunta al quoziente se il divisore *)
286 (*    puo' essere sottratto al dividendo *) 
287   | false ⇒ div_b8_ex_aux b (nat_it ? rol_b8 〈x0,e〉 nat3) x8 x0 nat3 ]].
288
289 (* byte ricorsivi *)
290 ninductive rec_byte8 : byte8 → Type ≝
291   b8_O : rec_byte8 〈x0,x0〉
292 | b8_S : ∀n.rec_byte8 n → rec_byte8 (succ_b8 n).
293
294 (* byte → byte ricorsivi *)
295 ndefinition b8_to_recb8_aux1 : Πn.rec_byte8 〈n,x0〉 → rec_byte8 〈succ_ex n,x0〉 ≝
296 λn.λrecb:rec_byte8 〈n,x0〉.
297  b8_S 〈n,xF〉 (b8_S 〈n,xE〉 (b8_S 〈n,xD〉 (b8_S 〈n,xC〉 (
298  b8_S 〈n,xB〉 (b8_S 〈n,xA〉 (b8_S 〈n,x9〉 (b8_S 〈n,x8〉 (
299  b8_S 〈n,x7〉 (b8_S 〈n,x6〉 (b8_S 〈n,x5〉 (b8_S 〈n,x4〉 (
300  b8_S 〈n,x3〉 (b8_S 〈n,x2〉 (b8_S 〈n,x1〉 (b8_S 〈n,x0〉 recb))))))))))))))).
301
302 (* ... cifra esadecimale superiore *)
303 nlet rec b8_to_recb8_aux2 (n:exadecim) (r:rec_exadecim n) on r ≝
304  match r return λx.λy:rec_exadecim x.rec_byte8 〈x,x0〉 with
305   [ ex_O ⇒ b8_O
306   | ex_S t n' ⇒ b8_to_recb8_aux1 ? (b8_to_recb8_aux2 t n')
307   ].
308
309 (* ... cifra esadecimale inferiore *)
310 ndefinition b8_to_recb8_aux3 : Πn1,n2.rec_byte8 〈n1,x0〉 → rec_byte8 〈n1,n2〉 ≝
311 λn1,n2.λrecb:rec_byte8 〈n1,x0〉.
312  match n2 return λx.rec_byte8 〈n1,x〉 with
313   [ x0 ⇒ recb
314   | x1 ⇒ b8_S 〈n1,x0〉 recb
315   | x2 ⇒ b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)
316   | x3 ⇒ b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))
317   | x4 ⇒ b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))
318   | x5 ⇒ b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
319          b8_S 〈n1,x0〉 recb))))
320   | x6 ⇒ b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
321          b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))
322   | x7 ⇒ b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
323          b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))
324   | x8 ⇒ b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (
325          b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))
326   | x9 ⇒ b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (
327          b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
328          b8_S 〈n1,x0〉 recb))))))))
329   | xA ⇒ b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (
330          b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
331          b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))
332   | xB ⇒ b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (
333          b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
334          b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))))))
335   | xC ⇒ b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (
336          b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (
337          b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))))
338   | xD ⇒ b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (
339          b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (
340          b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
341          b8_S 〈n1,x0〉 recb))))))))))))
342   | xE ⇒ b8_S 〈n1,xD〉 (b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (
343          b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (
344          b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
345          b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))))))
346   | xF ⇒ b8_S 〈n1,xE〉 (b8_S 〈n1,xD〉 (b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (
347          b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (
348          b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
349          b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))))))))))
350   ].
351
352 (*
353 nlemma b8_to_recb8 : Πb.rec_byte8 b.
354  #b; nletin K ≝ (b8_to_recb8_aux3 
355      (b8h b) (b8l b) (b8_to_recb8_aux2 (b8h b) (ex_to_recex (b8h b))));
356  ncases b in K; #e1; #e2; #K; napply K;
357 nqed.
358 *)
359
360 ndefinition b8_to_recb8 : Πb.rec_byte8 b ≝
361 λb.match b with
362  [ mk_comp_num h l ⇒ b8_to_recb8_aux3 h l (b8_to_recb8_aux2 h (ex_to_recex h)) ].
363
364 (* ottali → esadecimali *)
365 ndefinition b8_of_bit ≝
366 λn.match n with
367  [ t00 ⇒ 〈x0,x0〉 | t01 ⇒ 〈x0,x1〉 | t02 ⇒ 〈x0,x2〉 | t03 ⇒ 〈x0,x3〉
368  | t04 ⇒ 〈x0,x4〉 | t05 ⇒ 〈x0,x5〉 | t06 ⇒ 〈x0,x6〉 | t07 ⇒ 〈x0,x7〉
369  | t08 ⇒ 〈x0,x8〉 | t09 ⇒ 〈x0,x9〉 | t0A ⇒ 〈x0,xA〉 | t0B ⇒ 〈x0,xB〉
370  | t0C ⇒ 〈x0,xC〉 | t0D ⇒ 〈x0,xD〉 | t0E ⇒ 〈x0,xE〉 | t0F ⇒ 〈x0,xF〉
371  | t10 ⇒ 〈x1,x0〉 | t11 ⇒ 〈x1,x1〉 | t12 ⇒ 〈x1,x2〉 | t13 ⇒ 〈x1,x3〉
372  | t14 ⇒ 〈x1,x4〉 | t15 ⇒ 〈x1,x5〉 | t16 ⇒ 〈x1,x6〉 | t17 ⇒ 〈x1,x7〉
373  | t18 ⇒ 〈x1,x8〉 | t19 ⇒ 〈x1,x9〉 | t1A ⇒ 〈x1,xA〉 | t1B ⇒ 〈x1,xB〉
374  | t1C ⇒ 〈x1,xC〉 | t1D ⇒ 〈x1,xD〉 | t1E ⇒ 〈x1,xE〉 | t1F ⇒ 〈x1,xF〉
375  ].