forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node.cpp
206 lines (184 loc) · 4.45 KB
/
Node.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
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
// Copyright (c) 2011, 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 "Node.h"
#include "Core.h"
XLink::XLink() :
destNode(0),
destXLinkNum(0),
enabled(false),
fiveWire(false),
network(0),
direction(0),
// TODO find out defaults.
interTokenDelay(0),
interSymbolDelay(0)
{
}
const XLink *XLink::getDestXLink() const
{
if (!destNode)
return 0;
return &destNode->getXLink(destXLinkNum);
}
bool XLink::isConnected() const
{
if (!isEnabled())
return false;
const XLink *otherEnd = getDestXLink();
if (!otherEnd || !otherEnd->isEnabled())
return false;
return isFiveWire() == otherEnd->isFiveWire();
}
Node::Node(Type t, unsigned numXLinks) :
jtagIndex(0),
nodeID(0),
parent(0),
type(t),
sswitch(this),
coreNumberBits(0)
{
xLinks.resize(numXLinks);
}
Node::~Node()
{
for (std::vector<Core*>::iterator it = cores.begin(), e = cores.end();
it != e; ++it) {
delete *it;
}
}
void Node::finalize()
{
computeCoreNumberBits();
directions.resize(getNodeNumberBits());
if (type == XS1_G) {
for (unsigned i = 0, e = directions.size(); i != e; ++i) {
directions[i] = i;
}
}
for (std::vector<Core*>::iterator it = cores.begin(), e = cores.end();
it != e; ++it) {
(*it)->updateIDs();
(*it)->finalize();
}
sswitch.initRegisters();
}
void Node::addCore(std::auto_ptr<Core> c)
{
c->setParent(this);
cores.push_back(c.get());
c.release();
}
void Node::setNodeID(unsigned value)
{
nodeID = value;
for (std::vector<Core*>::iterator it = cores.begin(), e = cores.end();
it != e; ++it) {
(*it)->updateIDs();
}
}
/// Returns the minimum number of bits needed to encode the specified number
/// of values.
static unsigned getMinimumBits(unsigned values)
{
if (values == 0)
return 0;
return 32 - countLeadingZeros(values - 1);
}
void Node::computeCoreNumberBits()
{
coreNumberBits = getMinimumBits(cores.size());
if (getType() == Node::XS1_G && coreNumberBits < 8)
coreNumberBits = 8;
}
unsigned Node::getCoreNumberBits() const
{
return coreNumberBits;
}
unsigned Node::getNodeNumberBits() const
{
return 16 - coreNumberBits;
}
uint32_t Node::getCoreID(unsigned coreNum) const
{
unsigned coreBits = getCoreNumberBits();
assert(coreNum <= makeMask(getCoreNumberBits()));
return (getNodeID() << coreBits) | coreNum;
}
bool Node::getTypeFromJtagID(unsigned jtagID, Type &type)
{
switch (jtagID) {
default:
return false;
case 0x2731:
type = XS1_G;
return true;
case 0x2633:
type = XS1_L;
return true;
}
}
bool Node::hasMatchingNodeID(ResourceID ID)
{
switch (type) {
default: assert(0 && "Unexpected node type");
case Node::XS1_G:
return ((ID.node() >> 8) & 0xff) == nodeID;
case Node::XS1_L:
return ID.node() == nodeID;
}
}
void Node::connectXLink(unsigned num, Node *destNode, unsigned destNum)
{
xLinks[num].destNode = destNode;
xLinks[num].destXLinkNum = destNum;
}
XLink *Node::getXLinkForDirection(unsigned direction)
{
for (unsigned i = 0, e = xLinks.size(); i != e; ++i) {
XLink &xLink = xLinks[i];
if (xLink.isEnabled() && xLinks[i].getDirection() == direction)
return &xLink;
}
return 0;
}
ChanEndpoint *Node::getChanendDest(ResourceID ID)
{
Node *node = this;
// Use Brent's algorithm to detect cycles.
Node *tortoise = node;
unsigned hops = 0;
unsigned leapCount = 8;
while (1) {
unsigned destNode = ID.node() >> node->getCoreNumberBits();
unsigned diff = destNode ^ node->getNodeID();
if (diff == 0)
break;
// Lookup direction
unsigned bit = countLeadingZeros(diff) + getNodeNumberBits() - 32;
unsigned direction = directions[bit];
// Lookup Xlink.
XLink *xLink = getXLinkForDirection(direction);
if (!xLink || !xLink->isConnected())
return 0;
node = xLink->destNode;
++hops;
// Junk message if a cycle is detected.
if (node == tortoise)
return 0;
if (hops == leapCount) {
leapCount <<= 1;
tortoise = node;
}
}
if (ID.isConfig() && ID.num() == RES_CONFIG_SSCTRL) {
return &node->sswitch;
}
unsigned destCore = ID.node() & makeMask(node->getCoreNumberBits());
if (destCore >= node->cores.size())
return 0;
ChanEndpoint *dest = 0;
node->getCores()[destCore]->getLocalChanendDest(ID, dest);
return dest;
}