File: pilotcpu.c

package info (click to toggle)
xcopilot 1%3A0.6.6
  • links: PTS
  • area: contrib
  • in suites: potato, slink
  • size: 3,252 kB
  • ctags: 3,406
  • sloc: ansic: 37,932; cpp: 1,918; sh: 329; makefile: 68
file content (325 lines) | stat: -rw-r--r-- 7,324 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
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
/*****************************************************************************

			       XCopilot

This code is part of XCopilot, a port of copilot

     Portions of this code are Copyright (C) 1997 Ivan A. Curtis
		       icurtis@radlogic.com.au

The original MS-Windows95 copilot emulator was written by Greg Hewgill.
The following copyright notice appeared on the original copilot sources:

		  Copyright (c) 1996 Greg Hewgill

 MC68000 Emulation code is from Bernd Schmidt's Unix Amiga Emulator.
       The following copyright notice appeared in those files:

	  Original UAE code Copyright (c) 1995 Bernd Schmidt

This code must not be distributed without these copyright notices intact.

*******************************************************************************
*******************************************************************************

Filename:	pilotcpu.c

Description:	Copilot cpu interface module

Update History:   (most recent first)
   Ian Goldberg 26-Jun-98 12:47 -- generalized scratch location
   I. Curtis     5-Mar-97 21:06 -- added cpu_loadapp procedure
   I. Curtis    ??-Mar-97 ??:?? -- reorganized main loop in CPU()

******************************************************************************/
#include <unistd.h>
#include <stdio.h>

#include "sysdeps.h"
#include "shared.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "pilotcpu.h"
#include "fakecall.h"

int CPU(shared_img *shptr)
{
  shptr->CpuState = cpuStopped;
  do {
    switch (shptr->CpuReq) {
    case cpuStart:
      shptr->CpuReq = cpuNone;	            /* clear request */
      /*
       *       fprintf(stderr, "I - CPU Started\n");
       *       fflush(stderr);
       */
      MC68000_run();
      break;
    case cpuStop:
      shptr->CpuReq = cpuNone;
      shptr->CpuState = cpuStopped;
      /*
       *       fprintf(stderr, "I - CPU Stopped\n");
       *       fflush(stderr);
       */
      break;
    case cpuReset:
      shptr->CpuReq = cpuNone;
      MC68000_reset();
      /*
       *       fprintf(stderr, "I - CPU Reset\n");
       *       fflush(stderr);
       */
      break;
    case cpuLoadApp:
      shptr->CpuReq = cpuNone;
      CPU_loadapp(shptr);
      shptr->CpuState = cpuLoaded;
      break;
    case cpuExit:
      break;
    default:
      usleep(1000);		            /* sleep for 1 ms */
    }
  }
  while (shptr->CpuReq != cpuExit);
  /*
   *   fprintf(stderr, "I - CPU Exiting\n");
   *   fflush(stderr);
   */
  return 0;
}

int CPU_init(shared_img *shptr, int ramsize, const char *dir,
             const char *romfile, const char *ramfile, const char *scratchfile,
	     int reset, int nocheck)
{
  int r;
  /*
   *   fprintf(stderr, "I - initializing memory\n");
   *   fflush(stderr);
   */
  r = memory_init(ramsize, dir, romfile, ramfile, scratchfile, reset,
		  nocheck);
  if (r != 0) {
    return r;
  }

  /*
   *   fprintf(stderr, "I - doing custom initialization\n");
   *   fflush(stderr);
   */
  custom_init(shptr);

  /*
   *   fprintf(stderr, "I - doing m68k initialization\n");
   *   fflush(stderr);
   */
  MC68000_init(shptr);

  /*
   *   fprintf(stderr, "I - doing m68k reset\n");
   *   fflush(stderr);
   */
  MC68000_reset();

  return 0;
}

void CPU_reset(shared_img *shptr)
{
  /*
   *   fprintf(stderr, "I - Resetting CPU..\n");
   *   fflush(stderr);
   */
  shptr->CpuReq = cpuReset;
  while (shptr->CpuState != cpuStopped) {
    usleep(1000);
  }
}

void CPU_start(shared_img *shptr)
{
  /*
   *   fprintf(stderr, "I - Starting CPU..\n");
   *   fflush(stderr);
   */
  shptr->CpuReq = cpuStart;
  while (shptr->CpuState != cpuRunning) {
    usleep(1000);
  }
}

void CPU_stop(shared_img *shptr)
{
  /*
   *   fprintf(stderr, "I - Stopping CPU..\n");
   *   fflush(stderr);
   */
  shptr->CpuReq = cpuStop;
  while (shptr->CpuState != cpuStopped) {
    usleep(1000);
  }
}

