avr: cleanup, added labels to calculate module sizes.

This commit is contained in:
Martin Preuss
2023-04-08 18:18:19 +02:00
parent 4a915a3c85
commit 0f678d7d5a
21 changed files with 416 additions and 167 deletions

View File

@@ -75,6 +75,9 @@ utilsDataEnd:
.cseg
UTILS_BEGIN:
utilsDateString: .db "%YEAR%-%MONTH%-%DAY%-%HOUR%:%MINUTE%", 0, 0
@@ -86,50 +89,17 @@ utilsDateString: .db "%YEAR%-%MONTH%-%DAY%-%HOUR%:%MINUTE%", 0, 0
; IN:
; OUT:
; - CFLAG: set if okay, clear on error
; USED: R16, R17, R18, R24, R25, X, Y
; USED: R16, R17, R18, R19, X, (R20, R21, R24, R25, X, Z)
Utils_Init:
; preset initial seed from compile date string
ldi zl, LOW(utilsDateString*2)
ldi zh, HIGH(utilsDateString*2)
ldi r18, 0xe1
ldi r19, 0xac
Utils_Init_l1:
lpm r16, Z+
tst r16
breq Utils_Init_l2
eor r18, r16
clc
sbrc r19, 7
sec ; only executed if bit 7 is set in r19
rol r18
rol r19
rjmp Utils_Init_l1
Utils_Init_l2:
; build initial SRAM content into intial seed
ldi xl, LOW(SRAM_START)
ldi xh, HIGH(SRAM_START)
ldi r24, LOW(RAMEND-SRAM_START)
ldi r25, HIGH(RAMEND-SRAM_START)
Utils_Init_l3:
ld r16, X+
eor r18, r16
clc
sbrc r19, 7
sec ; only executed if bit 7 is set in r19
rol r18
rol r19
sbiw r25:r24, 1
brne Utils_Init_l3
rcall utilsSetupSeed ; (R16, R18, R19, R20, R21, R24, R25, X, Z)
; preset SRAM data area
ldi xh, HIGH(utilsDataBegin)
ldi xl, LOW(utilsDataBegin)
clr r16
ldi r17, (utilsDataEnd-utilsDataBegin)
rcall Utils_FillSram
rcall Utils_FillSram ; (r17, x)
sts utilsSeed, r18
sts utilsSeed+1, r19
@@ -139,6 +109,112 @@ Utils_Init_l3:
; ---------------------------------------------------------------------------
; setup seed
;
; IN:
; OUT:
; REGS: R16, R18, R19, R20, R21, R24, R25, X, Z
utilsSetupSeed:
rcall Utils_ReadSeed
mov r20, r18
mov r21, r19
; default initial seed
ldi r18, 0xe1
ldi r19, 0xac
; work stored seed into it
mov r16, r20
rcall utilsWorkByteIntoSeed
mov r16, r21
rcall utilsWorkByteIntoSeed
; work date string into seed
ldi zl, LOW(utilsDateString*2)
ldi zh, HIGH(utilsDateString*2)
rcall utilsWorkProgStringIntoSeed ; (R16, Z)
; work sram content into seed
rcall utilsWorkSramContentIntoSeed ; (R16, R24, R25, X)
; store seed in EEPROM
rcall Utils_WriteSeed ; (R16, R17, X)
ret
; ---------------------------------------------------------------------------
; utilsWorkProgStringIntoSeed
;
; IN:
; - Z : pointer to string to work into seed
; - R18: low byte of current seed
; - R19: high byte of current seed
; OUT:
; - R18: low byte of updated seed
; - R19: high byte of updated seed
; USED: R16, Z
utilsWorkProgStringIntoSeed:
lpm r16, Z+
tst r16
breq utilsWorkProgStringIntoSeed_done
rcall utilsWorkByteIntoSeed
rjmp utilsWorkProgStringIntoSeed
utilsWorkProgStringIntoSeed_done:
ret
; ---------------------------------------------------------------------------
; utilsWorkSramContentIntoSeed
; IN:
; - Z : pointer to string to work into seed
; - R18: low byte of current seed
; - R19: high byte of current seed
; OUT:
; - R18: low byte of updated seed
; - R19: high byte of updated seed
; USED: R16, R24, R25, X
utilsWorkSramContentIntoSeed:
ldi xl, LOW(SRAM_START)
ldi xh, HIGH(SRAM_START)
ldi r24, LOW(RAMEND-SRAM_START)
ldi r25, HIGH(RAMEND-SRAM_START)
utilsWorkSramContentIntoSeed_loop:
ld r16, X+
rcall utilsWorkByteIntoSeed
sbiw r25:r24, 1
brne utilsWorkSramContentIntoSeed_loop
ret
; ---------------------------------------------------------------------------
; utilsWorkByteIntoSeed
;
; IN:
; - R16: byte to work into the seed
; - R18: low byte of current seed
; - R19: high byte of current seed
; OUT:
; - R18: low byte of updated seed
; - R19: high byte of updated seed
; USED:
utilsWorkByteIntoSeed:
eor r18, r16
clc
sbrc r19, 7
sec ; only executed if bit 7 is set in r19
rol r18
rol r19
ret
; ---------------------------------------------------------------------------
; Utils_Fini
;
@@ -230,7 +306,7 @@ Utils_IncrementCounter16:
; ---------------------------------------------------------------------------
; Utils_ReadEeprom
; Utils_ReadEepromIncr
;
; Read a byte from EEPROM (see example in ATtiny24/44/84 manual p.19).
;
@@ -238,34 +314,36 @@ Utils_IncrementCounter16:
; - X: EEPROM Address to read from
; OUT:
; - R16: byte read
; - X: EEPROM Address incremented
; MODIFIED REGISTERS: R16
Utils_ReadEeprom:
Utils_ReadEepromIncr:
sbic EECR, EEPE ; wait for previous write to complete (if any)
rjmp Utils_ReadEeprom
rjmp Utils_ReadEepromIncr
out EEARH, xh ; set EEPROM address
out EEARL, xl
sbi EECR, EERE ; start EEPROM read by writing EERE
in r16, EEDR ; read data from data register
adiw xh:xl, 1
ret
; ---------------------------------------------------------------------------
; Utils_WriteEeprom
; Utils_WriteEepromIncr
;
; Write a byte to EEPROM (see example in ATtiny24/44/84 manual p.18).
;
; IN:
; - R16: byte to write
; - X: EEPROM Address to read from
; - X: EEPROM Address to write to
; OUT:
; - nothing
; - X: EEPROM Address incremented
; MODIFIED REGISTERS: R17
Utils_WriteEeprom:
Utils_WriteEepromIncr:
sbic EECR, EEPE ; wait for previous write to complete (if any)
rjmp Utils_WriteEeprom
rjmp Utils_WriteEepromIncr
ldi r17, (0<<EEPM1) | (0<<EEPM0) ; set programming mode
out EECR, r17
out EEARH, xh ; set EEPROM address
@@ -273,6 +351,7 @@ Utils_WriteEeprom:
out EEDR, r16 ; write data to data register
sbi EECR, EEMPE ; write logical one to EEMPE
sbi EECR, EEPE ; start EEPROM write by setting EEPE
adiw xh:xl, 1
ret
@@ -294,12 +373,12 @@ Utils_PseudoRandom:
ldi r18, 0x9c
ldi r19, 8
Utils_PseudoRandom_step:
lsr r17
ror r16
brcc Utils_PseudoRandom_nomask
eor r17, r18
lsr r17
ror r16
brcc Utils_PseudoRandom_nomask
eor r17, r18
Utils_PseudoRandom_nomask:
dec r19
dec r19
brne Utils_PseudoRandom_step
sts utilsSeed, r16
sts utilsSeed+1, r17
@@ -307,6 +386,21 @@ Utils_PseudoRandom_nomask:
; ---------------------------------------------------------------------------
; Update seed in EEPROM.
;
; IN:
; OUT:
; REGS: R18, R19, (R16, R17, X)
Utils_UpdateSeedInEeprom:
lds r18, utilsSeed
lds r19, utilsSeed+1
rcall Utils_WriteSeed ; (R16, R17, X)
ret
; ---------------------------------------------------------------------------
; Utils_ReadUid
;
@@ -323,16 +417,13 @@ Utils_ReadUid:
cli
ldi xl, LOW(EEPROM_OFFS_UUID)
ldi xh, HIGH(EEPROM_OFFS_UUID)
rcall Utils_ReadEeprom ; (R16)
rcall Utils_ReadEepromIncr ; (R16)
mov r18, r16
adiw xh:xl, 1
rcall Utils_ReadEeprom ; (R16)
rcall Utils_ReadEepromIncr ; (R16)
mov r19, r16
adiw xh:xl, 1
rcall Utils_ReadEeprom ; (R16)
rcall Utils_ReadEepromIncr ; (R16)
mov r20, r16
adiw xh:xl, 1
rcall Utils_ReadEeprom ; (R16)
rcall Utils_ReadEepromIncr ; (R16)
mov r21, r16
pop r15
out SREG, r15
@@ -348,42 +439,41 @@ Utils_ReadUid:
; IN:
; OUT:
; - CFLAG set if new generated, cleared if there already was one.
; REGS: R15, R16, R18, R19, R20, R21, X
; REGS: R15, R16, R18, R19, R20, R21, X (R17)
Utils_SetupUid:
in r15, SREG
cli
rcall Utils_ReadUid ; R16, X
mov r16, r18 ; all 0x00?
or r16, r19
or r16, r20
or r16, r21
breq Utils_SetupUid_generate ; yep, go generate one
mov r16, r18 ; all 0xff?
and r16, r19
and r16, r20
and r16, r21
inc r16
breq Utils_SetupUid_generate ; yep, go generate one
rcall Utils_ReadUid ; (R16, X)
mov r16, r18 ; all 0x00?
or r16, r19
or r16, r20
or r16, r21
breq Utils_SetupUid_generate ; yep, go generate one
mov r16, r18 ; all 0xff?
and r16, r19
and r16, r20
and r16, r21
inc r16
breq Utils_SetupUid_generate ; yep, go generate one
out SREG, r15
clc
ret
Utils_SetupUid_generate:
ldi xl, LOW(EEPROM_OFFS_UUID)
ldi xh, HIGH(EEPROM_OFFS_UUID)
rcall Utils_PseudoRandom ; byte 0 (R16, R17, R18, R19)
inc r16
rcall Utils_WriteEeprom ; (R17)
adiw xh:xl, 1
rcall Utils_PseudoRandom ; byte 1
rcall Utils_WriteEeprom
adiw xh:xl, 1
rcall Utils_PseudoRandom ; byte 2
rcall Utils_WriteEeprom
adiw xh:xl, 1
rcall Utils_PseudoRandom ; byte 3
rcall Utils_WriteEeprom
adiw xh:xl, 1
ldi xl, LOW(EEPROM_OFFS_UUID)
ldi xh, HIGH(EEPROM_OFFS_UUID)
rcall Utils_PseudoRandom ; byte 0 (R16, R17, R18, R19)
inc r16
rcall Utils_WriteEepromIncr ; (R17)
rcall Utils_PseudoRandom ; byte 1
rcall Utils_WriteEepromIncr
rcall Utils_PseudoRandom ; byte 2
rcall Utils_WriteEepromIncr
rcall Utils_PseudoRandom ; byte 3
rcall Utils_WriteEepromIncr
rcall Utils_UpdateSeedInEeprom ; (R16, R17, R18, R19, X)
out SREG, r15
sec
ret
@@ -391,33 +481,56 @@ Utils_SetupUid_generate:
; ---------------------------------------------------------------------------
; Get offset and mask for a given bit in a bitfield
; Utils_ReadSeed
;
; Read seed from EEPROM.
;
; IN:
; - R16: bit to request position for
; OUT:
; - R1: offset into the bitfield to the byte containing the given bit
; - R2: mask for given id (apply to r1)
; USED REGISTERS: r1, r2, r17, Z
; - R18:R19: seed read
; REGS: R16, X
Utils_GetPosAndMaskInBitField:
mov r1, r16 ; divide by 8 to get the offset to the byte containing the given module id
lsr r1
lsr r1
lsr r1 ; r1=offset of the byte holding the given bit
mov r2, r16 ; get bit mask for bit position in table byte
ldi r17, 7 ; keep lower 3 bits
and r2, r17
ldi zh, HIGH(utilsModuleBitNumToMaskMap*2)
ldi zl, LOW(utilsModuleBitNumToMaskMap*2)
add zl, r2
brcc Utils_GetPosAndMaskInBitField_noOverflow
inc zh
Utils_GetPosAndMaskInBitField_noOverflow:
lpm r2, z ; r2=mask for bit in byte from bitfield
Utils_ReadSeed:
push r15
in r15, SREG
cli
ldi xl, LOW(EEPROM_OFFS_SEED)
ldi xh, HIGH(EEPROM_OFFS_SEED)
rcall Utils_ReadEepromIncr ; (R16)
mov r18, r16
rcall Utils_ReadEepromIncr ; (R16)
mov r19, r16
pop r15
out SREG, r15
ret
utilsModuleBitNumToMaskMap:
.db 1, 2, 4, 8, 16, 32, 64, 128
; ---------------------------------------------------------------------------
; Utils_WriteSeed
;
; Read seed from EEPROM.
;
; IN:
; - R18:R19: seed to write
; OUT:
; REGS: R16, X, (R17)
Utils_WriteSeed:
push r15
in r15, SREG
cli
ldi xl, LOW(EEPROM_OFFS_SEED)
ldi xh, HIGH(EEPROM_OFFS_SEED)
mov r16, r18
rcall Utils_WriteEepromIncr ; (R17)
mov r16, r19
rcall Utils_WriteEepromIncr ; (R17)
pop r15
out SREG, r15
ret
UTILS_END:
.equ MODULE_SIZE_UTILS = UTILS_END-UTILS_BEGIN