 "C" + ddr_idx + "_MEM_ADDR_MAP";
	std::string mem_map = model_param_props.getString(property);
	if ("ROW_BANK_COLUMN" == mem_map) { //RBC
		column_addr_offset = 0;
		bank_group_offset = column_addr_offset + (column_addr_width - 3); //column_addr_width-3:: LSB 3 bits are always dedicated to column.
		bank_addr_offset = bank_group_offset + bank_group_addr_width; //column_addr_width-3:: LSB 3 bits are always dedicated to column.
		row_addr_offset = bank_addr_offset + bank_addr_width;
	}
	if ("BANK_ROW_COLUMN" == mem_map) { // BRC
		column_addr_offset = 0;
		row_addr_offset = column_addr_offset + (column_addr_width - 3);
		bank_group_offset = row_addr_offset + row_addr_width;
		bank_addr_offset = bank_group_offset + bank_group_addr_width; //column_addr_width-3:: LSB 3 bits are always dedicated to column.
	}
	if ("ROW_COLUMN_BANK" == mem_map) { //RCB
		bank_group_offset = 0;
		bank_addr_offset = bank_group_offset + bank_group_addr_width;
		column_addr_offset = bank_addr_offset + bank_addr_width;
		row_addr_offset = column_addr_offset + (column_addr_width - 3);
	}

#ifdef DEBUG_LATENCY
	if ("ROW_BANK_COLUMN" == mem_map) { //RBC
		cout << "RBC" << endl;
	}
	if ("BANK_ROW_COLUMN" == mem_map) { // BRC
		cout << "BRC" << endl;
	}
	if ("ROW_COLUMN_BANK" == mem_map) { //RCB
		cout << "RCB" << endl;
	}
	cout << "row_addr_width:" << row_addr_width << endl;
	cout << "column_addr_width:" << column_addr_width << endl;
	cout << "bank_addr_width:" << bank_addr_width << endl;
	cout << "bank_group_addr_width:" << bank_group_addr_width << endl;
	cout << "column_addr_offset:" << column_addr_offset << endl;
	cout << "bank_addr_offset:" << bank_addr_offset << endl;
	cout << "bank_group_addr_offset:" << bank_group_offset << endl;
	cout << "row_addr_offset:" << row_addr_offset << endl;
#endif

	property = "C" + ddr_idx + "_TIMEPERIOD_PS";
	ddr_timeperiod = sc_core::sc_time(model_param_props.getLongLong(property),
			sc_core::SC_PS);

	SC_METHOD(spendcmd2cmd_delay);
	sensitive << cmd2cmd_event;
	dont_initialize();

	SC_METHOD(spendtx_delay);
	sensitive << tx_latency_event;
	dont_initialize();

}

app_ddrx_xtlm::~app_ddrx_xtlm() {
	int n_fsm = 1 << bank_group_addr_width;
	for (int i = 0; i < n_fsm; i++) {
		delete fsm_instance_arr[i];
	}

}

