gfxlib.c 47.3 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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
 /*
  * UAE - The Un*x Amiga Emulator
  *
  * graphics.library emulation
  *
  * Copyright 1996, 1997 Bernd Schmidt
  *
  * Ideas for this:
  * Rewrite layers completely. When there are lots of windows on the screen
  * it can take 3 minutes to update everything after resizing or moving one
  * (at least with Kick 1.3). Hide the internal structure of the layers as far
  * as possible, keep most of the data in emulator space so we save copying/
  * conversion time. Programs really shouldn't do anything directly with the
  * Layer or ClipRect structures.
  * This means that a lot of graphics.library functions will have to be
  * rewritten as well.
  * Once that's done, add support for non-planar bitmaps. Conveniently, the
  * struct Bitmap has an unused pad field which we could abuse as some sort of
  * type field. Need to add chunky<->planar conversion routines to get it
  * going, plus variants of all the drawing functions for speed reasons.
  *
  * When it becomes necessary to convert a structure from Amiga memory, make
  * a function with a name ending in ..FA, which takes a pointer to the
  * native structure and a uaecptr and returns the native pointer.
  */

#include "sysconfig.h"
#include "sysdeps.h"

#include <assert.h>

#include "options.h"
#include "threaddep/thread.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "xwin.h"
#include "autoconf.h"
#include "osemu.h"

#ifdef USE_EXECLIB

/* Uniq list management. Should be in a separate file. */
struct uniq_head {
    struct uniq_head *next;
    uae_u32 uniq;
};

typedef struct {
    struct uniq_head *head;
    uae_u32 uniq;
} uniq_list;

#define UNIQ_INIT { NULL, 1 }

static void init_uniq(uniq_list *list)
{
    list->head = NULL;
    list->uniq = 1;
}

static struct uniq_head *find_uniq (uniq_list *a, uae_u32 uniq)
{
    struct uniq_head *b = a->head;
    while (b && b->uniq != uniq)
	b = b->next;
    if (!b)
	write_log ("Couldn't find structure. Bad\n");
    return b;
}

static struct uniq_head *find_and_rem_uniq (uniq_list *a, uae_u32 uniq)
{
    struct uniq_head **b = &a->head, *c;
    while (*b && (*b)->uniq != uniq)
	b = &(*b)->next;
    c = *b;
    if (!c)
	write_log ("Couldn't find structure. Bad\n");
    else
	*b = c->next;
    return c;
}

static void add_uniq (uniq_list *a, struct uniq_head *item, uaecptr amem)
{
    item->uniq = a->uniq++;
    put_long (amem, item->uniq);
    if (a->uniq == 0)
	a->uniq++;
    item->next = a->head;
    a->head = item;
}

/* Graphics stuff begins here */
#define CLIPRECT_SIZE 40
#define LAYER_SIZE 160
#define LINFO_SIZE 102

static uaecptr gfxbase, layersbase;

static void do_LockLayer(uaecptr layer)
{
#if 0 /* Later.. */
    uaecptr sigsem = layer + 72;
    m68k_areg (regs, 0) = sigsem;
    CallLib (get_long (4), -564);
#else
    m68k_areg (regs, 1) = layer;
    CallLib (layersbase, -96);
#endif
}

static void do_UnlockLayer(uaecptr layer)
{
    m68k_areg (regs, 0) = layer;
    CallLib (layersbase, -102);
}

static uae_u32 gfxlibname, layerslibname;

struct Rectangle {
    int MinX, MinY, MaxX, MaxY;
};

static int GFX_PointInRectangle(uaecptr rect, int x, int y)
{
    uae_s16 minx = get_word (rect);
    uae_s16 miny = get_word (rect+2);
    uae_s16 maxx = get_word (rect+4);
    uae_s16 maxy = get_word (rect+6);

    if (x < minx || x > maxx || y < miny || y > maxy)
	return 0;
    return 1;
}

static int GFX_RectContainsRect(struct Rectangle *r1, struct Rectangle *r2)
{
    return (r2->MinX >= r1->MinX && r2->MaxX <= r1->MaxX
	    && r2->MinY >= r1->MinY && r2->MaxY <= r1->MaxY);
}

static struct Rectangle *GFX_RectFA(struct Rectangle *rp, uaecptr rect)
{
    rp->MinX = (uae_s16)get_word (rect);
    rp->MinY = (uae_s16)get_word (rect+2);
    rp->MaxX = (uae_s16)get_word (rect+4);
    rp->MaxY = (uae_s16)get_word (rect+6);
    return rp;
}

static int GFX_Bitmap_WritePixel(uaecptr bitmap, int x, int y, uaecptr rp)
{
    int i, offs;
    unsigned int bpr = get_word (bitmap);
    unsigned int rows = get_word (bitmap + 2);
    uae_u16 mask;

    uae_u8 planemask = get_byte (rp + 24);
    uae_u8 fgpen = get_byte (rp + 25);
    uae_u8 bgpen = get_byte (rp + 26);
    uae_u8 drmd = get_byte (rp + 28);
    uae_u8 pen = drmd & 4 ? bgpen : fgpen;

    if (x < 0 || y < 0 || x >= 8*bpr || y >= rows)
	return -1;

    offs = y*bpr + (x & ~15)/8;

    for (i = 0; i < get_byte (bitmap + 5); i++) {
	uaecptr planeptr;
	uae_u16 data;

	if ((planemask & (1 << i)) == 0)
	    continue;

	planeptr = get_long (bitmap + 8 + i*4);
	data = get_word (planeptr + offs);

	mask = 0x8000 >> (x & 15);

	if (drmd & 2) {
	    if ((pen & (1 << i)) != 0)
		data ^=mask;
	} else {
	    data &= ~mask;
	    if ((pen & (1 << i)) != 0)
		data |= mask;
	}
	put_word (planeptr + offs, data);
    }
    return 0;
}

int GFX_WritePixel(uaecptr rp, int x, int y)
{
    int v;
    uaecptr layer = get_long (rp);
    uaecptr bitmap = get_long (rp + 4);
    uaecptr cliprect;
    int x2, y2;

    if (bitmap == 0) {
	write_log ("bogus RastPort in WritePixel\n");
	return -1;
    }

    /* Easy case first */
    if (layer == 0) {
	return GFX_Bitmap_WritePixel(bitmap, x, y, rp);
    }
    do_LockLayer(layer);
    /*
     * Now, in theory we ought to obtain the semaphore.
     * Since we don't, the programs will happily write into the raster
     * even though we are currently moving the window around.
     * Not good.
     */

    x2 = x + (uae_s16)get_word (layer + 16);
    y2 = y + (uae_s16)get_word (layer + 18);

    if (!GFX_PointInRectangle (layer + 16, x2, y2)) {
	do_UnlockLayer(layer);
	return -1;
    }
    /* Find the right ClipRect */
    cliprect = get_long (layer + 8);
    while (cliprect != 0 && !GFX_PointInRectangle (cliprect + 16, x2, y2))
	cliprect = get_long (cliprect);
    if (cliprect == 0) {
	/* Don't complain: The "Dots" demo does this all the time. I
	 * suppose if we can't find a ClipRect, we aren't supposed to draw
	 * the dot.
	 */
	/*write_log ("Weirdness in WritePixel\n");*/
	v = -1;
    } else if (get_long (cliprect + 8) == 0) {
	v = GFX_Bitmap_WritePixel(bitmap, x2, y2, rp);
    } else if (get_long (cliprect + 12) == 0) {
	/* I don't really know what to do here... */
	v = 0;
    } else {
	/* This appears to be normal for smart refresh layers which are obscured */
	v = GFX_Bitmap_WritePixel (get_long (cliprect + 12), x2 - (uae_s16)get_word (cliprect + 16),
				   y2 - (uae_s16)get_word (cliprect + 18), rp);
    }
    do_UnlockLayer(layer);
    return v;
}


static uae_u32 gfxl_WritePixel(void) { return GFX_WritePixel(m68k_areg (regs, 1), (uae_s16)m68k_dreg (regs, 0), (uae_s16)m68k_dreg (regs, 1)); }

