rogue
Loading...
Searching...
No Matches
Emulate.cpp
Go to the documentation of this file.
1
19#include "rogue/Directives.h"
20
22
23#include <inttypes.h>
24
25#include <cstring>
26#include <memory>
27#include <string>
28#include <utility>
29
30#include "rogue/GilRelease.h"
34
36
37#ifndef NO_PYTHON
38 #include <boost/python.hpp>
39namespace bp = boost::python;
40#endif
41
43rim::EmulatePtr rim::Emulate::create(uint32_t min, uint32_t max) {
44 rim::EmulatePtr b = std::make_shared<rim::Emulate>(min, max);
45 return (b);
46}
47
49rim::Emulate::Emulate(uint32_t min, uint32_t max) : Slave(min, max) {
50 totAlloc_ = 0;
51 totSize_ = 0;
52 log_ = rogue::Logging::create("memory.Emulate");
53}
54
56rim::Emulate::~Emulate() {
57 EMULATE_MAP_TYPE::iterator it = memMap_.begin();
58 while (it != memMap_.end()) {
59 free(it->second);
60 ++it;
61 }
62}
63
65void rim::Emulate::doTransaction(rim::TransactionPtr tran) {
66 uint64_t addr4k;
67 uint64_t off4k;
68 uint64_t size4k;
69 uint32_t size = tran->size();
70 uint32_t type = tran->type();
71 uint64_t addr = tran->address();
72 uint8_t* ptr = tran->begin();
73
74 // printf("Got transaction address=0x%" PRIx64 ", size=%" PRIu32 ", type = %" PRIu32 "\n", addr, size, type);
75
77 {
78 std::lock_guard<std::mutex> lock(mtx_);
79
80 while (size > 0) {
81 addr4k = (addr / 0x1000) * 0x1000;
82 off4k = addr % 0x1000;
83 size4k = (addr4k + 0x1000) - (addr4k + off4k);
84
85 if (size4k > size) size4k = size;
86 size -= size4k;
87 addr += size4k;
88
89 if (memMap_.find(addr4k) == memMap_.end()) {
90 memMap_.insert(std::make_pair(addr4k, reinterpret_cast<uint8_t*>(malloc(0x1000))));
91 totSize_ += 0x1000;
92 totAlloc_++;
93 log_->debug("Allocating block at 0x%x. Total Blocks %i, Total Size = %i", addr4k, totAlloc_, totSize_);
94 }
95
96 // Write or post
97 if (tran->type() == rogue::interfaces::memory::Write || tran->type() == rogue::interfaces::memory::Post) {
98 // printf("Write data to 4k=0x%" PRIx64 ", offset=0x%" PRIx64 ", size=%" PRIu64 "\n", addr4k, off4k,
99 // size4k);
100 memcpy(memMap_[addr4k] + off4k, ptr, size4k);
101
102 // Read or verify
103 } else {
104 // printf("Read data from 4k=0x%" PRIx64 ", offset=0x%" PRIx64 ", size=%" PRIu64 "\n", addr4k, off4k,
105 // size4k);
106 memcpy(ptr, memMap_[addr4k] + off4k, size4k);
107 }
108
109 ptr += size4k;
110 }
111 }
112 tran->done();
113}
114
115void rim::Emulate::setup_python() {
116#ifndef NO_PYTHON
117 bp::class_<rim::Emulate, rim::EmulatePtr, bp::bases<rim::Slave>, boost::noncopyable>(
118 "Emulate",
119 bp::init<uint32_t, uint32_t>());
120 bp::implicitly_convertible<rim::EmulatePtr, rim::SlavePtr>();
121#endif
122}
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:60
Memory slave device.
Definition Slave.h:54
uint32_t max()
Returns configured maximum transaction size.
Definition Slave.cpp:116
uint32_t min()
Returns configured minimum transaction size.
Definition Slave.cpp:111
std::shared_ptr< rogue::interfaces::memory::TransactionLock > TransactionLockPtr
Shared pointer alias for TransactionLock.
std::shared_ptr< rogue::interfaces::memory::Emulate > EmulatePtr
Shared pointer alias for Emulate.
Definition Emulate.h:112
static const uint32_t Write
Memory write transaction type.
Definition Constants.h:43
std::shared_ptr< rogue::interfaces::memory::Transaction > TransactionPtr
Shared pointer alias for Transaction.
static const uint32_t Post
Memory posted write transaction type.
Definition Constants.h:50