File: TIA.cxx

package info (click to toggle)
stella 0.7-2
  • links: PTS
  • area: non-free
  • in suites: hamm, slink
  • size: 864 kB
  • ctags: 1,158
  • sloc: cpp: 6,615; ansic: 492; makefile: 224; asm: 31
file content (1992 lines) | stat: -rw-r--r-- 59,301 bytes parent folder | download
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
//============================================================================
//
//    SSSS    tt          lll  lll
//   SS  SS   tt           ll   ll
//   SS     tttttt  eeee   ll   ll   aaaa    "An Atari 2600 VCS Emulator"
//    SSSS    tt   ee  ee  ll   ll      aa
//       SS   tt   eeeeee  ll   ll   aaaaa   Copyright (c) 1995,1996,1997
//   SS  SS   tt   ee      ll   ll  aa  aa         Bradford W. Mott
//    SSSS     ttt  eeeee llll llll  aaaaa
//
//============================================================================

/**
  A device that emulates the Television Interface Adapator found in
  the Atari 2600.  The Television Interface Adapator is an integrated
  circuit designed to interface between an eight bit microprocessor
  and a television video modulator. It converts eight bit parallel data
  into serial outputs for the color, luminosity, and composite sync
  required by a video modulator.

  This class emulates the TIA by outputing the serial outputs into
  a frame buffer which can then be displayed on screen.

  @author  Bradford W. Mott
  @version $Id: TIA.cxx,v 1.3 1997/05/31 14:01:38 bwmott Exp $
*/

#include <assert.h>
#include <iostream.h>
#include <string.h>

#include "Snd.hxx"
#include "System.hxx"
#include "Term.hxx"
#include "TIA.hxx"

#define HBLANKOFFSET 0
#define HBLANK (68 - HBLANKOFFSET)

//============================================================================
// Constructor
//============================================================================
TIA::TIA(System& system)
    : Device(system)
{
  // Map all of my addresses in the system
  for(uWord address = 0; address < 8192; ++address)
  {
    if((address & 0x1080) == 0x0000)
    {
      mySystem.mapPeek(address, *this);
      mySystem.mapPoke(address, *this);
    }
  }

  // Ask the sound system to create me a sound object
  mySound = Sound::create();

  // Allocate buffers for two frame buffers
  myCurrentFrameBuffer = new uByte[160 * 300];
  myPreviousFrameBuffer = new uByte[160 * 300];

  #if defined OS_2
  DiveMap((PBYTE) myCurrentFrameBuffer);
  #endif

  // Compute all of the mask tables
  computeBallMaskTable();
  computeCollisionTable();
  computeMissleMaskTable();
  computePlayerMaskTable();
  computePlayerReflectTable();
  computePlayfieldMaskTable();

  // Put myself in a sane state
  reset();
}

//============================================================================
// Destructor
//============================================================================
TIA::~TIA()
{
  #if defined OS_2
    DiveUnMap();
  #endif

  delete mySound;
  delete[] myCurrentFrameBuffer;
  delete[] myPreviousFrameBuffer;
}

//============================================================================
// Reset to power on state
//============================================================================
void TIA::reset()
{
  // Clear frame buffers
  for(uLong i = 0; i < 160 * 300; ++i)
  {
    myCurrentFrameBuffer[i] = myPreviousFrameBuffer[i] = 0;
  }

  // Reset pixel pointer and drawing flag
  myFramePointer = myCurrentFrameBuffer;
  myDrawFrame = false;

  // Calculate color clock offsets for starting and stoping frame drawing
  myStartDisplayOffset = mySystem.properties().integer("Display.YStart") * 228;
  myStopDisplayOffset = mySystem.properties().integer("Display.Height") * 228 
      + myStartDisplayOffset;

  // Reasonable values to start and stop the current frame drawing
  myClockWhenFrameStarted = mySystem.m6507().cycles() * 3;
  myClockStartDisplay = myClockWhenFrameStarted + myStartDisplayOffset;
  myClockStopDisplay = myClockWhenFrameStarted + myStopDisplayOffset;
  myClockAtLastUpdate = myClockWhenFrameStarted;
  myClockToEndOfScanLine = 228;
  myVSYNCFinishClock = 0xFFFFFFFF;

  // Currently no objects are enabled
  myEnabledObjects = 0;

  // Some default values for the registers
  myVSYNC = 0;
  myVBLANK = 0;
  myNUSIZ0 = 0;
  myNUSIZ1 = 0;
  myCOLUP0 = 0;
  myCOLUP1 = 0;
  myCOLUPF = 0;
  myCOLUBK = 0;
  myCTRLPF = 0;
  myREFP0 = false;
  myREFP1 = false;
  myPF = 0;
  myGRP0 = 0;
  myGRP1 = 0;
  myDGRP0 = 0;
  myDGRP1 = 0;
  myENAM0 = false;
  myENAM1 = false;
  myENABL = false;
  myDENABL = false;
  myHMP0 = 0;
  myHMP1 = 0;
  myHMM0 = 0;
  myHMM1 = 0;
  myHMBL = 0;
  myVDELP0 = false;
  myVDELP1 = false;
  myVDELBL = false;
  myRESMP0 = false;
  myRESMP1 = false;
  myCollision = 0;
  myPOSP0 = 0;
  myPOSP1 = 0;
  myPOSM0 = 0;
  myPOSM1 = 0;
  myPOSBL = 0;

  // Some default values for the "current" variables
  myCurrentPF = 0;
  myCurrentGRP0 = 0;
  myCurrentGRP1 = 0;
  myCurrentBLMask = ourBallMaskTable[0][0];
  myCurrentM0Mask = ourMissleMaskTable[0][0][0];
  myCurrentM1Mask = ourMissleMaskTable[0][0][0];
  myCurrentP0Mask = ourPlayerMaskTable[0][0];
  myCurrentP1Mask = ourPlayerMaskTable[0][0];
  myCurrentPFMask = ourPlayfieldTable[0];

  // Get the number of color clocks to delay a player's graphics write by
  myPlayerDelay = mySystem.properties().integer("TIA.PlayerDelay");

  myLastHMOVEClock = 0;
  myM0CosmicArkMotionEnabled = false;
}
 
//============================================================================
// Execute instructions until the next frame has been generated
//============================================================================
void TIA::frame(bool drawFrame)
{
  // Indicate that we should draw the frame
  myDrawFrame = drawFrame;

  #ifndef OS_2    // remove for OS/2 - conflicts with DIVE support
  // Swap frame buffers if we're actually drawing the frame
  if(myDrawFrame)
  {
    uByte* tmp = myCurrentFrameBuffer;
    myCurrentFrameBuffer = myPreviousFrameBuffer;
    myPreviousFrameBuffer = tmp;
  }
  #endif

  // Reset frame buffer pointer
  myFramePointer = myCurrentFrameBuffer;

  // Execute instructions until frame is finished or the given number 
  // of cycles has been processed
  mySystem.m6507().execute(150000);
}

//============================================================================
// Answer the height of my frame buffer
//============================================================================
uWord TIA::height() const 
{ 
  return mySystem.properties().integer("Display.Height"); 
}

//============================================================================
// Answer sound object used for playing audio
//============================================================================
Sound& TIA::sound() const
{
  return *mySound;
}

//============================================================================
// Compute the ball mask table
//============================================================================
void TIA::computeBallMaskTable()
{
  // First, calculate masks for alignment 0
  for(Long size = 0; size < 4; ++size)
  {
    Long x;

    // Set all of the masks to false to start with
    for(x = 0; x < 160; ++x)
    {
      ourBallMaskTable[0][size][x] = false;
    }

    // Set the necessary fields true
    for(x = 0; x < 160 + 8; ++x)
    {
      if((x >= 0) && (x < (1 << size)))
      {
        ourBallMaskTable[0][size][x % 160] = true;
      }
    }

    // Copy fields into the wrap-around area of the mask
    for(x = 0; x < 160; ++x)
    {
      ourBallMaskTable[0][size][x + 160] = ourBallMaskTable[0][size][x];
    }
  }

  // Now, copy data for alignments of 1, 2 and 3
  for(uLong align = 1; align < 4; ++align)
  {
    for(uLong size = 0; size < 4; ++size)
    {
      for(uLong x = 0; x < 320; ++x)
      {
        ourBallMaskTable[align][size][x] = 
            ourBallMaskTable[0][size][(x + 320 - align) % 320];
      }
    }
  }
}

//============================================================================
// Compute the collision decode table
//============================================================================
void TIA::computeCollisionTable()
{
  for(int i = 0; i < 64; ++i)
  { 
    ourCollisionTable[i] = 0;

    if((i & myM0Bit) && (i & myP1Bit))    // M0-P1
      ourCollisionTable[i] |= 0x0001;

    if((i & myM0Bit) && (i & myP0Bit))    // M0-P0
      ourCollisionTable[i] |= 0x0002;

    if((i & myM1Bit) && (i & myP0Bit))    // M1-P0
      ourCollisionTable[i] |= 0x0004;

    if((i & myM1Bit) && (i & myP1Bit))    // M1-P1
      ourCollisionTable[i] |= 0x0008;

    if((i & myP0Bit) && (i & myPFBit))    // P0-PF
      ourCollisionTable[i] |= 0x0010;

    if((i & myP0Bit) && (i & myBLBit))    // P0-BL
      ourCollisionTable[i] |= 0x0020;

    if((i & myP1Bit) && (i & myPFBit))    // P1-PF
      ourCollisionTable[i] |= 0x0040;

    if((i & myP1Bit) && (i & myBLBit))    // P1-BL
      ourCollisionTable[i] |= 0x0080;

    if((i & myM0Bit) && (i & myPFBit))    // M0-PF
      ourCollisionTable[i] |= 0x0100;

    if((i & myM0Bit) && (i & myBLBit))    // M0-BL
      ourCollisionTable[i] |= 0x0200;

    if((i & myM1Bit) && (i & myPFBit))    // M1-PF
      ourCollisionTable[i] |= 0x0400;

    if((i & myM1Bit) && (i & myBLBit))    // M1-BL
      ourCollisionTable[i] |= 0x0800;

    if((i & myBLBit) && (i & myPFBit))    // BL-PF
      ourCollisionTable[i] |= 0x1000;

    if((i & myP0Bit) && (i & myP1Bit))    // P0-P1
      ourCollisionTable[i] |= 0x2000;

    if((i & myM0Bit) && (i & myM1Bit))    // M0-M1
      ourCollisionTable[i] |= 0x4000;
  }
}

