forked from levskaya/jslinux-deobfuscated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serial.js
146 lines (143 loc) · 3.6 KB
/
Serial.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
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
Serial Controller Emulator
*/
function Serial(Ng, mem8_loc, kh, lh) {
this.divider = 0;
this.rbr = 0;
this.ier = 0;
this.iir = 0x01;
this.lcr = 0;
this.mcr;
this.lsr = 0x40 | 0x20;
this.msr = 0;
this.scr = 0;
this.set_irq_func = kh;
this.write_func = lh;
this.receive_fifo = "";
Ng.register_ioport_write(0x3f8, 8, 1, this.ioport_write.bind(this));
Ng.register_ioport_read(0x3f8, 8, 1, this.ioport_read.bind(this));
}
Serial.prototype.update_irq = function() {
if ((this.lsr & 0x01) && (this.ier & 0x01)) {
this.iir = 0x04;
} else if ((this.lsr & 0x20) && (this.ier & 0x02)) {
this.iir = 0x02;
} else {
this.iir = 0x01;
}
if (this.iir != 0x01) {
this.set_irq_func(1);
} else {
this.set_irq_func(0);
}
};
Serial.prototype.ioport_write = function(mem8_loc, x) {
mem8_loc &= 7;
switch (mem8_loc) {
default:
case 0:
if (this.lcr & 0x80) {
this.divider = (this.divider & 0xff00) | x;
} else {
this.lsr &= ~0x20;
this.update_irq();
this.write_func(String.fromCharCode(x));
this.lsr |= 0x20;
this.lsr |= 0x40;
this.update_irq();
}
break;
case 1:
if (this.lcr & 0x80) {
this.divider = (this.divider & 0x00ff) | (x << 8);
} else {
this.ier = x;
this.update_irq();
}
break;
case 2:
break;
case 3:
this.lcr = x;
break;
case 4:
this.mcr = x;
break;
case 5:
break;
case 6:
this.msr = x;
break;
case 7:
this.scr = x;
break;
}
};
Serial.prototype.ioport_read = function(mem8_loc) {
var Pg;
mem8_loc &= 7;
switch (mem8_loc) {
default:
case 0:
if (this.lcr & 0x80) {
Pg = this.divider & 0xff;
} else {
Pg = this.rbr;
this.lsr &= ~(0x01 | 0x10);
this.update_irq();
this.send_char_from_fifo();
}
break;
case 1:
if (this.lcr & 0x80) {
Pg = (this.divider >> 8) & 0xff;
} else {
Pg = this.ier;
}
break;
case 2:
Pg = this.iir;
break;
case 3:
Pg = this.lcr;
break;
case 4:
Pg = this.mcr;
break;
case 5:
Pg = this.lsr;
break;
case 6:
Pg = this.msr;
break;
case 7:
Pg = this.scr;
break;
}
return Pg;
};
Serial.prototype.send_break = function() {
this.rbr = 0;
this.lsr |= 0x10 | 0x01;
this.update_irq();
};
Serial.prototype.send_char = function(mh) {
this.rbr = mh;
this.lsr |= 0x01;
this.update_irq();
};
Serial.prototype.send_char_from_fifo = function() {
var nh;
nh = this.receive_fifo;
if (nh != "" && !(this.lsr & 0x01)) {
this.send_char(nh.charCodeAt(0));
this.receive_fifo = nh.substr(1, nh.length - 1);
}
};
Serial.prototype.send_chars = function(na) {
this.receive_fifo += na;
this.send_char_from_fifo();
};