Delay in bitvector Output assignment - systemc

I am a beginner in SystemC, and I really need your help in solving a timing issue.
Please find the stimuli.h code below,
SC_MODULE(datagen)
{
public:
sc_out<sc_bv<8>> busin_o;
SC_CTOR(datagen);
/*private:*/
void testgen(void);
void asTakt(void);
};
void datagen::testgen(void)
{
busin_o->write("11111111");
cout<< "-------------------------------------"<< endl;
cout << "In dataGen::testgen: #"
<< sc_time_stamp()
<< " Busin in datagen: "<< busin_o
<<endl;
wait(1,SC_NS);
cout<< sc_delta_count() << endl;
busin_o->write("00111111");
cout<< "-------------------------------------"<< endl;
cout << "In dataGen::testgen: #"
<< sc_time_stamp()
<< " Busin in datagen: "<< busin_o
<<endl;
wait(1,SC_NS);
busin_o->write("10000111");
cout<< "-------------------------------------"<< endl;
cout << "In dataGen::testgen: #"
<< sc_time_stamp()
<< " Busin in datagen: "<< busin_o
<<endl;
wait(1,SC_NS);
busin_o->write("11111110");
cout<< "-------------------------------------"<< endl;
cout << "In dataGen::testgen: #"
<< sc_time_stamp()
<< " Busin in datagen: "<< busin_o
<<endl;
cout<<"Intended end of simulation"<< endl;
sc_stop();
}
inputs2.h
SC_MODULE(inputs)
{
public:
sc_in<sc_bv<8>> busin;
sc_out<sc_bv<8>> pout;
sc_out<sc_bv<8>> out;
SC_CTOR(inputs);
private:
/* method*/
void mydesign(void);
};
inputs2.cpp
inputs::inputs(sc_module_name inst)
: sc_module(inst)
{
cout<<"Constructor- inputs: "<< name() <<endl;
SC_METHOD(mydesign);
sensitive << busin;
}
void inputs::mydesign()
{
cout<< "-------------------------------------"<< endl;
cout<<"Mydesign Activated # "<<sc_time_stamp() <<endl;
cout<< "-------------------------------------"<< endl;
cout << "In Inputs::mydesign: #"
<< sc_time_stamp()
<< " Busin in Inputs: "<< busin
<<endl;
pout-> write(busin.read());
cout << "In Inputs::mydesign: #"
<< sc_time_stamp()
<< " pout in Inputs: "<< pout
<<endl;
}
The output that is seen in the terminal is shown below.
Copyright (c) 1996-2018 by all Contributors,
ALL RIGHTS RESERVED
Warning: (W506) illegal characters: data generator substituted by data_generator
In file: ../../../src/sysc/kernel/sc_object.cpp:247
Constructor- datagen: topblock.data_generator
Constructor- inputs: topblock.inputs
Constructor- top :topblock
Simulation started time resolution :1 ps
-------------------------------------
Mydesign Activated # 0 s
-------------------------------------
In Inputs::mydesign: #0 s Busin in Inputs: 00000000
In Inputs::mydesign: #0 s pout in Inputs: 00000000
-------------------------------------
In dataGen::testgen: #0 s Busin in datagen: 00000000
-------------------------------------
Mydesign Activated # 0 s
-------------------------------------
In Inputs::mydesign: #0 s Busin in Inputs: 11111111
In Inputs::mydesign: #0 s pout in Inputs: 00000000
2
-------------------------------------
In dataGen::testgen: #1 ns Busin in datagen: 11111111
-------------------------------------
Mydesign Activated # 1 ns
-------------------------------------
In Inputs::mydesign: #1 ns Busin in Inputs: 00111111
In Inputs::mydesign: #1 ns pout in Inputs: 11111111
-------------------------------------
In dataGen::testgen: #2 ns Busin in datagen: 00111111
-------------------------------------
Mydesign Activated # 2 ns
-------------------------------------
In Inputs::mydesign: #2 ns Busin in Inputs: 10000111
In Inputs::mydesign: #2 ns pout in Inputs: 00111111
-------------------------------------
In dataGen::testgen: #3 ns Busin in datagen: 10000111
Intended end of simulation
Info: /OSCI/SystemC: Simulation stopped by user.
The two questions I have are,
1) mydesign block is being called twice # 0 NS
2) Why does busin in my datagen file update after 1ns? I can already see the value in inputs.cpp at 0 NS. How can it happen that busin gets its value in datagen but updates in inputs.cpp first.
(Note : Inputs.cpp file receives busin value from datagen)
If you say that the behavior is correct and I don't have to modify anything in my code, then it's all good.
Any help is appreciated. Thanks in advance.

