StateMachine.py revision 10308
16657Snate@binkert.org# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
26657Snate@binkert.org# Copyright (c) 2009 The Hewlett-Packard Development Company
36657Snate@binkert.org# All rights reserved.
46657Snate@binkert.org#
56657Snate@binkert.org# Redistribution and use in source and binary forms, with or without
66657Snate@binkert.org# modification, are permitted provided that the following conditions are
76657Snate@binkert.org# met: redistributions of source code must retain the above copyright
86657Snate@binkert.org# notice, this list of conditions and the following disclaimer;
96657Snate@binkert.org# redistributions in binary form must reproduce the above copyright
106657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
116657Snate@binkert.org# documentation and/or other materials provided with the distribution;
126657Snate@binkert.org# neither the name of the copyright holders nor the names of its
136657Snate@binkert.org# contributors may be used to endorse or promote products derived from
146657Snate@binkert.org# this software without specific prior written permission.
156657Snate@binkert.org#
166657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176657Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226657Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246657Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256657Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266657Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276657Snate@binkert.org
286999Snate@binkert.orgfrom m5.util import orderdict
296657Snate@binkert.org
306657Snate@binkert.orgfrom slicc.symbols.Symbol import Symbol
316657Snate@binkert.orgfrom slicc.symbols.Var import Var
326657Snate@binkert.orgimport slicc.generate.html as html
338189SLisa.Hsu@amd.comimport re
346657Snate@binkert.org
359499Snilay@cs.wisc.edupython_class_map = {
369499Snilay@cs.wisc.edu                    "int": "Int",
379364Snilay@cs.wisc.edu                    "uint32_t" : "UInt32",
387055Snate@binkert.org                    "std::string": "String",
396882SBrad.Beckmann@amd.com                    "bool": "Bool",
406882SBrad.Beckmann@amd.com                    "CacheMemory": "RubyCache",
418191SLisa.Hsu@amd.com                    "WireBuffer": "RubyWireBuffer",
426882SBrad.Beckmann@amd.com                    "Sequencer": "RubySequencer",
436882SBrad.Beckmann@amd.com                    "DirectoryMemory": "RubyDirectoryMemory",
449102SNuwan.Jayasena@amd.com                    "MemoryControl": "MemoryControl",
459366Snilay@cs.wisc.edu                    "DMASequencer": "DMASequencer",
469499Snilay@cs.wisc.edu                    "Prefetcher":"Prefetcher",
479499Snilay@cs.wisc.edu                    "Cycles":"Cycles",
489499Snilay@cs.wisc.edu                   }
496882SBrad.Beckmann@amd.com
506657Snate@binkert.orgclass StateMachine(Symbol):
516657Snate@binkert.org    def __init__(self, symtab, ident, location, pairs, config_parameters):
526657Snate@binkert.org        super(StateMachine, self).__init__(symtab, ident, location, pairs)
536657Snate@binkert.org        self.table = None
546657Snate@binkert.org        self.config_parameters = config_parameters
559366Snilay@cs.wisc.edu        self.prefetchers = []
567839Snilay@cs.wisc.edu
576657Snate@binkert.org        for param in config_parameters:
586882SBrad.Beckmann@amd.com            if param.pointer:
5910308Snilay@cs.wisc.edu                var = Var(symtab, param.ident, location, param.type_ast.type,
6010308Snilay@cs.wisc.edu                          "(*m_%s_ptr)" % param.ident, {}, self)
616882SBrad.Beckmann@amd.com            else:
6210308Snilay@cs.wisc.edu                var = Var(symtab, param.ident, location, param.type_ast.type,
6310308Snilay@cs.wisc.edu                          "m_%s" % param.ident, {}, self)
6410308Snilay@cs.wisc.edu
6510308Snilay@cs.wisc.edu            self.symtab.registerSym(param.ident, var)
6610308Snilay@cs.wisc.edu
679366Snilay@cs.wisc.edu            if str(param.type_ast.type) == "Prefetcher":
689366Snilay@cs.wisc.edu                self.prefetchers.append(var)
696657Snate@binkert.org
706657Snate@binkert.org        self.states = orderdict()
716657Snate@binkert.org        self.events = orderdict()
726657Snate@binkert.org        self.actions = orderdict()
739104Shestness@cs.utexas.edu        self.request_types = orderdict()
746657Snate@binkert.org        self.transitions = []
756657Snate@binkert.org        self.in_ports = []
766657Snate@binkert.org        self.functions = []
776657Snate@binkert.org        self.objects = []
787839Snilay@cs.wisc.edu        self.TBEType   = None
797839Snilay@cs.wisc.edu        self.EntryType = None
806657Snate@binkert.org
816657Snate@binkert.org    def __repr__(self):
826657Snate@binkert.org        return "[StateMachine: %s]" % self.ident
836657Snate@binkert.org
846657Snate@binkert.org    def addState(self, state):
856657Snate@binkert.org        assert self.table is None
866657Snate@binkert.org        self.states[state.ident] = state
876657Snate@binkert.org
886657Snate@binkert.org    def addEvent(self, event):
896657Snate@binkert.org        assert self.table is None
906657Snate@binkert.org        self.events[event.ident] = event
916657Snate@binkert.org
926657Snate@binkert.org    def addAction(self, action):
936657Snate@binkert.org        assert self.table is None
946657Snate@binkert.org
956657Snate@binkert.org        # Check for duplicate action
966657Snate@binkert.org        for other in self.actions.itervalues():
976657Snate@binkert.org            if action.ident == other.ident:
986779SBrad.Beckmann@amd.com                action.warning("Duplicate action definition: %s" % action.ident)
996657Snate@binkert.org                action.error("Duplicate action definition: %s" % action.ident)
1006657Snate@binkert.org            if action.short == other.short:
1016657Snate@binkert.org                other.warning("Duplicate action shorthand: %s" % other.ident)
1026657Snate@binkert.org                other.warning("    shorthand = %s" % other.short)
1036657Snate@binkert.org                action.warning("Duplicate action shorthand: %s" % action.ident)
1046657Snate@binkert.org                action.error("    shorthand = %s" % action.short)
1056657Snate@binkert.org
1066657Snate@binkert.org        self.actions[action.ident] = action
1076657Snate@binkert.org
1089104Shestness@cs.utexas.edu    def addRequestType(self, request_type):
1099104Shestness@cs.utexas.edu        assert self.table is None
1109104Shestness@cs.utexas.edu        self.request_types[request_type.ident] = request_type
1119104Shestness@cs.utexas.edu
1126657Snate@binkert.org    def addTransition(self, trans):
1136657Snate@binkert.org        assert self.table is None
1146657Snate@binkert.org        self.transitions.append(trans)
1156657Snate@binkert.org
1166657Snate@binkert.org    def addInPort(self, var):
1176657Snate@binkert.org        self.in_ports.append(var)
1186657Snate@binkert.org
1196657Snate@binkert.org    def addFunc(self, func):
1206657Snate@binkert.org        # register func in the symbol table
1216657Snate@binkert.org        self.symtab.registerSym(str(func), func)
1226657Snate@binkert.org        self.functions.append(func)
1236657Snate@binkert.org
1246657Snate@binkert.org    def addObject(self, obj):
12510307Snilay@cs.wisc.edu        self.symtab.registerSym(str(obj), obj)
1266657Snate@binkert.org        self.objects.append(obj)
1276657Snate@binkert.org
1287839Snilay@cs.wisc.edu    def addType(self, type):
1297839Snilay@cs.wisc.edu        type_ident = '%s' % type.c_ident
1307839Snilay@cs.wisc.edu
1317839Snilay@cs.wisc.edu        if type_ident == "%s_TBE" %self.ident:
1327839Snilay@cs.wisc.edu            if self.TBEType != None:
1337839Snilay@cs.wisc.edu                self.error("Multiple Transaction Buffer types in a " \
1347839Snilay@cs.wisc.edu                           "single machine.");
1357839Snilay@cs.wisc.edu            self.TBEType = type
1367839Snilay@cs.wisc.edu
1377839Snilay@cs.wisc.edu        elif "interface" in type and "AbstractCacheEntry" == type["interface"]:
1387839Snilay@cs.wisc.edu            if self.EntryType != None:
1397839Snilay@cs.wisc.edu                self.error("Multiple AbstractCacheEntry types in a " \
1407839Snilay@cs.wisc.edu                           "single machine.");
1417839Snilay@cs.wisc.edu            self.EntryType = type
1427839Snilay@cs.wisc.edu
1436657Snate@binkert.org    # Needs to be called before accessing the table
1446657Snate@binkert.org    def buildTable(self):
1456657Snate@binkert.org        assert self.table is None
1466657Snate@binkert.org
1476657Snate@binkert.org        table = {}
1486657Snate@binkert.org
1496657Snate@binkert.org        for trans in self.transitions:
1506657Snate@binkert.org            # Track which actions we touch so we know if we use them
1516657Snate@binkert.org            # all -- really this should be done for all symbols as
1526657Snate@binkert.org            # part of the symbol table, then only trigger it for
1536657Snate@binkert.org            # Actions, States, Events, etc.
1546657Snate@binkert.org
1556657Snate@binkert.org            for action in trans.actions:
1566657Snate@binkert.org                action.used = True
1576657Snate@binkert.org
1586657Snate@binkert.org            index = (trans.state, trans.event)
1596657Snate@binkert.org            if index in table:
1606657Snate@binkert.org                table[index].warning("Duplicate transition: %s" % table[index])
1616657Snate@binkert.org                trans.error("Duplicate transition: %s" % trans)
1626657Snate@binkert.org            table[index] = trans
1636657Snate@binkert.org
1646657Snate@binkert.org        # Look at all actions to make sure we used them all
1656657Snate@binkert.org        for action in self.actions.itervalues():
1666657Snate@binkert.org            if not action.used:
1676657Snate@binkert.org                error_msg = "Unused action: %s" % action.ident
1686657Snate@binkert.org                if "desc" in action:
1696657Snate@binkert.org                    error_msg += ", "  + action.desc
1706657Snate@binkert.org                action.warning(error_msg)
1716657Snate@binkert.org        self.table = table
1726657Snate@binkert.org
1739219Spower.jg@gmail.com    def writeCodeFiles(self, path, includes):
1746877Ssteve.reinhardt@amd.com        self.printControllerPython(path)
1756657Snate@binkert.org        self.printControllerHH(path)
1769219Spower.jg@gmail.com        self.printControllerCC(path, includes)
1776657Snate@binkert.org        self.printCSwitch(path)
1789219Spower.jg@gmail.com        self.printCWakeup(path, includes)
1796657Snate@binkert.org
1806877Ssteve.reinhardt@amd.com    def printControllerPython(self, path):
1816999Snate@binkert.org        code = self.symtab.codeFormatter()
1826877Ssteve.reinhardt@amd.com        ident = self.ident
18310308Snilay@cs.wisc.edu
1846877Ssteve.reinhardt@amd.com        py_ident = "%s_Controller" % ident
1856877Ssteve.reinhardt@amd.com        c_ident = "%s_Controller" % self.ident
18610308Snilay@cs.wisc.edu
1876877Ssteve.reinhardt@amd.com        code('''
1886877Ssteve.reinhardt@amd.comfrom m5.params import *
1896877Ssteve.reinhardt@amd.comfrom m5.SimObject import SimObject
1906877Ssteve.reinhardt@amd.comfrom Controller import RubyController
1916877Ssteve.reinhardt@amd.com
1926877Ssteve.reinhardt@amd.comclass $py_ident(RubyController):
1936877Ssteve.reinhardt@amd.com    type = '$py_ident'
1949338SAndreas.Sandberg@arm.com    cxx_header = 'mem/protocol/${c_ident}.hh'
1956877Ssteve.reinhardt@amd.com''')
1966877Ssteve.reinhardt@amd.com        code.indent()
1976877Ssteve.reinhardt@amd.com        for param in self.config_parameters:
1986877Ssteve.reinhardt@amd.com            dflt_str = ''
19910308Snilay@cs.wisc.edu
20010308Snilay@cs.wisc.edu            if param.rvalue is not None:
20110308Snilay@cs.wisc.edu                dflt_str = str(param.rvalue.inline()) + ', '
20210308Snilay@cs.wisc.edu
2036882SBrad.Beckmann@amd.com            if python_class_map.has_key(param.type_ast.type.c_ident):
2046882SBrad.Beckmann@amd.com                python_type = python_class_map[param.type_ast.type.c_ident]
20510308Snilay@cs.wisc.edu                code('${{param.ident}} = Param.${{python_type}}(${dflt_str}"")')
20610308Snilay@cs.wisc.edu
2076882SBrad.Beckmann@amd.com            else:
2086882SBrad.Beckmann@amd.com                self.error("Unknown c++ to python class conversion for c++ " \
2096882SBrad.Beckmann@amd.com                           "type: '%s'. Please update the python_class_map " \
2106882SBrad.Beckmann@amd.com                           "in StateMachine.py", param.type_ast.type.c_ident)
2116877Ssteve.reinhardt@amd.com        code.dedent()
2126877Ssteve.reinhardt@amd.com        code.write(path, '%s.py' % py_ident)
2136877Ssteve.reinhardt@amd.com
2146877Ssteve.reinhardt@amd.com
2156657Snate@binkert.org    def printControllerHH(self, path):
2166657Snate@binkert.org        '''Output the method declarations for the class declaration'''
2176999Snate@binkert.org        code = self.symtab.codeFormatter()
2186657Snate@binkert.org        ident = self.ident
2196657Snate@binkert.org        c_ident = "%s_Controller" % self.ident
2206657Snate@binkert.org
2216657Snate@binkert.org        code('''
2227007Snate@binkert.org/** \\file $c_ident.hh
2236657Snate@binkert.org *
2246657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
2256657Snate@binkert.org * Created by slicc definition of Module "${{self.short}}"
2266657Snate@binkert.org */
2276657Snate@binkert.org
2287007Snate@binkert.org#ifndef __${ident}_CONTROLLER_HH__
2297007Snate@binkert.org#define __${ident}_CONTROLLER_HH__
2306657Snate@binkert.org
2317002Snate@binkert.org#include <iostream>
2327002Snate@binkert.org#include <sstream>
2337002Snate@binkert.org#include <string>
2347002Snate@binkert.org
2356657Snate@binkert.org#include "mem/protocol/TransitionResult.hh"
2366657Snate@binkert.org#include "mem/protocol/Types.hh"
2378229Snate@binkert.org#include "mem/ruby/common/Consumer.hh"
2388229Snate@binkert.org#include "mem/ruby/common/Global.hh"
2398229Snate@binkert.org#include "mem/ruby/slicc_interface/AbstractController.hh"
2408229Snate@binkert.org#include "params/$c_ident.hh"
2416657Snate@binkert.org''')
2426657Snate@binkert.org
2436657Snate@binkert.org        seen_types = set()
2449595Snilay@cs.wisc.edu        has_peer = False
2456657Snate@binkert.org        for var in self.objects:
2466793SBrad.Beckmann@amd.com            if var.type.ident not in seen_types and not var.type.isPrimitive:
2476657Snate@binkert.org                code('#include "mem/protocol/${{var.type.c_ident}}.hh"')
2489595Snilay@cs.wisc.edu            if "network" in var and "physical_network" in var:
2499595Snilay@cs.wisc.edu                has_peer = True
2506657Snate@binkert.org            seen_types.add(var.type.ident)
2516657Snate@binkert.org
2526657Snate@binkert.org        # for adding information to the protocol debug trace
2536657Snate@binkert.org        code('''
2547002Snate@binkert.orgextern std::stringstream ${ident}_transitionComment;
2556657Snate@binkert.org
2567007Snate@binkert.orgclass $c_ident : public AbstractController
2577007Snate@binkert.org{
2589271Snilay@cs.wisc.edu  public:
2596877Ssteve.reinhardt@amd.com    typedef ${c_ident}Params Params;
2606877Ssteve.reinhardt@amd.com    $c_ident(const Params *p);
2616657Snate@binkert.org    static int getNumControllers();
2626877Ssteve.reinhardt@amd.com    void init();
2636657Snate@binkert.org    MessageBuffer* getMandatoryQueue() const;
2649745Snilay@cs.wisc.edu
2657002Snate@binkert.org    void print(std::ostream& out) const;
2666657Snate@binkert.org    void wakeup();
26710012Snilay@cs.wisc.edu    void resetStats();
2689745Snilay@cs.wisc.edu    void regStats();
2699745Snilay@cs.wisc.edu    void collateStats();
2709745Snilay@cs.wisc.edu
2718683Snilay@cs.wisc.edu    void recordCacheTrace(int cntrl, CacheRecorder* tr);
2728683Snilay@cs.wisc.edu    Sequencer* getSequencer() const;
2737007Snate@binkert.org
2749302Snilay@cs.wisc.edu    bool functionalReadBuffers(PacketPtr&);
2759302Snilay@cs.wisc.edu    uint32_t functionalWriteBuffers(PacketPtr&);
2769302Snilay@cs.wisc.edu
2779745Snilay@cs.wisc.edu    void countTransition(${ident}_State state, ${ident}_Event event);
2789745Snilay@cs.wisc.edu    void possibleTransition(${ident}_State state, ${ident}_Event event);
2799745Snilay@cs.wisc.edu    uint64 getEventCount(${ident}_Event event);
2809745Snilay@cs.wisc.edu    bool isPossible(${ident}_State state, ${ident}_Event event);
2819745Snilay@cs.wisc.edu    uint64 getTransitionCount(${ident}_State state, ${ident}_Event event);
2829745Snilay@cs.wisc.edu
2836657Snate@binkert.orgprivate:
2846657Snate@binkert.org''')
2856657Snate@binkert.org
2866657Snate@binkert.org        code.indent()
2876657Snate@binkert.org        # added by SS
2886657Snate@binkert.org        for param in self.config_parameters:
2896882SBrad.Beckmann@amd.com            if param.pointer:
2906882SBrad.Beckmann@amd.com                code('${{param.type_ast.type}}* m_${{param.ident}}_ptr;')
2916882SBrad.Beckmann@amd.com            else:
2926882SBrad.Beckmann@amd.com                code('${{param.type_ast.type}} m_${{param.ident}};')
2936657Snate@binkert.org
2946657Snate@binkert.org        code('''
2957007Snate@binkert.orgTransitionResult doTransition(${ident}_Event event,
2967839Snilay@cs.wisc.edu''')
2977839Snilay@cs.wisc.edu
2987839Snilay@cs.wisc.edu        if self.EntryType != None:
2997839Snilay@cs.wisc.edu            code('''
3007839Snilay@cs.wisc.edu                              ${{self.EntryType.c_ident}}* m_cache_entry_ptr,
3017839Snilay@cs.wisc.edu''')
3027839Snilay@cs.wisc.edu        if self.TBEType != None:
3037839Snilay@cs.wisc.edu            code('''
3047839Snilay@cs.wisc.edu                              ${{self.TBEType.c_ident}}* m_tbe_ptr,
3057839Snilay@cs.wisc.edu''')
3067839Snilay@cs.wisc.edu
3077839Snilay@cs.wisc.edu        code('''
30810010Snilay@cs.wisc.edu                              const Address addr);
3097007Snate@binkert.org
3107007Snate@binkert.orgTransitionResult doTransitionWorker(${ident}_Event event,
3117007Snate@binkert.org                                    ${ident}_State state,
3127007Snate@binkert.org                                    ${ident}_State& next_state,
3137839Snilay@cs.wisc.edu''')
3147839Snilay@cs.wisc.edu
3157839Snilay@cs.wisc.edu        if self.TBEType != None:
3167839Snilay@cs.wisc.edu            code('''
3177839Snilay@cs.wisc.edu                                    ${{self.TBEType.c_ident}}*& m_tbe_ptr,
3187839Snilay@cs.wisc.edu''')
3197839Snilay@cs.wisc.edu        if self.EntryType != None:
3207839Snilay@cs.wisc.edu            code('''
3217839Snilay@cs.wisc.edu                                    ${{self.EntryType.c_ident}}*& m_cache_entry_ptr,
3227839Snilay@cs.wisc.edu''')
3237839Snilay@cs.wisc.edu
3247839Snilay@cs.wisc.edu        code('''
3257007Snate@binkert.org                                    const Address& addr);
3267007Snate@binkert.org
3279745Snilay@cs.wisc.eduint m_counters[${ident}_State_NUM][${ident}_Event_NUM];
3289745Snilay@cs.wisc.eduint m_event_counters[${ident}_Event_NUM];
3299745Snilay@cs.wisc.edubool m_possible[${ident}_State_NUM][${ident}_Event_NUM];
3309745Snilay@cs.wisc.edu
3319745Snilay@cs.wisc.edustatic std::vector<Stats::Vector *> eventVec;
3329745Snilay@cs.wisc.edustatic std::vector<std::vector<Stats::Vector *> > transVec;
3336657Snate@binkert.orgstatic int m_num_controllers;
3347007Snate@binkert.org
3356657Snate@binkert.org// Internal functions
3366657Snate@binkert.org''')
3376657Snate@binkert.org
3386657Snate@binkert.org        for func in self.functions:
3396657Snate@binkert.org            proto = func.prototype
3406657Snate@binkert.org            if proto:
3416657Snate@binkert.org                code('$proto')
3426657Snate@binkert.org
3439595Snilay@cs.wisc.edu        if has_peer:
3449595Snilay@cs.wisc.edu            code('void getQueuesFromPeer(AbstractController *);')
3457839Snilay@cs.wisc.edu        if self.EntryType != None:
3467839Snilay@cs.wisc.edu            code('''
3477839Snilay@cs.wisc.edu
3487839Snilay@cs.wisc.edu// Set and Reset for cache_entry variable
3497839Snilay@cs.wisc.eduvoid set_cache_entry(${{self.EntryType.c_ident}}*& m_cache_entry_ptr, AbstractCacheEntry* m_new_cache_entry);
3507839Snilay@cs.wisc.eduvoid unset_cache_entry(${{self.EntryType.c_ident}}*& m_cache_entry_ptr);
3517839Snilay@cs.wisc.edu''')
3527839Snilay@cs.wisc.edu
3537839Snilay@cs.wisc.edu        if self.TBEType != None:
3547839Snilay@cs.wisc.edu            code('''
3557839Snilay@cs.wisc.edu
3567839Snilay@cs.wisc.edu// Set and Reset for tbe variable
3577839Snilay@cs.wisc.eduvoid set_tbe(${{self.TBEType.c_ident}}*& m_tbe_ptr, ${ident}_TBE* m_new_tbe);
3587839Snilay@cs.wisc.eduvoid unset_tbe(${{self.TBEType.c_ident}}*& m_tbe_ptr);
3597839Snilay@cs.wisc.edu''')
3607839Snilay@cs.wisc.edu
36110121Snilay@cs.wisc.edu        # Prototype the actions that the controller can take
3626657Snate@binkert.org        code('''
3636657Snate@binkert.org
3646657Snate@binkert.org// Actions
3656657Snate@binkert.org''')
3667839Snilay@cs.wisc.edu        if self.TBEType != None and self.EntryType != None:
3677839Snilay@cs.wisc.edu            for action in self.actions.itervalues():
3687839Snilay@cs.wisc.edu                code('/** \\brief ${{action.desc}} */')
36910121Snilay@cs.wisc.edu                code('void ${{action.ident}}(${{self.TBEType.c_ident}}*& '
37010121Snilay@cs.wisc.edu                     'm_tbe_ptr, ${{self.EntryType.c_ident}}*& '
37110121Snilay@cs.wisc.edu                     'm_cache_entry_ptr, const Address& addr);')
3727839Snilay@cs.wisc.edu        elif self.TBEType != None:
3737839Snilay@cs.wisc.edu            for action in self.actions.itervalues():
3747839Snilay@cs.wisc.edu                code('/** \\brief ${{action.desc}} */')
37510121Snilay@cs.wisc.edu                code('void ${{action.ident}}(${{self.TBEType.c_ident}}*& '
37610121Snilay@cs.wisc.edu                     'm_tbe_ptr, const Address& addr);')
3777839Snilay@cs.wisc.edu        elif self.EntryType != None:
3787839Snilay@cs.wisc.edu            for action in self.actions.itervalues():
3797839Snilay@cs.wisc.edu                code('/** \\brief ${{action.desc}} */')
38010121Snilay@cs.wisc.edu                code('void ${{action.ident}}(${{self.EntryType.c_ident}}*& '
38110121Snilay@cs.wisc.edu                     'm_cache_entry_ptr, const Address& addr);')
3827839Snilay@cs.wisc.edu        else:
3837839Snilay@cs.wisc.edu            for action in self.actions.itervalues():
3847839Snilay@cs.wisc.edu                code('/** \\brief ${{action.desc}} */')
3857839Snilay@cs.wisc.edu                code('void ${{action.ident}}(const Address& addr);')
3866657Snate@binkert.org
3876657Snate@binkert.org        # the controller internal variables
3886657Snate@binkert.org        code('''
3896657Snate@binkert.org
3907007Snate@binkert.org// Objects
3916657Snate@binkert.org''')
3926657Snate@binkert.org        for var in self.objects:
3939273Snilay@cs.wisc.edu            th = var.get("template", "")
39410305Snilay@cs.wisc.edu            code('${{var.type.c_ident}}$th* m_${{var.ident}}_ptr;')
3956657Snate@binkert.org
3966657Snate@binkert.org        code.dedent()
3976657Snate@binkert.org        code('};')
3987007Snate@binkert.org        code('#endif // __${ident}_CONTROLLER_H__')
3996657Snate@binkert.org        code.write(path, '%s.hh' % c_ident)
4006657Snate@binkert.org
4019219Spower.jg@gmail.com    def printControllerCC(self, path, includes):
4026657Snate@binkert.org        '''Output the actions for performing the actions'''
4036657Snate@binkert.org
4046999Snate@binkert.org        code = self.symtab.codeFormatter()
4056657Snate@binkert.org        ident = self.ident
4066657Snate@binkert.org        c_ident = "%s_Controller" % self.ident
4079595Snilay@cs.wisc.edu        has_peer = False
4086657Snate@binkert.org
4096657Snate@binkert.org        code('''
4107007Snate@binkert.org/** \\file $c_ident.cc
4116657Snate@binkert.org *
4126657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
4136657Snate@binkert.org * Created by slicc definition of Module "${{self.short}}"
4146657Snate@binkert.org */
4156657Snate@binkert.org
4168946Sandreas.hansson@arm.com#include <sys/types.h>
4178946Sandreas.hansson@arm.com#include <unistd.h>
4188946Sandreas.hansson@arm.com
4197832Snate@binkert.org#include <cassert>
4207002Snate@binkert.org#include <sstream>
4217002Snate@binkert.org#include <string>
4227002Snate@binkert.org
4238641Snate@binkert.org#include "base/compiler.hh"
4247056Snate@binkert.org#include "base/cprintf.hh"
4258232Snate@binkert.org#include "debug/RubyGenerated.hh"
4268232Snate@binkert.org#include "debug/RubySlicc.hh"
4276657Snate@binkert.org#include "mem/protocol/${ident}_Controller.hh"
4288229Snate@binkert.org#include "mem/protocol/${ident}_Event.hh"
4296657Snate@binkert.org#include "mem/protocol/${ident}_State.hh"
4306657Snate@binkert.org#include "mem/protocol/Types.hh"
4317056Snate@binkert.org#include "mem/ruby/common/Global.hh"
4326657Snate@binkert.org#include "mem/ruby/system/System.hh"
4339219Spower.jg@gmail.com''')
4349219Spower.jg@gmail.com        for include_path in includes:
4359219Spower.jg@gmail.com            code('#include "${{include_path}}"')
4369219Spower.jg@gmail.com
4379219Spower.jg@gmail.com        code('''
4387002Snate@binkert.org
4397002Snate@binkert.orgusing namespace std;
4406657Snate@binkert.org''')
4416657Snate@binkert.org
4426657Snate@binkert.org        # include object classes
4436657Snate@binkert.org        seen_types = set()
4446657Snate@binkert.org        for var in self.objects:
4456793SBrad.Beckmann@amd.com            if var.type.ident not in seen_types and not var.type.isPrimitive:
4466657Snate@binkert.org                code('#include "mem/protocol/${{var.type.c_ident}}.hh"')
4476657Snate@binkert.org            seen_types.add(var.type.ident)
4486657Snate@binkert.org
44910121Snilay@cs.wisc.edu        num_in_ports = len(self.in_ports)
45010121Snilay@cs.wisc.edu
4516657Snate@binkert.org        code('''
4526877Ssteve.reinhardt@amd.com$c_ident *
4536877Ssteve.reinhardt@amd.com${c_ident}Params::create()
4546877Ssteve.reinhardt@amd.com{
4556877Ssteve.reinhardt@amd.com    return new $c_ident(this);
4566877Ssteve.reinhardt@amd.com}
4576877Ssteve.reinhardt@amd.com
4586657Snate@binkert.orgint $c_ident::m_num_controllers = 0;
4599745Snilay@cs.wisc.edustd::vector<Stats::Vector *>  $c_ident::eventVec;
4609745Snilay@cs.wisc.edustd::vector<std::vector<Stats::Vector *> >  $c_ident::transVec;
4616657Snate@binkert.org
4627007Snate@binkert.org// for adding information to the protocol debug trace
4636657Snate@binkert.orgstringstream ${ident}_transitionComment;
4649801Snilay@cs.wisc.edu
4659801Snilay@cs.wisc.edu#ifndef NDEBUG
4666657Snate@binkert.org#define APPEND_TRANSITION_COMMENT(str) (${ident}_transitionComment << str)
4679801Snilay@cs.wisc.edu#else
4689801Snilay@cs.wisc.edu#define APPEND_TRANSITION_COMMENT(str) do {} while (0)
4699801Snilay@cs.wisc.edu#endif
4707007Snate@binkert.org
4716657Snate@binkert.org/** \\brief constructor */
4726877Ssteve.reinhardt@amd.com$c_ident::$c_ident(const Params *p)
4736877Ssteve.reinhardt@amd.com    : AbstractController(p)
4746657Snate@binkert.org{
47510078Snilay@cs.wisc.edu    m_machineID.type = MachineType_${ident};
47610078Snilay@cs.wisc.edu    m_machineID.num = m_version;
47710121Snilay@cs.wisc.edu    m_num_controllers++;
47810121Snilay@cs.wisc.edu
47910121Snilay@cs.wisc.edu    m_in_ports = $num_in_ports;
4806657Snate@binkert.org''')
4816657Snate@binkert.org        code.indent()
4826882SBrad.Beckmann@amd.com
4836882SBrad.Beckmann@amd.com        #
4846882SBrad.Beckmann@amd.com        # After initializing the universal machine parameters, initialize the
48510121Snilay@cs.wisc.edu        # this machines config parameters.  Also if these configuration params
48610121Snilay@cs.wisc.edu        # include a sequencer, connect the it to the controller.
4876882SBrad.Beckmann@amd.com        #
4886877Ssteve.reinhardt@amd.com        for param in self.config_parameters:
4896882SBrad.Beckmann@amd.com            if param.pointer:
49010308Snilay@cs.wisc.edu                code('m_${{param.ident}}_ptr = p->${{param.ident}};')
4916882SBrad.Beckmann@amd.com            else:
49210308Snilay@cs.wisc.edu                code('m_${{param.ident}} = p->${{param.ident}};')
49310308Snilay@cs.wisc.edu            if re.compile("sequencer").search(param.ident):
49410308Snilay@cs.wisc.edu                code('m_${{param.ident}}_ptr->setController(this);')
4956888SBrad.Beckmann@amd.com
4966657Snate@binkert.org        for var in self.objects:
4976657Snate@binkert.org            if var.ident.find("mandatoryQueue") >= 0:
4989508Snilay@cs.wisc.edu                code('''
49910305Snilay@cs.wisc.edum_${{var.ident}}_ptr = new ${{var.type.c_ident}}();
50010305Snilay@cs.wisc.edum_${{var.ident}}_ptr->setReceiver(this);
5019508Snilay@cs.wisc.edu''')
5029595Snilay@cs.wisc.edu            else:
5039595Snilay@cs.wisc.edu                if "network" in var and "physical_network" in var and \
5049595Snilay@cs.wisc.edu                   var["network"] == "To":
5059595Snilay@cs.wisc.edu                    has_peer = True
5069595Snilay@cs.wisc.edu                    code('''
50710305Snilay@cs.wisc.edum_${{var.ident}}_ptr = new ${{var.type.c_ident}}();
50810305Snilay@cs.wisc.edupeerQueueMap[${{var["physical_network"]}}] = m_${{var.ident}}_ptr;
50910305Snilay@cs.wisc.edum_${{var.ident}}_ptr->setSender(this);
5109595Snilay@cs.wisc.edu''')
5116657Snate@binkert.org
5129595Snilay@cs.wisc.edu        code('''
5139595Snilay@cs.wisc.eduif (p->peer != NULL)
5149595Snilay@cs.wisc.edu    connectWithPeer(p->peer);
5159745Snilay@cs.wisc.edu
5169745Snilay@cs.wisc.edufor (int state = 0; state < ${ident}_State_NUM; state++) {
5179745Snilay@cs.wisc.edu    for (int event = 0; event < ${ident}_Event_NUM; event++) {
5189745Snilay@cs.wisc.edu        m_possible[state][event] = false;
5199745Snilay@cs.wisc.edu        m_counters[state][event] = 0;
5209745Snilay@cs.wisc.edu    }
5219745Snilay@cs.wisc.edu}
5229745Snilay@cs.wisc.edufor (int event = 0; event < ${ident}_Event_NUM; event++) {
5239745Snilay@cs.wisc.edu    m_event_counters[event] = 0;
5249745Snilay@cs.wisc.edu}
5259595Snilay@cs.wisc.edu''')
5266657Snate@binkert.org        code.dedent()
5276657Snate@binkert.org        code('''
5286657Snate@binkert.org}
5296657Snate@binkert.org
5307007Snate@binkert.orgvoid
5317007Snate@binkert.org$c_ident::init()
5326657Snate@binkert.org{
5339745Snilay@cs.wisc.edu    MachineType machine_type = string_to_MachineType("${{var.machine.ident}}");
53410008Snilay@cs.wisc.edu    int base M5_VAR_USED = MachineType_base_number(machine_type);
5357007Snate@binkert.org
5367007Snate@binkert.org    // initialize objects
5377007Snate@binkert.org
5386657Snate@binkert.org''')
5396657Snate@binkert.org
5406657Snate@binkert.org        code.indent()
5416657Snate@binkert.org        for var in self.objects:
5426657Snate@binkert.org            vtype = var.type
54310305Snilay@cs.wisc.edu            vid = "m_%s_ptr" % var.ident
5446657Snate@binkert.org            if "network" not in var:
5456657Snate@binkert.org                # Not a network port object
5466657Snate@binkert.org                if "primitive" in vtype:
5476657Snate@binkert.org                    code('$vid = new ${{vtype.c_ident}};')
5486657Snate@binkert.org                    if "default" in var:
5496657Snate@binkert.org                        code('(*$vid) = ${{var["default"]}};')
5506657Snate@binkert.org                else:
5516657Snate@binkert.org                    # Normal Object
5529595Snilay@cs.wisc.edu                    if var.ident.find("mandatoryQueue") < 0:
5539273Snilay@cs.wisc.edu                        th = var.get("template", "")
5546657Snate@binkert.org                        expr = "%s  = new %s%s" % (vid, vtype.c_ident, th)
5556657Snate@binkert.org                        args = ""
5566657Snate@binkert.org                        if "non_obj" not in vtype and not vtype.isEnumeration:
5579364Snilay@cs.wisc.edu                            args = var.get("constructor", "")
5587007Snate@binkert.org                        code('$expr($args);')
5596657Snate@binkert.org
5606657Snate@binkert.org                    code('assert($vid != NULL);')
5616657Snate@binkert.org
5626657Snate@binkert.org                    if "default" in var:
5637007Snate@binkert.org                        code('*$vid = ${{var["default"]}}; // Object default')
5646657Snate@binkert.org                    elif "default" in vtype:
5657007Snate@binkert.org                        comment = "Type %s default" % vtype.ident
5667007Snate@binkert.org                        code('*$vid = ${{vtype["default"]}}; // $comment')
5676657Snate@binkert.org
5686657Snate@binkert.org                    # Set ordering
5699508Snilay@cs.wisc.edu                    if "ordered" in var:
5706657Snate@binkert.org                        # A buffer
5716657Snate@binkert.org                        code('$vid->setOrdering(${{var["ordered"]}});')
5726657Snate@binkert.org
5736657Snate@binkert.org                    # Set randomization
5746657Snate@binkert.org                    if "random" in var:
5756657Snate@binkert.org                        # A buffer
5766657Snate@binkert.org                        code('$vid->setRandomization(${{var["random"]}});')
5776657Snate@binkert.org
5786657Snate@binkert.org                    # Set Priority
5799508Snilay@cs.wisc.edu                    if vtype.isBuffer and "rank" in var:
5806657Snate@binkert.org                        code('$vid->setPriority(${{var["rank"]}});')
5817566SBrad.Beckmann@amd.com
5829508Snilay@cs.wisc.edu                    # Set sender and receiver for trigger queue
5839508Snilay@cs.wisc.edu                    if var.ident.find("triggerQueue") >= 0:
5849508Snilay@cs.wisc.edu                        code('$vid->setSender(this);')
5859508Snilay@cs.wisc.edu                        code('$vid->setReceiver(this);')
5869508Snilay@cs.wisc.edu                    elif vtype.c_ident == "TimerTable":
5879508Snilay@cs.wisc.edu                        code('$vid->setClockObj(this);')
5889604Snilay@cs.wisc.edu                    elif var.ident.find("optionalQueue") >= 0:
5899604Snilay@cs.wisc.edu                        code('$vid->setSender(this);')
5909604Snilay@cs.wisc.edu                        code('$vid->setReceiver(this);')
5919508Snilay@cs.wisc.edu
5926657Snate@binkert.org            else:
5936657Snate@binkert.org                # Network port object
5946657Snate@binkert.org                network = var["network"]
5956657Snate@binkert.org                ordered =  var["ordered"]
5966657Snate@binkert.org
5979595Snilay@cs.wisc.edu                if "virtual_network" in var:
5989595Snilay@cs.wisc.edu                    vnet = var["virtual_network"]
5999595Snilay@cs.wisc.edu                    vnet_type = var["vnet_type"]
6009595Snilay@cs.wisc.edu
6019595Snilay@cs.wisc.edu                    assert var.machine is not None
6029595Snilay@cs.wisc.edu                    code('''
6038308Stushar@csail.mit.edu$vid = m_net_ptr->get${network}NetQueue(m_version + base, $ordered, $vnet, "$vnet_type");
6049595Snilay@cs.wisc.eduassert($vid != NULL);
6056657Snate@binkert.org''')
6066657Snate@binkert.org
6079595Snilay@cs.wisc.edu                    # Set the end
6089595Snilay@cs.wisc.edu                    if network == "To":
6099595Snilay@cs.wisc.edu                        code('$vid->setSender(this);')
6109595Snilay@cs.wisc.edu                    else:
6119595Snilay@cs.wisc.edu                        code('$vid->setReceiver(this);')
6129508Snilay@cs.wisc.edu
6136657Snate@binkert.org                # Set ordering
6146657Snate@binkert.org                if "ordered" in var:
6156657Snate@binkert.org                    # A buffer
6166657Snate@binkert.org                    code('$vid->setOrdering(${{var["ordered"]}});')
6176657Snate@binkert.org
6186657Snate@binkert.org                # Set randomization
6196657Snate@binkert.org                if "random" in var:
6206657Snate@binkert.org                    # A buffer
6218187SLisa.Hsu@amd.com                    code('$vid->setRandomization(${{var["random"]}});')
6226657Snate@binkert.org
6236657Snate@binkert.org                # Set Priority
6246657Snate@binkert.org                if "rank" in var:
6256657Snate@binkert.org                    code('$vid->setPriority(${{var["rank"]}})')
6266657Snate@binkert.org
6276657Snate@binkert.org                # Set buffer size
6286657Snate@binkert.org                if vtype.isBuffer:
6296657Snate@binkert.org                    code('''
6306657Snate@binkert.orgif (m_buffer_size > 0) {
6317454Snate@binkert.org    $vid->resize(m_buffer_size);
6326657Snate@binkert.org}
6336657Snate@binkert.org''')
6346657Snate@binkert.org
6356657Snate@binkert.org                # set description (may be overriden later by port def)
6367007Snate@binkert.org                code('''
63710305Snilay@cs.wisc.edu$vid->setDescription("[Version " + to_string(m_version) + ", ${ident}, name=${{var.ident}}]");
6387007Snate@binkert.org
6397007Snate@binkert.org''')
6406657Snate@binkert.org
6417566SBrad.Beckmann@amd.com            if vtype.isBuffer:
6427566SBrad.Beckmann@amd.com                if "recycle_latency" in var:
6439499Snilay@cs.wisc.edu                    code('$vid->setRecycleLatency( ' \
6449499Snilay@cs.wisc.edu                         'Cycles(${{var["recycle_latency"]}}));')
6457566SBrad.Beckmann@amd.com                else:
6467566SBrad.Beckmann@amd.com                    code('$vid->setRecycleLatency(m_recycle_latency);')
6477566SBrad.Beckmann@amd.com
6489366Snilay@cs.wisc.edu        # Set the prefetchers
6499366Snilay@cs.wisc.edu        code()
6509366Snilay@cs.wisc.edu        for prefetcher in self.prefetchers:
6519366Snilay@cs.wisc.edu            code('${{prefetcher.code}}.setController(this);')
6527566SBrad.Beckmann@amd.com
6537672Snate@binkert.org        code()
6546657Snate@binkert.org        for port in self.in_ports:
6559465Snilay@cs.wisc.edu            # Set the queue consumers
6566657Snate@binkert.org            code('${{port.code}}.setConsumer(this);')
6579465Snilay@cs.wisc.edu            # Set the queue descriptions
6587056Snate@binkert.org            code('${{port.code}}.setDescription("[Version " + to_string(m_version) + ", $ident, $port]");')
6596657Snate@binkert.org
6606657Snate@binkert.org        # Initialize the transition profiling
6617672Snate@binkert.org        code()
6626657Snate@binkert.org        for trans in self.transitions:
6636657Snate@binkert.org            # Figure out if we stall
6646657Snate@binkert.org            stall = False
6656657Snate@binkert.org            for action in trans.actions:
6666657Snate@binkert.org                if action.ident == "z_stall":
6676657Snate@binkert.org                    stall = True
6686657Snate@binkert.org
6696657Snate@binkert.org            # Only possible if it is not a 'z' case
6706657Snate@binkert.org            if not stall:
6716657Snate@binkert.org                state = "%s_State_%s" % (self.ident, trans.state.ident)
6726657Snate@binkert.org                event = "%s_Event_%s" % (self.ident, trans.event.ident)
6739745Snilay@cs.wisc.edu                code('possibleTransition($state, $event);')
6746657Snate@binkert.org
6756657Snate@binkert.org        code.dedent()
6769496Snilay@cs.wisc.edu        code('''
6779496Snilay@cs.wisc.edu    AbstractController::init();
67810012Snilay@cs.wisc.edu    resetStats();
6799496Snilay@cs.wisc.edu}
6809496Snilay@cs.wisc.edu''')
6816657Snate@binkert.org
68210121Snilay@cs.wisc.edu        mq_ident = "NULL"
6836657Snate@binkert.org        for port in self.in_ports:
6846657Snate@binkert.org            if port.code.find("mandatoryQueue_ptr") >= 0:
68510305Snilay@cs.wisc.edu                mq_ident = "m_mandatoryQueue_ptr"
6866657Snate@binkert.org
6878683Snilay@cs.wisc.edu        seq_ident = "NULL"
6888683Snilay@cs.wisc.edu        for param in self.config_parameters:
68910308Snilay@cs.wisc.edu            if param.ident == "sequencer":
6908683Snilay@cs.wisc.edu                assert(param.pointer)
69110308Snilay@cs.wisc.edu                seq_ident = "m_%s_ptr" % param.ident
6928683Snilay@cs.wisc.edu
6936657Snate@binkert.org        code('''
6949745Snilay@cs.wisc.edu
6959745Snilay@cs.wisc.eduvoid
6969745Snilay@cs.wisc.edu$c_ident::regStats()
6979745Snilay@cs.wisc.edu{
69810012Snilay@cs.wisc.edu    AbstractController::regStats();
69910012Snilay@cs.wisc.edu
7009745Snilay@cs.wisc.edu    if (m_version == 0) {
7019745Snilay@cs.wisc.edu        for (${ident}_Event event = ${ident}_Event_FIRST;
7029745Snilay@cs.wisc.edu             event < ${ident}_Event_NUM; ++event) {
7039745Snilay@cs.wisc.edu            Stats::Vector *t = new Stats::Vector();
7049745Snilay@cs.wisc.edu            t->init(m_num_controllers);
70510012Snilay@cs.wisc.edu            t->name(g_system_ptr->name() + ".${c_ident}." +
70610012Snilay@cs.wisc.edu                ${ident}_Event_to_string(event));
7079745Snilay@cs.wisc.edu            t->flags(Stats::pdf | Stats::total | Stats::oneline |
7089745Snilay@cs.wisc.edu                     Stats::nozero);
7099745Snilay@cs.wisc.edu
7109745Snilay@cs.wisc.edu            eventVec.push_back(t);
7119745Snilay@cs.wisc.edu        }
7129745Snilay@cs.wisc.edu
7139745Snilay@cs.wisc.edu        for (${ident}_State state = ${ident}_State_FIRST;
7149745Snilay@cs.wisc.edu             state < ${ident}_State_NUM; ++state) {
7159745Snilay@cs.wisc.edu
7169745Snilay@cs.wisc.edu            transVec.push_back(std::vector<Stats::Vector *>());
7179745Snilay@cs.wisc.edu
7189745Snilay@cs.wisc.edu            for (${ident}_Event event = ${ident}_Event_FIRST;
7199745Snilay@cs.wisc.edu                 event < ${ident}_Event_NUM; ++event) {
7209745Snilay@cs.wisc.edu
7219745Snilay@cs.wisc.edu                Stats::Vector *t = new Stats::Vector();
7229745Snilay@cs.wisc.edu                t->init(m_num_controllers);
72310012Snilay@cs.wisc.edu                t->name(g_system_ptr->name() + ".${c_ident}." +
72410012Snilay@cs.wisc.edu                        ${ident}_State_to_string(state) +
7259745Snilay@cs.wisc.edu                        "." + ${ident}_Event_to_string(event));
7269745Snilay@cs.wisc.edu
7279745Snilay@cs.wisc.edu                t->flags(Stats::pdf | Stats::total | Stats::oneline |
7289745Snilay@cs.wisc.edu                         Stats::nozero);
7299745Snilay@cs.wisc.edu                transVec[state].push_back(t);
7309745Snilay@cs.wisc.edu            }
7319745Snilay@cs.wisc.edu        }
7329745Snilay@cs.wisc.edu    }
7339745Snilay@cs.wisc.edu}
7349745Snilay@cs.wisc.edu
7359745Snilay@cs.wisc.eduvoid
7369745Snilay@cs.wisc.edu$c_ident::collateStats()
7379745Snilay@cs.wisc.edu{
7389745Snilay@cs.wisc.edu    for (${ident}_Event event = ${ident}_Event_FIRST;
7399745Snilay@cs.wisc.edu         event < ${ident}_Event_NUM; ++event) {
7409745Snilay@cs.wisc.edu        for (unsigned int i = 0; i < m_num_controllers; ++i) {
7419745Snilay@cs.wisc.edu            std::map<uint32_t, AbstractController *>::iterator it =
7429745Snilay@cs.wisc.edu                                g_abs_controls[MachineType_${ident}].find(i);
7439745Snilay@cs.wisc.edu            assert(it != g_abs_controls[MachineType_${ident}].end());
7449745Snilay@cs.wisc.edu            (*eventVec[event])[i] =
7459745Snilay@cs.wisc.edu                (($c_ident *)(*it).second)->getEventCount(event);
7469745Snilay@cs.wisc.edu        }
7479745Snilay@cs.wisc.edu    }
7489745Snilay@cs.wisc.edu
7499745Snilay@cs.wisc.edu    for (${ident}_State state = ${ident}_State_FIRST;
7509745Snilay@cs.wisc.edu         state < ${ident}_State_NUM; ++state) {
7519745Snilay@cs.wisc.edu
7529745Snilay@cs.wisc.edu        for (${ident}_Event event = ${ident}_Event_FIRST;
7539745Snilay@cs.wisc.edu             event < ${ident}_Event_NUM; ++event) {
7549745Snilay@cs.wisc.edu
7559745Snilay@cs.wisc.edu            for (unsigned int i = 0; i < m_num_controllers; ++i) {
7569745Snilay@cs.wisc.edu                std::map<uint32_t, AbstractController *>::iterator it =
7579745Snilay@cs.wisc.edu                                g_abs_controls[MachineType_${ident}].find(i);
7589745Snilay@cs.wisc.edu                assert(it != g_abs_controls[MachineType_${ident}].end());
7599745Snilay@cs.wisc.edu                (*transVec[state][event])[i] =
7609745Snilay@cs.wisc.edu                    (($c_ident *)(*it).second)->getTransitionCount(state, event);
7619745Snilay@cs.wisc.edu            }
7629745Snilay@cs.wisc.edu        }
7639745Snilay@cs.wisc.edu    }
7649745Snilay@cs.wisc.edu}
7659745Snilay@cs.wisc.edu
7669745Snilay@cs.wisc.eduvoid
7679745Snilay@cs.wisc.edu$c_ident::countTransition(${ident}_State state, ${ident}_Event event)
7689745Snilay@cs.wisc.edu{
7699745Snilay@cs.wisc.edu    assert(m_possible[state][event]);
7709745Snilay@cs.wisc.edu    m_counters[state][event]++;
7719745Snilay@cs.wisc.edu    m_event_counters[event]++;
7729745Snilay@cs.wisc.edu}
7739745Snilay@cs.wisc.eduvoid
7749745Snilay@cs.wisc.edu$c_ident::possibleTransition(${ident}_State state,
7759745Snilay@cs.wisc.edu                             ${ident}_Event event)
7769745Snilay@cs.wisc.edu{
7779745Snilay@cs.wisc.edu    m_possible[state][event] = true;
7789745Snilay@cs.wisc.edu}
7799745Snilay@cs.wisc.edu
7809745Snilay@cs.wisc.eduuint64
7819745Snilay@cs.wisc.edu$c_ident::getEventCount(${ident}_Event event)
7829745Snilay@cs.wisc.edu{
7839745Snilay@cs.wisc.edu    return m_event_counters[event];
7849745Snilay@cs.wisc.edu}
7859745Snilay@cs.wisc.edu
7869745Snilay@cs.wisc.edubool
7879745Snilay@cs.wisc.edu$c_ident::isPossible(${ident}_State state, ${ident}_Event event)
7889745Snilay@cs.wisc.edu{
7899745Snilay@cs.wisc.edu    return m_possible[state][event];
7909745Snilay@cs.wisc.edu}
7919745Snilay@cs.wisc.edu
7929745Snilay@cs.wisc.eduuint64
7939745Snilay@cs.wisc.edu$c_ident::getTransitionCount(${ident}_State state,
7949745Snilay@cs.wisc.edu                             ${ident}_Event event)
7959745Snilay@cs.wisc.edu{
7969745Snilay@cs.wisc.edu    return m_counters[state][event];
7979745Snilay@cs.wisc.edu}
7989745Snilay@cs.wisc.edu
7997007Snate@binkert.orgint
8007007Snate@binkert.org$c_ident::getNumControllers()
8017007Snate@binkert.org{
8026657Snate@binkert.org    return m_num_controllers;
8036657Snate@binkert.org}
8046657Snate@binkert.org
8057007Snate@binkert.orgMessageBuffer*
8067007Snate@binkert.org$c_ident::getMandatoryQueue() const
8077007Snate@binkert.org{
8086657Snate@binkert.org    return $mq_ident;
8096657Snate@binkert.org}
8106657Snate@binkert.org
8118683Snilay@cs.wisc.eduSequencer*
8128683Snilay@cs.wisc.edu$c_ident::getSequencer() const
8138683Snilay@cs.wisc.edu{
8148683Snilay@cs.wisc.edu    return $seq_ident;
8158683Snilay@cs.wisc.edu}
8168683Snilay@cs.wisc.edu
8177007Snate@binkert.orgvoid
8187007Snate@binkert.org$c_ident::print(ostream& out) const
8197007Snate@binkert.org{
8207007Snate@binkert.org    out << "[$c_ident " << m_version << "]";
8217007Snate@binkert.org}
8226657Snate@binkert.org
82310012Snilay@cs.wisc.eduvoid $c_ident::resetStats()
8249745Snilay@cs.wisc.edu{
8259745Snilay@cs.wisc.edu    for (int state = 0; state < ${ident}_State_NUM; state++) {
8269745Snilay@cs.wisc.edu        for (int event = 0; event < ${ident}_Event_NUM; event++) {
8279745Snilay@cs.wisc.edu            m_counters[state][event] = 0;
8289745Snilay@cs.wisc.edu        }
8299745Snilay@cs.wisc.edu    }
8306902SBrad.Beckmann@amd.com
8319745Snilay@cs.wisc.edu    for (int event = 0; event < ${ident}_Event_NUM; event++) {
8329745Snilay@cs.wisc.edu        m_event_counters[event] = 0;
8339745Snilay@cs.wisc.edu    }
8349745Snilay@cs.wisc.edu
83510012Snilay@cs.wisc.edu    AbstractController::resetStats();
8366902SBrad.Beckmann@amd.com}
8377839Snilay@cs.wisc.edu''')
8387839Snilay@cs.wisc.edu
8397839Snilay@cs.wisc.edu        if self.EntryType != None:
8407839Snilay@cs.wisc.edu            code('''
8417839Snilay@cs.wisc.edu
8427839Snilay@cs.wisc.edu// Set and Reset for cache_entry variable
8437839Snilay@cs.wisc.eduvoid
8447839Snilay@cs.wisc.edu$c_ident::set_cache_entry(${{self.EntryType.c_ident}}*& m_cache_entry_ptr, AbstractCacheEntry* m_new_cache_entry)
8457839Snilay@cs.wisc.edu{
8467839Snilay@cs.wisc.edu  m_cache_entry_ptr = (${{self.EntryType.c_ident}}*)m_new_cache_entry;
8477839Snilay@cs.wisc.edu}
8487839Snilay@cs.wisc.edu
8497839Snilay@cs.wisc.eduvoid
8507839Snilay@cs.wisc.edu$c_ident::unset_cache_entry(${{self.EntryType.c_ident}}*& m_cache_entry_ptr)
8517839Snilay@cs.wisc.edu{
8527839Snilay@cs.wisc.edu  m_cache_entry_ptr = 0;
8537839Snilay@cs.wisc.edu}
8547839Snilay@cs.wisc.edu''')
8557839Snilay@cs.wisc.edu
8567839Snilay@cs.wisc.edu        if self.TBEType != None:
8577839Snilay@cs.wisc.edu            code('''
8587839Snilay@cs.wisc.edu
8597839Snilay@cs.wisc.edu// Set and Reset for tbe variable
8607839Snilay@cs.wisc.eduvoid
8617839Snilay@cs.wisc.edu$c_ident::set_tbe(${{self.TBEType.c_ident}}*& m_tbe_ptr, ${{self.TBEType.c_ident}}* m_new_tbe)
8627839Snilay@cs.wisc.edu{
8637839Snilay@cs.wisc.edu  m_tbe_ptr = m_new_tbe;
8647839Snilay@cs.wisc.edu}
8657839Snilay@cs.wisc.edu
8667839Snilay@cs.wisc.eduvoid
8677839Snilay@cs.wisc.edu$c_ident::unset_tbe(${{self.TBEType.c_ident}}*& m_tbe_ptr)
8687839Snilay@cs.wisc.edu{
8697839Snilay@cs.wisc.edu  m_tbe_ptr = NULL;
8707839Snilay@cs.wisc.edu}
8717839Snilay@cs.wisc.edu''')
8727839Snilay@cs.wisc.edu
8737839Snilay@cs.wisc.edu        code('''
8746902SBrad.Beckmann@amd.com
8758683Snilay@cs.wisc.eduvoid
8768683Snilay@cs.wisc.edu$c_ident::recordCacheTrace(int cntrl, CacheRecorder* tr)
8778683Snilay@cs.wisc.edu{
8788683Snilay@cs.wisc.edu''')
8798683Snilay@cs.wisc.edu        #
8808683Snilay@cs.wisc.edu        # Record cache contents for all associated caches.
8818683Snilay@cs.wisc.edu        #
8828683Snilay@cs.wisc.edu        code.indent()
8838683Snilay@cs.wisc.edu        for param in self.config_parameters:
8848683Snilay@cs.wisc.edu            if param.type_ast.type.ident == "CacheMemory":
8858683Snilay@cs.wisc.edu                assert(param.pointer)
8868683Snilay@cs.wisc.edu                code('m_${{param.ident}}_ptr->recordCacheContents(cntrl, tr);')
8878683Snilay@cs.wisc.edu
8888683Snilay@cs.wisc.edu        code.dedent()
8898683Snilay@cs.wisc.edu        code('''
8908683Snilay@cs.wisc.edu}
8918683Snilay@cs.wisc.edu
8926657Snate@binkert.org// Actions
8936657Snate@binkert.org''')
8947839Snilay@cs.wisc.edu        if self.TBEType != None and self.EntryType != None:
8957839Snilay@cs.wisc.edu            for action in self.actions.itervalues():
8967839Snilay@cs.wisc.edu                if "c_code" not in action:
8977839Snilay@cs.wisc.edu                 continue
8986657Snate@binkert.org
8997839Snilay@cs.wisc.edu                code('''
9007839Snilay@cs.wisc.edu/** \\brief ${{action.desc}} */
9017839Snilay@cs.wisc.eduvoid
9027839Snilay@cs.wisc.edu$c_ident::${{action.ident}}(${{self.TBEType.c_ident}}*& m_tbe_ptr, ${{self.EntryType.c_ident}}*& m_cache_entry_ptr, const Address& addr)
9037839Snilay@cs.wisc.edu{
9048055Sksewell@umich.edu    DPRINTF(RubyGenerated, "executing ${{action.ident}}\\n");
9057839Snilay@cs.wisc.edu    ${{action["c_code"]}}
9067839Snilay@cs.wisc.edu}
9076657Snate@binkert.org
9087839Snilay@cs.wisc.edu''')
9097839Snilay@cs.wisc.edu        elif self.TBEType != None:
9107839Snilay@cs.wisc.edu            for action in self.actions.itervalues():
9117839Snilay@cs.wisc.edu                if "c_code" not in action:
9127839Snilay@cs.wisc.edu                 continue
9137839Snilay@cs.wisc.edu
9147839Snilay@cs.wisc.edu                code('''
9157839Snilay@cs.wisc.edu/** \\brief ${{action.desc}} */
9167839Snilay@cs.wisc.eduvoid
9177839Snilay@cs.wisc.edu$c_ident::${{action.ident}}(${{self.TBEType.c_ident}}*& m_tbe_ptr, const Address& addr)
9187839Snilay@cs.wisc.edu{
9198055Sksewell@umich.edu    DPRINTF(RubyGenerated, "executing ${{action.ident}}\\n");
9207839Snilay@cs.wisc.edu    ${{action["c_code"]}}
9217839Snilay@cs.wisc.edu}
9227839Snilay@cs.wisc.edu
9237839Snilay@cs.wisc.edu''')
9247839Snilay@cs.wisc.edu        elif self.EntryType != None:
9257839Snilay@cs.wisc.edu            for action in self.actions.itervalues():
9267839Snilay@cs.wisc.edu                if "c_code" not in action:
9277839Snilay@cs.wisc.edu                 continue
9287839Snilay@cs.wisc.edu
9297839Snilay@cs.wisc.edu                code('''
9307839Snilay@cs.wisc.edu/** \\brief ${{action.desc}} */
9317839Snilay@cs.wisc.eduvoid
9327839Snilay@cs.wisc.edu$c_ident::${{action.ident}}(${{self.EntryType.c_ident}}*& m_cache_entry_ptr, const Address& addr)
9337839Snilay@cs.wisc.edu{
9348055Sksewell@umich.edu    DPRINTF(RubyGenerated, "executing ${{action.ident}}\\n");
9357839Snilay@cs.wisc.edu    ${{action["c_code"]}}
9367839Snilay@cs.wisc.edu}
9377839Snilay@cs.wisc.edu
9387839Snilay@cs.wisc.edu''')
9397839Snilay@cs.wisc.edu        else:
9407839Snilay@cs.wisc.edu            for action in self.actions.itervalues():
9417839Snilay@cs.wisc.edu                if "c_code" not in action:
9427839Snilay@cs.wisc.edu                 continue
9437839Snilay@cs.wisc.edu
9447839Snilay@cs.wisc.edu                code('''
9456657Snate@binkert.org/** \\brief ${{action.desc}} */
9467007Snate@binkert.orgvoid
9477007Snate@binkert.org$c_ident::${{action.ident}}(const Address& addr)
9486657Snate@binkert.org{
9498055Sksewell@umich.edu    DPRINTF(RubyGenerated, "executing ${{action.ident}}\\n");
9506657Snate@binkert.org    ${{action["c_code"]}}
9516657Snate@binkert.org}
9526657Snate@binkert.org
9536657Snate@binkert.org''')
9548478Snilay@cs.wisc.edu        for func in self.functions:
9558478Snilay@cs.wisc.edu            code(func.generateCode())
9568478Snilay@cs.wisc.edu
9579302Snilay@cs.wisc.edu        # Function for functional reads from messages buffered in the controller
9589302Snilay@cs.wisc.edu        code('''
9599302Snilay@cs.wisc.edubool
9609302Snilay@cs.wisc.edu$c_ident::functionalReadBuffers(PacketPtr& pkt)
9619302Snilay@cs.wisc.edu{
9629302Snilay@cs.wisc.edu''')
9639302Snilay@cs.wisc.edu        for var in self.objects:
9649302Snilay@cs.wisc.edu            vtype = var.type
9659302Snilay@cs.wisc.edu            if vtype.isBuffer:
96610305Snilay@cs.wisc.edu                vid = "m_%s_ptr" % var.ident
9679302Snilay@cs.wisc.edu                code('if ($vid->functionalRead(pkt)) { return true; }')
9689302Snilay@cs.wisc.edu        code('''
9699302Snilay@cs.wisc.edu                return false;
9709302Snilay@cs.wisc.edu}
9719302Snilay@cs.wisc.edu''')
9729302Snilay@cs.wisc.edu
9739302Snilay@cs.wisc.edu        # Function for functional writes to messages buffered in the controller
9749302Snilay@cs.wisc.edu        code('''
9759302Snilay@cs.wisc.eduuint32_t
9769302Snilay@cs.wisc.edu$c_ident::functionalWriteBuffers(PacketPtr& pkt)
9779302Snilay@cs.wisc.edu{
9789302Snilay@cs.wisc.edu    uint32_t num_functional_writes = 0;
9799302Snilay@cs.wisc.edu''')
9809302Snilay@cs.wisc.edu        for var in self.objects:
9819302Snilay@cs.wisc.edu            vtype = var.type
9829302Snilay@cs.wisc.edu            if vtype.isBuffer:
98310305Snilay@cs.wisc.edu                vid = "m_%s_ptr" % var.ident
9849302Snilay@cs.wisc.edu                code('num_functional_writes += $vid->functionalWrite(pkt);')
9859302Snilay@cs.wisc.edu        code('''
9869302Snilay@cs.wisc.edu    return num_functional_writes;
9879302Snilay@cs.wisc.edu}
9889302Snilay@cs.wisc.edu''')
9899302Snilay@cs.wisc.edu
9909595Snilay@cs.wisc.edu        # Check if this controller has a peer, if yes then write the
9919595Snilay@cs.wisc.edu        # function for connecting to the peer.
9929595Snilay@cs.wisc.edu        if has_peer:
9939595Snilay@cs.wisc.edu            code('''
9949595Snilay@cs.wisc.edu
9959595Snilay@cs.wisc.eduvoid
9969595Snilay@cs.wisc.edu$c_ident::getQueuesFromPeer(AbstractController *peer)
9979595Snilay@cs.wisc.edu{
9989595Snilay@cs.wisc.edu''')
9999595Snilay@cs.wisc.edu            for var in self.objects:
10009595Snilay@cs.wisc.edu                if "network" in var and "physical_network" in var and \
10019595Snilay@cs.wisc.edu                   var["network"] == "From":
10029595Snilay@cs.wisc.edu                    code('''
100310305Snilay@cs.wisc.edum_${{var.ident}}_ptr = peer->getPeerQueue(${{var["physical_network"]}});
100410305Snilay@cs.wisc.eduassert(m_${{var.ident}}_ptr != NULL);
100510305Snilay@cs.wisc.edum_${{var.ident}}_ptr->setReceiver(this);
10069595Snilay@cs.wisc.edu
10079595Snilay@cs.wisc.edu''')
10089595Snilay@cs.wisc.edu            code('}')
10099595Snilay@cs.wisc.edu
10106657Snate@binkert.org        code.write(path, "%s.cc" % c_ident)
10116657Snate@binkert.org
10129219Spower.jg@gmail.com    def printCWakeup(self, path, includes):
10136657Snate@binkert.org        '''Output the wakeup loop for the events'''
10146657Snate@binkert.org
10156999Snate@binkert.org        code = self.symtab.codeFormatter()
10166657Snate@binkert.org        ident = self.ident
10176657Snate@binkert.org
10189104Shestness@cs.utexas.edu        outputRequest_types = True
10199104Shestness@cs.utexas.edu        if len(self.request_types) == 0:
10209104Shestness@cs.utexas.edu            outputRequest_types = False
10219104Shestness@cs.utexas.edu
10226657Snate@binkert.org        code('''
10236657Snate@binkert.org// Auto generated C++ code started by $__file__:$__line__
10246657Snate@binkert.org// ${ident}: ${{self.short}}
10256657Snate@binkert.org
10268946Sandreas.hansson@arm.com#include <sys/types.h>
10278946Sandreas.hansson@arm.com#include <unistd.h>
10288946Sandreas.hansson@arm.com
10297832Snate@binkert.org#include <cassert>
10307832Snate@binkert.org
10317007Snate@binkert.org#include "base/misc.hh"
10328232Snate@binkert.org#include "debug/RubySlicc.hh"
10338229Snate@binkert.org#include "mem/protocol/${ident}_Controller.hh"
10348229Snate@binkert.org#include "mem/protocol/${ident}_Event.hh"
10358229Snate@binkert.org#include "mem/protocol/${ident}_State.hh"
10369104Shestness@cs.utexas.edu''')
10379104Shestness@cs.utexas.edu
10389104Shestness@cs.utexas.edu        if outputRequest_types:
10399104Shestness@cs.utexas.edu            code('''#include "mem/protocol/${ident}_RequestType.hh"''')
10409104Shestness@cs.utexas.edu
10419104Shestness@cs.utexas.edu        code('''
10428229Snate@binkert.org#include "mem/protocol/Types.hh"
10436657Snate@binkert.org#include "mem/ruby/common/Global.hh"
10446657Snate@binkert.org#include "mem/ruby/system/System.hh"
10459219Spower.jg@gmail.com''')
10469219Spower.jg@gmail.com
10479219Spower.jg@gmail.com
10489219Spower.jg@gmail.com        for include_path in includes:
10499219Spower.jg@gmail.com            code('#include "${{include_path}}"')
10509219Spower.jg@gmail.com
10519219Spower.jg@gmail.com        code('''
10526657Snate@binkert.org
10537055Snate@binkert.orgusing namespace std;
10547055Snate@binkert.org
10557007Snate@binkert.orgvoid
10567007Snate@binkert.org${ident}_Controller::wakeup()
10576657Snate@binkert.org{
10586657Snate@binkert.org    int counter = 0;
10596657Snate@binkert.org    while (true) {
10606657Snate@binkert.org        // Some cases will put us into an infinite loop without this limit
10616657Snate@binkert.org        assert(counter <= m_transitions_per_cycle);
10626657Snate@binkert.org        if (counter == m_transitions_per_cycle) {
10637007Snate@binkert.org            // Count how often we are fully utilized
10649496Snilay@cs.wisc.edu            m_fully_busy_cycles++;
10657007Snate@binkert.org
10667007Snate@binkert.org            // Wakeup in another cycle and try again
10679499Snilay@cs.wisc.edu            scheduleEvent(Cycles(1));
10686657Snate@binkert.org            break;
10696657Snate@binkert.org        }
10706657Snate@binkert.org''')
10716657Snate@binkert.org
10726657Snate@binkert.org        code.indent()
10736657Snate@binkert.org        code.indent()
10746657Snate@binkert.org
10756657Snate@binkert.org        # InPorts
10766657Snate@binkert.org        #
10776657Snate@binkert.org        for port in self.in_ports:
10786657Snate@binkert.org            code.indent()
10796657Snate@binkert.org            code('// ${ident}InPort $port')
10807567SBrad.Beckmann@amd.com            if port.pairs.has_key("rank"):
10819996Snilay@cs.wisc.edu                code('m_cur_in_port = ${{port.pairs["rank"]}};')
10827567SBrad.Beckmann@amd.com            else:
10839996Snilay@cs.wisc.edu                code('m_cur_in_port = 0;')
10846657Snate@binkert.org            code('${{port["c_code_in_port"]}}')
10856657Snate@binkert.org            code.dedent()
10866657Snate@binkert.org
10876657Snate@binkert.org            code('')
10886657Snate@binkert.org
10896657Snate@binkert.org        code.dedent()
10906657Snate@binkert.org        code.dedent()
10916657Snate@binkert.org        code('''
10926657Snate@binkert.org        break;  // If we got this far, we have nothing left todo
10936657Snate@binkert.org    }
10946657Snate@binkert.org}
10956657Snate@binkert.org''')
10966657Snate@binkert.org
10976657Snate@binkert.org        code.write(path, "%s_Wakeup.cc" % self.ident)
10986657Snate@binkert.org
10996657Snate@binkert.org    def printCSwitch(self, path):
11006657Snate@binkert.org        '''Output switch statement for transition table'''
11016657Snate@binkert.org
11026999Snate@binkert.org        code = self.symtab.codeFormatter()
11036657Snate@binkert.org        ident = self.ident
11046657Snate@binkert.org
11056657Snate@binkert.org        code('''
11066657Snate@binkert.org// Auto generated C++ code started by $__file__:$__line__
11076657Snate@binkert.org// ${ident}: ${{self.short}}
11086657Snate@binkert.org
11097832Snate@binkert.org#include <cassert>
11107832Snate@binkert.org
11117805Snilay@cs.wisc.edu#include "base/misc.hh"
11127832Snate@binkert.org#include "base/trace.hh"
11138232Snate@binkert.org#include "debug/ProtocolTrace.hh"
11148232Snate@binkert.org#include "debug/RubyGenerated.hh"
11158229Snate@binkert.org#include "mem/protocol/${ident}_Controller.hh"
11168229Snate@binkert.org#include "mem/protocol/${ident}_Event.hh"
11178229Snate@binkert.org#include "mem/protocol/${ident}_State.hh"
11188229Snate@binkert.org#include "mem/protocol/Types.hh"
11196657Snate@binkert.org#include "mem/ruby/common/Global.hh"
11206657Snate@binkert.org#include "mem/ruby/system/System.hh"
11216657Snate@binkert.org
11226657Snate@binkert.org#define HASH_FUN(state, event)  ((int(state)*${ident}_Event_NUM)+int(event))
11236657Snate@binkert.org
11246657Snate@binkert.org#define GET_TRANSITION_COMMENT() (${ident}_transitionComment.str())
11256657Snate@binkert.org#define CLEAR_TRANSITION_COMMENT() (${ident}_transitionComment.str(""))
11266657Snate@binkert.org
11277007Snate@binkert.orgTransitionResult
11287007Snate@binkert.org${ident}_Controller::doTransition(${ident}_Event event,
11297839Snilay@cs.wisc.edu''')
11307839Snilay@cs.wisc.edu        if self.EntryType != None:
11317839Snilay@cs.wisc.edu            code('''
11327839Snilay@cs.wisc.edu                                  ${{self.EntryType.c_ident}}* m_cache_entry_ptr,
11337839Snilay@cs.wisc.edu''')
11347839Snilay@cs.wisc.edu        if self.TBEType != None:
11357839Snilay@cs.wisc.edu            code('''
11367839Snilay@cs.wisc.edu                                  ${{self.TBEType.c_ident}}* m_tbe_ptr,
11377839Snilay@cs.wisc.edu''')
11387839Snilay@cs.wisc.edu        code('''
113910010Snilay@cs.wisc.edu                                  const Address addr)
11406657Snate@binkert.org{
11417839Snilay@cs.wisc.edu''')
114210305Snilay@cs.wisc.edu        code.indent()
114310305Snilay@cs.wisc.edu
11447839Snilay@cs.wisc.edu        if self.TBEType != None and self.EntryType != None:
11458337Snilay@cs.wisc.edu            code('${ident}_State state = getState(m_tbe_ptr, m_cache_entry_ptr, addr);')
11467839Snilay@cs.wisc.edu        elif self.TBEType != None:
11478337Snilay@cs.wisc.edu            code('${ident}_State state = getState(m_tbe_ptr, addr);')
11487839Snilay@cs.wisc.edu        elif self.EntryType != None:
11498337Snilay@cs.wisc.edu            code('${ident}_State state = getState(m_cache_entry_ptr, addr);')
11507839Snilay@cs.wisc.edu        else:
11518337Snilay@cs.wisc.edu            code('${ident}_State state = getState(addr);')
11527839Snilay@cs.wisc.edu
11537839Snilay@cs.wisc.edu        code('''
115410305Snilay@cs.wisc.edu${ident}_State next_state = state;
11556657Snate@binkert.org
115610305Snilay@cs.wisc.eduDPRINTF(RubyGenerated, "%s, Time: %lld, state: %s, event: %s, addr: %s\\n",
115710305Snilay@cs.wisc.edu        *this, curCycle(), ${ident}_State_to_string(state),
115810305Snilay@cs.wisc.edu        ${ident}_Event_to_string(event), addr);
11596657Snate@binkert.org
116010305Snilay@cs.wisc.eduTransitionResult result =
11617839Snilay@cs.wisc.edu''')
11627839Snilay@cs.wisc.edu        if self.TBEType != None and self.EntryType != None:
11637839Snilay@cs.wisc.edu            code('doTransitionWorker(event, state, next_state, m_tbe_ptr, m_cache_entry_ptr, addr);')
11647839Snilay@cs.wisc.edu        elif self.TBEType != None:
11657839Snilay@cs.wisc.edu            code('doTransitionWorker(event, state, next_state, m_tbe_ptr, addr);')
11667839Snilay@cs.wisc.edu        elif self.EntryType != None:
11677839Snilay@cs.wisc.edu            code('doTransitionWorker(event, state, next_state, m_cache_entry_ptr, addr);')
11687839Snilay@cs.wisc.edu        else:
11697839Snilay@cs.wisc.edu            code('doTransitionWorker(event, state, next_state, addr);')
11706657Snate@binkert.org
11717839Snilay@cs.wisc.edu        code('''
11726657Snate@binkert.org
117310305Snilay@cs.wisc.eduif (result == TransitionResult_Valid) {
117410305Snilay@cs.wisc.edu    DPRINTF(RubyGenerated, "next_state: %s\\n",
117510305Snilay@cs.wisc.edu            ${ident}_State_to_string(next_state));
117610305Snilay@cs.wisc.edu    countTransition(state, event);
117710305Snilay@cs.wisc.edu
117810305Snilay@cs.wisc.edu    DPRINTFR(ProtocolTrace, "%15d %3s %10s%20s %6s>%-6s %s %s\\n",
117910305Snilay@cs.wisc.edu             curTick(), m_version, "${ident}",
118010305Snilay@cs.wisc.edu             ${ident}_Event_to_string(event),
118110305Snilay@cs.wisc.edu             ${ident}_State_to_string(state),
118210305Snilay@cs.wisc.edu             ${ident}_State_to_string(next_state),
118310305Snilay@cs.wisc.edu             addr, GET_TRANSITION_COMMENT());
118410305Snilay@cs.wisc.edu
118510305Snilay@cs.wisc.edu    CLEAR_TRANSITION_COMMENT();
11867839Snilay@cs.wisc.edu''')
11877839Snilay@cs.wisc.edu        if self.TBEType != None and self.EntryType != None:
11888337Snilay@cs.wisc.edu            code('setState(m_tbe_ptr, m_cache_entry_ptr, addr, next_state);')
11898341Snilay@cs.wisc.edu            code('setAccessPermission(m_cache_entry_ptr, addr, next_state);')
11907839Snilay@cs.wisc.edu        elif self.TBEType != None:
11918337Snilay@cs.wisc.edu            code('setState(m_tbe_ptr, addr, next_state);')
11928341Snilay@cs.wisc.edu            code('setAccessPermission(addr, next_state);')
11937839Snilay@cs.wisc.edu        elif self.EntryType != None:
11948337Snilay@cs.wisc.edu            code('setState(m_cache_entry_ptr, addr, next_state);')
11958341Snilay@cs.wisc.edu            code('setAccessPermission(m_cache_entry_ptr, addr, next_state);')
11967839Snilay@cs.wisc.edu        else:
11978337Snilay@cs.wisc.edu            code('setState(addr, next_state);')
11988341Snilay@cs.wisc.edu            code('setAccessPermission(addr, next_state);')
11997839Snilay@cs.wisc.edu
12007839Snilay@cs.wisc.edu        code('''
120110305Snilay@cs.wisc.edu} else if (result == TransitionResult_ResourceStall) {
120210305Snilay@cs.wisc.edu    DPRINTFR(ProtocolTrace, "%15s %3s %10s%20s %6s>%-6s %s %s\\n",
120310305Snilay@cs.wisc.edu             curTick(), m_version, "${ident}",
120410305Snilay@cs.wisc.edu             ${ident}_Event_to_string(event),
120510305Snilay@cs.wisc.edu             ${ident}_State_to_string(state),
120610305Snilay@cs.wisc.edu             ${ident}_State_to_string(next_state),
120710305Snilay@cs.wisc.edu             addr, "Resource Stall");
120810305Snilay@cs.wisc.edu} else if (result == TransitionResult_ProtocolStall) {
120910305Snilay@cs.wisc.edu    DPRINTF(RubyGenerated, "stalling\\n");
121010305Snilay@cs.wisc.edu    DPRINTFR(ProtocolTrace, "%15s %3s %10s%20s %6s>%-6s %s %s\\n",
121110305Snilay@cs.wisc.edu             curTick(), m_version, "${ident}",
121210305Snilay@cs.wisc.edu             ${ident}_Event_to_string(event),
121310305Snilay@cs.wisc.edu             ${ident}_State_to_string(state),
121410305Snilay@cs.wisc.edu             ${ident}_State_to_string(next_state),
121510305Snilay@cs.wisc.edu             addr, "Protocol Stall");
121610305Snilay@cs.wisc.edu}
12176657Snate@binkert.org
121810305Snilay@cs.wisc.edureturn result;
121910305Snilay@cs.wisc.edu''')
122010305Snilay@cs.wisc.edu        code.dedent()
122110305Snilay@cs.wisc.edu        code('''
12226657Snate@binkert.org}
12236657Snate@binkert.org
12247007Snate@binkert.orgTransitionResult
12257007Snate@binkert.org${ident}_Controller::doTransitionWorker(${ident}_Event event,
12267007Snate@binkert.org                                        ${ident}_State state,
12277007Snate@binkert.org                                        ${ident}_State& next_state,
12287839Snilay@cs.wisc.edu''')
12297839Snilay@cs.wisc.edu
12307839Snilay@cs.wisc.edu        if self.TBEType != None:
12317839Snilay@cs.wisc.edu            code('''
12327839Snilay@cs.wisc.edu                                        ${{self.TBEType.c_ident}}*& m_tbe_ptr,
12337839Snilay@cs.wisc.edu''')
12347839Snilay@cs.wisc.edu        if self.EntryType != None:
12357839Snilay@cs.wisc.edu                  code('''
12367839Snilay@cs.wisc.edu                                        ${{self.EntryType.c_ident}}*& m_cache_entry_ptr,
12377839Snilay@cs.wisc.edu''')
12387839Snilay@cs.wisc.edu        code('''
12397007Snate@binkert.org                                        const Address& addr)
12406657Snate@binkert.org{
12416657Snate@binkert.org    switch(HASH_FUN(state, event)) {
12426657Snate@binkert.org''')
12436657Snate@binkert.org
12446657Snate@binkert.org        # This map will allow suppress generating duplicate code
12456657Snate@binkert.org        cases = orderdict()
12466657Snate@binkert.org
12476657Snate@binkert.org        for trans in self.transitions:
12486657Snate@binkert.org            case_string = "%s_State_%s, %s_Event_%s" % \
12496657Snate@binkert.org                (self.ident, trans.state.ident, self.ident, trans.event.ident)
12506657Snate@binkert.org
12516999Snate@binkert.org            case = self.symtab.codeFormatter()
12526657Snate@binkert.org            # Only set next_state if it changes
12536657Snate@binkert.org            if trans.state != trans.nextState:
12546657Snate@binkert.org                ns_ident = trans.nextState.ident
12556657Snate@binkert.org                case('next_state = ${ident}_State_${ns_ident};')
12566657Snate@binkert.org
12576657Snate@binkert.org            actions = trans.actions
12589104Shestness@cs.utexas.edu            request_types = trans.request_types
12596657Snate@binkert.org
12606657Snate@binkert.org            # Check for resources
12616657Snate@binkert.org            case_sorter = []
12626657Snate@binkert.org            res = trans.resources
12636657Snate@binkert.org            for key,val in res.iteritems():
126410228Snilay@cs.wisc.edu                val = '''
12657007Snate@binkert.orgif (!%s.areNSlotsAvailable(%s))
12666657Snate@binkert.org    return TransitionResult_ResourceStall;
12676657Snate@binkert.org''' % (key.code, val)
12686657Snate@binkert.org                case_sorter.append(val)
12696657Snate@binkert.org
12709105SBrad.Beckmann@amd.com            # Check all of the request_types for resource constraints
12719105SBrad.Beckmann@amd.com            for request_type in request_types:
12729105SBrad.Beckmann@amd.com                val = '''
12739105SBrad.Beckmann@amd.comif (!checkResourceAvailable(%s_RequestType_%s, addr)) {
12749105SBrad.Beckmann@amd.com    return TransitionResult_ResourceStall;
12759105SBrad.Beckmann@amd.com}
12769105SBrad.Beckmann@amd.com''' % (self.ident, request_type.ident)
12779105SBrad.Beckmann@amd.com                case_sorter.append(val)
12786657Snate@binkert.org
12796657Snate@binkert.org            # Emit the code sequences in a sorted order.  This makes the
12806657Snate@binkert.org            # output deterministic (without this the output order can vary
12816657Snate@binkert.org            # since Map's keys() on a vector of pointers is not deterministic
12826657Snate@binkert.org            for c in sorted(case_sorter):
12836657Snate@binkert.org                case("$c")
12846657Snate@binkert.org
12859104Shestness@cs.utexas.edu            # Record access types for this transition
12869104Shestness@cs.utexas.edu            for request_type in request_types:
12879104Shestness@cs.utexas.edu                case('recordRequestType(${ident}_RequestType_${{request_type.ident}}, addr);')
12889104Shestness@cs.utexas.edu
12896657Snate@binkert.org            # Figure out if we stall
12906657Snate@binkert.org            stall = False
12916657Snate@binkert.org            for action in actions:
12926657Snate@binkert.org                if action.ident == "z_stall":
12936657Snate@binkert.org                    stall = True
12946657Snate@binkert.org                    break
12956657Snate@binkert.org
12966657Snate@binkert.org            if stall:
12976657Snate@binkert.org                case('return TransitionResult_ProtocolStall;')
12986657Snate@binkert.org            else:
12997839Snilay@cs.wisc.edu                if self.TBEType != None and self.EntryType != None:
13007839Snilay@cs.wisc.edu                    for action in actions:
13017839Snilay@cs.wisc.edu                        case('${{action.ident}}(m_tbe_ptr, m_cache_entry_ptr, addr);')
13027839Snilay@cs.wisc.edu                elif self.TBEType != None:
13037839Snilay@cs.wisc.edu                    for action in actions:
13047839Snilay@cs.wisc.edu                        case('${{action.ident}}(m_tbe_ptr, addr);')
13057839Snilay@cs.wisc.edu                elif self.EntryType != None:
13067839Snilay@cs.wisc.edu                    for action in actions:
13077839Snilay@cs.wisc.edu                        case('${{action.ident}}(m_cache_entry_ptr, addr);')
13087839Snilay@cs.wisc.edu                else:
13097839Snilay@cs.wisc.edu                    for action in actions:
13107839Snilay@cs.wisc.edu                        case('${{action.ident}}(addr);')
13116657Snate@binkert.org                case('return TransitionResult_Valid;')
13126657Snate@binkert.org
13136657Snate@binkert.org            case = str(case)
13146657Snate@binkert.org
13156657Snate@binkert.org            # Look to see if this transition code is unique.
13166657Snate@binkert.org            if case not in cases:
13176657Snate@binkert.org                cases[case] = []
13186657Snate@binkert.org
13196657Snate@binkert.org            cases[case].append(case_string)
13206657Snate@binkert.org
13216657Snate@binkert.org        # Walk through all of the unique code blocks and spit out the
13226657Snate@binkert.org        # corresponding case statement elements
13236657Snate@binkert.org        for case,transitions in cases.iteritems():
13246657Snate@binkert.org            # Iterative over all the multiple transitions that share
13256657Snate@binkert.org            # the same code
13266657Snate@binkert.org            for trans in transitions:
13276657Snate@binkert.org                code('  case HASH_FUN($trans):')
132810305Snilay@cs.wisc.edu            code('    $case\n')
13296657Snate@binkert.org
13306657Snate@binkert.org        code('''
13316657Snate@binkert.org      default:
13327805Snilay@cs.wisc.edu        fatal("Invalid transition\\n"
13338159SBrad.Beckmann@amd.com              "%s time: %d addr: %s event: %s state: %s\\n",
13349465Snilay@cs.wisc.edu              name(), curCycle(), addr, event, state);
13356657Snate@binkert.org    }
133610305Snilay@cs.wisc.edu
13376657Snate@binkert.org    return TransitionResult_Valid;
13386657Snate@binkert.org}
13396657Snate@binkert.org''')
13406657Snate@binkert.org        code.write(path, "%s_Transitions.cc" % self.ident)
13416657Snate@binkert.org
13426657Snate@binkert.org
13436657Snate@binkert.org    # **************************
13446657Snate@binkert.org    # ******* HTML Files *******
13456657Snate@binkert.org    # **************************
13467007Snate@binkert.org    def frameRef(self, click_href, click_target, over_href, over_num, text):
13476999Snate@binkert.org        code = self.symtab.codeFormatter(fix_newlines=False)
13487007Snate@binkert.org        code("""<A href=\"$click_href\" target=\"$click_target\" onmouseover=\"
13497007Snate@binkert.org    if (parent.frames[$over_num].location != parent.location + '$over_href') {
13507007Snate@binkert.org        parent.frames[$over_num].location='$over_href'
13517007Snate@binkert.org    }\">
13527007Snate@binkert.org    ${{html.formatShorthand(text)}}
13537007Snate@binkert.org    </A>""")
13546657Snate@binkert.org        return str(code)
13556657Snate@binkert.org
13566657Snate@binkert.org    def writeHTMLFiles(self, path):
13576657Snate@binkert.org        # Create table with no row hilighted
13586657Snate@binkert.org        self.printHTMLTransitions(path, None)
13596657Snate@binkert.org
13606657Snate@binkert.org        # Generate transition tables
13616657Snate@binkert.org        for state in self.states.itervalues():
13626657Snate@binkert.org            self.printHTMLTransitions(path, state)
13636657Snate@binkert.org
13646657Snate@binkert.org        # Generate action descriptions
13656657Snate@binkert.org        for action in self.actions.itervalues():
13666657Snate@binkert.org            name = "%s_action_%s.html" % (self.ident, action.ident)
13676657Snate@binkert.org            code = html.createSymbol(action, "Action")
13686657Snate@binkert.org            code.write(path, name)
13696657Snate@binkert.org
13706657Snate@binkert.org        # Generate state descriptions
13716657Snate@binkert.org        for state in self.states.itervalues():
13726657Snate@binkert.org            name = "%s_State_%s.html" % (self.ident, state.ident)
13736657Snate@binkert.org            code = html.createSymbol(state, "State")
13746657Snate@binkert.org            code.write(path, name)
13756657Snate@binkert.org
13766657Snate@binkert.org        # Generate event descriptions
13776657Snate@binkert.org        for event in self.events.itervalues():
13786657Snate@binkert.org            name = "%s_Event_%s.html" % (self.ident, event.ident)
13796657Snate@binkert.org            code = html.createSymbol(event, "Event")
13806657Snate@binkert.org            code.write(path, name)
13816657Snate@binkert.org
13826657Snate@binkert.org    def printHTMLTransitions(self, path, active_state):
13836999Snate@binkert.org        code = self.symtab.codeFormatter()
13846657Snate@binkert.org
13856657Snate@binkert.org        code('''
13867007Snate@binkert.org<HTML>
13877007Snate@binkert.org<BODY link="blue" vlink="blue">
13886657Snate@binkert.org
13896657Snate@binkert.org<H1 align="center">${{html.formatShorthand(self.short)}}:
13906657Snate@binkert.org''')
13916657Snate@binkert.org        code.indent()
13926657Snate@binkert.org        for i,machine in enumerate(self.symtab.getAllType(StateMachine)):
13936657Snate@binkert.org            mid = machine.ident
13946657Snate@binkert.org            if i != 0:
13956657Snate@binkert.org                extra = " - "
13966657Snate@binkert.org            else:
13976657Snate@binkert.org                extra = ""
13986657Snate@binkert.org            if machine == self:
13996657Snate@binkert.org                code('$extra$mid')
14006657Snate@binkert.org            else:
14016657Snate@binkert.org                code('$extra<A target="Table" href="${mid}_table.html">$mid</A>')
14026657Snate@binkert.org        code.dedent()
14036657Snate@binkert.org
14046657Snate@binkert.org        code("""
14056657Snate@binkert.org</H1>
14066657Snate@binkert.org
14076657Snate@binkert.org<TABLE border=1>
14086657Snate@binkert.org<TR>
14096657Snate@binkert.org  <TH> </TH>
14106657Snate@binkert.org""")
14116657Snate@binkert.org
14126657Snate@binkert.org        for event in self.events.itervalues():
14136657Snate@binkert.org            href = "%s_Event_%s.html" % (self.ident, event.ident)
14146657Snate@binkert.org            ref = self.frameRef(href, "Status", href, "1", event.short)
14156657Snate@binkert.org            code('<TH bgcolor=white>$ref</TH>')
14166657Snate@binkert.org
14176657Snate@binkert.org        code('</TR>')
14186657Snate@binkert.org        # -- Body of table
14196657Snate@binkert.org        for state in self.states.itervalues():
14206657Snate@binkert.org            # -- Each row
14216657Snate@binkert.org            if state == active_state:
14226657Snate@binkert.org                color = "yellow"
14236657Snate@binkert.org            else:
14246657Snate@binkert.org                color = "white"
14256657Snate@binkert.org
14266657Snate@binkert.org            click = "%s_table_%s.html" % (self.ident, state.ident)
14276657Snate@binkert.org            over = "%s_State_%s.html" % (self.ident, state.ident)
14286657Snate@binkert.org            text = html.formatShorthand(state.short)
14296657Snate@binkert.org            ref = self.frameRef(click, "Table", over, "1", state.short)
14306657Snate@binkert.org            code('''
14316657Snate@binkert.org<TR>
14326657Snate@binkert.org  <TH bgcolor=$color>$ref</TH>
14336657Snate@binkert.org''')
14346657Snate@binkert.org
14356657Snate@binkert.org            # -- One column for each event
14366657Snate@binkert.org            for event in self.events.itervalues():
14376657Snate@binkert.org                trans = self.table.get((state,event), None)
14386657Snate@binkert.org                if trans is None:
14396657Snate@binkert.org                    # This is the no transition case
14406657Snate@binkert.org                    if state == active_state:
14416657Snate@binkert.org                        color = "#C0C000"
14426657Snate@binkert.org                    else:
14436657Snate@binkert.org                        color = "lightgrey"
14446657Snate@binkert.org
14456657Snate@binkert.org                    code('<TD bgcolor=$color>&nbsp;</TD>')
14466657Snate@binkert.org                    continue
14476657Snate@binkert.org
14486657Snate@binkert.org                next = trans.nextState
14496657Snate@binkert.org                stall_action = False
14506657Snate@binkert.org
14516657Snate@binkert.org                # -- Get the actions
14526657Snate@binkert.org                for action in trans.actions:
14536657Snate@binkert.org                    if action.ident == "z_stall" or \
14546657Snate@binkert.org                       action.ident == "zz_recycleMandatoryQueue":
14556657Snate@binkert.org                        stall_action = True
14566657Snate@binkert.org
14576657Snate@binkert.org                # -- Print out "actions/next-state"
14586657Snate@binkert.org                if stall_action:
14596657Snate@binkert.org                    if state == active_state:
14606657Snate@binkert.org                        color = "#C0C000"
14616657Snate@binkert.org                    else:
14626657Snate@binkert.org                        color = "lightgrey"
14636657Snate@binkert.org
14646657Snate@binkert.org                elif active_state and next.ident == active_state.ident:
14656657Snate@binkert.org                    color = "aqua"
14666657Snate@binkert.org                elif state == active_state:
14676657Snate@binkert.org                    color = "yellow"
14686657Snate@binkert.org                else:
14696657Snate@binkert.org                    color = "white"
14706657Snate@binkert.org
14716657Snate@binkert.org                code('<TD bgcolor=$color>')
14726657Snate@binkert.org                for action in trans.actions:
14736657Snate@binkert.org                    href = "%s_action_%s.html" % (self.ident, action.ident)
14746657Snate@binkert.org                    ref = self.frameRef(href, "Status", href, "1",
14756657Snate@binkert.org                                        action.short)
14767007Snate@binkert.org                    code('  $ref')
14776657Snate@binkert.org                if next != state:
14786657Snate@binkert.org                    if trans.actions:
14796657Snate@binkert.org                        code('/')
14806657Snate@binkert.org                    click = "%s_table_%s.html" % (self.ident, next.ident)
14816657Snate@binkert.org                    over = "%s_State_%s.html" % (self.ident, next.ident)
14826657Snate@binkert.org                    ref = self.frameRef(click, "Table", over, "1", next.short)
14836657Snate@binkert.org                    code("$ref")
14847007Snate@binkert.org                code("</TD>")
14856657Snate@binkert.org
14866657Snate@binkert.org            # -- Each row
14876657Snate@binkert.org            if state == active_state:
14886657Snate@binkert.org                color = "yellow"
14896657Snate@binkert.org            else:
14906657Snate@binkert.org                color = "white"
14916657Snate@binkert.org
14926657Snate@binkert.org            click = "%s_table_%s.html" % (self.ident, state.ident)
14936657Snate@binkert.org            over = "%s_State_%s.html" % (self.ident, state.ident)
14946657Snate@binkert.org            ref = self.frameRef(click, "Table", over, "1", state.short)
14956657Snate@binkert.org            code('''
14966657Snate@binkert.org  <TH bgcolor=$color>$ref</TH>
14976657Snate@binkert.org</TR>
14986657Snate@binkert.org''')
14996657Snate@binkert.org        code('''
15007007Snate@binkert.org<!- Column footer->
15016657Snate@binkert.org<TR>
15026657Snate@binkert.org  <TH> </TH>
15036657Snate@binkert.org''')
15046657Snate@binkert.org
15056657Snate@binkert.org        for event in self.events.itervalues():
15066657Snate@binkert.org            href = "%s_Event_%s.html" % (self.ident, event.ident)
15076657Snate@binkert.org            ref = self.frameRef(href, "Status", href, "1", event.short)
15086657Snate@binkert.org            code('<TH bgcolor=white>$ref</TH>')
15096657Snate@binkert.org        code('''
15106657Snate@binkert.org</TR>
15116657Snate@binkert.org</TABLE>
15126657Snate@binkert.org</BODY></HTML>
15136657Snate@binkert.org''')
15146657Snate@binkert.org
15156657Snate@binkert.org
15166657Snate@binkert.org        if active_state:
15176657Snate@binkert.org            name = "%s_table_%s.html" % (self.ident, active_state.ident)
15186657Snate@binkert.org        else:
15196657Snate@binkert.org            name = "%s_table.html" % self.ident
15206657Snate@binkert.org        code.write(path, name)
15216657Snate@binkert.org
15226657Snate@binkert.org__all__ = [ "StateMachine" ]
1523