static uae_u32 gfxl_BltClear(void)
{
    uaecptr mem=m68k_areg (regs, 1);
    uae_u8 *mptr = chipmem_bank.xlateaddr(m68k_areg (regs, 1));
    uae_u32 count=m68k_dreg (regs, 0);
    uae_u32 flags=m68k_dreg (regs, 1);
    unsigned int i;
    uae_u32 pattern;

    if ((flags & 2) == 2){
	/* count is given in Rows / Bytes per row */
	count=(count & 0xFFFF) * (count >> 16);
    }

    if ((mem & 1) != 0 || (count & 1) != 0)
	write_log ("gfx: BltClear called with odd parameters\n");

    /* Bit 2 set means use pattern (V36+ only, but we might as well emulate
     * it always) */
    if ((flags & 4) == 0)
	pattern = 0;
    else
	pattern= ((flags >> 16) & 0xFFFF) | (flags & 0xFFFF0000ul);

    if ((pattern & 0xFF) == ((pattern >> 8) & 0xFF)) {
	memset(mptr, pattern, count);
	return 0;
    }

    for(i = 0; i < count; i += 4)
	chipmem_bank.lput(mem+i, pattern);

    if ((count & 3) != 0)
	chipmem_bank.wput(mem + i - 4, pattern);

    return 0;
}

static uae_u32 gfxl_BltBitmap(void)
{
    uaecptr srcbitmap = m68k_areg (regs, 0), dstbitmap = m68k_areg (regs, 1);
    int srcx = (uae_s16)m68k_dreg (regs, 0), srcy = (uae_s16)m68k_dreg (regs, 1);
    int dstx = (uae_s16)m68k_dreg (regs, 2), dsty = (uae_s16)m68k_dreg (regs, 3);
    int sizex = (uae_s16)m68k_dreg (regs, 4), sizey = (uae_s16)m68k_dreg (regs, 5);
    uae_u8 minterm = (uae_u8)m68k_dreg (regs, 6), mask = m68k_dreg (regs, 7);
    return 0; /* sam: a return was missing here ! */
}

static uaecptr amiga_malloc(int len)
{
    m68k_dreg (regs, 0) = len;
    m68k_dreg (regs, 1) = 1; /* MEMF_PUBLIC */
    return CallLib (get_long (4), -198); /* AllocMem */
}

static void amiga_free(uaecptr addr, int len)
{
    m68k_areg (regs, 1) = addr;
    m68k_dreg (regs, 0) = len;
    CallLib (get_long (4), -210); /* FreeMem */
}

/*
 * Region handling code
 *
 * General ideas stolen from xc/verylongpath/miregion.c
 *
 * The Clear code is untested. And and Or seem to work, Xor is only used
 * by the 1.3 Prefs program and seems to work, too.
 */

struct RegionRectangle {
    struct RegionRectangle *Next,*Prev;
    struct Rectangle bounds;
};

struct Region {
    struct Rectangle bounds;
    struct RegionRectangle *RegionRectangle;
};

struct RectList {
    int count;
    int space;
    struct Rectangle bounds;
    struct Rectangle *rects;
};

struct BandList {
    int count;
    int space;
    int *miny, *maxy;
};

static void init_bandlist(struct BandList *bl)
{
    bl->count = 0;
    bl->space = 20;
    bl->miny = (int *)malloc(20*sizeof(int));
    bl->maxy = (int *)malloc(20*sizeof(int));
}

static void dup_bandlist(struct BandList *to, struct BandList *from)
{
    to->count = from->count;
    to->space = to->count+4;
    to->miny = (int *)malloc (to->space*sizeof(int));
    to->maxy = (int *)malloc (to->space*sizeof(int));
    memcpy(to->miny, from->miny, to->count*sizeof(int));
    memcpy(to->maxy, from->maxy, to->count*sizeof(int));
}

STATIC_INLINE void add_band(struct BandList *bl, int miny, int maxy, int pos)
{
    if (bl->count == bl->space) {
	bl->space += 20;
	bl->miny = (int *)realloc(bl->miny, bl->space*sizeof(int));
	bl->maxy = (int *)realloc(bl->maxy, bl->space*sizeof(int));
    }
    memmove(bl->miny + pos + 1, bl->miny + pos, (bl->count - pos) * sizeof(int));
    memmove(bl->maxy + pos + 1, bl->maxy + pos, (bl->count - pos) * sizeof(int));
    bl->count++;
    bl->miny[pos] = miny;
    bl->maxy[pos] = maxy;
}

static void init_rectlist(struct RectList *rl)
{
    rl->count = 0;
    rl->space = 100;
    rl->bounds.MinX = rl->bounds.MinY = rl->bounds.MaxX = rl->bounds.MaxY = 0;
    rl->rects = (struct Rectangle *)malloc(100*sizeof(struct Rectangle));
}

static void dup_rectlist(struct RectList *to, struct RectList *from)
{
    to->count = from->count;
    to->space = to->count+4;
    to->bounds = from->bounds;
    to->rects = (struct Rectangle *)malloc (to->space*sizeof(struct Rectangle));
    memcpy(to->rects, from->rects, to->count*sizeof(struct Rectangle));
}

STATIC_INLINE void add_rect(struct RectList *rl, struct Rectangle r)
{
    if (rl->count == 0)
	rl->bounds = r;
    else {
	if (r.MinX < rl->bounds.MinX)
	    rl->bounds.MinX = r.MinX;
	if (r.MinY < rl->bounds.MinY)
	    rl->bounds.MinY = r.MinY;
	if (r.MaxX > rl->bounds.MaxX)
	    rl->bounds.MaxX = r.MaxX;
	if (r.MaxY > rl->bounds.MaxY)
	    rl->bounds.MaxY = r.MaxY;
    }
    if (rl->count == rl->space) {
	rl->space += 100;
	rl->rects = (struct Rectangle *)realloc(rl->rects, rl->space*sizeof(struct Rectangle));
    }
    rl->rects[rl->count++] = r;
}

STATIC_INLINE void rem_rect(struct RectList *rl, int num)
{
    rl->count--;
    if (num == rl->count)
	return;
    rl->rects[num] = rl->rects[rl->count];
}

static void free_rectlist(struct RectList *rl)
{
    free(rl->rects);
}

static void free_bandlist(struct BandList *bl)
{
    free(bl->miny);
    free(bl->maxy);
}

static int regionrect_cmpfn(const void *a, const void *b)
{
    struct Rectangle *ra = (struct Rectangle *)a;
    struct Rectangle *rb = (struct Rectangle *)b;

    if (ra->MinY < rb->MinY)
	return -1;
    if (ra->MinY > rb->MinY)
	return 1;
    if (ra->MinX < rb->MinX)
	return -1;
    if (ra->MinX > rb->MinX)
	return 1;
    if (ra->MaxX < rb->MaxX)
	return -1;
    return 1;
}

STATIC_INLINE int min(int x, int y)
{
    return x < y ? x : y;
}

STATIC_INLINE int max(int x, int y)
{
    return x > y ? x : y;
}

