cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Adventurer
Adventurer
364 Views
Registered: ‎02-07-2020

Create testbench From C++ code.

Jump to solution

Hi to all

I have the following working  cpp Code that removes all red pixel from a picture.I want to implement it in Vivado HLS but i don not know how to create a testbench fot this code...

Can you show me a way or a guide to create the testbench?

Thanks in advance!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 64*64*4

unsigned char INPUT_RGBA[64 * 64 * 4];
unsigned char OUTPUT_RGBA[64 * 64 * 4];


int main(int argc, char *argv[])
{	
	unsigned int line_counter = 0;
	unsigned int i = 0;
	unsigned char temp;

	FILE *rFILE ; 
	char in_filename [1024] ; 

	FILE *wFILE ; 
	char out_filename[] = "out.rgba" ; 
	
	
	strcpy (in_filename, argv[1] );
 	rFILE = fopen(in_filename,"rb"); 

	wFILE = fopen(out_filename,"wb");

	if ( (rFILE == NULL) || (wFILE == NULL))
	{
		// segnematation fault
		exit(0);
	}

// initializationn part: read the input rgba file and store the results to INPUT_RGBA array  
	for(i = 0; i < SIZE; i++) {
		fscanf(rFILE,"%c", &temp);  
		INPUT_RGBA[i] = temp; 
	}		

// processing part: INPUT_RGBA --> some kind of processing OUTPUT_RGBA  
	for(i = 0; i < SIZE; i++) {

		if (i % 4 == 0) {
			OUTPUT_RGBA[i] = 0 ; 
		}
	        else {
			OUTPUT_RGBA[i] = INPUT_RGBA[i] ; 
		}
	}

// store the results to output file: print the OUTPUT_RGBA to a wFILE  
	for(i = 0; i < 64 * 64 * 4 ; i++) {
		fprintf(wFILE,"%c", OUTPUT_RGBA[i]) ; 
	}

	fclose(rFILE);
	fclose(wFILE);

	
	return 0;	
}
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Advisor
Advisor
332 Views
Registered: ‎04-26-2015

Right, so at the moment your code is all-in-one testbench and "functional" code. If we split it up:

 

Testbench initialization

int main(int argc, char *argv[])
{	
	unsigned int line_counter = 0;
	unsigned int i = 0;
	unsigned char temp;

	FILE *rFILE ; 
	char in_filename [1024] ; 

	FILE *wFILE ; 
	char out_filename[] = "out.rgba" ; 
	
	
	strcpy (in_filename, argv[1] );
 	rFILE = fopen(in_filename,"rb"); 

	wFILE = fopen(out_filename,"wb");

	if ( (rFILE == NULL) || (wFILE == NULL))
	{
		// segnematation fault
		exit(0);
	}

// initializationn part: read the input rgba file and store the results to INPUT_RGBA array  
	for(i = 0; i < SIZE; i++) {
		fscanf(rFILE,"%c", &temp);  
		INPUT_RGBA[i] = temp; 
	}		

Functional code

// processing part: INPUT_RGBA --> some kind of processing OUTPUT_RGBA  
	for(i = 0; i < SIZE; i++) {

		if (i % 4 == 0) {
			OUTPUT_RGBA[i] = 0 ; 
		}
	        else {
			OUTPUT_RGBA[i] = INPUT_RGBA[i] ; 
		}
	}

Testbench clean-up

// store the results to output file: print the OUTPUT_RGBA to a wFILE  
	for(i = 0; i < 64 * 64 * 4 ; i++) {
		fprintf(wFILE,"%c", OUTPUT_RGBA[i]) ; 
	}

	fclose(rFILE);
	fclose(wFILE);

	
	return 0;	
}

 

 

So, what you want to do is something like this:

testbench.cpp

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 64*64*4

unsigned char INPUT_RGBA[64 * 64 * 4];
unsigned char OUTPUT_RGBA[64 * 64 * 4];
process(unsigned char * in, unsigned char * out);

int main(int argc, char *argv[])
{	
	unsigned int line_counter = 0;
	unsigned int i = 0;
	unsigned char temp;

	FILE *rFILE ; 
	char in_filename [1024] ; 

	FILE *wFILE ; 
	char out_filename[] = "out.rgba" ; 
	
	
	strcpy (in_filename, argv[1] );
 	rFILE = fopen(in_filename,"rb"); 

	wFILE = fopen(out_filename,"wb");

	if ( (rFILE == NULL) || (wFILE == NULL))
	{
		// segnematation fault
		exit(0);
	}

// initializationn part: read the input rgba file and store the results to INPUT_RGBA array  
	for(i = 0; i < SIZE; i++) {
		fscanf(rFILE,"%c", &temp);  
		INPUT_RGBA[i] = temp; 
	}	

	process (INPUT_RGBA, OUTPUT_RGBA);	

// store the results to output file: print the OUTPUT_RGBA to a wFILE  
	for(i = 0; i < 64 * 64 * 4 ; i++) {
		fprintf(wFILE,"%c", OUTPUT_RGBA[i]) ; 
	}

	fclose(rFILE);
	fclose(wFILE);

	
	return 0;	
}

