OpenCores
URL https://opencores.org/ocsvn/common/common/trunk

Subversion Repositories common

[/] [common/] [trunk/] [grey_to_binary.v] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 bbeaver
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// grey_to_binary #(N), binary_to_grey #(N)                     ////
4
////                                                              ////
5
//// This file is part of the general opencores effort.           ////
6
//// <http://www.opencores.org/cores/misc/>                       ////
7
////                                                              ////
8
//// Module Description:                                          ////
9
////    Example of how to convert Grey Code to Binary.            ////
10
////    Example of how to convert Binary to Grey Code.            ////
11
////                                                              ////
12
//// CRITICAL USAGE NOTE:                                         ////
13
////    These functions produce combinational outputs which       ////
14
////      have glitches.  To use these safely, the outputs        ////
15
////      must be latched using the same clock before and         ////
16
////      after the combinational function.                       ////
17
////                                                              ////
18
////    There are other sequences of numbers which share the      ////
19
////      property of Grey Code that only 1 bit transitions per   ////
20
////      value change.                                           ////
21
////    The sequence 0x00, 0x1, 0x3, 0x2, 0x6, 0x4 is one such    ////
22
////      sequence.                                               ////
23
////    It should be possible to make a library which counts      ////
24
////      in sequences less than 2**n long, yet which still       ////
25
////      change only 1 bot per increment.                        ////
26
////                                                              ////
27
//// To Do:                                                       ////
28
//// Might make this handle more than 16 bits.                    ////
29
////                                                              ////
30
//// Author(s):                                                   ////
31
//// - Anonymous                                                  ////
32
////                                                              ////
33
//////////////////////////////////////////////////////////////////////
34
////                                                              ////
35
//// Copyright (C) 2000 Anonymous and OPENCORES.ORG               ////
36
////                                                              ////
37
//// This source file may be used and distributed without         ////
38
//// restriction provided that this copyright statement is not    ////
39
//// removed from the file and that any derivative work contains  ////
40
//// the original copyright notice and the associated disclaimer. ////
41
////                                                              ////
42
//// This source file is free software; you can redistribute it   ////
43
//// and/or modify it under the terms of the GNU Lesser General   ////
44
//// Public License as published by the Free Software Foundation; ////
45
//// either version 2.1 of the License, or (at your option) any   ////
46
//// later version.                                               ////
47
////                                                              ////
48
//// This source is distributed in the hope that it will be       ////
49
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
50
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
51
//// PURPOSE. See the GNU Lesser General Public License for more  ////
52
//// details.                                                     ////
53
////                                                              ////
54
//// You should have received a copy of the GNU Lesser General    ////
55
//// Public License along with this source; if not, download it   ////
56
//// from <http://www.opencores.org/lgpl.shtml>                   ////
57
////                                                              ////
58
//////////////////////////////////////////////////////////////////////
59 5 bbeaver
//
60 18 bbeaver
// $Id: grey_to_binary.v,v 1.5 2001-10-22 12:29:08 bbeaver Exp $
61 5 bbeaver
//
62 6 bbeaver
// CVS Revision History
63 5 bbeaver
//
64 6 bbeaver
// $Log: not supported by cvs2svn $
65
// Revision 1.3  2001/09/03 12:12:44  Blue Beaver
66
// no message
67 5 bbeaver
//
68 6 bbeaver
// Revision 1.2  2001/09/03 12:09:24  Blue Beaver
69
// no message
70 5 bbeaver
//
71
//
72
 
