|
| 1 | +// Copyright 2021 ETH Zurich. |
| 2 | +// Copyright and related rights are licensed under the Solderpad Hardware |
| 3 | +// License, Version 0.51 (the "License"); you may not use this file except in |
| 4 | +// compliance with the License. You may obtain a copy of the License at |
| 5 | +// http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law |
| 6 | +// or agreed to in writing, software, hardware and materials distributed under |
| 7 | +// this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 8 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | +// specific language governing permissions and limitations under the License. |
| 10 | + |
| 11 | +/// Hardware implementation of SystemVerilog's `$onehot()` function. |
| 12 | +/// It uses a tree of half adders and a separate |
| 13 | +/// or reduction tree for the carry. |
| 14 | + |
| 15 | +// Author: Florian Zaruba <[email protected]> |
| 16 | +// Author: Fabian Schuiki <[email protected]> |
| 17 | +// Author: Stefan Mach <[email protected]> |
| 18 | +module cc_onehot #( |
| 19 | + parameter int unsigned Width = 4 |
| 20 | +) ( |
| 21 | + input logic [Width-1:0] d_i, |
| 22 | + output logic is_onehot_o |
| 23 | +); |
| 24 | + // trivial base case |
| 25 | + if (Width == 1) begin : gen_degenerated_onehot |
| 26 | + assign is_onehot_o = d_i; |
| 27 | + end else begin : gen_onehot |
| 28 | + localparam int LVLS = $clog2(Width) + 1; |
| 29 | + |
| 30 | + logic [LVLS-1:0][2**(LVLS-1)-1:0] sum, carry; |
| 31 | + logic [LVLS-2:0] carry_array; |
| 32 | + |
| 33 | + // Extend to a power of two. |
| 34 | + assign sum[0] = $unsigned(d_i); |
| 35 | + |
| 36 | + // generate half adders for each lvl |
| 37 | + // lvl 0 is the input level |
| 38 | + for (genvar i = 1; i < LVLS; i++) begin : gen_lvl |
| 39 | + localparam int unsigned LVLWidth = 2**LVLS / 2**i; |
| 40 | + for (genvar j = 0; j < LVLWidth; j+=2) begin : gen_width |
| 41 | + assign sum[i][j/2] = sum[i-1][j] ^ sum[i-1][j+1]; |
| 42 | + assign carry[i][j/2] = sum[i-1][j] & sum[i-1][j+1]; |
| 43 | + end |
| 44 | + // generate carry tree |
| 45 | + assign carry_array[i-1] = |carry[i][LVLWidth/2-1:0]; |
| 46 | + end |
| 47 | + assign is_onehot_o = sum[LVLS-1][0] & ~|carry_array; |
| 48 | + end |
| 49 | + |
| 50 | +endmodule |
0 commit comments