aboutsummaryrefslogtreecommitdiff
path: root/cmos/cd4516.c
blob: 2949dcfc8fb41a3b9b0eb8294fbea6ba353066e1 (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
/* cd4516.c MP 20070312 */
/* Emulate a cd4516b */
#include "m_pd.h"

typedef struct _cd4516
{
    t_object        x_obj;
    t_outlet        *x_Q1Out;
    t_outlet        *x_Q2Out;
    t_outlet        *x_Q3Out;
    t_outlet        *x_Q4Out;
    t_outlet        *x_CarryOut;
    t_int           x_clock;
    t_int           x_upDown;
    t_int           x_preset_enable;
    t_int           x_carry;
    t_int           x_P1;
    t_int           x_P2;
    t_int           x_P3;
    t_int           x_P4;
    t_int           x_reset;
    t_int           x_count;
    t_inlet         *x_UpDownIn;/* All inlets take one or zero as acceptable inputs. */
    t_inlet         *x_ResetIn;
    t_inlet         *x_PresetEnableIn;
    t_inlet         *x_CarryIn;
    t_inlet         *x_P1In;
    t_inlet         *x_P2In;
    t_inlet         *x_P3In;
    t_inlet         *x_P4In;
    /* 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_cd4516;

static t_class *cd4516_class;

void cd4516_setup(void);
static void *cd4516_new(t_symbol *s, int argc, t_atom *argv);
static void cd4516_free(t_cd4516 *x);
static void cd4516_bang(t_cd4516 *x);
static void cd4516_float(t_cd4516 *x, t_float f);
static void cd4516_reset(t_cd4516 *x, t_float f);
static void cd4516_preset_enable(t_cd4516 *x, t_float f);
static void cd4516_up_down(t_cd4516 *x, t_float f);
static void cd4516_carry(t_cd4516 *x, t_float f);
static void cd4516_P1(t_cd4516 *x, t_float f);
static void cd4516_P2(t_cd4516 *x, t_float f);
static void cd4516_P3(t_cd4516 *x, t_float f);
static void cd4516_P4(t_cd4516 *x, t_float f);
static void cd4516_update_outlets(t_cd4516 *x);

static void cd4516_float(t_cd4516 *x, t_float f)
{
    if (f == 1)
    { /* if clock is high and was low, count up. */
        if ((x->x_clock == 0)&&(x->x_reset == 0)&&(x->x_preset_enable == 0)&&(x->x_carry == 0))
        {
            x->x_clock = 1;
            if (x->x_upDown == 1) x->x_count = (x->x_count + 1)%16;
            else x->x_count = (x->x_count - 1)%16;
            cd4516_update_outlets(x);
        }
    }
    else if (f == 0)
    {
        x->x_clock = 0;
    }
    else post("cd4516 accepts bang, 1 or 0.");
}

static void cd4516_bang(t_cd4516 *x)
{
    if ((x->x_reset == 0)&&(x->x_preset_enable == 0)&&(x->x_carry == 0))
    {
        if (x->x_upDown == 1)x->x_count = (x->x_count + 1)%16;
        else x->x_count = (x->x_count - 1)%16;
        cd4516_update_outlets(x);
    }
}

static void cd4516_reset(t_cd4516 *x, t_float f)
{
    if (f == 1)
    {
        if (x->x_reset == 0)
        {
            x->x_count = 0;
            cd4516_update_outlets(x);
            x->x_reset = 1;
        }
    }
    else if (f == 0)
    {
        x->x_reset = 0;
        if (x->x_preset_enable == 1)
        { /* the strange case of a low-going reset enabling an already high preset enable */
            x->x_count = x->x_P1 + 2*x->x_P2 + 4*x->x_P3 + 8*x->x_P4;
            cd4516_update_outlets(x);
        }
    }
    else
    {
        post("cd4516 reset takes 1 or 0 only.");
        return;
    }
}

static void cd4516_preset_enable(t_cd4516 *x, t_float f)
{
    if (f == 0)
    {
        x->x_preset_enable = 0;
    }
    else if (f == 1)
    {
        if (x->x_preset_enable == 0)
        {
            x->x_preset_enable = 1;
            if (x->x_reset == 0)
            {
                x->x_count = x->x_P1 + 2*x->x_P2 + 4*x->x_P3 + 8*x->x_P4;
                cd4516_update_outlets(x);
            }
        }
    }
    else
    {
        post("cd4516 x_preset_enable takes 1 or 0 only.");
        return;
    }
}

