source: caseStudy_Huffmann/huffmann/huff_TMR/huff_TMR.v @ 105

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

Hufmann case study

File size: 8.2 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 main(clk, addr);
57    input clk;
58    input [4:0] addr;
59
60    wire  cipher;
61    wire [7:0] character, plain;
62
63    huffmanEnc encoder (clk, addr, cipher, character);
64
65    huffmanDec decoder (clk, cipher, plain);
66
67    // Latch data that we want to refer to in properties.
68    reg        ci;
69    reg [7:0]  ch;
70
71    initial begin
72        ci = 0;
73        ch = 0;
74    end
75
76    always @ (posedge clk) begin
77        ci = cipher;
78        ch = character;
79    end
80
81endmodule // main
82
83
84module huffmanEnc (clk, addr, cipher, character);
85    input        clk;
86    input [4:0]  addr;
87    output       cipher;
88    output [7:0] character;
89   
90    reg [7:0]  character;
91
92    // This function is the map from symbols (ASCII space and uppercase
93    // letters) to codes.  Each code consists of from 3 to 9 bits.
94    // Since the codes are of variable length, an additional
95    // bit is used to mark the end of the symbol.  This bit is the
96    // leftmost 1.  The code is sent out LSB first; hence, it is reversed
97    // in this map.  For instance, 0000010100 (the entry of the map for S)
98    // says that the code for S is 0010.
99    function [9:0] code;
100        input [7:0] c;
101        begin: _code
102            case (c)
103              69: code = 10'b0000001010; // E
104              32: code = 10'b0000001011; // space
105              83: code = 10'b0000010100; // S
106              65: code = 10'b0000011110; // A
107              73: code = 10'b0000010001; // I
108              79: code = 10'b0000011001; // O
109              82: code = 10'b0000010101; // R
110              78: code = 10'b0000011101; // N
111            84: code = 10'b0000011111; // T
112              85: code = 10'b0000100000; // U
113              80: code = 10'b0000110000; // P
114              70: code = 10'b0000101000; // F
115              67: code = 10'b0000111000; // C
116              76: code = 10'b0000111100; // L
117              72: code = 10'b0000100110; // H
118              68: code = 10'b0000100111; // D
119              87: code = 10'b0001101100; // W
120              71: code = 10'b0001010110; // G
121              89: code = 10'b0001110110; // Y
122              77: code = 10'b0001110111; // M
123              66: code = 10'b0010010111; // B
124              86: code = 10'b0011010111; // V
125              81: code = 10'b0100001100; // Q
126              75: code = 10'b0101001100; // K
127              88: code = 10'b0111001100; // X
128              90: code = 10'b1010001100; // Z
129              74: code = 10'b1110001100; // J
130              default: code = 10'b0;
131            endcase // case(character)
132        end
133    endfunction // code
134
135    // This function supplies the ASCII codes of the symbols.
136    function [7:0] ROM;
137        input [4:0] address;
138        begin: _ROM
139            if (address < 26)
140              ROM = 65 + {3'b0, address};
141            else
142              ROM = 32;
143        end
144    endfunction // ROM
145   
146    wire [9:0] shiftreg;
147    reg [9:0] shiftreg1;
148    reg [9:0] shiftreg2;
149    reg [9:0] shiftreg3;
150
151   function [9:0] shift;
152      input [9:0] d1;
153      input [9:0] d2;
154      input [9:0] d3;
155    begin: _shift
156    if((d1 == d2) && (d1 == d3))
157      shift = d1;
158     else if ((d1 == d2) && (d1!= d3))
159      shift = d1;
160      else if ((d1 == d3) && (d3!= d2))
161      shift = d1;
162       else if ((d3 == d2) && (d3!= d1))
163      shift= d2;
164       else shift=10'b1;
165    end
166    endfunction   
167   
168    initial begin
169      character = ROM(addr);
170      shiftreg1 = code(character);
171      shiftreg2 = code(character); 
172      shiftreg3 = code(character);
173    end
174
175    always @ (posedge clk) begin
176    if (shiftreg[9:1] == 1) begin
177          character = ROM(addr);
178          shiftreg1 = code(character); // load a new code
179          shiftreg2 = code(character); 
180          shiftreg3 = code(character); 
181    end else begin
182          shiftreg1 = {1'b0, shiftreg1[9:1]}; // shift right
183          shiftreg2 = {1'b0, shiftreg2[9:1]};
184          shiftreg3 = {1'b0, shiftreg3[9:1]};   
185       end
186    end
187    assign shiftreg = shift(shiftreg1,shiftreg2,shiftreg3);
188    assign cipher = shiftreg[0];
189
190endmodule // huffmanEnc
191
192
193// The output plain is 0 except for one clock cycle when a character has
194// been decoded.
195module huffmanDec (clk,cipher,plain);
196    input        clk;
197    input        cipher; 
198    output [7:0] plain;
199
200    reg [9:0]    state;
201           
202    wire         leaf;
203    wire [7:0]   character;
204
205    initial state = 0;
206
207    // This function maps states to characters.  All non-leaf states are
208    // mapped to NUL.  The leaf states are mapped to the ASCII code of the
209    // corresponding symbol.
210    function [7:0] map;
211        input [9:0] state;
212        begin: _map
213            case (state)
214                9: map = 69; // E
215               13: map = 32; // space
216               17: map = 83; // S
217               22: map = 65; // A
218               23: map = 73; // I
219               24: map = 79; // O
220               25: map = 82; // R
221               26: map = 78; // N
222               30: map = 84; // T
223               31: map = 85; // U
224               32: map = 80; // P
225               33: map = 70; // F
226               34: map = 67; // C
227               38: map = 76; // L
228               43: map = 72; // H
229               59: map = 68; // D
230               76: map = 87; // W
231               89: map = 71; // G
232               90: map = 89; // Y
233              122: map = 77; // M
234              243: map = 66; // B
235              244: map = 86; // V
236              303: map = 81; // Q
237              305: map = 75; // K
238              306: map = 88; // X
239              609: map = 90; // Z
240              610: map = 74; // J
241              default: map = 0;
242            endcase // case(state)
243        end // block: _map
244    endfunction // map
245
246    assign plain = map(state);
247    assign leaf = plain != 0;
248
249    always @ (posedge clk) begin
250        state = (leaf ? 0 : {state[8:0],1'b0}) + (cipher ? 2 : 1);
251    end
252
253endmodule // huffmanDec
Note: See TracBrowser for help on using the repository browser.