sim65: add tracing, and a sim65 control peripheral for sim65 runtime control.

This PR is the first of two PRs that replaces earlier PRs #2589 and #2590.
Due to a git branching mishap it was decided to re-partition the new
functionality in two sequential PRs that offer self-contained, new
functionality to sim65.

The functionality in this first PR extends the sim65 simulator in the following ways:

(1) It provides tracing functionality, i.e., the possibility of printing one line of simulator state information per instruction executed.
(2) It provides a memory mapped "sim65 control" peripheral that allows control of (a) the tracing functionality, and (b) the cpu mode.
(3) It provides command-line options to sim65 to enable the tracing, and to override the CPU mode as specified in the program file header.

More detailed information and some discussion can be found in the discussions with the (now retracted) PRs #2589 and #2590.

This PR provides the technical infrastructure inside the sim65 simulator program itself. Once this PR is accepted, a follow-up PR will be posted that adds C and assembly-language support for the new tracing and peripheral features so they can be easily accessed from the CC65 compiler and the CA65 assembler; some examples; and the documentation for these features. The lack of the latter, in this pull request, will be addressed then.
This commit is contained in:
sidney
2025-01-03 21:39:20 +01:00
parent 823f2045cf
commit 5be0b10b62
9 changed files with 1389 additions and 32 deletions

View File

@@ -35,6 +35,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
/* common */
@@ -49,6 +50,7 @@
#include "memory.h"
#include "peripherals.h"
#include "paravirt.h"
#include "trace.h"
@@ -61,6 +63,9 @@
/* Name of program file */
const char* ProgramFile;
/* Set to True if CPU mode override is in effect. If set, the CPU is not read from the program file. */
static bool CPUOverrideActive = false;
/* exit simulator after MaxCycles Cccles */
unsigned long long MaxCycles = 0;
@@ -95,6 +100,8 @@ static void Usage (void)
"Long options:\n"
" --help\t\tHelp (this text)\n"
" --cycles\t\tPrint amount of executed CPU cycles\n"
" --cpu <type>\t\tOverride CPU type (6502, 65C02, 6502X)\n"
" --trace\t\tEnable CPU trace\n"
" --verbose\t\tIncrease verbosity\n"
" --version\t\tPrint the simulator version number\n",
ProgName);
@@ -112,6 +119,35 @@ static void OptHelp (const char* Opt attribute ((unused)),
static void OptCPU (const char* Opt, const char* Arg)
/* Set CPU type */
{
/* Don't use FindCPU here. Enum constants would clash. */
if (strcmp(Arg, "6502") == 0) {
CPU = CPU_6502;
CPUOverrideActive = true;
} else if (strcmp(Arg, "65C02") == 0 || strcmp(Arg, "65c02") == 0) {
CPU = CPU_65C02;
CPUOverrideActive = true;
} else if (strcmp(Arg, "6502X") == 0 || strcmp(Arg, "6502x") == 0) {
CPU = CPU_6502X;
CPUOverrideActive = true;
} else {
AbEnd ("Invalid argument for %s: '%s'", Opt, Arg);
}
}
static void OptTrace (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused)))
/* Enable trace mode */
{
TraceMode = TRACE_ENABLE_FULL; /* Enable full trace mode. */
}
static void OptVerbose (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused)))
/* Increase verbosity */
@@ -135,16 +171,20 @@ static void OptVersion (const char* Opt attribute ((unused)),
/* Print the simulator version */
{
fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
exit(EXIT_SUCCESS);
exit (EXIT_SUCCESS);
}
static void OptQuitXIns (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused)))
/* quit after MaxCycles cycles */
const char* Arg)
/* Quit after MaxCycles cycles */
{
MaxCycles = strtoull(Arg, NULL, 0);
}
static unsigned char ReadProgramFile (void)
/* Load program into memory */
{
@@ -173,17 +213,20 @@ static unsigned char ReadProgramFile (void)
Error ("'%s': Invalid header version.", ProgramFile);
}
/* Get the CPU type from the file header */
/* Get the CPU type from the file header.
* Use it to set the CPU type, unless CPUOverrideActive is set.
*/
if ((Val = fgetc(F)) != EOF) {
switch (Val) {
case CPU_6502:
case CPU_65C02:
case CPU_6502X:
CPU = Val;
break;
default:
Error ("'%s': Invalid CPU type", ProgramFile);
if (!CPUOverrideActive) {
switch (Val) {
case CPU_6502:
case CPU_65C02:
case CPU_6502X:
CPU = Val;
break;
default:
Error ("'%s': Invalid CPU type", ProgramFile);
}
}
}
@@ -238,16 +281,22 @@ int main (int argc, char* argv[])
{
/* Program long options */
static const LongOpt OptTab[] = {
{ "--help", 0, OptHelp },
{ "--cycles", 0, OptCycles },
{ "--verbose", 0, OptVerbose },
{ "--version", 0, OptVersion },
{ "--help", 0, OptHelp },
{ "--cycles", 0, OptCycles },
{ "--cpu", 1, OptCPU },
{ "--trace", 0, OptTrace },
{ "--verbose", 0, OptVerbose },
{ "--version", 0, OptVersion },
};
unsigned I;
unsigned char SPAddr;
unsigned int Cycles;
/* Set reasonable defaults. */
CPU = CPU_6502;
TraceMode = TRACE_DISABLED; /* Disabled by default */
/* Initialize the cmdline module */
InitCmdLine (&argc, &argv, "sim65");
@@ -302,16 +351,29 @@ int main (int argc, char* argv[])
}
/* Do we have a program file? */
if (ProgramFile == 0) {
if (ProgramFile == NULL) {
AbEnd ("No program file");
}
/* Reset memory */
MemInit ();
/* Reset peripherals. */
PeripheralsInit ();
/* Read program file into memory.
* This also sets the CPU type, unless a CPU override is in effect.
*/
SPAddr = ReadProgramFile ();
/* Initialize the paravirtualization subsystem. It requires the stack pointer address, to be able to
* simulate 6502 subroutine calls.
*/
TraceInit(SPAddr);
ParaVirtInit (I, SPAddr);
/* Reset the CPU */
Reset ();
RemainCycles = MaxCycles;