Taking input from Keyboard during runtime : one of the possible solutions (DPI-C) does not work for me :(...! Any more solutions

I have a processor simulator and needs to take keyboard during run time.

I tried DPI as below and questa sim, hangs and does not recover.

Code in C:

#include "svdpi.h"
#include "stdio.h"
#include "conio.h" 
//extern void write(int, int);    // Imported from SystemVerilog
void slave_write(const int I1, const int I2)
{  
  char i;
  printf("Hello write");
  i=getchar();
  printf("\n Entered data I1=%d I2=%d %c ",I1,I2,i);
}

I also tried scanf which did not work and gave the same behaviour.

Code in SV :

module Bus();
  import "DPI-C" function void slave_write(int,int);
initial begin
  // This SystemVerilog function could be called from C
  //function void write(int address, int data);
    // Call C function
    slave_write(20,23); // Arguments passed by copy
  //endfunction
end
endmodule

The simulator is going to have the std input open for itself. You will need to open a file and read that instead of the keyboard.

In reply to dave_59:

Is it possible to invoke a tcl script or tk window and have it ask user input during simulation ?
Or have a tcl proc change stdin from simulator and pass it on to the function/task (get specific number of inputs, like maybe 8 characters) and then hand over stdin back to the simulator ?

In reply to niletronics:

It really does not make sense to try to have interactive stimulus in a simulation. You want to create an environment that is repeatable so that you can debug your design, fix a problem, then repeat the stimulus to see check the new results. Also, simulation runs significantly slower that the real hardware.

In reply to dave_59:

Hi Dave,

Thanks for inputs, however we have this requirement for a processor simulation, which is more like a software simulation.

We need to read from keyboard and my design would process the input, that’s the basic requirement.

However if you have any suggestion for doing this, please let me know.

In reply to niletronics:

I have same kind of problem. Is that possible?

In reply to Ankit_Patel:

I would expect that reading a file in the simulator and appending to it in the “real world” might be the simplest solution.

In reply to Richard Hamer (EnSilica):

There seems to be a fundamental misunderstanding here in the difference between a hardware description language used to simulate a design versus using that same description to implement a design in actual hardware. It’s like drawing a picture of a pinwheel, blowing on that picture, and expecting the pinwheel to start turning.

You can certainly build a 3-D model of that pinwheel, simulate the force of the wind on that model and watch it turn, and then send that model to a 3-d printer to get your pinwheel. I suppose you could put wind sensors in front of your monitor, and write a program that converts a value from the sensor to a value used in the simulation. The point is, the simulator has no knowledge that the value came from someone blowing on the monitor, it just sees a parameter value change.

Unless you are designing the keyboard hardware yourself and simulating that, there really is not much point in taking keyboard input from a computer and using that to stimulate your design in simulation. The operating system has already abstracted away the keyboard hardware and provides you with a string of character codes. The reason you are simulating in the first place is to verify the functionality of your design. If you find a problem, you are going to want to replay the exact same stimulus until you fix your problem.

Just like the pinwheel example, I do know it’s possible for someone to set up a program that reads keyboard input and provides that as stimulus to a simulation. But that involves inter-process communication(IPC) and specific tool knowledge to set that up.

In reply to dave_59:

When you are trying to read the character from the keyboard, the characters gets stored in the buffer and assigns those characters all at once. An example, if you are having a for loop of ten iterations which includes reading the character from keyboard, when you run the simulation, you have to type all the characters continuously for example, “abcde12345”. After this, the simulation takes the characters in the same order in which they were typed.

If you want your simulation to behave like a regular simulation, other than one mentioned above and which most of us are interested in, you need to access the buffer’s entry address (Which I don’t remember). Considering the same example, using the current method, the simulator waits till the key is pressed in every iteration.

Bhargav