summaryrefslogtreecommitdiffstats
path: root/utils/regtools/include/soc_desc.hpp
blob: 66f2e3b6e16efc5cf7cc77380adefaa352d7d8ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2014 by Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/  
#ifndef __SOC_DESC__
#define __SOC_DESC__

#include <stdint.h>
#include <vector>
#include <list>
#include <string>
#include <map>

namespace soc_desc
{

const size_t MAJOR_VERSION = 2;
const size_t MINOR_VERSION = 0;
const size_t REVISION_VERSION = 0;

/** Typedef for SoC types: word, address and flags */
typedef uint32_t soc_addr_t;
typedef uint32_t soc_word_t;
typedef int soc_id_t;

/** Error class */
class error_t
{
public:
    enum level_t
    {
        INFO,
        WARNING,
        FATAL
    };
    error_t(level_t lvl, const std::string& loc, const std::string& msg)
        :m_level(lvl), m_loc(loc), m_msg(msg) {}
    level_t level() const { return m_level; }
    std::string location() const { return m_loc; }
    std::string message() const { return m_msg; }
protected:
    level_t m_level;
    std::string m_loc, m_msg;
};

/** Error context to log errors */
class error_context_t
{
public:
    void add(const error_t& err) { m_list.push_back(err); }
    size_t count() const { return m_list.size(); }
    error_t get(size_t i) const { return m_list[i]; }
protected:
    std::vector< error_t > m_list;
};

/**
 * Bare representation of the format
 */

/** Enumerated value (aka named value), represents a special value for a field */
struct enum_t
{
    soc_id_t id; /** ID (must be unique among field enums) */
    std::string name; /** Name (must be unique among field enums) */
    std::string desc; /** Optional description of the meaning of this value */
    soc_word_t value; /** Value of the field */
};

/** Register field information */
struct field_t
{
    soc_id_t id; /** ID (must be unique among register fields) */
    std::string name; /** Name (must be unique among register fields) */
    std::string desc; /** Optional description of the field */
    size_t pos; /** Position of the least significant bit */
    size_t width; /** Width of the field in bits  */
    std::vector< enum_t > enum_; /** List of special values */

    /** Returns the bit mask of the field within the register */
    soc_word_t bitmask() const
    {
        // WARNING beware of the case where width is 32
        if(width == 32)
            return 0xffffffff;
        else
            return ((1 << width) - 1) << pos;
    }

    /** Extract field value from register value */
    soc_word_t extract(soc_word_t reg_val) const
    {
        return (reg_val & bitmask()) >> pos;
    }

    /** Replace the field value in a register value */
    soc_word_t replace(soc_word_t reg_val, soc_word_t field_val) const
    {
        return (reg_val & ~bitmask()) | ((field_val << pos) & bitmask());
    }

    /** Return field value index, or -1 if none */
    int find_value(soc_word_t v) const
    {
        for(size_t i = 0; i < enum_.size(); i++)
            if(enum_[i].value == v)
                return i;
        return -1;
    }
};

/** Register information */
struct register_t
{
    size_t width; /** Size in bits */
    std::vector< field_t > field; /** List of fields */
};

/** Node address range information */
struct range_t
{
    enum type_t
    {
        STRIDE, /** Addresses are given by a base address and a stride */
        FORMULA /** Addresses are given by a formula */
    };

    type_t type; /** Range type */
    size_t first; /** First index in the range */
    size_t count; /** Number of indexes in the range */
    soc_word_t base; /** Base address (for STRIDE) */
    soc_word_t stride; /** Stride value (for STRIDE) */
    std::string formula; /** Formula (for FORMULA) */
    std::string variable; /** Formula variable name (for FORMULA) */
};

/** Node instance information */
struct instance_t
{
    enum type_t
    {
        SINGLE, /** There is a single instance at a specified address */
        RANGE /** There are multiple addresses forming a range */
    };

