This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
ports.js
383 lines (354 loc) · 10.8 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* @fileoverview Implements Space Invaders I/O ports
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 Jeff Parsons
* @license MIT
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*/
"use strict";
/**
* @typedef {PortsConfig} InvadersPortsConfig
* @property {number} addr
* @property {number} size
* @property {Object} switches
*/
/**
* @class {InvadersPorts}
* @unrestricted
* @property {InvadersPortsConfig} config
*/
class InvadersPorts extends Ports {
/**
* InvadersPorts(idMachine, idDevice, config)
*
* @this {InvadersPorts}
* @param {string} idMachine
* @param {string} idDevice
* @param {InvadersPortsConfig} [config]
*/
constructor(idMachine, idDevice, config)
{
super(idMachine, idDevice, config);
this.addIOTable(this, InvadersPorts.IOTABLE);
this.input = /** @type {Input} */ (this.findDeviceByClass("Input"));
let onButton = this.onButton.bind(this);
let buttonIDs = Object.keys(InvadersPorts.STATUS1.KEYMAP);
for (let i = 0; i < buttonIDs.length; i++) {
this.input.addListener(Input.TYPE.IDMAP, buttonIDs[i], onButton);
}
this.switchConfig = this.config['switches'] || {};
this.defaultSwitches = this.parseSwitches(this.switchConfig['default'], 0xff);
this.setSwitches(this.defaultSwitches);
this.onReset();
}
/**
* loadState(state)
*
* Memory and Ports states are managed by the Bus onLoad() handler, which calls our loadState() handler.
*
* @this {InvadersPorts}
* @param {Array|undefined} state
* @returns {boolean}
*/
loadState(state)
{
if (state) {
let idDevice = state.shift();
if (this.idDevice == idDevice) {
this.bStatus0 = state.shift();
this.bStatus1 = state.shift();
this.bStatus2 = state.shift();
this.wShiftData = state.shift();
this.bShiftCount = state.shift();
this.setSwitches(state.shift());
return true;
}
}
return false;
}
/**
* saveState(state)
*
* Memory and Ports states are managed by the Bus onSave() handler, which calls our saveState() handler.
*
* @this {InvadersPorts}
* @param {Array} state
*/
saveState(state)
{
state.push(this.idDevice);
state.push(this.bStatus0);
state.push(this.bStatus1);
state.push(this.bStatus2);
state.push(this.wShiftData);
state.push(this.bShiftCount);
state.push(this.switches);
}
/**
* onButton(id, down)
*
* @this {InvadersPorts}
* @param {string} id
* @param {boolean} down
*/
onButton(id, down)
{
let bit = InvadersPorts.STATUS1.KEYMAP[id];
this.bStatus1 = (this.bStatus1 & ~bit) | (down? bit : 0);
}
/**
* onReset()
*
* Called by the Machine device to provide notification of a reset event.
*
* @this {InvadersPorts}
*/
onReset()
{
this.bStatus0 = 0;
this.bStatus1 = 0;
this.bStatus2 = 0;
this.wShiftData = 0;
this.bShiftCount = 0;
}
/**
* setSwitches(switches)
*
* @this {InvadersPorts}
* @param {number|undefined} switches
*/
setSwitches(switches)
{
/*
* switches may be undefined when called from loadState() if a "pre-switches" state was loaded.
*/
if (switches == undefined) return;
/*
* If this.switches is undefined, then this is the first setSwitches() call, so we should set func
* to onSwitch(); otherwise, we omit func so that all addListener() will do is initialize the visual
* state of the SWITCH controls.
*/
let func = this.switches == undefined? this.onSwitch.bind(this) : null;
/*
* Now we can set the actual switches to the supplied setting, and initialize each of the (8) switches.
*/
this.switches = switches;
for (let i = 1; i <= 8; i++) {
this.input.addListener(Input.TYPE.SWITCH, "sw"+i, func, !(switches & (1 << (i - 1))));
}
}
/**
* onSwitch(id, state)
*
* @this {InvadersPorts}
* @param {string} id
* @param {boolean} state
*/
onSwitch(id, state)
{
let desc;
let i = +id.slice(-1) - 1, bit = 1 << i;
if (!state) {
this.switches |= bit;
} else {
this.switches &= ~bit;
}
for (let sws in this.switchConfig) {
if (sws == "default" || sws[i] != '0' && sws[i] != '1') continue;
let mask = this.parseSwitches(sws, -1);
let switches = this.parseSwitches(sws);
if (switches == (this.switches & mask)) {
desc = this.switchConfig[sws];
break;
}
}
this.printf("%s: %b (%s)\n", id, state, desc);
}
/**
* inStatus0(port)
*
* @this {InvadersPorts}
* @param {number} port (0x00)
* @returns {number} simulated port value
*/
inStatus0(port)
{
let value = this.bStatus0;
this.printf(MESSAGE.BUS, "inStatus0(%#04x): %#04x\n", port, value);
return value;
}
/**
* inStatus1(port)
*
* @this {InvadersPorts}
* @param {number} port (0x01)
* @returns {number} simulated port value
*/
inStatus1(port)
{
let value = this.bStatus1;
this.printf(MESSAGE.PORTS, "inStatus1(%#04x): %#04x\n", port, value);
return value;
}
/**
* inStatus2(port)
*
* @this {InvadersPorts}
* @param {number} port (0x02)
* @returns {number} simulated port value
*/
inStatus2(port)
{
let value = this.bStatus2 | (this.switches & (InvadersPorts.STATUS2.DIP1_2 | InvadersPorts.STATUS2.DIP4 | InvadersPorts.STATUS2.DIP7));
this.printf(MESSAGE.PORTS, "inStatus2(%#04x): %#04x\n", port, value);
return value;
}
/**
* inShiftResult(port)
*
* @this {InvadersPorts}
* @param {number} port (0x03)
* @returns {number} simulated port value
*/
inShiftResult(port)
{
let value = (this.wShiftData >> (8 - this.bShiftCount)) & 0xff;
this.printf(MESSAGE.PORTS, "inShiftResult(%#04x): %#04x\n", port, value);
return value;
}
/**
* outShiftCount(port, value)
*
* @this {InvadersPorts}
* @param {number} port (0x02)
* @param {number} value
*/
outShiftCount(port, value)
{
this.printf(MESSAGE.PORTS, "outShiftCount(%#04x): %#04x\n", port, value);
this.bShiftCount = value;
}
/**
* outSound1(port, value)
*
* @this {InvadersPorts}
* @param {number} port (0x03)
* @param {number} value
*/
outSound1(port, value)
{
this.printf(MESSAGE.PORTS, "outSound1(%#04x): %#04x\n", port, value);
this.bSound1 = value;
}
/**
* outShiftData(port, value)
*
* @this {InvadersPorts}
* @param {number} port (0x04)
* @param {number} value
*/
outShiftData(port, value)
{
this.printf(MESSAGE.PORTS, "outShiftData(%#04x): %#04x\n", port, value);
this.wShiftData = (value << 8) | (this.wShiftData >> 8);
}
/**
* outSound2(port, value)
*
* @this {InvadersPorts}
* @param {number} port (0x05)
* @param {number} value
*/
outSound2(port, value)
{
this.printf(MESSAGE.PORTS, "outSound2(%#04x): %#04x\n", port, value);
this.bSound2 = value;
}
/**
* outWatchdog(port, value)
*
* @this {InvadersPorts}
* @param {number} port (0x06)
* @param {number} value
*/
outWatchdog(port, value)
{
this.printf(MESSAGE.PORTS, "outWatchDog(%#04x): %#04x\n", port, value);
}
}
InvadersPorts.STATUS0 = { // NOTE: STATUS0 not used by the SI1978 ROMs; refer to STATUS1 instead
PORT: 0,
DIP4: 0x01, // self-test request at power up?
FIRE: 0x10, // 1 = fire
LEFT: 0x20, // 1 = left
RIGHT: 0x40, // 1 = right
PORT7: 0x80, // some connection to (undocumented) port 7
ALWAYS_SET: 0x0E // always set
};
InvadersPorts.STATUS1 = {
PORT: 1,
CREDIT: 0x01, // credit (coin slot)
P2: 0x02, // 1 = 2P start
P1: 0x04, // 1 = 1P start
P1_FIRE: 0x10, // 1 = fire (P1 fire if cocktail machine?)
P1_LEFT: 0x20, // 1 = left (P1 left if cocktail machine?)
P1_RIGHT: 0x40, // 1 = right (P1 right if cocktail machine?)
ALWAYS_SET: 0x08 // always set
};
InvadersPorts.STATUS2 = {
PORT: 2,
DIP1_2: 0x03, // 00 = 3 ships, 01 = 4 ships, 10 = 5 ships, 11 = 6 ships
TILT: 0x04, // 1 = tilt detected
DIP4: 0x08, // 0 = extra ship at 1500, 1 = extra ship at 1000
P2_FIRE: 0x10, // 1 = P2 fire (cocktail machines only?)
P2_LEFT: 0x20, // 1 = P2 left (cocktail machines only?)
P2_RIGHT: 0x40, // 1 = P2 right (cocktail machines only?)
DIP7: 0x80, // 0 = display coin info on demo ("attract") screen
ALWAYS_SET: 0x00
};
InvadersPorts.SHIFT_RESULT = { // bits 0-7 of barrel shifter result
PORT: 3
};
InvadersPorts.SHIFT_COUNT = {
PORT: 2,
MASK: 0x07
};
InvadersPorts.SOUND1 = {
PORT: 3,
UFO: 0x01,
SHOT: 0x02,
PDEATH: 0x04,
IDEATH: 0x08,
EXPLAY: 0x10,
AMP_ENABLE: 0x20
};
InvadersPorts.SHIFT_DATA = {
PORT: 4
};
InvadersPorts.SOUND2 = {
PORT: 5,
FLEET1: 0x01,
FLEET2: 0x02,
FLEET3: 0x04,
FLEET4: 0x08,
UFO_HIT: 0x10
};
InvadersPorts.STATUS1.KEYMAP = {
"1p": InvadersPorts.STATUS1.P1,
"2p": InvadersPorts.STATUS1.P2,
"coin": InvadersPorts.STATUS1.CREDIT,
"left": InvadersPorts.STATUS1.P1_LEFT,
"right": InvadersPorts.STATUS1.P1_RIGHT,
"fire": InvadersPorts.STATUS1.P1_FIRE
};
InvadersPorts.IOTABLE = {
0: [InvadersPorts.prototype.inStatus0],
1: [InvadersPorts.prototype.inStatus1],
2: [InvadersPorts.prototype.inStatus2, InvadersPorts.prototype.outShiftCount],
3: [InvadersPorts.prototype.inShiftResult, InvadersPorts.prototype.outSound1],
4: [null, InvadersPorts.prototype.outShiftData],
5: [null, InvadersPorts.prototype.outSound2],
6: [null, InvadersPorts.prototype.outWatchdog]
};
Defs.CLASSES["InvadersPorts"] = InvadersPorts;