1
0
mirror of https://github.com/cc65/cc65.git synced 2025-04-10 02:39:26 +00:00

3929 Commits

Author SHA1 Message Date
Sidney Cadot
fb6745573e Fixed whitespace. 2024-12-16 16:55:26 +01:00
Sidney Cadot
dfc88d5796
Merge branch 'cc65:master' into fix-adc-sbc 2024-12-16 16:46:26 +01:00
Sidney Cadot
eda8774e08 Fixed ADC/SBC for the 65C02.
The current (before-this-patch) version of sim65.c does not correctly implement
the ADC and SBC instructions in 65C02 mode. This PR fixes that.

The 6502 and 65C02 behave identically in binary mode; in decimal behavior
however they diverge, both in the handling of inputs that are not BCD values,
and in the handling of processor flags.

This fix restructures the original "ADC" and "SBC" macros in versions that
are specific for the 6502 and the 65C02, and updates the opcode tables to
ensure that they point to the correct implementations.

Considering the ADC instruction for a moment, the original "ADC" macro was
changed to two macros ADC_6502 and ADC_65C02. These check the D (decimal
mode) bit, and defer their implementation to any of three macros ADC_BINARY_MODE,
ADC_DECIMAL_MODE_6502, and ADC_DECIMAL_MODE_65C02. This is a bit verbose but it
makes it very clear what's going on.

(For the SBC changes, the analogous changes were made.)

The correctness of the changes made is ensured as follows:

First, an in-depth study was made how ADC and SBC work, both in the original
6502 and the later 65C02 processor. The actual behavior of both processors
was captured on hardware (an Atari 800 XL with a 6502 and a Neo6502 equipped
with a WDC 65C02 processor), and was analyzed. The results were cross-referenced
with internet sources, leading to a C implementation that reproduces the exact
result of the hardware processors. See:

https://github.com/sidneycadot/6502-test/blob/main/functional_test/adc_sbc/c_and_python_implementations/6502_adc_sbc.c

Next, these C implementations of ADC and SBC were fitted into sim65's macro-
based implementation scheme, replacing the existing 6502-only implementation.

Finally, the new sim65 implementation was tested against the 65x02 testsuite,
showing that (1) the 6502 implementation was still correct; and (2) that
the 65C02 implementation is now also correct.

As an added bonus, this new implementation of ADC/SBC no longer relies on a
dirty implementation detail in sim65: the previous implementation relied on
the fact that currently, the A register in the simulator is implemented as
an "unsigned", with more bits than the actual A register (8 bits). In the
future we want to change the register width to 8 bits, and this updated
ADC/SBC is a necessary precursor to that change.
2024-12-16 16:36:23 +01:00
Bob Andrews
11699a4124
Merge pull request #2557 from sidneycadot/fix-sim65-rol-ror-ops
sim65 : improve implementation of ROL and ROR operations
2024-12-15 22:59:39 +01:00
Sidney Cadot
05b3825683 sim65 : improve implementation of ROL and ROR operations
Issue #2539 brings to light a number of issues in the sim65 simulator.

Several issues can be traced back to undesirable side effects of the
use of bare 'unsigned' types for the CPU registers in the 'CPURegs'
type defined in src/sim65/6502.h.

The intention is to tighten the types of the registers defined there
to uint8_t and uint16_t, in accordance with the actual number of bits
that those registers have in the 6502. However, it turns out that a
handful of opcode implementations depend on the fact that the register
types currently have more bits than the actual 6502 registers themselves
for correct operation. This mostly involves operations that involve
the carry bit (ROL, ROR, ADC, SBC).

In preparation of fixing the CPURegs field types, we will first make
sure that those opcode implementations are changed in such a way that
they still work if the underlying register types are tightened to their
actual bit width.

This PR concerns this specific change for the ROL and ROR operations.

The correct functioning of ROL and ROR after this patch has been verified
by testing against the 65x02 test suite.
2024-12-03 23:33:57 +01:00
Sidney Cadot
fbd8961be1 sim65: changing memory access types to uint8_t and uint16_t.
In sim65, simulator memory access to a 64 KB array is implemented via functions
defined in src/sim65/memory.h and src/sim65/memory.c.

In the old version, the types for both content bytes (8 bits), content words
(16 bits), regular addresses (16 bits), and zero-page addresses (8 bits) were
all given as bare 'unsigned'.

This lead to several cases of address overrun (e.g., when an instruction wraps
around from address 0xffff to 0x0000) when running the simulator against a
stress test (specifically, the 65x02 test suite). To protect from this, and to
more properly express the bit width of the types involved which is a good idea
anyway, we start using the fixed-width types provided by 'stdint.h'.

In the process, we also change the MemReadByte macro to a full function call.
This may impact performance (by a small amount), but it improves memory safety,
as cases where the address is accidentally expressed as a value exceeding 0xffff
are handled by wrap-around (as it is in the actual hardware), rather than causing
access outside of the Mem[] array where the 64 KB of simulated RAM resides.

The reason for this patch is twofold.

(1) It is a partial patch for issue #2539.

Several issues brought to the surface by running the 65x02 testsuite are
eliminated by these changes. In the discussion about this issue, it was
concluded that it is a Good Idea to use the stdint-types, both for the
simulated CPU registers and for the memory. This patch addresses the
memory-part of that change.

(2) It is a precursor patch for issue #2355.

For that issue, we will implement a memory-mapped timer register. This will
make handling of memory access in the simulator a bit more complex.

Having proper functions with the proper types in place will help to make the
timer register patch easier.
2024-12-03 21:21:49 +01:00
Sidney Cadot
84c4ea062d Sim65: removed ZR register from CPURegs type. 2024-12-03 01:17:44 +01:00
Bob Andrews
c0a4942b5d
Merge pull request #2550 from sidneycadot/fix-bit-imm
Fixed behavior of the 65C02 "BIT #imm" instruction in sim65.
2024-12-02 00:28:07 +01:00
Bob Andrews
918c39cbeb
Merge pull request #2548 from sidneycadot/fix-branch-timings
Fixed clock-cycle timing of branch (Bxx) instructions.
2024-12-02 00:25:54 +01:00
Bob Andrews
3895caae90
Style fix 2024-12-02 00:25:24 +01:00
Bob Andrews
79e26c1bc5
Merge pull request #2547 from sidneycadot/fix-jmp-ind
Fixed the behavior of JMP (ind) in sim65 when it runs with the 6502X CPU type.
2024-12-02 00:21:31 +01:00
Sidney Cadot
e26c17fd50 Fixed wrong clearing of D-flag on interrupts for sim65 with 6502X CPU type.
The 65C02 clears the D flag on interrupts while the 6502 does not.

The old code cleared the D flag also for the 6502X CPU type, which
was incorrect.
2024-12-01 09:59:10 +01:00
Sidney Cadot
1d9d056da5 Fixed behavior of the 65C02 "BIT #imm" instruction.
The BIT #imm instruction behaves differently from the BIT instruction with other
addressing modes, in that it does /not/ set the N and V flags according to the
value of its operand. It only sets the Z flag, in accordance to the value of
(A & operand).

This is corroborated in two ways:

- The 65x02 test suite;
- Documentation about BIT #imm such as http://www.6502.org/tutorials/65c02opcodes.html

This patch implements the correct behavior for BIT with immediate addressing.
The patched version passes the 65x02 test suite for 65C02 opcode 0x89.
2024-11-30 23:46:19 +01:00
Sidney Cadot
709d71ef70 Fixed clock-cycle timing of branch (Bxx) instructions.
Branch instructions, when taken, take three or four cycles,
depending on whether a page is crossed by the branch.

The proper check to determine whether the extra cycle must be added
is the target address of the branch vs the address immediately
following the branch.

In the former version of the BRANCH instruction handler, the target
address was incorrectly checked vs the address of the branch instruction
itself.

