StateMachine.py revision 6793
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 286657Snate@binkert.orgfrom m5.util import code_formatter, 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 336657Snate@binkert.org 346657Snate@binkert.orgclass StateMachine(Symbol): 356657Snate@binkert.org def __init__(self, symtab, ident, location, pairs, config_parameters): 366657Snate@binkert.org super(StateMachine, self).__init__(symtab, ident, location, pairs) 376657Snate@binkert.org self.table = None 386657Snate@binkert.org self.config_parameters = config_parameters 396657Snate@binkert.org for param in config_parameters: 406657Snate@binkert.org var = Var(symtab, param.name, location, param.type_ast.type, 416657Snate@binkert.org "m_%s" % param.name, {}, self) 426657Snate@binkert.org self.symtab.registerSym(param.name, var) 436657Snate@binkert.org 446657Snate@binkert.org self.states = orderdict() 456657Snate@binkert.org self.events = orderdict() 466657Snate@binkert.org self.actions = orderdict() 476657Snate@binkert.org self.transitions = [] 486657Snate@binkert.org self.in_ports = [] 496657Snate@binkert.org self.functions = [] 506657Snate@binkert.org self.objects = [] 516657Snate@binkert.org 526657Snate@binkert.org self.message_buffer_names = [] 536657Snate@binkert.org 546657Snate@binkert.org def __repr__(self): 556657Snate@binkert.org return "[StateMachine: %s]" % self.ident 566657Snate@binkert.org 576657Snate@binkert.org def addState(self, state): 586657Snate@binkert.org assert self.table is None 596657Snate@binkert.org self.states[state.ident] = state 606657Snate@binkert.org 616657Snate@binkert.org def addEvent(self, event): 626657Snate@binkert.org assert self.table is None 636657Snate@binkert.org self.events[event.ident] = event 646657Snate@binkert.org 656657Snate@binkert.org def addAction(self, action): 666657Snate@binkert.org assert self.table is None 676657Snate@binkert.org 686657Snate@binkert.org # Check for duplicate action 696657Snate@binkert.org for other in self.actions.itervalues(): 706657Snate@binkert.org if action.ident == other.ident: 716779SBrad.Beckmann@amd.com action.warning("Duplicate action definition: %s" % action.ident) 726657Snate@binkert.org action.error("Duplicate action definition: %s" % action.ident) 736657Snate@binkert.org if action.short == other.short: 746657Snate@binkert.org other.warning("Duplicate action shorthand: %s" % other.ident) 756657Snate@binkert.org other.warning(" shorthand = %s" % other.short) 766657Snate@binkert.org action.warning("Duplicate action shorthand: %s" % action.ident) 776657Snate@binkert.org action.error(" shorthand = %s" % action.short) 786657Snate@binkert.org 796657Snate@binkert.org self.actions[action.ident] = action 806657Snate@binkert.org 816657Snate@binkert.org def addTransition(self, trans): 826657Snate@binkert.org assert self.table is None 836657Snate@binkert.org self.transitions.append(trans) 846657Snate@binkert.org 856657Snate@binkert.org def addInPort(self, var): 866657Snate@binkert.org self.in_ports.append(var) 876657Snate@binkert.org 886657Snate@binkert.org def addFunc(self, func): 896657Snate@binkert.org # register func in the symbol table 906657Snate@binkert.org self.symtab.registerSym(str(func), func) 916657Snate@binkert.org self.functions.append(func) 926657Snate@binkert.org 936657Snate@binkert.org def addObject(self, obj): 946657Snate@binkert.org self.objects.append(obj) 956657Snate@binkert.org 966657Snate@binkert.org # Needs to be called before accessing the table 976657Snate@binkert.org def buildTable(self): 986657Snate@binkert.org assert self.table is None 996657Snate@binkert.org 1006657Snate@binkert.org table = {} 1016657Snate@binkert.org 1026657Snate@binkert.org for trans in self.transitions: 1036657Snate@binkert.org # Track which actions we touch so we know if we use them 1046657Snate@binkert.org # all -- really this should be done for all symbols as 1056657Snate@binkert.org # part of the symbol table, then only trigger it for 1066657Snate@binkert.org # Actions, States, Events, etc. 1076657Snate@binkert.org 1086657Snate@binkert.org for action in trans.actions: 1096657Snate@binkert.org action.used = True 1106657Snate@binkert.org 1116657Snate@binkert.org index = (trans.state, trans.event) 1126657Snate@binkert.org if index in table: 1136657Snate@binkert.org table[index].warning("Duplicate transition: %s" % table[index]) 1146657Snate@binkert.org trans.error("Duplicate transition: %s" % trans) 1156657Snate@binkert.org table[index] = trans 1166657Snate@binkert.org 1176657Snate@binkert.org # Look at all actions to make sure we used them all 1186657Snate@binkert.org for action in self.actions.itervalues(): 1196657Snate@binkert.org if not action.used: 1206657Snate@binkert.org error_msg = "Unused action: %s" % action.ident 1216657Snate@binkert.org if "desc" in action: 1226657Snate@binkert.org error_msg += ", " + action.desc 1236657Snate@binkert.org action.warning(error_msg) 1246657Snate@binkert.org self.table = table 1256657Snate@binkert.org 1266657Snate@binkert.org def writeCodeFiles(self, path): 1276657Snate@binkert.org self.printControllerHH(path) 1286657Snate@binkert.org self.printControllerCC(path) 1296657Snate@binkert.org self.printCSwitch(path) 1306657Snate@binkert.org self.printCWakeup(path) 1316657Snate@binkert.org self.printProfilerCC(path) 1326657Snate@binkert.org self.printProfilerHH(path) 1336657Snate@binkert.org 1346657Snate@binkert.org for func in self.functions: 1356657Snate@binkert.org func.writeCodeFiles(path) 1366657Snate@binkert.org 1376657Snate@binkert.org def printControllerHH(self, path): 1386657Snate@binkert.org '''Output the method declarations for the class declaration''' 1396657Snate@binkert.org code = code_formatter() 1406657Snate@binkert.org ident = self.ident 1416657Snate@binkert.org c_ident = "%s_Controller" % self.ident 1426657Snate@binkert.org 1436657Snate@binkert.org self.message_buffer_names = [] 1446657Snate@binkert.org 1456657Snate@binkert.org code(''' 1466657Snate@binkert.org/** \\file $ident.hh 1476657Snate@binkert.org * 1486657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__ 1496657Snate@binkert.org * Created by slicc definition of Module "${{self.short}}" 1506657Snate@binkert.org */ 1516657Snate@binkert.org 1526657Snate@binkert.org#ifndef ${ident}_CONTROLLER_H 1536657Snate@binkert.org#define ${ident}_CONTROLLER_H 1546657Snate@binkert.org 1556657Snate@binkert.org#include "mem/ruby/common/Global.hh" 1566657Snate@binkert.org#include "mem/ruby/common/Consumer.hh" 1576657Snate@binkert.org#include "mem/ruby/slicc_interface/AbstractController.hh" 1586657Snate@binkert.org#include "mem/protocol/TransitionResult.hh" 1596657Snate@binkert.org#include "mem/protocol/Types.hh" 1606657Snate@binkert.org#include "mem/protocol/${ident}_Profiler.hh" 1616657Snate@binkert.org''') 1626657Snate@binkert.org 1636657Snate@binkert.org seen_types = set() 1646657Snate@binkert.org for var in self.objects: 1656793SBrad.Beckmann@amd.com if var.type.ident not in seen_types and not var.type.isPrimitive: 1666657Snate@binkert.org code('#include "mem/protocol/${{var.type.c_ident}}.hh"') 1676657Snate@binkert.org seen_types.add(var.type.ident) 1686657Snate@binkert.org 1696657Snate@binkert.org # for adding information to the protocol debug trace 1706657Snate@binkert.org code(''' 1716657Snate@binkert.orgextern stringstream ${ident}_transitionComment; 1726657Snate@binkert.org 1736657Snate@binkert.orgclass $c_ident : public AbstractController { 1746657Snate@binkert.org#ifdef CHECK_COHERENCE 1756657Snate@binkert.org#endif /* CHECK_COHERENCE */ 1766657Snate@binkert.orgpublic: 1776657Snate@binkert.org $c_ident(const string & name); 1786657Snate@binkert.org static int getNumControllers(); 1796657Snate@binkert.org void init(Network* net_ptr, const vector<string> & argv); 1806657Snate@binkert.org MessageBuffer* getMandatoryQueue() const; 1816657Snate@binkert.org const int & getVersion() const; 1826657Snate@binkert.org const string toString() const; 1836657Snate@binkert.org const string getName() const; 1846657Snate@binkert.org const MachineType getMachineType() const; 1856657Snate@binkert.org void print(ostream& out) const; 1866657Snate@binkert.org void printConfig(ostream& out) const; 1876657Snate@binkert.org void wakeup(); 1886657Snate@binkert.org void set_atomic(Address addr); 1896657Snate@binkert.org void started_writes(); 1906657Snate@binkert.org void clear_atomic(); 1916657Snate@binkert.org void printStats(ostream& out) const { s_profiler.dumpStats(out); } 1926657Snate@binkert.org void clearStats() { s_profiler.clearStats(); } 1936657Snate@binkert.orgprivate: 1946657Snate@binkert.org''') 1956657Snate@binkert.org 1966657Snate@binkert.org code.indent() 1976657Snate@binkert.org # added by SS 1986657Snate@binkert.org for param in self.config_parameters: 1996657Snate@binkert.org code('int m_${{param.ident}};') 2006657Snate@binkert.org 2016657Snate@binkert.org if self.ident == "L1Cache": 2026657Snate@binkert.org code(''' 2036657Snate@binkert.orgint servicing_atomic; 2046657Snate@binkert.orgbool started_receiving_writes; 2056657Snate@binkert.orgAddress locked_read_request1; 2066657Snate@binkert.orgAddress locked_read_request2; 2076657Snate@binkert.orgAddress locked_read_request3; 2086657Snate@binkert.orgAddress locked_read_request4; 2096657Snate@binkert.orgint read_counter; 2106657Snate@binkert.org''') 2116657Snate@binkert.org 2126657Snate@binkert.org code(''' 2136657Snate@binkert.orgint m_number_of_TBEs; 2146657Snate@binkert.org 2156657Snate@binkert.orgTransitionResult doTransition(${ident}_Event event, ${ident}_State state, const Address& addr); // in ${ident}_Transitions.cc 2166657Snate@binkert.orgTransitionResult doTransitionWorker(${ident}_Event event, ${ident}_State state, ${ident}_State& next_state, const Address& addr); // in ${ident}_Transitions.cc 2176657Snate@binkert.orgstring m_name; 2186657Snate@binkert.orgint m_transitions_per_cycle; 2196657Snate@binkert.orgint m_buffer_size; 2206657Snate@binkert.orgint m_recycle_latency; 2216657Snate@binkert.orgmap< string, string > m_cfg; 2226657Snate@binkert.orgNodeID m_version; 2236657Snate@binkert.orgNetwork* m_net_ptr; 2246657Snate@binkert.orgMachineID m_machineID; 2256657Snate@binkert.org${ident}_Profiler s_profiler; 2266657Snate@binkert.orgstatic int m_num_controllers; 2276657Snate@binkert.org// Internal functions 2286657Snate@binkert.org''') 2296657Snate@binkert.org 2306657Snate@binkert.org for func in self.functions: 2316657Snate@binkert.org proto = func.prototype 2326657Snate@binkert.org if proto: 2336657Snate@binkert.org code('$proto') 2346657Snate@binkert.org 2356657Snate@binkert.org code(''' 2366657Snate@binkert.org 2376657Snate@binkert.org// Actions 2386657Snate@binkert.org''') 2396657Snate@binkert.org for action in self.actions.itervalues(): 2406657Snate@binkert.org code('/** \\brief ${{action.desc}} */') 2416657Snate@binkert.org code('void ${{action.ident}}(const Address& addr);') 2426657Snate@binkert.org 2436657Snate@binkert.org # the controller internal variables 2446657Snate@binkert.org code(''' 2456657Snate@binkert.org 2466657Snate@binkert.org// Object 2476657Snate@binkert.org''') 2486657Snate@binkert.org for var in self.objects: 2496657Snate@binkert.org th = var.get("template_hack", "") 2506657Snate@binkert.org code('${{var.type.c_ident}}$th* m_${{var.c_ident}}_ptr;') 2516657Snate@binkert.org 2526657Snate@binkert.org if var.type.ident == "MessageBuffer": 2536657Snate@binkert.org self.message_buffer_names.append("m_%s_ptr" % var.c_ident) 2546657Snate@binkert.org 2556657Snate@binkert.org code.dedent() 2566657Snate@binkert.org code('};') 2576657Snate@binkert.org code('#endif // ${ident}_CONTROLLER_H') 2586657Snate@binkert.org code.write(path, '%s.hh' % c_ident) 2596657Snate@binkert.org 2606657Snate@binkert.org def printControllerCC(self, path): 2616657Snate@binkert.org '''Output the actions for performing the actions''' 2626657Snate@binkert.org 2636657Snate@binkert.org code = code_formatter() 2646657Snate@binkert.org ident = self.ident 2656657Snate@binkert.org c_ident = "%s_Controller" % self.ident 2666657Snate@binkert.org 2676657Snate@binkert.org code(''' 2686657Snate@binkert.org/** \\file $ident.cc 2696657Snate@binkert.org * 2706657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__ 2716657Snate@binkert.org * Created by slicc definition of Module "${{self.short}}" 2726657Snate@binkert.org */ 2736657Snate@binkert.org 2746657Snate@binkert.org#include "mem/ruby/common/Global.hh" 2756657Snate@binkert.org#include "mem/ruby/slicc_interface/RubySlicc_includes.hh" 2766657Snate@binkert.org#include "mem/protocol/${ident}_Controller.hh" 2776657Snate@binkert.org#include "mem/protocol/${ident}_State.hh" 2786657Snate@binkert.org#include "mem/protocol/${ident}_Event.hh" 2796657Snate@binkert.org#include "mem/protocol/Types.hh" 2806657Snate@binkert.org#include "mem/ruby/system/System.hh" 2816657Snate@binkert.org''') 2826657Snate@binkert.org 2836657Snate@binkert.org # include object classes 2846657Snate@binkert.org seen_types = set() 2856657Snate@binkert.org for var in self.objects: 2866793SBrad.Beckmann@amd.com if var.type.ident not in seen_types and not var.type.isPrimitive: 2876657Snate@binkert.org code('#include "mem/protocol/${{var.type.c_ident}}.hh"') 2886657Snate@binkert.org seen_types.add(var.type.ident) 2896657Snate@binkert.org 2906657Snate@binkert.org code(''' 2916657Snate@binkert.orgint $c_ident::m_num_controllers = 0; 2926657Snate@binkert.org 2936657Snate@binkert.orgstringstream ${ident}_transitionComment; 2946657Snate@binkert.org#define APPEND_TRANSITION_COMMENT(str) (${ident}_transitionComment << str) 2956657Snate@binkert.org/** \\brief constructor */ 2966657Snate@binkert.org$c_ident::$c_ident(const string &name) 2976657Snate@binkert.org : m_name(name) 2986657Snate@binkert.org{ 2996657Snate@binkert.org''') 3006657Snate@binkert.org code.indent() 3016657Snate@binkert.org if self.ident == "L1Cache": 3026657Snate@binkert.org code(''' 3036657Snate@binkert.orgservicing_atomic = 0; 3046657Snate@binkert.orgstarted_receiving_writes = false; 3056657Snate@binkert.orglocked_read_request1 = Address(-1); 3066657Snate@binkert.orglocked_read_request2 = Address(-1); 3076657Snate@binkert.orglocked_read_request3 = Address(-1); 3086657Snate@binkert.orglocked_read_request4 = Address(-1); 3096657Snate@binkert.orgread_counter = 0; 3106657Snate@binkert.org''') 3116657Snate@binkert.org 3126657Snate@binkert.org code('m_num_controllers++;') 3136657Snate@binkert.org for var in self.objects: 3146657Snate@binkert.org if var.ident.find("mandatoryQueue") >= 0: 3156657Snate@binkert.org code('m_${{var.c_ident}}_ptr = new ${{var.type.c_ident}}();') 3166657Snate@binkert.org 3176657Snate@binkert.org code.dedent() 3186657Snate@binkert.org code(''' 3196657Snate@binkert.org} 3206657Snate@binkert.org 3216657Snate@binkert.orgvoid $c_ident::init(Network *net_ptr, const vector<string> &argv) 3226657Snate@binkert.org{ 3236657Snate@binkert.org for (size_t i = 0; i < argv.size(); i += 2) { 3246657Snate@binkert.org if (argv[i] == "version") 3256657Snate@binkert.org m_version = atoi(argv[i+1].c_str()); 3266657Snate@binkert.org else if (argv[i] == "transitions_per_cycle") 3276657Snate@binkert.org m_transitions_per_cycle = atoi(argv[i+1].c_str()); 3286657Snate@binkert.org else if (argv[i] == "buffer_size") 3296657Snate@binkert.org m_buffer_size = atoi(argv[i+1].c_str()); 3306657Snate@binkert.org else if (argv[i] == "recycle_latency") 3316657Snate@binkert.org m_recycle_latency = atoi(argv[i+1].c_str()); 3326657Snate@binkert.org else if (argv[i] == "number_of_TBEs") 3336657Snate@binkert.org m_number_of_TBEs = atoi(argv[i+1].c_str()); 3346657Snate@binkert.org''') 3356657Snate@binkert.org 3366657Snate@binkert.org code.indent() 3376657Snate@binkert.org code.indent() 3386657Snate@binkert.org for param in self.config_parameters: 3396657Snate@binkert.org code('else if (argv[i] == "${{param.name}}")') 3406657Snate@binkert.org if param.type_ast.type.ident == "int": 3416657Snate@binkert.org code(' m_${{param.name}} = atoi(argv[i+1].c_str());') 3426793SBrad.Beckmann@amd.com elif param.type_ast.type.ident == "bool": 3436793SBrad.Beckmann@amd.com code(' m_${{param.name}} = string_to_bool(argv[i+1]);') 3446657Snate@binkert.org else: 3456793SBrad.Beckmann@amd.com self.error("only int and bool parameters are "\ 3466793SBrad.Beckmann@amd.com "currently supported") 3476657Snate@binkert.org code.dedent() 3486657Snate@binkert.org code.dedent() 3496657Snate@binkert.org code(''' 3506657Snate@binkert.org } 3516657Snate@binkert.org 3526657Snate@binkert.org m_net_ptr = net_ptr; 3536657Snate@binkert.org m_machineID.type = MachineType_${ident}; 3546657Snate@binkert.org m_machineID.num = m_version; 3556657Snate@binkert.org for (size_t i = 0; i < argv.size(); i += 2) { 3566657Snate@binkert.org if (argv[i] != "version") 3576657Snate@binkert.org m_cfg[argv[i]] = argv[i+1]; 3586657Snate@binkert.org } 3596657Snate@binkert.org 3606657Snate@binkert.org // Objects 3616657Snate@binkert.org s_profiler.setVersion(m_version); 3626657Snate@binkert.org''') 3636657Snate@binkert.org 3646657Snate@binkert.org code.indent() 3656657Snate@binkert.org for var in self.objects: 3666657Snate@binkert.org vtype = var.type 3676657Snate@binkert.org vid = "m_%s_ptr" % var.c_ident 3686657Snate@binkert.org if "network" not in var: 3696657Snate@binkert.org # Not a network port object 3706657Snate@binkert.org if "primitive" in vtype: 3716657Snate@binkert.org code('$vid = new ${{vtype.c_ident}};') 3726657Snate@binkert.org if "default" in var: 3736657Snate@binkert.org code('(*$vid) = ${{var["default"]}};') 3746657Snate@binkert.org else: 3756657Snate@binkert.org # Normal Object 3766657Snate@binkert.org # added by SS 3776657Snate@binkert.org if "factory" in var: 3786657Snate@binkert.org code('$vid = ${{var["factory"]}};') 3796657Snate@binkert.org elif var.ident.find("mandatoryQueue") < 0: 3806657Snate@binkert.org th = var.get("template_hack", "") 3816657Snate@binkert.org expr = "%s = new %s%s" % (vid, vtype.c_ident, th) 3826657Snate@binkert.org 3836657Snate@binkert.org args = "" 3846657Snate@binkert.org if "non_obj" not in vtype and not vtype.isEnumeration: 3856657Snate@binkert.org if expr.find("TBETable") >= 0: 3866657Snate@binkert.org args = "m_number_of_TBEs" 3876657Snate@binkert.org else: 3886657Snate@binkert.org args = var.get("constructor_hack", "") 3896657Snate@binkert.org args = "(%s)" % args 3906657Snate@binkert.org 3916657Snate@binkert.org code('$expr$args;') 3926657Snate@binkert.org else: 3936657Snate@binkert.org code(';') 3946657Snate@binkert.org 3956657Snate@binkert.org code('assert($vid != NULL);') 3966657Snate@binkert.org 3976657Snate@binkert.org if "default" in var: 3986657Snate@binkert.org code('(*$vid) = ${{var["default"]}}; // Object default') 3996657Snate@binkert.org elif "default" in vtype: 4006657Snate@binkert.org code('(*$vid) = ${{vtype["default"]}}; // Type ${{vtype.ident}} default') 4016657Snate@binkert.org 4026657Snate@binkert.org # Set ordering 4036657Snate@binkert.org if "ordered" in var and "trigger_queue" not in var: 4046657Snate@binkert.org # A buffer 4056657Snate@binkert.org code('$vid->setOrdering(${{var["ordered"]}});') 4066657Snate@binkert.org 4076657Snate@binkert.org # Set randomization 4086657Snate@binkert.org if "random" in var: 4096657Snate@binkert.org # A buffer 4106657Snate@binkert.org code('$vid->setRandomization(${{var["random"]}});') 4116657Snate@binkert.org 4126657Snate@binkert.org # Set Priority 4136657Snate@binkert.org if vtype.isBuffer and \ 4146657Snate@binkert.org "rank" in var and "trigger_queue" not in var: 4156657Snate@binkert.org code('$vid->setPriority(${{var["rank"]}});') 4166657Snate@binkert.org else: 4176657Snate@binkert.org # Network port object 4186657Snate@binkert.org network = var["network"] 4196657Snate@binkert.org ordered = var["ordered"] 4206657Snate@binkert.org vnet = var["virtual_network"] 4216657Snate@binkert.org 4226657Snate@binkert.org assert var.machine is not None 4236657Snate@binkert.org code(''' 4246657Snate@binkert.org$vid = m_net_ptr->get${network}NetQueue(m_version+MachineType_base_number(string_to_MachineType("${{var.machine.ident}}")), $ordered, $vnet); 4256657Snate@binkert.org''') 4266657Snate@binkert.org 4276657Snate@binkert.org code('assert($vid != NULL);') 4286657Snate@binkert.org 4296657Snate@binkert.org # Set ordering 4306657Snate@binkert.org if "ordered" in var: 4316657Snate@binkert.org # A buffer 4326657Snate@binkert.org code('$vid->setOrdering(${{var["ordered"]}});') 4336657Snate@binkert.org 4346657Snate@binkert.org # Set randomization 4356657Snate@binkert.org if "random" in var: 4366657Snate@binkert.org # A buffer 4376657Snate@binkert.org code('$vid->setRandomization(${{var["random"]}})') 4386657Snate@binkert.org 4396657Snate@binkert.org # Set Priority 4406657Snate@binkert.org if "rank" in var: 4416657Snate@binkert.org code('$vid->setPriority(${{var["rank"]}})') 4426657Snate@binkert.org 4436657Snate@binkert.org # Set buffer size 4446657Snate@binkert.org if vtype.isBuffer: 4456657Snate@binkert.org code(''' 4466657Snate@binkert.orgif (m_buffer_size > 0) { 4476657Snate@binkert.org $vid->setSize(m_buffer_size); 4486657Snate@binkert.org} 4496657Snate@binkert.org''') 4506657Snate@binkert.org 4516657Snate@binkert.org # set description (may be overriden later by port def) 4526657Snate@binkert.org code('$vid->setDescription("[Version " + int_to_string(m_version) + ", ${ident}, name=${{var.c_ident}}]");') 4536657Snate@binkert.org 4546657Snate@binkert.org # Set the queue consumers 4556657Snate@binkert.org code.insert_newline() 4566657Snate@binkert.org for port in self.in_ports: 4576657Snate@binkert.org code('${{port.code}}.setConsumer(this);') 4586657Snate@binkert.org 4596657Snate@binkert.org # Set the queue descriptions 4606657Snate@binkert.org code.insert_newline() 4616657Snate@binkert.org for port in self.in_ports: 4626657Snate@binkert.org code('${{port.code}}.setDescription("[Version " + int_to_string(m_version) + ", $ident, $port]");') 4636657Snate@binkert.org 4646657Snate@binkert.org # Initialize the transition profiling 4656657Snate@binkert.org code.insert_newline() 4666657Snate@binkert.org for trans in self.transitions: 4676657Snate@binkert.org # Figure out if we stall 4686657Snate@binkert.org stall = False 4696657Snate@binkert.org for action in trans.actions: 4706657Snate@binkert.org if action.ident == "z_stall": 4716657Snate@binkert.org stall = True 4726657Snate@binkert.org 4736657Snate@binkert.org # Only possible if it is not a 'z' case 4746657Snate@binkert.org if not stall: 4756657Snate@binkert.org state = "%s_State_%s" % (self.ident, trans.state.ident) 4766657Snate@binkert.org event = "%s_Event_%s" % (self.ident, trans.event.ident) 4776657Snate@binkert.org code('s_profiler.possibleTransition($state, $event);') 4786657Snate@binkert.org 4796657Snate@binkert.org # added by SS to initialize recycle_latency of message buffers 4806657Snate@binkert.org for buf in self.message_buffer_names: 4816657Snate@binkert.org code("$buf->setRecycleLatency(m_recycle_latency);") 4826657Snate@binkert.org 4836657Snate@binkert.org code.dedent() 4846657Snate@binkert.org code('}') 4856657Snate@binkert.org 4866657Snate@binkert.org has_mandatory_q = False 4876657Snate@binkert.org for port in self.in_ports: 4886657Snate@binkert.org if port.code.find("mandatoryQueue_ptr") >= 0: 4896657Snate@binkert.org has_mandatory_q = True 4906657Snate@binkert.org 4916657Snate@binkert.org if has_mandatory_q: 4926657Snate@binkert.org mq_ident = "m_%s_mandatoryQueue_ptr" % self.ident 4936657Snate@binkert.org else: 4946657Snate@binkert.org mq_ident = "NULL" 4956657Snate@binkert.org 4966657Snate@binkert.org code(''' 4976657Snate@binkert.orgint $c_ident::getNumControllers() { 4986657Snate@binkert.org return m_num_controllers; 4996657Snate@binkert.org} 5006657Snate@binkert.org 5016657Snate@binkert.orgMessageBuffer* $c_ident::getMandatoryQueue() const { 5026657Snate@binkert.org return $mq_ident; 5036657Snate@binkert.org} 5046657Snate@binkert.org 5056657Snate@binkert.orgconst int & $c_ident::getVersion() const{ 5066657Snate@binkert.org return m_version; 5076657Snate@binkert.org} 5086657Snate@binkert.org 5096657Snate@binkert.orgconst string $c_ident::toString() const{ 5106657Snate@binkert.org return "$c_ident"; 5116657Snate@binkert.org} 5126657Snate@binkert.org 5136657Snate@binkert.orgconst string $c_ident::getName() const{ 5146657Snate@binkert.org return m_name; 5156657Snate@binkert.org} 5166657Snate@binkert.orgconst MachineType $c_ident::getMachineType() const{ 5176657Snate@binkert.org return MachineType_${ident}; 5186657Snate@binkert.org} 5196657Snate@binkert.org 5206657Snate@binkert.orgvoid $c_ident::print(ostream& out) const { out << "[$c_ident " << m_version << "]"; } 5216657Snate@binkert.org 5226657Snate@binkert.orgvoid $c_ident::printConfig(ostream& out) const { 5236657Snate@binkert.org out << "$c_ident config: " << m_name << endl; 5246657Snate@binkert.org out << " version: " << m_version << endl; 5256657Snate@binkert.org for (map<string, string>::const_iterator it = m_cfg.begin(); it != m_cfg.end(); it++) { 5266657Snate@binkert.org out << " " << (*it).first << ": " << (*it).second << endl; 5276657Snate@binkert.org } 5286657Snate@binkert.org} 5296657Snate@binkert.org 5306657Snate@binkert.org// Actions 5316657Snate@binkert.org''') 5326657Snate@binkert.org 5336657Snate@binkert.org for action in self.actions.itervalues(): 5346657Snate@binkert.org if "c_code" not in action: 5356657Snate@binkert.org continue 5366657Snate@binkert.org 5376657Snate@binkert.org code(''' 5386657Snate@binkert.org/** \\brief ${{action.desc}} */ 5396657Snate@binkert.orgvoid $c_ident::${{action.ident}}(const Address& addr) 5406657Snate@binkert.org{ 5416657Snate@binkert.org DEBUG_MSG(GENERATED_COMP, HighPrio, "executing"); 5426657Snate@binkert.org ${{action["c_code"]}} 5436657Snate@binkert.org} 5446657Snate@binkert.org 5456657Snate@binkert.org''') 5466657Snate@binkert.org code.write(path, "%s.cc" % c_ident) 5476657Snate@binkert.org 5486657Snate@binkert.org def printCWakeup(self, path): 5496657Snate@binkert.org '''Output the wakeup loop for the events''' 5506657Snate@binkert.org 5516657Snate@binkert.org code = code_formatter() 5526657Snate@binkert.org ident = self.ident 5536657Snate@binkert.org 5546657Snate@binkert.org code(''' 5556657Snate@binkert.org// Auto generated C++ code started by $__file__:$__line__ 5566657Snate@binkert.org// ${ident}: ${{self.short}} 5576657Snate@binkert.org 5586657Snate@binkert.org#include "mem/ruby/common/Global.hh" 5596657Snate@binkert.org#include "mem/ruby/slicc_interface/RubySlicc_includes.hh" 5606657Snate@binkert.org#include "mem/protocol/${ident}_Controller.hh" 5616657Snate@binkert.org#include "mem/protocol/${ident}_State.hh" 5626657Snate@binkert.org#include "mem/protocol/${ident}_Event.hh" 5636657Snate@binkert.org#include "mem/protocol/Types.hh" 5646657Snate@binkert.org#include "mem/ruby/system/System.hh" 5656657Snate@binkert.org 5666657Snate@binkert.orgvoid ${ident}_Controller::wakeup() 5676657Snate@binkert.org{ 5686657Snate@binkert.org 5696657Snate@binkert.org int counter = 0; 5706657Snate@binkert.org while (true) { 5716657Snate@binkert.org // Some cases will put us into an infinite loop without this limit 5726657Snate@binkert.org assert(counter <= m_transitions_per_cycle); 5736657Snate@binkert.org if (counter == m_transitions_per_cycle) { 5746657Snate@binkert.org g_system_ptr->getProfiler()->controllerBusy(m_machineID); // Count how often we\'re fully utilized 5756657Snate@binkert.org g_eventQueue_ptr->scheduleEvent(this, 1); // Wakeup in another cycle and try again 5766657Snate@binkert.org break; 5776657Snate@binkert.org } 5786657Snate@binkert.org''') 5796657Snate@binkert.org 5806657Snate@binkert.org code.indent() 5816657Snate@binkert.org code.indent() 5826657Snate@binkert.org 5836657Snate@binkert.org # InPorts 5846657Snate@binkert.org # 5856657Snate@binkert.org # Find the position of the mandatory queue in the vector so 5866657Snate@binkert.org # that we can print it out first 5876657Snate@binkert.org 5886657Snate@binkert.org mandatory_q = None 5896657Snate@binkert.org if self.ident == "L1Cache": 5906657Snate@binkert.org for i,port in enumerate(self.in_ports): 5916657Snate@binkert.org assert "c_code_in_port" in port 5926657Snate@binkert.org if str(port).find("mandatoryQueue_in") >= 0: 5936657Snate@binkert.org assert mandatory_q is None 5946657Snate@binkert.org mandatory_q = port 5956657Snate@binkert.org 5966657Snate@binkert.org assert mandatory_q is not None 5976657Snate@binkert.org 5986657Snate@binkert.org # print out the mandatory queue here 5996657Snate@binkert.org port = mandatory_q 6006657Snate@binkert.org code('// ${ident}InPort $port') 6016657Snate@binkert.org output = port["c_code_in_port"] 6026657Snate@binkert.org 6036657Snate@binkert.org pos = output.find("TransitionResult result = doTransition((L1Cache_mandatory_request_type_to_event(((*in_msg_ptr)).m_Type)), L1Cache_getState(addr), addr);") 6046657Snate@binkert.org assert pos >= 0 6056657Snate@binkert.org atomics_string = ''' 6066657Snate@binkert.orgif ((((*in_msg_ptr)).m_Type) == CacheRequestType_ATOMIC) { 6076657Snate@binkert.org if (servicing_atomic == 0) { 6086657Snate@binkert.org if (locked_read_request1 == Address(-1)) { 6096657Snate@binkert.org assert(read_counter == 0); 6106657Snate@binkert.org locked_read_request1 = addr; 6116657Snate@binkert.org assert(read_counter == 0); 6126657Snate@binkert.org read_counter++; 6136657Snate@binkert.org } 6146657Snate@binkert.org else if (addr == locked_read_request1) { 6156657Snate@binkert.org ; // do nothing 6166657Snate@binkert.org } 6176657Snate@binkert.org else { 6186657Snate@binkert.org assert(0); // should never be here if servicing one request at a time 6196657Snate@binkert.org } 6206657Snate@binkert.org } 6216657Snate@binkert.org else if (!started_receiving_writes) { 6226657Snate@binkert.org if (servicing_atomic == 1) { 6236657Snate@binkert.org if (locked_read_request2 == Address(-1)) { 6246657Snate@binkert.org assert(locked_read_request1 != Address(-1)); 6256657Snate@binkert.org assert(read_counter == 1); 6266657Snate@binkert.org locked_read_request2 = addr; 6276657Snate@binkert.org assert(read_counter == 1); 6286657Snate@binkert.org read_counter++; 6296657Snate@binkert.org } 6306657Snate@binkert.org else if (addr == locked_read_request2) { 6316657Snate@binkert.org ; // do nothing 6326657Snate@binkert.org } 6336657Snate@binkert.org else { 6346657Snate@binkert.org assert(0); // should never be here if servicing one request at a time 6356657Snate@binkert.org } 6366657Snate@binkert.org } 6376657Snate@binkert.org else if (servicing_atomic == 2) { 6386657Snate@binkert.org if (locked_read_request3 == Address(-1)) { 6396657Snate@binkert.org assert(locked_read_request1 != Address(-1)); 6406657Snate@binkert.org assert(locked_read_request2 != Address(-1)); 6416657Snate@binkert.org assert(read_counter == 1); 6426657Snate@binkert.org locked_read_request3 = addr; 6436657Snate@binkert.org assert(read_counter == 2); 6446657Snate@binkert.org read_counter++; 6456657Snate@binkert.org } 6466657Snate@binkert.org else if (addr == locked_read_request3) { 6476657Snate@binkert.org ; // do nothing 6486657Snate@binkert.org } 6496657Snate@binkert.org else { 6506657Snate@binkert.org assert(0); // should never be here if servicing one request at a time 6516657Snate@binkert.org } 6526657Snate@binkert.org } 6536657Snate@binkert.org else if (servicing_atomic == 3) { 6546657Snate@binkert.org if (locked_read_request4 == Address(-1)) { 6556657Snate@binkert.org assert(locked_read_request1 != Address(-1)); 6566657Snate@binkert.org assert(locked_read_request2 != Address(-1)); 6576657Snate@binkert.org assert(locked_read_request3 != Address(-1)); 6586657Snate@binkert.org assert(read_counter == 1); 6596657Snate@binkert.org locked_read_request4 = addr; 6606657Snate@binkert.org assert(read_counter == 3); 6616657Snate@binkert.org read_counter++; 6626657Snate@binkert.org } 6636657Snate@binkert.org else if (addr == locked_read_request4) { 6646657Snate@binkert.org ; // do nothing 6656657Snate@binkert.org } 6666657Snate@binkert.org else { 6676657Snate@binkert.org assert(0); // should never be here if servicing one request at a time 6686657Snate@binkert.org } 6696657Snate@binkert.org } 6706657Snate@binkert.org else { 6716657Snate@binkert.org assert(0); 6726657Snate@binkert.org } 6736657Snate@binkert.org } 6746657Snate@binkert.org} 6756657Snate@binkert.orgelse { 6766657Snate@binkert.org if (servicing_atomic > 0) { 6776657Snate@binkert.org // reset 6786657Snate@binkert.org servicing_atomic = 0; 6796657Snate@binkert.org read_counter = 0; 6806657Snate@binkert.org started_receiving_writes = false; 6816657Snate@binkert.org locked_read_request1 = Address(-1); 6826657Snate@binkert.org locked_read_request2 = Address(-1); 6836657Snate@binkert.org locked_read_request3 = Address(-1); 6846657Snate@binkert.org locked_read_request4 = Address(-1); 6856657Snate@binkert.org } 6866657Snate@binkert.org} 6876657Snate@binkert.org''' 6886657Snate@binkert.org 6896657Snate@binkert.org output = output[:pos] + atomics_string + output[pos:] 6906657Snate@binkert.org code('$output') 6916657Snate@binkert.org 6926657Snate@binkert.org for port in self.in_ports: 6936657Snate@binkert.org # don't print out mandatory queue twice 6946657Snate@binkert.org if port == mandatory_q: 6956657Snate@binkert.org continue 6966657Snate@binkert.org 6976657Snate@binkert.org if ident == "L1Cache": 6986657Snate@binkert.org if str(port).find("forwardRequestNetwork_in") >= 0: 6996657Snate@binkert.org code(''' 7006657Snate@binkert.orgbool postpone = false; 7016657Snate@binkert.orgif ((((*m_L1Cache_forwardToCache_ptr)).isReady())) { 7026657Snate@binkert.org const RequestMsg* in_msg_ptr; 7036657Snate@binkert.org in_msg_ptr = dynamic_cast<const RequestMsg*>(((*m_L1Cache_forwardToCache_ptr)).peek()); 7046657Snate@binkert.org if ((((servicing_atomic == 1) && (locked_read_request1 == ((*in_msg_ptr)).m_Address)) || 7056657Snate@binkert.org ((servicing_atomic == 2) && (locked_read_request1 == ((*in_msg_ptr)).m_Address || locked_read_request2 == ((*in_msg_ptr)).m_Address)) || 7066657Snate@binkert.org ((servicing_atomic == 3) && (locked_read_request1 == ((*in_msg_ptr)).m_Address || locked_read_request2 == ((*in_msg_ptr)).m_Address || locked_read_request3 == ((*in_msg_ptr)).m_Address)) || 7076657Snate@binkert.org ((servicing_atomic == 4) && (locked_read_request1 == ((*in_msg_ptr)).m_Address || locked_read_request2 == ((*in_msg_ptr)).m_Address || locked_read_request3 == ((*in_msg_ptr)).m_Address || locked_read_request1 == ((*in_msg_ptr)).m_Address)))) { 7086657Snate@binkert.org postpone = true; 7096657Snate@binkert.org } 7106657Snate@binkert.org} 7116657Snate@binkert.orgif (!postpone) { 7126657Snate@binkert.org''') 7136657Snate@binkert.org code.indent() 7146657Snate@binkert.org code('// ${ident}InPort $port') 7156657Snate@binkert.org code('${{port["c_code_in_port"]}}') 7166657Snate@binkert.org code.dedent() 7176657Snate@binkert.org 7186657Snate@binkert.org if ident == "L1Cache": 7196657Snate@binkert.org if str(port).find("forwardRequestNetwork_in") >= 0: 7206657Snate@binkert.org code.dedent() 7216657Snate@binkert.org code('}') 7226657Snate@binkert.org code.indent() 7236657Snate@binkert.org code('') 7246657Snate@binkert.org 7256657Snate@binkert.org code.dedent() 7266657Snate@binkert.org code.dedent() 7276657Snate@binkert.org code(''' 7286657Snate@binkert.org break; // If we got this far, we have nothing left todo 7296657Snate@binkert.org } 7306657Snate@binkert.org} 7316657Snate@binkert.org''') 7326657Snate@binkert.org 7336657Snate@binkert.org if self.ident == "L1Cache": 7346657Snate@binkert.org code(''' 7356657Snate@binkert.orgvoid ${ident}_Controller::set_atomic(Address addr) 7366657Snate@binkert.org{ 7376657Snate@binkert.org servicing_atomic++; 7386657Snate@binkert.org} 7396657Snate@binkert.org 7406657Snate@binkert.orgvoid ${ident}_Controller::started_writes() 7416657Snate@binkert.org{ 7426657Snate@binkert.org started_receiving_writes = true; 7436657Snate@binkert.org} 7446657Snate@binkert.org 7456657Snate@binkert.orgvoid ${ident}_Controller::clear_atomic() 7466657Snate@binkert.org{ 7476657Snate@binkert.org assert(servicing_atomic > 0); 7486657Snate@binkert.org read_counter--; 7496657Snate@binkert.org servicing_atomic--; 7506657Snate@binkert.org if (read_counter == 0) { 7516657Snate@binkert.org servicing_atomic = 0; 7526657Snate@binkert.org started_receiving_writes = false; 7536657Snate@binkert.org locked_read_request1 = Address(-1); 7546657Snate@binkert.org locked_read_request2 = Address(-1); 7556657Snate@binkert.org locked_read_request3 = Address(-1); 7566657Snate@binkert.org locked_read_request4 = Address(-1); 7576657Snate@binkert.org } 7586657Snate@binkert.org} 7596657Snate@binkert.org''') 7606657Snate@binkert.org else: 7616657Snate@binkert.org code(''' 7626657Snate@binkert.orgvoid ${ident}_Controller::started_writes() 7636657Snate@binkert.org{ 7646657Snate@binkert.org assert(0); 7656657Snate@binkert.org} 7666657Snate@binkert.org 7676657Snate@binkert.orgvoid ${ident}_Controller::set_atomic(Address addr) 7686657Snate@binkert.org{ 7696657Snate@binkert.org assert(0); 7706657Snate@binkert.org} 7716657Snate@binkert.org 7726657Snate@binkert.orgvoid ${ident}_Controller::clear_atomic() 7736657Snate@binkert.org{ 7746657Snate@binkert.org assert(0); 7756657Snate@binkert.org} 7766657Snate@binkert.org''') 7776657Snate@binkert.org 7786657Snate@binkert.org 7796657Snate@binkert.org code.write(path, "%s_Wakeup.cc" % self.ident) 7806657Snate@binkert.org 7816657Snate@binkert.org def printCSwitch(self, path): 7826657Snate@binkert.org '''Output switch statement for transition table''' 7836657Snate@binkert.org 7846657Snate@binkert.org code = code_formatter() 7856657Snate@binkert.org ident = self.ident 7866657Snate@binkert.org 7876657Snate@binkert.org code(''' 7886657Snate@binkert.org// Auto generated C++ code started by $__file__:$__line__ 7896657Snate@binkert.org// ${ident}: ${{self.short}} 7906657Snate@binkert.org 7916657Snate@binkert.org#include "mem/ruby/common/Global.hh" 7926657Snate@binkert.org#include "mem/protocol/${ident}_Controller.hh" 7936657Snate@binkert.org#include "mem/protocol/${ident}_State.hh" 7946657Snate@binkert.org#include "mem/protocol/${ident}_Event.hh" 7956657Snate@binkert.org#include "mem/protocol/Types.hh" 7966657Snate@binkert.org#include "mem/ruby/system/System.hh" 7976657Snate@binkert.org 7986657Snate@binkert.org#define HASH_FUN(state, event) ((int(state)*${ident}_Event_NUM)+int(event)) 7996657Snate@binkert.org 8006657Snate@binkert.org#define GET_TRANSITION_COMMENT() (${ident}_transitionComment.str()) 8016657Snate@binkert.org#define CLEAR_TRANSITION_COMMENT() (${ident}_transitionComment.str("")) 8026657Snate@binkert.org 8036657Snate@binkert.orgTransitionResult ${ident}_Controller::doTransition(${ident}_Event event, ${ident}_State state, const Address& addr 8046657Snate@binkert.org) 8056657Snate@binkert.org{ 8066657Snate@binkert.org ${ident}_State next_state = state; 8076657Snate@binkert.org 8086657Snate@binkert.org DEBUG_NEWLINE(GENERATED_COMP, MedPrio); 8096657Snate@binkert.org DEBUG_MSG(GENERATED_COMP, MedPrio, *this); 8106657Snate@binkert.org DEBUG_EXPR(GENERATED_COMP, MedPrio, g_eventQueue_ptr->getTime()); 8116657Snate@binkert.org DEBUG_EXPR(GENERATED_COMP, MedPrio,state); 8126657Snate@binkert.org DEBUG_EXPR(GENERATED_COMP, MedPrio,event); 8136657Snate@binkert.org DEBUG_EXPR(GENERATED_COMP, MedPrio,addr); 8146657Snate@binkert.org 8156657Snate@binkert.org TransitionResult result = doTransitionWorker(event, state, next_state, addr); 8166657Snate@binkert.org 8176657Snate@binkert.org if (result == TransitionResult_Valid) { 8186657Snate@binkert.org DEBUG_EXPR(GENERATED_COMP, MedPrio, next_state); 8196657Snate@binkert.org DEBUG_NEWLINE(GENERATED_COMP, MedPrio); 8206657Snate@binkert.org s_profiler.countTransition(state, event); 8216657Snate@binkert.org if (Debug::getProtocolTrace()) { 8226657Snate@binkert.org g_system_ptr->getProfiler()->profileTransition("${ident}", m_version, addr, 8236657Snate@binkert.org ${ident}_State_to_string(state), 8246657Snate@binkert.org ${ident}_Event_to_string(event), 8256657Snate@binkert.org ${ident}_State_to_string(next_state), GET_TRANSITION_COMMENT()); 8266657Snate@binkert.org } 8276657Snate@binkert.org CLEAR_TRANSITION_COMMENT(); 8286657Snate@binkert.org ${ident}_setState(addr, next_state); 8296657Snate@binkert.org 8306657Snate@binkert.org } else if (result == TransitionResult_ResourceStall) { 8316657Snate@binkert.org if (Debug::getProtocolTrace()) { 8326657Snate@binkert.org g_system_ptr->getProfiler()->profileTransition("${ident}", m_version, addr, 8336657Snate@binkert.org ${ident}_State_to_string(state), 8346657Snate@binkert.org ${ident}_Event_to_string(event), 8356657Snate@binkert.org ${ident}_State_to_string(next_state), 8366657Snate@binkert.org "Resource Stall"); 8376657Snate@binkert.org } 8386657Snate@binkert.org } else if (result == TransitionResult_ProtocolStall) { 8396657Snate@binkert.org DEBUG_MSG(GENERATED_COMP, HighPrio, "stalling"); 8406657Snate@binkert.org DEBUG_NEWLINE(GENERATED_COMP, MedPrio); 8416657Snate@binkert.org if (Debug::getProtocolTrace()) { 8426657Snate@binkert.org g_system_ptr->getProfiler()->profileTransition("${ident}", m_version, addr, 8436657Snate@binkert.org ${ident}_State_to_string(state), 8446657Snate@binkert.org ${ident}_Event_to_string(event), 8456657Snate@binkert.org ${ident}_State_to_string(next_state), 8466657Snate@binkert.org "Protocol Stall"); 8476657Snate@binkert.org } 8486657Snate@binkert.org } 8496657Snate@binkert.org 8506657Snate@binkert.org return result; 8516657Snate@binkert.org} 8526657Snate@binkert.org 8536657Snate@binkert.orgTransitionResult ${ident}_Controller::doTransitionWorker(${ident}_Event event, ${ident}_State state, ${ident}_State& next_state, const Address& addr 8546657Snate@binkert.org) 8556657Snate@binkert.org{ 8566657Snate@binkert.org switch(HASH_FUN(state, event)) { 8576657Snate@binkert.org''') 8586657Snate@binkert.org 8596657Snate@binkert.org # This map will allow suppress generating duplicate code 8606657Snate@binkert.org cases = orderdict() 8616657Snate@binkert.org 8626657Snate@binkert.org for trans in self.transitions: 8636657Snate@binkert.org case_string = "%s_State_%s, %s_Event_%s" % \ 8646657Snate@binkert.org (self.ident, trans.state.ident, self.ident, trans.event.ident) 8656657Snate@binkert.org 8666657Snate@binkert.org case = code_formatter() 8676657Snate@binkert.org # Only set next_state if it changes 8686657Snate@binkert.org if trans.state != trans.nextState: 8696657Snate@binkert.org ns_ident = trans.nextState.ident 8706657Snate@binkert.org case('next_state = ${ident}_State_${ns_ident};') 8716657Snate@binkert.org 8726657Snate@binkert.org actions = trans.actions 8736657Snate@binkert.org 8746657Snate@binkert.org # Check for resources 8756657Snate@binkert.org case_sorter = [] 8766657Snate@binkert.org res = trans.resources 8776657Snate@binkert.org for key,val in res.iteritems(): 8786657Snate@binkert.org if key.type.ident != "DNUCAStopTable": 8796657Snate@binkert.org val = ''' 8806657Snate@binkert.orgif (!%s.areNSlotsAvailable(%s)) { 8816657Snate@binkert.org return TransitionResult_ResourceStall; 8826657Snate@binkert.org} 8836657Snate@binkert.org''' % (key.code, val) 8846657Snate@binkert.org case_sorter.append(val) 8856657Snate@binkert.org 8866657Snate@binkert.org 8876657Snate@binkert.org # Emit the code sequences in a sorted order. This makes the 8886657Snate@binkert.org # output deterministic (without this the output order can vary 8896657Snate@binkert.org # since Map's keys() on a vector of pointers is not deterministic 8906657Snate@binkert.org for c in sorted(case_sorter): 8916657Snate@binkert.org case("$c") 8926657Snate@binkert.org 8936657Snate@binkert.org # Figure out if we stall 8946657Snate@binkert.org stall = False 8956657Snate@binkert.org for action in actions: 8966657Snate@binkert.org if action.ident == "z_stall": 8976657Snate@binkert.org stall = True 8986657Snate@binkert.org break 8996657Snate@binkert.org 9006657Snate@binkert.org if stall: 9016657Snate@binkert.org case('return TransitionResult_ProtocolStall;') 9026657Snate@binkert.org else: 9036657Snate@binkert.org for action in actions: 9046657Snate@binkert.org case('${{action.ident}}(addr);') 9056657Snate@binkert.org case('return TransitionResult_Valid;') 9066657Snate@binkert.org 9076657Snate@binkert.org case = str(case) 9086657Snate@binkert.org 9096657Snate@binkert.org # Look to see if this transition code is unique. 9106657Snate@binkert.org if case not in cases: 9116657Snate@binkert.org cases[case] = [] 9126657Snate@binkert.org 9136657Snate@binkert.org cases[case].append(case_string) 9146657Snate@binkert.org 9156657Snate@binkert.org # Walk through all of the unique code blocks and spit out the 9166657Snate@binkert.org # corresponding case statement elements 9176657Snate@binkert.org for case,transitions in cases.iteritems(): 9186657Snate@binkert.org # Iterative over all the multiple transitions that share 9196657Snate@binkert.org # the same code 9206657Snate@binkert.org for trans in transitions: 9216657Snate@binkert.org code(' case HASH_FUN($trans):') 9226657Snate@binkert.org code(' {') 9236657Snate@binkert.org code(' $case') 9246657Snate@binkert.org code(' }') 9256657Snate@binkert.org 9266657Snate@binkert.org code(''' 9276657Snate@binkert.org default: 9286657Snate@binkert.org WARN_EXPR(m_version); 9296657Snate@binkert.org WARN_EXPR(g_eventQueue_ptr->getTime()); 9306657Snate@binkert.org WARN_EXPR(addr); 9316657Snate@binkert.org WARN_EXPR(event); 9326657Snate@binkert.org WARN_EXPR(state); 9336657Snate@binkert.org ERROR_MSG(\"Invalid transition\"); 9346657Snate@binkert.org } 9356657Snate@binkert.org return TransitionResult_Valid; 9366657Snate@binkert.org} 9376657Snate@binkert.org''') 9386657Snate@binkert.org code.write(path, "%s_Transitions.cc" % self.ident) 9396657Snate@binkert.org 9406657Snate@binkert.org def printProfilerHH(self, path): 9416657Snate@binkert.org code = code_formatter() 9426657Snate@binkert.org ident = self.ident 9436657Snate@binkert.org 9446657Snate@binkert.org code(''' 9456657Snate@binkert.org// Auto generated C++ code started by $__file__:$__line__ 9466657Snate@binkert.org// ${ident}: ${{self.short}} 9476657Snate@binkert.org 9486657Snate@binkert.org#ifndef ${ident}_PROFILER_H 9496657Snate@binkert.org#define ${ident}_PROFILER_H 9506657Snate@binkert.org 9516657Snate@binkert.org#include "mem/ruby/common/Global.hh" 9526657Snate@binkert.org#include "mem/protocol/${ident}_State.hh" 9536657Snate@binkert.org#include "mem/protocol/${ident}_Event.hh" 9546657Snate@binkert.org 9556657Snate@binkert.orgclass ${ident}_Profiler { 9566657Snate@binkert.org public: 9576657Snate@binkert.org ${ident}_Profiler(); 9586657Snate@binkert.org void setVersion(int version); 9596657Snate@binkert.org void countTransition(${ident}_State state, ${ident}_Event event); 9606657Snate@binkert.org void possibleTransition(${ident}_State state, ${ident}_Event event); 9616657Snate@binkert.org void dumpStats(ostream& out) const; 9626657Snate@binkert.org void clearStats(); 9636657Snate@binkert.org 9646657Snate@binkert.org private: 9656657Snate@binkert.org int m_counters[${ident}_State_NUM][${ident}_Event_NUM]; 9666657Snate@binkert.org int m_event_counters[${ident}_Event_NUM]; 9676657Snate@binkert.org bool m_possible[${ident}_State_NUM][${ident}_Event_NUM]; 9686657Snate@binkert.org int m_version; 9696657Snate@binkert.org}; 9706657Snate@binkert.org 9716657Snate@binkert.org#endif // ${ident}_PROFILER_H 9726657Snate@binkert.org''') 9736657Snate@binkert.org code.write(path, "%s_Profiler.hh" % self.ident) 9746657Snate@binkert.org 9756657Snate@binkert.org def printProfilerCC(self, path): 9766657Snate@binkert.org code = code_formatter() 9776657Snate@binkert.org ident = self.ident 9786657Snate@binkert.org 9796657Snate@binkert.org code(''' 9806657Snate@binkert.org// Auto generated C++ code started by $__file__:$__line__ 9816657Snate@binkert.org// ${ident}: ${{self.short}} 9826657Snate@binkert.org 9836657Snate@binkert.org#include "mem/protocol/${ident}_Profiler.hh" 9846657Snate@binkert.org 9856657Snate@binkert.org${ident}_Profiler::${ident}_Profiler() 9866657Snate@binkert.org{ 9876657Snate@binkert.org for (int state = 0; state < ${ident}_State_NUM; state++) { 9886657Snate@binkert.org for (int event = 0; event < ${ident}_Event_NUM; event++) { 9896657Snate@binkert.org m_possible[state][event] = false; 9906657Snate@binkert.org m_counters[state][event] = 0; 9916657Snate@binkert.org } 9926657Snate@binkert.org } 9936657Snate@binkert.org for (int event = 0; event < ${ident}_Event_NUM; event++) { 9946657Snate@binkert.org m_event_counters[event] = 0; 9956657Snate@binkert.org } 9966657Snate@binkert.org} 9976657Snate@binkert.orgvoid ${ident}_Profiler::setVersion(int version) 9986657Snate@binkert.org{ 9996657Snate@binkert.org m_version = version; 10006657Snate@binkert.org} 10016657Snate@binkert.orgvoid ${ident}_Profiler::clearStats() 10026657Snate@binkert.org{ 10036657Snate@binkert.org for (int state = 0; state < ${ident}_State_NUM; state++) { 10046657Snate@binkert.org for (int event = 0; event < ${ident}_Event_NUM; event++) { 10056657Snate@binkert.org m_counters[state][event] = 0; 10066657Snate@binkert.org } 10076657Snate@binkert.org } 10086657Snate@binkert.org 10096657Snate@binkert.org for (int event = 0; event < ${ident}_Event_NUM; event++) { 10106657Snate@binkert.org m_event_counters[event] = 0; 10116657Snate@binkert.org } 10126657Snate@binkert.org} 10136657Snate@binkert.orgvoid ${ident}_Profiler::countTransition(${ident}_State state, ${ident}_Event event) 10146657Snate@binkert.org{ 10156657Snate@binkert.org assert(m_possible[state][event]); 10166657Snate@binkert.org m_counters[state][event]++; 10176657Snate@binkert.org m_event_counters[event]++; 10186657Snate@binkert.org} 10196657Snate@binkert.orgvoid ${ident}_Profiler::possibleTransition(${ident}_State state, ${ident}_Event event) 10206657Snate@binkert.org{ 10216657Snate@binkert.org m_possible[state][event] = true; 10226657Snate@binkert.org} 10236657Snate@binkert.orgvoid ${ident}_Profiler::dumpStats(ostream& out) const 10246657Snate@binkert.org{ 10256657Snate@binkert.org out << " --- ${ident} " << m_version << " ---" << endl; 10266657Snate@binkert.org out << " - Event Counts -" << endl; 10276657Snate@binkert.org for (int event = 0; event < ${ident}_Event_NUM; event++) { 10286657Snate@binkert.org int count = m_event_counters[event]; 10296657Snate@binkert.org out << (${ident}_Event) event << " " << count << endl; 10306657Snate@binkert.org } 10316657Snate@binkert.org out << endl; 10326657Snate@binkert.org out << " - Transitions -" << endl; 10336657Snate@binkert.org for (int state = 0; state < ${ident}_State_NUM; state++) { 10346657Snate@binkert.org for (int event = 0; event < ${ident}_Event_NUM; event++) { 10356657Snate@binkert.org if (m_possible[state][event]) { 10366657Snate@binkert.org int count = m_counters[state][event]; 10376657Snate@binkert.org out << (${ident}_State) state << " " << (${ident}_Event) event << " " << count; 10386657Snate@binkert.org if (count == 0) { 10396657Snate@binkert.org out << " <-- "; 10406657Snate@binkert.org } 10416657Snate@binkert.org out << endl; 10426657Snate@binkert.org } 10436657Snate@binkert.org } 10446657Snate@binkert.org out << endl; 10456657Snate@binkert.org } 10466657Snate@binkert.org} 10476657Snate@binkert.org''') 10486657Snate@binkert.org code.write(path, "%s_Profiler.cc" % self.ident) 10496657Snate@binkert.org 10506657Snate@binkert.org # ************************** 10516657Snate@binkert.org # ******* HTML Files ******* 10526657Snate@binkert.org # ************************** 10536657Snate@binkert.org def frameRef(self, click_href, click_target, over_href, over_target_num, 10546657Snate@binkert.org text): 10556657Snate@binkert.org code = code_formatter(fix_newlines=False) 10566657Snate@binkert.org code("""<A href=\"$click_href\" target=\"$click_target\" onMouseOver=\"if (parent.frames[$over_target_num].location != parent.location + '$over_href') { parent.frames[$over_target_num].location='$over_href' }\" >${{html.formatShorthand(text)}}</A>""") 10576657Snate@binkert.org return str(code) 10586657Snate@binkert.org 10596657Snate@binkert.org def writeHTMLFiles(self, path): 10606657Snate@binkert.org # Create table with no row hilighted 10616657Snate@binkert.org self.printHTMLTransitions(path, None) 10626657Snate@binkert.org 10636657Snate@binkert.org # Generate transition tables 10646657Snate@binkert.org for state in self.states.itervalues(): 10656657Snate@binkert.org self.printHTMLTransitions(path, state) 10666657Snate@binkert.org 10676657Snate@binkert.org # Generate action descriptions 10686657Snate@binkert.org for action in self.actions.itervalues(): 10696657Snate@binkert.org name = "%s_action_%s.html" % (self.ident, action.ident) 10706657Snate@binkert.org code = html.createSymbol(action, "Action") 10716657Snate@binkert.org code.write(path, name) 10726657Snate@binkert.org 10736657Snate@binkert.org # Generate state descriptions 10746657Snate@binkert.org for state in self.states.itervalues(): 10756657Snate@binkert.org name = "%s_State_%s.html" % (self.ident, state.ident) 10766657Snate@binkert.org code = html.createSymbol(state, "State") 10776657Snate@binkert.org code.write(path, name) 10786657Snate@binkert.org 10796657Snate@binkert.org # Generate event descriptions 10806657Snate@binkert.org for event in self.events.itervalues(): 10816657Snate@binkert.org name = "%s_Event_%s.html" % (self.ident, event.ident) 10826657Snate@binkert.org code = html.createSymbol(event, "Event") 10836657Snate@binkert.org code.write(path, name) 10846657Snate@binkert.org 10856657Snate@binkert.org def printHTMLTransitions(self, path, active_state): 10866657Snate@binkert.org code = code_formatter() 10876657Snate@binkert.org 10886657Snate@binkert.org code(''' 10896657Snate@binkert.org<HTML><BODY link="blue" vlink="blue"> 10906657Snate@binkert.org 10916657Snate@binkert.org<H1 align="center">${{html.formatShorthand(self.short)}}: 10926657Snate@binkert.org''') 10936657Snate@binkert.org code.indent() 10946657Snate@binkert.org for i,machine in enumerate(self.symtab.getAllType(StateMachine)): 10956657Snate@binkert.org mid = machine.ident 10966657Snate@binkert.org if i != 0: 10976657Snate@binkert.org extra = " - " 10986657Snate@binkert.org else: 10996657Snate@binkert.org extra = "" 11006657Snate@binkert.org if machine == self: 11016657Snate@binkert.org code('$extra$mid') 11026657Snate@binkert.org else: 11036657Snate@binkert.org code('$extra<A target="Table" href="${mid}_table.html">$mid</A>') 11046657Snate@binkert.org code.dedent() 11056657Snate@binkert.org 11066657Snate@binkert.org code(""" 11076657Snate@binkert.org</H1> 11086657Snate@binkert.org 11096657Snate@binkert.org<TABLE border=1> 11106657Snate@binkert.org<TR> 11116657Snate@binkert.org <TH> </TH> 11126657Snate@binkert.org""") 11136657Snate@binkert.org 11146657Snate@binkert.org for event in self.events.itervalues(): 11156657Snate@binkert.org href = "%s_Event_%s.html" % (self.ident, event.ident) 11166657Snate@binkert.org ref = self.frameRef(href, "Status", href, "1", event.short) 11176657Snate@binkert.org code('<TH bgcolor=white>$ref</TH>') 11186657Snate@binkert.org 11196657Snate@binkert.org code('</TR>') 11206657Snate@binkert.org # -- Body of table 11216657Snate@binkert.org for state in self.states.itervalues(): 11226657Snate@binkert.org # -- Each row 11236657Snate@binkert.org if state == active_state: 11246657Snate@binkert.org color = "yellow" 11256657Snate@binkert.org else: 11266657Snate@binkert.org color = "white" 11276657Snate@binkert.org 11286657Snate@binkert.org click = "%s_table_%s.html" % (self.ident, state.ident) 11296657Snate@binkert.org over = "%s_State_%s.html" % (self.ident, state.ident) 11306657Snate@binkert.org text = html.formatShorthand(state.short) 11316657Snate@binkert.org ref = self.frameRef(click, "Table", over, "1", state.short) 11326657Snate@binkert.org code(''' 11336657Snate@binkert.org<TR> 11346657Snate@binkert.org <TH bgcolor=$color>$ref</TH> 11356657Snate@binkert.org''') 11366657Snate@binkert.org 11376657Snate@binkert.org # -- One column for each event 11386657Snate@binkert.org for event in self.events.itervalues(): 11396657Snate@binkert.org trans = self.table.get((state,event), None) 11406657Snate@binkert.org if trans is None: 11416657Snate@binkert.org # This is the no transition case 11426657Snate@binkert.org if state == active_state: 11436657Snate@binkert.org color = "#C0C000" 11446657Snate@binkert.org else: 11456657Snate@binkert.org color = "lightgrey" 11466657Snate@binkert.org 11476657Snate@binkert.org code('<TD bgcolor=$color> </TD>') 11486657Snate@binkert.org continue 11496657Snate@binkert.org 11506657Snate@binkert.org next = trans.nextState 11516657Snate@binkert.org stall_action = False 11526657Snate@binkert.org 11536657Snate@binkert.org # -- Get the actions 11546657Snate@binkert.org for action in trans.actions: 11556657Snate@binkert.org if action.ident == "z_stall" or \ 11566657Snate@binkert.org action.ident == "zz_recycleMandatoryQueue": 11576657Snate@binkert.org stall_action = True 11586657Snate@binkert.org 11596657Snate@binkert.org # -- Print out "actions/next-state" 11606657Snate@binkert.org if stall_action: 11616657Snate@binkert.org if state == active_state: 11626657Snate@binkert.org color = "#C0C000" 11636657Snate@binkert.org else: 11646657Snate@binkert.org color = "lightgrey" 11656657Snate@binkert.org 11666657Snate@binkert.org elif active_state and next.ident == active_state.ident: 11676657Snate@binkert.org color = "aqua" 11686657Snate@binkert.org elif state == active_state: 11696657Snate@binkert.org color = "yellow" 11706657Snate@binkert.org else: 11716657Snate@binkert.org color = "white" 11726657Snate@binkert.org 11736657Snate@binkert.org fix = code.nofix() 11746657Snate@binkert.org code('<TD bgcolor=$color>') 11756657Snate@binkert.org for action in trans.actions: 11766657Snate@binkert.org href = "%s_action_%s.html" % (self.ident, action.ident) 11776657Snate@binkert.org ref = self.frameRef(href, "Status", href, "1", 11786657Snate@binkert.org action.short) 11796657Snate@binkert.org code(' $ref\n') 11806657Snate@binkert.org if next != state: 11816657Snate@binkert.org if trans.actions: 11826657Snate@binkert.org code('/') 11836657Snate@binkert.org click = "%s_table_%s.html" % (self.ident, next.ident) 11846657Snate@binkert.org over = "%s_State_%s.html" % (self.ident, next.ident) 11856657Snate@binkert.org ref = self.frameRef(click, "Table", over, "1", next.short) 11866657Snate@binkert.org code("$ref") 11876657Snate@binkert.org code("</TD>\n") 11886657Snate@binkert.org code.fix(fix) 11896657Snate@binkert.org 11906657Snate@binkert.org # -- Each row 11916657Snate@binkert.org if state == active_state: 11926657Snate@binkert.org color = "yellow" 11936657Snate@binkert.org else: 11946657Snate@binkert.org color = "white" 11956657Snate@binkert.org 11966657Snate@binkert.org click = "%s_table_%s.html" % (self.ident, state.ident) 11976657Snate@binkert.org over = "%s_State_%s.html" % (self.ident, state.ident) 11986657Snate@binkert.org ref = self.frameRef(click, "Table", over, "1", state.short) 11996657Snate@binkert.org code(''' 12006657Snate@binkert.org <TH bgcolor=$color>$ref</TH> 12016657Snate@binkert.org</TR> 12026657Snate@binkert.org''') 12036657Snate@binkert.org code(''' 12046657Snate@binkert.org<TR> 12056657Snate@binkert.org <TH> </TH> 12066657Snate@binkert.org''') 12076657Snate@binkert.org 12086657Snate@binkert.org for event in self.events.itervalues(): 12096657Snate@binkert.org href = "%s_Event_%s.html" % (self.ident, event.ident) 12106657Snate@binkert.org ref = self.frameRef(href, "Status", href, "1", event.short) 12116657Snate@binkert.org code('<TH bgcolor=white>$ref</TH>') 12126657Snate@binkert.org code(''' 12136657Snate@binkert.org</TR> 12146657Snate@binkert.org</TABLE> 12156657Snate@binkert.org</BODY></HTML> 12166657Snate@binkert.org''') 12176657Snate@binkert.org 12186657Snate@binkert.org 12196657Snate@binkert.org if active_state: 12206657Snate@binkert.org name = "%s_table_%s.html" % (self.ident, active_state.ident) 12216657Snate@binkert.org else: 12226657Snate@binkert.org name = "%s_table.html" % self.ident 12236657Snate@binkert.org code.write(path, name) 12246657Snate@binkert.org 12256657Snate@binkert.org__all__ = [ "StateMachine" ] 1226