tlm::tlm_sync_enum app_ddrx_xtlm::s_axi_wr_tlm_saxi_nb_transport_fw(
		xtlm::aximm_payload & trans, tlm::tlm_phase & phase,
		sc_core::sc_time & t) {
#ifdef DEBUGMSG
	myfile_app_ddrx << " (APP_DDRX) WR_TX :: AAT-->APP_DDRX NB_FW  @"
	<< sc_time_stamp();
	myfile_app_ddrx << "\n (APP_DDRX) addr :: " << hex << trans.get_address();
	myfile_app_ddrx << "\n (APP_DDRX) phase :: " << phase << std::endl
	<< std::endl;
#endif
#ifdef DEBUGMSG
	cout << "app_ddrx_xtlm::s_axi_wr_tlm_saxi_nb_transport_fw write address: "
	<< trans.get_address() << endl;
#endif
	uint64_t local_offset = trans.get_address();
	local_offset = local_offset >> 3;
	unsigned int curr_bank_grp = get_bank_group_addr(local_offset);

#ifdef DEBUGMSG
	unsigned int curr_bank_address = get_bank_addr(local_offset);
	cout << "app_ddrx_xtlm::write_req row" << get_row_addr(local_offset)
	<< " column:" << get_column_addr(local_offset) << " bank_group: "
	<< curr_bank_grp << " bank:" << (curr_bank_address >> 2) << endl;
#endif

	//checking if particular FSM is full
	//TODO::There are two stage FIFO in each FSM. Stage 1 FIFO has depth of 4 but only tx at the top will be parallised
	//Stage 2 has only 1 depth. Command from stage 1 move to Stage 2 once Stage 2 is free
	bool queue_full = false;
	if (fsm_q_cnt[curr_bank_grp] > 4) {
		queue_full = true;
	}

	if (queue_full) {
#ifdef DEBUGMSG
		cout << "app_ddrx_xtlm::write fifo full back pressure "
		<< trans.get_address() << endl;
#endif
		if (req_queue.empty()) {
			cmd2cmd_event.notify(
					calculate_cmd2cmd_delay(&trans) * ddr_timeperiod);
		}
		phase = xtlm::BEGIN_WADDR;
		req_queue.push(&trans);
		fsm_q_cnt[curr_bank_grp]++;
		return tlm::TLM_ACCEPTED;
	} else {
#ifdef DEBUGMSG
		cout << "app_ddrx_xtlm::write fifo not full accepted "
		<< trans.get_address() << endl;
#endif
		if (req_queue.empty()) {
			cmd2cmd_event.notify(
					calculate_cmd2cmd_delay(&trans) * ddr_timeperiod);
		}
		phase = xtlm::END_WADDR;
		req_queue.push(&trans);
		fsm_q_cnt[curr_bank_grp]++;
		trans.set_response_status(xtlm::XTLM_OK_RESPONSE);
		return tlm::TLM_UPDATED;
	}

}

tlm::tlm_sync_enum app_ddrx_xtlm::s_axi_rd_tlm_saxi_nb_transport_fw(
		xtlm::aximm_payload & trans, tlm::tlm_phase & phase,
		sc_core::sc_time & t) {
#ifdef DEBUGMSG
	myfile_app_ddrx << " (APP_DDRX) RD_TX :: AAT-->APP_DDRX NB_FW  @"
	<< sc_time_stamp();
	myfile_app_ddrx << "\n (APP_DDRX) addr :: " << hex << trans.get_address();
	myfile_app_ddrx << "\n (APP_DDRX) phase :: " << phase << std::endl
	<< std::endl;
#endif
#ifdef DEBUGMSG
	cout << "app_ddrx_xtlm::fw s_axi_rd_tlm_saxi_nb_transport_fw address : "
	<< trans.get_address() << endl;
#endif
	uint64_t local_offset = trans.get_address();
	local_offset = local_offset >> 3;
	unsigned int curr_bank_grp = get_bank_group_addr(local_offset);

#ifdef DEBUGMSG
	unsigned int curr_bank_address = get_bank_addr(local_offset);
	cout << "app_ddrx_xtlm::write_req row" << get_row_addr(local_offset)
	<< " column:" << get_column_addr(local_offset) << " bank_group: "
	<< curr_bank_grp << " bank:" << (curr_bank_address >> 2) << endl;
#endif

	bool queue_full = false;

	if (fsm_q_cnt[curr_bank_grp] > 4) {
		queue_full = true;
	}

	if (queue_full) {
#ifdef DEBUGMSG
		cout << "app_ddrx_xtlm::read fifo full back pressure "
		<< trans.get_address() << endl;
		cout << "req_queue size " << req_queue.size() << std::endl;
#endif
		phase = xtlm::BEGIN_RADDR;
		if (req_queue.empty()) {
			cmd2cmd_event.notify(
					calculate_cmd2cmd_delay(&trans) * ddr_timeperiod);
		}
		req_queue.push(&trans);
		fsm_q_cnt[curr_bank_grp]++;
		return tlm::TLM_ACCEPTED;
	} else {
#ifdef DEBUGMSG
		cout << "app_ddrx_xtlm::read fifo not full accepted "
		<< trans.get_address() << endl;
		cout << "req_queue size " << req_queue.size() << std::endl;
#endif
		if (req_queue.empty()) {
			cmd2cmd_event.notify(
					calculate_cmd2cmd_delay(&trans) * ddr_timeperiod);
		}
		phase = xtlm::END_RADDR;
		req_queue.push(&trans);
		trans.set_response_status(xtlm::XTLM_OK_RESPONSE);
		fsm_q_cnt[curr_bank_grp]++;
		return tlm::TLM_UPDATED;
	}

}

