~ubuntu-branches/ubuntu/raring/vice/raring

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
/*
 * vicii-mem.c - Memory interface for the MOS6569 (VIC-II) emulation.
 *
 * Written by
 *  Ettore Perazzoli <ettore@comm2000.it>
 *  Andreas Boose <viceteam@t-online.de>
 *
 * DTV sections written by
 *  Hannu Nuotio <hannu.nuotio@tut.fi>
 *  Daniel Kahlin <daniel@kahlin.net>
 *
 * This file is part of VICE, the Versatile Commodore Emulator.
 * See README for copyright notice.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA.
 *
 */

#include "vice.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "alarm.h"
#include "c64cia.h"
#include "debug.h"
#include "maincpu.h"
#include "raster-sprite-status.h"
#include "raster-sprite.h"
#include "types.h"
#include "vicii-badline.h"
#include "vicii-fetch.h"
#include "vicii-irq.h"
#include "vicii-resources.h"
#include "vicii-sprites.h"
#include "vicii-mem.h"
#include "vicii.h"
#include "viciitypes.h"


/* Unused bits in VIC-II registers: these are always 1 when read.  */
static int unused_bits_in_registers[0x50] =
{
    0x00 /* $D000 */ , 0x00 /* $D001 */ , 0x00 /* $D002 */ , 0x00 /* $D003 */ ,
    0x00 /* $D004 */ , 0x00 /* $D005 */ , 0x00 /* $D006 */ , 0x00 /* $D007 */ ,
    0x00 /* $D008 */ , 0x00 /* $D009 */ , 0x00 /* $D00A */ , 0x00 /* $D00B */ ,
    0x00 /* $D00C */ , 0x00 /* $D00D */ , 0x00 /* $D00E */ , 0x00 /* $D00F */ ,
    0x00 /* $D010 */ , 0x00 /* $D011 */ , 0x00 /* $D012 */ , 0x00 /* $D013 */ ,
    0x00 /* $D014 */ , 0x00 /* $D015 */ , 0x00 /* $D016 */ , 0xc0 /* $D017 */ ,
    0x01 /* $D018 */ , 0x70 /* $D019 */ , 0xf0 /* $D01A */ , 0x00 /* $D01B */ ,
    0x00 /* $D01C */ , 0x00 /* $D01D */ , 0x00 /* $D01E */ , 0x00 /* $D01F */ ,
    0x00 /* $D020 */ , 0x00 /* $D021 */ , 0x00 /* $D022 */ , 0x00 /* $D023 */ ,
    0x00 /* $D024 */ , 0xf0 /* $D025 */ , 0xf0 /* $D026 */ , 0xf0 /* $D027 */ ,
    0xf0 /* $D028 */ , 0xf0 /* $D029 */ , 0xf0 /* $D02A */ , 0xf0 /* $D02B */ ,
    0xf0 /* $D02C */ , 0xf0 /* $D02D */ , 0xf0 /* $D02E */ , 0xff /* $D02F */ ,
    0xff /* $D030 */ , 0xff /* $D031 */ , 0xff /* $D032 */ , 0xff /* $D033 */ ,
    0xff /* $D034 */ , 0xff /* $D035 */ , 0x00 /* $D036 */ , 0x00 /* $D037 */ ,
    0x00 /* $D038 */ , 0xf0 /* $D039 */ , 0x00 /* $D03A */ , 0x00 /* $D03B */ ,
    0x80 /* $D03C */ , 0x00 /* $D03D */ , 0xff /* $D03E */ , 0xfc /* $D03F */ ,
    0xf8 /* $D040 */ , 0x00 /* $D041 */ , 0x00 /* $D042 */ , 0x00 /* $D043 */ ,
    0x80 /* $D044 */ , 0xc0 /* $D045 */ , 0x00 /* $D046 */ , 0x00 /* $D047 */ ,
    0xf0 /* $D048 */ , 0x00 /* $D049 */ , 0x00 /* $D04A */ , 0xc0 /* $D04B */ ,
    0x00 /* $D04C */ , 0xc0 /* $D04D */ , 0x00 /* $D04E */ , 0xf0 /* $D04F */
};


/* Store a value in the video bank (it is assumed to be in RAM).  */
inline static void REGPARM2 vicii_local_store_vbank(WORD addr, BYTE value)
{
    unsigned int f;

    if (vicii.viciie != 0)
        vicii_delay_clk();

    do {
        CLOCK mclk;

        /* WARNING: Assumes `maincpu_rmw_flag' is 0 or 1.  */
        mclk = maincpu_clk - maincpu_rmw_flag - 1;
        f = 0;

        if (mclk >= vicii.fetch_clk) {
            /* If the fetch starts here, the sprite fetch routine should
               get the new value, not the old one.  */
            if (mclk == vicii.fetch_clk)
                vicii.ram_base_phi2[addr] = value;

            /* The sprite DMA check can be followed by a real fetch.
               Save the location to execute the store if a real
               fetch actually happens.  */
            if (vicii.fetch_idx == VICII_CHECK_SPRITE_DMA) {
                vicii.store_clk = mclk;
                vicii.store_value = value;
                vicii.store_addr = addr;
            }

            vicii_fetch_alarm_handler(maincpu_clk - vicii.fetch_clk, NULL);
            f = 1;
            /* WARNING: Assumes `maincpu_rmw_flag' is 0 or 1.  */
            mclk = maincpu_clk - maincpu_rmw_flag - 1;
            vicii.store_clk = CLOCK_MAX;
        }

        if (mclk >= vicii.draw_clk) {
            vicii_raster_draw_alarm_handler(0, NULL);
            f = 1;
        }
        if (vicii.viciie != 0)
            vicii_delay_clk();
    } while (f);

    vicii.ram_base_phi2[addr] = value;
}

/* Encapsulate inlined function for other modules */
void REGPARM2 vicii_mem_vbank_store(WORD addr, BYTE value)
{
    vicii_local_store_vbank(addr, value);
}

/* As `store_vbank()', but for the $3900...$39FF address range.  */
void REGPARM2 vicii_mem_vbank_39xx_store(WORD addr, BYTE value)
{
    vicii_local_store_vbank(addr, value);

    if (vicii.idle_data_location == IDLE_39FF && (addr & 0x3fff) == 0x39ff)
        raster_changes_foreground_add_int
            (&vicii.raster,
            VICII_RASTER_CHAR(VICII_RASTER_CYCLE(maincpu_clk)),
            &vicii.idle_data,
            value);
}

/* As `store_vbank()', but for the $3F00...$3FFF address range.  */
void REGPARM2 vicii_mem_vbank_3fxx_store(WORD addr, BYTE value)
{
    vicii_local_store_vbank(addr, value);

    if ((addr & 0x3fff) == 0x3fff) {
        if (vicii.idle_data_location == IDLE_3FFF)
            raster_changes_foreground_add_int
                (&vicii.raster,
                VICII_RASTER_CHAR(VICII_RASTER_CYCLE(maincpu_clk)),
                &vicii.idle_data,
                value);

        if (vicii.raster.sprite_status->visible_msk != 0
            || vicii.raster.sprite_status->dma_msk != 0) {
            vicii.idle_3fff[vicii.num_idle_3fff].cycle = maincpu_clk;
            vicii.idle_3fff[vicii.num_idle_3fff].value = value;
            vicii.num_idle_3fff++;
        }
    }
}


inline static void store_sprite_x_position_lsb(const WORD addr, BYTE value)
{
    int n;
    int new_x;

    if (value == vicii.regs[addr])
        return;

    vicii.regs[addr] = value;

    n = addr >> 1;                /* Number of changed sprite.  */

    VICII_DEBUG_REGISTER(("Sprite #%d X position LSB: $%02X", n, value));

    new_x = (value | (vicii.regs[0x10] & (1 << n) ? 0x100 : 0));
    vicii_sprites_set_x_position(n, new_x,
        VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk)));
}

inline static void store_sprite_y_position(const WORD addr, BYTE value)
{
    int cycle;

    VICII_DEBUG_REGISTER(("Sprite #%d Y position: $%02X", addr >> 1, value));

    if (vicii.regs[addr] == value)
        return;

    cycle = VICII_RASTER_CYCLE(maincpu_clk);

    if (cycle == vicii.sprite_fetch_cycle + 1
        && value == (vicii.raster.current_line & 0xff)) {
        vicii.fetch_idx = VICII_CHECK_SPRITE_DMA;
        vicii.fetch_clk = (VICII_LINE_START_CLK(maincpu_clk)
                          + vicii.sprite_fetch_cycle + 1);
        alarm_set(vicii.raster_fetch_alarm, vicii.fetch_clk);
    }

    vicii.raster.sprite_status->sprites[addr >> 1].y = value;
    vicii.regs[addr] = value;
}

