shithub: 9ferno

Download patch

ref: e8c5c6689fe0924c2a285910fa2cf6af02302023
parent: e1fc0ec55d4e898d2decb8d8e61e5a6d5c8dc52a
author: joe9 <joe9mail@gmail.com>
date: Sat Jul 3 00:27:39 EDT 2021

fixed bug in byteaddr on negative x or y values

--- a/emu/port/devdraw.c
+++ b/emu/port/devdraw.c
@@ -27,8 +27,8 @@
  */
 #define	QSHIFT	4	/* location in qid of client # */
 
-#define	QID(q)		((((ulong)(q).path)&0x0000000F)>>0)
-#define	CLIENTPATH(q)	((((ulong)q)&0x7FFFFFF0)>>QSHIFT)
+#define	QID(q)		((((u32int)(q).path)&0x0000000F)>>0)
+#define	CLIENTPATH(q)	((((u32int)q)&0x7FFFFFF0)>>QSHIFT)
 #define	CLIENT(q)	CLIENTPATH((q).path)
 
 #define	NHASH		(1<<5)
@@ -45,21 +45,21 @@
 typedef struct Refx Refx;
 typedef struct DName DName;
 
-ulong blanktime = 30;	/* in minutes; a half hour */
+u32int blanktime = 30;	/* in minutes; a half hour */
 
 struct Draw
 {
 	QLock	q;
-	int		clientid;
-	int		nclient;
+	s32int		clientid;
+	s32int		nclient;
 	Client**	client;
-	int		nname;
+	s32int		nname;
 	DName*	name;
-	int		vers;
-	int		softscreen;
-	int		blanked;	/* screen turned off */
-	ulong		blanktime;	/* time of last operation */
-	ulong		savemap[3*256];
+	s32int		vers;
+	s32int		softscreen;
+	s32int		blanked;	/* screen turned off */
+	u32int		blanktime;	/* time of last operation */
+	u32int		savemap[3*256];
 };
 
 struct Client
@@ -70,13 +70,13 @@
 	Refresh*	refresh;
 	Rendez		refrend;
 	uchar*		readdata;
-	int		nreaddata;
-	int		busy;
-	int		clientid;
-	int		slot;
-	int		refreshme;
-	int		infoid;
-	int	op;	/* compositing operator - SoverD by default */
+	s32int		nreaddata;
+	s32int		busy;
+	s32int		clientid;
+	s32int		slot;
+	s32int		refreshme;
+	s32int		infoid;
+	s32int	op;	/* compositing operator - SoverD by default */
 };
 
 struct Refresh
@@ -97,13 +97,13 @@
 	char			*name;
 	Client	*client;
 	DImage*		dimage;
