ICP  1
vme_simulation.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
3 #include "vme_simulation.h"
4 #include "sim_environment_card.h"
5 #include "sim_detector_card.h"
7 //#include "sim_period_card.h"
8 #include "sim_env_period_card.h"
9 #include "icputils.h"
10 
11 
12 // Simulate a DAE with NSIMDETCARDS detector card (64Mb, position 1 to NSIMDETCARDS)
13 // 1 Environment card (position 0) and 1 period card (position NSIMDETCARD+1)
14 
15 //int VMESimulation::zeroDetectorCardPeriod()
16 //{
17 // int k;
18 // for(k=1; k<=NSIMDETCARDS; k++)
19 // {
20 // m_periodCounterLow[k] = m_periodCounterHigh[k] = 0;
21 // }
22 // return 0;
23 //
24 
25 //int VMESimulation::incrementDetectorCardPeriod(int period)
26 //{
27 // int k;
28 // for(k=1; k<=NSIMDETCARDS; k++)
29 // {
30 // increment2(1, m_periodCounterLow[k], m_periodCounterHigh[k]);
31 // }
32 // return 0;
33 //}
34 
35 template <class DAEPolicy>
36 VMESimulation<DAEPolicy>::VMESimulation(DAEstatus& status) : VMESimulationIface(), m_shutdown_requested(false), m_trigger_func(NULL), m_trigger_func_arg(NULL)
37 {
38  int i;
39  unsigned long ul;
40  status.addInfo(FAC_SIMDAE, "Creating Simulated VME DAE");
41  InitializeCriticalSection(&m_critical);
42  srand((unsigned)time(NULL));
43 
44  m_sim_vme.push_back(this);
45  int crate_number = m_sim_vme.size() - 1;
46  if (crate_number == 0) // only environment card on first DAE
47  {
48 // m_card_list.push_back(new SIMEnvCard(0, this, Working, status));
49  m_env_card_list.push_back(new MySIMEnvPeriodCard(0, this, Working, status));
50  }
51 // m_card_list.push_back(new SIMPeriodCard(3, this, status));
52 
53  bool sharedDetMemory = Poco::Util::Application::instance().config().getBool("isisicp.simulation.detcards.shared", false);
54  std::string card_type = Poco::Util::Application::instance().config().getString("isisicp.simulation.detcards.type", "neutron");
55  int numDetCards = Poco::Util::Application::instance().config().getInt(Poco::format("isisicp.simulation.detcards.crate%d.number", crate_number), 0);
56  int numDataDaeDetCards = Poco::Util::Application::instance().config().getInt(Poco::format("isisicp.simulation.detcards.crate%d.datadae.number", crate_number), 0);
57  for(i=1; i<=numDetCards; i++)
58  {
59  if (card_type == "muon")
60  {
61  m_det_card_list.push_back(new SIMMuonDetectorCard<DAEPolicy::det_t>(i, this, sharedDetMemory, Working, status));
62  }
63  else
64  {
65  m_det_card_list.push_back(new SIMDetectorCard<DAEPolicy::det_t>(i, this, sharedDetMemory, Working, true, status));
66  }
67  }
68  for(i=1; i <= numDataDaeDetCards; i++)
69  {
70  m_det_card_list.push_back(new SIMDetectorCard<DAEPolicy::det_t>(i+numDetCards, this, sharedDetMemory, Working, false, status));
71  }
72 
73 // m_card_list.push_back(new SIMDetectorCard(3, this, true, /*DetectedButNot*/Working, status));
74  ul = _beginthread(simulation_routine, 0, (void*)this);
75  if (ul == -1)
76  {
77  status.add(FAC_SIMDAE, SEV_ERROR, ERRTYPE_INVCARD, "error creating simulation thread");
78  }
79 }
80 
81 template <class DAEPolicy>
82 int VMESimulation<DAEPolicy>::getCardIds(std::vector<int>& card_ids)
83 {
84  card_ids.clear();
85  for(int i=0; i<m_env_card_list.size(); ++i)
86  {
87  card_ids.push_back(m_env_card_list[i]->position());
88  }
89  for(int i=0; i<m_det_card_list.size(); ++i)
90  {
91  card_ids.push_back(m_det_card_list[i]->position());
92  }
93 // typedef void (std::vector<int>::*push_back_t)(const int&); // we need this because push_back is overloaded and hence ambiguous
94 // std::for_each(m_env_card_list.begin(), m_env_card_list.end(),
95 // boost::bind( (push_back_t)&std::vector<int>::push_back, boost::ref(card_ids), boost::bind(&MySIMDAE2Card::position, _1) )
96 // );
97 // std::for_each(m_det_card_list.begin(), m_det_card_list.end(),
98 // boost::bind( (push_back_t)&std::vector<int>::push_back, boost::ref(card_ids), boost::bind(&MySIMDAE2Card::position, _1) )
99 // );
100  return 0;
101 }
102 
104 {
106  dae->simulation_routineImpl();
107 }
108 
109 template <class DAEPolicy>
111 {
112  int i;
113  int delay = 20; // in milliseconds, 20ms gives us 50Hz running
114  Sleep(2000); // to let other things complete
116  ICPCritical cs(&(dae->m_critical), false);
117  struct timeb tb;
118  while(!dae->shutdownRequested())
119  {
120  Sleep(delay);
121  cs.lock();
122  for(i=0; i< dae->m_det_card_list.size(); ++i)
123  {
124  dae->m_det_card_list[i]->simulate(delay);
125  }
126  // do env card last so we get a frame number of zero for event mode
127  if (dae->m_env_card_list.size() > 0)
128  {
129  dae->m_env_card_list[0]->simulate(delay);
130  }
131  ftime(&tb);
132  // should really call this 50 times
133  dae->onTrigger(tb.time, tb.millitm, 8); // 8 is frame sync, 9 is run
134  cs.unlock();
135  }
136 }
137 
138 template <class DAEPolicy>
139 int VMESimulation<DAEPolicy>::onTrigger(time_t the_secs, unsigned short the_ms, int trig_id)
140 {
141  if (m_trigger_func != NULL)
142  {
143  return (*m_trigger_func)(m_trigger_func_arg, the_secs, the_ms, trig_id);
144  }
145  else
146  {
147  return ISISVME::Success;
148  }
149 }
150 
151 template <class DAEPolicy>
153 {
154  int i;
155  m_shutdown_requested = true;
156  Sleep(300);
157  for(i=0; i<m_det_card_list.size(); i++)
158  {
159  delete m_det_card_list[i];
160  m_det_card_list[i] = 0;
161  }
162  for(i=0; i<m_env_card_list.size(); i++)
163  {
164  delete m_env_card_list[i];
165  m_env_card_list[i] = 0;
166  }
167  DeleteCriticalSection(&m_critical);
168 }
169 
170 template <class DAEPolicy>
172 {
173  int i;
174  ICPCritical cs(&m_critical);
175  for(i=0; i< m_det_card_list.size(); i++)
176  {
177  if (m_det_card_list[i]->type() == MySIMDAE2Card::DetCard)
178  {
179  ((MySIMDetectorCard*)m_det_card_list[i])->incrementPeriod();
180  }
181  }
182  return 0;
183 }
184 
185 template <class DAEPolicy>
187 {
188  int i;
189  ICPCritical cs(&m_critical);
190  for(i=0; i< m_det_card_list.size(); i++)
191  {
192  if (m_det_card_list[i]->type() == MySIMDAE2Card::DetCard)
193  {
194  ((MySIMDetectorCard*)m_det_card_list[i])->resetPeriod();
195  }
196  }
197  return 0;
198 }
199 
200 
201 template <class DAEPolicy>
203 {
204  return true;
205 }
206 
207 template <class DAEPolicy>
208 int VMESimulation<DAEPolicy>::open(const char* device, DAEstatus& status)
209 {
210  status.addInfoVa(FAC_SIMDAE, "Simulation open %s", device);
211  return ISISVME::Success;
212 }
213 
214 template <class DAEPolicy>
215 int VMESimulation<DAEPolicy>::openBackplane(const char* device_bp, DAEstatus& status)
216 {
217  status.addInfoVa(FAC_SIMDAE, "Simulation open backplane %s", device_bp);
218  return ISISVME::Success;
219 }
220 
221 template <class DAEPolicy>
223 {
224  return ISISVME::Success;
225 }
226 
227 template <class DAEPolicy>
229 {
230  return ISISVME::Success;
231 }
232 
233 template <class DAEPolicy>
234 int VMESimulation<DAEPolicy>::readU16(unsigned long address, isisU16_t* data16, TransferProps props, DAEstatus& status)
235 {
236  return simulateRead(props, address, data16, 1, status);
237 }
238 
239 template <class DAEPolicy>
240 int VMESimulation<DAEPolicy>::readU16noRetry(unsigned long address, isisU16_t* data16, TransferProps props, DAEstatus& status)
241 {
242  return readU16(address, data16, props, status);
243 }
244 
245 template <class DAEPolicy>
246 int VMESimulation<DAEPolicy>::readU32(unsigned long address, isisU32_t* data32, TransferProps props, DAEstatus& status)
247 {
248  return simulateRead(props, address, data32, 1, status);
249 }
250 
251 template <class DAEPolicy>
252 int VMESimulation<DAEPolicy>::readU32noRetry(unsigned long address, isisU32_t* data32, TransferProps props, DAEstatus& status)
253 {
254  return readU32(address, data32, props, status);
255 }
256 
257 // nove nitems of 2 bytes
258 template <class DAEPolicy>
259 int VMESimulation<DAEPolicy>::readBlockU16(unsigned long address, isisU16_t* data16,
260  long nitems, TransferProps props, DAEstatus& status)
261 {
262  return simulateRead(props, address, data16, nitems, status);
263 }
264 // nove nitems of 4 bytes
265 template <class DAEPolicy>
266 int VMESimulation<DAEPolicy>::readBlockU32(unsigned long address, isisU32_t* data32, long nitems, TransferProps props, DAEstatus& status)
267 {
268  return simulateRead(props, address, data32, nitems, status);
269 }
270 template <class DAEPolicy>
271 int VMESimulation<DAEPolicy>::writeU16(unsigned long address, isisU16_t data16, TransferProps props, DAEstatus& status)
272 {
273  return simulateWrite(props, address, &data16, 1, status);
274 }
275 template <class DAEPolicy>
276 int VMESimulation<DAEPolicy>::writeU32(unsigned long address, isisU32_t data32, TransferProps props, DAEstatus& status)
277 {
278  return simulateWrite(props, address, &data32, 1, status);
279 }
280 // nove nitems of 2 bytes
281 template <class DAEPolicy>
282 int VMESimulation<DAEPolicy>::writeBlockU16(unsigned long address, isisU16_t* data16, long nitems, TransferProps props, DAEstatus& status)
283 {
284  return simulateWrite(props, address, data16, nitems, status);
285 }
286 // nove nitems of 4 bytes
287 template <class DAEPolicy>
288 int VMESimulation<DAEPolicy>::writeBlockU32(unsigned long address, isisU32_t* data32, long nitems, TransferProps props, DAEstatus& status)
289 {
290  return simulateWrite(props, address, data32, nitems, status);
291 }
292 
293 template <class DAEPolicy>
295 {
296  status.addInfo(FAC_SIMDAE, "Simulate close");
297  return ISISVME::Success;
298 }
299 template <class DAEPolicy>
301 {
302  status.addInfo(FAC_SIMDAE, "Simulate scan bus");
303  return 3;
304 }
305 
306 template <class DAEPolicy>
308 {
309  status.addInfo(FAC_SIMDAE, "Simulate reset bus");
310  return true;
311 }
312 
313 template <class DAEPolicy>
314 int VMESimulation<DAEPolicy>::simulateRead(TransferProps props, unsigned long address, isisU16_t* data16, long nitems, DAEstatus& status)
315 {
316  int i, stat = ISISVME::Error;
317  ICPCritical cs(&m_critical);
318  for(i=0; (i<m_det_card_list.size()) && (stat == ISISVME::Error); i++)
319  {
320  stat = m_det_card_list[i]->read16(props, address, data16, nitems, status);
321  if (stat == ISISVME::Success)
322  {
323  m_det_card_list[i]->updateCardAfterRead(status);
324  }
325  }
326  for(i=0; (i<m_env_card_list.size()) && (stat == ISISVME::Error); i++)
327  {
328  stat = m_env_card_list[i]->read16(props, address, data16, nitems, status);
329  if (stat == ISISVME::Success)
330  {
331  m_env_card_list[i]->updateCardAfterRead(status);
332  }
333  }
334  if (stat == ISISVME::Error)
335  {
336  std::ostringstream message;
337  message << "Simulation error (read16) at " << std::hex << address << std::dec << std::endl;
338  status.add(FAC_SIMDAE, SEV_ERROR, ERRTYPE_INVCARD, message.str());
339  }
340  return stat;
341 }
342 
343 template <class DAEPolicy>
344 int VMESimulation<DAEPolicy>::simulateWrite(TransferProps props, unsigned long address, isisU16_t* data16, long nitems, DAEstatus& status)
345 {
346  int i, stat = ISISVME::Error;
347  ICPCritical cs(&m_critical);
348  for(i=0; (i<m_det_card_list.size()) && (stat == ISISVME::Error); i++)
349  {
350  stat = m_det_card_list[i]->write16(props, address, data16, nitems, status);
351  if (stat == ISISVME::Success)
352  {
353  m_det_card_list[i]->updateCardAfterWrite(status);
354  }
355  }
356  for(i=0; (i<m_env_card_list.size()) && (stat == ISISVME::Error); i++)
357  {
358  stat = m_env_card_list[i]->write16(props, address, data16, nitems, status);
359  if (stat == ISISVME::Success)
360  {
361  m_env_card_list[i]->updateCardAfterWrite(status);
362  }
363  }
364  if (stat == ISISVME::Error)
365  {
366  std::ostringstream message;
367  message << "Simulation error (write16) at " << std::hex << address << std::dec << std::endl;
368  status.add(FAC_SIMDAE, SEV_ERROR, ERRTYPE_INVCARD, message.str());
369  }
370  return stat;
371 }
372 
374 {
375  bool res = false;
376  for(int i=0; i<m_sim_vme.size(); i++)
377  {
378  res |= m_sim_vme[i]->isCrateVetoing();
379  }
380  return res;
381 }
382 
383 template <class DAEPolicy>
385 {
386  bool res = false;
387  for(int i=0; i<m_det_card_list.size(); i++)
388  {
389  res |= m_det_card_list[i]->isVetoing();
390  }
391  for(int i=0; i<m_env_card_list.size(); i++)
392  {
393  res |= m_env_card_list[i]->isVetoing();
394  }
395  return res;
396 }
397 
399 {
400  bool res = true;
401  for(int i=0; i<m_sim_vme.size(); i++)
402  {
403  res &= m_sim_vme[i]->isCrateRunning();
404  }
405  return res;
406 }
407 
409 {
410  int res = -1;
411  for(int i=0; i<m_sim_vme.size(); ++i)
412  {
413  if ( (res = m_sim_vme[i]->frameNumberImpl()) != -1 )
414  {
415  break;
416  }
417  }
418  return res;
419 }
420 
421 template <class DAEPolicy>
423 {
424  int res = -1;
425  for(int i=0; i<m_env_card_list.size(); i++)
426  {
427  if (m_env_card_list[i]->type() == MySIMDAE2Card::EnvCard)
428  {
429  res = (dynamic_cast<MySIMEnvCard*>(m_env_card_list[i]))->frameNumber();
430  }
431  if (m_env_card_list[i]->type() == MySIMDAE2Card::EnvPeriodCard)
432  {
433  res = (dynamic_cast<MySIMEnvPeriodCard*>(m_env_card_list[i]))->frameNumber();
434  }
435  }
436  return res;
437 }
438 
439 template <class DAEPolicy>
441 {
442  bool res = true;
443  for(int i=0; i<m_env_card_list.size(); i++)
444  {
445  if (m_env_card_list[i]->type() == MySIMDAE2Card::EnvCard)
446  {
447  res &= ((MySIMEnvCard*)m_env_card_list[i])->isRunning();
448  }
449  if (m_env_card_list[i]->type() == MySIMDAE2Card::EnvPeriodCard)
450  {
451  res &= ((MySIMEnvPeriodCard*)m_env_card_list[i])->isRunning();
452  }
453  }
454  return res;
455 }
456 
457 template <class DAEPolicy>
458 int VMESimulation<DAEPolicy>::simulateRead(TransferProps props, unsigned long address, isisU32_t* data32, long nitems, DAEstatus& status)
459 {
460  int i, stat = ISISVME::Error;
461  ICPCritical cs(&m_critical);
462  for(i=0; (i<m_det_card_list.size()) && (stat == ISISVME::Error); i++)
463  {
464  stat = m_det_card_list[i]->read32(props, address, data32, nitems, status);
465  if (stat == ISISVME::Success)
466  {
467  m_det_card_list[i]->updateCardAfterRead(status);
468  }
469  }
470  for(i=0; (i<m_env_card_list.size()) && (stat == ISISVME::Error); i++)
471  {
472  stat = m_env_card_list[i]->read32(props, address, data32, nitems, status);
473  if (stat == ISISVME::Success)
474  {
475  m_env_card_list[i]->updateCardAfterRead(status);
476  }
477  }
478  if (stat == ISISVME::Error)
479  {
480  std::ostringstream message;
481  message << "Simulation error (read32) at " << std::hex << address << std::dec << std::endl;
482  status.add(FAC_SIMDAE, SEV_ERROR, ERRTYPE_INVCARD, message.str());
483  }
484  return stat;
485 }
486 
487 template <class DAEPolicy>
488 int VMESimulation<DAEPolicy>::simulateRead(TransferProps props, unsigned long address, uint64_t* data64, long nitems, DAEstatus& status)
489 {
490  return simulateRead(props, address, reinterpret_cast<isisU32_t*>(data64), 2 * nitems, status);
491 }
492 
493 
494 template <class DAEPolicy>
495 int VMESimulation<DAEPolicy>::simulateWrite(TransferProps props, unsigned long address, isisU32_t* data32, long nitems, DAEstatus& status)
496 {
497  int i, stat = ISISVME::Error;
498  ICPCritical cs(&m_critical);
499  for(i=0; (i<m_det_card_list.size()) && (stat == ISISVME::Error); i++)
500  {
501  stat = m_det_card_list[i]->write32(props, address, data32, nitems, status);
502  if (stat == ISISVME::Success)
503  {
504  m_det_card_list[i]->updateCardAfterWrite(status);
505  }
506  }
507  for(i=0; (i<m_env_card_list.size()) && (stat == ISISVME::Error); i++)
508  {
509  stat = m_env_card_list[i]->write32(props, address, data32, nitems, status);
510  if (stat == ISISVME::Success)
511  {
512  m_env_card_list[i]->updateCardAfterWrite(status);
513  }
514  }
515  if (stat == ISISVME::Error)
516  {
517  std::ostringstream message;
518  message << "Simulation error (write32) at " << std::hex << address << std::dec << std::endl;
519  status.add(FAC_SIMDAE, SEV_ERROR, ERRTYPE_INVCARD, message.str());
520  }
521  return stat;
522 }
523 
524 template <class DAEPolicy>
525 int VMESimulation<DAEPolicy>::simulateWrite(TransferProps props, unsigned long address, uint64_t* data64, long nitems, DAEstatus& status)
526 {
527  return simulateWrite(props, address, reinterpret_cast<isisU32_t*>(data64), 2 * nitems, status);
528 }
529 
530 template <class DAEPolicy>
531 int VMESimulation<DAEPolicy>::writeBlock(unsigned long address, isisU32_t* data, long nitems, TransferProps props, DAEstatus& status)
532 {
533  return simulateWrite(props, address, data, nitems, status);
534 }
535 
536 template <class DAEPolicy>
537 int VMESimulation<DAEPolicy>::writeBlock(unsigned long address, uint64_t* data, long nitems, TransferProps props, DAEstatus& status)
538 {
539  return simulateWrite(props, address, data, nitems, status);
540 }
541 
542 template <class DAEPolicy>
543 int VMESimulation<DAEPolicy>::readBlock(unsigned long address, isisU32_t* data, long nitems, TransferProps props, DAEstatus& status)
544 {
545  return simulateRead(props, address, data, nitems, status);
546 }
547 
548 template <class DAEPolicy>
549 int VMESimulation<DAEPolicy>::readBlock(unsigned long address, uint64_t* data, long nitems, TransferProps props, DAEstatus& status)
550 {
551  return simulateRead(props, address, data, nitems, status);
552 }
553 
554 std::vector<VMESimulationIface*> VMESimulationIface::m_sim_vme;
555 
556 template class VMESimulation<DAE2Policy>;
557 template class VMESimulation<DAE3Policy>;
std::vector< MySIMEnvPeriodCard * > m_env_card_list
uint32_t TransferProps
combination of TransferProp values
Definition: isisvme.h:16
int writeBlockU16(unsigned long address, isisU16_t *data16, long nitems, TransferProps props, DAEstatus &status)
virtual ~VMESimulation()
int readU16(unsigned long address, isisU16_t *data16, TransferProps props, DAEstatus &status)
int writeU32(unsigned long address, isisU32_t data32, TransferProps props, DAEstatus &status)
int readU32noRetry(unsigned long address, isisU32_t *data32, TransferProps props, DAEstatus &dstatus)
int writeU16(unsigned long address, isisU16_t data16, TransferProps props, DAEstatus &status)
unsigned long isisU32_t
Definition: isisvme_types.h:8
int add(DAEstatus &dstatus, bool clear)
Definition: DAEstatus.cpp:131
static int simulateWrite(SIMDAE2Card< DAECardPolicy > *d2card, ISISVME::TransferProps props, unsigned long address, __type *data, long nitems, DAEstatus &status)
#define FAC_SIMDAE
static int frameNumber()
static ISISDAE * dae
Definition: daeset.cpp:7
int getCardIds(std::vector< int > &card_ids)
int writeBlockU32(unsigned long address, isisU32_t *data32, long nitems, TransferProps props, DAEstatus &status)
void simulation_routineImpl()
int lockDevice(int timeout, DAEstatus &status)
int resetDetectorCardPeriod()
#define SEV_ERROR
int readBlockU16(unsigned long address, isisU16_t *data16, long nitems, TransferProps props, DAEstatus &status)
VMESimulation(DAEstatus &status)
static bool isRunning()
bool shutdownRequested()
unsigned short isisU16_t
Definition: isisvme_types.h:7
static void simulation_routine(void *arg)
int readU32(unsigned long address, isisU32_t *data32, TransferProps props, DAEstatus &status)
virtual int frameNumberImpl()=0
bool resetBus(DAEstatus &status)
void unlock()
Definition: icputils.h:354
int open(const char *device, DAEstatus &status)
static bool isVetoing()
int writeBlock(unsigned long address, isisU32_t *data, long nitems, TransferProps props, DAEstatus &status)
static int simulateRead(SIMDAE2Card< DAECardPolicy > *d2card, ISISVME::TransferProps props, unsigned long address, __type *data, long nitems, DAEstatus &status)
SIMEnvPeriodCard< typename DAEPolicy::env_t > MySIMEnvPeriodCard
void lock()
Definition: icputils.h:349
int incrementDetectorCardPeriod()
#define ERRTYPE_INVCARD
int readBlockU32(unsigned long address, isisU32_t *data32, long nitems, TransferProps props, DAEstatus &status)
std::vector< MySIMDetectorCard * > m_det_card_list
int unlockDevice(DAEstatus &status)
int readBlock(unsigned long address, isisU32_t *data, long nitems, TransferProps props, DAEstatus &status)
int scanBus(DAEstatus &status)
int addInfo(int facility, const std::string &text)
Definition: DAEstatus.cpp:86
CRITICAL_SECTION m_critical
int close(DAEstatus &status)
virtual void simulation_routineImpl()=0
int simulateRead(TransferProps props, unsigned long address, isisU16_t *data16, long nitems, DAEstatus &status)
int addInfoVa(int facility, const char *format,...)
Definition: DAEstatus.cpp:91
int onTrigger(time_t the_secs, unsigned short the_ms, int trig_id)
int simulateWrite(TransferProps props, unsigned long address, isisU16_t *data16, long nitems, DAEstatus &status)
int openBackplane(const char *device_bp, DAEstatus &status)
static std::vector< VMESimulationIface * > m_sim_vme
int readU16noRetry(unsigned long address, isisU16_t *data16, TransferProps props, DAEstatus &dstatus)