ic_com/lib/enc_dec/oh_bin/oh2bin.sv

45 lines
1.8 KiB
Systemverilog
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// =============================================================
// 模块名称oh2bin
// 功能描述:参数化 one-hot 到二进制编码器
// 参数:
// WIDTH - one-hot 输入宽度
// AREA_OPT- 1: 面积优化0: 频率优化
// =============================================================
module oh2bin #(
parameter WIDTH = 8, // one-hot 输入宽度
parameter AREA_OPT = 1 // 1: 面积优化0: 频率优化
)(
input logic [WIDTH-1:0] oh, // one-hot 输入
output logic [$clog2(WIDTH)-1:0] bin // 二进制输出
);
// =========================================================
// 面积优化方案使用for循环优先节省面积
// =========================================================
generate
if (AREA_OPT) begin : gen_area_opt
always_comb begin
bin = '0;
for (int i = 0; i < WIDTH; i++) begin
if (oh[i]) bin = i[$clog2(WIDTH)-1:0];
end
end
end else begin : gen_speed_opt
// =========================================================
// 频率优化方案并行掩码法参考pulp-platform/common_cells实现
// =========================================================
for (genvar j = 0; j < $clog2(WIDTH); j++) begin : gen_jl
logic [WIDTH-1:0] tmp_mask; //xinbbin每1bit的掩码实际上取巧了。相当于是逆向思维。还可以正向编码待补充.
for (genvar i = 0; i < WIDTH; i++) begin : gen_il
logic [$clog2(WIDTH)-1:0] tmp_i;
assign tmp_i = i[$clog2(WIDTH)-1:0];
assign tmp_mask[i] = tmp_i[j];
end
assign bin[j] = |(tmp_mask & oh);
end
end
endgenerate
endmodule