static void cd4516_up_down(t_cd4516 *x, t_float f)
{
    if (f == 1)
    {
        x->x_upDown = 1;
    }
    else if (f == 0)
    {
        x->x_upDown = 0;
    }
    else post("cd4516 updown accepts bang, 1 or 0.");
}

static void cd4516_carry(t_cd4516 *x, t_float f)
{
    if (f == 1)
    {
        x->x_carry = 1;
    }
    else if (f == 0)
    {
        x->x_carry = 0;
    }
    else post("cd4516 carry accepts bang, 1 or 0.");
}

static void cd4516_P1(t_cd4516 *x, t_float f)
{
    if (f == 1) x->x_P1 = 1;
    else if (f == 0) x->x_P1 = 0;
    else
    {
        post("cd4516 P1 takes 1 or 0 only.");
        return;
    }
}
static void cd4516_P2(t_cd4516 *x, t_float f)
{
    if (f == 1) x->x_P2 = 1;
    else if (f == 0) x->x_P2 = 0;
    else
    {
        post("cd4516 P2 takes 1 or 0 only.");
        return;
    }
}
static void cd4516_P3(t_cd4516 *x, t_float f)
{
    if (f == 1) x->x_P3 = 1;
    else if (f == 0) x->x_P3 = 0;
    else
    {
        post("cd4516 P3 takes 1 or 0 only.");
        return;
    }
}
static void cd4516_P4(t_cd4516 *x, t_float f)
{
    if (f == 1) x->x_P4 = 1;
    else if (f == 0) x->x_P4 = 0;
    else
    {
        post("cd4516 P4 takes 1 or 0 only.");
        return;
    }
}

static void cd4516_update_outlets(t_cd4516 *x)
{
    if (x->x_upDown == 1) outlet_float(x->x_CarryOut, (x->x_count == 15)?0:1);
    else outlet_float(x->x_CarryOut, (x->x_count == 0)?0:1);
    outlet_float(x->x_Q4Out, ((x->x_count & 8) != 0)?1:0);
    outlet_float(x->x_Q3Out, ((x->x_count & 4) != 0)?1:0);
    outlet_float(x->x_Q2Out, ((x->x_count & 2) != 0)?1:0);
    outlet_float(x->x_Q1Out, ((x->x_count & 1) != 0)?1:0);
}

static void cd4516_free(t_cd4516 *x)
{
    return;
}

static void *cd4516_new(t_symbol *s, int argc, t_atom *argv)
{
    t_cd4516           *x;

    x = (t_cd4516 *)pd_new(cd4516_class);
    if (x == NULL) return (x);
    x->x_Q1Out = outlet_new((t_object *)x, &s_float);
    x->x_Q2Out = outlet_new((t_object *)x, &s_float);
    x->x_Q3Out = outlet_new((t_object *)x, &s_float);
    x->x_Q4Out = outlet_new((t_object *)x, &s_float);
    x->x_CarryOut = outlet_new((t_object *)x, &s_float);

    x->x_UpDownIn = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("updown"));
    x->x_ResetIn = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("reset"));
    x->x_PresetEnableIn = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("presetenable"));
    x->x_CarryIn = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("carry"));
    x->x_P1In = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P1"));
    x->x_P2In = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P2"));
    x->x_P3In = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P3"));
    x->x_P4In = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("P4"));
    return (x);
}

void cd4516_setup(void)
{
    cd4516_class = class_new(gensym("cd4516"),
                    (t_newmethod)cd4516_new,
                    (t_method)cd4516_free,
                    sizeof(t_cd4516), 0, 0); /* no arguments */
    class_addbang(cd4516_class, cd4516_bang);
    class_addfloat(cd4516_class, cd4516_float);
    class_addmethod(cd4516_class, (t_method)cd4516_up_down, gensym("updown"), A_FLOAT, 0);
    class_addmethod(cd4516_class, (t_method)cd4516_preset_enable, gensym("presetenable"), A_FLOAT, 0);
    class_addmethod(cd4516_class, (t_method)cd4516_reset, gensym("reset"), A_FLOAT, 0);
    class_addmethod(cd4516_class, (t_method)cd4516_carry, gensym("carry"), A_FLOAT, 0);
    class_addmethod(cd4516_class, (t_method)cd4516_P1, gensym("P1"), A_FLOAT, 0);
    class_addmethod(cd4516_class, (t_method)cd4516_P2, gensym("P2"), A_FLOAT, 0);
    class_addmethod(cd4516_class, (t_method)cd4516_P3, gensym("P3"), A_FLOAT, 0);
    class_addmethod(cd4516_class, (t_method)cd4516_P4, gensym("P4"), A_FLOAT, 0);
}
/* end cd4516.c */