⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 编码程序.c

📁 在嵌入式FPGA开发环境(ISE7.1)下的Huffman编解码的程序
💻 C
字号:
/* *  * Copyright (c) 2004 Xilinx, Inc.  All rights reserved. * * Xilinx, Inc. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A  * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR  * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION  * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE  * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO  * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO  * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE  * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY  * AND FITNESS FOR A PARTICULAR PURPOSE. *//* * Xilinx EDK 7.1.2 EDK_H.12.5.1 * * This file is a sample test application * * This application is intended to test and/or illustrate some  * functionality of your system.  The contents of this file may * vary depending on the IP in your system and may use existing * IP driver functions.  These drivers will be generated in your * XPS project when you run the "Generate Libraries" menu item * in XPS. * * Your XPS project directory is at: *    C:\Documents and Settings\Administrator\桌面\final\spartan3 */
// Located in: microblaze_0/include/xparameters.h#include "xparameters.h"#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "xutil.h"
#include "xbasic_types.h"
#include "xstatus.h"
#include"xuartlite_l.h"
#include "xuartlite.h"

//====================================================#define MAXCHAR 30000
//#define MAXNODE 255
#define LENGTH  128
#define REARPOS 65
#define XPAR_RS232_1_DEVICE_ID 1
#define XPAR_RS232_1_BASEADDR 0x40600000
#define BUFFER 300

//用到的串口地址的定义
#define RS232_BASEADDR  XPAR_RS232_1_BASEADDR

//用到的检测状态的宏定义
#define STATUS_CHECK(Status)            \
    {                                   \
        if (Status != XST_SUCCESS)      \
        {                               \
            return Status;              \
        }                               \
    }
//////////////////////////////////////////////////////////////////
struct HtNode
{
	int ww;
	char info;	
	int llinkIndex;
	int rlinkIndex;
	int parentIndex;
};

//struct HtTree
//{
	struct HtNode ht[255];
	int rootIndex;
//}

//typedef struct HtTree* PHtTree;


typedef struct
{
	char asciiCode;
	unsigned long haffCode;
	int haffCodeLen;
}HaffCode;



//////////////////////////////////////////////////////////////////////
void preHaffListMake(int rootIndex,unsigned long youBiao,int sDepth,HaffCode * inList)
{
	
	if(ht[rootIndex].llinkIndex==-1&&ht[rootIndex].rlinkIndex==-1)
	{
		inList[(int)ht[rootIndex].info].haffCode=youBiao;
		inList[(int)ht[rootIndex].info].haffCodeLen=sDepth;
		
		//XUartLite_SendByte(RS232_BASEADDR,inList[(int)ht[rootIndex].info].haffCodeLen);
	}
	else
	{
		int w=1;
		preHaffListMake(ht[rootIndex].llinkIndex,youBiao<<1,sDepth+1,inList);
		preHaffListMake(ht[rootIndex].rlinkIndex,(youBiao<<1)|w,sDepth+1,inList);
	}
}


void haffmanAlgorithm(int m, int * w)
{
	
	//PHtTree pht;
	int i,j;
	int firstMinIndex, secondMinIndex;
	int firstMinW, secondMinW;
	char ch1;
	Xuint32 data=0;

    ch1=(char)m;
    //XUartLite_SendByte(RS232_BASEADDR,m);
 	//pht = (PHtTree)malloc(sizeof(struct HtTree));/////  meiyou malloc hanshu

	//assertF(pht != NULL, "in huffman algorithsm, mem apply failure!\n");

	/* Initialize the array */
	for(i=0; i<2*m-1; i++)
	{
		ht[i].llinkIndex = -1;
		ht[i].rlinkIndex = -1;
		ht[i].parentIndex = -1;
		//XUartLite_SendByte(RS232_BASEADDR,ht[i].llinkIndex);

		if(i<m)
		{
			
			ht[i].ww = w[i];
			ht[i].info = (char)i; //
			//XUartLite_SendByte(RS232_BASEADDR, i);
			//XUartLite_SendByte(RS232_BASEADDR,ht[i].info);
		}
		else
		{
			//XUartLite_SendByte(RS232_BASEADDR, i);
			ht[i].ww = -1;
			//XUartLite_SendByte(RS232_BASEADDR, ht[i].ww);
 		}
		
		
	}
	//for(i=0; i<m; i++)
	//	XUartLite_SendByte(RS232_BASEADDR,ht[i].info);//

	
	for(i=0; i<m-1; i++)
	{
		firstMinW = MAXCHAR;
		firstMinIndex = -1;

		secondMinW = MAXCHAR;
		secondMinIndex = -1;

		for(j=0; j<m+i; j++)
		{
			if(ht[j].ww<firstMinW && ht[j].parentIndex==-1)
			{
				secondMinIndex = firstMinIndex;
				secondMinW = firstMinW;

				firstMinIndex = j;
				firstMinW = ht[j].ww;
			}
			else if(ht[j].ww<secondMinW && ht[j].parentIndex==-1)
			{
				secondMinW = ht[j].ww;
				secondMinIndex = j;
			}
		}
		ht[firstMinIndex].parentIndex = m+i;
		ht[secondMinIndex].parentIndex = m+i;
		
		ht[m+i].ww = firstMinW + secondMinW;
		ht[m+i].llinkIndex = firstMinIndex;
		ht[m+i].rlinkIndex = secondMinIndex;
		rootIndex = m+i;
	}
	//XUartLite_SendByte(RS232_BASEADDR,ht[i].rootIndex);
	//for(i=0; i<255; i++)
	//XUartLite_SendByte(RS232_BASEADDR,ht[i].parentIndex);

}



