BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
blitzhelp.h
Go to the documentation of this file.
1/*
2 * blitzhelp.h - small helpers shared by the C library ports (included by
3 * blitzlib.h; no need to include it separately).
4 *
5 * inlining BL_ALWAYS_INLINE
6 * register arithmetic BL_SETW BL_SETB BL_EXTL BL_SWAP bl_divu_w bl_divs_w
7 * bl_mulu_w bl_muls_w
8 * string copy BL_STRCOPY
9 * a2 argument stack BL_POPL BL_POPW
10 * custom chips BL_CUSTOM BL_CUSTOMW BL_CUSTOML BL_BLITBUSY
11 * BL_BLITWAIT bl_blitoff BL_INTENA BL_INTS_OFF
12 * BL_INTS_ON
13 * mathffp LVO_SPFix ... LVO_SPCeil (with BLITZ_SYSJSR1/2)
14 * system lib routines TOK_globalloc ... (for BLITZ_ALIBJSR),
15 * MEMBASE_localloc ... (memlib table, BLITZ_SYSJSR)
16 * command lib routines TOK_setin ... TOK_iffopen ... TOK_getpal ...
17 *
18 * Only what several libraries need; anything specific to one library
19 * stays in its source.
20 */
21#ifndef BLITZHELP_H
22#define BLITZHELP_H
23
24#include <exec/types.h> /* already there via blitzlib.h; for the IDE */
25
26/* ------------------------------------------------------------ inlining */
27
28/* BL_ALWAYS_INLINE - a static helper of a hot routine that GCC is to
29 inline: at -Os it keeps static helpers as functions with their
30 arguments on the stack and their state in memory (the blit core). */
31#define BL_ALWAYS_INLINE static inline __attribute__((always_inline))
32
33/* ------------------------------------------------- register arithmetic */
34
35/* Word/byte operations of the original change only the low word/byte of
36 the register; these keep the rest (r: an ULONG lvalue). */
37#define BL_SETW(r, v) ((r) = ((r) & 0xffff0000UL) | (UWORD)(v)) /* op.w */
38#define BL_SETB(r, v) ((r) = ((r) & 0xffffff00UL) | (UBYTE)(v)) /* op.b */
39#define BL_EXTL(r) ((ULONG)(LONG)(WORD)(r)) /* EXT.l */
40#define BL_SWAP(r) ((ULONG)(r) << 16 | (ULONG)(r) >> 16) /* SWAP */
41
42/* DIVU.w / DIVS.w as instructions: C has nothing with the same behaviour
43 - on overflow (quotient does not fit in 16 bits) the register stays
44 UNCHANGED, and a divisor of 0 raises the 68000 divide-by-zero exception
45 (vector 5) like the original. Result: remainder<<16 | quotient. */
46static inline ULONG bl_divu_w(ULONG d, UWORD s)
47{
48 __asm__("divu.w %1,%0" : "+d"(d) : "dmi"(s) : "cc");
49 return d;
50}
51
52static inline ULONG bl_divs_w(ULONG d, WORD s)
53{
54 __asm__("divs.w %1,%0" : "+d"(d) : "dmi"(s) : "cc");
55 return d;
56}
57
58/* MULU.w / MULS.w as instructions: 16 x 16 -> 32 bit. GCC sometimes
59 turns a product of two words into a __mulsi3 call on the 68000 (e.g.
60 after loop strength reduction), which a library cannot link. */
61static inline ULONG bl_mulu_w(UWORD a, UWORD b)
62{
63 ULONG r = a;
64
65 __asm__("mulu.w %1,%0" : "+d"(r) : "dmi"(b) : "cc");
66 return r;
67}
68
69static inline LONG bl_muls_w(WORD a, WORD b)
70{
71 LONG r = a;
72
73 __asm__("muls.w %1,%0" : "+d"(r) : "dmi"(b) : "cc");
74 return r;
75}
76
77/* -------------------------------------------------------- string copy */
78
79/* BL_STRCOPY(d, s, len) - copy len bytes from s to d, forward, and
80 advance both pointers past the copied bytes (no terminating 0): the
81 copy loops of strings1lib copstring and strings2lib lastring/astring.
82 d and s are pointer lvalues (blitz_string, a3, may be one of them: the
83 loops then work on a3 itself), len an ULONG variable (changed). From
84 8 bytes on, where both addresses have the same parity (and len is
85 below 64K), an odd first byte is copied alone and the rest by longs (a
86 MOVE.l (a0)+,(a1)+ DBF loop); otherwise by bytes, 8 per pass as in the
87 original (below 512K). The rest, and strings of 512K and more, by a
88 MOVE.b DBF loop (with an outer loop above 64K). The empty asm
89 statements keep GCC from rewriting the pointers into index registers
90 (it would then need more registers and save them). The result in
91 memory is the one of a byte loop, as the strings do not overlap. A
92 macro, not an inline function, so that a3 is not copied to a local
93 register; shaped for -Os: at -O2 GCC adds register copies to the
94 loops. */
95#define BL_STRCOPY_B_(d, s) do { \
96 *(d)++ = *(s)++; \
97 __asm__("" : "+a"(d), "+a"(s)); \
98 } while (0)
99#define BL_STRCOPY(d, s, len) do { \
100 UWORD n_; \
101 if ((len) >= 8) { \
102 if ((len) < 0x10000 && !(((ULONG)(s) - (ULONG)(d)) & 1)) { \
103 if ((ULONG)(d) & 1) { \
104 BL_STRCOPY_B_(d, s); \
105 (len)--; \
106 } \
107 n_ = (UWORD)(((len) >> 2) - 1); \
108 do { \
109 *(ULONG *)(d) = *(const ULONG *)(s); \
110 (d) += 4; \
111 (s) += 4; \
112 __asm__("" : "+a"(d), "+a"(s)); \
113 } while ((WORD)--n_ != -1); \
114 (len) &= 3; \
115 } else if ((len) < 0x80000) { \
116 n_ = (UWORD)(((len) >> 3) - 1); \
117 do { \
118 BL_STRCOPY_B_(d, s); BL_STRCOPY_B_(d, s); \
119 BL_STRCOPY_B_(d, s); BL_STRCOPY_B_(d, s); \
120 BL_STRCOPY_B_(d, s); BL_STRCOPY_B_(d, s); \
121 BL_STRCOPY_B_(d, s); BL_STRCOPY_B_(d, s); \
122 } while ((WORD)--n_ != -1); \
123 (len) &= 7; \
124 } \
125 } \
126 if ((len) < 8) { \
127 if (len) { \
128 n_ = (UWORD)((len) - 1); \
129 do \
130 BL_STRCOPY_B_(d, s); \
131 while ((WORD)--n_ != -1); \
132 } \
133 } else { \
134 ULONG hi_ = ((len) - 1) >> 16; \
135 n_ = (UWORD)((len) - 1); \
136 do { \
137 do \
138 BL_STRCOPY_B_(d, s); \
139 while ((WORD)--n_ != -1); \
140 } while (hi_-- != 0); \
141 } \
142} while (0)
143
144/* ------------------------------------------------- a2 argument stack */
145
146/* Arguments the compiler puts on its stack (string lengths as longs,
147 types as words) are read below a2: the routine takes a2 as register
148 parameter (`const UBYTE *args BL_REG(a2)`, or `const ULONG *lens`) and
149 pops through a local copy, the cursor `sp`; a2 itself stays unchanged
150 (see notes/cmdlibs-port.md "Allgemein"). The cursor may be a pointer
151 of any type.
152 BL_POPL(sp) MOVE.l -(a2),dN
153 BL_POPW(sp) MOVE.w -(a2),dN */
154#define BL_POPL(sp) \
155 (*(const ULONG *)((sp) = (const void *)((const UBYTE *)(sp) - 4)))
156#define BL_POPW(sp) \
157 (*(const UWORD *)((sp) = (const void *)((const UBYTE *)(sp) - 2)))
158
159/* ------------------------------------------------------- custom chips */
160
161/* Custom chip registers relative to a chip base (chipbaselib's value, a
162 register parameter, or BL_CUSTOM): BL_CUSTOMW(cb, 0x58) = BLTSIZE.
163 Accesses are volatile and keep the width of the original (MOVE.w /
164 MOVE.l - a long write sets two registers at once, e.g. BLTCON0/1). */
165#define BL_CUSTOM ((UBYTE *)0xdff000)
166#define BL_CUSTOMW(cb, o) (*(volatile UWORD *)((UBYTE *)(cb) + (o)))
167#define BL_CUSTOML(cb, o) (*(volatile ULONG *)((UBYTE *)(cb) + (o)))
168/* DMACONR BBUSY: BTST #6,$002(a1) (the byte at $002, bit 6 = bit 14) */
169#define BL_BLITBUSY(cb) (*(volatile UBYTE *)((UBYTE *)(cb) + 2) & 0x40)
170/* the usual blitter wait (libmacs `blitwait`): BTST once for the Agnus
171 bug, then loop while busy */
172#define BL_BLITWAIT(cb) do { (void)BL_BLITBUSY(cb); while (BL_BLITBUSY(cb)) ; } while (0)
173/* the same wait with the first read as TST.b 2(An), which needs no data
174 register; for a loop where BL_BLITWAIT's read into a register makes
175 GCC spill a value (a volatile asm: GCC keeps it in order with the
176 volatile chip accesses) */
177#define BL_BLITWAIT_TST(cb) do { \
178 __asm__ volatile("tst.b 2(%0)" : : "a"(cb) : "cc"); \
179 while (BL_BLITBUSY(cb)) ; \
180 } while (0)
181/* bl_blitoff(&bm->bpr, x, y) - the byte offset of pixel x, y in a plane
182 as the blit libraries compute it: MULS bpr,y : ASR.w #3,x : EXT.l x :
183 ADD.l x,y (only the low words of x and y count). Assembler because
184 GCC adds with LEA in an address register instead, and takes a6 for it
185 in an __entrypoint routine, which must keep a6. */
186BL_ALWAYS_INLINE ULONG bl_blitoff(const UWORD *bpr, WORD x, WORD y)
187{
188 ULONG off;
189
190 __asm__("muls.w %2,%0\n\tasr.w #3,%1\n\text.l %1\n\tadd.l %1,%0"
191 : "=d"(off), "+d"(x) : "m"(*bpr), "0"(y) : "cc");
192 return off;
193}
194/* INTENA: all interrupts off/on (MOVE #$4000/$C000,$DFF09A) */
195#define BL_INTENA (*(volatile UWORD *)0xdff09a)
196#define BL_INTS_OFF() (BL_INTENA = 0x4000)
197#define BL_INTS_ON() (BL_INTENA = 0xc000)
198
199/* ------------------------------------------------------------ mathffp */
200
201/* mathffp.library LVOs, for BLITZ_SYSJSR/BLITZ_SYSJSR1/2 with a6 = the
202 ffplib base (!libs #ffplib,#la6). (Not named SPFix ...: those are the
203 NDK's inline macros.) */
204#define LVO_SPFix (-30)
205#define LVO_SPFlt (-36)
206#define LVO_SPCmp (-42)
207#define LVO_SPTst (-48)
208#define LVO_SPAbs (-54)
209#define LVO_SPNeg (-60)
210#define LVO_SPAdd (-66)
211#define LVO_SPSub (-72)
212#define LVO_SPMul (-78)
213#define LVO_SPDiv (-84)
214#define LVO_SPFloor (-90)
215#define LVO_SPCeil (-96)
216
217/* ------------------------------------------- routines of system libs */
218
219/* Tokens of system library routines that command libraries call with
220 BLITZ_ALIBJSR (names = labels of the original sources). */
221#define TOK_localloc 0xc000 /* memlib: d0 = size, d1 = flags -> d0 = mem */
222#define TOK_locfree 0xc001 /* memlib: a1 = mem */
223#define TOK_globalloc 0xc002 /* memlib: like localloc, global list */
224#define TOK_globfree 0xc003 /* memlib: a1 = mem, global list */
225#define TOK_getvbr 0xc31d /* runerrlib: a0 = VBR */
226#define TOK_ctrlcend 0xc321 /* runerrlib: end after Ctrl-C, return code 20
227 (error check code only, #43) */
228#define TOK_endjmp 0xc800 /* exitslib: End */
229#define TOK_lmultiply 0xca01 /* lmullib: d0 = d0 * d1 */
230#define TOK_ldivide 0xcb01 /* ldivlib: d0 = d0 / d1 */
231#define TOK_strcomp2 0xce02 /* strcomplib */
232#define TOK_qutofl 0xd300 /* floatquicklib: Quick -> Float (a6 = ffplib,
233 BLITZ_ALIBJSR_A6) */
234#define TOK_fltoqu 0xd301 /* floatquicklib: Float -> Quick (a6 = ffplib) */
235#define TOK_setint 0xc100 /* intlib SetInt: d0 = level, d1 = code
236 (Blitz/Amiga variants: BLIBJSR/ALIBJSR) */
237#define TOK_clrint 0xc101 /* intlib ClrInt: d0 = level */
238#define TOK_vwait 0xc104 /* intlib VWait: d0 = frames */
239#define TOK_allocst 0xcf01 /* maxslib: d0 = source, d1 = length -> d0 = copy */
240#define TOK_freest 0xcf02 /* maxslib: d0 = copy (or 0) */
242/* Tokens of command library routines called by other command libraries:
243 (lib << 7) + number of the token in the lib */
244#define TOK_setin ((LIB_inputoutputlib << 7) + 3) /* inputoutputlib: input handler */
245#define TOK_setout ((LIB_inputoutputlib << 7) + 4) /* inputoutputlib: output handler */
246#define TOK_iogetsingle ((LIB_inputoutputlib << 7) + 5) /* -> d0 = character */
247#define TOK_iogetstring ((LIB_inputoutputlib << 7) + 6) /* a0 = buffer, d0 = max */
248#define TOK_iooutsingle ((LIB_inputoutputlib << 7) + 7) /* d0 = character */
249#define TOK_iooutstring1 ((LIB_inputoutputlib << 7) + 8) /* a0 = characters, d0 = length */
250#define TOK_iooutstring2 ((LIB_inputoutputlib << 7) + 9) /* a0 = string with 0 */
251#define TOK_ioctrlcout ((LIB_inputoutputlib << 7) + 12) /* Ctrl-C check of the output
252 (error check code only, #43) */
253#define TOK_ioctrlcin ((LIB_inputoutputlib << 7) + 13) /* ... of the input (#43) */
254#define TOK_ustr ((LIB_printlib << 7) + 6) /* printlib UStr$: d0 = value, a2 = type */
255#define TOK_val ((LIB_vallib << 7) + 1) /* vallib Val: d0 = string, a2 = length */
256#define TOK_exists ((LIB_fileiolib << 7) + 21) /* fileiolib Exists: d1 = name,
257 a6 = dos -> d0 = size */
258#define TOK_execversion ((LIB_environlib << 7) + 7) /* environlib: -> d0.w */
259#define TOK_setevfilter ((LIB_windowslib << 7) + 58) /* windowslib SetEventFilter */
260#define TOK_extbitmap ((LIB_bitmaplib << 7) + 4) /* bitmaplib: a3 = object,
261 d1-d3 = w, h, depth */
262#define TOK_getpal ((LIB_palettelib << 7) + 3) /* palettelib: a0 = name, a1 = palette */
263#define TOK_getpal2 ((LIB_palettelib << 7) + 4) /* palettelib: and d0 = offset */
264#define TOK_showsprite ((LIB_spriteslib << 7) + 2) /* spriteslib ShowSprite (Blitz):
265 d0-d3 = sprite, x, y, channel */
266/* rawkeylib */
267#define TOK_convertkey ((LIB_rawkeylib << 7) + 1) /* d0 = code, d1 = qualifier -> d0 */
268#define TOK_unconvert ((LIB_rawkeylib << 7) + 2) /* d0 = ascii -> d0 = code, d1 */
269#define TOK_qconvertkey ((LIB_rawkeylib << 7) + 3)
270/* iffiolib (Z flag = ok: BLITZ_ALIBJSR_CCR) */
271#define TOK_iffopen ((LIB_iffiolib << 7) + 1) /* a0 = name */
272#define TOK_iffclose ((LIB_iffiolib << 7) + 2)
273#define TOK_iffbyte ((LIB_iffiolib << 7) + 3) /* -> d0 */
274#define TOK_iffword ((LIB_iffiolib << 7) + 4) /* -> d0 */
275#define TOK_ifflong ((LIB_iffiolib << 7) + 5) /* -> d0 */
276#define TOK_ifffind ((LIB_iffiolib << 7) + 6) /* d0 = id -> d0 = length */
277#define TOK_iffskip ((LIB_iffiolib << 7) + 7) /* d0 = longs */
278#define TOK_iffread ((LIB_iffiolib << 7) + 8) /* a0 = buffer, d0 = bytes */
279/* ilbmifflib */
280#define TOK_ilbminit ((LIB_ilbmifflib << 7) + 1)
281#define TOK_ilbmgetbyte ((LIB_ilbmifflib << 7) + 2)
282#define TOK_ilbmbitmap ((LIB_ilbmifflib << 7) + 3) /* a0 = name, a1 = bitmap */
284/* memlib's init value (!libs #memlib,#la6): a table of BRA instructions,
285 called with BLITZ_SYSJSR(mem, MEMBASE_xxx, r) or BLITZ_SYSJSR2 (alloc),
286 same registers as the tokens above. */
287#define MEMBASE_localloc 0
288#define MEMBASE_locfree 4
289#define MEMBASE_globalloc 8
290#define MEMBASE_globfree 12
291
292#endif
ULONG x
Definition 2dlib.c:2491
WORD y
Definition 2dlib.c:285
static ULONG bl_mulu_w(UWORD a, UWORD b)
Definition blitzhelp.h:61
static LONG bl_muls_w(WORD a, WORD b)
Definition blitzhelp.h:69
#define BL_ALWAYS_INLINE
Definition blitzhelp.h:31
BL_ALWAYS_INLINE ULONG bl_blitoff(const UWORD *bpr, WORD x, WORD y)
Definition blitzhelp.h:186
static ULONG bl_divu_w(ULONG d, UWORD s)
Definition blitzhelp.h:46
static ULONG bl_divs_w(ULONG d, WORD s)
Definition blitzhelp.h:52
static struct @065163037313156332013005143365272036211065172003 d