| |
Conrad
Registered: Nov 2006 Posts: 849 |
How easy is it to calculate sine tables from the bare bones in ML?
Hi!
I'd like to have a go at coding my own routine of calculating sine/cosine tables without use of the BASIC interpreters (pi, sin(), etc) and just use pure ML instead.
Can I ask, how easy (or how SMALL) is this to do? Do I need to do additional algorithms like factorial or are there KERNAL routines for that (if any)? Multiplication code is no problem as I've seen articles on those, I'm just thinking about the rest of math involved with sine calculation.
Thanx in advance. |
|
... 22 posts hidden. Click here to view all posts.... |
| |
doynax Account closed
Registered: Oct 2004 Posts: 212 |
Quote: Nice, much smaller than my faked routine. But how fast is it compared to Basic?
The assembler version takes 5.89 seconds ($584f0f cycles) while a BASIC implementation needs about 17.2 seconds according to my stopwatch.
Then again BASIC isn't exactly my forte, so perhaps I made some stupid newbie mistake?0 fori=0to255:poke1024+i,sin(i*.02454)*128and255:next |
| |
Skate
Registered: Jul 2003 Posts: 494 |
I usually use something like;
0 FORI=0TO255:POKE1024+I,128+128*SIN(I/40.7):NEXT
where 40.7 is 256/(2*PI)
don't forget, we need less bytes for small intros and /40.7 is shorter ;) |
| |
Cruzer
Registered: Dec 2001 Posts: 1048 |
5.89 is definitely fast enough for a tinytro, but I think I'll stick to generating them with a KickAss script for normal cases.
|
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
@Cruzer: I agree and it actually SAVES memory in the end because you will not have the generator in memory, only the end result. (And buhu what I miss trig-operations and floatingpoints in ca65...) |
| |
doynax Account closed
Registered: Oct 2004 Posts: 212 |
Quote: @Cruzer: I agree and it actually SAVES memory in the end because you will not have the generator in memory, only the end result. (And buhu what I miss trig-operations and floatingpoints in ca65...)
I'd say if you're not kicking out your initialization code then a sine generator is the least of your worries (drivecode anyone?).
Personally I prefer to mirror the first 64 entries to create the rest of the table, which saves 170 bytes or so and loads a tad faster. Hey.. things like this add up, especially for game code.
Oh, and I saved another byte in the generator (down to 28) at the cost of creating a negated sine table instead, though I seriously doubt that matters for 256-byte intro use. |
| |
Monte Carlos
Registered: Jun 2004 Posts: 359 |
To avoid that the sinmax and sinmin is reached only for one byte, use a+(a-0.5)*sin(pi/180*i) instead of
a*(1+sin(pi/180*i)).
Monti Carlotti
|
| |
Cruzer
Registered: Dec 2001 Posts: 1048 |
Quoting doynaxOh, and I saved another byte in the generator (down to 28) at the cost of creating a negated sine table instead, though I seriously doubt that matters for 256-byte intro use. Do want! :) |
| |
doynax Account closed
Registered: Oct 2004 Posts: 212 |
Quoting CruzerDo want! :) It took a bit of filesystem spelunking but here you go: http://pastebin.com/dMFP3F47
Now be sure to buy yourself something nice with that extra byte.
Sidenote: I must confess to some surprise at finding no less than three 6502 assembly dialects to choose from at pastebin. It's almost enough to get an embittered atheist into the Christmas spirit. |
| |
ChristopherJam
Registered: Aug 2004 Posts: 1409 |
An alternate approach is to fill a table with a square wave, then filter it with a few smoothing passes to eliminate the harmonics. The following hasn't been optimised for size, but it's only 38 bytes, and only takes about 170,000 cycles to create a table of 127.5+63*cosine(i*pi/128) with an RMS error of under 1%
(warning, also trashes 52 bytes after the end of the table. Number of passes, and initial square wave phase and amplitude optimised by brute force search with a Python script. Output ranges from $40 to $bf).
ldx#127
prefill
sta cosine,x
lda#19
sta cosine+52,x
lda#250
sta cosine+128+52,x
dex
bpl prefill
ldy#30
clc
smoothloop
lda cosine,x
adc cosine,y
ror
sta cosine,x
iny
inx
bne smoothloop
dey
bne smoothloop
|
| |
Cruzer
Registered: Dec 2001 Posts: 1048 |
Thanx, Doynax! The sine is too different to be used in my current project, and since I'm already under 256 bytes I'm good for now. But will keep it for another time. |
Previous - 1 | 2 | 3 | 4 - Next |