process.cpp:

void process(unsigned char * INPUT_RGBA, unsigned char * OUTPUT_RGBA) {
	for(i = 0; i < SIZE; i++) {

		if (i % 4 == 0) {
			OUTPUT_RGBA[i] = 0 ; 
		}
	        else {
			OUTPUT_RGBA[i] = INPUT_RGBA[i] ; 
		}
	}
}

Once you get closer to synthesis you'll want to think about interface types. Will this block be pulling data straight from off-chip RAM? Will it be getting an image stream from a camera? Those will determine just how you do your interfaces and what restrictions are put on the block.

View solution in original post

0 Kudos
2 Replies
Highlighted
Advisor
Advisor
333 Views
Registered: ‎04-26-2015

Right, so at the moment your code is all-in-one testbench and "functional" code. If we split it up:

 

Testbench initialization

int main(int argc, char *argv[])
{	
	unsigned int line_counter = 0;
	unsigned int i = 0;
	unsigned char temp;

	FILE *rFILE ; 
	char in_filename [1024] ; 

	FILE *wFILE ; 
	char out_filename[] = "out.rgba" ; 
	
	
	strcpy (in_filename, argv[1] );
 	rFILE = fopen(in_filename,"rb"); 

	wFILE = fopen(out_filename,"wb");

	if ( (rFILE == NULL) || (wFILE == NULL))
	{
		// segnematation fault
		exit(0);
	}

// initializationn part: read the input rgba file and store the results to INPUT_RGBA array  
	for(i = 0; i < SIZE; i++) {
		fscanf(rFILE,"%c", &temp);  
		INPUT_RGBA[i] = temp; 
	}		

Functional code

// processing part: INPUT_RGBA --> some kind of processing OUTPUT_RGBA  
	for(i = 0; i < SIZE; i++) {

		if (i % 4 == 0) {
			OUTPUT_RGBA[i] = 0 ; 
		}
	        else {
			OUTPUT_RGBA[i] = INPUT_RGBA[i] ; 
		}
	}

Testbench clean-up

// store the results to output file: print the OUTPUT_RGBA to a wFILE  
	for(i = 0; i < 64 * 64 * 4 ; i++) {
		fprintf(wFILE,"%c", OUTPUT_RGBA[i]) ; 
	}

	fclose(rFILE);
	fclose(wFILE);

	
	return 0;	
}

 

 

So, what you want to do is something like this:

testbench.cpp

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 64*64*4

unsigned char INPUT_RGBA[64 * 64 * 4];
unsigned char OUTPUT_RGBA[64 * 64 * 4];
process(unsigned char * in, unsigned char * out);

int main(int argc, char *argv[])
{	
	unsigned int line_counter = 0;
	unsigned int i = 0;
	unsigned char temp;

	FILE *rFILE ; 
	char in_filename [1024] ; 

	FILE *wFILE ; 
	char out_filename[] = "out.rgba" ; 
	
	
	strcpy (in_filename, argv[1] );
 	rFILE = fopen(in_filename,"rb"); 

	wFILE = fopen(out_filename,"wb");

	if ( (rFILE == NULL) || (wFILE == NULL))
	{
		// segnematation fault
		exit(0);
	}

// initializationn part: read the input rgba file and store the results to INPUT_RGBA array  
	for(i = 0; i < SIZE; i++) {
		fscanf(rFILE,"%c", &temp);  
		INPUT_RGBA[i] = temp; 
	}	

	process (INPUT_RGBA, OUTPUT_RGBA);	

// store the results to output file: print the OUTPUT_RGBA to a wFILE  
	for(i = 0; i < 64 * 64 * 4 ; i++) {
		fprintf(wFILE,"%c", OUTPUT_RGBA[i]) ; 
	}

	fclose(rFILE);
	fclose(wFILE);

	
	return 0;	
}

process.cpp:

void process(unsigned char * INPUT_RGBA, unsigned char * OUTPUT_RGBA) {
	for(i = 0; i < SIZE; i++) {

		if (i % 4 == 0) {
			OUTPUT_RGBA[i] = 0 ; 
		}
	        else {
			OUTPUT_RGBA[i] = INPUT_RGBA[i] ; 
		}
	}
}

Once you get closer to synthesis you'll want to think about interface types. Will this block be pulling data straight from off-chip RAM? Will it be getting an image stream from a camera? Those will determine just how you do your interfaces and what restrictions are put on the block.

View solution in original post

0 Kudos
Highlighted
Adventurer
Adventurer
294 Views
Registered: ‎02-07-2020
@scholar
Thanks a lot my friend!!!
0 Kudos