summaryrefslogtreecommitdiff
path: root/src/gfx/rndr_gu2.c
blob: 31a26e76c51dd502c879c78f5d6794f7c8134c25 (plain)
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
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
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
/* Copyright (c) 2005 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Neither the name of the Advanced Micro Devices, Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * */

/* 
 * This file contains routines to program the 2D acceleration hardware for
 * the second generation graphics unit.
 * 
 * Basic rendering routines (common to all Geode processors):
 *    gfx_set_bpp           
 *    gfx_set_solid_pattern  
 *    gfx_set_mono_pattern
 *    gfx_set_color_pattern
 *    gfx_set_solid_source  
 *    gfx_set_mono_source
 *    gfx_set_raster_operation
 *    gfx_pattern_fill
 *    gfx_color_pattern_fill
 *    gfx_screen_to_screen_blt
 *    gfx_screen_to_screen_xblt
 *    gfx_color_bitmap_to_screen_blt
 *    gfx_color_bitmap_to_screen_xblt
 *    gfx_mono_bitmap_to_screen_blt
 *    gfx_bresenham_line 
 *    gfx_wait_until_idle   
 *
 * Extended rendering routines for second generation functionality:
 *    gfx2_set_source_stride
 *    gfx2_set_destination_stride
 *    gfx2_set_pattern_origins
 *    gfx2_set_source_transparency
 *    gfx2_set_alpha_mode
 *    gfx2_set_alpha_value
 *    gfx2_pattern_fill
 *    gfx2_color_pattern_fill
 *    gfx2_screen_to_screen_blt
 *    gfx2_mono_expand_blt
 *    gfx2_color_bitmap_to_screen_blt
 *    gfx2_mono_bitmap_to_screen_blt
 *    gfx2_bresenham_line
 *    gfx2_sync_to_vblank
 * */

#define GU2_WAIT_PENDING		\
		while(READ_GP32(MGP_BLT_STATUS) & MGP_BS_BLT_PENDING)
#define GU2_WAIT_BUSY			\
		while(READ_GP32(MGP_BLT_STATUS) & MGP_BS_BLT_BUSY)
#define GU2_WAIT_HALF_EMPTY		\
		while(!(READ_GP32(MGP_BLT_STATUS) & MGP_BS_HALF_EMPTY))

/* PATTERN SWIZZLES */

#define WORD_SWIZZLE(x) (((x) << 16) | ((x) >> 16))
#define BYTE_SWIZZLE(x) (((x) << 24) | ((x) >> 24) | (((x) << 8) & 	\
						0x00FF0000) | (((x) >> 8) & 0x0000FF00))

/* GLOBAL VARIABLES USED BY THE RENDERING ROUTINES */

unsigned long gu2_bpp;
unsigned long gu2_pitch = 1280;
unsigned long gu2_src_pitch = 1280;
unsigned long gu2_dst_pitch = 1280;
unsigned long gu2_xshift = 1;
unsigned long gu2_pattern_origin = 0;
unsigned long gu2_rop32;
unsigned long gu2_alpha32 = 0;
unsigned long gu2_alpha_value = 0;
unsigned long gu2_alpha_mode = 0;
unsigned long gu2_alpha_active = 0;
unsigned short gu2_alpha_blt_mode = 0;
unsigned short gu2_alpha_vec_mode = 0;
unsigned short gu2_blt_mode = 0;
unsigned short gu2_vector_mode = 0;
unsigned short gu2_bm_throttle = 0;
unsigned short gu2_vm_throttle = 0;
int gu2_current_line = 0;

/*---------------------------------------------------------------------------
 * GFX_RESET_PITCH (PRIVATE ROUTINE - NOT PART OF API)
 *
 * This routine resets all pitches in the graphics engine to one value.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_reset_pitch(unsigned short pitch)
#else
void
gfx_reset_pitch(unsigned short pitch)
#endif
{
    gu2_pitch = pitch;
    gu2_dst_pitch = pitch;
    gu2_src_pitch = pitch;
}

/*---------------------------------------------------------------------------
 * GFX_SET_BPP
 *
 * This routine sets the bits per pixel value in the graphics engine.
 * It is also stored in the static variable "gu2_bpp" to use in the future 
 * calls to the rendering routines.  That variable contains the hardware 
 * specific value to load into the MGP_RASTER_MODE register.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_set_bpp(unsigned short bpp)
#else
void
gfx_set_bpp(unsigned short bpp)
#endif
{
    GFXbpp = bpp;

    /* COVERT TO BPP/FORMAT VALUE */
    /* Save in global to combine with ROP later. */
    /* Could write register here and then use byte access for */
    /* the ROP, but would need to set other 24 bits to make */
    /* sure all are set to their appropriate values. */

    switch (bpp) {
    case 8:
        gu2_bpp = MGP_RM_BPPFMT_332;
        gu2_xshift = 0;
        break;
    case 12:
        gu2_bpp = MGP_RM_BPPFMT_4444;
        gu2_xshift = 1;
        break;
    case 15:
        gu2_bpp = MGP_RM_BPPFMT_1555;
        gu2_xshift = 1;
        break;
    case 16:
        gu2_bpp = MGP_RM_BPPFMT_565;
        gu2_xshift = 1;
        break;
    case 32:
        gu2_bpp = MGP_RM_BPPFMT_8888;
        gu2_xshift = 2;
        break;
    }

    /* SET INITIAL ROP BASED ONLY ON BPP */
    /* Needs to be set before loading any pattern or source colors. */
    /* We must wait for BUSY because these bits are not pipelined   */
    /* in the hardware.                                             */

    GU2_WAIT_BUSY;
    WRITE_GP32(MGP_RASTER_MODE, gu2_bpp);
}

/*
 *---------------------------------------------------------------------------
 * GFX_SET_SOLID_SOURCE
 *
 * This routine is used to specify a solid source color.  For the Xfree96
 * display driver, the source color is used to specify a planemask and the 
 * ROP is adjusted accordingly.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_set_solid_source(unsigned long color)
#else
void
gfx_set_solid_source(unsigned long color)
#endif
{
    /* CLEAR TRANSPARENCY FLAG */

    GFXsourceFlags = 0;

    /* WRITE REGISTERS TO SPECIFY SOURCE COLOR */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_SRC_COLOR_FG, color);
}

/*
 *---------------------------------------------------------------------------
 * GFX_SET_MONO_SOURCE
 *
 * This routine is used to specify the monochrome source colors.  
 * It must be called *after* loading any pattern data (those routines 
 * clear the source flags).
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_set_mono_source(unsigned long bgcolor, unsigned long fgcolor,
    unsigned short transparent)
#else
void
gfx_set_mono_source(unsigned long bgcolor, unsigned long fgcolor,
    unsigned short transparent)
#endif
{
    /* SET TRANSPARENCY FLAG */

    GFXsourceFlags = transparent ? MGP_RM_SRC_TRANS : 0;

    /* WRITE COLOR VALUES */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_SRC_COLOR_FG, fgcolor);
    WRITE_GP32(MGP_SRC_COLOR_BG, bgcolor);
}

/*
 *---------------------------------------------------------------------------
 * GFX_SET_SOLID_PATTERN
 *
 * This routine is used to specify a solid pattern color.  It is called 
 * before performing solid rectangle fills or more complicated BLTs that 
 * use a solid pattern color. 
 *
 * The driver should always call "gfx_load_raster_operation" after a call 
 * to this routine to make sure that the pattern flags are set appropriately.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_set_solid_pattern(unsigned long color)
#else
void
gfx_set_solid_pattern(unsigned long color)
#endif
{
    /* CLEAR TRANSPARENCY FLAG */

    GFXsourceFlags = 0;

    /* SET PATTERN FLAGS */

    GFXpatternFlags = 0;

    /* POLL UNTIL ABLE TO WRITE THE PATTERN COLOR */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE, gu2_bpp);
    WRITE_GP32(MGP_PAT_COLOR_0, color);
}

/*
 *---------------------------------------------------------------------------
 * GFX_SET_MONO_PATTERN
 *
 * This routine is used to specify a monochrome pattern. 
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_set_mono_pattern(unsigned long bgcolor, unsigned long fgcolor,
    unsigned long data0, unsigned long data1, unsigned char transparent)
#else
void
gfx_set_mono_pattern(unsigned long bgcolor, unsigned long fgcolor,
    unsigned long data0, unsigned long data1, unsigned char transparent)
#endif
{
    /* CLEAR TRANSPARENCY FLAG */

    GFXsourceFlags = 0;

    /* SET PATTERN FLAGS */

    if (transparent)
        GFXpatternFlags = MGP_RM_PAT_MONO | MGP_RM_PAT_TRANS;
    else
        GFXpatternFlags = MGP_RM_PAT_MONO;

    /* POLL UNTIL ABLE TO WRITE THE PATTERN COLOR */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE, gu2_bpp | GFXpatternFlags);
    WRITE_GP32(MGP_PAT_COLOR_0, bgcolor);
    WRITE_GP32(MGP_PAT_COLOR_1, fgcolor);
    WRITE_GP32(MGP_PAT_DATA_0, data0);
    WRITE_GP32(MGP_PAT_DATA_1, data1);
}

/*
 *---------------------------------------------------------------------------
 * GFX_SET_COLOR_PATTERN
 *
 * This routine is used to specify a color pattern. 
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_set_color_pattern(unsigned long bgcolor, unsigned long fgcolor,
    unsigned long data0, unsigned long data1, unsigned long data2,
    unsigned long data3, unsigned char transparent)
#else
void
gfx_set_color_pattern(unsigned long bgcolor, unsigned long fgcolor,
    unsigned long data0, unsigned long data1, unsigned long data2,
    unsigned long data3, unsigned char transparent)
#endif
{
    /* REMOVE */
}

/*
 *---------------------------------------------------------------------------
 * GFX_LOAD_COLOR_PATTERN_LINE
 *
 * This routine is used to load a single line of a 8x8 color pattern.   
 *---------------------------------------------------------------------------  */
#if GFX_2DACCEL_DYNAMIC
void
gu2_load_color_pattern_line(short y, unsigned long *pattern_8x8)
#else
void
gfx_load_color_pattern_line(short y, unsigned long *pattern_8x8)
#endif
{
    unsigned long temp1, temp2, temp3, temp4;

    /* CLEAR TRANSPARENCY FLAG */

    GFXsourceFlags = 0;

    /* SET PATTERN FLAGS */

    GFXpatternFlags = MGP_RM_PAT_COLOR;

    /* OVERRIDE THE RASTER MODE REGISTER */
    /* If the pattern format is set to anything but color  */
    /* before loading the registers, some of the data will */
    /* be duplicated according to the current mode.        */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE,
        (gu2_rop32 & ~MGP_RM_PAT_FLAGS) | MGP_RM_PAT_COLOR);

    /* LOAD THE PATTERN DATA */
    /* This routine is designed to work in tandem with gfx_pattern_fill.  */
    /* It is used for cases when multiple BLTs with color pattern data    */
    /* are desired on the same line.  It would be inefficient to          */
    /* repeatedly call gfx_color_pattern_fill for each single-line BLT.   */
    /* So, we will simply replicate the pattern data across all available */
    /* lines such that the pattern y origin plays no part in the BLT.     */

    /* 8 BPP */

    if (gu2_xshift == 0) {
        pattern_8x8 += (y & 7) << 1;
        temp1 = BYTE_SWIZZLE(pattern_8x8[0]);
        temp2 = BYTE_SWIZZLE(pattern_8x8[1]);
        WRITE_GP32(MGP_PAT_DATA_1, temp1);
        WRITE_GP32(MGP_PAT_DATA_0, temp2);
        WRITE_GP32(MGP_PAT_COLOR_1, temp1);
        WRITE_GP32(MGP_PAT_COLOR_0, temp2);

        GU2_WAIT_BUSY;
        WRITE_GP32(MGP_PAT_COLOR_3, temp1);
        WRITE_GP32(MGP_PAT_COLOR_2, temp2);
        WRITE_GP32(MGP_PAT_COLOR_5, temp1);
        WRITE_GP32(MGP_PAT_COLOR_4, temp2);
    } else if (gu2_xshift == 1) {
        pattern_8x8 += (y & 7) << 2;
        temp1 = WORD_SWIZZLE(pattern_8x8[0]);
        temp2 = WORD_SWIZZLE(pattern_8x8[1]);
        temp3 = WORD_SWIZZLE(pattern_8x8[2]);
        temp4 = WORD_SWIZZLE(pattern_8x8[3]);

        WRITE_GP32(MGP_PAT_COLOR_1, temp1);
        WRITE_GP32(MGP_PAT_COLOR_0, temp2);
        WRITE_GP32(MGP_PAT_DATA_1, temp3);
        WRITE_GP32(MGP_PAT_DATA_0, temp4);

        GU2_WAIT_BUSY;
        WRITE_GP32(MGP_PAT_COLOR_5, temp1);
        WRITE_GP32(MGP_PAT_COLOR_4, temp2);
        WRITE_GP32(MGP_PAT_COLOR_3, temp3);
        WRITE_GP32(MGP_PAT_COLOR_2, temp4);
    } else {
        pattern_8x8 += (y & 7) << 3;

        WRITE_GP32(MGP_PAT_COLOR_1, pattern_8x8[4]);
        WRITE_GP32(MGP_PAT_COLOR_0, pattern_8x8[5]);
        WRITE_GP32(MGP_PAT_DATA_1, pattern_8x8[6]);
        WRITE_GP32(MGP_PAT_DATA_0, pattern_8x8[7]);

        GU2_WAIT_BUSY;
        WRITE_GP32(MGP_PAT_COLOR_5, pattern_8x8[0]);
        WRITE_GP32(MGP_PAT_COLOR_4, pattern_8x8[1]);
        WRITE_GP32(MGP_PAT_COLOR_3, pattern_8x8[2]);
        WRITE_GP32(MGP_PAT_COLOR_2, pattern_8x8[3]);
    }
}

/*
 *---------------------------------------------------------------------------
 * GFX_SET_RASTER_OPERATION
 *
 * This routine loads the specified raster operation.  It sets the pattern
 * flags appropriately.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_set_raster_operation(unsigned char rop)
#else
void
gfx_set_raster_operation(unsigned char rop)
#endif
{
    gu2_blt_mode = 0;

    /* DISABLE ALPHA BLENDING */

    gu2_alpha_active = 0;

    /* GENERATE 32-BIT VERSION OF ROP WITH PATTERN FLAGS */

    gu2_rop32 = (unsigned long)rop | GFXpatternFlags | gu2_bpp;

    /* CHECK IF SOURCE FLAGS SHOULD BE MERGED */

    if ((rop & 0x33) ^ ((rop >> 2) & 0x33))
        gu2_rop32 |= GFXsourceFlags;
    else
        gu2_blt_mode = 0x40;

    /* SET FLAG INDICATING ROP REQUIRES DESTINATION DATA */
    /* True if even bits (0:2:4:6) do not equal the corresponding */
    /* even bits (1:3:5:7). */

    if ((rop & 0x55) ^ ((rop >> 1) & 0x55)) {
        gu2_blt_mode |= MGP_BM_DST_REQ;
        gu2_vector_mode = MGP_VM_DST_REQ;
    } else {
        gu2_vector_mode = 0;
    }
}

/*
 *----------------------------------------------------------------------------
 * GFX_PATTERN_FILL
 *
 * This routine is used to fill a rectangular region.  The pattern must 
 * be previously loaded using one of GFX_load_*_pattern routines.  Also, the 
 * raster operation must be previously specified using the 
 * "GFX_load_raster_operation" routine.
 *
 *      X               screen X position (left)
 *      Y               screen Y position (top)
 *      WIDTH           width of rectangle, in pixels
 *      HEIGHT          height of rectangle, in scanlines
 *----------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_pattern_fill(unsigned short x, unsigned short y,
    unsigned short width, unsigned short height)
#else
void
gfx_pattern_fill(unsigned short x, unsigned short y,
    unsigned short width, unsigned short height)
#endif
{
    unsigned long offset = 0, size;

    size = (((unsigned long)width) << 16) | height;

    /* CALCULATE STARTING OFFSET */

    offset = (unsigned long)y *gu2_pitch + (((unsigned long)x) << gu2_xshift);

    /* CHECK IF PATTERN ORIGINS NEED TO BE SET */

    if (GFXpatternFlags) {
        /* COMBINE X AND Y PATTERN ORIGINS WITH OFFSET */

        offset |= ((unsigned long)(x & 7)) << 26;
        offset |= ((unsigned long)(y & 7)) << 29;
    }

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first). */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE, gu2_rop32);
    WRITE_GP32(MGP_DST_OFFSET, offset);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_pitch);
    WRITE_GP32(MGP_BLT_MODE, gu2_blt_mode);
}

/*
 *----------------------------------------------------------------------------
 * GFX_COLOR_PATTERN_FILL
 *
 * This routine is used to render a rectangle using the current raster 
 * operation and the specified color pattern.  It allows an 8x8 color 
 * pattern to be rendered without multiple calls to the gfx_set_color_pattern
 * and gfx_pattern_fill routines.
 *
 *      X               screen X position (left)
 *      Y               screen Y position (top)
 *      WIDTH           width of rectangle, in pixels
 *      HEIGHT          height of rectangle, in scanlines
 *      *PATTERN		pointer to 8x8 color pattern data
 *----------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_color_pattern_fill(unsigned short x, unsigned short y,
    unsigned short width, unsigned short height, unsigned long *pattern)
#else
void
gfx_color_pattern_fill(unsigned short x, unsigned short y,
    unsigned short width, unsigned short height, unsigned long *pattern)
#endif
{
    /* CALL GFX2 ROUTINE TO AVOID DUPLICATION OF CODE */

    unsigned long offset = (unsigned long)y * gu2_pitch +
        (((unsigned long)x) << gu2_xshift);
    unsigned long origin = gu2_pattern_origin;
    unsigned long pitch = gu2_dst_pitch;

    gfx2_set_pattern_origin(x, y);
    gfx2_set_destination_stride((unsigned short)gu2_pitch);
    gfx2_color_pattern_fill(offset, width, height, pattern);

    /* RESTORE GFX2 VALUES */

    gu2_pattern_origin = origin;
    gu2_dst_pitch = pitch;
}

/*
 *----------------------------------------------------------------------------
 * SCREEN TO SCREEN BLT
 *
 * This routine should be used to perform a screen to screen BLT when the 
 * ROP does not require destination data.
 *
 *      SRCX            screen X position to copy from
 *      SRCY            screen Y position to copy from
 *      DSTX            screen X position to copy to
 *      DSTY            screen Y position to copy to
 *      WIDTH           width of rectangle, in pixels
 *      HEIGHT          height of rectangle, in scanlines
 *----------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_screen_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height)
#else
void
gfx_screen_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height)
#endif
{
    unsigned long srcoffset, dstoffset, size;
    unsigned short blt_mode;

    size = (((unsigned long)width) << 16) | height;

    /* CALCULATE THE DIRECTION OF THE BLT */

    blt_mode = (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK) | MGP_BM_SRC_FB;
    if (dstx > srcx) {
        blt_mode |= MGP_BM_NEG_XDIR;
        srcx += width - 1;
        dstx += width - 1;
    }
    if (dsty > srcy) {
        blt_mode |= MGP_BM_NEG_YDIR;
        srcy += height - 1;
        dsty += height - 1;
    }

    /* CALCULATE STARTING OFFSETS */

    srcoffset = (unsigned long)srcy *gu2_pitch +
        (((unsigned long)srcx) << gu2_xshift);
    dstoffset = ((unsigned long)dsty * gu2_pitch +
        (((unsigned long)dstx) << gu2_xshift)) & 0xFFFFFF;

    /* MERGE PATTERN INFORMATION */
    /* This must be done after the x and y coordinates have been updated,  */
    /* as the x and y pattern origins correspond to the first ROPed pixel. */

    if (GFXpatternFlags) {
        /* COMBINE X AND Y PATTERN ORIGINS WITH OFFSET */

        dstoffset |= ((unsigned long)(dstx & 7)) << 26;
        dstoffset |= ((unsigned long)(dsty & 7)) << 29;
    }

    /* TURN INTO BYTE ADDRESS IF NEGATIVE X DIRECTION */
    /* This is a quirk of the hardware. */

    if (blt_mode & MGP_BM_NEG_XDIR) {
        srcoffset += (1 << gu2_xshift) - 1;
        dstoffset += (1 << gu2_xshift) - 1;
    }

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first). */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    WRITE_GP32(MGP_SRC_OFFSET, srcoffset);
    WRITE_GP32(MGP_DST_OFFSET, dstoffset);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_pitch | (gu2_pitch << 16));
    WRITE_GP16(MGP_BLT_MODE, blt_mode);
}

/*
 *----------------------------------------------------------------------------
 * SCREEN TO SCREEN TRANSPARENT BLT
 *
 * This routine should be used to perform a screen to screen BLT when a 
 * specified color should by transparent.  The only supported ROP is SRCCOPY.
 *
 *      SRCX            screen X position to copy from
 *      SRCY            screen Y position to copy from
 *      DSTX            screen X position to copy to
 *      DSTY            screen Y position to copy to
 *      WIDTH           width of rectangle, in pixels
 *      HEIGHT          height of rectangle, in scanlines
 *      COLOR           transparent color
 *----------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_screen_to_screen_xblt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned long color)
#else
void
gfx_screen_to_screen_xblt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned long color)
#endif
{
    unsigned long rop32;

    /* SAVE ORIGINAL RASTER MODE SETTINGS */

    rop32 = gu2_rop32;

    /* WRITE REGISTERS TO SPECIFY COLOR TRANSPARENCY */
    /* Match GU1 implementation that only allows SRCCOPY for the ROP. */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_SRC_COLOR_FG, color);
    WRITE_GP32(MGP_SRC_COLOR_BG, 0xFFFFFFFF);

    /* SET GLOBAL RASTER SETTINGS */
    /* This is needed, as the screen-to-screen BLT      */
    /* routine will overwrite the raster mode register. */

    gu2_rop32 = gu2_bpp | MGP_RM_SRC_TRANS | 0xCC;

    /* CALL NORMAL SCREEN TO SCREEN BLT ROUTINE */

    gfx_screen_to_screen_blt(srcx, srcy, dstx, dsty, width, height);

    /* RESTORE GLOBAL RASTER SETTINGS */

    gu2_rop32 = rop32;
}

/*
 *----------------------------------------------------------------------------
 * COLOR BITMAP TO SCREEN BLT
 *
 * This routine transfers color bitmap data to the screen.
 *
 *      SRCX            X offset within source bitmap
 *      SRCY            Y offset within source bitmap
 *      DSTX            screen X position to render data
 *      DSTY            screen Y position to render data
 *      WIDTH           width of rectangle, in pixels
 *      HEIGHT          height of rectangle, in scanlines
 *      *DATA           pointer to bitmap data
 *      PITCH           pitch of bitmap data (bytes between scanlines)
 *
 * Transparency is handled by another routine.
 *----------------------------------------------------------------------------
 */

#if GFX_2DACCEL_DYNAMIC
void
gu2_color_bitmap_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned char *data, long pitch)
#else
void
gfx_color_bitmap_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned char *data, long pitch)
#endif
{
    unsigned long dstoffset, srcoffset, size, bytes;
    unsigned long offset, temp_offset;
    unsigned long dword_bytes, bytes_extra;
    unsigned short blt_mode;

    blt_mode = (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK) | MGP_BM_SRC_FB;
    size = (((unsigned long)width) << 16) | 1;

    /* CALCULATE STARTING OFFSETS */

    offset = (unsigned long)srcy *pitch + ((unsigned long)srcx << gu2_xshift);

    dstoffset = (unsigned long)dsty *gu2_pitch +
        (((unsigned long)dstx) << gu2_xshift);

    /* CHECK IF PATTERN ORIGINS NEED TO BE SET */

    if (GFXpatternFlags) {
        /* COMBINE X AND Y PATTERN ORIGINS WITH OFFSET */

        dstoffset |= ((unsigned long)(dstx & 7)) << 26;
        dstoffset |= ((unsigned long)(dsty & 7)) << 29;
    }

    bytes = width << gu2_xshift;
    dword_bytes = bytes & ~0x3L;
    bytes_extra = bytes & 0x3L;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first).   */
    /* The source offset is always 0 since we allow misaligned dword reads. */
    /* We must wait for BLT busy because the GP may be executing a screen   */
    /* to screen BLT from the scratchpad area.                              */

    GU2_WAIT_BUSY;
    WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_pitch);

    /* WRITE DATA ONE LINE AT A TIME */
    /* For speed reasons, data is written to an offscreen scratch area and then        */
    /* BLTed using a screen to screen BLT. This is similar to the GX1 BLT buffers, but */
    /* slightly more efficient in that we can queue up data while the GP is rendering  */
    /* a line.                                                                         */

    while (height--) {
        temp_offset = offset;
        srcoffset = gfx_gx2_scratch_base;
        if (gu2_current_line)
            srcoffset += 8192;

        GU2_WAIT_PENDING;
        WRITE_GP32(MGP_SRC_OFFSET, srcoffset);
        WRITE_GP32(MGP_DST_OFFSET, dstoffset);
        dstoffset += gu2_pitch;
        dstoffset += 0x20000000;

        WRITE_FRAME_BUFFER_STRING32(srcoffset, dword_bytes, data,
            temp_offset);
        if (bytes_extra) {
            temp_offset += dword_bytes;
            srcoffset += dword_bytes;
            WRITE_FRAME_BUFFER_STRING8(srcoffset, bytes_extra, data,
                temp_offset);
        }
        WRITE_GP16(MGP_BLT_MODE, blt_mode);
        offset += pitch;
        gu2_current_line = 1 - gu2_current_line;
    }
}

/*
 *----------------------------------------------------------------------------
 * COLOR BITMAP TO SCREEN TRANSPARENT BLT
 *
 * This routine transfers color bitmap data to the screen with transparency.
 * The transparent color is specified.  The only supported ROP is SRCCOPY, 
 * meaning that transparency cannot be applied if the ROP requires 
 * destination data (this is a hardware restriction).
 *
 *      SRCX            X offset within source bitmap
 *      SRCY            Y offset within source bitmap
 *      DSTX            screen X position to render data
 *      DSTY            screen Y position to render data
 *      WIDTH           width of rectangle, in pixels
 *      HEIGHT          height of rectangle, in scanlines
 *      *DATA           pointer to bitmap data
 *      PITCH           pitch of bitmap data (bytes between scanlines)
 *      COLOR           transparent color
 *----------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_color_bitmap_to_screen_xblt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned char *data, long pitch,
    unsigned long color)
#else
void
gfx_color_bitmap_to_screen_xblt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned char *data, long pitch,
    unsigned long color)
#endif
{
    unsigned long rop32;

    /* SAVE EXISTING RASTER MODE SETTINGS */

    rop32 = gu2_rop32;

    /* WRITE REGISTERS TO SPECIFY COLOR TRANSPARENCY */
    /* Match GU1 implementation that only allows SRCCOPY for the ROP. */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_SRC_COLOR_FG, color);
    WRITE_GP32(MGP_SRC_COLOR_BG, 0xFFFFFFFF);

    /* SET GLOBAL RASTER SETTINGS */
    /* This is needed, as the screen-to-screen BLT      */
    /* routine will overwrite the raster mode register. */

    gu2_rop32 = gu2_bpp | MGP_RM_SRC_TRANS | 0xCC;

    /* CALL NORMAL COLOR BITMAP TO SCREEN BLT ROUTINE */

    gfx_color_bitmap_to_screen_blt(srcx, srcy, dstx, dsty, width, height,
        data, pitch);

    /* RESTORE RASTER SETTINGS */

    gu2_rop32 = rop32;
}

/*
 *----------------------------------------------------------------------------
 * MONOCHROME BITMAP TO SCREEN BLT
 *
 * This routine transfers monochrome bitmap data to the screen.  
 *
 *      SRCX            X offset within source bitmap
 *      SRCY            Y offset within source bitmap
 *      DSTX            screen X position to render data
 *      DSTY            screen Y position to render data
 *      WIDTH           width of rectangle, in pixels
 *      HEIGHT          height of rectangle, in scanlines
 *      *DATA           pointer to bitmap data
 *      PITCH           pitch of bitmap data (bytes between scanlines)
 *----------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_mono_bitmap_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned char *data, short pitch)
#else
void
gfx_mono_bitmap_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned char *data, short pitch)
#endif
{
    unsigned long dstoffset, size, bytes;
    unsigned long offset, temp_offset, temp1 = 0, temp2 = 0;
    unsigned long i, j = 0, fifo_lines, dwords_extra, bytes_extra;
    unsigned long shift = 0;

    size = (((unsigned long)width) << 16) | height;

    /* CALCULATE STARTING OFFSETS */

    offset = (unsigned long)srcy *pitch + ((unsigned long)srcx >> 3);

    dstoffset = (unsigned long)dsty *gu2_pitch +
        (((unsigned long)dstx) << gu2_xshift);

    /* CHECK IF PATTERN ORIGINS NEED TO BE SET */

    if (GFXpatternFlags) {
        /* COMBINE X AND Y PATTERN ORIGINS WITH OFFSET */

        dstoffset |= ((unsigned long)(dstx & 7)) << 26;
        dstoffset |= ((unsigned long)(dsty & 7)) << 29;
    }

    bytes = ((srcx & 7) + width + 7) >> 3;
    fifo_lines = bytes >> 5;
    dwords_extra = (bytes & 0x0000001Cl) >> 2;
    bytes_extra = bytes & 0x00000003l;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first).   */
    /* The source offset is always 0 since we allow misaligned dword reads. */
    /* Need to wait for busy instead of pending, since hardware clears      */
    /* the host data FIFO at the beginning of a BLT.                        */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    WRITE_GP32(MGP_SRC_OFFSET, ((unsigned long)srcx & 7) << 26);
    WRITE_GP32(MGP_DST_OFFSET, dstoffset);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_pitch);
    WRITE_GP16(MGP_BLT_MODE,
        (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK) | MGP_BM_SRC_HOST |
        MGP_BM_SRC_MONO);

    /* WAIT FOR BLT TO BE LATCHED */

    GU2_WAIT_PENDING;

    /* WRITE ALL OF THE DATA TO THE HOST SOURCE REGISTER */

    while (height--) {
        temp_offset = offset;

        /* WRITE ALL FULL FIFO LINES */

        for (i = 0; i < fifo_lines; i++) {
            GU2_WAIT_HALF_EMPTY;
            WRITE_GPREG_STRING32(MGP_HST_SOURCE, 8, j, data, temp_offset,
                temp1);
            temp_offset += 32;
        }

        /* WRITE ALL FULL DWORDS */

        GU2_WAIT_HALF_EMPTY;
        if (dwords_extra) {
            WRITE_GPREG_STRING32(MGP_HST_SOURCE, dwords_extra, i, data,
                temp_offset, temp1);
            temp_offset += (dwords_extra << 2);
        }

        /* WRITE REMAINING BYTES */

        shift = 0;
        if (bytes_extra)
            WRITE_GPREG_STRING8(MGP_HST_SOURCE, bytes_extra, shift, i, data,
                temp_offset, temp1, temp2);

        offset += pitch;
    }
}

/*---------------------------------------------------------------------------
 * GFX_TEXT_BLT	
 *
 * This routine is similar to the gfx_mono_bitmap_to_screen_blt routine 
 * but assumes that source data is byte-packed.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_text_blt(unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned char *data)
#else
void
gfx_text_blt(unsigned short dstx, unsigned short dsty, unsigned short width,
    unsigned short height, unsigned char *data)
#endif
{
    unsigned long size, bytes;
    unsigned long dstoffset, temp1 = 0, temp2 = 0, temp_offset = 0;
    unsigned long i, j = 0, fifo_lines, dwords_extra, bytes_extra;
    unsigned long shift;

    size = (((unsigned long)width) << 16) | height;

    dstoffset = (unsigned long)dsty *gu2_pitch +
        (((unsigned long)dstx) << gu2_xshift);

    /* CHECK IF PATTERN ORIGINS NEED TO BE SET */

    if (GFXpatternFlags) {
        /* COMBINE X AND Y PATTERN ORIGINS WITH OFFSET */

        dstoffset |= ((unsigned long)(dstx & 7)) << 26;
        dstoffset |= ((unsigned long)(dsty & 7)) << 29;
    }

    /* CALCULATE STARTING OFFSETS */

    bytes = ((width + 7) >> 3) * height;
    fifo_lines = bytes >> 5;
    dwords_extra = (bytes & 0x0000001Cl) >> 2;
    bytes_extra = bytes & 0x00000003l;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    WRITE_GP32(MGP_SRC_OFFSET, 0);
    WRITE_GP32(MGP_DST_OFFSET, dstoffset);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_pitch);
    WRITE_GP16(MGP_BLT_MODE,
        (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK) | MGP_BM_SRC_HOST |
        MGP_BM_SRC_BP_MONO);

    /* WAIT FOR BLT TO BE LATCHED */

    GU2_WAIT_PENDING;

    /* WRITE ALL FULL FIFO LINES */

    for (i = 0; i < fifo_lines; i++) {
        GU2_WAIT_HALF_EMPTY;
        WRITE_GPREG_STRING32(MGP_HST_SOURCE, 8, j, data, temp_offset, temp1);
        temp_offset += 32;
    }

    /* WRITE ALL FULL DWORDS */

    if (dwords_extra || bytes_extra) {
        GU2_WAIT_HALF_EMPTY;
        if (dwords_extra) {
            WRITE_GPREG_STRING32(MGP_HST_SOURCE, dwords_extra, i, data,
                temp_offset, temp1);
            temp_offset += (dwords_extra << 2);
        }
        if (bytes_extra) {
            shift = 0;
            WRITE_GPREG_STRING8(MGP_HST_SOURCE, bytes_extra, shift, i, data,
                temp_offset, temp1, temp2);
        }
    }
}

/*
 *----------------------------------------------------------------------------
 * BRESENHAM LINE
 *
 * This routine draws a vector using the specified Bresenham parameters.  
 * Currently this file does not support a routine that accepts the two 
 * endpoints of a vector and calculates the Bresenham parameters.  If it 
 * ever does, this routine is still required for vectors that have been 
 * clipped.
 *
 *      X               screen X position to start vector
 *      Y               screen Y position to start vector
 *      LENGTH          length of the vector, in pixels
 *      INITERR         Bresenham initial error term
 *      AXIALERR        Bresenham axial error term
 *      DIAGERR         Bresenham diagonal error term
 *      FLAGS           VM_YMAJOR, VM_MAJOR_INC, VM_MINOR_INC
 *----------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_bresenham_line(unsigned short x, unsigned short y,
    unsigned short length, unsigned short initerr,
    unsigned short axialerr, unsigned short diagerr, unsigned short flags)
#else
void
gfx_bresenham_line(unsigned short x, unsigned short y,
    unsigned short length, unsigned short initerr,
    unsigned short axialerr, unsigned short diagerr, unsigned short flags)
#endif
{
    unsigned long offset;
    unsigned long data1 = (((unsigned long)axialerr) << 16) | diagerr;
    unsigned long data2 = (((unsigned long)length) << 16) | initerr;
    unsigned short vector_mode = gu2_vector_mode | flags;

    /* CALCULATE STARTING OFFSET */

    offset = (unsigned long)y *gu2_pitch + (((unsigned long)x) << gu2_xshift);

    /* CHECK NULL LENGTH */

    if (!length)
        return;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first). */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE, gu2_rop32);
    WRITE_GP32(MGP_DST_OFFSET, offset);
    WRITE_GP32(MGP_VEC_ERR, data1);
    WRITE_GP32(MGP_VEC_LEN, data2);
    WRITE_GP32(MGP_STRIDE, gu2_pitch);
    WRITE_GP32(MGP_VECTOR_MODE, vector_mode);
}

/*---------------------------------------------------------------------------
 * GFX_WAIT_UNTIL_IDLE
 *
 * This routine waits until the graphics engine is idle.  This is required
 * before allowing direct access to the frame buffer.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu2_wait_until_idle(void)
#else
void
gfx_wait_until_idle(void)
#endif
{
    while (READ_GP32(MGP_BLT_STATUS) & MGP_BS_BLT_BUSY) ;
}

/*---------------------------------------------------------------------------
 * GFX_TEST_BLT_PENDING
 *
 * This routine returns 1 if a BLT is pending, meaning that a call to 
 * perform a rendering operation would stall.  Otherwise it returns 0.
 * It is used by Darwin during random testing to only start a BLT 
 * operation when it knows the Durango routines won't spin on graphics
 * (so Darwin can continue to do frame buffer reads and writes).
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
int
gu2_test_blt_pending(void)
#else
int
gfx_test_blt_pending(void)
#endif
{
    if (READ_GP32(MGP_BLT_STATUS) & MGP_BS_BLT_PENDING)
        return (1);
    return (0);
}

/*---------------------------------------------------------------------------
 * NEW ROUTINES FOR REDCLOUD
 *---------------------------------------------------------------------------
 */

/*---------------------------------------------------------------------------
 * GFX2_SET_SOURCE_STRIDE
 *
 * This routine sets the stride to be used in successive screen to screen 
 * BLTs (used by gfx2_screen_to_screen_blt and gfx2_mono_expand_blt).
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_set_source_stride(unsigned short stride)
#else
void
gfx2_set_source_stride(unsigned short stride)
#endif
{
    /* SAVE STRIDE TO BE USED LATER */

    gu2_src_pitch = (unsigned long)stride;
}

/*---------------------------------------------------------------------------
 * GFX2_SET_DESTINATION_STRIDE
 *
 * This routine sets the stride used when rendering to the screen.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_set_destination_stride(unsigned short stride)
#else
void
gfx2_set_destination_stride(unsigned short stride)
#endif
{
    /* SAVE STRIDE TO BE USED LATER */

    gu2_dst_pitch = (unsigned long)stride;
}

/*---------------------------------------------------------------------------
 * GFX2_SET_PATTERN_ORIGIN
 *
 * This routine sets the origin within an 8x8 pattern.  It is needed if 
 * using a monochrome or color pattern (not used for a solid pattern).
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_set_pattern_origin(int x, int y)
#else
void
gfx2_set_pattern_origin(int x, int y)
#endif
{
    /* STORE IN FORMAT THAT CAN BE COMBINED WITH THE DESTINATION OFFSET */

    gu2_pattern_origin = (((unsigned long)(x & 7)) << 26) |
        (((unsigned long)(y & 7)) << 29);
}

/*---------------------------------------------------------------------------
 * GFX2_SET_SOURCE_TRANSPARENCY
 *
 * This routine sets the source transparency color and mask to be used
 * in future rendering operations.  If both the color and mask are set 
 * to zero (normally completely transparent), those values indicate that
 * transparency should be disabled.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_set_source_transparency(unsigned long color, unsigned long mask)
#else
void
gfx2_set_source_transparency(unsigned long color, unsigned long mask)
#endif
{
    /* WRITE REGISTERS TO SPECIFY COLOR TRANSPARENCY */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_SRC_COLOR_FG, color);
    WRITE_GP32(MGP_SRC_COLOR_BG, mask);

    /* SET TRANSPARENCY FLAG */

    GFXsourceFlags = (color || mask) ? MGP_RM_SRC_TRANS : 0;
}

/*---------------------------------------------------------------------------
 * GFX2_SET_ALPHA_MODE
 *
 * This routine sets the alpha blending mode to be used in successive 
 * rendering operations.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_set_alpha_mode(int mode)
#else
void
gfx2_set_alpha_mode(int mode)
#endif
{
    /* SAVE ALPHA MODE FOR LATER */

    gu2_alpha_mode = mode;
}

/*---------------------------------------------------------------------------
 * GFX2_SET_ALPHA_VALUE
 *
 * This routine sets the alpha value to be used with certain alpha blending
 * modes (ALPHA_MODE_BLEND).
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_set_alpha_value(unsigned char value)
#else
void
gfx2_set_alpha_value(unsigned char value)
#endif
{
    /* SAVE ALPHA VALUE TO BE USED LATER */

    gu2_alpha_value = (unsigned long)value;

    /* SET GLOBAL FLAG */
    /* gfx2_* routines will use this flag to program alpha values */
    /* appropriately.  Normal gfx_* routines will always write    */
    /* the current ROP settings.  In this way, the alpha mode     */
    /* affects only second generation routines.                   */

    gu2_alpha_active = 1;

    switch (gu2_alpha_mode) {
    case ALPHA_MODE_BLEND:

        /* GENERATE 32-BIT VERSION OF RASTER MODE REGISTER */
        /* Pattern data is not involved in the operation.  */

        gu2_alpha32 = gu2_alpha_value | gu2_bpp;

        /* HANDLE SPECIAL CASES FOR ENDPOINTS              */
        /* The 8-bit hardware alpha value is always        */
        /* interpreted as a fraction.  Consequently, there */
        /* is no way to use values of 255 or 0 to exclude  */
        /* one of the inputs.                              */

        switch (gu2_alpha_value) {
            /* DESTINATION ONLY                               */
            /* Operation is alpha * A, where A is destination */
            /* and alpha is 1.                                */

        case 0:

            gu2_alpha32 |= MGP_RM_SELECT_ALPHA_1 |
                MGP_RM_ALPHA_TIMES_A |
                MGP_RM_ALPHA_TO_RGB | MGP_RM_DEST_FROM_CHAN_A;
            break;

            /* SOURCE ONLY                                    */
            /* Operation is alpha * A, where A is source and  */
            /* alpha is 1.                                    */

        case 255:

            gu2_alpha32 |= MGP_RM_SELECT_ALPHA_1 |
                MGP_RM_ALPHA_TO_RGB | MGP_RM_ALPHA_TIMES_A;
            break;

            /* DEFAULT                                        */
            /* Operation is alpha * A + (1 - alpha) * B;      */
            /* A is source, B is destination and alpha is the */
            /* programmed 8-bit value.                        */

        default:

            gu2_alpha32 |= MGP_RM_SELECT_ALPHA_R |
                MGP_RM_ALPHA_TO_RGB | MGP_RM_ALPHA_A_PLUS_BETA_B;

        }

        /* CHECK IF SOURCE INFORMATION SHOULD BE MERGED    */
        /* Alpha value of 0 indicates destination only.    */

        if (gu2_alpha_value != 0)
            gu2_alpha32 |= GFXsourceFlags;

        /* SET FLAG FOR DESTINATION DATA IF NECESSARY      */
        /* Alpha value of 255 indicates no destination     */

        if (gu2_alpha_value != 255) {
            gu2_alpha_blt_mode = MGP_BM_DST_REQ;
            gu2_alpha_vec_mode = MGP_VM_DST_REQ;
        }

        break;
    }
}

/*---------------------------------------------------------------------------
 * GFX2_PATTERN_FILL
 *
 * This routine is similar to the gfx_pattern_fill routine, but allows the
 * use of an arbitrary destination stride.  The rendering position is 
 * also specified as an offset instead of an (x,y) position.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_pattern_fill(unsigned long dstoffset, unsigned short width,
    unsigned short height)
#else
void
gfx2_pattern_fill(unsigned long dstoffset, unsigned short width,
    unsigned short height)
#endif
{
    unsigned long size;

    size = (((unsigned long)width) << 16) | height;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first). */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE, gu2_rop32);
    WRITE_GP32(MGP_DST_OFFSET, dstoffset | gu2_pattern_origin);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_dst_pitch);
    WRITE_GP32(MGP_BLT_MODE, gu2_blt_mode | gu2_bm_throttle);
    gu2_bm_throttle = 0;
    gu2_vm_throttle = 0;
}

/*---------------------------------------------------------------------------
 * GFX2_COLOR_PATTERN_FILL
 *
 * This routine is used to render a rectangle using the current raster 
 * operation and the specified color pattern.  It allows an 8x8 color 
 * pattern to be rendered without multiple calls to the gfx_set_color_pattern
 * and gfx_pattern_fill routines.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_color_pattern_fill(unsigned long dstoffset, unsigned short width,
    unsigned short height, unsigned long *pattern)
#else
void
gfx2_color_pattern_fill(unsigned long dstoffset, unsigned short width,
    unsigned short height, unsigned long *pattern)
#endif
{
    int pass;
    unsigned long lines, size, patxorigin, patoffset;

    /* ONLY USE HW PATTERN ORIGIN FOR THE X DIRECTION */
    /* Y direction handled by referencing proper location in pattern data. */

    patxorigin = (gu2_pattern_origin) & 0x1C000000;

    /* OVERRIDE PATTERN FLAGS IN ROP TO FORCE COLOR PATTERN */

    GU2_WAIT_PENDING;
    WRITE_GP32(MGP_RASTER_MODE,
        (gu2_rop32 & ~MGP_RM_PAT_FLAGS) | MGP_RM_PAT_COLOR);

    /* ATTEMPT TO OPTIMIZE */
    /* If possible, we can perform the pattern fill in only a few passes    */
    /* This is performed by multiplying the pitch by an appropriate amount. */
    /* Consequently, if the multiplied pitch exceeds 16 bits, this          */
    /* optimization is impossible.                                          */

    if ((gu2_dst_pitch << (gu2_xshift + 1)) <= 0xFFFF) {
        /* HANDLE VARIOUS COLOR DEPTHS DIFFERENTLY */

        switch (gu2_xshift) {
        case 0:                       /* 8 BPP */

            /* TWO PASSES FOR 8 BPP */
            /* Render every other line per pass by doubling the pitch. */

            patoffset = (gu2_pattern_origin >> 28) & 0x0E;
            for (pass = 0; pass < 2; pass++) {
                /* CAN WRITE SOME PATTERN REGISTERS WHILE "PENDING" */

                GU2_WAIT_PENDING;
                WRITE_GP32(MGP_DST_OFFSET, dstoffset | patxorigin);
                lines = (height + 1 - pass) >> 1;
                if (!lines)
                    break;
                size = (((unsigned long)width) << 16) | lines;
                WRITE_GP32(MGP_WID_HEIGHT, size);
                WRITE_GP32(MGP_STRIDE, gu2_dst_pitch << 1);
                WRITE_GP32(MGP_PAT_DATA_1, BYTE_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_DATA_0,
                    BYTE_SWIZZLE(pattern[patoffset + 1]));
                patoffset = (patoffset + 4) & 0x0E;
                WRITE_GP32(MGP_PAT_COLOR_1, BYTE_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_0,
                    BYTE_SWIZZLE(pattern[patoffset + 1]));
                patoffset = (patoffset + 4) & 0x0E;

                /* NEED TO WAIT UNTIL IDLE FOR COLORS 2 THROUGH 5 */
                /* Those registers are not pipelined. */

                GU2_WAIT_BUSY;
                WRITE_GP32(MGP_PAT_COLOR_3, BYTE_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_2,
                    BYTE_SWIZZLE(pattern[patoffset + 1]));
                patoffset = (patoffset + 4) & 0x0E;
                WRITE_GP32(MGP_PAT_COLOR_5, BYTE_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_4,
                    BYTE_SWIZZLE(pattern[patoffset + 1]));
                WRITE_GP16(MGP_BLT_MODE, gu2_blt_mode | gu2_bm_throttle);
                gu2_bm_throttle = 0;
                gu2_vm_throttle = 0;

                /* ADJUST FOR NEXT PASS */

                dstoffset += gu2_dst_pitch;
                patoffset = (patoffset + 6) & 0x0E;
            }
            break;

        case 1:                       /* 12, 15, OR 16 BPP */

            /* FOUR PASSES FOR 16 BPP */
            /* Render every 4th line per pass by quadrupling the pitch. */

            patoffset = (gu2_pattern_origin >> 27) & 0x1C;
            for (pass = 0; pass < 4; pass++) {
                /* CAN WRITE SOME PATTERN REGISTERS WHILE "PENDING" */

                GU2_WAIT_PENDING;
                WRITE_GP32(MGP_DST_OFFSET, dstoffset | patxorigin);
                lines = (height + 3 - pass) >> 2;
                if (!lines)
                    break;
                size = (((unsigned long)width) << 16) | lines;
                WRITE_GP32(MGP_WID_HEIGHT, size);
                WRITE_GP32(MGP_STRIDE, gu2_dst_pitch << 2);
                WRITE_GP32(MGP_PAT_COLOR_1, WORD_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_0,
                    WORD_SWIZZLE(pattern[patoffset + 1]));
                WRITE_GP32(MGP_PAT_DATA_1,
                    WORD_SWIZZLE(pattern[patoffset + 2]));
                WRITE_GP32(MGP_PAT_DATA_0,
                    WORD_SWIZZLE(pattern[patoffset + 3]));
                patoffset = (patoffset + 16) & 0x1C;

                /* NEED TO WAIT UNTIL IDLE FOR COLORS 2 THROUGH 5 */
                /* Those registers are not pipelined. */

                GU2_WAIT_BUSY;
                WRITE_GP32(MGP_PAT_COLOR_5, WORD_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_4,
                    WORD_SWIZZLE(pattern[patoffset + 1]));
                WRITE_GP32(MGP_PAT_COLOR_3,
                    WORD_SWIZZLE(pattern[patoffset + 2]));
                WRITE_GP32(MGP_PAT_COLOR_2,
                    WORD_SWIZZLE(pattern[patoffset + 3]));
                WRITE_GP16(MGP_BLT_MODE, gu2_blt_mode | gu2_bm_throttle);
                gu2_bm_throttle = 0;
                gu2_vm_throttle = 0;

                /* ADJUST FOR NEXT PASS */

                dstoffset += gu2_dst_pitch;
                patoffset = (patoffset + 20) & 0x1C;
            }
            break;

        case 2:                       /* 32 BPP */

            /* EIGHT PASSES FOR 32 BPP */
            /* Render every 8th line per pass by setting pitch * 8. */

            patoffset = (gu2_pattern_origin >> 26) & 0x38;
            for (pass = 0; pass < 8; pass++) {
                /* CAN WRITE SOME PATTERN REGISTERS WHILE "PENDING" */

                GU2_WAIT_PENDING;
                WRITE_GP32(MGP_DST_OFFSET, dstoffset | patxorigin);
                lines = (height + 7 - pass) >> 3;
                if (!lines)
                    break;
                size = (((unsigned long)width) << 16) | lines;
                WRITE_GP32(MGP_WID_HEIGHT, size);
                WRITE_GP32(MGP_STRIDE, gu2_dst_pitch << 3);
                WRITE_GP32(MGP_PAT_COLOR_1, pattern[patoffset + 4]);
                WRITE_GP32(MGP_PAT_COLOR_0, pattern[patoffset + 5]);
                WRITE_GP32(MGP_PAT_DATA_1, pattern[patoffset + 6]);
                WRITE_GP32(MGP_PAT_DATA_0, pattern[patoffset + 7]);

                /* NEED TO WAIT UNTIL IDLE FOR COLORS 2 THROUGH 5 */
                /* Those registers are not pipelined. */

                GU2_WAIT_BUSY;
                WRITE_GP32(MGP_PAT_COLOR_5, pattern[patoffset]);
                WRITE_GP32(MGP_PAT_COLOR_4, pattern[patoffset + 1]);
                WRITE_GP32(MGP_PAT_COLOR_3, pattern[patoffset + 2]);
                WRITE_GP32(MGP_PAT_COLOR_2, pattern[patoffset + 3]);
                WRITE_GP16(MGP_BLT_MODE, gu2_blt_mode | gu2_bm_throttle);
                gu2_bm_throttle = 0;
                gu2_vm_throttle = 0;

                /* ADJUST FOR NEXT PASS */

                dstoffset += gu2_dst_pitch;
                patoffset = (patoffset + 8) & 0x38;
            }
            break;
        }
    }

    else {
        WRITE_GP32(MGP_STRIDE, gu2_dst_pitch);

        switch (gu2_xshift) {
        case 0:                       /* 8 BPP - 4 LINES PER PASS */

            patoffset = (gu2_pattern_origin >> 28) & 0x0E;
            while (height) {
                lines = height > 4 ? 4 : height;

                /* CAN WRITE SOME REGISTERS WHILE PENDING */

                WRITE_GP32(MGP_DST_OFFSET, dstoffset | patxorigin);
                WRITE_GP32(MGP_WID_HEIGHT,
                    (((unsigned long)width) << 16) | lines);
                WRITE_GP32(MGP_PAT_DATA_1, BYTE_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_DATA_0,
                    BYTE_SWIZZLE(pattern[patoffset + 1]));
                patoffset = (patoffset + 2) & 0x0E;
                WRITE_GP32(MGP_PAT_COLOR_1, BYTE_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_0,
                    BYTE_SWIZZLE(pattern[patoffset + 1]));
                patoffset = (patoffset + 2) & 0x0E;

                /* NEED TO WAIT UNTIL IDLE FOR COLORS 2 THROUGH 5 */
                /* Those registers are not pipelined.             */

                GU2_WAIT_BUSY;
                WRITE_GP32(MGP_PAT_COLOR_3, BYTE_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_2,
                    BYTE_SWIZZLE(pattern[patoffset + 1]));
                patoffset = (patoffset + 2) & 0x0E;
                WRITE_GP32(MGP_PAT_COLOR_5, BYTE_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_4,
                    BYTE_SWIZZLE(pattern[patoffset + 1]));
                patoffset = (patoffset + 2) & 0x0E;
                WRITE_GP16(MGP_BLT_MODE, gu2_blt_mode | gu2_bm_throttle);

                /* ADJUST FOR NEXT PASS */

                dstoffset += gu2_dst_pitch << 2;
                height -= (unsigned short)lines;
            }
            break;

        case 1:                       /* 12, 15 AND 16 BPP - 2 LINES PER PASS */

            patoffset = (gu2_pattern_origin >> 27) & 0x1C;
            while (height) {
                lines = height > 2 ? 2 : height;

                /* CAN WRITE SOME REGISTERS WHILE PENDING */

                WRITE_GP32(MGP_DST_OFFSET, dstoffset | patxorigin);
                WRITE_GP32(MGP_WID_HEIGHT,
                    (((unsigned long)width) << 16) | lines);
                WRITE_GP32(MGP_PAT_COLOR_1, WORD_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_0,
                    WORD_SWIZZLE(pattern[patoffset + 1]));
                WRITE_GP32(MGP_PAT_DATA_1,
                    WORD_SWIZZLE(pattern[patoffset + 2]));
                WRITE_GP32(MGP_PAT_DATA_0,
                    WORD_SWIZZLE(pattern[patoffset + 3]));
                patoffset = (patoffset + 4) & 0x1C;

                /* NEED TO WAIT UNTIL IDLE FOR COLORS 2 THROUGH 5 */
                /* Those registers are not pipelined.             */

                GU2_WAIT_BUSY;
                WRITE_GP32(MGP_PAT_COLOR_5, WORD_SWIZZLE(pattern[patoffset]));
                WRITE_GP32(MGP_PAT_COLOR_4,
                    WORD_SWIZZLE(pattern[patoffset + 1]));
                WRITE_GP32(MGP_PAT_COLOR_3,
                    WORD_SWIZZLE(pattern[patoffset + 2]));
                WRITE_GP32(MGP_PAT_COLOR_2,
                    WORD_SWIZZLE(pattern[patoffset + 3]));
                patoffset = (patoffset + 4) & 0x1C;
                WRITE_GP16(MGP_BLT_MODE, gu2_blt_mode | gu2_bm_throttle);

                /* ADJUST FOR NEXT PASS */

                dstoffset += gu2_dst_pitch << 1;
                height -= (unsigned short)lines;
            }
            break;

        case 2:                       /* 32 BPP - 1 LINE PER PASS */

            patoffset = (gu2_pattern_origin >> 26) & 0x38;
            while (height) {
                /* CAN WRITE SOME REGISTERS WHILE PENDING */

                WRITE_GP32(MGP_DST_OFFSET, dstoffset | patxorigin);
                WRITE_GP32(MGP_WID_HEIGHT,
                    (((unsigned long)width) << 16) | 1l);
                WRITE_GP32(MGP_PAT_COLOR_1, pattern[patoffset + 4]);
                WRITE_GP32(MGP_PAT_COLOR_0, pattern[patoffset + 5]);
                WRITE_GP32(MGP_PAT_DATA_1, pattern[patoffset + 6]);
                WRITE_GP32(MGP_PAT_DATA_0, pattern[patoffset + 7]);

                /* NEED TO WAIT UNTIL IDLE FOR COLORS 2 THROUGH 5 */
                /* Those registers are not pipelined.             */

                GU2_WAIT_BUSY;
                WRITE_GP32(MGP_PAT_COLOR_5, pattern[patoffset]);
                WRITE_GP32(MGP_PAT_COLOR_4, pattern[patoffset + 1]);
                WRITE_GP32(MGP_PAT_COLOR_3, pattern[patoffset + 2]);
                WRITE_GP32(MGP_PAT_COLOR_2, pattern[patoffset + 3]);
                patoffset = (patoffset + 8) & 0x38;
                WRITE_GP16(MGP_BLT_MODE, gu2_blt_mode | gu2_bm_throttle);

                /* ADJUST FOR NEXT PASS */

                dstoffset += gu2_dst_pitch;
                height--;
            }
            break;
        }

    }

}

