1 /*****************************************************************************/
5 /* Paravirtualization for the sim65 6502 simulator */
9 /* (C) 2013-2013 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
39 #include <sys/types.h>
42 # define O_INITIAL O_BINARY
47 /* Microsoft compiler */
55 # define S_IREAD _S_IREAD
57 # define S_IREAD S_IRUSR
62 # define S_IWRITE _S_IWRITE
64 # define S_IWRITE S_IWUSR
80 /*****************************************************************************/
82 /*****************************************************************************/
86 typedef void (*PVFunc) (CPURegs* Regs);
88 static unsigned ArgStart;
92 /*****************************************************************************/
94 /*****************************************************************************/
98 static unsigned GetAX (CPURegs* Regs)
100 return Regs->AC + (Regs->XR << 8);
105 static void SetAX (CPURegs* Regs, unsigned Val)
107 Regs->AC = Val & 0xFF;
114 static void MemWriteWord (unsigned Addr, unsigned Val)
116 MemWriteByte (Addr, Val);
118 MemWriteByte (Addr + 1, Val);
123 static unsigned char Pop (CPURegs* Regs)
125 return MemReadByte (0x0100 + ++Regs->SP);
130 static unsigned PopParam (unsigned char Incr)
132 unsigned SP = MemReadZPWord (0x00);
133 unsigned Val = MemReadWord (SP);
134 MemWriteWord (0x0000, SP + Incr);
140 static void PVArgs (CPURegs* Regs)
142 unsigned ArgC = ArgCount - ArgStart;
143 unsigned ArgV = GetAX (Regs);
144 unsigned SP = MemReadZPWord (0x00);
145 unsigned Args = SP - (ArgC + 1) * 2;
147 Print (stderr, 2, "PVArgs ($%04X)\n", ArgV);
149 MemWriteWord (ArgV, Args);
152 while (ArgStart < ArgCount) {
154 const char* Arg = ArgVec[ArgStart++];
155 SP -= strlen (Arg) + 1;
157 MemWriteByte (SP + I, Arg[I]);
161 MemWriteWord (Args, SP);
164 MemWriteWord (Args, 0x0000);
166 MemWriteWord (0x0000, SP);
172 static void PVExit (CPURegs* Regs)
174 Print (stderr, 1, "PVExit ($%02X)\n", Regs->AC);
176 Print (stdout, 0, "%lu cycles\n", GetCycles ());
184 static void PVOpen (CPURegs* Regs)
187 int OFlag = O_INITIAL;
188 unsigned RetVal, I = 0;
191 unsigned Mode = PopParam (Regs->YR - 4);
192 unsigned Flags = PopParam (2);
193 unsigned Name = PopParam (2);
195 if (Regs->YR - 4 < 2) {
196 /* If the caller did not supply the mode argument,
197 ** use a reasonable default.
203 Path[I] = MemReadByte (Name++);
207 Print (stderr, 2, "PVOpen (\"%s\", $%04X)\n", Path, Flags);
209 switch (Flags & 0x03) {
240 RetVal = open (Path, OFlag, OMode);
242 SetAX (Regs, RetVal);
247 static void PVClose (CPURegs* Regs)
251 unsigned FD = GetAX (Regs);
253 Print (stderr, 2, "PVClose ($%04X)\n", FD);
257 SetAX (Regs, RetVal);
262 static void PVRead (CPURegs* Regs)
265 unsigned RetVal, I = 0;
267 unsigned Count = GetAX (Regs);
268 unsigned Buf = PopParam (2);
269 unsigned FD = PopParam (2);
271 Print (stderr, 2, "PVRead ($%04X, $%04X, $%04X)\n", FD, Buf, Count);
273 Data = xmalloc (Count);
275 RetVal = read (FD, Data, Count);
277 if (RetVal != (unsigned) -1) {
279 MemWriteByte (Buf++, Data[I++]);
284 SetAX (Regs, RetVal);
289 static void PVWrite (CPURegs* Regs)
292 unsigned RetVal, I = 0;
294 unsigned Count = GetAX (Regs);
295 unsigned Buf = PopParam (2);
296 unsigned FD = PopParam (2);
298 Print (stderr, 2, "PVWrite ($%04X, $%04X, $%04X)\n", FD, Buf, Count);
300 Data = xmalloc (Count);
302 Data[I++] = MemReadByte (Buf++);
305 RetVal = write (FD, Data, Count);
309 SetAX (Regs, RetVal);
314 static const PVFunc Hooks[] = {
325 void ParaVirtInit (unsigned aArgStart)
326 /* Initialize the paravirtualization subsystem */
328 ArgStart = aArgStart;
333 void ParaVirtHooks (CPURegs* Regs)
334 /* Potentially execute paravirtualization hooks */
336 /* Check for paravirtualization address range */
337 if (Regs->PC < 0xFFF0 ||
338 Regs->PC >= 0xFFF0 + sizeof (Hooks) / sizeof (Hooks[0])) {
342 /* Call paravirtualization hook */
343 Hooks[Regs->PC - 0xFFF0] (Regs);
346 Regs->PC = Pop(Regs) + (Pop(Regs) << 8) + 1;