// Filename : simulation_timer.cpp
//
// This file was initially generated by SpaceStudio but is meant to be modified
// by the user.
#include "simulation_timer.h"
#include "spacecomp_pre_cpp.h"

// Do NOT move macros, functions and includes beginning with "spacecomp".

simulation_timer::simulation_timer(SPACECOMP_CTOR_PARAMS(simulation_timer, INDEX))
	: sdevice(SPACECOMP_SDBASE_ARGS(simulation_timer, INDEX))
	, SPACECOMP_START_ILIST(simulation_timer, INDEX)
	// Other initializers may go here
{ }

simulation_timer::~simulation_timer() {}

/**
 * Called on slave access.
 *
 * @param trans TLM 2.0 generic payload.
 * @param delay TLM 2.0 delay parameter.
 */
void simulation_timer::access(tlm::tlm_generic_payload& trans, sc_core::sc_time& delay) {
	// reached offset
	unsigned long offset = get_offset(trans);

	if (trans.is_read()) {
		unsigned long* data = (unsigned long*)trans.get_data_ptr();
		unsigned long long time = sc_core::sc_time_stamp().value();
		float time_d = sc_core::sc_time_stamp().to_seconds();

		if(offset == SIMULATION_TIME_LOW) {
			*data = (unsigned long)time;
			trans.set_response_status(tlm::TLM_OK_RESPONSE);
		} else if(offset == SIMULATION_TIME_HIGH) {
			*data = (unsigned long)(time >> 32);
			trans.set_response_status(tlm::TLM_OK_RESPONSE);
		} else if (offset == SIMULATION_TIME_FLOAT) {
			*data = *(unsigned long*)&time_d;
			trans.set_response_status(tlm::TLM_OK_RESPONSE);
		} else {
			SpacePrint("Error. Simulation timer received an invalid read request to register address %#X\n", offset);
			trans.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
		}
	}
	else
		SpacePrint("writing offset %#X @[%f s]\n", offset, sc_core::sc_time_stamp().to_seconds());

	trans.set_response_status(tlm::TLM_OK_RESPONSE);	// don't forget to set the TLM 2.0 response status
	delay += get_clock_period();	// simulate device access delay
}

#include "spacecomp_post_cpp.h"