    soc_id_t id; /** ID (must be unique among node instances) */
    std::string name; /** Name (must be unique among node instances) */
    std::string title; /** Optional instance human name */
    std::string desc; /** Optional description of the instance */
    type_t type; /** Instance type */
    soc_word_t addr; /** Address (for SINGLE) */
    range_t range; /** Range (for RANGE) */
};

/** Node information */
struct node_t
{
    soc_id_t id; /** ID (must be unique among nodes) */
    std::string name; /** Name (must be unique for the among nodes) */
    std::string title; /** Optional node human name */
    std::string desc; /** Optional description of the node */
    std::vector< register_t> register_; /** Optional register */
    std::vector< instance_t> instance; /** List of instances */
    std::vector< node_t > node; /** List of sub-nodes */
};

/** System-on-chip information */
struct soc_t
{
    std::string name; /** Codename of the SoC */
    std::string title; /** Human name of the SoC */
    std::string desc; /** Optional description of the SoC */
    std::string isa; /** Instruction Set Assembly */
    std::string version; /** Description version */
    std::vector< std::string > author; /** List of authors of the description */
    std::vector< node_t > node; /** List of nodes */
};

/** Parse a SoC description from a XML file, put it into <soc>. */
bool parse_xml(const std::string& filename, soc_t& soc, error_context_t& error_ctx);
/** Write a SoC description to a XML file, overwriting it. A file can contain
 * multiple Soc descriptions */
bool produce_xml(const std::string& filename, const soc_t& soc, error_context_t& error_ctx);
/** Formula parser: try to parse and evaluate a formula with some variables */
bool evaluate_formula(const std::string& formula,
    const std::map< std::string, soc_word_t>& var, soc_word_t& result,
    const std::string& loc, error_context_t& error_ctx);

/**
 * Convenience API to manipulate the format
 *
 * The idea is that *_ref_t objects are stable pointers: they stay valid even
 * when the underlying soc changes. In particular:
 * - modifying any structure data (except id fields) preserves all references
 * - removing a structure invalidates all references pointing to this structure
 *   and its children
 * - adding any structure preserves all references
 * These references can be used to get pointers to the actual data
 * of the representation when it needs to be read or write.
 */

class soc_ref_t;
class node_ref_t;
class register_ref_t;
class field_ref_t;
class node_inst_t;

/** SoC reference */
class soc_ref_t
{
    soc_t *m_soc; /* pointer to the soc */
public:
     /** Builds an invalid reference */
    soc_ref_t();
     /** Builds a reference to a soc */
    soc_ref_t(soc_t *soc);
    /** Checks whether this reference is valid */
    bool valid() const;
    /** Returns a pointer to the soc */
    soc_t *get() const;
    /** Returns a reference to the root node */
    node_ref_t root() const;
    /** Returns a reference to the root node instance */
    node_inst_t root_inst() const;
    /** Compare this reference to another */
    bool operator==(const soc_ref_t& r) const;
    inline bool operator!=(const soc_ref_t& r) const { return !operator==(r); }
};

/** SoC node reference
 * NOTE: the root soc node is presented as a node with empty path */
class node_ref_t
{
    friend class soc_ref_t;
    friend class node_inst_t;
    soc_ref_t m_soc; /* reference to the soc */
    std::vector< soc_id_t > m_path; /* path from the root */

    node_ref_t(soc_ref_t soc);
    node_ref_t(soc_ref_t soc, const std::vector< soc_id_t >& path);
public:
    /** Builds an invalid reference */
    node_ref_t();
    /** Check whether this reference is valid */
    bool valid() const;
    /** Check whether this reference is the root node */
    bool is_root() const;
    /** Returns a pointer to the node, or 0 if invalid or root */
    node_t *get() const;
    /** Returns a reference to the soc */
    soc_ref_t soc() const;
    /** Returns a reference to the parent node */
    node_ref_t parent() const;
    /** Returns a reference to the register (which may be on a parent node) */
    register_ref_t reg() const;
    /** Returns a list of references to the sub-nodes */
    std::vector< node_ref_t > children() const;
    /** Returns a reference to a specific child */
    node_ref_t child(const std::string& name) const;
    /** Returns the path of the node, as the list of node names from the root */
    std::vector< std::string > path() const;
    /** Returns the name of the node */
    std::string name() const;
    /** Compare this reference to another */
    bool operator==(const node_ref_t& r) const;
    inline bool operator!=(const node_ref_t& r) const { return !operator==(r); }
};

/** SoC register reference */
class register_ref_t
{
    friend class node_ref_t;
    node_ref_t m_node; /* reference to the node owning the register */

