cpct_memset_f64

Fills up a complete array in memory setting bytes 2-by-2, in chuncks of 64 bytes.  Size of the array must be multiple of 64.

C Definition

void cpct_memset_f64 (void* array, u16 value, u16 size);

Warning

  • This function disables interrupts while operating and uses the stack pointer.  Take it into account when you require interrupts or the stack pointer.  When in doubt, use cpct_memset instead.
  • At the end of the function, it reenables interrupts, no matter how they were before.  Please, take into account.

Input Parameters (5 Bytes)

(2B HL) arrayPointer to the first byte of the array to be filled up (starting point in memory)
(2B DE) value16-bit value to be set (Pair of bytes)
(2B BC) sizeNumber of bytes to be set (>= 64, multiple of 64)

Assembly call (Input parameters on registers)

call cpct_memset_f64_asm

Parameter Restrictions

  • array could theoretically be any 16-bit memory location.  However, take into account that this function does no check at all, and you could mistakenly overwrite important parts of your program, the screen, the firmware...  Use it with care.
  • size must be greater than 63 and multiple of 64.  It represents the size of the array, or the number of total bytes that will be set to the value.  This function sets bytes 2-by-2, in chuncks of 64 bytes, so the minimum amount of bytes to be set is 64.  Beware! sizes below 64 can cause this function to overwrite the entire memory.
  • value could be any 16-bit value, without restrictions.  It is considered as a pair of bytes that will be copied to every 2-bytes in the array.

Known limitations

  • This function will not work from ROM, as it uses self-modifying code.

Details

Sets all pairs of bytes of an array in memory to the same given value.  This is the same operation as std memset from standard C library, but with the added advantage of being faster and letting the user define the contents 16-bits-by-16-bits instead of 8.  The technique used by this function is as follows:

1It saves the value of SP to recover it at the end of the function
2It places SP at the last 2-bytes of the array
3It uses PUSH instructions to set bytes 2-by-2, in chuncks of 64 bytes, until the entire array is set

This function works for array sizes from 64 to 65472.  However, it is recommended that you use it for values much greater than 64.

Destroyed Register values

AF, BC, DE, HL

Required memory

C-binding72 bytes
ASM-binding67 bytes

Time Measures

  Case      |   microSecs (us)   |      CPU Cycles       |
----------------------------------------------------------
   Any      | 47 + 132CH + 3CHHH | 188 + 528*CH + 12CHHH |
----------------------------------------------------------
 CH%256 = 0 |         +1         |         +4            |
----------------------------------------------------------
Asm saving  |        -16         |        -64            |
----------------------------------------------------------

BC = array size (Number of total bytes to set) CH = BC \ 64 (number of chuncks, 1 chunck = 64 bytes) CHHH = CH \ 256 - 1 \ = integer division

Fills up a complete array in memory setting bytes 2-by-2, in chuncks of 64 bytes.
unsigned int (u16 = unsigned 16-bits, 2 bytes)
Fills up a complete byte-array in memory with a given 8-bit value (as std memset)
Close