File: register.c

package info (click to toggle)
urjtag 2021.03-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: ansic: 56,769; sh: 4,897; yacc: 2,159; lex: 1,545; vhdl: 1,324; makefile: 1,275; python: 72; sed: 16
file content (348 lines) | stat: -rw-r--r-- 10,034 bytes parent folder | download | duplicates (4)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * $Id$
 *
 * Copyright (C) 2011 Steve Tell
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Python bindings for urjtag intially written by Steve Tell.
 * Additional methods by Jonathan Stroud.
 *
 */
#include <Python.h>
#include "structmember.h"
#include "pycompat23.h"

#include <sysdep.h>

#include <urjtag/urjtag.h>
#include <urjtag/chain.h>
#include <urjtag/cmd.h>

#include "py_urjtag.h"

extern PyObject *UrjtagError;

static void
urj_pyr_dealloc (urj_pyregister_t *self)
{
    Py_TYPE (self)->tp_free ((PyObject *) self);
}

static PyObject *
urj_pyr_str (PyObject *s)
{
    urj_pyregister_t *self = (urj_pyregister_t *) s;
    char buf[200];
    urj_chain_t *urc = self->urc;

    if (self->urreg == NULL || self->inst == NULL)
        snprintf (buf, 200, "<urjtag.register chain=%p invalid>", urc);
    else
        snprintf (buf, 200, "<urjtag.register chain=%p reg=%s inst=%s>", urc, 
             self->urreg->name, self->inst->name);
    return PyUnicode_FromString (buf);
}


static PyObject *
urj_pyr_get_dr (urj_pyregister_t *self, int in, int string, PyObject *args)
{
    urj_data_register_t *dr = self->urreg;
    urj_chain_t *urc = self->urc;
    urj_tap_register_t *r;
    int lsb = -1;
    int msb = -1;
    const char *value_string;

    if (!PyArg_ParseTuple (args, "|ii", &msb, &lsb))
        return NULL;
    if (lsb == -1)
        lsb = msb;
    if (!urj_pyc_precheck (urc, UPRC_CBL))
        return NULL;

    if (dr == NULL)
    {
        PyErr_SetString (UrjtagError,
                         _("invalid data register object"));
        return NULL;
    }

    if (in)
        r = dr->in;             /* input buffer for next shift_dr */
    else
        r = dr->out;            /* recently captured+scanned-out values */

    if (msb == -1)
        value_string = urj_tap_register_get_string (r);
    else
        value_string = urj_tap_register_get_string_bit_range (r, msb, lsb);
    if (value_string == NULL)
    {
        PyErr_SetString (UrjtagError,
                         _("error obtaining tap register value"));
        return NULL;
    }

    if (string)
        return Py_BuildValue ("s", value_string);
    else
        return PyLong_FromString ((char *)value_string, NULL, 2);
}

static PyObject *
urj_pyr_get_str_dr_out (urj_pyregister_t *self, PyObject *args)
{
    return urj_pyr_get_dr (self, 0, 1, args);
}

static PyObject *
urj_pyr_get_str_dr_in (urj_pyregister_t *self, PyObject *args)
{
    return urj_pyr_get_dr (self, 1, 1, args);
}

static PyObject *
urj_pyr_get_int_dr_out (urj_pyregister_t *self, PyObject *args)
{
    return urj_pyr_get_dr (self, 0, 0, args);
}

static PyObject *
urj_pyr_get_int_dr_in (urj_pyregister_t *self, PyObject *args)
{
    return urj_pyr_get_dr (self, 1, 0, args);
}

static PyObject *
urj_pyr_set_dr (urj_pyregister_t *self, int in, PyObject *args)
{
    urj_data_register_t *dr = self->urreg;
    urj_tap_register_t *r;
    char *newstr = NULL;
    uint64_t newval;
    int lsb = -1;
    int msb = -1;


    if (!PyArg_ParseTuple (args, "s|ii", &newstr, &msb, &lsb))
    {
        PyErr_Clear ();
        if (!PyArg_ParseTuple (args, "L|ii", &newval, &msb, &lsb))
            return NULL;
    }

    if (dr == NULL)
    {
        PyErr_SetString (UrjtagError,
                         _("invalid register object"));
        return NULL;
    }

    if (in)
        r = dr->in;
    else
        r = dr->out;

    if (msb == -1)
    {
        if (newstr)
            return urj_py_chkret (urj_tap_register_set_string (r, newstr));
        else
            return urj_py_chkret (urj_tap_register_set_value (r, newval));
    }
    else
    {
        if (lsb == -1)
            lsb = msb;

        if (newstr)
            return urj_py_chkret (urj_tap_register_set_string_bit_range (r, newstr, msb, lsb));
        else
            return urj_py_chkret (urj_tap_register_set_value_bit_range (r, newval, msb, lsb));
    }
}

static PyObject *
urj_pyr_set_dr_out (urj_pyregister_t *self, PyObject *args)
{
    return urj_pyr_set_dr (self, 0, args);
}

static PyObject *
urj_pyr_set_dr_in (urj_pyregister_t *self, PyObject *args)
{
    return urj_pyr_set_dr (self, 1, args);
}

