forked from mattgodbolt/jsbeeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.js
77 lines (68 loc) · 1.92 KB
/
adc.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
"use strict";
export function Adc(sysvia, scheduler) {
this.sysvia = sysvia;
this.task = scheduler.newTask(this.onComplete.bind(this));
this.status = 0x40;
this.low = 0x00;
this.high = 0x00;
}
Adc.prototype.read = function (addr) {
switch (addr & 3) {
case 0:
return this.status;
case 1:
return this.high;
case 2:
return this.low;
default:
break;
}
return 0x40;
};
Adc.prototype.write = function (addr, val) {
if ((addr & 3) !== 0) return;
// 8 bit conversion takes 4ms whereas 10 bit conversions take 10ms, according to AUG
this.task.cancel();
this.task.schedule((val & 0x08) ? 20000 : 8000);
this.status = (val & 0x0f) | 0x80;
this.sysvia.setcb1(true);
};
Adc.prototype.onComplete = function () {
var val = 0x8000;
var pads = this.sysvia.getGamepads();
if (pads && pads[0]) {
var pad = pads[0];
var pad2 = pads[1];
var rawValue = 0;
var stick = Math.floor(this.status & 0x03);
switch (stick) {
default:
case 0:
rawValue = pad.axes[0];
break;
case 1:
rawValue = pad.axes[1];
break;
case 2:
if (pad2) {
rawValue = pad2.axes[0];
} else {
rawValue = pad.axes[2];
}
break;
case 3:
if (pad2) {
rawValue = pad2.axes[1];
} else {
rawValue = pad.axes[3];
}
break;
}
// scale from [1,-1] to [0,0xffff]
val = Math.floor((1 - rawValue) / 2 * 0xffff);
}
this.status = (this.status & 0x0f) | 0x40 | ((val >>> 10) & 0x03);
this.low = val & 0xff;
this.high = (val >>> 8) & 0xff;
this.sysvia.setcb1(false);
};