Esadecimale e flags

di Gianluca Sordiglioni <inzet at gufi dot org>

Creato il 10 Settembre 2000
Modificato il 10 Settembre 2000
 

Questo documento illustra il procedimento per il conteggio in esadecimale e la sua applicazione nel calcolo dei flags di parametri per i devices di FreeBSD.

Un nuovo modo di contare

Un sistema di numerazione si basa su simboli e ad essi attribuisce un "valore" crescente. Nel sistema decimale i simboli sono dieci, nel sistema binario solo due, nel sistema esadecimale sedici. Il numero dei simboli è arbitrario, per esempio gli Assiro-Babilonesi ne usavano sessanta. Il numero di simboli viene detto "base", così i numeri esadecimali sono a base sedici.
Per spiegare il meccanismo di conteggio consideriamo, per semplicità, il sistema decimale (base 10). Ogni simbolo (o "cifra") viene posto in sequenza secondo un preciso ordine. Quando i simboli (cifre) sono esauriti, si scala di una posizione a sinistra e si ripete il ciclo. Si viene a formare un ordine di posizione, nel quale le cifre a destra hanno meno valore di quelle a sinistra (in gergo, meno significative).

      0
  1
  2
[...]
  9       i simboli (cifre 0-9) sono finiti, devo scalare di una posizione a sinistra
 10
 11
 12
[...]
 19
 20
 21
[...]

... e così via.

In esadecimale i simboli sono sedici, quindi le cifre non bastano: si ricorre alle lettere dell'alfabeto per i simboli mancanti.

   0
   1
   2
   3
   4
   5
   6
   7
   8
   9
   A
   B
   C
   D
   E

Il conteggio viene eseguito seguendo la sequenza di simboli:

   0
   1
 [...]
   9
   A
   B
   C
   D
   E
  10
  12
 [...]
  19
  1A
  1B
  1C
  1D
  1E
  20
  21
  22

... e così via.

Da notare che mentre nel sistema decimale la sequenza di simboli '10' viene letta 'dieci', nel sistema esadecimale viene letta scandendo i simboli, ovvero 'uno-zero'. Così '58' si legge 'cinque-otto'  e '12D3' si legge 'uno-due-di-tre'.

Le operazioni matematiche +,-,*,/ seguono le stesse regole del sistema decimale, salvo il differente numero di simboli.
 
 

Binario ed esadecimale


Nel sistema binario i simboli sono 0,1 e ci vogliono quattro simboli perchè le combinazioni diventino sedici. La tabellina di equivalenza tra binario ed esadecimale è di seguito riportata

    bit
3  2  1  0  |  hex
------------+------
0  0  0  0  |  0
0  0  0  1  |  1
0  0  1  0  |  2
0  0  1  1  |  3
0  1  0  0  |  4
0  1  0  1  |  5
0  1  1  0  |  6
0  1  1  1  |  7
1  0  0  0  |  8
1  0  0  1  |  9
1  0  1  0  |  A
1  0  1  1  |  B
1  1  0  0  |  C
1  1  0  1  |  D
1  1  1  0  |  E
1  1  1  1  |  F

Basandosi su questa tabellina è facile convertire un byte (8 bit) in esadecimale. Si prendono gruppi di quattro bit (nibble) e si usa la tabellina per trovare la cifra esadecimale corrispondente. Il byte 00000000 corrisponde in esadecimale a 00, 10011110 a 9E, e così via.
Dato il più elevato numero di simboli una cifra esadecimale risulta di scrittura più compatta rispetto al binario ed anche al decimale. La compattezza di scrittura e la facilità di conversione in binario sono caratteristiche molto apprezzate in informatica e il largo uso dell'esadecimale lo testimonia.
 
 

Configurazione con i flags


I flags sono paragonabili a degli interruttori che, in base alla loro posizione di spento o acceso, determinano un comportamento diverso nel sistema. I flags frequentemente sono usati per configurare i driver al bootstrap, in modo che i dispositivi si trovino nello stato voluto. Per attivare gli "interruttori" si usano dei bit che vengono posti ad 1 o 0. Poichè l'unità minima di informazione è il byte (8 bit) il ricorso alla numerazione esadecimale risulta comodo.
Per il calcolo da binario ad esadecimale bisogna tener presente che FreeBSD è un sistema operativo concepito per processori Intel e, se gira su questo tipo di processori, il verso di priorità del byte è da destra verso sinistra (big endian); ovvero, il bit più a destra è meno significativo dei bit più a sinistra.
Consideriamo come esempio questo byte

  7   6   5   4   3   2   1   0
