From d17936488c9260774fe2e28dcc1277d34a878358 Mon Sep 17 00:00:00 2001 From: cuz Date: Mon, 1 Apr 2002 17:42:24 +0000 Subject: [PATCH] Working git-svn-id: svn://svn.cc65.org/cc65/trunk@1212 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/sim65/cpucore.c | 33 +++++++++++++- src/sim65/cpucore.h | 23 ++++++++++ src/sim65/main.c | 6 ++- src/sim65/memory.c | 107 +++++++++++++++++++++++++++++++++++++++++--- src/sim65/memory.h | 3 ++ 5 files changed, 164 insertions(+), 8 deletions(-) diff --git a/src/sim65/cpucore.c b/src/sim65/cpucore.c index b469b95c9..2058ebda6 100644 --- a/src/sim65/cpucore.c +++ b/src/sim65/cpucore.c @@ -2470,13 +2470,42 @@ static OPCFunc OPCTable[256] = { -void RunCPU (void) +void CPUInit (void) +/* Initialize the CPU */ +{ + PC = MemReadWord (0xFFFC); +} + + + +void Reset (void) +/* Reset the CPU */ +{ +} + + + +void IRQ (void) +/* Generate an IRQ */ +{ +} + + + +void NMI (void) +/* Generate an NMI */ +{ +} + + + +void CPURun (void) /* Run the CPU */ { while (!CPUHalted) { /* Get the next opcode */ - unsigned char B = 0x00; + unsigned char B = MemReadByte (PC); /* Execute it */ OPCTable[B] (); diff --git a/src/sim65/cpucore.h b/src/sim65/cpucore.h index c3da89654..09867fd28 100644 --- a/src/sim65/cpucore.h +++ b/src/sim65/cpucore.h @@ -63,6 +63,29 @@ extern unsigned PC; /* Program counter */ +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void CPUInit (void); +/* Initialize the CPU */ + +void Reset (void); +/* Reset the CPU */ + +void IRQ (void); +/* Generate an IRQ */ + +void NMI (void); +/* Generate an NMI */ + +void CPURun (void); +/* Run the CPU */ + + + /* End of cpucore.h */ #endif diff --git a/src/sim65/main.c b/src/sim65/main.c index 20ac09d82..89f48148d 100644 --- a/src/sim65/main.c +++ b/src/sim65/main.c @@ -47,7 +47,7 @@ /* sim65 */ #include "cputype.h" #include "global.h" - +#include "memory.h" /*****************************************************************************/ @@ -203,6 +203,10 @@ int main (int argc, char* argv[]) AbEnd ("No input files"); } + /* Initialize modules */ + MemInit (); + + /* Return an apropriate exit code */ return EXIT_SUCCESS; } diff --git a/src/sim65/memory.c b/src/sim65/memory.c index c433d41f3..3bf85f6db 100644 --- a/src/sim65/memory.c +++ b/src/sim65/memory.c @@ -33,23 +33,90 @@ +/* common */ +#include "coll.h" + /* sim65 */ #include "memory.h" /*****************************************************************************/ -/* Data */ +/* Forwards */ +/*****************************************************************************/ + + + +static void MemWrite (unsigned Addr, unsigned char Val); +/* Write one byte to the memory cell */ + +static unsigned char MemRead (unsigned Attr); +/* Read one memory cell */ + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/* RAM attributes */ +#define RA_READFUNC_MASK 0x000F /* Up to 16 read functions */ +#define RA_WRITEFUNC_MASK 0x00F0 /* Up to 16 write functions */ +#define RA_INITIALIZED 0x0100 /* Memory cell is initialized */ +#define RA_WPROT 0x0200 /* Memory cell is write protected */ + +/* Defines reader and writer functions */ +#define RA_READFUNC_SHIFT 0 +#define RA_WRITEFUNC_SHIFT 4 +#define RA_READFUNC_MAX 16 +#define RA_WRITEFUNC_MAX 16 + +/* Read/write function declarations */ +typedef unsigned char (*ReadFunc) (unsigned Addr); +typedef void (*WriteFunc) (unsigned Addr, unsigned char Val); +static Collection ReadFuncs = STATIC_COLLECTION_INITIALIZER; +static Collection WriteFuncs = STATIC_COLLECTION_INITIALIZER; + +/* Memory attributes and the memory */ +static unsigned short MemAttr[0x10000]; +static unsigned char Mem[0x10000]; + + + +/*****************************************************************************/ +/* Internal functions */ /*****************************************************************************/ -static unsigned char RAM[0x10000]; +static void MemWrite (unsigned Addr, unsigned char Val) +/* Write one byte to the memory cell */ +{ + if (MemAttr[Addr] & RA_WPROT) { + /* ### */ + } + Mem[Addr] = Val; + MemAttr[Addr] |= RA_INITIALIZED; +} + + + +static unsigned char MemRead (unsigned Addr) +/* Read one memory cell */ +{ + if ((MemAttr[Addr] & RA_INITIALIZED) == 0) { + /* We're reading a memory cell that was never written */ + /* ### */ + } + return Mem[Addr]; +} /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ @@ -57,7 +124,12 @@ static unsigned char RAM[0x10000]; void MemWriteByte (unsigned Addr, unsigned char Val) /* Write a byte to a memory location */ { - RAM[Addr] = Val; + /* Get the writer function */ + unsigned WI = (MemAttr[Addr] & RA_WRITEFUNC_MASK) >> RA_WRITEFUNC_SHIFT; + WriteFunc WF = CollAt (&WriteFuncs, WI); + + /* Call the writer function */ + WF (Addr, Val); } @@ -65,7 +137,12 @@ void MemWriteByte (unsigned Addr, unsigned char Val) unsigned char MemReadByte (unsigned Addr) /* Read a byte from a memory location */ { - return RAM[Addr]; + /* Get the reader function */ + unsigned RI = (MemAttr[Addr] & RA_READFUNC_MASK) >> RA_READFUNC_SHIFT; + ReadFunc RF = CollAt (&WriteFuncs, RI); + + /* Call the reader function */ + return RF (Addr); } @@ -91,3 +168,23 @@ unsigned MemReadZPWord (unsigned char Addr) +void MemInit (void) +/* Initialize the memory subsystem */ +{ + unsigned I; + + /* Clear the memory and it's attributes. Writing zeroes to the + * attribute array will cause any special flags to be reset and + * the default read and write functions to be used. + */ + for (I = 0; I < sizeof (Mem) / sizeof (Mem[0]); ++I) { + Mem[I] = 0; + } + for (I = 0; I < sizeof (MemAttr) / sizeof (MemAttr[0]); ++I) { + MemAttr[I] = 0; + } + + /* Add the default reader and writer functions to the collection */ + CollAppend (&ReadFuncs, MemRead); + CollAppend (&WriteFuncs, MemWrite); +} diff --git a/src/sim65/memory.h b/src/sim65/memory.h index 201a27237..d080c9a8f 100644 --- a/src/sim65/memory.h +++ b/src/sim65/memory.h @@ -65,6 +65,9 @@ unsigned MemReadZPWord (unsigned char Addr); * overflow. */ +void MemInit (void); +/* Initialize the memory subsystem */ + /* End of memory.h */ -- 2.39.5