-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathports.js
332 lines (293 loc) · 8.24 KB
/
ports.js
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict';
/** @const */ var IO_TR_DIRECTION = 0;
/** @const */ var IO_TH_DIRECTION = 1;
/** @const */ var IO_TR_OUTPUT = 2;
/** @const */ var IO_TH_OUTPUT = 3;
/** @const */ var IO_TH_INPUT = 4;
/** @const */ var PORT_A = 0;
/** @const */ var PORT_B = 5;
/**
* @constructor
* @param {JSSMS} sms
*/
JSSMS.Ports = function(sms) {
this.main = sms;
this.vdp = sms.vdp;
this.psg = sms.psg;
this.keyboard = sms.keyboard;
/**
* European / Domestic system.
* @type {number}
*/
this.europe = 0x40;
/**
* Horizontal counter latch.
* @type {number}
*/
this.hCounter = 0;
/**
* I/O Ports A and B * (5 ints each).
* @type {Array.<number>}
*/
this.ioPorts = [];
};
JSSMS.Ports.prototype = {
reset: function() {
if (LIGHTGUN) {
this.ioPorts = new Array(10);
this.ioPorts[PORT_A + IO_TH_INPUT] = 1;
this.ioPorts[PORT_B + IO_TH_INPUT] = 1;
} else {
this.ioPorts = new Array(2);
}
},
/**
* Output to a Z80 port.
*
* @param {number} port Port number.
* @param {number} value Value to output.
*/
out: function(port, value) {
// Game Gear Serial Ports (do nothing for now)
if (this.main.is_gg && port < 0x07) {
return;
}
switch (port & 0xc1) {
// 0x3F IO Port
// 0xD7 : Port B TH pin output level (1=high, 0=low)
// 0xD6 : Port B TR pin output level (1=high, 0=low)
// 0xD5 : Port A TH pin output level (1=high, 0=low)
// 0xD4 : Port A TR pin output level (1=high, 0=low)
// 0xD3 : Port B TH pin direction (1=input, 0=output)
// 0xD2 : Port B TR pin direction (1=input, 0=output)
// 0xD1 : Port A TH pin direction (1=input, 0=output)
// 0xD0 : Port A TR pin direction (1=input, 0=output)
case 0x01:
// Accurate emulation with HCounter
if (LIGHTGUN) {
this.oldTH = this.getTH(PORT_A) !== 0 || this.getTH(PORT_B) !== 0;
this.writePort(PORT_A, value);
this.writePort(PORT_B, value >> 2);
// Toggling TH latches H Counter
if (
!this.oldTH &&
(this.getTH(PORT_A) !== 0 || this.getTH(PORT_B) !== 0)
) {
this.hCounter = this.getHCount();
}
} else {
// Rough emulation of Nationalisation bits
this.ioPorts[0] = (value & 0x20) << 1;
this.ioPorts[1] = value & 0x80;
if (this.europe === 0) {
// Not European system
this.ioPorts[0] = ~this.ioPorts[0];
this.ioPorts[1] = ~this.ioPorts[1];
}
}
break;
// 0xBE VDP Data port
case 0x80:
this.vdp.dataWrite(value);
break;
// 0xBD / 0xBF VDP Control port (Mirrored at two locations)
case 0x81:
this.vdp.controlWrite(value);
break;
// 0x7F: PSG
case 0x40:
case 0x41:
if (this.main.soundEnabled) {
this.psg.write(value);
}
break;
}
},
/**
* Read from a Z80 port.
*
* @param {number} port Port number.
* @return {number} Value from port number.
*/
in_: function(port) {
// Game Gear Serial Ports (not fully emulated)
if (this.main.is_gg && port < 0x07) {
switch (port) {
// Game Gear (Start Button and Nationalisation)
case 0x00:
return (this.keyboard.ggstart & 0xbf) | this.europe;
// GG Serial Communication Ports -
// Return 0 for now as "OutRun" gets stuck in a loop by returning 0xFF
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
return 0x00;
case 0x06:
return 0xff;
}
}
switch (port & 0xc1) {
// 0x7E - Vertical Port
case 0x40:
return this.vdp.getVCount();
// 0x7F - Horizontal Port
case 0x41:
return this.hCounter;
// VDP Data port
case 0x80:
return this.vdp.dataRead();
// VDP Control port
case 0x81:
return this.vdp.controlRead();
// 0xC0 / 0xDC - I/O Port A
// 0xD7 : Port B DOWN pin input
// 0xD6 : Port B UP pin input
// 0xD5 : Port A TR pin input
// 0xD4 : Port A TL pin input
// 0xD3 : Port A RIGHT pin input
// 0xD2 : Port A LEFT pin input
// 0xD1 : Port A DOWN pin input
// 0xD0 : Port A UP pin input
case 0xc0:
return this.keyboard.controller1;
// 0xC1 / 0xDD - I/O Port B and Misc
// 0xD7 : Port B TH pin input
// 0xD6 : Port A TH pin input
// 0xD5 : Unused
// 0xD4 : RESET button (1= not pressed, 0= pressed)
// 0xD3 : Port B TR pin input
// 0xD2 : Port B TL pin input
// 0xD1 : Port B RIGHT pin input
// 0xD0 : Port B LEFT pin input
case 0xc1:
if (LIGHTGUN) {
if (this.keyboard.lightgunClick) {
this.lightPhaserSync();
}
return (
(this.keyboard.controller2 & 0x3f) |
(this.getTH(PORT_A) !== 0 ? 0x40 : 0) |
(this.getTH(PORT_B) !== 0 ? 0x80 : 0)
);
} else {
return (
(this.keyboard.controller2 & 0x3f) |
this.ioPorts[0] |
this.ioPorts[1]
);
}
}
// Default Value is 0xFF
return 0xff;
},
// Port A/B Emulation
/**
* @param {number} index
* @param {number} value
*/
writePort: function(index, value) {
this.ioPorts[index + IO_TR_DIRECTION] = value & 0x01;
this.ioPorts[index + IO_TH_DIRECTION] = value & 0x02;
this.ioPorts[index + IO_TR_OUTPUT] = value & 0x10;
this.ioPorts[index + IO_TH_OUTPUT] =
this.europe === 0 ? ~value & 0x20 : value & 0x20;
},
/**
* @param {number} index
* @return {number}
*/
getTH: function(index) {
return this.ioPorts[index + IO_TH_DIRECTION] === 0
? this.ioPorts[index + IO_TH_OUTPUT]
: this.ioPorts[index + IO_TH_INPUT];
},
/**
* @param {number} index
* @param {boolean} on
*/
setTH: function(index, on) {
this.ioPorts[index + IO_TH_DIRECTION] = 1;
this.ioPorts[index + IO_TH_INPUT] = on ? 1 : 0;
},
// H Counter Emulation
//
// The H counter is 9 bits, and reading it returns the upper 8 bits. This is
// because a scanline consists of 342 pixels, which couldn't be represented
// with an 8-bit counter. Each scanline is divided up as follows:
//
// Pixels H.Cnt Description
// 256 : 0x00-0x7F : Active display
// 15 : 0x80-0x87 : Right border
// 8 : 0x87-0x8B : Right blanking
// 26 : 0x8B-0xED : Horizontal sync
// 2 : 0xED-0xEE : Left blanking
// 14 : 0xEE-0xF5 : Color burst
// 8 : 0xF5-0xF9 : Left blanking
// 13 : 0xF9-0xFF : Left border
/**
* @return {number}
*/
getHCount: function() {
var pixels = Math.round(
this.main.cpu.getCycle() * SMS_X_PIXELS / this.main.cyclesPerLine
);
var v = (pixels - 8) >> 1;
if (v > 0x93) {
v += 0xe9 - 0x94;
}
return v & 0xff;
},
// Lightgun <-> Port Synchronisation
// This is a hacky way to do things, but works reasonably well.
/**
* X range of Lightgun.
* @type {number}
*/
X_RANGE: 48,
/**
* Y range of Lightgun.
* @type {number}
*/
Y_RANGE: 4,
lightPhaserSync: function() {
var oldTH = this.getTH(PORT_A);
var hc = this.getHCount();
var dx = this.keyboard.lightgunX - (hc << 1);
var dy = this.keyboard.lightgunY - this.vdp.line;
// Within 8 pixels of click on Y value
// Within 96 pixels of click on X value
if (
dy > -this.Y_RANGE &&
dy < this.Y_RANGE &&
(dx > -this.X_RANGE && dx < this.X_RANGE)
) {
this.setTH(PORT_A, false);
// TH has been toggled, update with lightgun position
if (oldTH !== this.getTH(PORT_A)) {
this.hCounter = 20 + (this.keyboard.lightgunX >> 1);
}
} else {
this.setTH(PORT_A, true);
// TH has been toggled, update with usual HCounter value
if (oldTH !== this.getTH(PORT_A)) {
this.hCounter = hc;
}
}
},
/**
* Set console to European / Japanese model.
*
* @param {boolean} value True is European, false is Japanese.
*/
setDomestic: function(value) {
this.europe = value ? 0x40 : 0;
},
/**
* @return {boolean}
*/
isDomestic: function() {
return this.europe !== 0;
},
};