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 /*****************************************************************************/
40 # define O_INITIAL O_BINARY
45 /* Microsoft compiler */
64 /*****************************************************************************/
66 /*****************************************************************************/
70 typedef void (*PVFunc) (CPURegs* Regs);
72 static unsigned ArgStart;
76 /*****************************************************************************/
78 /*****************************************************************************/
82 static unsigned GetAX (CPURegs* Regs)
84 return Regs->AC + (Regs->XR << 8);
89 static void SetAX (CPURegs* Regs, unsigned Val)
91 Regs->AC = Val & 0xFF;
98 static void MemWriteWord (unsigned Addr, unsigned Val)
100 MemWriteByte (Addr, Val);
102 MemWriteByte (Addr + 1, Val);
107 static unsigned char Pop (CPURegs* Regs)
109 return MemReadByte (0x0100 + ++Regs->SP);
114 static unsigned PopParam (unsigned char Incr)
116 unsigned SP = MemReadZPWord (0x00);
117 unsigned Val = MemReadWord (SP);
118 MemWriteWord (0x0000, SP + Incr);
124 static void PVArgs (CPURegs* Regs)
126 unsigned ArgC = ArgCount - ArgStart;
127 unsigned ArgV = GetAX (Regs);
128 unsigned SP = MemReadZPWord (0x00);
129 unsigned Args = SP - (ArgC + 1) * 2;
131 Print (stderr, 2, "PVArgs ($%04X)\n", ArgV);
133 MemWriteWord (ArgV, Args);
136 while (ArgStart < ArgCount) {
138 const char* Arg = ArgVec[ArgStart++];
139 SP -= strlen (Arg) + 1;
141 MemWriteByte (SP + I, Arg[I]);
145 MemWriteWord (Args, SP);
148 MemWriteWord (Args, 0x0000);
150 MemWriteWord (0x0000, SP);
156 static void PVExit (CPURegs* Regs)
158 Print (stderr, 1, "PVExit ($%02X)\n", Regs->AC);
160 Print (stdout, 0, "%lu cycles\n", GetCycles ());
168 static void PVOpen (CPURegs* Regs)
171 int OFlag = O_INITIAL;
172 unsigned RetVal, I = 0, OMode = 0;
174 unsigned Mode = PopParam (Regs->YR - 4);
175 unsigned Flags = PopParam (2);
176 unsigned Name = PopParam (2);
179 Path[I] = MemReadByte (Name++);
183 Print (stderr, 2, "PVOpen (\"%s\", $%04X)\n", Path, Flags);
185 switch (Flags & 0x03) {
216 RetVal = open (Path, OFlag, OMode);
218 SetAX (Regs, RetVal);
223 static void PVClose (CPURegs* Regs)
227 unsigned FD = GetAX (Regs);
229 Print (stderr, 2, "PVClose ($%04X)\n", FD);
233 SetAX (Regs, RetVal);
238 static void PVRead (CPURegs* Regs)
241 unsigned RetVal, I = 0;
243 unsigned Count = GetAX (Regs);
244 unsigned Buf = PopParam (2);
245 unsigned FD = PopParam (2);
247 Print (stderr, 2, "PVRead ($%04X, $%04X, $%04X)\n", FD, Buf, Count);
249 Data = xmalloc (Count);
251 RetVal = read (FD, Data, Count);
253 if (RetVal != (unsigned) -1) {
255 MemWriteByte (Buf++, Data[I++]);
260 SetAX (Regs, RetVal);
265 static void PVWrite (CPURegs* Regs)
268 unsigned RetVal, I = 0;
270 unsigned Count = GetAX (Regs);
271 unsigned Buf = PopParam (2);
272 unsigned FD = PopParam (2);
274 Print (stderr, 2, "PVWrite ($%04X, $%04X, $%04X)\n", FD, Buf, Count);
276 Data = xmalloc (Count);
278 Data[I++] = MemReadByte (Buf++);
281 RetVal = write (FD, Data, Count);
285 SetAX (Regs, RetVal);
290 static const PVFunc Hooks[] = {
301 void ParaVirtInit (unsigned aArgStart)
302 /* Initialize the paravirtualization subsystem */
304 ArgStart = aArgStart;
309 void ParaVirtHooks (CPURegs* Regs)
310 /* Potentially execute paravirtualization hooks */
312 /* Check for paravirtualization address range */
313 if (Regs->PC < 0xFFF0 ||
314 Regs->PC >= 0xFFF0 + sizeof (Hooks) / sizeof (Hooks[0])) {
318 /* Call paravirtualization hook */
319 Hooks[Regs->PC - 0xFFF0] (Regs);
322 Regs->PC = Pop(Regs) + (Pop(Regs) << 8) + 1;