CacheComponent.cc Source File

Back to the index.

CacheComponent.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2019 Anders Gavare. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
29 #include "GXemul.h"
30 
31 
32 CacheComponent::CacheComponent(const string& visibleClassName)
33  : Component("cache", visibleClassName)
34  , m_size(0)
35  , m_lineSize(64)
36  , m_lastDumpAddr(0)
37  , m_associativity(1)
38  , m_addressSelect(0)
39 {
40  AddVariable("size", &m_size);
41  AddVariable("lineSize", &m_lineSize);
42  AddVariable("lastDumpAddr", &m_lastDumpAddr);
43  AddVariable("associativity", &m_associativity);
44 // AddCustomVariable("data", &m_dataHandler);
45 }
46 
47 
49 {
50 }
51 
52 
54 {
55  return new CacheComponent();
56 }
57 
58 
59 string CacheComponent::GetAttribute(const string& attributeName)
60 {
61  if (attributeName == "description")
62  return "A generic memory cache component.";
63 
64  return Component::GetAttribute(attributeName);
65 }
66 
67 
69 {
70  stringstream ss;
72 
73  if (!ss.str().empty())
74  ss << ", ";
75 
76  if (m_size >= (1 << 30))
77  ss << (m_size >> 30) << " GB";
78  else if (m_size >= (1 << 20))
79  ss << (m_size >> 20) << " MB";
80  else if (m_size >= (1 << 10))
81  ss << (m_size >> 10) << " KB";
82  else if (m_size != 1)
83  ss << m_size << " bytes";
84  else
85  ss << m_size << " byte";
86 
87  if (m_associativity == 0)
88  ss << ", fully associative, ";
89  else if (m_associativity == 1)
90  ss << ", direct-mapped, ";
91  else
92  ss << ", " << m_associativity << "-way, ";
93 
94  if (m_lineSize >= (1 << 30))
95  ss << (m_lineSize >> 30) << " GB";
96  else if (m_lineSize >= (1 << 20))
97  ss << (m_lineSize >> 20) << " MB";
98  else if (m_lineSize >= (1 << 10))
99  ss << (m_lineSize >> 10) << " KB";
100  else if (m_lineSize != 1)
101  ss << m_lineSize << " bytes";
102  else
103  ss << m_lineSize << " byte";
104 
105  ss << " per line";
106 
107  return ss.str();
108 }
109 
110 
112 {
113 }
114 
115 
116 void CacheComponent::GetMethodNames(vector<string>& names) const
117 {
118  // Add our method names...
119  names.push_back("dump");
120 
121  // ... and make sure to call the base class implementation:
123 }
124 
125 
126 bool CacheComponent::MethodMayBeReexecutedWithoutArgs(const string& methodName) const
127 {
128  if (methodName == "dump")
129  return true;
130 
131  // ... and make sure to call the base class implementation:
133 }
134 
135 
136 void CacheComponent::ExecuteMethod(GXemul* gxemul, const string& methodName,
137  const vector<string>& arguments)
138 {
139  if (methodName == "dump") {
140  uint64_t vaddr = m_lastDumpAddr;
141 
142  if (arguments.size() > 1) {
143  gxemul->GetUI()->ShowDebugMessage("syntax: .dump [addr]\n");
144  return;
145  }
146 
147  if (arguments.size() == 1) {
148  gxemul->GetUI()->ShowDebugMessage("TODO: parse address expression\n");
149  gxemul->GetUI()->ShowDebugMessage("(for now, only hex immediate values are supported!)\n");
150 
151  stringstream ss;
152  ss << arguments[0];
153  ss.flags(std::ios::hex);
154  ss >> vaddr;
155  }
156 
157  const int nRows = 16;
158  for (int i=0; i<nRows; i++) {
159  const size_t len = 16;
160  unsigned char data[len];
161  bool readable[len];
162 
163  stringstream ss;
164  ss.flags(std::ios::hex);
165 
166  if (vaddr > 0xffffffff)
167  ss << std::setw(16);
168  else
169  ss << std::setw(8);
170 
171  ss << std::setfill('0') << vaddr;
172 
173  size_t k;
174  for (k=0; k<len; ++k) {
175  AddressSelect(vaddr + k);
176  readable[k] = ReadData(data[k], BigEndian);
177  }
178 
179  ss << " ";
180 
181  for (k=0; k<len; ++k) {
182  if ((k&3) == 0)
183  ss << " ";
184 
185  ss << std::setw(2) << std::setfill('0');
186  if (readable[k])
187  ss << (int)data[k];
188  else
189  ss << "--";
190  }
191 
192  ss << " ";
193 
194  for (k=0; k<len; ++k) {
195  char s[2];
196  s[0] = data[k] >= 32 && data[k] < 127? data[k] : '.';
197  s[1] = '\0';
198 
199  if (readable[k])
200  ss << s;
201  else
202  ss << "-";
203  }
204 
205  ss << "\n";
206 
207  gxemul->GetUI()->ShowDebugMessage(ss.str());
208 
209  vaddr += len;
210  }
211 
212  m_lastDumpAddr = vaddr;
213 
214  return;
215  }
216 
217  // Call base...
218  Component::ExecuteMethod(gxemul, methodName, arguments);
219 }
220 
221 
223 {
224  return this;
225 }
226 
227 
228 void CacheComponent::AddressSelect(uint64_t address)
229 {
230 #if 0
231  m_addressSelect = address;
232 
233  uint64_t blockNr = address >> m_blockSizeShift;
234 
235  if (blockNr+1 > m_memoryBlocks.size())
236  m_selectedHostMemoryBlock = NULL;
237  else
238  m_selectedHostMemoryBlock = m_memoryBlocks[blockNr];
239 
240  m_selectedOffsetWithinBlock = address & (m_blockSize-1);
241 #endif
242 }
243 
244 
245 bool CacheComponent::ReadData(uint8_t& data, Endianness endianness)
246 {
247 #if 0
248  if (m_selectedHostMemoryBlock == NULL)
249  data = 0;
250  else
251  data = (((uint8_t*)m_selectedHostMemoryBlock)
252  [m_selectedOffsetWithinBlock]);
253 
254  return true;
255 #endif
256  return false;
257 }
258 
259 
260 bool CacheComponent::ReadData(uint16_t& data, Endianness endianness)
261 {
262 #if 0
263  assert((m_addressSelect & 1) == 0);
264 
265  if (m_selectedHostMemoryBlock == NULL)
266  data = 0;
267  else
268  data = (((uint16_t*)m_selectedHostMemoryBlock)
269  [m_selectedOffsetWithinBlock >> 1]);
270 
271  if (endianness == BigEndian)
273  else
275 
276  return true;
277 #endif
278  return false;
279 }
280 
281 
282 bool CacheComponent::ReadData(uint32_t& data, Endianness endianness)
283 {
284 #if 0
285  assert((m_addressSelect & 3) == 0);
286 
287  if (m_selectedHostMemoryBlock == NULL)
288  data = 0;
289  else
290  data = (((uint32_t*)m_selectedHostMemoryBlock)
291  [m_selectedOffsetWithinBlock >> 2]);
292 
293  if (endianness == BigEndian)
295  else
297 
298  return true;
299 #endif
300  return false;
301 }
302 
303 
304 bool CacheComponent::ReadData(uint64_t& data, Endianness endianness)
305 {
306 #if 0
307  assert((m_addressSelect & 7) == 0);
308 
309  if (m_selectedHostMemoryBlock == NULL)
310  data = 0;
311  else
312  data = (((uint64_t*)m_selectedHostMemoryBlock)
313  [m_selectedOffsetWithinBlock >> 3]);
314 
315  if (endianness == BigEndian)
317  else
319 
320  return true;
321 #endif
322  return false;
323 }
324 
325 
326 bool CacheComponent::WriteData(const uint8_t& data, Endianness endianness)
327 {
328 #if 0
329  if (m_writeProtected)
330  return false;
331 
332  if (m_selectedHostMemoryBlock == NULL)
333  m_selectedHostMemoryBlock = AllocateBlock();
334 
335  (((uint8_t*)m_selectedHostMemoryBlock)
336  [m_selectedOffsetWithinBlock]) = data;
337 
338  return true;
339 #endif
340  return false;
341 }
342 
343 
344 bool CacheComponent::WriteData(const uint16_t& data, Endianness endianness)
345 {
346 #if 0
347  assert((m_addressSelect & 1) == 0);
348 
349  if (m_writeProtected)
350  return false;
351 
352  if (m_selectedHostMemoryBlock == NULL)
353  m_selectedHostMemoryBlock = AllocateBlock();
354 
355  uint16_t d;
356  if (endianness == BigEndian)
357  d = BE16_TO_HOST(data);
358  else
359  d = LE16_TO_HOST(data);
360 
361  (((uint16_t*)m_selectedHostMemoryBlock)
362  [m_selectedOffsetWithinBlock >> 1]) = d;
363 
364  return true;
365 #endif
366  return false;
367 }
368 
369 
370 bool CacheComponent::WriteData(const uint32_t& data, Endianness endianness)
371 {
372 #if 0
373  assert((m_addressSelect & 3) == 0);
374 
375  if (m_writeProtected)
376  return false;
377 
378  if (m_selectedHostMemoryBlock == NULL)
379  m_selectedHostMemoryBlock = AllocateBlock();
380 
381  uint32_t d;
382  if (endianness == BigEndian)
383  d = BE32_TO_HOST(data);
384  else
385  d = LE32_TO_HOST(data);
386 
387  (((uint32_t*)m_selectedHostMemoryBlock)
388  [m_selectedOffsetWithinBlock >> 2]) = d;
389 
390  return true;
391 #endif
392  return false;
393 }
394 
395 
396 bool CacheComponent::WriteData(const uint64_t& data, Endianness endianness)
397 {
398 #if 0
399  assert((m_addressSelect & 7) == 0);
400 
401  if (m_writeProtected)
402  return false;
403 
404  if (m_selectedHostMemoryBlock == NULL)
405  m_selectedHostMemoryBlock = AllocateBlock();
406 
407  uint64_t d;
408  if (endianness == BigEndian)
409  d = BE64_TO_HOST(data);
410  else
411  d = LE64_TO_HOST(data);
412 
413  (((uint64_t*)m_selectedHostMemoryBlock)
414  [m_selectedOffsetWithinBlock >> 3]) = d;
415 
416  return true;
417 #endif
418  return false;
419 }
420 
421 
422 /*****************************************************************************/
423 
424 
425 #ifdef WITHUNITTESTS
426 
427 #include "ComponentFactory.h"
428 
429 static void Test_CacheComponent_AddressDataBus()
430 {
432 
433  AddressDataBus* bus = ram->AsAddressDataBus();
434  UnitTest::Assert("The CacheComponent should implement the "
435  "AddressDataBus interface", bus != NULL);
436 }
437 
439 {
440  UNITTEST(Test_CacheComponent_AddressDataBus);
441 }
442 
443 #endif
444 
CacheComponent::WriteData
virtual bool WriteData(const uint8_t &data, Endianness endianness)
Writes 8-bit data to the currently selected address.
Definition: CacheComponent.cc:326
data
u_short data
Definition: siireg.h:79
CacheComponent::CacheComponent
CacheComponent(const string &visibleClassName="cache")
Constructs a CacheComponent.
Definition: CacheComponent.cc:32
Component::GenerateDetails
virtual string GenerateDetails() const
Generate details about the component.
Definition: Component.cc:417
Component::AddVariable
bool AddVariable(const string &name, T *variablePointer)
Adds a state variable of type T to the Component.
Definition: Component.h:563
CacheComponent::AsAddressDataBus
virtual AddressDataBus * AsAddressDataBus()
Returns the component's AddressDataBus interface.
Definition: CacheComponent.cc:222
ComponentFactory.h
GXemul
The main emulator class.
Definition: GXemul.h:55
CacheComponent::GenerateDetails
string GenerateDetails() const
Generate details about the component.
Definition: CacheComponent.cc:68
refcount_ptr< Component >
UNITTESTS
#define UNITTESTS(class)
Helper for unit test case execution.
Definition: UnitTest.h:184
LE32_TO_HOST
#define LE32_TO_HOST(x)
Definition: misc.h:180
BE32_TO_HOST
#define BE32_TO_HOST(x)
Definition: misc.h:181
UNITTEST
#define UNITTEST(functionname)
Helper for unit test case execution.
Definition: UnitTest.h:217
LE64_TO_HOST
#define LE64_TO_HOST(x)
Definition: misc.h:188
Component::GetMethodNames
virtual void GetMethodNames(vector< string > &names) const
Retrieves a component's implemented method names.
Definition: Component.cc:393
CacheComponent::MethodMayBeReexecutedWithoutArgs
virtual bool MethodMayBeReexecutedWithoutArgs(const string &methodName) const
Returns whether a method name may be re-executed without args.
Definition: CacheComponent.cc:126
UnitTest::Assert
static void Assert(const string &strFailMessage, bool condition)
Asserts that a boolean condition is correct.
Definition: UnitTest.cc:40
Component::ExecuteMethod
virtual void ExecuteMethod(GXemul *gxemul, const string &methodName, const vector< string > &arguments)
Executes a method on the component.
Definition: Component.cc:406
UI::ShowDebugMessage
virtual void ShowDebugMessage(const string &msg)=0
Shows a debug message.
Endianness
Endianness
Definition: misc.h:157
CacheComponent::GetMethodNames
virtual void GetMethodNames(vector< string > &names) const
Retrieves a component's implemented method names.
Definition: CacheComponent.cc:116
ComponentFactory::CreateComponent
static refcount_ptr< Component > CreateComponent(const string &componentNameAndOptionalArgs, GXemul *gxemul=NULL)
Creates a component given a short component name.
Definition: ComponentFactory.cc:87
CacheComponent
A memory Cache Component.
Definition: CacheComponent.h:62
BE64_TO_HOST
#define BE64_TO_HOST(x)
Definition: misc.h:189
CacheComponent.h
CacheComponent::ResetState
virtual void ResetState()
Resets the state variables of this component.
Definition: CacheComponent.cc:111
GXemul::GetUI
UI * GetUI()
Gets a pointer to the GXemul instance' active UI.
Definition: GXemul.cc:653
Component::MethodMayBeReexecutedWithoutArgs
virtual bool MethodMayBeReexecutedWithoutArgs(const string &methodName) const
Returns whether a method name may be re-executed without args.
Definition: Component.cc:399
Component
A Component is a node in the configuration tree that makes up an emulation setup.
Definition: Component.h:64
BE16_TO_HOST
#define BE16_TO_HOST(x)
Definition: misc.h:173
CacheComponent::ReadData
virtual bool ReadData(uint8_t &data, Endianness endianness)
Reads 8-bit data from the currently selected address.
Definition: CacheComponent.cc:245
CacheComponent::GetAttribute
static string GetAttribute(const string &attributeName)
Get attribute information about the CacheComponent class.
Definition: CacheComponent.cc:59
LE16_TO_HOST
#define LE16_TO_HOST(x)
Definition: misc.h:172
CacheComponent::~CacheComponent
virtual ~CacheComponent()
Definition: CacheComponent.cc:48
CacheComponent::AddressSelect
virtual void AddressSelect(uint64_t address)
Place an address on the bus.
Definition: CacheComponent.cc:228
AddressDataBus
An interface for implementing components that read/write data via an address bus.
Definition: AddressDataBus.h:45
Component::GetAttribute
static string GetAttribute(const string &attributeName)
Creates a Component.
Definition: Component.cc:66
BigEndian
@ BigEndian
Definition: misc.h:158
Component::AsAddressDataBus
virtual AddressDataBus * AsAddressDataBus()
Returns the component's AddressDataBus interface, if any.
Definition: Component.cc:367
ComponentCreateArgs
Definition: Component.h:49
CacheComponent::Create
static refcount_ptr< Component > Create(const ComponentCreateArgs &args)
Creates a CacheComponent.
Definition: CacheComponent.cc:53
GXemul.h
CacheComponent::ExecuteMethod
virtual void ExecuteMethod(GXemul *gxemul, const string &methodName, const vector< string > &arguments)
Executes a method on the component.
Definition: CacheComponent.cc:136

Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18