For question #1, there are two activations at time zero because all SystemC processes will execute at time zero, without being triggered by sensitivities. The second activation is then triggered by your first write to the bus at time zero.
If you don't want automatic execution of the method at time zero, place dont_initialize() after specifying the method:
SC_METHOD(mydesign);
dont_initialize(); // this can go before or after sensitivities
sensitive << busin;
For question #2, the inputs block looks to be seeing the value earlier, but the real problem is that testgen is printing the previous value it had written to the bus. In your code, the value of busin_o is printed immediately after writing it:
busin_o->write("11111111");
cout<< "-------------------------------------"<< endl;
cout << "In dataGen::testgen: #"
<< sc_time_stamp()
<< " Busin in datagen: "<< busin_o
<<endl;
wait(1,SC_NS);
When doing a write() to an sc_out (or sc_signal), the new value does not take effect until you suspend the thread by calling wait(). To see the correct value that was applied, you need to print the value of busin_o after a wait(). One approach could be to put a zero-delay wait after the write:
busin_o->write("11111111");
wait(SC_ZERO_TIME);
cout<< "-------------------------------------"<< endl;
cout << "In dataGen::testgen: #"
<< sc_time_stamp()
<< " Busin in datagen: "<< busin_o
<<endl;
wait(1,SC_NS);
Note that you're seeing similar behavior with pout. Since the print of pout is done before crossing a wait, you are printing the previous value of pout.
To find more about this behavior, search for the terms "signal semantics" and "delayed assignment".

Related

Boost UDP Server