unsigned int app_ddrx_xtlm::cal_write_latency(xtlm::aximm_payload & trans) {
	//TODO:: value of burst_length and data_width is hardcoded according to
	//alphadata board configuration
	unsigned int data_width = 72;
	unsigned int burst_length = 8;
	prev_row_address = curr_row_address;
	prev_column_address = curr_column_address;
	prev_bank_address = curr_bank_address;
	prev_bank_group = curr_bank_group;

	uint64_t local_offset = trans.get_address();
	local_offset = local_offset >> 3;
	curr_row_address = get_row_addr(local_offset);
	curr_column_address = get_column_addr(local_offset);
	curr_bank_address = get_bank_addr(local_offset);
	curr_bank_group = get_bank_group_addr(local_offset);

	if (prev_row_address == (-1)) {
		prev_row_address = curr_row_address;
		prev_column_address = curr_column_address;
		prev_bank_address = curr_bank_address;
		prev_bank_group = curr_bank_group;
	}
	if (enable_latency == false) {
		fsm_instance_arr[curr_bank_group]->m_latencyCnt = 1;
	} else {
		fsm_instance_arr[curr_bank_group]->fsm_write(curr_row_address,
				curr_column_address, curr_bank_address, curr_bank_group,
				prev_bank_group);
	}
#ifdef DEBUGMSG
	cout << "write fsm_instance_arr[" << curr_bank_group << "]->m_latencyCnt :="
	<< fsm_instance_arr[curr_bank_group]->m_latencyCnt << endl;
#endif
	return fsm_instance_arr[curr_bank_group]->m_latencyCnt;
}

unsigned int app_ddrx_xtlm::cal_read_latency(xtlm::aximm_payload & trans) {
	//TODO:: value of burst_length and data_width is hardcoded according to
	//alphadata board configuration
	unsigned int data_width = 72;
	unsigned int burst_length = 8;
	prev_row_address = curr_row_address;
	prev_column_address = curr_column_address;
	prev_bank_address = curr_bank_address;
	prev_bank_group = curr_bank_group;

	uint64_t local_offset = trans.get_address();
	local_offset = local_offset >> 3;
	curr_row_address = get_row_addr(local_offset);
	curr_column_address = get_column_addr(local_offset);
	curr_bank_address = get_bank_addr(local_offset);
	curr_bank_group = get_bank_group_addr(local_offset);

	if (prev_row_address == (-1)) {
		prev_row_address = curr_row_address;
		prev_column_address = curr_column_address;
		prev_bank_address = curr_bank_address;
		prev_bank_group = curr_bank_group;
	}
	if (enable_latency == false) {
		fsm_instance_arr[curr_bank_group]->m_latencyCnt = 1;
	} else {
		fsm_instance_arr[curr_bank_group]->fsm_read(curr_row_address,
				curr_column_address, curr_bank_address, curr_bank_group,
				prev_bank_group);
	}
#ifdef DEBUGMSG
	cout << "read fsm_instance_arr[" << curr_bank_group << "]->m_latencyCnt :="
	<< fsm_instance_arr[curr_bank_group]->m_latencyCnt << endl;
#endif
	return fsm_instance_arr[curr_bank_group]->m_latencyCnt;
}

tlm::tlm_sync_enum app_ddrx_xtlm::nb_transport_fw(xtlm::aximm_payload & trans,
		tlm::tlm_phase & phase, sc_core::sc_time & t) {
#ifdef DEBUGMSG
	if ((phase == xtlm::END_RADDR) || (phase == xtlm::BEGIN_RDATA)
			|| (phase == xtlm::END_WADDR) || (phase == xtlm::BEGIN_WRESP)) {
		std::cerr << "Illegal transaction phase received by target." << endl;
	}
#endif
	if (trans.get_command() == xtlm::XTLM_WRITE_COMMAND) {
		return s_axi_wr_tlm_saxi_nb_transport_fw(trans, phase, t);
	} else if (trans.get_command() == xtlm::XTLM_READ_COMMAND) {
		return s_axi_rd_tlm_saxi_nb_transport_fw(trans, phase, t);
	}
	return tlm::TLM_ACCEPTED;
}

void app_ddrx_xtlm::spendcmd2cmd_delay() {
	xtlm::aximm_payload* trans = req_queue.front();
	req_queue.pop();
	unsigned int fsm_latency;
	if (trans->get_command() == xtlm::XTLM_WRITE_COMMAND) {
		if (trans->get_data_length() < partial_data_length) {
			fsm_latency = cal_read_latency(*trans);
		} else {
			fsm_latency = 0;
		}

		fsm_latency += cal_write_latency(*trans);
	} else if (trans->get_command() == xtlm::XTLM_READ_COMMAND) {
		fsm_latency = cal_read_latency(*trans);
	}
#ifdef DEBUG_LATENCY
	cout << name() << " setting fsm_latency:" << fsm_latency << endl;
#endif
	rsp_queue.push_back(trans);
	tx_latency_event.notify(fsm_latency * ddr_timeperiod);

	if (req_queue.empty()) {
		return;
	}
	cmd2cmd_event.notify(calculate_cmd2cmd_delay(trans) * ddr_timeperiod);

}

void app_ddrx_xtlm::spendtx_delay() {
	xtlm::aximm_payload* trans = rsp_queue.front();

	rsp_queue.pop_front();

	uint64_t local_offset = trans->get_address();
	local_offset = local_offset >> 3;
	int curr_bg = get_bank_group_addr(local_offset);
	fsm_q_cnt[curr_bg]--;

	tlm::tlm_phase phase;
	sc_core::sc_time t = sc_core::SC_ZERO_TIME;
	if (trans->get_command() == xtlm::XTLM_WRITE_COMMAND) {
		if (NULL != trans) {
			phase = xtlm::BEGIN_WRESP;
			trans->set_response_status(xtlm::XTLM_OK_RESPONSE);
#ifdef DEBUGMSG
			cout
			<< "app_ddrx_xtlm::write making bw call END_RESP address:"
			<< (trans->get_address()) << endl;
#endif
#ifdef DEBUGMSG
			myfile_app_ddrx
			<< " (APP_DDRX) WR_TX :: AAT<--APP_DDRX NB_BW  @"
			<< sc_time_stamp();
			myfile_app_ddrx << "\n (APP_DDRX) addr :: " << hex
			<< trans->get_address();
			myfile_app_ddrx << "\n (APP_DDRX) phase :: " << phase
			<< std::endl << std::endl;
#endif
			tlm::tlm_sync_enum status = (*wr_skt)->nb_transport_bw(*trans,
					phase, t);

		}
	} else if (trans->get_command() == xtlm::XTLM_READ_COMMAND) {
		if (NULL != trans) {
			phase = xtlm::BEGIN_RDATA;
			trans->set_response_status(xtlm::XTLM_OK_RESPONSE);
#ifdef DEBUGMSG
			cout
			<< "app_ddrx_xtlm::read making bw call END_RESP address:"
			<< (trans->get_address()) << endl;
#endif
#ifdef DEBUGMSG
			myfile_app_ddrx
			<< " (APP_DDRX) RD_TX :: AAT<--APP_DDRX NB_BW  @"
			<< sc_time_stamp();
			myfile_app_ddrx << "\n (APP_DDRX) addr :: " << hex
			<< trans->get_address();
			myfile_app_ddrx << "\n (APP_DDRX) phase :: " << phase
			<< std::endl << std::endl;
#endif
			tlm::tlm_sync_enum status = (*rd_skt)->nb_transport_bw(*trans,
					phase, t);
		}
	}
}

unsigned int app_ddrx_xtlm::calculate_cmd2cmd_delay(
		xtlm::aximm_payload* trans) {
	unsigned int cmd2cmd_latency = 1;
	if (enable_latency == false) {
		cmd2cmd_latency = 1;
	} else if ((-1) == prev_bank_group) {
		cmd2cmd_latency = 0;
	} else {
		if (curr_bank_group == prev_bank_group) {
			cmd2cmd_latency = 11;
		} else {
			cmd2cmd_latency = 4;
		}
	}
#ifdef DEBUG_LATENCY
	cout << name() << " setting cmd2cmd_latency:" << cmd2cmd_latency
	<< endl;
#endif
	return cmd2cmd_latency;
}

}
}


