Sei sulla pagina 1di 5

--------------------------------------------------------------------------------------------------------------------------------------------------------------------//HUFFMAN CODING//-------------------------------------------------************************------------------------------------------------------------------------------------------------//PAKEGE DECLERATION//------------------library ieee;

use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
package huffmanalgopakege is
--generic ( MessageSize : integer := 80 ) ;
type datasize is array(0 to (51)) of std_logic_vector(0 to 7);
type stringx is array(0 to 51) of string(1 to 100);
type integerx is array(0 to 51) of integer;
type datacount is array(0 to 51) of integer;
type Header is
record
Character : std_logic_vector(0 to 7) ;
HuffmanCode : std_logic_vector(0 to 51) ;
HuffmanCodeSize : integer ;
end record ;
type Head is array(0 to 51) of Header ;
end huffmanalgopakege;
--------//END OF PAKEGE//--------library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
library huffmanalgo;
use huffmanalgo.huffmanalgopakege.all;
entity Huffmanalgorithm is
generic (MessageSize : integer := 51 ) ;
port( Input : in std_logic_vector(0 to 159);
InLimit : in integer ;
InHeader : inout Head ;
InHeaderLimit : inout integer ;
--input table is needed
Mode : in std_logic ;
OutLimit : out integer ;
Output: out std_logic_vector(0 to 159);
OutHeader : out Head ;
OutHeaderLimit : out integer);
end Huffmanalgorithm;

---MODE=0,COMPRESSION
---MODE=1,DECOMPRESSION

architecture algorithm of Huffmanalgorithm is

begin
process(Input,InLimit,Mode)
----//FUNCTION TO CONVERT STRING TO std_logic_vector
variable SymbolsCount,NodeCount,Temp,CodeSize,CodeCount,Limit,Index: inte
ger ;
variable UniqueSymbols: datasize ;
variable TempSymbol : std_logic_vector(0 to 7) ;

variable
variable
variable
variable
variable
variable
variable
variable

Message : std_logic_vector(0 to 159) ;


SymbolCount,TempCount,NodesCount,HuffmanCodeSize:datacount ;
Found:std_logic;
Tree,HuffmanCodes:stringx;
TempCharacter,Code:string(1 to 100);
TempInput : datasize ;
TempHeader : Head ;
TempCode: std_logic_vector(0 to 51) ;

function StringToStdLogic( a:string;b:integer)


return std_logic_vector is
--only zero and one is converted
variable x:std_logic_vector(0 to b-1);
variable ReturnValue:std_logic_vector(0 to b-1);
begin
for i in 1 to b loop
case a(i) is
when '0'=> x(i-1):='0';
when '1'=> x(i-1):='1';
when others =>null;
end case ;
end loop ;
ReturnValue:=x;
return ReturnValue;
end StringToStdLogic;
-----END OF FUNCTION---------begin
--------//CHANGE INPUT STREAM TO INPUT TABLE (ARRAY OF CHARACTORS)//------for i in 0 to (InLimit-1) loop
TempInput(i):=Input((i*8) to ((i*8)+7)) ;
end loop ;
-------//COUNT THE DIFFERENT SYMBOLS //------SymbolsCount := 0 ;
for i in 0 to (InLimit-1) loop
Found := '0';
for j in 0 to (SymbolsCount-1) loop
if(TempInput(i) = UniqueSymbols(j))then
Found := '1' ;
end if;
end loop ;
if(Found = '0')then
SymbolsCount := SymbolsCount + 1;
UniqueSymbols((SymbolsCount-1)) := TempInput(i) ;
end if;
end loop ;
--Setting Counts to 0
for i in 0 to (MessageSize-1) loop

--SymbolsCount=NO.OF UN

IQUE CHR.
SymbolCount(i) := 0 ;

--SymbolCount=NO.OF EAC

