/* $Id: bunchLengthSim.c,v 1.2 2013/02/28 02:02:33 gwbrown Exp $ */ #include #include #include #include #include /* for ntohs & friends */ volatile int kmax = 0; volatile int blmSimMax = 1000; volatile int blmSimMod = 0; volatile int blmSimInc = 1; static void modCalc() { if ( blmSimInc ) { blmSimMod += blmSimInc; if ( blmSimMod > blmSimMax ) { blmSimMod = 0; } } else { blmSimMod = 0; } } static inline int isBigEndian() { union { uint8_t x[2]; uint16_t tst; } endian = { x : {0xBE, 0} }; return(endian.tst == 0xBE00); } static inline int16_t swap(int16_t x) { return x<<8 | ((uint16_t)x)>>8; } static inline int32_t swapl(int32_t x) { uint32_t v = (uint32_t)x; v = ((v & 0x00ff00ff) << 8) | ((v>>8) & 0x00ff00ff); v = (v >> 16) | (v<<16); return (int32_t) v; } static void * blmsim16(void *packetBuffer, int idx, int nchannels, int nsamples_tot, int nsamples_frag, int little_endian, int column_major, void *uarg) { int16_t *buf = packetBuffer; int i,j,k,ch; PadStripSimVal ampl = uarg; int16_t v[4], of[4] = { 1000, 200, -3000, -400 }; int lim = nsamples_tot/2; if ( nchannels < PADRPLY_STRM_NCHANNELS ) { ch = nchannels; nchannels = 1; } else { ch = 0; } if ( !little_endian != isBigEndian() ) { v[0] = swap((int16_t)ampl->val[0] + of[0]); v[1] = swap((int16_t)ampl->val[1] + of[1]); v[2] = swap((int16_t)ampl->val[2] + of[2]); v[3] = swap((int16_t)ampl->val[3] + of[3]); of[0] = swap(of[0]); of[1] = swap(of[1]); of[2] = swap(of[2]); of[3] = swap(of[3]); } else { v[0] = (int16_t)ampl->val[0] + of[0]; v[1] = (int16_t)ampl->val[1] + of[1]; v[2] = (int16_t)ampl->val[2] + of[2]; v[3] = (int16_t)ampl->val[3] + of[3]; } if ( 1 == nchannels ) { v[0] = v[ch]; of[0] = of[ch]; } if ( column_major ) { k = idx*nsamples_frag; for (i=0; i kmax ) kmax = k; return buf; } static void * blmsim32(void *packetBuffer, int idx, int nchannels, int nsamples_tot, int nsamples_frag, int little_endian, int column_major, void *uarg) { int32_t *buf = packetBuffer; int i,j,k,ch; PadStripSimVal ampl = uarg; int32_t v[4], of[4] = { 1000, 200, -3000, -400 }; int lim = nsamples_tot/2; if ( nchannels < PADRPLY_STRM_NCHANNELS ) { ch = nchannels; nchannels = 1; } else { ch = 0; } if ( !little_endian != isBigEndian() ) { v[0] = swapl(ampl->val[0] + of[0]); v[1] = swapl(ampl->val[1] + of[1]); v[2] = swapl(ampl->val[2] + of[2]); v[3] = swapl(ampl->val[3] + of[3]); of[0] = swapl(of[0]); of[1] = swapl(of[1]); of[2] = swapl(of[2]); of[3] = swapl(of[3]); } else { v[0] = ampl->val[0] + of[0]; v[1] = ampl->val[1] + of[1]; v[2] = ampl->val[2] + of[2]; v[3] = ampl->val[3] + of[3]; } if ( 1 == nchannels ) { v[0] = v[ch]; of[0] = of[ch]; } if ( column_major ) { k = idx*nsamples_frag; for (i=0; i kmax ) kmax = k; return buf; } void * bunchLengthSim_getdata(void *packetBuffer, int idx, int channels, int nsamples_tot, int nsamples_frag, int d32, int little_endian, int column_major, void *uarg) { if ( 0 == idx ) modCalc(); return d32 ? blmsim32(packetBuffer, idx, channels, nsamples_tot, nsamples_frag, little_endian, column_major, uarg) : blmsim16(packetBuffer, idx, channels, nsamples_tot, nsamples_frag, little_endian, column_major, uarg); } static long drv_init(void) { /* Install our simulator */ padStreamSim_getdata = bunchLengthSim_getdata; return 0; } static long drv_report(int level) { errlogPrintf("drvBunchLengthSim: Generate Simulated Step-function for Bunch-Length\n"); if ( level > 0 ) { padStreamDumpSimVals(); } return 0; } static struct drvet drvBunchLengthSim = { 2, drv_report, drv_init }; epicsExportAddress(drvet, drvBunchLengthSim);