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: 447
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-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: 490
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. :)
2012-01-18 08:57
Graham
Account closed

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

Yes, if you add an offset of 0.5 to the sinus index (0.5 to 255.5 instead of 0 to 255), you can use a 64 byte table. This works nice for plasmas, sinus movements etc. However for "real maths" like 3D rotation or similar, it's better to use a sinus wave without that 0.5 index.
2012-01-18 20:54
Wisdom

Registered: Dec 2001
Posts: 90
Quoting ChristopherJam
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!


In Tied, there is a simple delta encoded sinus table, consisting of only 8 bytes, but the decoder itself is around 38 bytes (might change due to initialization in a different perspective). Extends a full sinus for 00-3F values to a 256 bytes table.

There is also a second one, which has a more complex shape, consisting of a 32 bytes table and a separate 40 bytes decoder (again, might be different a byte or two in a different initialization scenario). Again, extends to a 256 bytes table with values between 00-3F.
2012-01-19 12:45
Adam

Registered: Jul 2009
Posts: 321
Quoting SIDwave
... what i want to say really, is, why dont someone make a c64 tool ? if its so important, then make a c64 generator? :)


Hi Jan, Here is a tool that will allow you to generate sine waves using MS-Windows:

Sinus Creator V3 by 'The Gang'
http://www.thegang.nu/get_data.php?type=attachment&id=160

The website says version 2.x but the program in the zip archive is version 3. This program is very useful ;)
Output: Binary, Assembler, BASIC, C, C Source and WAV Audio samples =D

2012-01-19 13:43
ChristopherJam

Registered: Aug 2004
Posts: 1378
This routine has an accuracy ±3 (should be able to improve that by tweaking the table a little), but relies on interrupts being clear, character DMA disabled, and voice 3 of SID being fully decayed with a release rate of zero! 58 bytes including table, piecewise linear using env3 to select one of 13 segments:

sin=$4000
v3ctrl = $d40e+4
v3AD   = $d40e+5
v3SR   = $d40e+6
env3   = $d41c

initSin
	ldx#$50
	stx v3AD
	inc v3ctrl
	lda#127
	clc
lp 
ly  ldy#63
  	eor#$ff
	sta sin-$50,x
	sta sin+64,y
	eor#$ff
	sta sin+128-$50,x
	sta sin+192,y           ;26 cycles

	rol  ; note this will clear carry
	ldy env3  ; increases by 1 every 208 cycles for attack=5,
	sbc dt,y
  	lsr
	inx
	dec ly+1
  	bpl lp            ; 22+26 =48 for entire loop

	rts
dt
	.byt 5,5,5,5,5, 4,4,3,3,2, 2,1,0,255


CIA would probably be a better bet than SID, but I'm not very familiar with it, and it's less of an evil hack :)
2012-01-19 19:10
Cruzer

Registered: Dec 2001
Posts: 1048
Generating sines with the SID, now that's what I call a hack! Is it based on sine-like waveforms, or is the SID just used as a counter as the comments imply?
2012-01-19 19:27
chatGPZ

Registered: Dec 2001
Posts: 11108
the envelope generator uses some sort of spline approximation, so its linear steps and the slope changes at a few points.
2012-01-19 20:38
ChristopherJam

Registered: Aug 2004
Posts: 1378
Much simpler than all that; I just needed a value that increased by one every four or five iterations through the loop. The sin table generated is a linear approximation calculated in 8.1 fixed point, with a new slope value for each envelope increment.

Envelope generation is itself piecewise linear in the decay and release phases, but attack (as used here) just increases at a constant rate.
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
Naufr4g0
Smasher/F4CG
Didi/Laxity
Dymo/G★P
Strepto/Lethargy
Thunder.Bird/HF/MYD!..
Guests online: 133
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 The Ghost  (9.6)
9 Wonderland XIV  (9.6)
10 Bromance  (9.6)
Top onefile Demos
1 It's More Fun to Com..  (9.8)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 Rainbow Connection  (9.5)
6 TRSAC, Gabber & Pebe..  (9.5)
7 Onscreen 5k  (9.5)
8 Wafer Demo  (9.5)
9 Dawnfall V1.1  (9.5)
10 Quadrants  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Fullscreen Graphicians
1 Carrion  (9.8)
2 Joe  (9.8)
3 Duce  (9.8)
4 Mirage  (9.7)
5 Facet  (9.7)

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