plugins/randomgenerator: add deterministic PRNG plugin

A simple plugin which deterministically returns a new random value
each time the specified symbol is read.

Change-Id: I6ccac421fc064f02a88e8b126f8a26044d1f51c6
This commit is contained in:
Florian Lukas
2014-02-18 16:28:42 +01:00
parent e4bf980b97
commit b82e547b53
3 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,10 @@
set(PLUGIN_NAME randomgenerator)
set(MY_PLUGIN_SRCS
RandomGenerator.cc
RandomGenerator.hpp
)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
## Build library
add_library(fail-${PLUGIN_NAME} ${MY_PLUGIN_SRCS})

View File

@ -0,0 +1,25 @@
#include "RandomGenerator.hpp"
#include "sal/Listener.hpp"
#include "sal/Memory.hpp"
using namespace std;
using namespace fail;
bool RandomGenerator::run()
{
m_log << "RandomGenerator started. @ " << m_symbol << std::endl;
MemoryManager& mm = simulator.getMemoryManager();
MemReadListener l_mem(m_symbol.getAddress());
uint32_t value = m_seed;
while (true) {
simulator.addListenerAndResume(&l_mem);
value = value * 1103515245 + 12345;
mm.setBytes(m_symbol.getAddress(), sizeof(value), &value);
}
return true;
}

View File

@ -0,0 +1,38 @@
#ifndef __RandomGenerator_HPP__
#define __RandomGenerator_HPP__
#include <unistd.h>
#include "efw/ExperimentFlow.hpp"
#include "config/FailConfig.hpp"
#include "util/Logger.hpp"
#include "util/ElfReader.hpp"
#include <math.h>
/**
* @file
* @brief A deterministic pseudo-random number generator
*/
/**
* @class RandomGenerator
* @brief Plugin to provide deterministic pseudo-random numbers on a specific memory location
*/
class RandomGenerator : public fail::ExperimentFlow
{
private:
const fail::ElfSymbol m_symbol; //!< the target's memory symbol the plugin is listening on to generate value
fail::Logger m_log; //!< debug output
const unsigned m_seed; //!< PRNG seed value
public:
/**
* Constructor
*
* @param symbol The resulting random is placed in the SUT symbol
* @param seed seed value for PRNG
*/
RandomGenerator( const fail::ElfSymbol & symbol, unsigned seed ) : m_symbol(symbol), m_log("RandGen", false), m_seed(seed){}
bool run();
};
#endif // __RandomGenerator_HPP__