The corrected behavior was verified against a real 6502 (Atari) and
the 65x02 testsuite.
2024-11-30 19:56:31 +01:00
Sidney Cadot
2abd66ea0c Fixed the behavior of the JMP (ind) instruction in sim65 when it runs with the "6502X" CPU type.
The JMP (ind) bug is present in the 6502 which is emulated by both the "6502" and "6502X"
emulation targets of sim65; specifically, the OPC_6502_6C handler. In the old code, the
bug-exhibiting code was not executed when the target was set to 6502X, which is incorrect.
the patch removes the (CPU == CPU_6502) check, which made no sense.

The JMP (ind) bug was actually fixed in the 65c02. Indeed, the OPC_65C02_6C opcode handler
has code that implements the 'right' behavior.
2024-11-30 12:36:35 +01:00
Bob Andrews
05a653d3f9
Merge pull request #2538 from clydeshaffer/dbg_banknum
[LD65] Add bank number to `seg` entries in dbgfile
2024-11-26 02:27:29 +01:00
Clyde Shaffer
90e4360958 Parse and report segment bank number in dbginfo module and test shell 2024-11-16 17:13:04 -05:00
Clyde Shaffer
fa80e171a2 [LD65] Add bank number to dbgfile 2024-11-12 01:57:27 -05:00
Bob Andrews
4dfbccfafd
Merge pull request #2522 from kugelfuhr/kugelfuhr/code-optimizations
Improve generated code for AND/EOR/ORA
2024-10-05 15:03:46 +02:00
Bob Andrews
270aa4417b
Merge pull request #2524 from kugelfuhr/kugelfuhr/fix-2523
Fix some issues with preprocessor expressions
2024-10-05 14:14:29 +02:00
Kugel Fuhr
9c69aac097 Fix some issues with signedness in preprocessor expressions. Do also disallow
comma expressions since the aren't compliant and collide with macro invocations.
2024-09-17 11:45:46 +02:00
Kugel Fuhr
175ec65af1 Fix #2520. 2024-09-14 21:12:19 +02:00
Kugel Fuhr
f43cfd1ad0 Fix the check for CPU flags being used after an instruction that gets removed.
Previously only the next instruction was checked for usage of the CPU flags
but this fails for certain code.
2024-09-13 19:30:38 +02:00
Kugel Fuhr
6e18e0880a Added/improved the optimizations:
* Added a new pass that optimizes PHA/PLA sequences
* Added a new pass that optimizes AND/EOR/ORA when an operand is known
* Added a run of an existing pass at later stages to remove code that
  otherwise goes unchanged.
* Handle binary operations in OptUnusedLoads in addition to real loads.
2024-09-13 19:30:38 +02:00
Bob Andrews
7260c10062
Merge pull request #2518 from kugelfuhr/kugelfuhr/fix-2515
Fix minor preprocessor problems
2024-09-13 17:53:20 +02:00
Bob Andrews
b5135b3ae0
Merge pull request #2502 from kugelfuhr/kugelfuhr/fix-2461
Fix issue #2461
2024-09-13 15:53:49 +02:00
Kugel Fuhr
70ca6d4200 Fixed a standard noncompliance: In C99 and above there must be whitespace
between a name of an object like macro and its replacement list.
2024-09-11 19:21:19 +02:00
Kugel Fuhr
2f6f5f0da1 Fix problem with #line when there is no whitespace between line number and
filename.
y
2024-09-11 19:20:01 +02:00
Bob Andrews
38038fd0d3
Merge pull request #2512 from kugelfuhr/kugelfuhr/fix-2134
Warn for braces around a pointer initializer
2024-09-08 16:05:58 +02:00
Kugel Fuhr
3c5269dede Warn for braces around a pointer initializer. 2024-09-08 09:11:47 +02:00
Bob Andrews
4e2a3bde92
Merge pull request #2499 from kugelfuhr/kugelfuhr/disable-recursive-calls-to-main
Disallow recursive calls to main() in cc65 mode
2024-09-07 14:39:28 +02:00
Kugel Fuhr
d996e20c5f Fix issues #2461. This was always wrong even in cases where it seemed to work.
If it did, it was by coincidence.
2024-09-03 20:21:48 +02:00
Kugel Fuhr
cd4357057f The change from #2495 didn't take into account that recursive calls to main()
are legal in C. With the changes from #2495, such calls will usually crash the
machine. But recursive calls to main() are rare and on the 6502 every byte
saved is precious. So this change limits the effect of #2495 to cc65 mode and
at the same time disallows recursive calls to main() in this mode. If
recursive calls to main() are actually required, the code must be compiled in
c89 or c99 mode.
2024-09-02 10:39:42 +02:00
Kugel Fuhr
e2014611ef Improve the usage output for the '-W' option. 2024-09-02 07:02:41 +02:00
Bob Andrews
5e5dd1d6c4
Merge pull request #2498 from kugelfuhr/kugelfuhr/fix-include-in-macros
Fix .include within .macro/.repeat
2024-09-01 23:26:33 +02:00
Bob Andrews
2680bc8dec
Merge pull request #2495 from kugelfuhr/kugelfuhr/dont-save-stuff-in-main
Generate shorter code for main() in some cases
2024-09-01 22:54:45 +02:00
Kugel Fuhr
b2aceaea24 Fix behavior of .INCLUDE within a macro or .REPEAT. In the original code
.INCLUDE was executed after expansion of the macro or .REPEAT - which was
wrong and caused all sorts of unexpected behavior. Related issues/PRs
are #231, #1473, #2159 and maybe others.

