Advertisement
Harmony5757

Shift Add Multiplier

Jun 14th, 2025
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SystemVerilog 7.70 KB | Source Code | 0 0
  1. //============================
  2. // Top Module
  3. //============================
  4.  
  5. `timescale 1ns / 1ps
  6.  
  7. //Shift-Add Mplier
  8. //Checks Mplier bit [0] => If 1 => Add Mcand to Accumulator,
  9. //                         If 0 => Skip
  10. //Right Shift Mplier, Left Shift Mcand
  11. //Repeat Until all Mplier Bits have been checked.
  12.  
  13. module ShiftAdd_Mplier #(parameter WIDTH = 2)
  14.     (
  15.     input logic clk, srst, Mul_Start,
  16.     input logic [WIDTH - 1 : 0] Mplier, Mcand,
  17.     output logic [2*WIDTH - 1 : 0] Product,
  18.     output logic Done
  19.     );
  20.     logic Acc_en, R_Shift, L_Shift, R_Shift_Done, L_Shift_Done, Acc_Done;
  21.    
  22.     logic [$clog2(WIDTH + 1) - 1:0] Mplier_Bit_Count;
  23.     logic [2*WIDTH - 1 : 0] temp_Mcand; logic [WIDTH - 1 : 0] temp_Mplier;
  24.     logic [2*WIDTH - 1: 0] Acc_Out, temp_Sum;
  25.     logic [WIDTH - 1 : 0] temp_reg1;
  26.     logic [2*WIDTH - 1: 0] temp_reg2;
  27.    
  28.     Shifter #(.WIDTH(WIDTH)) R_Shifter(
  29.                                            .clk(clk),
  30.                                            .srst(srst),
  31.                                            .R_Shift(R_Shift),
  32.                                            .L_Shift(0),
  33.                                            .In(temp_Mplier),
  34.                                            .Shifter_Out(temp_reg1),
  35.                                            .Shift_Done(R_Shift_Done)
  36.                                            );
  37.                                            
  38.     Shifter #(.WIDTH(2*WIDTH)) L_Shifter(
  39.                                            .clk(clk),
  40.                                            .srst(srst),
  41.                                            .R_Shift(0),
  42.                                            .L_Shift(L_Shift),
  43.                                            .In(temp_Mcand),
  44.                                            .Shifter_Out(temp_reg2),
  45.                                            .Shift_Done(L_Shift_Done)
  46.                                            );
  47.                                      
  48.     Carry_LookAhead #(.WIDTH(2*WIDTH)) CLA(    .clk(clk),
  49.                                                .srst(srst),
  50.                                                .A(temp_Mcand),
  51.                                                .B(Acc_Out),
  52.                                                .Cin(0),
  53.                                                .Sum(temp_Sum)
  54.                                                );
  55.                                                
  56.     Accumulator #(.WIDTH(2*WIDTH)) Acc(
  57.                                          .clk(clk),
  58.                                          .srst(srst),
  59.                                          .Acc_en(Acc_en),
  60.                                          .Acc_In(temp_Sum),
  61.                                          .Acc_Out(Acc_Out),
  62.                                          .Acc_Done(Acc_Done)
  63.                                          );
  64.                                        
  65.    
  66.     always_ff @(posedge clk) begin
  67.         if (srst) begin
  68.             temp_Mcand      <= 0;
  69.             temp_Mplier     <= 0;
  70.             Product         <= 0;
  71.             Mplier_Bit_Count<= 0;
  72.             Done            <= 0;
  73.             L_Shift         <= 0;
  74.             R_Shift         <= 0;
  75.             Acc_en          <= 0;
  76.         end
  77.         else if (!L_Shift && !R_Shift && !Acc_en) begin //if not performing Shifting or accumulating Operations
  78.             if (Mul_Start) begin
  79.                 temp_Mcand             <= {{WIDTH{1'b0}}, Mcand};
  80.                 temp_Mplier            <= Mplier;
  81.                 Mplier_Bit_Count       <= WIDTH;
  82.             end
  83.             else if (Mplier_Bit_Count != 0) begin
  84.                 L_Shift            <= 1;
  85.                 R_Shift            <= 1;
  86.                 Mplier_Bit_Count   <= Mplier_Bit_Count - 1;
  87.                 if (temp_Mplier[0]) begin
  88.                     Acc_en <= 1;
  89.                 end
  90.                 if (Mplier_Bit_Count == WIDTH - 1) begin
  91.                     Done <= 1;
  92.                 end    
  93.             end
  94.             if (Done) begin
  95.                 Product <= Acc_Out[2*WIDTH - 1 : 0];
  96.             end
  97.         end
  98.         else begin
  99.             if (L_Shift) begin
  100.                 if (L_Shift_Done) begin
  101.                     L_Shift    <= 0;
  102.                     temp_Mcand <= temp_reg2;
  103.                 end
  104.             end
  105.             if (R_Shift) begin
  106.                 if (R_Shift_Done) begin
  107.                     R_Shift     <= 0;
  108.                     temp_Mplier <= temp_reg1;
  109.                 end
  110.             end
  111.             if (Acc_en) begin
  112.                 if (Acc_Done) begin
  113.                     Acc_en <= 0;
  114.                 end
  115.             end
  116.         end
  117.     end
  118. endmodule
  119.  
  120. //============================
  121. // Shifter Module
  122. //============================
  123.  
  124. `timescale 1ns / 1ps
  125. module Shifter #(parameter WIDTH = 4)
  126.     (
  127.     input logic clk, srst, R_Shift, L_Shift,
  128.     input logic [WIDTH - 1 : 0] In,
  129.     output logic [WIDTH - 1 : 0] Shifter_Out,
  130.     output logic Shift_Done
  131.     );
  132.    
  133.     always_ff @(posedge clk) begin
  134.         if (srst) begin
  135.             Shifter_Out <= 0;
  136.             Shift_Done <= 0;
  137.         end
  138.         else if (R_Shift) begin
  139.             Shifter_Out <= In >> 1;
  140.             Shift_Done <= 1;
  141.         end
  142.         else if (L_Shift) begin
  143.             Shifter_Out <= In << 1;
  144.             Shift_Done <= 1;
  145.         end
  146.         else if (!L_Shift || !R_Shift) begin
  147.             Shift_Done <= 0;
  148.         end
  149.     end
  150. endmodule
  151.  
  152. //============================
  153. // Carry Lookahead Adder Module
  154. //============================
  155.  
  156. `timescale 1ns / 1ps
  157. module Carry_LookAhead #(parameter WIDTH = 4)
  158.     (
  159.     input logic [WIDTH - 1:0] A, B,
  160.     input logic Cin, clk, srst,
  161.     output logic [WIDTH - 1:0] Sum,
  162.     output logic Cout
  163.     );
  164.     logic [WIDTH - 1:0] Gen, Prop, temp_Sum; logic [WIDTH:0] Carry_in;
  165.    
  166.     assign Carry_in[0] = Cin;
  167.     genvar i;
  168.     generate
  169.         for( i = 0; i < WIDTH; i++) begin : LookAhead
  170.             Full_Adder Full_Adder_inst
  171.                 (
  172.                     .A(A[i]),
  173.                     .B(B[i]),
  174.                     .Cin(Carry_in[i]),
  175.                     .Sum(temp_Sum[i]),
  176.                     .Cout()
  177.                     );
  178.         end
  179.     endgenerate
  180.    
  181.     genvar j;
  182.     generate
  183.         for( j = 0; j < WIDTH; j++) begin
  184.             assign Gen[j] = A[j] & B[j];
  185.             assign Prop[j] = A[j] | B[j];
  186.             assign Carry_in[j+1] = Gen[j] | (Prop[j] & Carry_in[j]);
  187.         end
  188.     endgenerate
  189.     always_ff @(posedge clk) begin
  190.         if (srst) begin
  191.             Sum <= 0;
  192.             Cout <= 0;
  193.         end
  194.         else begin
  195.             Sum <= temp_Sum;
  196.             Cout <= Carry_in[WIDTH];
  197.         end
  198.     end  
  199. endmodule
  200.  
  201. `timescale 1ns / 1ps
  202. //==================================================
  203. //FULL ADDER Implementation
  204. //==================================================
  205. module Full_Adder(
  206.     input logic A,
  207.     input logic B,
  208.     input logic Cin,
  209.     output logic Sum,
  210.     output logic Cout
  211.     );
  212.    
  213.     assign Sum = A ^ B ^ Cin;
  214.     assign Cout = A&B | A&Cin | B&Cin;
  215. endmodule
  216.  
  217.  
  218. //============================
  219. // Accumulator Module
  220. //============================
  221.  
  222. `timescale 1ns / 1ps
  223. module Accumulator #(parameter WIDTH = 4)
  224.     (
  225.     input logic clk, srst, Acc_en,
  226.     input logic [WIDTH - 1 : 0] Acc_In,
  227.     output logic [WIDTH - 1: 0] Acc_Out,
  228.     output logic Acc_Done
  229.     );
  230.    
  231.     always_ff @(posedge clk) begin
  232.         if (srst) begin
  233.             Acc_Out <= 0;
  234.         end
  235.         else if (Acc_en) begin
  236.             Acc_Out <= Acc_In;
  237.             Acc_Done <= 1;
  238.         end
  239.         else if (!Acc_en) begin
  240.             Acc_Done <= 0;
  241.         end
  242.     end
  243.    
  244. endmodule
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement