How to count time in chisel with iotesters? - hdl

Is there a way to get the step value in iotesters ?
Currently I'm using a var counter but I'm sure there is a better way :
class MyTest (dut: MyModule) extends PeekPokeTester(dut) {
var timeCounter = 0
for(i <- 0 to 10) {
step(1)
timeCounter = timeCounter + 1
}
println("Step value is " + timeCounter)
Is there a getStepValue() like function to get that ?

You can get this using the (presently undocumented) method t.
There's an internal var simTime that is tracking time. This is automatically incremented on a step (just like how you're doing it). The method t lets you query the query its value.

Related

Kotlin: How to detect 'date change'?

I will use Timer() to execute function by 5 minutes in Kotlin.
And when I execute function by 5m, if a day passed,I want count var to be 0.
So my idea was
declare two vars
var todayDate = LocalDate.now() // 2019-09-23
var todayCount:Int = 0
After that I will check this vars in 5 minutes by using Timer()
Then todayDate value differs from previous todayDate, then I can detect date change.
However, I don't know how to compare current todayDate and previous todayDate.
Any idea? or is there any other way to know day change?
For your specific question about comparing dates you can use the isEqual() method on your LocalDate instance (docs). Something like the following would likely do what you want:
// initial state
var todayDate = LocalDate.now()
var todayCount = 0
// in each timer iteration:
val now = LocalDate.now()
if (!todayDate.isEqual(now)) {
// it's a new day
todayCount = 0
todayDate = now
} else {
// it's the same day
++todayCount
}
However if you're talking about Android and using its Timer class, you need to be aware that that runs on a background thread and you will need to persist your todayDate and todayCount values somewhere (which could be app preferences, your app DB, etc.).

How to use sql executeQuery method in groovy?

In groovy how can i use sql executeQuery method. In my below code i get the error as "Method executeQuery is protected in groovy.sql.Sql" on line 2. Please help!
def sql = new Sql(rdbDsService.getDataSource())
ResultSet rs=sql.executeQuery("select top 5 Id from MACHINES where (Total != Rightcount) order by FinishingTime");
order[0] = 0;
order[1] = 0;
order[2] = 0;
order[3] = 0;
count = 0;
while (rs.next())
{
order[count] = Integer.parseInt(rs["Id"].toString());
count = count + 1;
}
As per the message you're calling a protected method, the javadoc on the method states the following:
Useful helper method which handles resource management when executing
a query which returns a result set. Derived classes of Sql can
override "createQueryCommand" and then call this method to access the
ResultSet returned from the provided query or alternatively can use
the higher-level method of Sql which return result sets which are
funnelled through this method, e.g. eachRow, query.
Instead try using something like eachRow
sql.eachRow("select top 5 Id from MACHINES where (Total != Rightcount) order by FinishingTime") {
order[count] = it.Id
count = count + 1
}

How can this code be written shorter/clearer?

newRow("OrderReference") = line.Substring(line.IndexOf("*1003") + 5, line.IndexOf("*", line.IndexOf("*1003") + 5) - line.IndexOf("*1003") - 5)
There you have it. Very long and ugly. I was thinking about this:
Dim indexPlus = line.IndexOf("*1003") + 5
Dim indexMinus = line.IndexOf("*1003") - 5
newRow("OrderReference") = line.Substring(indexPlus, line.IndexOf("*", indexPlus) - indexMinus)
But that introduces new and meaningless vars. Unsatisfying.
Maybe RegEx is the savior here?
Unfortunately I mustn't change the input data :-(
The input data consist of the BWA-format (popular with books). Here you can see the part in question:
All codes in this example set are required. Only corresponding values change.
I don't even think your second code works. It seems more like this.
Dim index = line.IndexOf("*1003") + 5
newRow("OrderReference") = line.Substring(index, line.IndexOf("*", indexPlus) - index)
10 - 5 - 2 isn't the same as 10 - (5 - 2) but instead it's the same as 10 - (5 + 2).
Next time, check out the codereview stack exchange.
Given that your data is always constant, and what you're looking for always begins with "*1003", you don't need to use Regex (Even though you could). Just use what you're already using but with some corrections.
using System;
public class Program
{
public static void Main()
{
string input = "L10113540 VD44444 VD2002100234949 000116161 04201261\r\n";
input += " KN00010000000129000LPEUR003000001*1003A.Muller-Schulz*1017Bastei\r\n";
input += "Lubbe.61204 Laund.Meine Schuld*1019KL*102990300*1030NO*1032EUR*1131KT";
int start = input.IndexOf("*1003");
int end = input.IndexOf("*", start + 1);
string result = input.Substring(start + 5, end - start - 5);
Console.WriteLine(result);
// Your code
start = input.IndexOf("*1003") + 5;
end = input.IndexOf("*1003") - 5;
result = input.Substring(start, input.IndexOf("*", start) - end);
Console.WriteLine(result);
}
}
Result
A.Muller-Schulz
A.Muller-Schulz*1017Baste
You can see that what you posted in your question, doesn't give the results you want. All you're really looking for is just the next asterisk after the first "*1003". You can see the difference between your code and what I've given.
.NET Fiddle Example

Easiest way of defining and using of Global Variable

