(* ********************************************************************** *)
include "num/exadecim.ma".
+include "num/comp_num.ma".
include "num/bitrigesim.ma".
+include "common/nat.ma".
(* **** *)
(* BYTE *)
(* **** *)
-nrecord byte8 : Type ≝
- {
- b8h: exadecim;
- b8l: exadecim
- }.
+ndefinition byte8 ≝ comp_num exadecim.
+ndefinition mk_byte8 ≝ λe1,e2.mk_comp_num exadecim e1 e2.
(* \langle \rangle *)
notation "〈x,y〉" non associative with precedence 80
- for @{ 'mk_byte8 $x $y }.
-interpretation "mk_byte8" 'mk_byte8 x y = (mk_byte8 x y).
+ for @{ mk_comp_num exadecim $x $y }.
+
+(* iteratore sui byte *)
+ndefinition forall_b8 ≝ forall_cn ? forall_ex.
(* operatore = *)
-ndefinition eq_b8 ≝ λb1,b2:byte8.(eq_ex (b8h b1) (b8h b2)) ⊗ (eq_ex (b8l b1) (b8l b2)).
+ndefinition eq_b8 ≝ eq2_cn ? eq_ex.
(* operatore < *)
-ndefinition lt_b8 ≝
-λb1,b2:byte8.
- (lt_ex (b8h b1) (b8h b2)) ⊕
- ((eq_ex (b8h b1) (b8h b2)) ⊗ (lt_ex (b8l b1) (b8l b2))).
+ndefinition lt_b8 ≝ ltgt_cn ? eq_ex lt_ex.
(* operatore ≤ *)
-ndefinition le_b8 ≝
-λb1,b2:byte8.
- (lt_ex (b8h b1) (b8h b2)) ⊕
- ((eq_ex (b8h b1) (b8h b2)) ⊗ (le_ex (b8l b1) (b8l b2))).
+ndefinition le_b8 ≝ lege_cn ? eq_ex lt_ex le_ex.
(* operatore > *)
-ndefinition gt_b8 ≝
-λb1,b2:byte8.
- (gt_ex (b8h b1) (b8h b2)) ⊕
- ((eq_ex (b8h b1) (b8h b2)) ⊗ (gt_ex (b8l b1) (b8l b2))).
+ndefinition gt_b8 ≝ ltgt_cn ? eq_ex gt_ex.
(* operatore ≥ *)
-ndefinition ge_b8 ≝
-λb1,b2:byte8.
- (gt_ex (b8h b1) (b8h b2)) ⊕
- ((eq_ex (b8h b1) (b8h b2)) ⊗ (ge_ex (b8l b1) (b8l b2))).
+ndefinition ge_b8 ≝ lege_cn ? eq_ex gt_ex ge_ex.
(* operatore and *)
-ndefinition and_b8 ≝
-λb1,b2:byte8.mk_byte8 (and_ex (b8h b1) (b8h b2)) (and_ex (b8l b1) (b8l b2)).
+ndefinition and_b8 ≝ fop2_cn ? and_ex.
(* operatore or *)
-ndefinition or_b8 ≝
-λb1,b2:byte8.mk_byte8 (or_ex (b8h b1) (b8h b2)) (or_ex (b8l b1) (b8l b2)).
+ndefinition or_b8 ≝ fop2_cn ? or_ex.
(* operatore xor *)
-ndefinition xor_b8 ≝
-λb1,b2:byte8.mk_byte8 (xor_ex (b8h b1) (b8h b2)) (xor_ex (b8l b1) (b8l b2)).
+ndefinition xor_b8 ≝ fop2_cn ? xor_ex.
+
+(* operatore Most Significant Bit *)
+ndefinition getMSB_b8 ≝ getOPH_cn ? getMSB_ex.
+ndefinition setMSB_b8 ≝ setOPH_cn ? setMSB_ex.
+
+(* operatore Least Significant Bit *)
+ndefinition getLSB_b8 ≝ getOPL_cn ? getLSB_ex.
+ndefinition setLSB_b8 ≝ setOPL_cn ? setLSB_ex.
(* operatore rotazione destra con carry *)
-ndefinition rcr_b8 ≝
-λb:byte8.λc:bool.match rcr_ex (b8h b) c with
- [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
- [ pair bl' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
+ndefinition rcr_b8 ≝ opcr_cn ? rcr_ex.
(* operatore shift destro *)
-ndefinition shr_b8 ≝
-λb:byte8.match shr_ex (b8h b) with
- [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
- [ pair bl' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
+ndefinition shr_b8 ≝ opcr_cn ? rcr_ex false.
(* operatore rotazione destra *)
ndefinition ror_b8 ≝
-λb:byte8.match shr_ex (b8h b) with
- [ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
- [ pair bl' c'' ⇒ match c'' with
- [ true ⇒ mk_byte8 (or_ex x8 bh') bl'
- | false ⇒ mk_byte8 bh' bl' ]]].
-
-(* operatore rotazione destra n-volte *)
-nlet rec ror_b8_n (b:byte8) (o:oct) (ro:rec_oct o) on ro ≝
- match ro with
- [ oc_O ⇒ b
- | oc_S o' n' ⇒ ror_b8_n (ror_b8 b) o' n' ].
+λb.match shr_b8 b with
+ [ pair c b' ⇒ match c with
+ [ true ⇒ setMSB_b8 b' | false ⇒ b' ]].
(* operatore rotazione sinistra con carry *)
-ndefinition rcl_b8 ≝
-λb:byte8.λc:bool.match rcl_ex (b8l b) c with
- [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
- [ pair bh' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
+ndefinition rcl_b8 ≝ opcl_cn ? rcl_ex.
(* operatore shift sinistro *)
-ndefinition shl_b8 ≝
-λb:byte8.match shl_ex (b8l b) with
- [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
- [ pair bh' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
+ndefinition shl_b8 ≝ opcl_cn ? rcl_ex false.
(* operatore rotazione sinistra *)
ndefinition rol_b8 ≝
-λb:byte8.match shl_ex (b8l b) with
- [ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
- [ pair bh' c'' ⇒ match c'' with
- [ true ⇒ mk_byte8 bh' (or_ex x1 bl')
- | false ⇒ mk_byte8 bh' bl' ]]].
-
-(* operatore rotazione sinistra n-volte *)
-nlet rec rol_b8_n (b:byte8) (o:oct) (ro:rec_oct o) on ro ≝
- match ro with
- [ oc_O ⇒ b
- | oc_S o' n' ⇒ rol_b8_n (rol_b8 b) o' n' ].
+λb.match shl_b8 b with
+ [ pair c b' ⇒ match c with
+ [ true ⇒ setLSB_b8 b' | false ⇒ b' ]].
(* operatore not/complemento a 1 *)
-ndefinition not_b8 ≝
-λb:byte8.mk_byte8 (not_ex (b8h b)) (not_ex (b8l b)).
+ndefinition not_b8 ≝ fop_cn ? not_ex.
(* operatore somma con data+carry → data+carry *)
-ndefinition plus_b8_dc_dc ≝
-λb1,b2:byte8.λc:bool.
- match plus_ex_dc_dc (b8l b1) (b8l b2) c with
- [ pair l c' ⇒ match plus_ex_dc_dc (b8h b1) (b8h b2) c' with
- [ pair h c'' ⇒ pair … 〈h,l〉 c'' ]].
+ndefinition plus_b8_dc_dc ≝ opcl2_cn ? plus_ex_dc_dc.
(* operatore somma con data+carry → data *)
-ndefinition plus_b8_dc_d ≝
-λb1,b2:byte8.λc:bool.
- match plus_ex_dc_dc (b8l b1) (b8l b2) c with
- [ pair l c' ⇒ 〈plus_ex_dc_d (b8h b1) (b8h b2) c',l〉 ].
+ndefinition plus_b8_dc_d ≝ λc,b1,b2.snd … (plus_b8_dc_dc c b1 b2).
(* operatore somma con data+carry → c *)
-ndefinition plus_b8_dc_c ≝
-λb1,b2:byte8.λc:bool.
- plus_ex_dc_c (b8h b1) (b8h b2) (plus_ex_dc_c (b8l b1) (b8l b2) c).
+ndefinition plus_b8_dc_c ≝ λc,b1,b2.fst … (plus_b8_dc_dc c b1 b2).
(* operatore somma con data → data+carry *)
-ndefinition plus_b8_d_dc ≝
-λb1,b2:byte8.
- match plus_ex_d_dc (b8l b1) (b8l b2) with
- [ pair l c ⇒ match plus_ex_dc_dc (b8h b1) (b8h b2) c with
- [ pair h c' ⇒ pair … 〈h,l〉 c' ]].
+ndefinition plus_b8_d_dc ≝ opcl2_cn ? plus_ex_dc_dc false.
(* operatore somma con data → data *)
-ndefinition plus_b8_d_d ≝
-λb1,b2:byte8.
- match plus_ex_d_dc (b8l b1) (b8l b2) with
- [ pair l c ⇒ 〈plus_ex_dc_d (b8h b1) (b8h b2) c,l〉 ].
+ndefinition plus_b8_d_d ≝ λb1,b2.snd … (plus_b8_d_dc b1 b2).
(* operatore somma con data → c *)
-ndefinition plus_b8_d_c ≝
-λb1,b2:byte8.
- plus_ex_dc_c (b8h b1) (b8h b2) (plus_ex_d_c (b8l b1) (b8l b2)).
-
-(* operatore Most Significant Bit *)
-ndefinition MSB_b8 ≝ λb:byte8.eq_ex x8 (and_ex x8 (b8h b)).
+ndefinition plus_b8_d_c ≝ λb1,b2.fst … (plus_b8_d_dc b1 b2).
(* operatore predecessore *)
-ndefinition pred_b8 ≝
-λb:byte8.match eq_ex (b8l b) x0 with
- [ true ⇒ mk_byte8 (pred_ex (b8h b)) (pred_ex (b8l b))
- | false ⇒ mk_byte8 (b8h b) (pred_ex (b8l b)) ].
+ndefinition pred_b8 ≝ predsucc_cn ? (eq_ex x0) pred_ex.
(* operatore successore *)
-ndefinition succ_b8 ≝
-λb:byte8.match eq_ex (b8l b) xF with
- [ true ⇒ mk_byte8 (succ_ex (b8h b)) (succ_ex (b8l b))
- | false ⇒ mk_byte8 (b8h b) (succ_ex (b8l b)) ].
+ndefinition succ_b8 ≝ predsucc_cn ? (eq_ex xF) succ_ex.
(* operatore neg/complemento a 2 *)
ndefinition compl_b8 ≝
-λb:byte8.match MSB_b8 b with
+λb:byte8.match getMSB_b8 b with
[ true ⇒ succ_b8 (not_b8 b)
| false ⇒ not_b8 (pred_b8 b) ].
+(* operatore x in [inf,sup] o in sup],[inf *)
+ndefinition inrange_b8 ≝
+λx,inf,sup:byte8.
+ match le_b8 inf sup with
+ [ true ⇒ and_bool | false ⇒ or_bool ]
+ (le_b8 inf x) (le_b8 x sup).
+
(* operatore moltiplicazione senza segno: e*e=[0x00,0xE1] *)
ndefinition mul_ex ≝
λe1,e2:exadecim.match e1 with
(* X' = [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + [c=1 ? 0x60 : 0x00]
[(b16l X):0xA-0xF] X + 0x06 + [c=1 ? 0x60 : 0x00] *)
[ true ⇒
- let X' ≝ match (lt_ex (b8l X) xA) ⊗ (⊖h) with
+ let X' ≝ match (lt_ex (cnL ? X) xA) ⊗ (⊖h) with
[ true ⇒ X
| false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
let X'' ≝ match c with
[(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + 0x60
[(b16l X):0xA-0xF] X + 0x6 + 0x60 *)
| false ⇒
- let X' ≝ match (lt_ex (b8l X) xA) ⊗ (⊖h) with
+ let X' ≝ match (lt_ex (cnL ? X) xA) ⊗ (⊖h) with
[ true ⇒ X
| false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
let X'' ≝ plus_b8_d_d X' 〈x6,x0〉 in
].
(* divisione senza segno (secondo la logica delle ALU): (quoziente resto) overflow *)
-nlet rec div_b8_ex_aux (divd:byte8) (divs:byte8) (molt:exadecim) (q:exadecim) (qu:quatern) (rqu:rec_quatern qu) on rqu ≝
+nlet rec div_b8_ex_aux (divd:byte8) (divs:byte8) (molt:exadecim) (q:exadecim) (n:nat) on n ≝
let w' ≝ plus_b8_d_d divd (compl_b8 divs) in
- match rqu with
- [ qu_O ⇒ match le_b8 divs divd with
- [ true ⇒ triple … (or_ex molt q) (b8l w') (⊖ (eq_ex (b8h w') x0))
- | false ⇒ triple … q (b8l divd) (⊖ (eq_ex (b8h divd) x0)) ]
- | qu_S qu' rqu' ⇒ match le_b8 divs divd with
- [ true ⇒ div_b8_ex_aux w' (ror_b8 divs) (ror_ex molt) (or_ex molt q) qu' rqu'
- | false ⇒ div_b8_ex_aux divd (ror_b8 divs) (ror_ex molt) q qu' rqu' ]].
+ match n with
+ [ O ⇒ match le_b8 divs divd with
+ [ true ⇒ triple … (or_ex molt q) (cnL ? w') (⊖ (eq_ex (cnH ? w') x0))
+ | false ⇒ triple … q (cnL ? divd) (⊖ (eq_ex (cnH ? divd) x0)) ]
+ | S n' ⇒ match le_b8 divs divd with
+ [ true ⇒ div_b8_ex_aux w' (ror_b8 divs) (ror_ex molt) (or_ex molt q) n'
+ | false ⇒ div_b8_ex_aux divd (ror_b8 divs) (ror_ex molt) q n' ]].
ndefinition div_b8_ex ≝
λb:byte8.λe:exadecim.match eq_ex e x0 with
-(*
- la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato
-*)
- [ true ⇒ triple … xF (b8l b) true
+(* la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato *)
+ [ true ⇒ triple … xF (cnL ? b) true
| false ⇒ match eq_b8 b 〈x0,x0〉 with
(* 0 diviso qualsiasi cosa diverso da 0 da' q=0 r=0 o=false *)
[ true ⇒ triple … x0 x0 false
(* 3) ad ogni ciclo il divisore e il moltiplicatore vengono scalati di 1 a dx *)
(* 4) il moltiplicatore e' la quantita' aggiunta al quoziente se il divisore *)
(* puo' essere sottratto al dividendo *)
- | false ⇒ div_b8_ex_aux b (rol_b8_n 〈x0,e〉 ? (oct_to_recoct o3)) x8 x0 ? (qu_to_recqu q3) ]].
-
-(* operatore x in [inf,sup] o in sup],[inf *)
-ndefinition inrange_b8 ≝
-λx,inf,sup:byte8.
- match le_b8 inf sup with
- [ true ⇒ and_bool | false ⇒ or_bool ]
- (le_b8 inf x) (le_b8 x sup).
-
-(* iteratore sui byte *)
-ndefinition forall_b8 ≝
- λP.
- forall_ex (λbh.
- forall_ex (λbl.
- P (mk_byte8 bh bl))).
+ | false ⇒ div_b8_ex_aux b (nat_it ? rol_b8 〈x0,e〉 nat3) x8 x0 nat3 ]].
(* byte ricorsivi *)
ninductive rec_byte8 : byte8 → Type ≝
*)
ndefinition b8_to_recb8 : Πb.rec_byte8 b ≝
-λb.match b with [ mk_byte8 h l ⇒ b8_to_recb8_aux3 h l (b8_to_recb8_aux2 h (ex_to_recex h)) ].
+λb.match b with
+ [ mk_comp_num h l ⇒ b8_to_recb8_aux3 h l (b8_to_recb8_aux2 h (ex_to_recex h)) ].
(* ottali → esadecimali *)
ndefinition b8_of_bit ≝