-
Notifications
You must be signed in to change notification settings - Fork 2
/
node-srec.js
126 lines (119 loc) · 3.03 KB
/
node-srec.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
fs = require('fs');
sprintf = require('sprintf').sprintf;
module.exports.block2srec = block2srec = (a, data) => {
var byte, j, len1, s, sum;
sum = data.length + 5;
sum += a & 0xff;
sum += (a >> 8) & 0xff;
sum += (a >> 16) & 0xff;
sum += (a >> 24) & 0xff;
s = sprintf("S3%02X%08X", data.length + 5, a);
for (j = 0, len1 = data.length; j < len1; j++) {
byte = data[j];
s += sprintf("%02X", byte);
sum += byte;
}
sum = (~sum) & 0xff;
s += sprintf("%02X", sum);
return s;
};
module.exports.parseSrec = parseSrec = (data) => {
var addr, alen, b, boot, byte, dp, i, info, j, k, l, len, len1, len2, max, mem, min, ref, ref1, s, srecs, type;
mem = {};
info = "";
min = max = boot = null;
srecs = data.split("\n");
for (j = 0, len1 = srecs.length; j < len1; j++) {
s = srecs[j];
if (s[0] === "S") {
switch (type = parseInt(s[1])) {
case 0:
case 1:
case 9:
case 5:
alen = 2;
break;
case 2:
case 6:
case 8:
alen = 3;
break;
case 3:
case 7:
alen = 4;
break;
default:
continue;
}
addr = parseInt(s.slice(4, 4 + alen * 2), 16);
dp = 4 + alen * 2;
b = [];
len = parseInt(s.slice(2, 4), 16);
for (i = k = ref = alen + 2, ref1 = len; ref <= ref1 ? k <= ref1 : k >= ref1; i = ref <= ref1 ? ++k : --k) {
b.push(parseInt(s.slice(i * 2, +(i * 2 + 1) + 1 || 9e9), 16));
}
switch (type) {
case 1:
case 2:
case 3:
mem[addr] = b;
if (!min || addr < min) {
min = addr;
}
if (!max || addr + b.length > max) {
max = addr;
}
break;
case 7:
case 8:
case 9:
boot = addr;
break;
case 0:
for (l = 0, len2 = b.length; l < len2; l++) {
byte = b[l];
info += String.fromCharCode(byte);
}
}
}
}
return {
recs: mem,
min: min,
max: max,
boot: boot,
info: info
};
};
module.exports.readSrecFile = (fn) => {
return parseSrec(fs.readFileSync(fn, 'utf8'));
};
module.exports.blockify = (data, min, max, size) => {
var a, as, b, blk, blks, donee, i, j, len, oset, ref, ref1;
blks = {};
donee = false;
for (as in data.recs) {
b = data.recs[as];
a = parseInt(as);
len = b.length;
for (var i = 0; i<len ; i++) {
if (a + i > max || a + i < min) {
if (!donee) {
console.log("Error -- out of range: " + ((a + i).toString(16)) + " [" + (min.toString(16)) + ".." + (max.toString(16)) + "]");
donee = true;
}
continue;
}
blk = Math.floor((a + i - min) / size);
oset = (a + i - min) % size;
if (!blks[blk]) {
blks[blk] = Array.apply(null, new Array(size)).map(Number.prototype.valueOf, 0xff);
}
if (b[i] === void 0) {
console.log("????? ", i, len);
}
blks[blk][oset] = b[i];
}
}
return blks;
};