R&Y: Added DAY RTA Tapp Card and Additional BKK BEM Stored Value Card AIDs to `aid_de...
[RRG-proxmark3.git] / fpga / min_max_tracker.v
blob2aaab66bbd6d684e49daf2c0c4b7c423d9e6c90f
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
17 // track min and max peak values (envelope follower)
19 // NB: the min value (resp. max value) is updated only when the next high peak
20 // (resp. low peak) is reached/detected, since you can't know it isn't a
21 // local minima (resp. maxima) until then.
22 // This also means the peaks are detected with an unpredictable delay.
23 // This algorithm therefore can't be used directly for realtime peak detections,
24 // but it can be used as a simple envelope follower.
26 module min_max_tracker(
27 input clk,
28 input [7:0] adc_d,
29 input [7:0] threshold,
30 output [7:0] min,
31 output [7:0] max
34 reg [7:0] min_val = 255;
35 reg [7:0] max_val = 0;
36 reg [7:0] cur_min_val = 255;
37 reg [7:0] cur_max_val = 0;
38 reg [1:0] state = 0;
40 always @(posedge clk)
41 begin
42 case (state)
43 0: // initialize
44 begin
45 if (cur_max_val >= ({1'b0, adc_d} + threshold))
46 state <= 2;
47 else if (adc_d >= ({1'b0, cur_min_val} + threshold))
48 state <= 1;
49 if (cur_max_val <= adc_d)
50 cur_max_val <= adc_d;
51 else if (adc_d <= cur_min_val)
52 cur_min_val <= adc_d;
53 end
54 1: // high phase
55 begin
56 if (cur_max_val <= adc_d)
57 cur_max_val <= adc_d;
58 else if (({1'b0, adc_d} + threshold) <= cur_max_val) begin
59 state <= 2;
60 cur_min_val <= adc_d;
61 max_val <= cur_max_val;
62 end
63 end
64 2: // low phase
65 begin
66 if (adc_d <= cur_min_val)
67 cur_min_val <= adc_d;
68 else if (adc_d >= ({1'b0, cur_min_val} + threshold)) begin
69 state <= 1;
70 cur_max_val <= adc_d;
71 min_val <= cur_min_val;
72 end
73 end
74 endcase
75 end
77 assign min = min_val;
78 assign max = max_val;
80 endmodule