flash: use 16-bit counters.

This commit is contained in:
Martin Preuss
2025-06-01 19:22:04 +02:00
parent bb14dd4c22
commit 1e5de0da23
2 changed files with 26 additions and 17 deletions

View File

@@ -42,13 +42,14 @@ flash1pMegaWritePage:
ldi xl, LOW(flashPageBuffer) ldi xl, LOW(flashPageBuffer)
ldi xh, HIGH(flashPageBuffer) ldi xh, HIGH(flashPageBuffer)
ldi r24, LOW(PAGESIZE) ldi r24, LOW(PAGESIZE)
ldi r25, HIGH(PAGESIZE)
flash1pMegaWritePages_loop: flash1pMegaWritePages_loop:
ld r0, X+ ; read source data from buffer (low) ld r0, X+ ; read source data from buffer (low)
ld r1, X+ ; read source data from buffer (high) ld r1, X+ ; read source data from buffer (high)
ldi r20, (1<<SPMEN) ; enable next SPM, write R1:R0 into temp page buffer ldi r20, (1<<SPMEN) ; enable next SPM, write R1:R0 into temp page buffer
rcall flashDoSpm ; (R16) rcall flashDoSpm ; (R16)
adiw zh:zl, 2 adiw zh:zl, 2
dec r24 sbiw r25:r24, 1
brne flash1pMegaWritePages_loop brne flash1pMegaWritePages_loop
subi zl, LOW(PAGESIZE*2) ; point back to begin of page subi zl, LOW(PAGESIZE*2) ; point back to begin of page

View File

@@ -7,6 +7,8 @@
; * Please see toplevel file COPYING of that project for license details. * ; * Please see toplevel file COPYING of that project for license details. *
; *************************************************************************** ; ***************************************************************************
; Code for large pagesizes (128 and larger)
; *************************************************************************** ; ***************************************************************************
; code ; code
@@ -92,7 +94,7 @@ flashWriteData:
flashWriteData_beginPage: flashWriteData_beginPage:
rcall flashBeginPage ; (r16, r24, r25, X) rcall flashBeginPage ; (r16, r24, r25, X)
flashWriteData_calcPosAndLength: flashWriteData_calcPosAndLength:
rcall flashCalcPosAndLength ; r18=bytes_to_write, x=pos_in_buffer (r24, r25) rcall flashCalcPosAndLength ; r18=bytes_to_write, x=pos_in_buffer (r19, r24, r25)
; X=abs pos in buffer, r18=bytes to read, r17=bytes initially requested ; X=abs pos in buffer, r18=bytes to read, r17=bytes initially requested
; prepare data for return ; prepare data for return
sub r17, r18 ; r17 holds remainder sub r17, r18 ; r17 holds remainder
@@ -117,24 +119,30 @@ flashWriteData_copyLoop:
; @param R17 number of bytes to write ; @param R17 number of bytes to write
; @return X absolute write position inside buffer ; @return X absolute write position inside buffer
; @return R18 bytes to write ; @return R18 bytes to write
; @clobbers r24, r25 ; @clobbers r19, r24, r25
flashCalcPosAndLength: flashCalcPosAndLength:
; calc offset into buffer ; calc offset into buffer
mov r24, zl mov r24, zl
andi r24, (FLASH_PAGESIZE-1) ; r24=rel pos inside buffer mov r25, zh
ldi r25, FLASH_PAGESIZE andi r24, LOW(FLASH_PAGESIZE-1)
sub r25, r24 ; r25=bytes left inside page andi r25, HIGH(FLASH_PAGESIZE-1) ; R25:r24=pos inside page
mov r18, r17 ldi r18, LOW(FLASH_PAGESIZE)
cp r25, r17 ; bytes to read > bytes left in page? ldi r19, HIGH(FLASH_PAGESIZE)
brcc flashCalcPosAndLength_l1 ; no: jump sub r18, r24
mov r18, r25 ; yes: cut r18 to number of bytes left in page sbc r19, r25 ; R19:R18=FLASH_PAGESIZE-relPos
flashCalcPosAndLength_l1: tst r19 ; high byte zero?
ldi xl, LOW(flashPageBuffer) ; set X to pos within page buffer 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) ldi xh, HIGH(flashPageBuffer)
add xl, r24 add xl, r24
adc xh, r24 adc xh, r25
sub xh, r24
ret ret
; @end ; @end
@@ -245,17 +253,17 @@ flashEndPage_write:
; @routine flashReadPageIntoSram ; @routine flashReadPageIntoSram
; ;
; @param Z Address to read from (byte address as for LPM!) ; @param Z Address to read from (byte address as for LPM!)
; @clobbers r16, r24, X, Z ; @clobbers r16, r24, r25, X, Z
flashReadPageIntoSram: flashReadPageIntoSram:
ldi xl, LOW(flashPageBuffer) ldi xl, LOW(flashPageBuffer)
ldi xh, HIGH(flashPageBuffer) ldi xh, HIGH(flashPageBuffer)
ldi r24, LOW(FLASH_PAGESIZE) ldi r24, LOW(FLASH_PAGESIZE)
ldi r25, HIGH(FLASH_PAGESIZE)
flashReadPageIntoSram_loop: flashReadPageIntoSram_loop:
lpm r16, Z+ lpm r16, Z+
st X+, r16 st X+, r16
; adiw ZH:ZL, 1 sbiw r25:r24, 1
dec r24
brne flashReadPageIntoSram_loop brne flashReadPageIntoSram_loop
ret ret
; @end ; @end