-	int			vers;
+	s32int			vers;
 };
 
 struct FChar
 {
-	int		minx;	/* left edge of bits */
-	int		maxx;	/* right edge of bits */
+	s32int		minx;	/* left edge of bits */
+	s32int		maxx;	/* right edge of bits */
 	uchar		miny;	/* first non-zero scan-line */
 	uchar		maxy;	/* last non-zero scan-line + 1 */
 	schar		left;	/* offset of baseline */
@@ -118,13 +118,13 @@
  */
 struct DImage
 {
-	int		id;
-	int		ref;
+	s32int		id;
+	s32int		ref;
 	char		*name;
-	int		vers;
+	s32int		vers;
 	Memimage*	image;
-	int		ascent;
-	int		nfchar;
+	s32int		ascent;
+	s32int		nfchar;
 	FChar*		fchar;
 	DScreen*	dscreen;	/* 0 if not a window */
 	DImage*	fromname;	/* image this one is derived from, by name */
@@ -139,9 +139,9 @@
 
 struct DScreen
 {
-	int		id;
-	int		public;
-	int		ref;
+	s32int		id;
+	s32int		public;
+	s32int		ref;
 	DImage	*dimage;
 	DImage	*dfill;
 	Memscreen*	screen;
@@ -810,9 +810,11 @@
 {
 	DImage *d;
 
-	d = drawlookup(client, BGLONG(a), 1);
-	if(d == nil)
+	d = drawlookup(client, BG32INT(a), 1);
+	if(d == nil){
+		iprint("drawimage error\n");
 		error(Enodrawimage);
+	}
 	return d->image;
 }
 
@@ -819,17 +821,19 @@
 void
 drawrectangle(Rectangle *r, uchar *a)
 {
-	r->min.x = BGLONG(a+0*4);
-	r->min.y = BGLONG(a+1*4);
-	r->max.x = BGLONG(a+2*4);
-	r->max.y = BGLONG(a+3*4);
+	r->min.x = BG32INT(a+0*4);
+	r->min.y = BG32INT(a+1*4);
+	r->max.x = BG32INT(a+2*4);
+	r->max.y = BG32INT(a+3*4);
+	// iprint("drawrectangle %R\n", *r);
 }
 
 void
 drawpoint(Point *p, uchar *a)
 {
-	p->x = BGLONG(a+0*4);
-	p->y = BGLONG(a+1*4);
+	p->x = BG32INT(a+0*4);
+	p->y = BG32INT(a+1*4);
+	// iprint("drawpoint %P\n", *p);
 }
 
 Point
@@ -864,7 +868,7 @@
 
 	memimageinit();
 	screendata.base = nil;
-	screendata.bdata = attachscreen(&r, &chan, &depth, &width, &sdraw.softscreen);
+	screendata.bdata = (uchar*)attachscreen(&r, &chan, &depth, &width, &sdraw.softscreen);
 	if(screendata.bdata == nil)
 		return 0;
 	screendata.ref = 1;
@@ -1030,6 +1034,16 @@
 	char buf[16];
 
 	USED(offset);
+	SET(red);
+	SET(green);
+	SET(blue);
+	SET(m);
+	SET(index);
+	USED(red);
+	USED(green);
+	USED(blue);
+	USED(m);
+	USED(index);
 	if(c->qid.type & QTDIR)
 		return devdirread(c, a, n, 0, 0, drawgen);
 	cl = drawclient(c);
@@ -1107,11 +1121,11 @@
 		p = a;
 		while(cl->refresh && n>=5*4){
 			r = cl->refresh;
-			BPLONG(p+0*4, r->dimage->id);
-			BPLONG(p+1*4, r->r.min.x);
-			BPLONG(p+2*4, r->r.min.y);
-			BPLONG(p+3*4, r->r.max.x);
-			BPLONG(p+4*4, r->r.max.y);
+			BP32INT(p+0*4, r->dimage->id);
+			BP32INT(p+1*4, r->r.min.x);
+			BP32INT(p+2*4, r->r.min.y);
+			BP32INT(p+3*4, r->r.max.x);
+			BP32INT(p+4*4, r->r.max.y);
 			cl->refresh = r->next;
 			free(r);
 			p += 5*4;
@@ -1147,6 +1161,24 @@
 	ulong offset = off;
 
 	USED(offset);
+	SET(red);
+	SET(green);
+	SET(blue);
+	SET(m);
+	SET(i);
+	SET(x);
+	SET(q);
+	SET(fields);
+	SET(buf);
+	USED(red);
+	USED(green);
+	USED(blue);
+	USED(m);
+	USED(i);
+	USED(x);
+	USED(q);
+	USED(fields);
+	USED(buf);
 	if(c->qid.type & QTDIR)
 		error(Eisdir);
 	cl = drawclient(c);
@@ -1160,7 +1192,7 @@
 	case Qctl:
 		if(n != 4)
 			error("unknown draw control request");
-		cl->infoid = BGLONG((uchar*)a);
+		cl->infoid = BG32INT((uchar*)a);
 		break;
 
 	case Qcolormap:
@@ -1216,7 +1248,7 @@
 }
 
 uchar*
-drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
+drawcoord(uchar *p, uchar *maxp, s32int oldx, s32int *newx)
 {
 	int b, x;
 
@@ -1257,19 +1289,19 @@
 	for(p=fmt; *p; p++){
 		switch(*p){
 		case 'l':
-			q += sprint(q, " %ld", (long)BGLONG(a));
+			q += sprint(q, " %ld", (long)BG32INT(a));
 			a += 4;
 			break;
 		case 'L':
-			q += sprint(q, " %.8lux", (ulong)BGLONG(a));
+			q += sprint(q, " %.8lux", (ulong)BG32INT(a));
 			a += 4;
 			break;
 		case 'R':
-			q += sprint(q, " [%d %d %d %d]", BGLONG(a), BGLONG(a+4), BGLONG(a+8), BGLONG(a+12));
+			q += sprint(q, " [%d %d %d %d]", BG32INT(a), BG32INT(a+4), BG32INT(a+8), BG32INT(a+12));
 			a += 16;
 			break;
 		case 'P':
-			q += sprint(q, " [%d %d]", BGLONG(a), BGLONG(a+4));
+			q += sprint(q, " [%d %d]", BG32INT(a), BG32INT(a+4));
 			a += 8;
 			break;
 		case 'b':
@@ -1276,11 +1308,11 @@
 			q += sprint(q, " %d", *a++);
 			break;
 		case 's':
-			q += sprint(q, " %d", BGSHORT(a));
+			q += sprint(q, " %d", BG16INT(a));
 			a += 2;
 			break;
 		case 'S':
-			q += sprint(q, " %.4ux", BGSHORT(a));
+			q += sprint(q, " %.4ux", BG16INT(a));
 			a += 2;
 			break;
 		}
@@ -1293,10 +1325,10 @@
 void
 drawmesg(Client *client, void *av, int n)
 {
-	int c, op, repl, m, y, dstid, scrnid, ni, ci, j, nw, e0, e1, ox, oy, esize, oesize, doflush;
+	s32int c, op, repl, m, y, dstid, scrnid, ni, ci, j, nw, e0, e1, ox, oy, esize, oesize, doflush;
 	uchar *u, *a, refresh;
 	char *fmt;
-	ulong value, chan;
+	u32int value, chan;
 	Rectangle r, clipr;
 	Point p, q, *pp, sp;
 	Memimage *i, *dst, *src, *mask;
@@ -1330,14 +1362,14 @@
 			m = 1+4+4+1+4+1+4*4+4*4+4;
 			if(n < m)
 				error(Eshortdraw);
-			dstid = BGLONG(a+1);
-			scrnid = BGSHORT(a+5);
+			dstid = BG32INT(a+1);
+			scrnid = BG32INT(a+5);
 			refresh = a[9];
-			chan = BGLONG(a+10);
+			chan = BG32INT(a+10);
 			repl = a[14];
 			drawrectangle(&r, a+15);
 			drawrectangle(&clipr, a+31);
-			value = BGLONG(a+47);
+			value = BG32INT(a+47);
 			if(drawlookup(client, dstid, 0))
 				error(Eimageexists);
 			if(scrnid){
@@ -1405,13 +1437,13 @@
 			m = 1+4+4+4+1;
 			if(n < m)
 				error(Eshortdraw);
-			dstid = BGLONG(a+1);
+			dstid = BG32INT(a+1);
 			if(dstid == 0)
 				error(Ebadarg);
 			if(drawlookupdscreen(dstid))
 				error(Escreenexists);
-			ddst = drawlookup(client, BGLONG(a+5), 1);
-			dsrc = drawlookup(client, BGLONG(a+9), 1);
+			ddst = drawlookup(client, BG32INT(a+5), 1);
+			dsrc = drawlookup(client, BG32INT(a+9), 1);
 			if(ddst==0 || dsrc==0)
 				error(Enodrawimage);
 			if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
@@ -1424,7 +1456,7 @@
 			m = 1+4+1+4*4;
 			if(n < m)
 				error(Eshortdraw);
-			ddst = drawlookup(client, BGLONG(a+1), 1);
+			ddst = drawlookup(client, BG32INT(a+1), 1);
 			if(ddst == nil)
 				error(Enodrawimage);
 			if(ddst->name)
@@ -1471,11 +1503,11 @@
 			dst = drawimage(client, a+1);
 			src = drawimage(client, a+5);
 			drawpoint(&p, a+9);
-			e0 = BGLONG(a+17);
-			e1 = BGLONG(a+21);
+			e0 = BG32INT(a+17);
+			e1 = BG32INT(a+21);
 			if(e0<0 || e1<0)
 				error("invalid ellipse semidiameter");
-			j = BGLONG(a+25);
+			j = BG32INT(a+25);
 			if(j < 0)
 				error("negative ellipse thickness");
 			drawpoint(&sp, a+29);
@@ -1482,8 +1514,8 @@
 			c = j;
 			if(*a == 'E')
 				c = -1;
-			ox = BGLONG(a+37);
-			oy = BGLONG(a+41);
+			ox = BG32INT(a+37);
+			oy = BG32INT(a+41);
 			op = drawclientop(client);
 			/* high bit indicates arc angles are present */
 			if(ox & (1<<31)){
@@ -1501,10 +1533,10 @@
 			m = 1+4;
 			if(n < m)
 				error(Eshortdraw);
-			ll = drawlookup(client, BGLONG(a+1), 0);
+			ll = drawlookup(client, BG32INT(a+1), 0);
 			if(ll && ll->dscreen && ll->dscreen->owner != client)
 				ll->dscreen->owner->refreshme = 1;
-			drawuninstall(client, BGLONG(a+1));
+			drawuninstall(client, BG32INT(a+1));
 			continue;
 
 		/* free screen: 'F' id[4] */
@@ -1513,7 +1545,7 @@
 			m = 1+4;
 			if(n < m)
 				error(Eshortdraw);
-			drawlookupscreen(client, BGLONG(a+1), &cs);
+			drawlookupscreen(client, BG32INT(a+1), &cs);
 			drawuninstallscreen(client, cs);
 			continue;
 
@@ -1523,7 +1555,7 @@
 			m = 1+4+4+1;
 			if(n < m)
 				error(Eshortdraw);
-			dstid = BGLONG(a+1);
+			dstid = BG32INT(a+1);
 			dst = drawimage(client, a+1);
 			if(dstid == 0 || dst == screenimage)
 				error("cannot use display as font");
@@ -1532,7 +1564,7 @@
 				error(Enodrawimage);
 			if(font->image->layer)
 				error("cannot use window as font");
-			ni = BGLONG(a+5);
+			ni = BG32INT(a+5);
 			if(ni<=0 || ni>4096)
 				error("bad font size (4096 chars max)");
 			free(font->fchar);	/* should we complain if non-zero? */
@@ -1550,13 +1582,13 @@
 			m = 1+4+4+2+4*4+2*4+1+1;
 			if(n < m)
 				error(Eshortdraw);
-			font = drawlookup(client, BGLONG(a+1), 1);
+			font = drawlookup(client, BG32INT(a+1), 1);
 			if(font == 0)
 				error(Enodrawimage);
 			if(font->nfchar == 0)
 				error(Enotfont);
 			src = drawimage(client, a+5);
-			ci = BGSHORT(a+9);
+			ci = BG16INT(a+9);
 			if(ci >= font->nfchar)
 				error(Eindex);
 			drawrectangle(&r, a+11);
@@ -1580,9 +1612,9 @@
 			dst = drawimage(client, a+1);
 			drawpoint(&p, a+5);
 			drawpoint(&q, a+13);
-			e0 = BGLONG(a+21);
-			e1 = BGLONG(a+25);
-			j = BGLONG(a+29);
+			e0 = BG32INT(a+21);
+			e1 = BG32INT(a+25);
+			j = BG32INT(a+29);
 			if(j < 0)
 				error("negative line width");
 			src = drawimage(client, a+33);
@@ -1621,7 +1653,7 @@
 			m += j;
 			if(n < m)
 				error(Eshortdraw);
-			dstid = BGLONG(a+1);
+			dstid = BG32INT(a+1);
 			if(drawlookup(client, dstid, 0))
 				error(Eimageexists);
 			dn = drawlookupname(j, (char*)a+6);
@@ -1654,7 +1686,7 @@
 			m += j;
 			if(n < m)
 				error(Eshortdraw);
-			di = drawlookup(client, BGLONG(a+1), 0);
+			di = drawlookup(client, BG32INT(a+1), 0);
 			if(di == 0)
 				error(Enodrawimage);
 			if(di->name)
@@ -1688,7 +1720,7 @@
 				if(ni > 0){
 					dstflush(dst->layer->screen->image, r);
 					dstflush(dst->layer->screen->image, dst->layer->screenr);
-					ll = drawlookup(client, BGLONG(a+1), 1);
+					ll = drawlookup(client, BG32INT(a+1), 1);
 					drawrefreshscreen(ll, client);
 				}
 			}
@@ -1712,14 +1744,14 @@
 			if(n < m)
 				error(Eshortdraw);
 			dst = drawimage(client, a+1);
-			ni = BGSHORT(a+5);
+			ni = BG16INT(a+5);
 			if(ni < 0)
 				error("negative count in polygon");
-			e0 = BGLONG(a+7);
-			e1 = BGLONG(a+11);
+			e0 = BG32INT(a+7);
+			e1 = BG32INT(a+11);
 			j = 0;
 			if(*a == 'p'){
-				j = BGLONG(a+15);
+				j = BG32INT(a+15);
 				if(j < 0)
 					error("negative polygon line width");
 			}
@@ -1816,7 +1848,7 @@
 
 			dst = drawimage(client, a+1);
 			src = drawimage(client, a+5);
-			font = drawlookup(client, BGLONG(a+9), 1);
+			font = drawlookup(client, BG32INT(a+9), 1);
 			if(font == 0)
 				error(Enodrawimage);
 			if(font->nfchar == 0)
@@ -1824,7 +1856,7 @@
 			drawpoint(&p, a+13);
 			drawrectangle(&r, a+21);
 			drawpoint(&sp, a+37);
-			ni = BGSHORT(a+45);
+			ni = BG16INT(a+45);
 			u = a+m;
 			m += ni*2;
 			if(n < m)
@@ -1842,7 +1874,7 @@
 				r.max.y = r.min.y+Dy(font->image->r);
 				j = ni;
 				while(--j >= 0){
-					ci = BGSHORT(u);
+					ci = BG16INT(u);
 					if(ci<0 || ci>=font->nfchar){
 						dst->clipr = clipr;
 						error(Eindex);
@@ -1855,7 +1887,7 @@
 			}
 			q = p;
 			while(--ni >= 0){
-				ci = BGSHORT(u);
+				ci = BG16INT(u);
 				if(ci<0 || ci>=font->nfchar){
 					dst->clipr = clipr;
 					error(Eindex);
@@ -1874,13 +1906,13 @@
 			m = 1+4+4;
 			if(n < m)
 				error(Eshortdraw);
-			dstid = BGLONG(a+1);
+			dstid = BG32INT(a+1);
 			if(dstid == 0)
 				error(Ebadarg);
 			dscrn = drawlookupdscreen(dstid);
 			if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
 				error(Enodrawscreen);
-			if(dscrn->screen->image->chan != BGLONG(a+5))
+			if(dscrn->screen->image->chan != BG32INT(a+5))
 				error("inconsistent chan");
 			if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
 				error(Edrawmem);
@@ -1892,7 +1924,7 @@
 			m = 1+1+2;
 			if(n < m)
 				error(Eshortdraw);
-			nw = BGSHORT(a+2);
+			nw = BG16INT(a+2);
 			if(nw < 0)
 				error(Ebadarg);
 			if(nw == 0)
@@ -1921,7 +1953,7 @@
 			if(lp[0]->layer->screen->image->data == screenimage->data)
 				for(j=0; j<nw; j++)
 					dstflush(lp[j]->layer->screen->image, lp[j]->layer->screenr);
-			ll = drawlookup(client, BGLONG(a+1+1+2), 1);
+			ll = drawlookup(client, BG32INT(a+1+1+2), 1);
 			drawrefreshscreen(ll, client);
 			poperror();
 			free(lp);
--- a/emu/port/dis.c
+++ b/emu/port/dis.c
@@ -6,6 +6,8 @@
 #include	"error.h"
 #include	"raise.h"
 
+#define DP if(1){}else print
+
 struct
 {
 	Lock	l;
@@ -23,7 +25,7 @@
 	Atidle*	idletasks;
 } isched;
 
-int	bflag;
+extern	int	bflag;
 int	cflag;
 uvlong	gcbusy;
 uvlong	gcidle;
@@ -205,6 +207,10 @@
 	Osenv *o;
 	Prog **ph;
 
+	if(p->exstr != nil)
+		DP("delprog p->pid %d msg %s p->exstr %s\n", p->pid, msg, p->exstr);
+	else
+		DP("delprog p->pid %d msg --%s--\n", p->pid, msg);
 	tellsomeone(p, msg);	/* call before being removed from prog list */
 
 	o = p->osenv;
@@ -264,8 +270,10 @@
 {
 	Osenv *o;
 
+	DP("tellsomeone pid %d buf %s\n", p->pid, buf);
 	if(waserror())
 		return;
+	DP("tellsomeone after waserror() pid %d buf %s\n", p->pid, buf);
 	o = p->osenv;
 	if(o->childq != nil)
 		qproduce(o->childq, buf, strlen(buf));
@@ -934,6 +942,7 @@
 
 	estr = up->env->errstr;
 	broken = 0;
+	DP("progexit estr %s\n", estr);
 	if(estr[0] != '\0' && strcmp(estr, Eintr) != 0 && strncmp(estr, "fail:", 5) != 0)
 		broken = 1;
 
@@ -949,17 +958,28 @@
 	m = R.M->m;
 	if(broken){
 		if(cflag){	/* only works on Plan9 for now */
+			DP("progexit cflag set\n");
 			char *pc = strstr(estr, "pc=");
 
 			if(pc != nil)
 				R.PC = r->R.PC = (Inst*)strtol(pc+3, nil, 0);	/* for debugging */
 		}
-		print("[%s] Broken: \"%s\"\n", m->name, estr);
+		print("[%s] Broken: pid %d \"%s\"\n", m->name, getpid(), estr);
 	}
 
+	if(r->exstr != nil)
+		DP("progexit pid %d name %s estr %s exval %p exstr %s\n",
+			r->pid, m->name, estr, r->exval, r->exstr);
+	else
+		DP("progexit pid %d name %s estr %s exval %p\n",
+			r->pid, m->name, estr, r->exval);
+	// sh.b is matching on fail: not on "<pid> fail: "
 	snprint(msg, sizeof(msg), "%d \"%s\":%s", r->pid, m->name, estr);
+	// snprint(msg, sizeof(msg), "%s", estr);
+	DP("progexit msg %s\n", msg);
 
 	if(up->env->debug != nil) {
+		DP("progexit debug set\n");
 		dbgexit(r, broken, estr);
 		broken = 1;
 		/* must force it to break if in debug */
@@ -1010,6 +1030,7 @@
 
 	/* cause an exception in the dis prog.  As for error(), but Plan 9 needs reg*/
 	kstrcpy(up->env->errstr, msg, ERRMAX);
+	print("disfault: %s\n", msg);
 	oslongjmp(reg, up->estack[--up->nerr], 1);
 }
 
--- a/include/memdraw.h
+++ b/include/memdraw.h
@@ -19,11 +19,11 @@
 
 struct Memdata
 {
-	ulong	*base;	/* allocated data pointer */
+	uintptr	*base;	/* allocated data pointer */
 	uchar	*bdata;	/* pointer to first byte of actual data; word-aligned */
-	int		ref;		/* number of Memimages using this data */
+	s32int		ref;		/* number of Memimages using this data */
 	void*	imref;
-	int		allocd;	/* is this malloc'd? */
+	s32int		allocd;	/* is this malloc'd? */
 };
 
 enum {
@@ -39,20 +39,20 @@
 {
 	Rectangle	r;		/* rectangle in data area, local coords */
 	Rectangle	clipr;		/* clipping region */
-	int		depth;	/* number of bits of storage per pixel */
-	int		nchan;	/* number of channels */
-	ulong	chan;	/* channel descriptions */
+	s32int		depth;	/* number of bits of storage per pixel */
+	s32int		nchan;	/* number of channels */
+	u32int	chan;	/* channel descriptions */
 	Memcmap	*cmap;
 
 	Memdata	*data;	/* pointer to data; shared by windows in this image */
-	int		zero;		/* data->bdata+zero==&byte containing (0,0) */
-	ulong	width;	/* width in words of a single scan line */
+	s32int		zero;		/* data->bdata+zero==&byte containing (0,0) */
+	u32int	width;	/* width in words of a single scan line */
 	Memlayer	*layer;	/* nil if not a layer*/
-	ulong	flags;
+	u32int	flags;
 
-	int		shift[NChan];
-	int		mask[NChan];
-	int		nbits[NChan];
+	s32int		shift[NChan];
+	s32int		mask[NChan];
+	s32int		nbits[NChan];
 };
 
 struct Memcmap
@@ -76,7 +76,7 @@
 struct	Memsubfont
 {
 	char		*name;
-	short	n;		/* number of chars in font */
+	s16int	n;		/* number of chars in font */
 	uchar	height;		/* height of bitmap */
 	char	ascent;		/* top of bitmap to baseline */
 	Fontchar *info;		/* n+1 character descriptors */
@@ -101,14 +101,14 @@
 	Rectangle sr;
 	Memimage *mask;
 	Rectangle mr;
-	int op;
+	s32int op;
 
-	ulong state;
-	ulong mval;	/* if Simplemask, the mask pixel in mask format */
-	ulong mrgba;	/* mval in rgba */
-	ulong sval;	/* if Simplesrc, the source pixel in src format */
-	ulong srgba;	/* sval in rgba */
-	ulong sdval;	/* sval in dst format */
+	u32int state;
+	u32int mval;	/* if Simplemask, the mask pixel in mask format */
+	u32int mrgba;	/* mval in rgba */
+	u32int sval;	/* if Simplesrc, the source pixel in src format */
+	u32int srgba;	/* sval in rgba */
+	u32int sdval;	/* sval in dst format */
 };
 
 /*
@@ -115,8 +115,8 @@
  * Memimage management
  */
 
-extern Memimage*	allocmemimage(Rectangle, ulong);
-extern Memimage*	allocmemimaged(Rectangle, ulong, Memdata*);
+extern Memimage*	allocmemimage(Rectangle, u32int);
+extern Memimage*	allocmemimaged(Rectangle, u32int, Memdata*);
 extern Memimage*	readmemimage(int);
 extern Memimage*	creadmemimage(int);
 extern int	writememimage(int, Memimage*);
@@ -124,11 +124,11 @@
 extern int		loadmemimage(Memimage*, Rectangle, uchar*, int);
 extern int		cloadmemimage(Memimage*, Rectangle, uchar*, int);
 extern int		unloadmemimage(Memimage*, Rectangle, uchar*, int);
-extern ulong*	wordaddr(Memimage*, Point);
+extern u32int*	u32addr(Memimage*, Point);
 extern uchar*	byteaddr(Memimage*, Point);
 extern int		drawclip(Memimage*, Rectangle*, Memimage*, Point*, Memimage*, Point*, Rectangle*, Rectangle*);
-extern void	memfillcolor(Memimage*, ulong);
-extern int		memsetchan(Memimage*, ulong);
+extern void	memfillcolor(Memimage*, u32int);
+extern int		memsetchan(Memimage*, u32int);
 
 /*
  * Graphics
@@ -148,7 +148,7 @@
 extern Rectangle	memlinebbox(Point, Point, int, int, int);
 extern int	memlineendsize(int);
 extern void	_memmkcmap(void);
-extern void	memimageinit(void);
+extern s32	memimageinit(void);
 
 /*
  * Subfont management
@@ -171,7 +171,7 @@
 /*
  * Kernel interface
  */
-uchar*	attachscreen(Rectangle*, ulong*, int*, int*, int*);
+Memdata*	attachscreen(Rectangle*, ulong*, int*, int*, int*);
 void		memimagemove(void*, void*);
 
 /*
--- a/include/memlayer.h
+++ b/include/memlayer.h
@@ -18,7 +18,7 @@
 	Memscreen	*screen;	/* screen this layer belongs to */
 	Memimage	*front;	/* window in front of this one */
 	Memimage	*rear;	/* window behind this one*/
-	int		clear;	/* layer is fully visible */
+	s32int		clear;	/* layer is fully visible */
 	Memimage	*save;	/* save area for obscured parts */
 	Refreshfn	refreshfn;		/* function to call to refresh obscured parts if save==nil */
 	void		*refreshptr;	/* argument to refreshfn */
@@ -34,7 +34,7 @@
  * All these functions accept screen coordinates, not local ones.
  */
 void			_memlayerop(void (*fn)(Memimage*, Rectangle, Rectangle, void*, int), Memimage*, Rectangle, Rectangle, void*);
-Memimage*	memlalloc(Memscreen*, Rectangle, Refreshfn, void*, ulong);
+Memimage*	memlalloc(Memscreen*, Rectangle, Refreshfn, void*, u32int);
 void			memldelete(Memimage*);
 void			memlfree(Memimage*);
 void			memltofront(Memimage*);
--- a/libdraw/alloc.c
+++ b/libdraw/alloc.c
@@ -2,14 +2,16 @@
 #include "draw.h"
 #include "kernel.h"
 
+#define DP if(1){}else print
+
 Image*
-allocimage(Display *d, Rectangle r, ulong chan, int repl, ulong val)
+allocimage(Display *d, Rectangle r, u32int chan, int repl, u32int val)
 {
 	return _allocimage(nil, d, r, chan, repl, val, 0, 0);
 }
 
 Image*
-_allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val, int screenid, int refresh)
+_allocimage(Image *ai, Display *d, Rectangle r, u32int chan, int repl, u32int val, int screenid, int refresh)
 {
 	uchar *a;
 	char *err;
@@ -46,25 +48,26 @@
 	d->imageid++;
 	id = d->imageid;
 	a[0] = 'b';
-	BPLONG(a+1, id);
-	BPLONG(a+5, screenid);
+	BP32INT(a+1, id);
+	BP32INT(a+5, screenid);
 	a[9] = refresh;
-	BPLONG(a+10, chan);
+	BP32INT(a+10, chan);
 	a[14] = repl;
-	BPLONG(a+15, r.min.x);
-	BPLONG(a+19, r.min.y);
-	BPLONG(a+23, r.max.x);
-	BPLONG(a+27, r.max.y);
+	BP32INT(a+15, r.min.x);
+	BP32INT(a+19, r.min.y);
+	BP32INT(a+23, r.max.x);
+	BP32INT(a+27, r.max.y);
 	if(repl)
 		/* huge but not infinite, so various offsets will leave it huge, not overflow */
 		clipr = Rect(-0x3FFFFFFF, -0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF);
 	else
 		clipr = r;
-	BPLONG(a+31, clipr.min.x);
-	BPLONG(a+35, clipr.min.y);
-	BPLONG(a+39, clipr.max.x);
-	BPLONG(a+43, clipr.max.y);
-	BPLONG(a+47, val);
+	BP32INT(a+31, clipr.min.x);
+	BP32INT(a+35, clipr.min.y);
+	BP32INT(a+39, clipr.max.x);
+	BP32INT(a+43, clipr.max.y);
+	BP32INT(a+47, val);
+	DP("_allocimage id %d screenid %d r %R clipr %R\n", id, screenid, r, clipr);
 	if(flushimage(d, 0) < 0)
 		goto Error;
 
@@ -76,7 +79,7 @@
 			a = bufimage(d, 1+4);
 			if(a){
 				a[0] = 'f';
-				BPLONG(a+1, id);
+				BP32INT(a+1, id);
 				flushimage(d, 0);
 			}
 			goto Error;
@@ -101,7 +104,7 @@
 	char *err, buf[12*12+1];
 	Image *i;
 	int id, n;
-	ulong chan;
+	u32int chan;
 
 	err = 0;
 	i = 0;
@@ -126,7 +129,7 @@
 	d->imageid++;
 	id = d->imageid;
 	a[0] = 'n';
-	BPLONG(a+1, id);
+	BP32INT(a+1, id);
 	a[5] = n;
 	memmove(a+6, name, n);
 	if(flushimage(d, 0) < 0)
@@ -142,7 +145,7 @@
 		a = bufimage(d, 1+4);
 		if(a){
 			a[0] = 'f';
-			BPLONG(a+1, id);
+			BP32INT(a+1, id);
 			flushimage(d, 0);
 		}
 		goto Error;
@@ -180,7 +183,7 @@
 	if(a == 0)
 		return 0;
 	a[0] = 'N';
-	BPLONG(a+1, i->id);
+	BP32INT(a+1, i->id);
 	a[5] = in;
 	a[6] = n;
 	memmove(a+7, name, n);
@@ -206,7 +209,7 @@
 	if(a == 0)
 		return -1;
 	a[0] = 'f';
-	BPLONG(a+1, i->id);
+	BP32INT(a+1, i->id);
 	if(i->screen){
 		w = d->windows;
 		if(w == i)
--- a/libmemdraw/alloc.c
+++ b/libmemdraw/alloc.c
@@ -22,14 +22,14 @@
 }
 
 Memimage*
-allocmemimaged(Rectangle r, ulong chan, Memdata *md)
+allocmemimaged(Rectangle r, u32int chan, Memdata *md)
 {
-	int d;
-	ulong l;
+	s32int d;
+	u32int l;
 	Memimage *i;
 
 	if((d = chantodepth(chan)) == 0) {
-		werrstr("bad channel descriptor %.8lux", chan);
+		werrstr("bad channel descriptor %.8ux", chan);
 		return nil;
 	}
 
@@ -40,7 +40,7 @@
 		return nil;
 
 	i->data = md;
-	i->zero = sizeof(ulong)*l*r.min.y;
+	i->zero = sizeof(u32int)*l*r.min.y;
 	
 	if(r.min.x >= 0)
 		i->zero += (r.min.x*d)/8;
@@ -61,15 +61,15 @@
 }
 
 Memimage*
-allocmemimage(Rectangle r, ulong chan)
+allocmemimage(Rectangle r, u32int chan)
 {
-	int d;
-	ulong l, nw;
+	s32int d;
+	u32int l, nw;
 	Memdata *md;
 	Memimage *i;
 
 	if((d = chantodepth(chan)) == 0) {
-		werrstr("bad channel descriptor %.8lux", chan);
+		werrstr("bad channel descriptor %.8ux", chan);
 		return nil;
 	}
 
@@ -80,14 +80,14 @@
 		return nil;
 
 	md->ref = 1;
-	md->base = poolalloc(imagmem, (2+nw)*sizeof(ulong));
+	md->base = poolalloc(imagmem, 2*sizeof(intptr)+nw*sizeof(u32int));
 	if(md->base == nil){
 		free(md);
 		return nil;
 	}
 
-	md->base[0] = (ulong)md;
-	/* md->base[1] = getcallerpc(&r); */
+	md->base[0] = (uintptr)md;
+	md->base[1] = getcallerpc(&r);
 
 	/* if this changes, memimagemove must change too */
 	md->bdata = (uchar*)&md->base[2];
@@ -120,10 +120,10 @@
 /*
  * Wordaddr is deprecated.
  */
-ulong*
-wordaddr(Memimage *i, Point p)
+u32int*
+u32addr(Memimage *i, Point p)
 {
-	return (ulong*) ((ulong)byteaddr(i, p) & ~(sizeof(ulong)-1));
+	return (u32int*) ((uintptr)byteaddr(i, p) & ~(sizeof(uintptr)-1));
 }
 
 uchar*
@@ -131,7 +131,7 @@
 {
 	uchar *a;
 
-	a = i->data->bdata+i->zero+sizeof(ulong)*p.y*i->width;
+	a = i->data->bdata+i->zero+(s32int)(sizeof(u32int)*p.y*i->width);
 
 	if(i->depth < 8){
 		/*
@@ -138,7 +138,7 @@
 		 * We need to always round down,
 		 * but C rounds toward zero.
 		 */
-		int np;
+		s32int np;
 		np = 8/i->depth;
 		if(p.x < 0)
 			return a+(p.x-np+1)/np;
@@ -150,12 +150,12 @@
 }
 
 int
-memsetchan(Memimage *i, ulong chan)
+memsetchan(Memimage *i, u32int chan)
 {
-	int d;
-	int t, j, k;
-	ulong cc;
-	int bytes;
+	s32int d;
+	s32int t, j, k;
+	u32int cc;
+	s32int bytes;
 
 	if((d = chantodepth(chan)) == 0) {
 		werrstr("bad channel descriptor");
--- a/libmemdraw/draw.c
+++ b/libmemdraw/draw.c
@@ -43,7 +43,7 @@
 
 int	_ifmt(Fmt*);
 
-void
+s32
 memimageinit(void)
 {
 	static int didinit = 0;
@@ -50,7 +50,7 @@
 	char *n;
 
 	if(didinit)
-		return;
+		return 0;
 
 	didinit = 1;
 
@@ -74,20 +74,23 @@
 	memzeros->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
 	*byteaddr(memzeros, ZP) = 0;
 
-	if(memones == nil || memzeros == nil)
+	if(memones == nil || memzeros == nil){
 		assert(0 /*cannot initialize memimage library */);	/* RSC BUG */
+		return -1;
+	}
 
 	memwhite = memones;
 	memblack = memzeros;
 	memopaque = memones;
 	memtransparent = memzeros;
+	return 0;
 }
 
-static ulong imgtorgba(Memimage*, ulong);
-static ulong rgbatoimg(Memimage*, ulong);
-static ulong pixelbits(Memimage*, Point);
+static u32int imgtorgba(Memimage*, u32int);
+static u32int rgbatoimg(Memimage*, u32int);
+static u32int pixelbits(Memimage*, Point);
 
-#define DBG if(0)
+#define DBG if(drawdebug)
 void
 memimagedraw(Memimage *dst, Rectangle r, Memimage *src, Point p0, Memimage *mask, Point p1, int op)
 {
@@ -96,17 +99,18 @@
 	if(mask == nil)
 		mask = memopaque;
 
-DBG	print("memimagedraw %p/%luX %R @ %p %p/%luX %P %p/%luX %P... ", dst, dst->chan, r, dst->data->bdata, src, src->chan, p0, mask, mask->chan, p1);
+DBG	print("memimagedraw %p/%uX %R @ %p %p/%uX %P %p/%uX %P... ",
+		dst, dst->chan, r, dst->data->bdata, src, src->chan, p0, mask, mask->chan, p1);
 
 	if(drawclip(dst, &r, src, &p0, mask, &p1, &par.sr, &par.mr) == 0){
-//		if(drawdebug)
-//			iprint("empty clipped rectangle\n");
+		if(drawdebug)
+			iprint("empty clipped rectangle\n");
 		return;
 	}
 
 	if(op < Clear || op > SoverD){
-//		if(drawdebug)
-//			iprint("op out of range: %d\n", op);
+		if(drawdebug)
+			iprint("op out of range: %d\n", op);
 		return;
 	}
 
@@ -127,7 +131,7 @@
 			par.srgba = imgtorgba(src, par.sval);
 			par.sdval = rgbatoimg(dst, par.srgba);
 			if((par.srgba&0xFF) == 0 && (op&DoutS)){
-//				if (drawdebug) iprint("fill with transparent source\n");
+				if (drawdebug) iprint("fill with transparent source\n");
 				return;	/* no-op successfully handled */
 			}
 		}
@@ -138,7 +142,7 @@
 		if(Dx(mask->r)==1 && Dy(mask->r)==1){
 			par.mval = pixelbits(mask, mask->r.min);
 			if(par.mval == 0 && (op&DoutS)){
-//				if(drawdebug) iprint("fill with zero mask\n");
+				if(drawdebug) iprint("fill with zero mask\n");
 				return;	/* no-op successfully handled */
 			}
 			par.state |= Simplemask;
@@ -148,9 +152,9 @@
 		}
 	}
 
-//	if(drawdebug)
-//		iprint("dr %R sr %R mr %R...", r, par.sr, par.mr);
-DBG print("draw dr %R sr %R mr %R %lux\n", r, par.sr, par.mr, par.state);
+	if(drawdebug)
+		iprint("dr %R sr %R mr %R...", r, par.sr, par.mr);
+DBG print("draw dr %R sr %R mr %R %ux\n", r, par.sr, par.mr, par.state);
 
 	/*
 	 * Now that we've clipped the parameters down to be consistent, we 
@@ -166,7 +170,7 @@
 	 */
 DBG print("test hwdraw\n");
 	if(hwdraw(&par)){
-//if(drawdebug) iprint("hw handled\n");
+if(drawdebug) iprint("hw handled\n");
 DBG print("hwdraw handled\n");
 		return;
 	}
@@ -175,7 +179,7 @@
 	 */
 DBG print("test memoptdraw\n");
 	if(memoptdraw(&par)){
-//if(drawdebug) iprint("memopt handled\n");
+if(drawdebug) iprint("memopt handled\n");
 DBG print("memopt handled\n");
 		return;
 	}
@@ -186,7 +190,7 @@
 	 */
 DBG print("test chardraw\n");
 	if(chardraw(&par)){
-//if(drawdebug) iprint("chardraw handled\n");
+if(drawdebug) iprint("chardraw handled\n");
 DBG print("chardraw handled\n");
 		return;
 	}
@@ -196,10 +200,9 @@
 	 */
 DBG print("do alphadraw\n");
 	alphadraw(&par);
-//if(drawdebug) iprint("alphadraw handled\n");
+if(drawdebug) iprint("alphadraw handled\n");
 DBG print("alphadraw handled\n");
 }
-#undef DBG
 
 /*
  * Clip the destination rectangle further based on the properties of the 
@@ -222,8 +225,10 @@
 	splitcoords = (p0->x!=p1->x) || (p0->y!=p1->y);
 	/* clip to destination */
 	rmin = r->min;
-	if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr))
+	if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr)){
+		DBG print("drawclip: !rectclip(r, dst->r) || !rectclip(r, dst->clipr)\n");
 		return 0;
+	}
 	/* move mask point */
 	p1->x += r->min.x-rmin.x;
 	p1->y += r->min.y-rmin.y;
@@ -235,10 +240,15 @@
 	sr->max.x = p0->x+Dx(*r);
 	sr->max.y = p0->y+Dy(*r);
 	/* sr is r in source coordinates; clip to source */
-	if(!(src->flags&Frepl) && !rectclip(sr, src->r))
+	if(!(src->flags&Frepl) && !rectclip(sr, src->r)){
+		DBG print("drawclip: !(src->flags&Frepl) && !rectclip(sr, src->r)\n");
 		return 0;
-	if(!rectclip(sr, src->clipr))
+	}
+	if(!rectclip(sr, src->clipr)){
+		DBG print("drawclip: sr %R src->clipr %R\n\t!rectclip(sr, src->clipr)\n",
+			*sr, src->clipr);
 		return 0;
+	}
 	/* compute and clip rectangle in mask */
 	if(splitcoords){
 		/* move mask point with source */
@@ -249,10 +259,14 @@
 		mr->max.y = p1->y+Dy(*sr);
 		omr = *mr;
 		/* mr is now rectangle in mask; clip it */
-		if(!(mask->flags&Frepl) && !rectclip(mr, mask->r))
+		if(!(mask->flags&Frepl) && !rectclip(mr, mask->r)){
+			DBG print("drawclip: !(mask->flags&Frepl) && !rectclip(mr, mask->r)\n");
 			return 0;
-		if(!rectclip(mr, mask->clipr))
+		}
+		if(!rectclip(mr, mask->clipr)){
+			DBG print("drawclip: !rectclip(mr, mask->clipr)\n");
 			return 0;
+		}
 		/* reflect any clips back to source */
 		sr->min.x += mr->min.x-omr.min.x;
 		sr->min.y += mr->min.y-omr.min.y;
@@ -260,10 +274,14 @@
 		sr->max.y += mr->max.y-omr.max.y;
 		*p1 = mr->min;
 	}else{
-		if(!(mask->flags&Frepl) && !rectclip(sr, mask->r))
+		if(!(mask->flags&Frepl) && !rectclip(sr, mask->r)){
+			DBG print("drawclip: !(mask->flags&Frepl) && !rectclip(mr, mask->r)\n");
 			return 0;
-		if(!rectclip(sr, mask->clipr))
+		}
+		if(!rectclip(sr, mask->clipr)){
+			DBG print("drawclip: !rectclip(mr, mask->clipr)\n");
 			return 0;
+		}
 		*p1 = sr->min;
 	}
 
@@ -300,6 +318,7 @@
 
 	return 1;
 }
+#undef DBG
 
 /*
  * Conversion tables.
@@ -369,7 +388,7 @@
 	uchar	*blu;
 	uchar	*alpha;
 	uchar	*grey;
-	ulong	*rgba;
+	u32int	*rgba;
 	int	delta;	/* number of bytes to add to pointer to get next pixel to the right */
 
 	/* used by boolcalc* for mask data */
@@ -411,7 +430,7 @@
 
 	int	replcache;	/* if set, cache buffers */
 	Buffer	bcache[MAXBCACHE];
-	ulong	bfilled;
+	u32int	bfilled;
 	uchar	*bufbase;
 	int	bufoff;
 	int	bufdelta;
@@ -514,7 +533,7 @@
 	p->bytey0s = byteaddr(img, Pt(img->r.min.x, img->r.min.y));
 	p->bytermin = byteaddr(img, Pt(r.min.x, img->r.min.y));
 	p->bytey0e = byteaddr(img, Pt(img->r.max.x, img->r.min.y));
-	p->bwidth = sizeof(ulong)*img->width;
+	p->bwidth = sizeof(u32int)*img->width;
 
 	assert(p->bytey0s <= p->bytermin && p->bytermin <= p->bytey0e);
 
@@ -657,9 +676,9 @@
 	 * but it avoids a fair amount of code duplication to make this a case here
 	 * rather than have a separate booldraw.
 	 */
-//if(drawdebug) iprint("flag %lud mchan %lux=?%x dd %d\n", src->flags&Falpha, mask->chan, GREY1, dst->depth);
+if(drawdebug) iprint("flag %ud mchan %ux=?%x dd %d\n", src->flags&Falpha, mask->chan, GREY1, dst->depth);
 	if(!(src->flags&Falpha) && mask->chan == GREY1 && dst->depth >= 8 && op == SoverD){
-//if(drawdebug) iprint("boolcopy...");
+if(drawdebug) iprint("boolcopy...");
 		rdsrc = convfn(dst, &z->dpar, src, &z->spar, &ndrawbuf);
 		rddst = readptr;
 		rdmask = readfn(mask);
@@ -670,6 +689,7 @@
 		rdsrc = readfn(src);
 		rddst = readfn(dst);
 		wrdst = writefn(dst);
+if(drawdebug) iprint("alphacalc op %d\n", op);
 		calc = alphacalc[op];
 
 		/*
@@ -692,13 +712,17 @@
 			 * When there's no alpha involved and the mask is boolean,
 			 * we can avoid all the division and multiplication.
 			 */
-			if(mask->chan == GREY1 && !(src->flags&Falpha))
+			if(mask->chan == GREY1 && !(src->flags&Falpha)){
+if(drawdebug) iprint("boolcalc op %d\n", op);
 				calc = boolcalc[op];
-			else if(op == SoverD && !(src->flags&Falpha))
+			}else if(op == SoverD && !(src->flags&Falpha)){
+if(drawdebug) iprint("alphacalcS\n");
 				calc = alphacalcS;
+			}
 		}
 	}
 
+if(drawdebug) iprint("after if\n");
 	/*
 	 * If the image has a small enough repl rectangle,
 	 * we can just read each line once and cache them.
@@ -721,6 +745,7 @@
 		z->n = ndrawbuf;
 	}
 	drawbuf = z->p;
+if(drawdebug) iprint("after drawbuf\n");
 
 	/*
 	 * Before we were saving only offsets from drawbuf in the parameter
@@ -739,6 +764,7 @@
 		starty = dy-1;
 		endy = -1;
 	}
+if(drawdebug) iprint("before srcy\n");
 
 	/*
 	 * srcy, masky, and dsty are offsets from the top of their
@@ -753,7 +779,9 @@
 	assert(0 <= masky && masky < Dy(mask->r));
 	assert(0 <= dsty && dsty < Dy(dst->r));
 
+if(drawdebug) iprint("before for y=starty\n");
 	for(y=starty; y!=endy; y+=dir, srcy+=dir, masky+=dir, dsty+=dir){
+if(drawdebug) iprint("\ty=%d ", y);
 		clipy(src, &srcy);
 		clipy(dst, &dsty);
 		clipy(mask, &masky);
@@ -768,11 +796,13 @@
 DBG		dumpbuf("src", bsrc, dx);
 DBG		dumpbuf("mask", bmask, dx);
 DBG		dumpbuf("dst", bdst, dx);
+		/* this is where the trapBUS error is springing from */
 		bdst = calc(bdst, bsrc, bmask, dx, isgrey, op);
 		wrdst(&z->dpar, z->dpar.bytermin+dsty*z->dpar.bwidth, bdst);
 	}
 
 	z->inuse = 0;
+if(drawdebug) iprint("alphadraw return 1\n");
 	return 1;
 }
 #undef DBG
@@ -794,7 +824,7 @@
 	Buffer obdst;
 	int fd, sadelta;
 	int i, sa, ma;
-	ulong s, t;
+	u32int s, t;
 
 	obdst = bdst;
 	sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
@@ -845,7 +875,7 @@
 	Buffer obdst;
 	int fs, sadelta;
 	int i, ma, da;
-	ulong s, t;
+	u32int s, t;
 
 	obdst = bdst;
 	sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
@@ -898,7 +928,7 @@
 	Buffer obdst;
 	int fs, fd, sadelta;
 	int i, sa, ma, da;
-	ulong s, t, q1, q2;
+	u32int s, t, q1, q2;
 
 	obdst = bdst;
 	sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
@@ -972,7 +1002,7 @@
 	Buffer obdst;
 	int fd, sadelta;
 	int i, sa, ma;
-	ulong s, t, q1, q2;
+	u32int s, t, q1, q2;
 
 	USED(op);
 	obdst = bdst;
@@ -1065,12 +1095,16 @@
 	Buffer obdst;
 	int fd;
 	int i, ma;
-	ulong s, t;
+	u32int s, t;
 
+	// iprint("\t\talphacalcS");
 	USED(op);
 	obdst = bdst;
 
+	// iprint(" grey %d dx %d ", grey, dx);
 	for(i=0; i<dx; i++){
+		//if(i > dx-2)
+		//	iprint(" i %d ", i);
 		ma = *bmask.alpha;
 		fd = 255-ma;
 
@@ -1079,9 +1113,16 @@
 			bsrc.grey += bsrc.delta;
 			bdst.grey += bdst.delta;
 		}else{
+			/* if(i > dx-2){
+				iprint("ma %d *bsrc.red %d s %d fd %d *bdst.red %d t %d ",
+					ma, *bsrc.red, s, fd, *bdst.red, t);
+			} */
 			*bdst.red = MUL(ma, *bsrc.red, s)+MUL(fd, *bdst.red, t);
 			*bdst.grn = MUL(ma, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
 			*bdst.blu = MUL(ma, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
+			/* if(i > dx-2){
+				iprint("*%p %x ", bdst.red, *bdst.red);
+			} */
 			bsrc.red += bsrc.delta;
 			bsrc.blu += bsrc.delta;
 			bsrc.grn += bsrc.delta;
@@ -1090,11 +1131,13 @@
 			bdst.grn += bdst.delta;
 		}
 		if(bdst.alpha != &ones){
+			// iprint("alphacalcS != &ones\n");
 			*bdst.alpha = ma+MUL(fd, *bdst.alpha, t);
 			bdst.alpha += bdst.delta;
 		}
 		bmask.alpha += bmask.delta;
 	}
+	// iprint("\n");
 	return obdst;
 }
 
@@ -1138,7 +1181,7 @@
 	Buffer obdst;
 	int fs, fd;
 	int i, ma, da, zero;
-	ulong s, t;
+	u32int s, t;
 
 	obdst = bdst;
 	zero = !(op&1);
@@ -1272,7 +1315,7 @@
 	uchar *repl, *r, *w, *ow, bits;
 	int i, n, sh, depth, x, dx, npack, nbits;
 
-	b.rgba = (ulong*)buf;
+	b.rgba = (u32int*)buf;
 	b.grey = w = buf;
 	b.red = b.blu = b.grn = w;
 	b.alpha = &ones;
@@ -1362,7 +1405,7 @@
 writenbit(Param *p, uchar *w, Buffer src)
 {
 	uchar *r;
-	ulong bits;
+	u32int bits;
 	int i, sh, depth, npack, nbits, x, ex;
 
 	assert(src.grey != nil && src.delta == 1);
@@ -1450,7 +1493,7 @@
 		}
 	}
 
-	b.rgba = (ulong*)(buf-copyalpha);
+	b.rgba = (u32int*)(buf-copyalpha);
 
 	if(convgrey){
 		b.grey = buf;
@@ -1493,7 +1536,7 @@
 	int dx, isgrey, convgrey, alphaonly, copyalpha, i, nb;
 	uchar *begin, *end, *r, *w, *rrepl, *grepl, *brepl, *arepl, *krepl;
 	uchar ured, ugrn, ublu;
-	ulong u;
+	u32int u;
 
 	img = p->img;
 	begin = p->bytey0s + y*p->bwidth;
@@ -1517,7 +1560,7 @@
 			memmove(buf, r, dx*nb);
 			r = buf;
 		}
-		b.rgba = (ulong*)r;
+		b.rgba = (u32int*)r;
 		if(copyalpha)
 			b.alpha = r+img->shift[CAlpha]/8;
 		else
@@ -1570,7 +1613,7 @@
 	}
 	
 	b.alpha = copyalpha ? buf : &ones;
-	b.rgba = (ulong*)buf;
+	b.rgba = (u32int*)buf;
 	if(alphaonly){
 		b.red = b.grn = b.blu = b.grey = nil;
 		if(!copyalpha)
@@ -1599,7 +1642,7 @@
 	Memimage *img;
 	int i, isalpha, isgrey, nb, delta, dx, adelta;
 	uchar ff, *red, *grn, *blu, *grey, *alpha;
-	ulong u, mask;
+	u32int u, mask;
 
 	img = p->img;
 
@@ -1626,12 +1669,12 @@
 
 	for(i=0; i<dx; i++){
 		u = w[0] | (w[1]<<8) | (w[2]<<16) | (w[3]<<24);
-DBG print("u %.8lux...", u);
+DBG print("u %.8ux...", u);
 		u &= mask;
-DBG print("&mask %.8lux...", u);
+DBG print("&mask %.8ux...", u);
 		if(isgrey){
 			u |= ((*grey >> (8-img->nbits[CGrey])) & img->mask[CGrey]) << img->shift[CGrey];
-DBG print("|grey %.8lux...", u);
+DBG print("|grey %.8ux...", u);
 			grey += delta;
 		}else{
 			u |= ((*red >> (8-img->nbits[CRed])) & img->mask[CRed]) << img->shift[CRed];
@@ -1640,13 +1683,13 @@
 			red += delta;
 			grn += delta;
 			blu += delta;
-DBG print("|rgb %.8lux...", u);
+DBG print("|rgb %.8ux...", u);
 		}
 
 		if(isalpha){
 			u |= ((*alpha >> (8-img->nbits[CAlpha])) & img->mask[CAlpha]) << img->shift[CAlpha];
 			alpha += adelta;
-DBG print("|alpha %.8lux...", u);
+DBG print("|alpha %.8ux...", u);
 		}
 
 		w[0] = u;
@@ -1661,10 +1704,12 @@
 static Readfn*
 readfn(Memimage *img)
 {
-	if(img->depth < 8)
+	if(img->depth < 8){
 		return readnbit;
-	if(img->nbits[CMap] == 8)
+	}
+	if(img->nbits[CMap] == 8){
 		return readcmap;
+	}
 	return readbyte;
 }
 
@@ -1703,7 +1748,7 @@
 	q = p->bytermin + y*p->bwidth;
 	b.red = q;	/* ptr to data */
 	b.grn = b.blu = b.grey = b.alpha = nil;
-	b.rgba = (ulong*)q;
+	b.rgba = (u32int*)q;
 	b.delta = p->img->depth/8;
 	return b;
 }
@@ -1782,13 +1827,13 @@
 boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
 {
 	uchar *m;
-	ulong *r, *w, *ew;
+	u32int *r, *w, *ew;
 
 	USED(i);
 	USED(o);
 	m = bmask.grey;
-	w = (ulong*)bdst.red;
-	r = (ulong*)bsrc.red;
+	w = (u32int*)bdst.red;
+	r = (u32int*)bsrc.red;
 	ew = w+dx;
 	for(; w < ew; w++,r++)
 		if(*m++)
@@ -1820,7 +1865,7 @@
 
 	b.red = buf;
 	b.blu = b.grn = b.grey = b.alpha = nil;
-	b.rgba = (ulong*)buf;
+	b.rgba = (u32int*)buf;
 	b.delta = 0;
 	
 	return b;
@@ -1857,11 +1902,11 @@
 	return genconv;
 }
 
-static ulong
+static u32int
 pixelbits(Memimage *i, Point pt)
 {
 	uchar *p;
-	ulong val;
+	u32int val;
 	int off, bpp, npack;
 
 	val = 0;
@@ -1931,7 +1976,7 @@
 }
 
 static void
-memsets(void *vp, ushort val, int n)
+memset16(void *vp, ushort val, int n)
 {
 	ushort *p, *ep;
 
@@ -1942,9 +1987,9 @@
 }
 
 static void
-memsetl(void *vp, ulong val, int n)
+memset32(void *vp, u32int val, int n)
 {
-	ulong *p, *ep;
+	u32int *p, *ep;
 
 	p = vp;
 	ep = p+n;
@@ -1953,7 +1998,7 @@
 }
 
 static void
-memset24(void *vp, ulong val, int n)
+memset24(void *vp, u32int val, int n)
 {
 	uchar *p, *ep;
 	uchar a,b,c;
@@ -1970,12 +2015,12 @@
 	}
 }
 
-static ulong
-imgtorgba(Memimage *img, ulong val)
+static u32int
+imgtorgba(Memimage *img, u32int val)
 {
 	uchar r, g, b, a;
 	int nb, ov, v;
-	ulong chan;
+	u32int chan;
 	uchar *p;
 
 	a = 0xFF;
@@ -2018,12 +2063,12 @@
 	return (r<<24)|(g<<16)|(b<<8)|a;	
 }
 
-static ulong
-rgbatoimg(Memimage *img, ulong rgba)
+static u32int
+rgbatoimg(Memimage *img, u32int rgba)
 {
-	ulong chan;
+	u32int chan;
 	int d, nb;
-	ulong v;
+	u32int v;
 	uchar *p, r, g, b, a, m;
 
 	v = 0;
@@ -2067,8 +2112,8 @@
 static int
 memoptdraw(Memdrawparam *par)
 {
-	int m, y, dy, dx, op;
-	ulong v;
+	s32int m, y, dy, dx, op;
+	u32int v;
 	Memimage *src;
 	Memimage *dst;
 
@@ -2078,7 +2123,7 @@
 	dst = par->dst;
 	op = par->op;
 
-DBG print("state %lux mval %lux dd %d\n", par->state, par->mval, dst->depth);
+DBG print("state %ux mval %ux dd %d\n", par->state, par->mval, dst->depth);
 	/*
 	 * If we have an opaque mask and source is one opaque pixel we can convert to the
 	 * destination format and just replicate with memset.
@@ -2086,14 +2131,15 @@
 	m = Simplesrc|Simplemask|Fullmask;
 	if((par->state&m)==m && (par->srgba&0xFF) == 0xFF && (op ==S || op == SoverD)){
 		uchar *dp, p[4];
-		int d, dwid, ppb, np, nb;
+		s32int d, dwid, ppb, np, nb;
 		uchar lm, rm;
 
+DBG print("Simplesrc|Simplemask|Fullmask\n");
 DBG print("memopt, dst %p, dst->data->bdata %p\n", dst, dst->data->bdata);
-		dwid = dst->width*sizeof(ulong);
+		dwid = dst->width*sizeof(u32int);
 		dp = byteaddr(dst, par->r.min);
 		v = par->sdval;
-DBG print("sdval %lud, depth %d\n", v, dst->depth);
+DBG print("sdval %ud, depth %d\n", v, dst->depth);
 		switch(dst->depth){
 		case 1:
 		case 2:
@@ -2128,7 +2174,7 @@
 
 				for(y=0; y<dy; y++, dp+=dwid){
 					if(lm){
-DBG print("dp %p v %lux lm %ux (v ^ *dp) & lm %lux\n", dp, v, lm, (v^*dp)&lm);
+DBG print("dp %p v %ux lm %ux (v ^ *dp) & lm %ux\n", dp, v, lm, (v^*dp)&lm);
 						*dp ^= (v ^ *dp) & lm;
 						dp++;
 					}
@@ -2162,7 +2208,7 @@
 DBG print("dp=%p; dx=%d; for(y=0; y<%d; y++, dp+=%d)\nmemsets(dp, v, dx);\n",
 	dp, dx, dy, dwid);
 			for(y=0; y<dy; y++, dp+=dwid)
-				memsets(dp, v, dx);
+				memset16(dp, v, dx);
 			return 1;
 		case 24:
 			for(y=0; y<dy; y++, dp+=dwid)
@@ -2173,9 +2219,9 @@
 			p[1] = v>>8;
 			p[2] = v>>16;
 			p[3] = v>>24;
-			v = *(ulong*)p;
+			v = *(u32int*)p;
 			for(y=0; y<dy; y++, dp+=dwid)
-				memsetl(dp, v, dx);
+				memset32(dp, v, dx);
 			return 1;
 		default:
 			assert(0 /* bad dest depth in memoptdraw */);
@@ -2191,16 +2237,17 @@
 	if((par->state&(m|Replsrc))==m && src->depth >= 8 
 	&& src->chan == dst->chan && (op == S || (op == SoverD && !(src->flags&Falpha)))){
 		uchar *sp, *dp;
-		long swid, dwid, nb;
-		int dir;
+		s32int swid, dwid, nb;
+		u32int dir;
 
+DBG print("Simplemask|Fullmask\n");
 		if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min))
 			dir = -1;
 		else
 			dir = 1;
 
-		swid = src->width*sizeof(ulong);
-		dwid = dst->width*sizeof(ulong);
+		swid = src->width*sizeof(u32int);
+		dwid = dst->width*sizeof(u32int);
 		sp = byteaddr(src, par->sr.min);
 		dp = byteaddr(dst, par->r.min);
 		if(dir == -1){
@@ -2210,6 +2257,13 @@
 			dwid = -dwid;
 		}
 		nb = (dx*src->depth)/8;
+DBG print("swid %d dwid %d sp %p dp %p dir %d nb %d\n"
+	"	dst %p dst->data->bdata %p dst->zero %d"
+	" dst->width %d dst->depth %d\n"
+	"	par->r.min.x %d par->r.min.y %d\n",
+	swid, dwid, sp, dp, dir, nb,
+	dst, dst->data->bdata, dst->zero, dst->width, dst->depth,
+	par->r.min.x, par->r.min.y);
 		for(y=0; y<dy; y++, sp+=swid, dp+=dwid)
 			memmove(dp, sp, nb);
 		return 1;
@@ -2225,15 +2279,16 @@
 	&& (par->r.min.x&7)==(par->sr.min.x&7) && (par->r.min.x&7)==(par->mr.min.x&7)){
 		uchar *sp, *dp, *mp;
 		uchar lm, rm;
-		long swid, dwid, mwid;
-		int i, x, dir;
+		s32int swid, dwid, mwid;
+		s32int i, x, dir;
 
+DBG print("Simplemask|Simplesrc|Replmask|Replsrc");
 		sp = byteaddr(src, par->sr.min);
 		dp = byteaddr(dst, par->r.min);
 		mp = byteaddr(par->mask, par->mr.min);
-		swid = src->width*sizeof(ulong);
-		dwid = dst->width*sizeof(ulong);
-		mwid = par->mask->width*sizeof(ulong);
+		swid = src->width*sizeof(u32int);
+		dwid = dst->width*sizeof(u32int);
+		mwid = par->mask->width*sizeof(u32int);
 
 		if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min)){
 			dir = -1;
@@ -2322,17 +2377,17 @@
 static int
 chardraw(Memdrawparam *par)
 {
-	ulong bits;
+	u32int bits;
 	int i, ddepth, dy, dx, x, bx, ex, y, npack, bsh, depth, op;
-	ulong v, maskwid, dstwid;
+	u32int v, maskwid, dstwid;
 	uchar *wp, *rp, *q, *wc;
 	ushort *ws;
-	ulong *wl;
+	u32int *wl;
 	uchar sp[4];
 	Rectangle r, mr;
 	Memimage *mask, *src, *dst;
 
-if(0) if(drawdebug) iprint("chardraw? mf %lux md %d sf %lux dxs %d dys %d dd %d ddat %p sdat %p\n",
+if(0) if(drawdebug) iprint("chardraw? mf %ux md %d sf %ux dxs %d dys %d dd %d ddat %p sdat %p\n",
 		par->mask->flags, par->mask->depth, par->src->flags, 
 		Dx(par->src->r), Dy(par->src->r), par->dst->depth, par->dst->data, par->src->data);
 
@@ -2351,13 +2406,13 @@
 //if(drawdebug) iprint("chardraw...");
 
 	depth = mask->depth;
-	maskwid = mask->width*sizeof(ulong);
+	maskwid = mask->width*sizeof(u32int);
 	rp = byteaddr(mask, mr.min);
 	npack = 8/depth;
 	bsh = (mr.min.x % npack) * depth;
 
 	wp = byteaddr(dst, r.min);
-	dstwid = dst->width*sizeof(ulong);
+	dstwid = dst->width*sizeof(u32int);
 DBG print("bsh %d\n", bsh);
 	dy = Dy(r);
 	dx = Dx(r);
@@ -2397,7 +2452,7 @@
 				i = x&7;
 				if(i == 8-1)
 					bits = *q++;
-DBG print("bits %lux sh %d...", bits, i);
+DBG print("bits %ux sh %d...", bits, i);
 				if((bits>>i)&1)
 					*wc = v;
 			}
@@ -2409,7 +2464,7 @@
 				i = x&7;
 				if(i == 8-1)
 					bits = *q++;
-DBG print("bits %lux sh %d...", bits, i);
+DBG print("bits %ux sh %d...", bits, i);
 				if((bits>>i)&1)
 					*ws = v;
 			}
@@ -2420,7 +2475,7 @@
 				i = x&7;
 				if(i == 8-1)
 					bits = *q++;
-DBG print("bits %lux sh %d...", bits, i);
+DBG print("bits %ux sh %d...", bits, i);
 				if((bits>>i)&1){
 					wc[0] = sp[0];
 					wc[1] = sp[1];
@@ -2429,13 +2484,13 @@
 			}
 			break;
 		case 32:
-			wl = (ulong*)wp;
-			v = *(ulong*)sp;
+			wl = (u32int*)wp;
+			v = *(u32int*)sp;
 			for(x=bx; x>ex; x--, wl++){
 				i = x&7;
 				if(i == 8-1)
 					bits = *q++;
-DBG iprint("bits %lux sh %d...", bits, i);
+DBG iprint("bits %ux sh %d...", bits, i);
 				if((bits>>i)&1)
 					*wl = v;
 			}
@@ -2455,7 +2510,7 @@
  *
  * This code is just plain wrong for >8bpp.
  *
-ulong
+u32int
 membyteval(Memimage *src)
 {
 	int i, val, bpp;
@@ -2475,9 +2530,9 @@
  */
 
 void
-memfillcolor(Memimage *i, ulong val)
+memfillcolor(Memimage *i, u32int val)
 {
-	ulong bits;
+	u32int bits;
 	int d, y;
 	uchar p[4];
 
@@ -2498,7 +2553,7 @@
 		p[2] = bits>>16;
 		p[3] = bits>>24;
 		bits = *(u32int*)p;
-		memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));
+		memset32(u32addr(i, i->r.min), bits, i->width*Dy(i->r));
 		break;
 	}
 }
--- a/libmemdraw/drawtest.c
+++ b/libmemdraw/drawtest.c
@@ -306,9 +306,9 @@
 void
 checkline(Rectangle r, Point sp, Point mp, int y, Memimage *stmp, Memimage *mtmp)
 {
-	ulong *dp;
+	u32int *dp;
 	int nb;
-	ulong *saved;
+	u32int *saved;
 
 	dp = wordaddr(dst, Pt(0, y));
 	saved = savedstbits + y*dst->width;
--- a/libmemlayer/draw.c
+++ b/libmemlayer/draw.c
@@ -11,7 +11,7 @@
 	Memlayer		*dstlayer;
 	Memimage	*src;
 	Memimage	*mask;
-	int	op;
+	s32int	op;
 };
 
 static
--- a/libmemlayer/lalloc.c
+++ b/libmemlayer/lalloc.c
@@ -4,7 +4,7 @@
 #include "memlayer.h"
 
 Memimage*
-memlalloc(Memscreen *s, Rectangle screenr, Refreshfn refreshfn, void *refreshptr, ulong val)
+memlalloc(Memscreen *s, Rectangle screenr, Refreshfn refreshfn, void *refreshptr, u32int val)
 {
 	Memlayer *l;
 	Memimage *n;