Autor Tema: FILLRAM  (Leído 3463 veces)

0 Usuarios y 1 Visitante están viendo este tema.

SapphiRe

  • Visitante
FILLRAM
« en: 21 de Marzo de 2006, 05:44:19 pm »
A fast way to fill (bc+1) bytes of RAM with a given value:

Input:

hl = initial address
a = value
bc = length - 1 (so, don't use this routine to fill just 1 byte ;D)

Code:

Código: [Seleccionar]
ld [hl],a
ld d,h
ld e,l
inc de
ldir
ret

SapphiRe

  • Visitante
Re: FILLRAM
« Respuesta #1 en: 16 de Abril de 2006, 11:46:21 am »
A faster way, don't use just for a few bytes:

hl = initial address
a = value
bc = length / 2 (so, don't use this routine to fill an odd number of bytes ;D)

Code:

Código: [Seleccionar]
di ; NO INTERRUPTS ALLOWED INSIDE THIS CODE
ld [STACK],SP ; Saves Stack Pointer value
add hl,bc ; Go to the last address
add hl,bc ; Useless if you set initially the correct value on hl
ld SP,hl ; Stack Pointer -> Next byte to the desired fill area
ld d,a ; d = a
ld e,a ; de = aa
@@loop:
push de ; two bytes filled, SP decremented
cpi ; A-(hl) , inc hl (useless), dec bc (Parity flag set until BC=0)
jp PE,@@loop ; close loop
ld SP,[STACK] ; restore Stack (don't forget it!)
ei ; Enable interrupts (also, don't forget it!)
ret ; return

PUSH = 11 cycles
CPI = 16 cycles
JP PE = 10 cycles
Total = 37 cycles each 2 bytes

Using ldir = 21 cycles each byte -> 42 cycles each 2 bytes

Not many, but, of course, you can unroll the loop to avoid executing CPI and JP PE each PUSH... so you can gain more extra cycles

WYZ

  • Visitante
Re: FILLRAM
« Respuesta #2 en: 28 de Mayo de 2006, 11:25:54 am »
The old "fast clear screen" spectrum rutine....  :D
Simply brilliant.