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