Verilog - ABS代码重构
Posted wjcdx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Verilog - ABS代码重构相关的知识,希望对你有一定的参考价值。
https://mp.weixin.qq.com/s/-KUviTzO3Hdir_mI57L24g
module ABS #( parameter DATA_WIDTH = 8 ) ( input [DATA_WIDTH-1:0] din, output reg [DATA_WIDTH-1:0] dout ); localparam W = DATA_WIDTH; localparam MSB = DATA_WIDTH - 1; always @(*) begin if (din[MSB] == 1‘b1) begin // negative data if (din[W-2:0] == { (W-1){1‘b0} }) begin // Max dout = {1‘b0,{(W-1){1‘b1}}}; end else begin dout = {1‘b0,((~din[W-2:0])+1‘b1)}; end end else begin dout = din; end end endmodule
module ABS #( parameter DATA_WIDTH = 8 ) ( input [DATA_WIDTH-1:0] din, output reg [DATA_WIDTH-1:0] dout ); localparam W = DATA_WIDTH; localparam MSB = DATA_WIDTH - 1; wire din_sign = din[MSB]; wire [W-2:0] din_data = din[W-2:0]; wire [W-2:0] pad0 = { (W-1){1‘b0} }; wire [W-2:0] pad1 = { (W-1){1‘b1} }; always @(*) begin if (din_sign == 1‘b1) begin // negative data if (din_data == pad0) begin // Max dout = {1‘b0, pad1}; end else begin dout = {1‘b0,((~din_data)+1‘b1)}; end end else begin dout = din; end end endmodule
module ABS #( parameter DATA_WIDTH = 8 ) ( input [DATA_WIDTH-1:0] din, output reg [DATA_WIDTH-1:0] dout ); localparam W = DATA_WIDTH; localparam MSB = DATA_WIDTH - 1; wire din_sign = din[MSB]; wire [W-2:0] din_data = din[W-2:0]; wire [W-2:0] pad1 = { (W-1){1‘b1} }; always @(*) begin if (din_sign == 1‘b1) begin // negative data if (din_data == 0) begin // Max dout = {1‘b0, pad1}; end else begin dout = {1‘b0,((~din_data)+1‘b1)}; end end else begin dout = din; end end endmodule
module ABS #( parameter DATA_WIDTH = 8 ) ( input [DATA_WIDTH-1:0] din, output reg [DATA_WIDTH-1:0] dout ); localparam W = DATA_WIDTH; localparam MSB = DATA_WIDTH - 1; wire din_sign = din[MSB]; wire [W-2:0] din_data = din[W-2:0]; wire [W-2:0] pad1 = { (W-1){1‘b1} }; always @(*) begin if (din_sign == 1‘b1) begin // negative data if (din_data == 0) begin // Max dout = ~din; end else begin dout = {1‘b0,((~din_data)+1‘b1)}; end end else begin dout = din; end end endmodule
同时可以省略pad1。
module ABS #( parameter DATA_WIDTH = 8 ) ( input [DATA_WIDTH-1:0] din, output reg [DATA_WIDTH-1:0] dout ); localparam W = DATA_WIDTH; localparam MSB = DATA_WIDTH - 1; wire din_sign = din[MSB]; wire [W-2:0] din_data = din[W-2:0]; always @(*) begin if (din_sign == 1‘b1) begin // negative data if (din_data == 0) begin // Max dout = ~din; end else begin dout = ~din + 1; end end else begin dout = din; end end endmodule
module ABS #( parameter DATA_WIDTH = 8 ) ( input [DATA_WIDTH-1:0] din, output reg [DATA_WIDTH-1:0] dout ); localparam W = DATA_WIDTH; wire din_sign = din[W-1]; wire [W-2:0] din_data = din[W-2:0]; always @(*) begin if (din_sign == 1‘b1) begin // negative data if (din_data == 0) begin // Max dout = ~din; end else begin dout = ~din + 1; end end else begin dout = din; end end endmodule
以上是关于Verilog - ABS代码重构的主要内容,如果未能解决你的问题,请参考以下文章
我应该如何重构复杂的 If else block in view?