-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmouseif.c
283 lines (239 loc) · 5.33 KB
/
mouseif.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "compiler.h"
#include "mousemng.h"
#include "cpucore.h"
#include "pccore.h"
#include "iocore.h"
#include "keystat.h"
// マウス ver0.28
// 一部のゲームでマウスデータを切り捨てるので正常な動かなくなる事がある
// それを救う為に 均等に移動データが伝わるようにしなければならない
void mouseif_sync(void) {
// 前回の分を補正
mouseif.x += mouseif.rx;
mouseif.y += mouseif.ry;
// 今回の移動量を取得
mouseif.b = mousemng_getstat(&mouseif.sx, &mouseif.sy, 1);
if (np2cfg.KEY_MODE == 3) {
mouseif.b &= keystat_getmouse(&mouseif.sx, &mouseif.sy);
}
mouseif.rx = mouseif.sx;
mouseif.ry = mouseif.sy;
mouseif.lastc = CPU_CLOCK + CPU_BASECLOCK + CPU_REMCLOCK;
}
static void calc_mousexy(void) {
UINT32 clk;
SINT32 diff;
clk = CPU_CLOCK + CPU_BASECLOCK + CPU_REMCLOCK;
diff = clk - mouseif.lastc;
if (diff >= 2000) {
SINT32 dx;
SINT32 dy;
mouseif.rapid ^= 0xa0;
diff /= 1000;
dx = mouseif.sx;
if (dx > 0) {
dx = dx * diff / mouseif.moveclock;
if (dx > mouseif.rx) {
dx = mouseif.rx;
}
}
else if (dx < 0) {
dx *= -1;
dx = dx * diff / mouseif.moveclock;
dx *= -1;
if (dx < mouseif.rx) {
dx = mouseif.rx;
}
}
mouseif.x += dx;
mouseif.rx -= dx;
dy = mouseif.sy;
if (dy > 0) {
dy = dy * diff / mouseif.moveclock;
if (dy > mouseif.ry) {
dy = mouseif.ry;
}
}
else if (dy < 0) {
dy *= -1;
dy = dy * diff / mouseif.moveclock;
dy *= -1;
if (dy < mouseif.ry) {
dy = mouseif.ry;
}
}
mouseif.y += dy;
mouseif.ry -= dy;
mouseif.lastc += diff * 1000;
}
}
void mouseint(NEVENTITEM item) {
if (item->flag & NEVENT_SETEVENT) {
if (!(mouseif.upd8255.portc & 0x10)) {
pic_setirq(0x0d);
nevent_set(NEVENT_MOUSE, mouseif.intrclock << mouseif.timing,
mouseint, NEVENT_RELATIVE);
}
}
}
static void setportc(REG8 value) {
if ((value & 0x80) && (!(mouseif.upd8255.portc & 0x80))) {
calc_mousexy();
mouseif.latch_x = mouseif.x;
mouseif.x = 0;
mouseif.latch_y = mouseif.y;
mouseif.y = 0;
if (mouseif.latch_x > 127) {
mouseif.latch_x = 127;
}
else if (mouseif.latch_x < -128) {
mouseif.latch_x = -128;
}
if (mouseif.latch_y > 127) {
mouseif.latch_y = 127;
}
else if (mouseif.latch_y < -128) {
mouseif.latch_y = -128;
}
}
if ((value ^ mouseif.upd8255.portc) & 0x10) {
if (!(value & 0x10)) {
if (!nevent_iswork(NEVENT_MOUSE)) {
// 割り込みを入れとく
pic_setirq(0x0d);
nevent_set(NEVENT_MOUSE, mouseif.intrclock << mouseif.timing,
mouseint, NEVENT_ABSOLUTE);
}
}
}
mouseif.upd8255.portc = (UINT8)value;
}
// ---- I/O
static void IOOUTCALL mouseif_o7fd9(UINT port, REG8 dat) {
mouseif.upd8255.porta = dat;
(void)port;
}
static void IOOUTCALL mouseif_o7fdb(UINT port, REG8 dat) {
mouseif.upd8255.portb = dat;
(void)port;
}
static void IOOUTCALL mouseif_o7fdd(UINT port, REG8 dat) {
setportc(dat);
(void)port;
}
static void IOOUTCALL mouseif_o7fdf(UINT port, REG8 dat) {
REG8 portc;
UINT sft;
portc = 0;
if (dat & uPD8255_CTRL) {
mouseif.upd8255.mode = (UINT8)dat;
#if 0
pic_resetirq(0x0d);
nevent_set(NEVENT_MOUSE, mouseif.intrclock << mouseif.timing,
mouseint, NEVENT_ABSOLUTE);
#endif
}
else {
sft = (dat >> 1) & 7;
portc = mouseif.upd8255.portc;
portc &= (~(1 << sft));
portc |= (dat & 1) << sft;
}
setportc(portc);
(void)port;
}
static REG8 IOINPCALL mouseif_i7fd9(UINT port) {
SINT16 x;
SINT16 y;
REG8 ret;
REG8 portc;
if (mouseif.upd8255.mode & uPD8255_PORTA) {
calc_mousexy();
ret = mouseif.b;
if (np2cfg.MOUSERAPID) {
ret |= mouseif.rapid;
}
ret &= 0xf0;
#if 1
ret |= 0x40; // for shizuku
#else
ret |= 0x50;
#endif
portc = mouseif.upd8255.portc;
if (portc & 0x80) {
x = mouseif.latch_x;
y = mouseif.latch_y;
}
else {
x = mouseif.x;
y = mouseif.y;
}
if (portc & 0x40) {
x = y;
}
if (!(portc & 0x20)) {
ret |= x & 0x0f;
}
else {
ret |= (x >> 4) & 0x0f;
}
return(ret);
}
else {
return(mouseif.upd8255.porta);
}
(void)port;
}
static REG8 IOINPCALL mouseif_i7fdb(UINT port) {
if (mouseif.upd8255.mode & uPD8255_PORTB) {
return(0x40);
}
else {
return(mouseif.upd8255.portb);
}
(void)port;
}
static REG8 IOINPCALL mouseif_i7fdd(UINT port) {
REG8 mode;
REG8 ret;
mode = mouseif.upd8255.mode;
ret = mouseif.upd8255.portc;
if (mode & uPD8255_PORTCH) {
ret &= 0x1f;
}
if (mode & uPD8255_PORTCL) {
ret &= 0xf0;
ret |= 0x08;
ret |= (pccore.dipsw[2] >> 5) & 0x04;
ret |= ((~pccore.dipsw[0]) >> 4) & 0x03;
}
(void)port;
return(ret);
}
static void IOOUTCALL mouseif_obfdb(UINT port, REG8 dat) {
mouseif.timing = dat & 3;
(void)port;
}
// ---- I/F
void mouseif_reset(const NP2CFG *pConfig) {
ZeroMemory(&mouseif, sizeof(mouseif));
mouseif.upd8255.porta = 0x00;
mouseif.upd8255.portb = 0x00;
mouseif.upd8255.portc = 0xf0; // ver0.82
mouseif.upd8255.mode = 0x93;
mouseif.intrclock = pccore.realclock / 120;
mouseif.moveclock = pccore.realclock / 56400;
mouseif.latch_x = -1;
mouseif.latch_y = -1;
(void)pConfig;
}
void mouseif_bind(void) {
iocore_attachout(0x7fd9, mouseif_o7fd9);
iocore_attachout(0x7fdb, mouseif_o7fdb);
iocore_attachout(0x7fdd, mouseif_o7fdd);
iocore_attachout(0x7fdf, mouseif_o7fdf);
iocore_attachinp(0x7fd9, mouseif_i7fd9);
iocore_attachinp(0x7fdb, mouseif_i7fdb);
iocore_attachinp(0x7fdd, mouseif_i7fdd);
iocore_attachout(0xbfdb, mouseif_obfdb);
}