//============================================================================
// Compute the missle mask table
//============================================================================
void TIA::computeMissleMaskTable()
{
  // First, calculate masks for alignment 0
  Long x, size, number;

  // Clear the missle table to start with
  for(number = 0; number < 8; ++number)
    for(size = 0; size < 4; ++size)
      for(x = 0; x < 160; ++x)
        ourMissleMaskTable[0][number][size][x] = false;

  for(number = 0; number < 8; ++number)
  {
    for(size = 0; size < 4; ++size)
    {
      for(x = 0; x < 160 + 72; ++x)
      {
        // Only one copy of the missle
        if((number == 0x00) || (number == 0x05) || (number == 0x07))
        {
          if((x >= 0) && (x < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
        }
        // Two copies - close
        else if(number == 0x01)
        {
          if((x >= 0) && (x < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
          else if(((x - 16) >= 0) && ((x - 16) < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
        }
        // Two copies - medium
        else if(number == 0x02)
        {
          if((x >= 0) && (x < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
          else if(((x - 32) >= 0) && ((x - 32) < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
        }
        // Three copies - close
        else if(number == 0x03)
        {
          if((x >= 0) && (x < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
          else if(((x - 16) >= 0) && ((x - 16) < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
          else if(((x - 32) >= 0) && ((x - 32) < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
        }
        // Two copies - wide
        else if(number == 0x04)
        {
          if((x >= 0) && (x < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
          else if(((x - 64) >= 0) && ((x - 64) < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
        }
        // Three copies - medium
        else if(number == 0x06)
        {
          if((x >= 0) && (x < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
          else if(((x - 32) >= 0) && ((x - 32) < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
          else if(((x - 64) >= 0) && ((x - 64) < (1 << size)))
            ourMissleMaskTable[0][number][size][x % 160] = true;
        }
      }

      // Copy data into wrap-around area
      for(x = 0; x < 160; ++x)
        ourMissleMaskTable[0][number][size][x + 160] = 
          ourMissleMaskTable[0][number][size][x];
    }
  }

  // Now, copy data for alignments of 1, 2 and 3
  for(uLong align = 1; align < 4; ++align)
  {
    for(number = 0; number < 8; ++number)
    {
      for(size = 0; size < 4; ++size)
      {
        for(x = 0; x < 320; ++x)
        {
          ourMissleMaskTable[align][number][size][x] = 
            ourMissleMaskTable[0][number][size][(x + 320 - align) % 320];
        }
      }
    }
  }
}

//============================================================================
// Compute the player mask table
//============================================================================
void TIA::computePlayerMaskTable()
{
  // First, calculate masks for alignment 0
  Long x, mode;

  // Set the player mask table to all zeros
  for(mode = 0; mode < 8; ++mode)
    for(x = 0; x < 160; ++x)
      ourPlayerMaskTable[0][mode][x] = 0x00;

  // Now, compute the player mask table
  for(mode = 0; mode < 8; ++mode)
  {
    for(x = 0; x < 160 + 72; ++x)
    {
      if(mode == 0x00)
      {
        if((x >= 0) && (x < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> x;
      }
      else if(mode == 0x01)
      {
        if((x >= 0) && (x < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> x;
        else if(((x - 16) >= 0) && ((x - 16) < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x - 16);
      }
      else if(mode == 0x02)
      {
        if((x >= 0) && (x < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> x;
        else if(((x - 32) >= 0) && ((x - 32) < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x - 32);
      }
      else if(mode == 0x03)
      {
        if((x >= 0) && (x < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> x;
        else if(((x - 16) >= 0) && ((x - 16) < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x - 16);
        else if(((x - 32) >= 0) && ((x - 32) < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x - 32);
      }
      else if(mode == 0x04)
      {
        if((x >= 0) && (x < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> x;
        else if(((x - 64) >= 0) && ((x - 64) < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x - 64);
      }
      else if(mode == 0x05)
      {
//      if((x >= 0) && (x < 16))
//        ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x / 2);
        if((x > 0) && (x <= 16))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> ((x - 1) / 2);
      }
      else if(mode == 0x06)
      {
        if((x >= 0) && (x < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> x;
        else if(((x - 32) >= 0) && ((x - 32) < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x - 32);
        else if(((x - 64) >= 0) && ((x - 64) < 8))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x - 64);
      }
      else if(mode == 0x07)
      {
//      if((x >= 0) && (x < 32))
//        ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> (x / 4);
        if((x > 0) && (x <= 32))
          ourPlayerMaskTable[0][mode][x % 160] = 0x80 >> ((x - 1) / 4);
      }
    }

    // Copy data into wrap-around area
    for(x = 0; x < 160; ++x)
      ourPlayerMaskTable[0][mode][x + 160] = ourPlayerMaskTable[0][mode][x];
  }

  // Now, copy data for alignments of 1, 2 and 3
  for(uLong align = 1; align < 4; ++align)
  {
    for(mode = 0; mode < 8; ++mode)
    {
      for(x = 0; x < 320; ++x)
      {
        ourPlayerMaskTable[align][mode][x] =
            ourPlayerMaskTable[0][mode][(x + 320 - align) % 320];
      }
    }
  }
}

//============================================================================
// Compute the player reflect table
//============================================================================
void TIA::computePlayerReflectTable()
{
  for(uWord i = 0; i < 256; ++i)
  {
    uByte r = 0;

    for(uWord t = 1; t <= 128; t *= 2)
    {
      r = (r << 1) | ((i & t) ? 0x01 : 0x00);
    }

    ourPlayerReflectTable[i] = r;
  } 
}

//============================================================================
// Compute playfield mask table
//============================================================================
void TIA::computePlayfieldMaskTable()
{
  Long x;

  // Compute playfield mask table for non-reflected mode
  for(x = 0; x < 160; ++x)
  {
    if(x < 16)
      ourPlayfieldTable[0][x] = 0x00001 << (x / 4);
    else if(x < 48)
      ourPlayfieldTable[0][x] = 0x00800 >> ((x - 16) / 4);
    else if(x < 80) 
      ourPlayfieldTable[0][x] = 0x01000 << ((x - 48) / 4);
    else if(x < 96) 
      ourPlayfieldTable[0][x] = 0x00001 << ((x - 80) / 4);
    else if(x < 128)
      ourPlayfieldTable[0][x] = 0x00800 >> ((x - 96) / 4);
    else if(x < 160) 
      ourPlayfieldTable[0][x] = 0x01000 << ((x - 128) / 4);
  }

  // Compute playfield mask table for reflected mode
  for(x = 0; x < 160; ++x)
  {
    if(x < 16)
      ourPlayfieldTable[1][x] = 0x00001 << (x / 4);
    else if(x < 48)
      ourPlayfieldTable[1][x] = 0x00800 >> ((x - 16) / 4);
    else if(x < 80) 
      ourPlayfieldTable[1][x] = 0x01000 << ((x - 48) / 4);
    else if(x < 112) 
      ourPlayfieldTable[1][x] = 0x80000 >> ((x - 80) / 4);
    else if(x < 144) 
      ourPlayfieldTable[1][x] = 0x00010 << ((x - 112) / 4);
    else if(x < 160) 
      ourPlayfieldTable[1][x] = 0x00008 >> ((x - 144) / 4);
  }
}

//============================================================================
// Helper function for the updateFrame method
//============================================================================
inline void TIA::updateFrameHelper(uLong clock)
{
  // Update frame one scanline at a time
  do
  {
    // Compute the number of clocks we're going to update
    uLong clocksToUpdate = 0;

    Long clocksFromStartOfScanLine = 228 - myClockToEndOfScanLine;

    // See if we're updating more than the current scanline
    if((myClockToEndOfScanLine + myClockAtLastUpdate) <= clock)
    {
      // Yes, we have more than one scanline to update so finish current one
      clocksToUpdate = myClockToEndOfScanLine;
      myClockToEndOfScanLine = 228;
      myClockAtLastUpdate += clocksToUpdate;
    }
    else
    {
      // No, so do as much of the current scanline as possible
      clocksToUpdate = clock - myClockAtLastUpdate;
      myClockToEndOfScanLine -= clocksToUpdate;
      myClockAtLastUpdate = clock;
    }

    // Reset color clock position if it has reached the right side
    if(clocksFromStartOfScanLine >= (228 - HBLANKOFFSET))
    {
      clocksFromStartOfScanLine = 0;
    }

    // Skip over as many horizontal blank clocks as we can
    if(clocksFromStartOfScanLine < HBLANK)
    {
      uLong tmp = (HBLANK - clocksFromStartOfScanLine) < (Long)clocksToUpdate ? 
          (HBLANK - clocksFromStartOfScanLine) : clocksToUpdate;
      clocksFromStartOfScanLine += tmp;
      clocksToUpdate -= tmp;

    // TODO: make this cleaner plus is seems that it can be < 0 in some cases?
    if(clocksToUpdate == 0)
      break;
    }

    Word x = clocksFromStartOfScanLine - HBLANK;

    // Calculate the ending frame pointer value
    uByte* ending = myFramePointer + clocksToUpdate;
    
    // See if we're in the vertical blank region
    if(myVBLANK & 0x02)
    {
      memset(myFramePointer, 0, clocksToUpdate);
    }
    // Handle all other possible combinations
    else
    {
      switch(myEnabledObjects)
      {
        case 0:
        {
          memset(myFramePointer, myCOLUBK, clocksToUpdate);
          break;
        }

        case myPFBit:    // Playfield is enabled
        {
          uLong* mask = &myCurrentPFMask[x];

          if(myCTRLPF & 0x02)
          {
            // Update a uByte at a time until reaching a uLong boundary
            for(; ((int)myFramePointer & 0x03) && (myFramePointer < ending); 
                ++myFramePointer, ++mask, ++x)
            {
              *myFramePointer = (myCurrentPF & *mask) ? 
                  (x < 80 ? myCOLUP0 : myCOLUP1) : myCOLUBK;
            }

            // Now, update a uLong at a time
            for(; myFramePointer < ending; 
                myFramePointer += 4, mask += 4, x += 4)
            {
              *((uLong*)myFramePointer) = (myCurrentPF & *mask) ?
                  (x < 80 ? myCOLUP0 : myCOLUP1) : myCOLUBK;
            }
          }
          else
          {
            // Update a uByte at a time until reaching a uLong boundary
            for(; ((int)myFramePointer & 0x03) && (myFramePointer < ending);
                ++myFramePointer, ++mask)
            {
              *myFramePointer = (myCurrentPF & *mask) ? myCOLUPF : myCOLUBK;
            }

            // Now, update a uLong at a time
            for(; myFramePointer < ending; myFramePointer += 4, mask += 4)
            {
              *((uLong*)myFramePointer) = (myCurrentPF & *mask) ? 
                  myCOLUPF : myCOLUBK;
            }
          }
          break;
        }

        case myP0Bit:    // Player 0 is enabled
        {
          uByte* mP0 = &myCurrentP0Mask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mP0)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mP0 += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = (myCurrentGRP0 & *mP0) ? myCOLUP0 : myCOLUBK;
              ++mP0; ++myFramePointer;
            }
          }
          break;
        }

        case myP1Bit:    // Player 1 is enabled
        {
          uByte* mP1 = &myCurrentP1Mask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mP1)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mP1 += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = (myCurrentGRP1 & *mP1) ? myCOLUP1 : myCOLUBK;
              ++mP1; ++myFramePointer;
            }
          }
          break;
        }

        case myM0Bit:    // Missle 0 is enabled
        {
          uByte* mM0 = &myCurrentM0Mask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mM0)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mM0 += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = *mM0 ? myCOLUP0 : myCOLUBK;
              ++mM0; ++myFramePointer;
            }
          }
          break;
        }

        case myM1Bit:    // Missle 1 is enabled
        {
          uByte* mM1 = &myCurrentM1Mask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mM1)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mM1 += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = *mM1 ? myCOLUP1 : myCOLUBK;
              ++mM1; ++myFramePointer;
            }
          }
          break;
        }

        case myBLBit:    // Ball is enabled
        {
          uByte* mBL = &myCurrentBLMask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mBL)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mBL += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = *mBL ? myCOLUPF : myCOLUBK;
              ++mBL; ++myFramePointer;
            }
          }
          break;
        }

        case myP0Bit | myP1Bit:    // Player 0 and 1 are enabled
        {
          uByte* mP0 = &myCurrentP0Mask[x];
          uByte* mP1 = &myCurrentP1Mask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mP0 && !*(uLong*)mP1)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mP0 += 4; mP1 += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = (myCurrentGRP0 & *mP0) ? 
                  myCOLUP0 : ((myCurrentGRP1 & *mP1) ? myCOLUP1 : myCOLUBK);

              if((myCurrentGRP0 & *mP0) && (myCurrentGRP1 & *mP1))
                myCollision |= ourCollisionTable[myP0Bit | myP1Bit];

              ++mP0; ++mP1; ++myFramePointer;
            }
          }
          break;
        }

        case myM0Bit | myM1Bit:    // Missle 0 and 1 are enabled
        {
          uByte* mM0 = &myCurrentM0Mask[x];
          uByte* mM1 = &myCurrentM1Mask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mM0 && !*(uLong*)mM1)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mM0 += 4; mM1 += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = *mM0 ? myCOLUP0 : (*mM1 ? myCOLUP1 : myCOLUBK);

              if(*mM0 && *mM1)
                myCollision |= ourCollisionTable[myM0Bit | myM1Bit];

              ++mM0; ++mM1; ++myFramePointer;
            }
          }
          break;
        }

        case myBLBit | myM0Bit:    // Ball and Missle 0 are enabled
        {
          uByte* mBL = &myCurrentBLMask[x];
          uByte* mM0 = &myCurrentM0Mask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mBL && !*(uLong*)mM0)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mBL += 4; mM0 += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = (myCTRLPF & 0x04) ?
                  (*mBL ? myCOLUPF : (*mM0 ? myCOLUP0 : myCOLUBK)) :
                  (*mM0 ? myCOLUP0 : (*mBL ? myCOLUPF : myCOLUBK));

              if(*mBL && *mM0)
                myCollision |= ourCollisionTable[myBLBit | myM0Bit];

              ++mBL; ++mM0; ++myFramePointer;
            }
          }
          break;
        }

        case myBLBit | myM1Bit:    // Ball and Missle 1 are enabled
        {
          uByte* mBL = &myCurrentBLMask[x];
          uByte* mM1 = &myCurrentM1Mask[x];

          while(myFramePointer < ending)
          {
            if(!((int)myFramePointer & 0x03) && !*(uLong*)mBL && !*(uLong*)mM1)
            {
              *(uLong*)myFramePointer = myCOLUBK;
              mBL += 4; mM1 += 4; myFramePointer += 4;
            }
            else
            {
              *myFramePointer = (myCTRLPF & 0x04) ?
                  (*mBL ? myCOLUPF : (*mM1 ? myCOLUP1 : myCOLUBK)) :
                  (*mM1 ? myCOLUP1 : (*mBL ? myCOLUPF : myCOLUBK));

              if(*mBL && *mM1)
                myCollision |= ourCollisionTable[myBLBit | myM1Bit];

              ++mBL; ++mM1; ++myFramePointer;
            }
          }
          break;
        }

        case myPFBit | myP0Bit:    // Playfield and Player 0 are enabled
        {
          uLong* mPF = &myCurrentPFMask[x];
          uByte* mP0 = &myCurrentP0Mask[x];

          if(myCTRLPF & 0x02)
          {
            for(; myFramePointer < ending; ++myFramePointer, ++x, ++mPF, ++mP0)
            {
              *myFramePointer = (myCTRLPF & 0x04) ?
                  ((myCurrentPF & *mPF) ? 
                      (x < 80 ? myCOLUP0 : myCOLUP1) : 
                      ((myCurrentGRP0 & *mP0) ? myCOLUP0 : myCOLUBK)) :
                  ((myCurrentGRP0 & *mP0) ?
                      myCOLUP0 : 
                      ((myCurrentPF & *mPF) ? 
                          (x < 80 ? myCOLUP0 : myCOLUP1) : myCOLUBK));

              if((myCurrentPF & *mPF) && (myCurrentGRP0 & *mP0))
                myCollision |= ourCollisionTable[myPFBit | myP0Bit];
            }
          }
          else
          {
            while(myFramePointer < ending)
            {
              if(!((int)myFramePointer & 0x03) && !*(uLong*)mP0)
              {
                *(uLong*)myFramePointer = (myCurrentPF & *mPF) ? 
                    myCOLUPF : myCOLUBK;
                mPF += 4; mP0 += 4; myFramePointer += 4;
              }
              else
              {
                *myFramePointer = (myCTRLPF & 0x04) ?
                  ((myCurrentPF & *mPF) ? myCOLUPF : 
                      ((myCurrentGRP0 & *mP0) ? myCOLUP0 : myCOLUBK)) :
                  ((myCurrentGRP0 & *mP0) ? 
                      myCOLUP0 : ((myCurrentPF & *mPF) ? myCOLUPF : myCOLUBK));

                if((myCurrentPF & *mPF) && (myCurrentGRP0 & *mP0))
                  myCollision |= ourCollisionTable[myPFBit | myP0Bit];

                ++mPF; ++mP0; ++myFramePointer;
              }
            }
          }
          break;
        }

        case myPFBit | myP1Bit:    // Playfield and Player 1 are enabled
        {
          uLong* mPF = &myCurrentPFMask[x];
          uByte* mP1 = &myCurrentP1Mask[x];

          if(myCTRLPF & 0x02)
          {
            for(; myFramePointer < ending; ++myFramePointer, ++x, ++mPF, ++mP1)
            {
              *myFramePointer = (myCTRLPF & 0x04) ?
                  ((myCurrentPF & *mPF) ? 
                      (x < 80 ? myCOLUP0 : myCOLUP1) : 
                      ((myCurrentGRP1 & *mP1) ? myCOLUP1 : myCOLUBK)) :
                  ((myCurrentGRP1 & *mP1) ?
                      myCOLUP1 : 
                      ((myCurrentPF & *mPF) ? 
                          (x < 80 ? myCOLUP0 : myCOLUP1) : myCOLUBK));

              if((myCurrentPF & *mPF) && (myCurrentGRP1 & *mP1))
                myCollision |= ourCollisionTable[myPFBit | myP1Bit];
            }
          }
          else
          {
            while(myFramePointer < ending)
            {
              if(!((int)myFramePointer & 0x03) && !*(uLong*)mP1)
              {
                *(uLong*)myFramePointer = (myCurrentPF & *mPF) ? 
                    myCOLUPF : myCOLUBK;
                mPF += 4; mP1 += 4; myFramePointer += 4;
              }
              else
              {
                *myFramePointer = (myCTRLPF & 0x04) ?
                  ((myCurrentPF & *mPF) ? myCOLUPF : 
                      ((myCurrentGRP1 & *mP1) ? myCOLUP1 : myCOLUBK)) :
                  ((myCurrentGRP1 & *mP1) ? 
                      myCOLUP1 : ((myCurrentPF & *mPF) ? myCOLUPF : myCOLUBK));

                if((myCurrentPF & *mPF) && (myCurrentGRP1 & *mP1))
                  myCollision |= ourCollisionTable[myPFBit | myP1Bit];

                ++mPF; ++mP1; ++myFramePointer;
              }
            }
          }
          break;
        }

        case myPFBit | myM0Bit:    // Playfield and Missle 0 are enabled
        {
          uLong* mPF = &myCurrentPFMask[x];
          uByte* mM0 = &myCurrentM0Mask[x];

          if(myCTRLPF & 0x02)
          {
            for(; myFramePointer < ending; ++myFramePointer, ++x, ++mPF, ++mM0)
            {
              *myFramePointer = (myCTRLPF & 0x04) ?
                  ((myCurrentPF & *mPF) ? 
                      (x < 80 ? myCOLUP0 : myCOLUP1) : 
                      (*mM0 ? myCOLUP0 : myCOLUBK)) :
                  (*mM0 ? 
                      myCOLUP0 : ((myCurrentPF & *mPF) ? 
                          (x < 80 ? myCOLUP0 : myCOLUP1) : myCOLUBK));

              if((myCurrentPF & *mPF) && *mM0)
                myCollision |= ourCollisionTable[myPFBit | myM0Bit];
            }
          }
          else
          {
            while(myFramePointer < ending)
            {
              if(!((int)myFramePointer & 0x03) && !*(uLong*)mM0)
              {
                *(uLong*)myFramePointer = (myCurrentPF & *mPF) ? 
                    myCOLUPF : myCOLUBK;
                mPF += 4; mM0 += 4; myFramePointer += 4;
              }
              else
              {
                *myFramePointer = (myCTRLPF & 0x04) ?
                  ((myCurrentPF & *mPF) ? 
                      myCOLUPF : (*mM0 ? myCOLUP0 : myCOLUBK)) :
                  (*mM0 ? 
                      myCOLUP0 : ((myCurrentPF & *mPF) ? myCOLUPF : myCOLUBK));

                if((myCurrentPF & *mPF) && *mM0)
                  myCollision |= ourCollisionTable[myPFBit | myM0Bit];

                ++mPF; ++mM0; ++myFramePointer;
              }
            }
          }
          break;
        }

        case myPFBit | myM1Bit:    // Playfield and Missle 1 are enabled
        {
          uLong* mPF = &myCurrentPFMask[x];
          uByte* mM1 = &myCurrentM1Mask[x];

          if(myCTRLPF & 0x02)
          {
            for(; myFramePointer < ending; ++myFramePointer, ++x, ++mPF, ++mM1)
            {
              *myFramePointer = (myCTRLPF & 0x04) ?
                  ((myCurrentPF & *mPF) ? 
                      (x < 80 ? myCOLUP0 : myCOLUP1) : 
                      (*mM1 ? myCOLUP1 : myCOLUBK)) :
                  (*mM1 ? 
                      myCOLUP1 : ((myCurrentPF & *mPF) ? 
                          (x < 80 ? myCOLUP0 : myCOLUP1) : myCOLUBK));

              if((myCurrentPF & *mPF) && *mM1)
                myCollision |= ourCollisionTable[myPFBit | myM1Bit];
            }
          }
          else
          {
            while(myFramePointer < ending)
            {
              if(!((int)myFramePointer & 0x03) && !*(uLong*)mM1)
              {
                *(uLong*)myFramePointer = (myCurrentPF & *mPF) ? 
                    myCOLUPF : myCOLUBK;
                mPF += 4; mM1 += 4; myFramePointer += 4;
              }
              else
              {
                *myFramePointer = (myCTRLPF & 0x04) ?
                  ((myCurrentPF & *mPF) ? 
                      myCOLUPF : (*mM1 ? myCOLUP1 : myCOLUBK)) :
                  (*mM1 ? 
                      myCOLUP1 : ((myCurrentPF & *mPF) ? myCOLUPF : myCOLUBK));

                if((myCurrentPF & *mPF) && *mM1)
                  myCollision |= ourCollisionTable[myPFBit | myM1Bit];

                ++mPF; ++mM1; ++myFramePointer;
              }
            }
          }
          break;
        }

        case myPFBit | myBLBit:    // Playfield and Ball are enabled
        {
          uLong* mPF = &myCurrentPFMask[x];
          uByte* mBL = &myCurrentBLMask[x];

          if(myCTRLPF & 0x02)
          {
            for(; myFramePointer < ending; ++myFramePointer, ++x, ++mPF, ++mBL)
            {
              *myFramePointer = (myCurrentPF & *mPF) ? 
                  (x < 80 ? myCOLUP0 : myCOLUP1) : (*mBL ? myCOLUPF : myCOLUBK);

              if((myCurrentPF & *mPF) && *mBL)
                myCollision |= ourCollisionTable[myPFBit | myBLBit];
            }
          }
          else
          {
            while(myFramePointer < ending)
            {
              if(!((int)myFramePointer & 0x03) && !*(uLong*)mBL)
              {
                *(uLong*)myFramePointer = (myCurrentPF & *mPF) ? 
                    myCOLUPF : myCOLUBK;
                mPF += 4; mBL += 4; myFramePointer += 4;
              }
              else
              {
                *myFramePointer = ((myCurrentPF & *mPF) || *mBL) ?
                    myCOLUPF : myCOLUBK;

                if((myCurrentPF & *mPF) && *mBL)
                  myCollision |= ourCollisionTable[myPFBit | myBLBit];

                ++mPF; ++mBL; ++myFramePointer;
              }
            }
          }
          break;
        }

        default:
        {
          for(; myFramePointer < ending; ++myFramePointer, ++x)
          {
            uByte collision = 0;
 
            *myFramePointer = myCOLUBK;

            if(!(myCTRLPF & 0x04))
            {
              if(myCurrentPF & myCurrentPFMask[x])
              {
                *myFramePointer = (myCTRLPF & 2) ? 
                    (x < 80 ? myCOLUP0 : myCOLUP1) : myCOLUPF;
                collision |= myPFBit;
              }

              // Draw the ball
              if((myEnabledObjects & myBLBit) && myCurrentBLMask[x])
              {
                *myFramePointer = myCOLUPF;
                collision |= myBLBit;
              }
            }

            // Draw player 1
            if(myCurrentGRP1 & myCurrentP1Mask[x])
            {
              *myFramePointer = myCOLUP1;
              collision |= myP1Bit;
            }

            // Draw missle 1
            if((myEnabledObjects & myM1Bit) && myCurrentM1Mask[x])
            {
              *myFramePointer = myCOLUP1;
              collision |= myM1Bit;
            }

            // Draw player 0
            if(myCurrentGRP0 & myCurrentP0Mask[x])
            {
              *myFramePointer = myCOLUP0;
              collision |= myP0Bit;
            }

            // Draw missle 0
            if((myEnabledObjects & myM0Bit) && myCurrentM0Mask[x])
            {
              *myFramePointer = myCOLUP0;
              collision |= myM0Bit;
            }

            if(myCTRLPF & 0x04)
            {
              if(myCurrentPF & myCurrentPFMask[x])
              {
                *myFramePointer = (myCTRLPF & 2) ? 
                    (x < 80 ? myCOLUP0 : myCOLUP1) : myCOLUPF;
                collision |= myPFBit;
              }

              // Draw the ball
              if((myEnabledObjects & myBLBit) && myCurrentBLMask[x])
              {
                *myFramePointer = myCOLUPF;
                collision |= myBLBit;
              }
            }
  
            // Update the collision register
            myCollision = myCollision | ourCollisionTable[collision];
          }
          break;  
        }
      }
    }
    myFramePointer = ending;
  } 
  while(myClockAtLastUpdate < clock);
}

//============================================================================
// Update the current frame buffer to the specified color clock
//============================================================================
inline void TIA::updateFrame(uLong clock)
{
  // See if we're in the nondisplayable portion of the screen
  if(!myDrawFrame || (clock < myClockStartDisplay) || 
      (myClockAtLastUpdate >= myClockStopDisplay))
  {
    return;
  }

  // Truncate the number of cycles to update to the stop display point
  if(clock > myClockStopDisplay)
  {
    clock = myClockStopDisplay;
  }

  Long x = (228 - myClockToEndOfScanLine) - HBLANK;

  // See if we need to handle a latch point
  if(x & 0x03)
  {
    // Yes, we do so calculate latch point
    uLong lp = myClockAtLastUpdate + 4 - (x & 0x03);

    if(lp <= clock)
    {
      updateFrameHelper(lp);
      myCurrentPF = myPF;
    }
  }
  else
  {
    // No need to handle latch point we are at one now so latch
    myCurrentPF = myPF;
  }

  // Set the PF enabled bit if something is enabled in the playfield
  if(myCurrentPF != 0)
    myEnabledObjects |= myPFBit;
  else
    myEnabledObjects &= ~myPFBit;

  // Update the rest of the frame
  updateFrameHelper(clock);
}

//////////////////////////////////////////////////////////////////////////////
// Called to waste CPU cycles until current scanline is finished
//////////////////////////////////////////////////////////////////////////////
inline void TIA::waitHorizontalSync()
{
  uLong cyclesToEndOfLine = 76 - ((mySystem.m6507().cycles() - 
      (myClockWhenFrameStarted / 3)) % 76);

  if(cyclesToEndOfLine < 76)
  {
    mySystem.m6507().wasteCycles(cyclesToEndOfLine);
  }
}

//============================================================================
// Answer byte at the given address
//============================================================================
uByte TIA::peek(uWord addr)
{
  // Update frame to current color clock before we make any changes!
  updateFrame(mySystem.m6507().cycles() * 3);

  switch(addr & 0x000f)
  {
    case 0x00:    // CXM0P
      return ((myCollision & 0x0001) ? 0x80 : 0x00) | 
          ((myCollision & 0x0002) ? 0x40 : 0x00);

    case 0x01:    // CXM1P
      return ((myCollision & 0x0004) ? 0x80 : 0x00) | 
          ((myCollision & 0x0008) ? 0x40 : 0x00) | 0x01;

    case 0x02:    // CXP0FB
      return ((myCollision & 0x0010) ? 0x80 : 0x00) | 
          ((myCollision & 0x0020) ? 0x40 : 0x00) | 0x02;

    case 0x03:    // CXP1FB
      return ((myCollision & 0x0040) ? 0x80 : 0x00) | 
          ((myCollision & 0x0080) ? 0x40 : 0x00) | 0x03;

    case 0x04:    // CXM0FB
      return ((myCollision & 0x0100) ? 0x80 : 0x00) | 
          ((myCollision & 0x0200) ? 0x40 : 0x00) | 0x04;

    case 0x05:    // CXM1FB
      return ((myCollision & 0x0400) ? 0x80 : 0x00) | 
          ((myCollision & 0x0800) ? 0x40 : 0x00) | 0x05;

    case 0x06:    // CXBLPF
      return ((myCollision & 0x1000) ? 0x80 : 0x00) | 0x06;

    case 0x07:    // CXPPMM
      return ((myCollision & 0x2000) ? 0x80 : 0x00) | 
          ((myCollision & 0x4000) ? 0x40 : 0x00) | 0x07;

    case 0x08:    // INPT0
      return (mySystem.controller(0).readTIA() & 0x01) ? 0xff : 0x7f;

    case 0x09:    // INPT1
      return (mySystem.controller(0).readTIA() & 0x02) ? 0xff : 0x7f;

    case 0x0a:    // INPT2
      return (mySystem.controller(1).readTIA() & 0x01) ? 0xff : 0x7f;

    case 0x0b:    // INPT3
      return (mySystem.controller(1).readTIA() & 0x02) ? 0xff : 0x7f;

    case 0x0c:    // INPT4
      return (mySystem.controller(0).readTIA() & 0x04) ? 0xff : 0x7f;

    case 0x0d:    // INPT5
      return (mySystem.controller(1).readTIA() & 0x04) ? 0xff : 0x7f;

    case 0x0e:
      return 0x0e;

    default:
      return 0x0f;
  }
}

//============================================================================
// Write value at the given address.
//============================================================================
void TIA::poke(uWord addr, uByte value)
{
  addr = addr & 0x003f;

  uLong clock = mySystem.m6507().cycles() * 3;
  uLong delay = 0;

  // See if the result of this poke needs to be delayed 
  if((addr == 0x0D) || (addr == 0x0E) || (addr == 0x0F))
  { 
    delay = 2;
  }
  else if((addr == 0x1B) || (addr == 0x1C))
  {
    delay = myPlayerDelay;
  }

  // Update frame to current CPU cycle before we make any changes!
  updateFrame(clock + delay);
 
  switch(addr)
  {
    case 0x00:    // Vertical sync set-clear
    {
      myVSYNC = value;

      if(myVSYNC & 0x02)
      {
        // Indicate when the VSYNC should be finished.  This should
        // really be 3 * 228 according to Atari's documentation, however,
        // some games don't supply the full 3 scanlines of VSYNC.
        myVSYNCFinishClock = clock + 228;
      }
      else if(!(myVSYNC & 0x02) && (clock >= myVSYNCFinishClock))
      {
        // Setup clocks that'll be used for drawing the frame we are starting
        myClockWhenFrameStarted = myClockWhenFrameStarted + 
            228 * ((clock - myClockWhenFrameStarted) / 228);
        myClockStartDisplay = myClockWhenFrameStarted + myStartDisplayOffset;
        myClockStopDisplay = myClockWhenFrameStarted + myStopDisplayOffset;
        myClockAtLastUpdate = myClockStartDisplay;
        myClockToEndOfScanLine = 228;

        // We're no longer interested in myVSYNCFinishClock
        myVSYNCFinishClock = 0xFFFFFFFF;

        // Since we're finished with the frame tell the processor to halt
        mySystem.m6507().halt();
      }
      break;
    }

    case 0x01:    // Vertical blank set-clear
    {
      // Is the dump to ground path being set for I0, I1, I2, and I3?
      if(!(myVBLANK & 0x80) && (value & 0x80))
      {
        // Yes, so notify the controllers
        mySystem.controller(0).configureTIA(Controller::EnableDump);
        mySystem.controller(1).configureTIA(Controller::EnableDump);
      }

      // Is the dump to ground path being removed from I0, I1, I2, and I3?
      if((myVBLANK & 0x80) && !(value & 0x80))
      {
        // Yes, so notify the controllers
        mySystem.controller(0).configureTIA(Controller::DisableDump);
        mySystem.controller(1).configureTIA(Controller::DisableDump);
      }

      myVBLANK = value;
      break;
    }

    case 0x02:    // Wait for leading edge of HBLANK
    {
      // Tell the cpu to waste the necessary amount of time
      waitHorizontalSync();

      // Update frame since we changed the cpu cycles
      updateFrame(mySystem.m6507().cycles() * 3);

      // Do we need to handle the TIA M0 "bug" used by Cosmic Ark?
      if(myM0CosmicArkMotionEnabled)
      {
        myPOSM0 -= 15;

        if(myPOSM0 >= 160)
          myPOSM0 -= 160;
        else if(myPOSM0 < 0)
          myPOSM0 += 160;

        myCurrentM0Mask = &ourMissleMaskTable[myPOSM0 & 0x03]
            [myNUSIZ0 & 0x07][(myNUSIZ0 & 0x30) >> 4][160 - (myPOSM0 & 0xFC)];
      } 
      break;
    }

    case 0x03:    // Reset horizontal sync counter
    {
//      cerr << "TIA Poke: " << hex << addr << endl;
      break;
    }

    case 0x04:    // Number-size of player-missle 0
    {
      myNUSIZ0 = value;
      myCurrentP0Mask = &ourPlayerMaskTable[myPOSP0 & 0x03]
          [myNUSIZ0 & 0x07][160 - (myPOSP0 & 0xFC)];
      myCurrentM0Mask = &ourMissleMaskTable[myPOSM0 & 0x03]
          [myNUSIZ0 & 0x07][(myNUSIZ0 & 0x30) >> 4][160 - (myPOSM0 & 0xFC)];

      break;
    }

    case 0x05:    // Number-size of player-missle 1
    {
      myNUSIZ1 = value;
      myCurrentP1Mask = &ourPlayerMaskTable[myPOSP1 & 0x03]
          [myNUSIZ1 & 0x07][160 - (myPOSP1 & 0xFC)];
      myCurrentM1Mask = &ourMissleMaskTable[myPOSM1 & 0x03]
          [myNUSIZ1 & 0x07][(myNUSIZ1 & 0x30) >> 4][160 - (myPOSM1 & 0xFC)];

      break;
    }

    case 0x06:    // Color-Luminance Player 0
    {
      uLong color = (uLong)value;
      myCOLUP0 = (((((color << 8) | color) << 8) | color) << 8) | color;
      break;
    }

    case 0x07:    // Color-Luminance Player 1
    {
      uLong color = (uLong)value;
      myCOLUP1 = (((((color << 8) | color) << 8) | color) << 8) | color;
      break;
    }

    case 0x08:    // Color-Luminance Playfield
    {
      uLong color = (uLong)value;
      myCOLUPF = (((((color << 8) | color) << 8) | color) << 8) | color;
      break;
    }

    case 0x09:    // Color-Luminance Background
    {
      uLong color = (uLong)value;
      myCOLUBK = (((((color << 8) | color) << 8) | color) << 8) | color;
      break;
    }

    case 0x0A:    // Control Playfield, Ball size, Collisions
    {
      myCTRLPF = value;
      myCurrentPFMask = ourPlayfieldTable[value & 0x01];
      myCurrentBLMask = &ourBallMaskTable[myPOSBL & 0x03]
          [(myCTRLPF & 0x30) >> 4][160 - (myPOSBL & 0xFC)];
      break;
    }

    case 0x0B:    // Reflect Player 0
    {
      // See if the reflection state of the player is being changed
      if(((value & 0x08) && !myREFP0) || (!(value & 0x08) && myREFP0))
      {
        myREFP0 = (value & 0x08);
        myCurrentGRP0 = ourPlayerReflectTable[myCurrentGRP0];
      }
      break;
    }

    case 0x0C:    // Reflect Player 1
    {
      // See if the reflection state of the player is being changed
      if(((value & 0x08) && !myREFP1) || (!(value & 0x08) && myREFP1))
      {
        myREFP1 = (value & 0x08);
        myCurrentGRP1 = ourPlayerReflectTable[myCurrentGRP1];
      }
      break;
    }

    case 0x0D:    // Playfield register byte 0
    {
      myPF = (myPF & 0x000FFFF0) | ((value >> 4) & 0x0F);
      break;
    }

    case 0x0E:    // Playfield register byte 1
    {
      myPF = (myPF & 0x000FF00F) | ((uLong)value << 4);
      break;
    }

    case 0x0F:    // Playfield register byte 2
    {
      myPF = (myPF & 0x00000FFF) | ((uLong)value << 12);
      break;
    }

    case 0x10:    // Reset Player 0
    {
//      int x = ((clock + 3 - myClockWhenFrameStarted) + 1 ) % 228 ;
      int x = ((clock + 3 - myClockWhenFrameStarted) + 2) % 228 ;

      myPOSP0 = x < HBLANK ? 3 : x - HBLANK;
      myCurrentP0Mask = &ourPlayerMaskTable[myPOSP0 & 0x03]
          [myNUSIZ0 & 0x07][160 - (myPOSP0 & 0xFC)];
      break;
    }

    case 0x11:    // Reset Player 1
    {
//      int x = ((clock + 3 - myClockWhenFrameStarted) + 1) % 228 ;
      int x = ((clock + 3 - myClockWhenFrameStarted) + 2) % 228 ;

      myPOSP1 = x < HBLANK ? 3 : x - HBLANK;
      myCurrentP1Mask = &ourPlayerMaskTable[myPOSP1 & 0x03]
          [myNUSIZ1 & 0x07][160 - (myPOSP1 & 0xFC)];
      break;
    }

    case 0x12:    // Reset Missle 0
    {
//      int x = ((clock + 3 - myClockWhenFrameStarted) + 2) % 228 ;
      int x = ((clock + 3 - myClockWhenFrameStarted) + 1 ) % 228;

      myPOSM0 = x < HBLANK ? 3 : x - HBLANK;
      myCurrentM0Mask = &ourMissleMaskTable[myPOSM0 & 0x03]
          [myNUSIZ0 & 0x07][(myNUSIZ0 & 0x30) >> 4][160 - (myPOSM0 & 0xFC)];
      break;
    }

    case 0x13:    // Reset Missle 1
    {
//      int x = ((clock - myClockWhenFrameStarted) + 3 ) % 228;
      int x = ((clock + 3 - myClockWhenFrameStarted) + 1 ) % 228;

      myPOSM1 = x < HBLANK ? 3 : x - HBLANK;
      myCurrentM1Mask = &ourMissleMaskTable[myPOSM1 & 0x03]
          [myNUSIZ1 & 0x07][(myNUSIZ1 & 0x30) >> 4][160 - (myPOSM1 & 0xFC)];
      break;
    }

    case 0x14:    // Reset Ball
    {
//      int x = ((clock - myClockWhenFrameStarted) + 3 ) % 228;
      int x = ((clock + 3 - myClockWhenFrameStarted) + 1 ) % 228;

      myPOSBL = x < HBLANK ? 2 : x - HBLANK;
      myCurrentBLMask = &ourBallMaskTable[myPOSBL & 0x03]
          [(myCTRLPF & 0x30) >> 4][160 - (myPOSBL & 0xFC)];
      break;
    }

    case 0x15:    // Audio control 0
    {
      mySound->set(Sound::AUDC0, value);
      break;
    }
  
    case 0x16:    // Audio control 1
    {
      mySound->set(Sound::AUDC1, value);
      break;
    }
  
    case 0x17:    // Audio frequency 0
    {
      mySound->set(Sound::AUDF0, value);
      break;
    }
  
    case 0x18:    // Audio frequency 1
    {
      mySound->set(Sound::AUDF1, value);
      break;
    }
  
    case 0x19:    // Audio volume 0
    {
      mySound->set(Sound::AUDV0, value);
      break;
    }
  
    case 0x1A:    // Audio volume 1
    {
      mySound->set(Sound::AUDV1, value);
      break;
    }

    case 0x1B:    // Graphics Player 0
    {
      // Set player 0 graphics
      myGRP0 = value;

      // Copy player 1 graphics into its delayed register
      myDGRP1 = myGRP1;

      // Get the "current" data for GRP0 base on delay register and reflect
      uByte grp0 = myVDELP0 ? myDGRP0 : myGRP0;
      myCurrentGRP0 = myREFP0 ? ourPlayerReflectTable[grp0] : grp0; 

      // Get the "current" data for GRP1 base on delay register and reflect
      uByte grp1 = myVDELP1 ? myDGRP1 : myGRP1;
      myCurrentGRP1 = myREFP1 ? ourPlayerReflectTable[grp1] : grp1; 

      // Set enabled object bits
      if(myCurrentGRP0 != 0)
        myEnabledObjects |= myP0Bit;
      else
        myEnabledObjects &= ~myP0Bit;

      if(myCurrentGRP1 != 0)
        myEnabledObjects |= myP1Bit;
      else
        myEnabledObjects &= ~myP1Bit;

      break;
    }

    case 0x1C:    // Graphics Player 1
    {
      // Set player 1 graphics
      myGRP1 = value;

      // Copy player 0 graphics into its delayed register
      myDGRP0 = myGRP0;

      // Copy ball graphics into its delayed register
      myDENABL = myENABL;

      // Get the "current" data for GRP0 base on delay register
      uByte grp0 = myVDELP0 ? myDGRP0 : myGRP0;
      myCurrentGRP0 = myREFP0 ? ourPlayerReflectTable[grp0] : grp0; 

      // Get the "current" data for GRP1 base on delay register
      uByte grp1 = myVDELP1 ? myDGRP1 : myGRP1;
      myCurrentGRP1 = myREFP1 ? ourPlayerReflectTable[grp1] : grp1; 

      // Set enabled object bits
      if(myCurrentGRP0 != 0)
        myEnabledObjects |= myP0Bit;
      else
        myEnabledObjects &= ~myP0Bit;

      if(myCurrentGRP1 != 0)
        myEnabledObjects |= myP1Bit;
      else
        myEnabledObjects &= ~myP1Bit;

      if(myVDELBL ? myDENABL : myENABL)
        myEnabledObjects |= myBLBit;
      else
        myEnabledObjects &= ~myBLBit;

      break;
    }

    case 0x1D:    // Enable Missle 0 graphics
    {
      myENAM0 = value & 0x02;

      if(myENAM0 && !myRESMP0)
        myEnabledObjects |= myM0Bit;
      else
        myEnabledObjects &= ~myM0Bit;
      break;
    }

    case 0x1E:    // Enable Missle 1 graphics
    {
      myENAM1 = value & 0x02;

      if(myENAM1 && !myRESMP1)
        myEnabledObjects |= myM1Bit;
      else
        myEnabledObjects &= ~myM1Bit;
      break;
    }

    case 0x1F:    // Enable Ball graphics
    {
      myENABL = value & 0x02;

      if(myVDELBL ? myDENABL : myENABL)
        myEnabledObjects |= myBLBit;
      else
        myEnabledObjects &= ~myBLBit;

      break;
    }

    case 0x20:    // Horizontal Motion Player 0
    {
      myHMP0 = ourMotionTable[(value >> 4) & 0x0F];
      break;
    }

    case 0x21:    // Horizontal Motion Player 1
    {
      myHMP1 = ourMotionTable[(value >> 4) & 0x0F];
      break;
    }

    case 0x22:    // Horizontal Motion Missle 0
    {
      Byte tmp = ourMotionTable[(value >> 4) & 0x0F];

      // Should we enabled TIA M0 "bug" used for stars in Cosmic Ark?
      if((clock == (myLastHMOVEClock + 21 * 3)) &&
          (myHMM0 == ourMotionTable[7]) && (tmp == ourMotionTable[6]))
      {
        myM0CosmicArkMotionEnabled = true;
      }

      myHMM0 = tmp;
      break;
    }

    case 0x23:    // Horizontal Motion Missle 1
    {
      myHMM1 = ourMotionTable[(value >> 4) & 0x0F];
      break;
    }

    case 0x24:    // Horizontal Motion Ball
    {
      myHMBL = ourMotionTable[(value >> 4) & 0x0F];
      break;
    }

    case 0x25:    // Vertial Delay Player 0
    {
      myVDELP0 = value & 0x01;

      uByte grp0 = myVDELP0 ? myDGRP0 : myGRP0;
      myCurrentGRP0 = myREFP0 ? ourPlayerReflectTable[grp0] : grp0; 

      if(myCurrentGRP0 != 0)
        myEnabledObjects |= myP0Bit;
      else
        myEnabledObjects &= ~myP0Bit;
      break;
    }

    case 0x26:    // Vertial Delay Player 1
    {
      myVDELP1 = value & 0x01;

      uByte grp1 = myVDELP1 ? myDGRP1 : myGRP1;
      myCurrentGRP1 = myREFP1 ? ourPlayerReflectTable[grp1] : grp1; 

      if(myCurrentGRP1 != 0)
        myEnabledObjects |= myP1Bit;
      else
        myEnabledObjects &= ~myP1Bit;
      break;
    }

    case 0x27:    // Vertial Delay Ball
    {
      myVDELBL = value & 0x01;

      if(myVDELBL ? myDENABL : myENABL)
        myEnabledObjects |= myBLBit;
      else
        myEnabledObjects &= ~myBLBit;
      break;
    }

    case 0x28:    // Reset missle 0 to player 0
    {
      if(myRESMP0 && !(value & 0x02))
      {
        uWord middle;

        if((myNUSIZ0 & 0x07) == 0x05)
          middle = 8;
        else if((myNUSIZ0 & 0x07) == 0x07)
          middle = 16;
        else
          middle = 4;

        myPOSM0 = (myPOSP0 + middle) % 160;
        myCurrentM0Mask = &ourMissleMaskTable[myPOSM0 & 0x03]
            [myNUSIZ0 & 0x07][(myNUSIZ0 & 0x30) >> 4][160 - (myPOSM0 & 0xFC)];
      }

      myRESMP0 = value & 0x02;

      if(myENAM0 && !myRESMP0)
        myEnabledObjects |= myM0Bit;
      else
        myEnabledObjects &= ~myM0Bit;

      break;
    }

    case 0x29:    // Reset missle 1 to player 1
    {
      if(myRESMP1 && !(value & 0x02))
      {
        uWord middle;

        if((myNUSIZ1 & 0x07) == 0x05)
          middle = 8;
        else if((myNUSIZ1 & 0x07) == 0x07)
          middle = 16;
        else
          middle = 4;

        myPOSM1 = (myPOSP1 + middle) % 160;
        myCurrentM1Mask = &ourMissleMaskTable[myPOSM1 & 0x03]
            [myNUSIZ1 & 0x07][(myNUSIZ1 & 0x30) >> 4][160 - (myPOSM1 & 0xFC)];
      }

      myRESMP1 = value & 0x02;

      if(myENAM1 && !myRESMP1)
        myEnabledObjects |= myM1Bit;
      else
        myEnabledObjects &= ~myM1Bit;
      break;
    }

    case 0x2A:    // Apply horizontal motion
    {
      myPOSP0 += myHMP0;
      myPOSP1 += myHMP1;
      myPOSM0 += myHMM0;
      myPOSM1 += myHMM1;
      myPOSBL += myHMBL;

      if(myPOSP0 >= 160)
        myPOSP0 -= 160;
      else if(myPOSP0 < 0)
        myPOSP0 += 160;

      if(myPOSP1 >= 160)
        myPOSP1 -= 160;
      else if(myPOSP1 < 0)
        myPOSP1 += 160;

      if(myPOSM0 >= 160)
        myPOSM0 -= 160;
      else if(myPOSM0 < 0)
        myPOSM0 += 160;

      if(myPOSM1 >= 160)
        myPOSM1 -= 160;
      else if(myPOSM1 < 0)
        myPOSM1 += 160;

      if(myPOSBL >= 160)
        myPOSBL -= 160;
      else if(myPOSBL < 0)
        myPOSBL += 160;

      myCurrentBLMask = &ourBallMaskTable[myPOSBL & 0x03]
          [(myCTRLPF & 0x30) >> 4][160 - (myPOSBL & 0xFC)];


      myCurrentP0Mask = &ourPlayerMaskTable[myPOSP0 & 0x03]
          [myNUSIZ0 & 0x07][160 - (myPOSP0 & 0xFC)];
      myCurrentP1Mask = &ourPlayerMaskTable[myPOSP1 & 0x03]
          [myNUSIZ1 & 0x07][160 - (myPOSP1 & 0xFC)];

      myCurrentM0Mask = &ourMissleMaskTable[myPOSM0 & 0x03]
          [myNUSIZ0 & 0x07][(myNUSIZ0 & 0x30) >> 4][160 - (myPOSM0 & 0xFC)];
      myCurrentM1Mask = &ourMissleMaskTable[myPOSM1 & 0x03]
          [myNUSIZ1 & 0x07][(myNUSIZ1 & 0x30) >> 4][160 - (myPOSM1 & 0xFC)];

      // Remember what clock HMOVE occured at
      myLastHMOVEClock = clock;

      // Disable TIA M0 "bug" used for stars in Cosmic ark
      myM0CosmicArkMotionEnabled = false;
      break;
    }

    case 0x2b:    // Clear horizontal motion registers
    {
      myHMP0 = 0;
      myHMP1 = 0;
      myHMM0 = 0;
      myHMM1 = 0;
      myHMBL = 0;
      break;
    }

    case 0x2c:    // Clear collision latches
    {
      myCollision = 0;
      break;
    }

    default:
    {
#ifdef DEBUG_ACCESSES
      cerr << "BAD TIA Poke: " << hex << addr << endl;
#endif
      break;
    }
  }
}

//============================================================================
// Ball mask table (entries are true or false)
//============================================================================
uByte TIA::ourBallMaskTable[4][4][320];

//============================================================================
// Used to set the collision register to the correct value
//============================================================================
uWord TIA::ourCollisionTable[64];

//============================================================================
// Missle mask table (entries are true or false)
//============================================================================
uByte TIA::ourMissleMaskTable[4][8][4][320];

//============================================================================
// Used to convert value written in a motion register into
// its internal representation
//============================================================================
Byte TIA::ourMotionTable[16] = {
    0, -1, -2, -3, -4, -5, -6, -7, 8, 7, 6, 5, 4, 3, 2, 1
};

//============================================================================
// Player mask table
//============================================================================
uByte TIA::ourPlayerMaskTable[4][8][320];

//============================================================================
// Used to reflect a players graphics
//============================================================================
uByte TIA::ourPlayerReflectTable[256];

//============================================================================
// Playfield mask table for reflected and non-reflected playfields
//============================================================================
uLong TIA::ourPlayfieldTable[2][160];