## B-em and BeebEm N flag in BCD

discuss bbc micro and electron emulators (including mame) here!
dominicbeesley
Posts: 1075
Joined: Tue Apr 30, 2013 12:16 pm
Contact:

### B-em and BeebEm N flag in BCD

Hello,

I've been playing around with BCD mode and I think I've found an error in the BCD implementation on both BeebEm and B-em (or I'm doing something stupid).

On both emulators a decimal add of &99 to &99 with C set will return &99 as the result with N set (as I would have expected).
However, both the t65 fpga core and Visual6502 both give N unset (in t65 core N is tested before the decimal adjust of the high nibble is carried out).

I haven't got a real 6502 unpacked to test against - please could somebody confirm what happens if they run the program in the screenshot on a real 6502 and a real 65c02.

Thanks

D

BigEd
Posts: 3172
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

### Re: B-em and BeebEm N flag in BCD

(There's a very thorough treatment by Bruce Clark here.)

tricky
Posts: 4427
Joined: Tue Jun 21, 2011 9:25 am
Contact:

### Re: B-em and BeebEm N flag in BCD

On my 'B' (6502) I get P=&79, which I think is N=0 V=1 1=1 B=1 D=1 I=0 Z=0 C=1
I would trust Visual6502, but it looks like the reference below is wrong!

6502.org suggests that N=1 is correct.
However, another common (and correct) interpretation of the N flag is that it contains the value of the high bit (i.e. bit 7) of the result of the instruction. This second interpretation is the meaning of the N flag in decimal mode.
http://www.6502.org/tutorials/decimal_mode.html#3.2.2
EDIT: I may be quoting out of context, as I keep reading that section differently!

BigEd
Posts: 3172
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

### Re: B-em and BeebEm N flag in BCD

Just a little further down the document:
On the 6502, only the C flag is valid. The 65C02 and 65816 datasheets state that the N, V, and Z flags are valid as well as the C flag
It's best to read the whole thing, especially as the final section is an errata section!

tricky
Posts: 4427
Joined: Tue Jun 21, 2011 9:25 am
Contact:

### Re: B-em and BeebEm N flag in BCD

more haste less speed and more errors

hoglet
Posts: 9287
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

### Re: B-em and BeebEm N flag in BCD

Can you post the actual text of your test program, rather than just an image?

What version of B-em are you running?

And are you definitely running on a 6502 (Model B), not a 65C02 (Master) in the emulators?

Dave

hoglet
Posts: 9287
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

### Re: B-em and BeebEm N flag in BCD

Back in April 2017 several BCD bugs were fixed in B-Em, and the latest code look correct to me:
https://github.com/stardot/b-em/blob/ma ... 502.c#L809

There is a Beeb (and Atom) port of Bruce Clark's BCD test suite here:
https://github.com/hoglet67/AtomSoftwar ... ests/clark

Your example: &99 + &99 + 1 should return A=&99 and N=0

On the 6502, N is taken from bit 3 of AH, before AH is adjusted. In this case you have 9 + 9 + 1 = &13 which has bit 3 = 0.

Dave

dominicbeesley
Posts: 1075
Joined: Tue Apr 30, 2013 12:16 pm
Contact:

### Re: B-em and BeebEm N flag in BCD

hoglet wrote:
Wed Dec 04, 2019 6:52 pm
Can you post the actual text of your test program, rather than just an image?
Sorry, I had to run and the screenshot was all I had saved.
hoglet wrote:
Wed Dec 04, 2019 8:12 pm
Back in April 2017 several BCD bugs were fixed in B-Em, and the latest code look correct to me
It looks like I'm out of date then! Where does one get pre-built B-em images for Windows. I've tried in vain many times to build B-em from scratch and not succeeded.
hoglet wrote:
Wed Dec 04, 2019 8:12 pm
There is a Beeb (and Atom) port of Bruce Clark's BCD test suite here
Bugger! I didn't spot your post and I just ported it!

I'll fix BeebEm to match B-em!

Thanks everyone,

D

dominicbeesley
Posts: 1075
Joined: Tue Apr 30, 2013 12:16 pm
Contact:

### Re: B-em and BeebEm N flag in BCD

I've just checked and the B-em I've been using is on v-8f73f4c - that should contain these fixes?

D

dominicbeesley
Posts: 1075
Joined: Tue Apr 30, 2013 12:16 pm
Contact:

### Re: B-em and BeebEm N flag in BCD

I've also tried v-48e1203 which exhibits the same (incorrect BCD) behaviour - it also fails the Bruce Clark tests

D

hoglet
Posts: 9287
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

### Re: B-em and BeebEm N flag in BCD

dominicbeesley wrote:
Wed Dec 04, 2019 8:39 pm
I've just checked and the B-em I've been using is on v-8f73f4c - that should contain these fixes?

D
Yes is should:
https://github.com/stardot/b-em/blob/8f ... 502.c#L905

It's from 25th Sept 2019.

So something wierd is going on here, like it's using the wrong CPU type.

hoglet
Posts: 9287
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

### Re: B-em and BeebEm N flag in BCD

I've just rebuilt the version you are using from commit B-Em 8f73f4c (on Linux).

It passes the test suite:
There aren't many possibilities:
- my port of the test suite is borked (I don't think so, but it's possible)
- you are running B-Em as a Master rather than a Model B
- there's a windows specific bug here (maybe somehow silently changing the CPU type)

Dave

hoglet
Posts: 9287
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

### Re: B-em and BeebEm N flag in BCD

And your test program gives the expected result for a 6502:
Dave

hoglet
Posts: 9287
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

### Re: B-em and BeebEm N flag in BCD

hoglet wrote:
Wed Dec 04, 2019 9:03 pm
There aren't many possibilities:
- you are running on a 6502 Co Pro? (which is always a 65C02)

hoglet
Posts: 9287
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

### Re: B-em and BeebEm N flag in BCD

And it passes on WIndows as well:

dominicbeesley
Posts: 1075
Joined: Tue Apr 30, 2013 12:16 pm
Contact:

### Re: B-em and BeebEm N flag in BCD

Sorry my stupidity knows no bounds when I've got a cold - I was running it as a Master even though I could swear I'd set it as Model B+8271! Sorry for the time wasting. Sorry Dave!

So it looks like it is just BeebEm that gets it wrong in NMOS mode - which I will fix.

There doesn't look to be any sort of test in the t65_alu.vhd file for which CPU mode so I suspect that that will get it wrong in CMOS/816 mode as that does the N,Z tests the NMOS way.

When I was looking at this I found the Arlet's core (that I think you worked on?). In future am I perhaps better using that than t65, it seems to be more up to date and tested - is it better in terms of cycle accuracy?

D

hoglet
Posts: 9287
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

### Re: B-em and BeebEm N flag in BCD

dominicbeesley wrote:
Wed Dec 04, 2019 10:06 pm
There doesn't look to be any sort of test in the t65_alu.vhd file for which CPU mode so I suspect that that will get it wrong in CMOS/816 mode as that does the N,Z tests the NMOS way.
The T65 core is really only 6502 only - the C02 and 816 modes are very incomplete, with lots of missing instructions.
dominicbeesley wrote:
Wed Dec 04, 2019 10:06 pm
When I was looking at this I found the Arlet's core (that I think you worked on?).
Yes, Ed and I did the work to add the 65C02 instructions:
https://github.com/hoglet67/verilog-6502
dominicbeesley wrote:
Wed Dec 04, 2019 10:06 pm
In future am I perhaps better using that than t65, it seems to be more up to date and tested - is it better in terms of cycle accuracy?
The other decent (and small) 65C02 core is Alan D (used to be very active on this forum):
https://github.com/hoglet67/AtomBusMon/ ... /src/AlanD

Both of these pass the Dorman 65C02 test suite and (I think) the BCD Test Suite, but neither is cycle accurate.

I know of only one cycle accurate 65C02 core but it's huge in comparison. It's by Jens Gutschmidt and is called cpu65c02_tc (6502.org link).
I don't know of any project that uses it.

Dave

dominicbeesley
Posts: 1075
Joined: Tue Apr 30, 2013 12:16 pm
Contact:

### Re: B-em and BeebEm N flag in BCD

A realeted question: how good is the mame emulation for the 6502, 65c02, 65816 etc?

I'm looking at hacking it into beebem for the blitter but not sure how cycle accurate and whether it correctly supports illegal instructions.

D