static void add_rect_to_bands(struct BandList *bl, struct Rectangle *rect)
{
    int j;
    struct Rectangle tmpr = *rect;

    for (j = 0; j < bl->count; j++) {
	/* Is the current band before the rectangle? */
	if (bl->maxy[j] < tmpr.MinY)
	    continue;
	/* Band already present? */
	if (bl->miny[j] == tmpr.MinY && bl->maxy[j] == tmpr.MaxY)
	    break;
	/* Completely new band? Add it */
	if (bl->miny[j] > tmpr.MaxY) {
	    add_band(bl, tmpr.MinY, tmpr.MaxY, j);
	    break;
	}
	/* Now we know that the bands are overlapping.
	 * See whether they match in one point */
	if (bl->miny[j] == tmpr.MinY) {
	    int t;
	    if (bl->maxy[j] < tmpr.MaxY) {
		/* Rectangle exceeds band */
		tmpr.MinY = bl->maxy[j]+1;
		continue;
	    }
	    /* Rectangle splits band */
	    t = bl->maxy[j];
	    bl->maxy[j] = tmpr.MaxY;
	    tmpr.MinY = bl->maxy[j] + 1;
	    tmpr.MaxY = t;
	    continue;
	} else if (bl->maxy[j] == tmpr.MaxY) {
	    int t;
	    if (bl->miny[j] > tmpr.MinY) {
		/* Rectangle exceeds band */
		t = bl->miny[j];
		bl->miny[j] = tmpr.MinY;
		bl->maxy[j] = t-1;
		tmpr.MinY = t;
		continue;
	    }
	    /* Rectangle splits band */
	    bl->maxy[j] = tmpr.MinY - 1;
	    continue;
	}
	/* Bands overlap and match in no points. Get a new band and align */
	if (bl->miny[j] > tmpr.MinY) {
	    /* Rectangle begins before band, so make a new band before
	     * and adjust rectangle */
	    add_band(bl, tmpr.MinY, bl->miny[j] - 1, j);
	    tmpr.MinY = bl->miny[j+1];
	} else {
	    /* Rectangle begins in band */
	    add_band(bl, bl->miny[j], tmpr.MinY - 1, j);
	    bl->miny[j+1] = tmpr.MinY;
	}
	continue;
    }
    if (j == bl->count)
	add_band(bl, tmpr.MinY, tmpr.MaxY, j);
}

static void region_addbands(struct RectList *rl, struct BandList *bl)
{
    int i,j;

    for (i = 0; i < rl->count; i++) {
	add_rect_to_bands(bl, rl->rects + i);
    }
}

static void merge_bands(struct BandList *dest, struct BandList *src)
{
    int i;
    for (i = 0; i < src->count; i++) {
	struct Rectangle tmp;
	tmp.MinY = src->miny[i];
	tmp.MaxY = src->maxy[i];
	add_rect_to_bands(dest, &tmp);
    }
}

static void region_splitrects_band(struct RectList *rl, struct BandList *bl)
{
    int i,j;
    for (i = 0; i < rl->count; i++) {
	for (j = 0; j < bl->count; j++) {
	    if (bl->miny[j] == rl->rects[i].MinY && bl->maxy[j] == rl->rects[i].MaxY)
		break;
	    if (rl->rects[i].MinY > bl->maxy[j])
		continue;
	    if (bl->miny[j] == rl->rects[i].MinY) {
		struct Rectangle tmpr;
		tmpr.MinX = rl->rects[i].MinX;
		tmpr.MaxX = rl->rects[i].MaxX;
		tmpr.MinY = bl->maxy[j] + 1;
		tmpr.MaxY = rl->rects[i].MaxY;
		add_rect(rl, tmpr); /* will be processed later */
		rl->rects[i].MaxY = bl->maxy[j];
		break;
	    }
	    write_log ("Foo..\n");
	}
    }
    qsort(rl->rects, rl->count, sizeof (struct Rectangle), regionrect_cmpfn);
}

static void region_coalesce_rects(struct RectList *rl, int do_2nd_pass)
{
    int i,j;

    /* First pass: Coalesce horizontally */
    for (i = j = 0; i < rl->count;) {
	int offs = 1;
	while (i + offs < rl->count) {
	    if (rl->rects[i].MinY != rl->rects[i+offs].MinY
		|| rl->rects[i].MaxY != rl->rects[i+offs].MaxY
		|| rl->rects[i].MaxX+1 < rl->rects[i+offs].MinX)
		break;
	    rl->rects[i].MaxX = rl->rects[i+offs].MaxX;
	    offs++;
	}
	rl->rects[j++] = rl->rects[i];
	i += offs;
    }
    rl->count = j;

    if (!do_2nd_pass)
	return;

    /* Second pass: Coalesce bands */
    for (i = 0; i < rl->count;) {
	int match = 0;
	for (j = i + 1; j < rl->count; j++)
	    if (rl->rects[i].MinY != rl->rects[j].MinY)
		break;
	if (j < rl->count && rl->rects[i].MaxY + 1 == rl->rects[j].MinY) {
	    int k;
	    match = 1;
	    for (k = 0; i+k < j; k++) {
		if (j+k >= rl->count
		    || rl->rects[j+k].MinY != rl->rects[j].MinY)
		{
		    match = 0; break;
		}
		if (rl->rects[i+k].MinX != rl->rects[j+k].MinX
		    || rl->rects[i+k].MaxX != rl->rects[j+k].MaxX)
		{
		    match = 0;
		    break;
		}
	    }
	    if (j+k < rl->count && rl->rects[j+k].MinY == rl->rects[j].MinY)
		match = 0;
	    if (match) {
		for (k = 0; i+k < j; k++)
		    rl->rects[i+k].MaxY = rl->rects[j].MaxY;
		memmove(rl->rects + j, rl->rects + j + k, (rl->count - j - k)*sizeof(struct Rectangle));
		rl->count -= k;
	    }
	}
	if (!match)
	    i = j;
    }
}

static int copy_rects (uaecptr region, struct RectList *rl)
{
    uaecptr regionrect;
    int numrects = 0;
    struct Rectangle b;
    regionrect = get_long (region+8);
    b.MinX = get_word (region);
    b.MinY = get_word (region+2);
    b.MaxX = get_word (region+4);
    b.MaxY = get_word (region+6);

    while (regionrect != 0) {
	struct Rectangle tmpr;

	tmpr.MinX = (uae_s16)get_word (regionrect+8)  + b.MinX;
	tmpr.MinY = (uae_s16)get_word (regionrect+10) + b.MinY;
	tmpr.MaxX = (uae_s16)get_word (regionrect+12) + b.MinX;
	tmpr.MaxY = (uae_s16)get_word (regionrect+14) + b.MinY;
	add_rect(rl, tmpr);
	regionrect = get_long (regionrect);
	numrects++;
    }
    return numrects;
}

static int rect_in_region(struct RectList *rl, struct Rectangle *r)
{
    int i;
    int miny = r->MinY;

    for (i = 0; i < rl->count; i++) {
	int j;
	if (rl->rects[i].MaxY < miny)
	    continue;
	if (rl->rects[i].MinY > miny)
	    break;
	if (rl->rects[i].MaxX < r->MinX)
	    continue;
	if (rl->rects[i].MinX > r->MaxX)
	    break;
	/* Overlap! */
	j = i;
	for (;;) {
	    if (rl->rects[j].MaxX > r->MaxX) {
		miny = rl->rects[i].MaxY + 1;
		break;
	    }
	    j++;
	    if (j == rl->count)
		break;
	    if (rl->rects[j].MinX != rl->rects[j-1].MaxX+1)
		break;
	    if (rl->rects[i].MinY != rl->rects[j].MinY)
		break;
	}
	if (miny <= rl->rects[i].MaxY)
	    break;
    }
    return 0;
}

typedef void (*regionop)(struct RectList *,struct RectList *,struct RectList *);

static void region_do_ClearRegionRegion(struct RectList *rl1,struct RectList *rl2,
					struct RectList *rl3)
{
    int i,j;

