aboutsummaryrefslogtreecommitdiff
path: root/cmos/cd4076.c
blob: 0de9517c3bc164efd0b73ec259352e4d796a13c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* cd4076.c MP 20070312 */
/* Emulate a cd4076b */
#include "m_pd.h"

typedef struct _cd4076
{
    t_object        x_obj;
    t_outlet        *x_output_A;
    t_outlet        *x_output_B;
    t_outlet        *x_output_C;
    t_outlet        *x_output_D;
    int             x_clock;
    int             x_QA;
    int             x_QB;
    int             x_QC;
    int             x_QD;
    int             x_inA;
    int             x_inB;
    int             x_inC;
    int             x_inD;
    int             x_InDis1;
    int             x_InDis2;
    int             x_OutDis1;
    int             x_OutDis2;
    int             x_Clear;
    t_inlet         *x_clear;/* clear takes one or zero as acceptable inputs. */
    t_inlet         *x_input_disable_1;/* input_disable_1 takes one or zero as acceptable inputs. */
    t_inlet         *x_input_disable_2;/* input_disable_2 takes one or zero as acceptable inputs. */
    t_inlet         *x_output_disable_1;/* output_disable_1 takes one or zero as acceptable inputs. */
    t_inlet         *x_output_disable_2;/* output_disable_2 takes one or zero as acceptable inputs. */
    t_inlet         *x_input_A;/* input_A takes one or zero as acceptable inputs. */
    t_inlet         *x_input_B;/* input_B takes one or zero as acceptable inputs. */
    t_inlet         *x_input_C;/* input_C takes one or zero as acceptable inputs. */
    t_inlet         *x_input_D;/* input_D takes one or zero as acceptable inputs. */
    /* The main inlet (clock) should accept a bang or a one as valid clocks. */
    /* If a one is received, it must be followed by a zero before the clock will work again. */
} t_cd4076;

static t_class *cd4076_class;

void cd4076_setup(void);
static void *cd4076_new(t_symbol *s, int argc, t_atom *argv);
static void cd4076_free(t_cd4076 *x);
static void cd4076_bang(t_cd4076 *x);
static void cd4076_float(t_cd4076 *x, t_float f);
static void cd4076_clear(t_cd4076 *x, t_float f);
static void cd4076_input_disable_1(t_cd4076 *x, t_float f);
static void cd4076_input_disable_2(t_cd4076 *x, t_float f);
static void cd4076_output_disable_1(t_cd4076 *x, t_float f);
static void cd4076_output_disable_2(t_cd4076 *x, t_float f);
static void cd4076_input_a(t_cd4076 *x, t_float f);
static void cd4076_input_b(t_cd4076 *x, t_float f);
static void cd4076_input_c(t_cd4076 *x, t_float f);
static void cd4076_input_d(t_cd4076 *x, t_float f);
static void cd4076_update_outlets(t_cd4076 *x);

static void cd4076_float(t_cd4076 *x, t_float f)
{
    if (f == 1)
    { /* if clock is high and was low, clock it. */
        if ((x->x_clock) == 0) cd4076_bang(x);
        x->x_clock = 1;
    }
    else if (f == 0) x->x_clock = 0;
    else post("cd4076 accepts bang, 1 or 0.");
}

static void cd4076_bang(t_cd4076 *x)
{
    if ((x->x_InDis1 + x->x_InDis2 + x->x_Clear) == 0)
    {
        x->x_QA = x->x_inA;
        x->x_QB = x->x_inB;
        x->x_QC = x->x_inC;
        x->x_QD = x->x_inD;
    }
    cd4076_update_outlets(x);
}

static void cd4076_clear(t_cd4076 *x, t_float f)
{
    if (f == 1)
    {
        x->x_Clear = 1;
        x->x_QA = 0;
        x->x_QB = 0;
        x->x_QC = 0;
        x->x_QD = 0;
        cd4076_update_outlets(x);
    }
    else if (f == 0)
    {
        x->x_Clear = 0;
    }
    else
    {
        post("cd4076 clear takes 1 or 0 only.");
        return;
    }
}

static void cd4076_input_disable_1(t_cd4076 *x, t_float f)
{
    if (f == 1) x->x_InDis1 = 1;
    else if (f == 0) x->x_InDis1 = 0;
    else
    {
        post("cd4076 input disable 1 takes 1 or 0 only.");
        return;
    }
}

static void cd4076_input_disable_2(t_cd4076 *x, t_float f)
{
    if (f == 1) x->x_InDis2 = 1;
    else if (f == 0) x->x_InDis2 = 0;
    else
    {
        post("cd4076 input disable 2 takes 1 or 0 only.");
        return;
    }
}

