6809 / MC6809

Implementation of the MC6809 CPU in Python (Extracted from https://github.com/jedie/DragonPy project)
GNU General Public License v3.0
32 stars 6 forks source link

Invalid carry flag after DAA #3

Open KarolS opened 4 years ago

KarolS commented 4 years ago

Steps to reproduce:

Assemble and run the following code:

LDA #$91
ADD #$91
DAA

What happened?

Carry was reset.

When similar code was used for multi-byte decimal addition, it yielded incorrect results.

What was expected?

Carry should be set. DAA keeps the carry flag set if it was set beforehand, see the mc6809 emulator sources for comparison: https://github.com/spc476/mc6809/blob/master/mc6809.c#L420 or the official Programming Manual: https://archive.org/details/bitsavers_motorola68_13419254/page/n83/mode/2up

C – Set if a carry is generated or if the carry bit was set before the operation; cleared otherwise.

After some research, I noticed that this is a common bug in many 6809 emulators.

jedie commented 3 years ago

Hm. Interesting.

In the past I have come across incorrect information in the manual. Thus, I would not trust it 100%.

I had tested such behavior on real hardware a few times. I did this in a small BASIC program on my Dragon 32. See Listings here: https://github.com/jedie/DragonPy/tree/master/BASIC%20tools/TestCC_Registers

Would be cool to repeat that for this case. To be sure what the real processor is really doing.

EDIT: We already have some tests for DAA: https://github.com/6809/MC6809/blob/5b8b00b1b4475b8658d64f51d96777442002d79d/MC6809/tests/test_6809_arithmetic.py#L640

A later test, checks also the Carry flag, see: https://github.com/6809/MC6809/blob/5b8b00b1b4475b8658d64f51d96777442002d79d/MC6809/tests/test_6809_arithmetic.py#L688

There was also a discussion about DAA here: http://archive.worldofdragon.org/phpBB3/viewtopic.php?f=8&t=4896

The Carry flag was updated because of the call here: https://github.com/6809/MC6809/blob/5b8b00b1b4475b8658d64f51d96777442002d79d/MC6809/components/mc6809_base.py#L587 and in the end this code part touch it: https://github.com/6809/MC6809/blob/5b8b00b1b4475b8658d64f51d96777442002d79d/MC6809/components/mc6809_cc_register.py#L138 But yes, it only set the Carry flag, but doesn't clear it...

maddes-b commented 6 months ago

There is an inofficial version of the M6809PM document, with additional errata at https://github.com/M6809-Docs/m6809pm Incl. errata for DAA (H for LSN). Currently updating pages incl. upcoming errata for BGT. Please provide input to create a more reliable document. Thanks.