Advertisement
alexlad

Untitled

Jan 18th, 2021
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2.  
  3. module delays_and_counter #
  4.     (
  5.         parameter N_WIDTH = 32,
  6.         parameter delay_count_width = 32,
  7.         parameter CE_count_width = 32,
  8.         parameter delay_value = 50000,
  9.         parameter number_of_half_periods = 51
  10.     )
  11.    
  12.     (
  13.         (* X_INTERFACE_PARAMETER = "FREQ_HZ 125000000" *)
  14.         input clk,
  15.         input wire valid,
  16.         input wire signed [13:0] DDS_signal,
  17.         input [N_WIDTH-1:0] signal_freq,
  18.         input [31:0] gain,
  19.         input [31:0] DDS_Phase,
  20.         input signed [13:0] Raw_ADC,
  21.         input signed [13:0] Raw_ADC_measurment,
  22.         output wire cl_en,
  23.         output wire res,
  24.         output wire resDDS,
  25.         output wire DDS_valid,
  26.         output [31:0] gain_r,
  27.         output signed [13:0] max_ADC,
  28.         output signed [13:0] max_ADC_measurment
  29.      );
  30.    
  31.     reg delay_counter_permission = 0;
  32.     reg [delay_count_width-1:0] delay_counter = 0;
  33.     reg [delay_count_width-1:0] delay_counter_past = 0;
  34.     reg [CE_count_width-1:0] CE_permission_counter = 0;
  35.     reg CE = 0;
  36.     reg reset = 0;
  37.     reg reset_DDS = 0;
  38.     reg val_DDS = 0;
  39.     reg signed [13:0] present_value = 1;
  40.     reg signed [13:0] past_value = 1;
  41.     reg [31:0] present_phase = 0;
  42.     reg [31:0] past_phase = 0;
  43.     reg [31:0] counter_of_half_periods = 0;
  44.     reg [31:0] past_counter_of_half_periods = 0;
  45.     reg [31:0] gain_register = 0;
  46.     reg signed [13:0] present_value_ADC = 0;
  47.     reg signed [13:0] max_value_ADC = 0;
  48.     reg signed [13:0] max_value_ADC_measurment = 0;
  49.  
  50.  
  51.     always @(posedge clk)
  52.     begin
  53.         if (signal_freq == 0)
  54.             begin
  55.                 delay_counter <= 0;
  56.                 reset <= 1;
  57.                 reset_DDS <= 0;
  58.                 counter_of_half_periods <= 0;
  59.                 CE <= 0;
  60.                 val_DDS <= 0;
  61.                
  62.             end
  63.         else
  64.             begin
  65.                 delay_counter <= delay_counter + 1;
  66.                 reset <= 0;
  67.                 reset_DDS <= 1;
  68.                 val_DDS <= 1;
  69.                 past_value <= present_value;
  70.                 present_value <= DDS_signal;
  71.                 past_phase <= present_phase;
  72.                 present_phase <= DDS_Phase;
  73.                  if (delay_counter > delay_value)
  74.                     begin
  75.                        if(present_value[13] != past_value[13])
  76.                            begin
  77.                                counter_of_half_periods = counter_of_half_periods + 1;
  78.                            end
  79.                        if((counter_of_half_periods >= 1) & (counter_of_half_periods < number_of_half_periods))
  80.                            begin
  81.                                CE <= 1;
  82.                            end
  83.                        else
  84.                            begin
  85.                                CE <= 0;
  86.                            end
  87.                        end
  88.                    else
  89.                        begin
  90.                            counter_of_half_periods <= 0;
  91.             end
  92.     end
  93.  
  94.  
  95.     always @(posedge clk)
  96.     begin
  97.         if (CE == 1)
  98.         begin
  99.             if ($signed(Raw_ADC) > $signed(max_value_ADC))
  100.                 max_value_ADC <= Raw_ADC;
  101.             else
  102.                 max_value_ADC <= max_value_ADC;
  103.             end
  104.         else
  105.             if (signal_freq == 0)
  106.             begin
  107.                 max_value_ADC <= 0;
  108.             end
  109.         end
  110.  
  111.  
  112.     always @(posedge clk)
  113.     begin
  114.         if (CE == 1)
  115.         begin
  116.             if ($signed(Raw_ADC_measurment) > $signed(max_value_ADC_measurment))
  117.                 max_value_ADC_measurment <= Raw_ADC_measurment;
  118.             else
  119.                 max_value_ADC_measurment <= max_value_ADC_measurment;
  120.             end
  121.         else
  122.             if (signal_freq == 0)
  123.             begin
  124.                 max_value_ADC_measurment <= 0;
  125.             end
  126.     end
  127.  
  128.  
  129.     always @(posedge clk)
  130.     begin
  131.         if (valid == 1)
  132.         begin
  133.             gain_register <= gain;
  134.         end
  135.     end
  136.      
  137.     assign gain_r = gain_register;
  138.     assign cl_en = CE;
  139.     assign res = reset;
  140.     assign resDDS = reset_DDS;
  141.     assign DDS_valid = val_DDS;
  142.     assign max_ADC = max_value_ADC;
  143.     assign max_ADC_measurment = max_value_ADC_measurment;
  144.    
  145. endmodule
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement