1/*	$OpenBSD: maskbits.h,v 1.3 2017/11/03 06:54:06 aoyama Exp $	*/
2/*	$NetBSD: maskbits.h,v 1.3 1997/03/31 07:37:28 scottr Exp $	*/
3
4/*-
5 * Copyright (c) 1994
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	@(#)maskbits.h	8.2 (Berkeley) 3/21/94
33 */
34
35#include <luna88k/dev/omrasops.h>	/* R(), W(), RR_* defines */
36
37/* This file is derived from OpenBSD:/usr/src/sys/arch/hp300/dev/maskbits.h */
38
39/*
40 * Derived from X11R4
41 */
42
43/* the following notes use the following conventions:
44SCREEN LEFT				SCREEN RIGHT
45in this file and maskbits.c, left and right refer to screen coordinates,
46NOT bit numbering in registers.
47
48rasops_lmask[n]
49	bits[0,n-1] = 0	bits[n,31] = 1
50rasops_rmask[n] =
51	bits[0,n-1] = 1	bits[n,31] = 0
52
53maskbits(x, w, startmask, endmask, nlw)
54	for a span of width w starting at position x, returns
55a mask for ragged bits at start, mask for ragged bits at end,
56and the number of whole longwords between the ends.
57
58*/
59
60#define maskbits(x, w, startmask, endmask, nlw)				\
61do {									\
62	startmask = rasops_lmask[(x) & 0x1f];				\
63	endmask = rasops_rmask[((x) + (w)) & 0x1f];			\
64	if (startmask)							\
65		nlw = (((w) - (32 - ((x) & 0x1f))) >> 5);		\
66	else								\
67		nlw = (w) >> 5;						\
68} while (0)
69
70/*
71 * On LUNA's frame buffer, MSB(bit 31) is displayed at most left hand of
72 * the screen.  This is different from rasops_masks.{c,h} assumption.
73 * So we use our own MBL(Move Bit Left)/MBR(Move Bit Right) macros to
74 * handle display memory images.
75 */
76
77#define OMFB_MBL(x,y)	((y) > 31 ? 0 : (x) << (y))
78#define OMFB_MBR(x,y)	((y) > 31 ? 0 : (x) >> (y))
79
80/*
81 * And, our private version of GETBITS/PUTBITS.
82 * XXX: We consider only 1 bpp for now.
83 */
84
85/* Get a number of bits ( <= 32 ) from *sp and store in dw */
86#define OMFB_GETBITS(sp, x, w, dw)					\
87do {									\
88	dw = OMFB_MBL(*(sp), (x));					\
89	if (((x) + (w)) > 32)						\
90		dw |= (OMFB_MBR(*(sp + 1), 32 - (x)));	 		\
91} while(0);
92
93/* Put a number of bits ( <= 32 ) from sw to *dp */
94#define OMFB_PUTBITS(sw, x, w, dp)					\
95do {									\
96	int n = (x) + (w) - 32;						\
97									\
98	if (n <= 0) {							\
99		n = rasops_pmask[x & 31][w & 31];			\
100		*(dp) = (*(dp) & ~n) | (OMFB_MBR(sw, x) & n);		\
101	} else {							\
102		*(dp) = (*(dp) & rasops_rmask[x])			\
103			| (OMFB_MBR(sw, x));				\
104		*(dp + 1) = (*(dp + 1) & rasops_lmask[n])		\
105			| (OMFB_MBL(sw, 32-(x)) & rasops_rmask[n]);	\
106	}								\
107} while(0);
108
109#define getandputrop(psrc, srcbit, dstbit, width, pdst, rop)		\
110do {									\
111	u_int32_t _tmpdst; 						\
112	if (rop == RR_CLEAR)						\
113		_tmpdst = 0;						\
114	else								\
115		OMFB_GETBITS(psrc, srcbit, width, _tmpdst);		\
116	OMFB_PUTBITS(_tmpdst, dstbit, width, pdst); 			\
117} while (0)
118
119#define getunalignedword(psrc, x, dst)					\
120do {									\
121	u_int32_t _tmp; 						\
122	OMFB_GETBITS(psrc, x, 32, _tmp);				\
123	dst = _tmp;							\
124} while (0)
125