static inline void store_sprite_x_position_msb(const WORD addr, BYTE value)
{
    int i;
    BYTE b;
    int raster_x;

    VICII_DEBUG_REGISTER(("Sprite X position MSBs: $%02X", value));

    if (value == vicii.regs[addr])
        return;

    raster_x = VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk));

    vicii.regs[addr] = value;

    /* Recalculate the sprite X coordinates.  */
    for (i = 0, b = 0x01; i < 8; b <<= 1, i++) {
        int new_x;

        new_x = (vicii.regs[2 * i] | (value & b ? 0x100 : 0));
        vicii_sprites_set_x_position(i, new_x, raster_x);
    }
}

inline static void check_lower_upper_border(const BYTE value,
                                            unsigned int line, int cycle)
{
    if ((value ^ vicii.regs[0x11]) & 8) {
        if (value & 0x8) {
            /* 24 -> 25 row mode switch.  */

            vicii.raster.display_ystart = vicii.row_25_start_line;
            vicii.raster.display_ystop = vicii.row_25_stop_line;

            if (line == vicii.row_24_stop_line && cycle > 0) {
                /* If on the first line of the 24-line border, we
                   still see the 25-line (lowmost) border because the
                   border flip flop has already been turned on.  */
                vicii.raster.blank_enabled = 1;
            } else {
                if (!vicii.raster.blank && line == vicii.row_24_start_line
                    && cycle > 0) {
                    /* A 24 -> 25 switch somewhere on the first line of
                       the 24-row mode is enough to disable screen
                       blanking.  */
                    vicii.raster.blank_enabled = 0;
                }

                if (line == vicii.raster.display_ystart && cycle > 0
                    && vicii.raster.blank == 0) {
                    /* A 24 -> 25 switch somewhere on the first line of
                       the 25-row mode is enough to disable screen
                       blanking even if blank bit is switched on in the
                       same cycle (and set later in d011_store) */
                    vicii.raster.blank_enabled = 0;
                }
            }
            VICII_DEBUG_REGISTER(("25 line mode enabled"));
        } else {
            /* 25 -> 24 row mode switch.  */

            vicii.raster.display_ystart = vicii.row_24_start_line;
            vicii.raster.display_ystop = vicii.row_24_stop_line;

            /* If on the last line of the 25-line border, we still see the
               24-line (upmost) border because the border flip flop has
               already been turned off.  */
            if (!vicii.raster.blank && line == vicii.row_25_start_line
                && cycle > 0) {
                vicii.raster.blank_enabled = 0;
            } else {
                if (line == vicii.row_25_stop_line && cycle > 0)
                    vicii.raster.blank_enabled = 1;
            }

            VICII_DEBUG_REGISTER(("24 line mode enabled"));
        }
    }

    /* Check if border is already disabled even if DEN will be cleared */
    if (line == vicii.raster.display_ystart && cycle > 0 && !vicii.raster.blank)
    {
        vicii.raster.blank_off = 1;
    }
}

inline static void d011_store(BYTE value)
{
    int cycle, old_allow_bad_lines;
    unsigned int line;

    cycle = VICII_RASTER_CYCLE(maincpu_clk);
    line = VICII_RASTER_Y(maincpu_clk);

    VICII_DEBUG_REGISTER(("Control register: $%02X", value));
    VICII_DEBUG_REGISTER(("$D011 tricks at cycle %d, line $%04X, "
                          "value $%02X", cycle, line, value));

    vicii_irq_check_state(value, 1);

    /* This is the funniest part... handle bad line tricks.  */
    old_allow_bad_lines = vicii.allow_bad_lines;

    if (line == vicii.first_dma_line && cycle == 0)
        vicii.allow_bad_lines = (value & 0x10) ? 1 : 0;

    if (VICII_RASTER_Y(maincpu_clk - 1) == vicii.first_dma_line
        && (value & 0x10) != 0)
        vicii.allow_bad_lines = 1;

    if ((vicii.raster.ysmooth != (value & 7)
        || vicii.allow_bad_lines != old_allow_bad_lines)
        && line >= vicii.first_dma_line
        && line <= vicii.last_dma_line)
        vicii_badline_check_state(value, cycle, line, old_allow_bad_lines);

    vicii.raster.ysmooth = value & 0x7;

    /* Check for 24 <-> 25 line mode switch.  */
    check_lower_upper_border(value, line, cycle);

    vicii.raster.blank = !(value & 0x10); /* `DEN' bit.  */

    vicii.regs[0x11] = value;

    /* FIXME: save time.  */
    vicii_update_video_mode(cycle);
}

inline static void d012_store(BYTE value)
{
    VICII_DEBUG_REGISTER(("Raster compare register: $%02X", value));

    if (value == vicii.regs[0x12])
        return;

    vicii.regs[0x12] = value;

    vicii_irq_check_state(value, 0);

    VICII_DEBUG_REGISTER(("Raster interrupt line set to $%04X",
                         vicii.raster_irq_line));
}

inline static void d015_store(const BYTE value)
{
    int cycle;

    VICII_DEBUG_REGISTER(("Sprite Enable register: $%02X", value));

    cycle = VICII_RASTER_CYCLE(maincpu_clk);

    /* On the real C64, sprite DMA is checked two times: first at
       `VICII_SPRITE_FETCH_CYCLE', and then at `VICII_SPRITE_FETCH_CYCLE +
       1'.  In the average case, one DMA check is OK and there is no need to
       emulate both, but we have to kludge things a bit in case sprites are
       activated at cycle `VICII_SPRITE_FETCH_CYCLE + 1'.  */
    if (cycle == vicii.sprite_fetch_cycle + 1
        && ((value ^ vicii.regs[0x15]) & value) != 0) {
        vicii.fetch_idx = VICII_CHECK_SPRITE_DMA;
        vicii.fetch_clk = (VICII_LINE_START_CLK(maincpu_clk)
                          + vicii.sprite_fetch_cycle + 1);
        alarm_set(vicii.raster_fetch_alarm, vicii.fetch_clk);
    }

    /* Sprites are turned on: force a DMA check.  */
    if (vicii.raster.sprite_status->visible_msk == 0
        && vicii.raster.sprite_status->dma_msk == 0
        && value != 0) {
        if ((vicii.fetch_idx == VICII_FETCH_MATRIX
             && vicii.fetch_clk > maincpu_clk
             && cycle > VICII_FETCH_CYCLE
             && cycle <= vicii.sprite_fetch_cycle)
            || vicii.raster.current_line < vicii.first_dma_line
            || vicii.raster.current_line >= vicii.last_dma_line) {
            CLOCK new_fetch_clk;

            new_fetch_clk = (VICII_LINE_START_CLK(maincpu_clk)
                            + vicii.sprite_fetch_cycle);
            if (cycle > vicii.sprite_fetch_cycle)
                new_fetch_clk += vicii.cycles_per_line;
            if (new_fetch_clk < vicii.fetch_clk) {
                vicii.fetch_idx = VICII_CHECK_SPRITE_DMA;
                vicii.fetch_clk = new_fetch_clk;
                alarm_set(vicii.raster_fetch_alarm, vicii.fetch_clk);
            }
        }
    }

    vicii.regs[0x15] = vicii.raster.sprite_status->visible_msk = value;
}

