Use the following code. You must set register P1 ($ff00) to $30 or else a button held down
can cause STOP to not complete the process of changing speeds. Register KEY1 ($ff4d) bit 0
is used as a flag to determine if STOP should turn off the display & wait for a button
or toggle the CPU speed:
ToggleCPUSpeed:
di
ld hl,rIE
ld a,[hl]
push af
xor a
ld [hl],a ;disable interrupts
ld [rIF],a
ld a,$30
ld [rP1],a
ld a,1
ld [rKEY1],a
stop
pop af
ld [hl],a
ei
ret