1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-27 00:29:31 +00:00

Added support for cc65 program arguments.

This commit is contained in:
Oliver Schmidt 2013-05-24 18:36:30 +02:00
parent 1354e1265f
commit 5bd0a53ea2
6 changed files with 115 additions and 37 deletions

16
libsrc/sim6502/mainargs.s Normal file
View File

@ -0,0 +1,16 @@
;
; Oliver Schmidt, 2013-05-16
;
.constructor initmainargs, 24
.import __argc, __argv, args
.segment "INIT"
initmainargs:
lda #<__argv
ldx #>__argv
jsr args
sta __argc
stx __argc+1
rts

View File

@ -7,10 +7,11 @@
; int __fastcall__ write (int fd, const void* buf, unsigned count); ; int __fastcall__ write (int fd, const void* buf, unsigned count);
; ;
.export exit, _open, _close, _read, _write .export args, exit, _open, _close, _read, _write
exit := $FFF0 args := $FFF0
_open := $FFF1 exit := $FFF1
_close := $FFF2 _open := $FFF2
_read := $FFF3 _close := $FFF3
_write := $FFF4 _read := $FFF4
_write := $FFF5

View File

@ -537,7 +537,7 @@ static void OPC_6502_20 (void)
PUSH (PCL); PUSH (PCL);
Regs.PC = Addr; Regs.PC = Addr;
ParaVirtualization (&Regs); ParaVirtHooks (&Regs);
} }
@ -819,7 +819,7 @@ static void OPC_6502_4C (void)
Cycles = 3; Cycles = 3;
Regs.PC = MemReadWord (Regs.PC+1); Regs.PC = MemReadWord (Regs.PC+1);
ParaVirtualization (&Regs); ParaVirtHooks (&Regs);
} }

View File

@ -47,6 +47,7 @@
#include "6502.h" #include "6502.h"
#include "error.h" #include "error.h"
#include "memory.h" #include "memory.h"
#include "paravirt.h"
@ -200,13 +201,9 @@ int main (int argc, char* argv[])
UnknownOption (Arg); UnknownOption (Arg);
break; break;
} }
} else {
/* Filename. Check if we already had one */
if (ProgramFile) {
AbEnd ("Don't know what to do with `%s'", Arg);
} else { } else {
ProgramFile = Arg; ProgramFile = Arg;
} break;
} }
/* Next argument */ /* Next argument */
@ -218,6 +215,8 @@ int main (int argc, char* argv[])
AbEnd ("No program file"); AbEnd ("No program file");
} }
ParaVirtInit (I);
MemInit (); MemInit ();
ReadProgramFile (); ReadProgramFile ();

View File