    register_ref_t(node_ref_t node);
public:
    /** Builds an invalid reference */
    register_ref_t();
    /** Check whether this reference is valid/exists */
    bool valid() const;
    /** Returns a pointer to the register, or 0 */
    register_t *get() const;
    /** Returns a reference to the node containing the register */
    node_ref_t node() const;
    /** Returns a list of references to the fields of the register */
    std::vector< field_ref_t > fields() const;
    /** Returns a reference to a particular field */
    field_ref_t field(const std::string& name) const;
    /** Compare this reference to another */
    bool operator==(const register_ref_t& r) const;
    inline bool operator!=(const register_ref_t& r) const { return !operator==(r); }
};

/** SoC register field reference */
class field_ref_t
{
    friend class register_ref_t;
    register_ref_t m_reg; /* reference to the register */
    soc_id_t m_id; /* field name */

    field_ref_t(register_ref_t reg, soc_id_t id);
public:
    /** Builds an invalid reference */
    field_ref_t();
    /** Check whether this reference is valid/exists */
    bool valid() const;
    /** Returns a pointer to the field, or 0 */
    field_t *get() const;
    /** Returns a reference to the register containing the field */
    register_ref_t reg() const;
    /** Compare this reference to another */
    bool operator==(const field_ref_t& r) const;
    inline bool operator!=(const field_ref_t& r) const { return !operator==(r); }
};

/** SoC node instance
 * NOTE: the root soc node is presented as a node with a single instance at 0 */
class node_inst_t
{
    friend class node_ref_t;
    friend class soc_ref_t;
    node_ref_t m_node; /* reference to the node */
    std::vector< soc_id_t > m_id_path; /* list of instance IDs */
    std::vector< size_t > m_index_path; /* list of instance indexes */

    node_inst_t(soc_ref_t soc);
    node_inst_t(node_ref_t soc, const std::vector< soc_id_t >& path,
        const std::vector< size_t >& indexes);
public:
    /** Builds an invalid reference */
    node_inst_t();
    /** Check whether this instance is valid/exists */
    bool valid() const;
    /** Returns a reference to the soc */
    soc_ref_t soc() const;
    /** Returns a reference to the node */
    node_ref_t node() const;
    /** Check whether this reference is the root node instance */
    bool is_root() const;
    /** Returns a reference to the parent instance */
    node_inst_t parent() const;
     /** Returns a pointer to the instance of the node, or 0 */
    instance_t *get() const;
    /** Returns the address of this instance */
    soc_addr_t addr() const;
    /** Returns an instance to a child of this node's instance. If the subnode
     * instance is a range, the returned reference is invalid */
    node_inst_t child(const std::string& name) const;
    /** Returns an instance to a child of this node's instance with a range index.
     * If the subnode is not not a range or if the index is out of bounds,
     * the returned reference is invalid */
    node_inst_t child(const std::string& name, size_t index) const;
    /** Returns a list of all instances of subnodes of this node's instance */
    std::vector< node_inst_t > children() const;
    /** Returns the name of the instance */
    std::string name() const;
    /** Checks whether this instance is indexed */
    bool is_indexed() const;
    /** Returns the index of the instance */
    size_t index() const;
    /** Compare this reference to another */
    bool operator==(const node_inst_t& r) const;
    inline bool operator!=(const node_inst_t& r) const { return !operator==(r); }
};

} // soc_desc

#endif /* __SOC_DESC__ */