特点:位宽参数化
实现代码如下:
`timescale 1ns / 1ps
//
// Company: nssc
// Engineer: liumeng
// Create Date: 09:34:27 12/28/2021
// Module Name: bin_to_one_hot
// Revision 0.01 - File Created
//
module bin_to_one_hot #
(
parameter BIN_WIDTH = 4
)(
input[BIN_WIDTH-1:0] d_bin,
output[2**BIN_WIDTH-1:0] q_one_hot
);
parameter PART_NUMBER = BIN_WIDTH>>1;
parameter EXT_BIT = BIN_WIDTH%2;
wire[3:0] q_one_hot_temp [PART_NUMBER-1:0];
wire[PART_NUMBER-1+1:0] q_one_hot_cal[2**BIN_WIDTH-1:0];
genvar i,j;
generate
for (i=0; i
其中使用到的bin_to_one_hot_2_4代码:
`timescale 1ns / 1ps
//
// Company: nssc
// Engineer: liumeng
// Create Date: 20:07:24 12/20/2021
// Module Name: bin_to_one_hot_2_4
// Revision 0.01 - File Created
//
module bin_to_one_hot_2_4(
input[1:0] d_bin,
output[3:0] q_one_hot
);
assign q_one_hot[0] = ~d_bin[1] & ~d_bin[0];
assign q_one_hot[1] = ~d_bin[1] & d_bin[0];
assign q_one_hot[2] = d_bin[1] & ~d_bin[0];
assign q_one_hot[3] = d_bin[1] & d_bin[0];
endmodule
仿真代码如下:
`timescale 1ns / 1ps
//
// Company: nssc
// Engineer: liumeng
// Create Date: 11:03:55 12/28/2021
// Module Name: sim_bin_to_one_hot
// Revision 0.01 - File Created
//
module sim_bin_to_one_hot;
parameter BIN_WIDTH = 3;
reg [BIN_WIDTH-1:0] d_bin;
wire [2**BIN_WIDTH-1:0] q_one_hot;
bin_to_one_hot #(.BIN_WIDTH(BIN_WIDTH)) uut (
.d_bin(d_bin),
.q_one_hot(q_one_hot)
);
initial begin
d_bin = {BIN_WIDTH{1'b0}};
#200;
repeat(100) begin
d_bin = d_bin + 1;
#200;
end
end
endmodule
仿真波形见下图:



