Remove modulo operator from sar logic

Modulo operator is usualy heavy and shouldn't be normaly used.
This commit removes it. Additionaly a bug with number of used bits is fixed.
This commit is contained in:
Kuba Nowak 2026-02-26 14:57:36 +00:00
parent 98cdc3d18d
commit c65ce1f921
2 changed files with 90 additions and 26 deletions

View File

@ -1,33 +1,26 @@
module sar_logic ( module sar_logic (
input wire clk, input wire clk,
input wire Op, input wire Op,
input wire En, input wire En,
input wire Om, input wire Om,
input wire rst, input wire rst,
output reg [6:0] B, // 7-bit output wire [7:0] B, // 8-bit
output reg [6:0] BN, // 7-bit output reg [7:0] BN, // 8-bit
output reg [7:0] D // 8-bit output reg [7:0] D // 8-bit
); );
reg [3:0] counter = 4'b0000; // 4-bit counter reg [3:0] counter; // 3-bit counter
assign B = D;
always @(posedge clk) begin always @(posedge clk) begin
if (rst) begin if (rst) begin
B <= 7'b0000000; BN <= 8'b0000000;
BN <= 7'b0000000; D <= 8'b00000000;
D <= 8'b00000000; counter <= 4'b0000;
counter <= 4'b0000; end else if (En && (Op ^ Om) && ~(counter==8)) begin
end else if (En && (Op ^ Om)) begin D[counter[2:0]] <= Op;
if (counter < 7) begin BN[counter[2:0]] <= Om;
D <= D | ({7'b0, Op} << counter); counter <= counter + 1'b1;
B[counter % 7] <= (Op) ? 1'b1 : 1'b0;
BN[counter % 7] <= (Om) ? 1'b1 : 1'b0;
counter <= counter + 1'b1;
end
end end
end end
endmodule endmodule

View File

@ -10,8 +10,8 @@ module sar_logic_tb();
reg En; reg En;
// Outputs // Outputs
wire [6:0] B; wire [7:0] B;
wire [6:0] BN; wire [7:0] BN;
wire [7:0] D; wire [7:0] D;
// Instantiate the SAR Logic module // Instantiate the SAR Logic module
@ -46,14 +46,85 @@ module sar_logic_tb();
En = 1'b1; En = 1'b1;
Op = 1'b1; Op = 1'b1;
Om = 1'b0; Om = 1'b0;
// Apply reset again // Apply reset again
#80 rst = 1'b1; #100 rst = 1'b1;
#10 rst = 1'b0; #10 rst = 1'b0;
Op = 1'b0; Op = 1'b0;
Om = 1'b1; Om = 1'b1;
#70 rst = 1'b1; #50 rst = 1'b1;
#10 rst = 1'b0;
En = 1'b1;
Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#50 rst = 1'b1;
#10 rst = 1'b0;
En = 1'b1;
Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
#10 Op = 1'b1;
Om = 1'b0;
#10 Op = 1'b0;
Om = 1'b1;
// End of simulation // End of simulation
#100 $finish; #100 $finish;