#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
using namespace boost::chrono;
template <long long speed>
struct cycle_count
{
typedef typename boost::ratio_multiply<boost::ratio<speed>, boost::mega>::type frequency; typedef typename boost::ratio_divide<boost::ratio<1>, frequency>::type period;
typedef long long rep;
typedef boost::chrono::duration<rep, period> duration;
typedef boost::chrono::time_point<cycle_count> time_point;
static time_point now()
{
static long long tick = 0;
return time_point(duration(++tick)); }
};
template <long long speed>
struct approx_cycle_count
{
static const long long frequency = speed * 1000000; typedef nanoseconds duration;
typedef duration::rep rep;
typedef duration::period period;
static const long long nanosec_per_sec = period::den;
typedef boost::chrono::time_point<approx_cycle_count> time_point;
static time_point now()
{
static long long tick = 0;
return time_point(duration(++tick * nanosec_per_sec / frequency));
}
};
void cycle_count_delay()
{
{
typedef cycle_count<400> clock;
std::cout << "\nSimulated " << clock::frequency::num / boost::mega::num << "MHz clock which has a tick period of "
<< duration<double, boost::nano>(clock::duration(1)).count() << " nanoseconds\n";
nanoseconds delayns(500);
clock::duration delay = duration_cast<clock::duration>(delayns);
std::cout << "delay = " << delayns.count() << " nanoseconds which is " << delay.count() << " cycles\n";
clock::time_point start = clock::now();
clock::time_point stop = start + delay;
while (clock::now() < stop) ;
clock::time_point end = clock::now();
clock::duration elapsed = end - start;
std::cout << "paused " << elapsed.count() << " cycles ";
std::cout << "which is " << duration_cast<nanoseconds>(elapsed).count() << " nanoseconds\n";
}
{
typedef approx_cycle_count<400> clock;
std::cout << "\nSimulated " << clock::frequency / 1000000 << "MHz clock modeled with nanoseconds\n";
clock::duration delay = nanoseconds(500);
std::cout << "delay = " << delay.count() << " nanoseconds\n";
clock::time_point start = clock::now();
clock::time_point stop = start + delay;
while (clock::now() < stop) ;
clock::time_point end = clock::now();
clock::duration elapsed = end - start;
std::cout << "paused " << elapsed.count() << " nanoseconds\n";
}
{
typedef cycle_count<1500> clock;
std::cout << "\nSimulated " << clock::frequency::num / boost::mega::num << "MHz clock which has a tick period of "
<< duration<double, boost::nano>(clock::duration(1)).count() << " nanoseconds\n";
nanoseconds delayns(500);
clock::duration delay = duration_cast<clock::duration>(delayns);
std::cout << "delay = " << delayns.count() << " nanoseconds which is " << delay.count() << " cycles\n";
clock::time_point start = clock::now();
clock::time_point stop = start + delay;
while (clock::now() < stop) ;
clock::time_point end = clock::now();
clock::duration elapsed = end - start;
std::cout << "paused " << elapsed.count() << " cycles ";
std::cout << "which is " << duration_cast<nanoseconds>(elapsed).count() << " nanoseconds\n";
}
{
typedef approx_cycle_count<1500> clock;
std::cout << "\nSimulated " << clock::frequency / 1000000 << "MHz clock modeled with nanoseconds\n";
clock::duration delay = nanoseconds(500);
std::cout << "delay = " << delay.count() << " nanoseconds\n";
clock::time_point start = clock::now();
clock::time_point stop = start + delay;
while (clock::now() < stop) ;
clock::time_point end = clock::now();
clock::duration elapsed = end - start;
std::cout << "paused " << elapsed.count() << " nanoseconds\n";
}
}
int main()
{
cycle_count_delay();
return 0;
}