• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

simulator/Simulator.h (r4864/r4162)

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 // #define ETHER_ADDR_LEN 6
00043 // #define IP_ADDR_LEN 4
00044 
00045 /*****************************************************
00046  *
00047  * roc::Board Configuration Structure adapted from firmware
00048  *
00049  *****************************************************/
00050 
00051 /*****************************************************
00052  * SysCore Configuration Structure
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;          // id of buffer which is now filled
00076 
00077    std::list<roc::UdpDataPacketFull>  buf;
00078 
00079    unsigned  high_water;      /* Maximum number of buffers to be used in normal situation */
00080    unsigned  low_water;       /* Number of buffers to enable data taking after is was stopped */
00081 
00082    uint32_t  cmdsList[ROC_CMD_LST_SIZE/4]; // memory for commands lists
00083 
00084    uint32_t  cmdListPtr; // pointer on current active command in the list,
00085 
00086    uint32_t  send_limit;      /* limit for sending - can be in front of existing header */
00087    uint32_t  next_send_id;    /* id of next packet to send */
00088 
00089    uint32_t   resend_indx[RESEND_MAX_SIZE]; /* indexes of buffers which should be resent */
00090    unsigned   resend_size;                  /* number of indexes to be resent */
00091 
00092    uint32_t   last_req_pkt_id; /* id of last request packet, should be always send back */
00093 
00094    bool         masterConnected; // 1 if master controller is connected
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;  // 0-off , 1-master, 2-controller
00107    double       lockedTime;
00108 
00109    unsigned     daqState;
00110 
00111    double       lastFlushTime;
00112 
00113    uint32_t     droppkt_id;
00114 
00115    bool         data_taking_on;  /* indicate if we can take new buffer */
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; // data socket
00146       int      s_ctrl; // control socket
00147 
00148       long double clockScrewUp;     // coefficient (1. is normal), which indicate screwup of clock
00149       uint64_t nextTimeHit;   // when hit will be generated
00150       unsigned nextHitCounter;  // reverse counter for hits generation
00151       uint64_t nextEventNumber; // current event number
00152       uint64_t nextTimeNoise; // indicate when time stamp message should be
00153       uint64_t nextTimeSync;  // indicate when next time sync message should appear
00154       uint64_t nextTimeAUX0;   // when next AUX signal will be
00155       bool     nextAUX0Falling; // indicate if aux falling or rising
00156       uint64_t periodAUX0;      // period of AUX0 signal
00157       uint64_t nextTimeAUX1;    // when next AUX signal will be
00158       bool     nextAUX1Falling; // indicate if aux falling or rising
00159       uint64_t periodAUX1;      // period of AUX1 signal
00160       uint32_t lastEpoch;     // last epoch set in data stream
00161       uint64_t nextSyncEvent; // number of next sync event
00162 
00163       // list of last N packets send to PC, used in resubmit
00164       double recvLostRate; // relative loss rate of packets receive
00165       double sendLostRate; // relative loss rate of packets send
00166 
00167       unsigned recvLostCounter; // backward counter to next recv lost
00168       unsigned sendLostCounter; // backward counter to next send lost
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 /*SYSCORESIMULATOR_H_*/

Generated on Tue Dec 10 2013 04:52:23 for ROCsoft by  doxygen 1.7.1