wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 2 | * FILE bitfield.h |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 3 | * |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 4 | * Version 1.1 |
| 5 | * Author Copyright (c) Marc A. Viredaz, 1998 |
| 6 | * DEC Western Research Laboratory, Palo Alto, CA |
| 7 | * Date April 1998 (April 1997) |
| 8 | * System Advanced RISC Machine (ARM) |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 9 | * Language C or ARM Assembly |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 10 | * Purpose Definition of macros to operate on bit fields. |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 14 | #ifndef __BITFIELD_H |
| 15 | #define __BITFIELD_H |
| 16 | |
| 17 | #ifndef __ASSEMBLY__ |
| 18 | #define UData(Data) ((unsigned long) (Data)) |
| 19 | #else |
| 20 | #define UData(Data) (Data) |
| 21 | #endif |
| 22 | |
| 23 | |
| 24 | /* |
| 25 | * MACRO: Fld |
| 26 | * |
| 27 | * Purpose |
| 28 | * The macro "Fld" encodes a bit field, given its size and its shift value |
| 29 | * with respect to bit 0. |
| 30 | * |
| 31 | * Note |
| 32 | * A more intuitive way to encode bit fields would have been to use their |
| 33 | * mask. However, extracting size and shift value information from a bit |
| 34 | * field's mask is cumbersome and might break the assembler (255-character |
| 35 | * line-size limit). |
| 36 | * |
| 37 | * Input |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 38 | * Size Size of the bit field, in number of bits. |
| 39 | * Shft Shift value of the bit field with respect to bit 0. |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 40 | * |
| 41 | * Output |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 42 | * Fld Encoded bit field. |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | #define Fld(Size, Shft) (((Size) << 16) + (Shft)) |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | * MACROS: FSize, FShft, FMsk, FAlnMsk, F1stBit |
| 50 | * |
| 51 | * Purpose |
| 52 | * The macros "FSize", "FShft", "FMsk", "FAlnMsk", and "F1stBit" return |
| 53 | * the size, shift value, mask, aligned mask, and first bit of a |
| 54 | * bit field. |
| 55 | * |
| 56 | * Input |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 57 | * Field Encoded bit field (using the macro "Fld"). |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 58 | * |
| 59 | * Output |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 60 | * FSize Size of the bit field, in number of bits. |
| 61 | * FShft Shift value of the bit field with respect to bit 0. |
| 62 | * FMsk Mask for the bit field. |
| 63 | * FAlnMsk Mask for the bit field, aligned on bit 0. |
| 64 | * F1stBit First bit of the bit field. |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 65 | */ |
| 66 | |
| 67 | #define FSize(Field) ((Field) >> 16) |
| 68 | #define FShft(Field) ((Field) & 0x0000FFFF) |
| 69 | #define FMsk(Field) (((UData (1) << FSize (Field)) - 1) << FShft (Field)) |
| 70 | #define FAlnMsk(Field) ((UData (1) << FSize (Field)) - 1) |
| 71 | #define F1stBit(Field) (UData (1) << FShft (Field)) |
| 72 | |
| 73 | |
| 74 | /* |
| 75 | * MACRO: FInsrt |
| 76 | * |
| 77 | * Purpose |
| 78 | * The macro "FInsrt" inserts a value into a bit field by shifting the |
| 79 | * former appropriately. |
| 80 | * |
| 81 | * Input |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 82 | * Value Bit-field value. |
| 83 | * Field Encoded bit field (using the macro "Fld"). |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 84 | * |
| 85 | * Output |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 86 | * FInsrt Bit-field value positioned appropriately. |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 87 | */ |
| 88 | |
| 89 | #define FInsrt(Value, Field) \ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 90 | (UData (Value) << FShft (Field)) |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | /* |
| 94 | * MACRO: FExtr |
| 95 | * |
| 96 | * Purpose |
| 97 | * The macro "FExtr" extracts the value of a bit field by masking and |
| 98 | * shifting it appropriately. |
| 99 | * |
| 100 | * Input |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 101 | * Data Data containing the bit-field to be extracted. |
| 102 | * Field Encoded bit field (using the macro "Fld"). |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 103 | * |
| 104 | * Output |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 105 | * FExtr Bit-field value. |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 106 | */ |
| 107 | |
| 108 | #define FExtr(Data, Field) \ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 109 | ((UData (Data) >> FShft (Field)) & FAlnMsk (Field)) |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | #endif /* __BITFIELD_H */ |