source: caseStudy_Huffmann/huffmann/huff_any_text/huff_with_env_vis.v @ 105

Last change on this file since 105 was 105, checked in by cecile, 12 years ago

Hufmann case study

File size: 7.5 KB
Line 
1// Model of connected Huffman encoder and decoder.
2// The alphabet consists of the uppercase letters and the space.
3// The Huffman tree used by encoder and decoder is shown below.
4// All left branches are labeled 0, and all right branches are labeled 1.
5//
6//                       +-------------( )---------------+
7//                       |                               |
8//                       |                               |
9//              +-------( )------+               +------( )-----+
10//              |                |               |              |
11//              |                |               |              |
12//        +----( )----+         ( )          +--( )--+         ( )
13//        |           |         / \          |       |         / \
14//        |           |        |   |         |       |        |   |
15//    +--( )--+      ( )      [E] ( )       ( )     ( )      [ ] ( )
16//    |       |      / \          / \       / \     / \          / \
17//    |       |     |   |        |   |     |   |   |   |        |   |
18//   ( )     ( )   [S] ( )      ( ) [A]   [I] [O] [R] [N]      ( ) [T]
19//   / \     / \       / \      / \                            / \
20//  |   |   |   |     |   |    |   |                          |   |
21// [U] [P] [F] [C]   ( ) [L]  [H] ( )                        [D] ( )
22//                   / \          / \                            / \
23//                  |   |        |   |                          |   |
24//            +----( ) [W]      [G] [Y]                        ( ) [M]
25//            |      \                                         / \
26//            |       |                                       |   |
27//           ( )     ( )                                     [B] [V]
28//           / \     / \
29//          |   |   |   |
30//         [Q] ( ) [K] [X]
31//             / \
32//            |   |
33//           [Z] [J]
34//
35// As an example, the code of W is 001101.
36//
37// This tree is based on the following assumed frequencies.
38//
39//  E 130  T 93  N 78  R 77  I 74  O 74  A 73  S 63  D 44
40//  H  35  L 35  C 30  F 28  P 27  U 27  M 25  Y 19  G 16
41//  W  16  V 13  B  9  X  5  K  3  Q  3  J  2  Z  1
42//
43// That is, it is assumed that there are 130 Es for every thousand letters.
44// It is further assumed that there are 182 spaces for every 1000 letters.
45//
46// The encoder retrieves the code for each symbol from a map, and shifts it
47// out one bit at the time.  The decoder is a finite state machine whose
48// state transition graph is obtained from the tree by adding acs from the
49// leaves back to the top of the tree.  (To the second level nodes to be
50// precise.)  Each node uses ten bits for its encoding.  The code of the root
51// is 0.  If a state is not a leaf of the tree, and its encoding is n, then
52// the encodings of its two children are 2n+1 and 2n+2.
53
54// Author: Fabio Somenzi <Fabio@Colorado.EDU>
55
56module huffmann(clk,val,addr,ack,plain);
57input clk;
58input val;
59input [4:0]addr;
60output ack;
61output [7:0] plain;
62
63huffmanEnc encoder (clk, val, addr, cipher,ack,start);
64
65huffmanDec decoder (clk, cipher, start, plain);
66
67endmodule
68
69module huffmanEnc (clk, val_addr, addr, cipher,ack,start);
70    input        clk;
71    input        val_addr;
72    input [4:0]  addr;
73    output       ack;
74    output           cipher;
75    output       start;
76
77    reg [7:0]    character;
78
79    // This function is the map from symbols (ASCII space and uppercase
80    // letters) to codes.  Each code consists of from 3 to 9 bits.
81    // Since the codes are of variable length, an additional
82    // bit is used to mark the end of the symbol.  This bit is the
83    // leftmost 1.  The code is sent out LSB first; hence, it is reversed
84    // in this map.  For instance, 0000010100 (the entry of the map for S)
85    // says that the code for S is 0010.
86    function [9:0] code;
87        input [7:0] c;
88        begin: _code
89            case (c)
90              69: code = 10'b0000001010; // E
91              32: code = 10'b0000001011; // space
92              83: code = 10'b0000010100; // S
93              65: code = 10'b0000011110; // A
94              73: code = 10'b0000010001; // I
95              79: code = 10'b0000011001; // O
96              82: code = 10'b0000010101; // R
97              78: code = 10'b0000011101; // N
98          84: code = 10'b0000011111; // T
99              85: code = 10'b0000100000; // U
100              80: code = 10'b0000110000; // P
101              70: code = 10'b0000101000; // F
102              67: code = 10'b0000111000; // C
103              76: code = 10'b0000111100; // L
104              72: code = 10'b0000100110; // H
105              68: code = 10'b0000100111; // D
106              87: code = 10'b0001101100; // W
107              71: code = 10'b0001010110; // G
108              89: code = 10'b0001110110; // Y
109              77: code = 10'b0001110111; // M
110              66: code = 10'b0010010111; // B
111              86: code = 10'b0011010111; // V
112              81: code = 10'b0100001100; // Q
113              75: code = 10'b0101001100; // K
114              88: code = 10'b0111001100; // X
115              90: code = 10'b1010001100; // Z
116              74: code = 10'b1110001100; // J
117              default: code = 10'b0;
118            endcase // case(character)
119        end
120    endfunction // code
121
122    // This function supplies the ASCII codes of the symbols.
123    function [7:0] ROM;
124        input [4:0] address;
125        begin: _ROM
126            if (address < 26)
127              ROM = 65 + {3'b0, address};
128            else
129              ROM = 32;
130        end
131    endfunction // ROM
132
133    reg [9:0] shiftreg;
134    reg start;
135    reg r_ack;
136
137    initial begin
138        character = 0 ;//ROM(addr);
139        shiftreg = 2;//code(character);
140    start = 0;
141    r_ack = 0;
142    end
143    assign cipher = shiftreg[0];
144    assign ack = (shiftreg[9:1] == 2) || (shiftreg[9:1] == 3 ) || r_ack;
145
146    always @ (posedge clk) 
147        begin
148        if (shiftreg[9:1] == 1 )  // added by Cecile 24.06.11
149        begin
150      if(val_addr == 1)
151        begin
152          start = 1;
153              character = ROM(addr);
154              shiftreg = code(character); // load a new code
155          r_ack = 0;
156        end
157      else
158        begin
159           character = 0;
160               shiftreg = 2;
161           start = 0;
162           r_ack = 1;
163        end
164        end 
165        else 
166                begin
167             shiftreg = {1'b0, shiftreg[9:1]}; // shift right
168         start = 0;
169         r_ack = 0;
170                end
171    end
172
173   endmodule // huffmanEnc
174
175
176// The output plain is 0 except for one clock cycle when a character has
177// been decoded.
178module huffmanDec (clk,cipher,start,plain);
179    input        clk;
180    input            cipher;
181    input        start;
182    output [7:0] plain;
183
184    reg [9:0]    state;
185           
186    wire             leaf;
187    wire [7:0]   character;
188
189    initial state = 0;
190
191    // This function maps states to characters.  All non-leaf states are
192    // mapped to NUL.  The leaf states are mapped to the ASCII code of the
193    // corresponding symbol.
194    function [7:0] map;
195        input [9:0] state;
196        begin: _map
197            case (state)
198                9: map = 69; // E
199               13: map = 32; // space
200               17: map = 83; // S
201               22: map = 65; // A
202               23: map = 73; // I
203               24: map = 79; // O
204               25: map = 82; // R
205               26: map = 78; // N
206           30: map = 84; // T
207               31: map = 85; // U
208               32: map = 80; // P
209               33: map = 70; // F
210               34: map = 67; // C
211               38: map = 76; // L
212               43: map = 72; // H
213               59: map = 68; // D
214               76: map = 87; // W
215               89: map = 71; // G
216               90: map = 89; // Y
217              122: map = 77; // M
218              243: map = 66; // B
219              244: map = 86; // V
220              303: map = 81; // Q
221              305: map = 75; // K
222              306: map = 88; // X
223              609: map = 90; // Z
224              610: map = 74; // J
225              default: map = 0;
226            endcase // case(state)
227        end // block: _map
228    endfunction // map
229
230    assign plain = map(state);
231    assign leaf = plain != 0;
232
233    always @ (posedge clk) begin
234        state = (((leaf || start) ? 0 : {state[8:0],1'b0}) + (cipher ? 2 : 1));
235    end
236
237endmodule // huffmanDec
Note: See TracBrowser for help on using the repository browser.