KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
regfield.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 kos/regfield.h
4 Copyright (C) 2024 Paul Cercueil
5
6 Macros to extract / insert bit fields
7*/
8
9/** \file kos/regfield.h
10 \brief Macros to help dealing with register fields.
11 \ingroup register_macros
12
13 \author Paul Cercueil
14*/
15
16#ifndef __KOS_REGFIELD_H
17#define __KOS_REGFIELD_H
18
19#include <sys/cdefs.h>
20__BEGIN_DECLS
21
22/** \defgroup register_macros Register Macros
23 \brief Various macros used throughout the codebase
24 \ingroup system
25
26 @{
27*/
28
29/** \brief Create a mask with a bit set
30
31 \param bit The bit to set (from 0 to 31)
32 \return A 32-bit mask with the corresponding bit set
33 */
34#define BIT(bit) (1u << (bit))
35
36/** \brief Create a mask with a range of bits set
37
38 \param h The high bit of the range to set, included
39 \param l The low bit of the range to set, included
40 \return A 32-bit mask with the corresponding bits set
41 */
42#define GENMASK(h, l) ((0xffffffff << (l)) & (0xffffffff >> (31 - (h))))
43
44/** \brief Extract a field value from a variable
45
46 \param var The 32-bit variable containing the field
47 \param field A 32-bit mask that corresponds to the field
48 \return The value of the field (shifted)
49 */
50#define FIELD_GET(var, field) \
51 (((var) & (field)) >> __builtin_ctz(field))
52
53/** \brief Prepare a field with a given value
54
55 \param field A 32-bit mask that corresponds to the field
56 \param value The value to be put in the field
57 */
58#define FIELD_PREP(field, value) \
59 (((value) << __builtin_ctz(field)) & (field))
60
61/** @} */
62
63__END_DECLS
64#endif /* __KOS_REGFIELD_H */