static PyObject *
urj_pyr_shift_dr (urj_pyregister_t *self, PyObject *args)
{

    urj_chain_t *urc = self->urc;
    int partn = self->part;
    char *instname = NULL;
    urj_part_t *part;

    if (!PyArg_ParseTuple (args, "|s", &instname))
        return NULL;


    if (!urj_pyc_precheck (urc, UPRC_CBL))
        return NULL;

    if (self->urreg == NULL)
    {
        PyErr_SetString (UrjtagError,
                         _("invalid register object"));
        return NULL;
    }

    
    urc->active_part = partn;
    part = urj_tap_chain_active_part (urc);
    if (part == NULL)
    {
        PyErr_SetString (UrjtagError, _("No active part on chain"));
        return NULL;
    }
    if (instname) 
    {
        urj_part_set_instruction (part, instname);
    }
    else 
    {
        if (!self->inst) { 
            PyErr_SetString (UrjtagError, _("no instruction for data register"));
            return NULL;
        }
        part->active_instruction = self->inst;
    }

    return urj_py_chkret (urj_tap_chain_shift_data_registers (urc, 1));
}

static PyObject *
urj_pyr_shift_ir (urj_pyregister_t *self,  PyObject *args)
{
    urj_chain_t *urc = self->urc;
    char *instname = NULL;
    urj_part_t *part;

    if (!PyArg_ParseTuple (args, "|s", &instname)) 
        return NULL;

    if (!urj_pyc_precheck (urc, UPRC_CBL))
        return NULL;

    urc->active_part = self->part;
    part = urj_tap_chain_active_part (urc);
    if (part == NULL)
    {
        PyErr_SetString (UrjtagError, _("No active part on chain"));
        return NULL;
    }
    if (instname) {
        urj_part_set_instruction (part, instname);
    } 
    else 
    {
        if (!self->inst) 
        { 
            PyErr_SetString (UrjtagError, _("no instruction for data register"));
            return NULL;
        }
        part->active_instruction = self->inst;
    }

    return urj_py_chkret (urj_tap_chain_shift_instructions (urc));
}



static PyMethodDef urj_pyr_methods[] =
{
    {"get_dr_in", (PyCFunction) urj_pyr_get_int_dr_in, METH_VARARGS,
     "get bits that will be scanned in on next shift_dr, as integer"},
    {"get_dr_in_string", (PyCFunction) urj_pyr_get_str_dr_in, METH_VARARGS,
     "get bits that will be scanned in on next shift_dr, as string"},
    {"get_dr_out", (PyCFunction) urj_pyr_get_int_dr_out, METH_VARARGS,
     "retrieve values scanned out from the data registers on the last shift_dr, as integer"},
    {"get_dr_out_string", (PyCFunction) urj_pyr_get_str_dr_out, METH_VARARGS,
     "retrieve values scanned out from the data registers on the last shift_dr, as string"},

    {"set_dr_in", (PyCFunction) urj_pyr_set_dr_in, METH_VARARGS,
     "set bits that will be scanned in on next shiftdr"},
    {"set_dr_out", (PyCFunction) urj_pyr_set_dr_out, METH_VARARGS,
     "set the holding register for values scanned out from the data register"},
    {"shift_dr", (PyCFunction) urj_pyr_shift_dr, METH_VARARGS,
     "scan values through the data register"},
    {"shift_ir", (PyCFunction) urj_pyr_shift_ir, METH_VARARGS,
     "scan values through the instruction register to select this data regsiter"},
    {NULL},
};


PyTypeObject urj_pyregister_Type =
{
    PyVarObject_HEAD_INIT (NULL, 0) "urjtag.register", /* tp_name */
    sizeof (urj_pyregister_t),             /* tp_basicsize */
    0,                          /* tp_itemsize */
    (destructor) urj_pyr_dealloc, /* tp_dealloc */
    0,                          /* tp_print */
    0,                          /* tp_getattr */
    0,                          /* tp_setattr */
    0,                          /* tp_compare */
    urj_pyr_str,                /* tp_repr */
    0,                          /* tp_as_number */
    0,                          /* tp_as_sequence */
    0,                          /* tp_as_mapping */
    0,                          /* tp_hash */
    0,                          /* tp_call */
    urj_pyr_str,                /* tp_str */
    0,                          /* tp_getattro */
    0,                          /* tp_setattro */
    0,                          /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
    "JTAG register object",     /* tp_doc */
    0,                          /* tp_traverse */
    0,                          /* tp_clear */
    0,                          /* tp_richcompare */
    0,                          /* tp_weaklistoffset */
    0,                          /* tp_iter */
    0,                          /* tp_iternext */
    urj_pyr_methods,            /* tp_methods */
    0,                          /* tp_members */
    0,                          /* tp_getset */
    0,                          /* tp_base */
    0,                          /* tp_dict */
    0,                          /* tp_descr_get */
    0,                          /* tp_descr_set */
    0,                          /* tp_dictoffset */
    0,                          /* tp_init */
    0,                          /* tp_alloc */
    0,                          /* tp_new */
};


/* Local Variables: */
/* mode:c */
/* comment-column:0 */
/* c-basic-offset:4 */
/* space-before-funcall:t */
/* indent-tabs-mode:nil */
/* End: */