Skip to content
Snippets Groups Projects
pixel.cpp 4.34 KiB
Newer Older
Majd Hafiri's avatar
Majd Hafiri committed
#include "pixel.hpp"
Vipin Thomas's avatar
Vipin Thomas committed
void pixel(apint &in_decimal,
Majd Hafiri's avatar
Majd Hafiri committed
           apint selector,
		   apint stream_count,
		   stream &din,
		   stream &dout
) {
	#pragma HLS INTERFACE ap_ctrl_none port=return
Majd Hafiri's avatar
Majd Hafiri committed
    #pragma HLS INTERFACE s_axilite port=in_decimal     // ascii values for the characters(word)
    #pragma HLS INTERFACE s_axilite port=selector       // to select the mode (encoding or decoding)
	#pragma HLS INTERFACE s_axilite port=stream_count   // size of data stream 
	#pragma HLS INTERFACE axis port=din
	#pragma HLS INTERFACE axis port=dout

Majd Hafiri's avatar
Majd Hafiri committed
    din >> tmpA;  //input data
Majd Hafiri's avatar
Majd Hafiri committed

    switch(selector)
Majd Hafiri's avatar
Majd Hafiri committed
    {   
        // case 0 is for encoding
        case 0:
            
            if (count_streams == 0){
Majd Hafiri's avatar
Majd Hafiri committed
                
                final_char=0;             
                decNum = in_decimal;      //getting the ascii values of the word from the register
Majd Hafiri's avatar
Majd Hafiri committed

                addNum=0;
Majd Hafiri's avatar
Majd Hafiri committed

                if(decimalCounter % 8 == 0){
Majd Hafiri's avatar
Majd Hafiri committed
                    /*checking if the counter is 8 which means the process reached the 8th bit*/
                    lastDecimalVal = getDecimal(decNum);
Majd Hafiri's avatar
Majd Hafiri committed
                    decNum /= 100;                       //to delete the processed ascii value and get the next one
                    charIn=convert(lastDecimalVal);      //to find the binary of the next ascii value
Majd Hafiri's avatar
Majd Hafiri committed
                addNum=charIn%10;                   //LSB of charIn
		        charIn=(int)charIn/10;              //deleting lsb and get the next bit(lsb)
Majd Hafiri's avatar
Majd Hafiri committed
                //updating the input data according to addNum and lsb of the input data.
                //if addNum is equal to 1 and input data is even , 1 will be added to input data which turns to odd
                //otherwise no action needed
Vipin Thomas's avatar
Vipin Thomas committed
                if(tmpA.data % 2 == 0 && addNum == 1){
Majd Hafiri's avatar
Majd Hafiri committed
                    
Vipin Thomas's avatar
Vipin Thomas committed
                    tmpA.data += 1;
Majd Hafiri's avatar
Majd Hafiri committed
                }
                
                //if addNum is equal to 0, input data is odd, 1 will be subtracted fro the input data which will turn it to even
                //otherwise no action needed
                else if(tmpA.data % 2 != 0 && addNum == 0){
Vipin Thomas's avatar
Vipin Thomas committed
                    tmpA.data -= 1;
Majd Hafiri's avatar
Majd Hafiri committed

                decimalCounter++;

            break;

Majd Hafiri's avatar
Majd Hafiri committed
        //case 1 is for decoding
Majd Hafiri's avatar
Majd Hafiri committed
                decode(tmpA.data);    //getting decoded binary value bit by bit for every character
Vipin Thomas's avatar
Vipin Thomas committed
                decimalCounter++;
                if(decimalCounter == 8){
Majd Hafiri's avatar
Majd Hafiri committed
                    //after processing 8 bits, the ascii value will be returned by the convertBinInt
Vipin Thomas's avatar
Vipin Thomas committed
                    decimalOut=decimalOut*100+convertBinInt(final_char);  
                    decimalCounter=0;
                    final_char=0;   
Vipin Thomas's avatar
Vipin Thomas committed
                }
Vipin Thomas's avatar
Vipin Thomas committed
                
            break;

        default:
            break;
    }
	
	count_streams++;

Vipin Thomas's avatar
Vipin Thomas committed
	if (count_streams == stream_count){
Majd Hafiri's avatar
Majd Hafiri committed
        //resetting variables
		count_streams = 0;
        charIn=0;
        addNum=0;
Vipin Thomas's avatar
Vipin Thomas committed
        decimalCounter=0;
        if(selector == 1){
            final_char=0;
Vipin Thomas's avatar
Vipin Thomas committed
            in_decimal=decimalOut;
            decimalOut=0;
Vipin Thomas's avatar
Vipin Thomas committed
    if(count_streams == stream_count){
Majd Hafiri's avatar
Majd Hafiri committed
        //setting TLAST signal to 1 
	        tmpA.last = 1;              

Vipin Thomas's avatar
Vipin Thomas committed
	    }

	    dout << tmpA;
}


long long convert(int n) {
Majd Hafiri's avatar
Majd Hafiri committed
    /*
    This function converts a decimal number to a Binary number and returns it
    */
    long long bin = 0;
    int rem, i = 1, step = 1;
    while (n != 0) {
        rem = n % 2;
        n /= 2;
        bin += rem * i;
        i *= 10;
    }
    return bin;
}

Majd Hafiri's avatar
Majd Hafiri committed
void decode(int data) {
    /*
    get the binary value of the embedded ascii values by combining the LSBs.
    */
    int bit;
    if(data % 2 == 0){
    bit = 0;
    }else if(data % 2 != 0){
        bit=1;
    }
    final_char= final_char*10+bit;

}

int convertBinInt(long long n) {
Majd Hafiri's avatar
Majd Hafiri committed
    /*
    This function returns decimal value from a binary number
    */
    int dec = 0, i = 7, b=0,rem=0;

        while (n != 0) {
            b=pow(10,i);
            rem = n / b;
            n =n % b;
            dec += rem * pow(2, 7-i);
            --i;
        }
    return dec;
}

int getDecimal(int n) {
Majd Hafiri's avatar
Majd Hafiri committed
    /*
    returns the last two digits (which represents one character) of the corresponding ascii values passed to this function.
    */
    int num = 0;
Vipin Thomas's avatar
Vipin Thomas committed
    num = n % 100;