aboutsummaryrefslogtreecommitdiff
path: root/cmos/cd4014.c
blob: b9bf26dd58e7f7ff6a505f2e29bec0181f18033d (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
/* cd4014.c MP 20070315 */
/* Emulate a cd4014b */
#include "m_pd.h"

typedef struct _cd4014
{
    t_object        x_obj;
    int             x_P1;
    int             x_P2;
    int             x_P3;
    int             x_P4;
    int             x_P5;
    int             x_P6;
    int             x_P7;
    int             x_P8;
    int             x_SerialIn;
    int             x_Clk;
    int             x_Q;
    int             x_ParallelSerial;
    t_outlet        *x_outQ6;
    t_outlet        *x_outQ7;
    t_outlet        *x_outQ8;
    t_inlet         *x_inP1;/* extra inlets are 'live' like the first */
    t_inlet         *x_inP2;
    t_inlet         *x_inP3;
    t_inlet         *x_inP4;
    t_inlet         *x_inP5;
    t_inlet         *x_inP6;
    t_inlet         *x_inP7;
    t_inlet         *x_inP8;
    t_inlet         *x_inParallelSerial;
    t_inlet         *x_inSerial;
} t_cd4014;

static t_class *cd4014_class;

void cd4014_setup(void);
static void *cd4014_new(t_symbol *s, int argc, t_atom *argv);
static void cd4014_free(t_cd4014 *x);
static void cd4014_float(t_cd4014 *x, t_float f);
static void cd4014_bang(t_cd4014 *x);
static void cd4014_inP1(t_cd4014 *x, t_float f);
static void cd4014_inP2(t_cd4014 *x, t_float f);
static void cd4014_inP3(t_cd4014 *x, t_float f);
static void cd4014_inP4(t_cd4014 *x, t_float f);
static void cd4014_inP5(t_cd4014 *x, t_float f);
static void cd4014_inP6(t_cd4014 *x, t_float f);
static void cd4014_inP7(t_cd4014 *x, t_float f);
static void cd4014_inP8(t_cd4014 *x, t_float f);
static void cd4014_inParallelSerial(t_cd4014 *x, t_float f);
static void cd4014_inSerial(t_cd4014 *x, t_float f);
static void cd4014_update_outlets(t_cd4014 *x);

static void cd4014_float(t_cd4014 *x, t_float f)
{
    if (f == 1)
    {
        if (x->x_Clk == 0)
        {
            x->x_Clk = 1;
            cd4014_bang(x);
        }
    }
    else if (f == 0) x->x_Clk = 0;
    else
    {
        post("cd4014 Clock inlet accepts 1 or 0.");
        return;
    }
}

static void cd4014_bang(t_cd4014 *x)
{
    if (x->x_ParallelSerial == 0)
    { /* shift left by one */
        x->x_Q <<= 1;
        x->x_Q |= x->x_SerialIn;
    }
    else
    { /* parallel load */
        x->x_Q = 128*x->x_P8 + 64*x->x_P7 + 32*x->x_P6 + 16*x->x_P5 + 8*x->x_P4 + 4*x->x_P3 + 2*x->x_P2 + x->x_P1;
    }
    cd4014_update_outlets(x);
}

static void cd4014_inP1(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_P1 = 1;
    else if (f == 0) x->x_P1 = 0;
    else
    {
        post("cd4014 inlet P1 accepts 1 or 0.");
        return;
    }
}

static void cd4014_inP2(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_P2 = 2;
    else if (f == 0) x->x_P2 = 0;
    else
    {
        post("cd4014 inlet P2 accepts 1 or 0.");
        return;
    }
}

static void cd4014_inP3(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_P3 = 1;
    else if (f == 0) x->x_P3 = 0;
    else
    {
        post("cd4014 inlet P3 accepts 1 or 0.");
        return;
    }
}

static void cd4014_inP4(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_P4 = 1;
    else if (f == 0) x->x_P4 = 0;
    else
    {
        post("cd4014 inlet P4 accepts 1 or 0.");
        return;
    }
}

static void cd4014_inP5(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_P5 = 1;
    else if (f == 0) x->x_P5 = 0;
    else
    {
        post("cd4014 inlet P5 accepts 1 or 0.");
        return;
    }
}

static void cd4014_inP6(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_P6 = 1;
    else if (f == 0) x->x_P6 = 0;
    else
    {
        post("cd4014 inlet P6 accepts 1 or 0.");
        return;
    }
}

static void cd4014_inP7(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_P7 = 1;
    else if (f == 0) x->x_P7 = 0;
    else
    {
        post("cd4014 inlet P7 accepts 1 or 0.");
        return;
    }
}

static void cd4014_inP8(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_P8 = 1;
    else if (f == 0) x->x_P8 = 0;
    else
    {
        post("cd4014 inlet P8 accepts 1 or 0.");
        return;
    }
}

static void cd4014_inParallelSerial(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_ParallelSerial = 1;
    else if (f == 0) x->x_ParallelSerial = 0;
    else
    {
        post("cd4014 inlet ParallelSerial accepts 1 or 0.");
        return;
    }
}

static void cd4014_inSerial(t_cd4014 *x, t_float f)
{
    if (f == 1) x->x_SerialIn = 1;
    else if (f == 0) x->x_SerialIn = 0;
    else
    {
        post("cd4014 inlet Serial accepts 1 or 0.");
        return;
    }
}

static void cd4014_update_outlets(t_cd4014 *x)
{ /* Output Q8, Q7, Q6 */

    outlet_float(x->x_outQ8, ((x->x_Q & 128) != 0)?1:0);
    outlet_float(x->x_outQ7, ((x->x_Q & 64) != 0)?1:0);
    outlet_float(x->x_outQ6, ((x->x_Q & 32) != 0)?1:0);
}

static void cd4014_free(t_cd4014 *x)
{
    return;
}

static void *cd4014_new(t_symbol *s, int argc, t_atom *argv)
{
    t_cd4014           *x;

    x = (t_cd4014 *)pd_new(cd4014_class);
    if (x == NULL) return (x);
    x->x_outQ6 = outlet_new((t_object *)x, &s_float);
    x->x_outQ7 = outlet_new((t_object *)x, &s_float);
    x->x_outQ8 = outlet_new((t_object *)x, &s_float);
    x->x_inP1 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P1"));
    x->x_inP2 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P2"));
    x->x_inP3 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P3"));
    x->x_inP4 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P4"));
    x->x_inP5 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P5"));
    x->x_inP6 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P6"));
    x->x_inP7 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P7"));
    x->x_inP8 = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P8"));
    x->x_inParallelSerial = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ParallelSerial"));
    x->x_inSerial = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("Serial"));
    return (x);
}

void cd4014_setup(void)
{
    cd4014_class = class_new(gensym("cd4014"),
                    (t_newmethod)cd4014_new,
                    (t_method)cd4014_free,
                    sizeof(t_cd4014), 0, 0); /* no arguments */
    class_addfloat(cd4014_class, cd4014_float);
    class_addbang(cd4014_class, cd4014_bang);
    class_addmethod(cd4014_class, (t_method)cd4014_inP1, gensym("P1"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inP2, gensym("P2"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inP3, gensym("P3"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inP4, gensym("P4"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inP5, gensym("P5"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inP6, gensym("P6"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inP7, gensym("P7"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inP8, gensym("P8"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inParallelSerial, gensym("ParallelSerial"), A_FLOAT, 0);
    class_addmethod(cd4014_class, (t_method)cd4014_inSerial, gensym("Serial"), A_FLOAT, 0);
}
/* end cd4014.c */