Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user Werner ! (Registered 2024-05-30) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > sin/cos table generator
2012-11-17 18:59
Rudi
Account closed

Registered: May 2010
Posts: 125
sin/cos table generator

this is my sine table generator:
for(;x<256;){a+=b;b+=c;*s++=0xf-((a+t)>>3);x++==127?c=-c:0;}
not implemented on c64 yet.

it is harmonic oscillator based on simple physics.
a and b are initial seeds.

this one will work on 8-bit with a little offset-tweaking,
between the region[0..59] where x+=95 and y+=22.
the drawback is its amplitude will be something like -16 -> 16.
tweaking might be required. or an additional byte for bigger values.

i found some interesting areas while running through all possible states for b and c regions (total 65536 states) but none of them where perfect.

the table (after offsetting) looks something like this:
9,11,13,15,16,18,19,21,22,23,24,25,26,26,27,27,28,28,28,28,27,27,26,26,25,24,23, 22,21,19,18,16,15,13,11,9,8,6,5,4,3,2,1,1,0,0,0,0,0,0,0,0,1,1,2,3,4,5,6

i want to fit the code as tiny as possible
the code would probably more or less look something like this;

in loop:
adc b
pha
lda b
adc c
pha
lda #$f
sbc (a+t) ror,3
inx
cpx #127
beq chg_sign
sta [table]

of course this would not be usable for a demo.
but intros in 256b size or less, i hope it might bring some ideas if anyone is interested.

im not sure if my code is very useable but just too give you an idea. if anyone know any other/better tricks please let me know. im kind of tired, i wish there could be another byte to increase the sine-amplitude resolution.
2012-11-26 09:09
Karoshier
Account closed

Registered: May 2010
Posts: 15
There's a quick and dirty method explained on Codebase that uses the basic rom to do the trick. The results need a little adjustment if you are going to use the table for trigonometrics, but it's good enough for a demo effect.

http://www.codebase64.org/doku.php?id=base:generating_sines_wit..

Taken as it is, it takes only 33 bytes of code/data. If you need it for trigonometrics then you need some more space in order to use properly crafted data.

That said, with regard to your code, I can't see how it works. I tried quickly compiling it but can't get good results. Could you state which values you feed in as a, b, c and t to get acceptable results?

Cheers
2012-11-26 13:26
Rudi
Account closed

Registered: May 2010
Posts: 125
Karoshier:
as i said, ive not tested it on c64. so i cant say if it will be good enough. if it will look jerky etc. should use 16-bits values somewhere to get it better (and then if one use 16-bits the initial values would go all wrong).

the values one should feed into the variables are:
x=0
a=0
b=$71
c=$ff

so you when looking up the values from the table one needs to have the right offsets too:

loop x=0 to 59 {
amp = (sine[(x+95)&0xff]+22)&0x1f;
pixel[x+(100 + amp)*320] = color;
}

the sine length and amplitude is very tiny (0->59 and 0->0x1f) as said because of the 8-bit resolution. the basic one will of course win. i was going to look into it before, but thanks for posting the link! you saved me alot of time.

ah i forgot to say. t can be removed. its initially set to zero, and not used. sorry for that. that reduces the code size a tiny tiny bit :P
2012-11-26 14:13
Oswald

Registered: Apr 2002
Posts: 5029
Formula Too Simple [256 byte]

I remember shortly after this a few more 256 byte dycps were released some with assembly sin generators. am too lazy now but you can find them :)
2012-11-26 17:51
Skate

Registered: Jul 2003
Posts: 491
Most of you already know that but i'd like to remind again. When it comes to 256 bytes, there is no "best/shortest" sine table generator routine. It depends on the effect and the "other parts" of the code. I usually merge some other stuff with sine calculation. So, when you check sine calculation block, it can be over 40 bytes but doing more than just sine calculation. Of course base method is important but even if your routine is under 30 bytes, sometimes some other 40+ bytes routine might result a smaller sized effect. That happened to me at least twice. So, don't forget to check all known methods with your 256b effect or it may never reach 256b limit. ;)
2012-11-26 18:09
Burglar

Registered: Dec 2004
Posts: 1051
for a pseudo sine, check You Can't Process This REM Statement with a Normal Brain by cruzer
2012-11-30 10:31
Perplex

Registered: Feb 2009
Posts: 254
Quoting Burglar
for a pseudo sine, check You Can't Process This REM Statement with a Normal Brain by cruzer


Indeed, a parabola is pretty close to a sine curve, and suitable for many demo effects.

http://6581.no/sine_vs_parabola.png
2012-11-30 11:22
Oswald

Registered: Apr 2002
Posts: 5029
well, nobody cares if its a sine or just some nice curve anyway :) except.. I'm wondering how a sine table made of parabolas would affect a 3d rotation. :)
2012-11-30 11:49
enthusi

Registered: May 2004
Posts: 675
Your vectors would just wobble and vary in length between rotation steps.
2012-11-30 11:52
chatGPZ

Registered: Dec 2001
Posts: 11148
so it wouldnt be a big difference for most routines =)
2012-11-30 15:13
Perplex

Registered: Feb 2009
Posts: 254
Quoting Oswald
well, nobody cares if its a sine or just some nice curve anyway :) except.. I'm wondering how a sine table made of parabolas would affect a 3d rotation. :)


http://6581.no/plotball-sine.html

http://6581.no/plotball-parabola.html

Conclusion: don't use parabolas for rotations.
 
... 3 posts hidden. Click here to view all posts....
 
Previous - 1 | 2 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
kbs/Pht/Lxt
t0m3000/HF^BOOM!^IBX
Menace/Spaceballs
zscs
Nordischsound/Hokuto..
Guests online: 105
Top Demos
1 Next Level  (9.8)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.7)
5 Edge of Disgrace  (9.7)
6 Aliens in Wonderland  (9.6)
7 No Bounds  (9.6)
8 Comaland 100%  (9.6)
9 Uncensored  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 Happy Birthday Dr.J  (9.7)
2 Layers  (9.6)
3 It's More Fun to Com..  (9.6)
4 Cubic Dream  (9.6)
5 Party Elk 2  (9.6)
6 Copper Booze  (9.6)
7 TRSAC, Gabber & Pebe..  (9.5)
8 Rainbow Connection  (9.5)
9 Dawnfall V1.1  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Nostalgia  (9.4)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 SHAPE  (9.3)
Top Organizers
1 Burglar  (9.9)
2 Sixx  (9.8)
3 hedning  (9.7)
4 Irata  (9.7)
5 Tim  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.043 sec.