"first part" &&&& fun _ ->
let ident
"second part" &&&& fun _ ->
ident ....
I need to use variable "ident".
I just need to pass value of variable from first part of test to second one...
I want to ask you if there is any easy way how to define and use global variable or even if you have better (and easy) idea of doing that
Keep in mind, please, that I am a beginner, so I would prefer easier ones.
Global variables will often make your code difficult to work with - particularly if they are mutable.
Instead, consider returning the values you need to keep track of as composite values. An easy data type to start with would be a tuple:
let ``first part`` id =
let someOtherValue = "Foo"
someOtherValue, id + 1
This function takes an int (the current ID) as input, and returns string * int (a tuple where the first element is a string, and the second element and int) as output.
You can call it like this:
> let other, newId = ``first part`` 42;;
val other : string = "Foo"
val newId : int = 43
Notice that you can use pattern matching to immediately destructure the values into two named symbols: other and newId.
Your second function could also take an ID as input:
let ``second part`` id otherArgument =
// use id here, if you need it
"Bar"
You can call it like this, with the newId value from above:
> let result = ``second part`` newId "Baz";;
val result : string = "Bar"
If you find yourself doing this a lot, you can define a record for the purpose:
type Identifiable<'a> = { Id : int; Value : 'a }
Now you can begin to define higher-order functions to deal with such a type, such as e.g. a map function:
module Identifiable =
let map f x = { Id = x.Id; Value = f x.Value }
// Other functions go here...
This is a function that maps the Value of an Identifiable from one value to another, but preserves the identity.
Here's a simple example of using it:
> let original = { Id = 42; Value = "1337" };;
val original : Identifiable<string> = {Id = 42;
Value = "1337";}
> let result' = original |> Identifiable.map System.Int32.Parse;;
val result' : Identifiable<int> = {Id = 42;
Value = 1337;}
As you can see, it preserves the value 42, but changes the Value from a string to an int.
You can still change the ID explicitly, if you want to do that:
> let result'' = { result' with Id = 7 };;
val result'' : Identifiable<int> = {Id = 7;
Value = 1337;}
Since this was getting out of hand for comments this is how I would do it for an example
let mutable t = 0
let first =
t <- 1 + 1
//other stuff
let second =
//can use t here and it will have a value of 2
In some cases you have to use a ref:
let t = ref 0
let first =
t := 1 + 1
//other stuff
let second =
//can use t here and it will have a value of 2 -
// you use "!t" to get the value
If you define ident at the top of your file like this :
let ident = "foo"
// rest of your code using ident
ident are global and you can use in the next part of your file.
EDIT :
If ident wil change in the next part of your code, use this :
let ident = ref "foo"

dynamically change a part of the variable path

I know this question has been asked a bunch of times, but none of the answers (or at least what i took away from them) was a help to my particiular problem.
I want to dynamically change a part of the variable path, so i don't have to repeat the same code x-times with just two characters changing.
Here's what i got:
In the beginning of my script, i'm setting the reference to PlayerData scripts, attached to the GameManager object like this:
var P1 : P1_Data;
var P2 : P2_Data;
function Start(){
P1 = GameObject.Find("GameManager").GetComponent.<P1_Data>();
P2 = GameObject.Find("GameManager").GetComponent.<P2_Data>();
}
Later, i want to access these scripts using the currentPlayer variable to dynamically adjust the path:
var currentPlayer : String = "P1"; //this actually happens along with some other stuff in the SwitchPlayers function, i just put it here for better understanding
if (currentPlayer.PlayerEnergy >= value){
// do stuff
}
As i was afraid, i got an error saying, that PlayerEnergy was not a part of UnityEngine.String.
So how do I get unity to read "currentPlayer" as part of the variable path?
Maybe some parse function I haven't found?
Or am I going down an entirely wrong road here?
Cheers
PS: I also tried putting the P1 and P2 variables into an array and access them like this:
if (PlayerData[CurrentPlayerInt].PlayerEnergy >= value){
// do stuff
}
to no success.
First of all,
var currentPlayer : String = "P1"
here P1 is just string, not the previous P1/P2 which are referenced to two scripts. So, if you want, you can change
currentPlayer.PlayerEnergy >= value
to
P1.PlayerEnergy >= value
or,
P2.PlayerEnergy >= value
But if you just want one function for them, like
currentPlayer.PlayerEnergy >= value
Then you have to first set currentPlayer to P1/P2 which I assume you are trying to do. You must have some codes that can verify which player is selected. Then, maybe this can help -
var playerSelected: int = 0;
var currentPlayerEnergy: int = 0;
.....
//Use your codes to verify which player is selected and then,
if (playerSelected == 1) {
currentPlayerEnergy = P1.PlayerEnergy;
} else if (playerSelected == 2) {
currentPlayerEnergy = P2.PlayerEnergy;
}
//Now use your favorite function
if (currentPlayerEnergy >= value) {
//Do stuff
}
As there was no reply providing the answer I needed, I'll share the solution that did the trick for me, provided by a fellow student.
Instead of having the PlayerData scripts pre-written, I generate them using a public class function in a Playermanager script. This generates the Playerdata as attached scripts, saved into an array.
I can then access them through Playermanager.Playerlist[Playernumber].targetvariable.
Which is what I wanted to do, only with the Playerdata being attached to a script instead of a gameobject. And it works great!
Here's the full code of my Playermanager Script:
//initialise max players
public var maxplayers : int = 2;
// Initialise Playerlist
static var Players = new List.<PlayerData>();
function Start () {
for (var i : int = 0; i < maxplayers; i++){
var Player = new PlayerData();
Players.Add(Player);
Players[i].PlayerName = "Player " + i;
}
DontDestroyOnLoad (transform.gameObject);
}
public class PlayerData {
public var PlayerName : String;
public var PlayerEnergy : int = 15;
public var Fleet : List.<GameObject> = new List.<GameObject>();
}
As you see, you can put any type of variable in this class.
I hope this helps some of you who have the same problem.
cheers,
Tux