1
0
mirror of https://github.com/cc65/cc65.git synced 2025-02-14 03:31:59 +00:00

Cosmetic improvements.

This commit is contained in:
sidney 2024-12-28 06:49:23 +01:00
parent e785b88d42
commit 29063021a8

View File

@ -2,7 +2,7 @@
/* */ /* */
/* peripherals.c */ /* peripherals.c */
/* */ /* */
/* Memory-mapped peripheral subsystem for the 6502 simulator */ /* Memory-mapped peripheral subsystem for the 6502 simulator */
/* */ /* */
/* */ /* */
/* */ /* */
@ -30,13 +30,15 @@
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
#include "peripherals.h"
#if defined(__MINGW64__) #if defined(__MINGW64__)
/* For gettimeofday() */ /* For gettimeofday() */
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include "peripherals.h"
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
@ -62,13 +64,12 @@ static bool GetWallclockTime (struct timespec * ts)
#if defined(__MINGW64__) #if defined(__MINGW64__)
/* When using the MinGW64 compiler, neither timespec_get() nor clock_gettime() /* When using the MinGW64 compiler, neither timespec_get() nor clock_gettime()
* are available; using either of them makes the Linux workflow build fail. * are available; using either of them makes the Linux workflow build fail.
* The gettimeofday() function does work, so use that; its microsecond resulution * The gettimeofday() function does work, so use that; its microsecond resolution
* is plenty for most applications. * is fine for most applications.
*/ */
struct timeval tv; struct timeval tv;
time_valid = (gettimeofday(&tv, NULL) == 0); time_valid = (gettimeofday(&tv, NULL) == 0);
if (time_valid) if (time_valid) {
{
ts->tv_sec = tv.tv_sec; ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000; ts->tv_nsec = tv.tv_usec * 1000;
} }
@ -78,7 +79,8 @@ static bool GetWallclockTime (struct timespec * ts)
*/ */
time_valid = timespec_get(ts, TIME_UTC) == TIME_UTC; time_valid = timespec_get(ts, TIME_UTC) == TIME_UTC;
#else #else
/* On all other compilers, assume that the clock_gettime() function is available. /* On all other compilers, assume that clock_gettime() is available.
* This is true on Linux and MacOS, at least.
*/ */
time_valid = clock_gettime(CLOCK_REALTIME, ts) == 0; time_valid = clock_gettime(CLOCK_REALTIME, ts) == 0;
#endif #endif
@ -101,7 +103,7 @@ void PeripheralsWriteByte (uint8_t Addr, uint8_t Val)
/* Latch the current wallclock time before doing anything else. */ /* Latch the current wallclock time before doing anything else. */
struct timespec ts; /* The type is available on all compilers we use. */ struct timespec ts;
bool time_valid = GetWallclockTime (&ts); bool time_valid = GetWallclockTime (&ts);
if (time_valid) { if (time_valid) {
@ -109,11 +111,11 @@ void PeripheralsWriteByte (uint8_t Addr, uint8_t Val)
Peripherals.Counter.LatchedWallclockTime = 1000000000 * (uint64_t)ts.tv_sec + ts.tv_nsec; Peripherals.Counter.LatchedWallclockTime = 1000000000 * (uint64_t)ts.tv_sec + ts.tv_nsec;
/* Wallclock time, split: high word is number of seconds since 1-1-1970, /* Wallclock time, split: high word is number of seconds since 1-1-1970,
* low word is number of nanoseconds since the start of that second. */ * low word is number of nanoseconds since the start of that second. */
Peripherals.Counter.LatchedWallclockTimeSplit = ((uint64_t)ts.tv_sec << 32) | ts.tv_nsec; Peripherals.Counter.LatchedWallclockTimeSplit = (uint64_t)ts.tv_sec << 32 | ts.tv_nsec;
} else { } else {
/* Unable to get time. Report max uint64 value for both fields. */ /* Unable to get time. Report max uint64 value for both fields. */
Peripherals.Counter.LatchedWallclockTime = 0xffffffffffffffff; Peripherals.Counter.LatchedWallclockTime = -1;
Peripherals.Counter.LatchedWallclockTimeSplit = 0xffffffffffffffff; Peripherals.Counter.LatchedWallclockTimeSplit = -1;
} }
/* Latch the counters that reflect the state of the processor. */ /* Latch the counters that reflect the state of the processor. */
@ -160,8 +162,9 @@ uint8_t PeripheralsReadByte (uint8_t Addr)
/* Read from any of the eight counter bytes. /* Read from any of the eight counter bytes.
* The first byte is the 64 bit value's LSB, the seventh byte is its MSB. * The first byte is the 64 bit value's LSB, the seventh byte is its MSB.
*/ */
unsigned ByteIndex = Addr - PERIPHERALS_COUNTER_ADDRESS_OFFSET_VALUE; /* 0 .. 7 */ unsigned SelectedByteIndex = Addr - PERIPHERALS_COUNTER_ADDRESS_OFFSET_VALUE; /* 0 .. 7 */
uint64_t Value; uint64_t Value;
uint8_t SelectedByteValue;
switch (Peripherals.Counter.LatchedValueSelected) { switch (Peripherals.Counter.LatchedValueSelected) {
case PERIPHERALS_COUNTER_SELECT_CLOCKCYCLE_COUNTER: Value = Peripherals.Counter.LatchedClockCycles; break; case PERIPHERALS_COUNTER_SELECT_CLOCKCYCLE_COUNTER: Value = Peripherals.Counter.LatchedClockCycles; break;
case PERIPHERALS_COUNTER_SELECT_INSTRUCTION_COUNTER: Value = Peripherals.Counter.LatchedCpuInstructions; break; case PERIPHERALS_COUNTER_SELECT_INSTRUCTION_COUNTER: Value = Peripherals.Counter.LatchedCpuInstructions; break;
@ -172,7 +175,8 @@ uint8_t PeripheralsReadByte (uint8_t Addr)
default: Value = 0; /* Reading from a non-existent latch register will yield 0. */ default: Value = 0; /* Reading from a non-existent latch register will yield 0. */
} }
/* Return the desired byte of the latched counter. 0==LSB, 7==MSB. */ /* Return the desired byte of the latched counter. 0==LSB, 7==MSB. */
return (uint8_t)(Value >> (ByteIndex * 8)); SelectedByteValue = Value >> (SelectedByteIndex * 8);
return SelectedByteValue;
} }
/* Handle reads from unused peripheral and write-only addresses. */ /* Handle reads from unused peripheral and write-only addresses. */