@ -33,6 +33,7 @@
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#if defined(_MSC_VER) #if defined(_MSC_VER)
@ -47,6 +48,7 @@
#endif #endif
/* common */ /* common */
#include "cmdline.h"
#include "print.h" #include "print.h"
#include "xmalloc.h" #include "xmalloc.h"
@ -65,6 +67,8 @@
typedef void (*PVFunc) (CPURegs* Regs); typedef void (*PVFunc) (CPURegs* Regs);
static unsigned ArgStart;
/*****************************************************************************/ /*****************************************************************************/
@ -73,6 +77,31 @@ typedef void (*PVFunc) (CPURegs* Regs);
static unsigned GetAX (CPURegs* Regs)
{
return Regs->AC + (Regs->XR << 8);
}
static void SetAX (CPURegs* Regs, unsigned Val)
{
Regs->AC = Val & 0xFF;
Val >>= 8;
Regs->XR = Val;
}
static void MemWriteWord (unsigned Addr, unsigned Val)
{
MemWriteByte (Addr, Val);
Val >>= 8;
MemWriteByte (Addr + 1, Val);
}
static unsigned char Pop (CPURegs* Regs) static unsigned char Pop (CPURegs* Regs)
{ {
return MemReadByte (0x0100 + ++Regs->SP); return MemReadByte (0x0100 + ++Regs->SP);
@ -84,15 +113,44 @@ static unsigned PopParam (unsigned char Incr)
{ {
unsigned SP = MemReadZPWord (0x00); unsigned SP = MemReadZPWord (0x00);
unsigned Val = MemReadWord (SP); unsigned Val = MemReadWord (SP);
SP += Incr; MemWriteWord (0x0000, SP + Incr);
MemWriteByte (0x00, SP);
SP >>= 8;
MemWriteByte (0x01, SP);
return Val; return Val;
} }
static void PVArgs (CPURegs* Regs)
{
unsigned ArgC = ArgCount - ArgStart;
unsigned ArgV = GetAX (Regs);
unsigned SP = MemReadZPWord (0x00);
unsigned Args = SP - (ArgC + 1) * 2;
Print (stdout, 2, "PVArgs ($%04X)\n", ArgV);
MemWriteWord (ArgV, Args);
SP = Args;
while (ArgStart < ArgCount) {
unsigned I = 0;
const char* Arg = ArgVec[ArgStart++];
SP -= strlen (Arg) + 1;
do {
MemWriteByte (SP + I, Arg[I]);
}
while (Arg[I++]);
MemWriteWord (Args, SP);
Args += 2;
}
MemWriteWord (Args, 0x0000);
MemWriteWord (0x0000, SP);
SetAX (Regs, ArgC);
}
static void PVExit (CPURegs* Regs) static void PVExit (CPURegs* Regs)
{ {
Print (stdout, 1, "PVExit ($%02X)\n", Regs->AC); Print (stdout, 1, "PVExit ($%02X)\n", Regs->AC);
@ -148,9 +206,7 @@ static void PVOpen (CPURegs* Regs)
RetVal = open (Path, OFlag); RetVal = open (Path, OFlag);
Regs->AC = RetVal & 0xFF; SetAX (Regs, RetVal);
RetVal >>= 8;
Regs->XR = RetVal & 0xFF;
} }
@ -159,15 +215,13 @@ static void PVClose (CPURegs* Regs)
{ {
unsigned RetVal; unsigned RetVal;
unsigned FD = Regs->AC + (Regs->XR << 8); unsigned FD = GetAX (Regs);
Print (stdout, 2, "PVClose ($%04X)\n", FD); Print (stdout, 2, "PVClose ($%04X)\n", FD);
RetVal = close (FD); RetVal = close (FD);
Regs->AC = RetVal & 0xFF; SetAX (Regs, RetVal);
RetVal >>= 8;
Regs->XR = RetVal & 0xFF;
} }
@ -177,7 +231,7 @@ static void PVRead (CPURegs* Regs)
unsigned char* Data; unsigned char* Data;
unsigned RetVal, I = 0; unsigned RetVal, I = 0;
unsigned Count = Regs->AC + (Regs->XR << 8); unsigned Count = GetAX (Regs);
unsigned Buf = PopParam (2); unsigned Buf = PopParam (2);
unsigned FD = PopParam (2); unsigned FD = PopParam (2);
@ -194,9 +248,7 @@ static void PVRead (CPURegs* Regs)
} }
xfree (Data); xfree (Data);
Regs->AC = RetVal & 0xFF; SetAX (Regs, RetVal);
RetVal >>= 8;
Regs->XR = RetVal & 0xFF;
} }
@ -206,7 +258,7 @@ static void PVWrite (CPURegs* Regs)
unsigned char* Data; unsigned char* Data;
unsigned RetVal, I = 0; unsigned RetVal, I = 0;
unsigned Count = Regs->AC + (Regs->XR << 8); unsigned Count = GetAX (Regs);
unsigned Buf = PopParam (2); unsigned Buf = PopParam (2);
unsigned FD = PopParam (2); unsigned FD = PopParam (2);
@ -221,14 +273,13 @@ static void PVWrite (CPURegs* Regs)
xfree (Data); xfree (Data);
Regs->AC = RetVal & 0xFF; SetAX (Regs, RetVal);
RetVal >>= 8;
Regs->XR = RetVal & 0xFF;
} }
static const PVFunc Hooks[] = { static const PVFunc Hooks[] = {
PVArgs,
PVExit, PVExit,
PVOpen, PVOpen,
PVClose, PVClose,
@ -238,8 +289,16 @@ static const PVFunc Hooks[] = {
void ParaVirtualization (CPURegs* Regs) void ParaVirtInit (unsigned aArgStart)
/* Potentially execute paravirtualization hook */ /* Initialize the paravirtualization subsystem */
{
ArgStart = aArgStart;
};
void ParaVirtHooks (CPURegs* Regs)
/* Potentially execute paravirtualization hooks */
{ {
/* Check for paravirtualization address range */ /* Check for paravirtualization address range */
if (Regs->PC < 0xFFF0 || if (Regs->PC < 0xFFF0 ||

View File

@ -44,8 +44,11 @@
void ParaVirtualization (CPURegs* Regs); void ParaVirtInit (unsigned aArgStart);
/* Potentially execute paravirtualization hook */ /* Initialize the paravirtualization subsystem */
void ParaVirtHooks (CPURegs* Regs);
/* Potentially execute paravirtualization hooks */