a553f267 |
1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> |
3 | // |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, |
5 | // at your option, any later version. See the LICENSE.txt file for the text of |
6 | // the license. |
7 | //----------------------------------------------------------------------------- |
8 | // Graph utilities |
9 | //----------------------------------------------------------------------------- |
10 | |
7fe9b0b7 |
11 | #include <stdio.h> |
12 | #include <string.h> |
13 | #include "ui.h" |
14 | #include "graph.h" |
15 | |
16 | int GraphBuffer[MAX_GRAPH_TRACE_LEN]; |
17 | int GraphTraceLen; |
18 | |
19 | /* write a bit to the graph */ |
20 | void AppendGraph(int redraw, int clock, int bit) |
21 | { |
22 | int i; |
23 | |
24 | for (i = 0; i < (int)(clock / 2); ++i) |
25 | GraphBuffer[GraphTraceLen++] = bit ^ 1; |
26 | |
27 | for (i = (int)(clock / 2); i < clock; ++i) |
28 | GraphBuffer[GraphTraceLen++] = bit; |
29 | |
30 | if (redraw) |
31 | RepaintGraphWindow(); |
32 | } |
33 | |
34 | /* clear out our graph window */ |
35 | int ClearGraph(int redraw) |
36 | { |
37 | int gtl = GraphTraceLen; |
38 | GraphTraceLen = 0; |
39 | |
40 | if (redraw) |
41 | RepaintGraphWindow(); |
42 | |
43 | return gtl; |
44 | } |
45 | |
46 | /* |
47 | * Detect clock rate |
48 | */ |
49 | int DetectClock(int peak) |
50 | { |
51 | int i; |
52 | int clock = 0xFFFF; |
53 | int lastpeak = 0; |
54 | |
55 | /* Detect peak if we don't have one */ |
56 | if (!peak) |
57 | for (i = 0; i < GraphTraceLen; ++i) |
58 | if (GraphBuffer[i] > peak) |
59 | peak = GraphBuffer[i]; |
60 | |
61 | for (i = 1; i < GraphTraceLen; ++i) |
62 | { |
63 | /* If this is the beginning of a peak */ |
64 | if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] == peak) |
65 | { |
66 | /* Find lowest difference between peaks */ |
67 | if (lastpeak && i - lastpeak < clock) |
68 | clock = i - lastpeak; |
69 | lastpeak = i; |
70 | } |
71 | } |
72 | |
73 | return clock; |
74 | } |
75 | |
76 | /* Get or auto-detect clock rate */ |
77 | int GetClock(const char *str, int peak, int verbose) |
78 | { |
79 | int clock; |
80 | |
81 | sscanf(str, "%i", &clock); |
82 | if (!strcmp(str, "")) |
83 | clock = 0; |
84 | |
85 | /* Auto-detect clock */ |
86 | if (!clock) |
87 | { |
88 | clock = DetectClock(peak); |
89 | /* Only print this message if we're not looping something */ |
90 | if (!verbose) |
91 | PrintAndLog("Auto-detected clock rate: %d", clock); |
92 | } |
93 | |
94 | return clock; |
95 | } |