You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.6 KiB

  1. #----------------------------------------
  2. # Purpose - Create some $BIT variables
  3. # Create $K and $M variables
  4. # and some bit field extraction variables.
  5. # Create helper variables ...
  6. # BIT0.. BIT31
  7. for { set x 0 } { $x < 32 } { set x [expr {$x + 1}]} {
  8. set vn [format "BIT%d" $x]
  9. global $vn
  10. set $vn [expr {1 << $x}]
  11. }
  12. # Create K bytes values
  13. # __1K ... to __2048K
  14. for { set x 1 } { $x < 2048 } { set x [expr {$x * 2}]} {
  15. set vn [format "__%dK" $x]
  16. global $vn
  17. set $vn [expr {1024 * $x}]
  18. }
  19. # Create M bytes values
  20. # __1M ... to __2048K
  21. for { set x 1 } { $x < 2048 } { set x [expr {$x * 2}]} {
  22. set vn [format "__%dM" $x]
  23. global $vn
  24. set $vn [expr {1024 * 1024 * $x}]
  25. }
  26. proc create_mask { MSB LSB } {
  27. return [expr {((1 << ($MSB - $LSB + 1))-1) << $LSB}]
  28. }
  29. # Cut Bits $MSB to $LSB out of this value.
  30. # Example: % format "0x%08x" [extract_bitfield 0x12345678 27 16]
  31. # Result: 0x02340000
  32. proc extract_bitfield { VALUE MSB LSB } {
  33. return [expr {[create_mask $MSB $LSB] & $VALUE}]
  34. }
  35. # Cut bits $MSB to $LSB out of this value
  36. # and shift (normalize) them down to bit 0.
  37. #
  38. # Example: % format "0x%08x" [normalize_bitfield 0x12345678 27 16]
  39. # Result: 0x00000234
  40. #
  41. proc normalize_bitfield { VALUE MSB LSB } {
  42. return [expr {[extract_bitfield $VALUE $MSB $LSB ] >> $LSB}]
  43. }
  44. proc show_normalize_bitfield { VALUE MSB LSB } {
  45. set m [create_mask $MSB $LSB]
  46. set mr [expr {$VALUE & $m}]
  47. set sr [expr {$mr >> $LSB}]
  48. echo [format "((0x%08x & 0x%08x) -> 0x%08x) >> %2d => (0x%x) %5d " $VALUE $m $mr $LSB $sr $sr]
  49. return $sr
  50. }