1 #----------------------------------------
2 # Purpose - Create some $BIT variables
3 # Create $K and $M variables
4 # and some bit field extraction variables.
5 # Create helper variables ...
8 for { set x
0 } { $x < 32 } { set x
[expr {$x + 1}]} {
9 set vn
[format "BIT%d" $x]
11 set $vn [expr {1 << $x}]
14 # Create K bytes values
16 for { set x
1 } { $x < 2048 } { set x
[expr {$x * 2}]} {
17 set vn
[format "__%dK" $x]
19 set $vn [expr {1024 * $x}]
22 # Create M bytes values
24 for { set x
1 } { $x < 2048 } { set x
[expr {$x * 2}]} {
25 set vn
[format "__%dM" $x]
27 set $vn [expr {1024 * 1024 * $x}]
30 proc create_mask
{ MSB LSB
} {
31 return [expr {((1 << ($MSB - $LSB + 1))-1) << $LSB}]
34 # Cut Bits $MSB to $LSB out of this value.
35 # Example: % format "0x%08x" [extract_bitfield 0x12345678 27 16]
38 proc extract_bitfield
{ VALUE MSB LSB
} {
39 return [expr {[create_mask
$MSB $LSB] & $VALUE}]
43 # Cut bits $MSB to $LSB out of this value
44 # and shift (normalize) them down to bit 0.
46 # Example: % format "0x%08x" [normalize_bitfield 0x12345678 27 16]
49 proc normalize_bitfield
{ VALUE MSB LSB
} {
50 return [expr {[extract_bitfield
$VALUE $MSB $LSB ] >> $LSB}]
53 proc show_normalize_bitfield
{ VALUE MSB LSB
} {
54 set m
[create_mask
$MSB $LSB]
55 set mr
[expr {$VALUE & $m}]
56 set sr
[expr {$mr >> $LSB}]
57 echo
[format "((0x%08x & 0x%08x) -> 0x%08x) >> %2d => (0x%x) %5d " $VALUE $m $mr $LSB $sr $sr]