Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/Cherry/Core/Input.cpp
2 views
1
/*
2
* Gearcoleco - ColecoVision Emulator
3
* Copyright (C) 2021 Ignacio Sanchez
4
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* any later version.
9
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see http://www.gnu.org/licenses/
17
*
18
*/
19
20
#include "Input.h"
21
#include "Processor.h"
22
23
Input::Input(Processor* pProcessor)
24
{
25
m_pProcessor = pProcessor;
26
}
27
28
void Input::Init()
29
{
30
Reset();
31
}
32
33
void Input::Reset()
34
{
35
m_Segment = SegmentKeypadRightButtons;
36
m_Gamepad[0] = m_Gamepad[1] = 0xFF;
37
m_Keypad[0] = m_Keypad[1] = 0xFF;
38
m_iSpinnerRel[0] = m_iSpinnerRel[1] = 0;
39
}
40
41
void Input::SetInputSegment(InputSegments segment)
42
{
43
m_Segment = segment;
44
}
45
46
u8 Input::ReadInput(u8 port)
47
{
48
u8 c = (port & 0x02) >> 1;
49
u8 ret = 0xFF;
50
51
int rel = m_iSpinnerRel[c] / 4;
52
m_iSpinnerRel[c] -= rel;
53
54
if (m_Segment == SegmentKeypadRightButtons)
55
{
56
ret = (m_Keypad[c] & 0x0F) | (IsSetBit(m_Gamepad[c], 5) ? 0x70 : 0x30);
57
}
58
else
59
{
60
ret = (m_Gamepad[c] & 0x0F) | (IsSetBit(m_Gamepad[c], 4) ? 0x70 : 0x30);
61
62
if (rel > 0)
63
{
64
ret &= c ? 0xEF : 0xCF;
65
m_pProcessor->RequestINT(true);
66
}
67
else if (rel < 0)
68
{
69
ret &= c ? 0xCF : 0xEF;
70
m_pProcessor->RequestINT(true);
71
}
72
}
73
74
return ret;
75
}
76
77
void Input::KeyPressed(GC_Controllers controller, GC_Keys key)
78
{
79
if (key > 0x0F)
80
{
81
m_Gamepad[controller] = UnsetBit(m_Gamepad[controller], key & 0x0F);
82
}
83
else
84
{
85
m_Keypad[controller] &= (key & 0x0F);
86
}
87
}
88
89
void Input::KeyReleased(GC_Controllers controller, GC_Keys key)
90
{
91
if (key > 0x0F)
92
{
93
m_Gamepad[controller] = SetBit(m_Gamepad[controller], key & 0x0F);
94
}
95
else
96
{
97
m_Keypad[controller] |= ~(key & 0x0F);
98
}
99
}
100
101
void Input::Spinner1(int movement)
102
{
103
m_iSpinnerRel[0] = movement;
104
}
105
106
void Input::Spinner2(int movement)
107
{
108
m_iSpinnerRel[1] = movement;
109
}
110
111
void Input::SaveState(std::ostream& stream)
112
{
113
stream.write(reinterpret_cast<const char*> (m_Gamepad), sizeof(m_Gamepad));
114
stream.write(reinterpret_cast<const char*> (m_Keypad), sizeof(m_Keypad));
115
stream.write(reinterpret_cast<const char*> (&m_Segment), sizeof(m_Segment));
116
stream.write(reinterpret_cast<const char*> (m_iSpinnerRel), sizeof(m_iSpinnerRel));
117
}
118
119
void Input::LoadState(std::istream& stream)
120
{
121
stream.read(reinterpret_cast<char*> (m_Gamepad), sizeof(m_Gamepad));
122
stream.read(reinterpret_cast<char*> (m_Keypad), sizeof(m_Keypad));
123
stream.read(reinterpret_cast<char*> (&m_Segment), sizeof(m_Segment));
124
stream.read(reinterpret_cast<char*> (m_iSpinnerRel), sizeof(m_iSpinnerRel));
125
}
126
127