]> git.sur5r.net Git - cc65/blob - src/sim65/paravirt.c
sim65: add command line parameter to print number of CPU cycles at exit
[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 #if defined(_WIN32)
40 #  define O_INITIAL O_BINARY
41 #else
42 #  define O_INITIAL 0
43 #endif
44 #if defined(_MSC_VER)
45 /* Microsoft compiler */
46 #  include <io.h>
47 #else
48 /* Anyone else */
49 #  include <unistd.h>
50 #endif
51
52 /* common */
53 #include "cmdline.h"
54 #include "print.h"
55 #include "xmalloc.h"
56
57 /* sim65 */
58 #include "6502.h"
59 #include "memory.h"
60 #include "paravirt.h"
61
62
63
64 /*****************************************************************************/
65 /*                                   Data                                    */
66 /*****************************************************************************/
67
68
69
70 typedef void (*PVFunc) (CPURegs* Regs);
71
72 static unsigned ArgStart;
73
74
75
76 /*****************************************************************************/
77 /*                                   Code                                    */
78 /*****************************************************************************/
79
80
81
82 static unsigned GetAX (CPURegs* Regs)
83 {
84     return Regs->AC + (Regs->XR << 8);
85 }
86
87
88
89 static void SetAX (CPURegs* Regs, unsigned Val)
90 {
91     Regs->AC = Val & 0xFF;
92     Val >>= 8;
93     Regs->XR = Val;
94 }
95
96
97
98 static void MemWriteWord (unsigned Addr, unsigned Val)
99 {
100     MemWriteByte (Addr, Val);
101     Val >>= 8;
102     MemWriteByte (Addr + 1, Val);
103 }
104
105
106
107 static unsigned char Pop (CPURegs* Regs)
108 {
109     return MemReadByte (0x0100 + ++Regs->SP);
110 }
111
112
113
114 static unsigned PopParam (unsigned char Incr)
115 {
116     unsigned SP = MemReadZPWord (0x00);
117     unsigned Val = MemReadWord (SP);
118     MemWriteWord (0x0000, SP + Incr);
119     return Val;
120 }
121
122
123
124 static void PVArgs (CPURegs* Regs)
125 {
126     unsigned ArgC = ArgCount - ArgStart;
127     unsigned ArgV = GetAX (Regs);
128     unsigned SP   = MemReadZPWord (0x00);
129     unsigned Args = SP - (ArgC + 1) * 2;
130
131     Print (stderr, 2, "PVArgs ($%04X)\n", ArgV);
132
133     MemWriteWord (ArgV, Args);
134
135     SP = Args;
136     while (ArgStart < ArgCount) {
137         unsigned I = 0;
138         const char* Arg = ArgVec[ArgStart++];
139         SP -= strlen (Arg) + 1;
140         do {
141             MemWriteByte (SP + I, Arg[I]);
142         }
143         while (Arg[I++]);
144
145         MemWriteWord (Args, SP);
146         Args += 2;
147     }
148     MemWriteWord (Args, 0x0000);
149
150     MemWriteWord (0x0000, SP);
151     SetAX (Regs, ArgC);
152 }
153
154
155
156 static void PVExit (CPURegs* Regs)
157 {
158     Print (stderr, 1, "PVExit ($%02X)\n", Regs->AC);
159     if (PrintCycles) {
160       Print (stdout, 0, "%lu cycles\n", GetCycles ());
161     }
162
163     exit (Regs->AC);
164 }
165
166
167
168 static void PVOpen (CPURegs* Regs)
169 {
170     char Path[1024];
171     int OFlag = O_INITIAL;
172     unsigned RetVal, I = 0;
173
174     unsigned Mode  = PopParam (Regs->YR - 4);
175     unsigned Flags = PopParam (2);
176     unsigned Name  = PopParam (2);
177
178     do {
179         Path[I] = MemReadByte (Name++);
180     }
181     while (Path[I++]);
182
183     Print (stderr, 2, "PVOpen (\"%s\", $%04X)\n", Path, Flags);
184
185     switch (Flags & 0x03) {
186         case 0x01:
187             OFlag |= O_RDONLY;
188             break;
189         case 0x02:
190             OFlag |= O_WRONLY;
191             break;
192         case 0x03:
193             OFlag |= O_RDWR;
194             break;
195     }
196     if (Flags & 0x10) {
197         OFlag |= O_CREAT;
198     }
199     if (Flags & 0x20) {
200         OFlag |= O_TRUNC;
201     }
202     if (Flags & 0x40) {
203         OFlag |= O_APPEND;
204     }
205     if (Flags & 0x80) {
206         OFlag |= O_EXCL;
207     }
208
209     /* Avoid gcc warning */
210     (void) Mode;
211
212     RetVal = open (Path, OFlag);
213
214     SetAX (Regs, RetVal);
215 }
216
217
218
219 static void PVClose (CPURegs* Regs)
220 {
221     unsigned RetVal;
222
223     unsigned FD = GetAX (Regs);
224
225     Print (stderr, 2, "PVClose ($%04X)\n", FD);
226
227     RetVal = close (FD);
228
229     SetAX (Regs, RetVal);
230 }
231
232
233
234 static void PVRead (CPURegs* Regs)
235 {
236     unsigned char* Data;
237     unsigned RetVal, I = 0;
238
239     unsigned Count = GetAX (Regs);
240     unsigned Buf   = PopParam (2);
241     unsigned FD    = PopParam (2);
242
243     Print (stderr, 2, "PVRead ($%04X, $%04X, $%04X)\n", FD, Buf, Count);
244
245     Data = xmalloc (Count);
246
247     RetVal = read (FD, Data, Count);
248
249     if (RetVal != (unsigned) -1) {
250         while (I < RetVal) {
251             MemWriteByte (Buf++, Data[I++]);
252         }
253     }
254     xfree (Data);
255
256     SetAX (Regs, RetVal);
257 }
258
259
260
261 static void PVWrite (CPURegs* Regs)
262 {
263     unsigned char* Data;
264     unsigned RetVal, I = 0;
265
266     unsigned Count = GetAX (Regs);
267     unsigned Buf   = PopParam (2);
268     unsigned FD    = PopParam (2);
269
270     Print (stderr, 2, "PVWrite ($%04X, $%04X, $%04X)\n", FD, Buf, Count);
271
272     Data = xmalloc (Count);
273     while (I < Count) {
274         Data[I++] = MemReadByte (Buf++);
275     }
276
277     RetVal = write (FD, Data, Count);
278
279     xfree (Data);
280
281     SetAX (Regs, RetVal);
282 }
283
284
285
286 static const PVFunc Hooks[] = {
287     PVArgs,
288     PVExit,
289     PVOpen,
290     PVClose,
291     PVRead,
292     PVWrite,
293 };
294
295
296
297 void ParaVirtInit (unsigned aArgStart)
298 /* Initialize the paravirtualization subsystem */
299 {
300     ArgStart = aArgStart;
301 };
302
303
304
305 void ParaVirtHooks (CPURegs* Regs)
306 /* Potentially execute paravirtualization hooks */
307 {
308     /* Check for paravirtualization address range */
309     if (Regs->PC <  0xFFF0 ||
310         Regs->PC >= 0xFFF0 + sizeof (Hooks) / sizeof (Hooks[0])) {
311         return;
312     }
313
314     /* Call paravirtualization hook */
315     Hooks[Regs->PC - 0xFFF0] (Regs);
316
317     /* Simulate RTS */
318     Regs->PC = Pop(Regs) + (Pop(Regs) << 8) + 1;
319 }