-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.asm
120 lines (100 loc) · 2.52 KB
/
buffer.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
; file: buffer.asm target ATmega128L-4MHz-STK300
; purpose library, FIFO handling
; Modified: added macro CB_POP_PRESERVE to preserve the length and make the buffer trasversable multiple times
; Copyright 2023: Daniel Panero (342800), Yasmina Jemili (310507)
; === FIFO (First In First Out) ===
; CB (circular buffer)
.equ _in = 0
.equ _out = 1
.equ _nbr = 2
.equ _beg = 3
.macro CB_INIT ;buf
clr w
sts @0+_in ,w ; in-pointer = 0
sts @0+_out,w ; out-pointer = 0
sts @0+_nbr,w ; nbr of elems = 0
.endmacro
.macro CB_PUSH ;buf,len,elem
; in: a0 byte to push
; out: T 1=buffer full
lds w, @0+_nbr ; load nbr
cpi w, @1 ; compare with len
set
breq _end ; if nbr=len then T=1 (buffer full)
clt ; else T=0
inc w ; increment nbr
sts @0+_nbr,w ; store nbr
push xl ; push x on stack
push xh
lds w,@0+_in ; load in-pointer
mov xl,w
subi xl, low(-@0-_beg)
sbci xh,high(-@0-_beg) ; add in-pointer to buffer base
st x,@2 ; store new element in circular buffer
pop xh ; pop x from stack
pop xl
inc w ; incremenent in-pointer
cpi w,@1 ; if in=len then wrap around
brne PC+2
clr w
sts @0+_in,w ; store incremented in-pointer
_end:
.endmacro
.macro CB_POP ;buf,len,elem
; out: a0 byte to pop
; T 1=buffer empty
lds w,@0+_nbr ; load nbr
tst w
set
breq _end ; if nbr=0 then T=1 (buffer empty)
clt ; else T=0
dec w ; decrement nbr
sts @0+_nbr,w ; store nbr
push xl ; push x on stack
push xh
lds w,@0+_out ; load out-pointer
mov xl,w
subi xl, low(-@0-_beg)
sbci xh,high(-@0-_beg) ; add out-pointer to buffer base
ld @2,x ; take element from circular buffer
pop xh ; pop x from stack
pop xl
inc w ; increment out-pointer
cpi w,@1 ; if out=len then wrap around
brne PC+2
clr w
sts @0+_out,w ; store incremented out-pointer
_end:
.endmacro
.macro CB_POP_PRESERVE ;buf,len,elem
; out: a0 byte to pop
; T 1=buffer empty
lds w,@0+_nbr ; load nbr
tst w
set
breq _end ; if nbr=0 then T=1 (buffer empty)
clt ; else T=0
;dec w ; decrement nbr
;sts @0+_nbr,w ; store nbr
push xl ; push x on stack
push xh
lds w,@0+_out ; load out-pointer
mov xl,w
subi xl, low(-@0-_beg)
sbci xh,high(-@0-_beg) ; add out-pointer to buffer base
ld @2,x ; take element from circular buffer
pop xh ; pop x from stack
pop xl
inc w ; increment out-pointer
cpi w,@1 ; if out=len then wrap around
brne PC+3
clr w
set
lds _w, @0+_nbr
cp w,_w ; if out=len then wrap around
brne PC+3
clr w
set
sts @0+_out,w ; store incremented out-pointer
_end:
.endmacro