I am trying to convert a Boost UDP server from using raw pointers to using std::vector which I believe is possible from https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/buffer.html
However my vector is always filled with 0 values.
void start_receive()
{
std::vector<unsigned char> recv_buffer_(64);
socket_.async_receive_from(
boost::asio::buffer(recv_buffer_), remote_endpoint_,
boost::bind(&udp_server::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
std::cout << "Receive length is :" << recv_buffer_.size() << std::endl;
std::cout << "recv_buffer_ = \n";
for (unsigned char n : recv_buffer_)
std::cout << n << ' ';
std::cout << '\n';
}
Any ideas what I may be doing wrong ? socket_ is a udp::socket socket_; and the above code works with a unsigned char *

PIN input argument loggin/reading

I'm wondering if any of you guys have experience in reading/logging the input arguments of an instrumented method?
Technically it's so simple and straightforward when the method has some primitive data type as input parameters. For example, integer or double. However, when it's a more complex data type, like string, I faced some weird behavior from pin framework. In nutshell, I cannot recover the original data input.
Below you see my instrumentation function. It simply tries to print the input arguments passed to the instrumented method.
VOID start_of_execution(uint64_t hash_name, int type,
ADDRINT input_arg1,
ADDRINT input_arg2
){
cout << "int arg:" << input_arg1 << endl; // prints the integer input
cout << "string*:" << hex << input_arg2 << endl; // prints the pointer value
cout << "string arg:" << *(reinterpret_cast<string*>(input_arg2)) << endl; // prints nothing!
}
Here is the instrumented method which it's the only functionality is to print the input arguments.
int sample_method(int i, string* str){
cout << "-----" << endl;
cout << "i: " << i << ", str:" << *str <<", ptr:" << str << endl;
return 0;
}
Below is the output:
$ sudo ../../../pin -t obj-intel64/code/mypin.so -- ./code/a.out
int arg:5
string*:0x1704434923
string arg:
-----
i: 5, str:sample text, ptr:0x1704434923
As you see, the integer argument (number 5) has been retrived properly. However, even though the string pointer is valid, PIN wasn't able to recover the string input argument.
Does anybody have an idea about my problem?

getline(cin, string) not giving expected output

Using c++14. I have read many posts regarding the problem.
If I run this code below, it jumps over the getline lines.
#include <iostream>
#include "main_menu.h"
void MainMenu::AddTest()
{
std::string courseName = "";
std::string testName = "";
std::string date = "";
std::cout << "Enter course name: " << std::endl;
std::getline(std::cin, courseName);
std::cout << "Enter test name: " << std::endl;
std::getline(std::cin, testName);
std::cout << "Enter test date: " << std::endl;
std::getline(std::cin, date);
Test test(courseName, testName, date);
tests.Add(test);
std::cout << "Test registered : " << std::endl;
tests.Print(test.id);
}
If I add cin ignore after each getline lines (example below how I implement it), it deletes some characters from the input strings and uses wrong variables to store them. Note that I have strings with whitespaces.
std::getline(std::cin, courseName);
std::cin.ignore();
This is what I get:
Enter course name:
History 2
Enter test name:
History 2 exam
Enter test date:
2017.01.02
Test registered :
test id = 2, course name = , test name = istory 2, date = istory 2 exam
I also tried to flush cout, didn't help.
My Print function works like a charm, if I add courses manually from main, I get the expected output, so the problem is definitely the cin / getline.
Test registered :
test id = 1, course name = History 2, test name = History 2 exam , date = 01.02.2017
I use getline as explained here: http://www.cplusplus.com/reference/string/string/getline/?kw=getline
Any help would be much appreciated, thank you.
By using cin.ignore you are messing with the input itself. If you want to get rid of \n character you don't have to! getline will just do that automatically. So just don't use ignore function and the code will be fine.
This works:
#include<iostream>
using namespace std;
int main()
{
string courseName = "";
string testName = "";
string date = "";
cout << "Enter course name: " << std::endl;
getline(std::cin, courseName);
cout << "Enter test name: " << std::endl;
getline(std::cin, testName);
cout << "Enter test date: " << std::endl;
getline(std::cin, date);
cout << courseName << endl;
cout << testName << endl;
cout << date << endl;
return 0;
}
I'm answering an ancient question, but try clearing the input stream before you use all the getline()'s. It is possible that you have some extra returns in the buffer before you ask for input.
cin.clear();
cin.ignore(INT_MAX);

C++ Do while loop advice

So at my college we haven't covered do while loops yet so I wanted to try one out before we do but I am running into an issue with it, I want the program to carry out different functions based on the users input, A carries out an addition, B a subtraction and C exits the program.
The program I have wrote with a do while in it carries out these functions one after the other regardless of what the user has input and I am unsure how to get this working properly.
Any advice is much appreciated.
Couple things to note. You have user entering a character for a choice, so make userChoice a character. Notice how the do-while is used for input-validation in this case. I changed the extra while loops with semi-colons at end to condition statements. You can substitute the if and else ifs with switch, "menu driven program". Here is a working program that you want. If you want the do-while around the whole condition statement and not use it as input validation then have it around all the condition statements and check while it is not C.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char userChoice;
int vaulue1;
int vaulue2;
int addtionValue;
int subtractionValue;
cout << "Choice A: will preform an addition" << endl;
cout << "Choice B: will preform a subtraction" << endl;
cout << "Choice C: will quit\n" << endl;
do
{
cout << "Enter your choice\n" << endl;
cin >> userChoice;
} while(userChoice != 'A' && userChoice != 'B' && userChoice != 'C');
if(userChoice == 'A')
{
cout << "Addition\n" << endl;
cout << "Please enter the first value you want to add\n" << endl;
cin >> vaulue1;
cout << "Please enter the second value you want to add\n" << endl;
cin >> vaulue2;
addtionValue = vaulue1 + vaulue2;
cout << "The addtion answer is " << addtionValue << endl;
}
else if(userChoice == 'B')
{
cout << "Subtraction\n" << endl;
cout << "Please enter the first value you want to subtract\n" << endl;
cin >> vaulue1;
cout << "Please enter the second value you want to subtract\n" << endl;
cin >> vaulue2;
subtractionValue = vaulue1 - vaulue2;
cout << "The subtract answer is " << subtractionValue << endl;
}
else if(userChoice == 'C')
{
cout << "Exit ";
return -1;
}
return 0;
}
you should use only one "while" - just first while is correct
other "whiles" are empty loop; clean them
it's better to use "switch - case"
while you enter A as input your method wants to give A another one

Missing <ofstream>: No such file or Directory

Running Ubuntu 12.04, using the g++ compiler and have the essential-build installed. Currently trying to get a small little program to work the code is below:
#include "Muon.h"
#include "Electron.h"
#include "Photon.h"
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <ofstream>
using std::ofstream;
#include <vector>
using std::vector;
using namespace std;
int main(){
cout << "Let's create a Particle vector!" << endl;
Electron* a = new Electron(10.0,20.0,30.0);
cout << "Electron created" << endl;
Muon* b = new Muon(30.0,20.0,10.0);
cout << "Muon created" << endl;
Photon* c = new Photon(20.0,10.0,30.0);
cout << "Photon created" << endl;
vector<Particle*> particlesContainer;
particlesContainer.push_back(a);
particlesContainer.push_back(b);
particlesContainer.push_back(c);
int size = particlesContainer.size();
cout << "size is: " << size << endl;
ofstream file("Muon_Vector.data");
if (file.is_open()){
for (int i = 0; i < size; i++){
file << particlesContainer[0]->getType << endl;
}
}
else {cout << "Unable to open file" << endl;}
file.close();
return 0;
}
Upon trying to compile I receive the error:
"intParticles.cpp:15:20: fatal error: ofstream: No such file or directory
compilation terminated."
This is after typing: "g++ -Wall Particle.cpp intParticles.cpp -o run"
Any help as to what is going wrong would be grateful. I thought ofstream was part of the standard library?
ofsteam is class but not the header file. It is defined in 'fstream' file