Simulator interface

Using the simulator interface the simulated program can control the simulation, it can get information about the interface, stop simulation and it can do console and file io.

Interface is available behind a memory location, which can be any data memory location which is readable and writable by the program. Program can send command to the interface by writing one character command code into the memory location (although some of them needs parameter) and can read back answer from the same location.

The interface must be turned on before use. It means that you must specify the address of the memory location to use. It can be done in two ways.

Using -I option of the simulator program

-I option accepts a list of settings in form: setting=value. Know settings are:

this setting is used to turn on the interface. Its value must be a memory name and the address, like:
s51 -I if=xram[0xffff]
memory name must be followed by the address in square brackets. Address can be specified in decimal, octal or hexadecimal.
value specifies name of the file which will be used by READ command.
value specifies name of the file which will be used by WRITE command.

Configuring virtual simif peripheral

If you check hardware elements of simulated processor with conf commmand, you will see one called simif. Setting of this peripheral will setup the simulator interface:

0> conf
ucsim version 0.6-pre56
Type of microcontroller: C52 CMOS cmos
Controller has 12 hardware element(s).
   on simif[0]
   on vcd[0]
   on timer0[0]
   on timer1[1]
   on uart[0]
   on dport[0]
   on port[0]
   on port[1]
   on port[2]
   on port[3]
   on irq[0]
   on timer2[2]

Using info command you can get state of the interface, known commands, and other information:

0> info hw simif
uCsim simulator interface, version 1, at (null)[0x0]
Active command: none.
Known commands:
0x5f/_ if_detect: Detect existence of interface
0x69/i commands: Get information about known commands
0x76/v if_ver: Get version of simulator interface
0x56/V sim_ver: Get version of simulator
0x40/@ if_reset: Reset interface to default state
0x49/I cmdinfo: Get information about a command
0x68/h cmdhelp: Get help about a command
0x73/s stop: Stop simulation
0x70/p print: Print character
0x78/x print_hex: Print character in hex
0x66/f fin_check: Check input file if input available
0x72/r read input file: Read character from input file
0x77/w write to output file: Write character to output file
Input file: 
Output file: 

Use set hardware command to setup the interface:

0> set hw simif
set hardware simif memory address
set hardware simif fin "input_file_name"
set hardware simif fout "output_file_name"

To turn on the interface, use following command:

0> set hw simif xram 0xffff
0> i h simif
uCsim simulator interface, version 1, at xram[0xffff]

following commands can be used to specify input and output files:

0> set hw simif fin "infile.txt"
0> set hw simif fout "outfile.txt"
0> i h simif
Input file: infile.txt
Output file: outfile.txt

Access interface from simulated program

To access memory content, you have to use C variable. If address is already known, you can setup a pointer with that address to access the content:

#define SIF_ADDRESS_SPACE	__xdata
#define SIF_ADDRESS		0xffff

unsigned char SIF_ADDRESS_SPACE * volatile sif;

