Repository navigation
Expand file tree
/
Copy pathList.asm
More file actions
555 lines (489 loc) · 16.1 KB
/
Copy pathList.asm
File metadata and controls
555 lines (489 loc) · 16.1 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
;/------------------------------------------------------------------------------------------\
;| Link List System |
;|------------------------------------------------------------------------------------------|
;| PUBLIC FUNCTIONS: |
;| LIST_Init - Initialise the list header structure |
;| LIST_AddBefore - Add a node before another node in the list |
;| LIST_AddAfter - Add a node after another node in the list |
;| LIST_AddHead - Add a node at the head of the list |
;| LIST_AddTail - Add a node at the tail of the list |
;| LIST_Remove - Remove a node from the list it is in |
;| LIST_RemoveHead - Remove the head of the list |
;| LIST_RemoveTail - Remove the tail of the list |
;| LIST_GetHead - Get the head node of a list |
;| LIST_GetTail - Get the tail node of a list |
;| LIST_GetNext - Get the next node of a node |
;| LIST_GetPrev - Get the previous node of a node |
;| LIST_IsEndOfList - Checks if this is the end of the list |
;| |
;| PUBLIC MACROS: |
;| MACRO_LIST_GetHead - Get the head node of a list |
;| MACRO_LIST_GetTail - Get the tail node of a list |
;| MACRO_LIST_GetNext - Get the next node of a node |
;| MACRO_LIST_GetPrev - Get the previous node of a node |
;| MACRO_LIST_IsEndOfList - Checks if this is the end of the list |
;\------------------------------------------------------------------------------------------/
;--------------------------------------------------------------------------------------------
; Some configuration defines
LIST_DEBUG: EQU 1 ; Do some additional debug
;--------------------------------------------------------------------------------------------
; Defines for list header
LISTHDR: STRUCT
Head: rs 2
Tail: rs 2
TailPred: rs 2
ENDS
; Details of a list node
LISTNODE: STRUCT
Next: rs 2
Prev: rs 2
ENDS
;--------------------------------------------------------------------------------------------
; MACRO : Get Head of list
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List header
; OUTPUT:
; HL = Head Node
;--------------------------------------------------------------------------------------------
MACRO_LIST_GetHead: MACRO
ld l, (ix + LISTHDR.Head + 0)
ld h, (ix + LISTHDR.Head + 1)
ENDM
;--------------------------------------------------------------------------------------------
; MACRO : Get Tail of list
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List header
; OUTPUT:
; HL = Tail Node
;--------------------------------------------------------------------------------------------
MACRO_LIST_GetTail: MACRO
ld l, (ix + LISTHDR.TailPred + 0)
ld h, (ix + LISTHDR.TailPred + 1)
ENDM
;--------------------------------------------------------------------------------------------
; MACRO : Get Next node of the node
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = A List node
; OUTPUT:
; HL = Next Node
;--------------------------------------------------------------------------------------------
MACRO_LIST_GetNext: MACRO
ld a, (hl)
inc hl
ld l, (hl)
ld h, a
ENDM
;--------------------------------------------------------------------------------------------
; MACRO : Get Previous node of the node
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = A List node
; OUTPUT:
; HL = Prev Node
;--------------------------------------------------------------------------------------------
MACRO_LIST_GetPrev: MACRO
inc hl
inc hl
ld a, (hl)
inc hl
ld l, (hl)
ld h, a
ENDM
;--------------------------------------------------------------------------------------------
; MACRO : Checks if this is the end of the list or not
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = The current place in the list
; OUTPUT:
; Z Flag = Z Set if this was the end of the list else NZ
;--------------------------------------------------------------------------------------------
MACRO_LIST_IsEndOfList: MACRO
ld a, (hl)
inc hl
or (hl)
dec hl
ENDM
;--------------------------------------------------------------------------------------------
; Initialise a list header
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List header to initialise
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
LIST_Init:
; Get IX into HL
push ix
pop hl
; This is the TailPred address
ld (ix + LISTHDR.TailPred + 0), l
ld (ix + LISTHDR.TailPred + 1), h
; Move onto the tail address
inc hl
inc hl
; Set our head pointer
ld (ix + LISTHDR.Head + 0), l
ld (ix + LISTHDR.Head + 1), h
; Clear the tail pointer
xor a
ld (ix + LISTHDR.Tail + 0), a
ld (ix + LISTHDR.Tail + 1), a
ret
;--------------------------------------------------------------------------------------------
; Add a node before a given node
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = Node to add
; DE = Node to add before
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
LIST_AddBefore:
; Get the node before
ex de, hl
inc hl
inc hl
ld a, (hl)
inc hl
ld h, (hl)
ld l, a
ex de, hl
jp LIST_AddAfter
;--------------------------------------------------------------------------------------------
; Add a node after a given node
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = Node to add
; DE = Node to add after
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
LIST_AddAfter:
; Remember our node
push bc
push hl
; Get the next node of the node to add after and set the node to add to us
ex de, hl
ld c, (hl)
ld (hl), e
inc hl
ld b, (hl)
ld (hl), d
dec hl
ex de, hl
; our nodes next is the previous next
ld (hl), c
inc hl
ld (hl), b
inc hl
; Our previous is the previous
ld (hl), e
inc hl
ld (hl), d
pop hl
; The previous->next previous is us
inc bc
inc bc
ld a, l
ld (bc), a
inc bc
ld a, h
ld (bc), a
pop bc
ret
;--------------------------------------------------------------------------------------------
; Add a node to the head of a list
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List to add the node to the head of
; HL = Node to add
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
LIST_AddHead:
; Preserve the node
push hl
push de
; First get a copy ofthe node at the head of the list
ld e, (ix + LISTHDR.Head + 0)
ld d, (ix + LISTHDR.Head + 1)
; Store the our node as the head
ld (ix + LISTHDR.Head + 0), l
ld (ix + LISTHDR.Head + 1), h
; The item that was at the head is our next
push hl
ld (hl), e
inc hl
ld (hl), d
inc hl
; Our previous item is the list itself
ld a, ixl
ld (hl), a
inc hl
ld a, ixh
ld (hl), a
; Also whatever node was at the head - its previous points to us
pop hl
ex de, hl
inc hl
inc hl
ld (hl), e
inc hl
ld (hl), d
; We have done
pop de
pop hl
ret
;--------------------------------------------------------------------------------------------
; Add a node to the tail of a list
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List to add the node to
; HL = Node to add
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
LIST_AddTail:
; Preserve the node address and list
push hl
push de
push ix
; First get a copy of whats a before the tail
ld e, (ix + LISTHDR.TailPred + 0)
ld d, (ix + LISTHDR.TailPred + 1)
; Store the node to add as the tail pred
ld (ix + LISTHDR.TailPred + 0), l
ld (ix + LISTHDR.TailPred + 1), h
; We need to put the tail as the next node
inc ix
inc ix
; our next item is the tail
push hl
ld a, ixl
ld (hl), a
inc hl
ld a, ixh
ld (hl), a
inc hl
; Our previous item is what was previous
ld (hl), e
inc hl
ld (hl), d
; Also whatever node was at the tail - its next points to us
pop hl
ex de, hl
ld (hl), e
inc hl
ld (hl), d
; We have done
pop ix
pop de
pop hl
ret
;--------------------------------------------------------------------------------------------
; Remove an entry from the list
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = Node to remove
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
LIST_Remove:
push hl
push de
; Get our next node (in DE) and prev node (in HL)
ld e, (hl)
inc hl
ld d, (hl)
inc hl
ld a, (hl)
inc hl
ld h, (hl)
ld l, a
; Now set our previous node (HL) next to our next (DE)
ld (hl), e
inc hl
ld (hl), d
dec hl
; And our next node prev (DE) to our previous (HL)
ex de, hl
inc hl
inc hl
ld (hl), e
inc hl
ld (hl), d
pop de
pop hl
ret
;--------------------------------------------------------------------------------------------
; Remove an entry from the head of the list
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List to remove the head of
; OUTPUT:
; HL = Node removed (or Z Flag Set if empty)
;--------------------------------------------------------------------------------------------
LIST_RemoveHead:
push de
; First get a copy ofthe node at the head of the list
ld l, (ix + LISTHDR.Head + 0)
ld h, (ix + LISTHDR.Head + 1)
; Get whats next
ld e, (hl)
inc hl
ld d, (hl)
dec hl
; Check its not null (i.e. empty list)
ld a, d
or e
jr z, @LRH_Exit
; This becomes the new list head
ld (ix + LISTHDR.Head + 0), e
ld (ix + LISTHDR.Head + 1), d
; Now the list header becomes the next nodes previous
inc de
inc de
ld a, ixl
ld (de), a
inc de
ld a, ixh
ld (de), a
@LRH_Exit:
pop de
ret
;--------------------------------------------------------------------------------------------
; Remove an entry from the tail of the list
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List to remove the tail of
; OUTPUT:
; HL = Node removed (or Z Flag Set if empty)
;--------------------------------------------------------------------------------------------
LIST_RemoveTail:
push de
; First get a copy ofthe node before the tail
ld l, (ix + LISTHDR.TailPred + 0)
ld h, (ix + LISTHDR.TailPred + 1)
; Get its previous
inc hl
inc hl
ld e, (hl)
inc hl
ld d, (hl)
; Check its not null (i.e. empty list)
ld a, d
or e
jr z, @LRT_Exit
; Set our previous as the tailpred
ld (ix + LISTHDR.TailPred + 0), e
ld (ix + LISTHDR.TailPred + 1), d
; Now the list header becomes the previous nodes next
ld a, ixl
ld (de), a
inc de
ld a, ixh
ld (de), a
; Done - just sort the return
dec hl
dec hl
dec hl
@LRT_Exit:
pop de
ret
;--------------------------------------------------------------------------------------------
; Add the head of a list
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List to add the node to
; OUTPUT:
; HL = Head node
;--------------------------------------------------------------------------------------------
LIST_GetHead:
; Get the head node
ld l, (ix + LISTHDR.Head + 0)
ld h, (ix + LISTHDR.Head + 1)
ret
;--------------------------------------------------------------------------------------------
; Add the tail of a list
;--------------------------------------------------------------------------------------------
; INPUT:
; IX = List to add the node to
; OUTPUT:
; HL = Tail node
;--------------------------------------------------------------------------------------------
LIST_GetTail:
; Get the tail node
ld l, (ix + LISTHDR.Tail + 0)
ld h, (ix + LISTHDR.Tail + 1)
ret
;--------------------------------------------------------------------------------------------
; Add the next node of a node
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = The node to get the next node of
; OUTPUT:
; HL = Next node
;--------------------------------------------------------------------------------------------
LIST_GetNext:
; Get the next node
ld a, (hl)
inc hl
ld h, (hl)
ld l, a
ret
;--------------------------------------------------------------------------------------------
; Add the previous node of a node
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = The node to get the previous node of
; OUTPUT:
; HL = Previous node
;--------------------------------------------------------------------------------------------
LIST_GetPrev:
; Get the previous node
inc hl
inc hl
ld a, (hl)
inc hl
ld h, (hl)
ld l, a
ret
;--------------------------------------------------------------------------------------------
; Checks if this is the end of the list or not
;--------------------------------------------------------------------------------------------
; INPUT:
; HL = The current place in the list
; OUTPUT:
; DE = The next node if there is one
; Z Flag = Z Set if this was the end of the list else NZ
;--------------------------------------------------------------------------------------------
LIST_IsEndOfList:
; Get the next node
ld a, (hl)
inc hl
or (hl)
dec hl
ret
;--------------------------------------------------------------------------------------------
; INTERNAL: Called when there is an error
;--------------------------------------------------------------------------------------------
; INPUT:
; None
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
if LIST_DEBUG
_LIST_InternalError:
ld a, r
and 7
out (254), a
jr _LIST_InternalError
ret
endif
;--------------------------------------------------------------------------------------------
; Variables
;--------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------