    for (i = j = 0; i < rl2->count && j < rl1->count;) {
	struct Rectangle tmpr;

	while ((rl1->rects[j].MinY < rl2->rects[i].MinY
		|| (rl1->rects[j].MinY == rl2->rects[i].MinY
		    && rl1->rects[j].MaxX < rl2->rects[i].MinX))
	       && j < rl1->count)
	    j++;
	if (j >= rl1->count)
	    break;
	while ((rl1->rects[j].MinY > rl2->rects[i].MinY
		|| (rl1->rects[j].MinY == rl2->rects[i].MinY
		    && rl1->rects[j].MinX > rl2->rects[i].MaxX))
	       && i < rl2->count)
	{
	    add_rect(rl3, rl2->rects[i]);
	    i++;
	}
	if (i >= rl2->count)
	    break;

	tmpr = rl2->rects[i];

	while (i < rl2->count && j < rl1->count
	       && rl1->rects[j].MinY == tmpr.MinY
	       && rl2->rects[i].MinY == tmpr.MinY
	       && rl1->rects[j].MinX <= rl2->rects[i].MaxX
	       && rl1->rects[j].MaxX >= rl2->rects[i].MinX)
	{
	    int oldmin = tmpr.MinX;
	    int oldmax = tmpr.MaxX;
	    if (tmpr.MinX < rl1->rects[j].MinX) {
		tmpr.MaxX = rl1->rects[j].MinX - 1;
		add_rect(rl3, tmpr);
	    }
	    if (oldmax <= rl1->rects[j].MaxX) {
		i++;
		if (i < rl2->count && rl2->rects[i].MinY == tmpr.MinY)
		    tmpr = rl2->rects[i];
	    } else {
		tmpr.MinX = rl1->rects[j].MaxX + 1;
		tmpr.MaxX = oldmax;
		j++;
	    }
	}
    }
    for(; i < rl2->count; i++)
	add_rect(rl3, rl2->rects[i]);
}

static void region_do_AndRegionRegion(struct RectList *rl1,struct RectList *rl2,
				      struct RectList *rl3)
{
    int i,j;

    for (i = j = 0; i < rl2->count && j < rl1->count;) {
	while ((rl1->rects[j].MinY < rl2->rects[i].MinY
		|| (rl1->rects[j].MinY == rl2->rects[i].MinY
		    && rl1->rects[j].MaxX < rl2->rects[i].MinX))
	       && j < rl1->count)
	    j++;
	if (j >= rl1->count)
	    break;
	while ((rl1->rects[j].MinY > rl2->rects[i].MinY
		|| (rl1->rects[j].MinY == rl2->rects[i].MinY
		    && rl1->rects[j].MinX > rl2->rects[i].MaxX))
	       && i < rl2->count)
	    i++;
	if (i >= rl2->count)
	    break;
	if (rl1->rects[j].MinY == rl2->rects[i].MinY
	    && rl1->rects[j].MinX <= rl2->rects[i].MaxX
	    && rl1->rects[j].MaxX >= rl2->rects[i].MinX)
	{
	    /* We have an intersection! */
	    struct Rectangle tmpr;
	    tmpr = rl2->rects[i];
	    if (tmpr.MinX < rl1->rects[j].MinX)
		tmpr.MinX = rl1->rects[j].MinX;
	    if (tmpr.MaxX > rl1->rects[j].MaxX)
		tmpr.MaxX = rl1->rects[j].MaxX;
	    add_rect(rl3, tmpr);
	    if (rl1->rects[j].MaxX == rl2->rects[i].MaxX)
		i++, j++;
	    else if (rl1->rects[j].MaxX > rl2->rects[i].MaxX)
		i++;
	    else
		j++;
	}
    }
}

static void region_do_OrRegionRegion(struct RectList *rl1,struct RectList *rl2,
				     struct RectList *rl3)
{
    int i,j;

    for (i = j = 0; i < rl2->count && j < rl1->count;) {
	while ((rl1->rects[j].MinY < rl2->rects[i].MinY
		|| (rl1->rects[j].MinY == rl2->rects[i].MinY
		    && rl1->rects[j].MaxX < rl2->rects[i].MinX))
	       && j < rl1->count)
	{
	    add_rect(rl3, rl1->rects[j]);
	    j++;
	}
	if (j >= rl1->count)
	    break;
	while ((rl1->rects[j].MinY > rl2->rects[i].MinY
		|| (rl1->rects[j].MinY == rl2->rects[i].MinY
		    && rl1->rects[j].MinX > rl2->rects[i].MaxX))
	       && i < rl2->count)
	{
	    add_rect(rl3, rl2->rects[i]);
	    i++;
	}
	if (i >= rl2->count)
	    break;
	if (rl1->rects[j].MinY == rl2->rects[i].MinY
	    && rl1->rects[j].MinX <= rl2->rects[i].MaxX
	    && rl1->rects[j].MaxX >= rl2->rects[i].MinX)
	{
	    /* We have an intersection! */
	    struct Rectangle tmpr;
	    tmpr = rl2->rects[i];
	    if (tmpr.MinX > rl1->rects[j].MinX)
		tmpr.MinX = rl1->rects[j].MinX;
	    if (tmpr.MaxX < rl1->rects[j].MaxX)
		tmpr.MaxX = rl1->rects[j].MaxX;
	    i++; j++;
	    for (;;) {
		int cont = 0;
		if (j < rl1->count && rl1->rects[j].MinY == tmpr.MinY
		    && tmpr.MaxX+1 >= rl1->rects[j].MinX) {
		    if (tmpr.MaxX < rl1->rects[j].MaxX)
			tmpr.MaxX = rl1->rects[j].MaxX;
		    j++; cont = 1;
		}
		if (i < rl2->count && rl2->rects[i].MinY == tmpr.MinY
		    && tmpr.MaxX+1 >= rl2->rects[i].MinX) {
		    if (tmpr.MaxX < rl2->rects[i].MaxX)
			tmpr.MaxX = rl2->rects[i].MaxX;
		    i++; cont = 1;
		}
		if (!cont)
		    break;
	    }
	    add_rect(rl3, tmpr);
	}
    }
    for(; i < rl2->count; i++)
	add_rect(rl3, rl2->rects[i]);
    for(; j < rl1->count; j++)
	add_rect(rl3, rl1->rects[j]);
}

static void region_do_XorRegionRegion(struct RectList *rl1,struct RectList *rl2,
				      struct RectList *rl3)
{
    int i,j;

