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

Subversion Repositories nocmodel

[/] [nocmodel/] [trunk/] [examples/] [basic_example.py] - Blame information for rev 2

Go to most recent revision | 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
# NoCmodel basic example
6
#
7
# Author:  Oscar Diaz
8
# Version: 0.1
9
# Date:    03-03-2011
10
 
11
#
12
# This code is free software; you can redistribute it and/or
13
# modify it under the terms of the GNU Lesser General Public
14
# License as published by the Free Software Foundation; either
15
# version 2.1 of the License, or (at your option) any later version.
16
#
17
# This code is distributed in the hope that it will be useful,
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
# Lesser General Public License for more details.
21
#
22
# You should have received a copy of the GNU Lesser General Public
23
# License along with this library; if not, write to the
24
# Free Software  Foundation, Inc., 59 Temple Place, Suite 330,
25
# Boston, MA  02111-1307  USA
26
#
27
 
28
#
29
# Changelog:
30
#
31
# 03-03-2011 : (OD) initial release
32
#
33
 
34
import matplotlib.pyplot as plt
35
import myhdl
36
import logging
37
 
38
from nocmodel import *
39
from nocmodel.basicmodels import *
40
 
41
# Basic example model with TLM simulation
42
 
43
# 1. Create the model
44
 
45
basicnoc = noc(name="Basic 2x2 NoC example")
46
 
47
# 1.1 create a rectangular 2x2 NoC, make its connections and add default protocol
48
 
49
R11 = basicnoc.add_router("R11", with_ipcore=True, coord_x = 1, coord_y = 1)
50
R12 = basicnoc.add_router("R12", with_ipcore=True, coord_x = 1, coord_y = 2)
51
R21 = basicnoc.add_router("R21", with_ipcore=True, coord_x = 2, coord_y = 1)
52
R22 = basicnoc.add_router("R22", with_ipcore=True, coord_x = 2, coord_y = 2)
53
 
54
basicnoc.add_channel(R11,R12)
55
basicnoc.add_channel(R11,R21)
56
basicnoc.add_channel(R12,R22)
57
basicnoc.add_channel(R21,R22)
58
 
59
basicnoc.protocol_ref = basic_protocol()
60
 
61
for r in basicnoc.router_list():
62
    r.update_ports_info()
63
    r.update_routes_info()
64
 
65
# 2. add tlm support, and configure logging
66
add_tlm_basic_support(basicnoc, log_file="simulation.log", log_level=logging.DEBUG)
67
 
68
# 3. Declare generators to put in the TLM simulation
69
 
70
# set ip_cores functionality as myhdl generators
71
def sourcegen(din, dout, tlm_ref, mydest, data=None, startdelay=100, period=100):
72
    # this generator only drives dout
73
    @myhdl.instance
74
    def putnewdata():
75
        datacount = 0
76
        protocol_ref = tlm_ref.ipcore_ref.get_protocol_ref()
77
        mysrc = tlm_ref.ipcore_ref.router_ref.address
78
        tlm_ref.debug("sourcegen: init dout is %s" % repr(dout.val))
79
        yield myhdl.delay(startdelay)
80
        while True:
81
            if len(data) == datacount:
82
                tlm_ref.debug("sourcegen: end of data. waiting for %d steps" % (period*10))
83
                yield myhdl.delay(period*10)
84
                raise myhdl.StopSimulation("data ended at time %d" % myhdl.now())
85
            dout.next = protocol_ref.newpacket(False, mysrc, mydest, data[datacount])
86
            tlm_ref.debug("sourcegen: data next element %d dout is %s datacount is %d" % (data[datacount], repr(dout.val), datacount))
87
            yield myhdl.delay(period)
88
            datacount += 1
89
    return putnewdata
90
 
91
def checkgen(din, dout, tlm_ref, mysrc, data=None):
92
    # this generator only respond to din
93
    @myhdl.instance
94
    def checkdata():
95
        datacount = 0
96
        protocol_ref = tlm_ref.ipcore_ref.get_protocol_ref()
97
        mydest = tlm_ref.ipcore_ref.router_ref.address
98
        while True:
99
            yield din
100
            if len(data) > datacount:
101
                checkdata = din.val["data"]
102
                tlm_ref.debug("checkgen: assert checkdata != data[datacount] => %d != %d [%d]" % (checkdata, data[datacount], datacount))
103
                if checkdata != data[datacount]:
104
                    tlm_ref.error("checkgen: value != %d (%d)" % (data[datacount], checkdata))
105
                tlm_ref.debug("checkgen: assert source address != mysrc => %d != %d " % (din.val["src"], mysrc))
106
                if din.val["src"] != mysrc:
107
                    tlm_ref.error("checkgen: source address != %d (%d)" % (mysrc, din.val["src"]))
108
                tlm_ref.debug("checkgen: assert destination address != mydest => %d != %d " % (din.val["dst"], mydest))
109
                if din.val["dst"] != mydest:
110
                    tlm_ref.error("checkgen: destination address != %d (%d)" % (mydest, din.val["dst"]))
111
                datacount += 1
112
    return checkdata
113
 
114
# 4. Set test vectors
115
R11_testdata = [5, 12, 50, -11, 6, 9, 0, 3, 25]
116
R12_testdata = [x*5 for x in R11_testdata]
117
 
118
# 5. assign generators to ip cores (in TLM model !)
119
# R11 will send to R22, R12 will send to R21
120
R11.ipcore_ref.tlm.register_generator(sourcegen, mydest=R22.address, data=R11_testdata, startdelay=10, period=20)
121
R12.ipcore_ref.tlm.register_generator(sourcegen, mydest=R21.address, data=R12_testdata, startdelay=15, period=25)
122
R21.ipcore_ref.tlm.register_generator(checkgen, mysrc=R12.address, data=R12_testdata)
123
R22.ipcore_ref.tlm.register_generator(checkgen, mysrc=R11.address, data=R11_testdata)
124
 
125
# 6. configure simulation and run!
126
basicnoc.tlmsim.configure_simulation(max_time=1000)
127
print "Starting simulation..."
128
basicnoc.tlmsim.run()
129
print "Simulation finished. Pick the results in log files."
130
 
131
# 7. View graphical representation
132
 
133
draw_noc(basicnoc)

powered by: WebSVN 2.1.0

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