Note: After this change error messages for nested macro/.include statements
may be wrong. This is an unrelated bug that was always there and got exposed
by this fix. The bug needs to be addressed in a separate PR.
2024-09-01 19:58:07 +02:00
Bob Andrews
7a578be724
Merge pull request #2491 from kugelfuhr/kugelfuhr/fix-2431
Fix issue #2431
2024-09-01 19:35:32 +02:00
Bob Andrews
a712fbb8f9
Merge pull request #2493 from kugelfuhr/kugelfuhr/fix-1663
Fix issue #1663.
2024-09-01 18:07:14 +02:00
Bob Andrews
601deab3a2
Merge pull request #2492 from kugelfuhr/kugelfuhr/alternative-pragma-names
Allow alternative names for pragmas that contain underlines instead of dashes
2024-09-01 17:57:38 +02:00
Bob Andrews
4b38974918
Merge pull request #2496 from kugelfuhr/kugelfuhr/fix-2458
Fix macro preprocessing for #include
2024-09-01 17:54:36 +02:00
Kugel Fuhr
b4aef6eac4 Fix macro preprocessing for #include. Arguments enclosed in "" or <> must not
be preprocessed. See ISO/IEC 9899 1990 (E) section 6.8.2.
2024-09-01 13:16:35 +02:00
Kugel Fuhr
b5cc68d6e2 Do not save any register variables when entering main(). Do not restore the C
stack when leaving main(). Both are unnecessary and just bloat the executable.
2024-09-01 12:41:25 +02:00
Kugel Fuhr
4b68d19993 Fix issue #1663. 2024-09-01 10:42:18 +02:00
Kugel Fuhr
35c3fe5d0a Fix issue #2044. While doing so, cleanup copy&pasted code. 2024-09-01 10:29:59 +02:00
Kugel Fuhr
ef17250c64 Fixed a compiler warning. 2024-09-01 10:26:45 +02:00
Kugel Fuhr
ba263d13a7 Allow alternative #pragma names using underscores. 2024-09-01 10:22:57 +02:00
Kugel Fuhr
58b1c21996 Removed #pragma names that have been obsolete for over a decade. 2024-09-01 10:22:40 +02:00
Evgeny Vrublevsky
c500cb9086 Add support of unnamed labels with @ (.localchar) prefix. 2024-04-07 13:34:48 +03:00
Bob Andrews
25967e65b5
Merge pull request #2424 from acqn/Cleanup
[cc65] Cleanups in src/cc65/declare.c
2024-03-10 02:39:14 +01:00