// Copyright 2020-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <xs1.h>

// r0 - dest addr            must be word aligned
// r1 - src addr             must be vector aligned for max performance
// r2 - number of VPU_MEMCPU_BLOBS:   0 or more
    
#define FUNCTION_NAME memcpy_ext
#define NSTACKWORDS   0

    .text
    .cc_top FUNCTION_NAME.function
    .type   FUNCTION_NAME,@function
    .globl FUNCTION_NAME
    .align 16
    .issue_mode dual
    
FUNCTION_NAME:
    { dualentsp NSTACKWORDS ; add r11, r1, 4     } // Misalign first prefetch to catch two lines
    { ldc r3, 32            ; bf r2, .early_exit } // number of bytes address increments between vec accesses
    { prefetch r11          ; add r11, r1, r3 }
    add  r11, r11, r3 

    // Copy loop start
.copy_loop:
    { prefetch r11          ; add  r11, r11, r3  }   // prefetch a line for second half of loop
    { vldd r1[0]            ; add  r1, r1, r3    }
    { vstd r0[0]            ; add  r0, r0, r3    }
    { bt   r2, .copy_loop   ; sub r2, r2, 1      }
    // Copy loop end

.early_exit:
	retsp NSTACKWORDS

.tmp_FUNCTION_NAME:
    .size   FUNCTION_NAME, .tmp_FUNCTION_NAME-FUNCTION_NAME
    .align  4
    .cc_bottom FUNCTION_NAME.function
    
    .set    FUNCTION_NAME.nstackwords,NSTACKWORDS
    .globl  FUNCTION_NAME.nstackwords
    .set    FUNCTION_NAME.maxcores,1
    .globl  FUNCTION_NAME.maxcores
    .set    FUNCTION_NAME.maxtimers,0
    .globl  FUNCTION_NAME.maxtimers
    .set    FUNCTION_NAME.maxchanends,0
    .globl  FUNCTION_NAME.maxchanends
