1 |
2 |
dargor |
#!/usr/bin/env python
|
2 |
|
|
# -*- coding: utf-8 -*-
|
3 |
|
|
|
4 |
|
|
#
|
5 |
|
|
# Basic Channel model
|
6 |
|
|
# * TLM model
|
7 |
|
|
#
|
8 |
|
|
# Author: Oscar Diaz
|
9 |
|
|
# Version: 0.1
|
10 |
|
|
# Date: 03-03-2011
|
11 |
|
|
|
12 |
|
|
#
|
13 |
|
|
# This code is free software; you can redistribute it and/or
|
14 |
|
|
# modify it under the terms of the GNU Lesser General Public
|
15 |
|
|
# License as published by the Free Software Foundation; either
|
16 |
|
|
# version 2.1 of the License, or (at your option) any later version.
|
17 |
|
|
#
|
18 |
|
|
# This code is distributed in the hope that it will be useful,
|
19 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
21 |
|
|
# Lesser General Public License for more details.
|
22 |
|
|
#
|
23 |
|
|
# You should have received a copy of the GNU Lesser General Public
|
24 |
|
|
# License along with this library; if not, write to the
|
25 |
|
|
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
26 |
|
|
# Boston, MA 02111-1307 USA
|
27 |
|
|
#
|
28 |
|
|
|
29 |
|
|
#
|
30 |
|
|
# Changelog:
|
31 |
|
|
#
|
32 |
|
|
# 03-03-2011 : (OD) initial release
|
33 |
|
|
#
|
34 |
|
|
|
35 |
|
|
"""
|
36 |
|
|
Basic channel TLM model
|
37 |
|
|
"""
|
38 |
|
|
|
39 |
|
|
from nocmodel.noc_tlm_base import *
|
40 |
|
|
|
41 |
|
|
# ---------------------------
|
42 |
|
|
# Channel TLM model
|
43 |
|
|
|
44 |
|
|
class basic_channel_tlm(noc_tlm_base):
|
45 |
|
|
"""
|
46 |
|
|
TLM model of a NoC channel. It models a simple FIFO channel with
|
47 |
|
|
adjustable delay. This channel will move any kind of data as a whole, but
|
48 |
|
|
ideally will move packet objects.
|
49 |
|
|
|
50 |
|
|
Attributes:
|
51 |
|
|
* Channel delay: delay in clock ticks.
|
52 |
|
|
|
53 |
|
|
Notes:
|
54 |
|
|
*This model is completely behavioral
|
55 |
|
|
"""
|
56 |
|
|
def __init__(self, channel_ref, channel_delay=1):
|
57 |
|
|
noc_tlm_base.__init__(self)
|
58 |
|
|
if isinstance(channel_ref, channel):
|
59 |
|
|
self.channel_ref = channel_ref
|
60 |
|
|
self.graph_ref = channel_ref.graph_ref
|
61 |
|
|
self.logname = "Channel '%s'" % channel_ref.name
|
62 |
|
|
else:
|
63 |
|
|
raise TypeError("This class needs a channel object as constructor argument.")
|
64 |
|
|
|
65 |
|
|
self.debug("constructor")
|
66 |
|
|
|
67 |
|
|
# channel parameters
|
68 |
|
|
self.channel_delay = channel_delay
|
69 |
|
|
|
70 |
|
|
# list of router endpoints
|
71 |
|
|
self.endpoints = self.channel_ref.endpoints
|
72 |
|
|
|
73 |
|
|
# generators
|
74 |
|
|
self.generators = []
|
75 |
|
|
|
76 |
|
|
self.has_delay = False
|
77 |
|
|
if self.channel_delay > 0:
|
78 |
|
|
self.has_delay = True
|
79 |
|
|
self.delay_fifo = []
|
80 |
|
|
self.delay_fifo_max = 10 # error-catch parameter, avoid fifo excessive growing
|
81 |
|
|
self.delay_event = myhdl.Signal(False)
|
82 |
|
|
# implement delay generator
|
83 |
|
|
@myhdl.instance
|
84 |
|
|
def delay_generator():
|
85 |
|
|
while True:
|
86 |
|
|
while len(self.delay_fifo) > 0:
|
87 |
|
|
# extract packet and recorded time
|
88 |
|
|
timed_packet = self.delay_fifo.pop(0)
|
89 |
|
|
# calculate the exact delay value
|
90 |
|
|
next_delay = self.channel_delay - (myhdl.now() - timed_packet[0])
|
91 |
|
|
if next_delay <= 0:
|
92 |
|
|
self.debug("delay_generator CATCH next_delay is '%d'" % next_delay)
|
93 |
|
|
else:
|
94 |
|
|
yield myhdl.delay(next_delay)
|
95 |
|
|
self.debug("delay_generator sending delayed packet (by %d), timed_packet format %s" % (next_delay, repr(timed_packet)) )
|
96 |
|
|
# use send()
|
97 |
|
|
retval = self.send(*timed_packet[1:])
|
98 |
|
|
# what to do in error case? report and continue
|
99 |
|
|
if retval != noc_tlm_errcodes.no_error:
|
100 |
|
|
self.error("delay_generator send returns code '%d'?" % retval )
|
101 |
|
|
self.delay_event.next = False
|
102 |
|
|
yield self.delay_event
|
103 |
|
|
self.generators.append(delay_generator)
|
104 |
|
|
|
105 |
|
|
self.debugstate()
|
106 |
|
|
|
107 |
|
|
# channel only relays transactions
|
108 |
|
|
# Transaction - related methods
|
109 |
|
|
def send(self, src, dest, packet, addattrs=None):
|
110 |
|
|
"""
|
111 |
|
|
This method will be called by recv (no delay) or by delay_generator
|
112 |
|
|
src always is self
|
113 |
|
|
"""
|
114 |
|
|
# dest MUST be one of the channel endpoints
|
115 |
|
|
self.debug("-> send( %s , %s , %s , %s )" % (repr(src), repr(dest), repr(packet), repr(addattrs)))
|
116 |
|
|
if isinstance(dest, int):
|
117 |
|
|
# assume router direction
|
118 |
|
|
thedest = self.graph_ref.get_router_by_address(dest)
|
119 |
|
|
if thedest == False:
|
120 |
|
|
self.error("-> send: dest %s not found" % repr(dest) )
|
121 |
|
|
return noc_tlm_errcodes.tlm_badcall_send
|
122 |
|
|
elif isinstance(dest, (router, ipcore)):
|
123 |
|
|
thedest = dest
|
124 |
|
|
else:
|
125 |
|
|
self.error("-> send: what is dest '%s'?" % repr(dest) )
|
126 |
|
|
return noc_tlm_errcodes.tlm_badcall_send
|
127 |
|
|
|
128 |
|
|
# check dest as one of the channel endpoints
|
129 |
|
|
if thedest not in self.endpoints:
|
130 |
|
|
self.error("-> send: object %s is NOT one of the channel endpoints [%s,%s]" % (repr(thedest), repr(self.endpoints[0]), repr(self.endpoints[1])) )
|
131 |
|
|
return noc_tlm_errcodes.tlm_badcall_send
|
132 |
|
|
|
133 |
|
|
# call recv on the dest object
|
134 |
|
|
retval = thedest.tlm.recv(self.channel_ref, dest, packet, addattrs)
|
135 |
|
|
|
136 |
|
|
# Something to do with the retval? Only report it.
|
137 |
|
|
self.debug("-> send returns code '%s'" % repr(retval))
|
138 |
|
|
return retval
|
139 |
|
|
|
140 |
|
|
def recv(self, src, dest, packet, addattrs=None):
|
141 |
|
|
"""
|
142 |
|
|
receive a packet from an object. src is the object source and
|
143 |
|
|
it MUST be one of the objects in channel endpoints
|
144 |
|
|
"""
|
145 |
|
|
|
146 |
|
|
self.debug("-> recv( %s , %s , %s , %s )" % (repr(src), repr(dest), repr(packet), repr(addattrs)))
|
147 |
|
|
# src can be an address or a noc object.
|
148 |
|
|
if isinstance(src, int):
|
149 |
|
|
# assume router direction
|
150 |
|
|
thesrc = self.graph_ref.get_router_by_address(src)
|
151 |
|
|
if thesrc == False:
|
152 |
|
|
self.error("-> recv: src %s not found" % repr(src) )
|
153 |
|
|
return noc_tlm_errcodes.tlm_badcall_recv
|
154 |
|
|
elif isinstance(src, (router, ipcore)):
|
155 |
|
|
thesrc = src
|
156 |
|
|
else:
|
157 |
|
|
self.error("-> recv: what is src '%s'?" % repr(src) )
|
158 |
|
|
return noc_tlm_errcodes.tlm_badcall_recv
|
159 |
|
|
|
160 |
|
|
# check src as one of the channel endpoints
|
161 |
|
|
if thesrc not in self.endpoints:
|
162 |
|
|
self.error("-> recv: object %s is NOT one of the channel endpoints [%s,%s]" % (repr(thesrc), repr(self.endpoints[0]), repr(self.endpoints[1])) )
|
163 |
|
|
return noc_tlm_errcodes.tlm_badcall_recv
|
164 |
|
|
|
165 |
|
|
# calculate the other endpoint
|
166 |
|
|
end_index = self.endpoints.index(thesrc) - 1
|
167 |
|
|
|
168 |
|
|
if self.has_delay:
|
169 |
|
|
# put in delay fifo: store time and call attributes
|
170 |
|
|
self.delay_fifo.append([myhdl.now(), self.channel_ref, self.endpoints[end_index], packet, addattrs])
|
171 |
|
|
self.debug("-> recv put in delay_fifo (delay %d)" % self.channel_delay)
|
172 |
|
|
# catch growing fifo
|
173 |
|
|
if len(self.delay_fifo) > self.delay_fifo_max:
|
174 |
|
|
self.warning("-> recv: delay_fifo is getting bigger! current size is %d" % len(self.delay_fifo) )
|
175 |
|
|
# trigger event
|
176 |
|
|
self.delay_event.next = True
|
177 |
|
|
retval = noc_tlm_errcodes.no_error
|
178 |
|
|
else:
|
179 |
|
|
# use send() call directly
|
180 |
|
|
router_dest = self.endpoints[end_index]
|
181 |
|
|
retval = self.send(self.channel_ref, router_dest, packet, addattrs)
|
182 |
|
|
|
183 |
|
|
self.debug("-> recv returns code '%d'", retval)
|
184 |
|
|
return retval
|