1from fsgs.drivers.messdriver import MessDriver
2from fsgs.option import Option
3from fsgs.spectrum import (
4    ZXS_48_ROM,
5    ZXS_128_0_ROM,
6    ZXS_128_1_ROM,
7    ZXS_PLUS3_0_ROM,
8    ZXS_PLUS3_1_ROM,
9    ZXS_PLUS3_2_ROM,
10    ZXS_PLUS3_3_ROM,
11)
12
13MESS_SPECTRUM = "spectrum"  # ZX Spectrum
14MESS_SPEC128 = "spec128"  # ZX Spectrum 128
15MESS_SPECPLS3 = "specpls3"  # ZX Spectrum +3
16MESS_sp3e8bit = "sp3e8bit"  # ZX Spectrum +3e 8bit IDE
17# noinspection SpellCheckingInspection
18MESS_sp3eata = "sp3eata"  # ZX Spectrum +3e 8bit ZXATASP
19# noinspection SpellCheckingInspection
20MESS_sp3ezcf = "sp3ezcf"  # ZX Spectrum +3e 8bit ZXCF
21MESS_spec80k = "spec80k"  # ZX Spectrum 80K
22MESS_specide = "specide"  # ZX Spectrum IDE
23MESS_specpl2a = "specpl2a"  # ZX Spectrum +2a
24MESS_specpl3e = "specpl3e"  # ZX Spectrum +3e
25MESS_specpls2 = "specpls2"  # ZX Spectrum +2
26
27
28class MessSpectrumDriver(MessDriver):
29    JOYSTICK = {
30        "type": "joystick",
31        "description": "Joystick",
32        "mapping_name": "zx-spectrum-joystick",
33    }
34
35    PORTS = [{"description": "Joystick Port", "types": [JOYSTICK]}]
36
37    def mess_configure(self):
38        # FIXME: hack
39        # self.mess_configure_floppies(["cassette"])
40        # self.emulator.args.extend(["-ui_active"])
41
42        if self.config[Option.SNAPSHOT_FILE]:
43            # snapshot loads automatically, no need to do anything special
44            self.add_arg(
45                "-{0}".format("snapshot"),
46                self.get_game_file(Option.SNAPSHOT_FILE),
47            )
48        elif self.config[Option.FLOPPY_DRIVE_0]:
49            # pressing enter to select "loader" in the menu
50            self.inject_fake_input_string(160, "{0}\n".format(""))
51            self.add_arg(
52                "-{0}".format("floppydisk1"),
53                self.get_game_file(Option.FLOPPY_DRIVE_0),
54            )
55
56        elif self.config[Option.TAPE_DRIVE_0]:
57            self.emulator.args.extend(
58                ["-cassette", self.get_game_file(Option.TAPE_DRIVE_0)]
59            )
60            if self.config[Option.ZXS_MODEL] == "spectrum128":
61                self.inject_fake_input_string_list(
62                    160,
63                    [
64                        "1040",
65                        "0040",
66                        "0000",  # Return
67                        "0000",
68                        "0000",
69                        "0000",
70                        "1059",
71                        "0059",
72                        "0000",  # F2 (Load tape)
73                    ],
74                )
75            else:
76                self.inject_fake_input_string_list(
77                    160,
78                    [
79                        # Inject LOAD ""
80                        "1013",
81                        "0013",
82                        "0000",  # J (LOAD)
83                        "1229",
84                        "0000",  # Right Shift (press)
85                        "1019",
86                        "0019",
87                        "0000",  # P (with symbol shift = ")
88                        "0229",
89                        "0000",  # Right shift (release)
90                        "1229",
91                        "0000",  # Right shift (press)
92                        "1019",
93                        "0019",
94                        "0000",  # P (with symbol shift = ")
95                        "0299",
96                        "0000",  # Right shift (release)
97                        "1040",
98                        "0040",
99                        "0000",  # Return
100                        # "0000", "0000", "0000",
101                        # "1071", "0071", "0000",  # Scroll Lock
102                        "0000",
103                        "0000",
104                        "0000",
105                        "1059",
106                        "0059",
107                        "0000",  # F2 (Load tape)
108                    ],
109                )
110            # self.args.extend(
111            #     ["-autoboot_command", 'j" "\\n'])
112
113    def mess_full_keyboard(self):
114        return False
115
116    def mess_input_mapping(self, port):
117        return {
118            "UP": 'tag=":KEMPSTON" type="KEYBOARD" mask="8" defvalue="0"',
119            "DOWN": 'tag=":KEMPSTON" type="KEYBOARD" mask="4" defvalue="0"',
120            "LEFT": 'tag=":KEMPSTON" type="KEYBOARD" mask="2" defvalue="0"',
121            "RIGHT": 'tag=":KEMPSTON" type="KEYBOARD" mask="1" defvalue="0"',
122            "1": 'tag=":KEMPSTON" type="KEYBOARD" mask="16" defvalue="0"',
123        }
124
125    def get_game_refresh_rate(self):
126        # Refresh rate values retrieved from MESS
127        if self.config[Option.ZXS_MODEL] == "spectrum128":
128            return 50.021084
129        else:
130            return 50.080128
131
132    def mess_romset(self):
133        if self.config[Option.ZXS_MODEL] == "spectrum128":
134            return MESS_SPEC128, MESS_SPEC128_ROMS
135        if self.config[Option.ZXS_MODEL] == "spectrum+3":
136            return MESS_SPECPLS3, MESS_SPECPLS3_ROMS
137        else:
138            return MESS_SPECTRUM, MESS_SPECTRUM_ROMS
139
140
141# noinspection SpellCheckingInspection
142MESS_SPECTRUM_ROMS = [
143    (ZXS_48_ROM.sha1, "spectrum.rom"),
144    # ("f9d23f25640c51bcaa63e21ed5dd66bb2d5f63d4", "1986es.rom"),
145    # 9e535e2e24231ccb65e33d107f6d0ceb23e99477", "48e.rom"),
146    # ("e62a431b0938af414b7ab8b1349a18b3c4407f70", "48turbo.rom"),
147    # ("ab3c36daad4325c1d3b907b6dc9a14af483d14ec", "bsrom118.rom"),
148    # ("2ee2dbe6ab96b60d7af1d6cb763b299374c21776", "bsrom140.rom"),
149    # ("795c20324311dd5a56300e6e4ec49b0a694ac0b3", "deutsch.rom"),
150    # ("51165cde68e218512d3145467074bc7e786bf307", "groot.rom"),
151    # ("a701c3d4b698f7d2be537dc6f79e06e4dbc95929", "gw03.rom"),
152    # ("2a9745ba3b369a84c4913c98ede66ec87cb8aec1", "hdt-iso.rom"),
153    # ("dee814271c4d51de257d88128acdb324fb1d1d0d", "imc.rom"),
154    # ("5752e6f789769475711b91e0a75911fa5232c767", "iso8bm.rom"),
155    # ("04adbdb1380d6ccd4ab26ddd61b9ccbba462a60f", "isomoje.rom"),
156    # ("d7f02ed66455f1c08ac0c864c7038a92a88ba94a", "jgh.rom"),
157    # ("c103e89ef58e6ade0c01cea0247b332623bd9a30", "plus4.rom"),
158    # ("0853e25857d51dd41b20a6dbc8e80f028c5befaa", "psycho.rom"),
159    # ("c58ff44a28db47400f09ed362ca0527591218136", "sc01.rom"),
160    # ("84ea64af06adaf05e68abe1d69454b4fc6888505", "turbo2_3.rom"),
161    # ("21ad93ffe41a4458704c866cca2754f066f6a560", "turbo4_4.rom"),
162]
163
164# noinspection SpellCheckingInspection
165MESS_SPEC128_ROMS = [
166    (ZXS_128_0_ROM.sha1, "zx128_0.rom"),
167    (ZXS_128_1_ROM.sha1, "zx128_1.rom"),
168    # Spanish
169    # ("968937b1c750f0ef6205f01c6db4148da4cca4e3", "zx128s0.rom"),
170    # ("bea3f397cc705eafee995ea629f4a82550562f90", "zx128s1.rom"),
171]
172
173# noinspection SpellCheckingInspection
174MESS_SPECPLS3_ROMS = [
175    (ZXS_PLUS3_0_ROM.sha1, "pl3-0.rom"),
176    (ZXS_PLUS3_1_ROM.sha1, "pl3-1.rom"),
177    (ZXS_PLUS3_2_ROM.sha1, "pl3-2.rom"),
178    (ZXS_PLUS3_3_ROM.sha1, "pl3-3.rom"),
179    # ("4e5d114b72d464cefdde0566457f52a3c0c1cae2", "p3_01_4m.rom"),
180    # ("4e5d114b72d464cefdde0566457f52a3c0c1cae2", "p3_01_cm.rom"),
181    # ("752cdd6a083ab9910348995e483541d60bb6372b", "p3_23_4m.rom"),
182    # ("d062765ceb1f3cd2c94ea51cb737cac7ad6151b4", "p3_23_cm.rom"),
183    # ("500c0945760abeefcbd08bc22c0d07b14b336cf0", "plus341.rom"),
184    # ("e9b0a60a1a8def511d59090b945d175bdc646346", "plus3sp0.rom"),
185    # ("4e48f196427596c7990c175d135c15a039c274a4", "plus3sp1.rom"),
186    # ("09fc005625589ef5992515957ce7a3167dec24b2", "plus3sp2.rom"),
187    # ("ec8f644a81e2e9bcb58ace974103ea960361bad2", "plus3sp3.rom"),
188]
189