    for (i = j = 0; i < rl2->count && j < rl1->count;) {
	struct Rectangle tmpr1, tmpr2;

	while ((rl1->rects[j].MinY < rl2->rects[i].MinY
		|| (rl1->rects[j].MinY == rl2->rects[i].MinY
		    && rl1->rects[j].MaxX < rl2->rects[i].MinX))
	       && j < rl1->count)
	{
	    add_rect(rl3, rl1->rects[j]);
	    j++;
	}
	if (j >= rl1->count)
	    break;
	while ((rl1->rects[j].MinY > rl2->rects[i].MinY
		|| (rl1->rects[j].MinY == rl2->rects[i].MinY
		    && rl1->rects[j].MinX > rl2->rects[i].MaxX))
	       && i < rl2->count)
	{
	    add_rect(rl3, rl2->rects[i]);
	    i++;
	}
	if (i >= rl2->count)
	    break;

	tmpr2 = rl2->rects[i];
	tmpr1 = rl1->rects[j];

	while (i < rl2->count && j < rl1->count
	       && rl1->rects[j].MinY == tmpr1.MinY
	       && rl2->rects[i].MinY == tmpr1.MinY
	       && rl1->rects[j].MinX <= rl2->rects[i].MaxX
	       && rl1->rects[j].MaxX >= rl2->rects[i].MinX)
	{
	    int oldmin2 = tmpr2.MinX;
	    int oldmax2 = tmpr2.MaxX;
	    int oldmin1 = tmpr1.MinX;
	    int oldmax1 = tmpr1.MaxX;
	    int need_1 = 0, need_2 = 0;

	    if (tmpr2.MinX > tmpr1.MinX	&& tmpr2.MaxX < tmpr1.MaxX)
	    {
		/*
		 *    ###########
		 *       ****
		 */
		tmpr1.MaxX = tmpr2.MinX - 1;
		add_rect(rl3, tmpr1);
		tmpr1.MaxX = oldmax1;
		tmpr1.MinX = tmpr2.MaxX + 1;
		add_rect(rl3, tmpr1);
		need_2 = 1;
	    } else if (tmpr2.MinX > tmpr1.MinX && tmpr2.MaxX > tmpr1.MaxX) {
		/*
		 *    ##########
		 *       *********
		 */
		tmpr1.MaxX = tmpr2.MinX - 1;
		add_rect(rl3, tmpr1);
		tmpr2.MinX = oldmax1 + 1;
		add_rect(rl3, tmpr2);
		need_1 = 1;
	    } else if (tmpr2.MinX < tmpr1.MinX && tmpr2.MaxX < tmpr1.MaxX) {
		/*
		 *       ##########
		 *    *********
		 */
		tmpr2.MaxX = tmpr1.MinX - 1;
		add_rect(rl3, tmpr2);
		tmpr1.MinX = oldmax2 + 1;
		add_rect(rl3, tmpr1);
		need_2 = 1;
	    } else if (tmpr2.MinX < tmpr1.MinX && tmpr2.MaxX > tmpr1.MaxX) {
		/*
		 *       ###
		 *    *********
		 */
		tmpr2.MaxX = tmpr1.MinX - 1;
		add_rect(rl3, tmpr2);
		tmpr2.MaxX = oldmax2;
		tmpr2.MinX = tmpr1.MaxX + 1;
		add_rect(rl3, tmpr2);
		need_1 = 1;
	    } else if (tmpr1.MinX == tmpr2.MinX && tmpr2.MaxX < tmpr1.MaxX) {
		/*
		 *    #############
		 *    *********
		 */
		tmpr1.MinX = tmpr2.MaxX + 1;
		need_2 = 1;
	    } else if (tmpr1.MinX == tmpr2.MinX && tmpr2.MaxX > tmpr1.MaxX) {
		/*
		 *    #########
		 *    *************
		 */
		tmpr2.MinX = tmpr1.MaxX + 1;
		need_1 = 1;
	    } else if (tmpr1.MinX < tmpr2.MinX && tmpr2.MaxX == tmpr1.MaxX) {
		/*
		 *    #############
		 *        *********
		 */
		tmpr1.MaxX = tmpr2.MinX - 1;
		add_rect(rl3, tmpr1);
		need_2 = need_1 = 1;
	    } else if (tmpr1.MinX > tmpr2.MinX && tmpr2.MaxX == tmpr1.MaxX) {
		/*
		 *        #########
		 *    *************
		 */
		tmpr2.MaxX = tmpr1.MinX - 1;
		add_rect(rl3, tmpr2);
		need_2 = need_1 = 1;
	    } else {
		assert(tmpr1.MinX == tmpr2.MinX && tmpr2.MaxX == tmpr1.MaxX);
		need_1 = need_2 = 1;
	    }
	    if (need_1) {
		j++;
		if (j < rl1->count && rl1->rects[j].MinY == tmpr1.MinY)
		    tmpr1 = rl1->rects[j];
	    }
	    if (need_2) {
		i++;
		if (i < rl2->count && rl2->rects[i].MinY == tmpr2.MinY)
		    tmpr2 = rl2->rects[i];
	    }
	}
    }
    for(; i < rl2->count; i++)
	add_rect(rl3, rl2->rects[i]);
    for(; j < rl1->count; j++)
	add_rect(rl3, rl1->rects[j]);
}

static uae_u32 gfxl_perform_regionop(regionop op, int with_rect)
{
    int i,j,k;
    uaecptr reg1;
    uaecptr reg2;
    uaecptr tmp, rpp;
    struct RectList rl1, rl2, rl3;
    struct BandList bl;

    int retval = 0;
    int numrects2;

    init_rectlist(&rl1); init_rectlist(&rl2); init_rectlist(&rl3);

    if (with_rect) {
	struct Rectangle tmpr;
	reg2 = m68k_areg (regs, 0);
	numrects2 = copy_rects(reg2, &rl2);
	tmpr.MinX = get_word (m68k_areg (regs, 1));
	tmpr.MinY = get_word (m68k_areg (regs, 1) + 2);
	tmpr.MaxX = get_word (m68k_areg (regs, 1) + 4);
	tmpr.MaxY = get_word (m68k_areg (regs, 1) + 6);
	add_rect(&rl1, tmpr);
    } else {
	reg1 = m68k_areg (regs, 0);
	reg2 = m68k_areg (regs, 1);

	copy_rects(reg1, &rl1);
	numrects2 = copy_rects(reg2, &rl2);
    }

    init_bandlist(&bl);
    region_addbands(&rl1, &bl);
    region_addbands(&rl2, &bl);
    region_splitrects_band(&rl1, &bl);
    region_splitrects_band(&rl2, &bl);
    region_coalesce_rects(&rl1, 0);
    region_coalesce_rects(&rl2, 0);

    (*op)(&rl1, &rl2, &rl3);
    region_coalesce_rects(&rl3, 1);

    rpp = reg2 + 8;
    if (rl3.count < numrects2) {
	while (numrects2-- != rl3.count) {
	    tmp = get_long (rpp);
	    put_long (rpp, get_long (tmp));
	    amiga_free(tmp, 16);
	}
	if (rl3.count > 0)
	    put_long (get_long (rpp) + 4, rpp);
    } else if (rl3.count > numrects2) {
	while(numrects2++ != rl3.count) {
	    uaecptr prev = get_long (rpp);
	    tmp = amiga_malloc(16);
	    if (tmp == 0)
		goto done;
	    put_long (tmp, prev);
	    put_long (tmp + 4, rpp);
	    if (prev != 0)
		put_long (prev + 4, tmp);
	    put_long (rpp, tmp);
	}
    }

    if (rl3.count > 0) {
	rpp = reg2 + 8;
	for (i = 0; i < rl3.count; i++) {
	    uaecptr rr = get_long (rpp);
	    put_word (rr+8, rl3.rects[i].MinX - rl3.bounds.MinX);
	    put_word (rr+10, rl3.rects[i].MinY - rl3.bounds.MinY);
	    put_word (rr+12, rl3.rects[i].MaxX - rl3.bounds.MinX);
	    put_word (rr+14, rl3.rects[i].MaxY - rl3.bounds.MinY);
	    rpp = rr;
	}
	if (get_long (rpp) != 0)
	    write_log ("BUG\n");
    }
    put_word (reg2+0, rl3.bounds.MinX);
    put_word (reg2+2, rl3.bounds.MinY);
    put_word (reg2+4, rl3.bounds.MaxX);
    put_word (reg2+6, rl3.bounds.MaxY);
    retval = 1;

    done:
    free_rectlist(&rl1); free_rectlist(&rl2); free_rectlist(&rl3);
    free_bandlist(&bl);

    return retval;
}

static uae_u32 gfxl_AndRegionRegion(void)
{
    return gfxl_perform_regionop(region_do_AndRegionRegion, 0);
}
static uae_u32 gfxl_XorRegionRegion(void)
{
    return gfxl_perform_regionop(region_do_XorRegionRegion, 0);
}
static uae_u32 gfxl_OrRegionRegion(void)
{
    return gfxl_perform_regionop(region_do_OrRegionRegion, 0);
}

static uae_u32 gfxl_ClearRectRegion(void)
{
    return gfxl_perform_regionop(region_do_ClearRegionRegion, 1);
}
static uae_u32 gfxl_OrRectRegion(void)
{
    return gfxl_perform_regionop(region_do_OrRegionRegion, 1);
}

static uae_u32 gfxl_AndRectRegion(void)
{
    return gfxl_perform_regionop(region_do_AndRegionRegion, 1);
}

static uae_u32 gfxl_XorRectRegion(void)
{
    return gfxl_perform_regionop(region_do_XorRegionRegion, 1);
}


/* Layers code */

static uae_u32 LY_TryLockLayer(uaecptr layer)
{
    uaecptr sigsem = layer + 72;

    m68k_areg (regs, 0) = sigsem;
    return CallLib (get_long (4), -576);
}

static void LY_LockLayer(uaecptr layer)
{
    uaecptr sigsem = layer + 72;

    m68k_areg (regs, 0) = sigsem;
    CallLib (get_long (4), -564);
}

static void LY_UnlockLayer(uaecptr layer)
{
    uaecptr sigsem = layer + 72;

    m68k_areg (regs, 0) = sigsem;
    CallLib (get_long (4), -570);
}