static void cd4076_output_disable_1(t_cd4076 *x, t_float f)
{
    if (f == 1) x->x_OutDis1 = 1;
    else if (f == 0)
    {
        x->x_OutDis1 = 0;
        cd4076_update_outlets(x);
    }
    else
    {
        post("cd4076 output disable 1 takes 1 or 0 only.");
        return;
    }
}

static void cd4076_output_disable_2(t_cd4076 *x, t_float f)
{
    if (f == 1) x->x_OutDis2 = 1;
    else if (f == 0)
    {
        x->x_OutDis2 = 0;
        cd4076_update_outlets(x);
    }
    else
    {
        post("cd4076 output disable 2 takes 1 or 0 only.");
        return;
    }
}

static void cd4076_input_a(t_cd4076 *x, t_float f)
{
    if (f == 1) x->x_inA = 1;
    else if (f == 0) x->x_inA = 0;
    else
    {
        post("cd4076 input A takes 1 or 0 only.");
        return;
    }
}

static void cd4076_input_b(t_cd4076 *x, t_float f)
{
    if (f == 1) x->x_inB = 1;
    else if (f == 0) x->x_inB = 0;
    else
    {
        post("cd4076 input B takes 1 or 0 only.");
        return;
    }
}

static void cd4076_input_c(t_cd4076 *x, t_float f)
{
    if (f == 1) x->x_inC = 1;
    else if (f == 0) x->x_inC = 0;
    else
    {
        post("cd4076 input C takes 1 or 0 only.");
        return;
    }
}

static void cd4076_input_d(t_cd4076 *x, t_float f)
{
    if (f == 1) x->x_inD = 1;
    else if (f == 0) x->x_inD = 0;
    else
    {
        post("cd4076 input D takes 1 or 0 only.");
        return;
    }
}

static void cd4076_update_outlets(t_cd4076 *x)
{
    if (x->x_OutDis1 + x->x_OutDis2 == 0)
    {
        outlet_float(x->x_output_D, ((x->x_QD) == 1)?1:0);
        outlet_float(x->x_output_C, ((x->x_QC) == 1)?1:0);
        outlet_float(x->x_output_B, ((x->x_QB) == 1)?1:0);
        outlet_float(x->x_output_A, ((x->x_QA) == 1)?1:0);
    }
}

static void cd4076_free(t_cd4076 *x)
{
    return;
}

static void *cd4076_new(t_symbol *s, int argc, t_atom *argv)
{
    t_cd4076           *x;

    x = (t_cd4076 *)pd_new(cd4076_class);
    if (x == NULL) return (x);
    x->x_output_A = outlet_new((t_object *)x, &s_float);
    x->x_output_B = outlet_new((t_object *)x, &s_float);
    x->x_output_C = outlet_new((t_object *)x, &s_float);
    x->x_output_D = outlet_new((t_object *)x, &s_float);

    x->x_clear = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("clear"));
    x->x_input_disable_1 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("inputdisable1"));
    x->x_input_disable_2 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("inputdisable2"));
    x->x_output_disable_1 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("outputdisable1"));
    x->x_output_disable_2 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("outputdisable2"));
    x->x_input_A = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("inputA"));
    x->x_input_B = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("inputB"));
    x->x_input_C = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("inputC"));
    x->x_input_D = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("inputD"));
    return (x);
}

void cd4076_setup(void)
{
    cd4076_class = class_new(gensym("cd4076"),
                    (t_newmethod)cd4076_new,
                    (t_method)cd4076_free,
                    sizeof(t_cd4076), 0, 0); /* no arguments */
    class_addbang(cd4076_class, cd4076_bang);
    class_addfloat(cd4076_class, cd4076_float);
    class_addmethod(cd4076_class, (t_method)cd4076_clear, gensym("clear"), A_FLOAT, 0);
    class_addmethod(cd4076_class, (t_method)cd4076_input_disable_1, gensym("inputdisable1"), A_FLOAT, 0);
    class_addmethod(cd4076_class, (t_method)cd4076_input_disable_2, gensym("inputdisable2"), A_FLOAT, 0);
    class_addmethod(cd4076_class, (t_method)cd4076_output_disable_1, gensym("outputdisable1"), A_FLOAT, 0);
    class_addmethod(cd4076_class, (t_method)cd4076_output_disable_2, gensym("outputdisable2"), A_FLOAT, 0);
    class_addmethod(cd4076_class, (t_method)cd4076_input_a, gensym("inputA"), A_FLOAT, 0);
    class_addmethod(cd4076_class, (t_method)cd4076_input_b, gensym("inputB"), A_FLOAT, 0);
    class_addmethod(cd4076_class, (t_method)cd4076_input_c, gensym("inputC"), A_FLOAT, 0);
    class_addmethod(cd4076_class, (t_method)cd4076_input_d, gensym("inputD"), A_FLOAT, 0);
}
/* end cd4076.c */