Verilog - ABS程式碼重構
阿新 • • 發佈:2018-11-10
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 ); always@(*) begin if (din[DATA_WIDTH-1] == 1'b1) begin // negative data if (din[DATA_WIDTH-2:0] == {(DATA_WIDTH-1){1'b0}}) begin // Max dout = {1'b0,{(DATA_WIDTH-1){1'b1}}}; end else begin dout = {1'b0,((~din[DATA_WIDTH-2:0])+1'b1)}; end endelse 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; 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