mirror of
https://github.com/TomHarte/CLK.git
synced 2025-10-25 09:27:01 +00:00
Compare commits
591 Commits
2017-07-30
...
2017-11-03
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd10c42433 | ||
|
|
794437f20f | ||
|
|
23d5849cda | ||
|
|
5070a8414f | ||
|
|
5a3ca0e447 | ||
|
|
e384c50580 | ||
|
|
b9734278f6 | ||
|
|
f807a6b608 | ||
|
|
833f8c02a4 | ||
|
|
0248c6a282 | ||
|
|
218b976dbc | ||
|
|
513903890e | ||
|
|
1157bde453 | ||
|
|
46345c6a3e | ||
|
|
c13f8e5390 | ||
|
|
ad9df4bb90 | ||
|
|
e983854e71 | ||
|
|
ec999446e8 | ||
|
|
5e3e91373a | ||
|
|
c52348d8d7 | ||
|
|
9e0907ee76 | ||
|
|
9ad4025138 | ||
|
|
405f58d6a3 | ||
|
|
afbd1c425c | ||
|
|
b2c1b83fcd | ||
|
|
8d2b9a581a | ||
|
|
1825af0dd3 | ||
|
|
c2f6799f0c | ||
|
|
b5b6219cb7 | ||
|
|
185a699279 | ||
|
|
96b8f9ae9f | ||
|
|
88e2350b8f | ||
|
|
5c141af734 | ||
|
|
da580e4186 | ||
|
|
57ee09dffb | ||
|
|
7c8e830b90 | ||
|
|
ba5f668338 | ||
|
|
2c1e99858b | ||
|
|
7f2febeec9 | ||
|
|
2d7a4fe5f0 | ||
|
|
91b867a7b3 | ||
|
|
3944e734d3 | ||
|
|
ce78d9d12c | ||
|
|
edbc60a3fb | ||
|
|
6ea3ff62df | ||
|
|
88959571f1 | ||
|
|
b4583e976e | ||
|
|
92d9805f09 | ||
|
|
0c2dd62328 | ||
|
|
3f4d90d775 | ||
|
|
542ec4312f | ||
|
|
18798c9886 | ||
|
|
7aaf27389c | ||
|
|
ee179aa7bd | ||
|
|
3a05ce36de | ||
|
|
4f289ab10b | ||
|
|
78ee46270b | ||
|
|
edb632af52 | ||
|
|
19c03a08a6 | ||
|
|
44cdc124af | ||
|
|
b37787a414 | ||
|
|
53b99ea248 | ||
|
|
97a2be71e3 | ||
|
|
f623bff5c3 | ||
|
|
2511fc8401 | ||
|
|
d37ec9e5b0 | ||
|
|
95c82f5b36 | ||
|
|
ec202ed8be | ||
|
|
7190225603 | ||
|
|
52e7cabd4e | ||
|
|
064f1dfdbc | ||
|
|
f40e1fd840 | ||
|
|
e194a2a015 | ||
|
|
c39759333a | ||
|
|
edb9fd301c | ||
|
|
ea5023ac26 | ||
|
|
0fb363ea0e | ||
|
|
1cc85615d5 | ||
|
|
7b01c1bee6 | ||
|
|
35705c5345 | ||
|
|
f41da83d97 | ||
|
|
cd1e5dea4d | ||
|
|
ef605eda51 | ||
|
|
2f48ee59fa | ||
|
|
f86729c4ac | ||
|
|
5f99f4442c | ||
|
|
326857a84d | ||
|
|
5dd3945695 | ||
|
|
19eb975c73 | ||
|
|
698ffca51b | ||
|
|
fe3cc5c57c | ||
|
|
f488854720 | ||
|
|
51c0c45e04 | ||
|
|
c3e1489a8e | ||
|
|
e3420f62c6 | ||
|
|
970c80f2e3 | ||
|
|
9f4a407f94 | ||
|
|
5dda897334 | ||
|
|
3982e375e3 | ||
|
|
a8524daecb | ||
|
|
d1ce764201 | ||
|
|
8875982e1f | ||
|
|
3319a4f589 | ||
|
|
c7f27b2db4 | ||
|
|
631f630549 | ||
|
|
2a08bd9ecc | ||
|
|
f789ee4ff0 | ||
|
|
a295b42497 | ||
|
|
d8337492cc | ||
|
|
15c8debc16 | ||
|
|
67af153c16 | ||
|
|
d72dad2d1a | ||
|
|
698e4fe550 | ||
|
|
b5406b90cd | ||
|
|
05a93ba237 | ||
|
|
77548d14db | ||
|
|
b85dd608e7 | ||
|
|
231f13d810 | ||
|
|
704bfa114c | ||
|
|
44a56724cb | ||
|
|
5fbea625ae | ||
|
|
ac57b37e96 | ||
|
|
e3e9baeaa4 | ||
|
|
e071123f90 | ||
|
|
98adb01721 | ||
|
|
d6a5f9a29e | ||
|
|
0d84b4b9dd | ||
|
|
a85909198f | ||
|
|
98751e6ac8 | ||
|
|
da082673d7 | ||
|
|
35fe4d50d4 | ||
|
|
b835cb73e2 | ||
|
|
662d031e3c | ||
|
|
bf20c717fb | ||
|
|
4d4a0cf1d2 | ||
|
|
b62f3e726a | ||
|
|
82b13e98f2 | ||
|
|
9ac831b09c | ||
|
|
42616da7ff | ||
|
|
2f13517f38 | ||
|
|
fb9fd26af7 | ||
|
|
d3c385b471 | ||
|
|
96bf133924 | ||
|
|
6d6cac429d | ||
|
|
dc0b65f9c9 | ||
|
|
8882aa496f | ||
|
|
0622187ddf | ||
|
|
523e1288fa | ||
|
|
1a96cce26f | ||
|
|
a4e275e1fc | ||
|
|
6075064400 | ||
|
|
ff6e65cca9 | ||
|
|
90d2347c90 | ||
|
|
90c7056d12 | ||
|
|
fed2bc9fc9 | ||
|
|
ff510f3b84 | ||
|
|
3b12fca417 | ||
|
|
8eeb7e73cd | ||
|
|
7fd6699e0b | ||
|
|
ed70b15fc9 | ||
|
|
ff24e1de31 | ||
|
|
6547102511 | ||
|
|
d538ff5039 | ||
|
|
a49594c6a3 | ||
|
|
3544c0f014 | ||
|
|
f26fe3756c | ||
|
|
a42ca290cb | ||
|
|
da09098e49 | ||
|
|
450712f39c | ||
|
|
24b3faa427 | ||
|
|
40d11ea0e3 | ||
|
|
ab2bcb939f | ||
|
|
45499050b6 | ||
|
|
0c9197df30 | ||
|
|
a1e200cc65 | ||
|
|
8a612bb6ab | ||
|
|
e6ac939ae0 | ||
|
|
b034d4e6f8 | ||
|
|
de218611e4 | ||
|
|
615f7ce176 | ||
|
|
b306776ba9 | ||
|
|
0f85cffc78 | ||
|
|
96648df5fe | ||
|
|
2c99a2d6ec | ||
|
|
4af333d5ec | ||
|
|
a5f9869769 | ||
|
|
f10be2a18a | ||
|
|
c88d627b4e | ||
|
|
b30bb2a234 | ||
|
|
d498080eb4 | ||
|
|
334afbc710 | ||
|
|
17c13624e5 | ||
|
|
113349d272 | ||
|
|
0ced7866fc | ||
|
|
d06031dfcb | ||
|
|
3f22a71276 | ||
|
|
53a88a7e12 | ||
|
|
4a66dd9e82 | ||
|
|
522839143f | ||
|
|
b4c532c0d5 | ||
|
|
a3e2d142e3 | ||
|
|
63ee8c9d58 | ||
|
|
437023bff6 | ||
|
|
4465098157 | ||
|
|
56dd677e9c | ||
|
|
9aa150c338 | ||
|
|
fab6908129 | ||
|
|
e34d4ce903 | ||
|
|
d411827733 | ||
|
|
f1ba7755dd | ||
|
|
57bfec285f | ||
|
|
bdda701207 | ||
|
|
487fe83dca | ||
|
|
6c5a03187b | ||
|
|
97f57a3948 | ||
|
|
7d7aa2f5d5 | ||
|
|
e7ad79c79a | ||
|
|
28550c0227 | ||
|
|
6e99169348 | ||
|
|
1017bb9f6b | ||
|
|
3caa4705ca | ||
|
|
039aed1bd1 | ||
|
|
d77d7fdd78 | ||
|
|
c6e6c3fcfb | ||
|
|
ecd3350a6f | ||
|
|
fa19e2d9c2 | ||
|
|
95d360251d | ||
|
|
7af3de010e | ||
|
|
cefd421992 | ||
|
|
a914eadc85 | ||
|
|
131b340d75 | ||
|
|
e956740c56 | ||
|
|
8afd83b91f | ||
|
|
40d7a603db | ||
|
|
ee71be0e7e | ||
|
|
cde29c4bf4 | ||
|
|
e1aded0d95 | ||
|
|
1237f174fe | ||
|
|
0cbc1753b9 | ||
|
|
5cf0395936 | ||
|
|
6315c22b80 | ||
|
|
4614a56843 | ||
|
|
8f5ae4a326 | ||
|
|
8fdc5012e4 | ||
|
|
e88a51e75e | ||
|
|
49285e9caa | ||
|
|
e3f2118757 | ||
|
|
daeaa4752f | ||
|
|
5344e3098b | ||
|
|
cedb809c21 | ||
|
|
2d9efccc98 | ||
|
|
8ce46b6e49 | ||
|
|
f2699a3f2b | ||
|
|
85253a5876 | ||
|
|
911ee5a0d3 | ||
|
|
57c5b38a6d | ||
|
|
669e0caff5 | ||
|
|
b24d04fc09 | ||
|
|
ef07c33741 | ||
|
|
e559a65ede | ||
|
|
5bdd24d93f | ||
|
|
af61a7fa28 | ||
|
|
c8c1792c3f | ||
|
|
e6683e7f2d | ||
|
|
0c1714b695 | ||
|
|
dc0ca83003 | ||
|
|
2c2dd8073c | ||
|
|
4f8b89772e | ||
|
|
733ee5a5c3 | ||
|
|
fedf5a44a6 | ||
|
|
6a09022896 | ||
|
|
5b3c707959 | ||
|
|
9b21ef1507 | ||
|
|
da3e8655e9 | ||
|
|
41e4386164 | ||
|
|
b0a98bd239 | ||
|
|
42ad670ec8 | ||
|
|
58063b69a6 | ||
|
|
378f231499 | ||
|
|
f68565a33f | ||
|
|
175faebdc9 | ||
|
|
76c6b715a2 | ||
|
|
b476f06524 | ||
|
|
48290a8bbe | ||
|
|
9d9a1c341d | ||
|
|
952da1e581 | ||
|
|
a988255558 | ||
|
|
cca66ab450 | ||
|
|
bcd7a312a4 | ||
|
|
925e774015 | ||
|
|
4c15e46fd1 | ||
|
|
3c50903a2b | ||
|
|
75208b0762 | ||
|
|
f1e64169cd | ||
|
|
903a17ae11 | ||
|
|
de1c526789 | ||
|
|
b7e0f64892 | ||
|
|
148591b7f2 | ||
|
|
2105597910 | ||
|
|
3c148f5721 | ||
|
|
360c8a99a3 | ||
|
|
06e31f5102 | ||
|
|
42b5b66305 | ||
|
|
27018ba64e | ||
|
|
e208f03636 | ||
|
|
cc9d23f23b | ||
|
|
1a831bcf9b | ||
|
|
82367a2246 | ||
|
|
f18206767f | ||
|
|
3947347d88 | ||
|
|
8a37a0ff2e | ||
|
|
468770b382 | ||
|
|
6cfc3daacb | ||
|
|
aefbafa18d | ||
|
|
d12c834f9c | ||
|
|
56de5cb2b3 | ||
|
|
709257a0c5 | ||
|
|
75a9d2bb33 | ||
|
|
7b92b235e1 | ||
|
|
c196f0018f | ||
|
|
73080d6c36 | ||
|
|
9541a2a5f0 | ||
|
|
944222eba4 | ||
|
|
9d77f33611 | ||
|
|
7d132f81f7 | ||
|
|
0972f19fc5 | ||
|
|
6553bf05b4 | ||
|
|
0816d3f5a9 | ||
|
|
55055c7847 | ||
|
|
113da93796 | ||
|
|
cddcd0fb79 | ||
|
|
a366298022 | ||
|
|
4df9307d25 | ||
|
|
d7bed958b3 | ||
|
|
9038ba622e | ||
|
|
7b8bb0297a | ||
|
|
0da02d3902 | ||
|
|
334872d374 | ||
|
|
2e5ad19fe1 | ||
|
|
a10389a22c | ||
|
|
cefec7a19f | ||
|
|
7264fbb3d2 | ||
|
|
0e083e9dc6 | ||
|
|
8a7b23dc9e | ||
|
|
b7065575f3 | ||
|
|
7ea703f150 | ||
|
|
ea64125124 | ||
|
|
1011143dbe | ||
|
|
9ace6e1f71 | ||
|
|
750f2cb883 | ||
|
|
5221837be8 | ||
|
|
1576b4500b | ||
|
|
e1e9a06712 | ||
|
|
6e36f8ffa4 | ||
|
|
b0a7208cc7 | ||
|
|
eec42aa7ae | ||
|
|
6d2e969e7d | ||
|
|
5f42022c1d | ||
|
|
11d0c37506 | ||
|
|
58bad1e2a3 | ||
|
|
27d1dc5c37 | ||
|
|
e7345c7a20 | ||
|
|
186048a88e | ||
|
|
7135259cc1 | ||
|
|
4909325e79 | ||
|
|
a4ee697ed1 | ||
|
|
0f15a2f97f | ||
|
|
89ace671a4 | ||
|
|
e7db2a2f6d | ||
|
|
8c33ac71ee | ||
|
|
69914faf02 | ||
|
|
daafebe7ac | ||
|
|
2d81acb82e | ||
|
|
82ca49c840 | ||
|
|
bfe297052d | ||
|
|
ffb1a14ace | ||
|
|
7e35e44934 | ||
|
|
0c8769e335 | ||
|
|
83c7d34df2 | ||
|
|
ad3c9842d7 | ||
|
|
44dace2eef | ||
|
|
a12671010a | ||
|
|
23c149368b | ||
|
|
09716d4716 | ||
|
|
4b7c504d22 | ||
|
|
1e4f9d4eda | ||
|
|
e4f04d0977 | ||
|
|
0f75525640 | ||
|
|
edb088526f | ||
|
|
80ebc63101 | ||
|
|
cf1403bc79 | ||
|
|
fcf63a7547 | ||
|
|
9c0b75faba | ||
|
|
1f2bfc9581 | ||
|
|
14ab03d1e0 | ||
|
|
3831fbaca2 | ||
|
|
1d8edf58dd | ||
|
|
4785e316ff | ||
|
|
44da9de5b0 | ||
|
|
4ecd093891 | ||
|
|
dd4bc87d52 | ||
|
|
570d25214e | ||
|
|
f0b7e58968 | ||
|
|
0411b51582 | ||
|
|
dea782cff9 | ||
|
|
388dd99762 | ||
|
|
026101a268 | ||
|
|
734099a956 | ||
|
|
6be5851484 | ||
|
|
994179f188 | ||
|
|
6a65c7a52a | ||
|
|
0d2d3ea17c | ||
|
|
5d374ebb18 | ||
|
|
62eadbb51a | ||
|
|
ad8c8166bc | ||
|
|
a5593bec79 | ||
|
|
a1e2646301 | ||
|
|
cf810d8357 | ||
|
|
f258d6fbb2 | ||
|
|
4961fda2a9 | ||
|
|
6a6e5ae79c | ||
|
|
02d792c003 | ||
|
|
be8e7a4144 | ||
|
|
b1dbd7833a | ||
|
|
84ab05c5ef | ||
|
|
78138261c2 | ||
|
|
a4c910f1de | ||
|
|
2eed24e859 | ||
|
|
7d1023ea98 | ||
|
|
b11d142cff | ||
|
|
021ff8674e | ||
|
|
1b86bc21ab | ||
|
|
e3d1f4fe1e | ||
|
|
a7452aebff | ||
|
|
484524d781 | ||
|
|
dbf9927caf | ||
|
|
3bdedfd749 | ||
|
|
005084af3d | ||
|
|
46278ff297 | ||
|
|
d73dcbb268 | ||
|
|
390ecec3d9 | ||
|
|
41a30c147d | ||
|
|
4709ae80cb | ||
|
|
7fbb455836 | ||
|
|
745afd217f | ||
|
|
4427e9a254 | ||
|
|
2b0dcf8573 | ||
|
|
47732ffb98 | ||
|
|
d07f3216ab | ||
|
|
56d65ba6f3 | ||
|
|
895a3cbf24 | ||
|
|
d951c8c1c2 | ||
|
|
a294963e98 | ||
|
|
68c73184b1 | ||
|
|
7f824d6494 | ||
|
|
3219212f03 | ||
|
|
d90e35e5bd | ||
|
|
73f8488150 | ||
|
|
3853966a1e | ||
|
|
d63893a437 | ||
|
|
90c74043f5 | ||
|
|
600445d90a | ||
|
|
e4b405fd3d | ||
|
|
3b7ecbdf0d | ||
|
|
01efb645cb | ||
|
|
b5ec1f42d5 | ||
|
|
c839556a27 | ||
|
|
e9972aa0dd | ||
|
|
1c9a744b01 | ||
|
|
e6d4bb29d8 | ||
|
|
6c5b562d97 | ||
|
|
a7103f9333 | ||
|
|
c12425e141 | ||
|
|
89f6de1383 | ||
|
|
77da582e88 | ||
|
|
34eaf75352 | ||
|
|
ffadb04761 | ||
|
|
29288b690e | ||
|
|
25fd3f7e50 | ||
|
|
4d60b8801c | ||
|
|
3e984e75b6 | ||
|
|
9e8645ca7a | ||
|
|
caf3ac0645 | ||
|
|
0f4343cd84 | ||
|
|
192f232d3f | ||
|
|
6e4d3b8a77 | ||
|
|
8eda24261c | ||
|
|
75c59fefab | ||
|
|
4b19cf60df | ||
|
|
2b476554e7 | ||
|
|
b3788fed41 | ||
|
|
b999c1d8aa | ||
|
|
a63aa80dc9 | ||
|
|
63f57c8c4f | ||
|
|
7e6a6365c9 | ||
|
|
3dbf26ac49 | ||
|
|
f075fea78c | ||
|
|
cc8380c286 | ||
|
|
c0f0c68f4f | ||
|
|
c2bb019381 | ||
|
|
26ce6cdab2 | ||
|
|
d9097facf1 | ||
|
|
b927500487 | ||
|
|
e71eabedf9 | ||
|
|
33ed27c3ad | ||
|
|
45cbab6751 | ||
|
|
a7b74d6164 | ||
|
|
575b1dba75 | ||
|
|
a3b16b6dfa | ||
|
|
c8f4de6f11 | ||
|
|
bbb17acf3a | ||
|
|
ad3a98387f | ||
|
|
985fbf59c2 | ||
|
|
2f2071be8a | ||
|
|
6d510e4e70 | ||
|
|
8e0736fbe6 | ||
|
|
681d1e2f8d | ||
|
|
42e70ef993 | ||
|
|
039811ce6a | ||
|
|
a54ccd1969 | ||
|
|
707821ca55 | ||
|
|
d3bf8fa53b | ||
|
|
f5e2dd410e | ||
|
|
3ca9c38777 | ||
|
|
2d2cefb0b0 | ||
|
|
2fd071e45d | ||
|
|
d7a5c3f49a | ||
|
|
819761f9fb | ||
|
|
e50adf1cc8 | ||
|
|
dcab10f53e | ||
|
|
633d8965e2 | ||
|
|
f602f9b6ec | ||
|
|
f7e66dea61 | ||
|
|
bda9441620 | ||
|
|
4d5d5041df | ||
|
|
587eb3a67c | ||
|
|
6ca07f1e28 | ||
|
|
8d39a20088 | ||
|
|
4b6370eb86 | ||
|
|
c6e340a8a2 | ||
|
|
31c7153301 | ||
|
|
7e04d00cc1 | ||
|
|
9d43784c65 | ||
|
|
eca9586a0f | ||
|
|
0267bc237f | ||
|
|
2e4577f741 | ||
|
|
f5b278d683 | ||
|
|
e6854ff8db | ||
|
|
3b292273c7 | ||
|
|
cb732e5d5f | ||
|
|
2d4e202be3 | ||
|
|
64da8e17d1 | ||
|
|
08ad35efd9 | ||
|
|
58b98267fc | ||
|
|
ace71280a0 | ||
|
|
a27946102a | ||
|
|
1d99c116e7 | ||
|
|
ee27e16fb1 | ||
|
|
6ac7132799 | ||
|
|
ca42abab70 | ||
|
|
933d69a256 | ||
|
|
3b1db14817 | ||
|
|
10a5581aea | ||
|
|
3ae699964f | ||
|
|
9d953421d8 | ||
|
|
763e3b65d1 | ||
|
|
42dd27c9b1 | ||
|
|
2b168f7383 | ||
|
|
0536f089e1 | ||
|
|
3df13cddd4 | ||
|
|
e3f677fa37 | ||
|
|
c2253c1e0f | ||
|
|
5c68b6cc21 | ||
|
|
ffaa627820 | ||
|
|
f742fd5d4a | ||
|
|
69b99fe127 | ||
|
|
5a396f6787 | ||
|
|
cb0dc7b434 | ||
|
|
e28829bd1b | ||
|
|
68ceeab610 | ||
|
|
68dca9d047 | ||
|
|
d88ca151f4 | ||
|
|
3c90218c3d | ||
|
|
afd409c883 | ||
|
|
26b6c03a2a | ||
|
|
9c04d851e4 | ||
|
|
1d6fe11906 | ||
|
|
c0f1313830 | ||
|
|
fb51fadf00 | ||
|
|
5b5720fac0 | ||
|
|
d25d7d7d40 |
@@ -95,9 +95,17 @@ template <class T> class WrappedInt {
|
||||
return *static_cast<T *>(this);
|
||||
}
|
||||
|
||||
inline T &operator &=(const T &rhs) {
|
||||
length_ &= rhs.length_;
|
||||
return *static_cast<T *>(this);
|
||||
}
|
||||
|
||||
inline T operator +(const T &rhs) const { return T(length_ + rhs.length_); }
|
||||
inline T operator -(const T &rhs) const { return T(length_ - rhs.length_); }
|
||||
|
||||
inline T operator %(const T &rhs) const { return T(length_ % rhs.length_); }
|
||||
inline T operator &(const T &rhs) const { return T(length_ & rhs.length_); }
|
||||
|
||||
inline T operator -() const { return T(- length_); }
|
||||
|
||||
inline bool operator <(const T &rhs) const { return length_ < rhs.length_; }
|
||||
@@ -153,7 +161,7 @@ class HalfCycles: public WrappedInt<HalfCycles> {
|
||||
inline HalfCycles(int l) : WrappedInt<HalfCycles>(l) {}
|
||||
inline HalfCycles() : WrappedInt<HalfCycles>() {}
|
||||
|
||||
inline HalfCycles(const Cycles cycles) : WrappedInt<HalfCycles>(cycles.as_int() << 1) {}
|
||||
inline HalfCycles(const Cycles cycles) : WrappedInt<HalfCycles>(cycles.as_int() * 2) {}
|
||||
inline HalfCycles(const HalfCycles &half_cycles) : WrappedInt<HalfCycles>(half_cycles.length_) {}
|
||||
|
||||
/// @returns The number of whole cycles completely covered by this span of half cycles.
|
||||
@@ -167,6 +175,17 @@ class HalfCycles: public WrappedInt<HalfCycles> {
|
||||
length_ &= 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
Severs from @c this the effect of dividing by @c divisor — @c this will end up with
|
||||
the value of @c this modulo @c divisor and @c divided by @c divisor is returned.
|
||||
*/
|
||||
inline Cycles divide_cycles(const Cycles &divisor) {
|
||||
HalfCycles half_divisor = HalfCycles(divisor);
|
||||
Cycles result(length_ / half_divisor.length_);
|
||||
length_ %= half_divisor.length_;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
|
||||
26
ClockReceiver/ForceInline.hpp
Normal file
26
ClockReceiver/ForceInline.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// ForceInline.h
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 01/08/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef ForceInline_hpp
|
||||
#define ForceInline_hpp
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
#define forceinline
|
||||
|
||||
#else
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define forceinline __attribute__((always_inline)) inline
|
||||
#elif _MSC_VER
|
||||
#define forceinline __forceinline
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* ForceInline_h */
|
||||
60
ClockReceiver/Sleeper.hpp
Normal file
60
ClockReceiver/Sleeper.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Sleeper.h
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 20/08/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Sleeper_hpp
|
||||
#define Sleeper_hpp
|
||||
|
||||
/*!
|
||||
A sleeper is any component that sometimes requires a clock but at other times is 'asleep' — i.e. is not doing
|
||||
any clock-derived work, so needn't receive a clock. A disk controller is an archetypal example.
|
||||
|
||||
A sleeper will signal sleeps and wakes to an observer.
|
||||
|
||||
This is intended to allow for performance improvements to machines with components that can sleep. The observer
|
||||
callout is virtual so the intended use case is that a machine holds a component that might sleep. Its transitions
|
||||
into and out of sleep are sufficiently infrequent that a virtual call to announce them costs sufficiently little that
|
||||
the saved ::run_fors add up to a substantial amount.
|
||||
|
||||
By convention, sleeper components must be willing to accept ::run_for even after announcing sleep. It's a hint,
|
||||
not a command.
|
||||
*/
|
||||
class Sleeper {
|
||||
public:
|
||||
Sleeper() : sleep_observer_(nullptr) {}
|
||||
|
||||
class SleepObserver {
|
||||
public:
|
||||
/// Called to inform an observer that the component @c component has either gone to sleep or become awake.
|
||||
virtual void set_component_is_sleeping(void *component, bool is_sleeping) = 0;
|
||||
};
|
||||
|
||||
/// Registers @c observer as the new sleep observer;
|
||||
void set_sleep_observer(SleepObserver *observer) {
|
||||
sleep_observer_ = observer;
|
||||
}
|
||||
|
||||
/// @returns @c true if the component is currently sleeping; @c false otherwise.
|
||||
virtual bool is_sleeping() = 0;
|
||||
|
||||
protected:
|
||||
/// Provided for subclasses; send sleep announcements to the sleep_observer_.
|
||||
SleepObserver *sleep_observer_;
|
||||
|
||||
/*!
|
||||
Provided for subclasses; call this whenever is_sleeping might have changed, and the observer will be notified,
|
||||
if one exists.
|
||||
|
||||
@c is_sleeping will be called only if there is an observer.
|
||||
*/
|
||||
void update_sleep_observer() {
|
||||
if(!sleep_observer_) return;
|
||||
sleep_observer_->set_component_is_sleeping(this, is_sleeping());
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* Sleeper_h */
|
||||
@@ -7,7 +7,7 @@
|
||||
//
|
||||
|
||||
#include "1770.hpp"
|
||||
#include "../../Storage/Disk/Encodings/MFM.hpp"
|
||||
#include "../../Storage/Disk/Encodings/MFM/Constants.hpp"
|
||||
|
||||
using namespace WD;
|
||||
|
||||
@@ -25,29 +25,16 @@ WD1770::Status::Status() :
|
||||
busy(false) {}
|
||||
|
||||
WD1770::WD1770(Personality p) :
|
||||
Storage::Disk::Controller(8000000, 16, 300),
|
||||
crc_generator_(0x1021, 0xffff),
|
||||
interesting_event_mask_(Event::Command),
|
||||
Storage::Disk::MFMController(8000000),
|
||||
interesting_event_mask_(static_cast<int>(Event1770::Command)),
|
||||
resume_point_(0),
|
||||
delay_time_(0),
|
||||
index_hole_count_target_(-1),
|
||||
is_awaiting_marker_value_(false),
|
||||
data_mode_(DataMode::Scanning),
|
||||
delegate_(nullptr),
|
||||
personality_(p),
|
||||
head_is_loaded_(false) {
|
||||
set_is_double_density(false);
|
||||
posit_event(Event::Command);
|
||||
}
|
||||
|
||||
void WD1770::set_is_double_density(bool is_double_density) {
|
||||
is_double_density_ = is_double_density;
|
||||
Storage::Time bit_length;
|
||||
bit_length.length = 1;
|
||||
bit_length.clock_rate = is_double_density ? 500000 : 250000;
|
||||
set_expected_bit_length(bit_length);
|
||||
|
||||
if(!is_double_density) is_awaiting_marker_value_ = false;
|
||||
posit_event(static_cast<int>(Event1770::Command));
|
||||
}
|
||||
|
||||
void WD1770::set_register(int address, uint8_t value) {
|
||||
@@ -60,7 +47,7 @@ void WD1770::set_register(int address, uint8_t value) {
|
||||
});
|
||||
} else {
|
||||
command_ = value;
|
||||
posit_event(Event::Command);
|
||||
posit_event(static_cast<int>(Event1770::Command));
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -88,7 +75,7 @@ uint8_t WD1770::get_register(int address) {
|
||||
switch(status_.type) {
|
||||
case Status::One:
|
||||
status |=
|
||||
(get_is_track_zero() ? Flag::TrackZero : 0) |
|
||||
(get_drive().get_is_track_zero() ? Flag::TrackZero : 0) |
|
||||
(status_.seek_error ? Flag::SeekError : 0);
|
||||
// TODO: index hole
|
||||
break;
|
||||
@@ -104,11 +91,11 @@ uint8_t WD1770::get_register(int address) {
|
||||
}
|
||||
|
||||
if(!has_motor_on_line()) {
|
||||
status |= get_drive_is_ready() ? 0 : Flag::NotReady;
|
||||
status |= get_drive().get_is_ready() ? 0 : Flag::NotReady;
|
||||
if(status_.type == Status::One)
|
||||
status |= (head_is_loaded_ ? Flag::HeadLoaded : 0);
|
||||
} else {
|
||||
status |= (get_motor_on() ? Flag::MotorOn : 0);
|
||||
status |= (get_drive().get_motor_on() ? Flag::MotorOn : 0);
|
||||
if(status_.type == Status::One)
|
||||
status |= (status_.spin_up ? Flag::SpinUp : 0);
|
||||
}
|
||||
@@ -128,151 +115,27 @@ void WD1770::run_for(const Cycles cycles) {
|
||||
Storage::Disk::Controller::run_for(cycles);
|
||||
|
||||
if(delay_time_) {
|
||||
unsigned int number_of_cycles = (unsigned int)cycles.as_int();
|
||||
unsigned int number_of_cycles = static_cast<unsigned int>(cycles.as_int());
|
||||
if(delay_time_ <= number_of_cycles) {
|
||||
delay_time_ = 0;
|
||||
posit_event(Event::Timer);
|
||||
posit_event(static_cast<int>(Event1770::Timer));
|
||||
} else {
|
||||
delay_time_ -= number_of_cycles;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WD1770::process_input_bit(int value, unsigned int cycles_since_index_hole) {
|
||||
if(data_mode_ == DataMode::Writing) return;
|
||||
|
||||
shift_register_ = (shift_register_ << 1) | value;
|
||||
bits_since_token_++;
|
||||
|
||||
if(data_mode_ == DataMode::Scanning) {
|
||||
Token::Type token_type = Token::Byte;
|
||||
if(!is_double_density_) {
|
||||
switch(shift_register_ & 0xffff) {
|
||||
case Storage::Encodings::MFM::FMIndexAddressMark:
|
||||
token_type = Token::Index;
|
||||
crc_generator_.reset();
|
||||
crc_generator_.add(latest_token_.byte_value = Storage::Encodings::MFM::IndexAddressByte);
|
||||
break;
|
||||
case Storage::Encodings::MFM::FMIDAddressMark:
|
||||
token_type = Token::ID;
|
||||
crc_generator_.reset();
|
||||
crc_generator_.add(latest_token_.byte_value = Storage::Encodings::MFM::IDAddressByte);
|
||||
break;
|
||||
case Storage::Encodings::MFM::FMDataAddressMark:
|
||||
token_type = Token::Data;
|
||||
crc_generator_.reset();
|
||||
crc_generator_.add(latest_token_.byte_value = Storage::Encodings::MFM::DataAddressByte);
|
||||
break;
|
||||
case Storage::Encodings::MFM::FMDeletedDataAddressMark:
|
||||
token_type = Token::DeletedData;
|
||||
crc_generator_.reset();
|
||||
crc_generator_.add(latest_token_.byte_value = Storage::Encodings::MFM::DeletedDataAddressByte);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch(shift_register_ & 0xffff) {
|
||||
case Storage::Encodings::MFM::MFMIndexSync:
|
||||
bits_since_token_ = 0;
|
||||
is_awaiting_marker_value_ = true;
|
||||
|
||||
token_type = Token::Sync;
|
||||
latest_token_.byte_value = Storage::Encodings::MFM::MFMIndexSyncByteValue;
|
||||
break;
|
||||
case Storage::Encodings::MFM::MFMSync:
|
||||
bits_since_token_ = 0;
|
||||
is_awaiting_marker_value_ = true;
|
||||
crc_generator_.set_value(Storage::Encodings::MFM::MFMPostSyncCRCValue);
|
||||
|
||||
token_type = Token::Sync;
|
||||
latest_token_.byte_value = Storage::Encodings::MFM::MFMSyncByteValue;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(token_type != Token::Byte) {
|
||||
latest_token_.type = token_type;
|
||||
bits_since_token_ = 0;
|
||||
posit_event(Event::Token);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(bits_since_token_ == 16) {
|
||||
latest_token_.type = Token::Byte;
|
||||
latest_token_.byte_value = (uint8_t)(
|
||||
((shift_register_ & 0x0001) >> 0) |
|
||||
((shift_register_ & 0x0004) >> 1) |
|
||||
((shift_register_ & 0x0010) >> 2) |
|
||||
((shift_register_ & 0x0040) >> 3) |
|
||||
((shift_register_ & 0x0100) >> 4) |
|
||||
((shift_register_ & 0x0400) >> 5) |
|
||||
((shift_register_ & 0x1000) >> 6) |
|
||||
((shift_register_ & 0x4000) >> 7));
|
||||
bits_since_token_ = 0;
|
||||
|
||||
if(is_awaiting_marker_value_ && is_double_density_) {
|
||||
is_awaiting_marker_value_ = false;
|
||||
switch(latest_token_.byte_value) {
|
||||
case Storage::Encodings::MFM::IndexAddressByte:
|
||||
latest_token_.type = Token::Index;
|
||||
break;
|
||||
case Storage::Encodings::MFM::IDAddressByte:
|
||||
latest_token_.type = Token::ID;
|
||||
break;
|
||||
case Storage::Encodings::MFM::DataAddressByte:
|
||||
latest_token_.type = Token::Data;
|
||||
break;
|
||||
case Storage::Encodings::MFM::DeletedDataAddressByte:
|
||||
latest_token_.type = Token::DeletedData;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
crc_generator_.add(latest_token_.byte_value);
|
||||
posit_event(Event::Token);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void WD1770::process_index_hole() {
|
||||
index_hole_count_++;
|
||||
posit_event(Event::IndexHole);
|
||||
if(index_hole_count_target_ == index_hole_count_) {
|
||||
posit_event(Event::IndexHoleTarget);
|
||||
index_hole_count_target_ = -1;
|
||||
}
|
||||
|
||||
// motor power-down
|
||||
if(index_hole_count_ == 9 && !status_.busy && has_motor_on_line()) {
|
||||
set_motor_on(false);
|
||||
}
|
||||
|
||||
// head unload
|
||||
if(index_hole_count_ == 15 && !status_.busy && has_head_load_line()) {
|
||||
set_head_load_request(false);
|
||||
}
|
||||
}
|
||||
|
||||
void WD1770::process_write_completed() {
|
||||
posit_event(Event::DataWritten);
|
||||
}
|
||||
|
||||
#define WAIT_FOR_EVENT(mask) resume_point_ = __LINE__; interesting_event_mask_ = mask; return; case __LINE__:
|
||||
#define WAIT_FOR_TIME(ms) resume_point_ = __LINE__; interesting_event_mask_ = Event::Timer; delay_time_ = ms * 8000; if(delay_time_) return; case __LINE__:
|
||||
#define WAIT_FOR_BYTES(count) resume_point_ = __LINE__; interesting_event_mask_ = Event::Token; distance_into_section_ = 0; return; case __LINE__: if(latest_token_.type == Token::Byte) distance_into_section_++; if(distance_into_section_ < count) { interesting_event_mask_ = Event::Token; return; }
|
||||
#define WAIT_FOR_EVENT(mask) resume_point_ = __LINE__; interesting_event_mask_ = static_cast<int>(mask); return; case __LINE__:
|
||||
#define WAIT_FOR_TIME(ms) resume_point_ = __LINE__; delay_time_ = ms * 8000; WAIT_FOR_EVENT(Event1770::Timer);
|
||||
#define WAIT_FOR_BYTES(count) resume_point_ = __LINE__; distance_into_section_ = 0; WAIT_FOR_EVENT(Event::Token); if(get_latest_token().type == Token::Byte) distance_into_section_++; if(distance_into_section_ < count) { interesting_event_mask_ = static_cast<int>(Event::Token); return; }
|
||||
#define BEGIN_SECTION() switch(resume_point_) { default:
|
||||
#define END_SECTION() 0; }
|
||||
|
||||
#define READ_ID() \
|
||||
if(new_event_type == Event::Token) { \
|
||||
if(!distance_into_section_ && latest_token_.type == Token::ID) {data_mode_ = DataMode::Reading; distance_into_section_++; } \
|
||||
else if(distance_into_section_ && distance_into_section_ < 7 && latest_token_.type == Token::Byte) { \
|
||||
header_[distance_into_section_ - 1] = latest_token_.byte_value; \
|
||||
if(new_event_type == static_cast<int>(Event::Token)) { \
|
||||
if(!distance_into_section_ && get_latest_token().type == Token::ID) {set_data_mode(DataMode::Reading); distance_into_section_++; } \
|
||||
else if(distance_into_section_ && distance_into_section_ < 7 && get_latest_token().type == Token::Byte) { \
|
||||
header_[distance_into_section_ - 1] = get_latest_token().byte_value; \
|
||||
distance_into_section_++; \
|
||||
} \
|
||||
}
|
||||
@@ -285,7 +148,7 @@ void WD1770::process_write_completed() {
|
||||
set_motor_on(true); \
|
||||
index_hole_count_ = 0; \
|
||||
index_hole_count_target_ = 6; \
|
||||
WAIT_FOR_EVENT(Event::IndexHoleTarget); \
|
||||
WAIT_FOR_EVENT(Event1770::IndexHoleTarget); \
|
||||
status_.spin_up = true;
|
||||
|
||||
// +--------+----------+-------------------------+
|
||||
@@ -305,8 +168,26 @@ void WD1770::process_write_completed() {
|
||||
// ! 4 ! Forc int ! 1 1 0 1 i3 i2 i1 i0 !
|
||||
// +--------+----------+-------------------------+
|
||||
|
||||
void WD1770::posit_event(Event new_event_type) {
|
||||
if(!(interesting_event_mask_ & (int)new_event_type)) return;
|
||||
void WD1770::posit_event(int new_event_type) {
|
||||
if(new_event_type == static_cast<int>(Event::IndexHole)) {
|
||||
index_hole_count_++;
|
||||
if(index_hole_count_target_ == index_hole_count_) {
|
||||
posit_event(static_cast<int>(Event1770::IndexHoleTarget));
|
||||
index_hole_count_target_ = -1;
|
||||
}
|
||||
|
||||
// motor power-down
|
||||
if(index_hole_count_ == 9 && !status_.busy && has_motor_on_line()) {
|
||||
set_motor_on(false);
|
||||
}
|
||||
|
||||
// head unload
|
||||
if(index_hole_count_ == 15 && !status_.busy && has_head_load_line()) {
|
||||
set_head_load_request(false);
|
||||
}
|
||||
}
|
||||
|
||||
if(!(interesting_event_mask_ & static_cast<int>(new_event_type))) return;
|
||||
interesting_event_mask_ &= ~new_event_type;
|
||||
|
||||
Status new_status;
|
||||
@@ -315,7 +196,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
// Wait for a new command, branch to the appropriate handler.
|
||||
wait_for_command:
|
||||
printf("Idle...\n");
|
||||
data_mode_ = DataMode::Scanning;
|
||||
set_data_mode(DataMode::Scanning);
|
||||
index_hole_count_ = 0;
|
||||
|
||||
update_status([] (Status &status) {
|
||||
@@ -323,7 +204,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
status.interrupt_request = true;
|
||||
});
|
||||
|
||||
WAIT_FOR_EVENT(Event::Command);
|
||||
WAIT_FOR_EVENT(Event1770::Command);
|
||||
|
||||
update_status([] (Status &status) {
|
||||
status.busy = true;
|
||||
@@ -372,11 +253,11 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
}
|
||||
set_head_load_request(true);
|
||||
if(head_is_loaded_) goto test_type1_type;
|
||||
WAIT_FOR_EVENT(Event::HeadLoad);
|
||||
WAIT_FOR_EVENT(Event1770::HeadLoad);
|
||||
goto test_type1_type;
|
||||
|
||||
begin_type1_spin_up:
|
||||
if((command_&0x08) || get_motor_on()) goto test_type1_type;
|
||||
if((command_&0x08) || get_drive().get_motor_on()) goto test_type1_type;
|
||||
SPIN_UP();
|
||||
|
||||
test_type1_type:
|
||||
@@ -399,11 +280,11 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
if(step_direction_) track_++; else track_--;
|
||||
|
||||
perform_step:
|
||||
if(!step_direction_ && get_is_track_zero()) {
|
||||
if(!step_direction_ && get_drive().get_is_track_zero()) {
|
||||
track_ = 0;
|
||||
goto verify;
|
||||
}
|
||||
step(step_direction_ ? 1 : -1);
|
||||
get_drive().step(step_direction_ ? 1 : -1);
|
||||
unsigned int time_to_wait;
|
||||
switch(command_ & 3) {
|
||||
default:
|
||||
@@ -429,7 +310,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
distance_into_section_ = 0;
|
||||
|
||||
verify_read_data:
|
||||
WAIT_FOR_EVENT(Event::IndexHole | Event::Token);
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event::IndexHole) | static_cast<int>(Event::Token));
|
||||
READ_ID();
|
||||
|
||||
if(index_hole_count_ == 6) {
|
||||
@@ -439,8 +320,8 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
goto wait_for_command;
|
||||
}
|
||||
if(distance_into_section_ == 7) {
|
||||
data_mode_ = DataMode::Scanning;
|
||||
if(crc_generator_.get_value()) {
|
||||
set_data_mode(DataMode::Scanning);
|
||||
if(get_crc_generator().get_value()) {
|
||||
update_status([] (Status &status) {
|
||||
status.crc_error = true;
|
||||
});
|
||||
@@ -491,11 +372,11 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
begin_type2_load_head:
|
||||
set_head_load_request(true);
|
||||
if(head_is_loaded_) goto test_type2_delay;
|
||||
WAIT_FOR_EVENT(Event::HeadLoad);
|
||||
WAIT_FOR_EVENT(Event1770::HeadLoad);
|
||||
goto test_type2_delay;
|
||||
|
||||
begin_type2_spin_up:
|
||||
if(get_motor_on()) goto test_type2_delay;
|
||||
if(get_drive().get_motor_on()) goto test_type2_delay;
|
||||
// Perform spin up.
|
||||
SPIN_UP();
|
||||
|
||||
@@ -505,7 +386,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
WAIT_FOR_TIME(30);
|
||||
|
||||
test_type2_write_protection:
|
||||
if(command_&0x20 && get_drive_is_read_only()) {
|
||||
if(command_&0x20 && get_drive().get_is_read_only()) {
|
||||
update_status([] (Status &status) {
|
||||
status.write_protect = true;
|
||||
});
|
||||
@@ -513,7 +394,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
}
|
||||
|
||||
type2_get_header:
|
||||
WAIT_FOR_EVENT(Event::IndexHole | Event::Token);
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event::IndexHole) | static_cast<int>(Event::Token));
|
||||
READ_ID();
|
||||
|
||||
if(index_hole_count_ == 5) {
|
||||
@@ -525,11 +406,11 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
}
|
||||
if(distance_into_section_ == 7) {
|
||||
printf("Considering %d/%d\n", header_[0], header_[2]);
|
||||
data_mode_ = DataMode::Scanning;
|
||||
set_data_mode(DataMode::Scanning);
|
||||
if( header_[0] == track_ && header_[2] == sector_ &&
|
||||
(has_motor_on_line() || !(command_&0x02) || ((command_&0x08) >> 3) == header_[1])) {
|
||||
printf("Found %d/%d\n", header_[0], header_[2]);
|
||||
if(crc_generator_.get_value()) {
|
||||
if(get_crc_generator().get_value()) {
|
||||
printf("CRC error; back to searching\n");
|
||||
update_status([] (Status &status) {
|
||||
status.crc_error = true;
|
||||
@@ -554,20 +435,20 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
type2_read_data:
|
||||
WAIT_FOR_EVENT(Event::Token);
|
||||
// TODO: timeout
|
||||
if(latest_token_.type == Token::Data || latest_token_.type == Token::DeletedData) {
|
||||
if(get_latest_token().type == Token::Data || get_latest_token().type == Token::DeletedData) {
|
||||
update_status([this] (Status &status) {
|
||||
status.record_type = (latest_token_.type == Token::DeletedData);
|
||||
status.record_type = (get_latest_token().type == Token::DeletedData);
|
||||
});
|
||||
distance_into_section_ = 0;
|
||||
data_mode_ = DataMode::Reading;
|
||||
set_data_mode(DataMode::Reading);
|
||||
goto type2_read_byte;
|
||||
}
|
||||
goto type2_read_data;
|
||||
|
||||
type2_read_byte:
|
||||
WAIT_FOR_EVENT(Event::Token);
|
||||
if(latest_token_.type != Token::Byte) goto type2_read_byte;
|
||||
data_ = latest_token_.byte_value;
|
||||
if(get_latest_token().type != Token::Byte) goto type2_read_byte;
|
||||
data_ = get_latest_token().byte_value;
|
||||
update_status([] (Status &status) {
|
||||
status.lost_data |= status.data_request;
|
||||
status.data_request = true;
|
||||
@@ -581,11 +462,11 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
|
||||
type2_check_crc:
|
||||
WAIT_FOR_EVENT(Event::Token);
|
||||
if(latest_token_.type != Token::Byte) goto type2_read_byte;
|
||||
header_[distance_into_section_] = latest_token_.byte_value;
|
||||
if(get_latest_token().type != Token::Byte) goto type2_read_byte;
|
||||
header_[distance_into_section_] = get_latest_token().byte_value;
|
||||
distance_into_section_++;
|
||||
if(distance_into_section_ == 2) {
|
||||
if(crc_generator_.get_value()) {
|
||||
if(get_crc_generator().get_value()) {
|
||||
printf("CRC error; terminating\n");
|
||||
update_status([this] (Status &status) {
|
||||
status.crc_error = true;
|
||||
@@ -616,24 +497,24 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
goto wait_for_command;
|
||||
}
|
||||
WAIT_FOR_BYTES(1);
|
||||
if(is_double_density_) {
|
||||
if(get_is_double_density()) {
|
||||
WAIT_FOR_BYTES(11);
|
||||
}
|
||||
|
||||
data_mode_ = DataMode::Writing;
|
||||
begin_writing();
|
||||
for(int c = 0; c < (is_double_density_ ? 12 : 6); c++) {
|
||||
set_data_mode(DataMode::Writing);
|
||||
begin_writing(false);
|
||||
for(int c = 0; c < (get_is_double_density() ? 12 : 6); c++) {
|
||||
write_byte(0);
|
||||
}
|
||||
WAIT_FOR_EVENT(Event::DataWritten);
|
||||
|
||||
if(is_double_density_) {
|
||||
crc_generator_.set_value(Storage::Encodings::MFM::MFMPostSyncCRCValue);
|
||||
if(get_is_double_density()) {
|
||||
get_crc_generator().set_value(Storage::Encodings::MFM::MFMPostSyncCRCValue);
|
||||
for(int c = 0; c < 3; c++) write_raw_short(Storage::Encodings::MFM::MFMSync);
|
||||
write_byte((command_&0x01) ? Storage::Encodings::MFM::DeletedDataAddressByte : Storage::Encodings::MFM::DataAddressByte);
|
||||
} else {
|
||||
crc_generator_.reset();
|
||||
crc_generator_.add((command_&0x01) ? Storage::Encodings::MFM::DeletedDataAddressByte : Storage::Encodings::MFM::DataAddressByte);
|
||||
get_crc_generator().reset();
|
||||
get_crc_generator().add((command_&0x01) ? Storage::Encodings::MFM::DeletedDataAddressByte : Storage::Encodings::MFM::DataAddressByte);
|
||||
write_raw_short((command_&0x01) ? Storage::Encodings::MFM::FMDeletedDataAddressMark : Storage::Encodings::MFM::FMDataAddressMark);
|
||||
}
|
||||
|
||||
@@ -669,11 +550,8 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
|
||||
goto type2_write_loop;
|
||||
|
||||
type2_write_crc: {
|
||||
uint16_t crc = crc_generator_.get_value();
|
||||
write_byte(crc >> 8);
|
||||
write_byte(crc & 0xff);
|
||||
}
|
||||
type2_write_crc:
|
||||
write_crc();
|
||||
write_byte(0xff);
|
||||
WAIT_FOR_EVENT(Event::DataWritten);
|
||||
end_writing();
|
||||
@@ -712,11 +590,11 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
begin_type3_load_head:
|
||||
set_head_load_request(true);
|
||||
if(head_is_loaded_) goto type3_test_delay;
|
||||
WAIT_FOR_EVENT(Event::HeadLoad);
|
||||
WAIT_FOR_EVENT(Event1770::HeadLoad);
|
||||
goto type3_test_delay;
|
||||
|
||||
begin_type3_spin_up:
|
||||
if((command_&0x08) || get_motor_on()) goto type3_test_delay;
|
||||
if((command_&0x08) || get_drive().get_motor_on()) goto type3_test_delay;
|
||||
SPIN_UP();
|
||||
|
||||
type3_test_delay:
|
||||
@@ -733,17 +611,17 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
distance_into_section_ = 0;
|
||||
|
||||
read_address_get_header:
|
||||
WAIT_FOR_EVENT(Event::IndexHole | Event::Token);
|
||||
if(new_event_type == Event::Token) {
|
||||
if(!distance_into_section_ && latest_token_.type == Token::ID) {data_mode_ = DataMode::Reading; distance_into_section_++; }
|
||||
else if(distance_into_section_ && distance_into_section_ < 7 && latest_token_.type == Token::Byte) {
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event::IndexHole) | static_cast<int>(Event::Token));
|
||||
if(new_event_type == static_cast<int>(Event::Token)) {
|
||||
if(!distance_into_section_ && get_latest_token().type == Token::ID) {set_data_mode(DataMode::Reading); distance_into_section_++; }
|
||||
else if(distance_into_section_ && distance_into_section_ < 7 && get_latest_token().type == Token::Byte) {
|
||||
if(status_.data_request) {
|
||||
update_status([] (Status &status) {
|
||||
status.lost_data = true;
|
||||
});
|
||||
goto wait_for_command;
|
||||
}
|
||||
header_[distance_into_section_ - 1] = data_ = latest_token_.byte_value;
|
||||
header_[distance_into_section_ - 1] = data_ = get_latest_token().byte_value;
|
||||
track_ = header_[0];
|
||||
update_status([] (Status &status) {
|
||||
status.data_request = true;
|
||||
@@ -751,7 +629,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
distance_into_section_++;
|
||||
|
||||
if(distance_into_section_ == 7) {
|
||||
if(crc_generator_.get_value()) {
|
||||
if(get_crc_generator().get_value()) {
|
||||
update_status([] (Status &status) {
|
||||
status.crc_error = true;
|
||||
});
|
||||
@@ -774,7 +652,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
index_hole_count_ = 0;
|
||||
|
||||
read_track_read_byte:
|
||||
WAIT_FOR_EVENT(Event::Token | Event::IndexHole);
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event::Token) | static_cast<int>(Event::IndexHole));
|
||||
if(index_hole_count_) {
|
||||
goto wait_for_command;
|
||||
}
|
||||
@@ -784,7 +662,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
});
|
||||
goto wait_for_command;
|
||||
}
|
||||
data_ = latest_token_.byte_value;
|
||||
data_ = get_latest_token().byte_value;
|
||||
update_status([] (Status &status) {
|
||||
status.data_request = true;
|
||||
});
|
||||
@@ -797,7 +675,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
});
|
||||
|
||||
write_track_test_write_protect:
|
||||
if(get_drive_is_read_only()) {
|
||||
if(get_drive().get_is_read_only()) {
|
||||
update_status([] (Status &status) {
|
||||
status.write_protect = true;
|
||||
});
|
||||
@@ -815,25 +693,23 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
goto wait_for_command;
|
||||
}
|
||||
|
||||
WAIT_FOR_EVENT(Event::IndexHoleTarget);
|
||||
begin_writing();
|
||||
WAIT_FOR_EVENT(Event1770::IndexHoleTarget);
|
||||
begin_writing(true);
|
||||
index_hole_count_ = 0;
|
||||
|
||||
write_track_write_loop:
|
||||
if(is_double_density_) {
|
||||
if(get_is_double_density()) {
|
||||
switch(data_) {
|
||||
case 0xf5:
|
||||
write_raw_short(Storage::Encodings::MFM::MFMSync);
|
||||
crc_generator_.set_value(Storage::Encodings::MFM::MFMPostSyncCRCValue);
|
||||
get_crc_generator().set_value(Storage::Encodings::MFM::MFMPostSyncCRCValue);
|
||||
break;
|
||||
case 0xf6:
|
||||
write_raw_short(Storage::Encodings::MFM::MFMIndexSync);
|
||||
break;
|
||||
case 0xff: {
|
||||
uint16_t crc = crc_generator_.get_value();
|
||||
write_byte(crc >> 8);
|
||||
write_byte(crc & 0xff);
|
||||
} break;
|
||||
case 0xff:
|
||||
write_crc();
|
||||
break;
|
||||
default:
|
||||
write_byte(data_);
|
||||
break;
|
||||
@@ -844,7 +720,7 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
case 0xfd: case 0xfe:
|
||||
// clock is 0xc7 = 1010 0000 0010 1010 = 0xa022
|
||||
write_raw_short(
|
||||
(uint16_t)(
|
||||
static_cast<uint16_t>(
|
||||
0xa022 |
|
||||
((data_ & 0x80) << 7) |
|
||||
((data_ & 0x40) << 6) |
|
||||
@@ -856,17 +732,15 @@ void WD1770::posit_event(Event new_event_type) {
|
||||
(data_ & 0x01)
|
||||
)
|
||||
);
|
||||
crc_generator_.reset();
|
||||
crc_generator_.add(data_);
|
||||
get_crc_generator().reset();
|
||||
get_crc_generator().add(data_);
|
||||
break;
|
||||
case 0xfc:
|
||||
write_raw_short(Storage::Encodings::MFM::FMIndexAddressMark);
|
||||
break;
|
||||
case 0xf7: {
|
||||
uint16_t crc = crc_generator_.get_value();
|
||||
write_byte(crc >> 8);
|
||||
write_byte(crc & 0xff);
|
||||
} break;
|
||||
case 0xf7:
|
||||
write_crc();
|
||||
break;
|
||||
default:
|
||||
write_byte(data_);
|
||||
break;
|
||||
@@ -907,30 +781,9 @@ void WD1770::update_status(std::function<void(Status &)> updater) {
|
||||
}
|
||||
|
||||
void WD1770::set_head_load_request(bool head_load) {}
|
||||
void WD1770::set_motor_on(bool motor_on) {}
|
||||
|
||||
void WD1770::set_head_loaded(bool head_loaded) {
|
||||
head_is_loaded_ = head_loaded;
|
||||
if(head_loaded) posit_event(Event::HeadLoad);
|
||||
}
|
||||
|
||||
void WD1770::write_bit(int bit) {
|
||||
if(is_double_density_) {
|
||||
Controller::write_bit(!bit && !last_bit_);
|
||||
Controller::write_bit(!!bit);
|
||||
last_bit_ = bit;
|
||||
} else {
|
||||
Controller::write_bit(true);
|
||||
Controller::write_bit(!!bit);
|
||||
}
|
||||
}
|
||||
|
||||
void WD1770::write_byte(uint8_t byte) {
|
||||
for(int c = 0; c < 8; c++) write_bit((byte << c)&0x80);
|
||||
crc_generator_.add(byte);
|
||||
}
|
||||
|
||||
void WD1770::write_raw_short(uint16_t value) {
|
||||
for(int c = 0; c < 16; c++) {
|
||||
Controller::write_bit(!!((value << c)&0x8000));
|
||||
}
|
||||
if(head_loaded) posit_event(static_cast<int>(Event1770::HeadLoad));
|
||||
}
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
#ifndef _770_hpp
|
||||
#define _770_hpp
|
||||
|
||||
#include "../../Storage/Disk/DiskController.hpp"
|
||||
#include "../../NumberTheory/CRC.hpp"
|
||||
#include "../../Storage/Disk/Controller/MFMDiskController.hpp"
|
||||
|
||||
namespace WD {
|
||||
|
||||
@@ -18,7 +17,7 @@ namespace WD {
|
||||
Provides an emulation of various Western Digital drive controllers, including the
|
||||
WD1770, WD1772, FDC1773 and FDC1793.
|
||||
*/
|
||||
class WD1770: public Storage::Disk::Controller {
|
||||
class WD1770: public Storage::Disk::MFMController {
|
||||
public:
|
||||
enum Personality {
|
||||
P1770, // implies automatic motor-on management, with Type 2 commands offering a spin-up disable
|
||||
@@ -34,7 +33,7 @@ class WD1770: public Storage::Disk::Controller {
|
||||
WD1770(Personality p);
|
||||
|
||||
/// Sets the value of the double-density input; when @c is_double_density is @c true, reads and writes double-density format data.
|
||||
void set_is_double_density(bool is_double_density);
|
||||
using Storage::Disk::MFMController::set_is_double_density;
|
||||
|
||||
/// Writes @c value to the register at @c address. Only the low two bits of the address are decoded.
|
||||
void set_register(int address, uint8_t value);
|
||||
@@ -77,6 +76,7 @@ class WD1770: public Storage::Disk::Controller {
|
||||
|
||||
protected:
|
||||
virtual void set_head_load_request(bool head_load);
|
||||
virtual void set_motor_on(bool motor_on);
|
||||
void set_head_loaded(bool head_loaded);
|
||||
|
||||
private:
|
||||
@@ -107,66 +107,31 @@ class WD1770: public Storage::Disk::Controller {
|
||||
|
||||
int index_hole_count_;
|
||||
int index_hole_count_target_;
|
||||
int bits_since_token_;
|
||||
int distance_into_section_;
|
||||
bool is_awaiting_marker_value_;
|
||||
|
||||
int step_direction_;
|
||||
void update_status(std::function<void(Status &)> updater);
|
||||
|
||||
// Tokeniser
|
||||
enum DataMode {
|
||||
Scanning,
|
||||
Reading,
|
||||
Writing
|
||||
} data_mode_;
|
||||
bool is_double_density_;
|
||||
int shift_register_;
|
||||
struct Token {
|
||||
enum Type {
|
||||
Index, ID, Data, DeletedData, Sync, Byte
|
||||
} type;
|
||||
uint8_t byte_value;
|
||||
} latest_token_;
|
||||
|
||||
// Events
|
||||
enum Event: int {
|
||||
Command = (1 << 0), // Indicates receipt of a new command.
|
||||
Token = (1 << 1), // Indicates recognition of a new token in the flux stream. Interrogate latest_token_ for details.
|
||||
IndexHole = (1 << 2), // Indicates the passing of a physical index hole.
|
||||
HeadLoad = (1 << 3), // Indicates the head has been loaded (1973 only).
|
||||
DataWritten = (1 << 4), // Indicates that all queued bits have been written
|
||||
|
||||
enum Event1770: int {
|
||||
Command = (1 << 3), // Indicates receipt of a new command.
|
||||
HeadLoad = (1 << 4), // Indicates the head has been loaded (1973 only).
|
||||
Timer = (1 << 5), // Indicates that the delay_time_-powered timer has timed out.
|
||||
IndexHoleTarget = (1 << 6) // Indicates that index_hole_count_ has reached index_hole_count_target_.
|
||||
};
|
||||
void posit_event(Event type);
|
||||
void posit_event(int type);
|
||||
int interesting_event_mask_;
|
||||
int resume_point_;
|
||||
unsigned int delay_time_;
|
||||
|
||||
// Output
|
||||
int last_bit_;
|
||||
void write_bit(int bit);
|
||||
void write_byte(uint8_t byte);
|
||||
void write_raw_short(uint16_t value);
|
||||
|
||||
// ID buffer
|
||||
uint8_t header_[6];
|
||||
|
||||
// CRC generator
|
||||
NumberTheory::CRC16 crc_generator_;
|
||||
|
||||
// 1793 head-loading logic
|
||||
bool head_is_loaded_;
|
||||
|
||||
// delegate
|
||||
Delegate *delegate_;
|
||||
|
||||
// Storage::Disk::Controller
|
||||
virtual void process_input_bit(int value, unsigned int cycles_since_index_hole);
|
||||
virtual void process_index_hole();
|
||||
virtual void process_write_completed();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -13,9 +13,83 @@
|
||||
#include <typeinfo>
|
||||
#include <cstdio>
|
||||
|
||||
#include "Implementation/6522Storage.hpp"
|
||||
|
||||
#include "../../ClockReceiver/ClockReceiver.hpp"
|
||||
|
||||
namespace MOS {
|
||||
namespace MOS6522 {
|
||||
|
||||
enum Port {
|
||||
A = 0,
|
||||
B = 1
|
||||
};
|
||||
|
||||
enum Line {
|
||||
One = 0,
|
||||
Two = 1
|
||||
};
|
||||
|
||||
/*!
|
||||
Provides the mechanism for just-int-time communication from a 6522; the normal use case is to compose a
|
||||
6522 and a subclass of PortHandler in order to reproduce a 6522 and its original bus wiring.
|
||||
*/
|
||||
class PortHandler {
|
||||
public:
|
||||
/// Requests the current input value of @c port from the port handler.
|
||||
uint8_t get_port_input(Port port) { return 0xff; }
|
||||
|
||||
/// Sets the current output value of @c port and provides @c direction_mask, indicating which pins are marked as output.
|
||||
void set_port_output(Port port, uint8_t value, uint8_t direction_mask) {}
|
||||
|
||||
/// Sets the current logical output level for line @c line on port @c port.
|
||||
void set_control_line_output(Port port, Line line, bool value) {}
|
||||
|
||||
/// Sets the current logical value of the interrupt line.
|
||||
void set_interrupt_status(bool status) {}
|
||||
};
|
||||
|
||||
/*!
|
||||
Provided as an optional alternative base to @c PortHandler for port handlers; via the delegate pattern adds
|
||||
a virtual level of indirection for receiving changes to the interrupt line.
|
||||
*/
|
||||
class IRQDelegatePortHandler: public PortHandler {
|
||||
public:
|
||||
class Delegate {
|
||||
public:
|
||||
/// Indicates that the interrupt status has changed for the IRQDelegatePortHandler provided.
|
||||
virtual void mos6522_did_change_interrupt_status(void *irq_delegate) = 0;
|
||||
};
|
||||
|
||||
/// Sets the delegate that will receive notification of changes in the interrupt line.
|
||||
void set_interrupt_delegate(Delegate *delegate);
|
||||
|
||||
/// Overrides PortHandler::set_interrupt_status, notifying the delegate if one is set.
|
||||
void set_interrupt_status(bool new_status);
|
||||
|
||||
private:
|
||||
Delegate *delegate_ = nullptr;
|
||||
};
|
||||
|
||||
class MOS6522Base: public MOS6522Storage {
|
||||
public:
|
||||
/// Sets the input value of line @c line on port @c port.
|
||||
void set_control_line_input(Port port, Line line, bool value);
|
||||
|
||||
/// Runs for a specified number of half cycles.
|
||||
void run_for(const HalfCycles half_cycles);
|
||||
|
||||
/// Runs for a specified number of cycles.
|
||||
void run_for(const Cycles cycles);
|
||||
|
||||
/// @returns @c true if the IRQ line is currently active; @c false otherwise.
|
||||
bool get_interrupt_line();
|
||||
|
||||
private:
|
||||
inline void do_phase1();
|
||||
inline void do_phase2();
|
||||
virtual void reevaluate_interrupts() = 0;
|
||||
};
|
||||
|
||||
/*!
|
||||
Implements a template for emulation of the MOS 6522 Versatile Interface Adaptor ('VIA').
|
||||
@@ -28,353 +102,27 @@ namespace MOS {
|
||||
Consumers should derive their own curiously-recurring-template-pattern subclass,
|
||||
implementing bus communications as required.
|
||||
*/
|
||||
template <class T> class MOS6522 {
|
||||
private:
|
||||
enum InterruptFlag: uint8_t {
|
||||
CA2ActiveEdge = 1 << 0,
|
||||
CA1ActiveEdge = 1 << 1,
|
||||
ShiftRegister = 1 << 2,
|
||||
CB2ActiveEdge = 1 << 3,
|
||||
CB1ActiveEdge = 1 << 4,
|
||||
Timer2 = 1 << 5,
|
||||
Timer1 = 1 << 6,
|
||||
};
|
||||
|
||||
template <class T> class MOS6522: public MOS6522Base {
|
||||
public:
|
||||
enum Port {
|
||||
A = 0,
|
||||
B = 1
|
||||
};
|
||||
|
||||
enum Line {
|
||||
One = 0,
|
||||
Two = 1
|
||||
};
|
||||
MOS6522(T &bus_handler) noexcept : bus_handler_(bus_handler) {}
|
||||
MOS6522(const MOS6522 &) = delete;
|
||||
|
||||
/*! Sets a register value. */
|
||||
inline void set_register(int address, uint8_t value) {
|
||||
address &= 0xf;
|
||||
// printf("6522 [%s]: %0x <- %02x\n", typeid(*this).name(), address, value);
|
||||
switch(address) {
|
||||
case 0x0:
|
||||
registers_.output[1] = value;
|
||||
static_cast<T *>(this)->set_port_output(Port::B, value, registers_.data_direction[1]); // TODO: handshake
|
||||
|
||||
registers_.interrupt_flags &= ~(InterruptFlag::CB1ActiveEdge | ((registers_.peripheral_control&0x20) ? 0 : InterruptFlag::CB2ActiveEdge));
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
case 0xf:
|
||||
case 0x1:
|
||||
registers_.output[0] = value;
|
||||
static_cast<T *>(this)->set_port_output(Port::A, value, registers_.data_direction[0]); // TODO: handshake
|
||||
|
||||
registers_.interrupt_flags &= ~(InterruptFlag::CA1ActiveEdge | ((registers_.peripheral_control&0x02) ? 0 : InterruptFlag::CB2ActiveEdge));
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
// // No handshake, so write directly
|
||||
// registers_.output[0] = value;
|
||||
// static_cast<T *>(this)->set_port_output(0, value);
|
||||
// break;
|
||||
|
||||
case 0x2:
|
||||
registers_.data_direction[1] = value;
|
||||
break;
|
||||
case 0x3:
|
||||
registers_.data_direction[0] = value;
|
||||
break;
|
||||
|
||||
// Timer 1
|
||||
case 0x6: case 0x4: registers_.timer_latch[0] = (registers_.timer_latch[0]&0xff00) | value; break;
|
||||
case 0x5: case 0x7:
|
||||
registers_.timer_latch[0] = (registers_.timer_latch[0]&0x00ff) | (uint16_t)(value << 8);
|
||||
registers_.interrupt_flags &= ~InterruptFlag::Timer1;
|
||||
if(address == 0x05) {
|
||||
registers_.next_timer[0] = registers_.timer_latch[0];
|
||||
timer_is_running_[0] = true;
|
||||
}
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
|
||||
// Timer 2
|
||||
case 0x8: registers_.timer_latch[1] = value; break;
|
||||
case 0x9:
|
||||
registers_.interrupt_flags &= ~InterruptFlag::Timer2;
|
||||
registers_.next_timer[1] = registers_.timer_latch[1] | (uint16_t)(value << 8);
|
||||
timer_is_running_[1] = true;
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
|
||||
// Shift
|
||||
case 0xa: registers_.shift = value; break;
|
||||
|
||||
// Control
|
||||
case 0xb:
|
||||
registers_.auxiliary_control = value;
|
||||
break;
|
||||
case 0xc:
|
||||
// printf("Peripheral control %02x\n", value);
|
||||
registers_.peripheral_control = value;
|
||||
|
||||
// TODO: simplify below; trying to avoid improper logging of unimplemented warnings in input mode
|
||||
if(value & 0x08) {
|
||||
switch(value & 0x0e) {
|
||||
default: printf("Unimplemented control line mode %d\n", (value >> 1)&7); break;
|
||||
case 0x0c: static_cast<T *>(this)->set_control_line_output(Port::A, Line::Two, false); break;
|
||||
case 0x0e: static_cast<T *>(this)->set_control_line_output(Port::A, Line::Two, true); break;
|
||||
}
|
||||
}
|
||||
if(value & 0x80) {
|
||||
switch(value & 0xe0) {
|
||||
default: printf("Unimplemented control line mode %d\n", (value >> 5)&7); break;
|
||||
case 0xc0: static_cast<T *>(this)->set_control_line_output(Port::B, Line::Two, false); break;
|
||||
case 0xe0: static_cast<T *>(this)->set_control_line_output(Port::B, Line::Two, true); break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// Interrupt control
|
||||
case 0xd:
|
||||
registers_.interrupt_flags &= ~value;
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
case 0xe:
|
||||
if(value&0x80)
|
||||
registers_.interrupt_enable |= value;
|
||||
else
|
||||
registers_.interrupt_enable &= ~value;
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
}
|
||||
}
|
||||
void set_register(int address, uint8_t value);
|
||||
|
||||
/*! Gets a register value. */
|
||||
inline uint8_t get_register(int address) {
|
||||
address &= 0xf;
|
||||
// printf("6522 %p: %d\n", this, address);
|
||||
switch(address) {
|
||||
case 0x0:
|
||||
registers_.interrupt_flags &= ~(InterruptFlag::CB1ActiveEdge | InterruptFlag::CB2ActiveEdge);
|
||||
reevaluate_interrupts();
|
||||
return get_port_input(Port::B, registers_.data_direction[1], registers_.output[1]);
|
||||
case 0xf: // TODO: handshake, latching
|
||||
case 0x1:
|
||||
registers_.interrupt_flags &= ~(InterruptFlag::CA1ActiveEdge | InterruptFlag::CA2ActiveEdge);
|
||||
reevaluate_interrupts();
|
||||
return get_port_input(Port::A, registers_.data_direction[0], registers_.output[0]);
|
||||
|
||||
case 0x2: return registers_.data_direction[1];
|
||||
case 0x3: return registers_.data_direction[0];
|
||||
|
||||
// Timer 1
|
||||
case 0x4:
|
||||
registers_.interrupt_flags &= ~InterruptFlag::Timer1;
|
||||
reevaluate_interrupts();
|
||||
return registers_.timer[0] & 0x00ff;
|
||||
case 0x5: return registers_.timer[0] >> 8;
|
||||
case 0x6: return registers_.timer_latch[0] & 0x00ff;
|
||||
case 0x7: return registers_.timer_latch[0] >> 8;
|
||||
|
||||
// Timer 2
|
||||
case 0x8:
|
||||
registers_.interrupt_flags &= ~InterruptFlag::Timer2;
|
||||
reevaluate_interrupts();
|
||||
return registers_.timer[1] & 0x00ff;
|
||||
case 0x9: return registers_.timer[1] >> 8;
|
||||
|
||||
case 0xa: return registers_.shift;
|
||||
|
||||
case 0xb: return registers_.auxiliary_control;
|
||||
case 0xc: return registers_.peripheral_control;
|
||||
|
||||
case 0xd: return registers_.interrupt_flags | (get_interrupt_line() ? 0x80 : 0x00);
|
||||
case 0xe: return registers_.interrupt_enable | 0x80;
|
||||
}
|
||||
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
inline void set_control_line_input(Port port, Line line, bool value) {
|
||||
switch(line) {
|
||||
case Line::One:
|
||||
if( value != control_inputs_[port].line_one &&
|
||||
value == !!(registers_.peripheral_control & (port ? 0x10 : 0x01))
|
||||
) {
|
||||
registers_.interrupt_flags |= port ? InterruptFlag::CB1ActiveEdge : InterruptFlag::CA1ActiveEdge;
|
||||
reevaluate_interrupts();
|
||||
}
|
||||
control_inputs_[port].line_one = value;
|
||||
break;
|
||||
|
||||
case Line::Two:
|
||||
// TODO: output modes, but probably elsewhere?
|
||||
if( value != control_inputs_[port].line_two && // i.e. value has changed ...
|
||||
!(registers_.peripheral_control & (port ? 0x80 : 0x08)) && // ... and line is input ...
|
||||
value == !!(registers_.peripheral_control & (port ? 0x40 : 0x04)) // ... and it's either high or low, as required
|
||||
) {
|
||||
registers_.interrupt_flags |= port ? InterruptFlag::CB2ActiveEdge : InterruptFlag::CA2ActiveEdge;
|
||||
reevaluate_interrupts();
|
||||
}
|
||||
control_inputs_[port].line_two = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#define phase2() \
|
||||
registers_.last_timer[0] = registers_.timer[0];\
|
||||
registers_.last_timer[1] = registers_.timer[1];\
|
||||
\
|
||||
if(registers_.timer_needs_reload) {\
|
||||
registers_.timer_needs_reload = false;\
|
||||
registers_.timer[0] = registers_.timer_latch[0];\
|
||||
}\
|
||||
else\
|
||||
registers_.timer[0] --;\
|
||||
\
|
||||
registers_.timer[1] --; \
|
||||
if(registers_.next_timer[0] >= 0) { registers_.timer[0] = (uint16_t)registers_.next_timer[0]; registers_.next_timer[0] = -1; }\
|
||||
if(registers_.next_timer[1] >= 0) { registers_.timer[1] = (uint16_t)registers_.next_timer[1]; registers_.next_timer[1] = -1; }\
|
||||
|
||||
// IRQ is raised on the half cycle after overflow
|
||||
#define phase1() \
|
||||
if((registers_.timer[1] == 0xffff) && !registers_.last_timer[1] && timer_is_running_[1]) {\
|
||||
timer_is_running_[1] = false;\
|
||||
registers_.interrupt_flags |= InterruptFlag::Timer2;\
|
||||
reevaluate_interrupts();\
|
||||
}\
|
||||
\
|
||||
if((registers_.timer[0] == 0xffff) && !registers_.last_timer[0] && timer_is_running_[0]) {\
|
||||
registers_.interrupt_flags |= InterruptFlag::Timer1;\
|
||||
reevaluate_interrupts();\
|
||||
\
|
||||
if(registers_.auxiliary_control&0x40)\
|
||||
registers_.timer_needs_reload = true;\
|
||||
else\
|
||||
timer_is_running_[0] = false;\
|
||||
}
|
||||
|
||||
/*! Runs for a specified number of half cycles. */
|
||||
inline void run_for(const HalfCycles half_cycles) {
|
||||
int number_of_half_cycles = half_cycles.as_int();
|
||||
|
||||
if(is_phase2_) {
|
||||
phase2();
|
||||
number_of_half_cycles--;
|
||||
}
|
||||
|
||||
while(number_of_half_cycles >= 2) {
|
||||
phase1();
|
||||
phase2();
|
||||
number_of_half_cycles -= 2;
|
||||
}
|
||||
|
||||
if(number_of_half_cycles) {
|
||||
phase1();
|
||||
is_phase2_ = true;
|
||||
} else {
|
||||
is_phase2_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
/*! Runs for a specified number of cycles. */
|
||||
inline void run_for(const Cycles cycles) {
|
||||
int number_of_cycles = cycles.as_int();
|
||||
while(number_of_cycles--) {
|
||||
phase1();
|
||||
phase2();
|
||||
}
|
||||
}
|
||||
|
||||
#undef phase1
|
||||
#undef phase2
|
||||
|
||||
/*! @returns @c true if the IRQ line is currently active; @c false otherwise. */
|
||||
inline bool get_interrupt_line() {
|
||||
uint8_t interrupt_status = registers_.interrupt_flags & registers_.interrupt_enable & 0x7f;
|
||||
return !!interrupt_status;
|
||||
}
|
||||
|
||||
MOS6522() :
|
||||
timer_is_running_{false, false},
|
||||
last_posted_interrupt_status_(false),
|
||||
is_phase2_(false) {}
|
||||
uint8_t get_register(int address);
|
||||
|
||||
private:
|
||||
// Expected to be overridden
|
||||
uint8_t get_port_input(Port port) { return 0xff; }
|
||||
void set_port_output(Port port, uint8_t value, uint8_t direction_mask) {}
|
||||
void set_control_line_output(Port port, Line line, bool value) {}
|
||||
void set_interrupt_status(bool status) {}
|
||||
T &bus_handler_;
|
||||
|
||||
// Input/output multiplexer
|
||||
uint8_t get_port_input(Port port, uint8_t output_mask, uint8_t output) {
|
||||
uint8_t input = static_cast<T *>(this)->get_port_input(port);
|
||||
return (input & ~output_mask) | (output & output_mask);
|
||||
}
|
||||
|
||||
// Phase toggle
|
||||
bool is_phase2_;
|
||||
|
||||
// Delegate and communications
|
||||
bool last_posted_interrupt_status_;
|
||||
inline void reevaluate_interrupts() {
|
||||
bool new_interrupt_status = get_interrupt_line();
|
||||
if(new_interrupt_status != last_posted_interrupt_status_) {
|
||||
last_posted_interrupt_status_ = new_interrupt_status;
|
||||
static_cast<T *>(this)->set_interrupt_status(new_interrupt_status);
|
||||
}
|
||||
}
|
||||
|
||||
// The registers
|
||||
struct Registers {
|
||||
uint8_t output[2], input[2], data_direction[2];
|
||||
uint16_t timer[2], timer_latch[2], last_timer[2];
|
||||
int next_timer[2];
|
||||
uint8_t shift;
|
||||
uint8_t auxiliary_control, peripheral_control;
|
||||
uint8_t interrupt_flags, interrupt_enable;
|
||||
bool timer_needs_reload;
|
||||
|
||||
// "A low reset (RES) input clears all R6522 internal registers to logic 0"
|
||||
Registers() :
|
||||
output{0, 0}, input{0, 0}, data_direction{0, 0},
|
||||
auxiliary_control(0), peripheral_control(0),
|
||||
interrupt_flags(0), interrupt_enable(0),
|
||||
last_timer{0, 0}, timer_needs_reload(false),
|
||||
next_timer{-1, -1} {}
|
||||
} registers_;
|
||||
|
||||
// control state
|
||||
struct {
|
||||
bool line_one, line_two;
|
||||
} control_inputs_[2];
|
||||
|
||||
// Internal state other than the registers
|
||||
bool timer_is_running_[2];
|
||||
uint8_t get_port_input(Port port, uint8_t output_mask, uint8_t output);
|
||||
inline void reevaluate_interrupts();
|
||||
};
|
||||
|
||||
/*!
|
||||
Provided for optional composition with @c MOS6522, @c MOS6522IRQDelegate provides for a delegate
|
||||
that will receive IRQ line change notifications.
|
||||
*/
|
||||
class MOS6522IRQDelegate {
|
||||
public:
|
||||
class Delegate {
|
||||
public:
|
||||
virtual void mos6522_did_change_interrupt_status(void *mos6522) = 0;
|
||||
};
|
||||
|
||||
inline void set_interrupt_delegate(Delegate *delegate) {
|
||||
delegate_ = delegate;
|
||||
}
|
||||
|
||||
inline void set_interrupt_status(bool new_status) {
|
||||
if(delegate_) delegate_->mos6522_did_change_interrupt_status(this);
|
||||
}
|
||||
|
||||
private:
|
||||
Delegate *delegate_;
|
||||
};
|
||||
#include "Implementation/6522Implementation.hpp"
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* _522_hpp */
|
||||
|
||||
116
Components/6522/Implementation/6522Base.cpp
Normal file
116
Components/6522/Implementation/6522Base.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
//
|
||||
// 6522Base.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 04/09/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "../6522.hpp"
|
||||
|
||||
using namespace MOS::MOS6522;
|
||||
|
||||
void MOS6522Base::set_control_line_input(Port port, Line line, bool value) {
|
||||
switch(line) {
|
||||
case Line::One:
|
||||
if( value != control_inputs_[port].line_one &&
|
||||
value == !!(registers_.peripheral_control & (port ? 0x10 : 0x01))
|
||||
) {
|
||||
registers_.interrupt_flags |= port ? InterruptFlag::CB1ActiveEdge : InterruptFlag::CA1ActiveEdge;
|
||||
reevaluate_interrupts();
|
||||
}
|
||||
control_inputs_[port].line_one = value;
|
||||
break;
|
||||
|
||||
case Line::Two:
|
||||
// TODO: output modes, but probably elsewhere?
|
||||
if( value != control_inputs_[port].line_two && // i.e. value has changed ...
|
||||
!(registers_.peripheral_control & (port ? 0x80 : 0x08)) && // ... and line is input ...
|
||||
value == !!(registers_.peripheral_control & (port ? 0x40 : 0x04)) // ... and it's either high or low, as required
|
||||
) {
|
||||
registers_.interrupt_flags |= port ? InterruptFlag::CB2ActiveEdge : InterruptFlag::CA2ActiveEdge;
|
||||
reevaluate_interrupts();
|
||||
}
|
||||
control_inputs_[port].line_two = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void MOS6522Base::do_phase2() {
|
||||
registers_.last_timer[0] = registers_.timer[0];
|
||||
registers_.last_timer[1] = registers_.timer[1];
|
||||
|
||||
if(registers_.timer_needs_reload) {
|
||||
registers_.timer_needs_reload = false;
|
||||
registers_.timer[0] = registers_.timer_latch[0];
|
||||
} else {
|
||||
registers_.timer[0] --;
|
||||
}
|
||||
|
||||
registers_.timer[1] --;
|
||||
if(registers_.next_timer[0] >= 0) {
|
||||
registers_.timer[0] = static_cast<uint16_t>(registers_.next_timer[0]);
|
||||
registers_.next_timer[0] = -1;
|
||||
}
|
||||
if(registers_.next_timer[1] >= 0) {
|
||||
registers_.timer[1] = static_cast<uint16_t>(registers_.next_timer[1]);
|
||||
registers_.next_timer[1] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void MOS6522Base::do_phase1() {
|
||||
// IRQ is raised on the half cycle after overflow
|
||||
if((registers_.timer[1] == 0xffff) && !registers_.last_timer[1] && timer_is_running_[1]) {
|
||||
timer_is_running_[1] = false;
|
||||
registers_.interrupt_flags |= InterruptFlag::Timer2;
|
||||
reevaluate_interrupts();
|
||||
}
|
||||
|
||||
if((registers_.timer[0] == 0xffff) && !registers_.last_timer[0] && timer_is_running_[0]) {
|
||||
registers_.interrupt_flags |= InterruptFlag::Timer1;
|
||||
reevaluate_interrupts();
|
||||
|
||||
if(registers_.auxiliary_control&0x40)
|
||||
registers_.timer_needs_reload = true;
|
||||
else
|
||||
timer_is_running_[0] = false;
|
||||
}
|
||||
}
|
||||
|
||||
/*! Runs for a specified number of half cycles. */
|
||||
void MOS6522Base::run_for(const HalfCycles half_cycles) {
|
||||
int number_of_half_cycles = half_cycles.as_int();
|
||||
|
||||
if(is_phase2_) {
|
||||
do_phase2();
|
||||
number_of_half_cycles--;
|
||||
}
|
||||
|
||||
while(number_of_half_cycles >= 2) {
|
||||
do_phase1();
|
||||
do_phase2();
|
||||
number_of_half_cycles -= 2;
|
||||
}
|
||||
|
||||
if(number_of_half_cycles) {
|
||||
do_phase1();
|
||||
is_phase2_ = true;
|
||||
} else {
|
||||
is_phase2_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
/*! Runs for a specified number of cycles. */
|
||||
void MOS6522Base::run_for(const Cycles cycles) {
|
||||
int number_of_cycles = cycles.as_int();
|
||||
while(number_of_cycles--) {
|
||||
do_phase1();
|
||||
do_phase2();
|
||||
}
|
||||
}
|
||||
|
||||
/*! @returns @c true if the IRQ line is currently active; @c false otherwise. */
|
||||
bool MOS6522Base::get_interrupt_line() {
|
||||
uint8_t interrupt_status = registers_.interrupt_flags & registers_.interrupt_enable & 0x7f;
|
||||
return !!interrupt_status;
|
||||
}
|
||||
155
Components/6522/Implementation/6522Implementation.hpp
Normal file
155
Components/6522/Implementation/6522Implementation.hpp
Normal file
@@ -0,0 +1,155 @@
|
||||
//
|
||||
// Implementation.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 04/09/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
template <typename T> void MOS6522<T>::set_register(int address, uint8_t value) {
|
||||
address &= 0xf;
|
||||
switch(address) {
|
||||
case 0x0:
|
||||
registers_.output[1] = value;
|
||||
bus_handler_.set_port_output(Port::B, value, registers_.data_direction[1]); // TODO: handshake
|
||||
|
||||
registers_.interrupt_flags &= ~(InterruptFlag::CB1ActiveEdge | ((registers_.peripheral_control&0x20) ? 0 : InterruptFlag::CB2ActiveEdge));
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
case 0xf:
|
||||
case 0x1:
|
||||
registers_.output[0] = value;
|
||||
bus_handler_.set_port_output(Port::A, value, registers_.data_direction[0]); // TODO: handshake
|
||||
|
||||
registers_.interrupt_flags &= ~(InterruptFlag::CA1ActiveEdge | ((registers_.peripheral_control&0x02) ? 0 : InterruptFlag::CB2ActiveEdge));
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
|
||||
case 0x2:
|
||||
registers_.data_direction[1] = value;
|
||||
break;
|
||||
case 0x3:
|
||||
registers_.data_direction[0] = value;
|
||||
break;
|
||||
|
||||
// Timer 1
|
||||
case 0x6: case 0x4: registers_.timer_latch[0] = (registers_.timer_latch[0]&0xff00) | value; break;
|
||||
case 0x5: case 0x7:
|
||||
registers_.timer_latch[0] = (registers_.timer_latch[0]&0x00ff) | static_cast<uint16_t>(value << 8);
|
||||
registers_.interrupt_flags &= ~InterruptFlag::Timer1;
|
||||
if(address == 0x05) {
|
||||
registers_.next_timer[0] = registers_.timer_latch[0];
|
||||
timer_is_running_[0] = true;
|
||||
}
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
|
||||
// Timer 2
|
||||
case 0x8: registers_.timer_latch[1] = value; break;
|
||||
case 0x9:
|
||||
registers_.interrupt_flags &= ~InterruptFlag::Timer2;
|
||||
registers_.next_timer[1] = registers_.timer_latch[1] | static_cast<uint16_t>(value << 8);
|
||||
timer_is_running_[1] = true;
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
|
||||
// Shift
|
||||
case 0xa: registers_.shift = value; break;
|
||||
|
||||
// Control
|
||||
case 0xb:
|
||||
registers_.auxiliary_control = value;
|
||||
break;
|
||||
case 0xc:
|
||||
// printf("Peripheral control %02x\n", value);
|
||||
registers_.peripheral_control = value;
|
||||
|
||||
// TODO: simplify below; trying to avoid improper logging of unimplemented warnings in input mode
|
||||
if(value & 0x08) {
|
||||
switch(value & 0x0e) {
|
||||
default: printf("Unimplemented control line mode %d\n", (value >> 1)&7); break;
|
||||
case 0x0c: bus_handler_.set_control_line_output(Port::A, Line::Two, false); break;
|
||||
case 0x0e: bus_handler_.set_control_line_output(Port::A, Line::Two, true); break;
|
||||
}
|
||||
}
|
||||
if(value & 0x80) {
|
||||
switch(value & 0xe0) {
|
||||
default: printf("Unimplemented control line mode %d\n", (value >> 5)&7); break;
|
||||
case 0xc0: bus_handler_.set_control_line_output(Port::B, Line::Two, false); break;
|
||||
case 0xe0: bus_handler_.set_control_line_output(Port::B, Line::Two, true); break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// Interrupt control
|
||||
case 0xd:
|
||||
registers_.interrupt_flags &= ~value;
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
case 0xe:
|
||||
if(value&0x80)
|
||||
registers_.interrupt_enable |= value;
|
||||
else
|
||||
registers_.interrupt_enable &= ~value;
|
||||
reevaluate_interrupts();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> uint8_t MOS6522<T>::get_register(int address) {
|
||||
address &= 0xf;
|
||||
switch(address) {
|
||||
case 0x0:
|
||||
registers_.interrupt_flags &= ~(InterruptFlag::CB1ActiveEdge | InterruptFlag::CB2ActiveEdge);
|
||||
reevaluate_interrupts();
|
||||
return get_port_input(Port::B, registers_.data_direction[1], registers_.output[1]);
|
||||
case 0xf: // TODO: handshake, latching
|
||||
case 0x1:
|
||||
registers_.interrupt_flags &= ~(InterruptFlag::CA1ActiveEdge | InterruptFlag::CA2ActiveEdge);
|
||||
reevaluate_interrupts();
|
||||
return get_port_input(Port::A, registers_.data_direction[0], registers_.output[0]);
|
||||
|
||||
case 0x2: return registers_.data_direction[1];
|
||||
case 0x3: return registers_.data_direction[0];
|
||||
|
||||
// Timer 1
|
||||
case 0x4:
|
||||
registers_.interrupt_flags &= ~InterruptFlag::Timer1;
|
||||
reevaluate_interrupts();
|
||||
return registers_.timer[0] & 0x00ff;
|
||||
case 0x5: return registers_.timer[0] >> 8;
|
||||
case 0x6: return registers_.timer_latch[0] & 0x00ff;
|
||||
case 0x7: return registers_.timer_latch[0] >> 8;
|
||||
|
||||
// Timer 2
|
||||
case 0x8:
|
||||
registers_.interrupt_flags &= ~InterruptFlag::Timer2;
|
||||
reevaluate_interrupts();
|
||||
return registers_.timer[1] & 0x00ff;
|
||||
case 0x9: return registers_.timer[1] >> 8;
|
||||
|
||||
case 0xa: return registers_.shift;
|
||||
|
||||
case 0xb: return registers_.auxiliary_control;
|
||||
case 0xc: return registers_.peripheral_control;
|
||||
|
||||
case 0xd: return registers_.interrupt_flags | (get_interrupt_line() ? 0x80 : 0x00);
|
||||
case 0xe: return registers_.interrupt_enable | 0x80;
|
||||
}
|
||||
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
template <typename T> uint8_t MOS6522<T>::get_port_input(Port port, uint8_t output_mask, uint8_t output) {
|
||||
uint8_t input = bus_handler_.get_port_input(port);
|
||||
return (input & ~output_mask) | (output & output_mask);
|
||||
}
|
||||
|
||||
// Delegate and communications
|
||||
template <typename T> void MOS6522<T>::reevaluate_interrupts() {
|
||||
bool new_interrupt_status = get_interrupt_line();
|
||||
if(new_interrupt_status != last_posted_interrupt_status_) {
|
||||
last_posted_interrupt_status_ = new_interrupt_status;
|
||||
bus_handler_.set_interrupt_status(new_interrupt_status);
|
||||
}
|
||||
}
|
||||
63
Components/6522/Implementation/6522Storage.hpp
Normal file
63
Components/6522/Implementation/6522Storage.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// 6522Storage.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 04/09/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef _522Storage_hpp
|
||||
#define _522Storage_hpp
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace MOS {
|
||||
namespace MOS6522 {
|
||||
|
||||
class MOS6522Storage {
|
||||
protected:
|
||||
// Phase toggle
|
||||
bool is_phase2_ = false;
|
||||
|
||||
// The registers
|
||||
struct Registers {
|
||||
// "A low reset (RES) input clears all R6522 internal registers to logic 0"
|
||||
uint8_t output[2] = {0, 0};
|
||||
uint8_t input[2] = {0, 0};
|
||||
uint8_t data_direction[2] = {0, 0};
|
||||
uint16_t timer[2] = {0, 0};
|
||||
uint16_t timer_latch[2] = {0, 0};
|
||||
uint16_t last_timer[2] = {0, 0};
|
||||
int next_timer[2] = {-1, -1};
|
||||
uint8_t shift = 0;
|
||||
uint8_t auxiliary_control = 0;
|
||||
uint8_t peripheral_control = 0;
|
||||
uint8_t interrupt_flags = 0;
|
||||
uint8_t interrupt_enable = 0;
|
||||
bool timer_needs_reload = false;
|
||||
} registers_;
|
||||
|
||||
// control state
|
||||
struct {
|
||||
bool line_one = false;
|
||||
bool line_two = false;
|
||||
} control_inputs_[2];
|
||||
|
||||
bool timer_is_running_[2] = {false, false};
|
||||
bool last_posted_interrupt_status_ = false;
|
||||
|
||||
enum InterruptFlag: uint8_t {
|
||||
CA2ActiveEdge = 1 << 0,
|
||||
CA1ActiveEdge = 1 << 1,
|
||||
ShiftRegister = 1 << 2,
|
||||
CB2ActiveEdge = 1 << 3,
|
||||
CB1ActiveEdge = 1 << 4,
|
||||
Timer2 = 1 << 5,
|
||||
Timer1 = 1 << 6,
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* _522Storage_hpp */
|
||||
19
Components/6522/Implementation/IRQDelegatePortHandler.cpp
Normal file
19
Components/6522/Implementation/IRQDelegatePortHandler.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// IRQDelegatePortHandler.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 04/09/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "../6522.hpp"
|
||||
|
||||
using namespace MOS::MOS6522;
|
||||
|
||||
void IRQDelegatePortHandler::set_interrupt_delegate(Delegate *delegate) {
|
||||
delegate_ = delegate;
|
||||
}
|
||||
|
||||
void IRQDelegatePortHandler::set_interrupt_status(bool new_status) {
|
||||
if(delegate_) delegate_->mos6522_did_change_interrupt_status(this);
|
||||
}
|
||||
@@ -51,7 +51,7 @@ template <class T> class MOS6532 {
|
||||
case 0x04: case 0x05: case 0x06: case 0x07:
|
||||
if(address & 0x10) {
|
||||
timer_.writtenShift = timer_.activeShift = (decodedAddress - 0x04) * 3 + (decodedAddress / 0x07); // i.e. 0, 3, 6, 10
|
||||
timer_.value = ((unsigned int)value << timer_.activeShift) ;
|
||||
timer_.value = (static_cast<unsigned int>(value) << timer_.activeShift) ;
|
||||
timer_.interrupt_enabled = !!(address&0x08);
|
||||
interrupt_status_ &= ~InterruptFlag::Timer;
|
||||
evaluate_interrupts();
|
||||
@@ -79,7 +79,7 @@ template <class T> class MOS6532 {
|
||||
|
||||
// Timer and interrupt control
|
||||
case 0x04: case 0x06: {
|
||||
uint8_t value = (uint8_t)(timer_.value >> timer_.activeShift);
|
||||
uint8_t value = static_cast<uint8_t>(timer_.value >> timer_.activeShift);
|
||||
timer_.interrupt_enabled = !!(address&0x08);
|
||||
interrupt_status_ &= ~InterruptFlag::Timer;
|
||||
evaluate_interrupts();
|
||||
@@ -107,7 +107,7 @@ template <class T> class MOS6532 {
|
||||
}
|
||||
|
||||
inline void run_for(const Cycles cycles) {
|
||||
unsigned int number_of_cycles = (unsigned int)cycles.as_int();
|
||||
unsigned int number_of_cycles = static_cast<unsigned int>(cycles.as_int());
|
||||
|
||||
// permit counting _to_ zero; counting _through_ zero initiates the other behaviour
|
||||
if(timer_.value >= number_of_cycles) {
|
||||
@@ -126,7 +126,7 @@ template <class T> class MOS6532 {
|
||||
port_{{.output_mask = 0, .output = 0}, {.output_mask = 0, .output = 0}},
|
||||
a7_interrupt_({.last_port_value = 0, .enabled = false}),
|
||||
interrupt_line_(false),
|
||||
timer_{.value = (unsigned int)((rand() & 0xff) << 10), .activeShift = 10, .writtenShift = 10, .interrupt_enabled = false} {}
|
||||
timer_{.value = static_cast<unsigned int>((rand() & 0xff) << 10), .activeShift = 10, .writtenShift = 10, .interrupt_enabled = false} {}
|
||||
|
||||
inline void set_port_did_change(int port) {
|
||||
if(!port) {
|
||||
|
||||
@@ -98,7 +98,7 @@ static uint8_t noise_pattern[] = {
|
||||
|
||||
#define shift(r) shift_registers_[r] = (shift_registers_[r] << 1) | (((shift_registers_[r]^0x80)&control_registers_[r]) >> 7)
|
||||
#define increment(r) shift_registers_[r] = (shift_registers_[r]+1)%8191
|
||||
#define update(r, m, up) counters_[r]++; if((counters_[r] >> m) == 0x80) { up(r); counters_[r] = (unsigned int)(control_registers_[r]&0x7f) << m; }
|
||||
#define update(r, m, up) counters_[r]++; if((counters_[r] >> m) == 0x80) { up(r); counters_[r] = static_cast<unsigned int>(control_registers_[r]&0x7f) << m; }
|
||||
// Note on slightly askew test: as far as I can make out, if the value in the register is 0x7f then what's supposed to happen
|
||||
// is that the 0x7f is loaded, on the next clocked cycle the Vic spots a 0x7f, pumps the output, reloads, etc. No increment
|
||||
// ever occurs. It's conditional. I don't really want two conditionals if I can avoid it so I'm incrementing regardless and
|
||||
|
||||
@@ -66,7 +66,7 @@ template <class T> class MOS6560 {
|
||||
}
|
||||
|
||||
void set_clock_rate(double clock_rate) {
|
||||
speaker_->set_input_rate((float)(clock_rate / 4.0));
|
||||
speaker_->set_input_rate(static_cast<float>(clock_rate / 4.0));
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> get_crt() { return crt_; }
|
||||
@@ -100,10 +100,10 @@ template <class T> class MOS6560 {
|
||||
8, 88, 120, 56,
|
||||
};
|
||||
const uint8_t ntsc_chrominances[16] = {
|
||||
255, 255, 40, 104,
|
||||
64, 120, 80, 16,
|
||||
32, 32, 40, 104,
|
||||
64, 120, 80, 16,
|
||||
255, 255, 8, 72,
|
||||
32, 88, 48, 112,
|
||||
0, 0, 8, 72,
|
||||
32, 88, 48, 112,
|
||||
};
|
||||
const uint8_t *chrominances;
|
||||
Outputs::CRT::DisplayType display_type;
|
||||
@@ -128,7 +128,7 @@ template <class T> class MOS6560 {
|
||||
break;
|
||||
}
|
||||
|
||||
crt_->set_new_display_type((unsigned int)(timing_.cycles_per_line*4), display_type);
|
||||
crt_->set_new_display_type(static_cast<unsigned int>(timing_.cycles_per_line*4), display_type);
|
||||
crt_->set_visible_area(Outputs::CRT::Rect(0.05f, 0.05f, 0.9f, 0.9f));
|
||||
|
||||
// switch(output_mode) {
|
||||
@@ -141,7 +141,7 @@ template <class T> class MOS6560 {
|
||||
// }
|
||||
|
||||
for(int c = 0; c < 16; c++) {
|
||||
uint8_t *colour = (uint8_t *)&colours_[c];
|
||||
uint8_t *colour = reinterpret_cast<uint8_t *>(&colours_[c]);
|
||||
colour[0] = luminances[c];
|
||||
colour[1] = chrominances[c];
|
||||
}
|
||||
@@ -218,7 +218,7 @@ template <class T> class MOS6560 {
|
||||
if(column_counter_&1) {
|
||||
fetch_address = registers_.character_cell_start_address + (character_code_*(registers_.tall_characters ? 16 : 8)) + current_character_row_;
|
||||
} else {
|
||||
fetch_address = (uint16_t)(registers_.video_matrix_start_address + video_matrix_address_counter_);
|
||||
fetch_address = static_cast<uint16_t>(registers_.video_matrix_start_address + video_matrix_address_counter_);
|
||||
video_matrix_address_counter_++;
|
||||
if(
|
||||
(current_character_row_ == 15) ||
|
||||
@@ -270,7 +270,7 @@ template <class T> class MOS6560 {
|
||||
|
||||
pixel_pointer = nullptr;
|
||||
if(output_state_ == State::Pixels) {
|
||||
pixel_pointer = (uint16_t *)crt_->allocate_write_area(260);
|
||||
pixel_pointer = reinterpret_cast<uint16_t *>(crt_->allocate_write_area(260));
|
||||
}
|
||||
}
|
||||
cycles_in_state_++;
|
||||
@@ -345,7 +345,7 @@ template <class T> class MOS6560 {
|
||||
|
||||
case 0x2:
|
||||
registers_.number_of_columns = value & 0x7f;
|
||||
registers_.video_matrix_start_address = (uint16_t)((registers_.video_matrix_start_address & 0x3c00) | ((value & 0x80) << 2));
|
||||
registers_.video_matrix_start_address = static_cast<uint16_t>((registers_.video_matrix_start_address & 0x3c00) | ((value & 0x80) << 2));
|
||||
break;
|
||||
|
||||
case 0x3:
|
||||
@@ -354,8 +354,8 @@ template <class T> class MOS6560 {
|
||||
break;
|
||||
|
||||
case 0x5:
|
||||
registers_.character_cell_start_address = (uint16_t)((value & 0x0f) << 10);
|
||||
registers_.video_matrix_start_address = (uint16_t)((registers_.video_matrix_start_address & 0x0200) | ((value & 0xf0) << 6));
|
||||
registers_.character_cell_start_address = static_cast<uint16_t>((value & 0x0f) << 10);
|
||||
registers_.video_matrix_start_address = static_cast<uint16_t>((registers_.video_matrix_start_address & 0x0200) | ((value & 0xf0) << 6));
|
||||
break;
|
||||
|
||||
case 0xa:
|
||||
@@ -399,7 +399,7 @@ template <class T> class MOS6560 {
|
||||
int current_line = (full_frame_counter_ + timing_.line_counter_increment_offset) / timing_.cycles_per_line;
|
||||
switch(address) {
|
||||
default: return registers_.direct_values[address];
|
||||
case 0x03: return (uint8_t)(current_line << 7) | (registers_.direct_values[3] & 0x7f);
|
||||
case 0x03: return static_cast<uint8_t>(current_line << 7) | (registers_.direct_values[3] & 0x7f);
|
||||
case 0x04: return (current_line >> 1) & 0xff;
|
||||
}
|
||||
}
|
||||
@@ -454,7 +454,7 @@ template <class T> class MOS6560 {
|
||||
|
||||
uint16_t *pixel_pointer;
|
||||
void output_border(unsigned int number_of_cycles) {
|
||||
uint16_t *colour_pointer = (uint16_t *)crt_->allocate_write_area(1);
|
||||
uint16_t *colour_pointer = reinterpret_cast<uint16_t *>(crt_->allocate_write_area(1));
|
||||
if(colour_pointer) *colour_pointer = registers_.borderColour;
|
||||
crt_->output_level(number_of_cycles);
|
||||
}
|
||||
|
||||
275
Components/6845/CRTC6845.hpp
Normal file
275
Components/6845/CRTC6845.hpp
Normal file
@@ -0,0 +1,275 @@
|
||||
//
|
||||
// CRTC6845.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 31/07/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef CRTC6845_hpp
|
||||
#define CRTC6845_hpp
|
||||
|
||||
#include "../../ClockReceiver/ClockReceiver.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
namespace Motorola {
|
||||
namespace CRTC {
|
||||
|
||||
struct BusState {
|
||||
bool display_enable = false;
|
||||
bool hsync = false;
|
||||
bool vsync = false;
|
||||
bool cursor = false;
|
||||
uint16_t refresh_address = 0;
|
||||
uint16_t row_address = 0;
|
||||
};
|
||||
|
||||
class BusHandler {
|
||||
public:
|
||||
/*!
|
||||
Performs the first phase of a 6845 bus cycle; this is the phase in which it is intended that
|
||||
systems using the 6845 respect the bus state and produce pixels, sync or whatever they require.
|
||||
*/
|
||||
void perform_bus_cycle_phase1(const BusState &) {}
|
||||
|
||||
/*!
|
||||
Performs the second phase of a 6845 bus cycle. Some bus state — including sync — is updated
|
||||
directly after phase 1 and hence is visible to an observer during phase 2. Handlers may therefore
|
||||
implement @c perform_bus_cycle_phase2 to be notified of the availability of that state without
|
||||
having to wait until the next cycle has begun.
|
||||
*/
|
||||
void perform_bus_cycle_phase2(const BusState &) {}
|
||||
};
|
||||
|
||||
enum Personality {
|
||||
HD6845S, // Type 0 in CPC parlance. Zero-width HSYNC available, no status, programmable VSYNC length.
|
||||
// Considered exactly identical to the UM6845, so this enum covers both.
|
||||
UM6845R, // Type 1 in CPC parlance. Status register, fixed-length VSYNC.
|
||||
MC6845, // Type 2. No status register, fixed-length VSYNC, no zero-length HSYNC.
|
||||
AMS40226 // Type 3. Status is get register, fixed-length VSYNC, no zero-length HSYNC.
|
||||
};
|
||||
|
||||
// TODO UM6845R and R12/R13; see http://www.cpcwiki.eu/index.php/CRTC#CRTC_Differences
|
||||
|
||||
template <class T> class CRTC6845 {
|
||||
public:
|
||||
|
||||
CRTC6845(Personality p, T &bus_handler) noexcept :
|
||||
personality_(p), bus_handler_(bus_handler), status_(0) {}
|
||||
|
||||
void select_register(uint8_t r) {
|
||||
selected_register_ = r;
|
||||
}
|
||||
|
||||
uint8_t get_status() {
|
||||
switch(personality_) {
|
||||
case UM6845R: return status_ | (bus_state_.vsync ? 0x20 : 0x00);
|
||||
case AMS40226: return get_register();
|
||||
default: return 0xff;
|
||||
}
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
uint8_t get_register() {
|
||||
if(selected_register_ == 31) status_ &= ~0x80;
|
||||
if(selected_register_ == 16 || selected_register_ == 17) status_ &= ~0x40;
|
||||
|
||||
if(personality_ == UM6845R && selected_register_ == 31) return dummy_register_;
|
||||
if(selected_register_ < 12 || selected_register_ > 17) return 0xff;
|
||||
return registers_[selected_register_];
|
||||
}
|
||||
|
||||
void set_register(uint8_t value) {
|
||||
static uint8_t masks[] = {
|
||||
0xff, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0x7f, 0x7f,
|
||||
0xff, 0x1f, 0x7f, 0x1f, 0x3f, 0xff, 0x3f, 0xff
|
||||
};
|
||||
|
||||
// Per CPC documentation, skew doesn't work on a "type 1 or 2", i.e. an MC6845 or a UM6845R.
|
||||
if(selected_register_ == 8 && personality_ != UM6845R && personality_ != MC6845) {
|
||||
switch((value >> 4)&3) {
|
||||
default: display_skew_mask_ = 1; break;
|
||||
case 1: display_skew_mask_ = 2; break;
|
||||
case 2: display_skew_mask_ = 4; break;
|
||||
}
|
||||
}
|
||||
|
||||
if(selected_register_ < 16) {
|
||||
registers_[selected_register_] = value & masks[selected_register_];
|
||||
}
|
||||
if(selected_register_ == 31 && personality_ == UM6845R) {
|
||||
dummy_register_ = value;
|
||||
}
|
||||
}
|
||||
|
||||
void trigger_light_pen() {
|
||||
registers_[17] = bus_state_.refresh_address & 0xff;
|
||||
registers_[16] = bus_state_.refresh_address >> 8;
|
||||
status_ |= 0x40;
|
||||
}
|
||||
|
||||
void run_for(Cycles cycles) {
|
||||
int cyles_remaining = cycles.as_int();
|
||||
while(cyles_remaining--) {
|
||||
// check for end of visible characters
|
||||
if(character_counter_ == registers_[1]) {
|
||||
// TODO: consider skew in character_is_visible_. Or maybe defer until perform_bus_cycle?
|
||||
character_is_visible_ = false;
|
||||
end_of_line_address_ = bus_state_.refresh_address;
|
||||
}
|
||||
|
||||
perform_bus_cycle_phase1();
|
||||
bus_state_.refresh_address = (bus_state_.refresh_address + 1) & 0x3fff;
|
||||
|
||||
// check for end-of-line
|
||||
if(character_counter_ == registers_[0]) {
|
||||
character_counter_ = 0;
|
||||
do_end_of_line();
|
||||
character_is_visible_ = true;
|
||||
} else {
|
||||
// increment counter
|
||||
character_counter_++;
|
||||
}
|
||||
|
||||
// check for start of horizontal sync
|
||||
if(character_counter_ == registers_[2]) {
|
||||
hsync_counter_ = 0;
|
||||
bus_state_.hsync = true;
|
||||
}
|
||||
|
||||
// check for end of horizontal sync; note that a sync time of zero will result in an immediate
|
||||
// cancellation of the plan to perform sync if this is an HD6845S or UM6845R; otherwise zero
|
||||
// will end up counting as 16 as it won't be checked until after overflow.
|
||||
if(bus_state_.hsync) {
|
||||
switch(personality_) {
|
||||
case HD6845S:
|
||||
case UM6845R:
|
||||
bus_state_.hsync = hsync_counter_ != (registers_[3] & 15);
|
||||
hsync_counter_ = (hsync_counter_ + 1) & 15;
|
||||
break;
|
||||
default:
|
||||
hsync_counter_ = (hsync_counter_ + 1) & 15;
|
||||
bus_state_.hsync = hsync_counter_ != (registers_[3] & 15);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
perform_bus_cycle_phase2();
|
||||
}
|
||||
}
|
||||
|
||||
const BusState &get_bus_state() const {
|
||||
return bus_state_;
|
||||
}
|
||||
|
||||
private:
|
||||
inline void perform_bus_cycle_phase1() {
|
||||
// Skew theory of operation: keep a history of the last three states, and apply whichever is selected.
|
||||
character_is_visible_shifter_ = (character_is_visible_shifter_ << 1) | static_cast<unsigned int>(character_is_visible_);
|
||||
bus_state_.display_enable = (static_cast<int>(character_is_visible_shifter_) & display_skew_mask_) && line_is_visible_;
|
||||
bus_handler_.perform_bus_cycle_phase1(bus_state_);
|
||||
}
|
||||
|
||||
inline void perform_bus_cycle_phase2() {
|
||||
bus_handler_.perform_bus_cycle_phase2(bus_state_);
|
||||
}
|
||||
|
||||
inline void do_end_of_line() {
|
||||
// check for end of vertical sync
|
||||
if(bus_state_.vsync) {
|
||||
vsync_counter_ = (vsync_counter_ + 1) & 15;
|
||||
// on the UM6845R and AMS40226, honour the programmed vertical sync time; on the other CRTCs
|
||||
// always use a vertical sync count of 16.
|
||||
switch(personality_) {
|
||||
case HD6845S:
|
||||
case AMS40226:
|
||||
bus_state_.vsync = vsync_counter_ != (registers_[3] >> 4);
|
||||
break;
|
||||
default:
|
||||
bus_state_.vsync = vsync_counter_ != 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(is_in_adjustment_period_) {
|
||||
line_counter_++;
|
||||
if(line_counter_ == registers_[5]) {
|
||||
is_in_adjustment_period_ = false;
|
||||
do_end_of_frame();
|
||||
}
|
||||
} else {
|
||||
// advance vertical counter
|
||||
if(bus_state_.row_address == registers_[9]) {
|
||||
bus_state_.row_address = 0;
|
||||
line_address_ = end_of_line_address_;
|
||||
|
||||
// check for entry into the overflow area
|
||||
if(line_counter_ == registers_[4]) {
|
||||
if(registers_[5]) {
|
||||
line_counter_ = 0;
|
||||
is_in_adjustment_period_ = true;
|
||||
} else {
|
||||
do_end_of_frame();
|
||||
}
|
||||
} else {
|
||||
line_counter_ = (line_counter_ + 1) & 0x7f;
|
||||
|
||||
// check for start of vertical sync
|
||||
if(line_counter_ == registers_[7]) {
|
||||
bus_state_.vsync = true;
|
||||
vsync_counter_ = 0;
|
||||
}
|
||||
|
||||
// check for end of visible lines
|
||||
if(line_counter_ == registers_[6]) {
|
||||
line_is_visible_ = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bus_state_.row_address = (bus_state_.row_address + 1) & 0x1f;
|
||||
}
|
||||
}
|
||||
|
||||
bus_state_.refresh_address = line_address_;
|
||||
character_counter_ = 0;
|
||||
character_is_visible_ = (registers_[1] != 0);
|
||||
}
|
||||
|
||||
inline void do_end_of_frame() {
|
||||
line_counter_ = 0;
|
||||
line_is_visible_ = true;
|
||||
line_address_ = static_cast<uint16_t>((registers_[12] << 8) | registers_[13]);
|
||||
bus_state_.refresh_address = line_address_;
|
||||
}
|
||||
|
||||
Personality personality_;
|
||||
T &bus_handler_;
|
||||
BusState bus_state_;
|
||||
|
||||
uint8_t registers_[18] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
uint8_t dummy_register_ = 0;
|
||||
int selected_register_ = 0;
|
||||
|
||||
uint8_t character_counter_ = 0;
|
||||
uint8_t line_counter_ = 0;
|
||||
|
||||
bool character_is_visible_ = false, line_is_visible_ = false;
|
||||
|
||||
int hsync_counter_ = 0;
|
||||
int vsync_counter_ = 0;
|
||||
bool is_in_adjustment_period_ = false;
|
||||
|
||||
uint16_t line_address_ = 0;
|
||||
uint16_t end_of_line_address_ = 0;
|
||||
uint8_t status_ = 0;
|
||||
|
||||
int display_skew_mask_ = 1;
|
||||
unsigned int character_is_visible_shifter_ = 0;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CRTC6845_hpp */
|
||||
92
Components/8255/i8255.hpp
Normal file
92
Components/8255/i8255.hpp
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// i8255.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 01/08/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef i8255_hpp
|
||||
#define i8255_hpp
|
||||
|
||||
namespace Intel {
|
||||
namespace i8255 {
|
||||
|
||||
class PortHandler {
|
||||
public:
|
||||
void set_value(int port, uint8_t value) {}
|
||||
uint8_t get_value(int port) { return 0xff; }
|
||||
};
|
||||
|
||||
// TODO: Modes 1 and 2.
|
||||
template <class T> class i8255 {
|
||||
public:
|
||||
i8255(T &port_handler) : control_(0), outputs_{0, 0, 0}, port_handler_(port_handler) {}
|
||||
|
||||
/*!
|
||||
Stores the value @c value to the register at @c address. If this causes a change in 8255 output
|
||||
then the PortHandler will be informed.
|
||||
*/
|
||||
void set_register(int address, uint8_t value) {
|
||||
switch(address & 3) {
|
||||
case 0:
|
||||
if(!(control_ & 0x10)) {
|
||||
// TODO: so what would output be when switching from input to output mode?
|
||||
outputs_[0] = value; port_handler_.set_value(0, value);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if(!(control_ & 0x02)) {
|
||||
outputs_[1] = value; port_handler_.set_value(1, value);
|
||||
}
|
||||
break;
|
||||
case 2: outputs_[2] = value; port_handler_.set_value(2, value); break;
|
||||
case 3:
|
||||
if(value & 0x80) {
|
||||
control_ = value;
|
||||
} else {
|
||||
if(value & 1) {
|
||||
outputs_[2] |= 1 << ((value >> 1)&7);
|
||||
} else {
|
||||
outputs_[2] &= ~(1 << ((value >> 1)&7));
|
||||
}
|
||||
}
|
||||
update_outputs();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Obtains the current value for the register at @c address. If this provides a reading
|
||||
of input then the PortHandler will be queried.
|
||||
*/
|
||||
uint8_t get_register(int address) {
|
||||
switch(address & 3) {
|
||||
case 0: return (control_ & 0x10) ? port_handler_.get_value(0) : outputs_[0];
|
||||
case 1: return (control_ & 0x02) ? port_handler_.get_value(1) : outputs_[1];
|
||||
case 2: {
|
||||
if(!(control_ & 0x09)) return outputs_[2];
|
||||
uint8_t input = port_handler_.get_value(2);
|
||||
return ((control_ & 0x01) ? (input & 0x0f) : (outputs_[2] & 0x0f)) | ((control_ & 0x08) ? (input & 0xf0) : (outputs_[2] & 0xf0));
|
||||
}
|
||||
case 3: return control_;
|
||||
}
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
private:
|
||||
void update_outputs() {
|
||||
port_handler_.set_value(0, outputs_[0]);
|
||||
port_handler_.set_value(1, outputs_[1]);
|
||||
port_handler_.set_value(2, outputs_[2]);
|
||||
}
|
||||
|
||||
uint8_t control_;
|
||||
uint8_t outputs_[3];
|
||||
T &port_handler_;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* i8255_hpp */
|
||||
873
Components/8272/i8272.cpp
Normal file
873
Components/8272/i8272.cpp
Normal file
@@ -0,0 +1,873 @@
|
||||
//
|
||||
// i8272.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 05/08/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "i8272.hpp"
|
||||
//#include "../../Storage/Disk/Encodings/MFM/Encoder.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
using namespace Intel::i8272;
|
||||
|
||||
#define SetDataRequest() (main_status_ |= 0x80)
|
||||
#define ResetDataRequest() (main_status_ &= ~0x80)
|
||||
#define DataRequest() (main_status_ & 0x80)
|
||||
|
||||
#define SetDataDirectionToProcessor() (main_status_ |= 0x40)
|
||||
#define SetDataDirectionFromProcessor() (main_status_ &= ~0x40)
|
||||
#define DataDirectionToProcessor() (main_status_ & 0x40)
|
||||
|
||||
#define SetNonDMAExecution() (main_status_ |= 0x20)
|
||||
#define ResetNonDMAExecution() (main_status_ &= ~0x20)
|
||||
|
||||
#define SetBusy() (main_status_ |= 0x10)
|
||||
#define ResetBusy() (main_status_ &= ~0x10)
|
||||
#define Busy() (main_status_ & 0x10)
|
||||
|
||||
#define SetAbnormalTermination() (status_[0] |= 0x40)
|
||||
#define SetInvalidCommand() (status_[0] |= 0x80)
|
||||
#define SetReadyChanged() (status_[0] |= 0xc0)
|
||||
#define SetSeekEnd() (status_[0] |= 0x20)
|
||||
#define SetEquipmentCheck() (status_[0] |= 0x10)
|
||||
#define SetNotReady() (status_[0] |= 0x08)
|
||||
#define SetSide2() (status_[0] |= 0x04)
|
||||
|
||||
#define SetEndOfCylinder() (status_[1] |= 0x80)
|
||||
#define SetDataError() (status_[1] |= 0x20)
|
||||
#define SetOverrun() (status_[1] |= 0x10)
|
||||
#define SetNoData() (status_[1] |= 0x04)
|
||||
#define SetNotWriteable() (status_[1] |= 0x02)
|
||||
#define SetMissingAddressMark() (status_[1] |= 0x01)
|
||||
|
||||
#define SetControlMark() (status_[2] |= 0x40)
|
||||
#define ClearControlMark() (status_[2] &= ~0x40)
|
||||
#define ControlMark() (status_[2] & 0x40)
|
||||
|
||||
#define SetDataFieldDataError() (status_[2] |= 0x20)
|
||||
#define SetWrongCyinder() (status_[2] |= 0x10)
|
||||
#define SetScanEqualHit() (status_[2] |= 0x08)
|
||||
#define SetScanNotSatisfied() (status_[2] |= 0x04)
|
||||
#define SetBadCylinder() (status_[2] |= 0x02)
|
||||
#define SetMissingDataAddressMark() (status_[2] |= 0x01)
|
||||
|
||||
namespace {
|
||||
const uint8_t CommandReadData = 0x06;
|
||||
const uint8_t CommandReadDeletedData = 0x0c;
|
||||
|
||||
const uint8_t CommandWriteData = 0x05;
|
||||
const uint8_t CommandWriteDeletedData = 0x09;
|
||||
|
||||
const uint8_t CommandReadTrack = 0x02;
|
||||
const uint8_t CommandReadID = 0x0a;
|
||||
const uint8_t CommandFormatTrack = 0x0d;
|
||||
|
||||
const uint8_t CommandScanLow = 0x11;
|
||||
const uint8_t CommandScanLowOrEqual = 0x19;
|
||||
const uint8_t CommandScanHighOrEqual = 0x1d;
|
||||
|
||||
const uint8_t CommandRecalibrate = 0x07;
|
||||
const uint8_t CommandSeek = 0x0f;
|
||||
|
||||
const uint8_t CommandSenseInterruptStatus = 0x08;
|
||||
const uint8_t CommandSpecify = 0x03;
|
||||
const uint8_t CommandSenseDriveStatus = 0x04;
|
||||
}
|
||||
|
||||
i8272::i8272(BusHandler &bus_handler, Cycles clock_rate) :
|
||||
Storage::Disk::MFMController(clock_rate),
|
||||
bus_handler_(bus_handler) {
|
||||
posit_event(static_cast<int>(Event8272::CommandByte));
|
||||
}
|
||||
|
||||
bool i8272::is_sleeping() {
|
||||
return is_sleeping_ && Storage::Disk::MFMController::is_sleeping();
|
||||
}
|
||||
|
||||
void i8272::run_for(Cycles cycles) {
|
||||
Storage::Disk::MFMController::run_for(cycles);
|
||||
|
||||
if(is_sleeping_) return;
|
||||
|
||||
// check for an expired timer
|
||||
if(delay_time_ > 0) {
|
||||
if(cycles.as_int() >= delay_time_) {
|
||||
delay_time_ = 0;
|
||||
posit_event(static_cast<int>(Event8272::Timer));
|
||||
} else {
|
||||
delay_time_ -= cycles.as_int();
|
||||
}
|
||||
}
|
||||
|
||||
// update seek status of any drives presently seeking
|
||||
if(drives_seeking_) {
|
||||
int drives_left = drives_seeking_;
|
||||
for(int c = 0; c < 4; c++) {
|
||||
if(drives_[c].phase == Drive::Seeking) {
|
||||
drives_[c].step_rate_counter += cycles.as_int();
|
||||
int steps = drives_[c].step_rate_counter / (8000 * step_rate_time_);
|
||||
drives_[c].step_rate_counter %= (8000 * step_rate_time_);
|
||||
while(steps--) {
|
||||
// Perform a step.
|
||||
int direction = (drives_[c].target_head_position < drives_[c].head_position) ? -1 : 1;
|
||||
printf("Target %d versus believed %d\n", drives_[c].target_head_position, drives_[c].head_position);
|
||||
select_drive(c);
|
||||
get_drive().step(direction);
|
||||
if(drives_[c].target_head_position >= 0) drives_[c].head_position += direction;
|
||||
|
||||
// Check for completion.
|
||||
if(seek_is_satisfied(c)) {
|
||||
drives_[c].phase = Drive::CompletedSeeking;
|
||||
drives_seeking_--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
drives_left--;
|
||||
if(!drives_left) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check for any head unloads
|
||||
if(head_timers_running_) {
|
||||
int timers_left = head_timers_running_;
|
||||
for(int c = 0; c < 8; c++) {
|
||||
int drive = (c >> 1);
|
||||
int head = c&1;
|
||||
|
||||
if(drives_[drive].head_unload_delay[head] > 0) {
|
||||
if(cycles.as_int() >= drives_[drive].head_unload_delay[head]) {
|
||||
drives_[drive].head_unload_delay[head] = 0;
|
||||
drives_[drive].head_is_loaded[head] = false;
|
||||
head_timers_running_--;
|
||||
} else {
|
||||
drives_[drive].head_unload_delay[head] -= cycles.as_int();
|
||||
}
|
||||
timers_left--;
|
||||
if(!timers_left) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check for busy plus ready disabled
|
||||
if(is_executing_ && !get_drive().get_is_ready()) {
|
||||
posit_event(static_cast<int>(Event8272::NoLongerReady));
|
||||
}
|
||||
|
||||
is_sleeping_ = !delay_time_ && !drives_seeking_ && !head_timers_running_;
|
||||
if(is_sleeping_) update_sleep_observer();
|
||||
}
|
||||
|
||||
void i8272::set_register(int address, uint8_t value) {
|
||||
// don't consider attempted sets to the status register
|
||||
if(!address) return;
|
||||
|
||||
// if not ready for commands, do nothing
|
||||
if(!DataRequest() || DataDirectionToProcessor()) return;
|
||||
|
||||
if(expects_input_) {
|
||||
input_ = value;
|
||||
has_input_ = true;
|
||||
ResetDataRequest();
|
||||
} else {
|
||||
// accumulate latest byte in the command byte sequence
|
||||
command_.push_back(value);
|
||||
posit_event(static_cast<int>(Event8272::CommandByte));
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t i8272::get_register(int address) {
|
||||
if(address) {
|
||||
if(result_stack_.empty()) return 0xff;
|
||||
uint8_t result = result_stack_.back();
|
||||
result_stack_.pop_back();
|
||||
if(result_stack_.empty()) posit_event(static_cast<int>(Event8272::ResultEmpty));
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return main_status_;
|
||||
}
|
||||
}
|
||||
|
||||
#define BEGIN_SECTION() switch(resume_point_) { default:
|
||||
#define END_SECTION() }
|
||||
|
||||
#define MS_TO_CYCLES(x) x * 8000
|
||||
#define WAIT_FOR_EVENT(mask) resume_point_ = __LINE__; interesting_event_mask_ = static_cast<int>(mask); return; case __LINE__:
|
||||
#define WAIT_FOR_TIME(ms) resume_point_ = __LINE__; interesting_event_mask_ = static_cast<int>(Event8272::Timer); delay_time_ = MS_TO_CYCLES(ms); is_sleeping_ = false; update_sleep_observer(); case __LINE__: if(delay_time_) return;
|
||||
|
||||
#define PASTE(x, y) x##y
|
||||
#define CONCAT(x, y) PASTE(x, y)
|
||||
|
||||
#define FIND_HEADER() \
|
||||
set_data_mode(DataMode::Scanning); \
|
||||
CONCAT(find_header, __LINE__): WAIT_FOR_EVENT(static_cast<int>(Event::Token) | static_cast<int>(Event::IndexHole)); \
|
||||
if(event_type == static_cast<int>(Event::IndexHole)) { index_hole_limit_--; } \
|
||||
else if(get_latest_token().type == Token::ID) goto CONCAT(header_found, __LINE__); \
|
||||
\
|
||||
if(index_hole_limit_) goto CONCAT(find_header, __LINE__); \
|
||||
CONCAT(header_found, __LINE__): 0;\
|
||||
|
||||
#define FIND_DATA() \
|
||||
set_data_mode(DataMode::Scanning); \
|
||||
CONCAT(find_data, __LINE__): WAIT_FOR_EVENT(static_cast<int>(Event::Token) | static_cast<int>(Event::IndexHole)); \
|
||||
if(event_type == static_cast<int>(Event::Token)) { \
|
||||
if(get_latest_token().type == Token::Byte || get_latest_token().type == Token::Sync) goto CONCAT(find_data, __LINE__); \
|
||||
}
|
||||
|
||||
#define READ_HEADER() \
|
||||
distance_into_section_ = 0; \
|
||||
set_data_mode(DataMode::Reading); \
|
||||
CONCAT(read_header, __LINE__): WAIT_FOR_EVENT(Event::Token); \
|
||||
header_[distance_into_section_] = get_latest_token().byte_value; \
|
||||
distance_into_section_++; \
|
||||
if(distance_into_section_ < 6) goto CONCAT(read_header, __LINE__); \
|
||||
|
||||
#define SET_DRIVE_HEAD_MFM() \
|
||||
active_drive_ = command_[1]&3; \
|
||||
active_head_ = (command_[1] >> 2)&1; \
|
||||
status_[0] = (command_[1]&7); \
|
||||
select_drive(active_drive_); \
|
||||
get_drive().set_head(active_head_); \
|
||||
set_is_double_density(command_[0] & 0x40);
|
||||
|
||||
#define WAIT_FOR_BYTES(n) \
|
||||
distance_into_section_ = 0; \
|
||||
CONCAT(wait_bytes, __LINE__): WAIT_FOR_EVENT(Event::Token); \
|
||||
if(get_latest_token().type == Token::Byte) distance_into_section_++; \
|
||||
if(distance_into_section_ < (n)) goto CONCAT(wait_bytes, __LINE__);
|
||||
|
||||
#define LOAD_HEAD() \
|
||||
if(!drives_[active_drive_].head_is_loaded[active_head_]) { \
|
||||
drives_[active_drive_].head_is_loaded[active_head_] = true; \
|
||||
WAIT_FOR_TIME(head_load_time_); \
|
||||
} else { \
|
||||
if(drives_[active_drive_].head_unload_delay[active_head_] > 0) { \
|
||||
drives_[active_drive_].head_unload_delay[active_head_] = 0; \
|
||||
head_timers_running_--; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SCHEDULE_HEAD_UNLOAD() \
|
||||
if(drives_[active_drive_].head_is_loaded[active_head_]) {\
|
||||
if(drives_[active_drive_].head_unload_delay[active_head_] == 0) { \
|
||||
head_timers_running_++; \
|
||||
is_sleeping_ = false; \
|
||||
update_sleep_observer(); \
|
||||
} \
|
||||
drives_[active_drive_].head_unload_delay[active_head_] = MS_TO_CYCLES(head_unload_time_);\
|
||||
}
|
||||
|
||||
void i8272::posit_event(int event_type) {
|
||||
if(event_type == static_cast<int>(Event::IndexHole)) index_hole_count_++;
|
||||
if(event_type == static_cast<int>(Event8272::NoLongerReady)) {
|
||||
SetNotReady();
|
||||
goto abort;
|
||||
}
|
||||
if(!(interesting_event_mask_ & event_type)) return;
|
||||
interesting_event_mask_ &= ~event_type;
|
||||
|
||||
BEGIN_SECTION();
|
||||
|
||||
// Resets busy and non-DMA execution, clears the command buffer, sets the data mode to scanning and flows
|
||||
// into wait_for_complete_command_sequence.
|
||||
wait_for_command:
|
||||
expects_input_ = false;
|
||||
set_data_mode(Storage::Disk::MFMController::DataMode::Scanning);
|
||||
ResetBusy();
|
||||
ResetNonDMAExecution();
|
||||
command_.clear();
|
||||
|
||||
// Sets the data request bit, and waits for a byte. Then sets the busy bit. Continues accepting bytes
|
||||
// until it has a quantity that make up an entire command, then resets the data request bit and
|
||||
// branches to that command.
|
||||
wait_for_complete_command_sequence:
|
||||
SetDataRequest();
|
||||
SetDataDirectionFromProcessor();
|
||||
WAIT_FOR_EVENT(Event8272::CommandByte)
|
||||
SetBusy();
|
||||
|
||||
static const size_t required_lengths[32] = {
|
||||
0, 0, 9, 3, 2, 9, 9, 2,
|
||||
1, 9, 2, 0, 9, 6, 0, 3,
|
||||
0, 9, 0, 0, 0, 0, 0, 0,
|
||||
0, 9, 0, 0, 0, 9, 0, 0,
|
||||
};
|
||||
|
||||
if(command_.size() < required_lengths[command_[0] & 0x1f]) goto wait_for_complete_command_sequence;
|
||||
if(command_.size() == 9) {
|
||||
cylinder_ = command_[2];
|
||||
head_ = command_[3];
|
||||
sector_ = command_[4];
|
||||
size_ = command_[5];
|
||||
}
|
||||
ResetDataRequest();
|
||||
status_[0] = status_[1] = status_[2] = 0;
|
||||
|
||||
// If this is not clearly a command that's safe to carry out in parallel to a seek, end all seeks.
|
||||
switch(command_[0] & 0x1f) {
|
||||
case CommandReadData:
|
||||
case CommandReadDeletedData:
|
||||
case CommandWriteData:
|
||||
case CommandWriteDeletedData:
|
||||
case CommandReadTrack:
|
||||
case CommandReadID:
|
||||
case CommandFormatTrack:
|
||||
case CommandScanLow:
|
||||
case CommandScanLowOrEqual:
|
||||
case CommandScanHighOrEqual:
|
||||
is_access_command_ = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
is_access_command_ = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if(is_access_command_) {
|
||||
for(int c = 0; c < 4; c++) {
|
||||
if(drives_[c].phase == Drive::Seeking) {
|
||||
drives_[c].phase = Drive::NotSeeking;
|
||||
drives_seeking_--;
|
||||
}
|
||||
}
|
||||
// Establishes the drive and head being addressed, and whether in double density mode; populates the internal
|
||||
// cylinder, head, sector and size registers from the command stream.
|
||||
is_executing_ = true;
|
||||
if(!dma_mode_) SetNonDMAExecution();
|
||||
SET_DRIVE_HEAD_MFM();
|
||||
LOAD_HEAD();
|
||||
if(!get_drive().get_is_ready()) {
|
||||
SetNotReady();
|
||||
goto abort;
|
||||
}
|
||||
}
|
||||
|
||||
// Jump to the proper place.
|
||||
switch(command_[0] & 0x1f) {
|
||||
case CommandReadData:
|
||||
case CommandReadDeletedData:
|
||||
goto read_data;
|
||||
|
||||
case CommandWriteData:
|
||||
case CommandWriteDeletedData:
|
||||
goto write_data;
|
||||
|
||||
case CommandReadTrack: goto read_track;
|
||||
case CommandReadID: goto read_id;
|
||||
case CommandFormatTrack: goto format_track;
|
||||
|
||||
case CommandScanLow: goto scan_low;
|
||||
case CommandScanLowOrEqual: goto scan_low_or_equal;
|
||||
case CommandScanHighOrEqual: goto scan_high_or_equal;
|
||||
|
||||
case CommandRecalibrate: goto recalibrate;
|
||||
case CommandSeek: goto seek;
|
||||
|
||||
case CommandSenseInterruptStatus: goto sense_interrupt_status;
|
||||
case CommandSpecify: goto specify;
|
||||
case CommandSenseDriveStatus: goto sense_drive_status;
|
||||
|
||||
default: goto invalid;
|
||||
}
|
||||
|
||||
// Decodes drive, head and density, loads the head, loads the internal cylinder, head, sector and size registers,
|
||||
// and searches for a sector that meets those criteria. If one is found, inspects the instruction in use and
|
||||
// jumps to an appropriate handler.
|
||||
read_write_find_header:
|
||||
|
||||
// Sets a maximum index hole limit of 2 then performs a find header/read header loop, continuing either until
|
||||
// the index hole limit is breached or a sector is found with a cylinder, head, sector and size equal to the
|
||||
// values in the internal registers.
|
||||
index_hole_limit_ = 2;
|
||||
// printf("Seeking %02x %02x %02x %02x\n", cylinder_, head_, sector_, size_);
|
||||
find_next_sector:
|
||||
FIND_HEADER();
|
||||
if(!index_hole_limit_) {
|
||||
// Two index holes have passed wihout finding the header sought.
|
||||
// printf("Not found\n");
|
||||
SetNoData();
|
||||
goto abort;
|
||||
}
|
||||
index_hole_count_ = 0;
|
||||
// printf("Header\n");
|
||||
READ_HEADER();
|
||||
if(index_hole_count_) {
|
||||
// This implies an index hole was sighted within the header. Error out.
|
||||
SetEndOfCylinder();
|
||||
goto abort;
|
||||
}
|
||||
if(get_crc_generator().get_value()) {
|
||||
// This implies a CRC error in the header; mark as such but continue.
|
||||
SetDataError();
|
||||
}
|
||||
// printf("Considering %02x %02x %02x %02x [%04x]\n", header_[0], header_[1], header_[2], header_[3], get_crc_generator().get_value());
|
||||
if(header_[0] != cylinder_ || header_[1] != head_ || header_[2] != sector_ || header_[3] != size_) goto find_next_sector;
|
||||
|
||||
// Branch to whatever is supposed to happen next
|
||||
// printf("Proceeding\n");
|
||||
switch(command_[0] & 0x1f) {
|
||||
case CommandReadData:
|
||||
case CommandReadDeletedData:
|
||||
goto read_data_found_header;
|
||||
|
||||
case CommandWriteData: // write data
|
||||
case CommandWriteDeletedData: // write deleted data
|
||||
goto write_data_found_header;
|
||||
}
|
||||
|
||||
|
||||
// Performs the read data or read deleted data command.
|
||||
read_data:
|
||||
printf("Read [deleted] data [%02x %02x %02x %02x ... %02x %02x]\n", command_[2], command_[3], command_[4], command_[5], command_[6], command_[8]);
|
||||
read_next_data:
|
||||
goto read_write_find_header;
|
||||
|
||||
// Finds the next data block and sets data mode to reading, setting an error flag if the on-disk deleted
|
||||
// flag doesn't match the sort the command was looking for.
|
||||
read_data_found_header:
|
||||
FIND_DATA();
|
||||
ClearControlMark();
|
||||
if(event_type == static_cast<int>(Event::Token)) {
|
||||
if(get_latest_token().type != Token::Data && get_latest_token().type != Token::DeletedData) {
|
||||
// Something other than a data mark came next — impliedly an ID or index mark.
|
||||
SetMissingAddressMark();
|
||||
SetMissingDataAddressMark();
|
||||
goto abort; // TODO: or read_next_data?
|
||||
} else {
|
||||
if((get_latest_token().type == Token::Data) != ((command_[0] & 0x1f) == CommandReadData)) {
|
||||
if(!(command_[0]&0x20)) {
|
||||
// SK is not set; set the error flag but read this sector before finishing.
|
||||
SetControlMark();
|
||||
} else {
|
||||
// SK is set; skip this sector.
|
||||
goto read_next_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// An index hole appeared before the data mark.
|
||||
SetEndOfCylinder();
|
||||
goto abort; // TODO: or read_next_data?
|
||||
}
|
||||
|
||||
distance_into_section_ = 0;
|
||||
set_data_mode(Reading);
|
||||
|
||||
// Waits for the next token, then supplies it to the CPU by: (i) setting data request and direction; and (ii) resetting
|
||||
// data request once the byte has been taken. Continues until all bytes have been read.
|
||||
//
|
||||
// TODO: consider DTL.
|
||||
read_data_get_byte:
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event::Token) | static_cast<int>(Event::IndexHole));
|
||||
if(event_type == static_cast<int>(Event::Token)) {
|
||||
result_stack_.push_back(get_latest_token().byte_value);
|
||||
distance_into_section_++;
|
||||
SetDataRequest();
|
||||
SetDataDirectionToProcessor();
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event8272::ResultEmpty) | static_cast<int>(Event::Token) | static_cast<int>(Event::IndexHole));
|
||||
}
|
||||
switch(event_type) {
|
||||
case static_cast<int>(Event8272::ResultEmpty): // The caller read the byte in time; proceed as normal.
|
||||
ResetDataRequest();
|
||||
if(distance_into_section_ < (128 << size_)) goto read_data_get_byte;
|
||||
break;
|
||||
case static_cast<int>(Event::Token): // The caller hasn't read the old byte yet and a new one has arrived
|
||||
SetOverrun();
|
||||
goto abort;
|
||||
break;
|
||||
case static_cast<int>(Event::IndexHole):
|
||||
SetEndOfCylinder();
|
||||
goto abort;
|
||||
break;
|
||||
}
|
||||
|
||||
// read CRC, without transferring it, then check it
|
||||
WAIT_FOR_EVENT(Event::Token);
|
||||
WAIT_FOR_EVENT(Event::Token);
|
||||
if(get_crc_generator().get_value()) {
|
||||
// This implies a CRC error in the sector; mark as such and temrinate.
|
||||
SetDataError();
|
||||
SetDataFieldDataError();
|
||||
goto abort;
|
||||
}
|
||||
|
||||
// check whether that's it: either the final requested sector has been read, or because
|
||||
// a sector that was [/wasn't] marked as deleted when it shouldn't [/should] have been
|
||||
if(sector_ != command_[6] && !ControlMark()) {
|
||||
sector_++;
|
||||
goto read_next_data;
|
||||
}
|
||||
|
||||
// For a final result phase, post the standard ST0, ST1, ST2, C, H, R, N
|
||||
goto post_st012chrn;
|
||||
|
||||
write_data:
|
||||
printf("Write [deleted] data [%02x %02x %02x %02x ... %02x %02x]\n", command_[2], command_[3], command_[4], command_[5], command_[6], command_[8]);
|
||||
|
||||
if(get_drive().get_is_read_only()) {
|
||||
SetNotWriteable();
|
||||
goto abort;
|
||||
}
|
||||
|
||||
write_next_data:
|
||||
goto read_write_find_header;
|
||||
|
||||
write_data_found_header:
|
||||
WAIT_FOR_BYTES(get_is_double_density() ? 22 : 11);
|
||||
begin_writing(true);
|
||||
|
||||
write_id_data_joiner((command_[0] & 0x1f) == CommandWriteDeletedData, true);
|
||||
|
||||
SetDataDirectionFromProcessor();
|
||||
SetDataRequest();
|
||||
expects_input_ = true;
|
||||
distance_into_section_ = 0;
|
||||
|
||||
write_loop:
|
||||
WAIT_FOR_EVENT(Event::DataWritten);
|
||||
if(!has_input_) {
|
||||
SetOverrun();
|
||||
goto abort;
|
||||
}
|
||||
write_byte(input_);
|
||||
has_input_ = false;
|
||||
distance_into_section_++;
|
||||
if(distance_into_section_ < (128 << size_)) {
|
||||
SetDataRequest();
|
||||
goto write_loop;
|
||||
}
|
||||
|
||||
printf("Wrote %d bytes\n", distance_into_section_);
|
||||
write_crc();
|
||||
expects_input_ = false;
|
||||
WAIT_FOR_EVENT(Event::DataWritten);
|
||||
end_writing();
|
||||
|
||||
if(sector_ != command_[6]) {
|
||||
sector_++;
|
||||
goto write_next_data;
|
||||
}
|
||||
|
||||
goto post_st012chrn;
|
||||
|
||||
// Performs the read ID command.
|
||||
read_id:
|
||||
// Establishes the drive and head being addressed, and whether in double density mode.
|
||||
printf("Read ID [%02x %02x]\n", command_[0], command_[1]);
|
||||
|
||||
// Sets a maximum index hole limit of 2 then waits either until it finds a header mark or sees too many index holes.
|
||||
// If a header mark is found, reads in the following bytes that produce a header. Otherwise branches to data not found.
|
||||
index_hole_limit_ = 2;
|
||||
read_id_find_next_sector:
|
||||
FIND_HEADER();
|
||||
if(!index_hole_limit_) {
|
||||
SetMissingAddressMark();
|
||||
goto abort;
|
||||
}
|
||||
READ_HEADER();
|
||||
|
||||
// Sets internal registers from the discovered header and posts the standard ST0, ST1, ST2, C, H, R, N.
|
||||
cylinder_ = header_[0];
|
||||
head_ = header_[1];
|
||||
sector_ = header_[2];
|
||||
size_ = header_[3];
|
||||
|
||||
goto post_st012chrn;
|
||||
|
||||
// Performs read track.
|
||||
read_track:
|
||||
printf("Read track [%02x %02x %02x %02x]\n", command_[2], command_[3], command_[4], command_[5]);
|
||||
|
||||
// Wait for the index hole.
|
||||
WAIT_FOR_EVENT(Event::IndexHole);
|
||||
|
||||
sector_ = 0;
|
||||
index_hole_limit_ = 2;
|
||||
|
||||
// While not index hole again, stream all sector contents until EOT sectors have been read.
|
||||
read_track_next_sector:
|
||||
FIND_HEADER();
|
||||
if(!index_hole_limit_) {
|
||||
if(!sector_) {
|
||||
SetMissingAddressMark();
|
||||
goto abort;
|
||||
} else {
|
||||
goto post_st012chrn;
|
||||
}
|
||||
}
|
||||
READ_HEADER();
|
||||
|
||||
FIND_DATA();
|
||||
distance_into_section_ = 0;
|
||||
SetDataDirectionToProcessor();
|
||||
read_track_get_byte:
|
||||
WAIT_FOR_EVENT(Event::Token);
|
||||
result_stack_.push_back(get_latest_token().byte_value);
|
||||
distance_into_section_++;
|
||||
SetDataRequest();
|
||||
// TODO: other possible exit conditions; find a way to merge with the read_data version of this.
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event8272::ResultEmpty));
|
||||
ResetDataRequest();
|
||||
if(distance_into_section_ < (128 << header_[2])) goto read_track_get_byte;
|
||||
|
||||
sector_++;
|
||||
if(sector_ < command_[6]) goto read_track_next_sector;
|
||||
|
||||
goto post_st012chrn;
|
||||
|
||||
// Performs format [/write] track.
|
||||
format_track:
|
||||
printf("Format track\n");
|
||||
if(get_drive().get_is_read_only()) {
|
||||
SetNotWriteable();
|
||||
goto abort;
|
||||
}
|
||||
|
||||
// Wait for the index hole.
|
||||
WAIT_FOR_EVENT(Event::IndexHole);
|
||||
index_hole_count_ = 0;
|
||||
begin_writing(true);
|
||||
|
||||
// Write start-of-track.
|
||||
write_start_of_track();
|
||||
WAIT_FOR_EVENT(Event::DataWritten);
|
||||
sector_ = 0;
|
||||
|
||||
format_track_write_sector:
|
||||
write_id_joiner();
|
||||
|
||||
// Write the sector header, obtaining its contents
|
||||
// from the processor.
|
||||
SetDataDirectionFromProcessor();
|
||||
SetDataRequest();
|
||||
expects_input_ = true;
|
||||
distance_into_section_ = 0;
|
||||
format_track_write_header:
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event::DataWritten) | static_cast<int>(Event::IndexHole));
|
||||
switch(event_type) {
|
||||
case static_cast<int>(Event::IndexHole):
|
||||
SetOverrun();
|
||||
goto abort;
|
||||
break;
|
||||
case static_cast<int>(Event::DataWritten):
|
||||
header_[distance_into_section_] = input_;
|
||||
write_byte(input_);
|
||||
has_input_ = false;
|
||||
distance_into_section_++;
|
||||
if(distance_into_section_ < 4) {
|
||||
SetDataRequest();
|
||||
goto format_track_write_header;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
printf("W: %02x %02x %02x %02x, %04x\n", header_[0], header_[1], header_[2], header_[3], get_crc_generator().get_value());
|
||||
write_crc();
|
||||
|
||||
// Write the sector body.
|
||||
write_id_data_joiner(false, false);
|
||||
write_n_bytes(128 << command_[2], command_[5]);
|
||||
write_crc();
|
||||
|
||||
// Write the prescribed gap.
|
||||
write_n_bytes(command_[4], get_is_double_density() ? 0x4e : 0xff);
|
||||
|
||||
// Consider repeating.
|
||||
sector_++;
|
||||
if(sector_ < command_[3] && !index_hole_count_)
|
||||
goto format_track_write_sector;
|
||||
|
||||
// Otherwise, pad out to the index hole.
|
||||
format_track_pad:
|
||||
write_byte(get_is_double_density() ? 0x4e : 0xff);
|
||||
WAIT_FOR_EVENT(static_cast<int>(Event::DataWritten) | static_cast<int>(Event::IndexHole));
|
||||
if(event_type != static_cast<int>(Event::IndexHole)) goto format_track_pad;
|
||||
|
||||
end_writing();
|
||||
|
||||
cylinder_ = header_[0];
|
||||
head_ = header_[1];
|
||||
sector_ = header_[2] + 1;
|
||||
size_ = header_[3];
|
||||
|
||||
goto post_st012chrn;
|
||||
|
||||
scan_low:
|
||||
printf("Scan low unimplemented!!\n");
|
||||
goto wait_for_command;
|
||||
|
||||
scan_low_or_equal:
|
||||
printf("Scan low or equal unimplemented!!\n");
|
||||
goto wait_for_command;
|
||||
|
||||
scan_high_or_equal:
|
||||
printf("Scan high or equal unimplemented!!\n");
|
||||
goto wait_for_command;
|
||||
|
||||
// Performs both recalibrate and seek commands. These commands occur asynchronously, so the actual work
|
||||
// occurs in ::run_for; this merely establishes that seeking should be ongoing.
|
||||
recalibrate:
|
||||
seek:
|
||||
{
|
||||
int drive = command_[1]&3;
|
||||
select_drive(drive);
|
||||
|
||||
// Increment the seeking count if this drive wasn't already seeking.
|
||||
if(drives_[drive].phase != Drive::Seeking) {
|
||||
drives_seeking_++;
|
||||
is_sleeping_ = false;
|
||||
update_sleep_observer();
|
||||
}
|
||||
|
||||
// Set currently seeking, with a step to occur right now (yes, it sounds like jamming these
|
||||
// in could damage your drive motor).
|
||||
drives_[drive].phase = Drive::Seeking;
|
||||
drives_[drive].step_rate_counter = 8000 * step_rate_time_;
|
||||
drives_[drive].steps_taken = 0;
|
||||
drives_[drive].seek_failed = false;
|
||||
main_status_ |= 1 << (command_[1]&3);
|
||||
|
||||
// If this is a seek, set the processor-supplied target location; otherwise it is a recalibrate,
|
||||
// which means resetting the current state now but aiming to hit '-1' (which the stepping code
|
||||
// up in run_for understands to mean 'keep going until track 0 is active').
|
||||
if(command_.size() > 2) {
|
||||
drives_[drive].target_head_position = command_[2];
|
||||
printf("Seek to %02x\n", command_[2]);
|
||||
} else {
|
||||
drives_[drive].target_head_position = -1;
|
||||
drives_[drive].head_position = 0;
|
||||
printf("Recalibrate\n");
|
||||
}
|
||||
|
||||
// Check whether any steps are even needed; if not then mark as completed already.
|
||||
if(seek_is_satisfied(drive)) {
|
||||
drives_[drive].phase = Drive::CompletedSeeking;
|
||||
drives_seeking_--;
|
||||
}
|
||||
}
|
||||
goto wait_for_command;
|
||||
|
||||
// Performs sense interrupt status.
|
||||
sense_interrupt_status:
|
||||
printf("Sense interrupt status\n");
|
||||
{
|
||||
// Find the first drive that is in the CompletedSeeking state.
|
||||
int found_drive = -1;
|
||||
for(int c = 0; c < 4; c++) {
|
||||
if(drives_[c].phase == Drive::CompletedSeeking) {
|
||||
found_drive = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If a drive was found, return its results. Otherwise return a single 0x80.
|
||||
if(found_drive != -1) {
|
||||
drives_[found_drive].phase = Drive::NotSeeking;
|
||||
status_[0] = static_cast<uint8_t>(found_drive);
|
||||
main_status_ &= ~(1 << found_drive);
|
||||
SetSeekEnd();
|
||||
|
||||
result_stack_ = { drives_[found_drive].head_position, status_[0]};
|
||||
} else {
|
||||
result_stack_ = { 0x80 };
|
||||
}
|
||||
}
|
||||
goto post_result;
|
||||
|
||||
// Performs specify.
|
||||
specify:
|
||||
// Just store the values, and terminate the command.
|
||||
printf("Specify\n");
|
||||
step_rate_time_ = 16 - (command_[1] >> 4); // i.e. 1 to 16ms
|
||||
head_unload_time_ = (command_[1] & 0x0f) << 4; // i.e. 16 to 240ms
|
||||
head_load_time_ = command_[2] & ~1; // i.e. 2 to 254 ms in increments of 2ms
|
||||
|
||||
if(!head_unload_time_) head_unload_time_ = 16;
|
||||
if(!head_load_time_) head_load_time_ = 2;
|
||||
dma_mode_ = !(command_[2] & 1);
|
||||
goto wait_for_command;
|
||||
|
||||
sense_drive_status:
|
||||
printf("Sense drive status\n");
|
||||
{
|
||||
int drive = command_[1] & 3;
|
||||
select_drive(drive);
|
||||
result_stack_= {
|
||||
static_cast<uint8_t>(
|
||||
(command_[1] & 7) | // drive and head number
|
||||
0x08 | // single sided
|
||||
(get_drive().get_is_track_zero() ? 0x10 : 0x00) |
|
||||
(get_drive().get_is_ready() ? 0x20 : 0x00) |
|
||||
(get_drive().get_is_read_only() ? 0x40 : 0x00)
|
||||
)
|
||||
};
|
||||
}
|
||||
goto post_result;
|
||||
|
||||
// Performs any invalid command.
|
||||
invalid:
|
||||
// A no-op, but posts ST0 (but which ST0?)
|
||||
result_stack_ = {0x80};
|
||||
goto post_result;
|
||||
|
||||
// Sets abnormal termination of the current command and proceeds to an ST0, ST1, ST2, C, H, R, N result phase.
|
||||
abort:
|
||||
end_writing();
|
||||
SetAbnormalTermination();
|
||||
goto post_st012chrn;
|
||||
|
||||
// Posts ST0, ST1, ST2, C, H, R and N as a result phase.
|
||||
post_st012chrn:
|
||||
SCHEDULE_HEAD_UNLOAD();
|
||||
|
||||
result_stack_ = {size_, sector_, head_, cylinder_, status_[2], status_[1], status_[0]};
|
||||
|
||||
goto post_result;
|
||||
|
||||
// Posts whatever is in result_stack_ as a result phase. Be aware that it is a stack — the
|
||||
// last thing in it will be returned first.
|
||||
post_result:
|
||||
printf("Result to %02x, main %02x: ", command_[0] & 0x1f, main_status_);
|
||||
for(size_t c = 0; c < result_stack_.size(); c++) {
|
||||
printf("%02x ", result_stack_[result_stack_.size() - 1 - c]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Set ready to send data to the processor, no longer in non-DMA execution phase.
|
||||
is_executing_ = false;
|
||||
ResetNonDMAExecution();
|
||||
SetDataRequest();
|
||||
SetDataDirectionToProcessor();
|
||||
|
||||
// The actual stuff of unwinding result_stack_ is handled by ::get_register; wait
|
||||
// until the processor has read all result bytes.
|
||||
WAIT_FOR_EVENT(Event8272::ResultEmpty);
|
||||
|
||||
// Reset data direction and end the command.
|
||||
goto wait_for_command;
|
||||
|
||||
END_SECTION()
|
||||
}
|
||||
|
||||
bool i8272::seek_is_satisfied(int drive) {
|
||||
return (drives_[drive].target_head_position == drives_[drive].head_position) ||
|
||||
(drives_[drive].target_head_position == -1 && get_drive().get_is_track_zero());
|
||||
}
|
||||
|
||||
void i8272::set_dma_acknowledge(bool dack) {
|
||||
}
|
||||
|
||||
void i8272::set_terminal_count(bool tc) {
|
||||
}
|
||||
|
||||
void i8272::set_data_input(uint8_t value) {
|
||||
}
|
||||
|
||||
uint8_t i8272::get_data_output() {
|
||||
return 0xff;
|
||||
}
|
||||
135
Components/8272/i8272.hpp
Normal file
135
Components/8272/i8272.hpp
Normal file
@@ -0,0 +1,135 @@
|
||||
//
|
||||
// i8272.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 05/08/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef i8272_hpp
|
||||
#define i8272_hpp
|
||||
|
||||
#include "../../Storage/Disk/Controller/MFMDiskController.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace Intel {
|
||||
namespace i8272 {
|
||||
|
||||
class BusHandler {
|
||||
public:
|
||||
virtual void set_dma_data_request(bool drq) {}
|
||||
virtual void set_interrupt(bool irq) {}
|
||||
};
|
||||
|
||||
class i8272: public Storage::Disk::MFMController {
|
||||
public:
|
||||
i8272(BusHandler &bus_handler, Cycles clock_rate);
|
||||
|
||||
void run_for(Cycles);
|
||||
|
||||
void set_data_input(uint8_t value);
|
||||
uint8_t get_data_output();
|
||||
|
||||
void set_register(int address, uint8_t value);
|
||||
uint8_t get_register(int address);
|
||||
|
||||
void set_dma_acknowledge(bool dack);
|
||||
void set_terminal_count(bool tc);
|
||||
|
||||
bool is_sleeping();
|
||||
|
||||
protected:
|
||||
virtual void select_drive(int number) = 0;
|
||||
|
||||
private:
|
||||
// The bus handler, for interrupt and DMA-driven usage.
|
||||
BusHandler &bus_handler_;
|
||||
std::unique_ptr<BusHandler> allocated_bus_handler_;
|
||||
|
||||
// Status registers.
|
||||
uint8_t main_status_ = 0;
|
||||
uint8_t status_[3] = {0, 0, 0};
|
||||
|
||||
// A buffer for accumulating the incoming command, and one for accumulating the result.
|
||||
std::vector<uint8_t> command_;
|
||||
std::vector<uint8_t> result_stack_;
|
||||
uint8_t input_ = 0;
|
||||
bool has_input_ = false;
|
||||
bool expects_input_ = false;
|
||||
|
||||
// Event stream: the 8272-specific events, plus the current event state.
|
||||
enum class Event8272: int {
|
||||
CommandByte = (1 << 3),
|
||||
Timer = (1 << 4),
|
||||
ResultEmpty = (1 << 5),
|
||||
NoLongerReady = (1 << 6)
|
||||
};
|
||||
void posit_event(int type);
|
||||
int interesting_event_mask_ = static_cast<int>(Event8272::CommandByte);
|
||||
int resume_point_ = 0;
|
||||
bool is_access_command_ = false;
|
||||
|
||||
// The counter used for ::Timer events.
|
||||
int delay_time_ = 0;
|
||||
|
||||
// The connected drives.
|
||||
struct Drive {
|
||||
uint8_t head_position = 0;
|
||||
|
||||
// Seeking: persistent state.
|
||||
enum Phase {
|
||||
NotSeeking,
|
||||
Seeking,
|
||||
CompletedSeeking
|
||||
} phase = NotSeeking;
|
||||
bool did_seek = false;
|
||||
bool seek_failed = false;
|
||||
|
||||
// Seeking: transient state.
|
||||
int step_rate_counter = 0;
|
||||
int steps_taken = 0;
|
||||
int target_head_position = 0; // either an actual number, or -1 to indicate to step until track zero
|
||||
|
||||
// Head state.
|
||||
int head_unload_delay[2] = {0, 0};
|
||||
bool head_is_loaded[2] = {false, false};
|
||||
|
||||
} drives_[4];
|
||||
int drives_seeking_ = 0;
|
||||
|
||||
/// @returns @c true if the selected drive, which is number @c drive, can stop seeking.
|
||||
bool seek_is_satisfied(int drive);
|
||||
|
||||
// User-supplied parameters; as per the specify command.
|
||||
int step_rate_time_ = 1;
|
||||
int head_unload_time_ = 1;
|
||||
int head_load_time_ = 1;
|
||||
bool dma_mode_ = false;
|
||||
bool is_executing_ = false;
|
||||
|
||||
// A count of head unload timers currently running.
|
||||
int head_timers_running_ = 0;
|
||||
|
||||
// Transient storage and counters used while reading the disk.
|
||||
uint8_t header_[6] = {0, 0, 0, 0, 0, 0};
|
||||
int distance_into_section_ = 0;
|
||||
int index_hole_count_ = 0, index_hole_limit_ = 0;
|
||||
|
||||
// Keeps track of the drive and head in use during commands.
|
||||
int active_drive_ = 0;
|
||||
int active_head_ = 0;
|
||||
|
||||
// Internal registers.
|
||||
uint8_t cylinder_ = 0, head_ = 0, sector_ = 0, size_ = 0;
|
||||
|
||||
// Master switch on not performing any work.
|
||||
bool is_sleeping_ = false;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* i8272_hpp */
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#include "AY38910.hpp"
|
||||
|
||||
using namespace GI;
|
||||
using namespace GI::AY38910;
|
||||
|
||||
AY38910::AY38910() :
|
||||
selected_register_(0),
|
||||
@@ -16,7 +16,8 @@ AY38910::AY38910() :
|
||||
noise_shift_register_(0xffff), noise_period_(0), noise_counter_(0), noise_output_(0),
|
||||
envelope_divider_(0), envelope_period_(0), envelope_position_(0),
|
||||
master_divider_(0),
|
||||
output_registers_{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} {
|
||||
output_registers_{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
port_handler_(nullptr) {
|
||||
output_registers_[8] = output_registers_[9] = output_registers_[10] = 0;
|
||||
|
||||
// set up envelope lookup tables
|
||||
@@ -66,13 +67,13 @@ AY38910::AY38910() :
|
||||
float max_volume = 8192;
|
||||
float root_two = sqrtf(2.0f);
|
||||
for(int v = 0; v < 16; v++) {
|
||||
volumes_[v] = (int)(max_volume / powf(root_two, (float)(v ^ 0xf)));
|
||||
volumes_[v] = static_cast<int>(max_volume / powf(root_two, static_cast<float>(v ^ 0xf)));
|
||||
}
|
||||
volumes_[0] = 0;
|
||||
}
|
||||
|
||||
void AY38910::set_clock_rate(double clock_rate) {
|
||||
set_input_rate((float)clock_rate);
|
||||
set_input_rate(static_cast<float>(clock_rate));
|
||||
}
|
||||
|
||||
void AY38910::get_samples(unsigned int number_of_samples, int16_t *target) {
|
||||
@@ -159,18 +160,21 @@ void AY38910::evaluate_output_volume() {
|
||||
#undef channel_volume
|
||||
|
||||
// Mix additively.
|
||||
output_volume_ = (int16_t)(
|
||||
output_volume_ = static_cast<int16_t>(
|
||||
volumes_[volumes[0]] * channel_levels[0] +
|
||||
volumes_[volumes[1]] * channel_levels[1] +
|
||||
volumes_[volumes[2]] * channel_levels[2]
|
||||
);
|
||||
}
|
||||
|
||||
#pragma mark - Register manipulation
|
||||
|
||||
void AY38910::select_register(uint8_t r) {
|
||||
selected_register_ = r & 0xf;
|
||||
selected_register_ = r;
|
||||
}
|
||||
|
||||
void AY38910::set_register_value(uint8_t value) {
|
||||
if(selected_register_ > 15) return;
|
||||
registers_[selected_register_] = value;
|
||||
if(selected_register_ < 14) {
|
||||
int selected_register = selected_register_;
|
||||
@@ -182,26 +186,22 @@ void AY38910::set_register_value(uint8_t value) {
|
||||
int channel = selected_register >> 1;
|
||||
|
||||
if(selected_register & 1)
|
||||
tone_periods_[channel] = (tone_periods_[channel] & 0xff) | (uint16_t)((value&0xf) << 8);
|
||||
tone_periods_[channel] = (tone_periods_[channel] & 0xff) | static_cast<uint16_t>((value&0xf) << 8);
|
||||
else
|
||||
tone_periods_[channel] = (tone_periods_[channel] & ~0xff) | value;
|
||||
tone_counters_[channel] = tone_periods_[channel];
|
||||
}
|
||||
break;
|
||||
|
||||
case 6:
|
||||
noise_period_ = value & 0x1f;
|
||||
noise_counter_ = noise_period_;
|
||||
break;
|
||||
|
||||
case 11:
|
||||
envelope_period_ = (envelope_period_ & ~0xff) | value;
|
||||
envelope_divider_ = envelope_period_;
|
||||
break;
|
||||
|
||||
case 12:
|
||||
envelope_period_ = (envelope_period_ & 0xff) | (int)(value << 8);
|
||||
envelope_divider_ = envelope_period_;
|
||||
envelope_period_ = (envelope_period_ & 0xff) | static_cast<int>(value << 8);
|
||||
break;
|
||||
|
||||
case 13:
|
||||
@@ -212,53 +212,75 @@ void AY38910::set_register_value(uint8_t value) {
|
||||
output_registers_[selected_register] = masked_value;
|
||||
evaluate_output_volume();
|
||||
});
|
||||
} else {
|
||||
if(port_handler_) port_handler_->set_port_output(selected_register_ == 15, value);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t AY38910::get_register_value() {
|
||||
// This table ensures that bits that aren't defined within the AY are returned as 1s
|
||||
// when read. I can't find documentation on this and don't have a machine to test, so
|
||||
// this is provisionally a guess. TODO: investigate.
|
||||
// This table ensures that bits that aren't defined within the AY are returned as 0s
|
||||
// when read, conforming to CPC-sourced unit tests.
|
||||
const uint8_t register_masks[16] = {
|
||||
0x00, 0xf0, 0x00, 0xf0, 0x00, 0xf0, 0xe0, 0x00,
|
||||
0xe0, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0x00, 0x00
|
||||
0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0x1f, 0xff,
|
||||
0x1f, 0x1f, 0x1f, 0xff, 0xff, 0x0f, 0xff, 0xff
|
||||
};
|
||||
|
||||
return registers_[selected_register_] | register_masks[selected_register_];
|
||||
if(selected_register_ > 15) return 0xff;
|
||||
switch(selected_register_) {
|
||||
default: return registers_[selected_register_] & register_masks[selected_register_];
|
||||
case 14: return (registers_[0x7] & 0x40) ? registers_[14] : port_inputs_[0];
|
||||
case 15: return (registers_[0x7] & 0x80) ? registers_[15] : port_inputs_[1];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Port handling
|
||||
|
||||
uint8_t AY38910::get_port_output(bool port_b) {
|
||||
return registers_[port_b ? 15 : 14];
|
||||
}
|
||||
|
||||
#pragma mark - Bus handling
|
||||
|
||||
void AY38910::set_port_handler(PortHandler *handler) {
|
||||
port_handler_ = handler;
|
||||
}
|
||||
|
||||
void AY38910::set_data_input(uint8_t r) {
|
||||
data_input_ = r;
|
||||
update_bus();
|
||||
}
|
||||
|
||||
uint8_t AY38910::get_data_output() {
|
||||
if(control_state_ == Read && selected_register_ >= 14) {
|
||||
if(port_handler_) {
|
||||
return port_handler_->get_port_input(selected_register_ == 15);
|
||||
} else {
|
||||
return 0xff;
|
||||
}
|
||||
}
|
||||
return data_output_;
|
||||
}
|
||||
|
||||
void AY38910::set_control_lines(ControlLines control_lines) {
|
||||
ControlState new_state;
|
||||
switch((int)control_lines) {
|
||||
default: new_state = Inactive; break;
|
||||
switch(static_cast<int>(control_lines)) {
|
||||
default: control_state_ = Inactive; break;
|
||||
|
||||
case (int)(BCDIR | BC2 | BC1):
|
||||
case BCDIR:
|
||||
case BC1: new_state = LatchAddress; break;
|
||||
case static_cast<int>(BDIR | BC2 | BC1):
|
||||
case BDIR:
|
||||
case BC1: control_state_ = LatchAddress; break;
|
||||
|
||||
case (int)(BC2 | BC1): new_state = Read; break;
|
||||
case (int)(BCDIR | BC2): new_state = Write; break;
|
||||
case static_cast<int>(BC2 | BC1): control_state_ = Read; break;
|
||||
case static_cast<int>(BDIR | BC2): control_state_ = Write; break;
|
||||
}
|
||||
|
||||
if(new_state != control_state_) {
|
||||
control_state_ = new_state;
|
||||
switch(new_state) {
|
||||
default: break;
|
||||
case LatchAddress: select_register(data_input_); break;
|
||||
case Write: set_register_value(data_input_); break;
|
||||
case Read: data_output_ = get_register_value(); break;
|
||||
}
|
||||
update_bus();
|
||||
}
|
||||
|
||||
void AY38910::update_bus() {
|
||||
switch(control_state_) {
|
||||
default: break;
|
||||
case LatchAddress: select_register(data_input_); break;
|
||||
case Write: set_register_value(data_input_); break;
|
||||
case Read: data_output_ = get_register_value(); break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,43 @@
|
||||
#include "../../Outputs/Speaker.hpp"
|
||||
|
||||
namespace GI {
|
||||
namespace AY38910 {
|
||||
|
||||
/*!
|
||||
A port handler provides all input for an AY's two 8-bit ports, and may optionally receive
|
||||
active notification of changes in output.
|
||||
|
||||
Machines with an AY without ports or with nothing wired to them need not supply a port handler.
|
||||
Machines that use the AY ports as output but for which polling for changes is acceptable can
|
||||
instead use AY38910.get_port_output.
|
||||
*/
|
||||
class PortHandler {
|
||||
public:
|
||||
/*!
|
||||
Requests the current input on an AY port.
|
||||
|
||||
@param port_b @c true if the input being queried is Port B. @c false if it is Port A.
|
||||
*/
|
||||
virtual uint8_t get_port_input(bool port_b) {
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
/*!
|
||||
Requests the current input on an AY port.
|
||||
|
||||
@param port_b @c true if the input being queried is Port B. @c false if it is Port A.
|
||||
*/
|
||||
virtual void set_port_output(bool port_b, uint8_t value) {}
|
||||
};
|
||||
|
||||
/*!
|
||||
Names the control lines used as input to the AY, which uses CP1600 bus semantics.
|
||||
*/
|
||||
enum ControlLines {
|
||||
BC1 = (1 << 0),
|
||||
BC2 = (1 << 1),
|
||||
BDIR = (1 << 2)
|
||||
};
|
||||
|
||||
/*!
|
||||
Provides emulation of an AY-3-8910 / YM2149, which is a three-channel sound chip with a
|
||||
@@ -26,19 +63,13 @@ class AY38910: public ::Outputs::Filter<AY38910> {
|
||||
/// Sets the clock rate at which this AY38910 will be run.
|
||||
void set_clock_rate(double clock_rate);
|
||||
|
||||
enum ControlLines {
|
||||
BC1 = (1 << 0),
|
||||
BC2 = (1 << 1),
|
||||
BCDIR = (1 << 2)
|
||||
};
|
||||
|
||||
/// Sets the value the AY would read from its data lines if it were not outputting.
|
||||
void set_data_input(uint8_t r);
|
||||
|
||||
/// Gets the value that would appear on the data lines if only the AY is outputting.
|
||||
uint8_t get_data_output();
|
||||
|
||||
/// Sets the
|
||||
/// Sets the current control line state, as a bit field.
|
||||
void set_control_lines(ControlLines control_lines);
|
||||
|
||||
/*!
|
||||
@@ -47,12 +78,20 @@ class AY38910: public ::Outputs::Filter<AY38910> {
|
||||
*/
|
||||
uint8_t get_port_output(bool port_b);
|
||||
|
||||
/*!
|
||||
Sets the port handler, which will receive a call every time the AY either wants to sample
|
||||
input or else declare new output. As a convenience, current port output can be obtained
|
||||
without installing a port handler via get_port_output.
|
||||
*/
|
||||
void set_port_handler(PortHandler *);
|
||||
|
||||
// to satisfy ::Outputs::Speaker (included via ::Outputs::Filter; not for public consumption
|
||||
void get_samples(unsigned int number_of_samples, int16_t *target);
|
||||
|
||||
private:
|
||||
int selected_register_;
|
||||
uint8_t registers_[16], output_registers_[16];
|
||||
uint8_t port_inputs_[2];
|
||||
|
||||
int master_divider_;
|
||||
|
||||
@@ -88,8 +127,12 @@ class AY38910: public ::Outputs::Filter<AY38910> {
|
||||
|
||||
int16_t output_volume_;
|
||||
inline void evaluate_output_volume();
|
||||
|
||||
inline void update_bus();
|
||||
PortHandler *port_handler_;
|
||||
};
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* AY_3_8910_hpp */
|
||||
|
||||
@@ -46,6 +46,7 @@ AsyncTaskQueue::AsyncTaskQueue()
|
||||
AsyncTaskQueue::~AsyncTaskQueue() {
|
||||
#ifdef __APPLE__
|
||||
dispatch_release(serial_dispatch_queue_);
|
||||
serial_dispatch_queue_ = nullptr;
|
||||
#else
|
||||
should_destruct_ = true;
|
||||
enqueue([](){});
|
||||
|
||||
67
Concurrency/BestEffortUpdater.cpp
Normal file
67
Concurrency/BestEffortUpdater.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// BestEffortUpdater.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 04/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "BestEffortUpdater.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
using namespace Concurrency;
|
||||
|
||||
void BestEffortUpdater::update() {
|
||||
// Perform an update only if one is not currently ongoing.
|
||||
if(!update_is_ongoing_.test_and_set()) {
|
||||
async_task_queue_.enqueue([this]() {
|
||||
// Get time now using the highest-resolution clock provided by the implementation, and determine
|
||||
// the duration since the last time this section was entered.
|
||||
const std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
|
||||
const auto elapsed = now - previous_time_point_;
|
||||
previous_time_point_ = now;
|
||||
|
||||
if(has_previous_time_point_) {
|
||||
// If the duration is valid, convert it to integer cycles, maintaining a rolling error and call the delegate
|
||||
// if there is one. Proceed only if the number of cycles is positive, and cap it to the per-second maximum —
|
||||
// it's possible this is an adjustable clock so be ready to swallow unexpected adjustments.
|
||||
const int64_t duration = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
|
||||
if(duration > 0) {
|
||||
double cycles = ((static_cast<double>(duration) * clock_rate_) / 1e9) + error_;
|
||||
error_ = fmod(cycles, 1.0);
|
||||
|
||||
if(delegate_) {
|
||||
delegate_->update(this, static_cast<int>(std::min(cycles, clock_rate_)), has_skipped_);
|
||||
}
|
||||
has_skipped_ = false;
|
||||
}
|
||||
} else {
|
||||
has_previous_time_point_ = true;
|
||||
}
|
||||
|
||||
// Allow furthers updates to occur.
|
||||
update_is_ongoing_.clear();
|
||||
});
|
||||
} else {
|
||||
async_task_queue_.enqueue([this]() {
|
||||
has_skipped_ = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void BestEffortUpdater::flush() {
|
||||
async_task_queue_.flush();
|
||||
}
|
||||
|
||||
void BestEffortUpdater::set_delegate(Delegate *const delegate) {
|
||||
async_task_queue_.enqueue([this, delegate]() {
|
||||
delegate_ = delegate;
|
||||
});
|
||||
}
|
||||
|
||||
void BestEffortUpdater::set_clock_rate(const double clock_rate) {
|
||||
async_task_queue_.enqueue([this, clock_rate]() {
|
||||
this->clock_rate_ = clock_rate;
|
||||
});
|
||||
}
|
||||
63
Concurrency/BestEffortUpdater.hpp
Normal file
63
Concurrency/BestEffortUpdater.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// BestEffortUpdater.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 04/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef BestEffortUpdater_hpp
|
||||
#define BestEffortUpdater_hpp
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
|
||||
#include "AsyncTaskQueue.hpp"
|
||||
|
||||
namespace Concurrency {
|
||||
|
||||
/*!
|
||||
Accepts timing cues from multiple threads and ensures that a delegate receives calls to total
|
||||
a certain number of cycles per second, that those calls are strictly serialised, and that no
|
||||
backlog of calls accrues.
|
||||
|
||||
No guarantees about the thread that the delegate will be called on are made.
|
||||
*/
|
||||
class BestEffortUpdater {
|
||||
public:
|
||||
/// A delegate receives timing cues.
|
||||
struct Delegate {
|
||||
virtual void update(BestEffortUpdater *updater, int cycles, bool did_skip_previous_update) = 0;
|
||||
};
|
||||
|
||||
/// Sets the current delegate.
|
||||
void set_delegate(Delegate *);
|
||||
|
||||
/// Sets the clock rate of the delegate.
|
||||
void set_clock_rate(double clock_rate);
|
||||
|
||||
/*!
|
||||
If the delegate is not currently in the process of an `update` call, calls it now to catch up to the current time.
|
||||
The call is asynchronous; this method will return immediately.
|
||||
*/
|
||||
void update();
|
||||
|
||||
/// Blocks until any ongoing update is complete.
|
||||
void flush();
|
||||
|
||||
private:
|
||||
std::atomic_flag update_is_ongoing_;
|
||||
AsyncTaskQueue async_task_queue_;
|
||||
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> previous_time_point_;
|
||||
bool has_previous_time_point_ = false;
|
||||
double error_ = 0.0;
|
||||
bool has_skipped_ = false;
|
||||
|
||||
Delegate *delegate_ = nullptr;
|
||||
double clock_rate_ = 1.0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* BestEffortUpdater_hpp */
|
||||
41
Inputs/Joystick.hpp
Normal file
41
Inputs/Joystick.hpp
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Joystick.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 14/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Joystick_hpp
|
||||
#define Joystick_hpp
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Inputs {
|
||||
|
||||
/*!
|
||||
Provides an intermediate idealised model of a simple joystick, allowing a host
|
||||
machine to toggle states, while an interested party either observes or polls.
|
||||
*/
|
||||
class Joystick {
|
||||
public:
|
||||
virtual ~Joystick() {}
|
||||
|
||||
enum class DigitalInput {
|
||||
Up, Down, Left, Right, Fire
|
||||
};
|
||||
|
||||
// Host interface.
|
||||
virtual void set_digital_input(DigitalInput digital_input, bool is_active) = 0;
|
||||
virtual void reset_all_inputs() {
|
||||
set_digital_input(DigitalInput::Up, false);
|
||||
set_digital_input(DigitalInput::Down, false);
|
||||
set_digital_input(DigitalInput::Left, false);
|
||||
set_digital_input(DigitalInput::Right, false);
|
||||
set_digital_input(DigitalInput::Fire, false);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* Joystick_hpp */
|
||||
38
Inputs/Keyboard.cpp
Normal file
38
Inputs/Keyboard.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Keyboard.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/9/17.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "Keyboard.hpp"
|
||||
|
||||
using namespace Inputs;
|
||||
|
||||
Keyboard::Keyboard() {}
|
||||
|
||||
void Keyboard::set_key_pressed(Key key, bool is_pressed) {
|
||||
size_t key_offset = static_cast<size_t>(key);
|
||||
if(key_offset >= key_states_.size()) {
|
||||
key_states_.resize(key_offset+1, false);
|
||||
}
|
||||
key_states_[key_offset] = is_pressed;
|
||||
|
||||
if(delegate_) delegate_->keyboard_did_change_key(this, key, is_pressed);
|
||||
}
|
||||
|
||||
void Keyboard::reset_all_keys() {
|
||||
std::fill(key_states_.begin(), key_states_.end(), false);
|
||||
if(delegate_) delegate_->reset_all_keys(this);
|
||||
}
|
||||
|
||||
void Keyboard::set_delegate(Delegate *delegate) {
|
||||
delegate_ = delegate;
|
||||
}
|
||||
|
||||
bool Keyboard::get_key_state(Key key) {
|
||||
size_t key_offset = static_cast<size_t>(key);
|
||||
if(key_offset >= key_states_.size()) return false;
|
||||
return key_states_[key_offset];
|
||||
}
|
||||
61
Inputs/Keyboard.hpp
Normal file
61
Inputs/Keyboard.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Keyboard.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/9/17.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Keyboard_hpp
|
||||
#define Keyboard_hpp
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Inputs {
|
||||
|
||||
/*!
|
||||
Provides an intermediate idealised model of a modern-era computer keyboard
|
||||
(so, heavily indebted to the current Windows and Mac layouts), allowing a host
|
||||
machine to toggle states, while an interested party either observes or polls.
|
||||
*/
|
||||
class Keyboard {
|
||||
public:
|
||||
Keyboard();
|
||||
|
||||
enum class Key {
|
||||
Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen, ScrollLock, Pause,
|
||||
BackTick, k1, k2, k3, k4, k5, k6, k7, k8, k9, k0, Hyphen, Equals, BackSpace,
|
||||
Tab, Q, W, E, R, T, Y, U, I, O, P, OpenSquareBracket, CloseSquareBracket, BackSlash,
|
||||
CapsLock, A, S, D, F, G, H, J, K, L, Semicolon, Quote, Hash, Enter,
|
||||
LeftShift, Z, X, C, V, B, N, M, Comma, FullStop, ForwardSlash, RightShift,
|
||||
LeftControl, LeftOption, LeftMeta, Space, RightMeta, RightOption, RightControl,
|
||||
Left, Right, Up, Down,
|
||||
Insert, Home, PageUp, Delete, End, PageDown,
|
||||
NumLock, KeyPadSlash, KeyPadAsterisk, KeyPadDelete,
|
||||
KeyPad7, KeyPad8, KeyPad9, KeyPadPlus,
|
||||
KeyPad4, KeyPad5, KeyPad6, KeyPadMinus,
|
||||
KeyPad1, KeyPad2, KeyPad3, KeyPadEnter,
|
||||
KeyPad0, KeyPadDecimalPoint, KeyPadEquals,
|
||||
Help
|
||||
};
|
||||
|
||||
// Host interface.
|
||||
virtual void set_key_pressed(Key key, bool is_pressed);
|
||||
virtual void reset_all_keys();
|
||||
|
||||
// Delegate interface.
|
||||
struct Delegate {
|
||||
virtual void keyboard_did_change_key(Keyboard *keyboard, Key key, bool is_pressed) = 0;
|
||||
virtual void reset_all_keys(Keyboard *keyboard) = 0;
|
||||
};
|
||||
void set_delegate(Delegate *delegate);
|
||||
bool get_key_state(Key key);
|
||||
|
||||
private:
|
||||
std::vector<bool> key_states_;
|
||||
Delegate *delegate_ = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* Keyboard_hpp */
|
||||
1051
Machines/AmstradCPC/AmstradCPC.cpp
Normal file
1051
Machines/AmstradCPC/AmstradCPC.cpp
Normal file
File diff suppressed because it is too large
Load Diff
47
Machines/AmstradCPC/AmstradCPC.hpp
Normal file
47
Machines/AmstradCPC/AmstradCPC.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// AmstradCPC.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 30/07/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef AmstradCPC_hpp
|
||||
#define AmstradCPC_hpp
|
||||
|
||||
#include "../ConfigurationTarget.hpp"
|
||||
#include "../CRTMachine.hpp"
|
||||
#include "../KeyboardMachine.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace AmstradCPC {
|
||||
|
||||
enum ROMType: int {
|
||||
OS464 = 0, BASIC464,
|
||||
OS664, BASIC664,
|
||||
OS6128, BASIC6128,
|
||||
AMSDOS
|
||||
};
|
||||
|
||||
/*!
|
||||
Models an Amstrad CPC.
|
||||
*/
|
||||
class Machine:
|
||||
public CRTMachine::Machine,
|
||||
public ConfigurationTarget::Machine,
|
||||
public KeyboardMachine::Machine {
|
||||
public:
|
||||
virtual ~Machine();
|
||||
|
||||
/// Creates and returns an Amstrad CPC.
|
||||
static Machine *AmstradCPC();
|
||||
|
||||
/// Sets the contents of rom @c type to @c data. Assumed to be a setup step; has no effect once a machine is running.
|
||||
virtual void set_rom(ROMType type, std::vector<uint8_t> data) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* AmstradCPC_hpp */
|
||||
152
Machines/AmstradCPC/Keyboard.cpp
Normal file
152
Machines/AmstradCPC/Keyboard.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
//
|
||||
// Keyboard.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "Keyboard.hpp"
|
||||
|
||||
using namespace AmstradCPC;
|
||||
|
||||
uint16_t KeyboardMapper::mapped_key_for_key(Inputs::Keyboard::Key key) {
|
||||
#define BIND(source, dest) case Inputs::Keyboard::Key::source: return dest
|
||||
switch(key) {
|
||||
default: return KeyCopy;
|
||||
|
||||
BIND(k0, Key0); BIND(k1, Key1); BIND(k2, Key2); BIND(k3, Key3); BIND(k4, Key4);
|
||||
BIND(k5, Key5); BIND(k6, Key6); BIND(k7, Key7); BIND(k8, Key8); BIND(k9, Key9);
|
||||
BIND(Q, KeyQ); BIND(W, KeyW); BIND(E, KeyE); BIND(R, KeyR); BIND(T, KeyT);
|
||||
BIND(Y, KeyY); BIND(U, KeyU); BIND(I, KeyI); BIND(O, KeyO); BIND(P, KeyP);
|
||||
BIND(A, KeyA); BIND(S, KeyS); BIND(D, KeyD); BIND(F, KeyF); BIND(G, KeyG);
|
||||
BIND(H, KeyH); BIND(J, KeyJ); BIND(K, KeyK); BIND(L, KeyL);
|
||||
BIND(Z, KeyZ); BIND(X, KeyX); BIND(C, KeyC); BIND(V, KeyV);
|
||||
BIND(B, KeyB); BIND(N, KeyN); BIND(M, KeyM);
|
||||
|
||||
BIND(Escape, KeyEscape);
|
||||
BIND(F1, KeyF1); BIND(F2, KeyF2); BIND(F3, KeyF3); BIND(F4, KeyF4); BIND(F5, KeyF5);
|
||||
BIND(F6, KeyF6); BIND(F7, KeyF7); BIND(F8, KeyF8); BIND(F9, KeyF9); BIND(F10, KeyF0);
|
||||
|
||||
BIND(F11, KeyRightSquareBracket);
|
||||
BIND(F12, KeyClear);
|
||||
|
||||
BIND(Hyphen, KeyMinus); BIND(Equals, KeyCaret); BIND(BackSpace, KeyDelete);
|
||||
BIND(Tab, KeyTab);
|
||||
|
||||
BIND(OpenSquareBracket, KeyAt);
|
||||
BIND(CloseSquareBracket, KeyLeftSquareBracket);
|
||||
BIND(BackSlash, KeyBackSlash);
|
||||
|
||||
BIND(CapsLock, KeyCapsLock);
|
||||
BIND(Semicolon, KeyColon);
|
||||
BIND(Quote, KeySemicolon);
|
||||
BIND(Hash, KeyRightSquareBracket);
|
||||
BIND(Enter, KeyReturn);
|
||||
|
||||
BIND(LeftShift, KeyShift);
|
||||
BIND(Comma, KeyComma);
|
||||
BIND(FullStop, KeyFullStop);
|
||||
BIND(ForwardSlash, KeyForwardSlash);
|
||||
BIND(RightShift, KeyShift);
|
||||
|
||||
BIND(LeftControl, KeyControl); BIND(LeftOption, KeyControl); BIND(LeftMeta, KeyControl);
|
||||
BIND(Space, KeySpace);
|
||||
BIND(RightMeta, KeyControl); BIND(RightOption, KeyControl); BIND(RightControl, KeyControl);
|
||||
|
||||
BIND(Left, KeyLeft); BIND(Right, KeyRight);
|
||||
BIND(Up, KeyUp); BIND(Down, KeyDown);
|
||||
|
||||
BIND(KeyPad0, KeyF0);
|
||||
BIND(KeyPad1, KeyF1); BIND(KeyPad2, KeyF2); BIND(KeyPad3, KeyF3);
|
||||
BIND(KeyPad4, KeyF4); BIND(KeyPad5, KeyF5); BIND(KeyPad6, KeyF6);
|
||||
BIND(KeyPad7, KeyF7); BIND(KeyPad8, KeyF8); BIND(KeyPad9, KeyF9);
|
||||
BIND(KeyPadPlus, KeySemicolon);
|
||||
BIND(KeyPadMinus, KeyMinus);
|
||||
|
||||
BIND(KeyPadEnter, KeyEnter);
|
||||
BIND(KeyPadDecimalPoint, KeyFullStop);
|
||||
BIND(KeyPadEquals, KeyMinus);
|
||||
BIND(KeyPadSlash, KeyForwardSlash);
|
||||
BIND(KeyPadAsterisk, KeyColon);
|
||||
BIND(KeyPadDelete, KeyDelete);
|
||||
}
|
||||
#undef BIND
|
||||
}
|
||||
|
||||
uint16_t *CharacterMapper::sequence_for_character(char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define SHIFT(...) {KeyShift, __VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define X {KeyboardMachine::Machine::KeyNotMapped}
|
||||
static KeySequence key_sequences[] = {
|
||||
/* NUL */ X, /* SOH */ X,
|
||||
/* STX */ X, /* ETX */ X,
|
||||
/* EOT */ X, /* ENQ */ X,
|
||||
/* ACK */ X, /* BEL */ X,
|
||||
/* BS */ KEYS(KeyDelete), /* HT */ X,
|
||||
/* LF */ KEYS(KeyReturn), /* VT */ X,
|
||||
/* FF */ X, /* CR */ X,
|
||||
/* SO */ X, /* SI */ X,
|
||||
/* DLE */ X, /* DC1 */ X,
|
||||
/* DC2 */ X, /* DC3 */ X,
|
||||
/* DC4 */ X, /* NAK */ X,
|
||||
/* SYN */ X, /* ETB */ X,
|
||||
/* CAN */ X, /* EM */ X,
|
||||
/* SUB */ X, /* ESC */ X,
|
||||
/* FS */ X, /* GS */ X,
|
||||
/* RS */ X, /* US */ X,
|
||||
/* space */ KEYS(KeySpace), /* ! */ SHIFT(Key1),
|
||||
/* " */ SHIFT(Key2), /* # */ SHIFT(Key3),
|
||||
/* $ */ SHIFT(Key4), /* % */ SHIFT(Key5),
|
||||
/* & */ SHIFT(Key6), /* ' */ SHIFT(Key7),
|
||||
/* ( */ SHIFT(Key8), /* ) */ SHIFT(Key9),
|
||||
/* * */ SHIFT(KeyColon), /* + */ SHIFT(KeySemicolon),
|
||||
/* , */ KEYS(KeyComma), /* - */ KEYS(KeyMinus),
|
||||
/* . */ KEYS(KeyFullStop), /* / */ KEYS(KeyForwardSlash),
|
||||
/* 0 */ KEYS(Key0), /* 1 */ KEYS(Key1),
|
||||
/* 2 */ KEYS(Key2), /* 3 */ KEYS(Key3),
|
||||
/* 4 */ KEYS(Key4), /* 5 */ KEYS(Key5),
|
||||
/* 6 */ KEYS(Key6), /* 7 */ KEYS(Key7),
|
||||
/* 8 */ KEYS(Key8), /* 9 */ KEYS(Key9),
|
||||
/* : */ KEYS(KeyColon), /* ; */ KEYS(KeySemicolon),
|
||||
/* < */ SHIFT(KeyComma), /* = */ SHIFT(KeyMinus),
|
||||
/* > */ SHIFT(KeyFullStop), /* ? */ SHIFT(KeyForwardSlash),
|
||||
/* @ */ SHIFT(KeyAt), /* A */ SHIFT(KeyA),
|
||||
/* B */ SHIFT(KeyB), /* C */ SHIFT(KeyC),
|
||||
/* D */ SHIFT(KeyD), /* E */ SHIFT(KeyE),
|
||||
/* F */ SHIFT(KeyF), /* G */ SHIFT(KeyG),
|
||||
/* H */ SHIFT(KeyH), /* I */ SHIFT(KeyI),
|
||||
/* J */ SHIFT(KeyJ), /* K */ SHIFT(KeyK),
|
||||
/* L */ SHIFT(KeyL), /* M */ SHIFT(KeyM),
|
||||
/* N */ SHIFT(KeyN), /* O */ SHIFT(KeyO),
|
||||
/* P */ SHIFT(KeyP), /* Q */ SHIFT(KeyQ),
|
||||
/* R */ SHIFT(KeyR), /* S */ SHIFT(KeyS),
|
||||
/* T */ SHIFT(KeyT), /* U */ SHIFT(KeyU),
|
||||
/* V */ SHIFT(KeyV), /* W */ SHIFT(KeyW),
|
||||
/* X */ SHIFT(KeyX), /* Y */ SHIFT(KeyY),
|
||||
/* Z */ SHIFT(KeyZ), /* [ */ KEYS(KeyLeftSquareBracket),
|
||||
/* \ */ KEYS(KeyBackSlash), /* ] */ KEYS(KeyRightSquareBracket),
|
||||
/* ^ */ SHIFT(KeyCaret), /* _ */ SHIFT(Key0),
|
||||
/* ` */ X, /* a */ KEYS(KeyA),
|
||||
/* b */ KEYS(KeyB), /* c */ KEYS(KeyC),
|
||||
/* d */ KEYS(KeyD), /* e */ KEYS(KeyE),
|
||||
/* f */ KEYS(KeyF), /* g */ KEYS(KeyG),
|
||||
/* h */ KEYS(KeyH), /* i */ KEYS(KeyI),
|
||||
/* j */ KEYS(KeyJ), /* k */ KEYS(KeyK),
|
||||
/* l */ KEYS(KeyL), /* m */ KEYS(KeyM),
|
||||
/* n */ KEYS(KeyN), /* o */ KEYS(KeyO),
|
||||
/* p */ KEYS(KeyP), /* q */ KEYS(KeyQ),
|
||||
/* r */ KEYS(KeyR), /* s */ KEYS(KeyS),
|
||||
/* t */ KEYS(KeyT), /* u */ KEYS(KeyU),
|
||||
/* v */ KEYS(KeyV), /* w */ KEYS(KeyW),
|
||||
/* x */ KEYS(KeyX), /* y */ KEYS(KeyY),
|
||||
/* z */ KEYS(KeyZ), /* { */ X,
|
||||
/* | */ SHIFT(KeyAt), /* } */ X,
|
||||
/* ~ */ X
|
||||
};
|
||||
#undef KEYS
|
||||
#undef SHIFT
|
||||
#undef X
|
||||
|
||||
return table_lookup_sequence_for_character(key_sequences, sizeof(key_sequences), character);
|
||||
}
|
||||
46
Machines/AmstradCPC/Keyboard.hpp
Normal file
46
Machines/AmstradCPC/Keyboard.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Keyboard.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Machines_AmstradCPC_Keyboard_hpp
|
||||
#define Machines_AmstradCPC_Keyboard_hpp
|
||||
|
||||
#include "../KeyboardMachine.hpp"
|
||||
#include "../Utility/Typer.hpp"
|
||||
|
||||
namespace AmstradCPC {
|
||||
|
||||
enum Key: uint16_t {
|
||||
#define Line(l, k1, k2, k3, k4, k5, k6, k7, k8) \
|
||||
k1 = (l << 4) | 0x07, k2 = (l << 4) | 0x06, k3 = (l << 4) | 0x05, k4 = (l << 4) | 0x04,\
|
||||
k5 = (l << 4) | 0x03, k6 = (l << 4) | 0x02, k7 = (l << 4) | 0x01, k8 = (l << 4) | 0x00,
|
||||
|
||||
Line(0, KeyFDot, KeyEnter, KeyF3, KeyF6, KeyF9, KeyDown, KeyRight, KeyUp)
|
||||
Line(1, KeyF0, KeyF2, KeyF1, KeyF5, KeyF8, KeyF7, KeyCopy, KeyLeft)
|
||||
Line(2, KeyControl, KeyBackSlash, KeyShift, KeyF4, KeyRightSquareBracket, KeyReturn, KeyLeftSquareBracket, KeyClear)
|
||||
Line(3, KeyFullStop, KeyForwardSlash, KeyColon, KeySemicolon, KeyP, KeyAt, KeyMinus, KeyCaret)
|
||||
Line(4, KeyComma, KeyM, KeyK, KeyL, KeyI, KeyO, Key9, Key0)
|
||||
Line(5, KeySpace, KeyN, KeyJ, KeyH, KeyY, KeyU, Key7, Key8)
|
||||
Line(6, KeyV, KeyB, KeyF, KeyG, KeyT, KeyR, Key5, Key6)
|
||||
Line(7, KeyX, KeyC, KeyD, KeyS, KeyW, KeyE, Key3, Key4)
|
||||
Line(8, KeyZ, KeyCapsLock, KeyA, KeyTab, KeyQ, KeyEscape, Key2, Key1)
|
||||
Line(9, KeyDelete, KeyJoy1Fire3, KeyJoy2Fire2, KeyJoy1Fire1, KeyJoy1Right, KeyJoy1Left, KeyJoy1Down, KeyJoy1Up)
|
||||
|
||||
#undef Line
|
||||
};
|
||||
|
||||
struct KeyboardMapper: public KeyboardMachine::Machine::KeyboardMapper {
|
||||
uint16_t mapped_key_for_key(Inputs::Keyboard::Key key);
|
||||
};
|
||||
|
||||
struct CharacterMapper: public ::Utility::CharacterMapper {
|
||||
uint16_t *sequence_for_character(char character);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif /* KeyboardMapper_hpp */
|
||||
@@ -10,148 +10,213 @@
|
||||
#include <algorithm>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "Cartridges/CartridgeAtari8k.hpp"
|
||||
#include "Cartridges/CartridgeAtari16k.hpp"
|
||||
#include "Cartridges/CartridgeAtari32k.hpp"
|
||||
#include "Cartridges/CartridgeActivisionStack.hpp"
|
||||
#include "Cartridges/CartridgeCBSRAMPlus.hpp"
|
||||
#include "Cartridges/CartridgeCommaVid.hpp"
|
||||
#include "Cartridges/CartridgeMegaBoy.hpp"
|
||||
#include "Cartridges/CartridgeMNetwork.hpp"
|
||||
#include "Cartridges/CartridgeParkerBros.hpp"
|
||||
#include "Cartridges/CartridgePitfall2.hpp"
|
||||
#include "Cartridges/CartridgeTigervision.hpp"
|
||||
#include "Cartridges/CartridgeUnpaged.hpp"
|
||||
#include "Cartridges/Atari8k.hpp"
|
||||
#include "Cartridges/Atari16k.hpp"
|
||||
#include "Cartridges/Atari32k.hpp"
|
||||
#include "Cartridges/ActivisionStack.hpp"
|
||||
#include "Cartridges/CBSRAMPlus.hpp"
|
||||
#include "Cartridges/CommaVid.hpp"
|
||||
#include "Cartridges/MegaBoy.hpp"
|
||||
#include "Cartridges/MNetwork.hpp"
|
||||
#include "Cartridges/ParkerBros.hpp"
|
||||
#include "Cartridges/Pitfall2.hpp"
|
||||
#include "Cartridges/Tigervision.hpp"
|
||||
#include "Cartridges/Unpaged.hpp"
|
||||
|
||||
using namespace Atari2600;
|
||||
namespace {
|
||||
static const double NTSC_clock_rate = 1194720;
|
||||
static const double PAL_clock_rate = 1182298;
|
||||
}
|
||||
|
||||
Machine::Machine() :
|
||||
frame_record_pointer_(0),
|
||||
is_ntsc_(true) {
|
||||
set_clock_rate(NTSC_clock_rate);
|
||||
}
|
||||
namespace Atari2600 {
|
||||
|
||||
void Machine::setup_output(float aspect_ratio) {
|
||||
bus_->tia_.reset(new TIA);
|
||||
bus_->speaker_.reset(new Speaker);
|
||||
bus_->speaker_->set_input_rate((float)(get_clock_rate() / (double)CPUTicksPerAudioTick));
|
||||
bus_->tia_->get_crt()->set_delegate(this);
|
||||
}
|
||||
class Joystick: public Inputs::Joystick {
|
||||
public:
|
||||
Joystick(Bus *bus, size_t shift, size_t fire_tia_input) :
|
||||
bus_(bus), shift_(shift), fire_tia_input_(fire_tia_input) {}
|
||||
|
||||
void Machine::close_output() {
|
||||
bus_.reset();
|
||||
}
|
||||
void set_digital_input(DigitalInput digital_input, bool is_active) {
|
||||
switch(digital_input) {
|
||||
case DigitalInput::Up: bus_->mos6532_.update_port_input(0, 0x10 >> shift_, is_active); break;
|
||||
case DigitalInput::Down: bus_->mos6532_.update_port_input(0, 0x20 >> shift_, is_active); break;
|
||||
case DigitalInput::Left: bus_->mos6532_.update_port_input(0, 0x40 >> shift_, is_active); break;
|
||||
case DigitalInput::Right: bus_->mos6532_.update_port_input(0, 0x80 >> shift_, is_active); break;
|
||||
|
||||
Machine::~Machine() {
|
||||
close_output();
|
||||
}
|
||||
|
||||
void Machine::set_digital_input(Atari2600DigitalInput input, bool state) {
|
||||
switch (input) {
|
||||
case Atari2600DigitalInputJoy1Up: bus_->mos6532_.update_port_input(0, 0x10, state); break;
|
||||
case Atari2600DigitalInputJoy1Down: bus_->mos6532_.update_port_input(0, 0x20, state); break;
|
||||
case Atari2600DigitalInputJoy1Left: bus_->mos6532_.update_port_input(0, 0x40, state); break;
|
||||
case Atari2600DigitalInputJoy1Right: bus_->mos6532_.update_port_input(0, 0x80, state); break;
|
||||
|
||||
case Atari2600DigitalInputJoy2Up: bus_->mos6532_.update_port_input(0, 0x01, state); break;
|
||||
case Atari2600DigitalInputJoy2Down: bus_->mos6532_.update_port_input(0, 0x02, state); break;
|
||||
case Atari2600DigitalInputJoy2Left: bus_->mos6532_.update_port_input(0, 0x04, state); break;
|
||||
case Atari2600DigitalInputJoy2Right: bus_->mos6532_.update_port_input(0, 0x08, state); break;
|
||||
|
||||
// TODO: latching
|
||||
case Atari2600DigitalInputJoy1Fire: if(state) bus_->tia_input_value_[0] &= ~0x80; else bus_->tia_input_value_[0] |= 0x80; break;
|
||||
case Atari2600DigitalInputJoy2Fire: if(state) bus_->tia_input_value_[1] &= ~0x80; else bus_->tia_input_value_[1] |= 0x80; break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void Machine::set_switch_is_enabled(Atari2600Switch input, bool state) {
|
||||
switch(input) {
|
||||
case Atari2600SwitchReset: bus_->mos6532_.update_port_input(1, 0x01, state); break;
|
||||
case Atari2600SwitchSelect: bus_->mos6532_.update_port_input(1, 0x02, state); break;
|
||||
case Atari2600SwitchColour: bus_->mos6532_.update_port_input(1, 0x08, state); break;
|
||||
case Atari2600SwitchLeftPlayerDifficulty: bus_->mos6532_.update_port_input(1, 0x40, state); break;
|
||||
case Atari2600SwitchRightPlayerDifficulty: bus_->mos6532_.update_port_input(1, 0x80, state); break;
|
||||
}
|
||||
}
|
||||
|
||||
void Machine::configure_as_target(const StaticAnalyser::Target &target) {
|
||||
const std::vector<uint8_t> &rom = target.cartridges.front()->get_segments().front().data;
|
||||
switch(target.atari.paging_model) {
|
||||
case StaticAnalyser::Atari2600PagingModel::ActivisionStack: bus_.reset(new CartridgeActivisionStack(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::CBSRamPlus: bus_.reset(new CartridgeCBSRAMPlus(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::CommaVid: bus_.reset(new CartridgeCommaVid(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::MegaBoy: bus_.reset(new CartridgeMegaBoy(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::MNetwork: bus_.reset(new CartridgeMNetwork(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::None: bus_.reset(new CartridgeUnpaged(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::ParkerBros: bus_.reset(new CartridgeParkerBros(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::Pitfall2: bus_.reset(new CartridgePitfall2(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::Tigervision: bus_.reset(new CartridgeTigervision(rom)); break;
|
||||
|
||||
case StaticAnalyser::Atari2600PagingModel::Atari8k:
|
||||
if(target.atari.uses_superchip) {
|
||||
bus_.reset(new CartridgeAtari8kSuperChip(rom));
|
||||
} else {
|
||||
bus_.reset(new CartridgeAtari8k(rom));
|
||||
// TODO: latching
|
||||
case DigitalInput::Fire:
|
||||
if(is_active)
|
||||
bus_->tia_input_value_[fire_tia_input_] &= ~0x80;
|
||||
else
|
||||
bus_->tia_input_value_[fire_tia_input_] |= 0x80;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case StaticAnalyser::Atari2600PagingModel::Atari16k:
|
||||
if(target.atari.uses_superchip) {
|
||||
bus_.reset(new CartridgeAtari16kSuperChip(rom));
|
||||
} else {
|
||||
bus_.reset(new CartridgeAtari16k(rom));
|
||||
}
|
||||
break;
|
||||
case StaticAnalyser::Atari2600PagingModel::Atari32k:
|
||||
if(target.atari.uses_superchip) {
|
||||
bus_.reset(new CartridgeAtari32kSuperChip(rom));
|
||||
} else {
|
||||
bus_.reset(new CartridgeAtari32k(rom));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - CRT delegate
|
||||
|
||||
void Machine::crt_did_end_batch_of_frames(Outputs::CRT::CRT *crt, unsigned int number_of_frames, unsigned int number_of_unexpected_vertical_syncs) {
|
||||
const size_t number_of_frame_records = sizeof(frame_records_) / sizeof(frame_records_[0]);
|
||||
frame_records_[frame_record_pointer_ % number_of_frame_records].number_of_frames = number_of_frames;
|
||||
frame_records_[frame_record_pointer_ % number_of_frame_records].number_of_unexpected_vertical_syncs = number_of_unexpected_vertical_syncs;
|
||||
frame_record_pointer_ ++;
|
||||
|
||||
if(frame_record_pointer_ >= 6) {
|
||||
unsigned int total_number_of_frames = 0;
|
||||
unsigned int total_number_of_unexpected_vertical_syncs = 0;
|
||||
for(size_t c = 0; c < number_of_frame_records; c++) {
|
||||
total_number_of_frames += frame_records_[c].number_of_frames;
|
||||
total_number_of_unexpected_vertical_syncs += frame_records_[c].number_of_unexpected_vertical_syncs;
|
||||
}
|
||||
|
||||
if(total_number_of_unexpected_vertical_syncs >= total_number_of_frames >> 1) {
|
||||
for(size_t c = 0; c < number_of_frame_records; c++) {
|
||||
frame_records_[c].number_of_frames = 0;
|
||||
frame_records_[c].number_of_unexpected_vertical_syncs = 0;
|
||||
}
|
||||
is_ntsc_ ^= true;
|
||||
private:
|
||||
size_t shift_, fire_tia_input_;
|
||||
Bus *bus_;
|
||||
};
|
||||
|
||||
double clock_rate;
|
||||
if(is_ntsc_) {
|
||||
clock_rate = NTSC_clock_rate;
|
||||
bus_->tia_->set_output_mode(TIA::OutputMode::NTSC);
|
||||
} else {
|
||||
clock_rate = PAL_clock_rate;
|
||||
bus_->tia_->set_output_mode(TIA::OutputMode::PAL);
|
||||
}
|
||||
|
||||
bus_->speaker_->set_input_rate((float)(clock_rate / (double)CPUTicksPerAudioTick));
|
||||
bus_->speaker_->set_high_frequency_cut_off((float)(clock_rate / ((double)CPUTicksPerAudioTick * 2.0)));
|
||||
set_clock_rate(clock_rate);
|
||||
class ConcreteMachine:
|
||||
public Machine,
|
||||
public Outputs::CRT::Delegate {
|
||||
public:
|
||||
ConcreteMachine() :
|
||||
frame_record_pointer_(0),
|
||||
is_ntsc_(true) {
|
||||
set_clock_rate(NTSC_clock_rate);
|
||||
}
|
||||
}
|
||||
|
||||
~ConcreteMachine() {
|
||||
close_output();
|
||||
}
|
||||
|
||||
void configure_as_target(const StaticAnalyser::Target &target) override {
|
||||
const std::vector<uint8_t> &rom = target.media.cartridges.front()->get_segments().front().data;
|
||||
switch(target.atari.paging_model) {
|
||||
case StaticAnalyser::Atari2600PagingModel::ActivisionStack: bus_.reset(new Cartridge::Cartridge<Cartridge::ActivisionStack>(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::CBSRamPlus: bus_.reset(new Cartridge::Cartridge<Cartridge::CBSRAMPlus>(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::CommaVid: bus_.reset(new Cartridge::Cartridge<Cartridge::CommaVid>(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::MegaBoy: bus_.reset(new Cartridge::Cartridge<Cartridge::MegaBoy>(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::MNetwork: bus_.reset(new Cartridge::Cartridge<Cartridge::MNetwork>(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::None: bus_.reset(new Cartridge::Cartridge<Cartridge::Unpaged>(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::ParkerBros: bus_.reset(new Cartridge::Cartridge<Cartridge::ParkerBros>(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::Pitfall2: bus_.reset(new Cartridge::Cartridge<Cartridge::Pitfall2>(rom)); break;
|
||||
case StaticAnalyser::Atari2600PagingModel::Tigervision: bus_.reset(new Cartridge::Cartridge<Cartridge::Tigervision>(rom)); break;
|
||||
|
||||
case StaticAnalyser::Atari2600PagingModel::Atari8k:
|
||||
if(target.atari.uses_superchip) {
|
||||
bus_.reset(new Cartridge::Cartridge<Cartridge::Atari8kSuperChip>(rom));
|
||||
} else {
|
||||
bus_.reset(new Cartridge::Cartridge<Cartridge::Atari8k>(rom));
|
||||
}
|
||||
break;
|
||||
case StaticAnalyser::Atari2600PagingModel::Atari16k:
|
||||
if(target.atari.uses_superchip) {
|
||||
bus_.reset(new Cartridge::Cartridge<Cartridge::Atari16kSuperChip>(rom));
|
||||
} else {
|
||||
bus_.reset(new Cartridge::Cartridge<Cartridge::Atari16k>(rom));
|
||||
}
|
||||
break;
|
||||
case StaticAnalyser::Atari2600PagingModel::Atari32k:
|
||||
if(target.atari.uses_superchip) {
|
||||
bus_.reset(new Cartridge::Cartridge<Cartridge::Atari32kSuperChip>(rom));
|
||||
} else {
|
||||
bus_.reset(new Cartridge::Cartridge<Cartridge::Atari32k>(rom));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
joysticks_.emplace_back(new Joystick(bus_.get(), 0, 0));
|
||||
joysticks_.emplace_back(new Joystick(bus_.get(), 4, 1));
|
||||
}
|
||||
|
||||
bool insert_media(const StaticAnalyser::Media &media) override {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Inputs::Joystick>> &get_joysticks() override {
|
||||
return joysticks_;
|
||||
}
|
||||
|
||||
void set_switch_is_enabled(Atari2600Switch input, bool state) override {
|
||||
switch(input) {
|
||||
case Atari2600SwitchReset: bus_->mos6532_.update_port_input(1, 0x01, state); break;
|
||||
case Atari2600SwitchSelect: bus_->mos6532_.update_port_input(1, 0x02, state); break;
|
||||
case Atari2600SwitchColour: bus_->mos6532_.update_port_input(1, 0x08, state); break;
|
||||
case Atari2600SwitchLeftPlayerDifficulty: bus_->mos6532_.update_port_input(1, 0x40, state); break;
|
||||
case Atari2600SwitchRightPlayerDifficulty: bus_->mos6532_.update_port_input(1, 0x80, state); break;
|
||||
}
|
||||
}
|
||||
|
||||
void set_reset_switch(bool state) override {
|
||||
bus_->set_reset_line(state);
|
||||
}
|
||||
|
||||
// to satisfy CRTMachine::Machine
|
||||
void setup_output(float aspect_ratio) override {
|
||||
bus_->tia_.reset(new TIA);
|
||||
bus_->speaker_.reset(new Speaker);
|
||||
bus_->speaker_->set_input_rate(static_cast<float>(get_clock_rate() / static_cast<double>(CPUTicksPerAudioTick)));
|
||||
bus_->tia_->get_crt()->set_delegate(this);
|
||||
}
|
||||
|
||||
void close_output() override {
|
||||
bus_.reset();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> get_crt() override {
|
||||
return bus_->tia_->get_crt();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::Speaker> get_speaker() override {
|
||||
return bus_->speaker_;
|
||||
}
|
||||
|
||||
void run_for(const Cycles cycles) override {
|
||||
bus_->run_for(cycles);
|
||||
}
|
||||
|
||||
// to satisfy Outputs::CRT::Delegate
|
||||
void crt_did_end_batch_of_frames(Outputs::CRT::CRT *crt, unsigned int number_of_frames, unsigned int number_of_unexpected_vertical_syncs) override {
|
||||
const size_t number_of_frame_records = sizeof(frame_records_) / sizeof(frame_records_[0]);
|
||||
frame_records_[frame_record_pointer_ % number_of_frame_records].number_of_frames = number_of_frames;
|
||||
frame_records_[frame_record_pointer_ % number_of_frame_records].number_of_unexpected_vertical_syncs = number_of_unexpected_vertical_syncs;
|
||||
frame_record_pointer_ ++;
|
||||
|
||||
if(frame_record_pointer_ >= 6) {
|
||||
unsigned int total_number_of_frames = 0;
|
||||
unsigned int total_number_of_unexpected_vertical_syncs = 0;
|
||||
for(size_t c = 0; c < number_of_frame_records; c++) {
|
||||
total_number_of_frames += frame_records_[c].number_of_frames;
|
||||
total_number_of_unexpected_vertical_syncs += frame_records_[c].number_of_unexpected_vertical_syncs;
|
||||
}
|
||||
|
||||
if(total_number_of_unexpected_vertical_syncs >= total_number_of_frames >> 1) {
|
||||
for(size_t c = 0; c < number_of_frame_records; c++) {
|
||||
frame_records_[c].number_of_frames = 0;
|
||||
frame_records_[c].number_of_unexpected_vertical_syncs = 0;
|
||||
}
|
||||
is_ntsc_ ^= true;
|
||||
|
||||
double clock_rate;
|
||||
if(is_ntsc_) {
|
||||
clock_rate = NTSC_clock_rate;
|
||||
bus_->tia_->set_output_mode(TIA::OutputMode::NTSC);
|
||||
} else {
|
||||
clock_rate = PAL_clock_rate;
|
||||
bus_->tia_->set_output_mode(TIA::OutputMode::PAL);
|
||||
}
|
||||
|
||||
bus_->speaker_->set_input_rate(static_cast<float>(clock_rate / static_cast<double>(CPUTicksPerAudioTick)));
|
||||
bus_->speaker_->set_high_frequency_cut_off(static_cast<float>(clock_rate / (static_cast<double>(CPUTicksPerAudioTick) * 2.0)));
|
||||
set_clock_rate(clock_rate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
// the bus
|
||||
std::unique_ptr<Bus> bus_;
|
||||
|
||||
// output frame rate tracker
|
||||
struct FrameRecord {
|
||||
unsigned int number_of_frames;
|
||||
unsigned int number_of_unexpected_vertical_syncs;
|
||||
|
||||
FrameRecord() : number_of_frames(0), number_of_unexpected_vertical_syncs(0) {}
|
||||
} frame_records_[4];
|
||||
unsigned int frame_record_pointer_;
|
||||
bool is_ntsc_;
|
||||
std::vector<std::unique_ptr<Inputs::Joystick>> joysticks_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using namespace Atari2600;
|
||||
|
||||
Machine *Machine::Atari2600() {
|
||||
return new Atari2600::ConcreteMachine;
|
||||
}
|
||||
|
||||
Machine::~Machine() {}
|
||||
|
||||
@@ -9,59 +9,32 @@
|
||||
#ifndef Atari2600_cpp
|
||||
#define Atari2600_cpp
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../../Processors/6502/6502.hpp"
|
||||
#include "../CRTMachine.hpp"
|
||||
#include "Bus.hpp"
|
||||
#include "PIA.hpp"
|
||||
#include "Speaker.hpp"
|
||||
#include "TIA.hpp"
|
||||
|
||||
#include "../ConfigurationTarget.hpp"
|
||||
#include "../CRTMachine.hpp"
|
||||
#include "../JoystickMachine.hpp"
|
||||
|
||||
#include "Atari2600Inputs.h"
|
||||
|
||||
namespace Atari2600 {
|
||||
|
||||
/*!
|
||||
Models an Atari 2600.
|
||||
*/
|
||||
class Machine:
|
||||
public CRTMachine::Machine,
|
||||
public ConfigurationTarget::Machine,
|
||||
public Outputs::CRT::Delegate {
|
||||
|
||||
public JoystickMachine::Machine {
|
||||
public:
|
||||
Machine();
|
||||
~Machine();
|
||||
virtual ~Machine();
|
||||
|
||||
void configure_as_target(const StaticAnalyser::Target &target);
|
||||
void switch_region();
|
||||
/// Creates and returns an Atari 2600 on the heap.
|
||||
static Machine *Atari2600();
|
||||
|
||||
void set_digital_input(Atari2600DigitalInput input, bool state);
|
||||
void set_switch_is_enabled(Atari2600Switch input, bool state);
|
||||
void set_reset_line(bool state) { bus_->set_reset_line(state); }
|
||||
/// Sets the switch @c input to @c state.
|
||||
virtual void set_switch_is_enabled(Atari2600Switch input, bool state) = 0;
|
||||
|
||||
// to satisfy CRTMachine::Machine
|
||||
virtual void setup_output(float aspect_ratio);
|
||||
virtual void close_output();
|
||||
virtual std::shared_ptr<Outputs::CRT::CRT> get_crt() { return bus_->tia_->get_crt(); }
|
||||
virtual std::shared_ptr<Outputs::Speaker> get_speaker() { return bus_->speaker_; }
|
||||
virtual void run_for(const Cycles cycles) { bus_->run_for(cycles); }
|
||||
|
||||
// to satisfy Outputs::CRT::Delegate
|
||||
virtual void crt_did_end_batch_of_frames(Outputs::CRT::CRT *crt, unsigned int number_of_frames, unsigned int number_of_unexpected_vertical_syncs);
|
||||
|
||||
private:
|
||||
// the bus
|
||||
std::unique_ptr<Bus> bus_;
|
||||
|
||||
// output frame rate tracker
|
||||
struct FrameRecord {
|
||||
unsigned int number_of_frames;
|
||||
unsigned int number_of_unexpected_vertical_syncs;
|
||||
|
||||
FrameRecord() : number_of_frames(0), number_of_unexpected_vertical_syncs(0) {}
|
||||
} frame_records_[4];
|
||||
unsigned int frame_record_pointer_;
|
||||
bool is_ntsc_;
|
||||
// Presses or releases the reset button.
|
||||
virtual void set_reset_switch(bool state) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -6,18 +6,18 @@
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Atari2600_CartridgeActivisionStack_hpp
|
||||
#define Atari2600_CartridgeActivisionStack_hpp
|
||||
#ifndef Atari2600_ActivisionStack_hpp
|
||||
#define Atari2600_ActivisionStack_hpp
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeActivisionStack: public Cartridge<CartridgeActivisionStack> {
|
||||
class ActivisionStack: public BusExtender {
|
||||
public:
|
||||
CartridgeActivisionStack(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom),
|
||||
last_opcode_(0x00) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
ActivisionStack(uint8_t *rom_base, size_t rom_size) :
|
||||
BusExtender(rom_base, rom_size),
|
||||
rom_ptr_(rom_base),
|
||||
last_opcode_(0x00) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
if(!(address & 0x1000)) return;
|
||||
@@ -27,9 +27,9 @@ class CartridgeActivisionStack: public Cartridge<CartridgeActivisionStack> {
|
||||
// RST or JSR.
|
||||
if(operation == CPU::MOS6502::BusOperation::ReadOpcode && (last_opcode_ == 0x20 || last_opcode_ == 0x60)) {
|
||||
if(address & 0x2000) {
|
||||
rom_ptr_ = rom_.data();
|
||||
rom_ptr_ = rom_base_;
|
||||
} else {
|
||||
rom_ptr_ = rom_.data() + 4096;
|
||||
rom_ptr_ = rom_base_ + 4096;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ class CartridgeActivisionStack: public Cartridge<CartridgeActivisionStack> {
|
||||
uint8_t last_opcode_;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeActivisionStack_hpp */
|
||||
@@ -12,19 +12,19 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeAtari16k: public Cartridge<CartridgeAtari16k> {
|
||||
class Atari16k: public BusExtender {
|
||||
public:
|
||||
CartridgeAtari16k(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
Atari16k(uint8_t *rom_base, size_t rom_size) :
|
||||
BusExtender(rom_base, rom_size),
|
||||
rom_ptr_(rom_base) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
address &= 0x1fff;
|
||||
if(!(address & 0x1000)) return;
|
||||
|
||||
if(address >= 0x1ff6 && address <= 0x1ff9) rom_ptr_ = rom_.data() + (address - 0x1ff6) * 4096;
|
||||
if(address >= 0x1ff6 && address <= 0x1ff9) rom_ptr_ = rom_base_ + (address - 0x1ff6) * 4096;
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
*value = rom_ptr_[address & 4095];
|
||||
@@ -35,18 +35,17 @@ class CartridgeAtari16k: public Cartridge<CartridgeAtari16k> {
|
||||
uint8_t *rom_ptr_;
|
||||
};
|
||||
|
||||
class CartridgeAtari16kSuperChip: public Cartridge<CartridgeAtari16kSuperChip> {
|
||||
class Atari16kSuperChip: public BusExtender {
|
||||
public:
|
||||
CartridgeAtari16kSuperChip(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
Atari16kSuperChip(uint8_t *rom_base, size_t rom_size) :
|
||||
BusExtender(rom_base, rom_size),
|
||||
rom_ptr_(rom_base) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
address &= 0x1fff;
|
||||
if(!(address & 0x1000)) return;
|
||||
|
||||
if(address >= 0x1ff6 && address <= 0x1ff9) rom_ptr_ = rom_.data() + (address - 0x1ff6) * 4096;
|
||||
if(address >= 0x1ff6 && address <= 0x1ff9) rom_ptr_ = rom_base_ + (address - 0x1ff6) * 4096;
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
*value = rom_ptr_[address & 4095];
|
||||
@@ -61,6 +60,7 @@ class CartridgeAtari16kSuperChip: public Cartridge<CartridgeAtari16kSuperChip> {
|
||||
uint8_t ram_[128];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeAtari16k_hpp */
|
||||
@@ -12,19 +12,17 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeAtari32k: public Cartridge<CartridgeAtari32k> {
|
||||
class Atari32k: public BusExtender {
|
||||
public:
|
||||
CartridgeAtari32k(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
Atari32k(uint8_t *rom_base, size_t rom_size) : BusExtender(rom_base, rom_size), rom_ptr_(rom_base) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
address &= 0x1fff;
|
||||
if(!(address & 0x1000)) return;
|
||||
|
||||
if(address >= 0x1ff4 && address <= 0x1ffb) rom_ptr_ = rom_.data() + (address - 0x1ff4) * 4096;
|
||||
if(address >= 0x1ff4 && address <= 0x1ffb) rom_ptr_ = rom_base_ + (address - 0x1ff4) * 4096;
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
*value = rom_ptr_[address & 4095];
|
||||
@@ -35,18 +33,15 @@ class CartridgeAtari32k: public Cartridge<CartridgeAtari32k> {
|
||||
uint8_t *rom_ptr_;
|
||||
};
|
||||
|
||||
class CartridgeAtari32kSuperChip: public Cartridge<CartridgeAtari32kSuperChip> {
|
||||
class Atari32kSuperChip: public BusExtender {
|
||||
public:
|
||||
CartridgeAtari32kSuperChip(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
Atari32kSuperChip(uint8_t *rom_base, size_t rom_size) : BusExtender(rom_base, rom_size), rom_ptr_(rom_base) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
address &= 0x1fff;
|
||||
if(!(address & 0x1000)) return;
|
||||
|
||||
if(address >= 0x1ff4 && address <= 0x1ffb) rom_ptr_ = rom_.data() + (address - 0x1ff4) * 4096;
|
||||
if(address >= 0x1ff4 && address <= 0x1ffb) rom_ptr_ = rom_base_ + (address - 0x1ff4) * 4096;
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
*value = rom_ptr_[address & 4095];
|
||||
@@ -61,6 +56,7 @@ class CartridgeAtari32kSuperChip: public Cartridge<CartridgeAtari32kSuperChip> {
|
||||
uint8_t ram_[128];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeAtari32k_hpp */
|
||||
@@ -12,20 +12,18 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeAtari8k: public Cartridge<CartridgeAtari8k> {
|
||||
class Atari8k: public BusExtender {
|
||||
public:
|
||||
CartridgeAtari8k(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
Atari8k(uint8_t *rom_base, size_t rom_size) : BusExtender(rom_base, rom_size), rom_ptr_(rom_base) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
address &= 0x1fff;
|
||||
if(!(address & 0x1000)) return;
|
||||
|
||||
if(address == 0x1ff8) rom_ptr_ = rom_.data();
|
||||
else if(address == 0x1ff9) rom_ptr_ = rom_.data() + 4096;
|
||||
if(address == 0x1ff8) rom_ptr_ = rom_base_;
|
||||
else if(address == 0x1ff9) rom_ptr_ = rom_base_ + 4096;
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
*value = rom_ptr_[address & 4095];
|
||||
@@ -36,19 +34,16 @@ class CartridgeAtari8k: public Cartridge<CartridgeAtari8k> {
|
||||
uint8_t *rom_ptr_;
|
||||
};
|
||||
|
||||
class CartridgeAtari8kSuperChip: public Cartridge<CartridgeAtari8kSuperChip> {
|
||||
class Atari8kSuperChip: public BusExtender {
|
||||
public:
|
||||
CartridgeAtari8kSuperChip(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
Atari8kSuperChip(uint8_t *rom_base, size_t rom_size) : BusExtender(rom_base, rom_size), rom_ptr_(rom_base) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
address &= 0x1fff;
|
||||
if(!(address & 0x1000)) return;
|
||||
|
||||
if(address == 0x1ff8) rom_ptr_ = rom_.data();
|
||||
if(address == 0x1ff9) rom_ptr_ = rom_.data() + 4096;
|
||||
if(address == 0x1ff8) rom_ptr_ = rom_base_;
|
||||
if(address == 0x1ff9) rom_ptr_ = rom_base_ + 4096;
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
*value = rom_ptr_[address & 4095];
|
||||
@@ -63,6 +58,7 @@ class CartridgeAtari8kSuperChip: public Cartridge<CartridgeAtari8kSuperChip> {
|
||||
uint8_t ram_[128];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeAtari8k_hpp */
|
||||
@@ -12,19 +12,17 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeCBSRAMPlus: public Cartridge<CartridgeCBSRAMPlus> {
|
||||
class CBSRAMPlus: public BusExtender {
|
||||
public:
|
||||
CartridgeCBSRAMPlus(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
CBSRAMPlus(uint8_t *rom_base, size_t rom_size) : BusExtender(rom_base, rom_size), rom_ptr_(rom_base) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
address &= 0x1fff;
|
||||
if(!(address & 0x1000)) return;
|
||||
|
||||
if(address >= 0x1ff8 && address <= 0x1ffa) rom_ptr_ = rom_.data() + (address - 0x1ff8) * 4096;
|
||||
if(address >= 0x1ff8 && address <= 0x1ffa) rom_ptr_ = rom_base_ + (address - 0x1ff8) * 4096;
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
*value = rom_ptr_[address & 4095];
|
||||
@@ -39,6 +37,7 @@ class CartridgeCBSRAMPlus: public Cartridge<CartridgeCBSRAMPlus> {
|
||||
uint8_t ram_[256];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeCBSRAMPlus_hpp */
|
||||
@@ -13,18 +13,34 @@
|
||||
#include "../Bus.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class BusExtender: public CPU::MOS6502::BusHandler {
|
||||
public:
|
||||
BusExtender(uint8_t *rom_base, size_t rom_size) : rom_base_(rom_base), rom_size_(rom_size) {}
|
||||
|
||||
void advance_cycles(int cycles) {}
|
||||
|
||||
protected:
|
||||
uint8_t *rom_base_;
|
||||
size_t rom_size_;
|
||||
};
|
||||
|
||||
template<class T> class Cartridge:
|
||||
public CPU::MOS6502::Processor<Cartridge<T>>,
|
||||
public CPU::MOS6502::BusHandler,
|
||||
public Bus {
|
||||
|
||||
public:
|
||||
Cartridge(const std::vector<uint8_t> &rom) :
|
||||
rom_(rom) {}
|
||||
m6502_(*this),
|
||||
rom_(rom),
|
||||
bus_extender_(rom_.data(), rom.size()) {
|
||||
// The above works because bus_extender_ is declared after rom_ in the instance storage list;
|
||||
// consider doing something less fragile.
|
||||
}
|
||||
|
||||
void run_for(const Cycles cycles) { CPU::MOS6502::Processor<Cartridge<T>>::run_for(cycles); }
|
||||
void set_reset_line(bool state) { CPU::MOS6502::Processor<Cartridge<T>>::set_reset_line(state); }
|
||||
void advance_cycles(int cycles) {}
|
||||
void run_for(const Cycles cycles) { m6502_.run_for(cycles); }
|
||||
void set_reset_line(bool state) { m6502_.set_reset_line(state); }
|
||||
|
||||
// to satisfy CPU::MOS6502::Processor
|
||||
Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
@@ -41,11 +57,11 @@ template<class T> class Cartridge:
|
||||
cycles_since_speaker_update_ += Cycles(cycles_run_for);
|
||||
cycles_since_video_update_ += Cycles(cycles_run_for);
|
||||
cycles_since_6532_update_ += Cycles(cycles_run_for / 3);
|
||||
static_cast<T *>(this)->advance_cycles(cycles_run_for / 3);
|
||||
bus_extender_.advance_cycles(cycles_run_for / 3);
|
||||
|
||||
if(operation != CPU::MOS6502::BusOperation::Ready) {
|
||||
// give the cartridge a chance to respond to the bus access
|
||||
static_cast<T *>(this)->perform_bus_operation(operation, address, value);
|
||||
bus_extender_.perform_bus_operation(operation, address, value);
|
||||
|
||||
// check for a RIOT RAM access
|
||||
if((address&0x1280) == 0x80) {
|
||||
@@ -91,7 +107,7 @@ template<class T> class Cartridge:
|
||||
case 0x00: update_video(); tia_->set_sync(*value & 0x02); break;
|
||||
case 0x01: update_video(); tia_->set_blank(*value & 0x02); break;
|
||||
|
||||
case 0x02: CPU::MOS6502::Processor<Cartridge<T>>::set_ready_line(true); break;
|
||||
case 0x02: m6502_.set_ready_line(true); break;
|
||||
case 0x03: update_video(); tia_->reset_horizontal_counter(); break;
|
||||
// TODO: audio will now be out of synchronisation — fix
|
||||
|
||||
@@ -156,7 +172,7 @@ template<class T> class Cartridge:
|
||||
}
|
||||
}
|
||||
|
||||
if(!tia_->get_cycles_until_horizontal_blank(cycles_since_video_update_)) CPU::MOS6502::Processor<Cartridge<T>>::set_ready_line(false);
|
||||
if(!tia_->get_cycles_until_horizontal_blank(cycles_since_video_update_)) m6502_.set_ready_line(false);
|
||||
|
||||
return Cycles(cycles_run_for / 3);
|
||||
}
|
||||
@@ -168,9 +184,14 @@ template<class T> class Cartridge:
|
||||
}
|
||||
|
||||
protected:
|
||||
CPU::MOS6502::Processor<Cartridge<T>, true> m6502_;
|
||||
std::vector<uint8_t> rom_;
|
||||
|
||||
private:
|
||||
T bus_extender_;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_Cartridge_hpp */
|
||||
|
||||
@@ -9,12 +9,14 @@
|
||||
#ifndef Atari2600_CartridgeCommaVid_hpp
|
||||
#define Atari2600_CartridgeCommaVid_hpp
|
||||
|
||||
namespace Atari2600 {
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
class CartridgeCommaVid: public Cartridge<CartridgeCommaVid> {
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CommaVid: public BusExtender {
|
||||
public:
|
||||
CartridgeCommaVid(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {}
|
||||
CommaVid(uint8_t *rom_base, size_t rom_size) : BusExtender(rom_base, rom_size) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
if(!(address & 0x1000)) return;
|
||||
@@ -30,13 +32,14 @@ class CartridgeCommaVid: public Cartridge<CartridgeCommaVid> {
|
||||
return;
|
||||
}
|
||||
|
||||
if(isReadOperation(operation)) *value = rom_[address & 2047];
|
||||
if(isReadOperation(operation)) *value = rom_base_[address & 2047];
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t ram_[1024];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeCommaVid_hpp */
|
||||
@@ -12,12 +12,13 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeMNetwork: public Cartridge<CartridgeMNetwork> {
|
||||
class MNetwork: public BusExtender {
|
||||
public:
|
||||
CartridgeMNetwork(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_[0] = rom_.data() + rom_.size() - 4096;
|
||||
MNetwork(uint8_t *rom_base, size_t rom_size) :
|
||||
BusExtender(rom_base, rom_size) {
|
||||
rom_ptr_[0] = rom_base + rom_size_ - 4096;
|
||||
rom_ptr_[1] = rom_ptr_[0] + 2048;
|
||||
high_ram_ptr_ = high_ram_;
|
||||
}
|
||||
@@ -27,7 +28,7 @@ class CartridgeMNetwork: public Cartridge<CartridgeMNetwork> {
|
||||
if(!(address & 0x1000)) return;
|
||||
|
||||
if(address >= 0x1fe0 && address <= 0x1fe6) {
|
||||
rom_ptr_[0] = rom_.data() + (address - 0x1fe0) * 2048;
|
||||
rom_ptr_[0] = rom_base_ + (address - 0x1fe0) * 2048;
|
||||
} else if(address == 0x1fe7) {
|
||||
rom_ptr_[0] = nullptr;
|
||||
} else if(address >= 0x1ff8 && address <= 0x1ffb) {
|
||||
@@ -54,7 +55,6 @@ class CartridgeMNetwork: public Cartridge<CartridgeMNetwork> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -63,6 +63,7 @@ class CartridgeMNetwork: public Cartridge<CartridgeMNetwork> {
|
||||
uint8_t low_ram_[1024], high_ram_[1024];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeMNetwork_hpp */
|
||||
@@ -12,13 +12,14 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeMegaBoy: public Cartridge<CartridgeMegaBoy> {
|
||||
class MegaBoy: public BusExtender {
|
||||
public:
|
||||
CartridgeMegaBoy(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom),
|
||||
MegaBoy(uint8_t *rom_base, size_t rom_size) :
|
||||
BusExtender(rom_base, rom_size),
|
||||
rom_ptr_(rom_base),
|
||||
current_page_(0) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
@@ -27,7 +28,7 @@ class CartridgeMegaBoy: public Cartridge<CartridgeMegaBoy> {
|
||||
|
||||
if(address == 0x1ff0) {
|
||||
current_page_ = (current_page_ + 1) & 15;
|
||||
rom_ptr_ = rom_.data() + current_page_ * 4096;
|
||||
rom_ptr_ = rom_base_ + current_page_ * 4096;
|
||||
}
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
@@ -40,6 +41,7 @@ class CartridgeMegaBoy: public Cartridge<CartridgeMegaBoy> {
|
||||
uint8_t current_page_;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CartridgeMegaBoy_h */
|
||||
@@ -12,12 +12,13 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeParkerBros: public Cartridge<CartridgeParkerBros> {
|
||||
class ParkerBros: public BusExtender {
|
||||
public:
|
||||
CartridgeParkerBros(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_[0] = rom_.data() + 4096;
|
||||
ParkerBros(uint8_t *rom_base, size_t rom_size) :
|
||||
BusExtender(rom_base, rom_size) {
|
||||
rom_ptr_[0] = rom_base + 4096;
|
||||
rom_ptr_[1] = rom_ptr_[0] + 1024;
|
||||
rom_ptr_[2] = rom_ptr_[1] + 1024;
|
||||
rom_ptr_[3] = rom_ptr_[2] + 1024;
|
||||
@@ -29,7 +30,7 @@ class CartridgeParkerBros: public Cartridge<CartridgeParkerBros> {
|
||||
|
||||
if(address >= 0x1fe0 && address < 0x1ff8) {
|
||||
int slot = (address >> 3)&3;
|
||||
rom_ptr_[slot] = rom_.data() + ((address & 7) * 1024);
|
||||
rom_ptr_[slot] = rom_base_ + ((address & 7) * 1024);
|
||||
}
|
||||
|
||||
if(isReadOperation(operation)) {
|
||||
@@ -41,6 +42,7 @@ class CartridgeParkerBros: public Cartridge<CartridgeParkerBros> {
|
||||
uint8_t *rom_ptr_[4];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeParkerBros_hpp */
|
||||
@@ -10,17 +10,17 @@
|
||||
#define Atari2600_CartridgePitfall2_hpp
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgePitfall2: public Cartridge<CartridgePitfall2> {
|
||||
class Pitfall2: public BusExtender {
|
||||
public:
|
||||
CartridgePitfall2(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom),
|
||||
Pitfall2(uint8_t *rom_base, size_t rom_size) :
|
||||
BusExtender(rom_base, rom_size),
|
||||
rom_ptr_(rom_base),
|
||||
random_number_generator_(0),
|
||||
featcher_address_{0, 0, 0, 0, 0, 0, 0, 0},
|
||||
mask_{0, 0, 0, 0, 0, 0, 0, 0},
|
||||
cycles_since_audio_update_(0) {
|
||||
rom_ptr_ = rom_.data();
|
||||
}
|
||||
cycles_since_audio_update_(0) {}
|
||||
|
||||
void advance_cycles(int cycles) {
|
||||
cycles_since_audio_update_ += cycles;
|
||||
@@ -39,7 +39,7 @@ class CartridgePitfall2: public Cartridge<CartridgePitfall2> {
|
||||
if(isReadOperation(operation)) {
|
||||
*value = random_number_generator_;
|
||||
}
|
||||
random_number_generator_ = (uint8_t)(
|
||||
random_number_generator_ = static_cast<uint8_t>(
|
||||
(random_number_generator_ << 1) |
|
||||
(~( (random_number_generator_ >> 7) ^
|
||||
(random_number_generator_ >> 5) ^
|
||||
@@ -53,11 +53,11 @@ class CartridgePitfall2: public Cartridge<CartridgePitfall2> {
|
||||
break;
|
||||
|
||||
case 0x1008: case 0x1009: case 0x100a: case 0x100b: case 0x100c: case 0x100d: case 0x100e: case 0x100f:
|
||||
*value = rom_[8192 + address_for_counter(address & 7)];
|
||||
*value = rom_base_[8192 + address_for_counter(address & 7)];
|
||||
break;
|
||||
|
||||
case 0x1010: case 0x1011: case 0x1012: case 0x1013: case 0x1014: case 0x1015: case 0x1016: case 0x1017:
|
||||
*value = rom_[8192 + address_for_counter(address & 7)] & mask_[address & 7];
|
||||
*value = rom_base_[8192 + address_for_counter(address & 7)] & mask_[address & 7];
|
||||
break;
|
||||
|
||||
#pragma mark - Writes
|
||||
@@ -73,7 +73,7 @@ class CartridgePitfall2: public Cartridge<CartridgePitfall2> {
|
||||
mask_[address & 7] = 0x00;
|
||||
break;
|
||||
case 0x1058: case 0x1059: case 0x105a: case 0x105b: case 0x105c: case 0x105d: case 0x105e: case 0x105f:
|
||||
featcher_address_[address & 7] = (featcher_address_[address & 7] & 0x00ff) | (uint16_t)(*value << 8);
|
||||
featcher_address_[address & 7] = (featcher_address_[address & 7] & 0x00ff) | static_cast<uint16_t>(*value << 8);
|
||||
break;
|
||||
case 0x1070: case 0x1071: case 0x1072: case 0x1073: case 0x1074: case 0x1075: case 0x1076: case 0x1077:
|
||||
random_number_generator_ = 0;
|
||||
@@ -81,8 +81,8 @@ class CartridgePitfall2: public Cartridge<CartridgePitfall2> {
|
||||
|
||||
#pragma mark - Paging
|
||||
|
||||
case 0x1ff8: rom_ptr_ = rom_.data(); break;
|
||||
case 0x1ff9: rom_ptr_ = rom_.data() + 4096; break;
|
||||
case 0x1ff8: rom_ptr_ = rom_base_; break;
|
||||
case 0x1ff9: rom_ptr_ = rom_base_ + 4096; break;
|
||||
|
||||
#pragma mark - Business as usual
|
||||
|
||||
@@ -128,6 +128,7 @@ class CartridgePitfall2: public Cartridge<CartridgePitfall2> {
|
||||
Cycles cycles_since_audio_update_;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgePitfall2_hpp */
|
||||
@@ -12,19 +12,20 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeTigervision: public Cartridge<CartridgeTigervision> {
|
||||
class Tigervision: public BusExtender {
|
||||
public:
|
||||
CartridgeTigervision(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {
|
||||
rom_ptr_[0] = rom_.data() + rom_.size() - 4096;
|
||||
Tigervision(uint8_t *rom_base, size_t rom_size) :
|
||||
BusExtender(rom_base, rom_size) {
|
||||
rom_ptr_[0] = rom_base + rom_size - 4096;
|
||||
rom_ptr_[1] = rom_ptr_[0] + 2048;
|
||||
}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
if((address&0x1fff) == 0x3f) {
|
||||
int offset = ((*value) * 2048) & (rom_.size() - 1);
|
||||
rom_ptr_[0] = rom_.data() + offset;
|
||||
int offset = ((*value) * 2048) & (rom_size_ - 1);
|
||||
rom_ptr_[0] = rom_base_ + offset;
|
||||
return;
|
||||
} else if((address&0x1000) && isReadOperation(operation)) {
|
||||
*value = rom_ptr_[(address >> 11)&1][address & 2047];
|
||||
@@ -35,6 +36,7 @@ class CartridgeTigervision: public Cartridge<CartridgeTigervision> {
|
||||
uint8_t *rom_ptr_[2];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeTigervision_hpp */
|
||||
@@ -12,19 +12,20 @@
|
||||
#include "Cartridge.hpp"
|
||||
|
||||
namespace Atari2600 {
|
||||
namespace Cartridge {
|
||||
|
||||
class CartridgeUnpaged: public Cartridge<CartridgeUnpaged> {
|
||||
class Unpaged: public BusExtender {
|
||||
public:
|
||||
CartridgeUnpaged(const std::vector<uint8_t> &rom) :
|
||||
Cartridge(rom) {}
|
||||
Unpaged(uint8_t *rom_base, size_t rom_size) : BusExtender(rom_base, rom_size) {}
|
||||
|
||||
void perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
if(isReadOperation(operation) && (address & 0x1000)) {
|
||||
*value = rom_[address & (rom_.size() - 1)];
|
||||
*value = rom_base_[address & (rom_size_ - 1)];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Atari2600_CartridgeUnpaged_hpp */
|
||||
@@ -37,19 +37,19 @@ TIA::TIA(bool create_crt) :
|
||||
}
|
||||
|
||||
for(int c = 0; c < 256; c++) {
|
||||
reverse_table[c] = (uint8_t)(
|
||||
reverse_table[c] = static_cast<uint8_t>(
|
||||
((c & 0x01) << 7) | ((c & 0x02) << 5) | ((c & 0x04) << 3) | ((c & 0x08) << 1) |
|
||||
((c & 0x10) >> 1) | ((c & 0x20) >> 3) | ((c & 0x40) >> 5) | ((c & 0x80) >> 7)
|
||||
);
|
||||
}
|
||||
|
||||
for(int c = 0; c < 64; c++) {
|
||||
bool has_playfield = c & (int)(CollisionType::Playfield);
|
||||
bool has_ball = c & (int)(CollisionType::Ball);
|
||||
bool has_player0 = c & (int)(CollisionType::Player0);
|
||||
bool has_player1 = c & (int)(CollisionType::Player1);
|
||||
bool has_missile0 = c & (int)(CollisionType::Missile0);
|
||||
bool has_missile1 = c & (int)(CollisionType::Missile1);
|
||||
bool has_playfield = c & static_cast<int>(CollisionType::Playfield);
|
||||
bool has_ball = c & static_cast<int>(CollisionType::Ball);
|
||||
bool has_player0 = c & static_cast<int>(CollisionType::Player0);
|
||||
bool has_player1 = c & static_cast<int>(CollisionType::Player1);
|
||||
bool has_missile0 = c & static_cast<int>(CollisionType::Missile0);
|
||||
bool has_missile1 = c & static_cast<int>(CollisionType::Missile1);
|
||||
|
||||
uint8_t collision_registers[8];
|
||||
collision_registers[0] = ((has_missile0 && has_player1) ? 0x80 : 0x00) | ((has_missile0 && has_player0) ? 0x40 : 0x00);
|
||||
@@ -71,51 +71,51 @@ TIA::TIA(bool create_crt) :
|
||||
(collision_registers[7] << 8);
|
||||
|
||||
// all priority modes show the background if nothing else is present
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::Standard][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::OnTop][c] = (uint8_t)ColourIndex::Background;
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::Standard)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreLeft)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreRight)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::OnTop)][c] = static_cast<uint8_t>(ColourIndex::Background);
|
||||
|
||||
// test 1 for standard priority: if there is a playfield or ball pixel, plot that colour
|
||||
if(has_playfield || has_ball) {
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::Standard][c] = (uint8_t)ColourIndex::PlayfieldBall;
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::Standard)][c] = static_cast<uint8_t>(ColourIndex::PlayfieldBall);
|
||||
}
|
||||
|
||||
// test 1 for score mode: if there is a ball pixel, plot that colour
|
||||
if(has_ball) {
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] = (uint8_t)ColourIndex::PlayfieldBall;
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreLeft)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreRight)][c] = static_cast<uint8_t>(ColourIndex::PlayfieldBall);
|
||||
}
|
||||
|
||||
// test 1 for on-top mode, test 2 for everbody else: if there is a player 1 or missile 1 pixel, plot that colour
|
||||
if(has_player1 || has_missile1) {
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::Standard][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::OnTop][c] = (uint8_t)ColourIndex::PlayerMissile1;
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::Standard)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreLeft)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreRight)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::OnTop)][c] = static_cast<uint8_t>(ColourIndex::PlayerMissile1);
|
||||
}
|
||||
|
||||
// in the right-hand side of score mode, the playfield has the same priority as player 1
|
||||
if(has_playfield) {
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] = (uint8_t)ColourIndex::PlayerMissile1;
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreRight)][c] = static_cast<uint8_t>(ColourIndex::PlayerMissile1);
|
||||
}
|
||||
|
||||
// next test for everybody: if there is a player 0 or missile 0 pixel, plot that colour instead
|
||||
if(has_player0 || has_missile0) {
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::Standard][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] =
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::OnTop][c] = (uint8_t)ColourIndex::PlayerMissile0;
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::Standard)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreLeft)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreRight)][c] =
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::OnTop)][c] = static_cast<uint8_t>(ColourIndex::PlayerMissile0);
|
||||
}
|
||||
|
||||
// if this is the left-hand side of score mode, the playfield has the same priority as player 0
|
||||
if(has_playfield) {
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] = (uint8_t)ColourIndex::PlayerMissile0;
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreLeft)][c] = static_cast<uint8_t>(ColourIndex::PlayerMissile0);
|
||||
}
|
||||
|
||||
// a final test for 'on top' priority mode: if the playfield or ball are visible, prefer that colour to all others
|
||||
if(has_playfield || has_ball) {
|
||||
colour_mask_by_mode_collision_flags_[(int)ColourMode::OnTop][c] = (uint8_t)ColourIndex::PlayfieldBall;
|
||||
colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::OnTop)][c] = static_cast<uint8_t>(ColourIndex::PlayfieldBall);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,7 +162,7 @@ void TIA::set_output_mode(Atari2600::TIA::OutputMode output_mode) {
|
||||
// cycles_per_line * 2 cycles of information from one sync edge to the next
|
||||
crt_->set_new_display_type(cycles_per_line * 2 - 1, display_type);
|
||||
|
||||
/* speaker_->set_input_rate((float)(get_clock_rate() / 38.0));*/
|
||||
/* speaker_->set_input_rate(static_cast<float>(get_clock_rate() / 38.0));*/
|
||||
}
|
||||
|
||||
void TIA::run_for(const Cycles cycles) {
|
||||
@@ -203,23 +203,23 @@ int TIA::get_cycles_until_horizontal_blank(const Cycles from_offset) {
|
||||
}
|
||||
|
||||
void TIA::set_background_colour(uint8_t colour) {
|
||||
colour_palette_[(int)ColourIndex::Background] = colour;
|
||||
colour_palette_[static_cast<int>(ColourIndex::Background)] = colour;
|
||||
}
|
||||
|
||||
void TIA::set_playfield(uint16_t offset, uint8_t value) {
|
||||
assert(offset >= 0 && offset < 3);
|
||||
switch(offset) {
|
||||
case 0:
|
||||
background_[1] = (background_[1] & 0x0ffff) | ((uint32_t)reverse_table[value & 0xf0] << 16);
|
||||
background_[0] = (background_[0] & 0xffff0) | (uint32_t)(value >> 4);
|
||||
background_[1] = (background_[1] & 0x0ffff) | (static_cast<uint32_t>(reverse_table[value & 0xf0]) << 16);
|
||||
background_[0] = (background_[0] & 0xffff0) | static_cast<uint32_t>(value >> 4);
|
||||
break;
|
||||
case 1:
|
||||
background_[1] = (background_[1] & 0xf00ff) | ((uint32_t)value << 8);
|
||||
background_[0] = (background_[0] & 0xff00f) | ((uint32_t)reverse_table[value] << 4);
|
||||
background_[1] = (background_[1] & 0xf00ff) | (static_cast<uint32_t>(value) << 8);
|
||||
background_[0] = (background_[0] & 0xff00f) | (static_cast<uint32_t>(reverse_table[value]) << 4);
|
||||
break;
|
||||
case 2:
|
||||
background_[1] = (background_[1] & 0xfff00) | reverse_table[value];
|
||||
background_[0] = (background_[0] & 0x00fff) | ((uint32_t)value << 12);
|
||||
background_[0] = (background_[0] & 0x00fff) | (static_cast<uint32_t>(value) << 12);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -243,7 +243,7 @@ void TIA::set_playfield_control_and_ball_size(uint8_t value) {
|
||||
}
|
||||
|
||||
void TIA::set_playfield_ball_colour(uint8_t colour) {
|
||||
colour_palette_[(int)ColourIndex::PlayfieldBall] = colour;
|
||||
colour_palette_[static_cast<int>(ColourIndex::PlayfieldBall)] = colour;
|
||||
}
|
||||
|
||||
void TIA::set_player_number_and_size(int player, uint8_t value) {
|
||||
@@ -305,7 +305,7 @@ void TIA::set_player_motion(int player, uint8_t motion) {
|
||||
|
||||
void TIA::set_player_missile_colour(int player, uint8_t colour) {
|
||||
assert(player >= 0 && player < 2);
|
||||
colour_palette_[(int)ColourIndex::PlayerMissile0 + player] = colour;
|
||||
colour_palette_[static_cast<int>(ColourIndex::PlayerMissile0) + player] = colour;
|
||||
}
|
||||
|
||||
void TIA::set_missile_enable(int missile, bool enabled) {
|
||||
@@ -360,7 +360,7 @@ void TIA::clear_motion() {
|
||||
}
|
||||
|
||||
uint8_t TIA::get_collision_flags(int offset) {
|
||||
return (uint8_t)((collision_flags_ >> (offset << 1)) << 6) & 0xc0;
|
||||
return static_cast<uint8_t>((collision_flags_ >> (offset << 1)) << 6) & 0xc0;
|
||||
}
|
||||
|
||||
void TIA::clear_collision_flags() {
|
||||
@@ -401,22 +401,22 @@ void TIA::output_for_cycles(int number_of_cycles) {
|
||||
int latent_start = output_cursor + 4;
|
||||
int latent_end = horizontal_counter_ + 4;
|
||||
draw_playfield(latent_start, latent_end);
|
||||
draw_object<Player>(player_[0], (uint8_t)CollisionType::Player0, output_cursor, horizontal_counter_);
|
||||
draw_object<Player>(player_[1], (uint8_t)CollisionType::Player1, output_cursor, horizontal_counter_);
|
||||
draw_missile(missile_[0], player_[0], (uint8_t)CollisionType::Missile0, output_cursor, horizontal_counter_);
|
||||
draw_missile(missile_[1], player_[1], (uint8_t)CollisionType::Missile1, output_cursor, horizontal_counter_);
|
||||
draw_object<Ball>(ball_, (uint8_t)CollisionType::Ball, output_cursor, horizontal_counter_);
|
||||
draw_object<Player>(player_[0], static_cast<uint8_t>(CollisionType::Player0), output_cursor, horizontal_counter_);
|
||||
draw_object<Player>(player_[1], static_cast<uint8_t>(CollisionType::Player1), output_cursor, horizontal_counter_);
|
||||
draw_missile(missile_[0], player_[0], static_cast<uint8_t>(CollisionType::Missile0), output_cursor, horizontal_counter_);
|
||||
draw_missile(missile_[1], player_[1], static_cast<uint8_t>(CollisionType::Missile1), output_cursor, horizontal_counter_);
|
||||
draw_object<Ball>(ball_, static_cast<uint8_t>(CollisionType::Ball), output_cursor, horizontal_counter_);
|
||||
|
||||
// convert to television signals
|
||||
|
||||
#define Period(function, target) \
|
||||
if(output_cursor < target) { \
|
||||
if(horizontal_counter_ <= target) { \
|
||||
if(crt_) crt_->function((unsigned int)((horizontal_counter_ - output_cursor) * 2)); \
|
||||
if(crt_) crt_->function(static_cast<unsigned int>((horizontal_counter_ - output_cursor) * 2)); \
|
||||
horizontal_counter_ %= cycles_per_line; \
|
||||
return; \
|
||||
} else { \
|
||||
if(crt_) crt_->function((unsigned int)((target - output_cursor) * 2)); \
|
||||
if(crt_) crt_->function(static_cast<unsigned int>((target - output_cursor) * 2)); \
|
||||
output_cursor = target; \
|
||||
} \
|
||||
}
|
||||
@@ -442,12 +442,12 @@ void TIA::output_for_cycles(int number_of_cycles) {
|
||||
if(output_mode_ & blank_flag) {
|
||||
if(pixel_target_) {
|
||||
output_pixels(pixels_start_location_, output_cursor);
|
||||
if(crt_) crt_->output_data((unsigned int)(output_cursor - pixels_start_location_) * 2, 2);
|
||||
if(crt_) crt_->output_data(static_cast<unsigned int>(output_cursor - pixels_start_location_) * 2, 2);
|
||||
pixel_target_ = nullptr;
|
||||
pixels_start_location_ = 0;
|
||||
}
|
||||
int duration = std::min(228, horizontal_counter_) - output_cursor;
|
||||
if(crt_) crt_->output_blank((unsigned int)(duration * 2));
|
||||
if(crt_) crt_->output_blank(static_cast<unsigned int>(duration * 2));
|
||||
} else {
|
||||
if(!pixels_start_location_ && crt_) {
|
||||
pixels_start_location_ = output_cursor;
|
||||
@@ -464,7 +464,7 @@ void TIA::output_for_cycles(int number_of_cycles) {
|
||||
}
|
||||
|
||||
if(horizontal_counter_ == cycles_per_line && crt_) {
|
||||
crt_->output_data((unsigned int)(output_cursor - pixels_start_location_) * 2, 2);
|
||||
crt_->output_data(static_cast<unsigned int>(output_cursor - pixels_start_location_) * 2, 2);
|
||||
pixel_target_ = nullptr;
|
||||
pixels_start_location_ = 0;
|
||||
}
|
||||
@@ -490,18 +490,18 @@ void TIA::output_pixels(int start, int end) {
|
||||
if(playfield_priority_ == PlayfieldPriority::Score) {
|
||||
while(start < end && start < first_pixel_cycle + 80) {
|
||||
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
||||
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][buffer_value]];
|
||||
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreLeft)][buffer_value]];
|
||||
start++;
|
||||
target_position++;
|
||||
}
|
||||
while(start < end) {
|
||||
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
||||
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][buffer_value]];
|
||||
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[static_cast<int>(ColourMode::ScoreRight)][buffer_value]];
|
||||
start++;
|
||||
target_position++;
|
||||
}
|
||||
} else {
|
||||
int table_index = (int)((playfield_priority_ == PlayfieldPriority::Standard) ? ColourMode::Standard : ColourMode::OnTop);
|
||||
int table_index = static_cast<int>((playfield_priority_ == PlayfieldPriority::Standard) ? ColourMode::Standard : ColourMode::OnTop);
|
||||
while(start < end) {
|
||||
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
||||
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[table_index][buffer_value]];
|
||||
@@ -553,7 +553,7 @@ void TIA::draw_playfield(int start, int end) {
|
||||
while(aligned_position < end) {
|
||||
int offset = (aligned_position - first_pixel_cycle) >> 2;
|
||||
uint32_t value = ((background_[(offset/20)&background_half_mask_] >> (offset%20))&1) * 0x01010101;
|
||||
*(uint32_t *)&collision_buffer_[aligned_position - first_pixel_cycle] |= value;
|
||||
*reinterpret_cast<uint32_t *>(&collision_buffer_[aligned_position - first_pixel_cycle]) |= value;
|
||||
aligned_position += 4;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace CRTMachine {
|
||||
*/
|
||||
class Machine {
|
||||
public:
|
||||
Machine() : clock_is_unlimited_(false) {}
|
||||
Machine() : clock_is_unlimited_(false), delegate_(nullptr) {}
|
||||
|
||||
/*!
|
||||
Causes the machine to set up its CRT and, if it has one, speaker. The caller guarantees
|
||||
@@ -57,7 +57,7 @@ class Machine {
|
||||
virtual void machine_did_change_clock_rate(Machine *machine) = 0;
|
||||
virtual void machine_did_change_clock_is_unlimited(Machine *machine) = 0;
|
||||
};
|
||||
void set_delegate(Delegate *delegate) { this->delegate_ = delegate; }
|
||||
void set_delegate(Delegate *delegate) { delegate_ = delegate; }
|
||||
|
||||
protected:
|
||||
void set_clock_rate(double clock_rate) {
|
||||
|
||||
@@ -9,127 +9,20 @@
|
||||
#ifndef Commodore1540_hpp
|
||||
#define Commodore1540_hpp
|
||||
|
||||
#include "../../../Processors/6502/6502.hpp"
|
||||
#include "../../../Components/6522/6522.hpp"
|
||||
|
||||
#include "../SerialBus.hpp"
|
||||
|
||||
#include "../../../Storage/Disk/Disk.hpp"
|
||||
#include "../../../Storage/Disk/DiskController.hpp"
|
||||
#include "Implementation/C1540Base.hpp"
|
||||
|
||||
namespace Commodore {
|
||||
namespace C1540 {
|
||||
|
||||
/*!
|
||||
An implementation of the serial-port VIA in a Commodore 1540 — the VIA that facilitates all
|
||||
IEC bus communications.
|
||||
|
||||
It is wired up such that Port B contains:
|
||||
Bit 0: data input; 1 if the line is low, 0 if it is high;
|
||||
Bit 1: data output; 1 if the line should be low, 0 if it should be high;
|
||||
Bit 2: clock input; 1 if the line is low, 0 if it is high;
|
||||
Bit 3: clock output; 1 if the line is low, 0 if it is high;
|
||||
Bit 4: attention acknowledge output; exclusive ORd with the attention input and ORd onto the data output;
|
||||
Bits 5/6: device select input; the 1540 will act as device 8 + [value of bits]
|
||||
Bit 7: attention input; 1 if the line is low, 0 if it is high
|
||||
|
||||
The attention input is also connected to CA1, similarly inverted — the CA1 wire will be high when the bus is low and vice versa.
|
||||
*/
|
||||
class SerialPortVIA: public MOS::MOS6522<SerialPortVIA>, public MOS::MOS6522IRQDelegate {
|
||||
public:
|
||||
using MOS6522IRQDelegate::set_interrupt_status;
|
||||
|
||||
SerialPortVIA();
|
||||
|
||||
uint8_t get_port_input(Port);
|
||||
|
||||
void set_port_output(Port, uint8_t value, uint8_t mask);
|
||||
void set_serial_line_state(::Commodore::Serial::Line, bool);
|
||||
|
||||
void set_serial_port(const std::shared_ptr<::Commodore::Serial::Port> &);
|
||||
|
||||
private:
|
||||
uint8_t port_b_;
|
||||
std::weak_ptr<::Commodore::Serial::Port> serial_port_;
|
||||
bool attention_acknowledge_level_, attention_level_input_, data_level_output_;
|
||||
|
||||
void update_data_line();
|
||||
};
|
||||
|
||||
/*!
|
||||
An implementation of the drive VIA in a Commodore 1540 — the VIA that is used to interface with the disk.
|
||||
|
||||
It is wired up such that Port B contains:
|
||||
Bits 0/1: head step direction
|
||||
Bit 2: motor control
|
||||
Bit 3: LED control (TODO)
|
||||
Bit 4: write protect photocell status (TODO)
|
||||
Bits 5/6: read/write density
|
||||
Bit 7: 0 if sync marks are currently being detected, 1 otherwise.
|
||||
|
||||
... and Port A contains the byte most recently read from the disk or the byte next to write to the disk, depending on data direction.
|
||||
|
||||
It is implied that CA2 might be used to set processor overflow, CA1 a strobe for data input, and one of the CBs being definitive on
|
||||
whether the disk head is being told to read or write, but it's unclear and I've yet to investigate. So, TODO.
|
||||
*/
|
||||
class DriveVIA: public MOS::MOS6522<DriveVIA>, public MOS::MOS6522IRQDelegate {
|
||||
public:
|
||||
class Delegate {
|
||||
public:
|
||||
virtual void drive_via_did_step_head(void *driveVIA, int direction) = 0;
|
||||
virtual void drive_via_did_set_data_density(void *driveVIA, int density) = 0;
|
||||
};
|
||||
void set_delegate(Delegate *);
|
||||
|
||||
using MOS6522IRQDelegate::set_interrupt_status;
|
||||
|
||||
DriveVIA();
|
||||
|
||||
uint8_t get_port_input(Port port);
|
||||
|
||||
void set_sync_detected(bool);
|
||||
void set_data_input(uint8_t);
|
||||
bool get_should_set_overflow();
|
||||
bool get_motor_enabled();
|
||||
|
||||
void set_control_line_output(Port, Line, bool value);
|
||||
|
||||
void set_port_output(Port, uint8_t value, uint8_t direction_mask);
|
||||
|
||||
private:
|
||||
uint8_t port_b_, port_a_;
|
||||
bool should_set_overflow_;
|
||||
bool drive_motor_;
|
||||
uint8_t previous_port_b_output_;
|
||||
Delegate *delegate_;
|
||||
};
|
||||
|
||||
/*!
|
||||
An implementation of the C1540's serial port; this connects incoming line levels to the serial-port VIA.
|
||||
*/
|
||||
class SerialPort : public ::Commodore::Serial::Port {
|
||||
public:
|
||||
void set_input(::Commodore::Serial::Line, ::Commodore::Serial::LineLevel);
|
||||
void set_serial_port_via(const std::shared_ptr<SerialPortVIA> &);
|
||||
|
||||
private:
|
||||
std::weak_ptr<SerialPortVIA> serial_port_VIA_;
|
||||
};
|
||||
|
||||
/*!
|
||||
Provides an emulation of the C1540.
|
||||
*/
|
||||
class Machine:
|
||||
public CPU::MOS6502::Processor<Machine>,
|
||||
public MOS::MOS6522IRQDelegate::Delegate,
|
||||
public DriveVIA::Delegate,
|
||||
public Storage::Disk::Controller {
|
||||
|
||||
class Machine: public MachineBase {
|
||||
public:
|
||||
Machine();
|
||||
|
||||
/*!
|
||||
Sets the ROM image to use for this drive; it is assumed that the buffer provided will be at least 16 kb in size.
|
||||
Sets the ROM image to use for this drive; it is asserted that the buffer provided is 16 kb in size.
|
||||
*/
|
||||
void set_rom(const std::vector<uint8_t> &rom);
|
||||
|
||||
@@ -138,30 +31,11 @@ class Machine:
|
||||
*/
|
||||
void set_serial_bus(std::shared_ptr<::Commodore::Serial::Bus> serial_bus);
|
||||
|
||||
/// Advances time.
|
||||
void run_for(const Cycles cycles);
|
||||
|
||||
/// Inserts @c disk into the drive.
|
||||
void set_disk(std::shared_ptr<Storage::Disk::Disk> disk);
|
||||
|
||||
// to satisfy CPU::MOS6502::Processor
|
||||
Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value);
|
||||
|
||||
// to satisfy MOS::MOS6522::Delegate
|
||||
virtual void mos6522_did_change_interrupt_status(void *mos6522);
|
||||
|
||||
// to satisfy DriveVIA::Delegate
|
||||
void drive_via_did_step_head(void *driveVIA, int direction);
|
||||
void drive_via_did_set_data_density(void *driveVIA, int density);
|
||||
|
||||
private:
|
||||
uint8_t ram_[0x800];
|
||||
uint8_t rom_[0x4000];
|
||||
|
||||
std::shared_ptr<SerialPortVIA> serial_port_VIA_;
|
||||
std::shared_ptr<SerialPort> serial_port_;
|
||||
DriveVIA drive_VIA_;
|
||||
|
||||
int shift_register_, bit_window_offset_;
|
||||
virtual void process_input_bit(int value, unsigned int cycles_since_index_hole);
|
||||
virtual void process_index_hole();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -7,34 +7,44 @@
|
||||
//
|
||||
|
||||
#include "C1540.hpp"
|
||||
|
||||
#include <string>
|
||||
#include "../../../Storage/Disk/Encodings/CommodoreGCR.hpp"
|
||||
#include <cassert>
|
||||
|
||||
#include "../../../../Storage/Disk/Encodings/CommodoreGCR.hpp"
|
||||
|
||||
using namespace Commodore::C1540;
|
||||
|
||||
Machine::Machine() :
|
||||
MachineBase::MachineBase() :
|
||||
m6502_(*this),
|
||||
shift_register_(0),
|
||||
Storage::Disk::Controller(1000000, 4, 300),
|
||||
Storage::Disk::Controller(1000000),
|
||||
serial_port_(new SerialPort),
|
||||
serial_port_VIA_(new SerialPortVIA) {
|
||||
serial_port_VIA_port_handler_(new SerialPortVIA(serial_port_VIA_)),
|
||||
drive_VIA_(drive_VIA_port_handler_),
|
||||
serial_port_VIA_(*serial_port_VIA_port_handler_),
|
||||
drive_(new Storage::Disk::Drive(1000000, 300, 2)) {
|
||||
// attach the serial port to its VIA and vice versa
|
||||
serial_port_->set_serial_port_via(serial_port_VIA_);
|
||||
serial_port_VIA_->set_serial_port(serial_port_);
|
||||
serial_port_->set_serial_port_via(serial_port_VIA_port_handler_);
|
||||
serial_port_VIA_port_handler_->set_serial_port(serial_port_);
|
||||
|
||||
// set this instance as the delegate to receive interrupt requests from both VIAs
|
||||
serial_port_VIA_->set_interrupt_delegate(this);
|
||||
drive_VIA_.set_interrupt_delegate(this);
|
||||
drive_VIA_.set_delegate(this);
|
||||
serial_port_VIA_port_handler_->set_interrupt_delegate(this);
|
||||
drive_VIA_port_handler_.set_interrupt_delegate(this);
|
||||
drive_VIA_port_handler_.set_delegate(this);
|
||||
|
||||
// set a bit rate
|
||||
set_expected_bit_length(Storage::Encodings::CommodoreGCR::length_of_a_bit_in_time_zone(3));
|
||||
|
||||
// attach the only drive there is
|
||||
set_drive(drive_);
|
||||
}
|
||||
|
||||
void Machine::set_serial_bus(std::shared_ptr<::Commodore::Serial::Bus> serial_bus) {
|
||||
Commodore::Serial::AttachPortAndBus(serial_port_, serial_bus);
|
||||
}
|
||||
|
||||
Cycles Machine::perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
Cycles MachineBase::perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
/*
|
||||
Memory map (given that I'm unsure yet on any potential mirroring):
|
||||
|
||||
@@ -49,13 +59,14 @@ Cycles Machine::perform_bus_operation(CPU::MOS6502::BusOperation operation, uint
|
||||
else
|
||||
ram_[address] = *value;
|
||||
} else if(address >= 0xc000) {
|
||||
if(isReadOperation(operation))
|
||||
if(isReadOperation(operation)) {
|
||||
*value = rom_[address & 0x3fff];
|
||||
}
|
||||
} else if(address >= 0x1800 && address <= 0x180f) {
|
||||
if(isReadOperation(operation))
|
||||
*value = serial_port_VIA_->get_register(address);
|
||||
*value = serial_port_VIA_.get_register(address);
|
||||
else
|
||||
serial_port_VIA_->set_register(address, *value);
|
||||
serial_port_VIA_.set_register(address, *value);
|
||||
} else if(address >= 0x1c00 && address <= 0x1c0f) {
|
||||
if(isReadOperation(operation))
|
||||
*value = drive_VIA_.get_register(address);
|
||||
@@ -63,81 +74,82 @@ Cycles Machine::perform_bus_operation(CPU::MOS6502::BusOperation operation, uint
|
||||
drive_VIA_.set_register(address, *value);
|
||||
}
|
||||
|
||||
serial_port_VIA_->run_for(Cycles(1));
|
||||
serial_port_VIA_.run_for(Cycles(1));
|
||||
drive_VIA_.run_for(Cycles(1));
|
||||
|
||||
return Cycles(1);
|
||||
}
|
||||
|
||||
void Machine::set_rom(const std::vector<uint8_t> &rom) {
|
||||
assert(rom.size() == sizeof(rom_));
|
||||
memcpy(rom_, rom.data(), std::min(sizeof(rom_), rom.size()));
|
||||
}
|
||||
|
||||
void Machine::set_disk(std::shared_ptr<Storage::Disk::Disk> disk) {
|
||||
std::shared_ptr<Storage::Disk::Drive> drive(new Storage::Disk::Drive);
|
||||
drive->set_disk(disk);
|
||||
set_drive(drive);
|
||||
drive_->set_disk(disk);
|
||||
}
|
||||
|
||||
void Machine::run_for(const Cycles cycles) {
|
||||
CPU::MOS6502::Processor<Machine>::run_for(cycles);
|
||||
set_motor_on(drive_VIA_.get_motor_enabled());
|
||||
if(drive_VIA_.get_motor_enabled()) // TODO: motor speed up/down
|
||||
m6502_.run_for(cycles);
|
||||
|
||||
bool drive_motor = drive_VIA_port_handler_.get_motor_enabled();
|
||||
drive_->set_motor_on(drive_motor);
|
||||
if(drive_motor)
|
||||
Storage::Disk::Controller::run_for(cycles);
|
||||
}
|
||||
|
||||
#pragma mark - 6522 delegate
|
||||
|
||||
void Machine::mos6522_did_change_interrupt_status(void *mos6522) {
|
||||
void MachineBase::mos6522_did_change_interrupt_status(void *mos6522) {
|
||||
// both VIAs are connected to the IRQ line
|
||||
set_irq_line(serial_port_VIA_->get_interrupt_line() || drive_VIA_.get_interrupt_line());
|
||||
m6502_.set_irq_line(serial_port_VIA_.get_interrupt_line() || drive_VIA_.get_interrupt_line());
|
||||
}
|
||||
|
||||
#pragma mark - Disk drive
|
||||
|
||||
void Machine::process_input_bit(int value, unsigned int cycles_since_index_hole) {
|
||||
void MachineBase::process_input_bit(int value) {
|
||||
shift_register_ = (shift_register_ << 1) | value;
|
||||
if((shift_register_ & 0x3ff) == 0x3ff) {
|
||||
drive_VIA_.set_sync_detected(true);
|
||||
drive_VIA_port_handler_.set_sync_detected(true);
|
||||
bit_window_offset_ = -1; // i.e. this bit isn't the first within a data window, but the next might be
|
||||
} else {
|
||||
drive_VIA_.set_sync_detected(false);
|
||||
drive_VIA_port_handler_.set_sync_detected(false);
|
||||
}
|
||||
bit_window_offset_++;
|
||||
if(bit_window_offset_ == 8) {
|
||||
drive_VIA_.set_data_input((uint8_t)shift_register_);
|
||||
drive_VIA_port_handler_.set_data_input(static_cast<uint8_t>(shift_register_));
|
||||
bit_window_offset_ = 0;
|
||||
if(drive_VIA_.get_should_set_overflow()) {
|
||||
set_overflow_line(true);
|
||||
if(drive_VIA_port_handler_.get_should_set_overflow()) {
|
||||
m6502_.set_overflow_line(true);
|
||||
}
|
||||
}
|
||||
else set_overflow_line(false);
|
||||
else m6502_.set_overflow_line(false);
|
||||
}
|
||||
|
||||
// the 1540 does not recognise index holes
|
||||
void Machine::process_index_hole() {}
|
||||
void MachineBase::process_index_hole() {}
|
||||
|
||||
#pragma mak - Drive VIA delegate
|
||||
|
||||
void Machine::drive_via_did_step_head(void *driveVIA, int direction) {
|
||||
step(direction);
|
||||
void MachineBase::drive_via_did_step_head(void *driveVIA, int direction) {
|
||||
drive_->step(direction);
|
||||
}
|
||||
|
||||
void Machine::drive_via_did_set_data_density(void *driveVIA, int density) {
|
||||
set_expected_bit_length(Storage::Encodings::CommodoreGCR::length_of_a_bit_in_time_zone((unsigned int)density));
|
||||
void MachineBase::drive_via_did_set_data_density(void *driveVIA, int density) {
|
||||
set_expected_bit_length(Storage::Encodings::CommodoreGCR::length_of_a_bit_in_time_zone(static_cast<unsigned int>(density)));
|
||||
}
|
||||
|
||||
#pragma mark - SerialPortVIA
|
||||
|
||||
SerialPortVIA::SerialPortVIA() :
|
||||
port_b_(0x00), attention_acknowledge_level_(false), attention_level_input_(true), data_level_output_(false) {}
|
||||
SerialPortVIA::SerialPortVIA(MOS::MOS6522::MOS6522<SerialPortVIA> &via) :
|
||||
port_b_(0x00), attention_acknowledge_level_(false), attention_level_input_(true), data_level_output_(false), via_(via) {}
|
||||
|
||||
uint8_t SerialPortVIA::get_port_input(Port port) {
|
||||
uint8_t SerialPortVIA::get_port_input(MOS::MOS6522::Port port) {
|
||||
if(port) return port_b_;
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
void SerialPortVIA::set_port_output(Port port, uint8_t value, uint8_t mask) {
|
||||
void SerialPortVIA::set_port_output(MOS::MOS6522::Port port, uint8_t value, uint8_t mask) {
|
||||
if(port) {
|
||||
std::shared_ptr<::Commodore::Serial::Port> serialPort = serial_port_.lock();
|
||||
if(serialPort) {
|
||||
@@ -151,6 +163,8 @@ void SerialPortVIA::set_port_output(Port port, uint8_t value, uint8_t mask) {
|
||||
}
|
||||
|
||||
void SerialPortVIA::set_serial_line_state(::Commodore::Serial::Line line, bool value) {
|
||||
// printf("[C1540] %s is %s\n", StringForLine(line), value ? "high" : "low");
|
||||
|
||||
switch(line) {
|
||||
default: break;
|
||||
case ::Commodore::Serial::Line::Data: port_b_ = (port_b_ & ~0x01) | (value ? 0x00 : 0x01); break;
|
||||
@@ -158,7 +172,7 @@ void SerialPortVIA::set_serial_line_state(::Commodore::Serial::Line line, bool v
|
||||
case ::Commodore::Serial::Line::Attention:
|
||||
attention_level_input_ = !value;
|
||||
port_b_ = (port_b_ & ~0x80) | (value ? 0x00 : 0x80);
|
||||
set_control_line_input(Port::A, Line::One, !value);
|
||||
via_.set_control_line_input(MOS::MOS6522::Port::A, MOS::MOS6522::Line::One, !value);
|
||||
update_data_line();
|
||||
break;
|
||||
}
|
||||
@@ -186,7 +200,7 @@ void DriveVIA::set_delegate(Delegate *delegate) {
|
||||
// write protect tab uncovered
|
||||
DriveVIA::DriveVIA() : port_b_(0xff), port_a_(0xff), delegate_(nullptr) {}
|
||||
|
||||
uint8_t DriveVIA::get_port_input(Port port) {
|
||||
uint8_t DriveVIA::get_port_input(MOS::MOS6522::Port port) {
|
||||
return port ? port_b_ : port_a_;
|
||||
}
|
||||
|
||||
@@ -206,33 +220,35 @@ bool DriveVIA::get_motor_enabled() {
|
||||
return drive_motor_;
|
||||
}
|
||||
|
||||
void DriveVIA::set_control_line_output(Port port, Line line, bool value) {
|
||||
if(port == Port::A && line == Line::Two) {
|
||||
void DriveVIA::set_control_line_output(MOS::MOS6522::Port port, MOS::MOS6522::Line line, bool value) {
|
||||
if(port == MOS::MOS6522::Port::A && line == MOS::MOS6522::Line::Two) {
|
||||
should_set_overflow_ = value;
|
||||
}
|
||||
}
|
||||
|
||||
void DriveVIA::set_port_output(Port port, uint8_t value, uint8_t direction_mask) {
|
||||
void DriveVIA::set_port_output(MOS::MOS6522::Port port, uint8_t value, uint8_t direction_mask) {
|
||||
if(port) {
|
||||
// record drive motor state
|
||||
drive_motor_ = !!(value&4);
|
||||
if(previous_port_b_output_ != value) {
|
||||
// record drive motor state
|
||||
drive_motor_ = !!(value&4);
|
||||
|
||||
// check for a head step
|
||||
int step_difference = ((value&3) - (previous_port_b_output_&3))&3;
|
||||
if(step_difference) {
|
||||
if(delegate_) delegate_->drive_via_did_step_head(this, (step_difference == 1) ? 1 : -1);
|
||||
// check for a head step
|
||||
int step_difference = ((value&3) - (previous_port_b_output_&3))&3;
|
||||
if(step_difference) {
|
||||
if(delegate_) delegate_->drive_via_did_step_head(this, (step_difference == 1) ? 1 : -1);
|
||||
}
|
||||
|
||||
// check for a change in density
|
||||
int density_difference = (previous_port_b_output_^value) & (3 << 5);
|
||||
if(density_difference && delegate_) {
|
||||
delegate_->drive_via_did_set_data_density(this, (value >> 5)&3);
|
||||
}
|
||||
|
||||
// TODO: something with the drive LED
|
||||
// printf("LED: %s\n", value&8 ? "On" : "Off");
|
||||
|
||||
previous_port_b_output_ = value;
|
||||
}
|
||||
|
||||
// check for a change in density
|
||||
int density_difference = (previous_port_b_output_^value) & (3 << 5);
|
||||
if(density_difference && delegate_) {
|
||||
delegate_->drive_via_did_set_data_density(this, (value >> 5)&3);
|
||||
}
|
||||
|
||||
// TODO: something with the drive LED
|
||||
// printf("LED: %s\n", value&8 ? "On" : "Off");
|
||||
|
||||
previous_port_b_output_ = value;
|
||||
}
|
||||
}
|
||||
|
||||
158
Machines/Commodore/1540/Implementation/C1540Base.hpp
Normal file
158
Machines/Commodore/1540/Implementation/C1540Base.hpp
Normal file
@@ -0,0 +1,158 @@
|
||||
//
|
||||
// C1540Base.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 04/09/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef C1540Base_hpp
|
||||
#define C1540Base_hpp
|
||||
|
||||
#include "../../../../Processors/6502/6502.hpp"
|
||||
#include "../../../../Components/6522/6522.hpp"
|
||||
|
||||
#include "../../SerialBus.hpp"
|
||||
|
||||
#include "../../../../Storage/Disk/Disk.hpp"
|
||||
|
||||
#include "../../../../Storage/Disk/Controller/DiskController.hpp"
|
||||
|
||||
namespace Commodore {
|
||||
namespace C1540 {
|
||||
|
||||
/*!
|
||||
An implementation of the serial-port VIA in a Commodore 1540 — the VIA that facilitates all
|
||||
IEC bus communications.
|
||||
|
||||
It is wired up such that Port B contains:
|
||||
Bit 0: data input; 1 if the line is low, 0 if it is high;
|
||||
Bit 1: data output; 1 if the line should be low, 0 if it should be high;
|
||||
Bit 2: clock input; 1 if the line is low, 0 if it is high;
|
||||
Bit 3: clock output; 1 if the line is low, 0 if it is high;
|
||||
Bit 4: attention acknowledge output; exclusive ORd with the attention input and ORd onto the data output;
|
||||
Bits 5/6: device select input; the 1540 will act as device 8 + [value of bits]
|
||||
Bit 7: attention input; 1 if the line is low, 0 if it is high
|
||||
|
||||
The attention input is also connected to CA1, similarly inverted — the CA1 wire will be high when the bus is low and vice versa.
|
||||
*/
|
||||
class SerialPortVIA: public MOS::MOS6522::IRQDelegatePortHandler {
|
||||
public:
|
||||
SerialPortVIA(MOS::MOS6522::MOS6522<SerialPortVIA> &via);
|
||||
|
||||
uint8_t get_port_input(MOS::MOS6522::Port);
|
||||
|
||||
void set_port_output(MOS::MOS6522::Port, uint8_t value, uint8_t mask);
|
||||
void set_serial_line_state(::Commodore::Serial::Line, bool);
|
||||
|
||||
void set_serial_port(const std::shared_ptr<::Commodore::Serial::Port> &);
|
||||
|
||||
private:
|
||||
MOS::MOS6522::MOS6522<SerialPortVIA> &via_;
|
||||
uint8_t port_b_;
|
||||
std::weak_ptr<::Commodore::Serial::Port> serial_port_;
|
||||
bool attention_acknowledge_level_, attention_level_input_, data_level_output_;
|
||||
|
||||
void update_data_line();
|
||||
};
|
||||
|
||||
/*!
|
||||
An implementation of the drive VIA in a Commodore 1540 — the VIA that is used to interface with the disk.
|
||||
|
||||
It is wired up such that Port B contains:
|
||||
Bits 0/1: head step direction
|
||||
Bit 2: motor control
|
||||
Bit 3: LED control (TODO)
|
||||
Bit 4: write protect photocell status (TODO)
|
||||
Bits 5/6: read/write density
|
||||
Bit 7: 0 if sync marks are currently being detected, 1 otherwise.
|
||||
|
||||
... and Port A contains the byte most recently read from the disk or the byte next to write to the disk, depending on data direction.
|
||||
|
||||
It is implied that CA2 might be used to set processor overflow, CA1 a strobe for data input, and one of the CBs being definitive on
|
||||
whether the disk head is being told to read or write, but it's unclear and I've yet to investigate. So, TODO.
|
||||
*/
|
||||
class DriveVIA: public MOS::MOS6522::IRQDelegatePortHandler {
|
||||
public:
|
||||
class Delegate {
|
||||
public:
|
||||
virtual void drive_via_did_step_head(void *driveVIA, int direction) = 0;
|
||||
virtual void drive_via_did_set_data_density(void *driveVIA, int density) = 0;
|
||||
};
|
||||
void set_delegate(Delegate *);
|
||||
|
||||
DriveVIA();
|
||||
|
||||
uint8_t get_port_input(MOS::MOS6522::Port port);
|
||||
|
||||
void set_sync_detected(bool);
|
||||
void set_data_input(uint8_t);
|
||||
bool get_should_set_overflow();
|
||||
bool get_motor_enabled();
|
||||
|
||||
void set_control_line_output(MOS::MOS6522::Port, MOS::MOS6522::Line, bool value);
|
||||
|
||||
void set_port_output(MOS::MOS6522::Port, uint8_t value, uint8_t direction_mask);
|
||||
|
||||
private:
|
||||
uint8_t port_b_, port_a_;
|
||||
bool should_set_overflow_;
|
||||
bool drive_motor_;
|
||||
uint8_t previous_port_b_output_;
|
||||
Delegate *delegate_;
|
||||
};
|
||||
|
||||
/*!
|
||||
An implementation of the C1540's serial port; this connects incoming line levels to the serial-port VIA.
|
||||
*/
|
||||
class SerialPort : public ::Commodore::Serial::Port {
|
||||
public:
|
||||
void set_input(::Commodore::Serial::Line, ::Commodore::Serial::LineLevel);
|
||||
void set_serial_port_via(const std::shared_ptr<SerialPortVIA> &);
|
||||
|
||||
private:
|
||||
std::weak_ptr<SerialPortVIA> serial_port_VIA_;
|
||||
};
|
||||
|
||||
class MachineBase:
|
||||
public CPU::MOS6502::BusHandler,
|
||||
public MOS::MOS6522::IRQDelegatePortHandler::Delegate,
|
||||
public DriveVIA::Delegate,
|
||||
public Storage::Disk::Controller {
|
||||
|
||||
public:
|
||||
MachineBase();
|
||||
|
||||
// to satisfy CPU::MOS6502::Processor
|
||||
Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value);
|
||||
|
||||
// to satisfy MOS::MOS6522::Delegate
|
||||
virtual void mos6522_did_change_interrupt_status(void *mos6522);
|
||||
|
||||
// to satisfy DriveVIA::Delegate
|
||||
void drive_via_did_step_head(void *driveVIA, int direction);
|
||||
void drive_via_did_set_data_density(void *driveVIA, int density);
|
||||
|
||||
protected:
|
||||
CPU::MOS6502::Processor<MachineBase, false> m6502_;
|
||||
std::shared_ptr<Storage::Disk::Drive> drive_;
|
||||
|
||||
uint8_t ram_[0x800];
|
||||
uint8_t rom_[0x4000];
|
||||
|
||||
std::shared_ptr<SerialPortVIA> serial_port_VIA_port_handler_;
|
||||
std::shared_ptr<SerialPort> serial_port_;
|
||||
DriveVIA drive_VIA_port_handler_;
|
||||
|
||||
MOS::MOS6522::MOS6522<DriveVIA> drive_VIA_;
|
||||
MOS::MOS6522::MOS6522<SerialPortVIA> serial_port_VIA_;
|
||||
|
||||
int shift_register_, bit_window_offset_;
|
||||
virtual void process_input_bit(int value);
|
||||
virtual void process_index_hole();
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* C1540Base_hpp */
|
||||
@@ -27,12 +27,12 @@ void ::Commodore::Serial::AttachPortAndBus(std::shared_ptr<Port> port, std::shar
|
||||
|
||||
void Bus::add_port(std::shared_ptr<Port> port) {
|
||||
ports_.push_back(port);
|
||||
for(int line = (int)ServiceRequest; line <= (int)Reset; line++) {
|
||||
for(int line = static_cast<int>(ServiceRequest); line <= static_cast<int>(Reset); line++) {
|
||||
// the addition of a new device may change the line output...
|
||||
set_line_output_did_change((Line)line);
|
||||
set_line_output_did_change(static_cast<Line>(line));
|
||||
|
||||
// ... but the new device will need to be told the current state regardless
|
||||
port->set_input((Line)line, line_levels_[line]);
|
||||
port->set_input(static_cast<Line>(line), line_levels_[line]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@ void Bus::set_line_output_did_change(Line line) {
|
||||
}
|
||||
}
|
||||
|
||||
// printf("[Bus] %s is %s\n", StringForLine(line), new_line_level ? "high" : "low");
|
||||
|
||||
// post an update only if one occurred
|
||||
if(new_line_level != line_levels_[line]) {
|
||||
line_levels_[line] = new_line_level;
|
||||
|
||||
@@ -1,17 +1,80 @@
|
||||
//
|
||||
// Typer.cpp
|
||||
// Keyboard.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 05/11/2016.
|
||||
// Copyright © 2016 Thomas Harte. All rights reserved.
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "Vic20.hpp"
|
||||
#include "Keyboard.hpp"
|
||||
|
||||
uint16_t *Commodore::Vic20::Machine::sequence_for_character(Utility::Typer *typer, char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, EndSequence}
|
||||
#define SHIFT(...) {KeyLShift, __VA_ARGS__, EndSequence}
|
||||
#define X {NotMapped}
|
||||
using namespace Commodore::Vic20;
|
||||
|
||||
uint16_t KeyboardMapper::mapped_key_for_key(Inputs::Keyboard::Key key) {
|
||||
#define BIND(source, dest) case Inputs::Keyboard::Key::source: return Commodore::Vic20::dest
|
||||
switch(key) {
|
||||
default: break;
|
||||
|
||||
BIND(k0, Key0); BIND(k1, Key1); BIND(k2, Key2); BIND(k3, Key3); BIND(k4, Key4);
|
||||
BIND(k5, Key5); BIND(k6, Key6); BIND(k7, Key7); BIND(k8, Key8); BIND(k9, Key9);
|
||||
BIND(Q, KeyQ); BIND(W, KeyW); BIND(E, KeyE); BIND(R, KeyR); BIND(T, KeyT);
|
||||
BIND(Y, KeyY); BIND(U, KeyU); BIND(I, KeyI); BIND(O, KeyO); BIND(P, KeyP);
|
||||
BIND(A, KeyA); BIND(S, KeyS); BIND(D, KeyD); BIND(F, KeyF); BIND(G, KeyG);
|
||||
BIND(H, KeyH); BIND(J, KeyJ); BIND(K, KeyK); BIND(L, KeyL);
|
||||
BIND(Z, KeyZ); BIND(X, KeyX); BIND(C, KeyC); BIND(V, KeyV);
|
||||
BIND(B, KeyB); BIND(N, KeyN); BIND(M, KeyM);
|
||||
|
||||
BIND(BackTick, KeyLeft);
|
||||
BIND(Hyphen, KeyPlus);
|
||||
BIND(Equals, KeyDash);
|
||||
BIND(F11, KeyGBP);
|
||||
BIND(F12, KeyHome);
|
||||
|
||||
BIND(Tab, KeyControl);
|
||||
BIND(OpenSquareBracket, KeyAt);
|
||||
BIND(CloseSquareBracket, KeyAsterisk);
|
||||
|
||||
BIND(BackSlash, KeyRestore);
|
||||
BIND(Hash, KeyUp);
|
||||
BIND(F10, KeyUp);
|
||||
|
||||
BIND(Semicolon, KeyColon);
|
||||
BIND(Quote, KeySemicolon);
|
||||
BIND(F9, KeyEquals);
|
||||
|
||||
BIND(LeftMeta, KeyCBM);
|
||||
BIND(LeftOption, KeyCBM);
|
||||
BIND(RightOption, KeyCBM);
|
||||
BIND(RightMeta, KeyCBM);
|
||||
|
||||
BIND(LeftShift, KeyLShift);
|
||||
BIND(RightShift, KeyRShift);
|
||||
|
||||
BIND(Comma, KeyComma);
|
||||
BIND(FullStop, KeyFullStop);
|
||||
BIND(ForwardSlash, KeySlash);
|
||||
|
||||
BIND(Right, KeyRight);
|
||||
BIND(Down, KeyDown);
|
||||
|
||||
BIND(Enter, KeyReturn);
|
||||
BIND(Space, KeySpace);
|
||||
BIND(BackSpace, KeyDelete);
|
||||
|
||||
BIND(Escape, KeyRunStop);
|
||||
BIND(F1, KeyF1);
|
||||
BIND(F3, KeyF3);
|
||||
BIND(F5, KeyF5);
|
||||
BIND(F7, KeyF7);
|
||||
}
|
||||
#undef BIND
|
||||
return KeyboardMachine::Machine::KeyNotMapped;
|
||||
}
|
||||
|
||||
uint16_t *CharacterMapper::sequence_for_character(char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define SHIFT(...) {KeyLShift, __VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define X {KeyboardMachine::Machine::KeyNotMapped}
|
||||
static KeySequence key_sequences[] = {
|
||||
/* NUL */ X, /* SOH */ X,
|
||||
/* STX */ X, /* ETX */ X,
|
||||
52
Machines/Commodore/Vic-20/Keyboard.hpp
Normal file
52
Machines/Commodore/Vic-20/Keyboard.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Keyboard.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Machines_Commodore_Vic20_Keyboard_hpp
|
||||
#define Machines_Commodore_Vic20_Keyboard_hpp
|
||||
|
||||
#include "../../KeyboardMachine.hpp"
|
||||
#include "../../Utility/Typer.hpp"
|
||||
|
||||
namespace Commodore {
|
||||
namespace Vic20 {
|
||||
|
||||
enum Key: uint16_t {
|
||||
#define key(line, mask) (((mask) << 3) | (line))
|
||||
Key2 = key(7, 0x01), Key4 = key(7, 0x02), Key6 = key(7, 0x04), Key8 = key(7, 0x08),
|
||||
Key0 = key(7, 0x10), KeyDash = key(7, 0x20), KeyHome = key(7, 0x40), KeyF7 = key(7, 0x80),
|
||||
KeyQ = key(6, 0x01), KeyE = key(6, 0x02), KeyT = key(6, 0x04), KeyU = key(6, 0x08),
|
||||
KeyO = key(6, 0x10), KeyAt = key(6, 0x20), KeyUp = key(6, 0x40), KeyF5 = key(6, 0x80),
|
||||
KeyCBM = key(5, 0x01), KeyS = key(5, 0x02), KeyF = key(5, 0x04), KeyH = key(5, 0x08),
|
||||
KeyK = key(5, 0x10), KeyColon = key(5, 0x20), KeyEquals = key(5, 0x40), KeyF3 = key(5, 0x80),
|
||||
KeySpace = key(4, 0x01), KeyZ = key(4, 0x02), KeyC = key(4, 0x04), KeyB = key(4, 0x08),
|
||||
KeyM = key(4, 0x10), KeyFullStop = key(4, 0x20), KeyRShift = key(4, 0x40), KeyF1 = key(4, 0x80),
|
||||
KeyRunStop = key(3, 0x01), KeyLShift = key(3, 0x02), KeyX = key(3, 0x04), KeyV = key(3, 0x08),
|
||||
KeyN = key(3, 0x10), KeyComma = key(3, 0x20), KeySlash = key(3, 0x40), KeyDown = key(3, 0x80),
|
||||
KeyControl = key(2, 0x01), KeyA = key(2, 0x02), KeyD = key(2, 0x04), KeyG = key(2, 0x08),
|
||||
KeyJ = key(2, 0x10), KeyL = key(2, 0x20), KeySemicolon = key(2, 0x40), KeyRight = key(2, 0x80),
|
||||
KeyLeft = key(1, 0x01), KeyW = key(1, 0x02), KeyR = key(1, 0x04), KeyY = key(1, 0x08),
|
||||
KeyI = key(1, 0x10), KeyP = key(1, 0x20), KeyAsterisk = key(1, 0x40), KeyReturn = key(1, 0x80),
|
||||
Key1 = key(0, 0x01), Key3 = key(0, 0x02), Key5 = key(0, 0x04), Key7 = key(0, 0x08),
|
||||
Key9 = key(0, 0x10), KeyPlus = key(0, 0x20), KeyGBP = key(0, 0x40), KeyDelete = key(0, 0x80),
|
||||
|
||||
KeyRestore = 0xfffd
|
||||
#undef key
|
||||
};
|
||||
|
||||
struct KeyboardMapper: public KeyboardMachine::Machine::KeyboardMapper {
|
||||
uint16_t mapped_key_for_key(Inputs::Keyboard::Key key);
|
||||
};
|
||||
|
||||
struct CharacterMapper: public ::Utility::CharacterMapper {
|
||||
uint16_t *sequence_for_character(char character);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* Keyboard_hpp */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -11,17 +11,10 @@
|
||||
|
||||
#include "../../ConfigurationTarget.hpp"
|
||||
#include "../../CRTMachine.hpp"
|
||||
#include "../../Typer.hpp"
|
||||
#include "../../KeyboardMachine.hpp"
|
||||
#include "../../JoystickMachine.hpp"
|
||||
|
||||
#include "../../../Processors/6502/6502.hpp"
|
||||
#include "../../../Components/6560/6560.hpp"
|
||||
#include "../../../Components/6522/6522.hpp"
|
||||
|
||||
#include "../SerialBus.hpp"
|
||||
#include "../1540/C1540.hpp"
|
||||
|
||||
#include "../../../Storage/Tape/Tape.hpp"
|
||||
#include "../../../Storage/Disk/Disk.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
namespace Commodore {
|
||||
namespace Vic20 {
|
||||
@@ -44,181 +37,29 @@ enum Region {
|
||||
PAL
|
||||
};
|
||||
|
||||
#define key(line, mask) (((mask) << 3) | (line))
|
||||
|
||||
enum Key: uint16_t {
|
||||
Key2 = key(7, 0x01), Key4 = key(7, 0x02), Key6 = key(7, 0x04), Key8 = key(7, 0x08),
|
||||
Key0 = key(7, 0x10), KeyDash = key(7, 0x20), KeyHome = key(7, 0x40), KeyF7 = key(7, 0x80),
|
||||
KeyQ = key(6, 0x01), KeyE = key(6, 0x02), KeyT = key(6, 0x04), KeyU = key(6, 0x08),
|
||||
KeyO = key(6, 0x10), KeyAt = key(6, 0x20), KeyUp = key(6, 0x40), KeyF5 = key(6, 0x80),
|
||||
KeyCBM = key(5, 0x01), KeyS = key(5, 0x02), KeyF = key(5, 0x04), KeyH = key(5, 0x08),
|
||||
KeyK = key(5, 0x10), KeyColon = key(5, 0x20), KeyEquals = key(5, 0x40), KeyF3 = key(5, 0x80),
|
||||
KeySpace = key(4, 0x01), KeyZ = key(4, 0x02), KeyC = key(4, 0x04), KeyB = key(4, 0x08),
|
||||
KeyM = key(4, 0x10), KeyFullStop = key(4, 0x20), KeyRShift = key(4, 0x40), KeyF1 = key(4, 0x80),
|
||||
KeyRunStop = key(3, 0x01), KeyLShift = key(3, 0x02), KeyX = key(3, 0x04), KeyV = key(3, 0x08),
|
||||
KeyN = key(3, 0x10), KeyComma = key(3, 0x20), KeySlash = key(3, 0x40), KeyDown = key(3, 0x80),
|
||||
KeyControl = key(2, 0x01), KeyA = key(2, 0x02), KeyD = key(2, 0x04), KeyG = key(2, 0x08),
|
||||
KeyJ = key(2, 0x10), KeyL = key(2, 0x20), KeySemicolon = key(2, 0x40), KeyRight = key(2, 0x80),
|
||||
KeyLeft = key(1, 0x01), KeyW = key(1, 0x02), KeyR = key(1, 0x04), KeyY = key(1, 0x08),
|
||||
KeyI = key(1, 0x10), KeyP = key(1, 0x20), KeyAsterisk = key(1, 0x40), KeyReturn = key(1, 0x80),
|
||||
Key1 = key(0, 0x01), Key3 = key(0, 0x02), Key5 = key(0, 0x04), Key7 = key(0, 0x08),
|
||||
Key9 = key(0, 0x10), KeyPlus = key(0, 0x20), KeyGBP = key(0, 0x40), KeyDelete = key(0, 0x80),
|
||||
};
|
||||
|
||||
enum JoystickInput {
|
||||
Up = 0x04,
|
||||
Down = 0x08,
|
||||
Left = 0x10,
|
||||
Right = 0x80,
|
||||
Fire = 0x20
|
||||
};
|
||||
|
||||
class UserPortVIA: public MOS::MOS6522<UserPortVIA>, public MOS::MOS6522IRQDelegate {
|
||||
public:
|
||||
UserPortVIA();
|
||||
using MOS6522IRQDelegate::set_interrupt_status;
|
||||
|
||||
uint8_t get_port_input(Port port);
|
||||
void set_control_line_output(Port port, Line line, bool value);
|
||||
void set_serial_line_state(::Commodore::Serial::Line line, bool value);
|
||||
void set_joystick_state(JoystickInput input, bool value);
|
||||
void set_port_output(Port port, uint8_t value, uint8_t mask);
|
||||
|
||||
void set_serial_port(std::shared_ptr<::Commodore::Serial::Port> serialPort);
|
||||
void set_tape(std::shared_ptr<Storage::Tape::BinaryTapePlayer> tape);
|
||||
|
||||
private:
|
||||
uint8_t port_a_;
|
||||
std::weak_ptr<::Commodore::Serial::Port> serial_port_;
|
||||
std::shared_ptr<Storage::Tape::BinaryTapePlayer> tape_;
|
||||
};
|
||||
|
||||
class KeyboardVIA: public MOS::MOS6522<KeyboardVIA>, public MOS::MOS6522IRQDelegate {
|
||||
public:
|
||||
KeyboardVIA();
|
||||
using MOS6522IRQDelegate::set_interrupt_status;
|
||||
|
||||
void set_key_state(uint16_t key, bool isPressed);
|
||||
void clear_all_keys();
|
||||
|
||||
// to satisfy MOS::MOS6522
|
||||
uint8_t get_port_input(Port port);
|
||||
|
||||
void set_port_output(Port port, uint8_t value, uint8_t mask);
|
||||
void set_control_line_output(Port port, Line line, bool value);
|
||||
|
||||
void set_joystick_state(JoystickInput input, bool value);
|
||||
|
||||
void set_serial_port(std::shared_ptr<::Commodore::Serial::Port> serialPort);
|
||||
|
||||
private:
|
||||
uint8_t port_b_;
|
||||
uint8_t columns_[8];
|
||||
uint8_t activation_mask_;
|
||||
std::weak_ptr<::Commodore::Serial::Port> serial_port_;
|
||||
};
|
||||
|
||||
class SerialPort : public ::Commodore::Serial::Port {
|
||||
public:
|
||||
void set_input(::Commodore::Serial::Line line, ::Commodore::Serial::LineLevel level);
|
||||
void set_user_port_via(std::shared_ptr<UserPortVIA> userPortVIA);
|
||||
|
||||
private:
|
||||
std::weak_ptr<UserPortVIA> user_port_via_;
|
||||
};
|
||||
|
||||
class Vic6560: public MOS::MOS6560<Vic6560> {
|
||||
public:
|
||||
inline void perform_read(uint16_t address, uint8_t *pixel_data, uint8_t *colour_data) {
|
||||
*pixel_data = video_memory_map[address >> 10] ? video_memory_map[address >> 10][address & 0x3ff] : 0xff; // TODO
|
||||
*colour_data = colour_memory[address & 0x03ff];
|
||||
}
|
||||
|
||||
uint8_t *video_memory_map[16];
|
||||
uint8_t *colour_memory;
|
||||
};
|
||||
|
||||
class Machine:
|
||||
public CPU::MOS6502::Processor<Machine>,
|
||||
public CRTMachine::Machine,
|
||||
public MOS::MOS6522IRQDelegate::Delegate,
|
||||
public Utility::TypeRecipient,
|
||||
public Storage::Tape::BinaryTapePlayer::Delegate,
|
||||
public ConfigurationTarget::Machine {
|
||||
|
||||
public ConfigurationTarget::Machine,
|
||||
public KeyboardMachine::Machine,
|
||||
public JoystickMachine::Machine {
|
||||
public:
|
||||
Machine();
|
||||
~Machine();
|
||||
virtual ~Machine();
|
||||
|
||||
void set_rom(ROMSlot slot, size_t length, const uint8_t *data);
|
||||
void configure_as_target(const StaticAnalyser::Target &target);
|
||||
/// Creates and returns a Vic-20.
|
||||
static Machine *Vic20();
|
||||
|
||||
void set_key_state(uint16_t key, bool isPressed) { keyboard_via_->set_key_state(key, isPressed); }
|
||||
void clear_all_keys() { keyboard_via_->clear_all_keys(); }
|
||||
void set_joystick_state(JoystickInput input, bool isPressed) {
|
||||
user_port_via_->set_joystick_state(input, isPressed);
|
||||
keyboard_via_->set_joystick_state(input, isPressed);
|
||||
}
|
||||
/// Sets the contents of the rom in @c slot to the buffer @c data of length @c length.
|
||||
virtual void set_rom(ROMSlot slot, size_t length, const uint8_t *data) = 0;
|
||||
// TODO: take a std::vector<uint8_t> to collapse length and data.
|
||||
|
||||
void set_memory_size(MemorySize size);
|
||||
void set_region(Region region);
|
||||
/// Sets the memory size of this Vic-20.
|
||||
virtual void set_memory_size(MemorySize size) = 0;
|
||||
|
||||
inline void set_use_fast_tape_hack(bool activate) { use_fast_tape_hack_ = activate; }
|
||||
/// Sets the region of this Vic-20.
|
||||
virtual void set_region(Region region) = 0;
|
||||
|
||||
// to satisfy CPU::MOS6502::Processor
|
||||
Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value);
|
||||
void flush() { mos6560_->flush(); }
|
||||
|
||||
// to satisfy CRTMachine::Machine
|
||||
virtual void setup_output(float aspect_ratio);
|
||||
virtual void close_output();
|
||||
virtual std::shared_ptr<Outputs::CRT::CRT> get_crt() { return mos6560_->get_crt(); }
|
||||
virtual std::shared_ptr<Outputs::Speaker> get_speaker() { return mos6560_->get_speaker(); }
|
||||
virtual void run_for(const Cycles cycles) { CPU::MOS6502::Processor<Machine>::run_for(cycles); }
|
||||
|
||||
// to satisfy MOS::MOS6522::Delegate
|
||||
virtual void mos6522_did_change_interrupt_status(void *mos6522);
|
||||
|
||||
// for Utility::TypeRecipient
|
||||
uint16_t *sequence_for_character(Utility::Typer *typer, char character);
|
||||
|
||||
// for Tape::Delegate
|
||||
virtual void tape_did_change_input(Storage::Tape::BinaryTapePlayer *tape);
|
||||
|
||||
private:
|
||||
uint8_t character_rom_[0x1000];
|
||||
uint8_t basic_rom_[0x2000];
|
||||
uint8_t kernel_rom_[0x2000];
|
||||
uint8_t expansion_ram_[0x8000];
|
||||
|
||||
uint8_t *rom_;
|
||||
uint16_t rom_address_, rom_length_;
|
||||
|
||||
uint8_t user_basic_memory_[0x0400];
|
||||
uint8_t screen_memory_[0x1000];
|
||||
uint8_t colour_memory_[0x0400];
|
||||
std::vector<uint8_t> drive_rom_;
|
||||
|
||||
uint8_t *processor_read_memory_map_[64];
|
||||
uint8_t *processor_write_memory_map_[64];
|
||||
void write_to_map(uint8_t **map, uint8_t *area, uint16_t address, uint16_t length);
|
||||
|
||||
Region region_;
|
||||
|
||||
std::unique_ptr<Vic6560> mos6560_;
|
||||
std::shared_ptr<UserPortVIA> user_port_via_;
|
||||
std::shared_ptr<KeyboardVIA> keyboard_via_;
|
||||
std::shared_ptr<SerialPort> serial_port_;
|
||||
std::shared_ptr<::Commodore::Serial::Bus> serial_bus_;
|
||||
|
||||
// Tape
|
||||
std::shared_ptr<Storage::Tape::BinaryTapePlayer> tape_;
|
||||
bool use_fast_tape_hack_;
|
||||
bool is_running_at_zero_cost_;
|
||||
|
||||
// Disk
|
||||
std::shared_ptr<::Commodore::C1540::Machine> c1540_;
|
||||
void install_disk_rom();
|
||||
/// Enables or disables turbo-speed tape loading.
|
||||
virtual void set_use_fast_tape_hack(bool activate) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -15,11 +15,19 @@ namespace ConfigurationTarget {
|
||||
|
||||
/*!
|
||||
A ConfigurationTarget::Machine is anything that can accept a StaticAnalyser::Target
|
||||
and configure itself appropriately.
|
||||
and configure itself appropriately, or accept a list of media subsequently to insert.
|
||||
*/
|
||||
class Machine {
|
||||
public:
|
||||
/// Instructs the machine to configure itself as described by @c target and insert the included media.
|
||||
virtual void configure_as_target(const StaticAnalyser::Target &target) = 0;
|
||||
|
||||
/*!
|
||||
Requests that the machine insert @c media as a modification to current state
|
||||
|
||||
@returns @c true if any media was inserted; @c false otherwise.
|
||||
*/
|
||||
virtual bool insert_media(const StaticAnalyser::Media &media) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -8,390 +8,475 @@
|
||||
|
||||
#include "Electron.hpp"
|
||||
|
||||
#include "../../Processors/6502/6502.hpp"
|
||||
#include "../../Storage/Tape/Tape.hpp"
|
||||
#include "../../ClockReceiver/ClockReceiver.hpp"
|
||||
#include "../../ClockReceiver/ForceInline.hpp"
|
||||
|
||||
#include "../Utility/Typer.hpp"
|
||||
|
||||
#include "Interrupts.hpp"
|
||||
#include "Keyboard.hpp"
|
||||
#include "Plus3.hpp"
|
||||
#include "Speaker.hpp"
|
||||
#include "Tape.hpp"
|
||||
#include "Video.hpp"
|
||||
|
||||
namespace Electron {
|
||||
|
||||
class ConcreteMachine:
|
||||
public Machine,
|
||||
public CPU::MOS6502::BusHandler,
|
||||
public Tape::Delegate,
|
||||
public Utility::TypeRecipient {
|
||||
public:
|
||||
ConcreteMachine() : m6502_(*this) {
|
||||
memset(key_states_, 0, sizeof(key_states_));
|
||||
for(int c = 0; c < 16; c++)
|
||||
memset(roms_[c], 0xff, 16384);
|
||||
|
||||
tape_.set_delegate(this);
|
||||
set_clock_rate(2000000);
|
||||
}
|
||||
|
||||
void set_rom(ROMSlot slot, std::vector<uint8_t> data, bool is_writeable) override final {
|
||||
uint8_t *target = nullptr;
|
||||
switch(slot) {
|
||||
case ROMSlotDFS: dfs_ = data; return;
|
||||
case ROMSlotADFS: adfs_ = data; return;
|
||||
|
||||
case ROMSlotOS: target = os_; break;
|
||||
default:
|
||||
target = roms_[slot];
|
||||
rom_write_masks_[slot] = is_writeable;
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(target, &data[0], std::min(static_cast<size_t>(16384), data.size()));
|
||||
}
|
||||
|
||||
void set_key_state(uint16_t key, bool isPressed) override final {
|
||||
if(key == KeyBreak) {
|
||||
m6502_.set_reset_line(isPressed);
|
||||
} else {
|
||||
if(isPressed)
|
||||
key_states_[key >> 4] |= key&0xf;
|
||||
else
|
||||
key_states_[key >> 4] &= ~(key&0xf);
|
||||
}
|
||||
}
|
||||
|
||||
void clear_all_keys() override final {
|
||||
memset(key_states_, 0, sizeof(key_states_));
|
||||
if(is_holding_shift_) set_key_state(KeyShift, true);
|
||||
}
|
||||
|
||||
void set_use_fast_tape_hack(bool activate) override final {
|
||||
use_fast_tape_hack_ = activate;
|
||||
}
|
||||
|
||||
void configure_as_target(const StaticAnalyser::Target &target) override final {
|
||||
if(target.loadingCommand.length()) {
|
||||
set_typer_for_string(target.loadingCommand.c_str());
|
||||
}
|
||||
|
||||
if(target.acorn.should_shift_restart) {
|
||||
shift_restart_counter_ = 1000000;
|
||||
}
|
||||
|
||||
if(target.acorn.has_dfs || target.acorn.has_adfs) {
|
||||
plus3_.reset(new Plus3);
|
||||
|
||||
if(target.acorn.has_dfs) {
|
||||
set_rom(ROMSlot0, dfs_, true);
|
||||
}
|
||||
if(target.acorn.has_adfs) {
|
||||
set_rom(ROMSlot4, adfs_, true);
|
||||
set_rom(ROMSlot5, std::vector<uint8_t>(adfs_.begin() + 16384, adfs_.end()), true);
|
||||
}
|
||||
}
|
||||
|
||||
insert_media(target.media);
|
||||
}
|
||||
|
||||
bool insert_media(const StaticAnalyser::Media &media) override final {
|
||||
if(!media.tapes.empty()) {
|
||||
tape_.set_tape(media.tapes.front());
|
||||
}
|
||||
|
||||
if(!media.disks.empty() && plus3_) {
|
||||
plus3_->set_disk(media.disks.front(), 0);
|
||||
}
|
||||
|
||||
ROMSlot slot = ROMSlot12;
|
||||
for(std::shared_ptr<Storage::Cartridge::Cartridge> cartridge : media.cartridges) {
|
||||
set_rom(slot, cartridge->get_segments().front().data, false);
|
||||
slot = static_cast<ROMSlot>((static_cast<int>(slot) + 1)&15);
|
||||
}
|
||||
|
||||
return !media.tapes.empty() || !media.disks.empty() || !media.cartridges.empty();
|
||||
}
|
||||
|
||||
forceinline Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
unsigned int cycles = 1;
|
||||
|
||||
if(address < 0x8000) {
|
||||
if(isReadOperation(operation)) {
|
||||
*value = ram_[address];
|
||||
} else {
|
||||
if(address >= video_access_range_.low_address && address <= video_access_range_.high_address) update_display();
|
||||
ram_[address] = *value;
|
||||
}
|
||||
|
||||
// for the entire frame, RAM is accessible only on odd cycles; in modes below 4
|
||||
// it's also accessible only outside of the pixel regions
|
||||
cycles += video_output_->get_cycles_until_next_ram_availability(cycles_since_display_update_.as_int() + 1);
|
||||
} else {
|
||||
switch(address & 0xff0f) {
|
||||
case 0xfe00:
|
||||
if(isReadOperation(operation)) {
|
||||
*value = interrupt_status_;
|
||||
interrupt_status_ &= ~PowerOnReset;
|
||||
} else {
|
||||
interrupt_control_ = (*value) & ~1;
|
||||
evaluate_interrupts();
|
||||
}
|
||||
break;
|
||||
case 0xfe07:
|
||||
if(!isReadOperation(operation)) {
|
||||
// update speaker mode
|
||||
bool new_speaker_is_enabled = (*value & 6) == 2;
|
||||
if(new_speaker_is_enabled != speaker_is_enabled_) {
|
||||
update_audio();
|
||||
speaker_->set_is_enabled(new_speaker_is_enabled);
|
||||
speaker_is_enabled_ = new_speaker_is_enabled;
|
||||
}
|
||||
|
||||
tape_.set_is_enabled((*value & 6) != 6);
|
||||
tape_.set_is_in_input_mode((*value & 6) == 0);
|
||||
tape_.set_is_running(((*value)&0x40) ? true : false);
|
||||
|
||||
// TODO: caps lock LED
|
||||
}
|
||||
|
||||
// deliberate fallthrough
|
||||
case 0xfe02: case 0xfe03:
|
||||
case 0xfe08: case 0xfe09: case 0xfe0a: case 0xfe0b:
|
||||
case 0xfe0c: case 0xfe0d: case 0xfe0e: case 0xfe0f:
|
||||
if(!isReadOperation(operation)) {
|
||||
update_display();
|
||||
video_output_->set_register(address, *value);
|
||||
video_access_range_ = video_output_->get_memory_access_range();
|
||||
queue_next_display_interrupt();
|
||||
}
|
||||
break;
|
||||
case 0xfe04:
|
||||
if(isReadOperation(operation)) {
|
||||
*value = tape_.get_data_register();
|
||||
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
|
||||
} else {
|
||||
tape_.set_data_register(*value);
|
||||
tape_.clear_interrupts(Interrupt::TransmitDataEmpty);
|
||||
}
|
||||
break;
|
||||
case 0xfe05:
|
||||
if(!isReadOperation(operation)) {
|
||||
const uint8_t interruptDisable = (*value)&0xf0;
|
||||
if( interruptDisable ) {
|
||||
if( interruptDisable&0x10 ) interrupt_status_ &= ~Interrupt::DisplayEnd;
|
||||
if( interruptDisable&0x20 ) interrupt_status_ &= ~Interrupt::RealTimeClock;
|
||||
if( interruptDisable&0x40 ) interrupt_status_ &= ~Interrupt::HighToneDetect;
|
||||
evaluate_interrupts();
|
||||
|
||||
// TODO: NMI
|
||||
}
|
||||
|
||||
// latch the paged ROM in case external hardware is being emulated
|
||||
active_rom_ = (Electron::ROMSlot)(*value & 0xf);
|
||||
|
||||
// apply the ULA's test
|
||||
if(*value & 0x08) {
|
||||
if(*value & 0x04) {
|
||||
keyboard_is_active_ = false;
|
||||
basic_is_active_ = false;
|
||||
} else {
|
||||
keyboard_is_active_ = !(*value & 0x02);
|
||||
basic_is_active_ = !keyboard_is_active_;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 0xfe06:
|
||||
if(!isReadOperation(operation)) {
|
||||
update_audio();
|
||||
speaker_->set_divider(*value);
|
||||
tape_.set_counter(*value);
|
||||
}
|
||||
break;
|
||||
|
||||
case 0xfc04: case 0xfc05: case 0xfc06: case 0xfc07:
|
||||
if(plus3_ && (address&0x00f0) == 0x00c0) {
|
||||
if(is_holding_shift_ && address == 0xfcc4) {
|
||||
is_holding_shift_ = false;
|
||||
set_key_state(KeyShift, false);
|
||||
}
|
||||
if(isReadOperation(operation))
|
||||
*value = plus3_->get_register(address);
|
||||
else
|
||||
plus3_->set_register(address, *value);
|
||||
}
|
||||
break;
|
||||
case 0xfc00:
|
||||
if(plus3_ && (address&0x00f0) == 0x00c0) {
|
||||
if(!isReadOperation(operation)) {
|
||||
plus3_->set_control_register(*value);
|
||||
} else *value = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if(address >= 0xc000) {
|
||||
if(isReadOperation(operation)) {
|
||||
if(
|
||||
use_fast_tape_hack_ &&
|
||||
tape_.has_tape() &&
|
||||
(operation == CPU::MOS6502::BusOperation::ReadOpcode) &&
|
||||
(
|
||||
(address == 0xf4e5) || (address == 0xf4e6) || // double NOPs at 0xf4e5, 0xf6de, 0xf6fa and 0xfa51
|
||||
(address == 0xf6de) || (address == 0xf6df) || // act to disable the normal branch into tape-handling
|
||||
(address == 0xf6fa) || (address == 0xf6fb) || // code, forcing the OS along the serially-accessed ROM
|
||||
(address == 0xfa51) || (address == 0xfa52) || // pathway.
|
||||
|
||||
(address == 0xf0a8) // 0xf0a8 is from where a service call would normally be
|
||||
// dispatched; we can check whether it would be call 14
|
||||
// (i.e. read byte) and, if so, whether the OS was about to
|
||||
// issue a read byte call to a ROM despite being the tape
|
||||
// FS being selected. If so then this is a get byte that
|
||||
// we should service synthetically. Put the byte into Y
|
||||
// and set A to zero to report that action was taken, then
|
||||
// allow the PC read to return an RTS.
|
||||
)
|
||||
) {
|
||||
uint8_t service_call = static_cast<uint8_t>(m6502_.get_value_of_register(CPU::MOS6502::Register::X));
|
||||
if(address == 0xf0a8) {
|
||||
if(!ram_[0x247] && service_call == 14) {
|
||||
tape_.set_delegate(nullptr);
|
||||
|
||||
// TODO: handle tape wrap around.
|
||||
|
||||
int cycles_left_while_plausibly_in_data = 50;
|
||||
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
|
||||
while(!tape_.get_tape()->is_at_end()) {
|
||||
tape_.run_for_input_pulse();
|
||||
cycles_left_while_plausibly_in_data--;
|
||||
if(!cycles_left_while_plausibly_in_data) fast_load_is_in_data_ = false;
|
||||
if( (tape_.get_interrupt_status() & Interrupt::ReceiveDataFull) &&
|
||||
(fast_load_is_in_data_ || tape_.get_data_register() == 0x2a)
|
||||
) break;
|
||||
}
|
||||
tape_.set_delegate(this);
|
||||
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
|
||||
interrupt_status_ |= tape_.get_interrupt_status();
|
||||
|
||||
fast_load_is_in_data_ = true;
|
||||
m6502_.set_value_of_register(CPU::MOS6502::Register::A, 0);
|
||||
m6502_.set_value_of_register(CPU::MOS6502::Register::Y, tape_.get_data_register());
|
||||
*value = 0x60; // 0x60 is RTS
|
||||
}
|
||||
else *value = os_[address & 16383];
|
||||
}
|
||||
else *value = 0xea;
|
||||
} else {
|
||||
*value = os_[address & 16383];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(isReadOperation(operation)) {
|
||||
*value = roms_[active_rom_][address & 16383];
|
||||
if(keyboard_is_active_) {
|
||||
*value &= 0xf0;
|
||||
for(int address_line = 0; address_line < 14; address_line++) {
|
||||
if(!(address&(1 << address_line))) *value |= key_states_[address_line];
|
||||
}
|
||||
}
|
||||
if(basic_is_active_) {
|
||||
*value &= roms_[ROMSlotBASIC][address & 16383];
|
||||
}
|
||||
} else if(rom_write_masks_[active_rom_]) {
|
||||
roms_[active_rom_][address & 16383] = *value;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cycles_since_display_update_ += Cycles(static_cast<int>(cycles));
|
||||
cycles_since_audio_update_ += Cycles(static_cast<int>(cycles));
|
||||
if(cycles_since_audio_update_ > Cycles(16384)) update_audio();
|
||||
tape_.run_for(Cycles(static_cast<int>(cycles)));
|
||||
|
||||
cycles_until_display_interrupt_ -= cycles;
|
||||
if(cycles_until_display_interrupt_ < 0) {
|
||||
signal_interrupt(next_display_interrupt_);
|
||||
update_display();
|
||||
queue_next_display_interrupt();
|
||||
}
|
||||
|
||||
if(typer_) typer_->run_for(Cycles(static_cast<int>(cycles)));
|
||||
if(plus3_) plus3_->run_for(Cycles(4*static_cast<int>(cycles)));
|
||||
if(shift_restart_counter_) {
|
||||
shift_restart_counter_ -= cycles;
|
||||
if(shift_restart_counter_ <= 0) {
|
||||
shift_restart_counter_ = 0;
|
||||
m6502_.set_power_on(true);
|
||||
set_key_state(KeyShift, true);
|
||||
is_holding_shift_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
return Cycles(static_cast<int>(cycles));
|
||||
}
|
||||
|
||||
forceinline void flush() {
|
||||
update_display();
|
||||
update_audio();
|
||||
speaker_->flush();
|
||||
}
|
||||
|
||||
void setup_output(float aspect_ratio) override final {
|
||||
video_output_.reset(new VideoOutput(ram_));
|
||||
|
||||
// The maximum output frequency is 62500Hz and all other permitted output frequencies are integral divisions of that;
|
||||
// however setting the speaker on or off can happen on any 2Mhz cycle, and probably (?) takes effect immediately. So
|
||||
// run the speaker at a 2000000Hz input rate, at least for the time being.
|
||||
speaker_.reset(new Speaker);
|
||||
speaker_->set_input_rate(2000000 / Speaker::clock_rate_divider);
|
||||
}
|
||||
|
||||
void close_output() override final {
|
||||
video_output_.reset();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> get_crt() override final {
|
||||
return video_output_->get_crt();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::Speaker> get_speaker() override final {
|
||||
return speaker_;
|
||||
}
|
||||
|
||||
void run_for(const Cycles cycles) override final {
|
||||
m6502_.run_for(cycles);
|
||||
}
|
||||
|
||||
void tape_did_change_interrupt_status(Tape *tape) override final {
|
||||
interrupt_status_ = (interrupt_status_ & ~(Interrupt::TransmitDataEmpty | Interrupt::ReceiveDataFull | Interrupt::HighToneDetect)) | tape_.get_interrupt_status();
|
||||
evaluate_interrupts();
|
||||
}
|
||||
|
||||
HalfCycles get_typer_delay() override final {
|
||||
return m6502_.get_is_resetting() ? Cycles(625*25*128) : Cycles(0); // wait one second if resetting
|
||||
}
|
||||
|
||||
HalfCycles get_typer_frequency() override final {
|
||||
return Cycles(625*128*2); // accept a new character every two frames
|
||||
}
|
||||
|
||||
void set_typer_for_string(const char *string) override final {
|
||||
std::unique_ptr<CharacterMapper> mapper(new CharacterMapper());
|
||||
Utility::TypeRecipient::set_typer_for_string(string, std::move(mapper));
|
||||
}
|
||||
|
||||
KeyboardMapper &get_keyboard_mapper() override {
|
||||
return keyboard_mapper_;
|
||||
}
|
||||
|
||||
private:
|
||||
inline void update_display() {
|
||||
if(cycles_since_display_update_ > 0) {
|
||||
video_output_->run_for(cycles_since_display_update_.flush());
|
||||
}
|
||||
}
|
||||
|
||||
inline void queue_next_display_interrupt() {
|
||||
VideoOutput::Interrupt next_interrupt = video_output_->get_next_interrupt();
|
||||
cycles_until_display_interrupt_ = next_interrupt.cycles;
|
||||
next_display_interrupt_ = next_interrupt.interrupt;
|
||||
}
|
||||
|
||||
inline void update_audio() {
|
||||
if(cycles_since_audio_update_ > 0) {
|
||||
speaker_->run_for(cycles_since_audio_update_.divide(Cycles(Speaker::clock_rate_divider)));
|
||||
}
|
||||
}
|
||||
|
||||
inline void signal_interrupt(Interrupt interrupt) {
|
||||
interrupt_status_ |= interrupt;
|
||||
evaluate_interrupts();
|
||||
}
|
||||
|
||||
inline void clear_interrupt(Interrupt interrupt) {
|
||||
interrupt_status_ &= ~interrupt;
|
||||
evaluate_interrupts();
|
||||
}
|
||||
|
||||
inline void evaluate_interrupts() {
|
||||
if(interrupt_status_ & interrupt_control_) {
|
||||
interrupt_status_ |= 1;
|
||||
} else {
|
||||
interrupt_status_ &= ~1;
|
||||
}
|
||||
m6502_.set_irq_line(interrupt_status_ & 1);
|
||||
}
|
||||
|
||||
CPU::MOS6502::Processor<ConcreteMachine, false> m6502_;
|
||||
|
||||
// Things that directly constitute the memory map.
|
||||
uint8_t roms_[16][16384];
|
||||
bool rom_write_masks_[16] = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
|
||||
uint8_t os_[16384], ram_[32768];
|
||||
std::vector<uint8_t> dfs_, adfs_;
|
||||
|
||||
// Paging
|
||||
ROMSlot active_rom_ = ROMSlot::ROMSlot0;
|
||||
bool keyboard_is_active_ = false;
|
||||
bool basic_is_active_ = false;
|
||||
|
||||
// Interrupt and keyboard state
|
||||
uint8_t interrupt_status_ = Interrupt::PowerOnReset | Interrupt::TransmitDataEmpty | 0x80;
|
||||
uint8_t interrupt_control_ = 0;
|
||||
uint8_t key_states_[14];
|
||||
Electron::KeyboardMapper keyboard_mapper_;
|
||||
|
||||
// Counters related to simultaneous subsystems
|
||||
Cycles cycles_since_display_update_ = 0;
|
||||
Cycles cycles_since_audio_update_ = 0;
|
||||
int cycles_until_display_interrupt_ = 0;
|
||||
Interrupt next_display_interrupt_ = Interrupt::RealTimeClock;
|
||||
VideoOutput::Range video_access_range_ = {0, 0xffff};
|
||||
|
||||
// Tape
|
||||
Tape tape_;
|
||||
bool use_fast_tape_hack_ = false;
|
||||
bool fast_load_is_in_data_ = false;
|
||||
|
||||
// Disk
|
||||
std::unique_ptr<Plus3> plus3_;
|
||||
bool is_holding_shift_ = false;
|
||||
int shift_restart_counter_ = 0;
|
||||
|
||||
// Outputs
|
||||
std::unique_ptr<VideoOutput> video_output_;
|
||||
std::shared_ptr<Speaker> speaker_;
|
||||
bool speaker_is_enabled_ = false;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using namespace Electron;
|
||||
|
||||
#pragma mark - Lifecycle
|
||||
|
||||
Machine::Machine() :
|
||||
interrupt_control_(0),
|
||||
interrupt_status_(Interrupt::PowerOnReset | Interrupt::TransmitDataEmpty | 0x80),
|
||||
cycles_since_audio_update_(0),
|
||||
use_fast_tape_hack_(false),
|
||||
cycles_until_display_interrupt_(0) {
|
||||
memset(key_states_, 0, sizeof(key_states_));
|
||||
for(int c = 0; c < 16; c++)
|
||||
memset(roms_[c], 0xff, 16384);
|
||||
|
||||
tape_.set_delegate(this);
|
||||
set_clock_rate(2000000);
|
||||
Machine *Machine::Electron() {
|
||||
return new Electron::ConcreteMachine;
|
||||
}
|
||||
|
||||
#pragma mark - Output
|
||||
|
||||
void Machine::setup_output(float aspect_ratio) {
|
||||
video_output_.reset(new VideoOutput(ram_));
|
||||
|
||||
// The maximum output frequency is 62500Hz and all other permitted output frequencies are integral divisions of that;
|
||||
// however setting the speaker on or off can happen on any 2Mhz cycle, and probably (?) takes effect immediately. So
|
||||
// run the speaker at a 2000000Hz input rate, at least for the time being.
|
||||
speaker_.reset(new Speaker);
|
||||
speaker_->set_input_rate(2000000 / Speaker::clock_rate_divider);
|
||||
}
|
||||
|
||||
void Machine::close_output() {
|
||||
video_output_.reset();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> Machine::get_crt() {
|
||||
return video_output_->get_crt();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::Speaker> Machine::get_speaker() {
|
||||
return speaker_;
|
||||
}
|
||||
|
||||
#pragma mark - The keyboard
|
||||
|
||||
void Machine::clear_all_keys() {
|
||||
memset(key_states_, 0, sizeof(key_states_));
|
||||
if(is_holding_shift_) set_key_state(KeyShift, true);
|
||||
}
|
||||
|
||||
void Machine::set_key_state(uint16_t key, bool isPressed) {
|
||||
if(key == KeyBreak) {
|
||||
set_reset_line(isPressed);
|
||||
} else {
|
||||
if(isPressed)
|
||||
key_states_[key >> 4] |= key&0xf;
|
||||
else
|
||||
key_states_[key >> 4] &= ~(key&0xf);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Machine configuration
|
||||
|
||||
void Machine::configure_as_target(const StaticAnalyser::Target &target) {
|
||||
if(target.tapes.size()) {
|
||||
tape_.set_tape(target.tapes.front());
|
||||
}
|
||||
|
||||
if(target.disks.size()) {
|
||||
plus3_.reset(new Plus3);
|
||||
|
||||
if(target.acorn.has_dfs) {
|
||||
set_rom(ROMSlot0, dfs_, true);
|
||||
}
|
||||
if(target.acorn.has_adfs) {
|
||||
set_rom(ROMSlot4, adfs_, true);
|
||||
set_rom(ROMSlot5, std::vector<uint8_t>(adfs_.begin() + 16384, adfs_.end()), true);
|
||||
}
|
||||
|
||||
plus3_->set_disk(target.disks.front(), 0);
|
||||
}
|
||||
|
||||
ROMSlot slot = ROMSlot12;
|
||||
for(std::shared_ptr<Storage::Cartridge::Cartridge> cartridge : target.cartridges) {
|
||||
set_rom(slot, cartridge->get_segments().front().data, false);
|
||||
slot = (ROMSlot)(((int)slot + 1)&15);
|
||||
}
|
||||
|
||||
if(target.loadingCommand.length()) {
|
||||
set_typer_for_string(target.loadingCommand.c_str());
|
||||
}
|
||||
|
||||
if(target.acorn.should_shift_restart) {
|
||||
shift_restart_counter_ = 1000000;
|
||||
}
|
||||
}
|
||||
|
||||
void Machine::set_rom(ROMSlot slot, std::vector<uint8_t> data, bool is_writeable) {
|
||||
uint8_t *target = nullptr;
|
||||
switch(slot) {
|
||||
case ROMSlotDFS: dfs_ = data; return;
|
||||
case ROMSlotADFS: adfs_ = data; return;
|
||||
|
||||
case ROMSlotOS: target = os_; break;
|
||||
default:
|
||||
target = roms_[slot];
|
||||
rom_write_masks_[slot] = is_writeable;
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(target, &data[0], std::min((size_t)16384, data.size()));
|
||||
}
|
||||
|
||||
#pragma mark - The bus
|
||||
|
||||
Cycles Machine::perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
unsigned int cycles = 1;
|
||||
|
||||
if(address < 0x8000) {
|
||||
if(isReadOperation(operation)) {
|
||||
*value = ram_[address];
|
||||
} else {
|
||||
if(address >= video_access_range_.low_address && address <= video_access_range_.high_address) update_display();
|
||||
ram_[address] = *value;
|
||||
}
|
||||
|
||||
// for the entire frame, RAM is accessible only on odd cycles; in modes below 4
|
||||
// it's also accessible only outside of the pixel regions
|
||||
cycles += video_output_->get_cycles_until_next_ram_availability(cycles_since_display_update_.as_int() + 1);
|
||||
} else {
|
||||
switch(address & 0xff0f) {
|
||||
case 0xfe00:
|
||||
if(isReadOperation(operation)) {
|
||||
*value = interrupt_status_;
|
||||
interrupt_status_ &= ~PowerOnReset;
|
||||
} else {
|
||||
interrupt_control_ = (*value) & ~1;
|
||||
evaluate_interrupts();
|
||||
}
|
||||
break;
|
||||
case 0xfe07:
|
||||
if(!isReadOperation(operation)) {
|
||||
// update speaker mode
|
||||
bool new_speaker_is_enabled = (*value & 6) == 2;
|
||||
if(new_speaker_is_enabled != speaker_is_enabled_) {
|
||||
update_audio();
|
||||
speaker_->set_is_enabled(new_speaker_is_enabled);
|
||||
speaker_is_enabled_ = new_speaker_is_enabled;
|
||||
}
|
||||
|
||||
tape_.set_is_enabled((*value & 6) != 6);
|
||||
tape_.set_is_in_input_mode((*value & 6) == 0);
|
||||
tape_.set_is_running(((*value)&0x40) ? true : false);
|
||||
|
||||
// TODO: caps lock LED
|
||||
}
|
||||
|
||||
// deliberate fallthrough
|
||||
case 0xfe02: case 0xfe03:
|
||||
case 0xfe08: case 0xfe09: case 0xfe0a: case 0xfe0b:
|
||||
case 0xfe0c: case 0xfe0d: case 0xfe0e: case 0xfe0f:
|
||||
if(!isReadOperation(operation)) {
|
||||
update_display();
|
||||
video_output_->set_register(address, *value);
|
||||
video_access_range_ = video_output_->get_memory_access_range();
|
||||
queue_next_display_interrupt();
|
||||
}
|
||||
break;
|
||||
case 0xfe04:
|
||||
if(isReadOperation(operation)) {
|
||||
*value = tape_.get_data_register();
|
||||
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
|
||||
} else {
|
||||
tape_.set_data_register(*value);
|
||||
tape_.clear_interrupts(Interrupt::TransmitDataEmpty);
|
||||
}
|
||||
break;
|
||||
case 0xfe05:
|
||||
if(!isReadOperation(operation)) {
|
||||
const uint8_t interruptDisable = (*value)&0xf0;
|
||||
if( interruptDisable ) {
|
||||
if( interruptDisable&0x10 ) interrupt_status_ &= ~Interrupt::DisplayEnd;
|
||||
if( interruptDisable&0x20 ) interrupt_status_ &= ~Interrupt::RealTimeClock;
|
||||
if( interruptDisable&0x40 ) interrupt_status_ &= ~Interrupt::HighToneDetect;
|
||||
evaluate_interrupts();
|
||||
|
||||
// TODO: NMI
|
||||
}
|
||||
|
||||
// latch the paged ROM in case external hardware is being emulated
|
||||
active_rom_ = (Electron::ROMSlot)(*value & 0xf);
|
||||
|
||||
// apply the ULA's test
|
||||
if(*value & 0x08) {
|
||||
if(*value & 0x04) {
|
||||
keyboard_is_active_ = false;
|
||||
basic_is_active_ = false;
|
||||
} else {
|
||||
keyboard_is_active_ = !(*value & 0x02);
|
||||
basic_is_active_ = !keyboard_is_active_;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 0xfe06:
|
||||
if(!isReadOperation(operation)) {
|
||||
update_audio();
|
||||
speaker_->set_divider(*value);
|
||||
tape_.set_counter(*value);
|
||||
}
|
||||
break;
|
||||
|
||||
case 0xfc04: case 0xfc05: case 0xfc06: case 0xfc07:
|
||||
if(plus3_ && (address&0x00f0) == 0x00c0) {
|
||||
if(is_holding_shift_ && address == 0xfcc4) {
|
||||
is_holding_shift_ = false;
|
||||
set_key_state(KeyShift, false);
|
||||
}
|
||||
if(isReadOperation(operation))
|
||||
*value = plus3_->get_register(address);
|
||||
else
|
||||
plus3_->set_register(address, *value);
|
||||
}
|
||||
break;
|
||||
case 0xfc00:
|
||||
if(plus3_ && (address&0x00f0) == 0x00c0) {
|
||||
if(!isReadOperation(operation)) {
|
||||
plus3_->set_control_register(*value);
|
||||
} else *value = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if(address >= 0xc000) {
|
||||
if(isReadOperation(operation)) {
|
||||
if(
|
||||
use_fast_tape_hack_ &&
|
||||
tape_.has_tape() &&
|
||||
(operation == CPU::MOS6502::BusOperation::ReadOpcode) &&
|
||||
(
|
||||
(address == 0xf4e5) || (address == 0xf4e6) || // double NOPs at 0xf4e5, 0xf6de, 0xf6fa and 0xfa51
|
||||
(address == 0xf6de) || (address == 0xf6df) || // act to disable the normal branch into tape-handling
|
||||
(address == 0xf6fa) || (address == 0xf6fb) || // code, forcing the OS along the serially-accessed ROM
|
||||
(address == 0xfa51) || (address == 0xfa52) || // pathway.
|
||||
|
||||
(address == 0xf0a8) // 0xf0a8 is from where a service call would normally be
|
||||
// dispatched; we can check whether it would be call 14
|
||||
// (i.e. read byte) and, if so, whether the OS was about to
|
||||
// issue a read byte call to a ROM despite being the tape
|
||||
// FS being selected. If so then this is a get byte that
|
||||
// we should service synthetically. Put the byte into Y
|
||||
// and set A to zero to report that action was taken, then
|
||||
// allow the PC read to return an RTS.
|
||||
)
|
||||
) {
|
||||
uint8_t service_call = (uint8_t)get_value_of_register(CPU::MOS6502::Register::X);
|
||||
if(address == 0xf0a8) {
|
||||
if(!ram_[0x247] && service_call == 14) {
|
||||
tape_.set_delegate(nullptr);
|
||||
|
||||
// TODO: handle tape wrap around.
|
||||
|
||||
int cycles_left_while_plausibly_in_data = 50;
|
||||
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
|
||||
while(!tape_.get_tape()->is_at_end()) {
|
||||
tape_.run_for_input_pulse();
|
||||
cycles_left_while_plausibly_in_data--;
|
||||
if(!cycles_left_while_plausibly_in_data) fast_load_is_in_data_ = false;
|
||||
if( (tape_.get_interrupt_status() & Interrupt::ReceiveDataFull) &&
|
||||
(fast_load_is_in_data_ || tape_.get_data_register() == 0x2a)
|
||||
) break;
|
||||
}
|
||||
tape_.set_delegate(this);
|
||||
tape_.clear_interrupts(Interrupt::ReceiveDataFull);
|
||||
interrupt_status_ |= tape_.get_interrupt_status();
|
||||
|
||||
fast_load_is_in_data_ = true;
|
||||
set_value_of_register(CPU::MOS6502::Register::A, 0);
|
||||
set_value_of_register(CPU::MOS6502::Register::Y, tape_.get_data_register());
|
||||
*value = 0x60; // 0x60 is RTS
|
||||
}
|
||||
else *value = os_[address & 16383];
|
||||
}
|
||||
else *value = 0xea;
|
||||
} else {
|
||||
*value = os_[address & 16383];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(isReadOperation(operation)) {
|
||||
*value = roms_[active_rom_][address & 16383];
|
||||
if(keyboard_is_active_) {
|
||||
*value &= 0xf0;
|
||||
for(int address_line = 0; address_line < 14; address_line++) {
|
||||
if(!(address&(1 << address_line))) *value |= key_states_[address_line];
|
||||
}
|
||||
}
|
||||
if(basic_is_active_) {
|
||||
*value &= roms_[ROMSlotBASIC][address & 16383];
|
||||
}
|
||||
} else if(rom_write_masks_[active_rom_]) {
|
||||
roms_[active_rom_][address & 16383] = *value;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cycles_since_display_update_ += Cycles((int)cycles);
|
||||
cycles_since_audio_update_ += Cycles((int)cycles);
|
||||
if(cycles_since_audio_update_ > Cycles(16384)) update_audio();
|
||||
tape_.run_for(Cycles((int)cycles));
|
||||
|
||||
cycles_until_display_interrupt_ -= cycles;
|
||||
if(cycles_until_display_interrupt_ < 0) {
|
||||
signal_interrupt(next_display_interrupt_);
|
||||
update_display();
|
||||
queue_next_display_interrupt();
|
||||
}
|
||||
|
||||
if(typer_) typer_->run_for(Cycles((int)cycles));
|
||||
if(plus3_) plus3_->run_for(Cycles(4*(int)cycles));
|
||||
if(shift_restart_counter_) {
|
||||
shift_restart_counter_ -= cycles;
|
||||
if(shift_restart_counter_ <= 0) {
|
||||
shift_restart_counter_ = 0;
|
||||
set_power_on(true);
|
||||
set_key_state(KeyShift, true);
|
||||
is_holding_shift_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
return Cycles((int)cycles);
|
||||
}
|
||||
|
||||
void Machine::flush() {
|
||||
update_display();
|
||||
update_audio();
|
||||
speaker_->flush();
|
||||
}
|
||||
|
||||
#pragma mark - Deferred scheduling
|
||||
|
||||
inline void Machine::update_display() {
|
||||
if(cycles_since_display_update_ > 0) {
|
||||
video_output_->run_for(cycles_since_display_update_.flush());
|
||||
}
|
||||
}
|
||||
|
||||
inline void Machine::queue_next_display_interrupt() {
|
||||
VideoOutput::Interrupt next_interrupt = video_output_->get_next_interrupt();
|
||||
cycles_until_display_interrupt_ = next_interrupt.cycles;
|
||||
next_display_interrupt_ = next_interrupt.interrupt;
|
||||
}
|
||||
|
||||
inline void Machine::update_audio() {
|
||||
if(cycles_since_audio_update_ > 0) {
|
||||
speaker_->run_for(cycles_since_audio_update_.divide(Cycles(Speaker::clock_rate_divider)));
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Interrupts
|
||||
|
||||
inline void Machine::signal_interrupt(Electron::Interrupt interrupt) {
|
||||
interrupt_status_ |= interrupt;
|
||||
evaluate_interrupts();
|
||||
}
|
||||
|
||||
inline void Machine::clear_interrupt(Electron::Interrupt interrupt) {
|
||||
interrupt_status_ &= ~interrupt;
|
||||
evaluate_interrupts();
|
||||
}
|
||||
|
||||
inline void Machine::evaluate_interrupts() {
|
||||
if(interrupt_status_ & interrupt_control_) {
|
||||
interrupt_status_ |= 1;
|
||||
} else {
|
||||
interrupt_status_ &= ~1;
|
||||
}
|
||||
set_irq_line(interrupt_status_ & 1);
|
||||
}
|
||||
|
||||
#pragma mark - Tape::Delegate
|
||||
|
||||
void Machine::tape_did_change_interrupt_status(Tape *tape) {
|
||||
interrupt_status_ = (interrupt_status_ & ~(Interrupt::TransmitDataEmpty | Interrupt::ReceiveDataFull | Interrupt::HighToneDetect)) | tape_.get_interrupt_status();
|
||||
evaluate_interrupts();
|
||||
}
|
||||
Machine::~Machine() {}
|
||||
|
||||
@@ -9,19 +9,9 @@
|
||||
#ifndef Electron_hpp
|
||||
#define Electron_hpp
|
||||
|
||||
#include "../../Processors/6502/6502.hpp"
|
||||
#include "../../Storage/Tape/Tape.hpp"
|
||||
#include "../../ClockReceiver/ClockReceiver.hpp"
|
||||
|
||||
#include "../ConfigurationTarget.hpp"
|
||||
#include "../CRTMachine.hpp"
|
||||
#include "../Typer.hpp"
|
||||
|
||||
#include "Interrupts.hpp"
|
||||
#include "Plus3.hpp"
|
||||
#include "Speaker.hpp"
|
||||
#include "Tape.hpp"
|
||||
#include "Video.hpp"
|
||||
#include "../KeyboardMachine.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
@@ -41,25 +31,6 @@ enum ROMSlot: uint8_t {
|
||||
ROMSlotOS, ROMSlotDFS, ROMSlotADFS
|
||||
};
|
||||
|
||||
enum Key: uint16_t {
|
||||
KeySpace = 0x0000 | 0x08, KeyCopy = 0x0000 | 0x02, KeyRight = 0x0000 | 0x01,
|
||||
KeyDelete = 0x0010 | 0x08, KeyReturn = 0x0010 | 0x04, KeyDown = 0x0010 | 0x02, KeyLeft = 0x0010 | 0x01,
|
||||
KeyColon = 0x0020 | 0x04, KeyUp = 0x0020 | 0x02, KeyMinus = 0x0020 | 0x01,
|
||||
KeySlash = 0x0030 | 0x08, KeySemiColon = 0x0030 | 0x04, KeyP = 0x0030 | 0x02, Key0 = 0x0030 | 0x01,
|
||||
KeyFullStop = 0x0040 | 0x08, KeyL = 0x0040 | 0x04, KeyO = 0x0040 | 0x02, Key9 = 0x0040 | 0x01,
|
||||
KeyComma = 0x0050 | 0x08, KeyK = 0x0050 | 0x04, KeyI = 0x0050 | 0x02, Key8 = 0x0050 | 0x01,
|
||||
KeyM = 0x0060 | 0x08, KeyJ = 0x0060 | 0x04, KeyU = 0x0060 | 0x02, Key7 = 0x0060 | 0x01,
|
||||
KeyN = 0x0070 | 0x08, KeyH = 0x0070 | 0x04, KeyY = 0x0070 | 0x02, Key6 = 0x0070 | 0x01,
|
||||
KeyB = 0x0080 | 0x08, KeyG = 0x0080 | 0x04, KeyT = 0x0080 | 0x02, Key5 = 0x0080 | 0x01,
|
||||
KeyV = 0x0090 | 0x08, KeyF = 0x0090 | 0x04, KeyR = 0x0090 | 0x02, Key4 = 0x0090 | 0x01,
|
||||
KeyC = 0x00a0 | 0x08, KeyD = 0x00a0 | 0x04, KeyE = 0x00a0 | 0x02, Key3 = 0x00a0 | 0x01,
|
||||
KeyX = 0x00b0 | 0x08, KeyS = 0x00b0 | 0x04, KeyW = 0x00b0 | 0x02, Key2 = 0x00b0 | 0x01,
|
||||
KeyZ = 0x00c0 | 0x08, KeyA = 0x00c0 | 0x04, KeyQ = 0x00c0 | 0x02, Key1 = 0x00c0 | 0x01,
|
||||
KeyShift = 0x00d0 | 0x08, KeyControl = 0x00d0 | 0x04, KeyFunc = 0x00d0 | 0x02, KeyEscape = 0x00d0 | 0x01,
|
||||
|
||||
KeyBreak = 0xfffd,
|
||||
};
|
||||
|
||||
/*!
|
||||
@abstract Represents an Acorn Electron.
|
||||
|
||||
@@ -67,88 +38,23 @@ enum Key: uint16_t {
|
||||
Acorn Electron.
|
||||
*/
|
||||
class Machine:
|
||||
public CPU::MOS6502::Processor<Machine>,
|
||||
public CRTMachine::Machine,
|
||||
public Tape::Delegate,
|
||||
public Utility::TypeRecipient,
|
||||
public ConfigurationTarget::Machine {
|
||||
|
||||
public ConfigurationTarget::Machine,
|
||||
public KeyboardMachine::Machine {
|
||||
public:
|
||||
Machine();
|
||||
virtual ~Machine();
|
||||
|
||||
void set_rom(ROMSlot slot, std::vector<uint8_t> data, bool is_writeable);
|
||||
/// Creates and returns an Electron.
|
||||
static Machine *Electron();
|
||||
|
||||
void set_key_state(uint16_t key, bool isPressed);
|
||||
void clear_all_keys();
|
||||
/*!
|
||||
Sets the contents of @c slot to @c data. If @c is_writeable is @c true then writing to the slot
|
||||
is enabled — it acts as if it were sideways RAM. Otherwise the slot is modelled as containing ROM.
|
||||
*/
|
||||
virtual void set_rom(ROMSlot slot, std::vector<uint8_t> data, bool is_writeable) = 0;
|
||||
|
||||
inline void set_use_fast_tape_hack(bool activate) { use_fast_tape_hack_ = activate; }
|
||||
|
||||
// to satisfy ConfigurationTarget::Machine
|
||||
void configure_as_target(const StaticAnalyser::Target &target);
|
||||
|
||||
// to satisfy CPU::MOS6502::Processor
|
||||
Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value);
|
||||
void flush();
|
||||
|
||||
// to satisfy CRTMachine::Machine
|
||||
virtual void setup_output(float aspect_ratio);
|
||||
virtual void close_output();
|
||||
virtual std::shared_ptr<Outputs::CRT::CRT> get_crt();
|
||||
virtual std::shared_ptr<Outputs::Speaker> get_speaker();
|
||||
virtual void run_for(const Cycles cycles) { CPU::MOS6502::Processor<Machine>::run_for(cycles); }
|
||||
|
||||
// to satisfy Tape::Delegate
|
||||
virtual void tape_did_change_interrupt_status(Tape *tape);
|
||||
|
||||
// for Utility::TypeRecipient
|
||||
virtual HalfCycles get_typer_delay();
|
||||
virtual HalfCycles get_typer_frequency();
|
||||
uint16_t *sequence_for_character(Utility::Typer *typer, char character);
|
||||
|
||||
private:
|
||||
inline void update_display();
|
||||
inline void queue_next_display_interrupt();
|
||||
inline void update_audio();
|
||||
|
||||
inline void signal_interrupt(Interrupt interrupt);
|
||||
inline void clear_interrupt(Interrupt interrupt);
|
||||
inline void evaluate_interrupts();
|
||||
|
||||
// Things that directly constitute the memory map.
|
||||
uint8_t roms_[16][16384];
|
||||
bool rom_write_masks_[16];
|
||||
uint8_t os_[16384], ram_[32768];
|
||||
std::vector<uint8_t> dfs_, adfs_;
|
||||
|
||||
// Paging
|
||||
ROMSlot active_rom_;
|
||||
bool keyboard_is_active_, basic_is_active_;
|
||||
|
||||
// Interrupt and keyboard state
|
||||
uint8_t interrupt_status_, interrupt_control_;
|
||||
uint8_t key_states_[14];
|
||||
|
||||
// Counters related to simultaneous subsystems
|
||||
Cycles cycles_since_display_update_;
|
||||
Cycles cycles_since_audio_update_;
|
||||
int cycles_until_display_interrupt_;
|
||||
Interrupt next_display_interrupt_;
|
||||
VideoOutput::Range video_access_range_;
|
||||
|
||||
// Tape
|
||||
Tape tape_;
|
||||
bool use_fast_tape_hack_;
|
||||
bool fast_load_is_in_data_;
|
||||
|
||||
// Disk
|
||||
std::unique_ptr<Plus3> plus3_;
|
||||
bool is_holding_shift_;
|
||||
int shift_restart_counter_;
|
||||
|
||||
// Outputs
|
||||
std::unique_ptr<VideoOutput> video_output_;
|
||||
std::shared_ptr<Speaker> speaker_;
|
||||
bool speaker_is_enabled_;
|
||||
/// Enables or disables turbo-speed tape loading.
|
||||
virtual void set_use_fast_tape_hack(bool activate) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,26 +1,65 @@
|
||||
//
|
||||
// Typer.cpp
|
||||
// Keyboard.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 05/11/2016.
|
||||
// Copyright © 2016 Thomas Harte. All rights reserved.
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "Electron.hpp"
|
||||
#include "Keyboard.hpp"
|
||||
|
||||
HalfCycles Electron::Machine::get_typer_delay() {
|
||||
return get_is_resetting() ? Cycles(625*25*128) : Cycles(0); // wait one second if resetting
|
||||
using namespace Electron;
|
||||
|
||||
uint16_t KeyboardMapper::mapped_key_for_key(Inputs::Keyboard::Key key) {
|
||||
#define BIND(source, dest) case Inputs::Keyboard::Key::source: return Electron::Key::dest
|
||||
switch(key) {
|
||||
default: return KeyCopy;
|
||||
|
||||
BIND(k0, Key0); BIND(k1, Key1); BIND(k2, Key2); BIND(k3, Key3); BIND(k4, Key4);
|
||||
BIND(k5, Key5); BIND(k6, Key6); BIND(k7, Key7); BIND(k8, Key8); BIND(k9, Key9);
|
||||
BIND(Q, KeyQ); BIND(W, KeyW); BIND(E, KeyE); BIND(R, KeyR); BIND(T, KeyT);
|
||||
BIND(Y, KeyY); BIND(U, KeyU); BIND(I, KeyI); BIND(O, KeyO); BIND(P, KeyP);
|
||||
BIND(A, KeyA); BIND(S, KeyS); BIND(D, KeyD); BIND(F, KeyF); BIND(G, KeyG);
|
||||
BIND(H, KeyH); BIND(J, KeyJ); BIND(K, KeyK); BIND(L, KeyL);
|
||||
BIND(Z, KeyZ); BIND(X, KeyX); BIND(C, KeyC); BIND(V, KeyV);
|
||||
BIND(B, KeyB); BIND(N, KeyN); BIND(M, KeyM);
|
||||
|
||||
BIND(Comma, KeyComma);
|
||||
BIND(FullStop, KeyFullStop);
|
||||
BIND(ForwardSlash, KeySlash);
|
||||
BIND(Semicolon, KeySemiColon);
|
||||
BIND(Quote, KeyColon);
|
||||
|
||||
BIND(Escape, KeyEscape);
|
||||
BIND(Equals, KeyBreak);
|
||||
BIND(F12, KeyBreak);
|
||||
|
||||
BIND(Left, KeyLeft); BIND(Right, KeyRight); BIND(Up, KeyUp); BIND(Down, KeyDown);
|
||||
|
||||
BIND(Tab, KeyFunc); BIND(LeftOption, KeyFunc); BIND(RightOption, KeyFunc);
|
||||
BIND(LeftMeta, KeyFunc); BIND(RightMeta, KeyFunc);
|
||||
BIND(CapsLock, KeyControl); BIND(LeftControl, KeyControl); BIND(RightControl, KeyControl);
|
||||
BIND(LeftShift, KeyShift); BIND(RightShift, KeyShift);
|
||||
|
||||
BIND(Hyphen, KeyMinus);
|
||||
BIND(Delete, KeyDelete);
|
||||
BIND(Enter, KeyReturn); BIND(KeyPadEnter, KeyReturn);
|
||||
|
||||
BIND(KeyPad0, Key0); BIND(KeyPad1, Key1); BIND(KeyPad2, Key2); BIND(KeyPad3, Key3); BIND(KeyPad4, Key4);
|
||||
BIND(KeyPad5, Key5); BIND(KeyPad6, Key6); BIND(KeyPad7, Key7); BIND(KeyPad8, Key8); BIND(KeyPad9, Key9);
|
||||
|
||||
BIND(KeyPadMinus, KeyMinus); BIND(KeyPadPlus, KeyColon);
|
||||
|
||||
BIND(Space, KeySpace);
|
||||
}
|
||||
#undef BIND
|
||||
}
|
||||
|
||||
HalfCycles Electron::Machine::get_typer_frequency() {
|
||||
return Cycles(625*128*2); // accept a new character every two frames
|
||||
}
|
||||
|
||||
uint16_t *Electron::Machine::sequence_for_character(Utility::Typer *typer, char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, EndSequence}
|
||||
#define SHIFT(...) {KeyShift, __VA_ARGS__, EndSequence}
|
||||
#define CTRL(...) {KeyControl, __VA_ARGS__, EndSequence}
|
||||
#define X {NotMapped}
|
||||
uint16_t *CharacterMapper::sequence_for_character(char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define SHIFT(...) {KeyShift, __VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define CTRL(...) {KeyControl, __VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define X {KeyboardMachine::Machine::KeyNotMapped}
|
||||
static KeySequence key_sequences[] = {
|
||||
/* NUL */ X, /* SOH */ X,
|
||||
/* STX */ X, /* ETX */ X,
|
||||
46
Machines/Electron/Keyboard.hpp
Normal file
46
Machines/Electron/Keyboard.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Keyboard.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Machines_Electron_Keyboard_hpp
|
||||
#define Machines_Electron_Keyboard_hpp
|
||||
|
||||
#include "../KeyboardMachine.hpp"
|
||||
#include "../Utility/Typer.hpp"
|
||||
|
||||
namespace Electron {
|
||||
|
||||
enum Key: uint16_t {
|
||||
KeySpace = 0x0000 | 0x08, KeyCopy = 0x0000 | 0x02, KeyRight = 0x0000 | 0x01,
|
||||
KeyDelete = 0x0010 | 0x08, KeyReturn = 0x0010 | 0x04, KeyDown = 0x0010 | 0x02, KeyLeft = 0x0010 | 0x01,
|
||||
KeyColon = 0x0020 | 0x04, KeyUp = 0x0020 | 0x02, KeyMinus = 0x0020 | 0x01,
|
||||
KeySlash = 0x0030 | 0x08, KeySemiColon = 0x0030 | 0x04, KeyP = 0x0030 | 0x02, Key0 = 0x0030 | 0x01,
|
||||
KeyFullStop = 0x0040 | 0x08, KeyL = 0x0040 | 0x04, KeyO = 0x0040 | 0x02, Key9 = 0x0040 | 0x01,
|
||||
KeyComma = 0x0050 | 0x08, KeyK = 0x0050 | 0x04, KeyI = 0x0050 | 0x02, Key8 = 0x0050 | 0x01,
|
||||
KeyM = 0x0060 | 0x08, KeyJ = 0x0060 | 0x04, KeyU = 0x0060 | 0x02, Key7 = 0x0060 | 0x01,
|
||||
KeyN = 0x0070 | 0x08, KeyH = 0x0070 | 0x04, KeyY = 0x0070 | 0x02, Key6 = 0x0070 | 0x01,
|
||||
KeyB = 0x0080 | 0x08, KeyG = 0x0080 | 0x04, KeyT = 0x0080 | 0x02, Key5 = 0x0080 | 0x01,
|
||||
KeyV = 0x0090 | 0x08, KeyF = 0x0090 | 0x04, KeyR = 0x0090 | 0x02, Key4 = 0x0090 | 0x01,
|
||||
KeyC = 0x00a0 | 0x08, KeyD = 0x00a0 | 0x04, KeyE = 0x00a0 | 0x02, Key3 = 0x00a0 | 0x01,
|
||||
KeyX = 0x00b0 | 0x08, KeyS = 0x00b0 | 0x04, KeyW = 0x00b0 | 0x02, Key2 = 0x00b0 | 0x01,
|
||||
KeyZ = 0x00c0 | 0x08, KeyA = 0x00c0 | 0x04, KeyQ = 0x00c0 | 0x02, Key1 = 0x00c0 | 0x01,
|
||||
KeyShift = 0x00d0 | 0x08, KeyControl = 0x00d0 | 0x04, KeyFunc = 0x00d0 | 0x02, KeyEscape = 0x00d0 | 0x01,
|
||||
|
||||
KeyBreak = 0xfffd,
|
||||
};
|
||||
|
||||
struct KeyboardMapper: public KeyboardMachine::Machine::KeyboardMapper {
|
||||
uint16_t mapped_key_for_key(Inputs::Keyboard::Key key);
|
||||
};
|
||||
|
||||
struct CharacterMapper: public ::Utility::CharacterMapper {
|
||||
uint16_t *sequence_for_character(char character);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif /* KeyboardMapper_hpp */
|
||||
@@ -10,13 +10,13 @@
|
||||
|
||||
using namespace Electron;
|
||||
|
||||
Plus3::Plus3() : WD1770(P1770), last_control_(0) {
|
||||
Plus3::Plus3() : WD1770(P1770) {
|
||||
set_control_register(last_control_, 0xff);
|
||||
}
|
||||
|
||||
void Plus3::set_disk(std::shared_ptr<Storage::Disk::Disk> disk, int drive) {
|
||||
if(!drives_[drive]) {
|
||||
drives_[drive].reset(new Storage::Disk::Drive);
|
||||
drives_[drive].reset(new Storage::Disk::Drive(8000000, 300, 2));
|
||||
if(drive == selected_drive_) set_drive(drives_[drive]);
|
||||
}
|
||||
drives_[drive]->set_disk(disk);
|
||||
@@ -42,9 +42,14 @@ void Plus3::set_control_register(uint8_t control, uint8_t changes) {
|
||||
}
|
||||
}
|
||||
if(changes & 0x04) {
|
||||
invalidate_track();
|
||||
if(drives_[0]) drives_[0]->set_head((control & 0x04) ? 1 : 0);
|
||||
if(drives_[1]) drives_[1]->set_head((control & 0x04) ? 1 : 0);
|
||||
}
|
||||
if(changes & 0x08) set_is_double_density(!(control & 0x08));
|
||||
}
|
||||
|
||||
void Plus3::set_motor_on(bool on) {
|
||||
// TODO: this status should transfer if the selected drive changes. But the same goes for
|
||||
// writing state, so plenty of work to do in general here.
|
||||
get_drive().set_motor_on(on);
|
||||
}
|
||||
|
||||
@@ -23,8 +23,10 @@ class Plus3 : public WD::WD1770 {
|
||||
private:
|
||||
void set_control_register(uint8_t control, uint8_t changes);
|
||||
std::shared_ptr<Storage::Disk::Drive> drives_[2];
|
||||
int selected_drive_;
|
||||
uint8_t last_control_;
|
||||
int selected_drive_ = 0;
|
||||
uint8_t last_control_ = 0;
|
||||
|
||||
void set_motor_on(bool on);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ using namespace Electron;
|
||||
void Speaker::get_samples(unsigned int number_of_samples, int16_t *target) {
|
||||
if(is_enabled_) {
|
||||
while(number_of_samples--) {
|
||||
*target = (int16_t)((counter_ / (divider_+1)) * 8192);
|
||||
*target = static_cast<int16_t>((counter_ / (divider_+1)) * 8192);
|
||||
target++;
|
||||
counter_ = (counter_ + 1) % ((divider_+1) * 2);
|
||||
}
|
||||
|
||||
@@ -25,9 +25,9 @@ class Speaker: public ::Outputs::Filter<Speaker> {
|
||||
static const unsigned int clock_rate_divider = 8;
|
||||
|
||||
private:
|
||||
unsigned int counter_;
|
||||
unsigned int divider_;
|
||||
bool is_enabled_;
|
||||
unsigned int counter_ = 0;
|
||||
unsigned int divider_ = 0;
|
||||
bool is_enabled_ = false;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -10,19 +10,12 @@
|
||||
|
||||
using namespace Electron;
|
||||
|
||||
Tape::Tape() :
|
||||
TapePlayer(2000000),
|
||||
is_running_(false),
|
||||
data_register_(0),
|
||||
delegate_(nullptr),
|
||||
output_({.bits_remaining_until_empty = 0, .cycles_into_pulse = 0}),
|
||||
last_posted_interrupt_status_(0),
|
||||
interrupt_status_(0) {
|
||||
Tape::Tape() : TapePlayer(2000000) {
|
||||
shifter_.set_delegate(this);
|
||||
}
|
||||
|
||||
void Tape::push_tape_bit(uint16_t bit) {
|
||||
data_register_ = (uint16_t)((data_register_ >> 1) | (bit << 10));
|
||||
data_register_ = static_cast<uint16_t>((data_register_ >> 1) | (bit << 10));
|
||||
|
||||
if(input_.minimum_bits_until_full) input_.minimum_bits_until_full--;
|
||||
if(input_.minimum_bits_until_full == 8) interrupt_status_ &= ~Interrupt::ReceiveDataFull;
|
||||
@@ -64,12 +57,12 @@ void Tape::set_counter(uint8_t value) {
|
||||
}
|
||||
|
||||
void Tape::set_data_register(uint8_t value) {
|
||||
data_register_ = (uint16_t)((value << 2) | 1);
|
||||
data_register_ = static_cast<uint16_t>((value << 2) | 1);
|
||||
output_.bits_remaining_until_empty = 9;
|
||||
}
|
||||
|
||||
uint8_t Tape::get_data_register() {
|
||||
return (uint8_t)(data_register_ >> 2);
|
||||
return static_cast<uint8_t>(data_register_ >> 2);
|
||||
}
|
||||
|
||||
void Tape::process_input_pulse(const Storage::Tape::Tape::Pulse &pulse) {
|
||||
@@ -77,7 +70,7 @@ void Tape::process_input_pulse(const Storage::Tape::Tape::Pulse &pulse) {
|
||||
}
|
||||
|
||||
void Tape::acorn_shifter_output_bit(int value) {
|
||||
push_tape_bit((uint16_t)value);
|
||||
push_tape_bit(static_cast<uint16_t>(value));
|
||||
}
|
||||
|
||||
void Tape::run_for(const Cycles cycles) {
|
||||
@@ -87,7 +80,7 @@ void Tape::run_for(const Cycles cycles) {
|
||||
TapePlayer::run_for(cycles);
|
||||
}
|
||||
} else {
|
||||
output_.cycles_into_pulse += (unsigned int)cycles.as_int();
|
||||
output_.cycles_into_pulse += static_cast<unsigned int>(cycles.as_int());
|
||||
while(output_.cycles_into_pulse > 1664) { // 1664 = the closest you can get to 1200 baud if you're looking for something
|
||||
output_.cycles_into_pulse -= 1664; // that divides the 125,000Hz clock that the sound divider runs off.
|
||||
push_tape_bit(1);
|
||||
|
||||
@@ -53,21 +53,22 @@ class Tape:
|
||||
|
||||
struct {
|
||||
int minimum_bits_until_full;
|
||||
} input_;
|
||||
} input_ = {0};
|
||||
struct {
|
||||
unsigned int cycles_into_pulse;
|
||||
unsigned int bits_remaining_until_empty;
|
||||
} output_;
|
||||
} output_ = {.bits_remaining_until_empty = 0, .cycles_into_pulse = 0};
|
||||
|
||||
bool is_running_;
|
||||
bool is_enabled_;
|
||||
bool is_in_input_mode_;
|
||||
bool is_running_ = false;
|
||||
bool is_enabled_ = false;
|
||||
bool is_in_input_mode_ = false;
|
||||
|
||||
inline void evaluate_interrupts();
|
||||
uint16_t data_register_;
|
||||
uint16_t data_register_ = 0;
|
||||
|
||||
uint8_t interrupt_status_, last_posted_interrupt_status_;
|
||||
Delegate *delegate_;
|
||||
uint8_t interrupt_status_ = 0;
|
||||
uint8_t last_posted_interrupt_status_ = 0;
|
||||
Delegate *delegate_ = nullptr;
|
||||
|
||||
::Storage::Tape::Acorn::Shifter shifter_;
|
||||
};
|
||||
|
||||
@@ -32,17 +32,18 @@ namespace {
|
||||
static const int real_time_clock_interrupt_2 = 56704;
|
||||
static const int display_end_interrupt_1 = (first_graphics_line + display_end_interrupt_line)*cycles_per_line;
|
||||
static const int display_end_interrupt_2 = (first_graphics_line + field_divider_line + display_end_interrupt_line)*cycles_per_line;
|
||||
|
||||
struct FourBPPBookender: public Outputs::CRT::TextureBuilder::Bookender {
|
||||
void add_bookends(uint8_t *const left_value, uint8_t *const right_value, uint8_t *left_bookend, uint8_t *right_bookend) {
|
||||
*left_bookend = static_cast<uint8_t>(((*left_value) & 0x0f) | (((*left_value) & 0x0f) << 4));
|
||||
*right_bookend = static_cast<uint8_t>(((*right_value) & 0xf0) | (((*right_value) & 0xf0) >> 4));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#pragma mark - Lifecycle
|
||||
|
||||
VideoOutput::VideoOutput(uint8_t *memory) :
|
||||
ram_(memory),
|
||||
current_pixel_line_(-1),
|
||||
output_position_(0),
|
||||
screen_mode_(6),
|
||||
screen_map_pointer_(0),
|
||||
cycles_into_draw_action_(0) {
|
||||
VideoOutput::VideoOutput(uint8_t *memory) : ram_(memory) {
|
||||
memset(palette_, 0xf, sizeof(palette_));
|
||||
setup_screen_map();
|
||||
setup_base_address();
|
||||
@@ -55,6 +56,8 @@ VideoOutput::VideoOutput(uint8_t *memory) :
|
||||
"texValue >>= 4 - (int(icoordinate.x * 8) & 4);"
|
||||
"return vec3( uvec3(texValue) & uvec3(4u, 2u, 1u));"
|
||||
"}");
|
||||
std::unique_ptr<Outputs::CRT::TextureBuilder::Bookender> bookender(new FourBPPBookender);
|
||||
crt_->set_bookender(std::move(bookender));
|
||||
// TODO: as implied below, I've introduced a clock's latency into the graphics pipeline somehow. Investigate.
|
||||
crt_->set_visible_area(crt_->get_rect_for_area(first_graphics_line - 3, 256, (first_graphics_cycle+1) * crt_cycles_multiplier, 80 * crt_cycles_multiplier, 4.0f / 3.0f));
|
||||
}
|
||||
@@ -92,7 +95,7 @@ void VideoOutput::start_pixel_line() {
|
||||
}
|
||||
|
||||
void VideoOutput::end_pixel_line() {
|
||||
if(current_output_target_) crt_->output_data((unsigned int)((current_output_target_ - initial_output_target_) * current_output_divider_), current_output_divider_);
|
||||
if(current_output_target_) crt_->output_data(static_cast<unsigned int>((current_output_target_ - initial_output_target_) * current_output_divider_), current_output_divider_);
|
||||
current_character_row_++;
|
||||
}
|
||||
|
||||
@@ -110,9 +113,9 @@ void VideoOutput::output_pixels(unsigned int number_of_cycles) {
|
||||
}
|
||||
|
||||
if(!initial_output_target_ || divider != current_output_divider_) {
|
||||
if(current_output_target_) crt_->output_data((unsigned int)((current_output_target_ - initial_output_target_) * current_output_divider_), current_output_divider_);
|
||||
if(current_output_target_) crt_->output_data(static_cast<unsigned int>((current_output_target_ - initial_output_target_) * current_output_divider_), current_output_divider_);
|
||||
current_output_divider_ = divider;
|
||||
initial_output_target_ = current_output_target_ = crt_->allocate_write_area(640 / current_output_divider_);
|
||||
initial_output_target_ = current_output_target_ = crt_->allocate_write_area(640 / current_output_divider_, 4);
|
||||
}
|
||||
|
||||
#define get_pixel() \
|
||||
@@ -127,7 +130,7 @@ void VideoOutput::output_pixels(unsigned int number_of_cycles) {
|
||||
if(initial_output_target_) {
|
||||
while(number_of_cycles--) {
|
||||
get_pixel();
|
||||
*(uint32_t *)current_output_target_ = palette_tables_.eighty1bpp[last_pixel_byte_];
|
||||
*reinterpret_cast<uint32_t *>(current_output_target_) = palette_tables_.eighty1bpp[last_pixel_byte_];
|
||||
current_output_target_ += 4;
|
||||
current_pixel_column_++;
|
||||
}
|
||||
@@ -138,7 +141,7 @@ void VideoOutput::output_pixels(unsigned int number_of_cycles) {
|
||||
if(initial_output_target_) {
|
||||
while(number_of_cycles--) {
|
||||
get_pixel();
|
||||
*(uint16_t *)current_output_target_ = palette_tables_.eighty2bpp[last_pixel_byte_];
|
||||
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.eighty2bpp[last_pixel_byte_];
|
||||
current_output_target_ += 2;
|
||||
current_pixel_column_++;
|
||||
}
|
||||
@@ -160,7 +163,7 @@ void VideoOutput::output_pixels(unsigned int number_of_cycles) {
|
||||
if(initial_output_target_) {
|
||||
if(current_pixel_column_&1) {
|
||||
last_pixel_byte_ <<= 4;
|
||||
*(uint16_t *)current_output_target_ = palette_tables_.forty1bpp[last_pixel_byte_];
|
||||
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
||||
current_output_target_ += 2;
|
||||
|
||||
number_of_cycles--;
|
||||
@@ -168,11 +171,11 @@ void VideoOutput::output_pixels(unsigned int number_of_cycles) {
|
||||
}
|
||||
while(number_of_cycles > 1) {
|
||||
get_pixel();
|
||||
*(uint16_t *)current_output_target_ = palette_tables_.forty1bpp[last_pixel_byte_];
|
||||
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
||||
current_output_target_ += 2;
|
||||
|
||||
last_pixel_byte_ <<= 4;
|
||||
*(uint16_t *)current_output_target_ = palette_tables_.forty1bpp[last_pixel_byte_];
|
||||
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
||||
current_output_target_ += 2;
|
||||
|
||||
number_of_cycles -= 2;
|
||||
@@ -180,7 +183,7 @@ void VideoOutput::output_pixels(unsigned int number_of_cycles) {
|
||||
}
|
||||
if(number_of_cycles) {
|
||||
get_pixel();
|
||||
*(uint16_t *)current_output_target_ = palette_tables_.forty1bpp[last_pixel_byte_];
|
||||
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
||||
current_output_target_ += 2;
|
||||
current_pixel_column_++;
|
||||
}
|
||||
@@ -229,16 +232,16 @@ void VideoOutput::run_for(const Cycles cycles) {
|
||||
while(number_of_cycles) {
|
||||
int draw_action_length = screen_map_[screen_map_pointer_].length;
|
||||
int time_left_in_action = std::min(number_of_cycles, draw_action_length - cycles_into_draw_action_);
|
||||
if(screen_map_[screen_map_pointer_].type == DrawAction::Pixels) output_pixels((unsigned int)time_left_in_action);
|
||||
if(screen_map_[screen_map_pointer_].type == DrawAction::Pixels) output_pixels(static_cast<unsigned int>(time_left_in_action));
|
||||
|
||||
number_of_cycles -= time_left_in_action;
|
||||
cycles_into_draw_action_ += time_left_in_action;
|
||||
if(cycles_into_draw_action_ == draw_action_length) {
|
||||
switch(screen_map_[screen_map_pointer_].type) {
|
||||
case DrawAction::Sync: crt_->output_sync((unsigned int)(draw_action_length * crt_cycles_multiplier)); break;
|
||||
case DrawAction::ColourBurst: crt_->output_default_colour_burst((unsigned int)(draw_action_length * crt_cycles_multiplier)); break;
|
||||
case DrawAction::Blank: crt_->output_blank((unsigned int)(draw_action_length * crt_cycles_multiplier)); break;
|
||||
case DrawAction::Pixels: end_pixel_line(); break;
|
||||
case DrawAction::Sync: crt_->output_sync(static_cast<unsigned int>(draw_action_length * crt_cycles_multiplier)); break;
|
||||
case DrawAction::ColourBurst: crt_->output_default_colour_burst(static_cast<unsigned int>(draw_action_length * crt_cycles_multiplier)); break;
|
||||
case DrawAction::Blank: crt_->output_blank(static_cast<unsigned int>(draw_action_length * crt_cycles_multiplier)); break;
|
||||
case DrawAction::Pixels: end_pixel_line(); break;
|
||||
}
|
||||
screen_map_pointer_ = (screen_map_pointer_ + 1) % screen_map_.size();
|
||||
cycles_into_draw_action_ = 0;
|
||||
@@ -252,11 +255,11 @@ void VideoOutput::run_for(const Cycles cycles) {
|
||||
void VideoOutput::set_register(int address, uint8_t value) {
|
||||
switch(address & 0xf) {
|
||||
case 0x02:
|
||||
start_screen_address_ = (start_screen_address_ & 0xfe00) | (uint16_t)((value & 0xe0) << 1);
|
||||
start_screen_address_ = (start_screen_address_ & 0xfe00) | static_cast<uint16_t>((value & 0xe0) << 1);
|
||||
if(!start_screen_address_) start_screen_address_ |= 0x8000;
|
||||
break;
|
||||
case 0x03:
|
||||
start_screen_address_ = (start_screen_address_ & 0x01ff) | (uint16_t)((value & 0x3f) << 9);
|
||||
start_screen_address_ = (start_screen_address_ & 0x01ff) | static_cast<uint16_t>((value & 0x3f) << 9);
|
||||
if(!start_screen_address_) start_screen_address_ |= 0x8000;
|
||||
break;
|
||||
case 0x07: {
|
||||
@@ -298,17 +301,17 @@ void VideoOutput::set_register(int address, uint8_t value) {
|
||||
}
|
||||
|
||||
// regenerate all palette tables for now
|
||||
#define pack(a, b) (uint8_t)((a << 4) | (b))
|
||||
#define pack(a, b) static_cast<uint8_t>((a << 4) | (b))
|
||||
for(int byte = 0; byte < 256; byte++) {
|
||||
uint8_t *target = (uint8_t *)&palette_tables_.forty1bpp[byte];
|
||||
uint8_t *target = reinterpret_cast<uint8_t *>(&palette_tables_.forty1bpp[byte]);
|
||||
target[0] = pack(palette_[(byte&0x80) >> 4], palette_[(byte&0x40) >> 3]);
|
||||
target[1] = pack(palette_[(byte&0x20) >> 2], palette_[(byte&0x10) >> 1]);
|
||||
|
||||
target = (uint8_t *)&palette_tables_.eighty2bpp[byte];
|
||||
target = reinterpret_cast<uint8_t *>(&palette_tables_.eighty2bpp[byte]);
|
||||
target[0] = pack(palette_[((byte&0x80) >> 4) | ((byte&0x08) >> 2)], palette_[((byte&0x40) >> 3) | ((byte&0x04) >> 1)]);
|
||||
target[1] = pack(palette_[((byte&0x20) >> 2) | ((byte&0x02) >> 0)], palette_[((byte&0x10) >> 1) | ((byte&0x01) << 1)]);
|
||||
|
||||
target = (uint8_t *)&palette_tables_.eighty1bpp[byte];
|
||||
target = reinterpret_cast<uint8_t *>(&palette_tables_.eighty1bpp[byte]);
|
||||
target[0] = pack(palette_[(byte&0x80) >> 4], palette_[(byte&0x40) >> 3]);
|
||||
target[1] = pack(palette_[(byte&0x20) >> 2], palette_[(byte&0x10) >> 1]);
|
||||
target[2] = pack(palette_[(byte&0x08) >> 0], palette_[(byte&0x04) << 1]);
|
||||
@@ -382,9 +385,9 @@ unsigned int VideoOutput::get_cycles_until_next_ram_availability(int from_time)
|
||||
int output_position_line = graphics_line(output_position_);
|
||||
int implied_row = current_character_row_ + (current_line - output_position_line) % 10;
|
||||
if(implied_row < 8)
|
||||
result += (unsigned int)(80 - current_column);
|
||||
result += static_cast<unsigned int>(80 - current_column);
|
||||
}
|
||||
else result += (unsigned int)(80 - current_column);
|
||||
else result += static_cast<unsigned int>(80 - current_column);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -80,12 +80,13 @@ class VideoOutput {
|
||||
inline void output_pixels(unsigned int number_of_cycles);
|
||||
inline void setup_base_address();
|
||||
|
||||
int output_position_, unused_cycles_;
|
||||
int output_position_ = 0;
|
||||
int unused_cycles_ = 0;
|
||||
|
||||
uint8_t palette_[16];
|
||||
uint8_t screen_mode_;
|
||||
uint16_t screen_mode_base_address_;
|
||||
uint16_t start_screen_address_;
|
||||
uint8_t screen_mode_ = 6;
|
||||
uint16_t screen_mode_base_address_ = 0;
|
||||
uint16_t start_screen_address_ = 0;
|
||||
|
||||
uint8_t *ram_;
|
||||
struct {
|
||||
@@ -97,14 +98,18 @@ class VideoOutput {
|
||||
} palette_tables_;
|
||||
|
||||
// Display generation.
|
||||
uint16_t start_line_address_, current_screen_address_;
|
||||
int current_pixel_line_, current_pixel_column_, current_character_row_;
|
||||
uint8_t last_pixel_byte_;
|
||||
bool is_blank_line_;
|
||||
uint16_t start_line_address_ = 0;
|
||||
uint16_t current_screen_address_ = 0;
|
||||
int current_pixel_line_ = -1;
|
||||
int current_pixel_column_ = 0;
|
||||
int current_character_row_ = 0;
|
||||
uint8_t last_pixel_byte_ = 0;
|
||||
bool is_blank_line_ = false;
|
||||
|
||||
// CRT output
|
||||
uint8_t *current_output_target_, *initial_output_target_;
|
||||
unsigned int current_output_divider_;
|
||||
uint8_t *current_output_target_ = nullptr;
|
||||
uint8_t *initial_output_target_ = nullptr;
|
||||
unsigned int current_output_divider_ = 1;
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> crt_;
|
||||
|
||||
@@ -119,8 +124,8 @@ class VideoOutput {
|
||||
void setup_screen_map();
|
||||
void emplace_blank_line();
|
||||
void emplace_pixel_line();
|
||||
size_t screen_map_pointer_;
|
||||
int cycles_into_draw_action_;
|
||||
size_t screen_map_pointer_ = 0;
|
||||
int cycles_into_draw_action_ = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
24
Machines/JoystickMachine.hpp
Normal file
24
Machines/JoystickMachine.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// JoystickMachine.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 14/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef JoystickMachine_hpp
|
||||
#define JoystickMachine_hpp
|
||||
|
||||
#include "../Inputs/Joystick.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace JoystickMachine {
|
||||
|
||||
class Machine {
|
||||
public:
|
||||
virtual std::vector<std::unique_ptr<Inputs::Joystick>> &get_joysticks() = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* JoystickMachine_hpp */
|
||||
29
Machines/KeyboardMachine.cpp
Normal file
29
Machines/KeyboardMachine.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// KeyboardMachine.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "KeyboardMachine.hpp"
|
||||
|
||||
using namespace KeyboardMachine;
|
||||
|
||||
Machine::Machine() {
|
||||
keyboard_.set_delegate(this);
|
||||
}
|
||||
|
||||
void Machine::keyboard_did_change_key(Inputs::Keyboard *keyboard, Inputs::Keyboard::Key key, bool is_pressed) {
|
||||
uint16_t mapped_key = get_keyboard_mapper().mapped_key_for_key(key);
|
||||
if(mapped_key != KeyNotMapped) set_key_state(mapped_key, is_pressed);
|
||||
}
|
||||
|
||||
void Machine::reset_all_keys(Inputs::Keyboard *keyboard) {
|
||||
// TODO: unify naming.
|
||||
clear_all_keys();
|
||||
}
|
||||
|
||||
Inputs::Keyboard &Machine::get_keyboard() {
|
||||
return keyboard_;
|
||||
}
|
||||
@@ -9,20 +9,59 @@
|
||||
#ifndef KeyboardMachine_h
|
||||
#define KeyboardMachine_h
|
||||
|
||||
#include "../Inputs/Keyboard.hpp"
|
||||
|
||||
namespace KeyboardMachine {
|
||||
|
||||
class Machine {
|
||||
class Machine: public Inputs::Keyboard::Delegate {
|
||||
public:
|
||||
Machine();
|
||||
|
||||
/*!
|
||||
Indicates that the key @c key has been either pressed or released, according to
|
||||
the state of @c isPressed.
|
||||
*/
|
||||
virtual void set_key_state(uint16_t key, bool isPressed) = 0;
|
||||
virtual void set_key_state(uint16_t key, bool is_pressed) = 0;
|
||||
|
||||
/*!
|
||||
Instructs that all keys should now be treated as released.
|
||||
*/
|
||||
virtual void clear_all_keys() = 0;
|
||||
|
||||
/*!
|
||||
Provides a destination for keyboard input.
|
||||
*/
|
||||
virtual Inputs::Keyboard &get_keyboard();
|
||||
|
||||
/*!
|
||||
A keyboard mapper attempts to provide a physical mapping between host keys and emulated keys.
|
||||
See the character mapper for logical mapping.
|
||||
*/
|
||||
class KeyboardMapper {
|
||||
public:
|
||||
virtual uint16_t mapped_key_for_key(Inputs::Keyboard::Key key) = 0;
|
||||
};
|
||||
|
||||
/// Terminates a key sequence from the character mapper.
|
||||
static const uint16_t KeyEndSequence = 0xffff;
|
||||
|
||||
/*!
|
||||
Indicates that a key is not mapped (for the keyboard mapper) or that a
|
||||
character cannot be typed (for the character mapper).
|
||||
*/
|
||||
static const uint16_t KeyNotMapped = 0xfffe;
|
||||
|
||||
protected:
|
||||
/*!
|
||||
Allows individual machines to provide the mapping between host keys
|
||||
as per Inputs::Keyboard and their native scheme.
|
||||
*/
|
||||
virtual KeyboardMapper &get_keyboard_mapper() = 0;
|
||||
|
||||
private:
|
||||
void keyboard_did_change_key(Inputs::Keyboard *keyboard, Inputs::Keyboard::Key key, bool is_pressed) override;
|
||||
void reset_all_keys(Inputs::Keyboard *keyboard) override;
|
||||
Inputs::Keyboard keyboard_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,60 @@
|
||||
#include "Oric.hpp"
|
||||
//
|
||||
// Keyboard.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
uint16_t *Oric::Machine::sequence_for_character(Utility::Typer *typer, char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, EndSequence}
|
||||
#define SHIFT(...) {KeyLeftShift, __VA_ARGS__, EndSequence}
|
||||
#define X {NotMapped}
|
||||
#include "Keyboard.hpp"
|
||||
|
||||
using namespace Oric;
|
||||
|
||||
uint16_t KeyboardMapper::mapped_key_for_key(Inputs::Keyboard::Key key) {
|
||||
#define BIND(source, dest) case Inputs::Keyboard::Key::source: return Oric::dest
|
||||
switch(key) {
|
||||
default: break;
|
||||
|
||||
BIND(k0, Key0); BIND(k1, Key1); BIND(k2, Key2); BIND(k3, Key3); BIND(k4, Key4);
|
||||
BIND(k5, Key5); BIND(k6, Key6); BIND(k7, Key7); BIND(k8, Key8); BIND(k9, Key9);
|
||||
BIND(Q, KeyQ); BIND(W, KeyW); BIND(E, KeyE); BIND(R, KeyR); BIND(T, KeyT);
|
||||
BIND(Y, KeyY); BIND(U, KeyU); BIND(I, KeyI); BIND(O, KeyO); BIND(P, KeyP);
|
||||
BIND(A, KeyA); BIND(S, KeyS); BIND(D, KeyD); BIND(F, KeyF); BIND(G, KeyG);
|
||||
BIND(H, KeyH); BIND(J, KeyJ); BIND(K, KeyK); BIND(L, KeyL);
|
||||
BIND(Z, KeyZ); BIND(X, KeyX); BIND(C, KeyC); BIND(V, KeyV);
|
||||
BIND(B, KeyB); BIND(N, KeyN); BIND(M, KeyM);
|
||||
|
||||
BIND(Left, KeyLeft); BIND(Right, KeyRight); BIND(Up, KeyUp); BIND(Down, KeyDown);
|
||||
|
||||
BIND(Hyphen, KeyMinus); BIND(Equals, KeyEquals); BIND(BackSlash, KeyBackSlash);
|
||||
BIND(OpenSquareBracket, KeyOpenSquare); BIND(CloseSquareBracket, KeyCloseSquare);
|
||||
|
||||
BIND(BackSpace, KeyDelete); BIND(Delete, KeyDelete);
|
||||
|
||||
BIND(Semicolon, KeySemiColon); BIND(Quote, KeyQuote);
|
||||
BIND(Comma, KeyComma); BIND(FullStop, KeyFullStop); BIND(ForwardSlash, KeyForwardSlash);
|
||||
|
||||
BIND(Escape, KeyEscape); BIND(Tab, KeyEscape);
|
||||
BIND(CapsLock, KeyControl); BIND(LeftControl, KeyControl); BIND(RightControl, KeyControl);
|
||||
BIND(LeftOption, KeyFunction);
|
||||
BIND(RightOption, KeyFunction);
|
||||
BIND(LeftMeta, KeyFunction);
|
||||
BIND(RightMeta, KeyFunction);
|
||||
BIND(LeftShift, KeyLeftShift);
|
||||
BIND(RightShift, KeyRightShift);
|
||||
|
||||
BIND(Space, KeySpace);
|
||||
BIND(Enter, KeyReturn);
|
||||
}
|
||||
#undef BIND
|
||||
|
||||
return KeyboardMachine::Machine::KeyNotMapped;
|
||||
}
|
||||
|
||||
uint16_t *CharacterMapper::sequence_for_character(char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define SHIFT(...) {KeyLeftShift, __VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define X {KeyboardMachine::Machine::KeyNotMapped}
|
||||
static KeySequence key_sequences[] = {
|
||||
/* NUL */ X, /* SOH */ X,
|
||||
/* STX */ X, /* ETX */ X,
|
||||
48
Machines/Oric/Keyboard.hpp
Normal file
48
Machines/Oric/Keyboard.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Keyboard.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Machines_Oric_Keyboard_hpp
|
||||
#define Machines_Oric_Keyboard_hpp
|
||||
|
||||
#include "../KeyboardMachine.hpp"
|
||||
#include "../Utility/Typer.hpp"
|
||||
|
||||
namespace Oric {
|
||||
|
||||
enum Key: uint16_t {
|
||||
Key3 = 0x0000 | 0x80, KeyX = 0x0000 | 0x40, Key1 = 0x0000 | 0x20,
|
||||
KeyV = 0x0000 | 0x08, Key5 = 0x0000 | 0x04, KeyN = 0x0000 | 0x02, Key7 = 0x0000 | 0x01,
|
||||
KeyD = 0x0100 | 0x80, KeyQ = 0x0100 | 0x40, KeyEscape = 0x0100 | 0x20,
|
||||
KeyF = 0x0100 | 0x08, KeyR = 0x0100 | 0x04, KeyT = 0x0100 | 0x02, KeyJ = 0x0100 | 0x01,
|
||||
KeyC = 0x0200 | 0x80, Key2 = 0x0200 | 0x40, KeyZ = 0x0200 | 0x20, KeyControl = 0x0200 | 0x10,
|
||||
Key4 = 0x0200 | 0x08, KeyB = 0x0200 | 0x04, Key6 = 0x0200 | 0x02, KeyM = 0x0200 | 0x01,
|
||||
KeyQuote = 0x0300 | 0x80, KeyBackSlash = 0x0300 | 0x40,
|
||||
KeyMinus = 0x0300 | 0x08, KeySemiColon = 0x0300 | 0x04, Key9 = 0x0300 | 0x02, KeyK = 0x0300 | 0x01,
|
||||
KeyRight = 0x0400 | 0x80, KeyDown = 0x0400 | 0x40, KeyLeft = 0x0400 | 0x20, KeyLeftShift = 0x0400 | 0x10,
|
||||
KeyUp = 0x0400 | 0x08, KeyFullStop = 0x0400 | 0x04, KeyComma = 0x0400 | 0x02, KeySpace = 0x0400 | 0x01,
|
||||
KeyOpenSquare = 0x0500 | 0x80, KeyCloseSquare = 0x0500 | 0x40, KeyDelete = 0x0500 | 0x20, KeyFunction = 0x0500 | 0x10,
|
||||
KeyP = 0x0500 | 0x08, KeyO = 0x0500 | 0x04, KeyI = 0x0500 | 0x02, KeyU = 0x0500 | 0x01,
|
||||
KeyW = 0x0600 | 0x80, KeyS = 0x0600 | 0x40, KeyA = 0x0600 | 0x20,
|
||||
KeyE = 0x0600 | 0x08, KeyG = 0x0600 | 0x04, KeyH = 0x0600 | 0x02, KeyY = 0x0600 | 0x01,
|
||||
KeyEquals = 0x0700 | 0x80, KeyReturn = 0x0700 | 0x20, KeyRightShift = 0x0700 | 0x10,
|
||||
KeyForwardSlash = 0x0700 | 0x08, Key0 = 0x0700 | 0x04, KeyL = 0x0700 | 0x02, Key8 = 0x0700 | 0x01,
|
||||
|
||||
KeyNMI = 0xfffd,
|
||||
};
|
||||
|
||||
struct KeyboardMapper: public KeyboardMachine::Machine::KeyboardMapper {
|
||||
uint16_t mapped_key_for_key(Inputs::Keyboard::Key key);
|
||||
};
|
||||
|
||||
struct CharacterMapper: public ::Utility::CharacterMapper {
|
||||
uint16_t *sequence_for_character(char character);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif /* KeyboardMapper_hpp */
|
||||
@@ -30,7 +30,7 @@ Microdisc::Microdisc() :
|
||||
|
||||
void Microdisc::set_disk(std::shared_ptr<Storage::Disk::Disk> disk, int drive) {
|
||||
if(!drives_[drive]) {
|
||||
drives_[drive].reset(new Storage::Disk::Drive);
|
||||
drives_[drive].reset(new Storage::Disk::Drive(8000000, 300, 2));
|
||||
if(drive == selected_drive_) set_drive(drives_[drive]);
|
||||
}
|
||||
drives_[drive]->set_disk(disk);
|
||||
@@ -53,7 +53,7 @@ void Microdisc::set_control_register(uint8_t control, uint8_t changes) {
|
||||
|
||||
// b4: side select
|
||||
if(changes & 0x10) {
|
||||
unsigned int head = (control & 0x10) ? 1 : 0;
|
||||
int head = (control & 0x10) ? 1 : 0;
|
||||
for(int c = 0; c < 4; c++) {
|
||||
if(drives_[c]) drives_[c]->set_head(head);
|
||||
}
|
||||
@@ -95,7 +95,14 @@ uint8_t Microdisc::get_data_request_register() {
|
||||
}
|
||||
|
||||
void Microdisc::set_head_load_request(bool head_load) {
|
||||
set_motor_on(head_load);
|
||||
// The drive motors (at present: I believe **all drive motors** regardless of the selected drive) receive
|
||||
// the current head load request state.
|
||||
for(int c = 0; c < 4; c++) {
|
||||
if(drives_[c]) drives_[c]->set_motor_on(head_load);
|
||||
}
|
||||
|
||||
// A request to load the head results in a delay until the head is confirmed loaded. This delay is handled
|
||||
// in ::run_for. A request to unload the head results in an instant answer that the head is unloaded.
|
||||
if(head_load) {
|
||||
head_load_request_counter_ = 0;
|
||||
} else {
|
||||
|
||||
@@ -7,278 +7,458 @@
|
||||
//
|
||||
|
||||
#include "Oric.hpp"
|
||||
#include "../MemoryFuzzer.hpp"
|
||||
|
||||
#include "Keyboard.hpp"
|
||||
#include "Microdisc.hpp"
|
||||
#include "Video.hpp"
|
||||
|
||||
#include "../Utility/MemoryFuzzer.hpp"
|
||||
#include "../Utility/Typer.hpp"
|
||||
|
||||
#include "../../Processors/6502/6502.hpp"
|
||||
#include "../../Components/6522/6522.hpp"
|
||||
#include "../../Components/AY38910/AY38910.hpp"
|
||||
|
||||
#include "../../Storage/Tape/Tape.hpp"
|
||||
#include "../../Storage/Tape/Parsers/Oric.hpp"
|
||||
|
||||
#include "../../ClockReceiver/ForceInline.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace Oric {
|
||||
|
||||
/*!
|
||||
Models the Oric's keyboard: eight key rows, containing a bitfield of keys set.
|
||||
|
||||
Active line is selected through a port on the Oric's VIA, and a column mask is
|
||||
selected via a port on the AY, returning a single Boolean representation of the
|
||||
logical OR of every key selected by the column mask on the active row.
|
||||
*/
|
||||
class Keyboard {
|
||||
public:
|
||||
Keyboard() {
|
||||
clear_all_keys();
|
||||
}
|
||||
|
||||
/// Sets whether @c key is or is not pressed, per @c is_pressed.
|
||||
void set_key_state(uint16_t key, bool is_pressed) {
|
||||
uint8_t mask = key & 0xff;
|
||||
int line = key >> 8;
|
||||
|
||||
if(is_pressed) rows_[line] |= mask;
|
||||
else rows_[line] &= ~mask;
|
||||
}
|
||||
|
||||
/// Sets all keys as unpressed.
|
||||
void clear_all_keys() {
|
||||
memset(rows_, 0, sizeof(rows_));
|
||||
}
|
||||
|
||||
/// Selects the active row.
|
||||
void set_active_row(uint8_t row) {
|
||||
row_ = row & 7;
|
||||
}
|
||||
|
||||
/// Queries the keys on the active row specified by @c mask.
|
||||
bool query_column(uint8_t column_mask) {
|
||||
return !!(rows_[row_] & column_mask);
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t row_ = 0;
|
||||
uint8_t rows_[8];
|
||||
};
|
||||
|
||||
/*!
|
||||
Provide's the Oric's tape player: a standard binary-sampled tape which also holds
|
||||
an instance of the Oric tape parser, to provide fast-tape loading.
|
||||
*/
|
||||
class TapePlayer: public Storage::Tape::BinaryTapePlayer {
|
||||
public:
|
||||
TapePlayer() : Storage::Tape::BinaryTapePlayer(1000000) {}
|
||||
|
||||
/*!
|
||||
Parses the incoming tape event stream to obtain the next stored byte.
|
||||
|
||||
@param use_fast_encoding If set to @c true , inspects the tape as though it
|
||||
is encoded in the Oric's fast-loading scheme. Otherwise looks for a slow-encoded byte.
|
||||
|
||||
@returns The next byte from the tape.
|
||||
*/
|
||||
uint8_t get_next_byte(bool use_fast_encoding) {
|
||||
return static_cast<uint8_t>(parser_.get_next_byte(get_tape(), use_fast_encoding));
|
||||
}
|
||||
|
||||
private:
|
||||
Storage::Tape::Oric::Parser parser_;
|
||||
};
|
||||
|
||||
/*!
|
||||
Implements the Oric's VIA's port handler. On the Oric the VIA's ports connect
|
||||
to the AY, the tape's motor control signal and the keyboard.
|
||||
*/
|
||||
class VIAPortHandler: public MOS::MOS6522::IRQDelegatePortHandler {
|
||||
public:
|
||||
VIAPortHandler(TapePlayer &tape_player, Keyboard &keyboard) : tape_player_(tape_player), keyboard_(keyboard) {}
|
||||
|
||||
/*!
|
||||
Reponds to the 6522's control line output change signal; on an Oric A2 is connected to
|
||||
the AY's BDIR, and B2 is connected to the AY's A2.
|
||||
*/
|
||||
void set_control_line_output(MOS::MOS6522::Port port, MOS::MOS6522::Line line, bool value) {
|
||||
if(line) {
|
||||
if(port) ay_bdir_ = value; else ay_bc1_ = value;
|
||||
update_ay();
|
||||
ay8910_->set_control_lines( (GI::AY38910::ControlLines)((ay_bdir_ ? GI::AY38910::BDIR : 0) | (ay_bc1_ ? GI::AY38910::BC1 : 0) | GI::AY38910::BC2));
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Reponds to changes in the 6522's port output. On an Oric port B sets the tape motor control
|
||||
and the keyboard's active row. Port A is connected to the AY's data bus.
|
||||
*/
|
||||
void set_port_output(MOS::MOS6522::Port port, uint8_t value, uint8_t direction_mask) {
|
||||
if(port) {
|
||||
keyboard_.set_active_row(value);
|
||||
tape_player_.set_motor_control(value & 0x40);
|
||||
} else {
|
||||
update_ay();
|
||||
ay8910_->set_data_input(value);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Provides input data for the 6522. Port B reads the keyboard, and port A reads from the AY.
|
||||
*/
|
||||
uint8_t get_port_input(MOS::MOS6522::Port port) {
|
||||
if(port) {
|
||||
uint8_t column = ay8910_->get_port_output(false) ^ 0xff;
|
||||
return keyboard_.query_column(column) ? 0x08 : 0x00;
|
||||
} else {
|
||||
return ay8910_->get_data_output();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Advances time. This class manages the AY's concept of time to permit updating-on-demand.
|
||||
*/
|
||||
inline void run_for(const Cycles cycles) {
|
||||
cycles_since_ay_update_ += cycles;
|
||||
}
|
||||
|
||||
/// Flushes any queued behaviour (which, specifically, means on the AY).
|
||||
void flush() {
|
||||
ay8910_->run_for(cycles_since_ay_update_.flush());
|
||||
ay8910_->flush();
|
||||
}
|
||||
|
||||
/// Sets the AY in use by the machine the VIA that uses this port handler sits within.
|
||||
void set_ay(GI::AY38910::AY38910 *ay) {
|
||||
ay8910_ = ay;
|
||||
}
|
||||
|
||||
private:
|
||||
void update_ay() {
|
||||
ay8910_->run_for(cycles_since_ay_update_.flush());
|
||||
}
|
||||
bool ay_bdir_ = false;
|
||||
bool ay_bc1_ = false;
|
||||
Cycles cycles_since_ay_update_;
|
||||
|
||||
GI::AY38910::AY38910 *ay8910_ = nullptr;
|
||||
TapePlayer &tape_player_;
|
||||
Keyboard &keyboard_;
|
||||
};
|
||||
|
||||
class ConcreteMachine:
|
||||
public CPU::MOS6502::BusHandler,
|
||||
public MOS::MOS6522::IRQDelegatePortHandler::Delegate,
|
||||
public Utility::TypeRecipient,
|
||||
public Storage::Tape::BinaryTapePlayer::Delegate,
|
||||
public Microdisc::Delegate,
|
||||
public Machine {
|
||||
|
||||
public:
|
||||
ConcreteMachine() :
|
||||
m6502_(*this),
|
||||
paged_rom_(rom_),
|
||||
via_(via_port_handler_),
|
||||
via_port_handler_(tape_player_, keyboard_) {
|
||||
set_clock_rate(1000000);
|
||||
via_port_handler_.set_interrupt_delegate(this);
|
||||
tape_player_.set_delegate(this);
|
||||
Memory::Fuzz(ram_, sizeof(ram_));
|
||||
}
|
||||
|
||||
void set_rom(ROM rom, const std::vector<uint8_t> &data) override final {
|
||||
switch(rom) {
|
||||
case BASIC11: basic11_rom_ = std::move(data); break;
|
||||
case BASIC10: basic10_rom_ = std::move(data); break;
|
||||
case Microdisc: microdisc_rom_ = std::move(data); break;
|
||||
case Colour:
|
||||
colour_rom_ = std::move(data);
|
||||
if(video_output_) video_output_->set_colour_rom(colour_rom_);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void set_key_state(uint16_t key, bool is_pressed) override final {
|
||||
if(key == KeyNMI) {
|
||||
m6502_.set_nmi_line(is_pressed);
|
||||
} else {
|
||||
keyboard_.set_key_state(key, is_pressed);
|
||||
}
|
||||
}
|
||||
|
||||
void clear_all_keys() override final {
|
||||
keyboard_.clear_all_keys();
|
||||
}
|
||||
|
||||
void set_use_fast_tape_hack(bool activate) override final {
|
||||
use_fast_tape_hack_ = activate;
|
||||
}
|
||||
|
||||
void set_output_device(Outputs::CRT::OutputDevice output_device) override final {
|
||||
video_output_->set_output_device(output_device);
|
||||
}
|
||||
|
||||
// to satisfy ConfigurationTarget::Machine
|
||||
void configure_as_target(const StaticAnalyser::Target &target) override final {
|
||||
if(target.oric.has_microdisc) {
|
||||
microdisc_is_enabled_ = true;
|
||||
microdisc_did_change_paging_flags(µdisc_);
|
||||
microdisc_.set_delegate(this);
|
||||
}
|
||||
|
||||
if(target.loadingCommand.length()) {
|
||||
set_typer_for_string(target.loadingCommand.c_str());
|
||||
}
|
||||
|
||||
if(target.oric.use_atmos_rom) {
|
||||
memcpy(rom_, basic11_rom_.data(), std::min(basic11_rom_.size(), sizeof(rom_)));
|
||||
|
||||
is_using_basic11_ = true;
|
||||
tape_get_byte_address_ = 0xe6c9;
|
||||
scan_keyboard_address_ = 0xf495;
|
||||
tape_speed_address_ = 0x024d;
|
||||
} else {
|
||||
memcpy(rom_, basic10_rom_.data(), std::min(basic10_rom_.size(), sizeof(rom_)));
|
||||
|
||||
is_using_basic11_ = false;
|
||||
tape_get_byte_address_ = 0xe630;
|
||||
scan_keyboard_address_ = 0xf43c;
|
||||
tape_speed_address_ = 0x67;
|
||||
}
|
||||
|
||||
insert_media(target.media);
|
||||
}
|
||||
|
||||
bool insert_media(const StaticAnalyser::Media &media) override final {
|
||||
if(media.tapes.size()) {
|
||||
tape_player_.set_tape(media.tapes.front());
|
||||
}
|
||||
|
||||
int drive_index = 0;
|
||||
for(auto disk : media.disks) {
|
||||
if(drive_index < 4) microdisc_.set_disk(disk, drive_index);
|
||||
drive_index++;
|
||||
}
|
||||
|
||||
return !media.tapes.empty() || (!media.disks.empty() && microdisc_is_enabled_);
|
||||
}
|
||||
|
||||
// to satisfy CPU::MOS6502::BusHandler
|
||||
forceinline Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
if(address > ram_top_) {
|
||||
if(isReadOperation(operation)) *value = paged_rom_[address - ram_top_ - 1];
|
||||
|
||||
// 024D = 0 => fast; otherwise slow
|
||||
// E6C9 = read byte: return byte in A
|
||||
if( address == tape_get_byte_address_ &&
|
||||
paged_rom_ == rom_ &&
|
||||
use_fast_tape_hack_ &&
|
||||
operation == CPU::MOS6502::BusOperation::ReadOpcode &&
|
||||
tape_player_.has_tape() &&
|
||||
!tape_player_.get_tape()->is_at_end()) {
|
||||
|
||||
uint8_t next_byte = tape_player_.get_next_byte(!ram_[tape_speed_address_]);
|
||||
m6502_.set_value_of_register(CPU::MOS6502::A, next_byte);
|
||||
m6502_.set_value_of_register(CPU::MOS6502::Flags, next_byte ? 0 : CPU::MOS6502::Flag::Zero);
|
||||
*value = 0x60; // i.e. RTS
|
||||
}
|
||||
} else {
|
||||
if((address & 0xff00) == 0x0300) {
|
||||
if(microdisc_is_enabled_ && address >= 0x0310) {
|
||||
switch(address) {
|
||||
case 0x0310: case 0x0311: case 0x0312: case 0x0313:
|
||||
if(isReadOperation(operation)) *value = microdisc_.get_register(address);
|
||||
else microdisc_.set_register(address, *value);
|
||||
break;
|
||||
case 0x314: case 0x315: case 0x316: case 0x317:
|
||||
if(isReadOperation(operation)) *value = microdisc_.get_interrupt_request_register();
|
||||
else microdisc_.set_control_register(*value);
|
||||
break;
|
||||
case 0x318: case 0x319: case 0x31a: case 0x31b:
|
||||
if(isReadOperation(operation)) *value = microdisc_.get_data_request_register();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if(isReadOperation(operation)) *value = via_.get_register(address);
|
||||
else via_.set_register(address, *value);
|
||||
}
|
||||
} else {
|
||||
if(isReadOperation(operation))
|
||||
*value = ram_[address];
|
||||
else {
|
||||
if(address >= 0x9800 && address <= 0xc000) update_video();
|
||||
ram_[address] = *value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(typer_ && address == scan_keyboard_address_ && operation == CPU::MOS6502::BusOperation::ReadOpcode) {
|
||||
// the Oric 1 misses any key pressed on the very first entry into the read keyboard routine, so don't
|
||||
// do anything until at least the second, regardless of machine
|
||||
if(!keyboard_read_count_) keyboard_read_count_++;
|
||||
else if(!typer_->type_next_character()) {
|
||||
clear_all_keys();
|
||||
typer_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
via_.run_for(Cycles(1));
|
||||
via_port_handler_.run_for(Cycles(1));
|
||||
tape_player_.run_for(Cycles(1));
|
||||
if(microdisc_is_enabled_) microdisc_.run_for(Cycles(8));
|
||||
cycles_since_video_update_++;
|
||||
return Cycles(1);
|
||||
}
|
||||
|
||||
forceinline void flush() {
|
||||
update_video();
|
||||
via_port_handler_.flush();
|
||||
}
|
||||
|
||||
// to satisfy CRTMachine::Machine
|
||||
void setup_output(float aspect_ratio) override final {
|
||||
ay8910_.reset(new GI::AY38910::AY38910());
|
||||
ay8910_->set_clock_rate(1000000);
|
||||
via_port_handler_.set_ay(ay8910_.get());
|
||||
|
||||
video_output_.reset(new VideoOutput(ram_));
|
||||
if(!colour_rom_.empty()) video_output_->set_colour_rom(colour_rom_);
|
||||
}
|
||||
|
||||
void close_output() override final {
|
||||
video_output_.reset();
|
||||
ay8910_.reset();
|
||||
via_port_handler_.set_ay(nullptr);
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> get_crt() override final {
|
||||
return video_output_->get_crt();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::Speaker> get_speaker() override final {
|
||||
return ay8910_;
|
||||
}
|
||||
|
||||
void run_for(const Cycles cycles) override final {
|
||||
m6502_.run_for(cycles);
|
||||
}
|
||||
|
||||
// to satisfy MOS::MOS6522IRQDelegate::Delegate
|
||||
void mos6522_did_change_interrupt_status(void *mos6522) override final {
|
||||
set_interrupt_line();
|
||||
}
|
||||
|
||||
// to satisfy Storage::Tape::BinaryTapePlayer::Delegate
|
||||
void tape_did_change_input(Storage::Tape::BinaryTapePlayer *tape_player) override final {
|
||||
// set CB1
|
||||
via_.set_control_line_input(MOS::MOS6522::Port::B, MOS::MOS6522::Line::One, !tape_player->get_input());
|
||||
}
|
||||
|
||||
// for Utility::TypeRecipient::Delegate
|
||||
void set_typer_for_string(const char *string) override final {
|
||||
std::unique_ptr<CharacterMapper> mapper(new CharacterMapper);
|
||||
Utility::TypeRecipient::set_typer_for_string(string, std::move(mapper));
|
||||
}
|
||||
|
||||
// for Microdisc::Delegate
|
||||
void microdisc_did_change_paging_flags(class Microdisc *microdisc) override final {
|
||||
int flags = microdisc->get_paging_flags();
|
||||
if(!(flags&Microdisc::PagingFlags::BASICDisable)) {
|
||||
ram_top_ = 0xbfff;
|
||||
paged_rom_ = rom_;
|
||||
} else {
|
||||
if(flags&Microdisc::PagingFlags::MicrodscDisable) {
|
||||
ram_top_ = 0xffff;
|
||||
} else {
|
||||
ram_top_ = 0xdfff;
|
||||
paged_rom_ = microdisc_rom_.data();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wd1770_did_change_output(WD::WD1770 *wd1770) override final {
|
||||
set_interrupt_line();
|
||||
}
|
||||
|
||||
KeyboardMapper &get_keyboard_mapper() override {
|
||||
return keyboard_mapper_;
|
||||
}
|
||||
|
||||
private:
|
||||
CPU::MOS6502::Processor<ConcreteMachine, false> m6502_;
|
||||
|
||||
// RAM and ROM
|
||||
std::vector<uint8_t> basic11_rom_, basic10_rom_, microdisc_rom_, colour_rom_;
|
||||
uint8_t ram_[65536], rom_[16384];
|
||||
Cycles cycles_since_video_update_;
|
||||
inline void update_video() {
|
||||
video_output_->run_for(cycles_since_video_update_.flush());
|
||||
}
|
||||
|
||||
// ROM bookkeeping
|
||||
bool is_using_basic11_ = false;
|
||||
uint16_t tape_get_byte_address_ = 0, scan_keyboard_address_ = 0, tape_speed_address_ = 0;
|
||||
int keyboard_read_count_ = 0;
|
||||
|
||||
// Outputs
|
||||
std::unique_ptr<VideoOutput> video_output_;
|
||||
std::shared_ptr<GI::AY38910::AY38910> ay8910_;
|
||||
|
||||
// Inputs
|
||||
Oric::KeyboardMapper keyboard_mapper_;
|
||||
|
||||
// The tape
|
||||
TapePlayer tape_player_;
|
||||
bool use_fast_tape_hack_ = false;
|
||||
|
||||
VIAPortHandler via_port_handler_;
|
||||
MOS::MOS6522::MOS6522<VIAPortHandler> via_;
|
||||
Keyboard keyboard_;
|
||||
|
||||
// the Microdisc, if in use
|
||||
class Microdisc microdisc_;
|
||||
bool microdisc_is_enabled_ = false;
|
||||
uint16_t ram_top_ = 0xbfff;
|
||||
uint8_t *paged_rom_;
|
||||
|
||||
inline void set_interrupt_line() {
|
||||
m6502_.set_irq_line(
|
||||
via_.get_interrupt_line() ||
|
||||
(microdisc_is_enabled_ && microdisc_.get_interrupt_request_line()));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using namespace Oric;
|
||||
|
||||
Machine::Machine() :
|
||||
use_fast_tape_hack_(false),
|
||||
typer_delay_(2500000),
|
||||
keyboard_read_count_(0),
|
||||
keyboard_(new Keyboard),
|
||||
ram_top_(0xbfff),
|
||||
paged_rom_(rom_),
|
||||
microdisc_is_enabled_(false) {
|
||||
set_clock_rate(1000000);
|
||||
via_.set_interrupt_delegate(this);
|
||||
via_.keyboard = keyboard_;
|
||||
clear_all_keys();
|
||||
via_.tape->set_delegate(this);
|
||||
Memory::Fuzz(ram_, sizeof(ram_));
|
||||
Machine *Machine::Oric() {
|
||||
return new ConcreteMachine;
|
||||
}
|
||||
|
||||
void Machine::configure_as_target(const StaticAnalyser::Target &target) {
|
||||
if(target.tapes.size()) {
|
||||
via_.tape->set_tape(target.tapes.front());
|
||||
}
|
||||
|
||||
if(target.loadingCommand.length()) { // TODO: and automatic loading option enabled
|
||||
set_typer_for_string(target.loadingCommand.c_str());
|
||||
}
|
||||
|
||||
if(target.oric.has_microdisc) {
|
||||
microdisc_is_enabled_ = true;
|
||||
microdisc_did_change_paging_flags(µdisc_);
|
||||
microdisc_.set_delegate(this);
|
||||
}
|
||||
|
||||
int drive_index = 0;
|
||||
for(auto disk : target.disks) {
|
||||
if(drive_index < 4) microdisc_.set_disk(disk, drive_index);
|
||||
drive_index++;
|
||||
}
|
||||
|
||||
if(target.oric.use_atmos_rom) {
|
||||
memcpy(rom_, basic11_rom_.data(), std::min(basic11_rom_.size(), sizeof(rom_)));
|
||||
|
||||
is_using_basic11_ = true;
|
||||
tape_get_byte_address_ = 0xe6c9;
|
||||
scan_keyboard_address_ = 0xf495;
|
||||
tape_speed_address_ = 0x024d;
|
||||
} else {
|
||||
memcpy(rom_, basic10_rom_.data(), std::min(basic10_rom_.size(), sizeof(rom_)));
|
||||
|
||||
is_using_basic11_ = false;
|
||||
tape_get_byte_address_ = 0xe630;
|
||||
scan_keyboard_address_ = 0xf43c;
|
||||
tape_speed_address_ = 0x67;
|
||||
}
|
||||
}
|
||||
|
||||
void Machine::set_rom(ROM rom, const std::vector<uint8_t> &data) {
|
||||
switch(rom) {
|
||||
case BASIC11: basic11_rom_ = std::move(data); break;
|
||||
case BASIC10: basic10_rom_ = std::move(data); break;
|
||||
case Microdisc: microdisc_rom_ = std::move(data); break;
|
||||
case Colour:
|
||||
colour_rom_ = std::move(data);
|
||||
if(video_output_) video_output_->set_colour_rom(colour_rom_);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Cycles Machine::perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
||||
if(address > ram_top_) {
|
||||
if(isReadOperation(operation)) *value = paged_rom_[address - ram_top_ - 1];
|
||||
|
||||
// 024D = 0 => fast; otherwise slow
|
||||
// E6C9 = read byte: return byte in A
|
||||
if(address == tape_get_byte_address_ && paged_rom_ == rom_ && use_fast_tape_hack_ && operation == CPU::MOS6502::BusOperation::ReadOpcode && via_.tape->has_tape() && !via_.tape->get_tape()->is_at_end()) {
|
||||
uint8_t next_byte = via_.tape->get_next_byte(!ram_[tape_speed_address_]);
|
||||
set_value_of_register(CPU::MOS6502::A, next_byte);
|
||||
set_value_of_register(CPU::MOS6502::Flags, next_byte ? 0 : CPU::MOS6502::Flag::Zero);
|
||||
*value = 0x60; // i.e. RTS
|
||||
}
|
||||
} else {
|
||||
if((address & 0xff00) == 0x0300) {
|
||||
if(microdisc_is_enabled_ && address >= 0x0310) {
|
||||
switch(address) {
|
||||
case 0x0310: case 0x0311: case 0x0312: case 0x0313:
|
||||
if(isReadOperation(operation)) *value = microdisc_.get_register(address);
|
||||
else microdisc_.set_register(address, *value);
|
||||
break;
|
||||
case 0x314: case 0x315: case 0x316: case 0x317:
|
||||
if(isReadOperation(operation)) *value = microdisc_.get_interrupt_request_register();
|
||||
else microdisc_.set_control_register(*value);
|
||||
break;
|
||||
case 0x318: case 0x319: case 0x31a: case 0x31b:
|
||||
if(isReadOperation(operation)) *value = microdisc_.get_data_request_register();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if(isReadOperation(operation)) *value = via_.get_register(address);
|
||||
else via_.set_register(address, *value);
|
||||
}
|
||||
} else {
|
||||
if(isReadOperation(operation))
|
||||
*value = ram_[address];
|
||||
else {
|
||||
if(address >= 0x9800 && address <= 0xc000) { update_video(); typer_delay_ = 0; }
|
||||
ram_[address] = *value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(typer_ && address == scan_keyboard_address_ && operation == CPU::MOS6502::BusOperation::ReadOpcode) {
|
||||
// the Oric 1 misses any key pressed on the very first entry into the read keyboard routine, so don't
|
||||
// do anything until at least the second, regardless of machine
|
||||
if(!keyboard_read_count_) keyboard_read_count_++;
|
||||
else if(!typer_->type_next_character()) {
|
||||
clear_all_keys();
|
||||
typer_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
via_.run_for(Cycles(1));
|
||||
if(microdisc_is_enabled_) microdisc_.run_for(Cycles(8));
|
||||
cycles_since_video_update_++;
|
||||
return Cycles(1);
|
||||
}
|
||||
|
||||
void Machine::flush() {
|
||||
update_video();
|
||||
via_.flush();
|
||||
}
|
||||
|
||||
void Machine::update_video() {
|
||||
video_output_->run_for(cycles_since_video_update_.flush());
|
||||
}
|
||||
|
||||
void Machine::setup_output(float aspect_ratio) {
|
||||
via_.ay8910.reset(new GI::AY38910());
|
||||
via_.ay8910->set_clock_rate(1000000);
|
||||
video_output_.reset(new VideoOutput(ram_));
|
||||
if(!colour_rom_.empty()) video_output_->set_colour_rom(colour_rom_);
|
||||
}
|
||||
|
||||
void Machine::close_output() {
|
||||
video_output_.reset();
|
||||
via_.ay8910.reset();
|
||||
}
|
||||
|
||||
void Machine::mos6522_did_change_interrupt_status(void *mos6522) {
|
||||
set_interrupt_line();
|
||||
}
|
||||
|
||||
void Machine::set_key_state(uint16_t key, bool isPressed) {
|
||||
if(key == KeyNMI) {
|
||||
set_nmi_line(isPressed);
|
||||
} else {
|
||||
if(isPressed)
|
||||
keyboard_->rows[key >> 8] |= (key & 0xff);
|
||||
else
|
||||
keyboard_->rows[key >> 8] &= ~(key & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
void Machine::clear_all_keys() {
|
||||
memset(keyboard_->rows, 0, sizeof(keyboard_->rows));
|
||||
}
|
||||
|
||||
void Machine::set_use_fast_tape_hack(bool activate) {
|
||||
use_fast_tape_hack_ = activate;
|
||||
}
|
||||
|
||||
void Machine::set_output_device(Outputs::CRT::OutputDevice output_device) {
|
||||
video_output_->set_output_device(output_device);
|
||||
}
|
||||
|
||||
void Machine::tape_did_change_input(Storage::Tape::BinaryTapePlayer *tape_player) {
|
||||
// set CB1
|
||||
via_.set_control_line_input(VIA::Port::B, VIA::Line::One, !tape_player->get_input());
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> Machine::get_crt() {
|
||||
return video_output_->get_crt();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::Speaker> Machine::get_speaker() {
|
||||
return via_.ay8910;
|
||||
}
|
||||
|
||||
void Machine::run_for(const Cycles cycles) {
|
||||
CPU::MOS6502::Processor<Machine>::run_for(cycles);
|
||||
}
|
||||
|
||||
#pragma mark - The 6522
|
||||
|
||||
Machine::VIA::VIA() :
|
||||
MOS::MOS6522<Machine::VIA>(),
|
||||
tape(new TapePlayer) {}
|
||||
|
||||
void Machine::VIA::set_control_line_output(Port port, Line line, bool value) {
|
||||
if(line) {
|
||||
if(port) ay_bdir_ = value; else ay_bc1_ = value;
|
||||
update_ay();
|
||||
}
|
||||
}
|
||||
|
||||
void Machine::VIA::set_port_output(Port port, uint8_t value, uint8_t direction_mask) {
|
||||
if(port) {
|
||||
keyboard->row = value;
|
||||
tape->set_motor_control(value & 0x40);
|
||||
} else {
|
||||
ay8910->set_data_input(value);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t Machine::VIA::get_port_input(Port port) {
|
||||
if(port) {
|
||||
uint8_t column = ay8910->get_port_output(false) ^ 0xff;
|
||||
return (keyboard->rows[keyboard->row & 7] & column) ? 0x08 : 0x00;
|
||||
} else {
|
||||
return ay8910->get_data_output();
|
||||
}
|
||||
}
|
||||
|
||||
void Machine::VIA::flush() {
|
||||
ay8910->run_for(cycles_since_ay_update_.flush());
|
||||
ay8910->flush();
|
||||
}
|
||||
|
||||
void Machine::VIA::run_for(const Cycles cycles) {
|
||||
cycles_since_ay_update_ += cycles;
|
||||
MOS::MOS6522<VIA>::run_for(cycles);
|
||||
tape->run_for(cycles);
|
||||
}
|
||||
|
||||
void Machine::VIA::update_ay() {
|
||||
ay8910->run_for(cycles_since_ay_update_.flush());
|
||||
ay8910->set_control_lines( (GI::AY38910::ControlLines)((ay_bdir_ ? GI::AY38910::BCDIR : 0) | (ay_bc1_ ? GI::AY38910::BC1 : 0) | GI::AY38910::BC2));
|
||||
}
|
||||
|
||||
#pragma mark - TapePlayer
|
||||
|
||||
Machine::TapePlayer::TapePlayer() :
|
||||
Storage::Tape::BinaryTapePlayer(1000000) {}
|
||||
|
||||
uint8_t Machine::TapePlayer::get_next_byte(bool fast) {
|
||||
return (uint8_t)parser_.get_next_byte(get_tape(), fast);
|
||||
}
|
||||
|
||||
#pragma mark - Microdisc
|
||||
|
||||
void Machine::microdisc_did_change_paging_flags(class Microdisc *microdisc) {
|
||||
int flags = microdisc->get_paging_flags();
|
||||
if(!(flags&Microdisc::PagingFlags::BASICDisable)) {
|
||||
ram_top_ = 0xbfff;
|
||||
paged_rom_ = rom_;
|
||||
} else {
|
||||
if(flags&Microdisc::PagingFlags::MicrodscDisable) {
|
||||
ram_top_ = 0xffff;
|
||||
} else {
|
||||
ram_top_ = 0xdfff;
|
||||
paged_rom_ = microdisc_rom_.data();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Machine::wd1770_did_change_output(WD::WD1770 *wd1770) {
|
||||
set_interrupt_line();
|
||||
}
|
||||
|
||||
void Machine::set_interrupt_line() {
|
||||
set_irq_line(
|
||||
via_.get_interrupt_line() ||
|
||||
(microdisc_is_enabled_ && microdisc_.get_interrupt_request_line()));
|
||||
}
|
||||
Machine::~Machine() {}
|
||||
|
||||
@@ -11,161 +11,38 @@
|
||||
|
||||
#include "../ConfigurationTarget.hpp"
|
||||
#include "../CRTMachine.hpp"
|
||||
#include "../Typer.hpp"
|
||||
|
||||
#include "../../Processors/6502/6502.hpp"
|
||||
#include "../../Components/6522/6522.hpp"
|
||||
#include "../../Components/AY38910/AY38910.hpp"
|
||||
#include "../../Storage/Tape/Parsers/Oric.hpp"
|
||||
|
||||
#include "Video.hpp"
|
||||
#include "Microdisc.hpp"
|
||||
|
||||
#include "../../Storage/Tape/Tape.hpp"
|
||||
#include "../KeyboardMachine.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace Oric {
|
||||
|
||||
enum Key: uint16_t {
|
||||
Key3 = 0x0000 | 0x80, KeyX = 0x0000 | 0x40, Key1 = 0x0000 | 0x20,
|
||||
KeyV = 0x0000 | 0x08, Key5 = 0x0000 | 0x04, KeyN = 0x0000 | 0x02, Key7 = 0x0000 | 0x01,
|
||||
KeyD = 0x0100 | 0x80, KeyQ = 0x0100 | 0x40, KeyEscape = 0x0100 | 0x20,
|
||||
KeyF = 0x0100 | 0x08, KeyR = 0x0100 | 0x04, KeyT = 0x0100 | 0x02, KeyJ = 0x0100 | 0x01,
|
||||
KeyC = 0x0200 | 0x80, Key2 = 0x0200 | 0x40, KeyZ = 0x0200 | 0x20, KeyControl = 0x0200 | 0x10,
|
||||
Key4 = 0x0200 | 0x08, KeyB = 0x0200 | 0x04, Key6 = 0x0200 | 0x02, KeyM = 0x0200 | 0x01,
|
||||
KeyQuote = 0x0300 | 0x80, KeyBackSlash = 0x0300 | 0x40,
|
||||
KeyMinus = 0x0300 | 0x08, KeySemiColon = 0x0300 | 0x04, Key9 = 0x0300 | 0x02, KeyK = 0x0300 | 0x01,
|
||||
KeyRight = 0x0400 | 0x80, KeyDown = 0x0400 | 0x40, KeyLeft = 0x0400 | 0x20, KeyLeftShift = 0x0400 | 0x10,
|
||||
KeyUp = 0x0400 | 0x08, KeyFullStop = 0x0400 | 0x04, KeyComma = 0x0400 | 0x02, KeySpace = 0x0400 | 0x01,
|
||||
KeyOpenSquare = 0x0500 | 0x80, KeyCloseSquare = 0x0500 | 0x40, KeyDelete = 0x0500 | 0x20, KeyFunction = 0x0500 | 0x10,
|
||||
KeyP = 0x0500 | 0x08, KeyO = 0x0500 | 0x04, KeyI = 0x0500 | 0x02, KeyU = 0x0500 | 0x01,
|
||||
KeyW = 0x0600 | 0x80, KeyS = 0x0600 | 0x40, KeyA = 0x0600 | 0x20,
|
||||
KeyE = 0x0600 | 0x08, KeyG = 0x0600 | 0x04, KeyH = 0x0600 | 0x02, KeyY = 0x0600 | 0x01,
|
||||
KeyEquals = 0x0700 | 0x80, KeyReturn = 0x0700 | 0x20, KeyRightShift = 0x0700 | 0x10,
|
||||
KeyForwardSlash = 0x0700 | 0x08, Key0 = 0x0700 | 0x04, KeyL = 0x0700 | 0x02, Key8 = 0x0700 | 0x01,
|
||||
|
||||
KeyNMI = 0xfffd,
|
||||
};
|
||||
|
||||
enum ROM {
|
||||
BASIC10, BASIC11, Microdisc, Colour
|
||||
};
|
||||
|
||||
/*!
|
||||
Models an Oric 1/Atmos with or without a Microdisc.
|
||||
*/
|
||||
class Machine:
|
||||
public CPU::MOS6502::Processor<Machine>,
|
||||
public CRTMachine::Machine,
|
||||
public ConfigurationTarget::Machine,
|
||||
public MOS::MOS6522IRQDelegate::Delegate,
|
||||
public Utility::TypeRecipient,
|
||||
public Storage::Tape::BinaryTapePlayer::Delegate,
|
||||
public Microdisc::Delegate {
|
||||
|
||||
public KeyboardMachine::Machine {
|
||||
public:
|
||||
Machine();
|
||||
virtual ~Machine();
|
||||
|
||||
void set_rom(ROM rom, const std::vector<uint8_t> &data);
|
||||
void set_key_state(uint16_t key, bool isPressed);
|
||||
void clear_all_keys();
|
||||
/// Creates and returns an Oric.
|
||||
static Machine *Oric();
|
||||
|
||||
void set_use_fast_tape_hack(bool activate);
|
||||
void set_output_device(Outputs::CRT::OutputDevice output_device);
|
||||
/// Sets the contents of @c rom to @c data. Assumed to be a setup step; has no effect once a machine is running.
|
||||
virtual void set_rom(ROM rom, const std::vector<uint8_t> &data) = 0;
|
||||
|
||||
// to satisfy ConfigurationTarget::Machine
|
||||
void configure_as_target(const StaticAnalyser::Target &target);
|
||||
/// Enables or disables turbo-speed tape loading.
|
||||
virtual void set_use_fast_tape_hack(bool activate) = 0;
|
||||
|
||||
// to satisfy CPU::MOS6502::Processor
|
||||
Cycles perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value);
|
||||
void flush();
|
||||
|
||||
// to satisfy CRTMachine::Machine
|
||||
virtual void setup_output(float aspect_ratio);
|
||||
virtual void close_output();
|
||||
virtual std::shared_ptr<Outputs::CRT::CRT> get_crt();
|
||||
virtual std::shared_ptr<Outputs::Speaker> get_speaker();
|
||||
virtual void run_for(const Cycles cycles);
|
||||
|
||||
// to satisfy MOS::MOS6522IRQDelegate::Delegate
|
||||
void mos6522_did_change_interrupt_status(void *mos6522);
|
||||
|
||||
// to satisfy Storage::Tape::BinaryTapePlayer::Delegate
|
||||
void tape_did_change_input(Storage::Tape::BinaryTapePlayer *tape_player);
|
||||
|
||||
// for Utility::TypeRecipient::Delegate
|
||||
uint16_t *sequence_for_character(Utility::Typer *typer, char character);
|
||||
|
||||
// for Microdisc::Delegate
|
||||
void microdisc_did_change_paging_flags(class Microdisc *microdisc);
|
||||
void wd1770_did_change_output(WD::WD1770 *wd1770);
|
||||
|
||||
private:
|
||||
// RAM and ROM
|
||||
std::vector<uint8_t> basic11_rom_, basic10_rom_, microdisc_rom_, colour_rom_;
|
||||
uint8_t ram_[65536], rom_[16384];
|
||||
Cycles cycles_since_video_update_;
|
||||
inline void update_video();
|
||||
|
||||
// ROM bookkeeping
|
||||
bool is_using_basic11_;
|
||||
uint16_t tape_get_byte_address_, scan_keyboard_address_, tape_speed_address_;
|
||||
int keyboard_read_count_;
|
||||
|
||||
// Outputs
|
||||
std::unique_ptr<VideoOutput> video_output_;
|
||||
|
||||
// Keyboard
|
||||
class Keyboard {
|
||||
public:
|
||||
uint8_t row;
|
||||
uint8_t rows[8];
|
||||
};
|
||||
int typer_delay_;
|
||||
|
||||
// The tape
|
||||
class TapePlayer: public Storage::Tape::BinaryTapePlayer {
|
||||
public:
|
||||
TapePlayer();
|
||||
uint8_t get_next_byte(bool fast);
|
||||
|
||||
private:
|
||||
Storage::Tape::Oric::Parser parser_;
|
||||
};
|
||||
bool use_fast_tape_hack_;
|
||||
|
||||
// VIA (which owns the tape and the AY)
|
||||
class VIA: public MOS::MOS6522<VIA>, public MOS::MOS6522IRQDelegate {
|
||||
public:
|
||||
VIA();
|
||||
using MOS6522IRQDelegate::set_interrupt_status;
|
||||
|
||||
void set_control_line_output(Port port, Line line, bool value);
|
||||
void set_port_output(Port port, uint8_t value, uint8_t direction_mask);
|
||||
uint8_t get_port_input(Port port);
|
||||
inline void run_for(const Cycles cycles);
|
||||
|
||||
std::shared_ptr<GI::AY38910> ay8910;
|
||||
std::unique_ptr<TapePlayer> tape;
|
||||
std::shared_ptr<Keyboard> keyboard;
|
||||
|
||||
void flush();
|
||||
|
||||
private:
|
||||
void update_ay();
|
||||
bool ay_bdir_, ay_bc1_;
|
||||
Cycles cycles_since_ay_update_;
|
||||
};
|
||||
VIA via_;
|
||||
std::shared_ptr<Keyboard> keyboard_;
|
||||
|
||||
// the Microdisc, if in use
|
||||
class Microdisc microdisc_;
|
||||
bool microdisc_is_enabled_;
|
||||
uint16_t ram_top_;
|
||||
uint8_t *paged_rom_;
|
||||
|
||||
inline void set_interrupt_line();
|
||||
/// Sets the type of display the Oric is connected to.
|
||||
virtual void set_output_device(Outputs::CRT::OutputDevice output_device) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ VideoOutput::VideoOutput(uint8_t *memory) :
|
||||
crt_->set_composite_function_type(Outputs::CRT::CRT::CompositeSourceType::DiscreteFourSamplesPerCycle, 0.0f);
|
||||
|
||||
set_output_device(Outputs::CRT::Television);
|
||||
crt_->set_visible_area(crt_->get_rect_for_area(50, 224, 16 * 6, 40 * 6, 4.0f / 3.0f));
|
||||
crt_->set_visible_area(crt_->get_rect_for_area(53, 224, 16 * 6, 40 * 6, 4.0f / 3.0f));
|
||||
}
|
||||
|
||||
void VideoOutput::set_output_device(Outputs::CRT::OutputDevice output_device) {
|
||||
@@ -56,16 +56,16 @@ void VideoOutput::set_output_device(Outputs::CRT::OutputDevice output_device) {
|
||||
void VideoOutput::set_colour_rom(const std::vector<uint8_t> &rom) {
|
||||
for(size_t c = 0; c < 8; c++) {
|
||||
size_t index = (c << 2);
|
||||
uint16_t rom_value = (uint16_t)(((uint16_t)rom[index] << 8) | (uint16_t)rom[index+1]);
|
||||
uint16_t rom_value = static_cast<uint16_t>((static_cast<uint16_t>(rom[index]) << 8) | static_cast<uint16_t>(rom[index+1]));
|
||||
rom_value = (rom_value & 0xff00) | ((rom_value >> 4)&0x000f) | ((rom_value << 4)&0x00f0);
|
||||
colour_forms_[c] = rom_value;
|
||||
}
|
||||
|
||||
// check for big endianness and byte swap if required
|
||||
uint16_t test_value = 0x0001;
|
||||
if(*(uint8_t *)&test_value != 0x01) {
|
||||
if(*reinterpret_cast<uint8_t *>(&test_value) != 0x01) {
|
||||
for(size_t c = 0; c < 8; c++) {
|
||||
colour_forms_[c] = (uint16_t)((colour_forms_[c] >> 8) | (colour_forms_[c] << 8));
|
||||
colour_forms_[c] = static_cast<uint16_t>((colour_forms_[c] >> 8) | (colour_forms_[c] << 8));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ void VideoOutput::run_for(const Cycles cycles) {
|
||||
if(counter_ >= v_sync_start_position_ && counter_ < v_sync_end_position_) {
|
||||
// this is a sync line
|
||||
cycles_run_for = v_sync_end_position_ - counter_;
|
||||
clamp(crt_->output_sync((unsigned int)(v_sync_end_position_ - v_sync_start_position_) * 6));
|
||||
clamp(crt_->output_sync(static_cast<unsigned int>(v_sync_end_position_ - v_sync_start_position_) * 6));
|
||||
} else if(counter_ < 224*64 && h_counter < 40) {
|
||||
// this is a pixel line
|
||||
if(!h_counter) {
|
||||
@@ -97,7 +97,7 @@ void VideoOutput::run_for(const Cycles cycles) {
|
||||
paper_ = 0x0;
|
||||
use_alternative_character_set_ = use_double_height_characters_ = blink_text_ = false;
|
||||
set_character_set_base_address();
|
||||
pixel_target_ = (uint16_t *)crt_->allocate_write_area(240);
|
||||
pixel_target_ = reinterpret_cast<uint16_t *>(crt_->allocate_write_area(240));
|
||||
|
||||
if(!counter_) {
|
||||
frame_counter_++;
|
||||
@@ -133,8 +133,8 @@ void VideoOutput::run_for(const Cycles cycles) {
|
||||
if(pixel_target_) {
|
||||
uint16_t colours[2];
|
||||
if(output_device_ == Outputs::CRT::Monitor) {
|
||||
colours[0] = (uint8_t)(paper_ ^ inverse_mask);
|
||||
colours[1] = (uint8_t)(ink_ ^ inverse_mask);
|
||||
colours[0] = static_cast<uint8_t>(paper_ ^ inverse_mask);
|
||||
colours[1] = static_cast<uint8_t>(ink_ ^ inverse_mask);
|
||||
} else {
|
||||
colours[0] = colour_forms_[paper_ ^ inverse_mask];
|
||||
colours[1] = colour_forms_[ink_ ^ inverse_mask];
|
||||
@@ -202,7 +202,7 @@ void VideoOutput::run_for(const Cycles cycles) {
|
||||
cycles_run_for = 48 - h_counter;
|
||||
clamp(
|
||||
int period = (counter_ < 224*64) ? 8 : 48;
|
||||
crt_->output_blank((unsigned int)period * 6);
|
||||
crt_->output_blank(static_cast<unsigned int>(period) * 6);
|
||||
);
|
||||
} else if(h_counter < 54) {
|
||||
cycles_run_for = 54 - h_counter;
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
//
|
||||
// Typer.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 19/06/2016.
|
||||
// Copyright © 2016 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Typer_hpp
|
||||
#define Typer_hpp
|
||||
|
||||
#include <memory>
|
||||
#include "KeyboardMachine.hpp"
|
||||
#include "../ClockReceiver/ClockReceiver.hpp"
|
||||
|
||||
namespace Utility {
|
||||
|
||||
class Typer {
|
||||
public:
|
||||
class Delegate: public KeyboardMachine::Machine {
|
||||
public:
|
||||
virtual bool typer_set_next_character(Typer *typer, char character, int phase);
|
||||
virtual void typer_reset(Typer *typer) = 0;
|
||||
|
||||
virtual uint16_t *sequence_for_character(Typer *typer, char character);
|
||||
|
||||
typedef uint16_t KeySequence[16];
|
||||
uint16_t *table_lookup_sequence_for_character(KeySequence *sequences, size_t length, char character);
|
||||
|
||||
const uint16_t EndSequence = 0xffff;
|
||||
const uint16_t NotMapped = 0xfffe;
|
||||
};
|
||||
|
||||
Typer(const char *string, HalfCycles delay, HalfCycles frequency, Delegate *delegate);
|
||||
~Typer();
|
||||
void run_for(const HalfCycles duration);
|
||||
bool type_next_character();
|
||||
|
||||
const char BeginString = 0x02; // i.e. ASCII start of text
|
||||
const char EndString = 0x03; // i.e. ASCII end of text
|
||||
|
||||
private:
|
||||
char *string_;
|
||||
HalfCycles frequency_;
|
||||
HalfCycles counter_;
|
||||
int phase_;
|
||||
Delegate *delegate_;
|
||||
size_t string_pointer_;
|
||||
};
|
||||
|
||||
class TypeRecipient: public Typer::Delegate {
|
||||
public:
|
||||
void set_typer_for_string(const char *string) {
|
||||
typer_.reset(new Typer(string, get_typer_delay(), get_typer_frequency(), this));
|
||||
}
|
||||
|
||||
void typer_reset(Typer *typer) {
|
||||
clear_all_keys();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual HalfCycles get_typer_delay() { return HalfCycles(0); }
|
||||
virtual HalfCycles get_typer_frequency() { return HalfCycles(0); }
|
||||
std::unique_ptr<Typer> typer_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* Typer_hpp */
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <cstdlib>
|
||||
|
||||
void Memory::Fuzz(uint8_t *buffer, size_t size) {
|
||||
unsigned int divider = ((unsigned int)RAND_MAX + 1) / 256;
|
||||
unsigned int divider = (static_cast<unsigned int>(RAND_MAX) + 1) / 256;
|
||||
unsigned int shift = 1, value = 1;
|
||||
while(value < divider) {
|
||||
value <<= 1;
|
||||
@@ -19,7 +19,7 @@ void Memory::Fuzz(uint8_t *buffer, size_t size) {
|
||||
}
|
||||
|
||||
for(size_t c = 0; c < size; c++) {
|
||||
buffer[c] = (uint8_t)(std::rand() >> shift);
|
||||
buffer[c] = static_cast<uint8_t>(std::rand() >> shift);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,13 @@
|
||||
|
||||
using namespace Utility;
|
||||
|
||||
Typer::Typer(const char *string, HalfCycles delay, HalfCycles frequency, Delegate *delegate) :
|
||||
counter_(-delay), frequency_(frequency), string_pointer_(0), delegate_(delegate), phase_(0) {
|
||||
Typer::Typer(const char *string, HalfCycles delay, HalfCycles frequency, std::unique_ptr<CharacterMapper> character_mapper, Delegate *delegate) :
|
||||
counter_(-delay),
|
||||
frequency_(frequency),
|
||||
string_pointer_(0),
|
||||
delegate_(delegate),
|
||||
phase_(0),
|
||||
character_mapper_(std::move(character_mapper)) {
|
||||
size_t string_size = strlen(string) + 3;
|
||||
string_ = (char *)malloc(string_size);
|
||||
snprintf(string_, string_size, "%c%s%c", Typer::BeginString, string, Typer::EndString);
|
||||
@@ -36,10 +41,26 @@ void Typer::run_for(const HalfCycles duration) {
|
||||
}
|
||||
}
|
||||
|
||||
bool Typer::try_type_next_character() {
|
||||
uint16_t *sequence = character_mapper_->sequence_for_character(string_[string_pointer_]);
|
||||
|
||||
if(!sequence || sequence[0] == KeyboardMachine::Machine::KeyNotMapped) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!phase_) delegate_->clear_all_keys();
|
||||
else {
|
||||
delegate_->set_key_state(sequence[phase_ - 1], true);
|
||||
return sequence[phase_] != KeyboardMachine::Machine::KeyEndSequence;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Typer::type_next_character() {
|
||||
if(string_ == nullptr) return false;
|
||||
|
||||
if(delegate_->typer_set_next_character(this, string_[string_pointer_], phase_)) {
|
||||
if(!try_type_next_character()) {
|
||||
phase_ = 0;
|
||||
if(!string_[string_pointer_]) {
|
||||
free(string_);
|
||||
@@ -59,28 +80,11 @@ Typer::~Typer() {
|
||||
free(string_);
|
||||
}
|
||||
|
||||
#pragma mark - Delegate
|
||||
#pragma mark - Character mapper
|
||||
|
||||
bool Typer::Delegate::typer_set_next_character(Utility::Typer *typer, char character, int phase) {
|
||||
uint16_t *sequence = sequence_for_character(typer, character);
|
||||
if(!sequence) return true;
|
||||
|
||||
if(!phase) clear_all_keys();
|
||||
else {
|
||||
set_key_state(sequence[phase - 1], true);
|
||||
return sequence[phase] == Typer::Delegate::EndSequence;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t *Typer::Delegate::sequence_for_character(Typer *typer, char character) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uint16_t *Typer::Delegate::table_lookup_sequence_for_character(KeySequence *sequences, size_t length, char character) {
|
||||
size_t ucharacter = (size_t)((unsigned char)character);
|
||||
uint16_t *CharacterMapper::table_lookup_sequence_for_character(KeySequence *sequences, size_t length, char character) {
|
||||
size_t ucharacter = static_cast<size_t>((unsigned char)character);
|
||||
if(ucharacter > (length / sizeof(KeySequence))) return nullptr;
|
||||
if(sequences[ucharacter][0] == NotMapped) return nullptr;
|
||||
if(sequences[ucharacter][0] == KeyboardMachine::Machine::KeyNotMapped) return nullptr;
|
||||
return sequences[ucharacter];
|
||||
}
|
||||
120
Machines/Utility/Typer.hpp
Normal file
120
Machines/Utility/Typer.hpp
Normal file
@@ -0,0 +1,120 @@
|
||||
//
|
||||
// Typer.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 19/06/2016.
|
||||
// Copyright © 2016 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Typer_hpp
|
||||
#define Typer_hpp
|
||||
|
||||
#include <memory>
|
||||
#include "../KeyboardMachine.hpp"
|
||||
#include "../../ClockReceiver/ClockReceiver.hpp"
|
||||
|
||||
namespace Utility {
|
||||
|
||||
/*!
|
||||
An interface that provides a mapping from logical characters to the sequence of keys
|
||||
necessary to type that character on a given machine.
|
||||
*/
|
||||
class CharacterMapper {
|
||||
public:
|
||||
/// @returns The EndSequence-terminated sequence of keys that would cause @c character to be typed.
|
||||
virtual uint16_t *sequence_for_character(char character) = 0;
|
||||
|
||||
protected:
|
||||
typedef uint16_t KeySequence[16];
|
||||
|
||||
/*!
|
||||
Provided in the base class as a convenience: given the lookup table of key sequences @c sequences,
|
||||
with @c length entries, returns the sequence for character @c character if it exists; otherwise
|
||||
returns @c nullptr.
|
||||
*/
|
||||
uint16_t *table_lookup_sequence_for_character(KeySequence *sequences, size_t length, char character);
|
||||
};
|
||||
|
||||
/*!
|
||||
Provides a stateful mechanism for typing a sequence of characters. Each character is mapped to a key sequence
|
||||
by a character mapper. That key sequence is then replayed to a delegate.
|
||||
|
||||
Being given a delay and frequency at construction, the run_for interface can be used to produce time-based
|
||||
typing. Alternatively, an owner may decline to use run_for and simply call type_next_character each time a
|
||||
fresh key transition is ready to be consumed.
|
||||
*/
|
||||
class Typer {
|
||||
public:
|
||||
class Delegate: public KeyboardMachine::Machine {
|
||||
public:
|
||||
virtual void typer_reset(Typer *typer) = 0;
|
||||
};
|
||||
|
||||
Typer(const char *string, HalfCycles delay, HalfCycles frequency, std::unique_ptr<CharacterMapper> character_mapper, Delegate *delegate);
|
||||
~Typer();
|
||||
|
||||
void run_for(const HalfCycles duration);
|
||||
bool type_next_character();
|
||||
|
||||
bool is_completed();
|
||||
|
||||
const char BeginString = 0x02; // i.e. ASCII start of text
|
||||
const char EndString = 0x03; // i.e. ASCII end of text
|
||||
|
||||
private:
|
||||
char *string_;
|
||||
size_t string_pointer_;
|
||||
|
||||
HalfCycles frequency_;
|
||||
HalfCycles counter_;
|
||||
int phase_;
|
||||
|
||||
Delegate *delegate_;
|
||||
std::unique_ptr<CharacterMapper> character_mapper_;
|
||||
|
||||
bool try_type_next_character();
|
||||
};
|
||||
|
||||
/*!
|
||||
Provides a default base class for type recipients: classes that want to attach a single typer at a time and
|
||||
which may or may not want to nominate an initial delay and typing frequency.
|
||||
*/
|
||||
class TypeRecipient: public Typer::Delegate {
|
||||
public:
|
||||
/// Attaches a typer to this class that will type @c string using @c character_mapper as a source.
|
||||
void set_typer_for_string(const char *string, std::unique_ptr<CharacterMapper> character_mapper) {
|
||||
typer_.reset(new Typer(string, get_typer_delay(), get_typer_frequency(), std::move(character_mapper), this));
|
||||
}
|
||||
|
||||
/*!
|
||||
Provided as a hook for subclasses to implement so that external callers can install a typer
|
||||
without needing inside knowledge as to where the character mapper comes from.
|
||||
*/
|
||||
virtual void set_typer_for_string(const char *string) = 0;
|
||||
|
||||
/*!
|
||||
Provided in order to conform to that part of the Typer::Delegate interface that goes above and
|
||||
beyond KeyboardMachine::Machine; responds to the end of typing by clearing all keys.
|
||||
*/
|
||||
void typer_reset(Typer *typer) {
|
||||
clear_all_keys();
|
||||
|
||||
// It's unsafe to deallocate typer right now, since it is the caller, but also it has a small
|
||||
// memory footprint and it's desireable not to imply that the subclass need call it any more.
|
||||
// So shuffle it off into a siding.
|
||||
previous_typer_ = std::move(typer_);
|
||||
typer_ = nullptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual HalfCycles get_typer_delay() { return HalfCycles(0); }
|
||||
virtual HalfCycles get_typer_frequency() { return HalfCycles(0); }
|
||||
std::unique_ptr<Typer> typer_;
|
||||
|
||||
private:
|
||||
std::unique_ptr<Typer> previous_typer_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* Typer_hpp */
|
||||
@@ -1,17 +1,44 @@
|
||||
//
|
||||
// Typer.cpp
|
||||
// Keyboard.cpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 09/07/2017.
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#include "ZX8081.hpp"
|
||||
#include "Keyboard.hpp"
|
||||
|
||||
uint16_t *ZX8081::Machine::sequence_for_character(Utility::Typer *typer, char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, EndSequence}
|
||||
#define SHIFT(...) {KeyShift, __VA_ARGS__, EndSequence}
|
||||
#define X {NotMapped}
|
||||
using namespace ZX8081;
|
||||
|
||||
uint16_t KeyboardMapper::mapped_key_for_key(Inputs::Keyboard::Key key) {
|
||||
#define BIND(source, dest) case Inputs::Keyboard::Key::source: return ZX8081::dest
|
||||
switch(key) {
|
||||
default: break;
|
||||
|
||||
BIND(k0, Key0); BIND(k1, Key1); BIND(k2, Key2); BIND(k3, Key3); BIND(k4, Key4);
|
||||
BIND(k5, Key5); BIND(k6, Key6); BIND(k7, Key7); BIND(k8, Key8); BIND(k9, Key9);
|
||||
BIND(Q, KeyQ); BIND(W, KeyW); BIND(E, KeyE); BIND(R, KeyR); BIND(T, KeyT);
|
||||
BIND(Y, KeyY); BIND(U, KeyU); BIND(I, KeyI); BIND(O, KeyO); BIND(P, KeyP);
|
||||
BIND(A, KeyA); BIND(S, KeyS); BIND(D, KeyD); BIND(F, KeyF); BIND(G, KeyG);
|
||||
BIND(H, KeyH); BIND(J, KeyJ); BIND(K, KeyK); BIND(L, KeyL);
|
||||
BIND(Z, KeyZ); BIND(X, KeyX); BIND(C, KeyC); BIND(V, KeyV);
|
||||
BIND(B, KeyB); BIND(N, KeyN); BIND(M, KeyM);
|
||||
|
||||
BIND(LeftShift, KeyShift); BIND(RightShift, KeyShift);
|
||||
BIND(FullStop, KeyDot);
|
||||
BIND(Enter, KeyEnter);
|
||||
BIND(Space, KeySpace);
|
||||
}
|
||||
#undef BIND
|
||||
return KeyboardMachine::Machine::KeyNotMapped;
|
||||
}
|
||||
|
||||
CharacterMapper::CharacterMapper(bool is_zx81) : is_zx81_(is_zx81) {}
|
||||
|
||||
uint16_t *CharacterMapper::sequence_for_character(char character) {
|
||||
#define KEYS(...) {__VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define SHIFT(...) {KeyShift, __VA_ARGS__, KeyboardMachine::Machine::KeyEndSequence}
|
||||
#define X {KeyboardMachine::Machine::KeyNotMapped}
|
||||
static KeySequence zx81_key_sequences[] = {
|
||||
/* NUL */ X, /* SOH */ X,
|
||||
/* STX */ X, /* ETX */ X,
|
||||
43
Machines/ZX8081/Keyboard.hpp
Normal file
43
Machines/ZX8081/Keyboard.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// Keyboard.hpp
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 10/10/2017.
|
||||
// Copyright © 2017 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef Machines_ZX8081_Keyboard_hpp
|
||||
#define Machines_ZX8081_Keyboard_hpp
|
||||
|
||||
#include "../KeyboardMachine.hpp"
|
||||
#include "../Utility/Typer.hpp"
|
||||
|
||||
namespace ZX8081 {
|
||||
|
||||
enum Key: uint16_t {
|
||||
KeyShift = 0x0000 | 0x01, KeyZ = 0x0000 | 0x02, KeyX = 0x0000 | 0x04, KeyC = 0x0000 | 0x08, KeyV = 0x0000 | 0x10,
|
||||
KeyA = 0x0100 | 0x01, KeyS = 0x0100 | 0x02, KeyD = 0x0100 | 0x04, KeyF = 0x0100 | 0x08, KeyG = 0x0100 | 0x10,
|
||||
KeyQ = 0x0200 | 0x01, KeyW = 0x0200 | 0x02, KeyE = 0x0200 | 0x04, KeyR = 0x0200 | 0x08, KeyT = 0x0200 | 0x10,
|
||||
Key1 = 0x0300 | 0x01, Key2 = 0x0300 | 0x02, Key3 = 0x0300 | 0x04, Key4 = 0x0300 | 0x08, Key5 = 0x0300 | 0x10,
|
||||
Key0 = 0x0400 | 0x01, Key9 = 0x0400 | 0x02, Key8 = 0x0400 | 0x04, Key7 = 0x0400 | 0x08, Key6 = 0x0400 | 0x10,
|
||||
KeyP = 0x0500 | 0x01, KeyO = 0x0500 | 0x02, KeyI = 0x0500 | 0x04, KeyU = 0x0500 | 0x08, KeyY = 0x0500 | 0x10,
|
||||
KeyEnter = 0x0600 | 0x01, KeyL = 0x0600 | 0x02, KeyK = 0x0600 | 0x04, KeyJ = 0x0600 | 0x08, KeyH = 0x0600 | 0x10,
|
||||
KeySpace = 0x0700 | 0x01, KeyDot = 0x0700 | 0x02, KeyM = 0x0700 | 0x04, KeyN = 0x0700 | 0x08, KeyB = 0x0700 | 0x10,
|
||||
};
|
||||
|
||||
struct KeyboardMapper: public KeyboardMachine::Machine::KeyboardMapper {
|
||||
uint16_t mapped_key_for_key(Inputs::Keyboard::Key key);
|
||||
};
|
||||
|
||||
class CharacterMapper: public ::Utility::CharacterMapper {
|
||||
public:
|
||||
CharacterMapper(bool is_zx81);
|
||||
uint16_t *sequence_for_character(char character);
|
||||
|
||||
private:
|
||||
bool is_zx81_;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif /* KeyboardMapper_hpp */
|
||||
@@ -31,7 +31,7 @@ Video::Video() :
|
||||
|
||||
void Video::run_for(const HalfCycles half_cycles) {
|
||||
// Just keep a running total of the amount of time that remains owed to the CRT.
|
||||
cycles_since_update_ += (unsigned int)half_cycles.as_int();
|
||||
cycles_since_update_ += static_cast<unsigned int>(half_cycles.as_int());
|
||||
}
|
||||
|
||||
void Video::flush() {
|
||||
@@ -48,7 +48,7 @@ void Video::flush(bool next_sync) {
|
||||
if(line_data_) {
|
||||
// If there is output data queued, output it either if it's being interrupted by
|
||||
// sync, or if we're past its end anyway. Otherwise let it be.
|
||||
unsigned int data_length = (unsigned int)(line_data_pointer_ - line_data_);
|
||||
unsigned int data_length = static_cast<unsigned int>(line_data_pointer_ - line_data_);
|
||||
if(data_length < cycles_since_update_ || next_sync) {
|
||||
unsigned int output_length = std::min(data_length, cycles_since_update_);
|
||||
crt_->output_data(output_length, 1);
|
||||
@@ -58,7 +58,7 @@ void Video::flush(bool next_sync) {
|
||||
}
|
||||
|
||||
// Any pending pixels being dealt with, pad with the white level.
|
||||
uint8_t *colour_pointer = (uint8_t *)crt_->allocate_write_area(1);
|
||||
uint8_t *colour_pointer = static_cast<uint8_t *>(crt_->allocate_write_area(1));
|
||||
if(colour_pointer) *colour_pointer = 0xff;
|
||||
crt_->output_level(cycles_since_update_);
|
||||
}
|
||||
|
||||
@@ -8,291 +8,398 @@
|
||||
|
||||
#include "ZX8081.hpp"
|
||||
|
||||
#include "../MemoryFuzzer.hpp"
|
||||
#include "../../Processors/Z80/Z80.hpp"
|
||||
#include "../../Storage/Tape/Tape.hpp"
|
||||
#include "../../Storage/Tape/Parsers/ZX8081.hpp"
|
||||
|
||||
using namespace ZX8081;
|
||||
#include "../../ClockReceiver/ForceInline.hpp"
|
||||
|
||||
#include "../Utility/MemoryFuzzer.hpp"
|
||||
#include "../Utility/Typer.hpp"
|
||||
|
||||
#include "Keyboard.hpp"
|
||||
#include "Video.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace {
|
||||
// The clock rate is 3.25Mhz.
|
||||
const unsigned int ZX8081ClockRate = 3250000;
|
||||
}
|
||||
|
||||
Machine::Machine() :
|
||||
vsync_(false),
|
||||
hsync_(false),
|
||||
nmi_is_enabled_(false),
|
||||
tape_player_(ZX8081ClockRate),
|
||||
use_fast_tape_hack_(false),
|
||||
tape_advance_delay_(0),
|
||||
has_latched_video_byte_(false) {
|
||||
set_clock_rate(ZX8081ClockRate);
|
||||
clear_all_keys();
|
||||
}
|
||||
namespace ZX8081 {
|
||||
|
||||
HalfCycles Machine::perform_machine_cycle(const CPU::Z80::PartialMachineCycle &cycle) {
|
||||
HalfCycles previous_counter = horizontal_counter_;
|
||||
horizontal_counter_ += cycle.length;
|
||||
|
||||
if(previous_counter < vsync_start_ && horizontal_counter_ >= vsync_start_) {
|
||||
video_->run_for(vsync_start_ - previous_counter);
|
||||
set_hsync(true);
|
||||
line_counter_ = (line_counter_ + 1) & 7;
|
||||
if(nmi_is_enabled_) {
|
||||
set_non_maskable_interrupt_line(true);
|
||||
template<bool is_zx81> class ConcreteMachine:
|
||||
public Utility::TypeRecipient,
|
||||
public CPU::Z80::BusHandler,
|
||||
public Machine {
|
||||
public:
|
||||
ConcreteMachine() :
|
||||
z80_(*this),
|
||||
vsync_(false),
|
||||
hsync_(false),
|
||||
nmi_is_enabled_(false),
|
||||
tape_player_(ZX8081ClockRate),
|
||||
use_fast_tape_hack_(false),
|
||||
tape_advance_delay_(0),
|
||||
has_latched_video_byte_(false) {
|
||||
set_clock_rate(ZX8081ClockRate);
|
||||
clear_all_keys();
|
||||
}
|
||||
video_->run_for(horizontal_counter_ - vsync_start_);
|
||||
} else if(previous_counter < vsync_end_ && horizontal_counter_ >= vsync_end_) {
|
||||
video_->run_for(vsync_end_ - previous_counter);
|
||||
set_hsync(false);
|
||||
if(nmi_is_enabled_) {
|
||||
set_non_maskable_interrupt_line(false);
|
||||
set_wait_line(false);
|
||||
}
|
||||
video_->run_for(horizontal_counter_ - vsync_end_);
|
||||
} else {
|
||||
video_->run_for(cycle.length);
|
||||
}
|
||||
|
||||
if(is_zx81_) horizontal_counter_ %= HalfCycles(Cycles(207));
|
||||
if(!tape_advance_delay_) {
|
||||
tape_player_.run_for(cycle.length);
|
||||
} else {
|
||||
tape_advance_delay_ = std::max(tape_advance_delay_ - cycle.length, HalfCycles(0));
|
||||
}
|
||||
forceinline HalfCycles perform_machine_cycle(const CPU::Z80::PartialMachineCycle &cycle) {
|
||||
HalfCycles previous_counter = horizontal_counter_;
|
||||
horizontal_counter_ += cycle.length;
|
||||
|
||||
if(nmi_is_enabled_ && !get_halt_line() && get_non_maskable_interrupt_line()) {
|
||||
set_wait_line(true);
|
||||
}
|
||||
|
||||
if(!cycle.is_terminal()) {
|
||||
return Cycles(0);
|
||||
}
|
||||
|
||||
uint16_t address = cycle.address ? *cycle.address : 0;
|
||||
bool is_opcode_read = false;
|
||||
switch(cycle.operation) {
|
||||
case CPU::Z80::PartialMachineCycle::Output:
|
||||
if(!(address & 2)) nmi_is_enabled_ = false;
|
||||
if(!(address & 1)) nmi_is_enabled_ = is_zx81_;
|
||||
if(!nmi_is_enabled_) {
|
||||
// Line counter reset is held low while vsync is active; simulate that lazily by performing
|
||||
// an instant reset upon the transition from active to inactive.
|
||||
if(vsync_) line_counter_ = 0;
|
||||
set_vsync(false);
|
||||
}
|
||||
break;
|
||||
|
||||
case CPU::Z80::PartialMachineCycle::Input: {
|
||||
uint8_t value = 0xff;
|
||||
if(!(address&1)) {
|
||||
if(!nmi_is_enabled_) set_vsync(true);
|
||||
|
||||
uint16_t mask = 0x100;
|
||||
for(int c = 0; c < 8; c++) {
|
||||
if(!(address & mask)) value &= key_states_[c];
|
||||
mask <<= 1;
|
||||
if(previous_counter < vsync_start_ && horizontal_counter_ >= vsync_start_) {
|
||||
video_->run_for(vsync_start_ - previous_counter);
|
||||
set_hsync(true);
|
||||
line_counter_ = (line_counter_ + 1) & 7;
|
||||
if(nmi_is_enabled_) {
|
||||
z80_.set_non_maskable_interrupt_line(true);
|
||||
}
|
||||
|
||||
value &= ~(tape_player_.get_input() ? 0x00 : 0x80);
|
||||
}
|
||||
*cycle.value = value;
|
||||
} break;
|
||||
|
||||
case CPU::Z80::PartialMachineCycle::Interrupt:
|
||||
// resetting event is M1 and IOREQ both simultaneously having leading edges;
|
||||
// that happens 2 cycles before the end of INTACK. So the timer was reset and
|
||||
// now has advanced twice.
|
||||
horizontal_counter_ = HalfCycles(2);
|
||||
|
||||
*cycle.value = 0xff;
|
||||
break;
|
||||
|
||||
case CPU::Z80::PartialMachineCycle::Refresh:
|
||||
// The ZX80 and 81 signal an interrupt while refresh is active and bit 6 of the refresh
|
||||
// address is low. The Z80 signals a refresh, providing the refresh address during the
|
||||
// final two cycles of an opcode fetch. Therefore communicate a transient signalling
|
||||
// of the IRQ line if necessary.
|
||||
if(!(address & 0x40)) {
|
||||
set_interrupt_line(true, Cycles(-2));
|
||||
set_interrupt_line(false);
|
||||
}
|
||||
if(has_latched_video_byte_) {
|
||||
size_t char_address = (size_t)((address & 0xfe00) | ((latched_video_byte_ & 0x3f) << 3) | line_counter_);
|
||||
uint8_t mask = (latched_video_byte_ & 0x80) ? 0x00 : 0xff;
|
||||
if(char_address < ram_base_) {
|
||||
latched_video_byte_ = rom_[char_address & rom_mask_] ^ mask;
|
||||
} else {
|
||||
latched_video_byte_ = ram_[address & ram_mask_] ^ mask;
|
||||
video_->run_for(horizontal_counter_ - vsync_start_);
|
||||
} else if(previous_counter < vsync_end_ && horizontal_counter_ >= vsync_end_) {
|
||||
video_->run_for(vsync_end_ - previous_counter);
|
||||
set_hsync(false);
|
||||
if(nmi_is_enabled_) {
|
||||
z80_.set_non_maskable_interrupt_line(false);
|
||||
z80_.set_wait_line(false);
|
||||
}
|
||||
|
||||
video_->output_byte(latched_video_byte_);
|
||||
has_latched_video_byte_ = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case CPU::Z80::PartialMachineCycle::ReadOpcode:
|
||||
// Check for use of the fast tape hack.
|
||||
if(use_fast_tape_hack_ && address == tape_trap_address_ && tape_player_.has_tape()) {
|
||||
uint64_t prior_offset = tape_player_.get_tape()->get_offset();
|
||||
int next_byte = parser_.get_next_byte(tape_player_.get_tape());
|
||||
if(next_byte != -1) {
|
||||
uint16_t hl = get_value_of_register(CPU::Z80::Register::HL);
|
||||
ram_[hl & ram_mask_] = (uint8_t)next_byte;
|
||||
*cycle.value = 0x00;
|
||||
set_value_of_register(CPU::Z80::Register::ProgramCounter, tape_return_address_ - 1);
|
||||
|
||||
// Assume that having read one byte quickly, we're probably going to be asked to read
|
||||
// another shortly. Therefore, temporarily disable the tape motor for 1000 cycles in order
|
||||
// to avoid fighting with real time. This is a stop-gap fix.
|
||||
tape_advance_delay_ = 1000;
|
||||
return 0;
|
||||
} else {
|
||||
tape_player_.get_tape()->set_offset(prior_offset);
|
||||
}
|
||||
}
|
||||
|
||||
// Check for automatic tape control.
|
||||
if(use_automatic_tape_motor_control_) {
|
||||
tape_player_.set_motor_control((address >= automatic_tape_motor_start_address_) && (address < automatic_tape_motor_end_address_));
|
||||
}
|
||||
is_opcode_read = true;
|
||||
|
||||
case CPU::Z80::PartialMachineCycle::Read:
|
||||
if(address < ram_base_) {
|
||||
*cycle.value = rom_[address & rom_mask_];
|
||||
video_->run_for(horizontal_counter_ - vsync_end_);
|
||||
} else {
|
||||
uint8_t value = ram_[address & ram_mask_];
|
||||
|
||||
// If this is an M1 cycle reading from above the 32kb mark and HALT is not
|
||||
// currently active, latch for video output and return a NOP. Otherwise,
|
||||
// just return the value as read.
|
||||
if(is_opcode_read && address&0x8000 && !(value & 0x40) && !get_halt_line()) {
|
||||
latched_video_byte_ = value;
|
||||
has_latched_video_byte_ = true;
|
||||
*cycle.value = 0;
|
||||
} else *cycle.value = value;
|
||||
video_->run_for(cycle.length);
|
||||
}
|
||||
break;
|
||||
|
||||
case CPU::Z80::PartialMachineCycle::Write:
|
||||
if(address >= ram_base_) {
|
||||
ram_[address & ram_mask_] = *cycle.value;
|
||||
if(is_zx81_) horizontal_counter_ %= HalfCycles(Cycles(207));
|
||||
if(!tape_advance_delay_) {
|
||||
tape_player_.run_for(cycle.length);
|
||||
} else {
|
||||
tape_advance_delay_ = std::max(tape_advance_delay_ - cycle.length, HalfCycles(0));
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
if(nmi_is_enabled_ && !z80_.get_halt_line() && z80_.get_non_maskable_interrupt_line()) {
|
||||
z80_.set_wait_line(true);
|
||||
}
|
||||
|
||||
if(typer_) typer_->run_for(cycle.length);
|
||||
if(!cycle.is_terminal()) {
|
||||
return Cycles(0);
|
||||
}
|
||||
|
||||
return HalfCycles(0);
|
||||
}
|
||||
uint16_t address = cycle.address ? *cycle.address : 0;
|
||||
bool is_opcode_read = false;
|
||||
switch(cycle.operation) {
|
||||
case CPU::Z80::PartialMachineCycle::Output:
|
||||
if(!(address & 2)) nmi_is_enabled_ = false;
|
||||
if(!(address & 1)) nmi_is_enabled_ = is_zx81_;
|
||||
if(!nmi_is_enabled_) {
|
||||
// Line counter reset is held low while vsync is active; simulate that lazily by performing
|
||||
// an instant reset upon the transition from active to inactive.
|
||||
if(vsync_) line_counter_ = 0;
|
||||
set_vsync(false);
|
||||
}
|
||||
break;
|
||||
|
||||
void Machine::flush() {
|
||||
video_->flush();
|
||||
}
|
||||
case CPU::Z80::PartialMachineCycle::Input: {
|
||||
uint8_t value = 0xff;
|
||||
if(!(address&1)) {
|
||||
if(!nmi_is_enabled_) set_vsync(true);
|
||||
|
||||
void Machine::setup_output(float aspect_ratio) {
|
||||
video_.reset(new Video);
|
||||
}
|
||||
uint16_t mask = 0x100;
|
||||
for(int c = 0; c < 8; c++) {
|
||||
if(!(address & mask)) value &= key_states_[c];
|
||||
mask <<= 1;
|
||||
}
|
||||
|
||||
void Machine::close_output() {
|
||||
video_.reset();
|
||||
}
|
||||
value &= ~(tape_player_.get_input() ? 0x00 : 0x80);
|
||||
}
|
||||
*cycle.value = value;
|
||||
} break;
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> Machine::get_crt() {
|
||||
return video_->get_crt();
|
||||
}
|
||||
case CPU::Z80::PartialMachineCycle::Interrupt:
|
||||
// resetting event is M1 and IOREQ both simultaneously having leading edges;
|
||||
// that happens 2 cycles before the end of INTACK. So the timer was reset and
|
||||
// now has advanced twice.
|
||||
horizontal_counter_ = HalfCycles(2);
|
||||
|
||||
std::shared_ptr<Outputs::Speaker> Machine::get_speaker() {
|
||||
return nullptr;
|
||||
}
|
||||
*cycle.value = 0xff;
|
||||
break;
|
||||
|
||||
void Machine::run_for(const Cycles cycles) {
|
||||
CPU::Z80::Processor<Machine>::run_for(cycles);
|
||||
}
|
||||
case CPU::Z80::PartialMachineCycle::Refresh:
|
||||
// The ZX80 and 81 signal an interrupt while refresh is active and bit 6 of the refresh
|
||||
// address is low. The Z80 signals a refresh, providing the refresh address during the
|
||||
// final two cycles of an opcode fetch. Therefore communicate a transient signalling
|
||||
// of the IRQ line if necessary.
|
||||
if(!(address & 0x40)) {
|
||||
z80_.set_interrupt_line(true, Cycles(-2));
|
||||
z80_.set_interrupt_line(false);
|
||||
}
|
||||
if(has_latched_video_byte_) {
|
||||
size_t char_address = static_cast<size_t>((address & 0xfe00) | ((latched_video_byte_ & 0x3f) << 3) | line_counter_);
|
||||
uint8_t mask = (latched_video_byte_ & 0x80) ? 0x00 : 0xff;
|
||||
if(char_address < ram_base_) {
|
||||
latched_video_byte_ = rom_[char_address & rom_mask_] ^ mask;
|
||||
} else {
|
||||
latched_video_byte_ = ram_[address & ram_mask_] ^ mask;
|
||||
}
|
||||
|
||||
void Machine::configure_as_target(const StaticAnalyser::Target &target) {
|
||||
is_zx81_ = target.zx8081.isZX81;
|
||||
if(is_zx81_) {
|
||||
rom_ = zx81_rom_;
|
||||
tape_trap_address_ = 0x37c;
|
||||
tape_return_address_ = 0x380;
|
||||
vsync_start_ = HalfCycles(32);
|
||||
vsync_end_ = HalfCycles(64);
|
||||
automatic_tape_motor_start_address_ = 0x0340;
|
||||
automatic_tape_motor_end_address_ = 0x03c3;
|
||||
} else {
|
||||
rom_ = zx80_rom_;
|
||||
tape_trap_address_ = 0x220;
|
||||
tape_return_address_ = 0x248;
|
||||
vsync_start_ = HalfCycles(26);
|
||||
vsync_end_ = HalfCycles(66);
|
||||
automatic_tape_motor_start_address_ = 0x0206;
|
||||
automatic_tape_motor_end_address_ = 0x024d;
|
||||
}
|
||||
rom_mask_ = (uint16_t)(rom_.size() - 1);
|
||||
video_->output_byte(latched_video_byte_);
|
||||
has_latched_video_byte_ = false;
|
||||
}
|
||||
break;
|
||||
|
||||
switch(target.zx8081.memory_model) {
|
||||
case StaticAnalyser::ZX8081MemoryModel::Unexpanded:
|
||||
ram_.resize(1024);
|
||||
ram_base_ = 16384;
|
||||
ram_mask_ = 1023;
|
||||
break;
|
||||
case StaticAnalyser::ZX8081MemoryModel::SixteenKB:
|
||||
ram_.resize(16384);
|
||||
ram_base_ = 16384;
|
||||
ram_mask_ = 16383;
|
||||
break;
|
||||
case StaticAnalyser::ZX8081MemoryModel::SixtyFourKB:
|
||||
ram_.resize(65536);
|
||||
ram_base_ = 8192;
|
||||
ram_mask_ = 65535;
|
||||
break;
|
||||
}
|
||||
Memory::Fuzz(ram_);
|
||||
case CPU::Z80::PartialMachineCycle::ReadOpcode:
|
||||
// Check for use of the fast tape hack.
|
||||
if(use_fast_tape_hack_ && address == tape_trap_address_ && tape_player_.has_tape()) {
|
||||
uint64_t prior_offset = tape_player_.get_tape()->get_offset();
|
||||
int next_byte = parser_.get_next_byte(tape_player_.get_tape());
|
||||
if(next_byte != -1) {
|
||||
uint16_t hl = z80_.get_value_of_register(CPU::Z80::Register::HL);
|
||||
ram_[hl & ram_mask_] = static_cast<uint8_t>(next_byte);
|
||||
*cycle.value = 0x00;
|
||||
z80_.set_value_of_register(CPU::Z80::Register::ProgramCounter, tape_return_address_ - 1);
|
||||
|
||||
if(target.tapes.size()) {
|
||||
tape_player_.set_tape(target.tapes.front());
|
||||
}
|
||||
// Assume that having read one byte quickly, we're probably going to be asked to read
|
||||
// another shortly. Therefore, temporarily disable the tape motor for 1000 cycles in order
|
||||
// to avoid fighting with real time. This is a stop-gap fix.
|
||||
tape_advance_delay_ = 1000;
|
||||
return 0;
|
||||
} else {
|
||||
tape_player_.get_tape()->set_offset(prior_offset);
|
||||
}
|
||||
}
|
||||
|
||||
if(target.loadingCommand.length()) {
|
||||
set_typer_for_string(target.loadingCommand.c_str());
|
||||
}
|
||||
}
|
||||
// Check for automatic tape control.
|
||||
if(use_automatic_tape_motor_control_) {
|
||||
tape_player_.set_motor_control((address >= automatic_tape_motor_start_address_) && (address < automatic_tape_motor_end_address_));
|
||||
}
|
||||
is_opcode_read = true;
|
||||
|
||||
void Machine::set_rom(ROMType type, std::vector<uint8_t> data) {
|
||||
switch(type) {
|
||||
case ZX80: zx80_rom_ = data; break;
|
||||
case ZX81: zx81_rom_ = data; break;
|
||||
}
|
||||
}
|
||||
case CPU::Z80::PartialMachineCycle::Read:
|
||||
if(address < ram_base_) {
|
||||
*cycle.value = rom_[address & rom_mask_];
|
||||
} else {
|
||||
uint8_t value = ram_[address & ram_mask_];
|
||||
|
||||
#pragma mark - Video
|
||||
// If this is an M1 cycle reading from above the 32kb mark and HALT is not
|
||||
// currently active, latch for video output and return a NOP. Otherwise,
|
||||
// just return the value as read.
|
||||
if(is_opcode_read && address&0x8000 && !(value & 0x40) && !z80_.get_halt_line()) {
|
||||
latched_video_byte_ = value;
|
||||
has_latched_video_byte_ = true;
|
||||
*cycle.value = 0;
|
||||
} else *cycle.value = value;
|
||||
}
|
||||
break;
|
||||
|
||||
void Machine::set_vsync(bool sync) {
|
||||
vsync_ = sync;
|
||||
update_sync();
|
||||
}
|
||||
case CPU::Z80::PartialMachineCycle::Write:
|
||||
if(address >= ram_base_) {
|
||||
ram_[address & ram_mask_] = *cycle.value;
|
||||
}
|
||||
break;
|
||||
|
||||
void Machine::set_hsync(bool sync) {
|
||||
hsync_ = sync;
|
||||
update_sync();
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
|
||||
void Machine::update_sync() {
|
||||
video_->set_sync(vsync_ || hsync_);
|
||||
}
|
||||
if(typer_) typer_->run_for(cycle.length);
|
||||
|
||||
return HalfCycles(0);
|
||||
}
|
||||
|
||||
forceinline void flush() {
|
||||
video_->flush();
|
||||
}
|
||||
|
||||
void setup_output(float aspect_ratio) override final {
|
||||
video_.reset(new Video);
|
||||
}
|
||||
|
||||
void close_output() override final {
|
||||
video_.reset();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> get_crt() override final {
|
||||
return video_->get_crt();
|
||||
}
|
||||
|
||||
std::shared_ptr<Outputs::Speaker> get_speaker() override final {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void run_for(const Cycles cycles) override final {
|
||||
z80_.run_for(cycles);
|
||||
}
|
||||
|
||||
void configure_as_target(const StaticAnalyser::Target &target) override final {
|
||||
is_zx81_ = target.zx8081.isZX81;
|
||||
if(is_zx81_) {
|
||||
rom_ = zx81_rom_;
|
||||
tape_trap_address_ = 0x37c;
|
||||
tape_return_address_ = 0x380;
|
||||
vsync_start_ = HalfCycles(32);
|
||||
vsync_end_ = HalfCycles(64);
|
||||
automatic_tape_motor_start_address_ = 0x0340;
|
||||
automatic_tape_motor_end_address_ = 0x03c3;
|
||||
} else {
|
||||
rom_ = zx80_rom_;
|
||||
tape_trap_address_ = 0x220;
|
||||
tape_return_address_ = 0x248;
|
||||
vsync_start_ = HalfCycles(26);
|
||||
vsync_end_ = HalfCycles(66);
|
||||
automatic_tape_motor_start_address_ = 0x0206;
|
||||
automatic_tape_motor_end_address_ = 0x024d;
|
||||
}
|
||||
rom_mask_ = static_cast<uint16_t>(rom_.size() - 1);
|
||||
|
||||
switch(target.zx8081.memory_model) {
|
||||
case StaticAnalyser::ZX8081MemoryModel::Unexpanded:
|
||||
ram_.resize(1024);
|
||||
ram_base_ = 16384;
|
||||
ram_mask_ = 1023;
|
||||
break;
|
||||
case StaticAnalyser::ZX8081MemoryModel::SixteenKB:
|
||||
ram_.resize(16384);
|
||||
ram_base_ = 16384;
|
||||
ram_mask_ = 16383;
|
||||
break;
|
||||
case StaticAnalyser::ZX8081MemoryModel::SixtyFourKB:
|
||||
ram_.resize(65536);
|
||||
ram_base_ = 8192;
|
||||
ram_mask_ = 65535;
|
||||
break;
|
||||
}
|
||||
Memory::Fuzz(ram_);
|
||||
|
||||
if(target.loadingCommand.length()) {
|
||||
set_typer_for_string(target.loadingCommand.c_str());
|
||||
}
|
||||
|
||||
insert_media(target.media);
|
||||
}
|
||||
|
||||
bool insert_media(const StaticAnalyser::Media &media) override final {
|
||||
if(!media.tapes.empty()) {
|
||||
tape_player_.set_tape(media.tapes.front());
|
||||
}
|
||||
|
||||
return !media.tapes.empty();
|
||||
}
|
||||
|
||||
void set_typer_for_string(const char *string) override final {
|
||||
std::unique_ptr<CharacterMapper> mapper(new CharacterMapper(is_zx81_));
|
||||
Utility::TypeRecipient::set_typer_for_string(string, std::move(mapper));
|
||||
}
|
||||
|
||||
void set_rom(ROMType type, std::vector<uint8_t> data) override final {
|
||||
switch(type) {
|
||||
case ZX80: zx80_rom_ = data; break;
|
||||
case ZX81: zx81_rom_ = data; break;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Keyboard
|
||||
|
||||
void Machine::set_key_state(uint16_t key, bool isPressed) {
|
||||
if(isPressed)
|
||||
key_states_[key >> 8] &= (uint8_t)(~key);
|
||||
else
|
||||
key_states_[key >> 8] |= (uint8_t)key;
|
||||
void set_key_state(uint16_t key, bool isPressed) override final {
|
||||
if(isPressed)
|
||||
key_states_[key >> 8] &= static_cast<uint8_t>(~key);
|
||||
else
|
||||
key_states_[key >> 8] |= static_cast<uint8_t>(key);
|
||||
}
|
||||
|
||||
void clear_all_keys() override final {
|
||||
memset(key_states_, 0xff, 8);
|
||||
}
|
||||
|
||||
#pragma mark - Tape control
|
||||
|
||||
void set_use_fast_tape_hack(bool activate) override final {
|
||||
use_fast_tape_hack_ = activate;
|
||||
}
|
||||
|
||||
void set_use_automatic_tape_motor_control(bool enabled) override final {
|
||||
use_automatic_tape_motor_control_ = enabled;
|
||||
if(!enabled) {
|
||||
tape_player_.set_motor_control(false);
|
||||
}
|
||||
}
|
||||
void set_tape_is_playing(bool is_playing) override final {
|
||||
tape_player_.set_motor_control(is_playing);
|
||||
}
|
||||
|
||||
#pragma mark - Typer timing
|
||||
|
||||
HalfCycles get_typer_delay() override final { return Cycles(7000000); }
|
||||
HalfCycles get_typer_frequency() override final { return Cycles(390000); }
|
||||
|
||||
KeyboardMapper &get_keyboard_mapper() override {
|
||||
return keyboard_mapper_;
|
||||
}
|
||||
|
||||
private:
|
||||
CPU::Z80::Processor<ConcreteMachine, false, is_zx81> z80_;
|
||||
|
||||
std::shared_ptr<Video> video_;
|
||||
std::vector<uint8_t> zx81_rom_, zx80_rom_;
|
||||
|
||||
uint16_t tape_trap_address_, tape_return_address_;
|
||||
uint16_t automatic_tape_motor_start_address_, automatic_tape_motor_end_address_;
|
||||
|
||||
std::vector<uint8_t> ram_;
|
||||
uint16_t ram_mask_, ram_base_;
|
||||
|
||||
std::vector<uint8_t> rom_;
|
||||
uint16_t rom_mask_;
|
||||
|
||||
bool vsync_, hsync_;
|
||||
int line_counter_;
|
||||
|
||||
uint8_t key_states_[8];
|
||||
ZX8081::KeyboardMapper keyboard_mapper_;
|
||||
|
||||
HalfClockReceiver<Storage::Tape::BinaryTapePlayer> tape_player_;
|
||||
Storage::Tape::ZX8081::Parser parser_;
|
||||
|
||||
bool is_zx81_;
|
||||
bool nmi_is_enabled_;
|
||||
|
||||
HalfCycles vsync_start_, vsync_end_;
|
||||
HalfCycles horizontal_counter_;
|
||||
|
||||
uint8_t latched_video_byte_;
|
||||
bool has_latched_video_byte_;
|
||||
|
||||
bool use_fast_tape_hack_;
|
||||
bool use_automatic_tape_motor_control_;
|
||||
HalfCycles tape_advance_delay_;
|
||||
|
||||
#pragma mark - Video
|
||||
|
||||
inline void set_vsync(bool sync) {
|
||||
vsync_ = sync;
|
||||
update_sync();
|
||||
}
|
||||
|
||||
inline void set_hsync(bool sync) {
|
||||
hsync_ = sync;
|
||||
update_sync();
|
||||
}
|
||||
|
||||
inline void update_sync() {
|
||||
video_->set_sync(vsync_ || hsync_);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void Machine::clear_all_keys() {
|
||||
memset(key_states_, 0xff, 8);
|
||||
using namespace ZX8081;
|
||||
|
||||
// See header; constructs and returns an instance of the ZX80 or 81.
|
||||
Machine *Machine::ZX8081(const StaticAnalyser::Target &target_hint) {
|
||||
// Instantiate the correct type of machine.
|
||||
if(target_hint.zx8081.isZX81)
|
||||
return new ZX8081::ConcreteMachine<true>();
|
||||
else
|
||||
return new ZX8081::ConcreteMachine<false>();
|
||||
}
|
||||
|
||||
Machine::~Machine() {}
|
||||
|
||||
@@ -11,13 +11,7 @@
|
||||
|
||||
#include "../ConfigurationTarget.hpp"
|
||||
#include "../CRTMachine.hpp"
|
||||
#include "../Typer.hpp"
|
||||
|
||||
#include "../../Processors/Z80/Z80.hpp"
|
||||
#include "../../Storage/Tape/Tape.hpp"
|
||||
#include "../../Storage/Tape/Parsers/ZX8081.hpp"
|
||||
|
||||
#include "Video.hpp"
|
||||
#include "../KeyboardMachine.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
@@ -28,93 +22,19 @@ enum ROMType: uint8_t {
|
||||
ZX80, ZX81
|
||||
};
|
||||
|
||||
enum Key: uint16_t {
|
||||
KeyShift = 0x0000 | 0x01, KeyZ = 0x0000 | 0x02, KeyX = 0x0000 | 0x04, KeyC = 0x0000 | 0x08, KeyV = 0x0000 | 0x10,
|
||||
KeyA = 0x0100 | 0x01, KeyS = 0x0100 | 0x02, KeyD = 0x0100 | 0x04, KeyF = 0x0100 | 0x08, KeyG = 0x0100 | 0x10,
|
||||
KeyQ = 0x0200 | 0x01, KeyW = 0x0200 | 0x02, KeyE = 0x0200 | 0x04, KeyR = 0x0200 | 0x08, KeyT = 0x0200 | 0x10,
|
||||
Key1 = 0x0300 | 0x01, Key2 = 0x0300 | 0x02, Key3 = 0x0300 | 0x04, Key4 = 0x0300 | 0x08, Key5 = 0x0300 | 0x10,
|
||||
Key0 = 0x0400 | 0x01, Key9 = 0x0400 | 0x02, Key8 = 0x0400 | 0x04, Key7 = 0x0400 | 0x08, Key6 = 0x0400 | 0x10,
|
||||
KeyP = 0x0500 | 0x01, KeyO = 0x0500 | 0x02, KeyI = 0x0500 | 0x04, KeyU = 0x0500 | 0x08, KeyY = 0x0500 | 0x10,
|
||||
KeyEnter = 0x0600 | 0x01, KeyL = 0x0600 | 0x02, KeyK = 0x0600 | 0x04, KeyJ = 0x0600 | 0x08, KeyH = 0x0600 | 0x10,
|
||||
KeySpace = 0x0700 | 0x01, KeyDot = 0x0700 | 0x02, KeyM = 0x0700 | 0x04, KeyN = 0x0700 | 0x08, KeyB = 0x0700 | 0x10,
|
||||
};
|
||||
|
||||
class Machine:
|
||||
public CPU::Z80::Processor<Machine>,
|
||||
public CRTMachine::Machine,
|
||||
public Utility::TypeRecipient,
|
||||
public ConfigurationTarget::Machine {
|
||||
public ConfigurationTarget::Machine,
|
||||
public KeyboardMachine::Machine {
|
||||
public:
|
||||
Machine();
|
||||
static Machine *ZX8081(const StaticAnalyser::Target &target_hint);
|
||||
virtual ~Machine();
|
||||
|
||||
HalfCycles perform_machine_cycle(const CPU::Z80::PartialMachineCycle &cycle);
|
||||
void flush();
|
||||
virtual void set_rom(ROMType type, std::vector<uint8_t> data) = 0;
|
||||
|
||||
void setup_output(float aspect_ratio);
|
||||
void close_output();
|
||||
|
||||
std::shared_ptr<Outputs::CRT::CRT> get_crt();
|
||||
std::shared_ptr<Outputs::Speaker> get_speaker();
|
||||
|
||||
void run_for(const Cycles cycles);
|
||||
|
||||
void configure_as_target(const StaticAnalyser::Target &target);
|
||||
|
||||
void set_rom(ROMType type, std::vector<uint8_t> data);
|
||||
void set_key_state(uint16_t key, bool isPressed);
|
||||
void clear_all_keys();
|
||||
|
||||
inline void set_use_fast_tape_hack(bool activate) { use_fast_tape_hack_ = activate; }
|
||||
inline void set_use_automatic_tape_motor_control(bool enabled) {
|
||||
use_automatic_tape_motor_control_ = enabled;
|
||||
if(!enabled) {
|
||||
tape_player_.set_motor_control(false);
|
||||
}
|
||||
}
|
||||
inline void set_tape_is_playing(bool is_playing) { tape_player_.set_motor_control(is_playing); }
|
||||
|
||||
// for Utility::TypeRecipient::Delegate
|
||||
uint16_t *sequence_for_character(Utility::Typer *typer, char character);
|
||||
HalfCycles get_typer_delay() { return Cycles(7000000); }
|
||||
HalfCycles get_typer_frequency() { return Cycles(390000); }
|
||||
|
||||
private:
|
||||
std::shared_ptr<Video> video_;
|
||||
std::vector<uint8_t> zx81_rom_, zx80_rom_;
|
||||
|
||||
uint16_t tape_trap_address_, tape_return_address_;
|
||||
uint16_t automatic_tape_motor_start_address_, automatic_tape_motor_end_address_;
|
||||
|
||||
std::vector<uint8_t> ram_;
|
||||
uint16_t ram_mask_, ram_base_;
|
||||
|
||||
std::vector<uint8_t> rom_;
|
||||
uint16_t rom_mask_;
|
||||
|
||||
bool vsync_, hsync_;
|
||||
int line_counter_;
|
||||
|
||||
uint8_t key_states_[8];
|
||||
|
||||
void set_vsync(bool sync);
|
||||
void set_hsync(bool sync);
|
||||
void update_sync();
|
||||
|
||||
HalfClockReceiver<Storage::Tape::BinaryTapePlayer> tape_player_;
|
||||
Storage::Tape::ZX8081::Parser parser_;
|
||||
|
||||
bool is_zx81_;
|
||||
bool nmi_is_enabled_;
|
||||
|
||||
HalfCycles vsync_start_, vsync_end_;
|
||||
HalfCycles horizontal_counter_;
|
||||
|
||||
uint8_t latched_video_byte_;
|
||||
bool has_latched_video_byte_;
|
||||
|
||||
bool use_fast_tape_hack_;
|
||||
bool use_automatic_tape_motor_control_;
|
||||
HalfCycles tape_advance_delay_;
|
||||
virtual void set_use_fast_tape_hack(bool activate) = 0;
|
||||
virtual void set_tape_is_playing(bool is_playing) = 0;
|
||||
virtual void set_use_automatic_tape_motor_control(bool enabled) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -23,12 +23,12 @@ class CRC16 {
|
||||
CRC16(uint16_t polynomial, uint16_t reset_value) :
|
||||
reset_value_(reset_value), value_(reset_value) {
|
||||
for(int c = 0; c < 256; c++) {
|
||||
uint16_t shift_value = (uint16_t)(c << 8);
|
||||
uint16_t shift_value = static_cast<uint16_t>(c << 8);
|
||||
for(int b = 0; b < 8; b++) {
|
||||
uint16_t exclusive_or = (shift_value&0x8000) ? polynomial : 0x0000;
|
||||
shift_value = (uint16_t)(shift_value << 1) ^ exclusive_or;
|
||||
shift_value = static_cast<uint16_t>(shift_value << 1) ^ exclusive_or;
|
||||
}
|
||||
xor_table[c] = (uint16_t)shift_value;
|
||||
xor_table[c] = static_cast<uint16_t>(shift_value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class CRC16 {
|
||||
|
||||
/// Updates the CRC to include @c byte.
|
||||
inline void add(uint8_t byte) {
|
||||
value_ = (uint16_t)((value_ << 8) ^ xor_table[(value_ >> 8) ^ byte]);
|
||||
value_ = static_cast<uint16_t>((value_ << 8) ^ xor_table[(value_ >> 8) ^ byte]);
|
||||
}
|
||||
|
||||
/// @returns The current value of the CRC.
|
||||
|
||||
@@ -9,14 +9,17 @@
|
||||
#ifndef Factors_hpp
|
||||
#define Factors_hpp
|
||||
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
|
||||
namespace NumberTheory {
|
||||
/*!
|
||||
@returns The greatest common divisor of @c a and @c b as computed by Euclid's algorithm.
|
||||
@returns The greatest common divisor of @c a and @c b.
|
||||
*/
|
||||
template<class T> T greatest_common_divisor(T a, T b) {
|
||||
// TODO: replace with the C++17 GCD function, once available.
|
||||
#if __cplusplus > 201402L
|
||||
return std::gcd(a, b);
|
||||
#else
|
||||
if(a < b) {
|
||||
std::swap(a, b);
|
||||
}
|
||||
@@ -29,11 +32,12 @@ namespace NumberTheory {
|
||||
a = b;
|
||||
b = remainder;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*!
|
||||
@returns The least common multiple of @c a and @c b computed indirectly via Euclid's greatest
|
||||
common divisor algorithm.
|
||||
@returns The least common multiple of @c a and @c b computed indirectly via the greatest
|
||||
common divisor.
|
||||
*/
|
||||
template<class T> T least_common_multiple(T a, T b) {
|
||||
if(a == b) return a;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0800"
|
||||
LastUpgradeVersion = "0900"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
@@ -26,6 +26,7 @@
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
language = ""
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
codeCoverageEnabled = "YES">
|
||||
<Testables>
|
||||
@@ -71,10 +72,13 @@
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
enableUBSanitizer = "YES"
|
||||
language = ""
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
stopOnEveryUBSanitizerIssue = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "NO">
|
||||
<BuildableProductRunnable
|
||||
|
||||
48
OSBindings/Mac/Clock Signal/Base.lproj/AmstradCPCOptions.xib
Normal file
48
OSBindings/Mac/Clock Signal/Base.lproj/AmstradCPCOptions.xib
Normal file
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12121"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<customObject id="-2" userLabel="File's Owner" customClass="MachineDocument" customModule="Clock_Signal" customModuleProvider="target">
|
||||
<connections>
|
||||
<outlet property="optionsPanel" destination="ZW7-Bw-4RP" id="JpE-wG-zRR"/>
|
||||
</connections>
|
||||
</customObject>
|
||||
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
|
||||
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
|
||||
<window title="Options" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" hidesOnDeactivate="YES" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="ZW7-Bw-4RP" customClass="MachinePanel" customModule="Clock_Signal" customModuleProvider="target">
|
||||
<windowStyleMask key="styleMask" titled="YES" closable="YES" utility="YES" nonactivatingPanel="YES" HUD="YES"/>
|
||||
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
|
||||
<rect key="contentRect" x="83" y="102" width="200" height="54"/>
|
||||
<rect key="screenRect" x="0.0" y="0.0" width="1366" height="768"/>
|
||||
<view key="contentView" id="tpZ-0B-QQu">
|
||||
<rect key="frame" x="0.0" y="0.0" width="200" height="54"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<subviews>
|
||||
<button translatesAutoresizingMaskIntoConstraints="NO" id="e1J-pw-zGw">
|
||||
<rect key="frame" x="18" y="18" width="164" height="18"/>
|
||||
<buttonCell key="cell" type="check" title="Load Quickly" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="tD6-UB-ESB">
|
||||
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
|
||||
<font key="font" metaFont="system"/>
|
||||
</buttonCell>
|
||||
<connections>
|
||||
<action selector="setFastLoading:" target="ZW7-Bw-4RP" id="JmG-Ks-jSh"/>
|
||||
</connections>
|
||||
</button>
|
||||
</subviews>
|
||||
<constraints>
|
||||
<constraint firstItem="e1J-pw-zGw" firstAttribute="leading" secondItem="tpZ-0B-QQu" secondAttribute="leading" constant="20" id="HSD-3d-Bl7"/>
|
||||
<constraint firstAttribute="trailing" secondItem="e1J-pw-zGw" secondAttribute="trailing" constant="20" id="Q9M-FH-92N"/>
|
||||
<constraint firstAttribute="bottom" secondItem="e1J-pw-zGw" secondAttribute="bottom" constant="20" id="sdh-oJ-ZIQ"/>
|
||||
<constraint firstItem="e1J-pw-zGw" firstAttribute="top" secondItem="tpZ-0B-QQu" secondAttribute="top" constant="20" id="ul9-lf-Y3u"/>
|
||||
</constraints>
|
||||
</view>
|
||||
<connections>
|
||||
<outlet property="fastLoadingButton" destination="e1J-pw-zGw" id="jj7-OZ-mOH"/>
|
||||
</connections>
|
||||
<point key="canvasLocation" x="175" y="30"/>
|
||||
</window>
|
||||
</objects>
|
||||
</document>
|
||||
@@ -3,7 +3,6 @@
|
||||
//
|
||||
|
||||
#import "CSMachine.h"
|
||||
#import "CSKeyboardMachine.h"
|
||||
#import "CSFastLoading.h"
|
||||
|
||||
#import "CSAtari2600.h"
|
||||
@@ -17,3 +16,5 @@
|
||||
#import "CSOpenGLView.h"
|
||||
#import "CSAudioQueue.h"
|
||||
#import "CSBestEffortUpdater.h"
|
||||
|
||||
#include "KeyCodes.h"
|
||||
|
||||
@@ -24,9 +24,9 @@ class Atari2600OptionsPanel: MachinePanel {
|
||||
}
|
||||
|
||||
fileprivate func pushSwitchValues() {
|
||||
atari2600.colourButton = colourButton.state == NSOnState
|
||||
atari2600.leftPlayerDifficultyButton = leftPlayerDifficultyButton.state == NSOnState
|
||||
atari2600.rightPlayerDifficultyButton = rightPlayerDifficultyButton.state == NSOnState
|
||||
atari2600.colourButton = colourButton.state == .on
|
||||
atari2600.leftPlayerDifficultyButton = leftPlayerDifficultyButton.state == .on
|
||||
atari2600.rightPlayerDifficultyButton = rightPlayerDifficultyButton.state == .on
|
||||
}
|
||||
|
||||
@IBAction func optionWasPressed(_ sender: NSButton!) {
|
||||
|
||||
@@ -38,10 +38,10 @@ class MachineDocument:
|
||||
}
|
||||
|
||||
fileprivate var audioQueue: CSAudioQueue! = nil
|
||||
fileprivate var bestEffortUpdater: CSBestEffortUpdater!
|
||||
fileprivate var bestEffortUpdater: CSBestEffortUpdater?
|
||||
|
||||
override var windowNibName: String? {
|
||||
return "MachineDocument"
|
||||
override var windowNibName: NSNib.Name? {
|
||||
return NSNib.Name(rawValue: "MachineDocument")
|
||||
}
|
||||
|
||||
override func windowControllerDidLoadNib(_ aController: NSWindowController) {
|
||||
@@ -69,7 +69,7 @@ class MachineDocument:
|
||||
self.openGLView.window!.makeKeyAndOrderFront(self)
|
||||
|
||||
// start accepting best effort updates
|
||||
self.bestEffortUpdater.delegate = self
|
||||
self.bestEffortUpdater!.delegate = self
|
||||
}
|
||||
|
||||
func machineDidChangeClockRate(_ machine: CSMachine!) {
|
||||
@@ -77,7 +77,7 @@ class MachineDocument:
|
||||
}
|
||||
|
||||
func machineDidChangeClockIsUnlimited(_ machine: CSMachine!) {
|
||||
self.bestEffortUpdater.runAsUnlimited = machine.clockIsUnlimited
|
||||
self.bestEffortUpdater?.runAsUnlimited = machine.clockIsUnlimited
|
||||
}
|
||||
|
||||
fileprivate func setupClockRate() {
|
||||
@@ -91,15 +91,21 @@ class MachineDocument:
|
||||
self.machine.setAudioSamplingRate(selectedSamplingRate, bufferSize:audioQueue.preferredBufferSize)
|
||||
}
|
||||
|
||||
self.bestEffortUpdater.clockRate = self.machine.clockRate
|
||||
self.bestEffortUpdater?.clockRate = self.machine.clockRate
|
||||
}
|
||||
|
||||
override func close() {
|
||||
bestEffortUpdater.flush()
|
||||
optionsPanel?.setIsVisible(false)
|
||||
optionsPanel = nil
|
||||
|
||||
openGLView.delegate = nil
|
||||
bestEffortUpdater!.delegate = nil
|
||||
bestEffortUpdater = nil
|
||||
|
||||
actionLock.lock()
|
||||
drawLock.lock()
|
||||
machine = nil
|
||||
openGLView.invalidate()
|
||||
openGLView.openGLContext!.makeCurrentContext()
|
||||
actionLock.unlock()
|
||||
drawLock.unlock()
|
||||
|
||||
@@ -114,7 +120,7 @@ class MachineDocument:
|
||||
analysis.apply(to: self.machine)
|
||||
|
||||
if let optionsPanelNibName = analysis.optionsPanelNibName {
|
||||
Bundle.main.loadNibNamed(optionsPanelNibName, owner: self, topLevelObjects: nil)
|
||||
Bundle.main.loadNibNamed(NSNib.Name(rawValue: optionsPanelNibName), owner: self, topLevelObjects: nil)
|
||||
self.optionsPanel.machine = self.machine
|
||||
showOptions(self)
|
||||
}
|
||||
@@ -131,8 +137,8 @@ class MachineDocument:
|
||||
|
||||
// MARK: the pasteboard
|
||||
func paste(_ sender: AnyObject!) {
|
||||
let pasteboard = NSPasteboard.general()
|
||||
if let string = pasteboard.string(forType: NSPasteboardTypeString) {
|
||||
let pasteboard = NSPasteboard.general
|
||||
if let string = pasteboard.string(forType: .string) {
|
||||
self.machine.paste(string)
|
||||
}
|
||||
}
|
||||
@@ -143,24 +149,35 @@ class MachineDocument:
|
||||
}
|
||||
|
||||
func runForNumberOfCycles(_ numberOfCycles: Int32) {
|
||||
let cyclesToRunFor = min(numberOfCycles, Int32(bestEffortUpdater.clockRate / 10))
|
||||
if actionLock.try() {
|
||||
self.machine.runForNumber(ofCycles: cyclesToRunFor)
|
||||
actionLock.unlock()
|
||||
if let bestEffortUpdater = bestEffortUpdater {
|
||||
let cyclesToRunFor = min(numberOfCycles, Int32(bestEffortUpdater.clockRate / 10))
|
||||
if actionLock.try() {
|
||||
self.machine.runForNumber(ofCycles: cyclesToRunFor)
|
||||
actionLock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: CSAudioQueueDelegate
|
||||
final func audioQueueIsRunningDry(_ audioQueue: CSAudioQueue) {
|
||||
bestEffortUpdater.update()
|
||||
bestEffortUpdater?.update()
|
||||
}
|
||||
|
||||
// MARK: CSOpenGLViewDelegate
|
||||
final func openGLView(_ view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) {
|
||||
bestEffortUpdater.update()
|
||||
if drawLock.try() {
|
||||
self.machine.drawView(forPixelSize: view.backingSize, onlyIfDirty: onlyIfDirty)
|
||||
drawLock.unlock()
|
||||
if let bestEffortUpdater = bestEffortUpdater {
|
||||
bestEffortUpdater.update()
|
||||
if drawLock.try() {
|
||||
self.machine.drawView(forPixelSize: view.backingSize, onlyIfDirty: onlyIfDirty)
|
||||
drawLock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final func openGLView(_ view: CSOpenGLView, didReceiveFileAt URL: URL) {
|
||||
let mediaSet = CSMediaSet(fileAt: URL)
|
||||
if let mediaSet = mediaSet {
|
||||
mediaSet.apply(to: self.machine)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,48 +187,22 @@ class MachineDocument:
|
||||
}
|
||||
|
||||
// MARK: Input management
|
||||
fileprivate func withKeyboardMachine(_ action: (CSKeyboardMachine) -> ()) {
|
||||
if let keyboardMachine = self.machine as? CSKeyboardMachine {
|
||||
action(keyboardMachine)
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func withJoystickMachine(_ action: (CSJoystickMachine) -> ()) {
|
||||
if let joystickMachine = self.machine as? CSJoystickMachine {
|
||||
action(joystickMachine)
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func sendJoystickEvent(_ machine: CSJoystickMachine, keyCode: UInt16, isPressed: Bool) {
|
||||
switch keyCode {
|
||||
case 123: machine.setDirection(.left, onPad: 0, isPressed: isPressed)
|
||||
case 126: machine.setDirection(.up, onPad: 0, isPressed: isPressed)
|
||||
case 124: machine.setDirection(.right, onPad: 0, isPressed: isPressed)
|
||||
case 125: machine.setDirection(.down, onPad: 0, isPressed: isPressed)
|
||||
default: machine.setButtonAt(0, onPad: 0, isPressed: isPressed)
|
||||
}
|
||||
}
|
||||
|
||||
func windowDidResignKey(_ notification: Notification) {
|
||||
self.withKeyboardMachine { $0.clearAllKeys() }
|
||||
self.machine.clearAllKeys()
|
||||
}
|
||||
|
||||
func keyDown(_ event: NSEvent) {
|
||||
self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: true) }
|
||||
self.withJoystickMachine { sendJoystickEvent($0, keyCode: event.keyCode, isPressed: true) }
|
||||
self.machine.setKey(event.keyCode, isPressed: true)
|
||||
}
|
||||
|
||||
func keyUp(_ event: NSEvent) {
|
||||
self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: false) }
|
||||
self.withJoystickMachine { sendJoystickEvent($0, keyCode: event.keyCode, isPressed: false) }
|
||||
self.machine.setKey(event.keyCode, isPressed: false)
|
||||
}
|
||||
|
||||
func flagsChanged(_ newModifiers: NSEvent) {
|
||||
self.withKeyboardMachine {
|
||||
$0.setKey(VK_Shift, isPressed: newModifiers.modifierFlags.contains(.shift))
|
||||
$0.setKey(VK_Control, isPressed: newModifiers.modifierFlags.contains(.control))
|
||||
$0.setKey(VK_Command, isPressed: newModifiers.modifierFlags.contains(.command))
|
||||
$0.setKey(VK_Option, isPressed: newModifiers.modifierFlags.contains(.option))
|
||||
}
|
||||
self.machine.setKey(VK_Shift, isPressed: newModifiers.modifierFlags.contains(.shift))
|
||||
self.machine.setKey(VK_Control, isPressed: newModifiers.modifierFlags.contains(.control))
|
||||
self.machine.setKey(VK_Command, isPressed: newModifiers.modifierFlags.contains(.command))
|
||||
self.machine.setKey(VK_Option, isPressed: newModifiers.modifierFlags.contains(.option))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class MachinePanel: NSPanel {
|
||||
@IBOutlet var fastLoadingButton: NSButton?
|
||||
@IBAction func setFastLoading(_ sender: NSButton!) {
|
||||
if let fastLoadingMachine = machine as? CSFastLoading {
|
||||
let useFastLoadingHack = sender.state == NSOnState
|
||||
let useFastLoadingHack = sender.state == .on
|
||||
fastLoadingMachine.useFastLoadingHack = useFastLoadingHack
|
||||
UserDefaults.standard.set(useFastLoadingHack, forKey: fastLoadingUserDefaultsKey)
|
||||
}
|
||||
@@ -39,7 +39,7 @@ class MachinePanel: NSPanel {
|
||||
if let fastLoadingMachine = machine as? CSFastLoading {
|
||||
let useFastLoadingHack = standardUserDefaults.bool(forKey: self.fastLoadingUserDefaultsKey)
|
||||
fastLoadingMachine.useFastLoadingHack = useFastLoadingHack
|
||||
self.fastLoadingButton?.state = useFastLoadingHack ? NSOnState : NSOffState
|
||||
self.fastLoadingButton?.state = useFastLoadingHack ? .on : .off
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class ZX8081OptionsPanel: MachinePanel {
|
||||
get { return prefixedUserDefaultsKey("automaticTapeMotorControl") }
|
||||
}
|
||||
@IBAction func setAutomaticTapeMotorConrol(_ sender: NSButton!) {
|
||||
let isEnabled = sender.state == NSOnState
|
||||
let isEnabled = sender.state == .on
|
||||
UserDefaults.standard.set(isEnabled, forKey: self.automaticTapeMotorControlDefaultsKey)
|
||||
self.playOrPauseTapeButton.isEnabled = !isEnabled
|
||||
self.zx8081.useAutomaticTapeMotorControl = isEnabled
|
||||
@@ -42,7 +42,7 @@ class ZX8081OptionsPanel: MachinePanel {
|
||||
])
|
||||
|
||||
let automaticTapeMotorControlIsEnabled = standardUserDefaults.bool(forKey: self.automaticTapeMotorControlDefaultsKey)
|
||||
self.automaticTapeMotorControlButton.state = automaticTapeMotorControlIsEnabled ? NSOnState : NSOffState
|
||||
self.automaticTapeMotorControlButton.state = automaticTapeMotorControlIsEnabled ? .on : .off
|
||||
self.playOrPauseTapeButton.isEnabled = !automaticTapeMotorControlIsEnabled
|
||||
self.zx8081.useAutomaticTapeMotorControl = automaticTapeMotorControlIsEnabled
|
||||
}
|
||||
|
||||
@@ -224,6 +224,40 @@
|
||||
<string>Tape Image</string>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>Viewer</string>
|
||||
<key>LSTypeIsPackage</key>
|
||||
<integer>0</integer>
|
||||
<key>NSDocumentClass</key>
|
||||
<string>$(PRODUCT_MODULE_NAME).MachineDocument</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CFBundleTypeExtensions</key>
|
||||
<array>
|
||||
<string>cdt</string>
|
||||
</array>
|
||||
<key>CFBundleTypeIconFile</key>
|
||||
<string>cassette</string>
|
||||
<key>CFBundleTypeName</key>
|
||||
<string>Amstrad CPC Tape Image</string>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>Viewer</string>
|
||||
<key>LSTypeIsPackage</key>
|
||||
<integer>0</integer>
|
||||
<key>NSDocumentClass</key>
|
||||
<string>$(PRODUCT_MODULE_NAME).MachineDocument</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CFBundleTypeExtensions</key>
|
||||
<array>
|
||||
<string>hfe</string>
|
||||
</array>
|
||||
<key>CFBundleTypeIconFile</key>
|
||||
<string>floppy35</string>
|
||||
<key>CFBundleTypeName</key>
|
||||
<string>HxC Disk Image</string>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>Viewer</string>
|
||||
<key>LSTypeIsPackage</key>
|
||||
<integer>0</integer>
|
||||
<key>NSDocumentClass</key>
|
||||
<string>$(PRODUCT_MODULE_NAME).MachineDocument</string>
|
||||
</dict>
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
//
|
||||
// CSJoystickMachine.h
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 03/10/2016.
|
||||
// Copyright © 2016 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
typedef NS_ENUM(NSInteger, CSJoystickDirection)
|
||||
{
|
||||
CSJoystickDirectionUp,
|
||||
CSJoystickDirectionDown,
|
||||
CSJoystickDirectionLeft,
|
||||
CSJoystickDirectionRight
|
||||
};
|
||||
|
||||
@protocol CSJoystickMachine <NSObject>
|
||||
|
||||
- (void)setButtonAtIndex:(NSUInteger)button onPad:(NSUInteger)pad isPressed:(BOOL)isPressed;
|
||||
- (void)setDirection:(CSJoystickDirection)direction onPad:(NSUInteger)pad isPressed:(BOOL)isPressed;
|
||||
|
||||
@end
|
||||
@@ -1,16 +0,0 @@
|
||||
//
|
||||
// CSKeyboardMachine.h
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 05/06/2016.
|
||||
// Copyright © 2016 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#import "KeyCodes.h"
|
||||
|
||||
@protocol CSKeyboardMachine <NSObject>
|
||||
|
||||
- (void)setKey:(uint16_t)key isPressed:(BOOL)isPressed;
|
||||
- (void)clearAllKeys;
|
||||
|
||||
@end
|
||||
@@ -11,7 +11,5 @@
|
||||
|
||||
@interface CSMachine (Subclassing)
|
||||
|
||||
- (CRTMachine::Machine * const)machine;
|
||||
- (void)setupOutputWithAspectRatio:(float)aspectRatio;
|
||||
|
||||
@end
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user