-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJamma.cpp
199 lines (180 loc) · 4.39 KB
/
Jamma.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2019 Takashi Toyoshima <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
#include "Jamma.h"
#include <avr/io.h>
Jamma::Jamma() {
// Input, pull-up
DDRA = 0x00;
DDRB = 0x00;
DDRC = 0x00;
PORTA = 0xff;
PORTB = 0xff;
PORTC = 0xff;
#if defined(NO_DEBUG)
DDRD &= ~0x12;
PORTD |= 0x12;
#else
DDRD &= ~0x10;
PORTD |= 0x10;
#endif
Initialize();
}
void Jamma::Initialize() {
for (uint8_t i = 0; i < 2; ++i) {
current_coin_sw[i] = 1;
coin_sw[i] = 1;
coin_count[i] = 0;
}
for (uint8_t i = 0; i < 5; ++i)
sw[i] = 0;
}
void Jamma::DetectMode() {
mode = (PINA & 0x30) ? Mode::kJamma : Mode::kMahjong;
if (mode == Mode::kJamma) {
DDRC = 0x00;
PORTC = 0xff;
} else {
// Hi-Z w/o PU for bit7-4.
DDRC = 0x00;
PORTC = 0x0f;
}
}
Jamma::Mode Jamma::GetMode() {
return mode;
}
void Jamma::Update(bool swap) {
uint8_t pina = PINA;
uint8_t pinb = PINB;
uint8_t pinc = PINC;
uint8_t pind = PIND;
uint8_t csw1 = pinb & 1;
uint8_t csw2 = pinc & 1;
current_coin_sw[0] &= csw1;
current_coin_sw[1] &= csw2;
sw[0] = (pind & 0x10) ? 0 : 0x80;
sw[1] =
((pinb & 0x02) ? 0 : 0x80) |
((pind & 0x02) ? 0 : 0x40) |
((pina & 0x01) ? 0 : 0x20) |
((pina & 0x02) ? 0 : 0x10) |
((pina & 0x04) ? 0 : 0x08) |
((pina & 0x08) ? 0 : 0x04);
if (swap) {
sw[1] |=
#if defined(ALT_SWAP)
((pinb & 0x08) ? 0 : 0x02) | // B2 -> B1
((pinb & 0x10) ? 0 : 0x01); // B3 -> B2
#else
((pinb & 0x20) ? 0 : 0x02) | // B4 -> B1
((pinb & 0x04) ? 0 : 0x01); // B1 -> B2
#endif
} else {
sw[1] |=
((pinb & 0x04) ? 0 : 0x02) | // B1
((pinb & 0x08) ? 0 : 0x01); // B2
}
if (mode == Mode::kMahjong) {
sw[2] = sw[3] = sw[4] = 0;
return;
}
sw[2] =
((pinb & 0x40) ? 0 : 0x20) | // B5
((pinb & 0x80) ? 0 : 0x10); // B6
if (swap) {
sw[2] |=
#if defined(ALT_SWAP)
((pinb & 0x20) ? 0 : 0x80) | // B4 -> B3
((pinb & 0x04) ? 0 : 0x40); // B1 -> B4
#else
((pinb & 0x08) ? 0 : 0x80) | // B2 -> B3
((pinb & 0x10) ? 0 : 0x40); // B3 -> B4
#endif
} else {
sw[2] |=
((pinb & 0x10) ? 0 : 0x80) | // B3
((pinb & 0x20) ? 0 : 0x40); // B4
}
sw[3] =
((pinc & 0x02) ? 0 : 0x80) |
((pina & 0x10) ? 0 : 0x20) |
((pina & 0x20) ? 0 : 0x10) |
((pina & 0x40) ? 0 : 0x08) |
((pina & 0x80) ? 0 : 0x04);
if (swap) {
sw[3] |=
#if defined(ALT_SWAP)
((pinc & 0x08) ? 0 : 0x02) | // B2 -> B1
((pinc & 0x10) ? 0 : 0x01); // B3 -> B2
#else
((pinc & 0x20) ? 0 : 0x02) | // B4 -> B1
((pinc & 0x04) ? 0 : 0x01); // B1 -> B2
#endif
} else {
sw[3] |=
((pinc & 0x04) ? 0 : 0x02) | // B1
((pinc & 0x08) ? 0 : 0x01); // B2
}
sw[4] =
((pinc & 0x40) ? 0 : 0x20) | // B5
((pinc & 0x80) ? 0 : 0x10); // B6
if (swap) {
sw[4] |=
#if defined(ALT_SWAP)
((pinc & 0x20) ? 0 : 0x80) | // B4 -> B3
((pinc & 0x04) ? 0 : 0x40); // B1 -> B4
#else
((pinc & 0x08) ? 0 : 0x80) | // B2 -> B3
((pinc & 0x10) ? 0 : 0x40); // B3 -> B4
#endif
} else {
sw[4] |=
((pinc & 0x10) ? 0 : 0x80) | // B3
((pinc & 0x20) ? 0 : 0x40); // B4
}
}
void Jamma::Sync() {
for (uint8_t i = 0; i < 2; ++i) {
if (!coin_sw[i] && current_coin_sw[i])
coin_count[i]++;
coin_sw[i] = current_coin_sw[i];
current_coin_sw[i] = 1;
}
}
uint8_t Jamma::GetSw(uint8_t index, bool rapid_mode, bool rapid_mask) {
uint8_t result = sw[index];
if (rapid_mode) switch (index) {
case 0:
break;
case 1:
case 3:
if (!rapid_mask)
result |= (sw[index + 1] >> 5) & 0x03;
break;
case 2:
case 4:
if (!rapid_mask)
result |= (result << 3) & 0x80;
break;
}
return result;
}
uint8_t Jamma::GetCoin(uint8_t index) {
return coin_count[index];
}
void Jamma::SubCoin(uint8_t index, uint8_t sub) {
if (index < 2)
coin_count[index] -= sub;
}
void Jamma::AddCoin(uint8_t index, uint8_t add) {
if (index < 2)
coin_count[index] += add;
}
void Jamma::DriveOutput(uint8_t output) {
if (mode != Mode::kMahjong)
return;
// Output low signal for the activated outputs.
out = output & 0xf0;
out = ((out >> 1) | (out << 3)) & 0xf0;
DDRC = out;
}