嵌入式操作系统
Figure 1. Problem: overlapping memory copy.
Given the two visuals below (Figures 2 and 3), one should also be able to see that Solution #1 suffers additional overhead in order to calculate, store, and pass different source, destination, and length variables for multiple copy operations, whereas the second solution must do this only once.
Figure 2. Overlapping memory copy solution #1.
Figure 3. Overlapping memory copy solution #2.
In order to take advantage of the data pointer decrement feature, the application code first must determine if and how the source and destination ranges overlap, a task that would be executed even if the data pointer decrement feature weren't available. When a potentially problematic source/destination buffer overlap is detected, the data pointers are placed at the end of the respective source/destination copy ranges and the IDx bits are configured to enable the data pointer decrement mode. Example code for solution #2 is provided below. Note that the DPTR toggling ('INC DPS') and increment/decrement ('INC DPTR') functions have been included in the code for the purpose of understanding only and can be removed if the respective auto-toggle and/or auto-increment/decrement bits have been set.
;------------------------------------------------------------- ; Example memmove code which tests for overlapping src/dest ; address ranges before the copy operation. Code assumes that ; length>0, the copy is being done within a single 64kB xdata ; bank, and DPTR0 used otherwise (i.e. only DPTR0 saved). ; ; input: R6:R7 (pointer to destination) ; R4:R5 (pointer to source) ; R2:R3 (length) ; uses: R1 (src-dest pointer delta temp store) ; R0:R1 (length-1 later) ;------------------------------------------------------------- memmove: push dps ;save DPTR0 push dpl push dph ;-------< DETECT PROBLEMATIC SRC/DEST BUFFER OVERLAP>--------- clr c ; mov a, r5 ; check delta between subb a, r7 ; src/dest pointers mov r1, a ; mov a, r4 ; subb a, r6 ; jnc domemcpy ; dest pointer < ; src pointer addr? xch a, r1 ; NO add a, r3 ; check delta vs. xch a, r1 ; length addc a, r2 jnc domemcpy ; length < delta? xrl a, r1 ; NO jz domemcpy ; length = delta? ;-----< DATA POINTER DECREMENT MODE / ADJUST POINTERS >------- overlap: ; NO. mov dps, #0C0h ; ID1,ID0=11b (dec) mov a, r3 ; add a, #0ffh ; add (-1) to length mov r1, a ; prior to adding to mov a, r2 ; original src/dest jc lenless1 ; start pointers dec a lenless1: mov r0, a ; R0:R1 = (length-1) mov a, r5 ; src pointer add a, r1 ; starts at the end mov r5, a ; =src+length-1 mov a, r4 addc a, r0 mov r4, a mov a, r7 ; dest pointer add a, r1 ; starts at the end mov r7, a ; =dest+length-1 mov a, r6 addc a, r0 mov r6, a ;------------------< NORMAL COPY LOOP >------------------ domemcpy: mov a, r3 jz nomod ; inc msb if lsb<>00h inc r2 ; for proper loop cntrl nomod: mov dph, r4 ; load source mov dpl, r5 mov dph1, r6 ; load dest mov dpl1, r7 copyloop: movx a, @dptr ; read inc dptr ; inc/dec src pointer inc dps ; select dest movx @dptr, a ; write inc dptr ; inc/dec dest pointer inc dps ; select src djnz r3, copyloop djnz r2, copyloop pop dps pop dph pop dpl retAPPENDIX A: DATA POINTER FEATURES
HIGH-SPEED µC | DATA POINTERS | INCREMENT/DECREMENT SELECTION BITS | AUTO-TOGGLE FEATURE | AUTO-INCREMENT/DECREMENT FEATURE |
DS80C310 | 2 | No | No | No |
DS80C320/323 | 2 | No | No | No |
DS8XC520/530 | 2 | No | No | No |
DS80C390 | 2 | Yes | Yes | No |
DS89C430/450 | 2 | Yes | Yes | Yes |
全部0条评论
快来发表一下你的评论吧 !