summaryrefslogtreecommitdiffstats
path: root/utils/regtools/qeditor/backend.cpp
blob: e011965bd2510a017d02fe08aac9f4c0bafa0359 (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
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include "backend.h"

Backend::Backend()
{
}

QStringList Backend::GetSocNameList()
{
    QStringList sl;
    for(size_t i = 0; i < m_socs.size(); i++)
        sl.append(QString(m_socs[i].name.c_str()));
    return sl;
}

bool Backend::GetSocByName(const QString& name, soc_t& s)
{
    for(size_t i = 0; i < m_socs.size(); i++)
        if(m_socs[i].name == name.toStdString())
        {
            s = m_socs[i];
            return true;
        }
    return false;
}

bool Backend::LoadSocDesc(const QString& filename)
{
    bool ret = soc_desc_parse_xml(filename.toStdString(), m_socs);
    emit OnSocListChanged();
    return ret;
}

IoBackend *Backend::CreateFileIoBackend(const QString& filename)
{
    return new FileIoBackend(filename);
}

IoBackend *Backend::CreateDummyIoBackend()
{
    return new DummyIoBackend();
}

IoBackend::IoBackend()
{
}

FileIoBackend::FileIoBackend(const QString& filename)
{
    m_filename = filename;
    Reload();
}

QString FileIoBackend::GetSocName()
{
    return m_soc;
}

bool FileIoBackend::ReadRegister(const QString& name, soc_word_t& value)
{
    if(m_map.find(name) == m_map.end())
        return false;
    value = m_map[name];
    return true;
}

bool FileIoBackend::Reload()
{
    QFile file(m_filename);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;
    m_map.clear();

    QTextStream in(&file);
    while(!in.atEnd())
    {
        QString line = in.readLine();
        int idx = line.indexOf('=');
        if(idx == -1)
            continue;
        QString key = line.left(idx).trimmed();
        bool ok;
        soc_word_t val = line.mid(idx + 1).trimmed().toULong(&ok, 0);
        if(key == "HW")
            m_soc = line.mid(idx + 1).trimmed();
        else if(ok)
            m_map[key] = val;
    }
    return true;
}

DummyIoBackend::DummyIoBackend()
{
}

QString DummyIoBackend::GetSocName()
{
    return "";
}

bool DummyIoBackend::ReadRegister(const QString& name, soc_word_t& value)
{
    (void) name;
    (void) value;
    return false;
}

bool DummyIoBackend::Reload()
{
    return true;
}

BackendHelper::BackendHelper(IoBackend *io_backend, const soc_t& soc)
    :m_io_backend(io_backend), m_soc(soc)
{
}

bool BackendHelper::ReadRegister(const QString& dev, const QString& reg, soc_word_t& v)
{
    return m_io_backend->ReadRegister("HW." + dev + "." + reg, v);
}

bool BackendHelper::ReadRegisterField(const QString& dev, const QString& reg,
    const QString& field, soc_word_t& v)
{
    soc_dev_t *sdev = 0;
    for(size_t i = 0; i < m_soc.dev.size(); i++)
    {
        for(size_t j = 0; j < m_soc.dev[i].addr.size(); j++)
            if(m_soc.dev[i].addr[j].name.c_str() == dev)
                sdev = &m_soc.dev[i];
    }
    if(sdev == 0)
        return false;
    soc_reg_t *sreg = 0;
    for(size_t i = 0; i < sdev->reg.size(); i++)
    {
        for(size_t j = 0; j < sdev->reg[i].addr.size(); j++)
            if(sdev->reg[i].addr[j].name.c_str() == reg)
                sreg = &sdev->reg[i];
    }
    if(sreg == 0)
        return false;
    soc_reg_field_t *sfield = 0;
    for(size_t i = 0; i < sreg->field.size(); i++)
        if(sreg->field[i].name.c_str() == field)
            sfield = &sreg->field[i];
    if(sfield == 0)
        return false;
    if(!ReadRegister(dev, reg, v))
        return false;
    v = (v & sfield->bitmask()) >> sfield->first_bit;
    return true;
}