(* Progetto FreeScale *)
(* *)
(* Sviluppato da: Ing. Cosimo Oliboni, oliboni@cs.unibo.it *)
-(* Sviluppo: 2008-2010 *)
+(* Ultima modifica: 05/08/2009 *)
(* *)
(* ********************************************************************** *)
(* WORD *)
(* **** *)
-ndefinition word16 ≝ comp_num byte8.
-ndefinition mk_word16 ≝ λb1,b2.mk_comp_num byte8 b1 b2.
-ndefinition ext_word16 ≝ λb2.mk_comp_num byte8 〈x0,x0〉 b2.
-ndefinition ext2_word16 ≝ λe2.mk_comp_num byte8 〈x0,x0〉 〈x0,e2〉.
+nrecord word16 : Type ≝
+ {
+ w16h: byte8;
+ w16l: byte8
+ }.
(* \langle \rangle *)
notation "〈x:y〉" non associative with precedence 80
- for @{ mk_comp_num byte8 $x $y }.
-
-(* iteratore sulle word *)
-ndefinition forall_w16 ≝ forall_cn ? forall_b8.
+ for @{ 'mk_word16 $x $y }.
+interpretation "mk_word16" 'mk_word16 x y = (mk_word16 x y).
(* operatore = *)
-ndefinition eq_w16 ≝ eq2_cn ? eq_b8.
+ndefinition eq_w16 ≝ λw1,w2.(eq_b8 (w16h w1) (w16h w2)) ⊗ (eq_b8 (w16l w1) (w16l w2)).
(* operatore < *)
-ndefinition lt_w16 ≝ ltgt_cn ? eq_b8 lt_b8.
+ndefinition lt_w16 ≝
+λw1,w2:word16.
+ (lt_b8 (w16h w1) (w16h w2)) ⊕
+ ((eq_b8 (w16h w1) (w16h w2)) ⊗ (lt_b8 (w16l w1) (w16l w2))).
(* operatore ≤ *)
-ndefinition le_w16 ≝ lege_cn ? eq_b8 lt_b8 le_b8.
+ndefinition le_w16 ≝
+λw1,w2:word16.
+ (lt_b8 (w16h w1) (w16h w2)) ⊕
+ ((eq_b8 (w16h w1) (w16h w2)) ⊗ (le_b8 (w16l w1) (w16l w2))).
(* operatore > *)
-ndefinition gt_w16 ≝ ltgt_cn ? eq_b8 gt_b8.
+ndefinition gt_w16 ≝
+λw1,w2:word16.
+ (gt_b8 (w16h w1) (w16h w2)) ⊕
+ ((eq_b8 (w16h w1) (w16h w2)) ⊗ (gt_b8 (w16l w1) (w16l w2))).
(* operatore ≥ *)
-ndefinition ge_w16 ≝ lege_cn ? eq_b8 gt_b8 ge_b8.
+ndefinition ge_w16 ≝
+λw1,w2:word16.
+ (gt_b8 (w16h w1) (w16h w2)) ⊕
+ ((eq_b8 (w16h w1) (w16h w2)) ⊗ (ge_b8 (w16l w1) (w16l w2))).
(* operatore and *)
-ndefinition and_w16 ≝ fop2_cn ? and_b8.
+ndefinition and_w16 ≝
+λw1,w2:word16.mk_word16 (and_b8 (w16h w1) (w16h w2)) (and_b8 (w16l w1) (w16l w2)).
(* operatore or *)
-ndefinition or_w16 ≝ fop2_cn ? or_b8.
+ndefinition or_w16 ≝
+λw1,w2:word16.mk_word16 (or_b8 (w16h w1) (w16h w2)) (or_b8 (w16l w1) (w16l w2)).
(* operatore xor *)
-ndefinition xor_w16 ≝ fop2_cn ? xor_b8.
-
-(* operatore Most Significant Bit *)
-ndefinition getMSB_w16 ≝ getOPH_cn ? getMSB_b8.
-ndefinition setMSB_w16 ≝ setOPH_cn ? setMSB_b8.
-ndefinition clrMSB_w16 ≝ setOPH_cn ? clrMSB_b8.
-
-(* operatore Least Significant Bit *)
-ndefinition getLSB_w16 ≝ getOPL_cn ? getLSB_b8.
-ndefinition setLSB_w16 ≝ setOPL_cn ? setLSB_b8.
-ndefinition clrLSB_w16 ≝ setOPL_cn ? clrLSB_b8.
-
-(* operatore estensione unsigned *)
-ndefinition extu_w16 ≝ λb2.〈〈x0,x0〉:b2〉.
-ndefinition extu2_w16 ≝ λe2.〈〈x0,x0〉:〈x0,e2〉〉.
-
-(* operatore estensione signed *)
-ndefinition exts_w16 ≝
-λb2.〈(match getMSB_b8 b2 with
- [ true ⇒ 〈xF,xF〉 | false ⇒ 〈x0,x0〉 ]):b2〉.
-ndefinition exts2_w16 ≝
-λe2.(match getMSB_ex e2 with
- [ true ⇒ 〈〈xF,xF〉:〈xF,e2〉〉 | false ⇒ 〈〈x0,x0〉:〈x0,e2〉〉 ]).
+ndefinition xor_w16 ≝
+λw1,w2:word16.mk_word16 (xor_b8 (w16h w1) (w16h w2)) (xor_b8 (w16l w1) (w16l w2)).
(* operatore rotazione destra con carry *)
-ndefinition rcr_w16 ≝ opcr_cn ? rcr_b8.
+ndefinition rcr_w16 ≝
+λw:word16.λc:bool.match rcr_b8 (w16h w) c with
+ [ pair wh' c' ⇒ match rcr_b8 (w16l w) c' with
+ [ pair wl' c'' ⇒ pair … (mk_word16 wh' wl') c'' ]].
(* operatore shift destro *)
-ndefinition shr_w16 ≝ opcr_cn ? rcr_b8 false.
+ndefinition shr_w16 ≝
+λw:word16.match rcr_b8 (w16h w) false with
+ [ pair wh' c' ⇒ match rcr_b8 (w16l w) c' with
+ [ pair wl' c'' ⇒ pair … (mk_word16 wh' wl') c'' ]].
(* operatore rotazione destra *)
ndefinition ror_w16 ≝
-λw.match shr_w16 w with
- [ pair c w' ⇒ match c with
- [ true ⇒ setMSB_w16 w' | false ⇒ w' ]].
+λw:word16.match rcr_b8 (w16h w) false with
+ [ pair wh' c' ⇒ match rcr_b8 (w16l w) c' with
+ [ pair wl' c'' ⇒ match c'' with
+ [ true ⇒ mk_word16 (or_b8 (mk_byte8 x8 x0) wh') wl'
+ | false ⇒ mk_word16 wh' wl' ]]].
+
+(* operatore rotazione destra n-volte *)
+nlet rec ror_w16_n (w:word16) (n:nat) on n ≝
+ match n with
+ [ O ⇒ w
+ | S n' ⇒ ror_w16_n (ror_w16 w) n' ].
(* operatore rotazione sinistra con carry *)
-ndefinition rcl_w16 ≝ opcl_cn ? rcl_b8.
+ndefinition rcl_w16 ≝
+λw:word16.λc:bool.match rcl_b8 (w16l w) c with
+ [ pair wl' c' ⇒ match rcl_b8 (w16h w) c' with
+ [ pair wh' c'' ⇒ pair … (mk_word16 wh' wl') c'' ]].
(* operatore shift sinistro *)
-ndefinition shl_w16 ≝ opcl_cn ? rcl_b8 false.
+ndefinition shl_w16 ≝
+λw:word16.match rcl_b8 (w16l w) false with
+ [ pair wl' c' ⇒ match rcl_b8 (w16h w) c' with
+ [ pair wh' c'' ⇒ pair … (mk_word16 wh' wl') c'' ]].
(* operatore rotazione sinistra *)
ndefinition rol_w16 ≝
-λw.match shl_w16 w with
- [ pair c w' ⇒ match c with
- [ true ⇒ setLSB_w16 w' | false ⇒ w' ]].
+λw:word16.match rcl_b8 (w16l w) false with
+ [ pair wl' c' ⇒ match rcl_b8 (w16h w) c' with
+ [ pair wh' c'' ⇒ match c'' with
+ [ true ⇒ mk_word16 wh' (or_b8 (mk_byte8 x0 x1) wl')
+ | false ⇒ mk_word16 wh' wl' ]]].
+
+(* operatore rotazione sinistra n-volte *)
+nlet rec rol_w16_n (w:word16) (n:nat) on n ≝
+ match n with
+ [ O ⇒ w
+ | S n' ⇒ rol_w16_n (rol_w16 w) n' ].
(* operatore not/complemento a 1 *)
-ndefinition not_w16 ≝ fop_cn ? not_b8.
+ndefinition not_w16 ≝
+λw:word16.mk_word16 (not_b8 (w16h w)) (not_b8 (w16l w)).
(* operatore somma con data+carry → data+carry *)
-ndefinition plus_w16_dc_dc ≝ opcl2_cn ? plus_b8_dc_dc.
+ndefinition plus_w16_dc_dc ≝
+λw1,w2:word16.λc:bool.
+ match plus_b8_dc_dc (w16l w1) (w16l w2) c with
+ [ pair l c ⇒ match plus_b8_dc_dc (w16h w1) (w16h w2) c with
+ [ pair h c' ⇒ pair … 〈h:l〉 c' ]].
(* operatore somma con data+carry → data *)
-ndefinition plus_w16_dc_d ≝ λc,w1,w2.snd … (plus_w16_dc_dc c w1 w2).
+ndefinition plus_w16_dc_d ≝
+λw1,w2:word16.λc:bool.
+ match plus_b8_dc_dc (w16l w1) (w16l w2) c with
+ [ pair l c ⇒ 〈plus_b8_dc_d (w16h w1) (w16h w2) c:l〉 ].
(* operatore somma con data+carry → c *)
-ndefinition plus_w16_dc_c ≝ λc,w1,w2.fst … (plus_w16_dc_dc c w1 w2).
+ndefinition plus_w16_dc_c ≝
+λw1,w2:word16.λc:bool.
+ plus_b8_dc_c (w16h w1) (w16h w2) (plus_b8_dc_c (w16l w1) (w16l w2) c).
(* operatore somma con data → data+carry *)
-ndefinition plus_w16_d_dc ≝ opcl2_cn ? plus_b8_dc_dc false.
+ndefinition plus_w16_d_dc ≝
+λw1,w2:word16.
+ match plus_b8_d_dc (w16l w1) (w16l w2) with
+ [ pair l c ⇒ match plus_b8_dc_dc (w16h w1) (w16h w2) c with
+ [ pair h c' ⇒ pair … 〈h:l〉 c' ]].
(* operatore somma con data → data *)
-ndefinition plus_w16_d_d ≝ λw1,w2.snd … (plus_w16_d_dc w1 w2).
+ndefinition plus_w16_d_d ≝
+λw1,w2:word16.
+ match plus_b8_d_dc (w16l w1) (w16l w2) with
+ [ pair l c ⇒ 〈plus_b8_dc_d (w16h w1) (w16h w2) c:l〉 ].
(* operatore somma con data → c *)
-ndefinition plus_w16_d_c ≝ λw1,w2.fst … (plus_w16_d_dc w1 w2).
+ndefinition plus_w16_d_c ≝
+λw1,w2:word16.
+ plus_b8_dc_c (w16h w1) (w16h w2) (plus_b8_d_c (w16l w1) (w16l w2)).
+
+(* operatore Most Significant Bit *)
+ndefinition MSB_w16 ≝ λw:word16.eq_ex x8 (and_ex x8 (b8h (w16h w))).
(* operatore predecessore *)
-ndefinition pred_w16 ≝ predsucc_cn ? (eq_b8 〈x0,x0〉) pred_b8.
+ndefinition pred_w16 ≝
+λw:word16.match eq_b8 (w16l w) (mk_byte8 x0 x0) with
+ [ true ⇒ mk_word16 (pred_b8 (w16h w)) (pred_b8 (w16l w))
+ | false ⇒ mk_word16 (w16h w) (pred_b8 (w16l w)) ].
(* operatore successore *)
-ndefinition succ_w16 ≝ predsucc_cn ? (eq_b8 〈xF,xF〉) succ_b8.
+ndefinition succ_w16 ≝
+λw:word16.match eq_b8 (w16l w) (mk_byte8 xF xF) with
+ [ true ⇒ mk_word16 (succ_b8 (w16h w)) (succ_b8 (w16l w))
+ | false ⇒ mk_word16 (w16h w) (succ_b8 (w16l w)) ].
(* operatore neg/complemento a 2 *)
ndefinition compl_w16 ≝
-λw:word16.match getMSB_w16 w with
+λw:word16.match MSB_w16 w with
[ true ⇒ succ_w16 (not_w16 w)
| false ⇒ not_w16 (pred_w16 w) ].
-(* operatore abs *)
-ndefinition abs_w16 ≝
-λw:word16.match getMSB_w16 w with
- [ true ⇒ compl_w16 w | false ⇒ w ].
-
-(* operatore x in [inf,sup] o in sup],[inf *)
-ndefinition inrange_w16 ≝
-λx,inf,sup:word16.
- match le_w16 inf sup with
- [ true ⇒ and_bool | false ⇒ or_bool ]
- (le_w16 inf x) (le_w16 x sup).
-
-(* operatore moltiplicazione senza segno *)
-(* 〈a1,a2〉 * 〈b1,b2〉 = (a1*b1) x0 x0 + x0 (a1*b2) x0 + x0 (a2*b1) x0 + x0 x0 (a2*b2) *)
-ndefinition mulu_b8_aux ≝
-λw.nat_it ? rol_w16 w nat4.
-
-ndefinition mulu_b8 ≝
-λb1,b2:byte8.
- plus_w16_d_d 〈(mulu_ex (cnH ? b1) (cnH ? b2)):〈x0,x0〉〉
- (plus_w16_d_d (mulu_b8_aux (extu_w16 (mulu_ex (cnH ? b1) (cnL ? b2))))
- (plus_w16_d_d (mulu_b8_aux (extu_w16 (mulu_ex (cnL ? b1) (cnH ? b2))))
- (extu_w16 (mulu_ex (cnL ? b1) (cnL ? b2))))).
-
-(* operatore moltiplicazione con segno *)
-(* x * y = sgn(x) * sgn(y) * |x| * |y| *)
-ndefinition muls_b8 ≝
-λb1,b2:byte8.
-(* ++/-- → +, +-/-+ → - *)
- match (getMSB_b8 b1) ⊙ (getMSB_b8 b2) with
- (* +- -+ → - *)
- [ true ⇒ compl_w16
- (* ++/-- → + *)
- | false ⇒ λx.x ] (mulu_b8 (abs_b8 b1) (abs_b8 b2)).
+(*
+ operatore moltiplicazione senza segno: b*b=[0x0000,0xFE01]
+ ... in pratica (〈a,b〉*〈c,d〉) = (a*c)<<8+(a*d)<<4+(b*c)<<4+(b*d)
+*)
+ndefinition mul_b8 ≝
+λb1,b2:byte8.match b1 with
+[ mk_byte8 b1h b1l ⇒ match b2 with
+[ mk_byte8 b2h b2l ⇒ match mul_ex b1l b2l with
+[ mk_byte8 t1_h t1_l ⇒ match mul_ex b1h b2l with
+[ mk_byte8 t2_h t2_l ⇒ match mul_ex b2h b1l with
+[ mk_byte8 t3_h t3_l ⇒ match mul_ex b1h b2h with
+[ mk_byte8 t4_h t4_l ⇒
+ plus_w16_d_d
+ (plus_w16_d_d
+ (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〉〉
+]]]]]].
(* divisione senza segno (secondo la logica delle ALU): (quoziente resto) overflow *)
-nlet rec div_b8_aux (divd:word16) (divs:word16) (molt:byte8) (q:byte8) (n:nat) on n ≝
+nlet rec div_b8_aux (divd:word16) (divs:word16) (molt:byte8) (q:byte8) (c:nat) on c ≝
let w' ≝ plus_w16_d_d divd (compl_w16 divs) in
- match n with
+ match c with
[ O ⇒ match le_w16 divs divd with
- [ true ⇒ triple … (or_b8 molt q) (cnL ? w') (⊖ (eq_b8 (cnH ? w') 〈x0,x0〉))
- | false ⇒ triple … q (cnL ? divd) (⊖ (eq_b8 (cnH ? divd) 〈x0,x0〉)) ]
- | S n' ⇒ match le_w16 divs divd with
- [ true ⇒ div_b8_aux w' (ror_w16 divs) (ror_b8 molt) (or_b8 molt q) n'
- | false ⇒ div_b8_aux divd (ror_w16 divs) (ror_b8 molt) q n' ]].
+ [ true ⇒ triple … (or_b8 molt q) (w16l w') (⊖ (eq_b8 (w16h w') 〈x0,x0〉))
+ | false ⇒ triple … q (w16l divd) (⊖ (eq_b8 (w16h divd) 〈x0,x0〉)) ]
+ | S c' ⇒ match le_w16 divs divd with
+ [ true ⇒ div_b8_aux w' (ror_w16 divs) (ror_b8 molt) (or_b8 molt q) c'
+ | false ⇒ div_b8_aux divd (ror_w16 divs) (ror_b8 molt) q c' ]].
ndefinition div_b8 ≝
λw:word16.λb:byte8.match eq_b8 b 〈x0,x0〉 with
-(* la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato *)
- [ true ⇒ triple … 〈xF,xF〉 (cnL ? w) true
+(*
+ la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato
+*)
+ [ true ⇒ triple … 〈xF,xF〉 (w16l w) true
| false ⇒ match eq_w16 w 〈〈x0,x0〉:〈x0,x0〉〉 with
(* 0 diviso qualsiasi cosa diverso da 0 da' q=0 r=0 o=false *)
[ true ⇒ triple … 〈x0,x0〉 〈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_aux w (nat_it ? rol_w16 (extu_w16 b) nat7) 〈x8,x0〉 〈x0,x0〉 nat7 ]].
+ | false ⇒ div_b8_aux w (rol_w16_n 〈〈x0,x0〉:b〉 nat7) 〈x8,x0〉 〈x0,x0〉 nat7 ]].
+
+(* operatore x in [inf,sup] *)
+ndefinition inrange_w16 ≝
+λx,inf,sup:word16.(le_w16 inf x) ⊗ (le_w16 x sup).
+
+(* iteratore sulle word *)
+ndefinition forall_w16 ≝
+ λP.
+ forall_b8 (λbh.
+ forall_b8 (λbl.
+ P (mk_word16 bh bl ))).
(* word16 ricorsive *)
ninductive rec_word16 : word16 → Type ≝
ndefinition w16_to_recw16 : Πw.rec_word16 w ≝
λw.
- match w with [ mk_comp_num h l ⇒
- match l with [ mk_comp_num lh ll ⇒
+ match w with [ mk_word16 h l ⇒
+ match l with [ mk_byte8 lh ll ⇒
w16_to_recw16_aux4 h lh ll (w16_to_recw16_aux3 h lh (w16_to_recw16_aux2 h (b8_to_recb8 h))) ]].