int getBinLen(unsigned long inData)
{
	int i=0;
	if( inData==0) 
		return 1;
	else		
	
	while(inData!=0)
	{
		inData/=2;
		i++;
	}
	return i;
}


void wait(int loop_count)

{
	int	sum, data;
	sum	= 0;

	for (data = 0; data < loop_count; data++) {
		sum = (data << 8);
	}

}
//////////////////////////////////////////////////////////////////////////////////////
int main (void) {
   //print("Please enter data...\r\n");

	
	XUartLite Uart;  //串口句柄  
	XStatus Status;  //状态变量
	
	//对串口句柄初始化
	Status = XUartLite_Initialize( &Uart, XPAR_RS232_DEVICE_ID);  
	STATUS_CHECK(Status);
	
	int wList[LENGTH] = {0,0,0,0,0,0,0,0,0,0,261,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4470,3,92,0,9,4,8,56,17,17,1,7,264,104,237,3,112,68,43,41,28,21,31,14,17,29,4,3,1,1,1,4,3,119,43,65,55,28,20,28,36,76,29,12,20,66,39,23,28,1,7,135,96,26,13,49,20,11,13,0,0,0,0,0,0,1728,270,576,851,2588,453,447,1033,1564,19,114,837,510,1641,1586,407,22,1274,1283,1723,494,225,302,39,331,21,1,0,1,0,0};
	char Temp[BUFFER];//, Temp1[BUFFER];
	while(1)
	{
		int Stat=0;   //状态
		int count1=0; //接收字符的个数
    	while(!Stat)
    	{
        	Temp[count1++]=XUartLite_RecvByte(RS232_BASEADDR); //接收串口数据的函数,
        	//temp是unsigned char / Xuint8类型数据,RS232_BASEADDR是串口地址。       
         	//注意:通过串口接收的数据是以8位数据宽度为单位的,可以认为是char型的,即数据的范围是0-255
        	//但是只要在pc端发送文件时使用的数据类型和接收文件时使用的数据类型一致即可同时取用四个字节
        	//作long/int等类型数据使用。
        	wait(5000);
        	Stat=XUartLite_mIsReceiveEmpty(RS232_BASEADDR);  //判断串口是否为空的函数
		}
		break;
	}

	///////////////////////////////////////////////////////////////////////////////////	
	//int q=0;
	//for(q=0; q<BUFFER; q++)
	//{
	//	Temp[q] = q;
		//XUartLite_SendByte(RS232_BASEADDR,Temp[q]);
	//}
	

	int i,j=0,k=0;

	//PHtTree myHtTree;                       //定义一棵huffman树
	haffmanAlgorithm(LENGTH, wList);
	
	//for(i=0; i<255; i++)
		//XUartLite_SendByte(RS232_BASEADDR,ht[2].parentIndex);

	
	  	
	char inData='0', outputData;
	unsigned long curCode, tmpBinData;
	int curLen, realLen, curIndex;
	
	int count;

	unsigned long rearCode, youBiao=0;
	int rearCodeLen;
	int sDepth=0;
	
	//HaffCode haffList[LENGTH];
	
	//preHaffListMake(rootIndex, youBiao,sDepth , haffList);

	//XUartLite_SendByte(RS232_BASEADDR,haffList[44].haffCodeLen);//sDepth


	HaffCode haffList[LENGTH]={{0,1581614,21},{1,1581615,21},{2,1581616,21},{3,1581617,21},{4,1581618,21},{5,1581619,21},{6,1581620,21},{7,1581621,21},{8,1581622,21},{9,1581623,21},{10,97,7},{11,1581624,21},{12,1581625,21},{13,1581626,21},{14,1581627,21},{15,1581628,21},{16,1581629,21},{17,1581630,21},{18,1581631,21},{19,790784,20},{20,790785,20},{21,790786,20},{22,790787,20},{23,790788,20},{24,790789,20},{25,790790,20},{26,790791,20},{27,790792,20},{28,790793,20},{29,790794,20},{30,790795,20},{31,790796,20},{32,7,3},{33,13814,14},{34,99,8},{35,790797,20},{36,3452,12},{37,6179,13},{38,3090,12},{39,354,9},{40,1712,11},{41,1713,11},{42,24713,15},{43,1653,12},{44,104,7},{45,102,8},{46,93,7},{47,13815,14},{48,176,8},{49,421,9},{50,195,9},{51,193,9},{52,710,10},{53,388,10},{54,773,10},{55,827,11},{56,1716,11},{57,742,10},{58,6182,13},{59,3304,13},{60,24714,15},{61,24715,15},{62,27624,15},{63,6183,13},{64,3305,13},{65,192,8},{66,196,9},{67,387,9},{68,207,9},{69,711,10},{70,1727,11},{71,740,10},{72,857,10},{73,430,9},{74,743,10},{75,813,11},{76,384,10},{77,420,9},{78,859,10},{79,395,10},{80,741,10},{81,27625,15},{82,3088,12},{83,211,8},{84,100,8},{85,407,10},{86,824,11},{87,202,9},{88,385,10},{89,812,11},{90,825,11},{91,790798,20},{92,790799,20},{93,790800,20},{94,790801,20},{95,790802,20},{96,790803,20},{97,10,4},{98,106,7},{99,54,6},{100,16,5},{101,1,3},{102,45,6},{103,35,6},{104,25,5},{105,4,4},{106,1717,11},{107,184,8},{108,13,5},{109,49,6},{110,7,4},{111,5,4},{112,34,6},{113,394,10},{114,0,4},{115,1,4},{116,9,4},{117,47,6},{118,89,7},{119,110,7},{120,862,10},{121,111,7},{122,389,10},{123,27626,15},{124,790804,20},{125,27627,15},{126,790805,20},{127,790806,20}};




	//for(i=0; i<LENGTH; i++)
	//{
		//haffList[i].asciiCode = (char)i;
		//XUartLite_SendByte(RS232_BASEADDR,i);
		//XUartLite_SendByte(RS232_BASEADDR,haffList[i].haffCodeLen);
	//}
	//print("-- Entering main(1) --\r\n");	


	curIndex = curLen = 0;
	rearCode = haffList[REARPOS].haffCode;
	//for(i=97; i<123; i++)
		//XUartLite_SendByte(RS232_BASEADDR,haffList[i].haffCode);

	rearCodeLen = haffList[REARPOS].haffCodeLen;

	

	while(inData != 0)                    //while(!feof(inputFile))
	{
		count = 0;
		outputData = 0;

		//XUartLite_SendByte(RS232_BASEADDR,inData);
		//break;
		
		while(count<8)
		{
			if(curIndex == curLen)
			{

				inData = Temp[j++];
				//XUartLite_SendByte(RS232_BASEADDR,inData);


				if(inData==0)
				{
					if(count==0)
						outputData = 0;
					else
					{
						for(i=0;i<8-count;i++)
						{
							outputData<<=1;
							outputData|=((rearCode>>(rearCodeLen-1-i))&0x01);
						}
					}
					break;

				}
				curCode=haffList[inData].haffCode;
				curLen=haffList[inData].haffCodeLen;
				realLen=getBinLen(curCode);	
				i=curLen-realLen;
				curIndex=0;
				
			}
			if(i>0)
			{
				outputData<<=1;
				i--;
			}
			else
			{
				tmpBinData=(curCode>>(curLen-curIndex-1))&0x01;
				outputData<<=1;
				outputData=outputData|(char)tmpBinData;
				
			}
			curIndex++;
			count++;
		}
		//XUartLite_SendByte(RS232_BASEADDR,outputData);
		Temp[k++] = outputData;//fputc(outputData,outputFile);
		
		
		
	}

	//XUartLite_SendByte(RS232_BASEADDR,61);
	//int w;
	//for(w=0; w<BUFFER; w++)
		//XUartLite_SendByte(RS232_BASEADDR,Temp[w]);
	//XUartLite_SendByte(RS232_BASEADDR,k);
	while(k<BUFFER)
	{
		
		//if(Temp[k]==0)  
		//{
			Temp[k++] = 0;
			//XUartLite_SendByte(RS232_BASEADDR,Temp[k]);
		//}
		//else
		//	break;
	}
//	以上文本一转换为huffman码
	//////////////////////////////////////////////////////////////////////////////////

	i=0;
	//XUartLite_SendByte(RS232_BASEADDR,i);
	//print("-- The encrypted code is --\r\n");
	while(i<BUFFER)
    {
		if(Temp[i]!=0)
			XUartLite_SendByte(RS232_BASEADDR,Temp[i]);
		else
			break;
		i++;
    }

	//for(i=0; i<100; i++)
	//	XUartLite_SendByte(RS232_BASEADDR,Temp[i]);
  


		/*	char c;
    While UART receive FIFO has data 
    while (!XUartLite_mIsReceiveEmpty(XPAR_RS232_BASEADDR)) {
       Read a character 
      c = XUartLite_RecvByte(XPAR_RS232_BASEADDR);*/

   //print("-- Exiting main() --\r\n");
   //break;   return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -