Go to the documentation of this file.00001 #ifndef ROC_SIMULATOR_H
00002 #define ROC_SIMULATOR_H
00003
00004
00025
00026
00027 #define ROC_SIMULATOR_SW_VERSION 0x02000103
00028 #define ROC_SIMULATOR_HW_VERSION 0x02000103
00029 #define ROC_SIMULATOR_FPGA_TYPE 1
00030
00031 #include "roc/defines_roc.h"
00032 #include "roc/defines_udp.h"
00033 #include "base/defines_gpio.h"
00034 #include "roc/defines_i2c.h"
00035 #include "nxyter/defines_nxyter.h"
00036
00037 #include <list>
00038 #include <stdint.h>
00039
00040 #include <netinet/in.h>
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 struct sc_config
00055 {
00056 uint16_t ctrlPort;
00057 uint16_t dataPort;
00058
00059 int rocNumber;
00060 int flushTimeout;
00061 };
00062
00063 extern struct sc_config gC;
00064
00065
00066 #define RESEND_MAX_SIZE 128
00067
00068 #define NX_REG_SIZE 8192
00069
00070 #define GPIO_REG_SIZE 256
00071
00072
00073 struct sc_data
00074 {
00075 uint32_t headid;
00076
00077 std::list<roc::UdpDataPacketFull> buf;
00078
00079 unsigned high_water;
00080 unsigned low_water;
00081
00082 uint32_t cmdsList[ROC_CMD_LST_SIZE/4];
00083
00084 uint32_t cmdListPtr;
00085
00086 uint32_t send_limit;
00087 uint32_t next_send_id;
00088
00089 uint32_t resend_indx[RESEND_MAX_SIZE];
00090 unsigned resend_size;
00091
00092 uint32_t last_req_pkt_id;
00093
00094 bool masterConnected;
00095 int masterDataPort;
00096 int masterPort;
00097 in_addr_t masterIp;
00098 sockaddr_in masterDataAddr;
00099 double lastMasterTime;
00100
00101 bool controlConnected;
00102 int controlPort;
00103 in_addr_t controlIp;
00104 double lastControlTime;
00105
00106 int controlLocked;
00107 double lockedTime;
00108
00109 unsigned daqState;
00110
00111 double lastFlushTime;
00112
00113 uint32_t droppkt_id;
00114
00115 bool data_taking_on;
00116
00117 uint32_t lastMasterPacketId;
00118 uint8_t master_retry_buf[MAX_UDP_PAYLOAD];
00119 int master_retry_buf_rawsize;
00120
00121 uint32_t lastControlPacketId;
00122 uint8_t control_retry_buf[MAX_UDP_PAYLOAD];
00123 int control_retry_buf_rawsize;
00124 };
00125
00126 extern struct sc_data gD;
00127
00128
00137 class UdpSimulator {
00138 private:
00139 uint32_t nxRegister[NX_REG_SIZE];
00140 uint32_t gpioRegs[GPIO_REG_SIZE];
00141
00142 uint32_t& syncMDelay() { return gpioRegs[(base::GPIO_SYNCM_SCALEDOWN - base::GPIO_CONFIG) / 4]; }
00143 uint32_t& gpioConfig() { return gpioRegs[0]; }
00144
00145 int s_data;
00146 int s_ctrl;
00147
00148 long double clockScrewUp;
00149 uint64_t nextTimeHit;
00150 unsigned nextHitCounter;
00151 uint64_t nextEventNumber;
00152 uint64_t nextTimeNoise;
00153 uint64_t nextTimeSync;
00154 uint64_t nextTimeAUX0;
00155 bool nextAUX0Falling;
00156 uint64_t periodAUX0;
00157 uint64_t nextTimeAUX1;
00158 bool nextAUX1Falling;
00159 uint64_t periodAUX1;
00160 uint32_t lastEpoch;
00161 uint64_t nextSyncEvent;
00162
00163
00164 double recvLostRate;
00165 double sendLostRate;
00166
00167 unsigned recvLostCounter;
00168 unsigned sendLostCounter;
00169
00170 uint64_t totalSendCounter, totalSendLosts;
00171 uint64_t totalRecvCounter, totalRecvLosts;
00172
00173 uint32_t recvDataSize;
00174 uint32_t sendDataSize;
00175 uint32_t takeDataSize;
00176 roc::BoardStatistic fStat;
00177
00178 bool doRecvLost();
00179 bool doSendLost();
00180
00181 void resetSimulator();
00182
00183 bool sendDataPacket(uint32_t pktid);
00184
00185 public:
00187 UdpSimulator(int rocid, int syncscale = 2, int period0 = 0, int period1 = 0, int ctrlport = 0);
00188 virtual ~UdpSimulator();
00189
00190 bool initNewSockets();
00191
00192 void dispatchNewSockets(int tmout = 1000);
00193
00194 void OnRawData(int number, const char* buf, int len, struct sockaddr*, socklen_t);
00195
00196 int checkCtrlOperationAllowed(roc::UdpMessageFull* pkt, in_addr_t src_ip, int src_port);
00197
00198 void mainLoop();
00199
00200 uint32_t PutReg(int nodekind, uint32_t addr, uint32_t val, uint8_t* rawdata);
00201 uint32_t GetReg(int nodekind, uint32_t addr, uint32_t *retVal, void* rawdata = 0, int* rawdatasize = 0);
00202 uint32_t NOper(int nodekind, uint32_t num, uint32_t* oper);
00203
00205 void fillRareSyncData(uint8_t* target, uint16_t numOfDatas);
00206
00207 void makeSysMessage(uint8_t typ, uint32_t arg = 0);
00208
00209 void processDataRequest(roc::UdpDataRequestFull* pkt);
00210
00211 void setLostRates(double recv = 0., double send = 0.);
00212 };
00213
00214 #endif