Commit Graph

185 Commits

Author SHA1 Message Date
Gorilla Sapiens
b6f42f9ab2 changed "spc" to "c_sp" 2025-06-04 06:37:59 +00:00
Gorilla Sapiens
3d118dc6e5 rename "sp" to "spc", avoid conflict with 4510 opcodes 2025-06-04 02:06:40 +00:00
Bob Andrews
d5cf8ef7ea Merge pull request #2587 from colinleroy/rewind-asm-and-fseek-paravirt
Rewrite rewind() in assembly,add lseek() to paravirt
2025-01-27 17:18:12 +01:00
sidney
5be0b10b62 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.
2025-01-03 21:39:20 +01:00
Colin Leroy-Mira
50cccc2c3a Add lseek to sim6502 paravirt 2025-01-01 14:32:03 +01:00
sidney
66594d44a5 Forget to include sys/time.h which is needed for MingGW32 builds. 2025-01-01 08:58:51 +01:00
Sidney Cadot
acb8eae032 Merge branch 'master' into feature-add-timer 2024-12-31 12:37:55 +01:00
sidney
511f4478de Fixed style issues. 2024-12-29 22:26:38 +01:00
sidney
7576f59e6a Visual Studio compiler demands an explicit cast from uint64_t to uint8_t. 2024-12-28 06:57:37 +01:00
sidney
29063021a8 Cosmetic improvements. 2024-12-28 06:49:23 +01:00
sidney
e785b88d42 Cleaning up the GetWallclockTime function. 2024-12-27 09:21:04 +01:00
sidney
58b8c14738 Split off the compiler-depended wallclock time function in a separate function. 2024-12-27 08:57:58 +01:00
sidney
c735a83a98 Enable timespec_get in MINGW64 code path to see if that works (7). 2024-12-27 02:19:36 +01:00
sidney
6ccde66c86 Enable timespec_get in MINGW64 code path to see if that works (6). 2024-12-27 02:06:15 +01:00
sidney
083c968885 Enable timespec_get in MINGW64 code path to see if that works (5). 2024-12-27 01:53:16 +01:00
sidney
a94b389965 Enable timespec_get in MINGW64 code path to see if that works (4). 2024-12-27 01:39:18 +01:00
sidney
2743644b02 Enable timespec_get in MINGW64 code path to see if that works (3). 2024-12-27 01:24:44 +01:00
sidney
74f12b4498 Enable timespec_get in MINGW64 code path to see if that works (2). 2024-12-27 01:12:04 +01:00
sidney
65d20eaab4 Enable timespec_get in MINGW64 code path to see if that works. 2024-12-27 00:55:13 +01:00
sidney
328006e500 Split out cases for MINGW32 and MINGW64 2024-12-27 00:42:29 +01:00
sidney
f3e8f36f00 Corrected typo. 2024-12-27 00:28:19 +01:00
sidney
db0b8c2d2c Check if we have it working now on the MinGW32 and 64 compilers. 2024-12-27 00:21:50 +01:00
sidney
ae3106af4a Windows version now uses timespec_get() as a substitute for clock_gettime(). 2024-12-27 00:04:06 +01:00
sidney
1f9e731fc9 Try if gettimeofday() will work in Windows. 2024-12-26 23:48:34 +01:00
sidney
8c40568566 Made wallclock time fail on Windows (due to lack of clock_gettime), succeed everywhere else. 2024-12-26 23:32:00 +01:00
sidney
a906402308 Re-enabling the clock_gettime() code path. 2024-12-26 23:15:41 +01:00
sidney
61bedbdd55 Added explicit cast to uint8_t, to make the Cisual Studio compiler happy. 2024-12-26 22:55:52 +01:00
Sidney Cadot
af8a04dba0 Merge branch 'cc65:master' into feature-add-timer 2024-12-26 15:07:52 +01:00
Sidney Cadot
e149d1dcf6 Disable the use of clock_gettime(), to see if this fixes the CC65 CI builds. 2024-12-24 11:14:30 +01:00
Sidney Cadot
7980b81ddb sim65: Fix instruction timings for 6502 and 65C02.
This PR fixes all discrepancies of sim65 instruction timings, for both the 6502 and the 65C02 processors.