inline static void check_lateral_border(const BYTE value, int cycle,
                                        raster_t *raster)
{
    if ((value & 0x8) != (vicii.regs[0x16] & 0x8)) {
        if (value & 0x8) {
            /* 40 column mode.  */
            if (cycle <= 17)
                raster->display_xstart = VICII_40COL_START_PIXEL;
            else
                raster_changes_next_line_add_int(raster,
                                                 &raster->display_xstart,
                                                 VICII_40COL_START_PIXEL);
            if (cycle <= 56)
                raster->display_xstop = VICII_40COL_STOP_PIXEL;
            else
                raster_changes_next_line_add_int(raster,
                                                 &raster->display_xstop,
                                                 VICII_40COL_STOP_PIXEL);
            VICII_DEBUG_REGISTER(("40 column mode enabled"));

            /* If CSEL changes from 0 to 1 at cycle 17, the border is
               not turned off and this line is blank.  */
            if (cycle == 17 && !(vicii.regs[0x16] & 0x8))
                raster->blank_this_line = 1;
        } else {
            /* 38 column mode.  */
            if (cycle <= 17)
                raster->display_xstart = VICII_38COL_START_PIXEL;
            else
                raster_changes_next_line_add_int(raster,
                                                 &raster->display_xstart,
                                                 VICII_38COL_START_PIXEL);
            if (cycle <= 56)
                raster->display_xstop = VICII_38COL_STOP_PIXEL;
            else
                raster_changes_next_line_add_int(raster,
                                                 &raster->display_xstop,
                                                 VICII_38COL_STOP_PIXEL);
            VICII_DEBUG_REGISTER(("38 column mode enabled"));

            /* If CSEL changes from 1 to 0 at cycle 56, the lateral
               border is open.  */
            if (cycle == 56 && (vicii.regs[0x16] & 0x8)
                && (!raster->blank_enabled || raster->open_left_border)) {
                raster->open_right_border = 1;
                switch (vicii.get_background_from_vbuf) {
                  case VICII_HIRES_BITMAP_MODE:
                    raster_changes_background_add_int(
                        &vicii.raster,
                        VICII_RASTER_X(56),
                        &vicii.raster.xsmooth_color,
                        vicii.background_color_source & 0x0f);
                    break;
                  case VICII_EXTENDED_TEXT_MODE:
                    raster_changes_background_add_int(
                        &vicii.raster,
                        VICII_RASTER_X(56),
                        &vicii.raster.xsmooth_color,
                        vicii.regs[0x21 + (vicii.background_color_source
                        >> 6)]);
                    break;
                }
            }
        }
    }
}

inline static void d016_store(const BYTE value)
{
    raster_t *raster;
    int cycle;
    BYTE xsmooth;

    VICII_DEBUG_REGISTER(("Control register: $%02X", value));

    raster = &vicii.raster;
    cycle = VICII_RASTER_CYCLE(maincpu_clk);
    xsmooth = value & 7;

    if (xsmooth != (vicii.regs[0x16] & 7)) {
        if (xsmooth < (vicii.regs[0x16] & 7)) {
            if (cycle < 56)
                raster_changes_foreground_add_int(raster,
                             VICII_RASTER_CHAR(cycle) - 2,
                             &raster->xsmooth_shift_left,
                             (vicii.regs[0x16] & 7) - xsmooth);

        } else {
            raster_changes_background_add_int(raster,
                                              VICII_RASTER_X(cycle),
                                              &raster->xsmooth_shift_right,
                                              xsmooth - (vicii.regs[0x16] & 7));
            raster_changes_sprites_add_int(raster,
                                           VICII_RASTER_X(cycle) + 8 
                                                + (vicii.regs[0x16] & 7),
                                           &raster->sprite_xsmooth_shift_right,
                                           1);
        }
        raster_changes_foreground_add_int(raster,
                                          VICII_RASTER_CHAR(cycle) - 1,
                                          &raster->xsmooth,
                                          xsmooth);
        raster_changes_sprites_add_int(raster,
                                        VICII_RASTER_X(cycle) + 8 + xsmooth,
                                        &raster->sprite_xsmooth,
                                        xsmooth);
    }

    /* Bit 4 (CSEL) selects 38/40 column mode.  */
    check_lateral_border(value, cycle, raster);

    vicii.regs[0x16] = value;

    vicii_update_video_mode(cycle);
}

inline static void d017_store(const BYTE value)
{
    raster_sprite_status_t *sprite_status;
    int cycle;
    int i;
    BYTE b;

    VICII_DEBUG_REGISTER(("Sprite Y Expand register: $%02X", value));

    if (value == vicii.regs[0x17])
        return;

    cycle = VICII_RASTER_CYCLE(maincpu_clk);
    sprite_status = vicii.raster.sprite_status;

    for (i = 0, b = 0x01; i < 8; b <<= 1, i++) {
        raster_sprite_t *sprite;

        sprite = sprite_status->sprites + i;

        sprite->y_expanded = value & b ? 1 : 0;

        if (!sprite->y_expanded && !sprite->exp_flag) {
            /* Sprite crunch!  */
            if (cycle == 15)
                sprite->memptr_inc
                    = vicii_sprites_crunch_table[sprite->memptr];
            else if (cycle < 15 || cycle >= vicii.sprite_fetch_cycle)
                sprite->memptr_inc = 3;
            sprite->exp_flag = 1;
        }

        /* (Enabling sprite Y-expansion never causes side effects.)  */
    }

    vicii.regs[0x17] = value;
}

inline static void d018_store(const BYTE value)
{
    VICII_DEBUG_REGISTER(("Memory register: $%02X", value));

    if (vicii.regs[0x18] == value)
        return;

    vicii.regs[0x18] = value;
    vicii_update_memory_ptrs(VICII_RASTER_CYCLE(maincpu_clk));
}

inline static void d019_store(const BYTE value)
{
    /* Emulates Read-Modify-Write behaviour. */
    if (maincpu_rmw_flag) {
        vicii.irq_status &= ~((vicii.last_read & 0xf) | 0x80);
        if (maincpu_clk - 1 > vicii.raster_irq_clk
            && vicii.raster_irq_line < (unsigned int)vicii.screen_height) {
                if (maincpu_clk - 2 == vicii.raster_irq_clk)
                    vicii_irq_next_frame();
                else
                    vicii_irq_alarm_handler(0, NULL);
        }
    }

    if ((value & 1) && maincpu_clk > vicii.raster_irq_clk
        && vicii.raster_irq_line < (unsigned int)vicii.screen_height) {
            if (maincpu_clk - 1 == vicii.raster_irq_clk)
                vicii_irq_next_frame();
            else
                vicii_irq_alarm_handler(0, NULL);
    }

    vicii.irq_status &= ~((value & 0xf) | 0x80);
    vicii_irq_set_line();

    VICII_DEBUG_REGISTER(("IRQ flag register: $%02X", vicii.irq_status));
}

inline static void d01a_store(const BYTE value)
{
    vicii.regs[0x1a] = value & 0xf;

    vicii_irq_set_line();

/*    VICII_DEBUG_REGISTER(("IRQ mask register: $%02X", vicii.regs[addr])); */
}

inline static void d01b_store(const BYTE value)
{
    int i;
    BYTE b;
    int raster_x;

    VICII_DEBUG_REGISTER(("Sprite priority register: $%02X", value));

    if (value == vicii.regs[0x1b])
        return;

    raster_x = VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk));

    for (i = 0, b = 0x01; i < 8; b <<= 1, i++) {
        raster_sprite_t *sprite;

        sprite = vicii.raster.sprite_status->sprites + i;

        if (sprite->x < raster_x)
            raster_changes_next_line_add_int(&vicii.raster,
                                             &sprite->in_background,
                                             value & b ? 1 : 0);
        else
            sprite->in_background = value & b ? 1 : 0;
    }

    vicii.regs[0x1b] = value;
}

inline static void d01c_store(const BYTE value)
{
    int i;
    BYTE b;
    int raster_x;
    int sprite_x;
    int delayed_load, delayed_shift, delayed_pixel;
    int x_exp;

    VICII_DEBUG_REGISTER(("Sprite Multicolor Enable register: $%02X", value));

    if (value == vicii.regs[0x1c])
        return;

    raster_x = VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk));

    for (i = 0, b = 0x01; i < 8; b <<= 1, i++) {
        raster_sprite_t *sprite;

        sprite = vicii.raster.sprite_status->sprites + i;

        if ((vicii.regs[0x1c] & b) != (value & b)) {
            /* Test for MC bug condition */
            sprite_x = (vicii.regs[2 * i] | (vicii.regs[0x10] & b ? 0x100 : 0))
                    + vicii.screen_leftborderwidth - 0x20;
            x_exp = vicii.regs[0x1d] & b;
            delayed_pixel = 6;

            if (sprite_x < raster_x 
                && sprite_x + (x_exp ? 48 : 24) >= raster_x)
            {
                if (value & b) {
                    /* HIRES -> MC */
                    if (x_exp) {
                        delayed_load = sprite_x % 2;
                        delayed_shift = ((sprite_x & 1) == ((sprite_x >> 1) & 1) ? 1 : 0);
                    } else {
                        delayed_shift = (sprite_x & 1);
                        delayed_load = 0;
                    }
                    delayed_pixel = 6 - delayed_load;
                } else {
                    /* MC -> HIRES */
                    delayed_shift = 0;
                    delayed_load = 0;
                    if (x_exp)
                        delayed_pixel = (sprite_x & 1 ? 7 : 8 - (sprite_x & 2));
                    else
                        delayed_pixel = 6 + (sprite_x & 1);
                }
                raster_changes_sprites_add_int(&vicii.raster,
                    raster_x + delayed_pixel,
                    &sprite->mc_bug, delayed_shift << 1 | delayed_load);
            }

            raster_changes_sprites_add_int(&vicii.raster,
                raster_x + delayed_pixel,
                &sprite->multicolor, value & b ? 1 : 0);
        }
    }

    vicii.regs[0x1c] = value;
}

