-
Notifications
You must be signed in to change notification settings - Fork 0
/
jor1k-worker-min.js
19728 lines (16855 loc) · 632 KB
/
jor1k-worker-min.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// -------------------------------------------------
// ------------------ UTF8 Helpers -----------------
// -------------------------------------------------
// http://en.wikipedia.org/wiki/UTF-8
"use strict";
function UTF8StreamToUnicode() {
this.stream = new Uint8Array(5);
this.ofs = 0;
this.Put = function(key) {
this.stream[this.ofs] = key;
this.ofs++;
switch(this.ofs) {
case 1:
if (this.stream[0] < 0x80) {
this.ofs = 0;
return this.stream[0];
}
break;
case 2:
if ((this.stream[0]&0xE0) == 0xC0)
if ((this.stream[1]&0xC0) == 0x80) {
this.ofs = 0;
return ((this.stream[0]&0x1F)<<6) |
((this.stream[1]&0x3F)<<0);
}
break;
case 3:
if ((this.stream[0]&0xF0) == 0xE0)
if ((this.stream[1]&0xC0) == 0x80)
if ((this.stream[2]&0xC0) == 0x80) {
this.ofs = 0;
return ((this.stream[0]&0xF ) << 12) |
((this.stream[1]&0x3F) << 6) |
((this.stream[2]&0x3F) << 0);
}
break;
case 4:
if ((this.stream[0]&0xF8) == 0xF0)
if ((this.stream[1]&0xC0) == 0x80)
if ((this.stream[2]&0xC0) == 0x80)
if ((this.stream[3]&0xC0) == 0x80) {
this.ofs = 0;
return ((this.stream[0]&0x7 ) << 18) |
((this.stream[1]&0x3F) << 12) |
((this.stream[2]&0x3F) << 6) |
((this.stream[3]&0x3F) << 0);
}
this.ofs = 0;
return -1; //obviously illegal character, so reset
break;
default:
this.ofs = 0;
return -1;
break;
}
return -1;
}
}
function UnicodeToUTF8Stream(key) {
key = key|0;
if (key < 0x80) {
return [key];
} else
if (key <= 0x7FF) {
return [
(key >> 6) | 0xC0,
(key & 0x3F) | 0x80
];
} else
if (key <= 0xFFFF) {
return [
(key >> 12) | 0xE0,
((key >> 6) & 0x3F) | 0x80,
(key & 0x3F) | 0x80
];
} else
if (key <= 0x10FFFF) {
return [
(key >> 18) | 0xF0,
((key >> 12) & 0x3F) | 0x80,
((key >> 6) & 0x3F) | 0x80,
(key & 0x3F) | 0x80
];
} else {
//message.Debug("Error in utf-8 encoding: Invalid key");
}
return [];
}
function UTF8Length(s)
{
var length = 0;
for(var i=0; i<s.length; i++) {
var key = s.charCodeAt(i);
if (key < 0x80) {
length += 1;
} else
if (key <= 0x7FF) {
length += 2;
} else
if (key <= 0xFFFF) {
length += 3;
} else
if (key <= 0x10FFFF) {
length += 4;
} else {
}
}
return length;
}
module.exports.UTF8StreamToUnicode = UTF8StreamToUnicode;
module.exports.UTF8Length = UTF8Length;
module.exports.UnicodeToUTF8Stream = UnicodeToUTF8Stream;
},{}],2:[function(require,module,exports){
/*
bzip2.js - a small bzip2 decompression implementation
Copyright 2011 by antimatter15 ([email protected])
Based on micro-bunzip by Rob Landley ([email protected]).
Copyright (c) 2011 by antimatter15 ([email protected]).
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var message = require('./messagehandler');
var bzip2 = {};
bzip2.crcTable =
[
0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9,
0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011,
0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81,
0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49,
0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae,
0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066,
0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e,
0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686,
0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f,
0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47,
0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7,
0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f,
0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f,
0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30,
0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088,
0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0,
0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
];
bzip2.array = function(bytes) {
var bit = 0, byte = 0;
var BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF ];
return function(n) {
var result = 0;
while(n > 0) {
var left = 8 - bit;
if (n >= left) {
result <<= left;
result |= (BITMASK[left] & bytes[byte++]);
bit = 0;
n -= left;
} else {
result <<= n;
result |= ((bytes[byte] & (BITMASK[n] << (8 - n - bit))) >> (8 - n - bit));
bit += n;
n = 0;
}
}
return result;
}
}
bzip2.IsBZIP2 = function(buffer) {
if ((buffer[0] == 0x42) && (buffer[1] == 0x5A) && (buffer[2] == 0x68)) return true;
return false;
}
bzip2.simple = function(srcbuffer, stream) {
var bits = bzip2.array(srcbuffer);
var size = bzip2.header(bits);
var ret = false;
var bufsize = 100000 * size;
var buf = new Int32Array(bufsize);
this.byteCount = new Int32Array(256);
this.symToByte = new Uint8Array(256);
this.mtfSymbol = new Int32Array(256);
this.selectors = new Uint8Array(0x8000);
do {
ret = bzip2.decompress(bits, stream, buf, bufsize);
} while(!ret);
}
bzip2.header = function(bits) {
if (bits(8*3) != 4348520) message.Error("No magic number found");
var i = bits(8) - 48;
if (i < 1 || i > 9) message.Error("Not a BZIP archive");
return i;
};
//takes a function for reading the block data (starting with 0x314159265359)
//a block size (0-9) (optional, defaults to 9)
//a length at which to stop decompressing and return the output
bzip2.decompress = function(bits, stream, buf, bufsize) {
var MAX_HUFCODE_BITS = 20;
var MAX_SYMBOLS = 258;
var SYMBOL_RUNA = 0;
var SYMBOL_RUNB = 1;
var GROUP_SIZE = 50;
var crc = 0 ^ (-1);
for(var h = '', i = 0; i < 6; i++) h += bits(8).toString(16);
if (h == "177245385090") return true; //last block
if (h != "314159265359") message.Error("eek not valid bzip data");
var crcblock = bits(32)|0; // CRC code
if (bits(1)) message.Error("unsupported obsolete version");
var origPtr = bits(24);
if (origPtr > bufsize) message.Error("Initial position larger than buffer size");
var t = bits(16);
var symTotal = 0;
for (i = 0; i < 16; i++) {
if (t & (1 << (15 - i))) {
var k = bits(16);
for(j = 0; j < 16; j++) {
if (k & (1 << (15 - j))) {
this.symToByte[symTotal++] = (16 * i) + j;
}
}
}
}
var groupCount = bits(3);
if (groupCount < 2 || groupCount > 6) message.Error("another error");
var nSelectors = bits(15);
if (nSelectors == 0) message.Error("meh");
for(var i = 0; i < groupCount; i++) this.mtfSymbol[i] = i;
for(var i = 0; i < nSelectors; i++) {
for(var j = 0; bits(1); j++) if (j >= groupCount) message.Error("whoops another error");
var uc = this.mtfSymbol[j];
for(var k = j-1; k>=0; k--) {
this.mtfSymbol[k+1] = this.mtfSymbol[k];
}
this.mtfSymbol[0] = uc;
this.selectors[i] = uc;
}
var symCount = symTotal + 2;
var groups = [];
var length = new Uint8Array(MAX_SYMBOLS),
temp = new Uint8Array(MAX_HUFCODE_BITS+1);
var hufGroup;
for(var j = 0; j < groupCount; j++) {
t = bits(5); //lengths
for(var i = 0; i < symCount; i++) {
while(true){
if (t < 1 || t > MAX_HUFCODE_BITS) message.Error("I gave up a while ago on writing error messages");
if (!bits(1)) break;
if (!bits(1)) t++;
else t--;
}
length[i] = t;
}
var minLen, maxLen;
minLen = maxLen = length[0];
for(var i = 1; i < symCount; i++) {
if (length[i] > maxLen) maxLen = length[i];
else if (length[i] < minLen) minLen = length[i];
}
hufGroup = groups[j] = {};
hufGroup.permute = new Int32Array(MAX_SYMBOLS);
hufGroup.limit = new Int32Array(MAX_HUFCODE_BITS + 1);
hufGroup.base = new Int32Array(MAX_HUFCODE_BITS + 1);
hufGroup.minLen = minLen;
hufGroup.maxLen = maxLen;
var base = hufGroup.base.subarray(1);
var limit = hufGroup.limit.subarray(1);
var pp = 0;
for(var i = minLen; i <= maxLen; i++)
for(var t = 0; t < symCount; t++)
if (length[t] == i) hufGroup.permute[pp++] = t;
for(i = minLen; i <= maxLen; i++) temp[i] = limit[i] = 0;
for(i = 0; i < symCount; i++) temp[length[i]]++;
pp = t = 0;
for(i = minLen; i < maxLen; i++) {
pp += temp[i];
limit[i] = pp - 1;
pp <<= 1;
base[i+1] = pp - (t += temp[i]);
}
limit[maxLen] = pp + temp[maxLen] - 1;
base[minLen] = 0;
}
for(var i = 0; i < 256; i++) {
this.mtfSymbol[i] = i;
this.byteCount[i] = 0;
}
var runPos, count, symCount, selector;
runPos = count = symCount = selector = 0;
while(true) {
if (!(symCount--)) {
symCount = GROUP_SIZE - 1;
if (selector >= nSelectors) message.Error("meow i'm a kitty, that's an error");
hufGroup = groups[this.selectors[selector++]];
base = hufGroup.base.subarray(1);
limit = hufGroup.limit.subarray(1);
}
i = hufGroup.minLen;
j = bits(i);
while(true) {
if (i > hufGroup.maxLen) message.Error("rawr i'm a dinosaur");
if (j <= limit[i]) break;
i++;
j = (j << 1) | bits(1);
}
j -= base[i];
if (j < 0 || j >= MAX_SYMBOLS) message.Error("moo i'm a cow");
var nextSym = hufGroup.permute[j];
if (nextSym == SYMBOL_RUNA || nextSym == SYMBOL_RUNB) {
if (!runPos){
runPos = 1;
t = 0;
}
if (nextSym == SYMBOL_RUNA) t += runPos;
else t += 2 * runPos;
runPos <<= 1;
continue;
}
if (runPos) {
runPos = 0;
if (count + t >= bufsize) message.Error("Boom.");
uc = this.symToByte[this.mtfSymbol[0]];
this.byteCount[uc] += t;
while(t--) buf[count++] = uc;
}
if (nextSym > symTotal) break;
if (count >= bufsize) message.Error("I can't think of anything. Error");
i = nextSym - 1;
uc = this.mtfSymbol[i];
for(var k = i-1; k>=0; k--) {
this.mtfSymbol[k+1] = this.mtfSymbol[k];
}
this.mtfSymbol[0] = uc
uc = this.symToByte[uc];
this.byteCount[uc]++;
buf[count++] = uc;
}
if (origPtr < 0 || origPtr >= count) message.Error("I'm a monkey and I'm throwing something at someone, namely you");
var j = 0;
for(var i = 0; i < 256; i++) {
k = j + this.byteCount[i];
this.byteCount[i] = j;
j = k;
}
for(var i = 0; i < count; i++) {
uc = buf[i] & 0xff;
buf[this.byteCount[uc]] |= (i << 8);
this.byteCount[uc]++;
}
var pos = 0, current = 0, run = 0;
if (count) {
pos = buf[origPtr];
current = (pos & 0xff);
pos >>= 8;
run = -1;
}
count = count;
var copies, previous, outbyte;
while(count) {
count--;
previous = current;
pos = buf[pos];
current = pos & 0xff;
pos >>= 8;
if (run++ == 3) {
copies = current;
outbyte = previous;
current = -1;
} else {
copies = 1;
outbyte = current;
}
while(copies--) {
crc = ((crc << 8) ^ this.crcTable[((crc>>24) ^ outbyte) & 0xFF])&0xFFFFFFFF; // crc32
stream(outbyte);
}
if (current != previous) run = 0;
}
crc = (crc ^ (-1)) >>> 0;
if ((crc|0) != (crcblock|0)) message.Error("Error in bzip2: crc32 do not match");
return false;
}
module.exports = bzip2;
},{"./messagehandler":28}],3:[function(require,module,exports){
// -------------------------------------------------
// --------------------- ATA -----------------------
// -------------------------------------------------
"use strict";
var utils = require('../utils');
var message = require('../messagehandler');
// ata-generic implementation (according to Linux)
// simulation of a hard disk loaded on demand from the webserver in small chunks.
// specification
// ftp://ftp.seagate.com/pub/acrobat/reference/111-1c.pdf
/* use this dts lines
ata@9e000000 {
compatible = "ata-generic";
reg = <0x9e000000 0x100
0x9e000100 0xf00>;
pio-mode = <4>;
reg-shift = <2>;
interrupts = <15>;
};
*/
// ATA command block registers
// 2 is the reg_shift
var ATA_REG_DATA = 0x00<<2; // data register
var ATA_REG_ERR = 0x01<<2; // error register, feature register
var ATA_REG_NSECT = 0x02<<2; // sector count register
var ATA_REG_LBAL = 0x03<<2; // sector number register
var ATA_REG_LBAM = 0x04<<2; // cylinder low register
var ATA_REG_LBAH = 0x05<<2; // cylinder high register
var ATA_REG_DEVICE = 0x06<<2; // drive/head register
var ATA_REG_STATUS = 0x07<<2; // status register // command register
var ATA_REG_FEATURE = ATA_REG_ERR; // and their aliases (writing)
var ATA_REG_CMD = ATA_REG_STATUS;
var ATA_REG_BYTEL = ATA_REG_LBAM;
var ATA_REG_BYTEH = ATA_REG_LBAH;
var ATA_REG_DEVSEL = ATA_REG_DEVICE;
var ATA_REG_IRQ = ATA_REG_NSECT;
// device control register
var ATA_DCR_RST = 0x04; // Software reset (RST=1, reset)
var ATA_DCR_IEN = 0x02; // Interrupt Enable (IEN=0, enabled)
// ----- ATA (Alternate) Status Register
var ATA_SR_BSY = 0x80; // Busy
var ATA_SR_DRDY = 0x40; // Device Ready
var ATA_SR_DF = 0x20; // Device Fault
var ATA_SR_DSC = 0x10; // Device Seek Complete
var ATA_SR_DRQ = 0x08; // Data Request
var ATA_SR_COR = 0x04; // Corrected data (obsolete)
var ATA_SR_IDX = 0x02; // (obsolete)
var ATA_SR_ERR = 0x01; // Error
// constructor
function ATADev(intdev) {
this.intdev = intdev;
var buffer = new ArrayBuffer(512);
this.identifybuffer = new Uint16Array(buffer);
this.Reset();
var buffer = new ArrayBuffer(64*1024); // 64 kB
this.SetBuffer(buffer);
}
ATADev.prototype.Reset = function() {
this.DCR = 0x8; // fourth bis is always set
this.DR = 0xA0; // some bits are always set to one
this.SCR = 0x1;
this.SNR = 0x1;
this.SR = ATA_SR_DRDY; // status register
this.FR = 0x0; // Feature register
this.ER = 0x1; // Error register
this.CR = 0x0; // Command register
//this.error = 0x1;
this.lcyl = 0x0;
this.hcyl = 0x0;
this.select = 0xA0;
this.driveselected = true; // drive no 0
this.readbuffer = this.identifybuffer;
this.readbufferindex = 0;
this.readbuffermax = 256;
}
ATADev.prototype.SetBuffer = function(buffer) {
this.diskbuffer = new Uint16Array(buffer);
this.heads = 16;
this.sectors = 64;
this.cylinders = buffer.byteLength/(this.heads*this.sectors*512);
this.nsectors = this.heads*this.sectors*this.cylinders;
this.BuildIdentifyBuffer(this.identifybuffer);
}
ATADev.prototype.BuildIdentifyBuffer = function(buffer16)
{
for(var i=0; i<256; i++) {
buffer16[i] = 0x0000;
}
buffer16[0] = 0x0040;
buffer16[1] = this.cylinders; // cylinders
buffer16[3] = this.heads; // heads
buffer16[4] = 512*this.sectors; // Number of unformatted bytes per track (sectors*512)
buffer16[5] = 512; // Number of unformatted bytes per sector
buffer16[6] = this.sectors; // sectors per track
buffer16[20] = 0x0003; // buffer type
buffer16[21] = 512; // buffer size in 512 bytes increment
buffer16[22] = 4; // number of ECC bytes available
buffer16[27] = 0x6A6F; // jo (model string)
buffer16[28] = 0x7231; // r1
buffer16[29] = 0x6B2D; // k-
buffer16[30] = 0x6469; // di
buffer16[31] = 0x736B; // sk
for(var i=32; i<=46; i++) {
buffer16[i] = 0x2020; // (model string)
}
buffer16[47] = 0x8000 | 128;
buffer16[48] = 0x0000;
buffer16[49] = 1<<9;
buffer16[51] = 0x200; // PIO data transfer cycle timing mode
buffer16[52] = 0x200; // DMA data transfer cycle timing mode
buffer16[54] = this.cylinders;
buffer16[55] = this.heads;
buffer16[56] = this.sectors; // sectors per track
buffer16[57] = (this.nsectors >> 0)&0xFFFF; // number of sectors
buffer16[58] = (this.nsectors >>16)&0xFFFF;
buffer16[59] = 0x0000; // multiple sector settings
//buffer16[59] = 0x100 | 128;
buffer16[60] = (this.nsectors >> 0)&0xFFFF; // Total number of user-addressable sectors low
buffer16[61] = (this.nsectors >>16)&0xFFFF; // Total number of user-addressable sectors high
buffer16[80] = (1<<1)|(1<<2); // version, support ATA-1 and ATA-2
buffer16[82] = (1<<14); // Command sets supported. (NOP supported)
buffer16[83] = (1<<14); // this bit should be set to one
buffer16[84] = (1<<14); // this bit should be set to one
buffer16[85] = (1<<14); // Command set/feature enabled (NOP)
buffer16[86] = 0; // Command set/feature enabled
buffer16[87] = (1<<14); // Shall be set to one
}
ATADev.prototype.ReadReg8 = function(addr) {
if (!this.driveselected) {
return 0xFF;
}
switch(addr)
{
case ATA_REG_ERR:
//message.Debug("ATADev: read error register");
return this.ER;
case ATA_REG_NSECT:
//message.Debug("ATADev: read sector count register");
return this.SNR;
case ATA_REG_LBAL:
//message.Debug("ATADev: read sector number register");
return this.SCR;
case ATA_REG_LBAM:
//message.Debug("ATADev: read cylinder low register");
return this.lcyl;
case ATA_REG_LBAH:
//message.Debug("ATADev: read cylinder high register");
return this.hcyl;
case ATA_REG_DEVICE:
//message.Debug("ATADev: read drive/head register");
return this.DR;
case ATA_REG_STATUS:
//message.Debug("ATADev: read status register");
this.intdev.ClearInterrupt(15);
return this.SR;
case 0x100: // device control register, but read as status register
//message.Debug("ATADev: read alternate status register")
return this.SR;
break;
default:
message.Debug("ATADev: Error in ReadRegister8: register " + utils.ToHex(addr) + " not supported");
message.Abort();
break;
}
return 0x0;
};
ATADev.prototype.GetSector = function()
{
if (!(this.DR & 0x40)) {
message.Debug("ATADev: CHS mode not supported");
message.Abort();
}
return ((this.DR&0x0F) << 24) | (this.hcyl << 16) | (this.lcyl << 8) | this.SCR;
}
ATADev.prototype.SetSector = function(sector)
{
if (!(this.DR & 0x40)) {
message.Debug("ATADev: CHS mode not supported");
message.Abort();
}
this.SCR = sector & 0xFF;
this.lcyl = (sector >> 8) & 0xFF;
this.hcyl = (sector >> 16) & 0xFF;
this.DR = (this.DR & 0xF0) | ((sector >> 24) & 0x0F);
}
ATADev.prototype.ExecuteCommand = function()
{
switch(this.CR)
{
case 0xEC: // identify device
this.readbuffer = this.identifybuffer;
this.readbufferindex = 0;
this.readbuffermax = 256;
this.SR = ATA_SR_DRDY | ATA_SR_DSC | ATA_SR_DRQ;
if (!(this.DCR & ATA_DCR_IEN)) {
this.intdev.RaiseInterrupt(15);
}
break;
case 0x91: // initialize drive parameters
this.SR = ATA_SR_DRDY | ATA_SR_DSC;
this.ER = 0x0;
if (!(this.DCR & ATA_DCR_IEN)) {
this.intdev.RaiseInterrupt(15);
}
break;
case 0x20: // load sector
case 0x30: // save sector
var sector = this.GetSector();
if (this.SNR == 0) {
this.SNR = 256;
}
//message.Debug("ATADev: Load sector " + utils.ToHex(sector) + ". number of sectors " + utils.ToHex(this.SNR));
this.readbuffer = this.diskbuffer;
this.readbufferindex = sector*256;
this.readbuffermax = this.readbufferindex+256;
this.SR = ATA_SR_DRDY | ATA_SR_DSC | ATA_SR_DRQ;
this.ER = 0x0;
if (this.CR == 0x20) {
if (!(this.DCR & ATA_DCR_IEN)) {
this.intdev.RaiseInterrupt(15);
}
}
break;
case 0xC4: // read multiple sectors
case 0xC5: // write multiple sectors
var sector = this.GetSector();
if (this.SNR == 0) {
this.SNR = 256;
}
//message.Debug("ATADev: Load multiple sector " + utils.ToHex(sector) + ". number of sectors " + utils.ToHex(this.SNR));
this.readbuffer = this.diskbuffer;
this.readbufferindex = sector*256;
this.readbuffermax = this.readbufferindex + 256*this.SNR;
this.SR = ATA_SR_DRDY | ATA_SR_DSC | ATA_SR_DRQ;
this.ER = 0x0;
if (this.CR == 0xC4) {
if (!(this.DCR & ATA_DCR_IEN)) {
this.intdev.RaiseInterrupt(15);
}
}
break;
default:
message.Debug("ATADev: Command " + utils.ToHex(this.CR) + " not supported");
message.Abort();
break;
}
}
ATADev.prototype.WriteReg8 = function(addr, x) {
if (addr == ATA_REG_DEVICE) {
//message.Debug("ATADev: Write drive/head register value: " + utils.ToHex(x));
this.DR = x;
//message.Debug("Head " + (x&0xF));
//message.Debug("Drive No. " + ((x>>4)&1));
//message.Debug("LBA Mode " + ((x>>6)&1));
this.driveselected = ((x>>4)&1)?false:true;
return;
}
if (addr == 0x100) { //device control register
//message.Debug("ATADev: Write CTL register" + " value: " + utils.ToHex(x));
if (!(x&ATA_DCR_RST) && (this.DCR&ATA_DCR_RST)) { // reset done
//message.Debug("ATADev: drive reset done");
this.DR &= 0xF0; // reset head
this.SR = ATA_SR_DRDY | ATA_SR_DSC;
this.SCR = 0x1;
this.SNR = 0x1;
this.lcyl = 0x0;
this.hcyl = 0x0;
this.ER = 0x1;
this.CR = 0x0;
} else
if ((x&ATA_DCR_RST) && !(this.DCR&ATA_DCR_RST)) { // reset
//message.Debug("ATADev: drive reset");
this.ER = 0x1; // set diagnostics message
this.SR = ATA_SR_BSY | ATA_SR_DSC;
}
this.DCR = x;
return;
}
if (!this.driveselected) {
return;
}
switch(addr)
{
case ATA_REG_FEATURE:
//message.Debug("ATADev: Write feature register value: " + utils.ToHex(x));
this.FR = x;
break;
case ATA_REG_NSECT:
//message.Debug("ATADev: Write sector count register value: " + utils.ToHex(x));
this.SNR = x;
break;
case ATA_REG_LBAL:
//message.Debug("ATADev: Write sector number register value: " + utils.ToHex(x));
this.SCR = x;
break;
case ATA_REG_LBAM:
//message.Debug("ATADev: Write cylinder low register value: " + utils.ToHex(x));
this.lcyl = x;
break;
case ATA_REG_LBAH:
//message.Debug("ATADev: Write cylinder high number register value: " + utils.ToHex(x));
this.hcyl = x;
break;
case ATA_REG_CMD:
//message.Debug("ATADev: Write Command register " + utils.ToHex(x));
this.CR = x;
this.ExecuteCommand();
break;
default:
message.Debug("ATADev: Error in WriteRegister8: register " + utils.ToHex(addr) + " not supported (value: " + utils.ToHex(x) + ")");
message.Abort();
break;
}
};
ATADev.prototype.ReadReg16 = function(addr) {
if (addr != 0) { // data register
message.Debug("ATADev: Error in ReadRegister16: register " + utils.ToHex(addr) + " not supported");
message.Abort();
}
var val = utils.Swap16(this.readbuffer[this.readbufferindex]);
//message.Debug("ATADev: read data register");
this.readbufferindex++;
if (this.readbufferindex >= this.readbuffermax) {
this.SR = ATA_SR_DRDY | ATA_SR_DSC; // maybe no DSC for identify command but it works
if ((this.CR == 0x20) && (this.SNR > 1)) {
this.SNR--;
this.SetSector(this.GetSector() + 1);
this.readbuffermax += 256;
this.SR = ATA_SR_DRDY | ATA_SR_DSC | ATA_SR_DRQ;
if (!(this.DCR & ATA_DCR_IEN)) {
this.intdev.RaiseInterrupt(15);
}
}
}
return val;
};
ATADev.prototype.WriteReg16 = function(addr, x) {
if (addr != 0) { // data register
message.Debug("ATADev: Error in WriteRegister16: register " + utils.ToHex(addr) + " not supported");
message.Abort();
}
this.readbuffer[this.readbufferindex] = utils.Swap16(x);
//message.Debug("ATADev: write data register");
this.readbufferindex++;
if (this.readbufferindex >= this.readbuffermax) {
this.SR = ATA_SR_DRDY | ATA_SR_DSC;
if (!(this.DCR & ATA_DCR_IEN)) {
this.intdev.RaiseInterrupt(15);
}
if ((this.CR == 0x30) && (this.SNR > 1)) {
this.SNR--;
this.SetSector(this.GetSector() + 1);
this.readbuffermax += 256;
this.SR = ATA_SR_DRDY | ATA_SR_DSC | ATA_SR_DRQ;
}
}
};
ATADev.prototype.ReadReg32 = function(addr) {
message.Debug("ATADev: Error in ReadRegister32: register " + utils.ToHex(addr) + " not supported");
this.mesage.Abort();
};
ATADev.prototype.WriteReg32 = function(addr, x) {
message.Debug("ATADev: Error in WriteRegister32: register " + utils.ToHex(addr) + " not supported");
message.Abort()
};
module.exports = ATADev;
},{"../messagehandler":28,"../utils":43}],4:[function(require,module,exports){
// -------------------------------------------------
// ----------------- Ethernet ----------------------
// -------------------------------------------------
// Emulation of the OpenCores ethmac ethernet controller.
"use strict";
var message = require('../messagehandler');
var utils = require('../utils');
//REGISTER ADDRESSES
var ETHMAC_ADDR_MODER = 0x0;
var ETHMAC_ADDR_INT_SOURCE = 0x4;
var ETHMAC_ADDR_INT_MASK = 0x8;
var ETHMAC_ADDR_IPGT = 0xC;
var ETHMAC_ADDR_IPGR1 = 0x10;
var ETHMAC_ADDR_IPGR2 = 0x14;
var ETHMAC_ADDR_PACKETLEN = 0x18;
var ETHMAC_ADDR_COLLCONF = 0x1C;
var ETHMAC_ADDR_TX_BD_NUM = 0x20;
var ETHMAC_ADDR_CTRLMODER = 0x24;
var ETHMAC_ADDR_MIIMODER = 0x28;
var ETHMAC_ADDR_MIICOMMAND = 0x2C;
var ETHMAC_ADDR_MIIADDRESS = 0x30;
var ETHMAC_ADDR_MIITX_DATA = 0x34;
var ETHMAC_ADDR_MIIRX_DATA = 0x38;
var ETHMAC_ADDR_MIISTATUS = 0x3C;
var ETHMAC_ADDR_MAC_ADDR0 = 0x40;
var ETHMAC_ADDR_MAC_ADDR1 = 0x44;
var ETHMAC_ADDR_ETH_HASH0_ADR = 0x48;
var ETHMAC_ADDR_ETH_HASH1_ADR = 0x4C;
var ETHMAC_ADDR_ETH_TXCTRL = 0x50;
var ETHMAC_ADDR_BD_START = 0x400;
var ETHMAC_ADDR_BD_END = 0x7FF;
var MII_BMCR = 0x00; /* Basic mode control register */
var MII_BMSR = 0x01; /* Basic mode status register */
var MII_PHYSID1 = 0x02; /* PHYS ID 1 */
var MII_PHYSID2 = 0x03; /* PHYS ID 2 */
var MII_ADVERTISE = 0x04; /* Advertisement control reg */
var MII_LPA = 0x05; /* Link partner ability reg */
var MII_EXPANSION = 0x06; /* Expansion register */
var MII_CTRL1000 = 0x09; /* 1000BASE-T control */
var MII_STAT1000 = 0x0a; /* 1000BASE-T status */
var MII_ESTATUS = 0x0f; /* Extended Status */
var MII_DCOUNTER = 0x12; /* Disconnect counter */
var MII_FCSCOUNTER = 0x13; /* False carrier counter */
var MII_NWAYTEST = 0x14; /* N-way auto-neg test reg */
var MII_RERRCOUNTER = 0x15; /* Receive error counter */
var MII_SREVISION = 0x16; /* Silicon revision */
var MII_RESV1 = 0x17; /* Reserved... */
var MII_LBRERROR = 0x18; /* Lpback, rx, bypass error */
var MII_PHYADDR = 0x19; /* PHY address */
var MII_RESV2 = 0x1a; /* Reserved... */
var MII_TPISTATUS = 0x1b; /* TPI status for 10mbps */
var MII_NCONFIG = 0x1c; /* Network interface config */
//TODO: MODER.LOOPBCK - loopback support
//TODO: Carrier Sense?
//TODO: Huge frames
//TODO: IAM mode
//TODO: MODER.BRO
function EthDev(ram, intdev, mac) {
"use strict";
this.ram = ram;
this.intdev = intdev;
this.TransmitCallback = function(data){}; // Should call handler to send data asynchronously.
this.toTxStat = function(val) {
return {
LEN: val >>> 16,
RD: (val >>> 15) & 1,
IRQ: (val >>> 14) & 1,
WR: (val >>> 13) & 1,
PAD: (val >>> 12) & 1,
CRC: (val >>> 11) & 1,
UR: (val >>> 8) & 1,
RTRY: (val >>> 4) & 0xF,
RL: (val >>> 3) & 1,