OpenCores
URL https://opencores.org/ocsvn/nocmodel/nocmodel/trunk

Subversion Repositories nocmodel

[/] [nocmodel/] [trunk/] [nocmodel/] [basicmodels/] [basic_channel.py] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dargor
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
 
4
#
5
# Basic Channel model
6 4 dargor
#  * TBM model
7 2 dargor
#
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 4 dargor
Basic channel TBM model
37 2 dargor
"""
38
 
39 4 dargor
from nocmodel.noc_tbm_base import *
40 2 dargor
 
41
# ---------------------------
42 4 dargor
# Channel TBM model
43 2 dargor
 
44 4 dargor
class basic_channel_tbm(noc_tbm_base):
45 2 dargor
    """
46 4 dargor
    TBM model of a NoC channel. It models a simple FIFO channel with
47 2 dargor
    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 4 dargor
    def __init__(self, channel_ref, channel_delay=2):
57
        noc_tbm_base.__init__(self)
58 2 dargor
        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 4 dargor
            self.delay_fifo_max = 4 # error-catch parameter, avoid fifo excessive growing
81 2 dargor
            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 4 dargor
                        # time could be 0, when the packets arrive from both endpoints
92
                        # at the same time. In that case don't yield
93
                        if next_delay < 0:
94 2 dargor
                            self.debug("delay_generator CATCH next_delay is '%d'" % next_delay)
95 4 dargor
                        elif next_delay > 0:
96 2 dargor
                            yield myhdl.delay(next_delay)
97
                        self.debug("delay_generator sending delayed packet (by %d), timed_packet format %s" % (next_delay, repr(timed_packet)) )
98
                        # use send()
99
                        retval = self.send(*timed_packet[1:])
100
                        # what to do in error case? report and continue
101 4 dargor
                        if retval != noc_tbm_errcodes.no_error:
102 2 dargor
                            self.error("delay_generator send returns code '%d'?" % retval )
103
                    self.delay_event.next = False
104 4 dargor
                    yield self.delay_event.posedge
105 2 dargor
            self.generators.append(delay_generator)
106
 
107
        self.debugstate()
108
 
109
    # channel only relays transactions
110
    # Transaction - related methods
111
    def send(self, src, dest, packet, addattrs=None):
112
        """
113
        This method will be called by recv (no delay) or by delay_generator
114
        src always is self
115
        """
116
        # dest MUST be one of the channel endpoints
117
        self.debug("-> send( %s , %s , %s , %s )" % (repr(src), repr(dest), repr(packet), repr(addattrs)))
118
        if isinstance(dest, int):
119
            # assume router direction
120
            thedest = self.graph_ref.get_router_by_address(dest)
121
            if thedest == False:
122
                self.error("-> send: dest %s not found" % repr(dest) )
123 4 dargor
                return noc_tbm_errcodes.tbm_badcall_send
124 2 dargor
        elif isinstance(dest, (router, ipcore)):
125
            thedest = dest
126
        else:
127
            self.error("-> send: what is dest '%s'?" % repr(dest) )
128 4 dargor
            return noc_tbm_errcodes.tbm_badcall_send
129 2 dargor
 
130
        # check dest as one of the channel endpoints
131
        if thedest not in self.endpoints:
132
            self.error("-> send: object %s is NOT one of the channel endpoints [%s,%s]" % (repr(thedest), repr(self.endpoints[0]), repr(self.endpoints[1])) )
133 4 dargor
            return noc_tbm_errcodes.tbm_badcall_send
134
 
135
        # call trace functions
136
        traceargs = {"self": self, "src": src, "dest": dest, "packet": packet, "addattrs": addattrs}
137
        for f in self.tracesend:
138
            if callable(f):
139
                f(traceargs)
140 2 dargor
 
141
        # call recv on the dest object
142 4 dargor
        retval = thedest.tbm.recv(self.channel_ref, dest, packet, addattrs)
143 2 dargor
 
144
        # Something to do with the retval? Only report it.
145
        self.debug("-> send returns code '%s'" % repr(retval))
146
        return retval
147
 
148
    def recv(self, src, dest, packet, addattrs=None):
149
        """
150
        receive a packet from an object. src is the object source and
151
        it MUST be one of the objects in channel endpoints
152
        """
153
 
154
        self.debug("-> recv( %s , %s , %s , %s )" % (repr(src), repr(dest), repr(packet), repr(addattrs)))
155
        # src can be an address or a noc object.
156
        if isinstance(src, int):
157
            # assume router direction
158
            thesrc = self.graph_ref.get_router_by_address(src)
159
            if thesrc == False:
160
                self.error("-> recv: src %s not found" % repr(src) )
161 4 dargor
                return noc_tbm_errcodes.tbm_badcall_recv
162 2 dargor
        elif isinstance(src, (router, ipcore)):
163
            thesrc = src
164
        else:
165
            self.error("-> recv: what is src '%s'?" % repr(src) )
166 4 dargor
            return noc_tbm_errcodes.tbm_badcall_recv
167 2 dargor
 
168
        # check src as one of the channel endpoints
169
        if thesrc not in self.endpoints:
170
            self.error("-> recv: object %s is NOT one of the channel endpoints [%s,%s]" % (repr(thesrc), repr(self.endpoints[0]), repr(self.endpoints[1])) )
171 4 dargor
            return noc_tbm_errcodes.tbm_badcall_recv
172
 
173
        # call trace functions        
174
        for f in self.tracerecv:
175
            if callable(f):
176
                traceargs = {"self": self, "src": src, "dest": dest, "packet": packet, "addattrs": addattrs}
177
                f(traceargs)
178 2 dargor
 
179
        # calculate the other endpoint
180
        end_index = self.endpoints.index(thesrc) - 1
181
 
182
        if self.has_delay:
183
            # put in delay fifo: store time and call attributes
184
            self.delay_fifo.append([myhdl.now(), self.channel_ref, self.endpoints[end_index], packet, addattrs])
185
            self.debug("-> recv put in delay_fifo (delay %d)" % self.channel_delay)
186
            # catch growing fifo
187
            if len(self.delay_fifo) > self.delay_fifo_max:
188
                self.warning("-> recv: delay_fifo is getting bigger! current size is %d" % len(self.delay_fifo) )
189
            # trigger event
190
            self.delay_event.next = True
191 4 dargor
            retval = noc_tbm_errcodes.no_error
192 2 dargor
        else:
193
            # use send() call directly
194
            router_dest = self.endpoints[end_index]
195
            retval = self.send(self.channel_ref, router_dest, packet, addattrs)
196
 
197
        self.debug("-> recv returns code '%d'", retval)
198
        return retval

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.