;----------- 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:
<font color="#FF0000">;-------- insert your shift routine here ---------</font>

