6502 ALU pseudocode

Building your first 6502-based project? We'll help you get started here.
Post Reply
ProfJ
Posts: 2
Joined: 30 May 2019

6502 ALU pseudocode

Post by ProfJ »

Hi Folks,

This is my first post on this forum, hope it's in the right place!

I'm trying to write some pseudocode for the 6502 ALU operation. It seems to be straightforward, except for the BCD addition when DAA is high. This is what I have (the variable names come from the block diagram, and I/ADDC is a name, not a division operation). Apologies, this message editor kills my indenting:

if SUM == 1
if DAA==0
[ACR ADD] = AI + BI + I/ADDC
AVR = (AI(7) && BI(7) && ~ADD(7))||(~AI(7) && ~BI(7) && ADD(7))
else
[HC ADD(3:0)] = AI(3:0) + BI(3:0) + I/ADDC
[ACR ADD(7:4)] = AI(7:4) + BI(7:4)
end
end

I'm mainly concerned with how the carry in, and half-carry and carry out work, can anyone tell me if this is correct?

Thanks
Jon
bdk6
Posts: 20
Joined: 01 Oct 2017

Re: 6502 ALU pseudocode

Post by bdk6 »

You may want to explain your selection of names. I don't think are common/standard. The DAA input(?) seems like it may have come from 8080 land. But it may just be me; I'm not an '02 guru. Nevertheless, this page linked to on the front page of 6502.org may be of some help:
http://6502.org/users/dieter/bcd/bcd_0.htm
It's a description of how the 6502 does BCD.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 ALU pseudocode

Post by BigEd »

Welcome Jon! It's (very) tricky to get BCD right, so I know I can't say anything about the correctness of your approach even by careful code reading. Personally I'd inspect the code of any existing core (or emulator) which is open source and passes a suitable testsuite. Have you tried Bruce's decimal test? Or Klaus' full testsuite?
Ref: http://visual6502.org/wiki/index.php?ti ... stPrograms
User avatar
Dr Jefyll
Posts: 3526
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: 6502 ALU pseudocode

Post by Dr Jefyll »

ProfJ wrote:
this message editor kills my indenting:
Welcome! To preserve formatting, use the editor's Code style.

-- Jeff

Code: Select all

    if SUM == 1
        if DAA==0
		[ACR ADD] = AI + BI + I/ADDC
		AVR = (AI(7) && BI(7) && ~ADD(7))||(~AI(7) && ~BI(7) && ADD(7))
	   else 
		[HC ADD(3:0)] = AI(3:0) + BI(3:0) + I/ADDC
		[ACR ADD(7:4)] = AI(7:4) + BI(7:4)
	   end
   end
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
User avatar
GARTHWILSON
Forum Moderator
Posts: 8774
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: 6502 ALU pseudocode

Post by GARTHWILSON »

Dr Jefyll wrote:
To preserve formatting, use the editor's Code style.
Yes, put your code between the tags [code] and [/code]. (I did something else here to keep them from taking effect.)
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
ProfJ
Posts: 2
Joined: 30 May 2019

Re: 6502 ALU pseudocode

Post by ProfJ »

bdk6 wrote:
You may want to explain your selection of names. I don't think are common/standard. The DAA input(?) seems like it may have come from 8080 land. But it may just be me; I'm not an '02 guru. Nevertheless, this page linked to on the front page of 6502.org may be of some help:
http://6502.org/users/dieter/bcd/bcd_0.htm
It's a description of how the 6502 does BCD.
Thanks for that link. The signal names are shown in the standard 6502 block diagram, see for example here http://www.weihenstephan.org/~michaste/ ... 2/6502.jpg .
bdk6
Posts: 20
Joined: 01 Oct 2017

Re: 6502 ALU pseudocode

Post by bdk6 »

Quote:
The signal names are shown in the standard 6502 block diagram
Yeah, sorry about that. I was preoccupied when I first read your post and only got about half the information. I do think it would be useful to have put a link to that block diagram in the post for morons like me. I realized right after I commented that you mentioned it. I was gonna be quiet and hope nobody noticed :oops:
Post Reply