mcleod_ideafix

Measuring Z80 speed inside a ZX Spectrum

Mar 1st, 2020
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Speed measuring of a Z80 CPU into a ZX Spectrum
  2. ;The core of the routine is a tight loop in which HL is incremented and an
  3. ;unconditional relative jump is made to the INC instruction again and
  4. ;again. This continues until an interrupt is triggered, which modifies the
  5. ;target address of the relative jump so the loop actually finishes.
  6.  
  7. ;Label RealStart begins ressetting the counter (HL). Then waits for an INT
  8. ;and resets the target address for the relative jump. The count begins...
  9. ;... until a new interrupt is triggered. The task of the interrupt routine
  10. ;is just to change the target of the jump instruction so it does not jump
  11. ;to the INC HL instruction anymore. The current value of HL is transfered
  12. ;to BC and interrupts are restored to IM 1. From BASIC, it is possible to
  13. ;read the value of the counter (now in BC) and using it to derive the actual
  14. ;number of T-states consumed during an interrupt interval (which in a stock
  15. ;48K Spectrum is 69688 T-states, divided into 312 scans of 224T each. 224T
  16. ;equals to 64 microseconds)
  17.  
  18. TRGJUMP             equ 253
  19.  
  20.                     org 65000
  21. InstallINT          di
  22.                     ld a,253
  23.                     ld i,a
  24.                     im 2
  25.                     jp RealStart       ;install IM2 handler and go to start
  26.                     org 65023
  27.                     dw NewINT
  28. NewINT              xor a              ; 4T
  29.                     ld (JumpOpc+1),a   ;13T
  30.                     ei                 ; 4T
  31.                     ret                ;10T
  32.                                        ;---- = 31T + 24T(ack when interrupted within the loop) = 55T
  33. RealStart           ld hl,0          ;reset counter
  34.                     ei
  35.                     halt
  36.                     ld a,TRGJUMP     ;  7T (the interrupt has ended, time starts here! )
  37.                     ld (JumpOpc+1),a ; 13T
  38. ;---- Counting code ------------     ;----- = 20T (time pre-loop)
  39. KeepInc             inc hl           ;  6T
  40. JumpOpc             jr KeepInc       ; 12T
  41. ;----- End of counting ---------     ;------ = 18T
  42.                     di               ; (the interrupt has ended -again-, time stops here! )
  43.                     ld b,h
  44.                     ld c,l
  45.                     im 1
  46.                     ei
  47.                     ret
  48.  
  49.                     end InstallINT
  50.  
  51.                     ;SPEED (MHz) =  (7+13+18*BC+55)/(312*64) = (75+BC*18)/(312*64)
Add Comment
Please, Sign In to add comment