<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Tue, 05 Feb 2013 10:21:35 +0100
From   : j+bbcmicro@... (J. E. Klasek)
Subject: 6502 coding request

On Tue, Feb 05, 2013 at 12:02:41AM +0000, J.G.Harston wrote:
> J. E. Klasek wrote:
> > See http://unusedino.de/ec64/technical/aay/c64/romfddd.htm
> > BTW: I used a PAL version all the time and there was always a 1/60 
> > time base.
> 
> All the documentation said 1/60s and I did a test with a continuous 
> display with a stopwatch next to it and it matched.
> 
> Anyway, I was staring into space while eating my lunch and it occurred 
> to me that converting 60 to 100 is multiplying by 1.6666etc. and 
> wondered if I could make a fast multiply-by-1.6666 routine with shifts 
> and adds. A bit of scribbling showed that 1.6666 in binary is 
> 1.101010101010etc. Wonderful, a loop with a shift and an add on every 
> second shift:

Congratulations! My first tryout was multiplying by 5/3, times 5 is easy and
1/3 is multiply with 0.01010101010 with a two digits periodic ...
I missed the last step to combine these multiplications.
Anyway, I have to offer some refinements and fixes, respectively:

> 
> JSR &FFDE                    :\ Read Jiffy clock to &YYXXAA
> STA wksp+0:STA wksp+3        :\ Prepare to multiple by 1+2/3
> STX wksp+1:STX wksp+4        :\  to convert 1/60ths to 1/100ths
> STY wksp+2:STY wksp+5
> .c64Word01lp1                     :\ Can probably optimise this
> LSR wksp+5:ROR wksp+4:ROR wksp+3  :\ add=add/2
  CLC                               :\ clean add
> LDA wksp+0:ADC wksp+3:STA wksp+0  :\ num=num+add

This part can be done with registers which already have the proper values ...
> LDA wksp+1:ADC wksp+4:STA wksp+1
> LDA wksp+2:ADC wksp+5:STA wksp+2

  TXA:ADC wksp+4:TAX
  TYA:ADC wksp+5:TAY

> LSR wksp+5:ROR wksp+4:ROR wksp+3  :\ add=add/2

This 
> LDA wksp+5:ORA wksp+4:ORA wksp+3
> BNE c64Word01lp1                  :\ Loop until nothing left to add
can be replaced by

  BNE c64Word011p1             :\ wksp+3 not 0
  LDA wksp+4
  BNE c64Word011p1
  LDA wksp+5
  BNE c64Word011p1

Save registers into workspace
  STX wksp+1
  STY wksp+2

> Multiply by 1+2/3 is 1 + 1/2 + 1/8 + 1/32 + 1/128 etc. Significantly 
> faster than looping subtracting 3s and counting. See, learning infinite 
> series in 'A' level maths does have real-world applications :)

The value range of 1/100 count is expanded. The 2^24 modulo (24 bit arithmetics)
leads to an additional wrap-around

     1/60        1/100
        0            0
        .            .
        .            .
        .            .
~10000000    ~16666666 (2^24-1)
       +1            0
        .            .
        .            .
        .            .
   2^24-1    ~11000000
        0            0

But in most cases this can be neglected.

JK
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>