# ========== Copyright Header Begin ==========================================
# OpenSPARC T2 Processor File: SamFE.py
# Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
# The above named program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License version 2 as published by the Free Software Foundation.
# The above named program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public
# License along with this work; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
# ========== Copyright Header End ============================================
"""Utilities needed to emulate Python's interactive interpreter.
# copied from RiesCode.py, for cosim & sam environment
import os
, re
, string
, sys
, time
, types
from Pfe_Conversion
import *
from Pfe_Assembler
import *
from codeop
import CommandCompiler
, compile_command
__all__
= ["InteractiveInterpreter", "InteractiveConsole", "interact",
RUN_PYTHON_FILE
= 'run-python-file'
# when 1, ignore any exception associated with a wrong python statement
# regular expression used to catch execfile() command
execfileRE
= re
.compile("^execfile(\s)*\([^\(\)]+\)$")
NEW_CMD_RE
= re
.compile('^@?new_command\s*\(')
DIGITAL_RE
= re
.compile('^[0-9]+$')
RC_SYS_CONFIG_OBJ
= 'config0'
RC_MEM_IMAGE
= 'mem_image'
RC_COMMAND_EXT
= 'command_ext'
def softspace(file, newvalue
):
oldvalue
= file.softspace
file.softspace
= newvalue
except (AttributeError, TypeError):
# "attribute-less object" or "read-only attributes"
class InteractiveInterpreter
:
"""Base class for InteractiveConsole.
This class deals with parsing and interpreter state (the user's
namespace); it doesn't deal with input buffering or prompting or
input file naming (the filename is always passed in explicitly).
def __init__(self
, locals=None):
The optional 'locals' argument specifies the dictionary in
which code will be executed; it defaults to a newly created
dictionary with key "__name__" set to "__console__" and key
locals = {"__name__": "__console__", "__doc__": None}
self
.compile = CommandCompiler()
def runsource(self
, source
, filename
="<input>", symbol
="single"):
"""Compile and run some source in the interpreter.
Arguments are as for compile_command().
One several things can happen:
1) The input is incorrect; compile_command() raised an
exception (SyntaxError or OverflowError). A syntax traceback
will be printed by calling the showsyntaxerror() method.
2) The input is incomplete, and more input is required;
compile_command() returned None. Nothing happens.
3) The input is complete; compile_command() returned a code
object. The code is executed by calling self.runcode() (which
also handles run-time exceptions, except for SystemExit).
The return value is 1 in case 2, 0 in the other cases (unless
an exception is raised). The return value can be used to
decide whether to use sys.ps1 or sys.ps2 to prompt the next
code
= self
.compile(source
, filename
, symbol
)
except (OverflowError, SyntaxError, ValueError):
self
.showsyntaxerror(filename
)
"""Execute a code object.
When an exception occurs, self.showtraceback() is called to
display a traceback. All exceptions are caught except
SystemExit, which is reraised.
A note about KeyboardInterrupt: this exception may occur
elsewhere in this code, and may not always be caught. The
caller should be prepared to deal with it.
sys
.stderr
.write("# %s: end riesling frontend\n" % (time
.ctime()))
## except AttributeError:
if softspace(sys
.stdout
, 0):
def showsyntaxerror(self
, filename
=None):
"""Display the syntax error that just occurred.
This doesn't display a stack trace because there isn't one.
If a filename is given, it is stuffed in the exception instead
of what was there before (because Python's parser always uses
"<string>" when reading from a string).
The output is written by self.write(), below.
type, value
, sys
.last_traceback
= sys
.exc_info()
if filename
and type is SyntaxError:
# Work hard to stuff the correct filename in the exception
msg
, (dummy_filename
, lineno
, offset
, line
) = value
# Not the format we expect; leave it alone
# Stuff in the right filename
# Assume SyntaxError is a class exception
value
= SyntaxError(msg
, (filename
, lineno
, offset
, line
))
# If that failed, assume SyntaxError is a string
value
= msg
, (filename
, lineno
, offset
, line
)
list = traceback
.format_exception_only(type, value
)
"""Display the exception that just occurred.
We remove the first stack item because it is our own code.
The output is written by self.write(), below.
type, value
, tb
= sys
.exc_info()
tblist
= traceback
.extract_tb(tb
)
list = traceback
.format_list(tblist
)
list.insert(0, "Traceback (most recent call last):\n")
list[len(list):] = traceback
.format_exception_only(type, value
)
The base implementation writes to sys.stderr; a subclass may
replace this with a different implementation.
class InteractiveConsole(InteractiveInterpreter
):
"""Closely emulate the behavior of the interactive Python interpreter.
This class builds on InteractiveInterpreter and adds prompting
using the familiar sys.ps1 and sys.ps2, and input buffering.
def __init__(self
, locals=None, filename
="<console>"):
The optional locals argument will be passed to the
InteractiveInterpreter base class.
The optional filename argument should specify the (file)name
of the input stream; it will show up in tracebacks.
InteractiveInterpreter
.__init
__(self
, locals)
"""Reset the input buffer."""
def interact(self
, banner
=None, **parms
):
"""Closely emulate the interactive Python console.
The optional banner argument specify the banner to print
before the first interaction; by default it prints a banner
similar to the one printed by the real Python interpreter,
followed by the current class name in parentheses (so as not
to confuse this with the real interpreter -- since it's so
#sys.stderr.write("#DBX %s: enter InteractiveConsole.interact()\n" % (time.ctime()))
# connect to command parser
self
.parser
= parms
['parser']
## # if there are good_trap and/or bad_trap in symbol file, register
## # those as breakpoints
## # TODO by default, no breakpoint for good/bad_trap is set in sam,
## # we need an option to allow setting of those breakpoints.
## if parms['reposit'].symTable and not optdir.has_key('--blaze'):
## trapList = parms['reposit'].symTable.getTraps()
## for (trap,vaddr) in trapList:
## cmd = 'break %#x' % (vaddr)
## if self.parser.parseCmd(cmd, **parms):
## sys.stderr.write('ERROR: un-expected return value from parseCmd()\n')
# if there are command(s) specified in config, execute those first
if parms
.has_key('confRC'):
_configRC
= parms
['confRC']
if _configRC
.getObjData(ReadConfigDiag
.TYPE_SYS_CONFIG
, RC_SYS_CONFIG_OBJ
, RC_COMMAND
, silent
=1):
#sys.stderr.write("# %s: process config/init cmd\n" % (time.ctime()))
for cmd
in _configRC
.getObjData(ReadConfigDiag
.TYPE_SYS_CONFIG
, RC_SYS_CONFIG_OBJ
, RC_COMMAND
):
# if get_addr() is part of the command, better resolve
# that here, so we don't need to worry about that later
# when execute the command
index
= cmd
.find('get_addr')
cmd
= '%s%s' % (lop
, eval(rop
))
fdtmp
.write('%s\n' % (cmd
))
fdtmp
.write('%s\n' % (cmd
))
# execute the commands specified in config file, if a command
# cannot be run, just ignore it.
self
.parseExecfile('execfile("%s")' % (tmp
), ignore
=1, **parms
)
#sys.stderr.write("# %s: DBX: done config/init cmd\n" % (time.ctime()))
prompt
= _configRC
.getObjData(ReadConfigDiag
.TYPE_SYS_CONFIG
, RC_SYS_CONFIG_OBJ
, RC_PROMPT
)
parms
['reposit'].prompt
= prompt
sys
.ps1
= "%s>>> " % (prompt
)
sys
.ps2
= "%s... " % (prompt
)
sys
.ps3
= "%s!!! " % (prompt
)
cprt
= 'Type "copyright", "credits" or "license" for more information.'
self
.write("Python %s on %s\n%s\n(%s)\n" %
(sys
.version
, sys
.platform
, cprt
,
self
.__class
__.__name
__))
#self.write("# %s: %s\n" % (time.ctime(), str(banner)))
elif (no_sam
== 0) and sam
.is_stopped():
line
= self
.raw_input(prompt
)
line
= self
.parser
.parseCmd(line
, **parms
)
# most excepts are handled by push() and its inner
# methods, so we won't see them here.
if re
.match(execfileRE
, line
):
more
= self
.parseExecfile(line
, **parms
)
# to play it safe, flush out buffer when parser returns
except KeyboardInterrupt:
self
.write("\nKeyboardInterrupt\n")
"""Push a line to the interpreter.
The line should not have a trailing newline; it may have
internal newlines. The line is appended to a buffer and the
interpreter's runsource() method is called with the
concatenated contents of the buffer as source. If this
indicates that the command was executed or invalid, the buffer
is reset; otherwise, the command is incomplete, and the buffer
is left as it was after the line was appended. The return
value is 1 if more input is required, 0 if the line was dealt
with in some way (this is the same as runsource()).
source
= "\n".join(self
.buffer)
more
= self
.runsource(source
, self
.filename
)
def raw_input(self
, prompt
=""):
"""Write a prompt and read a line.
The returned line does not include the trailing newline.
When the user enters the EOF key sequence, EOFError is raised.
The base implementation uses the built-in function
raw_input(); a subclass may replace this with a different
#return raw_input(prompt)
if re
.match(NEW_CMD_RE
, line
):
if not line
.strip().endswith(')'):
while line
.strip() and not line
.strip().endswith(')'):
# remember to include the last line ending with ')'
def parseExecfile (self
, cmdLine
, ignore
=0, **parms
):
"""execfile() bypasses riesling cmd parser by default, so we have
to intercept the execfile() command, and process its content line
current_ignore_except
= IGNORE_EXCEPT
self
._parseExecfile
(cmdLine
, ignore
=ignore
, **parms
)
#sys.stderr.write('DBX: RiesCodeN2::parseExecfile: ignore <%s>, ex=%s\n' % (cmdLine, ex))
IGNORE_EXCEPT
= current_ignore_except
def _parseExecfile (self
, cmdLine
, ignore
=0, **parms
):
"""execfile() bypasses riesling cmd parser by default, so we have
to intercept the execfile() command, and process its content line
# the cmdLine is expected to be in the format of "execfile('zzz')"
lindex
= cmdLine
.find('(')
rindex
= cmdLine
.find(')')
filename
= eval(cmdLine
[lindex
+1:rindex
].strip())
for line
in fin
.readlines():
if line
and line
.startswith('def '):
# a new function definition
# CmdParserNi::registerDoc (self, key, fname, type, shortdoc, longdoc)
defline
= defline
[4:ii
].strip()
defkey
= defkey
[:jj
].strip()
parser
.registerDoc(defkey
, defline
, '@DEF', filename
+' : @def '+defline
, None)
# continue reading & concatenating line(s) until a complete
# expression is formed. Remember to remove trailing newline
if (line
!= '') and (line
[-1] == "\\"):
# exclude the trailing "\"
lineBuffer
.append(line
[:-1])
# make sure we include the last line of an expression
line
= ' '.join(lineBuffer
)
# keep track of leading whitespace
while (i
< n
) and (line
[i
] in string
.whitespace
):
# if see nested execfile(), handle that recursively
if re
.match(execfileRE
, cmd
):
self
.parseExecfile(cmd
, ignore
=ignore
, **parms
)
#sys.stderr.write('DBX: RiesCodeN2::_parseExecfile: ignore <%s>, ex=%s\n' % (cmd, ex))
# if not an execfile(), parse the line, then add back the
cmd
= self
.parser
.parseCmd(cmd
, **parms
)
# for run-command-file and run-python-file
# commands, parser will return execfile(file-name),
# since the file may contains frontend commands,
# so we must pass them through parser line by line.
if re
.match(execfileRE
, cmd
):
self
.parseExecfile(cmd
, ignore
=ignore
, **parms
)
# if it is an exit signal, go with it.
more
= self
.push(indent
+ 'raise')
#sys.stderr.write('DBX: RiesCodeN2::_parseExecfile: ignore <%s>, ex2=%s\n' % (cmd, ex))
# send in an extra newline to close the last definition in the file.
def interact(banner
=None, readfunc
=None, local
=None, **parms
):
"""Closely emulate the interactive Python interpreter.
This is a backwards compatible interface to the InteractiveConsole
class. When readfunc is not specified, it attempts to import the
readline module to enable GNU readline if it is available.
Arguments (all optional, all default to None):
banner -- passed to InteractiveConsole.interact()
readfunc -- if not None, replaces InteractiveConsole.raw_input()
local -- passed to InteractiveInterpreter.__init__()
console
= InteractiveConsole(local
)
console
.raw_input = readfunc
console
.interact(banner
, **parms
)
print "usage: python %s [options]" % (sys
.argv
[0])
print "\t -c conf-file # diag configuration file"
print "\t -h # this usage information"
#print "\t -s backend-config # riesling backend configuration file"
print "\t -x conf-file # riesling configuration file"
print "\t -p python-file # python configuration file to be executed at startup"
print "\t --blaze system # type of system, n2"
print "\t --blazeopt \"blaze options\" # blaze runtime options"
print "\t --ar system # type of system, n2, ignore if --blaze is used"
print "\t --rc riesling-rc # riesling config, a combination of -c & -x"
type, value
, tb
= sys
.exc_info()
tblist
= traceback
.extract_tb(tb
)
list = traceback
.format_list(tblist
)
list.insert(0, "Traceback (most recent call last):\n")
list[len(list):] = traceback
.format_exception_only(type, value
)
sys
.stderr
.write('%s' % line
)
def createSystem (optdir
):
"""invoke backend system constructor
#sys.stderr.write("# %s: create riesling.sys\n" % (time.ctime()))
# if --blaze is specified, use that, otherwise use --ar
if optdir
.has_key('--blaze'):
if optdir
['--blaze'] == 'n2':
sys
.stderr
.write('ERROR: unknown system %s\n' % (optdir
['--blaze']))
if optdir
['--ar'] == 'n2':
sys
.stderr
.write('ERROR: unknown system %s\n' % (optdir
['--ar']))
nstrandObjs
= ncpus
* ncores
* nstrands
#sys.stderr.write('# %s: sys=%s=(cpu,core,strand)=(%d,%d,%d)\n' % (time.ctime(), optdir['--ar'].upper(), ncpus, ncores, nstrands))
nstrandObjs
= ncpus
* ncores
* nucores
* nstrands
#sys.stderr.write('# %s: sys=(cpu,core,ucore,strand)=(%d,%d,%d,%d)\n' % (time.ctime(), ncpus, ncores, nucores, nstrands))
#sys.stderr.write("# %s: done creating riesling.sys\n" % (time.ctime()))
"""init N2 structure/variables
#sys.stderr.write("#DBX %s: initN2\n" % (time.ctime()))
def initN2_specific(rsys
):
"""special setting for N2
#sys.stderr.write("#DBX %s: initN2_specific\n" % (time.ctime()))
# cmp infomration in .riesling.rc
## OBJECT swvmem0 TYPE swerver-memory {
## ignore_sparc : ['1', '3', '4', '5', '6', '7']
## thread_status : 0x9a00000000
# (1) use 'cpu' to determine the number of cpus, (2) if thread_maskN is
# available, use that to calculate CMP, (3) otherwise, use ignore_sparc
# and threadsN to calculate CMP, less desirable.
numCpus
= int(configRC
.getData('swvmem0', 'cpu', silent
=1))
if ((optdir
['--ar'] == 'n2') and ((numCpus
< 0) or (numCpus
> 1))):
sys
.stderr
.write('ERROR: wrong number of %s cpus %d\n' % (optdir
['--ar'].upper(), numCpus
))
for ii
in range(numCpus
):
# set cmp init value, if any
coresAndStrands
= configRC
.getData('swvmem0', 'thread_mask'+str(ii
), silent
=1)
if (not coresAndStrands
) and (ii
== 0):
# for backward compatible, we used to just use thread_mask,
# instead of thread_mask0
coresAndStrands
= configRC
.getData('swvmem0', 'thread_mask', silent
=1)
#sys.stderr.write('#DBX: use thread_mask%d=%s\n' % (ii, coresAndStrands))
# if -sas_run_args=-DTHREAD_MASK is used, the value is stored in
# coresAndStrands, which alone handles both cores & strands status.
# this is the preferred option of specifying core/strand availability.
# e.g., -sas_run_args=-DTHREAD_MASK=11xx11, every 2 digits (from right
# to left) represents a core, 'xx' means disabled core.
coresAndStrands
= coresAndStrands
.lower()
tail
= len(coresAndStrands
)
core
= coresAndStrands
[tail
-2:tail
]
# record the lowest core that is available
coreAvailable
= coreAvailable |
(0xffL
<< i
*8)
coreRunning
= coreRunning |
(long(core
, 16) << i
*8)
# if the coresAndStrands string has odd digits, handle the last one
core
= coresAndStrands
[:tail
]
coreAvailable
= coreAvailable |
(0xffL
<< i
*8)
coreRunning
= coreRunning |
(long(core
, 16) << i
*8)
# if no core is available, make the lowest one available
# if o strand is running, make the lowest strand of the lowest
# available core running.
coreRunning
= 0x1L
<< (firstCore
*8)
#sys.stderr.write('#DBX: 1 coreAvailable=%#x\n' % (coreAvailable))
#sys.stderr.write('#DBX: 1 coreRunning=%#x\n' % (coreRunning))
# otherwise use the combination of cores & strands status
parkedCoreList
= configRC
.getData('swvmem0', 'ignore_sparc', silent
=1)
if parkedCoreList
!= None:
if not str(i
) in parkedCoreList
:
coreAvailable
= coreAvailable |
(0xffL
<< (i
-ii
*8)*8)
# don't change existing variable
unparkedStrands
= configRC
.getData('swvmem0', 'threads', silent
=1)
unparkedStrands
= configRC
.getData('swvmem0', 'threads'+str(ii
), silent
=1)
if unparkedStrands
!= None:
# only strands in enabled cores can be running
coreRunning
= long(unparkedStrands
, 16) & coreAvailable
coreRunning
= 0x1L
<< (firstCore
*8)
# if no threads is specified, all strands in enabled cores
coreRunning
= coreAvailable
#sys.stderr.write('#DBX: 2 coreAvailable=%#x\n' % (coreAvailable))
#sys.stderr.write('#DBX: 2 coreRunning=%#x\n' % (coreRunning))
#sys.stderr.write('#DBX: 3 coreAvailable=%#x\n' % (coreAvailable))
#sys.stderr.write('#DBX: 3 coreRunning=%#x\n' % (coreRunning))
if coreAvailable
!= 0x0 and coreRunning
!= 0x0:
strands
[headStrand
].wrasi(0x41, 0x0L
, coreAvailable
)
#???strands[headStrand].wrasi(0x41, 0x10L, coreAvailable)
strands
[headStrand
].wrasi(0x41, 0x20L
, coreAvailable
)
strands
[headStrand
].wrasi(0x41, 0x50L
, coreRunning
)
#???strands[headStrand].wrasi(0x41, 0x58L, coreRunning)
def initGenericNi (rsys
):
"""init generic Niagara structure
#sys.stderr.write("#DBX %s: initGenericNi\n" % (time.ctime()))
if not rsys
.__create
_cpu
__(1):
print "Failed to create %d Ni cpu instance(s)" % 1
# system configuration XXX
cores
[i
*8 + j
] = rsys
.p
[i
].c
[j
]
strands
[i
*8*4 + j
*8 + k
] = rsys
.p
[i
].c
[j
].s
[k
]
def initGenericN2 (rsys
):
"""init generic Niagara structure
#sys.stderr.write("#DBX %s: initGenericN2\n" % (time.ctime()))
nstrands
= len(rsys
.p0
.c0
.s
)
for j
in range(0,ncores
):
cores
[i
*ncores
+ j
] = rsys
.p
[i
].c
[j
]
for k
in range(0,nstrands
):
strands
[i
*ncores
*nstrands
+ j
*nstrands
+ k
] = rsys
.p
[i
].c
[j
].s
[k
]
"""read configuration data
#sys.stderr.write("#DBX %s: read config\n" % (time.ctime()))
# read -x config (diag.simics)
configRies
= ReadConfigRies
.ReadConfigRies()
configRies
.readConfig(optdir
['-x'])
#sys.stderr.write('DBX: configRies = %s\n' % (configRies))
if configRies
and configRies
.data
.has_key(ReadConfigRies
.DIAG_CONF
):
# if DIAG_CONF is specified in -x, use that
configDiag
= ReadConfigDiag
.ReadConfigDiag()
configDiag
.readConfig(configRies
.data
[ReadConfigRies
.DIAG_CONF
])
#sys.stderr.write('DBX: configDiag = %s\n' % (configDiag))
if configRies
== None or configDiag
== None:
#sys.stderr.write('ERROR: no configuration data is given by either -c & -x, or --rc option\n')
newline
= '@conf.%s.%s = %s' % (RC_SYS_CONFIG_OBJ
, RC_PROMPT
, configRies
.data
[ReadConfigRies
.PROMPT
])
newline
= '@conf.%s.%s = ries' % (RC_SYS_CONFIG_OBJ
, RC_PROMPT
)
configDiag
.setDataLine(newline
)
newline
= '@conf.%s.%s = %s' % (RC_SYS_CONFIG_OBJ
, RC_MEM_IMAGE
, RC_configRies
.data
[ReadConfigRies
.MEM_IMAGE
])
newline
= '@conf.%s.%s = mem.image' % (RC_SYS_CONFIG_OBJ
, RC_MEM_IMAGE
)
configDiag
.setDataLine(newline
)
# process @def and def specified in config file
handleAtDef(configRies
, configDiag
)
# process @conf.zzz in configRies
handleAtConf(configRies
, configDiag
)
if configRies
.data
.has_key(ReadConfigRies
.CMD
):
for cmd
in configRies
.data
[ReadConfigRies
.CMD
]:
newline
= '@conf.%s.%s =+ %s' % (RC_SYS_CONFIG_OBJ
, RC_COMMAND
, cmd
)
configDiag
.setDataLine(newline
)
# handle new_command() --- python command extension
if configRies
and configRies
.data
[ReadConfigRies
.NEW_CMD
] != {}:
for cmdkey
in configRies
.data
[ReadConfigRies
.NEW_CMD
].keys():
newline
= '@conf.%s.%s =+ %s=%s' % (RC_SYS_CONFIG_OBJ
, RC_COMMAND_EXT
, cmdkey
, configRies
.data
[ReadConfigRies
.NEW_CMD
][cmdkey
])
configDiag
.setDataLine(newline
)
fdtmp
= open(NEW_CONFIG
, 'w')
fdtmp
.write('%s\n' % (configDiag
))
# create the rc object to be used from now on
configRC
= ReadConfigDiag
.ReadConfigDiag()
configRC
.readConfig(NEW_CONFIG
)
def constructSystem (optdir
):
"""construct a backend system
#sys.stderr.write("#DBX %s: construct system\n" % (time.ctime()))
# create backend system and get the sys object's address, to be passed
#sys.stderr.write('# %s: sys addr=%u\n' % (time.ctime(), sysAddr))
#def handleAtDef (globals):
def handleAtDef (configRies
, configDiag
):
#sys.stderr.write("#DBX %s: process @def/def\n" % (time.ctime()))
# remove old file, if any
if configRies
.data
[ReadConfigRies
.AT_DEF
] != {}:
for func
in configRies
.data
[ReadConfigRies
.AT_DEF
].keys():
tfd
.write('%s\n' % (''.join(configRies
.data
[ReadConfigRies
.AT_DEF
][func
])))
newline
= '@conf.%s.%s = %s' % (RC_SYS_CONFIG_OBJ
, RC_FUNC_DEF
, ATDEF
)
configDiag
.setDataLine(newline
)
# run the function definition in ATDEF
if os
.path
.getsize(ATDEF
) > 0:
execfile(ATDEF
, globals())
def handleAtConf (configRies
, configDiag
):
#sys.stderr.write("#DBX %s: process @conf.zzz\n" % (time.ctime()))
if configDiag
and configRies
and configRies
.data
[ReadConfigRies
.AT_CONF
] != []:
for dline
in configRies
.data
[ReadConfigRies
.AT_CONF
]:
# the value is to be appended to existing one(s)
rop
= dline
[j
+2:].strip()
if re
.match(DIGITAL_RE
, rop
):
# if rhs are all [0-9], leave as is
# we need to eval things like get_addr()
newline
= '%s =+ %#x' % (lop
, eval(rop
))
# if the right operand cannot be eval'ed, just use its
# the value is to be used to overwrite existing one
rop
= dline
[j
+1:].strip()
if re
.match(DIGITAL_RE
, rop
):
# if rhs are all [0-9], leave as is
newline
= '%s = %#x' % (lop
, eval(rop
))
# add/update value in configDiag
configDiag
.setDataLine(newline
)
def createCommandParser ():
#sys.stderr.write("#DBX %s: createCommandParser()\n" % (time.ctime()))
parser
= CmdParserNi
.CmdParserNi(riesReposit
)
# set the value in parser for later use
parser
.setNstrandObjs(nstrandObjs
)
# init zzz_cmd_RS() & RS_zzz() functions in CmdParserNiCmd
#sys.stderr.write("#DBX %s: create CmdParserNiCmd\n" % (time.ctime()))
initCmdParserNiCmd(riesReposit
, nstrandObjs
)
# handle new_command() --- python command extension
if configRC
.getObjData(ReadConfigDiag
.TYPE_SYS_CONFIG
, RC_SYS_CONFIG_OBJ
, RC_COMMAND_EXT
, silent
=1):
for cmd
in configRC
.getObjData(ReadConfigDiag
.TYPE_SYS_CONFIG
, RC_SYS_CONFIG_OBJ
, RC_COMMAND_EXT
):
cmdVal
= cmd
[i
+1].strip()
parser
.registerCommand(cmdkey
, cmdVal
)
#sys.stderr.write("#DBX %s: registerAtDef()\n" % (time.ctime()))
if os
.path
.getsize(ATDEF
) > 0:
for line
in fdtmp
.readlines():
if line
.startswith('def '):
mindex
= line
.find('(', lindex
)
fname
= line
[lindex
+1:mindex
].strip()
func
= line
[lindex
+1:rindex
].strip()
parser
.registerDoc(fname
, func
, '@DEF', '@def '+func
, None)
def debugMomConfig (config
):
"""this function is N2 specific
momConfig
= ReadConfigDiag
.ReadConfigDiag()
momConfig
.readConfig(config
)
momKeys
= momConfig
.getObjKeys('mom', 'mom0', silent
=1)
data
= momConfig
.getData('mom0', key
, silent
=1)
sys
.stderr
.write('MOM: %s : %s\n' % (key
, data
))
def blazercCmd (newBlazeCmd
):
"""extract riesling commands (the ones with #@) from blazerc
# handle blaze -c option, if one is specifed.
if not optdir
.has_key('--blazeopt'):
blazeopt
= optdir
['--blazeopt']
#sys.stderr.write('#DBX: SamFR::blazercCmd: blazeopt=%s\n' % (blazeopt)) #DBX
ii
= blazeopt
.find('-c ')
# no -c option is specified
tmp
= blazeopt
[ii
+3:].strip()
sys
.stderr
.write('ERROR: fail to open %s\n' % (rcname
))
# sample sysconf statements in blazerc
# sysconf cpu name=cpu0 cpu-type=SUNW,UltraSPARC-T1 clock-frequency=0x200000 system-frequency=0x100000 cpu-loopticks=2 cpu-stickincr=1
# sysconf dumbserial serial1 type=GUEST startpa=0x9f10000000 endpa=0x9f1000004f
# sysconf dumbserial serial2 type=HYPERVISOR startpa=0xfff0c2c000 endpa=0xfff0c2c04f
riesReposit
.blazeNumcpu
= 0
for line
in fdrc
.readlines():
if line
and line
.startswith('#@'):
elif line
and line
.startswith('conf numcpu'):
# record the numcpu value in blazerc and initialize the pmask value
riesReposit
.blazeNumcpu
= int(tokens
[2])
for i
in range(1,riesReposit
.blazeNumcpu
):
riesReposit
.pmask
= (riesReposit
.pmask
<< 1) |
0x1
elif line
.startswith('sysconf'):
# sysconf cpu name=cpu0 cpu-type=...
# sysconf dumbserial serial1 type=GUEST ...
if (tokens
[1].lower() != 'cpu') and (tokens
[1] != '-p'):
newBlazeCmd
.append(tokens
[1])
newBlazeCmd
.append(tokens
[2])
elif (tokens
[1].lower() == 'cpu') and (tokens
[2].lower().startswith('name=cpu')):
if (riesReposit
.blazeNumcpu
== 0) and (cpuCount
> 0):
riesReposit
.blazeNumcpu
= cpuCount
for i
in range(1,riesReposit
.blazeNumcpu
):
riesReposit
.pmask
= (riesReposit
.pmask
<< 1) |
0x1L
if optdir
['--blaze'] in ['n2']:
# convert blaze numcpu to coreAvailable
if (riesReposit
.blazeNumcpu
% riesReposit
.nstrands
) == 0:
cores
= riesReposit
.blazeNumcpu
/ riesReposit
.nstrands
cores
= (riesReposit
.blazeNumcpu
/ riesReposit
.nstrands
) + 1
for i
in range(0, cores
):
if (i
% riesReposit
.ncores
) == 0:
cpuid
= i
/ riesReposit
.ncores
#coreAvail = (coreAvail << ((i%riesReposit.ncores)*8)) | 0xffL
coreAvail
= (coreAvail
<< 8) |
0xffL
if ((i
+1) % riesReposit
.ncores
) == 0:
# re-adjust CMP registers
strandid
= cpuid
* riesReposit
.ncores
* riesReposit
.nstrands
coreAvailable
= strands
[strandid
].rdasi(0x41, 0x0L
) & coreAvail
strands
[strandid
].wrasi(0x41, 0x0L
, coreAvailable
)
#???strands[strandid].wrasi(0x41, 0x10L, coreAvailable)
strands
[strandid
].wrasi(0x41, 0x20L
, coreAvailable
)
coreRunning
= strands
[strandid
].rdasi(0x41, 0x50L
) & coreAvail
strands
[strandid
].wrasi(0x41, 0x50L
, coreRunning
)
#???strands[strandid].wrasi(0x41, 0x58L, coreRunning)
strandid
= cpuid
* riesReposit
.ncores
* riesReposit
.nstrands
coreAvailable
= strands
[strandid
].rdasi(0x41, 0x0L
) & coreAvail
strands
[strandid
].wrasi(0x41, 0x0L
, coreAvailable
)
#???strands[strandid].wrasi(0x41, 0x10L, coreAvailable)
strands
[strandid
].wrasi(0x41, 0x20L
, coreAvailable
)
coreRunning
= strands
[strandid
].rdasi(0x41, 0x50L
) & coreAvail
strands
[strandid
].wrasi(0x41, 0x50L
, coreRunning
)
#???strands[strandid].wrasi(0x41, 0x58L, coreRunning)
if optdir
.has_key('--pmask'):
mask
= long(optdir
['--pmask'],16)
coreRunning
= mask
& strands
[strandid
].rdasi(0x41, 0x0L
)
strands
[strandid
].wrasi(0x41, 0x50L
, coreRunning
)
#if __name__ == '__main__':
from CmdParserNiCmd
import *
# default arch configuration
(ncpus
, ncores
, nucores
, nstrands
) = (0,0,0,0)
(cpus
, cores
, ucores
, strands
) = ({},{},{},{})
# folder to keep UI commands (the ones start with #@) extracted from blazerc
NEW_CONFIG
= '.riesling.rc'
def init (pfeSim
, _optdir
, uppers
={}):
#sys.stderr.write("# %s\n" % (commands.getoutput('uname -a')))
#sys.stderr.write("# Python %s\n" % sys.version)
#sys.stderr.write("# %s: start sam frontend\n" % (time.ctime()))
# read -x diag.sam (and then diag.conf)
if optdir
.has_key('--rc'):
# if --rc is used, then ignore -x
# NOTE it will be tricky to use --rc in rtl cosim environment,
# as we need socket-id to connect with rtl, and the id will
# change from run to run, which is recorded in diag.conf by sims,
# the socket-id in --rc won't work as it was from the previous
#sys.stderr.write('WARNING: cannot use --rc %s when cosim with RTL\n' % (optdir['--rc']))
NEW_CONFIG
= optdir
['--rc']
configRC
= ReadConfigDiag
.ReadConfigDiag()
configRC
.readConfig(optdir
['--rc'])
# run function definition specified by -x or --rc
ATDEF
= configRC
.getData(RC_SYS_CONFIG_OBJ
, RC_FUNC_DEF
)
#sys.stderr.write('#DBX: func-def %s\n' % (funcDef))
if ATDEF
and os
.path
.getsize(ATDEF
) > 0:
execfile(ATDEF
, globals())
# check if blaze/mom will be used
momObjs
= configRC
.getObjIds('mom', silent
=1)
# if user specifies a special version of mom, use that instead
momSoPath
= configRC
.data
['mom']['mom0']['so_path']
if (os
.path
.exists(momSoPath
)):
cmd
='ls -l *mom* ; /bin/cp -p %s . ; ls -l *mom*' % (momSoPath
)
sys
.stderr
.write('ERROR: mom module %s does not exist\n' % (momSoPath
))
if (momObjs
!= []) and (not optdir
.has_key('--blaze')):
prompt
= configRC
.data
[ReadConfigDiag
.TYPE_SYS_CONFIG
][RC_SYS_CONFIG_OBJ
][RC_PROMPT
]
sys
.stderr
.write('ERROR: unknown system prompt %s\n' % (prompt
))
# construct backend system
MEM_IMAGE
= configRC
.getObjData(ReadConfigDiag
.TYPE_SYS_CONFIG
, RC_SYS_CONFIG_OBJ
, RC_MEM_IMAGE
)
# a Repository object for variables that should be availabe globally,
# this is most useful in interactive mode, where we may need to eval()
# commands at various locations.
riesReposit
= Repository
.Repository()
riesReposit
.riesling
= rsys
riesReposit
.topName
= SYSTEM
# record main program's globals so other functions deep in the code
# structure can have access to top layer values.
riesReposit
.globals = globals()
riesReposit
.sysAddr
= sysAddr
riesReposit
.ncpus
= ncpus
riesReposit
.ncores
= ncores
riesReposit
.nucores
= nucores
riesReposit
.nstrands
= nstrands
riesReposit
.optdir
= optdir
riesReposit
.cores
= cores
riesReposit
.ucores
= ucores
riesReposit
.strands
= strands
#declare variables for single, double and quad fp regs
# is it ok to put n2 specific constants here? ---> fp is generic enough
riesReposit
.arch
= optdir
['--ar']
# create a command parser
# we have to import CmdParserNiCmd* at this level so that all the
# RS_zzz() can be globally available
parms
= { 'confRC':configRC
, 'reposit':riesReposit
, 'parser':parser
}
# register @def/def functions' doc, if any
# load personal customization, if available
filename
= os
.environ
.get('PYTHONSTARTUP')
if filename
and os
.path
.isfile(filename
):
#sys.stderr.write('# Executing User Startup %s .. \n' % filename)
#sys.stderr.write("#DBX %s: ready for interactive mode\n" % (time.ctime()))
if optdir
.has_key('--blaze'):
# parse blaze options, if available
rcCmd
= blazercCmd(newBlazeCmd
)
#sys.stderr.write('#DBX: rcCmd=%s\n' % (rcCmd)) #DBX
# register blaze cmooand map
samCmdMap
= SamCmdMap
.SamCmdMap()
parser
.registerCmdMap(samCmdMap
)
# register new commands introduced by sysconf in blazerc
samCmdMap
.cmdMap
.append(cmd
)
# switch to use 'sam' as prompt symbol, sam is blaze
# infrastructure plus risling core
newline
= '@conf.%s.%s = sam' % (RC_SYS_CONFIG_OBJ
, RC_PROMPT
)
configRC
.setDataLine(newline
)
# Historically a -p introduced pythoon script, so for now keep checking
files
= optdir
['-p'].split(',')
# The modern way is to treat all command line arguments that have no
# dash something prefix as python scripts that are executed in the
if optdir
.has_key('scripts'):
for script
in optdir
['scripts']:
#interact(banner="Sam Frontend Reader", local=vars(), **parms)
# merge (optional) upper level globals() before go into the customized
interact(banner
="Sam Frontend Reader", local
=uppers
, **parms
)