static void LY_LockLayerInfo(uaecptr li)
{
    uaecptr sigsem = li + 24;

    m68k_areg (regs, 0) = sigsem;
    CallLib (get_long (4), -564);
    put_byte (li+91, get_byte (li+91)+1);
}

static void LY_UnlockLayerInfo(uaecptr li)
{
    uaecptr sigsem = li + 24;

    put_byte (li+91, get_byte (li+91)-1);
    m68k_areg (regs, 0) = sigsem;
    CallLib (get_long (4), -570);
}

static void LY_LockLayers(uaecptr li)
{
    uaecptr l = get_long (li);
    LY_LockLayerInfo(li);
    while (l != 0) {
	LY_LockLayer(l);
	l = get_long (l);
    }
    LY_UnlockLayerInfo(li);
}

static void LY_UnlockLayers(uaecptr li)
{
    uaecptr l = get_long (li);
    LY_LockLayerInfo(li);
    while (l != 0) {
	LY_UnlockLayer(l);
	l = get_long (l);
    }
    LY_UnlockLayerInfo(li);
}

#define LAYER_CLUELESS	 0x8000 /* Indicates we know nothing about the layer's regions. */
#define LAYER_CR_CHANGED 0x4000 /* Indicates that the cliprects in Amiga memory need to be re-done */
#define LAYER_REDO	 0x2000 /* Indicates that we have regions, but they are bogus. */

static uae_u32 layer_uniq = 1;

struct MyLayerInfo {
    struct uniq_head head;
    uaecptr amigaos_linfo;
    uniq_list layer_list;
};

struct MyLayer {
    struct uniq_head head;
    uaecptr amigaos_layer, rastport;
    struct Rectangle bounds;
    struct RectList clipregion;
    struct RectList obscured;
    struct RectList visible;
    struct BandList big_bands; /* created by obscuring layers */
    struct BandList small_bands; /* big_bands + those from clipregion */
    struct RectList damage;
    struct BandList damage_bands;
    struct MyLayerInfo *mli;
};

static uniq_list MyLayerInfo_list = UNIQ_INIT;

static void LY_InitLayers(uaecptr li)
{
    memset (get_real_address (li), 0, 92);
    put_long (li + 0, 0); /* top layer */
    put_long (li+84, 0); /* uniq: */
    m68k_areg (regs, 0) = li + 24; CallLib (get_long (4), -558); /* InitSemaphore() */
    put_word (li+88, 0); /* flags (???) */
    put_byte (li+89, 0); /* fatten_count */
    /* @@@ How big can I assume the structure? What's all this 1.0/1.1 cruft? */
}

static void LY_FattenLayerInfo(uaecptr li)
{
    struct MyLayerInfo *mli;
    int fatten_count = get_byte (li + 89);
    if (fatten_count == 0) {
	mli = (struct MyLayerInfo *)malloc(sizeof(struct MyLayerInfo));
	add_uniq(&MyLayerInfo_list, &mli->head, li + 84);
	init_uniq(&mli->layer_list);
	mli->amigaos_linfo = li;
    }
    put_byte (li + 89, fatten_count + 1);
}

static void LY_ThinLayerInfo(uaecptr li)
{
    int fatten_count = get_byte (li + 89)-1;
    put_byte (li + 89, fatten_count);
    if (fatten_count == 0) {
	struct MyLayerInfo *mli = (struct MyLayerInfo *)find_and_rem_uniq(&MyLayerInfo_list, get_long (li+84));
	if (mli)
	    free(mli);
    }
}

static void build_cliprect (struct MyLayer *l, struct Rectangle *bounds,
			    int obscured, uaecptr *crp, uaecptr *prev)
{
    uaecptr cr = get_long (*crp);
    if (cr == 0) {
	put_long (*crp, cr = amiga_malloc(CLIPRECT_SIZE));
	put_long (cr, 0);
    }
    *prev = cr;
    *crp = cr;
    put_word (cr + 16, bounds->MinX);
    put_word (cr + 18, bounds->MinY);
    put_word (cr + 20, bounds->MaxX);
    put_word (cr + 22, bounds->MaxY);
    put_long (cr + 8, obscured ? l->amigaos_layer : 0); /* cheat */
    put_long (cr + 12, 0); /* no smart refresh yet */
}

static void build_cliprects (struct MyLayer *l)
{
    uaecptr layer = l->amigaos_layer;
    uaecptr cr = layer + 8;
    uaecptr prev = 0;
    uae_u16 flags = get_word (layer + 30);
    int i;

    if ((flags & LAYER_CR_CHANGED) == 0)
	return;
    put_word (layer + 30, flags & ~LAYER_CR_CHANGED);
    for (i = 0; i < l->obscured.count; i++) {
	build_cliprect (l, l->obscured.rects + i, 1, &cr, &prev);
    }
    for (i = 0; i < l->visible.count; i++) {
	build_cliprect (l, l->visible.rects + i, 1, &cr, &prev);
    }
    while ((prev = get_long (cr))) {
	put_long (cr, get_long (prev));
	amiga_free (prev, CLIPRECT_SIZE);
    }
}

static void propagate_clueless_redo (struct MyLayerInfo *mli)
{
    /* For all CLUELESS layers, set the REDO bit for all layers below it that overlap it
     * and delete the data associated with them. */
    uaecptr current_l = get_long (mli->amigaos_linfo);
    while (current_l) {
	struct MyLayer *l = (struct MyLayer *)find_uniq(&mli->layer_list, get_long (current_l + 24));
	if ((get_word (l->amigaos_layer + 32) & LAYER_CLUELESS) != 0) {
	    uaecptr next_l = get_long (current_l);
	    put_word (l->amigaos_layer + 32, get_word (l->amigaos_layer + 32) | LAYER_REDO);
	    while (next_l) {
		struct MyLayer *l2 = (struct MyLayer *)find_uniq(&mli->layer_list, get_long (next_l + 24));
		uae_u16 flags = get_word (l2->amigaos_layer + 32);
		if (l2->bounds.MinX <= l->bounds.MaxX && l->bounds.MinX <= l2->bounds.MaxX
		    && l2->bounds.MinY <= l->bounds.MaxY && l->bounds.MinY <= l2->bounds.MaxY)
		    put_word (l2->amigaos_layer + 32, flags | LAYER_REDO);
		if ((flags & (LAYER_REDO|LAYER_CLUELESS)) == 0) {
		    free_rectlist(&l->obscured);
		    free_rectlist(&l->visible);
		    free_bandlist(&l->big_bands);
		    free_bandlist(&l->small_bands);
		}
		next_l = get_long (next_l);
	    }
	}
	current_l = get_long (current_l);
    }
}

static void redo_layers(struct MyLayerInfo *mli, uaecptr bm)
{
    uaecptr current_l;
    struct RectList tmp_rl;

    propagate_clueless_redo(mli);
    current_l = get_long (mli->amigaos_linfo);

    while (current_l) {
	struct MyLayer *l = (struct MyLayer *)find_uniq(&mli->layer_list, get_long (current_l + 24));
	uae_u16 flags = get_word (l->amigaos_layer + 32);
	if ((flags & LAYER_REDO) != 0) {
	    uaecptr next_l = get_long (current_l+4);
	    int have_rects = 0;

	    init_rectlist(&l->obscured);
	    init_bandlist(&l->big_bands);
	    add_rect_to_bands(&l->big_bands, &l->bounds);

	    while (next_l) {
		struct MyLayer *l2 = (struct MyLayer *)find_uniq(&mli->layer_list, get_long (next_l + 24));
		if (l2->visible.bounds.MinX <= l->bounds.MaxX && l->bounds.MinX <= l2->visible.bounds.MaxX
		    && l2->visible.bounds.MinY <= l->bounds.MaxY && l->bounds.MinY <= l2->visible.bounds.MaxY
		    && !rect_in_region (&l->obscured, &l2->visible.bounds))
		{
		    add_rect_to_bands(&l->big_bands, &l2->visible.bounds);
		    add_rect(&l->obscured, l2->visible.bounds);
		    have_rects++;
		}
		next_l = get_long (next_l+4);
	    }
	    init_rectlist(&l->visible);
	    init_rectlist(&tmp_rl);
	    add_rect (&tmp_rl, l->bounds);

	    region_splitrects_band(&l->obscured, &l->big_bands);
	    region_splitrects_band(&tmp_rl, &l->big_bands);
	    region_do_ClearRegionRegion(&l->obscured, &tmp_rl, &l->visible);
	    flags |= LAYER_CR_CHANGED;
	}
	put_word (l->amigaos_layer + 32, flags & ~(LAYER_CLUELESS|LAYER_REDO));
	current_l = get_long (current_l);
    }
}

