Blob Blame Raw
From 52695523465cb8bc72a0728b1f5b46ffc3eb2eef Mon Sep 17 00:00:00 2001
From: bbbradsmith <bbbradsmith@users.noreply.github.com>
Date: Thu, 30 May 2019 15:34:05 -0400
Subject: [PATCH 01/29] sim65 common define for paravirt hooks base location
 allows the loaded binary to take up as much space as possible restored some
 documentation of the hooks but without reference to specific location

---
 doc/sim65.sgml       | 25 ++++++++++---------------
 src/sim65/main.c     |  4 ++--
 src/sim65/paravirt.c |  6 +++---
 src/sim65/paravirt.h | 11 +++++++++++
 4 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/doc/sim65.sgml b/doc/sim65.sgml
index 8a7329b0..075d9584 100644
--- a/doc/sim65.sgml
+++ b/doc/sim65.sgml
@@ -115,22 +115,12 @@ The <tt/exit/ function may also be used to terminate with an exit code.
  
 Exit codes are limited to 8 bits.
 
-The standard C library high level file input and output is functional,
-and can be used like a command line application in sim65.
+The standard C library high level file input and output is functional.
+A sim65 application can be written like a command line application,
+providing arguments to <tt/main/ and using the <tt/stdio.h/ interfaces.
 
-Lower level file input and output is provided by
-a set of built-in paravirtualization functions:
-
-<tscreen><verb>
-  int open (const char* name, int flags, ...);
-  int __fastcall__ close (int fd);
-  int __fastcall__ read (int fd, void* buf, unsigned count);
-  int __fastcall__ write (int fd, const void* buf, unsigned count);
-</verb></tscreen>
-
-These built-in functions can be used with
-<tt/STDIN_FILENO/, <tt/STDOUT_FILENO/ and <tt/STDERR_FILENO/
-which are mapped to sim65's corresponding file descriptors.
+Internally, file input and output is provided at a lower level by
+a set of built-in paravirtualization functions (<ref id="paravirt-internal" name="see below">).
 
 
 <sect>Creating a Test in Assembly<p>
@@ -169,6 +159,11 @@ pre-loaded with the given <bf/reset address/.
 <item>The <tt/exit/ address is <tt/$FFF9/.
 Jumping to this address will terminate execution with the A register value as an exit code.
 
+<label id="paravirt-internal">
+<item>Several bytes immediately below the vector table are reserved for paravirtualization functions.
+Except for <tt/exit/, a <tt/JSR/ to one of these addresses will return immediately after performing a special function.
+These use cc65 calling conventions, and are intended for use with the sim65 target C library.
+
 <item><tt/IRQ/ and <tt/NMI/ events will not be generated, though <tt/BRK/
 can be used if the IRQ vector at <tt/$FFFE/ is manually prepared by the test code.
 
diff --git a/src/sim65/main.c b/src/sim65/main.c
index bcd436a1..f2daf929 100644
--- a/src/sim65/main.c
+++ b/src/sim65/main.c
@@ -200,8 +200,8 @@ static unsigned char ReadProgramFile (void)
     /* Read the file body into memory */
     Addr = Load;
     while ((Val = fgetc(F)) != EOF) {
-        if (Addr == 0xFF00) {
-            Error ("'%s': To large to fit into $%04X-$FFF0", ProgramFile, Addr);
+        if (Addr >= PARAVIRT_BASE) {
+            Error ("'%s': To large to fit into $%04X-$%04X", ProgramFile, Addr, PARAVIRT_BASE);
         }
         MemWriteByte (Addr++, (unsigned char) Val);
     }
diff --git a/src/sim65/paravirt.c b/src/sim65/paravirt.c
index c9f6e61b..603a07e9 100644
--- a/src/sim65/paravirt.c
+++ b/src/sim65/paravirt.c
@@ -318,13 +318,13 @@ void ParaVirtHooks (CPURegs* Regs)
 /* Potentially execute paravirtualization hooks */
 {
     /* Check for paravirtualization address range */
-    if (Regs->PC <  0xFFF4 ||
-        Regs->PC >= 0xFFF4 + sizeof (Hooks) / sizeof (Hooks[0])) {
+    if (Regs->PC <  PARAVIRT_BASE ||
+        Regs->PC >= PARAVIRT_BASE + sizeof (Hooks) / sizeof (Hooks[0])) {
         return;
     }
 
     /* Call paravirtualization hook */
-    Hooks[Regs->PC - 0xFFF4] (Regs);
+    Hooks[Regs->PC - PARAVIRT_BASE] (Regs);
 
     /* Simulate RTS */
     Regs->PC = Pop(Regs) + (Pop(Regs) << 8) + 1;
diff --git a/src/sim65/paravirt.h b/src/sim65/paravirt.h
index cd491539..99c28fa0 100644
--- a/src/sim65/paravirt.h
+++ b/src/sim65/paravirt.h
@@ -38,6 +38,17 @@
 
 
 
+/*****************************************************************************/
+/*                                   Data                                    */
+/*****************************************************************************/
+
+
+
+#define PARAVIRT_BASE        0xFFF4
+/* Lowest address used by a paravirtualization hook */
+
+
+
 /*****************************************************************************/
 /*                                   Code                                    */
 /*****************************************************************************/
-- 
2.21.0