inline static void d01d_store(const BYTE value)
{
    int raster_x;
    int i;
    BYTE b;

    VICII_DEBUG_REGISTER(("Sprite X Expand register: $%02X", value));

    if (value == vicii.regs[0x1d])
        return;

    /* FIXME: The offset of 6 was calibrated with the GULP demo and CCS */
    raster_x = VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk)) + 6;

    for (i = 0, b = 0x01; i < 8; b <<= 1, i++) {
        raster_sprite_t *sprite;

        sprite = vicii.raster.sprite_status->sprites + i;

        if ((value & b) != (vicii.regs[0x1d] & b)) {
            raster_changes_sprites_add_int(&vicii.raster,
                                           raster_x,
                                           &sprite->x_expanded,
                                           value & b ? 1 : 0);

            /* We have to shift the sprite virtually for the drawing code */
            if (raster_x > sprite->x) {
                int actual_shift;
                if (value & b)
                    actual_shift = sprite->x - raster_x;
                else
                    actual_shift = (raster_x - sprite->x) / 2;

                sprite->x_shift_sum += actual_shift;

                raster_changes_sprites_add_int(&vicii.raster,
                                               raster_x,
                                               &sprite->x_shift,
                                               sprite->x_shift_sum);
            }
        }
    }

    vicii.regs[0x1d] = value;
}

inline static void collision_store(const WORD addr, const BYTE value)
{
    VICII_DEBUG_REGISTER(("(collision register, Read Only)"));
}

inline static void d020_store(BYTE value)
{
    VICII_DEBUG_REGISTER(("Border color register: $%02X", value));

    if (!vicii.extended_enable)
        value = (vicii.regs[0x20] & 0xf0) | (value & 0x0f);

    if (!vicii.viciidtv && (vicii.regs[0x20] == value))
        return;

    vicii.regs[0x20] = value;

    raster_changes_border_add_int(&vicii.raster,
        vicii.viciidtv?VICIIDTV_RASTER_X_ADJ(VICII_RASTER_CYCLE(maincpu_clk)):VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk)),
        (int *)&vicii.raster.border_color,
        vicii.viciidtv?vicii.dtvpalette[value]:value);
}

inline static void d021_store(BYTE value)
{
    int x_pos;
    BYTE cmask;
    cmask = (vicii.high_color) ? 0xff : 0x0f;

    if (!vicii.extended_enable)
        value = (vicii.regs[0x21] & 0xf0) | (value & 0x0f);

    VICII_DEBUG_REGISTER(("Background #0 color register: $%02X", value));

    if (!vicii.viciidtv && (vicii.regs[0x21] == value))
        return;

    x_pos = vicii.viciidtv?VICIIDTV_RASTER_X_ADJ(VICII_RASTER_CYCLE(maincpu_clk)):VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk));

    if (!vicii.force_black_overscan_background_color) {
        raster_changes_background_add_int(&vicii.raster, x_pos,
                                          &vicii.raster.idle_background_color,
                                          vicii.viciidtv?vicii.dtvpalette[value & cmask]:value);
        raster_changes_background_add_int(&vicii.raster, x_pos,
                                          &vicii.raster.xsmooth_color, 
                                          vicii.viciidtv?vicii.dtvpalette[value & cmask]:value);
    }

    raster_changes_background_add_int(&vicii.raster, x_pos,
                                      (int *)&vicii.raster.background_color,
                                      vicii.viciidtv?vicii.dtvpalette[value & cmask]:value);
    vicii.regs[0x21] = value;
}

inline static void ext_background_store(WORD addr, BYTE value)
{
    int char_num;

    BYTE cmask;
    cmask = (vicii.high_color) ? 0xff : 0x0f;

    if (!vicii.extended_enable)
        value = (vicii.regs[addr] & 0xf0) | (value & 0x0f);

    VICII_DEBUG_REGISTER(("Background color #%d register: $%02X",
                          addr - 0x21, value));

    if (!vicii.viciidtv && (vicii.regs[addr] == value))
        return;

    vicii.regs[addr] = value;

    char_num = VICII_RASTER_CHAR(VICII_RASTER_CYCLE(maincpu_clk));

    if (vicii.video_mode == VICII_EXTENDED_TEXT_MODE) {
        raster_changes_background_add_int(&vicii.raster,
            VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk)),
            &vicii.raster.xsmooth_color,
            vicii.regs[0x21 + (vicii.background_color_source >> 6)]);
    }

    raster_changes_foreground_add_int(&vicii.raster,
                                      char_num - 1,
                                      &vicii.ext_background_color[addr - 0x22],
                                      vicii.viciidtv?vicii.dtvpalette[value & cmask]:value);
}

inline static void d025_store(BYTE value)
{
    raster_sprite_status_t *sprite_status;

    value &= 0xf;

    VICII_DEBUG_REGISTER(("Sprite multicolor register #0: $%02X", value));

    if (!vicii.viciidtv && (vicii.regs[0x25] == value))
        return;

    sprite_status = vicii.raster.sprite_status;

    raster_changes_sprites_add_int(&vicii.raster,
        VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk)) + 1,
        (int *)&sprite_status->mc_sprite_color_1, 
        vicii.viciidtv?(int)vicii.dtvpalette[value]:(int)value);

    vicii.regs[0x25] = value;
}

inline static void d026_store(BYTE value)
{
    raster_sprite_status_t *sprite_status;

    value &= 0xf;

    VICII_DEBUG_REGISTER(("Sprite multicolor register #1: $%02X", value));

    if (!vicii.viciidtv && (vicii.regs[0x26] == value))
        return;

    sprite_status = vicii.raster.sprite_status;

    raster_changes_sprites_add_int(&vicii.raster,
        VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk)) + 1,
        (int*)&sprite_status->mc_sprite_color_2, 
        vicii.viciidtv?(int)vicii.dtvpalette[value]:(int)value);

    vicii.regs[0x26] = value;
}

inline static void sprite_color_store(WORD addr, BYTE value)
{
    raster_sprite_t *sprite;
    int n;

    value &= 0xf;

    VICII_DEBUG_REGISTER(("Sprite #%d color register: $%02X",
                         addr - 0x27, value));

    if (!vicii.viciidtv && (vicii.regs[addr] == value))
        return;

    n = addr - 0x27;

    sprite = vicii.raster.sprite_status->sprites + n;

        raster_changes_sprites_add_int(&vicii.raster,
            VICII_RASTER_X(VICII_RASTER_CYCLE(maincpu_clk)) + 1,
            (int *)&sprite->color, 
            vicii.viciidtv?vicii.dtvpalette[value]:value);

    vicii.regs[addr] = value;
}

inline static void d02f_store(BYTE value)
{
    if (vicii.viciie) {
        VICII_DEBUG_REGISTER(("Extended keyboard row enable: $%02X",
                             value));
        vicii.regs[0x2f] = value | 0xf8;
        cia1_set_extended_keyboard_rows_mask(value);
    } else {
        VICII_DEBUG_REGISTER(("(unused)"));
    }
}

inline static void d030_store(BYTE value)
{
    if (vicii.viciie) {
        VICII_DEBUG_REGISTER(("Store $D030: $%02X", value));
        vicii.regs[0x30] = value | 0xfc;
        vicii.fastmode = value & 1;
        vicii.half_cycles = 0;
    } else {
        VICII_DEBUG_REGISTER(("(unused)"));
    }
}

extern BYTE mem_ram[];

void viciidtv_update_colorram()
{
    vicii.color_ram_ptr = mem_ram
        + (vicii.regs[0x36] << 10)
        + ((vicii.regs[0x37] & 0x07) << 18); /* TODO test */
}

inline static void d036_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x36] = value;
        viciidtv_update_colorram();
    }
    
    VICII_DEBUG_REGISTER(("Color bank low: $%02x",value));
}

inline static void d037_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x37] = value;
        viciidtv_update_colorram();
    }
    VICII_DEBUG_REGISTER(("Color bank high: $%02x",value));
}

inline static void d038_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x38] = value;
        vicii.counta_mod &= 0xff00;
        vicii.counta_mod |= value;
    }

    VICII_DEBUG_REGISTER(("Linear Count A modulo low: $%02x",value));
}

inline static void d039_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x39] = value & 0xf;
        vicii.counta_mod &= 0x00ff;
        vicii.counta_mod |= ((value&0xf)<<8);
    }

    VICII_DEBUG_REGISTER(("Linear Count A modulo high: $%02x",value));
}

