Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/05/05 14:47
Modified:
  08/05/05 16:31

Read: times


 
#98870 - Tester/Counter
Responding to: ???'s previous message
I have prepared a "tester", which compares a result from shifting an arbitrary input value with a reference routine, and, more importantly, counts the instruction cycles of the "tested" shift routine, finds their minimum and maximum and adds them up to a sum (which then divided by 31 gives the average duration).

;----------- testing program for the shift code competition ----------------
;------------------------------- by wek ------------------------------------

   DSEG  AT  30h
MaxTime: ds  2     ;worst case duration
MaxAcc:  ds  1     ;value of acc (=shift count) for worst case
MinTime: ds  2     ;best case duration
MinAcc:  ds  1     ;value of acc (=shift count) for best case
TotTime: ds  2     ;total duration (TotTime/31=average duration)
Result:  ds  4     ;temporary store of reference result



   CSEG
Tester:
                   ;initialize the time measurement variables
   mov   MaxTime+0,#0
   mov   MaxTime+1,#0
   mov   MinTime+0,#0FFh
   mov   MinTime+1,#0FFh
   mov   TotTime+0,#0
   mov   TotTime+1,#0
   
   mov   tmod,#1   ;set timer0 to 16 bit counter

   mov   a,#1Fh

TesterLoop:        ;for acc:=1f downto 1 do this loop
   push  acc
   mov   r4,#0ABh     ;this is the test vector
   mov   r5,#0CDh
   mov   r6,#056h
   mov   r7,#078h
                      ;first, do the reference calculation
   lcall ShiftTrivial
   mov   Result+0,r4  ;...and store the result for future comparison
   mov   Result+1,r5
   mov   Result+2,r6
   mov   Result+3,r7

   pop   acc
   push  acc          ;restore accumulator (=shift count)
   mov   r4,#0ABh
   mov   r5,#0CDh
   mov   r6,#056h
   mov   r7,#078h     ;and use the same test vector
   
   mov   th0,#0       ;reset timer0 - we are going to measure the 
   mov   tl0,#0       ;               duration of the shift routine
   setb  tr0          ;start timer0
   lcall Shift        ;and call the tested routine
   clr   tr0          ;stop timer0

   mov   a,r4         ;now test the result against the reference
   cjne  a,Result+0,Error
   mov   a,r5
   cjne  a,Result+1,Error
   mov   a,r6
   cjne  a,Result+2,Error
   mov   a,r7
   cjne  a,Result+3,Error

   clr   c            ;get measured time into r2:r3
   mov   a,tl0
   subb  a,#3           ;correction for setb tr0 and call
   mov   r3,a
   mov   a,th0
   subb  a,#0
   mov   r2,a

   mov   a,TotTime+1  ;add the measured time to total time
   add   a,r3
   mov   TotTime+1,a
   mov   a,TotTime+0
   addc  a,r2
   mov   TotTime+0,a

   mov   a,r2         ;if measured time is higher than MaxTime then update MaxTime
   cjne  a,MaxTime+0,TesterX1
   mov   a,r3
   cjne  a,MaxTime+1,TesterX1
TesterX1:
   jc    TesterX2
   mov   MaxTime+0,r2
   mov   MaxTime+1,r3
   pop   MaxAcc
   push  MaxAcc
TesterX2:

   mov   a,r2         ;if measured time is lower than MinTime then update MinTime
   cjne  a,MinTime+0,TesterX11
   mov   a,r3
   cjne  a,MinTime+1,TesterX11
TesterX11:
   jnc   TesterX12
   mov   MinTime+0,r2
   mov   MinTime+1,r3
   pop   MinAcc
   push  MinAcc
TesterX12:

   pop   acc          ;restore acc - end of loop
   djnz  acc,TesterLoop

   
                   ;end of test, stay looping forever
                   ;in a non-simulated environment, the timing results could
                   ;  been sent out e.g. via UART for inspection
Stop:
   sjmp  Stop


                   ;in case of failed test, loops here forever
                   ;again in a non-simulated environment, something could be
                   ;   sent out (e.g. "ERROR" :-)
Error:
   sjmp  Error

;--------------------- the reference shift routine -----------------------
;--- input:    r4:r5:r6:r7 - the operand
;              acc         - number of shifts (any number)
;--- output:   r4:r5:r6:r7 - the result
;--- destroys: b
;
;Performance (cycles including ret):
; WorstCase - 470
; BestCase  - 20
; Average   - 245
; Code Length - 21 bytes

ShiftTrivial:
   jz    ShiftTRet
   mov   b,a
ShiftTLoop:
   clr   c
   mov   a,r4
   rrc   a
   mov   r4,a
   mov   a,r5
   rrc   a
   mov   r5,a
   mov   a,r6
   rrc   a
   mov   r6,a
   mov   a,r7
   rrc   a
   mov   r7,a
   djnz  b,ShiftTLoop
ShiftTRet:
   ret                  ;a*15+5 cycles (incl. ret)

Shift:
;-------- insert your shift routine here ---------




Enjoy!

Jan Waclawek

List of 17 messages in thread
TopicAuthorDate
Code Challenge operational            01/01/70 00:00      
   Questions for current challenge ?            01/01/70 00:00      
      Instruction Cycles?            01/01/70 00:00      
         best case/worst case            01/01/70 00:00      
            how about 30 instructions            01/01/70 00:00      
               30 instructions?            01/01/70 00:00      
                  i did count inside loops            01/01/70 00:00      
            Changed last night            01/01/70 00:00      
      Tester/Counter            01/01/70 00:00      
         Wow!            01/01/70 00:00      
   The 3 better solutions            01/01/70 00:00      
   Definition of BEST            01/01/70 00:00      
      Definition of best            01/01/70 00:00      
         Good idea            01/01/70 00:00      
   pre tags?            01/01/70 00:00      
      Yes            01/01/70 00:00      
         A highlight please            01/01/70 00:00      

Back to Subject List