Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Partial memory access

Not open for further replies.


Member level 1
May 16, 2012
Reaction score
Trophy points
Activity points
Dear all,

I want to acquire data using DMA (large data) and store it to large memory acquire and allocated using malloc in c++
later I want to take the partial data and save in separate files. Please refer to the image for more clarification(although it is worst diagram but was short of time).

Is it possible...
I wrote some test code that works on test, where I only give test data, but when I take it to acquire data from FPGA(through PCIe DMA)
it completely failed...acquiring nothing but trash for the first burst, and nothing for rest of the bursts....

const int samples = 2048;
  const int points = 10*10*4;

  short int *memspace, *temp_mem;
  memspace = (short int *)malloc(sizeof(short int)*samples*(points));
  temp_mem = (short int *)malloc(sizeof(short int)*samples);
  //unsigned char *pInData = (unsigned char *)_aligned_malloc(2*BurstSize, 4096);

  //FILE *hs1,*hs2; 
  char str1[20],str2[20];;
  t = clock();
  unsigned long wow = sizeof(short int)*samples*(points);
  printf("Size of short is %u and size of int short is %u\n", sizeof(short), sizeof(int));
  printf("The Value of sizeof here is %u words \n", wow);
  printf("In terms of Bytes it is %fMB\n", (float)(wow/2)/(1024*1024));

  for(short int i =0; i<points; i++){
	  //memspace[i] = i*1000;  
	  sprintf(str1, "Textfile%d.bin",i);
	//fprintf(hs,"memspace at this location is %d\n", memspace[i]);
	  for(short int k=0; k<2048; k++){temp_mem[k]  = i+k;	  
	 // printf("Value of temp_mem is %d \n", temp_mem[k]);

	//memory copy
	  //printf("Cannot memcpy %d\n", i);
	  if(i==0) memcpy(memspace + i*samples , temp_mem, sizeof(short int)*samples);
	  else	  memcpy(memspace + i*samples + 1 , temp_mem, sizeof(short int)*samples);

  //All data is captured and stored to mewmory once, 
  //now is the time to store them all in the separate files
  for(int i =0; i<points-1; i++){
	sprintf(str1, "Textfile%d.txt",i);
	printf("Cannot save to File...(%d)\n", i);
	Save16BitArrayToFile(memspace + i*samples , sizeof(short int)*samples/2, str1, ASCII);
	Save16BitArrayToFile(memspace + i*samples+1 , sizeof(short int)*samples/2, str1, ASCII);

where as code for function is as below..

static ULONG Save16BitArrayToFile(void *buf, short int bufsize, const char *filename, int mode)
	int j;
	FILE *fOutFile;
	char sOpenMode[55];

	// these pointers cannot be NULL
	if(!buf) {
		return -1;

	if(!filename) {
		return -2;

	// cast our stamp less pointer to a short
	short *buf16 = (short *)buf;

	// open the file given as argument
		sprintf(sOpenMode, "a");
		sprintf(sOpenMode, "ab");

	fOutFile = fopen(filename, sOpenMode);
	if(fOutFile==NULL) {
		return -3;

	// write to file either as ASCII or BINARY
	if(mode == BINARY)
		fwrite(buf, 2, bufsize, fOutFile);
	else // -> ASCII
		// Here we don't take risk. We pass a short casted as an int,
		// and we use the normalized int -> short format converter (%hi)
		for(j = 0; j < bufsize; j++)
			{fprintf(fOutFile, "%d\n", (short int)buf16[j]);
			//printf("\t%d ", (int)buf16[j]);

	return 0;

Also there is some concept related to memory aligned etc. but I could not completely understand this although I read some stuff over internet...
Can some one explain this stuff in easy-to-understand manner...

Not open for further replies.

Part and Inventory Search

Welcome to