TEMU  4.4
The Terma Emulator
Register.h
Go to the documentation of this file.
1 //===-- temu-c/Register.h - TEMU Register Introspection ---------*- C++ -*-===//
2 //
3 // TEMU: The Terma Emulator
4 // (c) Terma 2018-2019
5 // Authors: Mattias Holm <maho (at) terma.com>
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef TEMU_REGISTER_H
10 #define TEMU_REGISTER_H
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include "temu-c/Support/Attributes.h"
14 #include "temu-c/Support/Objsys.h"
15 #include "temu-c/Memory/Memory.h"
16 
17 /*
18  * EXPERIMENTAL API
19  * API Changes:
20  * 2019-07-09: Rework introspection API, no more temu_Field objects returned.
21  * Add index argument to set/get/read/write.
22  * 2018-06-11: Remove wr and rd arguments from temu_addField
23  * Replace the separate flag params with a flag bit field.
24  */
25 
26 // Registers are normal properties suitable for device registers (must
27 // be of an integer type), but have addtitional meta-data attached to
28 // them. Primarily you can associate fields with a register, a
29 // register field may even have individual accessors which can be
30 // invoked to set single bits.
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #define TEMU_FIELD_WR (1 << 0) // Writeable field
37 #define TEMU_FIELD_WARM_RESET (1 << 1) // Field is subject to warm reset
38 
39 typedef struct {
40  const char *Name;
41  const char *Desc;
42  uint64_t Mask;
43  uint64_t ResetValue;
44  unsigned Writable : 1;
45  unsigned Resettable : 1;
46  unsigned ClearOnSet : 1;
47 } temu_FieldInfo;
48 
49 #define TEMU_FIELD_INFO_NULL ((temu_FieldInfo){NULL, NULL, 0, 0})
50 
51 typedef struct {
52  const char *Name;
53  const char *Desc;
55 
56  unsigned DeviceOffset;
57  unsigned Stride;
58  uint64_t ColdResetValue;
59 
60  uint64_t WarmResetMask;
61  uint64_t WarmResetValue;
62 
63  uint64_t ReadMask;
64  uint64_t WriteMask;
65 
67  temu_FieldInfo *Field;
68 } temu_RegisterInfo;
69 
70 typedef struct {
71  const char *Name;
72  const char *Desc;
74  const char **RegNames;
75 } temu_RegisterBankInfo;
76 
77 typedef struct {
79  const char **BankNames;
80 } temu_ModelRegInfo;
81 
82 
83 typedef struct temu_RegisterBank temu_RegisterBank;
84 typedef struct temu_Register temu_Register;
85 typedef struct temu_Field temu_Field;
86 
87 /*!
88  * Adds a register bank to a TEMU class
89  * \param C Pointer to the TEMU class
90  * \param Name Name of the register
91  * \param MemAccessIface Pointer to the register memory access interface
92  * \result Pointer to the new register bank
93  */
94 TEMU_API temu_RegisterBank *temu_addRegisterBank(temu_Class *C, const char *Name,
95  temu_MemAccessIface *MemAccessIface);
96 
97 
98 /*!
99  * Retrieves the register bank name from its
100  * object \param Bank Pointer to the register bank name \result Bank name as a
101  * C-string
102  */
104 
105 /*!
106  * Add register property to class
107  *
108  * Adds a register with the given name to a class. It returns a reference
109  * to a meta register which can be used to add fields.
110  *
111  * \param Bank Register bank to add register to
112  * \param Name Name of register (must be a valid C-identifier)
113  * \param Offset Offset to storage element in the device struct.
114  * \param Typ Type of register, note that registers are limited to
115  * unsigned integer types with fixed width.
116  * \param Count Number of registers (normally 1)
117  * \param Wr Register write function
118  * \param Rd Register read function
119  * \param Doc Documentation string for register.
120  *
121  * \param DeviceOffset Offset of register in memory system. This is
122  * the same offset that is used in the memory transaction
123  * interface.
124  *
125  * \param Stride In case the register is an array of registers, then
126  * the stride is used for the offset in physical address space
127  * between each register.
128  * \result A reference to a meta register which can be used to add fields.
129  */
130 TEMU_API temu_Register *
131 temu_addRegister(temu_RegisterBank *Bank, const char *Name, int Offset,
132  temu_Type Typ, int Count, temu_PropWriter Wr,
133  temu_PropReader Rd, const char *Doc,
134  // Offset of register from device mapping (or bank mapping)
135  // in memory space
136  uint32_t DeviceOffset, uint32_t Stride);
137 
138 TEMU_API temu_Register *
139 temu_addRegisterWithFields(temu_RegisterBank *Bank, const char *Name, int Offset,
140  temu_Type Typ, int Count, temu_PropWriter Wr,
141  temu_PropReader Rd, const char *Doc,
142  // Offset of register from device mapping (or
143  // bank mapping) in memory space
144  uint32_t DeviceOffset, uint32_t Stride,
145  temu_FieldInfo Fields[]);
146 
147 /*!
148  * Add field to meta register
149  *
150  * Adds a field to the meta register.
151  *
152  * \param R Meta register pointer.
153  * \param Name Name of of field (must be a C-compatible identifier)
154  * \param Mask Mask identifying the bits in the register corresponding
155  * to the field. Mask must contain consecutive bits only.
156  * \param Reset Reset value.
157  * \param Flags Flags used: TEMU_FIELD_WR means field is writable,
158  * TEMU_FIELD_WARM_RESET means the field is subject to reset actions
159  * also on warm resets.
160  * \param Doc Documentation string
161  */
162 
165  const char *Doc);
166 
167 
168 /*!
169  * Get a register bank from a class by name
170  * \param C Pointer to the TEMU class
171  * \param Name Name of the register bank
172  * \result Pointer to the register bank
173  */
174 TEMU_API temu_RegisterBank *temu_getRegisterBank(temu_Class *C,
175  const char *Name);
176 
177 
178 /*!
179  * Get list of class register banks
180  * The returned object is thread local. It will be destroyed on the next call in
181  * the current thread.
182  *
183  * \param C The class object.
184  * \result Thread local mobel reg info object with a list of all register banks.
185  */
186 TEMU_API const temu_ModelRegInfo * temu_getModelRegisterBankInfo(temu_Class *C);
187 
188 /*!
189  * Get register info for named register
190  * The returned object is thread local. It will be destroyed on the next call in
191  * the current thread.
192  *
193  * \param C The class object.
194  * \param RegName Register name.
195  * \result Thread local register info object.
196  */
197 
198 TEMU_API const temu_RegisterInfo * temu_getRegisterInfo(temu_Class *C, const char *RegName);
199 
200 TEMU_API const temu_RegisterBankInfo *
201 temu_getRegisterBankInfo(temu_Class *C, const char *RegBankName);
202 
203 /*!
204  * Get a register from a register bank by name
205  * \param Bank Pointer to the register bank object
206  * \param Name Name of the register
207  * \result Pointer to the register object
208  */
209 TEMU_API temu_Register *temu_getRegister(temu_RegisterBank *Bank,
210  const char *Name);
211 
212 /*!
213  * Retrieve register name from pointer
214  * \param R Pointer to the register
215  * \result The name of the register
216  */
218 
219 /*!
220  * Get the documentation of a register as a string
221  * \param R Pointer to the register
222  * \result The documentation of the register as a C-string
223  */
225 
226 /*!
227  * Retrieve the value of a field in a register
228  * \param Obj The object of the class the has the register
229  * \param RegName Register name
230  * \param RegIdx Register index
231  * \param FieldName Field name
232  * \return The value of the field
233  */
234 
235 TEMU_API uint64_t temu_getFieldValue(temu_Object *Obj, const char *RegName, unsigned RegIdx,
236  const char *FieldName);
237 
238 /*!
239  * Retrieve the value of a field in a register with
240  * side-effects.
241  * \param Obj The object of the class the has the register
242  * \param RegName Register name
243  * \param RegIdx Register index
244  * \param FieldName Field name
245  * \return The value of the field
246  */
247 TEMU_API uint64_t temu_readFieldValue(temu_Object *Obj, const char *RegName,
248  unsigned RegIdx, const char *FieldName);
249 
250 
251 /*!
252  * Set a field's value in a register
253  * \param Obj The object that contains the register
254  * \param RegName Register name
255  * \param RegIdx Register index
256  * \param FieldName Field name
257  * \param Value The value to be set
258  * \result zero on success, otherwise non-zero value
259  */
260 
261 TEMU_API int temu_setFieldValue(temu_Object *Obj, const char *RegName, unsigned RegIdx,
262  const char *FieldName, uint64_t Value);
263 
264 
265 /*!
266  * Set a field's value in a register with side-effects
267  * \param Obj The object that contains the register
268  * \param RegName Register name
269  * \param RegIdx Register index
270  * \param FieldName Field name
271  * \param Value The value to be set
272  * \result zero on success, otherwise non-zero value
273  */
274 
275 TEMU_API int temu_writeFieldValue(temu_Object *Obj, const char *RegName, unsigned RegIdx,
276  const char *FieldName, uint64_t Value);
277 
278 /*!
279  * Returns the cold reset value of the register (computed from the field info
280  * data). Cold reset is also known as a hard reset, and implies that the power
281  * has been off for some time.
282  *
283  * \param Reg Pointer to the register
284  * \return The cold reset value
285  */
286 TEMU_API uint64_t temu_getRegisterColdResetValue(temu_Register *Reg);
287 
288 /*!
289  * Returns the warm reset value of the register
290  * \param Reg Pointer to the register
291  * \return The warm reset value
292  */
293 TEMU_API uint64_t temu_getRegisterWarmResetValue(temu_Register *Reg);
294 
295 /*!
296  * Get the current write mask of a register
297  * \param Reg Pointer to the register
298  * \return The value of the mask
299  */
300 TEMU_API uint64_t temu_getRegisterWriteMask(temu_Register *Reg);
301 
302 /*!
303  * Get the current read mask of a register
304  * \param Reg Pointer to the register
305  * \return The value of the mask
306  */
307 TEMU_API uint64_t temu_getRegisterReadMask(temu_Register *Reg);
308 
309 #ifdef __cplusplus
310 } // extern c
311 #endif
312 
313 #endif /* ! TEMU_REGISTER_H */
temu_getRegisterBankName
const TEMU_API char * temu_getRegisterBankName(temu_RegisterBank *Bank)
temu_getRegisterDocs
const TEMU_API char * temu_getRegisterDocs(temu_Register *R)
temu_RegisterInfo::WarmResetValue
uint64_t WarmResetValue
Definition: Register.h:61
temu_RegisterInfo::WriteMask
uint64_t WriteMask
Definition: Register.h:64
temu_RegisterBankInfo::RegNames
const char ** RegNames
Definition: Register.h:74
temu_RegisterBankInfo::NumRegisters
size_t NumRegisters
Definition: Register.h:73
temu_RegisterBankInfo::Desc
const char * Desc
Definition: Register.h:72
temu_RegisterInfo::Name
const char * Name
Definition: Register.h:52
temu_RegisterInfo::Field
temu_FieldInfo * Field
Definition: Register.h:67
temu_writeFieldValue
TEMU_API int temu_writeFieldValue(temu_Object *Obj, const char *RegName, unsigned RegIdx, const char *FieldName, uint64_t Value)
temu_FieldInfo::Name
const char * Name
Definition: Register.h:40
temu_setFieldValue
TEMU_API int temu_setFieldValue(temu_Object *Obj, const char *RegName, unsigned RegIdx, const char *FieldName, uint64_t Value)
temu_FieldInfo::ResetValue
uint64_t ResetValue
Definition: Register.h:43
temu_FieldInfo::Writable
unsigned Writable
Definition: Register.h:44
temu_FieldInfo::Mask
uint64_t Mask
Definition: Register.h:42
temu_RegisterInfo::NumFields
size_t NumFields
Definition: Register.h:66
temu_RegisterInfo::ReadMask
uint64_t ReadMask
Definition: Register.h:63
temu_Field
struct temu_Field temu_Field
Definition: Register.h:85
temu_ModelRegInfo::BankNames
const char ** BankNames
Definition: Register.h:79
temu_FieldInfo::Desc
const char * Desc
Definition: Register.h:41
temu_RegisterInfo::Stride
unsigned Stride
Definition: Register.h:57
temu_RegisterBankInfo::Name
const char * Name
Definition: Register.h:71
temu_Register
struct temu_Register temu_Register
Definition: Register.h:84
temu_RegisterInfo::NumRegs
size_t NumRegs
Definition: Register.h:54
temu_getRegisterName
const TEMU_API char * temu_getRegisterName(temu_Register *R)
temu_addField
TEMU_API void temu_addField(temu_Register *R, const char *Name, uint64_t Mask, uint64_t Reset, uint64_t Flags, const char *Doc)
temu_RegisterInfo::DeviceOffset
unsigned DeviceOffset
Definition: Register.h:56
temu_RegisterInfo::Desc
const char * Desc
Definition: Register.h:53
temu_FieldInfo::ClearOnSet
unsigned ClearOnSet
Definition: Register.h:46
temu_FieldInfo::Resettable
unsigned Resettable
Definition: Register.h:45
temu_RegisterBank
struct temu_RegisterBank temu_RegisterBank
Definition: Register.h:83
temu_RegisterInfo::WarmResetMask
uint64_t WarmResetMask
Definition: Register.h:60
temu_RegisterInfo::ColdResetValue
uint64_t ColdResetValue
Definition: Register.h:58
temu_ModelRegInfo::NumRegisterBanks
size_t NumRegisterBanks
Definition: Register.h:78