static struct MyLayer *LY_NewLayer(struct MyLayerInfo *mli, int x0, int x1, int y0, int y1,
				   uae_u16 flags, uaecptr bm, uaecptr sbm)
{
    struct MyLayer *l = (struct MyLayer *)malloc(sizeof (struct MyLayer));
    uaecptr layer = amiga_malloc(LAYER_SIZE);
    memset (get_real_address (layer), 0, LAYER_SIZE);
    l->amigaos_layer = layer;

    put_word (layer + 16, x0); /* bounds */
    put_word (layer + 18, y0);
    put_word (layer + 20, x1);
    put_word (layer + 22, y1);
    put_word (layer + 30, flags | LAYER_CLUELESS);
    put_long (layer + 32, flags & 4 ? sbm : 0); /* ClipRect */
    put_long (layer + 68, mli->amigaos_linfo);
    m68k_areg (regs, 0) = layer + 72; CallLib (get_long (4), -558); /* InitSemaphore() */
    add_uniq(&mli->layer_list, &l->head, layer + 24);
    l->mli = mli;

    l->bounds.MinX = x0;
    l->bounds.MaxX = x1;
    l->bounds.MinY = y0;
    l->bounds.MaxY = y1;
    return l;
}

static void LY_DeleteLayer(uaecptr layer)
{
    uaecptr cr;
    struct MyLayer *l = (struct MyLayer *)find_and_rem_uniq(&l->mli->layer_list, get_long (layer + 24));
    /* Free ClipRects */
    while ((cr = get_long (l->amigaos_layer + 8))) {
	put_long (l->amigaos_layer + 8, get_long (cr));
	amiga_free(cr, CLIPRECT_SIZE);
    }
    amiga_free (l->amigaos_layer, LAYER_SIZE);
    free(l);
}

static uaecptr find_behindlayer_position(uaecptr li, uae_u16 flags)
{
    uaecptr where = li;
    for (;;) {
	uaecptr other = get_long (where);
	/* End of list? */
	if (other == 0)
	    break;
	/* Backdrop? */
	if ((get_word (other + 30) & 0x40) > (flags & 0x40))
	    break;
	where = other;
    }
    return where;
}

static uaecptr LY_CreateLayer(uaecptr li, int x0, int x1, int y0, int y1,
			      uae_u16 flags, uaecptr bm, uaecptr sbm, uaecptr where)
{
    struct MyLayerInfo *mli = (struct MyLayerInfo *)find_uniq(&MyLayerInfo_list, get_long (li + 84));
    struct MyLayer *l;

    LY_LockLayerInfo(li);

    l = LY_NewLayer(mli, x0, x1, y0, y1, flags, bm, sbm);
    /* Chain into list */
    put_long (l->amigaos_layer, get_long (where));
    put_long (l->amigaos_layer + 4, where == li ? 0 : where);
    if (get_long (where) != 0)
	put_long (get_long (where) + 4, l->amigaos_layer);
    put_long (where, l->amigaos_layer);
    redo_layers(mli, bm);
    build_cliprects(l);
    LY_UnlockLayerInfo(li);
    return l->amigaos_layer;
}

static void LY_DisposeLayerInfo(uaecptr li)
{
    LY_ThinLayerInfo(li);
    amiga_free(li, LINFO_SIZE);
}

static uae_u32 layers_NewLayerInfo(void)
{
    uaecptr li = amiga_malloc(LINFO_SIZE);
    LY_InitLayers(li);
    LY_FattenLayerInfo(li);
    return li;
}
static uae_u32 layers_InitLayers(void) { LY_InitLayers (m68k_areg (regs, 0)); return 0; }
static uae_u32 layers_DisposeLayerInfo(void) { LY_DisposeLayerInfo (m68k_areg (regs, 0)); return 0; }

static uae_u32 layers_FattenLayerInfo(void) { LY_FattenLayerInfo(m68k_areg (regs, 0)); return 0; }
static uae_u32 layers_ThinLayerInfo(void) { LY_ThinLayerInfo(m68k_areg (regs, 0)); return 0; }

static uae_u32 layers_CreateUpfrontLayer(void)
{
    return LY_CreateLayer(m68k_areg (regs, 0), (uae_s32)m68k_dreg (regs, 0),
			  (uae_s32)m68k_dreg (regs, 1), (uae_s32)m68k_dreg (regs, 2),
			  (uae_s32)m68k_dreg (regs, 3),
			  m68k_dreg (regs, 4),
			  m68k_areg (regs, 1), m68k_areg (regs, 2), m68k_areg (regs, 0));
}
static uae_u32 layers_CreateBehindLayer(void)
{
    return LY_CreateLayer(m68k_areg (regs, 0), (uae_s32)m68k_dreg (regs, 0),
			  (uae_s32)m68k_dreg (regs, 1), (uae_s32)m68k_dreg (regs, 2),
			  (uae_s32)m68k_dreg (regs, 3),
			  m68k_dreg (regs, 4),
			  m68k_areg (regs, 1), m68k_areg (regs, 2),
			  find_behindlayer_position (m68k_areg (regs, 0), m68k_dreg (regs, 4)));
}
static uae_u32 layers_DeleteLayer(void) { LY_DeleteLayer (m68k_areg (regs, 1)); return 0; }

static void LY_LockLayer1(uaecptr layer)
{
    uaecptr li = get_long (layer + 68);
    struct MyLayerInfo *mli = (struct MyLayerInfo *)find_uniq (&MyLayerInfo_list, get_long (li + 84));
    struct MyLayer *l = (struct MyLayer *)find_uniq (&mli->layer_list, get_long (layer + 24));

    LY_LockLayer(layer);
    build_cliprects (l);
}
static uae_u32 LY_TryLockLayer1(uaecptr layer)
{
    uaecptr li = get_long (layer + 68);
    struct MyLayerInfo *mli = (struct MyLayerInfo *)find_uniq (&MyLayerInfo_list, get_long (li + 84));
    struct MyLayer *l = (struct MyLayer *)find_uniq (&mli->layer_list, get_long (layer + 24));

    if (!LY_TryLockLayer(layer))
	return 0;
    build_cliprects (l);
    return 1;
}
static uae_u32 gfx_TryLockLayer(void) { return LY_TryLockLayer1 (m68k_areg (regs, 5));  }
static uae_u32 gfx_LockLayer(void) { LY_LockLayer1 (m68k_areg (regs, 5)); return 0; }
static uae_u32 gfx_UnlockLayer(void) { LY_UnlockLayer(m68k_areg (regs, 5)); return 0; }
static uae_u32 layers_LockLayer(void) { LY_LockLayer1 (m68k_areg (regs, 1)); return 0; }
static uae_u32 layers_LockLayers(void) { LY_LockLayers(m68k_areg (regs, 0)); return 0; }
static uae_u32 layers_LockLayerInfo(void) { LY_LockLayerInfo(m68k_areg (regs, 0)); return 0; }
static uae_u32 layers_UnlockLayer(void) { LY_UnlockLayer(m68k_areg (regs, 0)); return 0; }
static uae_u32 layers_UnlockLayers(void) { LY_UnlockLayers(m68k_areg (regs, 0)); return 0; }
static uae_u32 layers_UnlockLayerInfo(void) { LY_UnlockLayerInfo(m68k_areg (regs, 0)); return 0; }

static uae_u32 layers_ScrollLayer(void)
{
    abort();
}

