diff --git a/avr/modules/flash/flash1pmega.asm b/avr/modules/flash/flash1pmega.asm index 2066e18..1b33007 100644 --- a/avr/modules/flash/flash1pmega.asm +++ b/avr/modules/flash/flash1pmega.asm @@ -42,13 +42,14 @@ flash1pMegaWritePage: ldi xl, LOW(flashPageBuffer) ldi xh, HIGH(flashPageBuffer) ldi r24, LOW(PAGESIZE) + ldi r25, HIGH(PAGESIZE) flash1pMegaWritePages_loop: ld r0, X+ ; read source data from buffer (low) ld r1, X+ ; read source data from buffer (high) ldi r20, (1< bytes left in page? - brcc flashCalcPosAndLength_l1 ; no: jump - mov r18, r25 ; yes: cut r18 to number of bytes left in page -flashCalcPosAndLength_l1: - ldi xl, LOW(flashPageBuffer) ; set X to pos within page buffer + mov r25, zh + andi r24, LOW(FLASH_PAGESIZE-1) + andi r25, HIGH(FLASH_PAGESIZE-1) ; R25:r24=pos inside page + ldi r18, LOW(FLASH_PAGESIZE) + ldi r19, HIGH(FLASH_PAGESIZE) + sub r18, r24 + sbc r19, r25 ; R19:R18=FLASH_PAGESIZE-relPos + tst r19 ; high byte zero? + breq flashCalcPosAndLength_8bit ; yes, already 8-bit + ldi r18, 255 ; clip size to 8 bit +flashCalcPosAndLength_8bit: + cp r17, r18 ; r18>r17? + brcc flashCalcPosAndLength_sizeOk ; nope: jump + mov r18, r17 ; clip r18 to size requested +flashCalcPosAndLength_sizeOk: ; r18=bytes to write + ldi xl, LOW(flashPageBuffer) ; set X to pos within page buffer ldi xh, HIGH(flashPageBuffer) add xl, r24 - adc xh, r24 - sub xh, r24 + adc xh, r25 ret ; @end @@ -245,17 +253,17 @@ flashEndPage_write: ; @routine flashReadPageIntoSram ; ; @param Z Address to read from (byte address as for LPM!) -; @clobbers r16, r24, X, Z +; @clobbers r16, r24, r25, X, Z flashReadPageIntoSram: ldi xl, LOW(flashPageBuffer) ldi xh, HIGH(flashPageBuffer) ldi r24, LOW(FLASH_PAGESIZE) + ldi r25, HIGH(FLASH_PAGESIZE) flashReadPageIntoSram_loop: lpm r16, Z+ st X+, r16 -; adiw ZH:ZL, 1 - dec r24 + sbiw r25:r24, 1 brne flashReadPageIntoSram_loop ret ; @end