The timings as implemented in this PR have been verified against actual hardware (Atari 800 XL for 6502; and WDC 65C02 for 65C02).

These timings can also be verified against the 65x02 test suite. However, in this case, a single discrepancy arises; the 65x02 testsuite suggests that the 65C02 opcode 0x5c should take 4 clocks. However, tests on a hardware 65C02 have conclusively shown that this instruction takes 8 clock cycles. The 8 clock cycles duration for the 65C02 0xfc opcode is also confirmed by other sources, e.g. Section 9 of http://www.6502.org/tutorials/65c02opcodes.html.

This test makes sim65 correct both in terms of functionality (all opcodes now do what they do on hardware) and in terms of timing (all instructions take as long as they would on real hardware).

The one discrepancy that remains, is that on a real 6502/65C02, some instructions issue R or W cycles on the bus while the instruction processing is being done. Those spurious bus cycles are not replicated in sim65. Sim65 is thus an instruction-level simulator, rather than a bus-cycle level simulator. In other words, while the clock cycle counts for each instruction are now correct, not all clock cycles are individually simulated.
2024-12-24 09:24:02 +01:00
Sidney Cadot
c3916455a7 Merge branch 'cc65:master' into feature-add-timer 2024-12-24 08:33:47 +01:00
Sidney Cadot
07b168e062 Merge pull request #2573 from sidneycadot/fix-6502x-illegals
sim65: properly implement 5 'illegal' 6502X opcodes.
2024-12-24 08:29:16 +01:00
Sidney Cadot
fbf3bde97c sim65: properly implement 5 'illegal' 6502X opcodes.
This PR fixes the implementation of 5 illegal opcodes
in the 6502, which the 6502X supports:

* $93   SHA (zp),y
* $9B   TAS abs,y
* $9C   SHY abs,x
* $9E   SHX abs,x
* $9F   SHA abs,y

The common denominator of the previous implementation was that it didn't correctly handle the case when the Y or X indexing induced a page crossing. In those cases, the effective address calculation of the instructions becomes truly messed up (with the high byte of the address equal to the value being written).

The correctness of the implementations in this PR was verified using the 65x02 test suite, and corresponds to a (detailed) reading of the "No More Secrets" document.

Stylistically, there is room for improvement in these implementations, specifically in factoring out common behavior into macros. However, for now the "explicit" coding style will suffice. It is clear enough, and we want to reach a situation soon where the sim65
code is able to pass the full '65x02' testsuite. Once we get to that point, we can refactor this code with a lot more confidence, since we will have the benefit of a working exhaustive test to make sure we don't break stuff.
2024-12-22 19:48:41 +01:00
Sidney Cadot
d064ca424f sim65: implemented missing 65C02 instructions
This PR implements support for 32 65C02-specific instructions
to sim65: BBRx, BBSx, RMBx, SMBx, with x = 0..7.

These instructions are implemented using two macros:

* The "ZP_BITOP" macro implements the RMBx and SMBx isntructions.
* The "ZP_BIT_BRANCH" macro implements the BBRx abd BBSx instructions.

The implementation of these instructions has been verified usingthe 65x02 test suite.
2024-12-22 19:15:20 +01:00
Sidney Cadot
5291f51e73 Merge branch 'cc65:master' into feature-add-timer 2024-12-22 18:16:15 +01:00
Sidney Cadot
075514c60c Merge pull request #2569 from sidneycadot/fix-cpu-register-types
sim65: tighten 6502 register types
2024-12-22 18:00:16 +01:00
Sidney Cadot
3c5d5aac63 Merge pull request #2568 from sidneycadot/fix-ane-behavior
sim65: changes constant of the unstable "ANE" instruction.
2024-12-22 17:59:57 +01:00
Sidney Cadot
8cb941985d sim65: tighten 6502 register types
After a lot of preparatory work, we are now in position to finally tighten
the types of the 6502 registers defined in the CPURegs struct of sim65.

