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_SIMPLE_OBJECT_PRINTER_HH__
31#define __SYSTEMC_SIMPLE_OBJECT_PRINTER_HH__
32
33#include <string>
34
35// This include brings in the gem5 statistics classes and DPRINTF mechanism
36// which is not necessary, but shows that if they're gem5 aware, models can
37// take advantage of these sorts of gem5 mechanisms.
38#include "base/statistics.hh"
39#include "base/trace.hh"
40
41// Include the standard top level "systemc" header. For models which aren't
42// aware of gem5, systemc/ext would be in their include path directly and the
43// include wouldn't need the systemc/ext prefix.
44#include "systemc/ext/systemc"
45
46// This class is a garden variety sc_module, except that it uses DPRINTFN in
47// one of its methods, and maintains a gem5 statistic.
48class Printer : public sc_core::sc_module
49{
50  public:
51    sc_core::sc_in<const char *> input;
52    std::string prefix;
53
54    SC_CTOR(Printer)
55    {
56        SC_THREAD(print);
57    }
58
59    void
60    print()
61    {
62        int i = 0;
63        while (true) {
64            wait(input.value_changed_event());
65
66            // DPRINTFN works as expected here because sc_objects have a name()
67            // method which DPRINTFN relies on. Normally name() would come from
68            // a SimObject whose members were in scope, but it doesn't have to.
69            DPRINTFN("Word %d: %s%s\n", i++, prefix, input.read());
70
71            // Manage the gem5 statistic like normal.
72            numWords++;
73        }
74    }
75
76    Stats::Scalar numWords;
77
78    // Gem5 statistics should be set up during the "end_of_elabortion"
79    // callback.
80    void
81    end_of_elaboration() override
82    {
83        numWords
84            .name(std::string(name()) + ".numWords")
85            .desc("number of words printed")
86            ;
87    }
88};
89
90#endif // __SYSTEMC_SIMPLE_OBJECT_PRINTER_HH__
91