]> git.sur5r.net Git - cc65/blob - src/sim65/paravirt.c
ld65: implement '--allow-multiple-definition' command line parameter
[cc65] / src / sim65 / paravirt.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                paravirt.c                                 */
4 /*                                                                           */
5 /*                Paravirtualization for the sim65 6502 simulator            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2013-2013 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <sys/stat.h>
40 #if defined(_WIN32)
41 #  define O_INITIAL O_BINARY
42 #else
43 #  define O_INITIAL 0
44 #endif
45 #if defined(_MSC_VER)
46 /* Microsoft compiler */
47 #  include <io.h>
48 #else
49 /* Anyone else */
50 #  include <unistd.h>
51 #endif
52 #ifndef S_IREAD
53 #  define S_IREAD  S_IRUSR
54 #endif
55 #ifndef S_IWRITE
56 #  define S_IWRITE S_IWUSR
57 #endif
58
59 /* common */
60 #include "cmdline.h"
61 #include "print.h"
62 #include "xmalloc.h"
63
64 /* sim65 */
65 #include "6502.h"
66 #include "memory.h"
67 #include "paravirt.h"
68
69
70
71 /*****************************************************************************/
72 /*                                   Data                                    */
73 /*****************************************************************************/
74
75
76
77 typedef void (*PVFunc) (CPURegs* Regs);
78
79 static unsigned ArgStart;
80
81
82
83 /*****************************************************************************/
84 /*                                   Code                                    */
85 /*****************************************************************************/
86
87
88
89 static unsigned GetAX (CPURegs* Regs)
90 {
91     return Regs->AC + (Regs->XR << 8);
92 }
93
94
95
96 static void SetAX (CPURegs* Regs, unsigned Val)
97 {
98     Regs->AC = Val & 0xFF;
99     Val >>= 8;
100     Regs->XR = Val;
101 }
102
103
104
105 static void MemWriteWord (unsigned Addr, unsigned Val)
106 {
107     MemWriteByte (Addr, Val);
108     Val >>= 8;
109     MemWriteByte (Addr + 1, Val);
110 }
111
112
113
114 static unsigned char Pop (CPURegs* Regs)
115 {
116     return MemReadByte (0x0100 + ++Regs->SP);
117 }
118
119
120
121 static unsigned PopParam (unsigned char Incr)
122 {
123     unsigned SP = MemReadZPWord (0x00);
124     unsigned Val = MemReadWord (SP);
125     MemWriteWord (0x0000, SP + Incr);
126     return Val;
127 }
128
129
130
131 static void PVArgs (CPURegs* Regs)
132 {
133     unsigned ArgC = ArgCount - ArgStart;
134     unsigned ArgV = GetAX (Regs);
135     unsigned SP   = MemReadZPWord (0x00);
136     unsigned Args = SP - (ArgC + 1) * 2;
137
138     Print (stderr, 2, "PVArgs ($%04X)\n", ArgV);
139
140     MemWriteWord (ArgV, Args);
141
142     SP = Args;
143     while (ArgStart < ArgCount) {
144         unsigned I = 0;
145         const char* Arg = ArgVec[ArgStart++];
146         SP -= strlen (Arg) + 1;
147         do {
148             MemWriteByte (SP + I, Arg[I]);
149         }
150         while (Arg[I++]);
151
152         MemWriteWord (Args, SP);
153         Args += 2;
154     }
155     MemWriteWord (Args, 0x0000);
156
157     MemWriteWord (0x0000, SP);
158     SetAX (Regs, ArgC);
159 }
160
161
162
163 static void PVExit (CPURegs* Regs)
164 {
165     Print (stderr, 1, "PVExit ($%02X)\n", Regs->AC);
166     if (PrintCycles) {
167         Print (stdout, 0, "%lu cycles\n", GetCycles ());
168     }
169
170     exit (Regs->AC);
171 }
172
173
174
175 static void PVOpen (CPURegs* Regs)
176 {
177     char Path[1024];
178     int OFlag = O_INITIAL;
179     int OMode = 0;
180     unsigned RetVal, I = 0;
181
182     unsigned Mode  = PopParam (Regs->YR - 4);
183     unsigned Flags = PopParam (2);
184     unsigned Name  = PopParam (2);
185
186     if (Regs->YR - 4 < 2) {
187         /* If the caller didn't supply the mode
188         ** argument, use a reasonable default.
189         */
190         Mode = 0x01 | 0x02;
191     }
192
193     do {
194         Path[I] = MemReadByte (Name++);
195     }
196     while (Path[I++]);
197
198     Print (stderr, 2, "PVOpen (\"%s\", $%04X)\n", Path, Flags);
199
200     switch (Flags & 0x03) {
201         case 0x01:
202             OFlag |= O_RDONLY;
203             break;
204         case 0x02:
205             OFlag |= O_WRONLY;
206             break;
207         case 0x03:
208             OFlag |= O_RDWR;
209             break;
210     }
211     if (Flags & 0x10) {
212         OFlag |= O_CREAT;
213     }
214     if (Flags & 0x20) {
215         OFlag |= O_TRUNC;
216     }
217     if (Flags & 0x40) {
218         OFlag |= O_APPEND;
219     }
220     if (Flags & 0x80) {
221         OFlag |= O_EXCL;
222     }
223
224     if (Mode & 0x01) {
225         OMode |= S_IREAD;
226     }
227     if (Mode & 0x02) {
228         OMode |= S_IWRITE;
229     }
230
231     RetVal = open (Path, OFlag, OMode);
232
233     SetAX (Regs, RetVal);
234 }
235
236
237
238 static void PVClose (CPURegs* Regs)
239 {
240     unsigned RetVal;
241
242     unsigned FD = GetAX (Regs);
243
244     Print (stderr, 2, "PVClose ($%04X)\n", FD);
245
246     RetVal = close (FD);
247
248     SetAX (Regs, RetVal);
249 }
250
251
252
253 static void PVRead (CPURegs* Regs)
254 {
255     unsigned char* Data;
256     unsigned RetVal, I = 0;
257
258     unsigned Count = GetAX (Regs);
259     unsigned Buf   = PopParam (2);
260     unsigned FD    = PopParam (2);
261
262     Print (stderr, 2, "PVRead ($%04X, $%04X, $%04X)\n", FD, Buf, Count);
263
264     Data = xmalloc (Count);
265
266     RetVal = read (FD, Data, Count);
267
268     if (RetVal != (unsigned) -1) {
269         while (I < RetVal) {
270             MemWriteByte (Buf++, Data[I++]);
271         }
272     }
273     xfree (Data);
274
275     SetAX (Regs, RetVal);
276 }
277
278
279
280 static void PVWrite (CPURegs* Regs)
281 {
282     unsigned char* Data;
283     unsigned RetVal, I = 0;
284
285     unsigned Count = GetAX (Regs);
286     unsigned Buf   = PopParam (2);
287     unsigned FD    = PopParam (2);
288
289     Print (stderr, 2, "PVWrite ($%04X, $%04X, $%04X)\n", FD, Buf, Count);
290
291     Data = xmalloc (Count);
292     while (I < Count) {
293         Data[I++] = MemReadByte (Buf++);
294     }
295
296     RetVal = write (FD, Data, Count);
297
298     xfree (Data);
299
300     SetAX (Regs, RetVal);
301 }
302
303
304
305 static const PVFunc Hooks[] = {
306     PVArgs,
307     PVExit,
308     PVOpen,
309     PVClose,
310     PVRead,
311     PVWrite,
312 };
313
314
315
316 void ParaVirtInit (unsigned aArgStart)
317 /* Initialize the paravirtualization subsystem */
318 {
319     ArgStart = aArgStart;
320 };
321
322
323
324 void ParaVirtHooks (CPURegs* Regs)
325 /* Potentially execute paravirtualization hooks */
326 {
327     /* Check for paravirtualization address range */
328     if (Regs->PC <  0xFFF0 ||
329         Regs->PC >= 0xFFF0 + sizeof (Hooks) / sizeof (Hooks[0])) {
330         return;
331     }
332
333     /* Call paravirtualization hook */
334     Hooks[Regs->PC - 0xFFF0] (Regs);
335
336     /* Simulate RTS */
337     Regs->PC = Pop(Regs) + (Pop(Regs) << 8) + 1;
338 }