aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/convert/gen_convert_impl.py
blob: 70d437ba2ad8cf8bd535d7320e9bafeb4d2dc69f (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
#!/usr/bin/env python
#
# Copyright 2010-2011 Ettus Research LLC
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#

TMPL_TEXT = """
#import time
/***********************************************************************
 * This file was generated by $file on $time.strftime("%c")
 **********************************************************************/
typedef size_t pred_type;

\#include <uhd/utils/algorithm.hpp>
\#include <boost/lexical_cast.hpp>
\#include <boost/detail/endian.hpp>
\#include <boost/cstdint.hpp>
\#include <stdexcept>

enum dir_type{
    DIR_OTW_TO_CPU = 0,
    DIR_CPU_TO_OTW = 1
};

struct pred_error : std::runtime_error{
    pred_error(const std::string &what)
    :std::runtime_error("convert::make_pred: " + what){
        /* NOP */
    }
};

pred_type make_pred(const std::string &markup, dir_type &dir){
    pred_type pred = 0;

    try{
        std::vector<std::string> tokens = std::split_string(markup, "_");
        //token 0 is <convert>
        std::string inp_type = tokens.at(1);
        std::string num_inps = tokens.at(2);
        //token 3 is <to>
        std::string out_type = tokens.at(4);
        std::string num_outs = tokens.at(5);
        std::string swap_type = tokens.at(6);

        std::string cpu_type, otw_type;
        if (inp_type.find("item") == std::string::npos){
            cpu_type = inp_type;
            otw_type = out_type;
            dir = DIR_CPU_TO_OTW;
        }
        else{
            cpu_type = out_type;
            otw_type = inp_type;
            dir = DIR_OTW_TO_CPU;
        }

        if      (cpu_type == "fc32") pred |= $ph.fc32_p;
        else if (cpu_type == "sc16") pred |= $ph.sc16_p;
        else throw pred_error("unhandled io type " + cpu_type);

        if (otw_type == "item32") pred |= $ph.item32_p;
        else throw pred_error("unhandled otw type " + otw_type);

        int num_inputs = boost::lexical_cast<int>(num_inps);
        int num_outputs = boost::lexical_cast<int>(num_outs);

        switch(num_inputs*num_outputs){ //FIXME treated as one value
        case 1: pred |= $ph.chan1_p; break;
        case 2: pred |= $ph.chan2_p; break;
        case 3: pred |= $ph.chan3_p; break;
        case 4: pred |= $ph.chan4_p; break;
        default: throw pred_error("unhandled number of channels");
        }

        if      (swap_type == "bswap") pred |= $ph.bswap_p;
        else if (swap_type == "nswap") pred |= $ph.nswap_p;
        else throw pred_error("unhandled swap type");

    }
    catch(...){
        throw pred_error("could not parse markup: " + markup);
    }

    return pred;
}

UHD_INLINE pred_type make_pred(
    const io_type_t &io_type,
    const otw_type_t &otw_type,
    size_t num_inputs,
    size_t num_outputs
){
    pred_type pred = 0;

    switch(otw_type.byteorder){
    \#ifdef BOOST_BIG_ENDIAN
    case otw_type_t::BO_BIG_ENDIAN:    pred |= $ph.nswap_p; break;
    case otw_type_t::BO_LITTLE_ENDIAN: pred |= $ph.bswap_p; break;
    \#else
    case otw_type_t::BO_BIG_ENDIAN:    pred |= $ph.bswap_p; break;
    case otw_type_t::BO_LITTLE_ENDIAN: pred |= $ph.nswap_p; break;
    \#endif
    case otw_type_t::BO_NATIVE:        pred |= $ph.nswap_p; break;
    default: throw pred_error("unhandled otw byteorder type");
    }

    switch(otw_type.get_sample_size()){
    case sizeof(boost::uint32_t): pred |= $ph.item32_p; break;
    default: throw pred_error("unhandled otw sample size");
    }

    switch(io_type.tid){
    case io_type_t::COMPLEX_FLOAT32: pred |= $ph.fc32_p; break;
    case io_type_t::COMPLEX_INT16:   pred |= $ph.sc16_p; break;
    default: throw pred_error("unhandled io type id");
    }

    switch(num_inputs*num_outputs){ //FIXME treated as one value
    case 1: pred |= $ph.chan1_p; break;
    case 2: pred |= $ph.chan2_p; break;
    case 3: pred |= $ph.chan3_p; break;
    case 4: pred |= $ph.chan4_p; break;
    default: throw pred_error("unhandled number of channels");
    }

    return pred;
}
"""

def parse_tmpl(_tmpl_text, **kwargs):
    from Cheetah.Template import Template
    return str(Template(_tmpl_text, kwargs))

class ph:
    bswap_p  = 0b00001
    nswap_p  = 0b00000
    item32_p = 0b00000
    sc16_p   = 0b00010
    fc32_p   = 0b00000
    chan1_p  = 0b00000
    chan2_p  = 0b00100
    chan3_p  = 0b01000
    chan4_p  = 0b01100

    nbits = 4 #see above

if __name__ == '__main__':
    import sys, os
    file = os.path.basename(__file__)
    open(sys.argv[1], 'w').write(parse_tmpl(TMPL_TEXT, file=file, ph=ph))