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/compiler.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 kernel
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/** \brief Create a mask with a bit set
23
24 \param bit The bit to set (from 0 to 31)
25 \return A 32-bit mask with the corresponding bit set
26 */
27#define BIT(bit) (1u << (bit))
28
29/** \brief Create a mask with a range of bits set
30
31 \param h The high bit of the range to set, included
32 \param l The low bit of the range to set, included
33 \return A 32-bit mask with the corresponding bits set
34 */
35#define GENMASK(h, l) ((0xffffffff << (l)) & (0xffffffff >> (31 - (h))))
36
37/** \brief Extract a field value from a variable
38
39 \param var The 32-bit variable containing the field
40 \param field A 32-bit mask that corresponds to the field
41 \return The value of the field (shifted)
42 */
43#define FIELD_GET(var, field) \
44 (((var) & (field)) >> __builtin_ctz(field))
45
46/** \brief Prepare a field with a given value
47
48 \param field A 32-bit mask that corresponds to the field
49 \param value The value to be put in the field
50 */
51#define FIELD_PREP(field, value) \
52 (((value) << __builtin_ctz(field)) & (field))
53
54__END_DECLS
55#endif /* __KOS_REGFIELD_H */