forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PortArg.cpp
81 lines (75 loc) · 2.05 KB
/
PortArg.cpp
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
// Copyright (c) 2012, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include "PortArg.h"
#include "PortNames.h"
#include <iostream>
#include <cstdlib>
#include "SystemState.h"
#include "Node.h"
#include "Core.h"
bool PortArg::parse(const std::string &s, PortArg &arg)
{
size_t pos;
std::string core;
std::string port;
if ((pos = s.find_first_of(':')) != std::string::npos) {
port = s.substr(pos + 1);
if (port.find_first_of(':') != std::string::npos)
return false;
core = s.substr(0, pos);
} else {
port = s;
}
arg = PortArg(core, port);
return true;
};
static bool convertPortString(const std::string &s, uint32_t &id)
{
if (s.substr(0, 4) == "XS1_") {
return getPortId(s.substr(4), id);
}
if (getPortId(s, id)) {
return true;
}
char *endp;
long value = std::strtol(s.c_str(), &endp, 0);
if (*endp != '\0')
return false;
id = value;
return true;
}
/// Finds the first core match with the specified code reference. If the
/// code reference is empty then the first core is returned.
static Core *findMatchingCore(const std::string &s, SystemState &system)
{
for (SystemState::node_iterator outerIt = system.node_begin(),
outerE = system.node_end(); outerIt != outerE; ++outerIt) {
Node &node = **outerIt;
for (Node::core_iterator innerIt = node.core_begin(),
innerE = node.core_end(); innerIt != innerE; ++innerIt) {
Core *core = *innerIt;
if (s.empty() || core->getCodeReference() == s)
return core;
}
}
return 0;
}
Port *PortArg::lookup(SystemState &system) const
{
Core *c = findMatchingCore(core, system);
if (!c)
return 0;
uint32_t id;
if (!convertPortString(port, id))
return 0;
Resource *res = c->getResourceByID(id);
if (!res || res->getType() != RES_TYPE_PORT)
return 0;
return static_cast<Port*>(res);
}
void PortArg::dump(std::ostream &s) const
{
s << port;
}