I am interfacing with a fis-gtm based mumps system and I needed some test data on my local Linux installaion (fis-gtm V6.2 on CENTOS 7).
So I read the manuals and came up with:--
set nword(1)="one"
set nword(2)="two"
set nword(3)="three"
set nword(4)="four"
set nword(5)="five"
set nword(6)="six"
set nword(7)="seven"
set nword(8)="eight"
set nword(9)="nine"
set nword(10)="ten"
set rn=^runcount+1
FOR i=1:1:10 DO
. FOR j=1:1:10 DO
.. set val="run:"_nword(rn)_" transaction:"_nword(i)_" update:"_nword(j)
.. w val,!
Which seems simple enough -- but I cannot get it to accept the inner DO loop.
Whatever I try I get:-
GTM>. FOR j=1:1:10 DO
%GTM-E-CMD, Command expected but not found
. FOR j=1:1:10 DO
^-----
I basically wasted a whole morning on this before giving up and downloading the perl bindings.
Is it at all possible to combine a nested DO with a simple FOR?
If so what was I doing wrong?
Bizarre -- the "." type nesting option works if you define a routine and compile but not for direct execution!
Solved eventually.
There should not be two spaces after your FOR iteration arguments, only one.
FOR i=1:1:10 DO
. FOR...
I know nothing about Mumps but here is a snippet I found online. Maybe it will help.
NESTLOOP
;.../loops/nested
;set up the 2D array with random values
NEW A,I,J,K,FLAG,TRIGGER
SET K=15 ;Magic - just to give us a size to work with
SET TRIGGER=20 ;Magic - the max value, and the end value
FOR I=1:1:K FOR J=1:1:K SET A(I,J)=$RANDOM(TRIGGER)+1
;Now, search through the array, halting when the value of TRIGGER is found
SET FLAG=0
SET (I,J)=0
FOR I=1:1:K Q:FLAG W ! FOR J=1:1:K WRITE A(I,J),$SELECT(J'=K:", ",1:"") SET FLAG=(A(I,J)=TRIGGER) Q:FLAG
KILL A,I,J,K,FLAG,TRIGGER
QUIT
Related
I am developing a CPU in VHDL. I am using ModelSim for simulation and testing. In the simulation script I load a program from a binary file to the instruction memory. Now I want to automatically check if the program fits into memory and abort simulation if it doesn't. Since the memory is basically an array of std_logic_vectors, all I would have to do is read the corresponding signal attribute for use in a comparison. My problem is: How do I access a VHDL signal attribute in TCL inside ModelSim?
The closest I have gotten so far is to use the describe command:
describe sim/:tb:uut:imem:mem_array
which prints something like
# Array(0 to 255) [length 256] of
# Array(31 downto 0) [length 32] of
# VHDL standard subtype STD_LOGIC
Now, of course I could parse the length out of there via string operations. But that would not be a very generic solution. Ideally I would like to have something like this:
set mem_size [get_attribute sim/:tb:uut:imem:mem_array'length]
I have searched stackoverflow, googled up and down and searched through the commands in the command reference manual, but I could not find a solution. I am confident there must be a rather easy solution and I just lack the proper wording to successfully search for it. To me, this doesn't look overly specific and I am sure this could come in hand on many occasions when automating design testing. I am using version 10.6.
I would be very grateful if an experienced ModelSim user could help me out.
Disclaimer: I'm not a Tcl expert, so there's probably a more optimized solution out there.
There's a command called examine that you can use to get the value of obejcts.
I created a similar testbench here with a 256 x 32 array, the results were
VSIM> examine -radix hex sim/:tb:uut:imem:mem_array
# {32'hXXXXXXXX} {32'hXXXXXXXX} {32'hXXXXXXXX} {32'hXXXXXXXX} {32'hXXXXXXXX} ...
This is the value of sim/:tb:uut:imem:mem_array at the last simulation step (i.e.,
now).
The command return a list of values for each match (you can use wildcards), so
in our case, it's a list with a single item. You can get the depth by counting
the number of elements it returns:
VSIM> llength [lindex [examine sim/:tb:uut:imem:mem_array] 0]
# 256
You can get the bit width of the first element by using examine -showbase -radix hex,
which will return 32'hFFFFFFFF, where 32'h is the part you want to parse. Wrapping
that into a function would look like
proc get_bit_width { signal } {
set first_element [lindex [lindex [examine -radix hex -showbase $signal] 0] 0]
# Replace everything after 'h, including 'h itself to return only the base
return [regsub "'h.*" $first_element ""]
}
Hope this gives some pointers!
So, I actually found an easy solution. While further studying of the command reference manual brought to light that it is only possible to access a few special signal attributes and length is not one of them, I noticed that ModelSim automatically adds a size object to its object database for the memory array. So I can easily use
set ms [examine sim/:tb:uut:imem:mem_array_size]
to obtain the size and then check if the program fits.
This is just perfect for me, elegant and easy.
I was wondering in what way you can group variables that patches-own to loop over them? I am using NetLogo 5.3.1.
Specifically I am doing this:
patches-own[some-variable other-variables]
to setup
gis:apply-coverage dataset-1 "some-variable" some-variable
;this line above for 1000 other-variables
end
and I would like to do it like this:
globals [group-variables]
patches-own [some-variable other-variables]
to setup
set group-variables (list some-variable other-variables)
foreach group-variables[
gis:apply-coverage dataset-1 "?" ?
]
end
But this seems to be impossible: setup is now turtle/patch only. I also got the message that gis:apply-coverage is expecting something, but got anything instead.
What other way can I use to group these variables somehow, without slowing the program down?
I have looked at lists, arrays and tables but the problem is the gis:apply-coverage demands a patch variable. This excludes arrays and tables. Lists would need to be defined in a patch context, but the gis:apply-coverage needs to be called in an observer context. The read-from-string variable does not support reading a variable and making a string of everything and then calling run on it does not improve execution speed.
I think the main problem is that you use the ? variable as a string ("?"). This cannot work, because it does not refer to the current foreach loop variable.
Maybe there are better solutions, but I got it to work by using the run primitive, which allows to create a command from a combination of strings and variables.
Here is a short example, using the countries dataset from the GIS code examples:
extensions[gis]
globals [group-vars shp]
patches-own [CNTRY_NAME POP_CNTRY]
to load-multiple-vars-from-shp
ca
; Load Data
set shp gis:load-dataset "C:/Program Files/NetLogo 5.3.1/app/models/Code Examples/GIS/data/countries.shp"
; Print properties
print gis:property-names shp
; Select two properties to write to patch-variable
set group-vars (list "CNTRY_NAME" "POP_CNTRY")
; Loop over group-vars
foreach group-vars
[
; Apply coverage of current variable
run (word "gis:apply-coverage shp \"" ? "\"" ?)
]
; Visualize patch variables to check if everything is working
ask patches
[
set plabel substring (word CNTRY_NAME) 0 1
set pcolor POP_CNTRY
]
end
My problem is simple: I'm trying to write a tcl script to use $grofile instead writing every time I need this file name.
So, what I did in TkConsole was:
% set grofile "file.gro"
% mol load gro ${grofile}
and, indeed, I succeeded uploading the file.
In the script I have the same lines, but still have this error:
wrong # args: should be "set varName ?newValue?"
can't read "grofile": no such variable
I tried to solve my problem with
% set grofile [./file.gro]
and I have this error,
invalid command name "./file.gro"
can't read "grofile": no such variable
I tried also with
% set grofile [file ./file.gro r]
and I got the first error, again.
I haven't found any simple way to avoid using the explicit name of the file I want to upload. It seems like you only can use the most trivial, but tedious way:
mol load file.gro
mol addfile file.xtc
and so on and so on...
Can you help me with a brief explanation about why in the TkConsole I can upload the file and use it as a variable while I can not in the tcl script?
Also, if you have where is my mistake, I will appreciate it.
I apologize if it is basic, but I could not find any answer. Thanks.
I add the head of my script:
set grofile "sim.part0001_protein_lipid.gro"
set xtcfile "protein_lipid.xtc"
set intime "0-5ms"
set system "lower"
source view_change_render.tcl
source cg_bonds.tcl
mol load gro $grofile xtc ${system}_${intime}_${xtcfile}
It was solved, thanks for your help.
You may think you've typed the same thing, but you haven't. I'm guessing that your real filename has spaces in it, and that you've not put double-quotes around it. That will confuse set as Tcl's general parser will end up giving set more arguments than it expects. (Tcl's general parser does not know that set only takes one or two arguments, by very long standing policy of the language.)
So you should really do:
set grofile "file.gro"
Don't leave the double quotes out if you have a complicated name.
Also, this won't work:
set grofile [./file.gro]
because […] is used to indicate running something as a command and using the result of that. While ./file.gro is actually a legal command name in Tcl, it's… highly unlikely.
And this won't work:
set grofile [file ./file.gro r]
Because the file command requires a subcommand as a first argument. The word you give is not one of the standard file subcommands, and none of them accept those arguments anyway, which look suitable for open (though that returns a channel handle suitable for use with commands like gets and read).
The TkConsole is actually pretty reasonable as quick-and-dirty terminal emulations go (given that it omits a lot of the complicated cases). The real problem is that you're not being consistently accurate about what you're really typing; that matters hugely in most programming languages, not just Tcl. You need to learn to be really exacting; cut-n-paste when creating a question helps a lot.
>> set signal_name [get_fanout abc_signal]
{xyz_blah_blah}
>> echo $signal_name
#142
>> set signal_name [get_fanout abc_signal]
{xyz_blah_blah}
>> echo $signal_name
#144
>>
I tried other stuff like catch etc, and every where, it returns #number. My goal is to be able to print the actual value instead of the number - xyz_blah_blah.
I am new to tcl. Want to understand, if this is an array or a pointer to an array or something like that. When I try the exact same thing with a different command, which returns just a value, then it works. This is a new command which returns value in parenthesis.
Please help. Thanks.
Every Tcl command produces a result value, which you capture and use by putting the call of the command in [square brackets] and putting the whole lot as part of an argument to another command. Thus, in:
set signal_name [get_fanout abc_signal]
the result of the call to get_fanout is used as the second argument to set. I suggest that you might also like to try doing this:
puts "-->[get_fanout abc_signal]<--"
It's just the same, except this time we're concatenating it with some other small string bits and printing the whole lot out. (In case you're wondering, the result of puts itself is always the empty string if there isn't an error, and set returns the contents of the variable.)
If that is still printing the wrong value (as well as the right one beforehand, without arrow marks around it) the real issue may well be that get_fanout is not doing what you expect. While it is possible to capture the standard output of a command, doing so is a considerably more advanced technique; it is probably better to consider whether there is an alternate mechanism to achieve what you want. (The get_fanout command is not a standard part of the Tcl language library or any very common add-on library like Tk or the Tcllib collection, so we can only guess at its behavior.)
here is an example of what I'm trying to do.
set t SNS
set ${t}_top [commands that return value]
Want to get the info stored at ${t}_top
puts “${t}_top”
SNS_top (really want the data stored there?)
Thought it was : ${{$t}_top} , maybe that was perl but {} inside the {} do not work.
One of the really interesting things about Tcl is that you can create variable names dynamically, as you are doing in the question you posted. However, this makes it tricky to write and makes your code harder than necessary to understand.
Instead of trying to figure out how to do the equivalent of ${{$t}_top}, it's arguably better to avoid the problem altogether. You can do that by using an associative array.
For example, instead of this:
set t SNS
set ${t}_top [commands that return value]
...
puts [set ${t}_top]
Do this:
set t SNS
set top($t) [commands that return value]
...
puts $top($t)
Most people agree that the latter example is much more readable.
try
puts [set ${t}_top]
Each line of code in Tcl is run through the substitution phase (in which variables, commands, etc are substituted) only once... generally. As such, something like
set var1 1
set var2 var1
set var3 $$var2
won't wind up with var3 equaling 1, since the substitutor will replace "$$var2" with "the value of the variable named '$var2' (literally)" and stop.
What you need it to either go about things another way or to force another round of substitution. The other way is generally to avoid needing a second round of substitution (as shown by Jackson):
set var3 [set $var2]
Here, the $var2 is replaced, during substitution, by "var1"... then [set var1] returns 1... then var3 gets set to the value of "1"... and you're good.
The syntax
puts [expr $${t}_top]
works as well, and avoids using the 'set' operation so a syntax error shouldn't overwrite your data.