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 > assembly sin gen code request
2011-12-30 12:14
Flavioweb

Registered: Nov 2011
Posts: 463
assembly sin gen code request

i need a code (assembly) to gen sin table. I need to set start, end and step value. Someone could help?
 
... 17 posts hidden. Click here to view all posts....
 
2012-01-11 17:37
Glasnost
Account closed

Registered: Aug 2011
Posts: 26
There is a fast and simple approximation.

Calculate first the hyperbolic approximated sine described earlier (sinH)

Then correct with this:

sin = 0.225 * (sinH * sinH - sinH) + sinH;

0.1% error in worst case compared to the real sinus..

Found it on...
http://lab.polygonal.de/2007/07/18/fast-and-accurate-sinecosine..
...but the link is not working anymore..
2012-01-12 09:17
Skate

Registered: Jul 2003
Posts: 494
Thanks Glasnost, nice article there.

P.S: You can reach the page using the archive.org's link below. (takes a while to load, be patient)

http://web.archive.org/web/20101229221122/http://lab.polygonal...
2012-01-12 16:31
SIDWAVE
Account closed

Registered: Apr 2002
Posts: 2238
i havent really ever seen a C64 sine gen in asm.

i use a basic line we made 25 years ago, then mod it for moves.

another way is to use excell spreadsheet, and generate curves from formulas there, and export to c64.

what i want to say really, is, why dont someone make a c64 tool ? if its so important, then make a c64 generator. ?

:)
2012-01-12 19:47
Cruzer

Registered: Dec 2001
Posts: 1048
SIDWave: Nowadays you can just use an assembler script to generate stuff like that. E.g. KickAss where you have access to all the basic math functions from Java. What this discussion is about is generating it in machine code, in order to avoid having to load it from disk.
2012-01-12 19:50
iAN CooG

Registered: May 2002
Posts: 3195
Jan: are you joking? apart that it's explained on codbase how to do it in asm, there is a plethora of native sinus table generators, I even mentioned one in my 1st post (wix bouncer), and if in doubt just search sinus:
http://csdb.dk/search/?seinsel=releases&search=sinus&all=1
2012-01-13 20:57
Wisdom

Registered: Dec 2001
Posts: 90
Working link for the article Glasnost referred to:

http://lab.polygonal.de/?p=205
2012-01-17 07:05
ChristopherJam

Registered: Aug 2004
Posts: 1409
Might be an interesting challenge to make a 'fewest bytes' sine table generator, criteria "can generate a 256 entry table for floor(128.5+127*sin(i*pi/128.0))", with errors of no more than +-1, at least 170 entries exact.

Of course, it's possible a delta compression scheme might end up winning out over a calculation.. and any entry of more than 256 bytes of code is pretty much an automatic fail!
2012-01-17 08:57
Graham
Account closed

Registered: Dec 2002
Posts: 990
Small sin?

10 FORI=0TO255:POKE4096+I,SIN(I*π/128)*127.5+128:NEXT

Is probably the shortest :)

Other than that a 65 bytes table + small mirror routine will probably be smaller than any real calculation or compression routine:

    LDX #$40
    LDY #$00
lp  LDA sinsrc,X
    STA sin,X
    STA sin+64,Y
    EOR #$FF
    STA sin+128,X
    STA sin+192,Y
    INY
    DEX
    BPL lp

2012-01-17 12:35
Cruzer

Registered: Dec 2001
Posts: 1048
I'm also usually using Graham's mirror trick in bigger productions, even with only a 64 byte table. :)

For 256b'ers I have this extremely inaccurate but only 46 bytes long polynomial pseudo sine generator...

.var sineAmp = $100
.var loStart
.var loAddAdd
.if (sineAmp == $40) {
	.eval loStart = $1c
	.eval loAddAdd = $03
}
.if (sineAmp == $80) {
	.eval loStart = $18
	.eval loAddAdd = $07
}
.if (sineAmp == $100) {
	.eval loStart = $14
	.eval loAddAdd = $0f
}
	lda #$00
	pha
	tax
	ldy #$40
loop:
loAdd:	lda #loStart
	adc #loAddAdd
	sta loAdd+1
	bcc lo
	inc hiAdd+1
	clc
lo:	adc #$00
	sta lo+1
	pla
hiAdd:	adc #$00
	pha
	sta sine,x
	sta sine+$bf,y
	eor #sineAmp-1
	sta sine+$80,x
	sta sine-$80+$bf,y
	inx
	dey
	bne loop


For an amplitude of $100 the max deviation is 7, with an avg of about 2.5. So it doesn't quite comply with Christopher's requirements, but it produces a smooth curve, which is usually good enough for plasmas, DYCP's, etc. For 3D calculations it probably needs to be more accureate though.
2012-01-17 14:15
Skate

Registered: Jul 2003
Posts: 494
I use a very similar routine with Cruzer's. Mine is shorter in a way but there are some initialization rules! :)

this part is taken from my Little Circles of Life product's source code.

        ...
	tSin		= $5f00
        ...
	value		= $3c ; 16 bits
	delta		= $3e ; 16 bits

        ...

	* = $0326
!word	codeStartAddress
codeStartAddress
; fill&clear memory
-	lda #$f6 : setByte = *-1 ; !word $f6a9
	sta ($2b),y
	iny
	bne -
	inc $2c
	ldx $2c
	cpx #$10
	bcc -
	sty setByte
	bpl -

;sinus calculation
	ldx #$40
-	pha
	clc
	adc value
	sta value
	lda tSin+$c0-1,y
	adc delta+1
	sta tSin+$c0,y
	sta tSin+$80-1,x
	eor #$ff
	sta tSin+$00-1,x
	sta tSin+$40,y
	pla
	adc #$10
	bcc +
	inc delta+1
+	iny
	dex
	bne -


very dirty trick at the beginning, i admit it. :) but since this start-up routine fills $1000-$7fff with zero and sinus tables are placed in that area, sinus calculation routine takes only 39 bytes. but of course, if we try to make a stand alone sinus generator, it would take more bytes just like Cruzer's routine.

My point is, you can always use the benefit of your "already required" parts of your effect for shorter sine gen routines. This applies to almost every routine of course, this is just a little example.

For instance, i had to clean up the bitmap area for my effect, anyway. Why not using the same routine for cleaning up the sinus tables area and make an optimization like above without spending any extra bytes?

P.S: This $f6 (or $ff) is a must if you use the $0326 start up trick. I use $f6 to fill the video ram to use as background and foreground colors for my effect and gain 2 more bytes. This is the real "coder colors" case i guess. :)
Previous - 1 | 2 | 3 - 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
Andy/AEG
rikib80
JonEgg
Alakran_64
St0rmfr0nt/Quantum
krissz
daimansion
Martin Piper
Avalanche/Atlantis
algorithm
Guests online: 94
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 Censor Design  (9.3)
5 Triad  (9.3)
Top Coders
1 Axis  (9.8)
2 Graham  (9.8)
3 Lft  (9.8)
4 Crossbow  (9.8)
5 HCL  (9.8)

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