+---+---+---+---+---+---+---+---+
| 1 | 1 | 1 | 0 | 0 | 1 | 1 | 0 |
+---+---+---+---+---+---+---+---+

il numero esadecimale rappresentato è E6.

Come esempio di configurazione consideriamo il caso del driver del mouse, psm. Un estratto del manuale (man psm) cita:
 

   Driver Flags
     The psm driver accepts the following driver flags.  Set them in the ker-
     nel configuration file or in the User Configuration Menu at the boot time
     (see boot(8)).
     bit 0..3 RESOLUTION
            This flag specifies the resolution of the pointing device.  It
            must be zero through four.  The greater the value is, the finer
            resolution the device will select.  Actual resolution selected by
            this field varies according to the model of the device.  Typical
            resolutions are:
            1 (low)            25 pulse per inch (ppi)
            2 (medium low)     50 ppi
            3 (medium high)    100 ppi
            4 (high)           200 ppi
            Leaving this flag zero will selects the default resolution for the
            device (whatever it is).
     bit 4..7 ACCELERATION
            This flag controls the amount of acceleration effect.  The smaller
            the value of this flag is, more sensitive the movement becomes.
            The minimum value allowed, thus the value for the most sensitive
            setting, is one.  Setting this flag to zero will completely dis-
            ables the acceleration effect.
     bit 8 NOCHECKSYNC
            The psm driver tries to detect the first byte of the data packet
            bit 9 NOIDPROBE
            The psm driver will not try to identify the model of the pointing
            device and will not carry out model-specific initialization.  The
            device should always act like a standard PS/2 mouse without such
            initialization.  Extra features, such as wheels and additional
            buttons, won't be recognized by the psm driver.
      bit 8 NOCHECKSYNC
            The psm driver tries to detect the first byte of the data packet
            by checking the bit pattern of that byte.  Although this method
            should work with most PS/2 pointing devices, it may interfere with
            some devices which are not so compatible with known devices.  If
            you think your pointing device is not functioning as expected, and
            the kernel frequently prints the following message to the console,
                  psmintr: out of sync (xxxx != yyyy).
            set this flag to disable synchronization check and see if it
            helps.
    bit 9 NOIDPROBE
            The psm driver will not try to identify the model of the pointing
            device and will not carry out model-specific initialization.  The
            device should always act like a standard PS/2 mouse without such
            initialization.  Extra features, such as wheels and additional
            buttons, won't be recognized by the psm driver.
     bit 10 NORESET
            When this flag is set, the psm driver won't reset the pointing de-
            vice when initializing the device.  If the FreeBSD kernel is
            started after another OS has run, the pointing device will inherit
            settings from the previous OS.  However, because there is no way
            for the psm driver to know the settings, the device and the driver
            may not work correctly.  The flag should never be necessary under
            normal circumstances.
     bit 11 FORCETAP
            Some pad devices report as if the fourth button is pressed when
            the user `taps' the surface of the device (see CAVEATS). This flag
            will make the psm driver assume that the device behaves this way.
            Without the flag, the driver will assume this behavior for ALPS
            GlidePoint models only.

 

 

Supponiamo di voler configurare il driver per configurarlo alla massima risoluzione (bit 0..3), accellerazione 2 (bit 4..7), nessun check del sincronismo (bit 8) e lasciare gli altri parametri a zero.

Nella file di configurazione GENERIC che si trova in /usr/src/sys/i386/conf non vi sono flags di configurazione che ci possano dare una traccia.

device          psm0    at atkbdc? irq 12

Dobbiamo usare due byte (8+8 = 16 bit) per contenere tutti gli 11 bit specificati . I bit superflui verranno ignorati dal driver.

Questo è lo schema risultate:

               Byte 1                             Byte 0
  7   6   5   4   3   2   1   0      7   6   5   4   3   2   1   0
+---+---+---+---+---+---+---+---+   +---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |   | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
+---+---+---+---+---+---+---+---+   +---+---+---+---+---+---+---+---+
                              |     |               |               |
                              |     +---------------+---------------+
                              |         ACCELERATION    RESOLUTION
                          NOIDPROBE

00000001 00100100 in esadecimale è 01 24.
Aggiungiamo alla riga del driver questo valore

device          psm0    at atkbdc? irq 12 flags 0x124

Ecco fatto.

Notiamo che può venire omesso lo zero più a sinistra in quanto non significativo. E' importante che sia preposto 0x al valore, per indicare che si tratta di un numero a base 16.