flash/nor/rp2040: fix size of flash write buffer
[openocd.git] / tcl / bitsbytes.tcl
blob03d758e7c8d199b1665f90dd05e49061c5c7a172
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 #----------------------------------------
4 # Purpose - Create some $BIT variables
5 # Create $K and $M variables
6 # and some bit field extraction variables.
7 # Create helper variables ...
8 # BIT0.. BIT31
10 for { set x 0 } { $x < 32 } { set x [expr {$x + 1}]} {
11 set vn [format "BIT%d" $x]
12 global $vn
13 set $vn [expr {1 << $x}]
16 # Create K bytes values
17 # __1K ... to __2048K
18 for { set x 1 } { $x < 2048 } { set x [expr {$x * 2}]} {
19 set vn [format "__%dK" $x]
20 global $vn
21 set $vn [expr {1024 * $x}]
24 # Create M bytes values
25 # __1M ... to __2048K
26 for { set x 1 } { $x < 2048 } { set x [expr {$x * 2}]} {
27 set vn [format "__%dM" $x]
28 global $vn
29 set $vn [expr {1024 * 1024 * $x}]
32 proc create_mask { MSB LSB } {
33 return [expr {((1 << ($MSB - $LSB + 1))-1) << $LSB}]
36 # Cut Bits $MSB to $LSB out of this value.
37 # Example: % format "0x%08x" [extract_bitfield 0x12345678 27 16]
38 # Result: 0x02340000
40 proc extract_bitfield { VALUE MSB LSB } {
41 return [expr {[create_mask $MSB $LSB] & $VALUE}]
45 # Cut bits $MSB to $LSB out of this value
46 # and shift (normalize) them down to bit 0.
48 # Example: % format "0x%08x" [normalize_bitfield 0x12345678 27 16]
49 # Result: 0x00000234
51 proc normalize_bitfield { VALUE MSB LSB } {
52 return [expr {[extract_bitfield $VALUE $MSB $LSB ] >> $LSB}]
55 proc show_normalize_bitfield { VALUE MSB LSB } {
56 set m [create_mask $MSB $LSB]
57 set mr [expr {$VALUE & $m}]
58 set sr [expr {$mr >> $LSB}]
59 echo [format "((0x%08x & 0x%08x) -> 0x%08x) >> %2d => (0x%x) %5d " $VALUE $m $mr $LSB $sr $sr]
60 return $sr