My goal is to create an ALU that adds and subtracts with a barrelshifter
alu.h
#include "systemc.h"
SC_MODULE(alu){
sc_in<bool> op;
sc_in<sc_int<8> > a;
sc_inout<sc_int<8> > b;
sc_out<sc_int<8> > output;
void alu_method();
SC_CTOR(alu) {
SC_METHOD(alu_method);
dont_initialize();
sensitive << a,b,op;
}
};
alu.cpp
#include "alu.h"
void ALU::alu_method(){
if (op.read() == 0){
//substract
out.write(in.read() - in_bs.read());
}
else{
//add
out.write(in.read() + in_bs.read());
}
}
barrelshift.h
#include <systemc.h>
void make_barrel();
SC_MODULE(barrel_shift) {
sc_in<bool> clk;
sc_in<bool> enable;
sc_in<bool> left_right;
sc_in<sc_uint<3> > shift_amt;
sc_in<sc_int<8> > din;
sc_inout<sc_int<8> > dout;
void barrel_method();
SC_CTOR(barrel_shift) {
SC_METHOD(barrel_method);
dont_initialize();
sensitive << clk.pos(); //edge sensitive
}
};
barrelshift.cpp
#include "barrelshift.h"
void barrel_shift :: barrel_method(){
if(enable.read() == 1){
if(left_right.read() == 0){ //shift left
dout.write(din.read() << shift_amt.read());
}else if(left_right.read() == 1){ // right shift
dout.write(din.read() >> shift_amt.read());
}
}
else
cout << "Not enabled "<<endl;
dout <= din;
}
sc_main.cpp
#include <systemc.h>
#include "alu.h"
#include "barrelshift.h"
int sc_main(int argc, char* argv[]){
sc_trace_file *tf;
//Signals
sc_signal <bool> enable, op, l_r;
sc_signal <sc_int<8> > a, output,b, bin;
sc_signal < sc_uint<3> > shift_amt;
//Clock
sc_clock clk("clk",10,SC_PS,0.5);
alu myALU("myALU");
barrel_shift myShifter("myShifter");
myALU.a(a);
myALU.b(b);
myALU.output(output);
myALU.op(op);
myShifter.clk(clk);
myShifter.din(bin);
myShifter.enable(enable);
myShifter.left_right(l_r);
myShifter.shift_amt(shift_amt);
myShifter.dout(b);
tf = sc_create_vcd_trace_file("trace_file");
sc_trace(tf, clk, "clk");
sc_trace(tf, a, "a");
sc_trace(tf, bin, "BarrelShifter In");
sc_trace(tf, op, "op");
sc_trace(tf, shift_amt, "shift_amt");
sc_trace(tf, l_r, "left_right");
sc_trace(tf, enable, "enable");
sc_trace(tf, b, "b");
sc_trace(tf, output, "output");
sc_close_vcd_trace_file(tf);
cout << "The result from the ALU is: " << output.read();
}
There are no errors when I build it. But whenever I try to execute it, I get the following error:
Error: (E112) get interface failed: port is not bound: port 'myALU.port_0' (sc_in)
In file: sc_port.cpp:231
What is causing this and how can I fix it?
The error message
Error: (E112) get interface failed: port is not bound: port 'myALU.port_0' (sc_in)
means that the port myALU.port_0 is not bound to a signal. But which port in the alu module corresponds to port_0?
It is good practice to name all ports and signals -- regardless of the type of hardware description language you are using -- to make errors like this easier to diagnose.
Name the ports in the alu constructor:
SC_CTOR(alu) :
op("op"),
a("a"),
b("b"),
output("output")
{
// ...
I couldn't reproduce the error you were seeing. I saw this error (after providing names for all ports and signals):
Error: (E115) sc_signal<T> cannot have more than one driver:
signal `signal_5' (sc_signal)
first driver `myShifter.dout' (sc_inout)
second driver `myALU.b' (sc_inout)
I noticed some other problems in your code:
Your code doesn't compile because the wrong variable names are used in alu_method().
sc_start() is not called in sc_main().
There is an invalid sensitive call in alu() -- should be sensitive << a << b << op;
The problem was with the sensitivity list for alu. It should have been:
sensitive << a << b << op;
As noted correctly by #DarrylLawson, this error message indicates that the the port myALU.port_0 is not bound to a signal (and you would get a clearer name if you had given the port a name in the constructor).
An important nuance imho is that it means the port is not bound to a signal at the time the error is given. You may have code that binds it, but this is only executed at some point during elaboration. If you try to use the port before that (e.g. inside the constructor of your module), you can still get this error message. In that case the error would occur at time 0.
Related
Sorry if the question is not clear. i'll try to explain it here.
I am working on a test project where two nodes will be communicating specialty packets to each other. As in: Node A will be sending a packet to Node B, and while Node B is generating it's packet, Node B Should also be processing the information from Node A. Node A should be preparing another packet for when Node B.
I've been reading into std::future but I'm not sure i understand how it works. The question i have is about the large section of commented out code. This is just psuedo code so i can try and get a better understanding about asynchronous functions in C++. I normally program in C#/Java where Asynchronous functions are fairly simple(ish). I hope the above kind of explains what i'm trying to do.
The code below might have some other errors. this is just a quick-and-dirty attempt to try and convey what i am asking.
My questions are:
What is the best way to check if Node B is ready, assuming that Node B is a separate instance of this theoretical app, running on a different device? if this makes any difference, this is being written with Linux C++.
Is this even the correct way of doing Tasks/Await/Await Async in C++? If this is incorrect, what is the proper way?
Thanks in advance.
//Excerpt from pseudo code. file would be kw_worker_delegate.h
#include <future>
#include <iostream>
#include <string>
#include <optional>
#include <system_error>
#include "kw_network.h"
class kw_worker
{
private:
/* ... */
std::future<kw_packet> *kw_packet_receive_delegate;
std::future<bool> *kw_packet_send_delegate;
bool worker_is_ready;
std::string kw_worker_adress;
network_interface *kw_network_interface;
/* Base CTOR that provides Delegates */
public:
kw_worker(std::future<kw_packet> *kwpktr, std::future<kw_packet> *kwpkts, network_interface *kwinetf, std::string kwaddr);
bool kw_worker_execute(bool isSendOrRecieve, kw_worker *active_worker, kw_worker *target_worker, std::optional<kw_packet> packet_data = std::nullopt);
};
kw_worker::kw_worker(std::future<kw_packet> *kwpktr, std::future<bool> *kwpkts, network_interface *kwinetf, std::string kwaddr)
{
&kw_packet_recieve_delegate = &kwpktr;
&kw_packet_send_delegate = &kwpkts;
&kw_network_interface = &kwintef;
kw_worker_address = kwaddr;
kw_worker_is_ready = true;
}
bool kw_Worker::kw_worker_execute(bool isSendOrRecieve, kw_worker *active_worker, kw_worker *target_worker, std::optional<kw_packet> packet_data = std::nullopt);
{
try
{
//if(isSendOrRecieve)
//{
// IS THIS CORRECT?
//
// if(!(&target_worker->worker_is_ready))
// {
// cout << "Worker B is not ready for a data submission...\n";
// do_something_or_await();
// }else{
// if(packet_data.has_value())
// kw_packet_send_delegate = std::async (&kw_network_interface->send_to, &target_worker->kw_worker_adress, packet_data);
// else throw -1;
// cout << "The data was sent to Worker B. Waiting for response on receipt of data...\n";
// &active_worker->worker_is_ready = true;
//
// //Do somewithng else...
// }
//}else{
// if(!(&active_worker->worker_is_ready))
// {
// cout << "Worker A is not ready to receive data...\n";
// do_something_or_await();
// }else{
// kw_packet_receive_delegate = std::async (&kw_network_interface->receive_from, &target_worker->kw_worker_adress);
// cout << "The data was received by Worker A. A Will now process the data...\n";
// &active_worker->kw_worker_is_ready = false;
//
// //Worker B will set it's worker_is_ready boolean value using it's send function
//
// //process the data now
// }
//}
}catch(const system_error &e)
{
cout << "There are no threads available to complete delegation. Please Try Again later";
return false;
}
catch(int)
{
cout << "Some other error occurred when getting data from worker" << &target_worker << ". Please Try Again later.";
return false;
}
return true;
}
I'm despairing! I want to have my ESP8266 receive a string from a TCP client, execute the corresponding function and give a TCP response. But unfortunately the response string gets corrupted in some strange way:
Assuming that I enter an 'unknown command', the first 11 bytes are printed correctly via the serial interface (the rest is dump) and the first 11 bytes received by the client are dump, but the rest is correct (see comments in script below). But when I enter the 'dim' command the result is correct (but the return string is also shorter than "error: unknown command").
For the moment I have absolutely no idea how to fix this, even though I've tried a lot.
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <string.h>
struct parsed_query{
String command;
String arguments;
};
struct parsed_query parser(void){
// this function receives and parses a query
struct parsed_query result;
result.command="entered command";
result.arguments="entered arguments"
return result
}
char* str2char(String as_string){
int i_0=0;
while(as_string[i_0]!='\0'){i_0++;}
char as_char[i_0+1];
as_char[i_0]='\0';
for(int i=0;i<i_0;i++){
as_char[i]=as_string[i];
}
return as_char;
}
String executor(String command,String arguments){
String response;
if(command=="dim"){
response="dimming";
}
else if(command=="on"){
response="switching ON";
}
else{
response="error: unknown command";
}
return response;
}
void setup(){
// initialize serial interface, wifi & tcp-server
Serial.begin(115200);
WiFi.begin("<SSID>","<PASSWORD>");
while (WiFi.status() != WL_CONNECTED){delay(500);}
TCPserver.begin();
}
void loop() {
if(!client.connected()){
client=TCPserver.available();
}else{
struct parsed_query query=parser();
// This prints "error: unkno??*/???*??"
Serial.println(str2char(executor(query.command,query.arguments)));
// here, the client receives "????**?*??*?wn command"
client.write(str2char(executor(query.command,query.arguments))));
}
}
I have two ideas what could be causing this result (even though I don't know where to fix it in my code):
Case 1:
Maybe, I meshed up call-by-reference and call-by-value at some point (if yes, where??)
Case 2:
My programm is causing a stack overflow (if yes, where??)
Any help highly appreciated as I don't want to spend one more night.
In str2char you're returning a pointer to a local array, but like every local variable, it doesn't exist anymore after the function has returned. So reading from the returned pointer causes undefined behavior.
Compiling with warnings enabled (which is highly recommended) should output something like:
warning: address of local variable 'as_char' returned
(one) correct code would be
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <string.h>
#define TCP_RESPONSE_L 1024
struct parsed_query{
String command;
String arguments;
};
struct parsed_query parser(void){
// this function receives and parses a query
struct parsed_query result;
result.command="entered command";
result.arguments="entered arguments"
return result
}
int str2char(char *as_char, String as_string, int max_length){
int i_0=0;
while(as_string[i_0]!='\0'){
if(i_0>=max_length){as_string="error: caught an overflow! increase TCP_BUFFER_L";break;}
i_0++;
}
as_char[i_0]='\0';
for(int i=0;i<i_0;i++){
as_char[i]=as_string[i];
}
return 1;
}
String executor(String command,String arguments){
String response;
if(command=="dim"){
response="dimming";
}
else if(command=="on"){
response="switching ON";
}
else{
response="error: unknown command";
}
return response;
}
void setup(){
// initialize serial interface, wifi & tcp-server
Serial.begin(115200);
WiFi.begin("<SSID>","<PASSWORD>");
while (WiFi.status() != WL_CONNECTED){delay(500);}
TCPserver.begin();
}
void loop() {
if(!client.connected()){
client=TCPserver.available();
}else{
struct parsed_query query=parser();
char response[TCP_RESPONSE_L];
str2char(response,executor(query.command,query.arguments),TCP_RESPONSE_L);
//prints fine
Serial.println(str2char(executor(query.command,query.arguments)));
//correctly sending to client
client.write(str2char(executor(query.command,query.arguments))));
}
}
I would like to check the difference between using sc_buffer and sc_signal. I have coded a module which adds two random numbers and then I run two tests in parallel: one using sc_buffer and the other using sc_signal. Nevertheless, when I check with gtkwave I see the same traces for both examples, so I think for this case there should not be any difference. How can I check the difference? or is it that these two different types of channel are intended for different applications?
sc_buffer is probably most useful when modeling at an abstract level.
For example, consider modeling a serial communication channel. The transmitter could send the same character twice in a row. If an sc_signal was used as the channel, the receiver wouldn't detect the second character, but with an sc_buffer, it would.
#include <systemc>
#include <iostream>
using namespace sc_core;
using namespace std;
struct Transmitter : public sc_module {
sc_out<char> out;
Transmitter(sc_module_name name) : sc_module(name) {
SC_THREAD(transmit);
}
void transmit() {
wait(1, SC_NS);
out.write('x');
wait(1, SC_NS);
out.write('x');
wait(1, SC_NS);
out.write('y');
};
SC_HAS_PROCESS(Transmitter);
};
struct Receiver : public sc_module {
sc_in<char> in;
Receiver(sc_module_name name) : sc_module(name) {
SC_METHOD(receive);
sensitive << in;
dont_initialize();
}
void receive() {
cout << sc_time_stamp() << ": " << name() << " received "
<< in.read() << endl;
}
SC_HAS_PROCESS(Receiver);
};
int sc_main(int argc, char* argv[])
{
sc_signal<char> signal;
sc_buffer<char> buffer;
Transmitter signal_transmitter("signal_transmitter");
Receiver signal_receiver("signal_receiver");
Transmitter buffer_transmitter("buffer_transmitter");
Receiver buffer_receiver("buffer_receiver");
signal_transmitter.out(signal);
signal_receiver.in(signal);
buffer_transmitter.out(buffer);
buffer_receiver.in(buffer);
sc_start();
return 0;
}
The above example produces this output:
1 ns: signal_receiver received x
1 ns: buffer_receiver received x
2 ns: buffer_receiver received x
3 ns: signal_receiver received y
3 ns: buffer_receiver received y
Notice that signal_receiver didn't detect the character sent at 2 ns.
You won't see any difference in a VCD trace, because the values stored in the sc_buffer and sc_signal channels are identical. The difference is when the receiver is triggered.
You can look at the answer below for the differences between sc_buffer and sc_signal.
In SystemC, can the sc_signal_in/out type port be bound to the primary channel sc_buffer?
sc_buffer is basically derived from sc_signal and it re-implements the write and update functions to generate notification for every change.
So if your generate new numbers which are same as the previous number written into the channel and you are dumping some output at each event notification you should see some difference.
There are several ways to list serial ports under Windows but I'm not sure what is the proper way: the way that does detect all serial ports that are available.
One good code example is http://www.naughter.com/enumser.html - where there are 9 (nine!) ways of enumerating serial devices.
The question is: what is the optimal way of doing it.
Requirements:
to not open ports in order to check if they are available.
to be able to detect ports with different names than COMx.
to work on Windows XP SP2 or above
void SelectComPort() //added function to find the present serial
{
TCHAR lpTargetPath[5000]; // buffer to store the path of the COMPORTS
DWORD test;
bool gotPort=0; // in case the port is not found
for(int i=0; i<255; i++) // checking ports from COM0 to COM255
{
CString str;
str.Format(_T("%d"),i);
CString ComName=CString("COM") + CString(str); // converting to COM0, COM1, COM2
test = QueryDosDevice(ComName, (LPSTR)lpTargetPath, 5000);
// Test the return value and error if any
if(test!=0) //QueryDosDevice returns zero if it didn't find an object
{
m_MyPort.AddString((CString)ComName); // add to the ComboBox
gotPort=1; // found port
}
if(::GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
lpTargetPath[10000]; // in case the buffer got filled, increase size of the buffer.
continue;
}
}
if(!gotPort) // if not port
m_MyPort.AddString((CString)"No Active Ports Found"); // to display error message incase no ports found
}
If you can access the registry, the HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM key contains a list of COM ports Windows currently supports (in some cases, this information may be stale/incorrect; like, I suspect, when a plug & play device providing serial ports has not completed detection/installation or has been recently removed).
This is the way .NET Framework's SerialPort.GetPortNames() method reports available COM ports, and the above information is derived from the linked page.
Serial ports are very simple devices, dating from the stone age of computing hardware. They don't support Plug & Play, there is no way to tell that somebody plugged in a device. The only thing you can do is discover what ports are available, the SerialPort.GetPortNames() returns the list. Some USB emulators can generate a descriptive name to go with the port name, you can discover those with WMI, Win32_SerialPort class.
None of which helps you discover what COM port is connected to a particular device. Only a human knows, she physically plugged the cable in the connector. You'll need to provide a config UI that lets the user select the port number. A combo box gets the job done. Save the selection in your config data, it is very likely that the device is still connected to the same port the next time your program starts.
This is a modernized version of #michael-jacob-mathew's answer:
#include <iostream>
#include <string>
#include <Windows.h>
bool SelectComPort() //added function to find the present serial
{
char lpTargetPath[5000]; // buffer to store the path of the COMPORTS
bool gotPort = false; // in case the port is not found
for (int i = 0; i < 255; i++) // checking ports from COM0 to COM255
{
std::string str = "COM" + std::to_string(i); // converting to COM0, COM1, COM2
DWORD test = QueryDosDevice(str.c_str(), lpTargetPath, 5000);
// Test the return value and error if any
if (test != 0) //QueryDosDevice returns zero if it didn't find an object
{
std::cout << str << ": " << lpTargetPath << std::endl;
gotPort = true;
}
if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
}
}
return gotPort;
}
It produces the following output on my computer:
COM1: \Device\Serial0
COM3: \Device\VCP0
Modified #Dženan answer to use wide characters and returning list of ints
#include <string>
#include <list>
list<int> getAvailablePorts()
{
wchar_t lpTargetPath[5000]; // buffer to store the path of the COM PORTS
list<int> portList;
for (int i = 0; i < 255; i++) // checking ports from COM0 to COM255
{
wstring str = L"COM" + to_wstring(i); // converting to COM0, COM1, COM2
DWORD res = QueryDosDevice(str.c_str(), lpTargetPath, 5000);
// Test the return value and error if any
if (res != 0) //QueryDosDevice returns zero if it didn't find an object
{
portList.push_back(i);
//std::cout << str << ": " << lpTargetPath << std::endl;
}
if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
}
}
return portList;
}
You can check the windows registry base to list all COM ports. Here is my code > github file
CUIntArray ports;
EnumerateSerialPorts(ports);
for (int i = 0; i<ports.GetSize(); i++)
{
CString str;
str.Format(_T("COM%d"), ports.ElementAt(i));
m_ctlPort.AddString(str);
}
I'm trying to do some simple packet capturing with pcap, and so I've created a handle to listen through eth0. My issue is with the pcap_loop(handle, 10, myCallback, NULL); line near the end of my code. I'm trying to use pcap_loop.
The expected output is supposed to be:
eth0
Activated!
1
2
3
...
10
Done processing packets!
Current output is missing the increments:
eth0
Activated!
Done processing packets!
Currently it's just skipping right through to "Done processing packets!" and I have no idea why. Even if it doesn't go to the callback, it should still be waiting on packets as the ;count' parameter (see documentation for pcap_loop) is set to 10.
#include <iostream>
#include <pcap.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void myCallback(u_char *useless, const struct pcap_pkthdr* hdr, const u_char*packet){
static int count = 1;
std::cout <<count<<std::endl;
count ++;
}
int main(){
char errbuf[PCAP_ERRBUF_SIZE];
char * devName;
char* net;
char* mask;
const u_char*packet;
struct in_addr addr;
struct pcap_pkthdr hdr;
bpf_u_int32 netp;
bpf_u_int32 maskp;
pcap_if_t *devs;
pcap_findalldevs(&devs, errbuf);
devName = pcap_lookupdev(errbuf);
std::cout <<devName<<std::endl;
int success = pcap_lookupnet(devName, &netp, &maskp, errbuf);
if(success<0){
exit(EXIT_FAILURE);
}
pcap_freealldevs(devs);
//Create a handle
pcap_t *handle = pcap_create(devName, errbuf);
pcap_set_promisc(handle, 1);
pcap_can_set_rfmon(handle);
//Activate the handle
if(pcap_activate(handle)){
std::cout <<"Activated!"<<std::endl;
}
else{
exit(EXIT_FAILURE);
}
pcap_loop(handle, 10, myCallback, NULL);
std::cout <<"Done processing packets!"<<std::endl;
//close handle
pcap_close(handle);
}
pcap_findalldevs(&devs, errbuf);
That call isn't doing anything useful, as you're not doing anything with devs other than freeing it. (You also aren't checking whether it succeeds or fails.) You might as well remove it unless you have some need to know what all the devices on which you can capture are.
pcap_can_set_rfmon(handle);
That all isn't doing anything useful, as you're not checking its return value. If you are capturing on a Wi-Fi device, and you want to capture in monitor mode, you call pcap_set_rfmon() - not pcap_can_set_rfmon() - on the handle after creating and before activating the handle.
//Activate the handle
if(pcap_activate(handle)){
std::cout <<"Activated!"<<std::endl;
}
else{
exit(EXIT_FAILURE);
}
To quote the pcap_activate() man page:
RETURN VALUE
pcap_activate() returns 0 on success without warnings, PCAP_WARN-
ING_PROMISC_NOTSUP on success on a device that doesn't support promis-
cuous mode if promiscuous mode was requested, PCAP_WARNING on success
with any other warning, PCAP_ERROR_ACTIVATED if the handle has already
been activated, PCAP_ERROR_NO_SUCH_DEVICE if the capture source speci-
fied when the handle was created doesn't exist, PCAP_ERROR_PERM_DENIED
if the process doesn't have permission to open the capture source,
PCAP_ERROR_RFMON_NOTSUP if monitor mode was specified but the capture
source doesn't support monitor mode, PCAP_ERROR_IFACE_NOT_UP if the
capture source is not up, and PCAP_ERROR if another error occurred. If
PCAP_WARNING or PCAP_ERROR is returned, pcap_geterr() or pcap_perror()
may be called with p as an argument to fetch or display a message
describing the warning or error. If PCAP_WARNING_PROMISC_NOTSUP,
PCAP_ERROR_NO_SUCH_DEVICE, or PCAP_ERROR_PERM_DENIED is returned,
pcap_geterr() or pcap_perror() may be called with p as an argument to
fetch or display an message giving additional details about the problem
that might be useful for debugging the problem if it's unexpected.
This means that the code above is 100% wrong - if pcap_activate() returns a non-zero value, it may have failed, and if it returns 0, it succeeded.
If the return value is negative, it's an error value, and it has failed. If it's non-zero but positive, it's a warning value; it has succeeded, but, for example, it might not have turned promiscuous mode on, as the OS or device might not let promiscuous mode be set.
So what you want is, instead:
//Activate the handle
int status;
status = pcap_activate(handle);
if(status >= 0){
if(status == PCAP_WARNING){
// warning
std:cout << "Activated, with warning: " << pcap_geterror(handle) << std::endl;
}
else if (status != 0){
// warning
std:cout << "Activated, with warning: " << pcap_statustostr(status) << std::endl;
}
else{
// no warning
std::cout <<"Activated!"<<std::endl;
}
}
else{
if(status == PCAP_ERROR){
std:cout << "Failed to activate: " << pcap_geterror(handle) << std::endl;
}
else{
std:cout << "Failed to activate: " << pcap_statustostr(status) << std::endl;
}
exit(EXIT_FAILURE);
}