1 | //============================================================================ |
2 | // |
3 | // SSSS tt lll lll |
4 | // SS SS tt ll ll |
5 | // SS tttttt eeee ll ll aaaa |
6 | // SSSS tt ee ee ll ll aa |
7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
8 | // SS SS tt ee ll ll aa aa |
9 | // SSSS ttt eeeee llll llll aaaaa |
10 | // |
11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
12 | // and the Stella Team |
13 | // |
14 | // See the file "License.txt" for information on usage and redistribution of |
15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
16 | //============================================================================ |
17 | |
18 | #include <cassert> |
19 | |
20 | #include "System.hxx" |
21 | #include "Control.hxx" |
22 | |
23 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
24 | Controller::Controller(Jack jack, const Event& event, const System& system, |
25 | Type type) |
26 | : myJack(jack), |
27 | myEvent(event), |
28 | mySystem(system), |
29 | myType(type), |
30 | myOnAnalogPinUpdateCallback(nullptr) |
31 | { |
32 | resetDigitalPins(); |
33 | resetAnalogPins(); |
34 | } |
35 | |
36 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
37 | uInt8 Controller::read() |
38 | { |
39 | uInt8 ioport = 0b0000; |
40 | if(read(DigitalPin::One)) ioport |= 0b0001; |
41 | if(read(DigitalPin::Two)) ioport |= 0b0010; |
42 | if(read(DigitalPin::Three)) ioport |= 0b0100; |
43 | if(read(DigitalPin::Four)) ioport |= 0b1000; |
44 | return ioport; |
45 | } |
46 | |
47 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
48 | bool Controller::read(DigitalPin pin) |
49 | { |
50 | return getPin(pin); |
51 | } |
52 | |
53 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
54 | Int32 Controller::read(AnalogPin pin) |
55 | { |
56 | return getPin(pin); |
57 | } |
58 | |
59 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
60 | bool Controller::save(Serializer& out) const |
61 | { |
62 | try |
63 | { |
64 | // Output the digital pins |
65 | out.putBool(getPin(DigitalPin::One)); |
66 | out.putBool(getPin(DigitalPin::Two)); |
67 | out.putBool(getPin(DigitalPin::Three)); |
68 | out.putBool(getPin(DigitalPin::Four)); |
69 | out.putBool(getPin(DigitalPin::Six)); |
70 | |
71 | // Output the analog pins |
72 | out.putInt(getPin(AnalogPin::Five)); |
73 | out.putInt(getPin(AnalogPin::Nine)); |
74 | } |
75 | catch(...) |
76 | { |
77 | cerr << "ERROR: Controller::save() exception\n" ; |
78 | return false; |
79 | } |
80 | return true; |
81 | } |
82 | |
83 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
84 | bool Controller::load(Serializer& in) |
85 | { |
86 | try |
87 | { |
88 | // Input the digital pins |
89 | setPin(DigitalPin::One, in.getBool()); |
90 | setPin(DigitalPin::Two, in.getBool()); |
91 | setPin(DigitalPin::Three, in.getBool()); |
92 | setPin(DigitalPin::Four, in.getBool()); |
93 | setPin(DigitalPin::Six, in.getBool()); |
94 | |
95 | // Input the analog pins |
96 | setPin(AnalogPin::Five, in.getInt()); |
97 | setPin(AnalogPin::Nine, in.getInt()); |
98 | } |
99 | catch(...) |
100 | { |
101 | cerr << "ERROR: Controller::load() exception\n" ; |
102 | return false; |
103 | } |
104 | return true; |
105 | } |
106 | |
107 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
108 | string Controller::getName(const Type type) |
109 | { |
110 | string NAMES[int(Controller::Type::LastType)] = |
111 | { |
112 | "Unknown" , |
113 | "AmigaMouse" , "AtariMouse" , "AtariVox" , "BoosterGrip" , "CompuMate" , |
114 | "Driving" , "Sega Genesis" , "Joystick" , "Keyboard" , "KidVid" , "MindLink" , |
115 | "Paddles" , "Paddles_IAxis" , "Paddles_IAxDr" , "SaveKey" , "TrakBall" |
116 | }; |
117 | |
118 | return NAMES[int(type)]; |
119 | } |
120 | |
121 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
122 | string Controller::getPropName(const Type type) |
123 | { |
124 | string PROP_NAMES[int(Controller::Type::LastType)] = |
125 | { |
126 | "AUTO" , |
127 | "AMIGAMOUSE" , "ATARIMOUSE" , "ATARIVOX" , "BOOSTERGRIP" , "COMPUMATE" , |
128 | "DRIVING" , "GENESIS" , "JOYSTICK" , "KEYBOARD" , "KIDVID" , "MINDLINK" , |
129 | "PADDLES" , "PADDLES_IAXIS" , "PADDLES_IAXDR" , "SAVEKEY" , "TRAKBALL" |
130 | }; |
131 | |
132 | return PROP_NAMES[int(type)]; |
133 | } |
134 | |
135 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
136 | Controller::Type Controller::getType(const string& propName) |
137 | { |
138 | for(int i = 0; i < static_cast<int>(Type::LastType); ++i) |
139 | { |
140 | if(BSPF::equalsIgnoreCase(propName, getPropName(Type(i)))) |
141 | { |
142 | return Type(i); |
143 | } |
144 | } |
145 | // special case |
146 | if(BSPF::equalsIgnoreCase(propName, "KEYPAD" )) |
147 | return Type::Keyboard; |
148 | |
149 | return Type::Unknown; |
150 | } |
151 | |