The Sparta Modeling Framework
Loading...
Searching...
No Matches
ClockManager.hpp
Go to the documentation of this file.
1// <ClockManager> -*- C++ -*-
2
3
9#pragma once
10
11#include <cstdint>
12#include <list>
13#include <iostream>
14#include <memory>
15#include <string>
16
20
21namespace sparta {
22 class RootTreeNode;
23
29 {
30 typedef std::list<Clock::Handle> ClockList;
31
32 public:
33
34 ClockManager(Scheduler * scheduler) :
35 any_clock_with_explicit_freq_(false),
36 scheduler_(scheduler)
37 { }
38
40 {
41 if(croot_) {
42 croot_->enterTeardown_(); // Allow deletion of Clock nodes in dtor
43 }
44 }
45
58 Clock::Handle makeRoot(RootTreeNode* parent = nullptr, const std::string& name = "Root")
59 {
60 sparta_assert(croot_.get() == nullptr,
61 "Cannot makeRoot on a ClockManager which already has a root");
62 croot_ = Clock::Handle(new Clock(parent, name, scheduler_));
63 clist_.push_back(croot_);
64 return croot_;
65 }
66
67 Clock::Handle getRoot() const
68 {
69 return croot_;
70 }
71
75 Clock::Handle makeClock(const std::string& name, const Clock::Handle &parent,
76 const uint32_t &p_rat, const uint32_t &c_rat)
77 {
78 Clock::Handle c = Clock::Handle(new Clock(name, parent, p_rat, c_rat));
79 clist_.push_back(c);
80 return c;
81 }
82
86 Clock::Handle makeClock(const std::string& name, const Clock::Handle &parent)
87 {
88 return makeClock(name, parent, 1, 1);
89 }
90
94 Clock::Handle makeClock(const std::string & name,
95 const Clock::Handle &parent, double frequency_mhz)
96 {
97 any_clock_with_explicit_freq_ = true;
98 Clock::Handle c = Clock::Handle(new Clock(name, parent, frequency_mhz));
99 clist_.push_back(c);
100 return c;
101 }
102
103 uint32_t normalize()
104 {
105 // Special case when we're dealing with clocks that have an
106 // explicit frequency
107 if (any_clock_with_explicit_freq_) {
108 normalize_frequencies_();
109 return uint32_t(1);
110 }
111
112 // Calculate the normalization factor
113 uint32_t norm_ = croot_->calcNorm();
114
115 // Set the clock periods, based on the normalization factor
116 for (auto i = clist_.begin(); i != clist_.end(); ++i) {
117 (*i)->setPeriod(norm_);
118 }
119
120 // Skip through all TreeNode phases to finalized
121 croot_->enterConfig_();
122 croot_->enterFinalizing_();
123 croot_->finalizeTree_();
124 croot_->enterFinalized_();
125
126 return norm_;
127 }
128
129 void print(std::ostream &os) const
130 {
131 for (auto i = clist_.begin(); i != clist_.end(); ++i) {
132 (*i)->print(os);
133 }
134 }
135
144 static uint64_t getClockPeriodFromFrequencyMhz(double frequency_mhz) {
145 return static_cast<uint64_t>(((1.0 / frequency_mhz) * 1000. * 1000.0));
146 }
147
148 private:
149
150 // Normalize the frequencies for clocks when the frequencies were
151 // set explicitly.
152 void normalize_frequencies_() {
153
154 for (auto i = clist_.begin(); i != clist_.end(); ++i) {
155 double frequency_mhz = (*i)->getFrequencyMhz();
156 sparta_assert(frequency_mhz > 0.0 || i == clist_.begin());
157 if (frequency_mhz == 0.0) {
158 continue;
159 }
160
161 uint64_t period = getClockPeriodFromFrequencyMhz(frequency_mhz);
162 (*i)->setPeriod(period);
163 }
164 }
165
166 Clock::Handle croot_;
167 ClockList clist_;
168 bool any_clock_with_explicit_freq_;
169 Scheduler* scheduler_ = nullptr;
170 }; // class ClockManager
171
172 std::ostream &operator<<(std::ostream &os, const ClockManager &m);
173}
File that defines the Clock class.
A simple time-based, event precedence based scheduler.
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Manages building a clock tree.
Clock::Handle makeClock(const std::string &name, const Clock::Handle &parent, double frequency_mhz)
Create a new clock with a given frequency.
Clock::Handle makeClock(const std::string &name, const Clock::Handle &parent)
Create a new clock with a 1:1 ratio to a parent clock.
static uint64_t getClockPeriodFromFrequencyMhz(double frequency_mhz)
Clock::Handle makeRoot(RootTreeNode *parent=nullptr, const std::string &name="Root")
Construct a root clock.
Clock::Handle makeClock(const std::string &name, const Clock::Handle &parent, const uint32_t &p_rat, const uint32_t &c_rat)
Create a new clock with a given ratio to a parent clock.
A representation of simulated time.
Definition Clock.hpp:51
TreeNode which represents the root ("top") of a device tree.
A class that lets you schedule events now and in the future.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo