summaryrefslogtreecommitdiffstatshomepage
path: root/README.md
blob: 56646f7ffff60efd92805caa5f88d430f070d1d8 (plain) (blame)
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
# **MAME** #

[![Join the chat at https://gitter.im/mamedev/mame](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mamedev/mame?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Build status for tiny build only, containing just core parts of project:

| OS/Compiler        | Status           | 
| ------------- |:-------------:| 
|Linux GCC / OSX Clang| [![Build Status](https://travis-ci.org/mamedev/mame.svg?branch=master)](https://travis-ci.org/mamedev/mame) |
|Windows MinGW | [![Build Status](https://tea-ci.org/api/badges/mamedev/mame/status.svg)](https://tea-ci.org/mamedev/mame) |
|Windows MSVC | [![Build status](https://ci.appveyor.com/api/projects/status/te0qy56b72tp5kmo?svg=true)](https://ci.appveyor.com/project/startaq/mame) |

Static analysis status for entire build (except for third-party parts of project):

[![Coverity Scan Status](https://scan.coverity.com/projects/5727/badge.svg?flat=1)](https://scan.coverity.com/projects/mame-emulator)

What is MAME?
=============

MAME is a multi-purpose emulation framework.

MAME's purpose is to preserve decades of software history. As electronic technology continues to rush forward, MAME prevents this important "vintage" software from being lost and forgotten. This is achieved by documenting the hardware and how it functions. The source code to MAME serves as this documentation. The fact that the software is usable serves primarily to validate the accuracy of the documentation (how else can you prove that you have recreated the hardware faithfully?). Over time, MAME (originally stood for Multiple Arcade Machine Emulator) absorbed the sister-project MESS (Multi Emulator Super System), so MAME now documents a wide variety of (mostly vintage) computers, video game consoles and calculators, in addition to the arcade video games that were its initial focus.

How to compile?
===============

If you're on a *NIX or OSX system, it could be as easy as typing

```
make
```

for a MAME build,

```
make SUBTARGET=arcade
```

for an arcade-only build, or

```
make SUBTARGET=mess
```

for MESS build.

See the [Compiling MAME](http://docs.mamedev.org/initialsetup/compilingmame.html) page on our documentation site for more information, including prerequisites for Mac OS X and popular Linux distributions.

For recent versions of OSX you need to install [Xcode](https://developer.apple.com/xcode/) including command-line tools and [SDL 2.0](https://www.libsdl.org/download-2.0.php).

For Windows users, we provide a ready-made [build environment](http://mamedev.org/tools/) based on MinGW-w64.

Visual Studio builds are also possible, but you still need [build environment](http://mamedev.org/tools/) based on MinGW-w64.
In order to generate solution and project files just run:

```
make vs2017
```
or use this command to build it directly using msbuild

```
make vs2017 MSBUILD=1
```


Where can I find out more?
=============

* [Official MAME Development Team Site](http://mamedev.org/) (includes binary downloads, wiki, forums, and more)
* [Official MESS Wiki](http://mess.redump.net/)
* [MAME Testers](http://mametesters.org/) (official bug tracker for MAME and MESS)


Contributing
=============

## Coding standard

MAME source code should be viewed and edited with your editor set to use four spaces per tab. Tabs are used for initial indentation of lines, with one tab used per indentation level. Spaces are used for other alignment within a line.

Some parts of the code follow [Allman style](https://en.wikipedia.org/wiki/Indent_style#Allman_style); some parts of the code follow [K&R style](https://en.wikipedia.org/wiki/Indent_style#K.26R_style) -- mostly depending on who wrote the original version. **Above all else, be consistent with what you modify, and keep whitespace changes to a minimum when modifying existing source.** For new code, the majority tends to prefer Allman style, so if you don't care much, use that.

All contributors need to either add a standard header for license info (on new files) or inform us of their wishes regarding which of the following licenses they would like their code to be made available under: the [BSD-3-Clause](http://opensource.org/licenses/BSD-3-Clause) license, the [LGPL-2.1](http://opensource.org/licenses/LGPL-2.1), or the [GPL-2.0](http://opensource.org/licenses/GPL-2.0).

License
=======
The MAME project as a whole is made available under the terms of the
[GNU General Public License, version 2](http://opensource.org/licenses/GPL-2.0)
or later (GPL-2.0+), since it contains code made available under multiple
GPL-compatible licenses.  A great majority of the source files (over 90%
including core files) are made available under the terms of the
[3-clause BSD License](http://opensource.org/licenses/BSD-3-Clause), and we
would encourage new contributors to make their contributions available under the
terms of this license.

Please note that MAME is a registered trademark of Gregory Ember, and permission
is required to use the "MAME" name, logo, or wordmark.

<a href="http://opensource.org/licenses/GPL-2.0" target="_blank">
<img align="right" src="http://opensource.org/trademarks/opensource/OSI-Approved-License-100x137.png">
</a>

    Copyright (C) 1997-2020  MAMEDev and contributors

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License version 2, as provided in
    docs/legal/GPL-2.0.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
    more details.

Please see COPYING for more details.
'n487' href='#n487'>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 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
// license:BSD-3-Clause
// copyright-holders:R. Belmont,Ryan Holtz
/***************************************************************************

    gba_lcd.c

    File to handle emulation of the video hardware of the Game Boy Advance

    By R. Belmont, Ryan Holtz

***************************************************************************/

#include "emu.h"
#include "gba_lcd.h"

#include "screen.h"

#include <cstdarg>


namespace {

// LCD I/O Registers
#define DISPCNT     HWLO(0x000)  // 0x4000000  2  R/W   LCD Control
#define GRNSWAP     HWHI(0x000)  // 0x4000002  2  R/W   Undocumented - Green Swap
#define DISPSTAT    HWLO(0x004)  // 0x4000004  2  R/W   General LCD Status (STAT,LYC)
#define VCOUNT      HWHI(0x004)  // 0x4000006  2  R     Vertical Counter (LY)
#define BG0CNT      HWLO(0x008)  // 0x4000008  2  R/W   BG0 Control
#define BG1CNT      HWHI(0x008)  // 0x400000a  2  R/W   BG1 Control
#define BG2CNT      HWLO(0x00c)  // 0x400000c  2  R/W   BG2 Control
#define BG3CNT      HWHI(0x00c)  // 0x400000e  2  R/W   BG3 Control
#define BG0HOFS     HWLO(0x010)  // 0x4000010  2  W     BG0 X-Offset
#define BG0VOFS     HWHI(0x010)  // 0x4000012  2  W     BG0 Y-Offset
#define BG1HOFS     HWLO(0x014)  // 0x4000014  2  W     BG1 X-Offset
#define BG1VOFS     HWHI(0x014)  // 0x4000016  2  W     BG1 Y-Offset
#define BG2HOFS     HWLO(0x018)  // 0x4000018  2  W     BG2 X-Offset
#define BG2VOFS     HWHI(0x018)  // 0x400001a  2  W     BG2 Y-Offset
#define BG3HOFS     HWLO(0x01c)  // 0x400001c  2  W     BG3 X-Offset
#define BG3VOFS     HWHI(0x01c)  // 0x400001e  2  W     BG3 Y-Offset
#define BG2PA       HWLO(0x020)  // 0x4000020  2  W     BG2 Rotation/Scaling Parameter A (dx)
#define BG2PB       HWHI(0x020)  // 0x4000022  2  W     BG2 Rotation/Scaling Parameter B (dmx)
#define BG2PC       HWLO(0x024)  // 0x4000024  2  W     BG2 Rotation/Scaling Parameter C (dy)
#define BG2PD       HWHI(0x024)  // 0x4000026  2  W     BG2 Rotation/Scaling Parameter D (dmy)
#define BG2X        WORD(0x028)  // 0x4000028  4  W     BG2 Reference Point X-Coordinate
#define BG2Y        WORD(0x02c)  // 0x400002c  4  W     BG2 Reference Point Y-Coordinate
#define BG3PA       HWLO(0x030)  // 0x4000030  2  W     BG3 Rotation/Scaling Parameter A (dx)
#define BG3PB       HWHI(0x030)  // 0x4000032  2  W     BG3 Rotation/Scaling Parameter B (dmx)
#define BG3PC       HWLO(0x034)  // 0x4000034  2  W     BG3 Rotation/Scaling Parameter C (dy)
#define BG3PD       HWHI(0x034)  // 0x4000036  2  W     BG3 Rotation/Scaling Parameter D (dmy)
#define BG3X        WORD(0x038)  // 0x4000038  4  W     BG3 Reference Point X-Coordinate
#define BG3Y        WORD(0x03c)  // 0x400003c  4  W     BG3 Reference Point Y-Coordinate
#define WIN0H       HWLO(0x040)  // 0x4000040  2  W     Window 0 Horizontal Dimensions
#define WIN1H       HWHI(0x040)  // 0x4000042  2  W     Window 1 Horizontal Dimensions
#define WIN0V       HWLO(0x044)  // 0x4000044  2  W     Window 0 Vertical Dimensions
#define WIN1V       HWHI(0x044)  // 0x4000046  2  W     Window 1 Vertical Dimensions
#define WININ       HWLO(0x048)  // 0x4000048  2  R/W   Inside of Window 0 and 1
#define WINOUT      HWHI(0x048)  // 0x400004a  2  R/W   Inside of OBJ Window & Outside of Windows
#define MOSAIC      HWLO(0x04c)  // 0x400004c  2  W     Mosaic Size
								 // 0x400004e  2  -     Unused
#define BLDCNT      HWLO(0x050)  // 0x4000050  2  R/W   Color Special Effects Selection
#define BLDALPHA    HWHI(0x050)  // 0x4000052  2  W     Alpha Blending Coefficients
#define BLDY        HWLO(0x054)  // 0x4000054  2  W     Brightness (Fade-In/Out) Coefficient
								 // 0x4000056  2  -     Unused

#define DISPSTAT_SET(val)       HWLO_SET(0x004, val)
#define DISPSTAT_RESET(val)     HWLO_RESET(0x004, val)

#define VERBOSE_LEVEL   (0)

inline void ATTR_PRINTF(3,4) verboselog(device_t &device, int n_level, const char *s_fmt, ...)
{
	if (VERBOSE_LEVEL >= n_level)
	{
		va_list v;
		char buf[32768];
		va_start(v, s_fmt);
		vsprintf(buf, s_fmt, v);
		va_end(v);
		device.logerror("%08x: %s", device.machine().describe_context(), buf);
	}
}

class object
{
public:
	object(device_t &device, uint16_t *oam, int index)
		: m_device(device)
	{
		m_attr0 = oam[(4 * index) + 0];
		m_attr1 = oam[(4 * index) + 1];
		m_attr2 = oam[(4 * index) + 2];
	}

	int    pos_y()       { return  m_attr0 & 0x00ff; }
	bool   roz()         { return  m_attr0 & 0x0100; }
	bool   roz_double()  { return  m_attr0 & 0x0200; }
	uint16_t mode_mask()   { return  m_attr0 & 0x0c00; }
	bool   mosaic()      { return  m_attr0 & 0x1000; }
	bool   palette_256() { return  m_attr0 & 0x2000; }

	int    pos_x()       { return  m_attr1 & 0x01ff; }
	int    roz_param()   { return (m_attr1 & 0x3e00) >> 9; }
	bool   hflip()       { return  m_attr1 & 0x1000; }
	bool   vflip()       { return  m_attr1 & 0x2000; }

	int    tile_number() { return  m_attr2 & 0x03ff; }
	int    priority()    { return (m_attr2 & 0x0c00) >> 10; }
	int    palette()     { return (m_attr2 & 0xf000) >> 8; }

	enum class mode : uint16_t
	{
		normal = 0x0000,
		alpha  = 0x0400,
		window = 0x0800
	};
	mode mode_enum() { return enum_value<mode>(m_attr0 & 0x0c00); }

	void size(int &width, int &height)
	{
		static int const size_table[4][4][2] =
		{
			{ { 8,   8 }, { 16, 16 }, { 32, 32 }, { 64, 64 } }, // square
			{ { 16,  8 }, { 32,  8 }, { 32, 16 }, { 64, 32 } }, // horizontal rect
			{ { 8,  16 }, { 8,  32 }, { 16, 32 }, { 32, 64 } }, // vertical rect
			{ { 0,   0 }, { 0,   0 }, { 0,   0 }, { 0,   0 } }  // invalid
		};

		int shape = (m_attr0 & 0xc000) >> 14;
		int size  = (m_attr1 & 0xc000) >> 14;

		width  = size_table[shape][size][0];
		height = size_table[shape][size][1];

		if (shape == 4)
			verboselog(m_device, 0, "WARNING: attempted to draw an object of invalid shape\n");
	}

private:
	device_t &m_device;

	uint16_t m_attr0;
	uint16_t m_attr1;
	uint16_t m_attr2;
};

} // anonymous namespace

DEFINE_DEVICE_TYPE(GBA_LCD, gba_lcd_device, "gba_lcd", "GBA LCD")

gba_lcd_device::gba_lcd_device(const machine_config &mconfig, const char *tag, device_t *owner, const XTAL &clock)
	: device_t(mconfig, GBA_LCD, tag, owner, clock)
	, device_video_interface(mconfig, *this)
	, m_int_hblank_cb(*this)
	, m_int_vblank_cb(*this)
	, m_int_vcount_cb(*this)
	, m_dma_hblank_cb(*this)
	, m_dma_vblank_cb(*this)
{
}

inline uint8_t gba_lcd_device::bg_video_mode()
{
	uint8_t mode = DISPCNT & 0x0007;

	if (mode > 5)
	{
		verboselog(*this, 0, "WARNING: attempted to set invalid BG video mode %d\n", mode);
		return 0;
	}

	return mode;
}

inline bool gba_lcd_device::is_set(dispcnt flag)
{
	return DISPCNT & underlying_value(flag);
}

inline void gba_lcd_device::set(dispstat flag)
{
	DISPSTAT_SET(underlying_value(flag));
}

inline void gba_lcd_device::clear(dispstat flag)
{
	DISPSTAT_RESET(underlying_value(flag));
}

inline bool gba_lcd_device::is_set(dispstat flag)
{
	return DISPSTAT & underlying_value(flag);
}

inline bool gba_lcd_device::is_set(uint16_t bgxcnt, bgcnt flag)
{
	return bgxcnt & underlying_value(flag);
}

inline uint8_t gba_lcd_device::bg_priority(uint16_t bgxcnt)
{
	return bgxcnt & 0x0003;
}

inline uint32_t gba_lcd_device::bg_char_base(uint16_t bgxcnt)
{
	return ((bgxcnt & 0x003c) >> 2) * 0x4000;
}

inline uint32_t gba_lcd_device::bg_screen_base(uint16_t bgxcnt)
{
	return ((bgxcnt & 0x1f00) >> 8) * 0x800;
}

inline void gba_lcd_device::bg_screen_size(uint16_t bgxcnt, bool text, int &width, int &height)
{
	static int const size_table[2][4][2] =
	{
		{ { 256, 256 }, { 512, 256 }, { 256, 512 }, { 512,   512 } }, // text mode
		{ { 128, 128 }, { 256, 256 }, { 512, 512 }, { 1024, 1024 } }  // rotation/scaling (roz) mode
	};

	int mode = text ? 0 : 1;
	int size = (bgxcnt & 0xc000) >> 14;

	width  = size_table[mode][size][0];
	height = size_table[mode][size][1];
}

inline uint16_t gba_lcd_device::mosaic_size(size_type type)
{
	return ((MOSAIC >> (4 * underlying_value(type))) & 0xf) + 1;
}

inline gba_lcd_device::sfx gba_lcd_device::color_sfx()
{
	return enum_value<sfx>(BLDCNT & 0x00c0);
}

inline uint8_t gba_lcd_device::color_sfx_target(target id)
{
	return (BLDCNT >> (8 * underlying_value(id))) & 0x3f;
}

inline void gba_lcd_device::update_mask(uint8_t* mask, int y)
{
	bool inwin0 = false;
	bool inwin1 = false;

	if (is_set(dispcnt::win0_en))
		inwin0 = is_in_window_v(y, 0);

	if (is_set(dispcnt::win1_en))
		inwin1 = is_in_window_v(y, 1);

	for (auto x = 0; x < 240; x++)
	{
		mask[x] = WINOUT & 0x00ff;

		if (m_scanline[5][x] != TRANSPARENT_PIXEL)
			mask[x] = WINOUT >> 8;

		if (inwin1 && is_in_window_h(x, 1))
			mask[x] = WININ >> 8;

		if (inwin0 && is_in_window_h(x, 0))
			mask[x] = WININ & 0x00ff;
	}
}

void gba_lcd_device::draw_scanline(int y)
{
	uint16_t *const scanline = &m_bitmap.pix(y);

	if (is_set(dispcnt::forced_blank))
	{
		// forced blank is white
		for (auto x = 0; x < 240; x++)
			scanline[x] = 0x7fff;

		return;
	}

	uint8_t mode = bg_video_mode();

	uint8_t submode;
	if (is_set(dispcnt::win0_en) || is_set(dispcnt::win1_en) || is_set(dispcnt::obj_win_en))
		submode = 2;
	else if (color_sfx() != sfx::none)
		submode = 1;
	else
		submode = 0;

	int depth = 0;
	if (mode == 3)
		depth = 16;
	else if (mode == 4)
		depth = 8;
	else if (mode == 5)
		depth = 4;

	// make all layers transparent at start
	for (auto l = 0; l < 6; l++)
	{
		for (auto x = 0; x < 240; x++)
		{
			m_scanline[l][x] = TRANSPARENT_PIXEL;
		}
	}

	// draw background
	switch (mode)
	{
	case 0:
		draw_bg_scanline(m_scanline[0], y, dispcnt::bg0_en, BG0CNT, BG0HOFS, BG0VOFS);
		draw_bg_scanline(m_scanline[1], y, dispcnt::bg1_en, BG1CNT, BG1HOFS, BG1VOFS);
		draw_bg_scanline(m_scanline[2], y, dispcnt::bg2_en, BG2CNT, BG2HOFS, BG2VOFS);
		draw_bg_scanline(m_scanline[3], y, dispcnt::bg3_en, BG3CNT, BG3HOFS, BG3VOFS);
		break;
	case 1:
		draw_bg_scanline(m_scanline[0], y, dispcnt::bg0_en, BG0CNT, BG0HOFS, BG0VOFS);
		draw_bg_scanline(m_scanline[1], y, dispcnt::bg1_en, BG1CNT, BG1HOFS, BG1VOFS);
		draw_roz_scanline(m_scanline[2], y, dispcnt::bg2_en, BG2CNT, BG2X, BG2Y, BG2PA, BG2PB, BG2PC, BG2PD, m_bg2x, m_bg2y);
		break;
	case 2:
		draw_roz_scanline(m_scanline[2], y, dispcnt::bg2_en, BG2CNT, BG2X, BG2Y, BG2PA, BG2PB, BG2PC, BG2PD, m_bg2x, m_bg2y);
		draw_roz_scanline(m_scanline[3], y, dispcnt::bg3_en, BG3CNT, BG3X, BG3Y, BG3PA, BG3PB, BG3PC, BG3PD, m_bg3x, m_bg3y);
		break;
	case 3:
	case 4:
	case 5:
		draw_roz_bitmap_scanline(m_scanline[2], y, dispcnt::bg2_en, BG2CNT, BG2X, BG2Y, BG2PA, BG2PB, BG2PC, BG2PD, m_bg2x, m_bg2y, depth);
		break;
	}

	uint8_t mask[240];

	// draw objects
	draw_oam(m_scanline[4], y);

	if (submode == 2)
	{
		draw_oam_window(m_scanline[5], y);
		update_mask(mask, y);
	}
	else
	{
		memset(mask, 0xff, sizeof(mask));
	}

	uint32_t backdrop = ((uint16_t *)m_pram.get())[0] | 0x30000000;

	for (auto x = 0; x < 240; x++)
	{
		uint32_t color = backdrop;
		uint8_t top = 0x20;

		for (auto l = 0; l < 5; l++)
		{
			if ((m_scanline[l][x] >> 24) < (color >> 24) && mask[x] & (0x01 << l))
			{
				color = m_scanline[l][x];
				top = (0x01 << l);
			}
		}

		if (color & 0x00010000)
		{
			if (submode != 0 || top == 0x10)
			{
				uint32_t back = backdrop;
				uint8_t top2 = 0x20;

				for (auto l = 0; l < 4; l++)
				{
					if ((m_scanline[l][x] >> 24) < (back >> 24) && mask[x] & (0x01 << l))
					{
						back = m_scanline[l][x];
						top2 = (0x01 << l);
					}
				}

				if (top2 & color_sfx_target(target::second))
				{
					color = alpha_blend(color, back);
				}
				else if (top & color_sfx_target(target::first))
				{
					switch (color_sfx())
					{
					case sfx::lighten:
						color = increase_brightness(color);
						break;
					case sfx::darken:
						color = decrease_brightness(color);
						break;
					default:
						break;
					}
				}
			}
		}
		else if (submode == 1 || (submode == 2 && mask[x] & 0x20))
		{
			if (top & color_sfx_target(target::first))
			{
				switch (color_sfx())
				{
				case sfx::none:
					break;
				case sfx::alpha:
				{
					uint32_t back = backdrop;
					uint8_t top2 = 0x20;

					for (auto l = 0; l < 5; l++)
					{
						if ((m_scanline[l][x] >> 24) < (back >> 24) && mask[x] & (0x01 << l))
						{
							if (top != (0x01 << l))
							{
								back = m_scanline[l][x];
								top2 = (0x01 << l);
							}
						}
					}

					if (top2 & color_sfx_target(target::second))
						color = alpha_blend(color, back);

					break;
				}
				case sfx::lighten:
					color = increase_brightness(color);
					break;
				case sfx::darken:
					color = decrease_brightness(color);
					break;
				}
			}
		}
		scanline[x] = color & 0x7fff;
	}
}

void gba_lcd_device::draw_roz_bitmap_scanline(uint32_t *scanline, int ypos, dispcnt bg_enable, uint32_t ctrl, int32_t X, int32_t Y, int32_t PA, int32_t PB, int32_t PC, int32_t PD, internal_reg &currentx, internal_reg &currenty, int depth)
{
	if (!is_set(bg_enable))
		return;

	uint8_t *src8 = (uint8_t *)m_vram.get();
	uint16_t *src16 = (uint16_t *)m_vram.get();
	uint16_t *palette = (uint16_t *)m_pram.get();
	int32_t sx = (depth == 4) ? 160 : 240;
	int32_t sy = (depth == 4) ? 128 : 160;
	uint32_t prio = (bg_priority(ctrl) << 25) + 0x1000000;

	if (is_set(dispcnt::alt_frame_sel))
	{
		if (depth == 8)
			src8 += 0xa000;

		if (depth == 4)
			src16 += 0xa000 / 2;
	}

	// sign extend roz parameters
	if (X & 0x08000000) X |= 0xf0000000;
	if (Y & 0x08000000) Y |= 0xf0000000;
	if (PA & 0x8000) PA |= 0xffff0000;
	if (PB & 0x8000) PB |= 0xffff0000;
	if (PC & 0x8000) PC |= 0xffff0000;
	if (PD & 0x8000) PD |= 0xffff0000;

	if (currentx.update)
	{
		currentx.status = X;
		currentx.update = false;
	}
	else
	{
		currentx.status += PB;
	}

	if (currenty.update)
	{
		currenty.status = Y;
		currenty.update = false;
	}
	else
	{
		currenty.status += PD;
	}

	int32_t cx = currentx.status;
	int32_t cy = currenty.status;

	if (is_set(ctrl, bgcnt::mosaic_en))
	{
		uint16_t mosaic_line = mosaic_size(size_type::bg_v);
		int32_t tempy = (ypos / mosaic_line) * mosaic_line;
		cx = X + tempy * PB;
		cy = Y + tempy * PD;
	}

	int32_t pixx = cx >> 8;
	int32_t pixy = cy >> 8;

	for (auto x = 0; x < 240; x++)
	{
		if (pixx >= 0 && pixy >= 0 && pixx < sx && pixy < sy)
		{
			if (depth == 8)
			{
				uint8_t color = src8[pixy * sx + pixx];
				if (color)
					scanline[x] = palette[color] | prio;
			}
			else
			{
				scanline[x] = src16[pixy * sx + pixx] | prio;
			}
		}

		cx += PA;
		cy += PC;

		pixx = cx >> 8;
		pixy = cy >> 8;
	}

	if (is_set(ctrl, bgcnt::mosaic_en))
	{
		uint16_t mosaicx = mosaic_size(size_type::bg_h);
		if (mosaicx > 1)
		{
			int32_t m = 1;
			for (auto x = 0; x < 239; x++)
			{
				scanline[x + 1] = scanline[x];
				m++;
				if (m == mosaicx)
				{
					m = 1;
					x++;
				}
			}
		}
	}
}

void gba_lcd_device::draw_roz_scanline(uint32_t *scanline, int ypos, dispcnt bg_enable, uint32_t ctrl, int32_t X, int32_t Y, int32_t PA, int32_t PB, int32_t PC, int32_t PD, internal_reg &currentx, internal_reg &currenty)
{
	if (!is_set(bg_enable))
		return;

	uint8_t *mgba_vram = (uint8_t *)m_vram.get();
	uint16_t *pgba_pram = (uint16_t *)m_pram.get();
	uint32_t priority = (bg_priority(ctrl) << 25) + 0x1000000;
	uint32_t base = bg_char_base(ctrl);
	uint32_t mapbase = bg_screen_base(ctrl);

	// size of map in submaps
	int width, height;
	bg_screen_size(ctrl, false, width, height);

	// sign extend roz parameters
	if (X & 0x08000000) X |= 0xf0000000;
	if (Y & 0x08000000) Y |= 0xf0000000;
	if (PA & 0x8000) PA |= 0xffff0000;
	if (PB & 0x8000) PB |= 0xffff0000;
	if (PC & 0x8000) PC |= 0xffff0000;
	if (PD & 0x8000) PD |= 0xffff0000;

	if (currentx.update)
	{
		currentx.status = X;
		currentx.update = false;
	}
	else
	{
		currentx.status += PB;
	}

	if (currenty.update)
	{
		currenty.status = Y;
		currenty.update = false;
	}
	else
	{
		currenty.status += PD;
	}

	int32_t cx = currentx.status;
	int32_t cy = currenty.status;

	if (is_set(ctrl, bgcnt::mosaic_en))
	{
		uint16_t mosaic_line = mosaic_size(size_type::bg_v);
		int y = ypos % mosaic_line;
		cx -= y * PB;
		cy -= y * PD;
	}

	int32_t pixx = cx >> 8;
	int32_t pixy = cy >> 8;

	if (is_set(ctrl, bgcnt::wraparound_en))
	{
		pixx %= width;
		pixy %= height;

		if (pixx < 0)
			pixx += width;

		if (pixy < 0)
			pixy += height;
	}

	for (auto x = 0; x < 240; x++)
	{
		if (pixx >= 0 && pixy >= 0 && pixx < width && pixy < height)
		{
			int tilex = pixx & 7;
			int tiley = pixy & 7;

			// shall we shift for is_set(ctrl, bgcnt::palette_256)? or is not effective for ROZ?
			uint32_t tile = mgba_vram[mapbase + (pixx >> 3) + (pixy >> 3) * (width >> 3)];
			uint16_t pixel = mgba_vram[base + (tile << 6) + (tiley << 3) + tilex];

			// plot it
			if (pixel)
				scanline[x] = pgba_pram[pixel] | priority;
		}

		cx += PA;
		cy += PC;

		pixx = cx >> 8;
		pixy = cy >> 8;

		if (is_set(ctrl, bgcnt::wraparound_en))
		{
			pixx %= width;
			pixy %= height;

			if (pixx < 0)
				pixx += width;

			if (pixy < 0)
				pixy += height;
		}
	}

	if (is_set(ctrl, bgcnt::mosaic_en))
	{
		uint16_t mosaicx = mosaic_size(size_type::bg_h);
		if (mosaicx > 1)
		{
			int m = 1;
			for (auto x = 0; x < 239; x++)
			{
				scanline[x + 1] = scanline[x];
				m++;
				if (m == mosaicx)
				{
					m = 1;
					x++;
				}
			}
		}
	}
}

void gba_lcd_device::draw_bg_scanline(uint32_t *scanline, int ypos, dispcnt bg_enable, uint32_t ctrl, uint32_t hofs, uint32_t vofs)
{
	if (!is_set(bg_enable))
		return;

	uint8_t *vram = (uint8_t*)m_vram.get();
	uint16_t *palette = (uint16_t *)m_pram.get();
	uint8_t *chardata = &vram[bg_char_base(ctrl)];
	uint16_t *screendata = (uint16_t *)&vram[bg_screen_base(ctrl)];
	uint32_t priority = (bg_priority(ctrl) << 25) + 0x1000000;
	uint16_t mosaicx = mosaic_size(size_type::bg_h);
	uint16_t mosaicy = mosaic_size(size_type::bg_v);

	int width, height;
	bg_screen_size(ctrl, true, width, height);

	int32_t pixx = hofs % width;
	int32_t pixy = (vofs + ypos) % height;

	if (is_set(ctrl, bgcnt::mosaic_en) && ypos % mosaicy)
	{
		mosaicy = (ypos / mosaicy) * mosaicy;
		pixy = (vofs + mosaicy) % height;
	}

	if (pixy > 255 && height > 256)
	{
		pixy &= 0x000000ff;
		screendata += 0x400;
		if (width > 256)
		{
			screendata += 0x400;
		}
	}

	int32_t stride = (pixy >> 3) << 5;

	uint16_t *src = screendata + 0x400 * (pixx >> 8) + ((pixx & 255) >> 3) + stride;

	for (auto x = 0; x < 240; x++)
	{
		uint16_t data = *src;
		int32_t tile = tile_number(data);
		int32_t tilex = pixx & 7;
		int32_t tiley = pixy & 7;
		uint8_t color;
		uint8_t palindex;

		if (tile_hflip(data))
			tilex = 7 - tilex;

		if (tile_vflip(data))
			tiley = 7 - tiley;

		if (is_set(ctrl, bgcnt::palette_256))
		{
			color = chardata[(tile << 6) + (tiley << 3) + tilex];
			palindex = 0;
		}
		else
		{
			color = chardata[(tile << 5) + (tiley << 2) + (tilex >> 1)];

			if (tilex & 1)
				color >>= 4;
			else
				color &= 0x0f;
			palindex = (data >> 8) & 0x00f0;
		}

		if (color)
			scanline[x] = palette[palindex + color] | priority;

		if (tile_hflip(data))
		{
			if (tilex == 0)
				src++;
		}
		else if (tilex == 7)
		{
			src++;
		}

		pixx++;
		if (pixx == 256)
		{
			if (width > 256)
			{
				src = screendata + 0x400 + stride;
			}
			else
			{
				src = screendata + stride;
				pixx = 0;
			}
		}
		else if (pixx >= width)
		{
			pixx = 0;
			src = screendata + stride;
		}
	}

	if (is_set(ctrl, bgcnt::mosaic_en) && mosaicx > 1)
	{
		int32_t m = 1;
		for (auto x = 0; x < 239; x++)
		{
			scanline[x+1] = scanline[x];
			m++;
			if (m == mosaicx)
			{
				m = 1;
				x++;
			}
		}
	}
}

void gba_lcd_device::draw_oam_window(uint32_t *scanline, int y)
{
	if (!is_set(dispcnt::obj_win_en))
		return;

	uint16_t *oam = (uint16_t *)m_oam.get();
	uint8_t *src = (uint8_t *)m_vram.get();

	for (auto obj_index = 127; obj_index >= 0; obj_index--)
	{
		object obj(*this, oam, obj_index);

		if (obj.mode_enum() != object::mode::window)
			continue;

		uint32_t tile_number = obj.tile_number();

		if (bg_video_mode() > 2 && tile_number < 0x200)
			continue;

		int32_t sx = obj.pos_x();
		int32_t sy = obj.pos_y();

		if (sy > 160)
			sy -= 256;

		int width, height;
		obj.size(width, height);

		if (obj.roz())
		{
			int32_t fx = width;
			int32_t fy = height;

			if (obj.roz_double())
			{
				fx *= 2;
				fy *= 2;
			}

			int32_t cury = y - sy;

			if (cury < 0 || cury >= fy)
				continue;

			if (sx >= 240 && ((sx + fx) % 512) >= 240)
				continue;

			int rot = obj.roz_param();
			int16_t dx  = (int16_t)oam[(rot << 4) + 3];
			int16_t dmx = (int16_t)oam[(rot << 4) + 7];
			int16_t dy  = (int16_t)oam[(rot << 4) + 11];
			int16_t dmy = (int16_t)oam[(rot << 4) + 15];

			int32_t rx = (width << 7) - (fx >> 1) * dx - (fy >> 1) * dmx + cury * dmx;
			int32_t ry = (height << 7) - (fx >> 1) * dy - (fy >> 1) * dmy + cury * dmy;

			int inc = 32;

			if (obj.palette_256())
			{
				if (is_set(dispcnt::vram_map_1d))
					inc = sx >> 2;
				else
					tile_number &= 0x3fe;

				for (auto x = 0; x < fx; x++)
				{
					int32_t ax = rx >> 8;
					int32_t ay = ry >> 8;

					if (ax >= 0 && ax < sx && ay >= 0 && ay < sy)
					{
						uint8_t color = src[0x10000 + ((((tile_number + (ay >> 3) * inc) << 5) + ((ay & 0x07) << 3) + ((ax >> 3) << 6) + (ax & 0x07)) & 0x7fff)];

						if (color)
							scanline[sx] = 1;
					}

					sx = (sx + 1) % 512;
					rx += dx;
					ry += dy;
				}
			}
			else
			{
				if (is_set(dispcnt::vram_map_1d))
					inc = sx >> 3;

				for (auto x = 0; x < fx; x++)
				{
					int32_t ax = rx >> 8;
					int32_t ay = ry >> 8;

					if (ax >= 0 && ax < sx && ay >= 0 && ay < sy)
					{
						uint8_t color = src[0x10000 + ((((tile_number + (ay >> 3) * inc) << 5) + ((ay & 0x07) << 2) + ((ax >> 3) << 5) + ((ax & 0x07) >> 1)) & 0x7fff)];

						if (ax & 1)
							color >>= 4;
						else
							color &= 0x0f;

						if (color)
							scanline[sx] = 1;
					}

					sx = (sx + 1) % 512;
					rx += dx;
					ry += dy;
				}
			}
		}
		else
		{
			// when roz bit is not set double roz bit means 'disable object'
			if (obj.roz_double())
				continue;

			int32_t cury = y - sy;

			if (cury < 0 || cury >= height)
				continue;

			if ((sx >= 240) && (((sx + width) % 512) >= 240))
				continue;

			int inc = 32;

			if (obj.vflip())
				cury = height - cury - 1;

			int32_t ax = obj.hflip() ? (width - 1) : 0;

			if (obj.palette_256())
			{
				if (is_set(dispcnt::vram_map_1d))
					inc = width >> 2;
				else
					tile_number &= 0x3fe;

				uint32_t address = 0x10000 + ((((tile_number + (cury >> 3) * inc) << 5) + ((cury & 7) << 3) + ((ax >> 3) << 6) + (ax & 7)) & 0x7fff);

				if (obj.hflip())
					ax = 7;

				for (auto x = 0; x < width; x++)
				{
					if (sx < 240)
					{
						uint8_t color = src[address];

						if (color)
							scanline[sx] = 1;
					}

					sx = (sx + 1) % 512;

					if (obj.hflip())
					{
						ax--;
						address--;

						if (ax == -1)
						{
							address -= 56;
							ax = 7;
						}

						if (address < 0x10000)
							address += 0x8000;
					}
					else
					{
						ax++;
						address++;

						if (ax == 8)
						{
							address += 56;
							ax = 0;
						}

						if (address > 0x17fff)
							address -= 0x8000;
					}
				}
			}
			else
			{
				if (is_set(dispcnt::vram_map_1d))
					inc = width >> 3;

				uint32_t address = 0x10000 + ((((tile_number + (cury >> 3) * inc) << 5) + ((cury & 0x07) << 2) + ((ax >> 3) << 5) + ((ax & 0x07) >> 1)) & 0x7fff);

				if (obj.hflip())
				{
					ax = 7;

					for (auto x = width - 1; x >= 0; x--)
					{
						if (sx < 240)
						{
							uint8_t color = src[address];
							if (x & 1)
								color >>= 4;
							else
								color &= 0x0f;

							if (color)
								scanline[sx] = 1;
						}

						sx = (sx + 1) % 512;
						ax--;

						if ((x & 1) == 0)
							address--;

						if (ax == -1)
						{
							ax = 7;
							address -= 28;
						}

						if (address < 0x10000)
							address += 0x8000;
					}
				}
				else
				{
					for (auto x = 0; x < width; x++)
					{
						if (sx < 240)
						{
							uint8_t color = src[address];

							if (x & 1)
								color >>= 4;
							else
								color &= 0x0f;

							if (color)
								scanline[sx] = 1;
						}

						sx = (sx + 1) % 512;
						ax++;

						if (x & 1)
							address++;

						if (ax == 8)
						{
							address += 28;
							ax = 0;
						}

						if (address > 0x17fff)
							address -= 0x8000;
					}
				}
			}
		}
	}
}

void gba_lcd_device::draw_oam(uint32_t *scanline, int y)
{
	if (!is_set(dispcnt::obj_en))
		return;

	int32_t mosaiccnt = 0;
	uint16_t mosaicx = mosaic_size(size_type::obj_h);
	uint16_t mosaicy = mosaic_size(size_type::obj_v);
	uint16_t *oam = (uint16_t *)m_oam.get();
	uint8_t *src = (uint8_t *)m_vram.get();
	uint16_t *palette = (uint16_t *)m_pram.get();

	for (auto obj_index = 0; obj_index < 128; obj_index++)
	{
		object obj(*this, oam, obj_index);

		if (obj.mode_enum() == object::mode::window)
			continue;

		uint32_t priority = obj.priority();
		uint32_t prio = (priority << 25) | (obj.mode_mask() << 6);

		int width, height;
		obj.size(width, height);

		uint32_t tile_number = obj.tile_number();

		if (bg_video_mode() > 2 && tile_number < 0x200)
			continue;

		if (obj.roz())
		{
			int32_t sx = obj.pos_x();
			int32_t sy = obj.pos_y();

			if (sy > 160)
				sy -= 256;

			int32_t fx = width;
			int32_t fy = height;

			if (obj.roz_double())
			{
				fx *= 2;
				fy *= 2;
			}

			int32_t cury = y - sy;

			if (cury < 0 || cury >= fy)
				continue;

			if (sx >= 240 && ((sx + fx) % 512) >= 240)
				continue;

			int32_t oamparam = obj.roz_param();

			int16_t dx  = (int16_t)oam[(oamparam << 4) + 3];
			int16_t dmx = (int16_t)oam[(oamparam << 4) + 7];
			int16_t dy  = (int16_t)oam[(oamparam << 4) + 11];
			int16_t dmy = (int16_t)oam[(oamparam << 4) + 15];

			if (obj.mosaic())
				cury -= (cury % mosaicy);

			int32_t rx = (width << 7) - (fx >> 1) * dx - (fy >> 1) * dmx + cury * dmx;
			int32_t ry = (height << 7) - (fx >> 1) * dy - (fy >> 1) * dmy + cury * dmy;

			int32_t inc = 32;

			if (obj.palette_256())
			{
				if (is_set(dispcnt::vram_map_1d))
					inc = width >> 2;
				else
					tile_number &= 0x3fe;

				for (auto x = 0; x < fx; x++)
				{
					int32_t pixx = rx >> 8;
					int32_t pixy = ry >> 8;

					if (!(pixx < 0 || pixx >= width || pixy < 0 || pixy >= height || sx >= 240))
					{
						uint8_t color = src[0x10000 + ((((tile_number + (pixy >> 3) * inc) << 5) + ((pixy & 7) << 3) + ((pixx >> 3) << 6) + (pixx & 7)) & 0x7fff)];

						if (color == 0 && priority < ((scanline[sx] >> 25) & 3))
						{
							scanline[sx] = (scanline[sx] & 0xf9ffffff) | prio;
							if (obj.mosaic() && mosaiccnt != 0)
							{
								scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
							}
						}
						else if (color != 0 && prio < (scanline[sx] & 0xff000000))
						{
							scanline[sx] = palette[256 + color] | prio;
							if (obj.mosaic() && mosaiccnt != 0)
							{
								scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
							}
						}

						if (obj.mosaic())
							mosaiccnt = (mosaiccnt + 1) % mosaicx;
					}

					sx = (sx + 1) % 512;
					rx += dx;
					ry += dy;
				}
			}
			else
			{
				if (is_set(dispcnt::vram_map_1d))
					inc = width >> 3;

				for (auto x = 0; x < fx; x++)
				{
					int32_t pixx = rx >> 8;
					int32_t pixy = ry >> 8;

					if (!(pixx < 0 || pixx >= width || pixy < 0 || pixy >= height || sx >= 240))
					{
						uint8_t color = src[0x10000 + ((((tile_number + (pixy >> 3) * inc) << 5) + ((pixy & 7) << 2) + ((pixx >> 3) << 5) + ((pixx & 7) >> 1)) & 0x7fff)];

						if (pixx & 1)
							color >>= 4;
						else
							color &= 0x0f;

						if (color == 0 && priority < ((scanline[sx] >> 25) & 3))
						{
							scanline[sx] = (scanline[sx] & 0xf9ffffff) | prio;
							if (obj.mosaic() && mosaiccnt != 0)
							{
								scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
							}
						}
						else if (color != 0 && prio < (scanline[sx] & 0xff000000))
						{
							scanline[sx] = palette[256 + obj.palette() + color] | prio;
							if (obj.mosaic() && mosaiccnt != 0)
							{
								scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
							}
						}
					}

					if (obj.mosaic())
						mosaiccnt = (mosaiccnt + 1) % mosaicx;

					sx = (sx + 1) % 512;
					rx += dx;
					ry += dy;
				}
			}
		}
		else
		{
			// when roz bit is not set double roz bit means 'disable object'
			if (obj.roz_double())
				continue;

			int32_t sx = obj.pos_x();
			int32_t sy = obj.pos_y();

			if (sy > 160)
				sy -= 256;

			int32_t cury = y - sy;

			if (cury < 0 || cury >= height)
				continue;

			if (sx >= 240 && ((sx + width) % 512) >= 240)
				continue;

			int32_t inc = 32;

			if (obj.vflip())
				cury = height - cury - 1;

			int32_t pixx = obj.hflip() ? (width - 1) : 0;

			if (obj.mosaic())
				cury -= (cury % mosaicy);

			if (obj.palette_256())
			{
				if (is_set(dispcnt::vram_map_1d))
					inc = width >> 2;
				else
					tile_number &= 0x3fe;

				uint32_t address = 0x10000 + ((((tile_number + (cury >> 3) * inc) << 5) + ((cury & 7) << 3) + ((pixx >> 3) << 6) + (pixx & 7)) & 0x7fff);

				if (obj.hflip())
					pixx = 7;

				for (auto x = 0; x < width; x++)
				{
					if (sx < 240)
					{
						uint8_t color = src[address];

						if (color == 0 && priority < ((scanline[sx] >> 25) & 3))
						{
							scanline[sx] = (scanline[sx] & 0xf9ffffff) | prio;
							if (obj.mosaic() && mosaiccnt != 0)
							{
								scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
							}
						}
						else if (color != 0 && prio < (scanline[sx] & 0xff000000))
						{
							scanline[sx] = palette[256 + color] | prio;
							if (obj.mosaic() && mosaiccnt != 0)
							{
								scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
							}
						}
					}

					if (obj.mosaic())
						mosaiccnt = (mosaiccnt + 1) % mosaicx;

					sx = (sx + 1) % 512;

					if (obj.hflip())
					{
						pixx--;
						address--;

						if (pixx == -1)
						{
							address -= 56;
							pixx = 7;
						}

						if (address < 0x10000)
							address += 0x8000;
					}
					else
					{
						pixx++;
						address++;

						if (pixx == 8)
						{
							address += 56;
							pixx = 0;
						}

						if (address > 0x17fff)
							address -= 0x8000;
					}
				}
			}
			else
			{
				if (is_set(dispcnt::vram_map_1d))
					inc = width >> 3;

				uint32_t address = 0x10000 + ((((tile_number + (cury >> 3) * inc) << 5) + ((cury & 7) << 2) + ((pixx >> 3) << 5) + ((pixx & 7) >> 1)) & 0x7fff);

				if (obj.hflip())
				{
					pixx = 7;
					for (auto x = width - 1; x >= 0; x--)
					{
						if (sx < 240)
						{
							uint8_t color = src[address];

							if (x & 1)
								color >>= 4;
							else
								color &= 0x0f;

							if (color == 0 && priority < ((scanline[sx] >> 25) & 3))
							{
								scanline[sx] = (scanline[sx] & 0xf9ffffff) | prio;
								if (obj.mosaic() && mosaiccnt != 0)
								{
									scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
								}
							}
							else if (color != 0 && prio < (scanline[sx] & 0xff000000))
							{
								scanline[sx] = palette[256 + obj.palette() + color] | prio;
								if (obj.mosaic() && mosaiccnt != 0)
								{
									scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
								}
							}
						}

						sx = (sx + 1) % 512;
						pixx--;

						if (!(x & 1))
							address--;

						if (pixx == -1)
						{
							address -= 28;
							pixx = 7;
						}

						if (address < 0x10000)
							address += 0x8000;
					}
				}
				else
				{
					for (auto x = 0; x < width; x++)
					{
						if (sx < 240)
						{
							uint8_t color = src[address];

							if (x & 1)
								color >>= 4;
							else
								color &= 0x0f;

							if (color == 0 && priority < ((scanline[sx] >> 25) & 3))
							{
								scanline[sx] = (scanline[sx] & 0xf9ffffff) | prio;
								if (obj.mosaic() && mosaiccnt != 0)
								{
									scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
								}
							}
							else if (color != 0 && prio < (scanline[sx] & 0xff000000))
							{
								scanline[sx] = palette[256 + obj.palette() + color] | prio;
								if (obj.mosaic() && mosaiccnt != 0)
								{
									scanline[sx] = (scanline[sx - 1] & 0xf9ffffff) | prio;
								}
							}
						}

						if (obj.mosaic())
							mosaiccnt = (mosaiccnt + 1) % mosaicx;

						sx = (sx + 1) % 512;
						pixx++;

						if (x & 1)
							address++;

						if (pixx == 8)
						{
							address += 28;
							pixx = 0;
						}

						if (address > 0x17fff)
							address -= 0x8000;
					}
				}
			}
		}
	}
}

inline bool gba_lcd_device::is_in_window_h(int x, int window)
{
	uint16_t reg = (window == 0) ? WIN0H : WIN1H;

	uint8_t x0 = reg >> 8;
	uint8_t x1 = reg & 0x00ff;

	if (x0 <= x1)
	{
		if (x >= x0 && x < x1)
			return true;
	}
	else
	{
		if (x >= x0 || x < x1)
			return true;
	}

	return false;
}

inline bool gba_lcd_device::is_in_window_v(int y, int window)
{
	uint16_t reg = (window == 0) ? WIN0V : WIN1V;

	uint8_t v0 = reg >> 8;
	uint8_t v1 = reg & 0x00ff;

	if ((v0 == v1) && (v0 >= 0xe8))
		return true;

	if (v1 >= v0)
	{
		if (y >= v0 && y < v1)
			return true;
	}
	else
	{
		if (y >= v0 || y < v1)
			return true;
	}

	return false;
}

static int const coeff[32] = {
	0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16
};

inline uint32_t gba_lcd_device::alpha_blend(uint32_t color0, uint32_t color1)
{
	int ca = coeff[BLDALPHA & 0x1f];
	int cb = coeff[(BLDALPHA >> 8) & 0x1f];

	if (color0 != TRANSPARENT_PIXEL)
	{
		int r0 = (color0 >>  0) & 0x1f;
		int g0 = (color0 >>  5) & 0x1f;
		int b0 = (color0 >> 10) & 0x1f;
		int r1 = (color1 >>  0) & 0x1f;
		int g1 = (color1 >>  5) & 0x1f;
		int b1 = (color1 >> 10) & 0x1f;
		int r = ((r0 * ca) >> 4) + ((r1 * cb) >> 4);
		int g = ((g0 * ca) >> 4) + ((g1 * cb) >> 4);
		int b = ((b0 * ca) >> 4) + ((b1 * cb) >> 4);

		if (r > 0x1f) r = 0x1f;
		if (g > 0x1f) g = 0x1f;
		if (b > 0x1f) b = 0x1f;

		return (color0 & 0xffff0000) | (b << 10) | (g << 5) | r;
	}

	return color0;
}

inline uint32_t gba_lcd_device::increase_brightness(uint32_t color)
{
	int cc = coeff[BLDY & 0x1f];

	int r = (color >>  0) & 0x1f;
	int g = (color >>  5) & 0x1f;
	int b = (color >> 10) & 0x1f;

	r += ((0x1f - r) * cc) >> 4;
	g += ((0x1f - g) * cc) >> 4;
	b += ((0x1f - b) * cc) >> 4;

	if (r > 0x1f) r = 0x1f;
	if (g > 0x1f) g = 0x1f;
	if (b > 0x1f) b = 0x1f;

	return (color & 0xffff0000) | (b << 10) | (g << 5) | r;
}

inline uint32_t gba_lcd_device::decrease_brightness(uint32_t color)
{
	int cc = coeff[BLDY & 0x1f];

	int r = (color >>  0) & 0x1f;
	int g = (color >>  5) & 0x1f;
	int b = (color >> 10) & 0x1f;

	r -= (r * cc) >> 4;
	g -= (g * cc) >> 4;
	b -= (b * cc) >> 4;

	if (r < 0) r = 0;
	if (g < 0) g = 0;
	if (b < 0) b = 0;

	return (color & 0xffff0000) | (b << 10) | (g << 5) | r;
}

static char const *const reg_names[] = {
	/* LCD I/O Registers */
	"DISPCNT", "GRNSWAP",  "DISPSTAT", "VCOUNT",
	"BG0CNT",  "BG1CNT",   "BG2CNT",   "BG3CNT",
	"BG0HOFS", "BG0VOFS",  "BG1HOFS",  "BG1VOFS",
	"BG2HOFS", "BG2VOFS",  "BG3HOFS",  "BG3VOFS",
	"BG2PA",   "BG2PB",    "BG2PC",    "BG2PD",
	"BG2X_L",  "BG2X_H",   "BG2Y_L",   "BG2Y_H",
	"BG3PA",   "BG3PB",    "BG3PC",    "BG3PD",
	"BG3X_L",  "BG3X_H",   "BG3Y_L",   "BG3Y_H",
	"WIN0H",   "WIN1H",    "WIN0V",    "WIN1V",
	"WININ",   "WINOUT",   "MOSAIC",   "Unused",
	"BLDCNT",  "BLDALPHA", "BLDY",     "Unused",
	"Unused",  "Unused",   "Unused",   "Unused",
};

uint32_t gba_lcd_device::video_r(offs_t offset, uint32_t mem_mask)
{
	uint32_t retval = 0;

	switch (offset)
	{
	case 0x0004/4:
		retval = DISPSTAT | (screen().vpos() << 16);
		break;
	default:
		if (ACCESSING_BITS_0_15)
		{
			retval |= m_regs[offset] & 0x0000ffff;
		}
		if (ACCESSING_BITS_16_31)
		{
			retval |= m_regs[offset] & 0xffff0000;
		}
		break;
	}

	if (offset >= std::size(reg_names) / 2)
		throw emu_fatalerror("gba_lcd_device::video_r: Not enough register names in gba_lcd_device");

	if (ACCESSING_BITS_0_15)
		verboselog(*this, 2, "GBA I/O Read: %s = %04x\n", reg_names[offset * 2], retval & 0x0000ffff);

	if (ACCESSING_BITS_16_31)
		verboselog(*this, 2, "GBA I/O Read: %s = %04x\n", reg_names[offset * 2 + 1], (retval & 0xffff0000) >> 16);

	return retval;
}

void gba_lcd_device::video_w(offs_t offset, uint32_t data, uint32_t mem_mask)
{
	COMBINE_DATA(&m_regs[offset]);

	if (offset >= std::size(reg_names) / 2)
		throw emu_fatalerror("gba_lcd_device::video_w: Not enough register names in gba_lcd_device");

	if (ACCESSING_BITS_0_15)
		verboselog(*this, 2, "GBA I/O Write: %s = %04x\n", reg_names[offset * 2], data & 0x0000ffff);

	if (ACCESSING_BITS_16_31)
		verboselog(*this, 2, "GBA I/O Write: %s = %04x\n", reg_names[offset * 2 + 1], (data & 0xffff0000) >> 16);

	switch (offset)
	{
	case 0x0028/4:
		m_bg2x.update = true;
		break;
	case 0x002c/4:
		m_bg2y.update = true;
		break;
	case 0x0038/4:
		m_bg3x.update = true;
		break;
	case 0x003c/4:
		m_bg3y.update = true;
		break;
	}
}

uint32_t gba_lcd_device::gba_pram_r(offs_t offset)
{
	return m_pram[offset];
}

void gba_lcd_device::gba_pram_w(offs_t offset, uint32_t data, uint32_t mem_mask)
{
	COMBINE_DATA(&m_pram[offset]);
}

uint32_t gba_lcd_device::gba_vram_r(offs_t offset)
{
	return m_vram[offset];
}

void gba_lcd_device::gba_vram_w(offs_t offset, uint32_t data, uint32_t mem_mask)
{
	COMBINE_DATA(&m_vram[offset]);
}

uint32_t gba_lcd_device::gba_oam_r(offs_t offset)
{
	return m_oam[offset];
}

void gba_lcd_device::gba_oam_w(offs_t offset, uint32_t data, uint32_t mem_mask)
{
	COMBINE_DATA(&m_oam[offset]);
}

TIMER_CALLBACK_MEMBER(gba_lcd_device::perform_hbl)
{
	int scanline = screen().vpos();

	// reload LCD controller internal registers from I/O ones at vblank
	if (scanline == 0)
	{
		m_bg2x.update = true;
		m_bg2y.update = true;
		m_bg3x.update = true;
		m_bg3y.update = true;
	}

	// draw only visible scanlines
	if (scanline < 160)
	{
		draw_scanline(scanline);

		if (!m_dma_hblank_cb.isnull())
			m_dma_hblank_cb(ASSERT_LINE);
	}

	if (is_set(dispstat::hblank_irq_en))
	{
		if (!m_int_hblank_cb.isnull())
			m_int_hblank_cb(ASSERT_LINE);
	}

	set(dispstat::hblank);

	m_hbl_timer->adjust(attotime::never);
}

TIMER_CALLBACK_MEMBER(gba_lcd_device::perform_scan)
{
	clear(dispstat::hblank);
	clear(dispstat::vcount);

	int scanline = screen().vpos();

	// VBLANK is set for scanlines 160 through 226 (but not 227, which is the last line)
	if (scanline >= 160 && scanline < 227)
	{
		set(dispstat::vblank);

		// VBL IRQ and DMA on line 160
		if (scanline == 160)
		{
			if (is_set(dispstat::vblank_irq_en))
			{
				if (!m_int_vblank_cb.isnull())
					m_int_vblank_cb(ASSERT_LINE);
			}

			if (!m_dma_vblank_cb.isnull())
				m_dma_vblank_cb(ASSERT_LINE);
		}
	}
	else
	{
		clear(dispstat::vblank);
	}

	// handle VCOUNT match interrupt flag
	if (scanline == ((DISPSTAT >> 8) & 0xff))
	{
		set(dispstat::vcount);

		if (is_set(dispstat::vcount_irq_en))
		{
			if (!m_int_vcount_cb.isnull())
				m_int_vcount_cb(ASSERT_LINE);
		}
	}

	m_hbl_timer->adjust(screen().time_until_pos(scanline, 240));
	m_scan_timer->adjust(screen().time_until_pos((scanline + 1) % 228, 0));
}

void gba_lcd_device::gba_palette(palette_device &palette) const
{
	for (uint8_t b = 0; b < 32; b++)
	{
		for (uint8_t g = 0; g < 32; g++)
		{
			for (uint8_t r = 0; r < 32; r++)
			{
				palette.set_pen_color((b << 10) | (g << 5) | r, pal5bit(r), pal5bit(g), pal5bit(b));
			}
		}
	}
}

uint32_t gba_lcd_device::screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	copybitmap(bitmap, m_bitmap, 0, 0, 0, 0, cliprect);

	return 0;
}

void gba_lcd_device::device_start()
{
	/* resolve callbacks */
	m_int_hblank_cb.resolve();
	m_int_vblank_cb.resolve();
	m_int_vcount_cb.resolve();
	m_dma_hblank_cb.resolve();
	m_dma_vblank_cb.resolve();

	m_pram = make_unique_clear<uint32_t[]>(0x400 / 4);
	m_vram = make_unique_clear<uint32_t[]>(0x18000 / 4);
	m_oam = make_unique_clear<uint32_t[]>(0x400 / 4);

	screen().register_screen_bitmap(m_bitmap);

	/* create a timer to fire scanline functions */
	m_scan_timer = timer_alloc(FUNC(gba_lcd_device::perform_scan), this);
	m_hbl_timer = timer_alloc(FUNC(gba_lcd_device::perform_hbl), this);
	m_scan_timer->adjust(screen().time_until_pos(0, 0));

	save_item(NAME(m_regs));

	save_pointer(NAME(m_pram), 0x400 / 4);
	save_pointer(NAME(m_vram), 0x18000 / 4);
	save_pointer(NAME(m_oam), 0x400 / 4);

	save_item(NAME(m_bg2x.status));
	save_item(NAME(m_bg2x.update));

	save_item(NAME(m_bg2y.status));
	save_item(NAME(m_bg2y.update));

	save_item(NAME(m_bg3x.status));
	save_item(NAME(m_bg3x.update));

	save_item(NAME(m_bg3y.status));
	save_item(NAME(m_bg3y.update));

	save_item(NAME(m_scanline));
}

void gba_lcd_device::device_reset()
{
	memset(m_regs, 0, sizeof(m_regs));

	m_bg2x = { 0, false };
	m_bg2y = { 0, false };
	m_bg3x = { 0, false };
	m_bg3y = { 0, false };

	m_scan_timer->adjust(screen().time_until_pos(0, 0));
	m_hbl_timer->adjust(attotime::never);
}

void gba_lcd_device::device_add_mconfig(machine_config &config)
{
	screen_device &screen(SCREEN(config, "screen", SCREEN_TYPE_LCD));
	screen.set_raw(XTAL(16'777'216) / 4, 308, 0, 240, 228, 0, 160);
	screen.set_screen_update(FUNC(gba_lcd_device::screen_update));
	screen.set_palette("palette");

	PALETTE(config, "palette", FUNC(gba_lcd_device::gba_palette), 32768);
}