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 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<<SPMEN) ; enable next SPM, write R1:R0 into temp page buffer
rcall flashDoSpm ; (R16)
adiw zh:zl, 2
dec r24
sbiw r25:r24, 1
brne flash1pMegaWritePages_loop
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. *
; ***************************************************************************
; Code for large pagesizes (128 and larger)
; ***************************************************************************
; code
@@ -92,7 +94,7 @@ flashWriteData:
flashWriteData_beginPage:
rcall flashBeginPage ; (r16, r24, r25, X)
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
; prepare data for return
sub r17, r18 ; r17 holds remainder
@@ -117,24 +119,30 @@ flashWriteData_copyLoop:
; @param R17 number of bytes to write
; @return X absolute write position inside buffer
; @return R18 bytes to write
; @clobbers r24, r25
; @clobbers r19, r24, r25
flashCalcPosAndLength:
; calc offset into buffer
mov r24, zl
andi r24, (FLASH_PAGESIZE-1) ; r24=rel pos inside buffer
ldi r25, FLASH_PAGESIZE
sub r25, r24 ; r25=bytes left inside page
mov r18, r17
cp r25, r17 ; bytes to read > 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