/*---------------------------------------------------------------------------
 * GFX2_SCREEN_TO_SCREEN_BLT
 *
 * This routine is similar to the gfx_screen_to_screen_blt routine but
 * allows the use of arbitrary source and destination strides and alpha
 * blending.  It also allows the use of an arbitrary ROP with transparency.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_screen_to_screen_blt(unsigned long srcoffset, unsigned long dstoffset,
    unsigned short width, unsigned short height, int flags)
#else
void
gfx2_screen_to_screen_blt(unsigned long srcoffset, unsigned long dstoffset,
    unsigned short width, unsigned short height, int flags)
#endif
{
    unsigned long size, xbytes;
    unsigned short blt_mode;

    size = (((unsigned long)width) << 16) | height;

    /* USE ALPHA SETTINGS, IF REQUESTED */

    if (gu2_alpha_active)
        blt_mode = gu2_alpha_blt_mode | MGP_BM_SRC_FB;

    else
        blt_mode = (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK) | MGP_BM_SRC_FB;

    /* CALCULATE THE DIRECTION OF THE BLT */
    /* Using offsets, so flags from the calling routine are needed. */

    if (flags & 1) {
        xbytes = (width - 1) << gu2_xshift;
        srcoffset += xbytes;
        dstoffset += xbytes;
        blt_mode |= MGP_BM_NEG_XDIR;
    }
    if (flags & 2) {
        srcoffset += (height - 1) * gu2_src_pitch;
        dstoffset += (height - 1) * gu2_dst_pitch;
        blt_mode |= MGP_BM_NEG_YDIR;
    }

    /* TURN INTO BYTE ADDRESS IF NEGATIVE X DIRECTION */
    /* This is a quirk of the hardware. */

    if (blt_mode & MGP_BM_NEG_XDIR) {
        srcoffset += (1 << gu2_xshift) - 1;
        dstoffset += (1 << gu2_xshift) - 1;
    }

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first). */

    GU2_WAIT_PENDING;

    if (gu2_alpha_active) {
        WRITE_GP32(MGP_RASTER_MODE, gu2_alpha32);
    } else {
        WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    }

    WRITE_GP32(MGP_SRC_OFFSET, srcoffset);
    WRITE_GP32(MGP_DST_OFFSET, dstoffset | gu2_pattern_origin);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_dst_pitch | (gu2_src_pitch << 16));
    WRITE_GP16(MGP_BLT_MODE, blt_mode | gu2_bm_throttle);
    gu2_bm_throttle = 0;
    gu2_vm_throttle = 0;
}

/*---------------------------------------------------------------------------
 * GFX2_MONO_EXPAND_BLT
 *
 * This routine is similar to the gfx2_screen_to_screen_blt routine but
 * expands monochrome data stored in graphics memory.  
 * WARNING: This routine assumes that the regions in graphics memory 
 * will not overlap, and therefore does not check the BLT direction.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_mono_expand_blt(unsigned long srcbase, unsigned short srcx,
    unsigned short srcy, unsigned long dstoffset, unsigned short width,
    unsigned short height, int byte_packed)
#else
void
gfx2_mono_expand_blt(unsigned long srcbase, unsigned short srcx,
    unsigned short srcy, unsigned long dstoffset, unsigned short width,
    unsigned short height, int byte_packed)
#endif
{
    unsigned long size, srcoffset;
    unsigned short blt_mode;

    size = (((unsigned long)width) << 16) | height;

    /* CALCULATE SOURCE OFFSET */

    srcoffset = srcbase + (unsigned long)srcy *gu2_src_pitch;

    srcoffset += srcx >> 3;
    srcoffset |= ((unsigned long)srcx & 7) << 26;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first). */

    GU2_WAIT_PENDING;

    if (gu2_alpha_active) {
        blt_mode = gu2_alpha_blt_mode;

        WRITE_GP32(MGP_RASTER_MODE, gu2_alpha32);
    } else {
        blt_mode = (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK);

        WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    }

    if (byte_packed)
        blt_mode |= MGP_BM_SRC_FB | MGP_BM_SRC_BP_MONO | gu2_bm_throttle;
    else
        blt_mode |= MGP_BM_SRC_FB | MGP_BM_SRC_MONO | gu2_bm_throttle;

    WRITE_GP32(MGP_SRC_OFFSET, srcoffset);
    WRITE_GP32(MGP_DST_OFFSET, dstoffset | gu2_pattern_origin);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_dst_pitch | (gu2_src_pitch << 16));
    WRITE_GP16(MGP_BLT_MODE, blt_mode);
    gu2_bm_throttle = 0;
    gu2_vm_throttle = 0;
}

/*---------------------------------------------------------------------------
 * GFX2_COLOR_BITMAP_TO_SCREEN_BLT	
 *
 * This routine is similar to the gfx_color_bitmap_to_screen_blt routine 
 * but allows the use of an arbitrary destination stride and alpha blending.
 * It also allows the use of an arbitrary ROP with transparency.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_color_bitmap_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned long dstoffset, unsigned short width,
    unsigned short height, unsigned char *data, short pitch)
#else
void
gfx2_color_bitmap_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned long dstoffset, unsigned short width,
    unsigned short height, unsigned char *data, short pitch)
#endif
{
    unsigned long size, bytes;
    unsigned long offset, temp_offset;
    unsigned long srcoffset, dword_bytes, bytes_extra;
    unsigned short blt_mode;

    size = (((unsigned long)width) << 16) | 1;

    /* CALCULATE STARTING OFFSETS */

    offset = (unsigned long)srcy *pitch + ((unsigned long)srcx << gu2_xshift);

    dstoffset |= gu2_pattern_origin;

    bytes = width << gu2_xshift;
    dword_bytes = bytes & ~0x3L;
    bytes_extra = bytes & 0x3L;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first).   */
    /* The source offset is always 0 since we allow misaligned dword reads. */
    /* We must wait for BLT busy because the GP may be executing a screen   */
    /* to screen BLT from the scratchpad area.                              */

    GU2_WAIT_BUSY;

    if (gu2_alpha_active) {
        blt_mode = gu2_alpha_blt_mode;

        WRITE_GP32(MGP_RASTER_MODE, gu2_alpha32);
    } else {
        blt_mode = (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK);

        WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    }
    blt_mode |= MGP_BM_SRC_FB | gu2_bm_throttle;
    gu2_bm_throttle = 0;
    gu2_vm_throttle = 0;

    WRITE_GP32(MGP_WID_HEIGHT, size);

    /* WRITE DATA ONE LINE AT A TIME */
    /* For speed reasons, data is written to an offscreen scratch area and 
     * then BLTed using a screen to screen BLT. This is similar to the GX1 BLT
     * buffers, but slightly more efficient in that we can queue up data while
     * the GP is rendering a line.
     * */

    while (height--) {
        temp_offset = offset;
        srcoffset = gfx_gx2_scratch_base;
        if (gu2_current_line)
            srcoffset += 8192;

        GU2_WAIT_PENDING;
        WRITE_GP32(MGP_SRC_OFFSET, srcoffset);
        WRITE_GP32(MGP_DST_OFFSET, dstoffset);
        dstoffset += gu2_dst_pitch;
        dstoffset += 0x20000000;

        WRITE_FRAME_BUFFER_STRING32(srcoffset, dword_bytes, data,
            temp_offset);
        if (bytes_extra) {
            temp_offset += dword_bytes;
            srcoffset += dword_bytes;
            WRITE_FRAME_BUFFER_STRING8(srcoffset, bytes_extra, data,
                temp_offset);
        }
        WRITE_GP16(MGP_BLT_MODE, blt_mode);
        offset += pitch;
        gu2_current_line = 1 - gu2_current_line;
    }
}