void main(void) { sif= (unsigned char SIF_ADDRESS_SPACE *) SIF_ADDRESS; ...

this example can be compiled with SDCC for MSC51 processor. Do not forget the volatile keyword! You can define names for command characters:

enum sif_command {
  DETECT_SIGN	        = '!',	// answer to detect command
  SIFCM_DETECT		= '_',	// command used to detect the interface
  SIFCM_COMMANDS	= 'i',	// get info about commands
  SIFCM_IFVER		= 'v',	// interface version
  SIFCM_SIMVER		= 'V',	// simulator version
  SIFCM_IFRESET		= '@',	// reset the interface
  SIFCM_CMDINFO		= 'I',	// info about a command
  SIFCM_CMDHELP		= 'h',	// help about a command
  SIFCM_STOP		= 's',	// stop simulation
  SIFCM_PRINT		= 'p',	// print character
  SIFCM_FIN_CHECK	= 'f',	// check input file for input
  SIFCM_READ		= 'r',	// read from input file
  SIFCM_WRITE		= 'w',	// write to output file

Command: detect

Command character: _
Answer: !

Following function can be used to detect if the interface is turned on or not:

  return *sif == DETECT_SIGN;

Command: commands

Command character: i
Answer: nuof_commands, command_char_1, command_char_2, ...

This command can be used to retrieve all know command characters. First answer is the number of known commands, further reads will get command characters. Following example will read all commands:

int nuof_commands;
unsigned char commands[100];

  int i;
  nuof_commands= *sif;
  for (i= 0; i < nuof_commands; i++)
    commands[i]= *sif;

Command: ifver

Command character: v
Answer: 1 byte version number

Following simple example is a function which returns the interface version:

  *sif= SIFCM_IFVER;

Command: simver

Command character: V
Answer: string

First byte of the string answer will be the length of the string, and after the last character a zero byte will arrive. Following function can be used to read string answer and store it (up to some limited length):

unsigned char sim_version[15];

  unsigned char c, i, n;
  sim_version[0]= 0;
  n= *sif;
  if (n)
      i= 0;
      c= *sif;
      while (c && (i<14))
	  sim_version[i++]= c;
	  c= *sif;
      while (c)
	c= *sif;
      sim_version[i]= 0;

Command: ifreset

Command character: @
Answer: -

This command resets the interface to default state.

Command: cmdinfo

Command character: I followed by a command character (which you would like to get info about)
Answer: array

1st byte of the answer
full length of the answer
2nd byte of the answer
number of parameters that the command needs
3rd byte of the answer
type of the answer that the command replies
one byte
array (of bytes): length, followed by bytes
string: length, characters and one zero byte
no answer
Command: cmdhelp

Command character: h followed by a command character (which you would like to get info about)
Answer: string

Returns textual information about. Following function prints information about all commands (retrieved by "i" command, see above):
enum sif_answer_type {
  SIFAT_UNKNOWN		= 0x00,	// we don't know...
  SIFAT_BYTE		= 0x01,	// just a byte
  SIFAT_ARRAY		= 0x02,	// array of some bytes
  SIFAT_STRING		= 0x03,	// a string
  SIFAT_NONE		= 0x04	// no answer at all

  int i, j;
  unsigned char inf[5];
  for (i= 0; i < nuof_commands; i++)
      printf("Command '%c' info:\n", commands[i]);
      *sif= SIFCM_CMDINFO;
      *sif= commands[i];
      inf[0]= *sif;
      for (j= 0; j < inf[0]; j++)
	  inf[j+1]= *sif;
	  //printf(" 0x%02x", inf[j+1]);
      printf("  need %d params, answers as ", inf[1]);
      switch (inf[2])
	case SIFAT_UNKNOWN	: printf("unknown"); break;
	case SIFAT_BYTE		: printf("byte"); break;
	case SIFAT_ARRAY	: printf("array"); break;
	case SIFAT_STRING	: printf("string"); break;
	case SIFAT_NONE		: printf("none"); break;
      printf(": ");
      *sif= SIFCM_CMDHELP;
      *sif= commands[i];
      if (*sif)
	  j= *sif;
	  while (j)
	      j= *sif;

Command: stop

Command character: s
Answer: -

Sending this command stops the simulation.

Command: print

Command character: p followed by a character to print
Answer: -

This command can be used to print out a character on the simulator console. Following functions can be used to print a character and a string:
sif_putchar(char c)
  *sif= SIFCM_PRINT;
  *sif= c;

sif_print(char *s)
  while (*s)

Command: fin_check

Command character: f
Answer: 0 (if input file is not readable), or 1 (if input file is readable)

This command checks if the input file is readable or not.Here is a simple function which can be used to check readability of the input file:
  return sif_get(SIFCM_FIN_CHECK);

Command: read

Commad character: r
Answer: next byte from input file

This command can be used to read next byte from the input file. Remember, name of the input file must be set before use.

Following demonstration code prints out content of the input file:
  char i, c;
  printf("Reading input from SIMIF input file:\n");
  while (i= sif_fin_avail())
      c= sif_read();
      if (c > 31)
  printf("\nRead demo finished\n");
Command: write

Command character: w, followed by a byte to write to output file
Answer: -

Following function can be used to write a string to the output file:
fout_demo(char *s)
  while (*s)
      *sif= SIFCM_WRITE;
      *sif= *s++;
Do not forget to specify name of the output file via setting of the simulator interface. Be careful, simulator will overwrite content of the output file!