inline static void d03a_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x3a] = value;
        vicii_update_memory_ptrs(VICII_RASTER_CYCLE(maincpu_clk));
    }

    VICII_DEBUG_REGISTER(("Linear Count A Start low: $%02x",value));
}

inline static void d03b_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x3b] = value;
        vicii_update_memory_ptrs(VICII_RASTER_CYCLE(maincpu_clk));
    }

    VICII_DEBUG_REGISTER(("Linear Count A Start middle: $%02x",value));
}

static void d03c_store(const BYTE value)
{
    int cycle, old_overscan;
  
    if (!vicii.extended_enable) {
        return;
    }

    cycle = VICII_RASTER_CYCLE(maincpu_clk);
    old_overscan = vicii.overscan;
    vicii.regs[0x3c] = value;
    vicii.badline_disable = value & 0x20 ? 1 : 0;
    vicii.colorfetch_disable = value & 0x10 ? 1: 0;
    vicii.overscan = value & 0x08 ? 1 : 0;
    vicii.high_color = value & 0x04 ? 1 : 0;
    vicii.border_off = value & 0x02 ? 1 : 0;

    /* make some of those constants below into defines */
    raster_changes_border_add_int(&vicii.raster,
        VICIIDTV_RASTER_X_ADJ(cycle),
        (int *)&vicii.raster.border_disable,
        vicii.border_off);

    if (vicii.overscan) {
        vicii.raster.geometry->gfx_size.width = VICII_SCREEN_XPIX+64;
        vicii.raster.geometry->text_size.width = VICII_SCREEN_TEXTCOLS + 8;
        vicii.raster.geometry->gfx_position.x = vicii.screen_leftborderwidth - 32;
        if (!old_overscan) { /* TODO should happen max. once per line */
            /* remove the 8 extra counter increments for the current line */
            vicii.counta -= vicii.counta_step * 8;
            vicii.countb -= vicii.countb_step * 8;
        }
    } else {
        vicii.raster.geometry->gfx_size.width = VICII_SCREEN_XPIX;
        vicii.raster.geometry->text_size.width = VICII_SCREEN_TEXTCOLS;
        vicii.raster.geometry->gfx_position.x = vicii.screen_leftborderwidth;
    }

    d020_store((BYTE)vicii.regs[0x20]);
    d021_store((BYTE)vicii.regs[0x21]);
    ext_background_store(0x22,(BYTE)vicii.regs[0x22]);
    ext_background_store(0x23,(BYTE)vicii.regs[0x23]);
    ext_background_store(0x24,(BYTE)vicii.regs[0x24]);

    vicii_update_video_mode(cycle);
    vicii_update_memory_ptrs(cycle);

    VICII_DEBUG_REGISTER(("VICIIDTV register 1: $%02x",value));
}

inline static void d03d_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x3d] = value & 0x1f;
        vicii_update_memory_ptrs(VICII_RASTER_CYCLE(maincpu_clk));
    }
    
    VICII_DEBUG_REGISTER(("Graphics fetch bank: $%02x",value));
}

inline static void d03f_store(const BYTE value)
{
    if (vicii.extended_lockout) {
        return;
    }

    vicii.extended_enable = value & 0x01 ? 1 : 0;
    vicii.extended_lockout = value & 0x02 ? 1 : 0;

    vicii.regs[0x3f] = value;

    VICII_DEBUG_REGISTER(("Enable extended features: $%02x",value));
}

int vicii_extended_regs(void)
{
    return vicii.extended_enable;
}


inline static void d040_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x40] = value;
    }

    VICII_DEBUG_REGISTER(("VICIIDTV register 2: $%02x",value));
}

inline static void d044_store(const BYTE value)
{
    int offs;

    if (vicii.extended_enable) {
        vicii.regs[0x44] = value;

        offs = value & 0x7f;
        vicii.raster_irq_prevent = 0;
        if (offs <= 64) {
            if (vicii.cycles_per_line == 63 && offs > 53) {
                if (offs == 54 || offs == 55) {
                    vicii.raster_irq_prevent = 1;
                }
                offs -= 2;
            }
            vicii.raster_irq_offset = (offs+1) % vicii.cycles_per_line;
        } else {
            vicii.raster_irq_prevent = 1;
        }
        vicii_irq_set_raster_line(vicii.raster_irq_line);
    }
    VICII_DEBUG_REGISTER(("CPU cycle/IRQ trigger cycle: $%02x",value));
}

inline static void d045_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x45] = value&0x1f;
        viciidtv_update_colorram();
        vicii_update_memory_ptrs(VICII_RASTER_CYCLE(maincpu_clk));
    }

    VICII_DEBUG_REGISTER(("Linear Count A Start high: $%02x",value));
}

inline static void d046_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x46] = value;
        vicii.counta_step = value;
    }

    VICII_DEBUG_REGISTER(("Linear Count A Step: $%02x",value));
}

inline static void d047_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x47] = value;
        vicii.countb_mod &= 0xff00;
        vicii.countb_mod |= value;
    }

    VICII_DEBUG_REGISTER(("Linear Count B modulo low: $%02x",value));
}

inline static void d048_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x48] = value & 0xf;
        vicii.countb_mod &= 0x00ff;
        vicii.countb_mod |= ((value&0xf)<<8);
    }

    VICII_DEBUG_REGISTER(("Linear Count B modulo high: $%02x",value));
}

inline static void d049_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x49] = value;
    }

    VICII_DEBUG_REGISTER(("Linear Count B Start low: $%02x",value));
}

inline static void d04a_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x4a] = value;
    }

    VICII_DEBUG_REGISTER(("Linear Count B Start middle: $%02x",value));
}

inline static void d04b_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x4b] = value&0x1f;
    }

    VICII_DEBUG_REGISTER(("Linear Count B Start high: $%02x",value));
}

inline static void d04c_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x4c] = value;
        vicii.countb_step = value;
    }

    VICII_DEBUG_REGISTER(("Linear Count B Step: $%02x",value));
}

inline static void d04d_store(const BYTE value)
{
    if (vicii.extended_enable) {
        vicii.regs[0x4d] = value & 0x1f;
    }

    VICII_DEBUG_REGISTER(("Sprite bank: $%02x",value));
}

/* DTV Palette registers at $d2xx */
void REGPARM2 vicii_palette_store(WORD addr, BYTE value)
{
    if (!vicii.extended_enable) {
        return;
    }

    if (vicii.dtvpalette[addr&0xf]==value) {
        return;
    }

    vicii.dtvpalette[addr&0xf]=value;
    d020_store((BYTE)vicii.regs[0x20]);
    d021_store((BYTE)vicii.regs[0x21]);
    ext_background_store(0x22,(BYTE)vicii.regs[0x22]);
    ext_background_store(0x23,(BYTE)vicii.regs[0x23]);
    ext_background_store(0x24,(BYTE)vicii.regs[0x24]);
    d025_store((BYTE)vicii.regs[0x25]);
    d026_store((BYTE)vicii.regs[0x26]);
    sprite_color_store(0x27,(BYTE)vicii.regs[0x27]);
    sprite_color_store(0x28,(BYTE)vicii.regs[0x28]);
    sprite_color_store(0x29,(BYTE)vicii.regs[0x29]);
    sprite_color_store(0x2a,(BYTE)vicii.regs[0x2a]);
    sprite_color_store(0x2b,(BYTE)vicii.regs[0x2b]);
    sprite_color_store(0x2c,(BYTE)vicii.regs[0x2c]);
    sprite_color_store(0x2d,(BYTE)vicii.regs[0x2d]);
    sprite_color_store(0x2e,(BYTE)vicii.regs[0x2e]);
    vicii.raster.dont_cache = 1;
}

BYTE REGPARM1 vicii_palette_read(WORD addr)
{
    return 0;
}

