Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > How easy is it to calculate sine tables from the bare bones in ML?
2008-08-05 09:52
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....
 
2008-08-06 08:27
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
2008-08-06 08:40
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 ;)
2008-08-06 09:27
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.
2008-08-06 11:19
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...)
2008-08-06 12:34
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.
2008-08-07 22:42
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
2013-12-17 23:03
Cruzer

Registered: Dec 2001
Posts: 1048
Quoting doynax
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.
Do want! :)
2013-12-19 20:25
doynax
Account closed

Registered: Oct 2004
Posts: 212
Quoting Cruzer
Do 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.
2013-12-20 22:29
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
2013-12-20 23:42
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
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
Epyx/TSA
Stone/Prosonix/Offence
Case/Padua
MCM/ONSLAUGHT
Andy/AEG
rikib80
Chesser/Blazon
Guests online: 108
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Webmasters
1 Slaygon  (9.6)
2 Perff  (9.6)
3 Sabbi  (9.5)
4 Morpheus  (9.4)
5 CreaMD  (9.1)

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