H CHR.
end loop;
----------//COUNTING OF EACH SYMBOL// -----for i in 0 to (SymbolsCount-1) loop
for j in 0 to (InLimit-1) loop
if(UniqueSymbols(i) = TempInput(j))then
SymbolCount(i) := SymbolCount(i) + 1 ;
end if;
end loop ;
end loop ;
---------//CREATING TREE//--------------for i in 0 to (MessageSize-1) loop
NodesCount(i) := 1;
end loop ;
---------//CHARACTER USED IN TREE//------Tree(0)(1 to 1) := "A" ;
Tree(1)(1 to 1) := "B" ;
Tree(2)(1 to 1) := "C" ;
Tree(3)(1 to 1) := "D" ;
Tree(4)(1 to 1) := "E" ;
Tree(5)(1 to 1) := "F" ;
Tree(6)(1 to 1) := "G" ;
Tree(7)(1 to 1) := "H" ;
Tree(8)(1 to 1) := "I" ;
Tree(9)(1 to 1):= "J" ;
Tree(10)(1 to 1) := "K" ;
Tree(11)(1 to 1) := "L" ;
Tree(12)(1 to 1):= "M" ;
Tree(13)(1 to 1) := "N" ;
Tree(14)(1 to 1) := "O" ;
Tree(15)(1 to 1) := "P" ;
Tree(16)(1 to 1):= "Q" ;
Tree(17)(1 to 1) := "R" ;
Tree(18)(1 to 1) := "S" ;
Tree(19)(1 to 1) := "T" ;
Tree(20)(1 to 1) := "U" ;
Tree(21)(1 to 1) := "V" ;
Tree(22)(1 to 1) := "W" ;
Tree(23)(1 to 1) := "X" ;
Tree(24)(1 to 1) := "Y" ;
Tree(25)(1 to 1) := "Z" ;
Tree(26)(1 to 1) := "a" ;
Tree(27)(1 to 1) := "b" ;
Tree(28)(1 to 1) := "c" ;
Tree(29)(1 to 1) := "d" ;
Tree(30)(1 to 1) := "e" ;
Tree(31)(1 to 1) := "f" ;
Tree(32)(1 to 1) := "g" ;
Tree(33)(1 to 1) := "h" ;
Tree(34)(1 to 1) := "i" ;
Tree(35)(1 to 1) := "j" ;
Tree(36)(1 to 1) := "k" ;

Tree(37)(1
Tree(38)(1
Tree(39)(1
Tree(40)(1
Tree(41)(1
Tree(42)(1
Tree(43)(1
Tree(44)(1
Tree(45)(1
Tree(46)(1
Tree(47)(1
Tree(48)(1
Tree(49)(1
Tree(50)(1
Tree(51)(1

to
to
to
to
to
to
to
to
to
to
to
to
to
to
to

1)
1)
1)
1)
1)
1)
1)
1)
1)
1)
1)
1)
1)
1)
1)

:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=

"l"
"m"
"n"
"o"
"p"
"q"
"r"
"s"
"t"
"u"
"v"
"w"
"x"
"y"
"z"

;
;
;
;
;
;
;
;
;
;
;
;
;
;
;

--------//THE CHARACTER COUNT IS USED TO FORM THE TREE(NODE)//--NodeCount := SymbolsCount ; --number of characters
TempCount := SymbolCount ; --count of each characters
while ( NodeCount > 1 ) loop
------SORT THE CHARACTER ACCORDING THEIR COUNT(ASSENDING)//--for i in 0 to (NodeCount-2) loop
for j in i+1 to (NodeCount-1) loop
if(TempCount(i)>TempCount(j))then
--swapping TempCount
Temp := TempCount(i) ;
TempCount(i) := TempCount(j) ;
TempCount(j) := Temp ;
--swapping Tree character
TempCharacter := Tree(i) ;
Tree(i) := Tree(j) ;
Tree(j) := TempCharacter ;
--swaping nodescount
Temp := NodesCount(i) ;
NodesCount(i) := NodesCount(j) ;
NodesCount(j) := Temp ;
end if;
end loop ;
end loop ;
--merging characters((,),+)---Temp := NodesCount(0) + NodesCount(1) + 3 ;
Tree(1)(1 to Temp):="(" & Tree(0)(1 to NodesCount(0)) & "+" & Tree(1)(
1 to NodesCount(1)) & ")" ;
TempCount(1) := TempCount(0) + TempCount(1) ;
NodesCount(1):= NodesCount(0) + NodesCount(1) + 3 ;
--Rearranging
for i in 0 to NodeCount-1 loop
Tree(i):= Tree(i+1) ;
TempCount(i) := TempCount(i+1) ;

NodesCount(i) := NodesCount(i+1) ;
end loop ;
--decrementing NodeCount
NodeCount := NodeCount - 1 ;
end loop;
---------TREE CREATED----------------