2 * Register Definition API: field macros
4 * Copyright (c) 2016 Xilinx Inc.
5 * Copyright (c) 2013 Peter Crosthwaite <peter.crosthwaite@xilinx.com>
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
11 #ifndef REGISTERFIELDS_H
12 #define REGISTERFIELDS_H
14 #include "qemu/bitops.h"
16 /* Define constants for a 32 bit register */
18 /* This macro will define A_FOO, for the byte address of a register
19 * as well as R_FOO for the uint32_t[] register number (A_FOO / 4).
21 #define REG32(reg, addr) \
22 enum { A_ ## reg = (addr) }; \
23 enum { R_ ## reg = (addr) / 4 };
25 /* Define SHIFT, LENGTH and MASK constants for a field within a register */
27 /* This macro will define R_FOO_BAR_MASK, R_FOO_BAR_SHIFT and R_FOO_BAR_LENGTH
28 * constants for field BAR in register FOO.
30 #define FIELD(reg, field, shift, length) \
31 enum { R_ ## reg ## _ ## field ## _SHIFT = (shift)}; \
32 enum { R_ ## reg ## _ ## field ## _LENGTH = (length)}; \
33 enum { R_ ## reg ## _ ## field ## _MASK = \
34 MAKE_64BIT_MASK(shift, length)};
36 /* Extract a field from a register */
37 #define FIELD_EX32(storage, reg, field) \
38 extract32((storage), R_ ## reg ## _ ## field ## _SHIFT, \
39 R_ ## reg ## _ ## field ## _LENGTH)
40 #define FIELD_EX64(storage, reg, field) \
41 extract64((storage), R_ ## reg ## _ ## field ## _SHIFT, \
42 R_ ## reg ## _ ## field ## _LENGTH)
44 /* Extract a field from an array of registers */
45 #define ARRAY_FIELD_EX32(regs, reg, field) \
46 FIELD_EX32((regs)[R_ ## reg], reg, field)
48 /* Deposit a register field.
49 * Assigning values larger then the target field will result in
50 * compilation warnings.
52 #define FIELD_DP32(storage, reg, field, val) ({ \
54 unsigned int v:R_ ## reg ## _ ## field ## _LENGTH; \
57 d = deposit32((storage), R_ ## reg ## _ ## field ## _SHIFT, \
58 R_ ## reg ## _ ## field ## _LENGTH, v.v); \
60 #define FIELD_DP64(storage, reg, field, val) ({ \
62 unsigned int v:R_ ## reg ## _ ## field ## _LENGTH; \
65 d = deposit64((storage), R_ ## reg ## _ ## field ## _SHIFT, \
66 R_ ## reg ## _ ## field ## _LENGTH, v.v); \
69 /* Deposit a field to array of registers. */
70 #define ARRAY_FIELD_DP32(regs, reg, field, val) \
71 (regs)[R_ ## reg] = FIELD_DP32((regs)[R_ ## reg], reg, field, val);