-
Notifications
You must be signed in to change notification settings - Fork 5
/
queues.c
250 lines (230 loc) · 5.01 KB
/
queues.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// Created by Vladimir Schneider on 2018-01-28.
//
/*
* Circular Link List Implementation
*/
#include "multitasker.h"
const uint16_t queueDescriptionSize = sizeof(QueueDescription);
void QInitNode(QNode *node)__naked
{
(void)node;
// node->next = node;
// node->prev = node;
// @formatter:off
__asm
ldw x,(1,sp)
; fallthrough to next function, assume function order is fixed by source
;jra __InitQNodeInX
__endasm;
// @formatter:on
}
/*
* X - node
* call from asm
*
* return
* Y = X, X unchanged
*/
void _InitQNodeInX()__naked
{
// @formatter:off
__asm
ldw y,x
ldw (QNEXT,x),y
ldw (QPREV,x),y
ret
__endasm;
// @formatter:on
}
/*
* X - node
* interrupts disabled
* return Y = X, X unchanged
*/
void _QNodeUnlinkInX()__naked
{
// __SAVE_DISABLE_INT
// if (node->next != node)
// {
// node->next->prev = node->prev;
// node->prev->next = node->next;
// node->next = node->prev = node;
// }
// __RESTORE_INT
// @formatter:off
__asm
ldw y,x
cpw x,(QPREV,y) ; see if already unlinked
jreq unlink.done
pushw x ; save node
ldw y,(QNEXT,y) ; y = node->next
ldw x,(QPREV,x) ; x = node->prevn
ldw (QPREV,y),x ; node->next->prev = node->prev
exgw x,y ; x = node->next
ldw y,(1,sp) ; y = node
ldw y,(QPREV,y) ; y = node->prev
ldw (QNEXT,y),x ; node->prev->next = node->next
popw x ; x = node
ldw y,x
ldw (QPREV,x),y
ldw (QNEXT,x),y
unlink.done:
ret
__endasm;
// @formatter:on
}
void QNodeUnlink(QNode *node)__naked
{
(void)node;
// __SAVE_DISABLE_INT
// if (node->next != node)
// {
// node->next->prev = node->prev;
// node->prev->next = node->next;
// node->next = node->prev = node;
// }
// __RESTORE_INT
// @formatter:off
__asm
push cc
sim
ldw x,(2,sp) ; get node sp: cc, x.h, x.l
callr __QNodeUnlinkInX
pop cc
ret
__endasm;
// @formatter:on
}
uint8_t QNodeIsEmpty(QNode *node)__naked
{
(void)node;
// uint8_t a;
// __SAVE_DISABLE_INT
// a = (uint8_t) (node->next == node);
// __RESTORE_INT
// return a;
// @formatter:off
__asm
push cc
sim
ldw x,(2,sp) ; get node sp: cc, x.h, x.l
ldw y,x
clr a
cpw y,(QNEXT,x)
jreq test.done
inc a
test.done:
pop cc
ret
__endasm;
// @formatter:on
}
/*
* X - node
* Y - other
* interrupts are disabled
* return Y is first unlinked, then linked previous to X
*/
void _QNodeLinkPrevInXY()__naked
{
// __SAVE_DISABLE_INT
// QNodeUnlink(other);
// other->prev = node->prev;
// node->prev->next = other
// other->next = node;
// node->prev = other;
// __RESTORE_INT
// @formatter:off
__asm
pushw x
exgw x,y
call __QNodeUnlinkInX
; x & y both other
ldw x,(1,sp) ; x = node
ldw (QNEXT,y),x ; other->next = node
ldw x,(QPREV,x) ; x = node->prev
ldw (QPREV,y),x ; other->prev = node->prev
ldw (QNEXT,x),y ; node->prev->next = other
popw x ; x = node
ldw (QPREV,x),y ; node->prev = other
ret
__endasm;
// @formatter:on
}
void QNodeLinkTail(QNode *queue, QNode *node) __naked {
(void) queue;
(void) node;
// __SYNONYM_FOR(QNodeLinkPrev)
// fallthrough to next function, assume function order is fixed by source
}
void QNodeLinkPrev(QNode *node, QNode *other)__naked
{
(void)node;
(void)other;
// @formatter:off
__asm
push cc
sim
ldw y,(4,sp) ; get other sp: cc, pc.h, pc.l, oth.h, oth.l, n.h, n.l
ldw x,(6,sp) ; get node
callr __QNodeLinkPrevInXY
pop cc
ret
__endasm;
// @formatter:on
}
/*
* X - node
* Y - other
* interrupts disabled
* return Y is first unlinked, then linked after X
*/
void _QNodeLinkNextInXY()
{
// __SAVE_DISABLE_INT
// QNodeUnlink(other);
// other->next = node->next;
// node->next->prev = other
// other->prev = node;
// node->next = other;
// __RESTORE_INT
// @formatter:off
__asm
pushw x
exgw x,y
call __QNodeUnlinkInX
; x & y both other
ldw x,(1,sp) ; x = node
ldw (QPREV,y),x ; other->prev = node
ldw x,(QNEXT,x) ; x = node->next
ldw (QNEXT,y),x ; other->next = node->next
ldw (QPREV,x),y ; node->next->prev = other
popw x ; x = node
ldw (QNEXT,x),y ; node->next = other
ret
__endasm;
// @formatter:on
}
void QNodeLinkHead(QNode *queue, QNode *node) __naked {
(void) queue;
(void) node;
// __SYNONYM_FOR(QNodeLinkNext)
// fallthrough to next function, assume function order is fixed by source
}
void QNodeLinkNext(QNode *node, QNode *other)
{
(void)node;
(void)other;
// @formatter:off
__asm
push cc
sim
ldw y,(4,sp) ; get other sp: cc, pc.h, pc.l, oth.h, oth.l, n.h, n.l
ldw x,(6,sp) ; get node
call __QNodeLinkNextInXY
pop cc
ret
__endasm;
// @formatter:on
}