1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#ifndef __SYSTEMC_UTILS_VCD_HH__
31#define __SYSTEMC_UTILS_VCD_HH__
32
33#include <map>
34
35#include "systemc/utils/tracefile.hh"
36
37namespace sc_gem5
38{
39
40class VcdTraceValBase;
41
42class VcdTraceScope
43{
44  private:
45    std::vector<std::pair<std::string, VcdTraceValBase *>> values;
46    std::map<std::string, VcdTraceScope *> scopes;
47
48  public:
49    void addValue(const std::string &name, VcdTraceValBase *value);
50    void output(const std::string &name, std::ostream &os);
51};
52
53class VcdTraceFile : public TraceFile
54{
55  private:
56    Tick lastPrintedTime;
57    uint64_t deltasAtNow;
58
59    static const int NextNameChars = 5;
60    char _nextName[NextNameChars + 1];
61    std::string nextSignalName();
62
63    bool initialized;
64    void initialize();
65
66    std::vector<VcdTraceValBase *> traceVals;
67    VcdTraceScope topScope;
68
69  public:
70    VcdTraceFile(const std::string &name) :
71        TraceFile(name + ".vcd"), lastPrintedTime(0), deltasAtNow(0),
72        initialized(false)
73    {
74        _nextName[NextNameChars] = '\0';
75        for (int i = 0; i < NextNameChars; i++)
76            _nextName[i] = 'a';
77    }
78    ~VcdTraceFile();
79
80    void trace(bool delta) override;
81
82    template<typename TV>
83    void
84    addNewTraceVal(const typename TV::TracedType *v, const std::string &name,
85                   int width=1)
86    {
87        VcdTraceValBase *tv = new TV(v, nextSignalName(), width);
88        traceVals.push_back(tv);
89        topScope.addValue(name, tv);
90    }
91
92    void addTraceVal(const bool *v, const std::string &name) override;
93    void addTraceVal(const float *v, const std::string &name) override;
94    void addTraceVal(const double *v, const std::string &name) override;
95
96    void addTraceVal(const sc_dt::sc_logic *v,
97                     const std::string &name) override;
98    void addTraceVal(const sc_dt::sc_int_base *v,
99                     const std::string &name) override;
100    void addTraceVal(const sc_dt::sc_uint_base *v,
101                     const std::string &name) override;
102    void addTraceVal(const sc_dt::sc_signed *v,
103                     const std::string &name) override;
104    void addTraceVal(const sc_dt::sc_unsigned *v,
105                     const std::string &name) override;
106    void addTraceVal(const sc_dt::sc_bv_base *v,
107                     const std::string &name) override;
108    void addTraceVal(const sc_dt::sc_lv_base *v,
109                     const std::string &name) override;
110    void addTraceVal(const sc_dt::sc_fxval *v,
111                     const std::string &name) override;
112    void addTraceVal(const sc_dt::sc_fxval_fast *v,
113                     const std::string &name) override;
114    void addTraceVal(const sc_dt::sc_fxnum *v,
115                     const std::string &name) override;
116    void addTraceVal(const sc_dt::sc_fxnum_fast *v,
117                     const std::string &name) override;
118
119    void addTraceVal(const sc_core::sc_event *v,
120                     const std::string &name) override;
121    void addTraceVal(const sc_core::sc_time *v,
122                     const std::string &name) override;
123
124    void addTraceVal(const unsigned char *v,
125                     const std::string &name, int width) override;
126    void addTraceVal(const char *v, const std::string &name,
127                     int width) override;
128    void addTraceVal(const unsigned short *v,
129                     const std::string &name, int width) override;
130    void addTraceVal(const short *v, const std::string &name,
131                     int width) override;
132    void addTraceVal(const unsigned int *v,
133                     const std::string &name, int width) override;
134    void addTraceVal(const int *v, const std::string &name,
135                     int width) override;
136    void addTraceVal(const unsigned long *v,
137                     const std::string &name, int width) override;
138    void addTraceVal(const long *v, const std::string &name,
139                     int width) override;
140
141    void addTraceVal(const sc_dt::int64 *v,
142                     const std::string &name, int width) override;
143    void addTraceVal(const sc_dt::uint64 *v,
144                     const std::string &name, int width) override;
145
146    void addTraceVal(const unsigned int *, const std::string &name,
147                     const char **literals) override;
148
149    void writeComment(const std::string &comment) override;
150};
151
152} // namespace sc_gem5
153
154#endif // __SYSTEMC_UTILS_VCD_HH__
155