/*---------------------------------------------------------------------------
 * GFX2_TEXT_BLT	
 *
 * This routine is similar to the gfx2_mono_bitmap_to_screen_blt routine 
 * but assumes that source data is byte-packed.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_text_blt(unsigned long dstoffset, unsigned short width,
    unsigned short height, unsigned char *data)
#else
void
gfx2_text_blt(unsigned long dstoffset, unsigned short width,
    unsigned short height, unsigned char *data)
#endif
{
    unsigned long size, bytes;
    unsigned long temp1 = 0, temp2 = 0, temp_offset = 0;
    unsigned long i, j = 0, fifo_lines, dwords_extra, bytes_extra;
    unsigned long shift;
    unsigned short blt_mode;

    size = (((unsigned long)width) << 16) | height;

    /* CALCULATE STARTING OFFSETS */

    bytes = ((width + 7) >> 3) * height;
    fifo_lines = bytes >> 5;
    dwords_extra = (bytes & 0x0000001Cl) >> 2;
    bytes_extra = bytes & 0x00000003l;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */

    GU2_WAIT_PENDING;

    if (gu2_alpha_active) {
        blt_mode = gu2_alpha_blt_mode;

        WRITE_GP32(MGP_RASTER_MODE, gu2_alpha32);
    } else {
        blt_mode = (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK);

        WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    }

    WRITE_GP32(MGP_SRC_OFFSET, 0);
    WRITE_GP32(MGP_DST_OFFSET, dstoffset | gu2_pattern_origin);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_dst_pitch);
    WRITE_GP16(MGP_BLT_MODE, blt_mode | MGP_BM_SRC_HOST |
        MGP_BM_SRC_BP_MONO | gu2_bm_throttle);
    gu2_bm_throttle = 0;
    gu2_vm_throttle = 0;

    /* WAIT FOR BLT TO BE LATCHED */

    GU2_WAIT_PENDING;

    /* WRITE ALL FULL FIFO LINES */

    for (i = 0; i < fifo_lines; i++) {
        GU2_WAIT_HALF_EMPTY;
        WRITE_GPREG_STRING32(MGP_HST_SOURCE, 8, j, data, temp_offset, temp1);
        temp_offset += 32;
    }

    /* WRITE ALL FULL DWORDS */

    if (dwords_extra || bytes_extra) {
        GU2_WAIT_HALF_EMPTY;
        if (dwords_extra) {
            WRITE_GPREG_STRING32(MGP_HST_SOURCE, dwords_extra, i, data,
                temp_offset, temp1);
            temp_offset += (dwords_extra << 2);
        }
        if (bytes_extra) {
            shift = 0;
            WRITE_GPREG_STRING8(MGP_HST_SOURCE, bytes_extra, shift, i, data,
                temp_offset, temp1, temp2);
        }
    }
}

/*---------------------------------------------------------------------------
 * GFX2_MONO_BITMAP_TO_SCREEN_BLT	
 *
 * This routine is similar to the gfx_mono_bitmap_to_screen_blt routine 
 * but allows the use of an arbitrary destination stride and alpha blending.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_mono_bitmap_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned long dstoffset, unsigned short width,
    unsigned short height, unsigned char *data, short pitch)
#else
void
gfx2_mono_bitmap_to_screen_blt(unsigned short srcx, unsigned short srcy,
    unsigned long dstoffset, unsigned short width,
    unsigned short height, unsigned char *data, short pitch)
#endif
{
    unsigned long size, bytes;
    unsigned long offset, temp_offset, temp1 = 0, temp2 = 0;
    unsigned long i, j = 0, fifo_lines, dwords_extra, bytes_extra;
    unsigned long shift = 0;
    unsigned short blt_mode;

    size = (((unsigned long)width) << 16) | height;

    /* CALCULATE STARTING OFFSETS */

    offset = (unsigned long)srcy *pitch + ((unsigned long)srcx >> 3);

    bytes = ((srcx & 7) + width + 7) >> 3;
    fifo_lines = bytes >> 5;
    dwords_extra = (bytes & 0x0000001Cl) >> 2;
    bytes_extra = bytes & 0x00000003l;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */

    GU2_WAIT_PENDING;

    if (gu2_alpha_active) {
        blt_mode = gu2_alpha_blt_mode;

        WRITE_GP32(MGP_RASTER_MODE, gu2_alpha32);
    } else {
        blt_mode = (gu2_blt_mode & ~MGP_BM_SRC_TYPE_MASK);

        WRITE_GP32(MGP_RASTER_MODE, gu2_rop32 | GFXsourceFlags);
    }

    WRITE_GP32(MGP_SRC_OFFSET, ((unsigned long)srcx & 7) << 26);
    WRITE_GP32(MGP_DST_OFFSET, dstoffset | gu2_pattern_origin);
    WRITE_GP32(MGP_WID_HEIGHT, size);
    WRITE_GP32(MGP_STRIDE, gu2_dst_pitch);
    WRITE_GP16(MGP_BLT_MODE, blt_mode | MGP_BM_SRC_HOST |
        MGP_BM_SRC_MONO | gu2_bm_throttle);
    gu2_bm_throttle = 0;
    gu2_vm_throttle = 0;

    /* WAIT FOR BLT TO BE LATCHED */

    GU2_WAIT_PENDING;

    /* WRITE ALL OF THE DATA TO THE HOST SOURCE REGISTER */

    while (height--) {
        temp_offset = offset;

        /* WRITE ALL FULL FIFO LINES */

        for (i = 0; i < fifo_lines; i++) {
            GU2_WAIT_HALF_EMPTY;
            WRITE_GPREG_STRING32(MGP_HST_SOURCE, 8, j, data, temp_offset,
                temp1);
            temp_offset += 32;
        }

        /* WRITE ALL FULL DWORDS */

        GU2_WAIT_HALF_EMPTY;
        if (dwords_extra)
            WRITE_GPREG_STRING32(MGP_HST_SOURCE, dwords_extra, i, data,
                temp_offset, temp1);
        temp_offset += (dwords_extra << 2);

        /* WRITE REMAINING BYTES */

        shift = 0;
        if (bytes_extra)
            WRITE_GPREG_STRING8(MGP_HST_SOURCE, bytes_extra, shift, i, data,
                temp_offset, temp1, temp2);

        offset += pitch;
    }
}

/*---------------------------------------------------------------------------
 * GFX2_BRESENHAM_LINE
 *
 * This routine is similar to the gfx_bresenam_line routine but allows 
 * the use of an arbitrary destination stride.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_bresenham_line(unsigned long dstoffset,
    unsigned short length, unsigned short initerr,
    unsigned short axialerr, unsigned short diagerr, unsigned short flags)
#else
void
gfx2_bresenham_line(unsigned long dstoffset,
    unsigned short length, unsigned short initerr,
    unsigned short axialerr, unsigned short diagerr, unsigned short flags)
#endif
{
    unsigned long vector_mode = gu2_vector_mode | flags;
    unsigned long data1 = (((unsigned long)axialerr) << 16) | diagerr;
    unsigned long data2 = (((unsigned long)length) << 16) | initerr;

    /* CHECK NULL LENGTH */

    if (!length)
        return;

    /* POLL UNTIL ABLE TO WRITE TO THE REGISTERS */
    /* Put off poll for as long as possible (do most calculations first). */

    GU2_WAIT_PENDING;

    if (gu2_alpha_active) {
        vector_mode = gu2_alpha_vec_mode | flags;

        WRITE_GP32(MGP_RASTER_MODE, gu2_alpha32);
    } else
        WRITE_GP32(MGP_RASTER_MODE, gu2_rop32);

    WRITE_GP32(MGP_DST_OFFSET, dstoffset | gu2_pattern_origin);
    WRITE_GP32(MGP_VEC_ERR, data1);
    WRITE_GP32(MGP_VEC_LEN, data2);
    WRITE_GP32(MGP_STRIDE, gu2_dst_pitch);
    WRITE_GP32(MGP_VECTOR_MODE, vector_mode | gu2_vm_throttle);
    gu2_bm_throttle = 0;
    gu2_vm_throttle = 0;
}

/*---------------------------------------------------------------------------
 * GFX2_SYNC_TO_VBLANK
 *
 * This routine sets a flag to synchronize the next rendering routine to 
 * VBLANK.  The flag is cleared by the rendering routine.
 *---------------------------------------------------------------------------
 */
#if GFX_2DACCEL_DYNAMIC
void
gu22_sync_to_vblank(void)
#else
void
gfx2_sync_to_vblank(void)
#endif
{
    /* SET FLAGS TO THROTTLE NEXT RENDERING ROUTINE */

    gu2_bm_throttle = MGP_BM_THROTTLE;
    gu2_vm_throttle = MGP_VM_THROTTLE;
}

/* END OF FILE */