All registers were previously defined as bare 'unsigned', leading to subtle
bugs where the bits beyond the 8 or 16 "true" bits in the register could
become non-zero. Tightening the types of the registers to uint8_t and
uint16_t as appropriate gets rid of these subtle bugs once and for all,
assisted by the semantics of C when assigning an unsigned value to an
unsigned type with less bits: the high-order bits are simply discarded,
which is precisely what we'd want to happen.

This change cleans up a lot of spurious failures of sim65 against the
65x02 test-set. For the 6502 and 65C02, we're now *functionally*
compliant. For timing (i.e., clock cycle counts for each instruction),
some work remains.
2024-12-19 23:13:20 +01:00
Sidney Cadot
b14f883e73 sim65: changes constant of the unstable "ANE" instruction to comply with 65x02 test suite.
ANE (0x8b) is an unstable illegal opcode that depends on a "constant" value that isn't
really constant. It varies between machines, with temperature, and so on. Original sim65
behavior was to use the constant value 0xEF. To get the behavior in line with the 65x02
testsuite, we now use the value 0xEE instead, which is also a reasonable choice that can
be observed in practice.
2024-12-19 22:58:42 +01:00
Sidney Cadot
bad2f54f75 Fix memory access order of the JSR instruction.
The obvious way to implement JSR for the 6502 is to (a) read the target address,
and then (b) push the return address minus one. Or do (b) first, then (a).

However, there is a non-obvious case where this conflicts with the actual order
of operations that the 6502 does, which is:

(a) Load the LSB of the target address.
(b) Push the MSB of the return address, minus one.
(c) Push the LSB of the return address, minus one.
(d) Load the MSB of the target address.

This can make a difference in a pretty esoteric case, if the JSR target is located,
wholly or in part, inside the stack page (!). This won't happen in normal code
but it can happen in specifically constructed examples.

To deal with this, we load the LSB and MSB of the target address separately, with
the pushing of the return address sandwiched in between, to mimic the order of the
bus operations on a real 6502.
2024-12-19 22:35:15 +01:00
Bob Andrews
c52d7b27e6 Merge pull request #2564 from sidneycadot/fix-cpuregs-linkage
sim65: simulated CPU registers can be accessed from outside 6502.c.
2024-12-19 21:09:37 +01:00
Sidney Cadot
eb8ea0f2c4 Replaced incidental tab by spaces. 2024-12-19 20:57:34 +01:00
sidney
743a3dc735 Changed nameing convention of fields (now CamelCase), and improved comments. 2024-12-19 07:44:01 +01:00
sidney
3cd7548b59 Cosmetic fixes. 2024-12-19 03:50:44 +01:00
sidney
5239d3a11b Polishing the peripherals (and counter) interface. 2024-12-19 03:48:15 +01:00
sidney
8a7cd9c632 Fixed format string. 2024-12-19 03:04:55 +01:00
Sidney Cadot
a3cc9b4757 Fixed mistake in the latching code. 2024-12-18 08:55:30 +01:00
Sidney Cadot
6f9406bbe3 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?
2024-12-17 23:24:35 +01:00
Sidney Cadot
86ccf25e81 CPU registers can be accessed from outside 6502.c.
The linkage of the 'Regs' variable in 6502.c was changed from static
to extern. This makes the Regs type visible (and even alterable) from
the outside.

This change helps tools to inspect the CPU state. In particular, it
was implemented to facilitate a tool that verifies opcode
functionality using the '65x02' testsuite. But the change is also
potentially useful for e.g. an online debugger that wants to inspect
the CPU state while the 6502 is neing simulated.
2024-12-16 17:12:07 +01:00
Sidney Cadot
fb6745573e Fixed whitespace. 2024-12-16 16:55:26 +01:00