source: caseStudy_Huffmann/huffmann/huff_ack/huff_reset_ext.v @ 105

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

Hufmann case study

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