/* Store a value in a VIC-II register.  */
void REGPARM2 vicii_store(WORD addr, BYTE value)
{
    if (vicii.extended_enable) addr &= 0x7f;
    else addr &= 0x3f;

    vicii_handle_pending_alarms_external_write();

    /* This is necessary as we must be sure that the previous line has been
       updated and `current_line' is actually set to the current Y position of
       the raster.  Otherwise we might mix the changes for this line with the
       changes for the previous one.  */
    if (maincpu_clk >= vicii.draw_clk)
        vicii_raster_draw_alarm_handler(maincpu_clk - vicii.draw_clk, NULL);

    VICII_DEBUG_REGISTER(("WRITE $D0%02X at cycle %d of current_line $%04X",
                         addr, VICII_RASTER_CYCLE(maincpu_clk),
                         VICII_RASTER_Y(maincpu_clk)));

    switch (addr) {
      case 0x0:                   /* $D000: Sprite #0 X position LSB */
      case 0x2:                   /* $D002: Sprite #1 X position LSB */
      case 0x4:                   /* $D004: Sprite #2 X position LSB */
      case 0x6:                   /* $D006: Sprite #3 X position LSB */
      case 0x8:                   /* $D008: Sprite #4 X position LSB */
      case 0xa:                   /* $D00a: Sprite #5 X position LSB */
      case 0xc:                   /* $D00c: Sprite #6 X position LSB */
      case 0xe:                   /* $D00e: Sprite #7 X position LSB */
        store_sprite_x_position_lsb(addr, value);
        break;

      case 0x1:                   /* $D001: Sprite #0 Y position */
      case 0x3:                   /* $D003: Sprite #1 Y position */
      case 0x5:                   /* $D005: Sprite #2 Y position */
      case 0x7:                   /* $D007: Sprite #3 Y position */
      case 0x9:                   /* $D009: Sprite #4 Y position */
      case 0xb:                   /* $D00B: Sprite #5 Y position */
      case 0xd:                   /* $D00D: Sprite #6 Y position */
      case 0xf:                   /* $D00F: Sprite #7 Y position */
        store_sprite_y_position(addr, value);
        break;

      case 0x10:                  /* $D010: Sprite X position MSB */
        store_sprite_x_position_msb(addr, value);
        break;

      case 0x11:                  /* $D011: video mode, Y scroll, 24/25 line
                                     mode and raster MSB */
        d011_store(value);
        break;

      case 0x12:                  /* $D012: Raster line compare */
        d012_store(value);
        break;

      case 0x13:                  /* $D013: Light Pen X */
      case 0x14:                  /* $D014: Light Pen Y */
        break;

      case 0x15:                  /* $D015: Sprite Enable */
        d015_store(value);
        break;

      case 0x16:                  /* $D016 */
        d016_store(value);
        break;

      case 0x17:                  /* $D017: Sprite Y-expand */
        d017_store(value);
        break;

      case 0x18:                  /* $D018: Video and char matrix base
                                     address */
        d018_store(value);
        break;

      case 0x19:                  /* $D019: IRQ flag register */
        d019_store(value);
        break;

      case 0x1a:                  /* $D01A: IRQ mask register */
        d01a_store(value);
        break;

      case 0x1b:                  /* $D01B: Sprite priority */
        d01b_store(value);
        break;

      case 0x1c:                  /* $D01C: Sprite Multicolor select */
        d01c_store(value);
        break;

      case 0x1d:                  /* $D01D: Sprite X-expand */
        d01d_store(value);
        break;

      case 0x1e:                  /* $D01E: Sprite-sprite collision */
      case 0x1f:                  /* $D01F: Sprite-background collision */
        collision_store(addr, value);
        break;

      case 0x20:                  /* $D020: Border color */
        d020_store(value);
        break;

      case 0x21:                  /* $D021: Background #0 color */
        d021_store(value);
        break;

      case 0x22:                  /* $D022: Background #1 color */
      case 0x23:                  /* $D023: Background #2 color */
      case 0x24:                  /* $D024: Background #3 color */
        ext_background_store(addr, value);
        break;

      case 0x25:                  /* $D025: Sprite multicolor register #0 */
        d025_store(value);
        break;

      case 0x26:                  /* $D026: Sprite multicolor register #1 */
        d026_store(value);
        break;

      case 0x27:                  /* $D027: Sprite #0 color */
      case 0x28:                  /* $D028: Sprite #1 color */
      case 0x29:                  /* $D029: Sprite #2 color */
      case 0x2a:                  /* $D02A: Sprite #3 color */
      case 0x2b:                  /* $D02B: Sprite #4 color */
      case 0x2c:                  /* $D02C: Sprite #5 color */
      case 0x2d:                  /* $D02D: Sprite #6 color */
      case 0x2e:                  /* $D02E: Sprite #7 color */
        sprite_color_store(addr, value);
        break;

      case 0x2f:                  /* $D02F: Unused (or extended keyboard row
                                     select) */
        d02f_store(value);
        break;

      case 0x30:                  /* $D030: Unused (or VIC-IIe extension) */
        d030_store(value);
        break;

      case 0x31:                  /* $D031: Unused */
      case 0x32:                  /* $D032: Unused */
      case 0x33:                  /* $D033: Unused */
      case 0x34:                  /* $D034: Unused */
      case 0x35:                  /* $D035: Unused */
      case 0x3e:                  /* $D03E: Unused */
      case 0x50:
      case 0x51:
      case 0x52:
      case 0x53:
      case 0x54:
      case 0x55:
      case 0x56:
      case 0x57:
      case 0x58:
      case 0x59:
      case 0x5a:
      case 0x5b:
      case 0x5c:
      case 0x5d:
      case 0x5e:
      case 0x5f:
      case 0x60:
      case 0x61:
      case 0x62:
      case 0x63:
      case 0x64:
      case 0x65:
      case 0x66:
      case 0x67:
      case 0x68:
      case 0x69:
      case 0x6a:
      case 0x6b:
      case 0x6c:
      case 0x6d:
      case 0x6e:
      case 0x6f:
      case 0x70:
      case 0x71:
      case 0x72:
      case 0x73:
      case 0x74:
      case 0x75:
      case 0x76:
      case 0x77:
      case 0x78:
      case 0x79:
      case 0x7a:
      case 0x7b:
      case 0x7c:
      case 0x7d:
      case 0x7e:
      case 0x7f:
        VICII_DEBUG_REGISTER(("(unused)"));
        break;

      case 0x36:                  /* $D036: Color Bank Low */
        d036_store(value);
        break;
      case 0x37:                  /* $D037: Color Bank High */
        d037_store(value);
        break;
      case 0x38:                  /* $D038: Linear Count A Modulo Low */
        d038_store(value);
        break;
      case 0x39:                  /* $D039: Linear Count A Modulo High */
        d039_store(value);
        break;
      case 0x3a:                  /* $D03a: Linear Count A Start Low */
        d03a_store(value);
        break;
      case 0x3b:                  /* $D03B: Linear Count A Start Middle */
        d03b_store(value);
        break;
      case 0x3c:                  /* $D03C: VICIIDTV register 1 */
        d03c_store(value);
        break;
      case 0x3d:                  /* $D03D: Graphics fetch bank */
        d03d_store(value);
        break;
      case 0x3f:                  /* $D03F: Enable extended features register */
        d03f_store(value);
        break;
      case 0x40:                  /* $D040: VICIIDTV register 2 */
        d040_store(value);
        break;
      case 0x41:                  /* $D041: Burst rate modulus high */
      case 0x42:                  /* $D042: Burst rate modulus middle */
      case 0x43:                  /* $D043: Burst rate modulus low */
        VICII_DEBUG_REGISTER(("Burst rate modulus (ignored)"));
        break;
      case 0x44:                  /* $D044: CPU cycle/IRQ trigger cycle */
        d044_store(value);
        break;
      case 0x45:                  /* $D045: Linear Count A Start High */
        d045_store(value);
        break;
      case 0x46:                  /* $D046: Linear Count A Step */
        d046_store(value);
        break;
      case 0x47:                  /* $D047: Linear Count B Modulo Low */
        d047_store(value);
        break;
      case 0x48:                  /* $D048: Linear Count B Modulo High */
        d048_store(value);
        break;
      case 0x49:                  /* $D049: Linear Count B Start Low */
        d049_store(value);
        break;
      case 0x4a:                  /* $D04A: Linear Count B Start Middle */
        d04a_store(value);
        break;
      case 0x4b:                  /* $D04B: Linear Count B Start High */
        d04b_store(value);
        break;
      case 0x4c:                  /* $D04C: Linear Count B Step */
        d04c_store(value);
        break;
      case 0x4d:                  /* $D04D: Sprite bank */
        d04d_store(value);
        break;
      case 0x4e:                  /* $D04E: Scan line timing adjust */
      case 0x4f:                  /* $D04F: VICIIDTV register 3 */
        VICII_DEBUG_REGISTER(("Scan line/Saturation/Burst lock (ignored)"));
        break;
    }
}


/* Helper function for reading from $D011/$D012.  */
inline static unsigned int read_raster_y(void)
{
    int raster_y;

    raster_y = VICII_RASTER_Y(maincpu_clk);

    /* Line 0 is 62 cycles long, while line (SCREEN_HEIGHT - 1) is 64
       cycles long.  As a result, the counter is incremented one
       cycle later on line 0.  */
    if (raster_y == 0 && VICII_RASTER_CYCLE(maincpu_clk) == 0)
        raster_y = vicii.screen_height - 1;

    return raster_y;
}

