特点:位宽参数化
实现代码如下:
`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<PART_NUMBER; i=i+1) begin :loop_1
bin_to_one_hot_2_4 bin_to_one_hot_2_4(
.d_bin ( d_bin[PART_NUMBER*i+1:PART_NUMBER*i] ),
.q_one_hot ( q_one_hot_temp[i] )
);
end
endgenerate
generate
if (EXT_BIT) begin
for (i=0; i<2**BIN_WIDTH; i=i+1) begin : loop
for (j=0; j+1<PART_NUMBER+1; j=j+1) begin : loop
if (i<2**(BIN_WIDTH-1)) begin
assign q_one_hot_cal[i][j+1] = ~d_bin[BIN_WIDTH-1] & q_one_hot_cal[i][j] & q_one_hot_temp[j][i[2*j+1:2*j]];;
end else begin
assign q_one_hot_cal[i][j+1] = d_bin[BIN_WIDTH-1] & q_one_hot_cal[i][j] & q_one_hot_temp[j][i[2*j+1:2*j]];;
end
end
assign q_one_hot_cal[i][0] = 1'b1;
assign q_one_hot[i] = q_one_hot_cal[i][PART_NUMBER];
end
end else begin
for (i=0; i<2**BIN_WIDTH; i=i+1) begin : loop
for (j=0; j+1<PART_NUMBER+1; j=j+1) begin : loop
assign q_one_hot_cal[i][j+1] = q_one_hot_cal[i][j] & q_one_hot_temp[j][i[2*j+1:2*j]];
end
assign q_one_hot_cal[i][0] = 1'b1;
assign q_one_hot[i] = q_one_hot_cal[i][PART_NUMBER];
end
end
endgenerate
endmodule
其中使用到的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
仿真波形见下图: