sim65: add C/assembly support, docs, and samples for the new peripheral functionality.
This PR is the second 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 second and last PR provides the following things in relation to the new "peripheral" support: * C support: there is now an include/sim65.h that can be included from C. It provides access to the memory-mapped peripheral addresses. * Asm support: there is now an asminc/sim65.inc that can be included from assembly. It provides symbolic labels for the memory-mapped peripheral addresses. Note: the two items above are implemented by adding a "_peripherals" symbol to cfg/sim6502.cfg and cfg/sim65c02.cfg, with the fixed base address of the peripherals memory aperture (0xffc0). * Updated the sim65 documentation to describe the peripherals in some detail, with examples that show to use the new features from within C. * Some examples in the new samples/sim5/ directory. These are currently not integrated in the build system (in other words, there's no Makefile there), because I don't know how to do that. I will happily implement that after #2582 is taken care of. If that is not acceptable, the next best thing will be for somebody else (who understands how the Makefiles are set up) to take care of this. If that's not going to happen, and we don't want examples that are not properly integrated with the build system, there's always the option of removing these samples from the PR.
This commit is contained in:
104
samples/sim65/cpumode_example.c
Normal file
104
samples/sim65/cpumode_example.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Sim65 cpu-mode switching example.
|
||||
*
|
||||
* Description
|
||||
* -----------
|
||||
*
|
||||
* We can inspect and manipulate the CPU model that sim65 emulates at runtime.
|
||||
*
|
||||
* Sim65 always runs in one of three modes:
|
||||
*
|
||||
* - 6502 mode: the 151 documented opcodes are supported; if the processor encounters
|
||||
* one of the 105 undocumented opcodes, the simulator ends with an
|
||||
* 'illegal opcode' message.
|
||||
* - 65C02 mode: the 105 undocumented opcodes now have well-defined behavior. Some
|
||||
* do useful things, while all others are now defined as NOPs.
|
||||
* - 6502X mode: the 105 undocumented opcodes don't have documented behavior, but
|
||||
* they /do/ have behavior on a real 6502. This behavior has been
|
||||
* figured out, and is deterministic (with minor exceptions).
|
||||
* In this mode, sim65 mimics the behavior of a real 6502 when
|
||||
* it encounters an undocumented opcode, rather than terminating.
|
||||
*
|
||||
* In the example below, we first switch to 6502X mode and execute a small
|
||||
* assembly code fragment, then repeat this in 65C02 mode.
|
||||
*
|
||||
* The code fragment is designed to distinguish between a 6502 and a 65C02
|
||||
* processor based on the behavior of the ADC function in decimal mode.
|
||||
*
|
||||
* Important Note:
|
||||
*
|
||||
* When running in a program compiled for the "sim6502" target, it is safe to switch to
|
||||
* 65C02 or 6502X mode, since the runtime library will only use plain 6502 opcodes, and
|
||||
* those work the same in 65C02 and 6502X mode.
|
||||
*
|
||||
* However, when running in a program compiled for the "sim65c02" target, it is NOT safe
|
||||
* to switch to 6502 or 6502X mode, since many routines in the runtime library use
|
||||
* 65C02-specific opcodes, and these will not work as expected when the CPU is switched
|
||||
* to 6502 or 6502X mode. When such an instruction is encountered, the program will
|
||||
* exhibit undefined behavior.
|
||||
*
|
||||
* For this reason, this program will only work when compiled for the "sim6502" target.
|
||||
*
|
||||
* Running the example
|
||||
* -------------------
|
||||
*
|
||||
* cl65 -t sim6502 -O cpumode_example.c -o cpumode_example.prg
|
||||
* sim65 cpumode_example.prg
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <sim65.h>
|
||||
|
||||
static bool __fastcall__ is_65c02(void)
|
||||
{
|
||||
/* This assembly routine loads 0 into AX on a 6502 (also on a 6502 on which decimal
|
||||
* mode is not implemented), and 1 on a 65C02.
|
||||
*
|
||||
* Note: this implementation triggers a "control reaches end of non-void function"
|
||||
* warning that can be safely ignored. While no return statement is present, the
|
||||
* return value is correctly loaded into AX by the assembly code.
|
||||
*/
|
||||
__asm__("ldx #0");
|
||||
__asm__("sed");
|
||||
__asm__("txa");
|
||||
__asm__("sbc #28");
|
||||
__asm__("asl a");
|
||||
__asm__("sbc #28");
|
||||
__asm__("and #1");
|
||||
__asm__("cld");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("CPU mode at startup ....... : %u\n", GET_CPU_MODE());
|
||||
printf("Is 65C02? ................. : %s\n", is_65c02() ? "YES" : "NO");
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Switching to 6502 mode ....\n");
|
||||
SET_CPU_MODE(SIM65_CPU_MODE_6502);
|
||||
printf("Current CPU mode .......... : %u\n", GET_CPU_MODE());
|
||||
printf("Is 65C02? ................. : %s\n", is_65c02() ? "YES" : "NO");
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Switching to 65C02 mode ...\n");
|
||||
SET_CPU_MODE(SIM65_CPU_MODE_65C02);
|
||||
printf("Current CPU mode .......... : %u\n", GET_CPU_MODE());
|
||||
printf("Is 65C02? ................. : %s\n", is_65c02() ? "YES" : "NO");
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Switching to 6502X mode ...\n");
|
||||
SET_CPU_MODE(SIM65_CPU_MODE_6502X);
|
||||
printf("Current CPU mode .......... : %u\n", GET_CPU_MODE());
|
||||
printf("Is 65C02? ................. : %s\n", is_65c02() ? "YES" : "NO");
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Bye!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
117
samples/sim65/timer_example.c
Normal file
117
samples/sim65/timer_example.c
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Sim65 timer example.
|
||||
*
|
||||
* Description
|
||||
* -----------
|
||||
*
|
||||
* This example tests the clock cycle counter feature of sim65.
|
||||
*
|
||||
* The function 'timestamp' obtains the lower 32-bits of the clock cycle counter.
|
||||
*
|
||||
* The function 'calc_sum_terms' calculates the sum of a range of integers
|
||||
* starting at zero. It simply iterates over all terms, which means that its
|
||||
* runtime is a linear function of its input value.
|
||||
*
|
||||
* In the main function, we first derive an 'offset' value by getting two timestamp
|
||||
* values, with nothing happening in between. Ideally this should yield a 0 clock
|
||||
* cycle duration, but due to the overhead of calling the 'timestamp' function,
|
||||
* and the 'timestamp' function itself, the difference between these timestamp
|
||||
* will be non-zero. We store this value in the 'overhead' variable, and subtract
|
||||
* this value in later measurements.
|
||||
*
|
||||
* Next, we measure the duration of calling the function 'calc_sum_terms' with two
|
||||
* input values, 0, and 1. The duration includes storing the result in the 'result'
|
||||
* variable.
|
||||
*
|
||||
* Extrapolating from these two measurements, and assuming that the runtime of
|
||||
* calling 'calc_sum_terms' and storing its result scales linearly with its argument,
|
||||
* we can predict the duration of a call to 'calc_sum_terms' with a much larger
|
||||
* argument (max_terms = 10000).
|
||||
*
|
||||
* Finally, we actually measure the duration with max_terms = 10000. If the
|
||||
* duration measured is equal to the predicted value, we exit successfully. If not,
|
||||
* we exit with failure.
|
||||
*
|
||||
* Running the example
|
||||
* -------------------
|
||||
*
|
||||
* cl65 -t sim6502 -O timer_example.c -o timer_example.prg
|
||||
* sim65 timer_example.prg
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sim65.h>
|
||||
|
||||
static uint32_t timestamp(void)
|
||||
{
|
||||
peripherals.counter.select = COUNTER_SELECT_CLOCKCYCLE_COUNTER;
|
||||
peripherals.counter.latch = 0;
|
||||
return peripherals.counter.value32[0];
|
||||
}
|
||||
|
||||
static unsigned long calc_sum_terms(unsigned max_term)
|
||||
/* A function with a runtime that scales linearly with its argument. */
|
||||
{
|
||||
unsigned k;
|
||||
unsigned long sum = 0;
|
||||
for (k = 0; k <= max_term; ++k)
|
||||
{
|
||||
sum += k;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned max_term;
|
||||
unsigned long result;
|
||||
uint32_t t1, t2, overhead, duration;
|
||||
int32_t d0, d1;
|
||||
int32_t predicted_duration;
|
||||
|
||||
/* Calibration measurement of zero clock cycles, to determine the overhead. */
|
||||
|
||||
overhead = 0;
|
||||
t1 = timestamp();
|
||||
t2 = timestamp() - overhead;
|
||||
overhead = (t2 - t1);
|
||||
|
||||
/* Calculate call duration (including assignment of result) for argument value 0. */
|
||||
|
||||
max_term = 0;
|
||||
t1 = timestamp();
|
||||
result = calc_sum_terms(max_term);
|
||||
t2 = timestamp();
|
||||
d0 = (t2 - t1) - overhead;
|
||||
printf("max_term = %u -> result = %lu; duration = %ld\n", max_term, result, d0);
|
||||
|
||||
/* Calculate call duration (including assignment of result) for argument value 1. */
|
||||
|
||||
max_term = 1;
|
||||
t1 = timestamp();
|
||||
result = calc_sum_terms(max_term);
|
||||
t2 = timestamp();
|
||||
d1 = (t2 - t1) - overhead;
|
||||
printf("max_term = %u -> result = %lu; duration = %ld\n", max_term, result, d1);
|
||||
|
||||
/* Predict runtime for a much bigger argument value, 10000. */
|
||||
|
||||
max_term = 10000;
|
||||
predicted_duration = d0 + max_term * (d1 - d0);
|
||||
|
||||
printf("predicted duration for max_term = %u: %lu\n", max_term, predicted_duration);
|
||||
|
||||
/* Do the actual measurement for max_term = 10000. */
|
||||
|
||||
t1 = timestamp();
|
||||
result = calc_sum_terms(max_term);
|
||||
t2 = timestamp();
|
||||
duration = (t2 - t1) - overhead;
|
||||
printf("max_term = %u -> result = %lu; duration = %ld\n", max_term, result, duration);
|
||||
|
||||
/* Report success or failure. */
|
||||
|
||||
return (duration == predicted_duration) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
40
samples/sim65/trace_example.c
Normal file
40
samples/sim65/trace_example.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Sim65 trace functionailty example.
|
||||
*
|
||||
* Description
|
||||
* -----------
|
||||
*
|
||||
* The easiest way to use tracing in sim65 is to pass the '--trace' option
|
||||
* to sim65 while starting a program.
|
||||
*
|
||||
* However, it is also possiblke to enable and disable the trace functionality
|
||||
* at runtime, from within the C code itself. This can be useful to produce
|
||||
* runtime traces of small code fragments for debugging purposes.
|
||||
*
|
||||
* In this example, We use the TRACE_ON and TRACE_OFF macros provided in sim65.h
|
||||
* to trace what the CPU is doing during a single statement: the assignment of
|
||||
* a constant to a global variable.
|
||||
*
|
||||
* Running the example
|
||||
* -------------------
|
||||
*
|
||||
* cl65 -t sim6502 -O trace_example.c -o trace_example.prg
|
||||
* sim65 trace_example.prg
|
||||
*
|
||||
* Compiling and running the program like this will produce a trace of six 6502 instructions.
|
||||
* The first four instructions correspond to the 'x = 0x1234' assignment statement.
|
||||
* The last two instructions (ending in a store to address $FFCB) disable the trace facility.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sim65.h>
|
||||
|
||||
unsigned x;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
TRACE_ON();
|
||||
x = 0x1234;
|
||||
TRACE_OFF();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user