void CPU_wait(shared_img *shptr)
{
  /*
   *   fprintf(stderr, "I - Waiting for CPU to Stop\n");
   *   fflush(stderr);
   */
  while (shptr->CpuState == cpuRunning) {
    usleep(1000);
  }
}

char *CPU_getstate(shared_img *shptr)
{
  switch (shptr->CpuState) {
  case cpuInitial: return "Initial";
  case cpuRunning: return "Running";
  case cpuStopped: return "Stopped";
  case cpuBreakpoint: return "Breakpoint";
  case cpuException: return "Exception";
  default: return "UNKNOWN-ERROR";
  }
}

void *CPU_getmemptr(CPTR addr)
{
  return get_real_address(addr);
}

void CPU_getregs(shared_img *shptr, struct regstruct *r)
{
  *r = shptr->regs;
  r->pc = MC68000_getpc();
}

void CPU_setregs(shared_img *shptr, struct regstruct *r)
{
  shptr->regs = *r;
  MakeFromSR();
  MC68000_setpc(r->pc);
}

int CPU_setexceptionflag(shared_img *shptr, int exception, int flag)
{
  int r;
  if (exception < 0 || exception >= 48) {
    return 0;
  }
  r = shptr->ExceptionFlags[exception];
  shptr->ExceptionFlags[exception] = flag;
  return r;
}

void CPU_putbyte(CPTR addr, UBYTE x)
{
  put_byte(addr, x);
}

void CPU_putword(CPTR addr, UWORD x)
{
  put_word(addr, x);
}

void CPU_putlong(CPTR addr, ULONG x)
{
  put_long(addr, x);
}

UBYTE CPU_getbyte(CPTR addr)
{
  return get_byte(addr);
}

UWORD CPU_getword(CPTR addr)
{
  return get_word(addr);
}

ULONG CPU_getlong(CPTR addr)
{
  return get_long(addr);
}

void CPU_loadapp(shared_img *shptr)
{
  int err = 0;
  int dbid;
  /*
   *   fprintf(stderr, "I - CPU Loading..\n");
   *   fflush(stderr);
   */
  {
    FakeCall *fc;
    fc = fc_New(shptr);
    fc_PushLong(fc, scratch_start + 8); /* nameP */
    fc_PushWord(fc, 0);	   /* cardNo */
    /*
     *     fprintf(stderr, "I - making fake call (FindDatabase)..\n");
     *     fflush(stderr);
     */
    fc_Call(fc, shptr, sysTrapDmFindDatabase, 0);
    dbid = fc_GetResultD0(fc);
    /*
     *     fprintf(stderr, "I - result was %d\n", dbid);
     *     fflush(stderr);
     */
    fc_Dispose(fc, shptr);
  }

  if (dbid) {
    FakeCall *fc;
    fc = fc_New(shptr);
    fc_PushLong(fc, dbid);	/* dbID */
    fc_PushWord(fc, 0);	/* cardNo */
    /*
     *     fprintf(stderr, "I - making fake call (DeleteDatabase)..\n");
     *     fflush(stderr);
     */
    fc_Call(fc, shptr, sysTrapDmDeleteDatabase, 0);
    err = fc_GetResultD0(fc);
    /*
     *     fprintf(stderr, "I - result was %d\n", err);
     */
    fc_Dispose(fc, shptr);
  }
  if (err == 0) {
    FakeCall *fc;
    fc = fc_New(shptr);
    fc_PushLong(fc, scratch_start + 8);
    /*
     *     fprintf(stderr, "I - making fake call (CreateDatabaseFromImage)..\n");
     *     fflush(stderr);
     */
    fc_Call(fc, shptr, sysTrapDmCreateDatabaseFromImage, 0);
    dbid = fc_GetResultD0(fc);
    /*
     *     fprintf(stderr, "I - result was %d\n", dbid);
     */
    fc_Dispose(fc, shptr);
  }

  shptr->ErrNo = err;
  /**********************************************************************
  if (err == 0) {
    fprintf(stderr, "Application loaded successfully!\n");
  } else {
    switch (err) {
    case memErrNotEnoughSpace:
      fprintf(stderr, "Not enough free memory to load app");
      break;
    case dmErrDatabaseOpen:
      fprintf(stderr, "Application is currently in use");
      break;
    default:
      fprintf(stderr, "Unknown error 0x%x loading application.", err);
      break;
    }
  }
  fflush(stderr);
  ********************************************************/
}