File: simif.c

package info (click to toggle)
sdcc 3.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 99,212 kB
  • sloc: ansic: 918,594; cpp: 69,526; makefile: 56,790; sh: 29,616; asm: 12,364; perl: 12,136; yacc: 7,179; lisp: 1,672; python: 812; lex: 773; awk: 495; sed: 89
file content (263 lines) | stat: -rw-r--r-- 5,036 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <mcs51reg.h>
#include <stdio.h>


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
};

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
};

unsigned char
_sdcc_external_startup (void)
{
  /* copied from device/examples/mcs51/simple2/hi.c */
  PCON = 0x80;  /* power control byte, set SMOD bit for serial port */
  SCON = 0x40;  /* serial control byte, mode 1, RI _NOT_ active */
  TMOD = 0x21;  /* timer control mode, byte operation */
  TCON = 0;     /* timer control register, byte operation */

  TH1  = 0xFA;  /* serial reload value, 9,600 baud at 11.0952Mhz */
  TR1  = 1;     /* start serial timer */

  TI   = 1;     /* enable transmission of first byte */
  return 0;
}

int
putchar (int c)
{
  while (!TI)
    ;
  SBUF = c;
  TI = 0;
  return c;
}

#define SIF_ADDRESS_SPACE_NAME	"xram"
#define SIF_ADDRESS_SPACE	__xdata
#define SIF_ADDRESS		0xffff

unsigned char SIF_ADDRESS_SPACE * volatile sif;

char
sif_get(char cmd)
{
  *sif= cmd;
  return *sif;
}

char
simulated(void)
{
  unsigned char c;
  *sif= 0x55;
  c= *sif;
  if (c != (unsigned char)~0x55)
    return(0);
  *sif= 0xaa;
  if (*sif != (unsigned char)~0xaa)
    return(0);
  return(1);
}

char
detect(void)
{
  *sif= SIFCM_DETECT;
  return *sif == DETECT_SIGN;
}

int nuof_commands;
unsigned char commands[100];

void
get_commands(void)
{
  int i;
  *sif= SIFCM_COMMANDS;
  nuof_commands= *sif;
  for (i= 0; i < nuof_commands; i++)
    commands[i]= *sif;
}

int
get_ifversion(void)
{
  *sif= SIFCM_IFVER;
  return(*sif);
}

unsigned char sim_version[15];

void
get_sim_version()
{
  unsigned char c;
  unsigned char i;
  
  *sif= SIFCM_SIMVER;
  sim_version[0]= 0;
  i= *sif;
  //printf("read answer of len=%d\n", i);
  if (i)
    {
      i= 0;
      c= *sif;
      //printf("  ans[%d]= %02x,%c\n", i, c, c);
      while (c && (i<14))
	{
	  sim_version[i++]= c;
	  c= *sif;
	  //printf("  ans[%d]= %02x,%c\n", i, c, c);
	}
      while (c)
	{
	  c= *sif;
	  //printf("  ans[]= %02x,%c\n", c, c);
	}
      sim_version[i]= 0;
    }
}

void
print_cmd_infos(void)
{
  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)
	    {
	      putchar(j);
	      j= *sif;
	    }
	}
      printf("\n");
    }
  
}

void
sif_putchar(char c)
{
  *sif= SIFCM_PRINT;
  *sif= c;
}

void
sif_print(char *s)
{
  while (*s)
    sif_putchar(*s++);
}

void
fout_demo(char *s)
{
  while (*s)
    {
      *sif= SIFCM_WRITE;
      *sif= *s++;
    }
}

char
sif_fin_avail()
{
  return sif_get(SIFCM_FIN_CHECK);
}

char
sif_read()
{
  return sif_get(SIFCM_READ);
}

void
fin_demo()
{
  char i, c;
  printf("Reading input from SIMIF input file:\n");
  while (i= sif_fin_avail())
    {
      c= sif_read();
      if (c > 31)
	putchar(c);
    }
  printf("\nRead demo finished\n");
}

void
main(void)
{
  sif= (unsigned char SIF_ADDRESS_SPACE *)0xffff;
  printf("Testing simulator interface at %s[0x%x]\n",
	 SIF_ADDRESS_SPACE_NAME, SIF_ADDRESS);
  printf("%s", detect()?"Interface found.":"Interface not found");
  if (detect())
    {
      int i;
      i= get_ifversion();
      get_sim_version();
      printf(" Version %d (of simulator %s)\n", i, sim_version);
      get_commands();
      printf("Interface knows about %d commands:\n", nuof_commands);
      for (i= 0; i < nuof_commands; i++)
	printf("%c", commands[i]);
      printf("\n");
      print_cmd_infos();
      sif_print("Message from simulated program: Hello World!\n");
      fout_demo("Write this message\n"
		"to simif output file.\n"
		"\n"
		"Done.\n");
      fin_demo();
    }
  else
    printf("\n");
  //* (char __idata *) 0 = * (char __xdata *) 0x7654;
  *sif= SIFCM_STOP;
}