inline static BYTE d01112_read(WORD addr)
{
    unsigned int tmp = read_raster_y();

    VICII_DEBUG_REGISTER(("Raster Line register %svalue = $%04X",
                         (addr == 0x11 ? "(highest bit) " : ""), tmp));
    if (addr == 0x11)
        vicii.last_read = (vicii.regs[addr] & 0x7f) | ((tmp & 0x100) >> 1);
    else
        vicii.last_read = tmp & 0xff;

    return vicii.last_read;
}


inline static BYTE d019_read(void)
{
    /* Manually set raster IRQ flag if the opcode reading $d019 has crossed
       the line end and the raster IRQ alarm has not been executed yet. */
    if (VICII_RASTER_Y(maincpu_clk) == vicii.raster_irq_line
        && vicii.raster_irq_clk != CLOCK_MAX
        && maincpu_clk >= vicii.raster_irq_clk) {
        if (vicii.regs[0x1a] & 0x1)
            vicii.last_read = vicii.irq_status | 0xf1;
        else
            vicii.last_read = vicii.irq_status | 0x71;
    } else {
        vicii.last_read = vicii.irq_status | 0x70;
    }

    return vicii.viciidtv?vicii.last_read | ((vicii.last_read&0xf) ? 0x80 : 0x00):vicii.last_read;
}

inline static BYTE d01e_read(void)
{
    /* Remove the pending sprite-sprite interrupt, as the collision
       register is reset upon read accesses.  */
    if (!vicii.viciidtv)
        vicii_irq_sscoll_clear();

    if (!vicii_resources.sprite_sprite_collisions_enabled) {
        VICII_DEBUG_REGISTER(("Sprite-sprite collision mask: $00 "
                             "(emulation disabled)"));
        vicii.sprite_sprite_collisions = 0;
        return 0;
    }

    vicii.regs[0x1e] = vicii.sprite_sprite_collisions;
    vicii.sprite_sprite_collisions = 0;
    VICII_DEBUG_REGISTER(("Sprite-sprite collision mask: $%02X",
                         vicii.regs[0x1e]));

    return vicii.regs[0x1e];
}

inline static BYTE d01f_read(void)
{
    /* Remove the pending sprite-background interrupt, as the collision
       register is reset upon read accesses.  */
    if (!vicii.viciidtv)
        vicii_irq_sbcoll_clear();

    if (!vicii_resources.sprite_background_collisions_enabled) {
        VICII_DEBUG_REGISTER(("Sprite-background collision mask: $00 "
                             "(emulation disabled)"));
        vicii.sprite_background_collisions = 0;
        return 0;
    }

    vicii.regs[0x1f] = vicii.sprite_background_collisions;
    vicii.sprite_background_collisions = 0;
    VICII_DEBUG_REGISTER(("Sprite-background collision mask: $%02X",
                          vicii.regs[0x1f]));

#if defined (VICII_DEBUG_SB_COLLISIONS)
    log_message(vicii.log,
                "vicii.sprite_background_collisions reset by $D01F "
                "read at line 0x%X.",
                VICII_RASTER_Y(clk));
#endif

    return vicii.regs[0x1f];
}

inline static BYTE d044_read(void)
{
    return VICIIDTV_RASTER_CYCLE(maincpu_clk) | 0x80;
}

