Page 1 of 1

6502 ALU pseudocode

Posted: Thu May 30, 2019 6:27 pm
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

Re: 6502 ALU pseudocode

Posted: Thu May 30, 2019 7:02 pm
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.

Re: 6502 ALU pseudocode

Posted: Thu May 30, 2019 7:22 pm
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

Re: 6502 ALU pseudocode

Posted: Thu May 30, 2019 7:28 pm
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

Re: 6502 ALU pseudocode

Posted: Thu May 30, 2019 7:51 pm
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.)

Re: 6502 ALU pseudocode

Posted: Thu May 30, 2019 9:35 pm
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 .

Re: 6502 ALU pseudocode

Posted: Thu May 30, 2019 10:23 pm
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: