Skip to content
Snippets Groups Projects
Commit bb14f1da authored by Satoru Yamada's avatar Satoru Yamada
Browse files

Add functions to read data from PCIe40 firmwre with user-logic.

You can use the UL version by defining USE_UL .

//#define USE_UL
parent 399f68b7
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,8 @@
#include <time.h>
#include "altera_dma_regs.h"
//#define USE_UL
const int CRC16_XMODEM_TABLE[] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
......@@ -82,6 +84,84 @@ int decode_data( unsigned int * data , unsigned int * ce , int * crc ) {
return 444 ;
}
int crc_calc( int crc, unsigned int data ){
int byte1, byte2, byte3, byte4, crc_temp ;
byte1 = data & 0xFF;
byte2 = ( data & 0xFF00 ) >> 8;
byte3 = ( data & 0xFF0000 ) >> 16;
byte4 = ( data & 0xFF000000 ) >> 24;
crc_temp = (((crc)<<8)&0xff00) ^ CRC16_XMODEM_TABLE[(((crc)>>8)&0xff)^byte4] ;
crc_temp = (((crc_temp)<<8)&0xff00) ^ CRC16_XMODEM_TABLE[(((crc_temp)>>8)&0xff)^byte3] ;
crc_temp = (((crc_temp)<<8)&0xff00) ^ CRC16_XMODEM_TABLE[(((crc_temp)>>8)&0xff)^byte2] ;
crc_temp = (((crc_temp)<<8)&0xff00) ^ CRC16_XMODEM_TABLE[(((crc_temp)>>8)&0xff)^byte1] ;
return crc_temp;
}
int decode_data_wUL( unsigned int * data , unsigned int * ce , int * crc ) {
int magic;
int size, size_link;
int ix ;
int offset;
unsigned int exp_run, evenum, ctime, utime;
*crc = 0 ;
size = data[ ( *ce ) % DMASIZE ] ;
magic = data[ ( *ce + 1 ) % DMASIZE ] ;
// Check a new event is available in the memory
if( ( magic & 0xffff0000 ) != 0x7f7f0000 ){
// printf("Invalid magic number %.8x\n", magic );
return -1;
}
ix = *ce + 4;
exp_run = data[ ix % DMASIZE ];
ix = *ce + 3;
evenum = data[ ix % DMASIZE ];
ix = *ce + 5;
ctime = data[ ix % DMASIZE ];
ix = *ce + 2;
utime = data[ ix % DMASIZE ];
offset = 8; // size of ROB header [words]
while( offset + 4 != size ){ // 4 = ROB trailer size
*crc = 0xffff ;
ix = *ce + offset;
size_link = data[ ix ] ;
// Calculation of CRC
*crc = crc_calc( *crc, exp_run );
*crc = crc_calc( *crc, evenum );
*crc = crc_calc( *crc, ctime );
*crc = crc_calc( *crc, utime );
for ( ix = *ce + 10 ; ix < *ce + 8 + size_link -2; ix++ ) {
*crc = crc_calc( *crc, data[ ix % DMASIZE ] );
// printf("%.8x %.8x\n", data[ ix % DMASIZE ], *crc );
}
ix = *ce + 8 + size_link -2;
if( ( *crc & 0xffff ) != ( data[ ix % DMASIZE ] & 0xffff ) ){
printf( "Error in CRC comparison %x %x\n" , *crc , data[ ix % DMASIZE ] );
}
offset += size_link;
if( offset + 4 > size ){
printf( "Invalid data size sum of link size %d size = %d\n" ,
offset + 4, size );
exit(1);
}
}
// return 444 ;
return size ;
}
int main (int argc ,char** argv) {
// Stop trigger
system( "ssh vme \"resetft -80\"" ) ;
......@@ -102,7 +182,7 @@ int main (int argc ,char** argv) {
retry = 0 ;
int i ;
int size, currentEventIndex, crc, nErr, nEvt, nRetry ;
int size, size_256, currentEventIndex, crc, nErr, nEvt, nRetry ;
currentEventIndex = 0 ;
nErr = 0 ;
nEvt = 0 ;
......@@ -113,8 +193,29 @@ int main (int argc ,char** argv) {
res = pcie40_b2dmapointerread( 0 , &data ) ;
#ifdef USE_UL
size = decode_data_wUL( data , &currentEventIndex , &crc ) ;
#else
size = decode_data( data , &currentEventIndex , &crc ) ;
#endif
if ( size <= 8 ) continue ;
#ifdef USE_UL
if( size % 8 != 0 ){
size_256 = ( size/8 + 1 )*8;
}
/* printf("size %d %d\n", size, i ); */
/* printf("%.8d ", 0 ); */
/* for (i= 0 ;i < 16;i++){ */
/* printf("%08x ",data[ ( currentEventIndex + i ) % DMASIZE ]); */
/* if( ( i % 8 ) == 7 ) printf("\n%.8d %.8d %.8d ", j, i); */
/* } */
/* printf("\n"); */
currentEventIndex = ( currentEventIndex + size_256 ) % DMASIZE ;
nEvt++ ;
#else
if ( ( data[(8+16*size+currentEventIndex)%DMASIZE]&0xFFFF ) != crc ) {
if ( retry == 1 ) {
nErr++ ;
......@@ -128,6 +229,7 @@ int main (int argc ,char** argv) {
currentEventIndex = ( ( size + 1 ) * 16 + currentEventIndex ) % DMASIZE ;
nEvt++ ;
}
#endif
}
clock_t end = clock();
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment