The Sparta Modeling Framework
Loading...
Searching...
No Matches
SimpleDL1.hpp
1
2#pragma once
3
5#include "sparta/utils/MathUtils.hpp"
6#include "cache/BasicCacheItem.hpp"
7#include "cache/SimpleCache2.hpp"
8#include "cache/ReplacementIF.hpp"
9#include "cache/preload/PreloadableIF.hpp"
10#include "cache/preload/PreloadableNode.hpp"
11
12using namespace std::placeholders;
13namespace core_example
14{
15 class SimpleCacheLine : public sparta::cache::BasicCacheItem
16 {
17 public:
18 SimpleCacheLine() = delete;
19
20 SimpleCacheLine(uint64_t line_size) :
21 line_size_(line_size),
22 valid_(false)
23 {
24 sparta_assert(sparta::utils::is_power_of_2(line_size),
25 "Cache line size must be a power of 2. line_size=" << line_size);
26 }
27
28 // Copy constructor
29 SimpleCacheLine(const SimpleCacheLine & rhs) :
30 BasicCacheItem(rhs),
31 line_size_(rhs.line_size_),
32 valid_(rhs.valid_)
33 {
34 }
35
36 // Copy assignment operator
37 SimpleCacheLine& operator=(const SimpleCacheLine & rhs)
38 {
39 BasicCacheItem::operator=(rhs);
40 line_size_ = rhs.line_size_;
41 valid_ = rhs.valid_;
42
43 return *this;
44 }
45
46 virtual ~SimpleCacheLine() {}
47
48 // Required by SimpleCache2
49 void reset(uint64_t addr)
50 {
51 setValid(true);
52 BasicCacheItem::setAddr(addr);
53 }
54
55 // Required by SimpleCache2
56 void setValid(bool v) { valid_ = v; }
57
58 // Required by BasicCacheSet
59 bool isValid() const { return valid_; }
60
61 // Required by SimpleCache2
62 void setModified(bool m) { (void) m; }
63
64 // Required by SimpleCache2
65 bool read(uint64_t offset, uint32_t size, uint32_t *buf) const
66 {
67 (void) offset;
68 (void) size;
69 (void) buf;
70 sparta_assert(false);
71 return true;
72 }
73
74 // Required by SimpleCache2
75 bool write(uint64_t offset, uint32_t size, uint32_t *buf) const
76 {
77 (void) offset;
78 (void) size;
79 (void) buf;
80 sparta_assert(false);
81 return true;
82 }
83
84 private:
85 uint32_t line_size_;
86 bool valid_;
87
88 }; // class SimpleCacheLine
89
90 class SimpleDL1 : public sparta::cache::SimpleCache2<SimpleCacheLine>,
91 public sparta::TreeNode,
92 public sparta::cache::PreloadableIF,
93 public sparta::cache::PreloadDumpableIF
94
95 {
96 public:
97 using Handle = std::shared_ptr<SimpleDL1>;
99 uint64_t cache_size_kb,
100 uint64_t line_size,
101 const sparta::cache::ReplacementIF& rep) :
102 sparta::cache::SimpleCache2<SimpleCacheLine> (cache_size_kb,
103 line_size,
104 line_size,
105 SimpleCacheLine(line_size),
106 rep),
107 sparta::TreeNode(parent, "l1cache", "Simple L1 Cache"),
108 sparta::cache::PreloadableIF(),
109 sparta::cache::PreloadDumpableIF(),
110 preloadable_(this, std::bind(&SimpleDL1::preloadPkt_, this, _1),
111 std::bind(&SimpleDL1::preloadDump_, this, _1))
112 {}
113 private:
117 bool preloadPkt_(sparta::cache::PreloadPkt& pkt) override
118 {
119 sparta::cache::PreloadPkt::NodeList lines;
120 pkt.getList(lines);
121 for (auto& line_data : lines)
122 {
123 uint64_t va = line_data->getScalar<uint64_t>("va");
124 auto& cache_line = getLineForReplacement(va);
125 std::cout << *this << " : Preloading VA: 0x" << std::hex << va
126 << std::endl;
127 allocateWithMRUUpdate(cache_line, va);
128 // Sanity check that the line was marked as valid.
129 sparta_assert(getLine(va) != nullptr);
130 }
131 return true;
132
133 }
134
135 void preloadDump_(sparta::cache::PreloadEmitter& emitter) const override
136 {
137 emitter << sparta::cache::PreloadEmitter::BeginMap;
138 emitter << sparta::cache::PreloadEmitter::Key << "lines";
139 emitter << sparta::cache::PreloadEmitter::Value;
140 emitter << sparta::cache::PreloadEmitter::BeginSeq;
141 for (auto set_it = begin();
142 set_it != end(); ++set_it)
143 {
144 for (auto line_it = set_it->begin();
145 line_it != set_it->end(); ++line_it)
146 {
147 if(line_it->isValid())
148 {
149 std::map<std::string, std::string> map;
150 std::stringstream t;
151 t << "0x" << std::hex << line_it->getAddr();
152 map["pa"] = t.str();
153 emitter << map;
154 }
155 }
156 }
157 emitter << sparta::cache::PreloadEmitter::EndSeq;
158 emitter << sparta::cache::PreloadEmitter::EndMap;
159 }
162 sparta::cache::PreloadableNode preloadable_;
163
164 }; // class SimpleDL1
165
166} // namespace core_example
167
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.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205