Hi guys

Bit of a maths question today. Funnily enough relating to scope code again. It's to do with sample rates (or periods! as they are called on the amiga) and time calculations.

This is a section of code from the original protracker scope code..

Code:

MOVEQ.L #0,D1
MOVE.W ns_period(A2),D1
LSR.W #1,D1
BEQ.S ScoNextChan
MOVE.L #35469,D2
DIVU D1,D2
EXT.L D2
ADD.L D2,D0

The above section calculates where the sample pointer needs to draw from in the next frame. The 35469 is the magic number which does this.

So, at present all the scopes I've seen or worked on simply draw the samples byte by byte to the screen and uses this calculation to simply move the pointer each frame to where the sample should be. This is not an accurate representation of the wave form, however it does suffice most requirements.

What I want to do is draw the wave at the frequency it is being played at. My idea was to simply divide the 35469 by the pixel width of the scope, then use this value to increment the sample pointer for each pixel. However, I very quickly realised that you cannot get away with this as the remainder from the division needs to be taken into consideration for each pixel iteration.

Code:

moveq #0,d1
MOVE.W #172,D1 ; period
LSR.W #1,D1
MOVE.L #35469/160,D2 ; magic number div by 160 pixels
DIVU D1,D2

So the result of this in D2 is $00310002, so I can assume that I can move forward two bytes to get the next pixel from the sample data, however if I just do that for all 160 pixels, it wont finish at the end because the remainder $0031 is not being taken into consideration.

So... how do you go about performing this type of calculation in 68k?

On a side note, it doesn't need to be fast, just accurate as it will all get pre-calced into look up tables anyhow.