GainControl.v 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. `timescale 1ns / 1ps
  2. (* KEEP = "TRUE" *)
  3. //////////////////////////////////////////////////////////////////////////////////
  4. // Company:
  5. // Engineer: Churbanov S.
  6. //
  7. // Create Date: 15:24:31 08/20/2019
  8. // Design Name:
  9. // Module Name: gain_master
  10. // Project Name:
  11. // Target Devices:
  12. // Tool versions:
  13. // Description:
  14. //
  15. // Dependencies:
  16. //
  17. // Revision:
  18. // Revision 0.02 - File Modified
  19. // Additional Comments: 16.09.2019 file modified in assotiate with task.
  20. //
  21. //////////////////////////////////////////////////////////////////////////////////
  22. module GainControl
  23. #(
  24. parameter AdcNcoMultWidth = 35,
  25. parameter ThresholdWidth = 24,
  26. parameter AdcDataWidth = 14,
  27. parameter MeasPeriod = 32
  28. )
  29. (
  30. input Rst_i,
  31. input Clk_i,
  32. input StartMeas_i,
  33. input GainAutoEn_i,
  34. input signed [AdcNcoMultWidth-1:0] AdcCos_i,
  35. input signed [AdcNcoMultWidth-1:0] AdcSin_i,
  36. input [ThresholdWidth-1:0] GainLowThreshold_i,
  37. input [ThresholdWidth-1:0] GainHighThreshold_i,
  38. output GainNewState_o,
  39. output SensEn_o,
  40. output MeasStart_o
  41. );
  42. //================================================================================
  43. // LOCALPARAMS
  44. localparam CntWidth = 32;
  45. localparam Delay = 100;
  46. localparam AverageDelay = MeasPeriod+Delay-1;
  47. localparam SumWidth = AdcNcoMultWidth+6-1;
  48. //================================================================================
  49. // REG/WIRE
  50. reg [CntWidth-1:0] measCnt;
  51. reg signed [SumWidth-1:0] adcSinSum;
  52. reg signed [SumWidth-1:0] adcCosSum;
  53. reg measWind;
  54. wire measEnd = (measCnt==AverageDelay-1)&measWind;
  55. reg gainNewStateR;
  56. reg gainNewState;
  57. wire sensEn = ((gainNewStateR& (!gainNewState))|(!gainNewStateR&gainNewState));
  58. reg signed [SumWidth-1:0] sinShifted;
  59. reg signed [SumWidth-1:0] cosShifted;
  60. wire signed [ThresholdWidth-5:0] sinShiftedCut = sinShifted [(SumWidth-1)-:20]; //width is 20
  61. wire signed [ThresholdWidth-5:0] cosShiftedCut = cosShifted [(SumWidth-1)-:20]; //width is 20
  62. wire signed [(ThresholdWidth*2)-9:0] sinSumSquared = (sinShiftedCut*sinShiftedCut); // width is 40
  63. wire signed [(ThresholdWidth*2)-9:0] cosSumSquared = (cosShiftedCut*cosShiftedCut); // width is 40
  64. wire signed [(ThresholdWidth*2)-9:0] sumSquared = (cosSumSquared+sinSumSquared); //width is 40
  65. wire [(ThresholdWidth*2)-9:0] lowThresholdCompl = {10'b0,GainLowThreshold_i,6'b0};
  66. wire [(ThresholdWidth*2)-9:0] highThresholdCompl = {10'b0,GainHighThreshold_i,6'b0};
  67. wire accWind = (measCnt>0 & measCnt <=MeasPeriod-2);
  68. //================================================================================
  69. // ASSIGNMENTS
  70. assign GainNewState_o = gainNewState;
  71. assign SensEn_o = sensEn;
  72. assign MeasStart_o = GainAutoEn_i? measEnd:StartMeas_i;
  73. //================================================================================
  74. // CODING
  75. always @(posedge Clk_i) begin
  76. if (!Rst_i) begin
  77. if (GainAutoEn_i) begin
  78. if (StartMeas_i) begin
  79. measWind <= 1'b1;
  80. end else if (measEnd) begin
  81. measWind <= 1'b0;
  82. end
  83. end else begin
  84. measWind <= 1'b0;
  85. end
  86. end else begin
  87. measWind <= 1'b0;
  88. end
  89. end
  90. always @(posedge Clk_i) begin
  91. if (measWind) begin
  92. if (measCnt == MeasPeriod-2) begin
  93. sinShifted <= adcSinSum>>>2;
  94. cosShifted <= adcCosSum>>>2;
  95. end
  96. end
  97. end
  98. always @(posedge Clk_i) begin
  99. if (!Rst_i) begin
  100. if (measWind) begin
  101. if (measCnt != AverageDelay-1) begin
  102. measCnt <= measCnt + 3'd1;
  103. end
  104. end else begin
  105. measCnt <= 3'd0;
  106. end
  107. end else begin
  108. measCnt <= 3'd0;
  109. end
  110. end
  111. always @(posedge Clk_i) begin
  112. if (!Rst_i) begin
  113. if (!accWind) begin
  114. adcSinSum <= AdcSin_i;
  115. adcCosSum <= AdcCos_i;
  116. end else begin
  117. adcSinSum <= adcSinSum + AdcSin_i;
  118. adcCosSum <= adcCosSum + AdcCos_i;
  119. end
  120. end else begin
  121. adcSinSum <= 0;
  122. adcCosSum <= 0;
  123. end
  124. end
  125. always @(posedge Clk_i) begin
  126. if (!Rst_i) begin
  127. if (GainAutoEn_i) begin
  128. if (measCnt == MeasPeriod-1) begin
  129. if (gainNewState) begin
  130. if (sumSquared > highThresholdCompl) begin
  131. gainNewState <= 1'b0;
  132. end else begin
  133. gainNewState <= gainNewState;
  134. end
  135. end else begin
  136. if (sumSquared < lowThresholdCompl) begin
  137. gainNewState <= 1'b1;
  138. end else begin
  139. gainNewState <= gainNewState;
  140. end
  141. end
  142. end
  143. end else begin
  144. gainNewState <= 1'b0;
  145. end
  146. end else begin
  147. gainNewState <= 1'b0;
  148. end
  149. gainNewStateR <= gainNewState;
  150. end
  151. endmodule