Hi Thomas,

> If you need to read less than 64K from the upper 96K Flash ROM then
> using a fixed absolute address with a 16bit index, e.g. "LDF
> A,(0x123456,X)", might be a solution.
>
> That's also a bit faster than indirect addressing.

Thank you for the advice. Yes, a 16-bit index would be no problem, 
seeing as I'm only writing up to 128 bytes (max flash block size) at a time.

So, if I reformulate my code to additionally implement the loop in ASM, 
such that I can specifically put the loop counter (aka the flash address 
and source buffer offset) in the X register, I have this:

static uint32_t flash_write_block_addr_tmp;
static uint8_t *flash_write_block_buf_tmp;

void flash_write_block(const uint32_t addr, const void *buf) {
      /* Unlock flash, etc... */

      flash_write_block_addr_tmp = addr;
      flash_write_block_buf_tmp = (uint8_t *)buf;

     __asm
             pushw x
             push a
             clrw x
         0001$:
             cpw x, #FLASH_BLOCK_SIZE
             jrnc 0002$
             ld a, ([_flash_write_block_buf_tmp], x)
             ldf ([_flash_write_block_addr_tmp], x), a
             incw x
             jra 0001$
         0002$:
             pop a
             popw x
     __endasm;

      /* Wait for write completion, etc... */
}

Is this correct? For the input buffer, I take the address of the buf_tmp 
variable (as represented by the symbol), use that as an indirect address 
to get the pointer address, which is then indexed by X (i.e. offset by 
loop counter value), and finally used as the source when loading the A 
register. Same for the flash address, but used as the destination for LDF.

And this should be faster than the previous way of using addr_tmp and 
buf_tmp as absolute addresses and incrementing them separately? I think 
I see why that may be: the STM8 programming manual says "ldf [addr], a" 
takes the same number of cycles as "ldf ([addr], x), a", with the same 
story for "ld". So if I save doing the separate address incrementations 
(expensive for a 32-bit value), then I guess it's a win. :-)

Regards,
Basil

_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to