1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-26 08:32:00 +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);
;
.export exit, _open, _close, _read, _write
.export args, exit, _open, _close, _read, _write
exit := $FFF0
_open := $FFF1
_close := $FFF2
_read := $FFF3
_write := $FFF4
args := $FFF0
exit := $FFF1
_open := $FFF2
_close := $FFF3
_read := $FFF4
_write := $FFF5

View File

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

View File

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

View File

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

View File

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