]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/protocol.c
470debe64e4a6e91d5a13ba08894fb703d17ca83
[libsigrok.git] / hardware / zeroplus-logic-cube / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "protocol.h"
21
22 SR_PRIV unsigned int get_memory_size(int type)
23 {
24         if (type == MEMORY_SIZE_8K)
25                 return 8 * 1024;
26         else if (type <= MEMORY_SIZE_8M)
27                 return (32 * 1024) << type;
28         else
29                 return 0;
30 }
31
32 SR_PRIV int clz(unsigned int x)
33 {
34         int n = 0;
35         if (x == 0)
36                 return 32;
37         if (!(x & 0xFFFF0000)) {
38                 n = n + 16;
39                 x = x << 16;
40         }
41         if (!(x & 0xFF000000)) {
42                 n = n + 8;
43                 x = x << 8;
44         }
45         if (!(x & 0xF0000000)) {
46                 n = n + 4;
47                 x = x << 4;
48         }
49         if (!(x & 0xC0000000)) {
50                 n = n + 2;
51                 x = x << 2;
52         }
53         if (!(x & 0x80000000))
54                 n = n + 1;
55         return n;
56 }
57
58 SR_PRIV int set_limit_samples(struct dev_context *devc, uint64_t samples)
59 {
60         devc->limit_samples = samples;
61
62         if (samples <= 2 * 1024)
63                 devc->memory_size = MEMORY_SIZE_8K;
64         else if (samples <= 16 * 1024)
65                 devc->memory_size = MEMORY_SIZE_64K;
66         else
67                 devc->memory_size = 19 - clz(samples - 1);
68
69         sr_info("Setting memory size to %dK.",
70                 get_memory_size(devc->memory_size) / 1024);
71
72         analyzer_set_memory_size(devc->memory_size);
73
74         return SR_OK;
75 }
76
77 SR_PRIV int set_capture_ratio(struct dev_context *devc, uint64_t ratio)
78 {
79         if (ratio > 100) {
80                 sr_err("Invalid capture ratio: %" PRIu64 ".", ratio);
81                 return SR_ERR_ARG;
82         }
83
84         devc->capture_ratio = ratio;
85
86         sr_info("Setting capture ratio to %d%%.", devc->capture_ratio);
87
88         return SR_OK;
89 }
90
91 SR_PRIV void set_triggerbar(struct dev_context *devc)
92 {
93         unsigned int ramsize, n, triggerbar;
94
95         ramsize = get_memory_size(devc->memory_size) / 4;
96         if (devc->trigger) {
97                 n = ramsize;
98                 if (devc->max_sample_depth < n)
99                         n = devc->max_sample_depth;
100                 if (devc->limit_samples < n)
101                         n = devc->limit_samples;
102                 n = n * devc->capture_ratio / 100;
103                 if (n > ramsize - 8)
104                         triggerbar = ramsize - 8;
105                 else
106                         triggerbar = n;
107         } else {
108                 triggerbar = 0;
109         }
110         analyzer_set_triggerbar_address(triggerbar);
111         analyzer_set_ramsize_trigger_address(ramsize - triggerbar);
112
113         sr_dbg("triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
114         sr_dbg("ramsize_triggerbar_address = %d(0x%x)",
115                ramsize - triggerbar, ramsize - triggerbar);
116 }