/* Read a value from a VIC-II register.  */
BYTE REGPARM1 vicii_read(WORD addr)
{
    if (vicii.extended_enable) addr &= 0x7f;
    else addr &= 0x3f;

    /* Serve all pending events.  */
    vicii_handle_pending_alarms(0);

    VICII_DEBUG_REGISTER(("READ $D0%02X at cycle %d of current_line $%04X:",
                         addr, VICII_RASTER_CYCLE(maincpu_clk),
                         VICII_RASTER_Y(maincpu_clk)));

    /* Note: we use hardcoded values instead of `unused_bits_in_registers[]'
       here because this is a little bit faster.  */
    switch (addr) {
      case 0x0:                   /* $D000: Sprite #0 X position LSB */
      case 0x2:                   /* $D002: Sprite #1 X position LSB */
      case 0x4:                   /* $D004: Sprite #2 X position LSB */
      case 0x6:                   /* $D006: Sprite #3 X position LSB */
      case 0x8:                   /* $D008: Sprite #4 X position LSB */
      case 0xa:                   /* $D00a: Sprite #5 X position LSB */
      case 0xc:                   /* $D00c: Sprite #6 X position LSB */
      case 0xe:                   /* $D00e: Sprite #7 X position LSB */
        VICII_DEBUG_REGISTER(("Sprite #%d X position LSB: $%02X",
                             addr >> 1, vicii.regs[addr]));
        return vicii.regs[addr];

      case 0x1:                   /* $D001: Sprite #0 Y position */
      case 0x3:                   /* $D003: Sprite #1 Y position */
      case 0x5:                   /* $D005: Sprite #2 Y position */
      case 0x7:                   /* $D007: Sprite #3 Y position */
      case 0x9:                   /* $D009: Sprite #4 Y position */
      case 0xb:                   /* $D00B: Sprite #5 Y position */
      case 0xd:                   /* $D00D: Sprite #6 Y position */
      case 0xf:                   /* $D00F: Sprite #7 Y position */
        VICII_DEBUG_REGISTER(("Sprite #%d Y position: $%02X",
                             addr >> 1, vicii.regs[addr]));
        return vicii.regs[addr];

      case 0x10:                  /* $D010: Sprite X position MSB */
        VICII_DEBUG_REGISTER(("Sprite X position MSB: $%02X",
                             vicii.regs[addr]));
        return vicii.regs[addr];

      case 0x11:                /* $D011: video mode, Y scroll, 24/25 line mode
                                   and raster MSB */
      case 0x12:                /* $D012: Raster line compare */
        return d01112_read(addr);

      case 0x13:                  /* $D013: Light Pen X */
        VICII_DEBUG_REGISTER(("Light pen X: %d", vicii.light_pen.x));
        return vicii.light_pen.x;

      case 0x14:                  /* $D014: Light Pen Y */
        VICII_DEBUG_REGISTER(("Light pen Y: %d", vicii.light_pen.y));
        return vicii.light_pen.y;

      case 0x15:                  /* $D015: Sprite Enable */
        VICII_DEBUG_REGISTER(("Sprite Enable register: $%02X",
                             vicii.regs[addr]));
        return vicii.regs[addr];

      case 0x16:                  /* $D016 */
        VICII_DEBUG_REGISTER(("$D016 Control register read: $%02X",
                             vicii.regs[addr]));
        return vicii.regs[addr] | 0xc0;

      case 0x17:                  /* $D017: Sprite Y-expand */
        VICII_DEBUG_REGISTER(("Sprite Y Expand register: $%02X",
                             vicii.regs[addr]));
        return vicii.regs[addr];

      case 0x18:                /* $D018: Video and char matrix base address */
        VICII_DEBUG_REGISTER(("Video memory address register: $%02X",
                             vicii.regs[addr]));
        return vicii.regs[addr] | 0x1;

      case 0x19:                  /* $D019: IRQ flag register */
        {
            BYTE tmp;

            tmp = d019_read();
            VICII_DEBUG_REGISTER(("Interrupt register: $%02X", tmp));

            return tmp;
        }

      case 0x1a:                  /* $D01A: IRQ mask register  */
        VICII_DEBUG_REGISTER(("Mask register: $%02X",
                             vicii.regs[addr] | 0xf0));
        return vicii.regs[addr] | 0xf0;

      case 0x1b:                  /* $D01B: Sprite priority */
        VICII_DEBUG_REGISTER(("Sprite Priority register: $%02X",
                             vicii.regs[addr]));
        return vicii.regs[addr];

      case 0x1c:                  /* $D01C: Sprite Multicolor select */
        VICII_DEBUG_REGISTER(("Sprite Multicolor Enable register: $%02X",
                             vicii.regs[addr]));
        return vicii.regs[addr];

      case 0x1d:                  /* $D01D: Sprite X-expand */
        VICII_DEBUG_REGISTER(("Sprite X Expand register: $%02X",
                             vicii.regs[addr]));
        return vicii.regs[addr];

      case 0x1e:                  /* $D01E: Sprite-sprite collision */
        return d01e_read();

      case 0x1f:                  /* $D01F: Sprite-background collision */
        return d01f_read();

      case 0x20:                  /* $D020: Border color */
        VICII_DEBUG_REGISTER(("Border Color register: $%02X",
                             vicii.regs[addr]));
        return vicii.viciidtv?vicii.regs[addr]:(vicii.regs[addr] | 0xf0);

      case 0x21:                  /* $D021: Background #0 color */
      case 0x22:                  /* $D022: Background #1 color */
      case 0x23:                  /* $D023: Background #2 color */
      case 0x24:                  /* $D024: Background #3 color */
        VICII_DEBUG_REGISTER(("Background Color #%d register: $%02X",
                             addr - 0x21, vicii.regs[addr]));
        return vicii.viciidtv?vicii.regs[addr]:(vicii.regs[addr] | 0xf0);

      case 0x25:                  /* $D025: Sprite multicolor register #0 */
      case 0x26:                  /* $D026: Sprite multicolor register #1 */
        VICII_DEBUG_REGISTER(("Multicolor register #%d: $%02X",
                             addr - 0x22, vicii.regs[addr]));
        return vicii.regs[addr] | 0xf0;

      case 0x27:                  /* $D027: Sprite #0 color */
      case 0x28:                  /* $D028: Sprite #1 color */
      case 0x29:                  /* $D029: Sprite #2 color */
      case 0x2a:                  /* $D02A: Sprite #3 color */
      case 0x2b:                  /* $D02B: Sprite #4 color */
      case 0x2c:                  /* $D02C: Sprite #5 color */
      case 0x2d:                  /* $D02D: Sprite #6 color */
      case 0x2e:                  /* $D02E: Sprite #7 color */
        VICII_DEBUG_REGISTER(("Sprite #%d color: $%02X",
                             addr - 0x22, vicii.regs[addr]));
        return vicii.regs[addr] | 0xf0;

      case 0x2f:                  /* $D02F: Unused (or extended keyboard row
                                     select) */
        if (vicii.viciie) {
            VICII_DEBUG_REGISTER(("Extended keyboard row enable: $%02X",
                                 vicii.regs[addr]));
            return vicii.regs[addr];
        } else {
            VICII_DEBUG_REGISTER(("(unused)"));
            return 0xff;
        }
        break;

      case 0x30:                  /* $D030: Unused (or VIC-IIe extension) */
        if (vicii.viciie) {
            VICII_DEBUG_REGISTER(("Read $D030: $%02X", vicii.regs[addr]));
            return vicii.regs[addr];
        } else {
            VICII_DEBUG_REGISTER(("(unused)"));
            return 0xff;
        }
        break;

      case 0x31:                  /* $D031: Unused */
      case 0x32:                  /* $D032: Unused */
      case 0x33:                  /* $D033: Unused */
      case 0x34:                  /* $D034: Unused */
      case 0x35:                  /* $D035: Unused */
        return 0xff;

      case 0x36:                  /* $D036: Color Bank Low */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("Color Bank Low: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff; 

      case 0x37:                  /* $D037: Color Bank High */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("Color Bank High: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff;

      case 0x38:                  /* $D038: Linear Count A Modulo Low */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("Linear Count A Modulo Low: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff;

      case 0x39:                  /* $D039: Linear Count A Modulo High */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("Linear Count A Modulo High: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff;

      case 0x3a:                  /* $D03a: Linear Count A Start Low */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("Linear Count A Start Low: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff;

      case 0x3b:                  /* $D03B: Linear Count A Start Middle */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("Linear Count A Start Middle: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff;

      case 0x3c:                  /* $D03C: VICIIDTV register 1 */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("VICIIDTV register 1: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff;

      case 0x3d:                  /* $D03D: Graphics fetch bank */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("Graphics fetch bank: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff;

      case 0x3e:                  /* $D03E: Unused */
        return 0xff;

      case 0x3f:                  /* $D03F: Enable extended features register */
        if (vicii.viciidtv) {
            VICII_DEBUG_REGISTER(("Enable extended features: $%02X",
                                 vicii.regs[addr]));
        }
        return 0xff;

      case 0x40:                  /* $D040: VICIIDTV register 2 */
        VICII_DEBUG_REGISTER(("VICIIDTV register 2: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x41:                  /* $D041: Burst rate modulus high */
        VICII_DEBUG_REGISTER(("Burst rate modulus high: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x42:                  /* $D042: Burst rate modulus middle */
        VICII_DEBUG_REGISTER(("Burst rate modulus middle: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x43:                  /* $D043: Burst rate modulus low */
        VICII_DEBUG_REGISTER(("Burst rate modulus low: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x44:                  /* $D044: CPU cycle/IRQ trigger cycle */
        VICII_DEBUG_REGISTER(("CPU cycle/IRQ trigger cycle: $%02X",
                             vicii.regs[addr]));
        return d044_read();

      case 0x45:                  /* $D045: Linear Count A Start High */
        VICII_DEBUG_REGISTER(("Linear Count A Start High: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x46:                  /* $D046: Linear Count A Step */
        VICII_DEBUG_REGISTER(("Linear Count A Step: $%02X",
                             vicii.regs[addr]));
        return 0xff;
	
      case 0x47:                  /* $D047: Linear Count B Modulo Low */
        VICII_DEBUG_REGISTER(("Linear Count B Modulo Low: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x48:                  /* $D048: Linear Count B Modulo High */
        VICII_DEBUG_REGISTER(("Linear Count B Modulo High: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x49:                  /* $D049: Linear Count B Start Low */
        VICII_DEBUG_REGISTER(("Linear Count B Start Low: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x4a:                  /* $D04A: Linear Count B Start Middle */
        VICII_DEBUG_REGISTER(("Linear Count B Start Middle: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x4b:                  /* $D04B: Linear Count B Start High */
        VICII_DEBUG_REGISTER(("Linear Count B Start High: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x4c:                  /* $D04C: Linear Count B Step */
        VICII_DEBUG_REGISTER(("Linear Count B Step: $%02X",
                             vicii.regs[addr]));
        return 0xff;
	
      case 0x4d:                  /* $D04D: Sprite bank */
        VICII_DEBUG_REGISTER(("Sprite bank: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x4e:                  /* $D04E: Scan line timing adjust */
        VICII_DEBUG_REGISTER(("Scan line timing adjust: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      case 0x4f:                  /* $D04F: VICIIDTV register 3 */
        VICII_DEBUG_REGISTER(("VICIIDTV register 3: $%02X",
                             vicii.regs[addr]));
        return 0xff;

      default:
        return 0xff;
    }
    return 0xff;  /* make compiler happy */
}

inline static BYTE d019_peek(void)
{
    /* Manually set raster IRQ flag if the opcode reading $d019 has crossed
       the line end and the raster IRQ alarm has not been executed yet. */
    if (VICII_RASTER_Y(maincpu_clk) == vicii.raster_irq_line
        && vicii.raster_irq_clk != CLOCK_MAX
        && maincpu_clk >= vicii.raster_irq_clk) {
        if (vicii.regs[0x1a] & 0x1)
            return vicii.irq_status | 0xf1;
        else
            return vicii.viciidtv?vicii.irq_status | ((vicii.irq_status & 0xf) ? 0xf1 : 0x71):vicii.irq_status | 0x71;
    } else {
        return vicii.viciidtv?vicii.irq_status | ((vicii.irq_status & 0xf) ? 0xf0 : 0x70):vicii.irq_status | 0x70;
    }

    return vicii.viciidtv?vicii.irq_status | ((vicii.irq_status & 0xf) ? 0x80 : 0x00):vicii.irq_status;
}

BYTE REGPARM1 vicii_peek(WORD addr)
{
    if (!vicii.viciidtv) {
        addr &= 0x3f;
    } else {
        addr &= 0x7f;
    }

    switch (addr) {
      case 0x11:              /* $D011: video mode, Y scroll, 24/25 line mode
                                 and raster MSB */
        return (vicii.regs[addr] & 0x7f) | ((read_raster_y () & 0x100) >> 1);
      case 0x12:              /* $D012: Raster line LSB */
        return read_raster_y() & 0xff;
      case 0x13:              /* $D013: Light Pen X */
        return vicii.light_pen.x;
      case 0x14:              /* $D014: Light Pen Y */
        return vicii.light_pen.y;
      case 0x19:
        return d019_peek();
      case 0x1e:              /* $D01E: Sprite-sprite collision */
        return vicii.sprite_sprite_collisions;
      case 0x1f:              /* $D01F: Sprite-background collision */
        return vicii.sprite_background_collisions;
      case 0x2f:              /* Extended keyboard row select */
        if (vicii.viciie)
            return vicii.regs[addr] | 0xf8;
        else
            return /* vicii.regs[addr] | */ 0xff;
      default:
        if (addr>0x4f) return 0xff;
        return vicii.regs[addr] | unused_bits_in_registers[addr];
    }
}