-
Notifications
You must be signed in to change notification settings - Fork 2
/
array.m4
454 lines (428 loc) · 15 KB
/
array.m4
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
dnl ## Array
dnl
dnl
dnl
define({ARRAY_SET},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_SET},{array_set},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_SET},{dnl
__{}define({__INFO},{array_set}){}dnl
ld IX, __FORM({%-11s},$1); 4:14 array_set ( -- )}){}dnl
dnl
dnl
define({ARRAY_INC},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_INC},{array_inc},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_INC},{dnl
__{}define({__INFO},{array_inc}){}dnl
inc IX ; 2:10 array_inc ( -- )}){}dnl
dnl
dnl
define({ARRAY_DEC},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_DEC},{array_dec},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_DEC},{dnl
__{}define({__INFO},{array_dec}){}dnl
dec IX ; 2:10 array_dec ( -- )}){}dnl
dnl
dnl
define({ARRAY_ADD},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_ADD},{array_add},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_ADD},{dnl
__{}define({__INFO},{array_add}){}dnl
ex DE, HL ; 1:4 array_add($1) ( x -- )
add IX, DE ; 2:15 array_add($1)
pop DE ; 1:11 array_add($1)}){}dnl
dnl
dnl
define({PUSH_ARRAY_ADD},{dnl
__{}__ADD_TOKEN({__TOKEN_PUSH_ARRAY_ADD},{$1 array add},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_PUSH_ARRAY_ADD},{dnl
__{}define({__INFO},__COMPILE_INFO){}dnl
__{}ifelse(__HAS_PTR($1),1,{
ld BC,format({%-12s},$1); 4:20 __INFO ( -- )
add IX, BC ; 2:15 __INFO},
__{}{
ld BC, __FORM({%-11s},$1); 3:10 __INFO ( -- )
add IX, BC ; 2:15 __INFO}){}dnl
}){}dnl
dnl
dnl
dnl
define({ARRAY_FETCH},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_FETCH},{array_fetch},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_FETCH},{dnl
__{}define({__INFO},{array_fetch}){}dnl
;[8:53] array_fetch ( -- (word) array[$1] )
push DE ; 1:11 array_fetch
ld D,format({%-12s},[IX+$1+1]); 3:19 array_fetch
ld E,format({%-12s},[IX+$1]); 3:19 array_fetch
ex DE, HL ; 1:4 array_fetch}){}dnl
dnl
dnl
define({DUP_ARRAY_FETCH},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_FETCH},{dup_array_fetch},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_FETCH},{dnl
__{}define({__INFO},{dup_array_fetch}){}dnl
;[9:64] dup_array_fetch ( x1 -- x1 x1 (word) array[$1] )
push DE ; 1:11 dup_array_fetch
push HL ; 1:11 dup_array_fetch
ld D,format({%-12s},[IX+$1+1]); 3:19 dup_array_fetch
ld E,format({%-12s},[IX+$1]); 3:19 dup_array_fetch
ex DE, HL ; 1:4 dup_array_fetch}){}dnl
dnl
dnl
define({ARRAY_FETCH_ADD},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_FETCH_ADD},{array_fetch_add},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_FETCH_ADD},{dnl
__{}define({__INFO},{array_fetch_add}){}dnl
;[7:49] array_fetch_add
ld B,format({%-12s},[IX+$1+1]); 3:19 array_fetch_add
ld C,format({%-12s},[IX+$1]); 3:19 array_fetch_add
add HL, BC ; 1:11 array_fetch_add {TOS} += (word) array[eval($1)]}){}dnl
dnl
dnl ;[11:52]
dnl ld B, IXh ; 2:8
dnl ld C, IXl ; 2:8
dnl ld A,[BC] ; 1:7
dnl add A, L ; 1:4
dnl ld L, A ; 1:4
dnl inc BC ; 1:6
dnl ld A,[BC] ; 1:7
dnl adc A, H ; 1:4
dnl ld H, A ; 1:4
dnl
dnl ;[8:75]
dnl push IX ; 2:15
dnl ex [SP],HL ; 1:19
dnl ld C,[HL] ; 1:7
dnl inc HL ; 1:6
dnl ld B,[HL] ; 1:7
dnl pop HL ; 1:10
dnl add HL, BC ; 1:11
dnl
dnl
dnl
dnl ( x -- )
define({ARRAY_STORE},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_STORE},{array_store},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_STORE},{dnl
__{}define({__INFO},{array_store}){}dnl
;[8:52] array_store($1) ( x -- )
ex DE, HL ; 1:4 array_store($1)
ld format({%-16s},[IX+$1+1]{,} D); 3:19 array_store($1)
ld format({%-16s},[IX+$1]{,} E); 3:19 array_store($1) array[eval($1)] = word
pop DE ; 1:10 array_store($1)}){}dnl
dnl
dnl
dnl
dnl
dnl ---------------------------------------------------------------------------
dnl ## 8bit Arithmetic
dnl ---------------------------------------------------------------------------
dnl
dnl The Z80 only supports a constant index for reading from the array:
dnl
dnl ld A,(IX+constant)
dnl
dnl It goes to load the value lying for example on the next row, because the size of the table is constant at the time of compilation.
dnl But if the size of the table depends on the input, then we need:
dnl
dnl ld A,(IX+variable)
dnl
dnl Where variable is a constant after getting the size of the table from the input.
dnl That's why I added another optional parameter for the word to enter the address name where variable is stored.
dnl So it goes to refer to it and change its value at runtime.
dnl
dnl
define({ARRAY_CFETCH},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_CFETCH},{array_cfetch},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_CFETCH},{dnl
__{}define({__INFO},{array_cfetch}){}dnl
push DE ; 1:11 array_cfetch ( -- char_array[$1] )
ld D, 0x00 ; 2:7 array_cfetch
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld E,format({%-12s},[IX+$1]); 3:19 array_cfetch
ex DE, HL ; 1:4 array_cfetch}){}dnl
dnl
dnl
dnl push DE ; 1:11
dnl ex DE, HL ; 1:4
dnl ld HL,[adr] ; 3:16
dnl ld L,[HL] ; 1:7
dnl ld H, 0x00 ; 2:7
dnl
dnl
define({ARRAY_LO_FETCH},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_LO_FETCH},{array_lo_fetch},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_LO_FETCH},{dnl
__{}define({__INFO},{array_lo_fetch}){}dnl
push DE ; 1:11 array_lo_fetch ( -- char_array[$1] )
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld E,format({%-12s},[IX+$1]); 3:19 array_lo_fetch
ex DE, HL ; 1:4 array_lo_fetch}){}dnl
dnl
dnl
define({ARRAY_HI_FETCH},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_HI_FETCH},{array_hi_fetch},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_HI_FETCH},{dnl
__{}define({__INFO},{array_hi_fetch}){}dnl
push DE ; 1:11 array_hi_fetch ( -- char_array[$1] )
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld D,format({%-12s},[IX+$1]); 3:19 array_hi_fetch
ex DE, HL ; 1:4 array_hi_fetch}){}dnl
dnl
dnl
define({DUP_ARRAY_CFETCH},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_CFETCH},{dup_array_cfetch},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_CFETCH},{dnl
__{}define({__INFO},{dup_array_cfetch}){}dnl
push DE ; 1:11 dup_array_cfetch ( a -- a a char_array[$1] )
push HL ; 1:11 dup_array_cfetch
ld D, 0x00 ; 2:7 dup_array_cfetch
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld E,format({%-12s},[IX+$1]); 3:19 dup_array_cfetch
ex DE, HL ; 1:4 dup_array_cfetch}){}dnl
dnl
dnl
define({DUP_ARRAY_CFETCH_EQ},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_CFETCH_EQ},{dup_array_cfetch_eq},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_CFETCH_EQ},{dnl
__{}define({__INFO},{dup_array_cfetch_eq}){}dnl
push DE ; 1:11 dup_array_cfetch_eq ( char1 -- char1 flag(char1 == char_array[$1]) )
ex DE, HL ; 1:4 dup_array_cfetch_eq
ld A, E ; 1:4 dup_array_cfetch_eq
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
xor format({%-15s},[IX+$1]); 3:19 dup_array_cfetch_eq
sub 0x01 ; 2:7 dup_array_cfetch_eq
sbc HL, HL ; 2:15 dup_array_cfetch_eq}){}dnl
dnl
dnl
define({DUP_ARRAY_CFETCH_NE},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_CFETCH_NE},{dup_array_cfetch_ne},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_CFETCH_NE},{dnl
__{}define({__INFO},{dup_array_cfetch_ne}){}dnl
push DE ; 1:11 dup_array_cfetch_ne ( char1 -- char1 flag(char1 <> char_array[$1]) )
ex DE, HL ; 1:4 dup_array_cfetch_ne
ld A, E ; 1:4 dup_array_cfetch_ne
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
xor format({%-15s},[IX+$1]); 3:19 dup_array_cfetch_ne
add A, 0xFF ; 2:7 dup_array_cfetch_ne
sbc HL, HL ; 2:15 dup_array_cfetch_ne}){}dnl
dnl
dnl
define({DUP_ARRAY_CFETCH_ULT},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_CFETCH_ULT},{dup_array_cfetch_ult},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_CFETCH_ULT},{dnl
__{}define({__INFO},{dup_array_cfetch_ult}){}dnl
push DE ; 1:11 dup_array_cfetch_ult ( char1 -- char1 flag(char1 (U)< char_array[$1]) )
ex DE, HL ; 1:4 dup_array_cfetch_ult
ld A, E ; 1:4 dup_array_cfetch_ult
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
sub format({%-15s},[IX+$1]); 3:19 dup_array_cfetch_ult
sbc HL, HL ; 2:15 dup_array_cfetch_ult}){}dnl
dnl
dnl
define({DUP_ARRAY_CFETCH_ULE},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_CFETCH_ULE},{dup_array_cfetch_ule},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_CFETCH_ULE},{dnl
__{}define({__INFO},{dup_array_cfetch_ule}){}dnl
push DE ; 1:11 dup_array_cfetch_ule ( char1 -- char1 flag(char1 (U)<= char_array[$1]) )
ex DE, HL ; 1:4 dup_array_cfetch_ule
scf ; 1:4 dup_array_cfetch_ule
ld A, E ; 1:4 dup_array_cfetch_ule
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
sbc A,format({%-12s},[IX+$1]); 3:19 dup_array_cfetch_ule
sbc HL, HL ; 2:15 dup_array_cfetch_ule}){}dnl
dnl
dnl
define({DUP_ARRAY_CFETCH_UGT},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_CFETCH_UGT},{dup_array_cfetch_ugt},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_CFETCH_UGT},{dnl
__{}define({__INFO},{dup_array_cfetch_ugt}){}dnl
push DE ; 1:11 dup_array_cfetch_ugt ( char1 -- char1 flag(char1 (U)> char_array[$1]) )
ex DE, HL ; 1:4 dup_array_cfetch_ugt
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld A,format({%-12s},[IX+$1]); 3:19 dup_array_cfetch_ugt
sub E ; 1:4 dup_array_cfetch_ugt
sbc HL, HL ; 2:15 dup_array_cfetch_ugt}){}dnl
dnl
dnl
define({DUP_ARRAY_CFETCH_UGE},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_CFETCH_UGE},{dup_array_cfetch_uge},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_CFETCH_UGE},{dnl
__{}define({__INFO},{dup_array_cfetch_uge}){}dnl
push DE ; 1:11 dup_array_cfetch_uge ( char1 -- char1 flag(char1 (U)>= char_array[$1]) )
ex DE, HL ; 1:4 dup_array_cfetch_uge
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld A,format({%-12s},[IX+$1]); 3:19 dup_array_cfetch_uge
scf ; 1:4 dup_array_cfetch_uge
sbc A, E ; 1:4 dup_array_cfetch_uge
sbc HL, HL ; 2:15 dup_array_cfetch_uge}){}dnl
dnl
dnl
define({DUP_ARRAY_LO_FETCH},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_LO_FETCH},{dup_array_lo_fetch},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_LO_FETCH},{dnl
__{}define({__INFO},{dup_array_lo_fetch}){}dnl
push DE ; 1:11 dup_array_lo_fetch ( a -- a char_array[$1] )
push HL ; 1:11 dup_array_lo_fetch
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld E,format({%-12s},[IX+$1]); 3:19 dup_array_lo_fetch
ex DE, HL ; 1:4 dup_array_lo_fetch}){}dnl
dnl
dnl
define({DUP_ARRAY_HI_FETCH},{dnl
__{}__ADD_TOKEN({__TOKEN_DUP_ARRAY_HI_FETCH},{dup_array_hi_fetch},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_DUP_ARRAY_HI_FETCH},{dnl
__{}define({__INFO},{dup_array_hi_fetch}){}dnl
push DE ; 1:11 dup_array_hi_fetch ( a -- a char_array[$1] )
push HL ; 1:11 dup_array_hi_fetch
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld D,format({%-12s},[IX+$1]); 3:19 dup_array_hi_fetch
ex DE, HL ; 1:4 dup_array_hi_fetch}){}dnl
dnl
dnl
define({ARRAY_CFETCH_ADD},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_CFETCH_ADD},{array_cfetch_add},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_CFETCH_ADD},{dnl
__{}define({__INFO},{array_cfetch_add}){}dnl
ld B, 0x00 ; 2:7 array_cfetch_add
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld C,format({%-12s},[IX+$1]); 3:19 array_cfetch_add
add HL, BC ; 1:11 array_cfetch_add {TOS} += char_array[eval($1)]}){}dnl
dnl
dnl
define({ARRAY_LO_FETCH_ADD},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_LO_FETCH_ADD},{array_lo_fetch_add},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_LO_FETCH_ADD},{dnl
__{}define({__INFO},{array_lo_fetch_add}){}dnl
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld A,format({%-12s},[IX+$1]); 3:19 array_lo_fetch_add
add A, L ; 1:4 array_lo_fetch_add
ld L, A ; 1:4 array_lo_fetch_add {lo(TOS)} += char_array[eval($1)]}){}dnl
dnl
dnl
define({ARRAY_HI_FETCH_ADD},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_HI_FETCH_ADD},{array_hi_fetch_add},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_HI_FETCH_ADD},{dnl
__{}define({__INFO},{array_hi_fetch_add}){}dnl
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld A,format({%-12s},[IX+$1]); 3:19 array_hi_fetch_add
add A, H ; 1:4 array_hi_fetch_add
ld H, A ; 1:4 array_hi_fetch_add {hi(TOS)} += char_array[eval($1)]}){}dnl
dnl
dnl
define({ARRAY_CSTORE},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_CSTORE},{array_cstore},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_CSTORE},{dnl
__{}define({__INFO},{array_cstore}){}dnl
ex DE, HL ; 1:4 array_cstore($1) ( char -- )
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld format({%-16s},[IX+$1]{,} E); 3:19 array_cstore($1) char_array[eval($1)] = char
pop DE ; 1:10 array_cstore($1)}){}dnl
dnl
dnl
define({ARRAY_LO_STORE},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_LO_STORE},{array_lo_store},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_LO_STORE},{dnl
__{}define({__INFO},{array_lo_store}){}dnl
ex DE, HL ; 1:4 array_lo_store($1) ( char -- )
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld format({%-16s},[IX+$1]{,} E); 3:19 array_lo_store($1) char_array[eval($1)] = lo({TOS})
pop DE ; 1:10 array_lo_store($1)}){}dnl
dnl
dnl
define({ARRAY_HI_STORE},{dnl
__{}__ADD_TOKEN({__TOKEN_ARRAY_HI_STORE},{array_hi_store},$@){}dnl
}){}dnl
dnl
define({__ASM_TOKEN_ARRAY_HI_STORE},{dnl
__{}define({__INFO},{array_hi_store}){}dnl
ex DE, HL ; 1:4 array_hi_store($1) ( char -- )
__{}ifelse($2,{},{},{$2 EQU $+2
})dnl
ld format({%-16s},[IX+$1]{,} E); 3:19 array_hi_store($1) char_array[eval($1)] = hi({TOS})
pop DE ; 1:10 array_hi_store($1)}){}dnl
dnl
dnl
dnl
dnl ---------------------------------------------------------------------------
dnl ## 16bit & 32bit mix Arithmetic
dnl ---------------------------------------------------------------------------
dnl
dnl
dnl
dnl ---------------------------------------------------------------------------
dnl ## 32bit Arithmetic
dnl ---------------------------------------------------------------------------
dnl
dnl
dnl
dnl