From 5bd0a53ea2da516488835bd2d0e8a4eabc806f06 Mon Sep 17 00:00:00 2001 From: Oliver Schmidt Date: Fri, 24 May 2013 18:36:30 +0200 Subject: [PATCH] Added support for cc65 program arguments. --- libsrc/sim6502/mainargs.s | 16 ++++++ libsrc/sim6502/paravirt.s | 13 ++--- src/sim65/6502.c | 4 +- src/sim65/main.c | 11 ++--- src/sim65/paravirt.c | 101 ++++++++++++++++++++++++++++++-------- src/sim65/paravirt.h | 7 ++- 6 files changed, 115 insertions(+), 37 deletions(-) create mode 100644 libsrc/sim6502/mainargs.s diff --git a/libsrc/sim6502/mainargs.s b/libsrc/sim6502/mainargs.s new file mode 100644 index 000000000..1daa23ace --- /dev/null +++ b/libsrc/sim6502/mainargs.s @@ -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 diff --git a/libsrc/sim6502/paravirt.s b/libsrc/sim6502/paravirt.s index 8c12cf85d..81b655c73 100644 --- a/libsrc/sim6502/paravirt.s +++ b/libsrc/sim6502/paravirt.s @@ -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 diff --git a/src/sim65/6502.c b/src/sim65/6502.c index f00aa3943..2a43e40dc 100644 --- a/src/sim65/6502.c +++ b/src/sim65/6502.c @@ -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); } diff --git a/src/sim65/main.c b/src/sim65/main.c index 2e2c2085b..8ac01a20b 100644 --- a/src/sim65/main.c +++ b/src/sim65/main.c @@ -47,6 +47,7 @@ #include "6502.h" #include "error.h" #include "memory.h" +#include "paravirt.h" @@ -201,12 +202,8 @@ int main (int argc, char* argv[]) break; } } else { - /* Filename. Check if we already had one */ - if (ProgramFile) { - AbEnd ("Don't know what to do with `%s'", Arg); - } else { - ProgramFile = Arg; - } + ProgramFile = Arg; + break; } /* Next argument */ @@ -218,6 +215,8 @@ int main (int argc, char* argv[]) AbEnd ("No program file"); } + ParaVirtInit (I); + MemInit (); ReadProgramFile (); diff --git a/src/sim65/paravirt.c b/src/sim65/paravirt.c index 2de8f4775..29b8ea930 100644 --- a/src/sim65/paravirt.c +++ b/src/sim65/paravirt.c @@ -33,6 +33,7 @@ +#include #include #include #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 || diff --git a/src/sim65/paravirt.h b/src/sim65/paravirt.h index bf483e29f..34317b49f 100644 --- a/src/sim65/paravirt.h +++ b/src/sim65/paravirt.h @@ -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 */ -- 2.39.5