aboutsummaryrefslogtreecommitdiff
path: root/cmos/cd4027.c
blob: 18058a68c11fc9bb00ec88820de12de2d8acb831 (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
/* cd4027.c MP 20070306 */
/* Emulate a cd4027b */
#include "m_pd.h"

typedef struct _cd4027
{
    t_object        x_obj;
    int             x_state;/* set = bit0, clock = bit1, reset = bit2 j = bit3 k = bit4 q = bit 5 */
    t_outlet        *x_Q;
    t_outlet        *x_notQ;
    t_inlet         *x_set;/* set takes one or zero as acceptable inputs. */
    t_inlet         *x_reset;/* reset takes one or zero as acceptable inputs. */
    t_inlet         *x_j;/* j takes one or zero as acceptable inputs. */
    t_inlet         *x_k;/* k 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_cd4027;
#define SET_4027 1
#define CLOCK_4027 2
#define RESET_4027 4
#define J_4027 8
#define K_4027 16
#define Q_4027 32
#define UPDATING_4027 64
#define CHANGED_4027 128

static t_class *cd4027_class;

void cd4027_setup(void);
static void *cd4027_new(t_symbol *s, int argc, t_atom *argv);
static void cd4027_free(t_cd4027 *x);
static void cd4027_bang(t_cd4027 *x);
static void cd4027_float(t_cd4027 *x, t_float f);
static void cd4027_set(t_cd4027 *x, t_float f);
static void cd4027_reset(t_cd4027 *x, t_float f);
static void cd4027_j(t_cd4027 *x, t_float f);
static void cd4027_k(t_cd4027 *x, t_float f);
static void cd4027_update_outlets(t_cd4027 *x);
static void cd4027_printstate (t_cd4027 *x);

static void cd4027_float(t_cd4027 *x, t_float f)
{
    if (f == 1)
    { /* if clock is high and was low, clock it. */
        if ((x->x_state & CLOCK_4027) == 0) cd4027_bang(x);
        x->x_state |= CLOCK_4027;
    }
    else if (f == 0) x->x_state &= ~CLOCK_4027;
    else post("cd4027 accepts bang, 1 or 0.");
}

static void cd4027_printstate (t_cd4027 *x)
{
    post ("SET %d RESET %d J %d K %d Q %d", ((x->x_state & SET_4027) != 0)?1:0
        , ((x->x_state & RESET_4027) != 0)?1:0
        , ((x->x_state & J_4027) != 0)?1:0
        , ((x->x_state & K_4027) != 0)?1:0
        , ((x->x_state & Q_4027) != 0)?1:0);
}

static void cd4027_bang(t_cd4027 *x)
{
    if ((x->x_state & (SET_4027 | RESET_4027)) == 0)
    { /* if set is low and reset is low, clock forward */
        if ((x->x_state & Q_4027) != 0)
        { /* Q is high */
            if ((x->x_state & K_4027)== 0) x->x_state |= Q_4027;
            else  x->x_state &= ~Q_4027;
        }
        else
        { /* Q is low */
            if ((x->x_state & J_4027) != 0) x->x_state |= Q_4027;
            else  x->x_state &= ~Q_4027;
        }
        cd4027_update_outlets(x);
    }
}

static void cd4027_set(t_cd4027 *x, t_float f)
{
    if (f == 1)
    {
        x->x_state |= SET_4027; /* set = 1 */
    }
    else if (f == 0)
    {
        x->x_state &= ~SET_4027; /* set = 0 */
    }
    else
    {
        post("cd4027 set takes 1 or 0 only.");
        return;
    }
    /* update outlets if not already doing that */
    if ((x->x_state & UPDATING_4027) == 0)
    {
        cd4027_update_outlets(x);
    }
    else
    {
        x->x_state |= CHANGED_4027;
    }
    return;
}

static void cd4027_reset(t_cd4027 *x, t_float f)
{
    if (f == 1)
    {
        x->x_state |= RESET_4027; /* reset = 1 */
    }
    else if (f == 0)
    {
        x->x_state &= ~RESET_4027; /* reset = 0 */
    }
    else
    {
        return;
    }
    /* update outlets if not already doing that */
    if ((x->x_state & UPDATING_4027) == 0)
    {
        cd4027_update_outlets(x);
    }
    else
    {
        x->x_state |= CHANGED_4027;
    }
    return;
}

static void cd4027_j(t_cd4027 *x, t_float f)
{
    if (f == 1)
    {
        x->x_state |= J_4027; /* j = 1 */
    }
    else if (f == 0)
    {
        x->x_state &= ~J_4027; /* j = 0 */
    }
    else post("cd4027 j takes 1 or 0 only.");
    return;
}

static void cd4027_k(t_cd4027 *x, t_float f)
{
    if (f == 1)
    {
        x->x_state |= K_4027; /* k = 1 */
    }
    else if (f == 0)
    {
        x->x_state &= ~K_4027; /* k = 0 */
    }
    else post("cd4027 k takes 1 or 0 only.");
    return;
}

static void cd4027_update_outlets(t_cd4027 *x)
{
    /*  cd4027_printstate (x);  */
    x->x_state |= UPDATING_4027;/* updating outlets */
reset:
    if ((x->x_state & SET_4027) != 0) x->x_state |= Q_4027;
    else if ((x->x_state & RESET_4027) != 0) x->x_state &= ~Q_4027;
    x->x_state &= ~CHANGED_4027; /* prepare to flag any changes that occur during outlet_update */
    outlet_float(x->x_notQ, ((x->x_state & Q_4027) == 0)?1:0);
/* we might get set or reset as a result of feedback from one of these outlets. */
    if ((x->x_state & CHANGED_4027) != 0) goto reset;
    outlet_float(x->x_Q, ((x->x_state & Q_4027) != 0)?1:0);
    if ((x->x_state & CHANGED_4027) != 0) goto reset;
    x->x_state &= ~UPDATING_4027;/* finished updating outlets */
}

static void cd4027_free(t_cd4027 *x)
{
    return;
}

static void *cd4027_new(t_symbol *s, int argc, t_atom *argv)
{
    t_cd4027           *x;

    x = (t_cd4027 *)pd_new(cd4027_class);
    if (x == NULL) return (x);
    x->x_Q = outlet_new((t_object *)x, &s_float);
    x->x_notQ = outlet_new((t_object *)x, &s_float);
    x->x_set = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("set"));
    x->x_reset = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("reset"));
    x->x_j = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("j"));
    x->x_k = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("k"));
    return (x);
}

void cd4027_setup(void)
{
    cd4027_class = class_new(gensym("cd4027"),
                    (t_newmethod)cd4027_new,
                    (t_method)cd4027_free,
                    sizeof(t_cd4027), 0, 0); /* no arguments */
    class_addbang(cd4027_class, cd4027_bang);
    class_addfloat(cd4027_class, cd4027_float);
    class_addmethod(cd4027_class, (t_method)cd4027_reset, gensym("reset"), A_FLOAT, 0);
    class_addmethod(cd4027_class, (t_method)cd4027_set, gensym("set"), A_FLOAT, 0);
    class_addmethod(cd4027_class, (t_method)cd4027_j, gensym("j"), A_FLOAT, 0);
    class_addmethod(cd4027_class, (t_method)cd4027_k, gensym("k"), A_FLOAT, 0);
}
/* end cd4027.c */