Computing sine and cosine using CORDIC and BCD

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
lightbeing
Posts: 11
Joined: 15 Oct 2023
Location: Paris (France)

Computing sine and cosine using CORDIC and BCD

Post by lightbeing »

Hello there,

I had this idea to implement a very simple 6502 routine to compute step by step in BCD mode, the sine and cosine of a given angle using the CORDIC method and the common formulas Cos(A+B) and Sin(A+B). Values for the angles B would be chosen in such way that Tan(B)=1, 0.5, 0.25, 0.125 etc.

Angles would be expressed in degrees with at least three "decimal" bytes stored in memory in the big endian order.

The starting angle would be 0 degrees. Cos(0)=1 and Sin(0)=0, but they would be multiplied with the constant 0,60725293500899 simplified in the routine with three bytes 60, 72 and 53.

My question might look a little bit silly but I assume that something similar has already been implemented in the past. Would anyone know where I could find a routine that performs this ? I guess I will save some time and cells to my brain :D
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Computing sine and cosine using CORDIC and BCD

Post by BigEd »

Welcome! As I understand it, CORDIC is a good fit for BCD calculations (and the 6502 has the handy BCD mode.)

Did you try searching the forum for previous discussions? I found this
[Contest] 6507 Calculator
Calculating log and trig - algorithms or tables
16 Bit Fixed Point Trig
Calc65: a BCD floating point package

Also, in the knowledgebase nearby on this site, we see CORDIC mentioned in
Source Code Repository
(in this case, points to a thread I've already listed above, but worth mentioning the reference section I think.)
User avatar
lightbeing
Posts: 11
Joined: 15 Oct 2023
Location: Paris (France)

Re: Computing sine and cosine using CORDIC and BCD

Post by lightbeing »

BigEd wrote:
Welcome!
Thank you! :)
Quote:
Also, in the knowledgebase nearby on this site, we see CORDIC mentioned in
Source Code Repository
(in this case, points to a thread I've already listed above, but worth mentioning the reference section I think.)
Many thanks for the suggestions.

I understand that the CORDIC reference is related to the calc65 package which is the closest thing to what I'm looking for (I now have this U2's song title ringing as an echo...). But the calc65 package is highly complex as it includes many other types of operations. Further it's a little bit different from my idea. It uses the formulas sin(x) = tan(x)/sec(x) and cos(x) = 1/sec(x). Also it seems to use an 8-byte floating point format BCD mantissa for the values. My idea was to use three BCD bytes instead of two hexadecimal bytes for the values added or subtracted. The results won't be as accurate as with the calc65 package but it will be really easy to read for a beginner in maths and programming.

If it does not exist, I will have to write it.
Post Reply