73
`timescale 1ns/1ps
74
 
75
// Convert 2-bit up to 16-bit binary value into same sized grey-code value
76
 
77
module bin_to_grey_code (
78
  grey_code_out,
79
  binary_in
80
);
81
  parameter NUM_BITS = 1;  // instantiate as "bin_to_grey_code #(width) instance ()"
82
 
83
  output [NUM_BITS - 1 : 0] grey_code_out;
84
  input  [NUM_BITS - 1 : 0] binary_in;
85
 
86
// Consider the sequences
87
//   Binary  Grey Code
88
//     00       00
89
//     01       01
90
//     10       11
91
//     11       10
92
// It seems that G[1] = B[1], and G[0] = B[1] ^ B[0];
93
// Now consider the sequences
94
//   Binary  Grey Code
95
//     000      000
96
//     001      001
97
//     010      011
98
//     011      010
99
//     100      110
100
//     101      111
101
//     110      101
102
//     111      100
103
// It seems that G[2] = B[2], and
104
//               G[1] = B[2] ^ B[1], and
105
//               G[0] = B[1] ^ B[0];
106
//
107
// But how to write that using a parameter?  Well, instead of
108
//   figuring it out, how about just making something which works
109
//   for a range of widths, like 2 to 16?
110
 
111
  wire   [15:0] widened_input = {binary_in[NUM_BITS - 1 : 0], {16 - NUM_BITS{1'b0}}};
112
 
113
  wire   [15:0] widened_output = {
114
                      widened_input[15],
115
                      widened_input[15] ^ widened_input[14],
116
                      widened_input[14] ^ widened_input[13],
117
                      widened_input[13] ^ widened_input[12],
118
                      widened_input[12] ^ widened_input[11],
119
                      widened_input[11] ^ widened_input[10],
120
                      widened_input[10] ^ widened_input[9],
121
                      widened_input[9]  ^ widened_input[8],
122
                      widened_input[8]  ^ widened_input[7],
123
                      widened_input[7]  ^ widened_input[6],
124
                      widened_input[6]  ^ widened_input[5],
125
                      widened_input[5]  ^ widened_input[4],
126
                      widened_input[4]  ^ widened_input[3],
127
                      widened_input[3]  ^ widened_input[2],
128
                      widened_input[2]  ^ widened_input[1],
129
                      widened_input[1]  ^ widened_input[0]
130
                  };
131
 
132
  assign  grey_code_out[NUM_BITS - 1 : 0] = widened_output[15 : 16 - NUM_BITS];
133
 
134
// synopsys translate_off
135
  initial
136
  begin
137
    if (NUM_BITS < 2)
138
    begin
139
      $display ("*** Exiting because %m bin_to_grey_code Number of bits %d < 2",
140
                   NUM_BITS);
141
      $finish;
142
    end
143
    if (NUM_BITS > 16)
144
    begin
145
      $display ("*** Exiting because %m bin_to_grey_code Number of bits %d > 16",
146
                   NUM_BITS);
147
      $finish;
148
    end
149
  end
150
// synopsys translate_on
151
endmodule
152
 
153
// Convert 2-bit up to 16-bit binary value into same sized grey-code value
154
 
155
module grey_code_to_bin (
156
  binary_out,
157
  grey_code_in
158
);
159
  parameter NUM_BITS = 1;  // instantiate as "grey_code_to_bin #(width) instance ()"
160
 
161
  output [NUM_BITS - 1 : 0] binary_out;
162
  input  [NUM_BITS - 1 : 0] grey_code_in;
163
 
164
// Consider the sequences
165
//   Grey Code   Binary
166
//       00        00
167
//       01        01
168
//       11        10
169
//       10        11
170
// It seems that B[1] = G[1], and B[0] = G[1] ^ G[0];
171
// Now consider the sequences
172
//   Grey Code   Binary
173
//      000       000
174
//      001       001
175
//      011       010
176
//      010       011
177
//      110       100
178
//      111       101
179
//      101       110
180
//      100       111
181
// It seems that B[2] = G[2], and
182
//               B[1] = G[2] ^ G[1], and
183
//               B[0] = G[2] ^ G[1] ^ G[0];
184
//
185
// But how to write that using a parameter?  Well, instead of
186
//   figuring it out, how about just making something which works
187
//   for a range of widths, like 2 to 16?
188
 
189
  wire   [15:0] widened_input = {grey_code_in[NUM_BITS - 1 : 0], {16 - NUM_BITS{1'b0}}};
190
  wire    xor_15_12 = widened_input[15] ^ widened_input[14]
191
                    ^ widened_input[13] ^ widened_input[12];
192
  wire    xor_11_10 = widened_input[11] ^ widened_input[10];
193
  wire    xor_11_8  = widened_input[11] ^ widened_input[10]
194
                    ^ widened_input[9]  ^ widened_input[8];
195
  wire    xor_7_6   = widened_input[7]  ^ widened_input[6];
196
  wire    xor_7_4   = widened_input[7]  ^ widened_input[6]
197
                    ^ widened_input[5]  ^ widened_input[4];
198
  wire    xor_3_2   = widened_input[3]  ^ widened_input[2];
199
  wire    xor_1_0   = widened_input[1]  ^ widened_input[0];
200
 
201
  wire   [15:0] widened_output = {
202
                      widened_input[15],
203
                      widened_input[15] ^ widened_input[14],
204
                      widened_input[15] ^ widened_input[14] ^ widened_input[13],
205
                      xor_15_12,
206
                      xor_15_12 ^ widened_input[11],
207
                      xor_15_12 ^ xor_11_10,
208
                      xor_15_12 ^ xor_11_10 ^ widened_input[9],
209
                      xor_15_12 ^ xor_11_8,
210
                      xor_15_12 ^ xor_11_8 ^ widened_input[7],
211
                      xor_15_12 ^ xor_11_8 ^ xor_7_6,
212
                      xor_15_12 ^ xor_11_8 ^ xor_7_6 ^ widened_input[5],
213
                      xor_15_12 ^ xor_11_8 ^ xor_7_4,
214
                      xor_15_12 ^ xor_11_8 ^ xor_7_4 ^ widened_input[3],
215
                      xor_15_12 ^ xor_11_8 ^ xor_7_4 ^ xor_3_2,
216
                      xor_15_12 ^ xor_11_8 ^ xor_7_4 ^ xor_3_2 ^ widened_input[1],
217
                      xor_15_12 ^ xor_11_8 ^ xor_7_4 ^ xor_3_2 ^ xor_1_0
218
                  };
219
 
220
  assign  binary_out[NUM_BITS - 1 : 0] = widened_output[15 : 16 - NUM_BITS];
221
 
222
// synopsys translate_off
223
  initial
224
  begin
225
    if (NUM_BITS < 2)
226
    begin
227
      $display ("*** Exiting because %m grey_code_to_bin Number of bits %d < 2",
228
                   NUM_BITS);
229
      $finish;
230
    end
231
    if (NUM_BITS > 16)
232
    begin
233
      $display ("*** Exiting because %m grey_code_to_bin Number of bits %d > 16",
234
                   NUM_BITS);
235
      $finish;
236
    end
237
  end
238
// synopsys translate_on
239
endmodule
240
 
241
 `define TEST_GREY_CODE
242
`ifdef TEST_GREY_CODE
243
module test_grey_code;
244
  reg    [7:0] test_val;
245
  wire   [1:0] grey_2;
246
  wire   [2:0] grey_3;
247
  wire   [3:0] grey_4;
248
 
249
  wire   [1:0] bin_2;
250
  wire   [2:0] bin_3;
251
  wire   [3:0] bin_4;
252
 
253
  initial
254
  begin
255
    for (test_val = 8'h00; test_val < 8'h04; test_val = test_val + 8'h01)
256
    begin
257
      # 0; $display ("test val, result %x %x %x", test_val[1:0], grey_2[1:0], bin_2[1:0]);
258
      if (test_val[1:0] !== bin_2[1:0])
259
        $display ("*** Encode, Decode failed %x %x", test_val[1:0], bin_2[1:0]);
260
    end
261
    $display (" ");
262
    for (test_val = 8'h00; test_val < 8'h08; test_val = test_val + 8'h01)
263
    begin
264
      # 0; $display ("test val, result %x %x %x", test_val[2:0], grey_3[2:0], bin_3[2:0]);
265
      if (test_val[2:0] !== bin_3[2:0])
266
        $display ("*** Encode, Decode failed %x %x", test_val[2:0], bin_3[2:0]);
267
    end
268
    $display (" ");
269
    for (test_val = 8'h00; test_val < 8'h10; test_val = test_val + 8'h01)
270
    begin
271
      # 0; $display ("test val, result %x %x %x", test_val[3:0], grey_4[3:0], bin_4[3:0]);
272
      if (test_val[3:0] !== bin_4[3:0])
273
        $display ("*** Encode, Decode failed %x %x", test_val[3:0], bin_4[3:0]);
274
    end
275
 
276
  end
277
 
278
bin_to_grey_code #(2) bin_to_grey_code_2 (
279
  .grey_code_out              (grey_2[1:0]),
280
  .binary_in                  (test_val[1:0])
281
);
282
bin_to_grey_code #(3) bin_to_grey_code_3 (
283
  .grey_code_out              (grey_3[2:0]),
284
  .binary_in                  (test_val[2:0])
285
);
286
bin_to_grey_code #(4) bin_to_grey_code_4 (
287
  .grey_code_out              (grey_4[3:0]),
288
  .binary_in                  (test_val[3:0])
289
);
290
 
291
grey_code_to_bin #(2) grey_code_to_bin_2 (
292
  .binary_out                 (bin_2[1:0]),
293
  .grey_code_in               (grey_2[1:0])
294
);
295
grey_code_to_bin #(3) grey_code_to_bin_3 (
296
  .binary_out                 (bin_3[2:0]),
297
  .grey_code_in               (grey_3[2:0])
298
);
299
grey_code_to_bin #(4) grey_code_to_bin_4 (
300
  .binary_out                 (bin_4[3:0]),
301
  .grey_code_in               (grey_4[3:0])
302
);
303
 
304
endmodule
305
`endif  // TEST_GREY_CODE
306
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.