forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClockBlock.cpp
170 lines (154 loc) · 3.75 KB
/
ClockBlock.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
// 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 "ClockBlock.h"
#include "Port.h"
ClockBlock::ClockBlock() :
Resource(RES_TYPE_CLKBLK),
source(0),
readyIn(0),
running(false)
{
}
void ClockBlock::updateAttachedPorts(ticks_t time)
{
for (std::set<Port*>::iterator it = ports.begin(), e = ports.end();
it != e; ++it) {
(*it)->update(time);
}
}
void ClockBlock::
seeChangeOnAttachedPorts(ticks_t time)
{
if (!running)
return;
for (std::set<Port*>::iterator it = ports.begin(), e = ports.end();
it != e; ++it) {
(*it)->seeClockChange(time);
}
}
void ClockBlock::
seeEdgeOnAttachedPorts(Edge::Type edgeType, ticks_t time) {
for (std::set<Port*>::iterator it = ports.begin(), e = ports.end();
it != e; ++it) {
(*it)->seeEdge(edgeType, time);
}
}
bool ClockBlock::
setCInUse(Thread &thread, bool val, ticks_t time)
{
updateAttachedPorts(time);
running = false;
divide = 1;
if (readyIn) {
readyIn->deregisterAsReadyInOf(this);
readyIn = 0;
readyInValue.setValue(1);
}
setSourceRefClock(thread, time);
setInUse(val);
return true;
}
bool ClockBlock::setSource(Thread &thread, Port *p, ticks_t time)
{
if (divide != 1)
return false;
p->update(time);
setValue(p->getPinsValue(), time);
if (source)
source->deregisterAsSourceOf(this);
source = p;
source->registerAsSourceOf(this);
return true;
}
void ClockBlock::setSourceRefClock(Thread &thread, ticks_t time)
{
updateAttachedPorts(time);
if (source) {
source->deregisterAsSourceOf(this);
source = 0;
}
Signal newValue = value;
newValue.changeFrequency(time, getHalfPeriod());
setValue(newValue, time);
}
bool ClockBlock::setData(Thread &thread, uint32_t newDivide, ticks_t time) {
if (source)
return false;
newDivide = newDivide & 0xff;
if (newDivide == 0)
divide = 1;
else
divide = 2 * newDivide;
Signal newValue = value;
newValue.changeFrequency(time, getHalfPeriod());
setValue(newValue, time);
return true;
}
void ClockBlock::
setValue(const Signal &newValue, ticks_t time)
{
if (newValue == value)
return;
if (isFixedFrequency() || newValue.isClock())
updateAttachedPorts(time);
uint32_t oldValue = value.getValue(time);
value = newValue;
if (!running)
return;
if (value.getValue(time) != oldValue) {
seeEdgeOnAttachedPorts(oldValue == 0 ? Edge::RISING : Edge::FALLING, time);
}
if (isFixedFrequency()) {
seeChangeOnAttachedPorts(time);
}
}
Signal ClockBlock::getValue() const
{
if (running)
return value;
return Signal(0);
}
bool ClockBlock::setReady(Thread &thread, Port *p, ticks_t time)
{
updateAttachedPorts(time);
p->update(time);
if (p->getPortWidth() != 1)
return false;
if (readyIn)
readyIn->deregisterAsReadyInOf(this);
readyIn = p;
readyIn->registerAsReadyInOf(this);
readyInValue = readyIn->getPinsValue();
seeChangeOnAttachedPorts(time);
return true;
}
void ClockBlock::start(Thread &thread, ticks_t time)
{
updateAttachedPorts(time);
// Start the clock
running = true;
if (!source) {
value.changeFrequency(time, 0, getHalfPeriod());
}
for (std::set<Port*>::iterator it = ports.begin(), e = ports.end();
it != e; ++it) {
// Update ports to current time
(*it)->seeClockStart(time);
}
}
void ClockBlock::stop(Thread &thread, ticks_t time)
{
if (!running)
return;
updateAttachedPorts(time);
running = false;
seeChangeOnAttachedPorts(time);
}
void ClockBlock::setReadyInValue(Signal value, ticks_t time)
{
updateAttachedPorts(time);
readyInValue = value;
seeChangeOnAttachedPorts(time);
}