static uae_u32 layers_SizeLayer(void)
{
    abort();
}

static uae_u32 layers_MoveLayer(void)
{
    abort();
}

static uae_u32 layers_UpfrontLayer(void)
{
    abort();
}

static uae_u32 layers_BehindLayer(void)
{
    abort();
}

static uae_u32 layers_MoveLayerInFrontOf(void)
{
    abort();
}

static uae_u32 layers_BeginUpdate(void)
{
    return 1;
}

static uae_u32 layers_EndUpdate(void)
{
    return 0;
}

static uae_u32 layers_WhichLayer(void)
{
    abort();
}

static uae_u32 layers_InstallClipRegion(void)
{
    return 0;
}

static uae_u32 layers_SwapBitsRastPortClipRect(void)
{
    abort();
}

/*
 *  Initialization
 */
static uae_u32 gfxlib_init(void)
{
    uae_u32 old_arr;
    uaecptr sysbase=m68k_areg (regs, 6);
    int i=0;

    /* Install new routines */
    m68k_dreg (regs, 0)=0;
    m68k_areg (regs, 1)=gfxlibname;
    gfxbase=CallLib (sysbase, -408);  /* OpenLibrary */
    m68k_dreg (regs, 0)=0;
    m68k_areg (regs, 1)=layerslibname;
    layersbase=CallLib (sysbase, -408);  /* OpenLibrary */

    libemu_InstallFunctionFlags(gfxl_WritePixel, gfxbase, -324, TRAPFLAG_EXTRA_STACK, "");
    libemu_InstallFunctionFlags(gfxl_BltClear, gfxbase, -300, 0, "");
    libemu_InstallFunctionFlags(gfxl_AndRegionRegion, gfxbase, -624, TRAPFLAG_EXTRA_STACK, "");
    libemu_InstallFunctionFlags(gfxl_OrRegionRegion, gfxbase, -612, TRAPFLAG_EXTRA_STACK, "");
    libemu_InstallFunctionFlags(gfxl_XorRegionRegion, gfxbase, -618, TRAPFLAG_EXTRA_STACK, "");
    libemu_InstallFunctionFlags(gfxl_AndRectRegion, gfxbase, -504, TRAPFLAG_EXTRA_STACK, "");
    libemu_InstallFunctionFlags(gfxl_OrRectRegion, gfxbase, -510, TRAPFLAG_EXTRA_STACK, "");
    libemu_InstallFunctionFlags(gfxl_XorRectRegion, gfxbase, -558, TRAPFLAG_EXTRA_STACK, "");
    libemu_InstallFunctionFlags(gfxl_ClearRectRegion, gfxbase, -522, TRAPFLAG_EXTRA_STACK, "");

#if 0
#define MAYBE_FUNCTION(a) NULL
#else
#define MAYBE_FUNCTION(a) (a)
#endif
#if 0
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(gfx_TryLockLayer), gfxbase, -654, TRAPFLAG_EXTRA_STACK|TRAPFLAG_NO_RETVAL, "AttemptLockLayerRom");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(gfx_LockLayer), gfxbase, -432, TRAPFLAG_EXTRA_STACK|TRAPFLAG_NO_RETVAL, "LockLayerRom");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(gfx_UnlockLayer), gfxbase, -438, TRAPFLAG_EXTRA_STACK|TRAPFLAG_NO_RETVAL, "UnlockLayerRom");

    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_InitLayers), layersbase, -30, TRAPFLAG_EXTRA_STACK, "InitLayers");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_CreateUpfrontLayer), layersbase, -36, TRAPFLAG_EXTRA_STACK, "CreateUpfrontLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_CreateBehindLayer), layersbase, -42, TRAPFLAG_EXTRA_STACK, "CreateBehindLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_UpfrontLayer), layersbase, -48, TRAPFLAG_EXTRA_STACK, "UpfrontLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_BehindLayer), layersbase, -54, TRAPFLAG_EXTRA_STACK, "BehindLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_MoveLayer), layersbase, -60, TRAPFLAG_EXTRA_STACK, "MoveLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_SizeLayer), layersbase, -66, TRAPFLAG_EXTRA_STACK, "SizeLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_ScrollLayer), layersbase, -72, TRAPFLAG_EXTRA_STACK, "ScrollLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_BeginUpdate), layersbase, -78, TRAPFLAG_EXTRA_STACK, "BeginUpdate");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_EndUpdate), layersbase, -84, TRAPFLAG_EXTRA_STACK, "EndUpdate");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_DeleteLayer), layersbase, -90, TRAPFLAG_EXTRA_STACK, "DeleteLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_LockLayer), layersbase, -96, TRAPFLAG_EXTRA_STACK, "LockLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_UnlockLayer), layersbase, -102, TRAPFLAG_EXTRA_STACK, "UnlockLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_LockLayers), layersbase, -108, TRAPFLAG_EXTRA_STACK, "LockLayers");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_UnlockLayers), layersbase, -114, TRAPFLAG_EXTRA_STACK, "UnlockLayers");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_LockLayerInfo), layersbase, -120, TRAPFLAG_EXTRA_STACK, "LockLayerInfo");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_SwapBitsRastPortClipRect), layersbase, -126, TRAPFLAG_EXTRA_STACK, "SwapBitsRastPortClipRect");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_WhichLayer), layersbase, -132, TRAPFLAG_EXTRA_STACK, "WhichLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_UnlockLayerInfo), layersbase, -138, TRAPFLAG_EXTRA_STACK, "UnlockLayerInfo");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_NewLayerInfo), layersbase, -144, TRAPFLAG_EXTRA_STACK, "NewLayerInfo");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_DisposeLayerInfo), layersbase, -150, TRAPFLAG_EXTRA_STACK, "DisposeLayerInfo");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_FattenLayerInfo), layersbase, -156, TRAPFLAG_EXTRA_STACK, "FattenLayerInfo");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_ThinLayerInfo), layersbase, -162, TRAPFLAG_EXTRA_STACK, "ThinLayerInfo");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_MoveLayerInFrontOf), layersbase, -168, TRAPFLAG_EXTRA_STACK, "MoveLayerInFrontOf");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_InstallClipRegion), layersbase, -174, TRAPFLAG_EXTRA_STACK, "InstallClipRegion");
#if 0
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_), layersbase, -180, TRAPFLAG_EXTRA_STACK, "MoveSizeLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_), layersbase, -186, TRAPFLAG_EXTRA_STACK, "CreateUpfrontHookLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_), layersbase, -192, TRAPFLAG_EXTRA_STACK, "CreateBehindHookLayer");
    libemu_InstallFunctionFlags(MAYBE_FUNCTION(layers_), layersbase, -198, TRAPFLAG_EXTRA_STACK, "InstallLayerHook");
#endif
#endif
    return 0;
}

/*
 *  Install the gfx-library-replacement
 */
void gfxlib_install(void)
{
    uae_u32 begin, end, resname, resid;
    int i;

    if (! currprefs.use_gfxlib)
	return;

    write_log ("Warning: you enabled the graphics.library replacement with -g\n"
	     "This may be buggy right now, and will not speed things up much.\n");

    resname = ds ("UAEgfxlib.resource");
    resid = ds ("UAE gfxlib 0.1");

    gfxlibname = ds ("graphics.library");
    layerslibname = ds ("layers.library");

    begin = here();
    dw(0x4AFC);             /* RTC_MATCHuae_s16 */
    dl(begin);              /* our start address */
    dl(0);                  /* Continue scan here */
    dw(0x0101);             /* RTF_COLDSTART; Version 1 */
    dw(0x0805);             /* NT_RESOURCE; pri 5 */
    dl(resname);            /* name */
    dl(resid);              /* ID */
    dl(here() + 4);         /* Init area: directly after this */

    calltrap(deftrap2(gfxlib_init, TRAPFLAG_EXTRA_STACK, "")); dw(RTS);

    end = here();
    org(begin + 6);
    dl(end);

    org(end);
}
#else

void gfxlib_install (void)
{
}

#endif