Files
aqhomecontrol/avr/common/m_fixedbuffers.asm
2025-02-14 22:42:46 +01:00

140 lines
3.3 KiB
NASM

; ***************************************************************************
; copyright : (C) 2025 by Martin Preuss
; email : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome". *
; * Please see toplevel file COPYING of that project for license details. *
; ***************************************************************************
; ---------------------------------------------------------------------------
; @macro m_fixedbuf_init
;
; @param X pointer to start of buffers
; @param %0 constant maxBytes per buffer (including statusbyte in front)
; @param %1 constant maxBuffers
; @clobbers r16, r17, X
.macro m_fixedbuf_init
ldi r17, @1
clr r16
l_loop:
st X, r16 ; unused
adiw xh:xl,@0
dec r17
brne l_loop
.endmacro
; @end
; ---------------------------------------------------------------------------
; @macro m_fixedbuf_reserve
;
; @return CFLAG set if buffer available, cleared otherwise
; @return r16 buffer num
; @return X pointer to start of buffer
; @param X pointer to start of buffers
; @param %0 constant maxBytes per buffer (including statusbyte in front)
; @param %1 constant maxBuffers
; @clobbers r16, r17, X
.macro m_fixedbuf_reserve
clr r17 ; loop var: current buffer num
l_loop:
ld r16, X
tst r16
breq l_foundfree
adiw xh:xl, @0
inc r17
cpi r17, @1
brcs l_loop
rjmp l_end ; use cleared CFLAG
l_foundfree:
ldi r16, 0x80 ; mark as used
st X, r16
mov r16, r17
sec ; set CFLAG, return bufnum in r16, pointre in X
l_end:
.endmacro
; @end
; ---------------------------------------------------------------------------
; @macro m_fixedbuf_release
;
; @param X pointer to start of buffers
; @param %0 constant maxBytes per buffer (including statusbyte in front)
; @param %1 constant maxBuffers
; @clobbers r16
.macro m_fixedbuf_release
clr r16
st X, r16
.endmacro
; @end
; ---------------------------------------------------------------------------
; @macro m_fixedbuf_locate
;
; use this routine only if you can't easily calculate the offset of the buffer
; (e.g. by using shift operations for calculation)
;
; @return CFLAG set if buffer found, cleared otherwise
; @return X pointer to start of located buffer (points to the statusbyte in front)
; @param r16 buffer num
; @param X pointer to start of buffers
; @param %0 constant maxBytes per buffer
; @param %1 constant maxBuffers
; @clobbers r16, X
.macro m_fixedbuf_locate
cpi r16, @1
brcc l_end ; idx out of range, use cleared CFLAG
tst r16 ; doesn't change CFLAG which is set from CPI above
breq l_end ; buffer 0, return start of buffers
l_loop:
adiw xh:xl, @0
dec r16
brne l_loop
sec
l_end:
.endmacro
; @end
; ---------------------------------------------------------------------------
; @macro m_fixedbuf_count_used
;
; @return r16 number of buffers in use
; @param X pointer to start of buffers
; @param %0 constant maxBytes per buffer (including statusbyte in front)
; @param %1 constant maxBuffers
; @clobbers r16, r17, r18, X
.macro m_fixedbuf_count_used
clr r16
ldi r17, @1
l_loop:
ld r18, X
tst r18
breq l_notused
inc r16
l_notused:
adiw xh:xl, @0
dec r17
brne l_loop
l_end:
.endmacro
; @end