This adds timer functionality to sim65.

It provides access to a handful of 64-bit counters that count different things:
- clock cycles
- instructions
- number of IRQ processed
- number of NMIs processed
- nanoseconds since 1-1-1970.

This in not ready yet to be pushed as a merge request into the upstream CC65
repository. What's lacking:

- documentation
- tests

And to be discussed:

- do we agree on this implementation direction and interface in principe?
- can I include inttypes.h for printing a 64-bit unsigned value?
- will clock_gettime() work on a Windows build?
This commit is contained in:
Sidney Cadot
2024-12-17 23:24:35 +01:00
parent a53524b9de
commit 6f9406bbe3
8 changed files with 293 additions and 15 deletions

98
src/sim65/peripherals.h Normal file
View File

@@ -0,0 +1,98 @@
/*****************************************************************************/
/* */
/* peripherals.h */
/* */
/* Memory-mapped peripheral subsystem for the 6502 simulator */
/* */
/* */
/* */
/* (C) 2024-2025, Sidney Cadot */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* warranty. In no event will the authors be held liable for any damages */
/* arising from the use of this software. */
/* */
/* Permission is granted to anyone to use this software for any purpose, */
/* including commercial applications, and to alter it and redistribute it */
/* freely, subject to the following restrictions: */
/* */
/* 1. The origin of this software must not be misrepresented; you must not */
/* claim that you wrote the original software. If you use this software */
/* in a product, an acknowledgment in the product documentation would be */
/* appreciated but is not required. */
/* 2. Altered source versions must be plainly marked as such, and must not */
/* be misrepresented as being the original software. */
/* 3. This notice may not be removed or altered from any source */
/* distribution. */
/* */
/*****************************************************************************/
#ifndef PERIPHERALS_H
#define PERIPHERALS_H
#include <stdint.h>
#define PERIPHERALS_APERTURE_BASE_ADDRESS 0xffc0
#define PERIPHERALS_APERTURE_LAST_ADDRESS 0xffc9
#define PERIPHERALS_ADDRESS_OFFSET_LATCH 0x00
#define PERIPHERALS_ADDRESS_OFFSET_SELECT 0x01
#define PERIPHERALS_ADDRESS_OFFSET_REG64 0x02
#define PERIPHERALS_LATCH (PERIPHERALS_APERTURE_BASE_ADDRESS + PERIPHERALS_ADDRESS_OFFSET_LATCH)
#define PERIPHERALS_SELECT (PERIPHERALS_APERTURE_BASE_ADDRESS + PERIPHERALS_ADDRESS_OFFSET_SELECT)
#define PERIPHERALS_REG64 (PERIPHERALS_APERTURE_BASE_ADDRESS + PERIPHERALS_ADDRESS_OFFSET_REG64)
#define PERIPHERALS_REG64_SELECT_CLOCKCYCLE_COUNTER 0x00
#define PERIPHERALS_REG64_SELECT_INSTRUCTION_COUNTER 0x01
#define PERIPHERALS_REG64_SELECT_IRQ_COUNTER 0x02
#define PERIPHERALS_REG64_SELECT_NMI_COUNTER 0x03
#define PERIPHERALS_REG64_SELECT_WALLCLOCK_TIME 0x80
typedef struct {
/* the invisible counters that are continuously updated */
uint64_t counter_clock_cycles;
uint64_t counter_instructions;
uint64_t counter_irq_events;
uint64_t counter_nmi_events;
/* latched counters upon a write to the 'latch' address.
* One of these will be visible (read only) through an each-byte aperture. */
uint64_t latched_counter_clock_cycles;
uint64_t latched_counter_instructions;
uint64_t latched_counter_irq_events;
uint64_t latched_counter_nmi_events;
uint64_t latched_wallclock_time;
/* Select which of the five latched registers will be visible.
* This is a Read/Write byte-wide register.
* If a non-existent register is selected, the 8-byte aperture will read as zero.
*/
uint8_t visible_latch_register;
} PeripheralRegs;
extern PeripheralRegs PRegs;
/*****************************************************************************/
/* Code */
/*****************************************************************************/
void PeripheralWriteByte (uint8_t Addr, uint8_t Val);
/* Write a byte to a memory location in the peripheral address aperture. */
uint8_t PeripheralReadByte (uint8_t Addr);
/* Read a byte from a memory location in the peripheral address aperture. */
void PeripheralsInit (void);
/* Initialize the peripheral registers */
/* End of peripherals.h */
#endif