aboutsummaryrefslogtreecommitdiff
path: root/sim/w65/interp.c
blob: 0a05fbc4a2acd1b484096947c5a70ce310c00c61 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* Simulator for the WDC 65816 architecture.

   Written by Steve Chamberlain of Cygnus Support.
   sac@cygnus.com

   This file is part of W65 sim


		THIS SOFTWARE IS NOT COPYRIGHTED

   Cygnus offers the following for use in the public domain.  Cygnus
   makes no warranty with regard to the software or it's performance
   and the user accepts the software "AS IS" with all faults.

   CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
   THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*/

#include <signal.h>
#include "sysdep.h"
#include <sys/times.h>
#include <sys/param.h>
#include "bfd.h"
#include "remote-sim.h"
#include "../../newlib/libc/sys/w65/sys/syscall.h"

#include "interp.h"

saved_state_type saved_state;

int
get_now ()
{
  return time ((long *) 0);
}
void
control_c (sig, code, scp, addr)
     int sig;
     int code;
     char *scp;
     char *addr;
{
  saved_state.exception = SIGINT;
}

wai ()
{
  saved_state.exception = SIGTRAP;
}



wdm (acc, x)
     int acc;
     int x;

{
int cycles;
  /* The x points to where the registers live, acc has code */

#define R(arg)  (x +  arg * 2)
unsigned  R0 = R(0);
unsigned  R4 = R(4);
unsigned  R5 = R(5);
unsigned  R6 = R(6);
unsigned  R7 = R(7);
unsigned  R8 = R(8);
unsigned char *memory = saved_state.memory;
  int a1 = fetch16 (R (4));
  switch (a1)
    {
    case SYS_write:
      {
	int file = fetch16 (R5);
	unsigned char *buf = fetch24 (R6) + memory;
	int len = fetch16 (R8);
	int res = write (file, buf, len);
	store16 (R0, res);
	break;
      }
    case 0:
      printf ("%c", acc);
      fflush (stdout);
      break;
    case 1:
      saved_state.exception = SIGTRAP;
      break;
    default:
      saved_state.exception = SIGILL;
      break;
    }
}


void
sim_resume (step, insignal)
     int step;
     int insignal;
{
  void (*prev) ();
  register unsigned char *memory;
  if (step)
    {
      saved_state.exception = SIGTRAP;
    }
  else
    {
      saved_state.exception = 0;
    }


  prev = signal (SIGINT, control_c);
  do
    {
      int x = (saved_state.p >> 4) & 1;
      int m = (saved_state.p >> 5) & 1;
      if (x == 0 && m == 0)
	{
	  ifunc_X0_M0 ();
	}
      else if (x == 0 && m == 1)
	{
	  ifunc_X0_M1 ();
	}
      else if (x == 1 && m == 0)
	{
	  ifunc_X1_M0 ();
	}
      else if (x == 1 && m == 1)
	{
	  ifunc_X1_M1 ();
	}
    }
  while (saved_state.exception == 0);

  signal (SIGINT, prev);
}




init_pointers ()
{
  if (!saved_state.memory)
    {
      saved_state.memory = calloc (64 * 1024, NUMSEGS);
    }
}

int
sim_write (addr, buffer, size)
     SIM_ADDR addr;
     unsigned char *buffer;
     int size;
{
  int i;
  init_pointers ();

  for (i = 0; i < size; i++)
    {
      saved_state.memory[(addr + i) & MMASK] = buffer[i];
    }
  return size;
}

int
sim_read (addr, buffer, size)
     SIM_ADDR addr;
     unsigned char *buffer;
     int size;
{
  int i;

  init_pointers ();

  for (i = 0; i < size; i++)
    {
      buffer[i] = saved_state.memory[(addr + i) & MMASK];
    }
  return size;
}



struct
{
  unsigned int *ptr;
  int size;
}
rinfo[] =

{
  &saved_state.r[0], 2,
  &saved_state.r[1], 2,
  &saved_state.r[2], 2,
  &saved_state.r[3], 2,
  &saved_state.r[4], 2,
  &saved_state.r[5], 2,
  &saved_state.r[6], 2,
  &saved_state.r[7], 2,
  &saved_state.r[8], 2,
  &saved_state.r[9], 2,
  &saved_state.r[10], 2,
  &saved_state.r[11], 2,
  &saved_state.r[12], 2,
  &saved_state.r[13], 2,
  &saved_state.r[14], 2,
  &saved_state.r[15], 4,
  &saved_state.pc, 4,
  &saved_state.a, 4,
  &saved_state.x, 4,
  &saved_state.y, 4,
  &saved_state.dbr, 4,
  &saved_state.d, 4,
  &saved_state.s, 4,
  &saved_state.p, 4,
  &saved_state.ticks, 4,
  &saved_state.cycles, 4,
  &saved_state.insts, 4,
  0
};

void
sim_store_register (rn, value)
     int rn;
     unsigned char *value;
{
  unsigned int val;
  int i;
  val = 0;
  for (i = 0; i < rinfo[rn].size; i++)
    {
      val |= (*value++) << (i * 8);
    }

  *(rinfo[rn].ptr) = val;
}

void
sim_fetch_register (rn, buf)
     int rn;
     unsigned char *buf;
{
  unsigned int val = *(rinfo[rn].ptr);
  int i;

  for (i = 0; i < rinfo[rn].size; i++)
    {
      *buf++ = val;
      val = val >> 8;
    }
}


sim_reg_size (n)
{
  return rinfo[n].size;
}
int
sim_trace ()
{
  return 0;
}

void
sim_stop_reason (reason, sigrc)
     enum sim_stop *reason;
     int *sigrc;
{
  *reason = sim_stopped;
  *sigrc = saved_state.exception;
}

int
sim_set_pc (x)
     SIM_ADDR x;
{
  saved_state.pc = x;
  return 0;
}


void
sim_info (verbose)
     int verbose;
{
  double timetaken = (double) saved_state.ticks;
  double virttime = saved_state.cycles / 2.0e6;

  printf ("\n\n# instructions executed  %10d\n", saved_state.insts);
  printf ("# cycles                 %10d\n", saved_state.cycles);
  printf ("# real time taken        %10.4f\n", timetaken);
  printf ("# virtual time taken     %10.4f\n", virttime);

  if (timetaken != 0)
    {
      printf ("# cycles/second          %10d\n", (int) (saved_state.cycles / timetaken));
      printf ("# simulation ratio       %10.4f\n", virttime / timetaken);
    }

}



void
sim_kill ()
{

}

void
sim_open (name)
     char *name;
{
}



#undef fetch8
fetch8func (x)
{
  if (x & ~MMASK)
    {
      saved_state.exception = SIGBUS;
      return 0;
    }
  return saved_state.memory[x];
}

fetch8 (x)
{
return fetch8func(x);
}

void
sim_close (quitting)
     int quitting;
{
  /* nothing to do */
}

int
sim_load (prog, from_tty)
     char *prog;
     int from_tty;
{
  /* Return nonzero so gdb will handle it.  */
  return 1;
}


void
sim_create_inferior (start_address, argv, env)
     SIM_ADDR start_address;
     char **argv;
     char **env;
{
  /* ??? We assume this is a 4 byte quantity.  */
  int pc;

  pc = start_address;
  sim_store_register (16, (unsigned char *) &pc);
}