-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
3381 lines (3316 loc) · 143 KB
/
editor.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
var DapJS =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
exports.readUInt32LE = function (b, idx) {
return (b[idx] |
(b[idx + 1] << 8) |
(b[idx + 2] << 16) |
(b[idx + 3] << 24)) >>> 0;
};
exports.bufferConcat = function (bufs) {
var len = 0;
for (var _i = 0, bufs_1 = bufs; _i < bufs_1.length; _i++) {
var b = bufs_1[_i];
len += b.length;
}
var r = new Uint8Array(len);
len = 0;
for (var _a = 0, bufs_2 = bufs; _a < bufs_2.length; _a++) {
var b = bufs_2[_a];
r.set(b, len);
len += b.length;
}
return r;
};
exports.delay = function (t) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
setTimeout(resolve, t);
})];
});
}); };
exports.addInt32 = function (arr, val) {
if (!arr) {
arr = [];
}
arr.push(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
return arr;
};
exports.hex = function (v) {
return "0x" + v.toString(16);
};
exports.rid = function (v) {
var m = [
"DP_0x0",
"DP_0x4",
"DP_0x8",
"DP_0xC",
"AP_0x0",
"AP_0x4",
"AP_0x8",
"AP_0xC",
];
return m[v] || "?";
};
exports.bank = function (addr) {
var APBANKSEL = 0x000000f0;
return (addr & APBANKSEL) | (addr & 0xff000000);
};
exports.apReg = function (r, mode) {
var v = r | mode | 1 /* AP_ACC */;
return (4 + ((v & 0x0c) >> 2));
};
exports.bufToUint32Array = function (buf) {
exports.assert((buf.length & 3) === 0);
var r = [];
if (!buf.length) {
return r;
}
r[buf.length / 4 - 1] = 0;
for (var i = 0; i < r.length; ++i) {
r[i] = exports.readUInt32LE(buf, i << 2);
}
return r;
};
exports.assert = function (cond) {
if (!cond) {
throw new Error("assertion failed");
}
};
exports.regRequest = function (regId, isWrite) {
if (isWrite === void 0) { isWrite = false; }
var request = !isWrite ? 2 /* READ */ : 0 /* WRITE */;
if (regId < 4) {
request |= 0 /* DP_ACC */;
}
else {
request |= 1 /* AP_ACC */;
}
request |= (regId & 3) << 2;
return request;
};
exports.hexBytes = function (bytes) {
var chk = 0;
var r = ":";
bytes.forEach(function (b) { return chk += b; });
bytes.push((-chk) & 0xff);
bytes.forEach(function (b) { return r += ("0" + b.toString(16)).slice(-2); });
return r.toUpperCase();
};
exports.hex2bin = function (hexstr) {
var array = new Uint8Array(hexstr.length / 2);
for (var i = 0; i < hexstr.length / 2; i++) {
array[i] = parseInt(hexstr.substr(2 * i, 2), 16);
}
return array;
};
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* # Prepared Memory Command
*
* Allows multiple memory operations to be batched together to improve HID
* interface utilisation.
*
* ## Usage
*
* Similarly to `CortexMPreparedCommand` and `DapPreparedCommand`, a convenience
* function exists to quickly create a prepared memory command:
*
* ```typescript
* const prep = core.memory.prepareCommand();
* ```
*
* You can then construct the sequence of commands using the same API as `Memory`.
*
* ```typescript
* prep.write32(0x20000, 1234);
* prep.write32(0x12344, 5678);
* prep.write16(0x12346, 123);
* ```
*
* And then dispatch the prepared commands asynchronously:
*
* ```typescript
* await prep.go();
* ```
*/
var PreparedMemoryCommand = (function () {
function PreparedMemoryCommand(dap) {
this.cmd = dap.prepareCommand();
}
/**
* Schedule a 32-bit memory write operation.
*
* @param addr Word-aligned memory address to write to.
* @param data Number to be written.
*/
PreparedMemoryCommand.prototype.write32 = function (addr, data) {
this.cmd.writeAp(0 /* CSW */, 587202640 /* CSW_VALUE */ | 2 /* CSW_SIZE32 */);
this.cmd.writeAp(4 /* TAR */, addr);
this.cmd.writeAp(12 /* DRW */, data);
};
/**
* Schedule a 16-bit memory write operation.
*
* @param addr Half word-aligned memory address to write to.
* @param data Number to be written.
*/
PreparedMemoryCommand.prototype.write16 = function (addr, data) {
data = data << ((addr & 0x02) << 3);
this.cmd.writeAp(0 /* CSW */, 587202640 /* CSW_VALUE */ | 1 /* CSW_SIZE16 */);
this.cmd.writeAp(4 /* TAR */, addr);
this.cmd.writeAp(12 /* DRW */, data);
};
/**
* Schedule a 32-bit memory read operation.
*
* @param addr Word-aligned memory address to read from.
*/
PreparedMemoryCommand.prototype.read32 = function (addr) {
this.cmd.writeAp(0 /* CSW */, 587202640 /* CSW_VALUE */ | 2 /* CSW_SIZE32 */);
this.cmd.writeAp(4 /* TAR */, addr);
this.cmd.readAp(12 /* DRW */);
};
/**
* Schedule a 16-bit memory read operation.
*
* FIXME: the values need to be shifted after being read.
*
* @param addr Half word-aligned memory address to read from.
*/
PreparedMemoryCommand.prototype.read16 = function (addr) {
this.cmd.writeAp(0 /* CSW */, 587202640 /* CSW_VALUE */ | 1 /* CSW_SIZE16 */);
this.cmd.writeAp(4 /* TAR */, addr);
this.cmd.readAp(12 /* DRW */);
};
/**
* Execute all commands asynchronously.
*/
PreparedMemoryCommand.prototype.go = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.cmd.go()];
});
});
};
return PreparedMemoryCommand;
}());
exports.PreparedMemoryCommand = PreparedMemoryCommand;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var debug_1 = __webpack_require__(5);
var memory_1 = __webpack_require__(7);
var prepared_1 = __webpack_require__(1);
var util_1 = __webpack_require__(0);
var constants_1 = __webpack_require__(3);
var prepared_2 = __webpack_require__(8);
/**
* # Cortex M
*
* Manages access to a CPU core, and its associated memory and debug functionality.
*
* > **NOTE:** all of the methods that involve interaction with the CPU core
* > are asynchronous, so must be `await`ed, or explicitly handled as a Promise.
*
* ## Usage
*
* First, let's create an instance of `CortexM`, using an associated _Debug Access
* Port_ (DAP) instance that we created earlier.
*
* ```typescript
* const core = new CortexM(dap);
* ```
*
* Now, we can halt and resume the core just like this:
*
* > **NOTE:** If you're not using ES2017, you can replace the use of `async` and
* > `await` with direct use of Promises. These examples also need to be run within
* > an `async` function for `async` to be used.
*
* ```typescript
* await core.halt();
* await core.resume();
* ```
*
* Resetting the core is just as easy:
*
* ```typescript
* await core.reset();
* ```
*
* You can even halt immediately after reset:
*
* ```typescript
* await core.reset(true);
* ```
*
* We can also read and write 32-bit values to/from core registers:
*
* ```typescript
* const sp = await core.readCoreRegister(CortexReg.SP);
*
* await core.writeCoreRegister(CortexReg.R0, 0x1000);
* await core.writeCoreRegister(CortexReg.PC, 0x1234);
* ```
*
* ### See also
*
* For details on debugging and memory features, see the documentation for
* `Debug` and `Memory`.
*/
var CortexM = (function () {
function CortexM(device) {
this.dev = device;
this.memory = new memory_1.Memory(device);
this.debug = new debug_1.Debug(this);
}
/**
* Initialise the debug access port on the device, and read the device type.
*/
CortexM.prototype.init = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.dev.init()];
case 1:
_a.sent();
// FIXME: don't run this if security is enabled on the K64F
return [4 /*yield*/, this.debug.init()];
case 2:
// FIXME: don't run this if security is enabled on the K64F
_a.sent();
return [4 /*yield*/, this.readCoreType()];
case 3:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Read the current state of the CPU.
*
* @returns A member of the `CoreState` enum corresponding to the current status of the CPU.
*/
CortexM.prototype.getState = function () {
return __awaiter(this, void 0, void 0, function () {
var dhcsr, newDHCSR;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.memory.read32(3758157296 /* DHCSR */)];
case 1:
dhcsr = _a.sent();
if (!(dhcsr & 33554432 /* S_RESET_ST */)) return [3 /*break*/, 3];
return [4 /*yield*/, this.memory.read32(3758157296 /* DHCSR */)];
case 2:
newDHCSR = _a.sent();
if (newDHCSR & 33554432 /* S_RESET_ST */ && !(newDHCSR & 16777216 /* S_RETIRE_ST */)) {
return [2 /*return*/, 0 /* TARGET_RESET */];
}
_a.label = 3;
case 3:
if (dhcsr & 524288 /* S_LOCKUP */) {
return [2 /*return*/, 1 /* TARGET_LOCKUP */];
}
else if (dhcsr & 262144 /* S_SLEEP */) {
return [2 /*return*/, 2 /* TARGET_SLEEPING */];
}
else if (dhcsr & 131072 /* S_HALT */) {
return [2 /*return*/, 3 /* TARGET_HALTED */];
}
else {
return [2 /*return*/, 4 /* TARGET_RUNNING */];
}
return [2 /*return*/];
}
});
});
};
/**
* Read the CPUID register from the CPU, and interpret its meaning in terms of implementer,
* architecture and core type.
*/
CortexM.prototype.readCoreType = function () {
return __awaiter(this, void 0, void 0, function () {
var cpuid, implementer, arch, coreType;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.memory.read32(3758157056 /* CPUID */)];
case 1:
cpuid = _a.sent();
implementer = ((cpuid & constants_1.CPUID_IMPLEMENTER_MASK) >> constants_1.CPUID_IMPLEMENTER_POS);
arch = ((cpuid & constants_1.CPUID_ARCHITECTURE_MASK) >> constants_1.CPUID_ARCHITECTURE_POS);
coreType = ((cpuid & constants_1.CPUID_PARTNO_MASK) >> constants_1.CPUID_PARTNO_POS);
console.debug("Found an ARM " + constants_1.CoreNames.get(coreType));
return [2 /*return*/, [implementer, arch, coreType]];
}
});
});
};
CortexM.prototype.prepareCommand = function () {
return new prepared_2.PreparedCortexMCommand(this.dev);
};
/**
* Read a core register from the CPU (e.g. r0...r15, pc, sp, lr, s0...)
*
* @param no Member of the `CortexReg` enum - an ARM Cortex CPU general-purpose register.
*/
CortexM.prototype.readCoreRegister = function (no) {
return __awaiter(this, void 0, void 0, function () {
var v;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.memory.write32(3758157300 /* DCRSR */, no)];
case 1:
_a.sent();
return [4 /*yield*/, this.memory.read32(3758157296 /* DHCSR */)];
case 2:
v = _a.sent();
util_1.assert(v & 65536 /* S_REGRDY */);
return [4 /*yield*/, this.memory.read32(3758157304 /* DCRDR */)];
case 3: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Write a 32-bit word to the specified CPU general-purpose register.
*
* @param no Member of the `CortexReg` enum - an ARM Cortex CPU general-purpose register.
* @param val Value to be written.
*/
CortexM.prototype.writeCoreRegister = function (no, val) {
return __awaiter(this, void 0, void 0, function () {
var prep, v;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
prep = new prepared_1.PreparedMemoryCommand(this.dev);
prep.write32(3758157304 /* DCRDR */, val);
prep.write32(3758157300 /* DCRSR */, no | 65536 /* DCRSR_REGWnR */);
prep.read32(3758157296 /* DHCSR */);
return [4 /*yield*/, prep.go()];
case 1:
v = (_a.sent())[0];
util_1.assert(v & 65536 /* S_REGRDY */);
return [2 /*return*/];
}
});
});
};
/**
* Halt the CPU core.
*/
CortexM.prototype.halt = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.memory.write32(3758157296 /* DHCSR */, -1604386816 /* DBGKEY */ | 1 /* C_DEBUGEN */ | 2 /* C_HALT */)];
});
});
};
/**
* Resume the CPU core.
*/
CortexM.prototype.resume = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.isHalted()];
case 1:
if (!_a.sent()) return [3 /*break*/, 4];
return [4 /*yield*/, this.memory.write32(3758157104 /* DFSR */, 4 /* DFSR_DWTTRAP */ | 2 /* DFSR_BKPT */ | 1 /* DFSR_HALTED */)];
case 2:
_a.sent();
return [4 /*yield*/, this.debug.enable()];
case 3:
_a.sent();
_a.label = 4;
case 4: return [2 /*return*/];
}
});
});
};
/**
* Find out whether the CPU is halted.
*/
CortexM.prototype.isHalted = function () {
return __awaiter(this, void 0, void 0, function () {
var s;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.status()];
case 1:
s = _a.sent();
return [2 /*return*/, s.isHalted];
}
});
});
};
/**
* Read the current status of the CPU.
*
* @returns Object containing the contents of the `DHCSR` register, the `DFSR` register, and a boolean value
* stating the current halted state of the CPU.
*/
CortexM.prototype.status = function () {
return __awaiter(this, void 0, void 0, function () {
var prep, results, dhcsr, dfsr;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
prep = new prepared_1.PreparedMemoryCommand(this.dev);
prep.read32(3758157296 /* DHCSR */);
prep.read32(3758157104 /* DFSR */);
return [4 /*yield*/, prep.go()];
case 1:
results = _a.sent();
dhcsr = results[0];
dfsr = results[1];
return [2 /*return*/, {
dfsr: dfsr,
dhscr: dhcsr,
isHalted: !!(dhcsr & 131072 /* S_HALT */),
}];
}
});
});
};
/**
* Reset the CPU core. This currently does a software reset - it is also technically possible to perform a 'hard'
* reset using the reset pin from the debugger.
*/
CortexM.prototype.reset = function (halt) {
if (halt === void 0) { halt = false; }
return __awaiter(this, void 0, void 0, function () {
var demcr;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!halt) return [3 /*break*/, 7];
return [4 /*yield*/, this.halt()];
case 1:
_a.sent();
return [4 /*yield*/, this.memory.read32(3758157308 /* DEMCR */)];
case 2:
demcr = _a.sent();
return [4 /*yield*/, this.memory.write32(3758157308 /* DEMCR */, demcr | 1 /* DEMCR_VC_CORERESET */)];
case 3:
_a.sent();
return [4 /*yield*/, this.softwareReset()];
case 4:
_a.sent();
return [4 /*yield*/, this.waitForHalt()];
case 5:
_a.sent();
// Unset the VC_CORERESET bit
return [4 /*yield*/, this.memory.write32(3758157308 /* DEMCR */, demcr)];
case 6:
// Unset the VC_CORERESET bit
_a.sent();
return [3 /*break*/, 9];
case 7: return [4 /*yield*/, this.softwareReset()];
case 8:
_a.sent();
_a.label = 9;
case 9: return [2 /*return*/];
}
});
});
};
/**
* Run specified machine code natively on the device. Assumes usual C calling conventions
* - returns the value of r0 once the program has terminated. The program _must_ terminate
* in order for this function to return. This can be achieved by placing a `bkpt`
* instruction at the end of the function.
*
* @param code array containing the machine code (32-bit words).
* @param address memory address at which to place the code.
* @param pc initial value of the program counter.
* @param lr initial value of the link register.
* @param sp initial value of the stack pointer.
* @param upload should we upload the code before running it.
* @param args set registers r0...rn before running code
*
* @returns A promise for the value of r0 on completion of the function call.
*/
CortexM.prototype.runCode = function (code, address, pc, lr, sp, upload) {
var args = [];
for (var _i = 6; _i < arguments.length; _i++) {
args[_i - 6] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var cmd, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
cmd = this.prepareCommand();
cmd.halt();
// Point the program counter to the start of the program
cmd.writeCoreRegister(15 /* PC */, pc);
cmd.writeCoreRegister(14 /* LR */, lr);
cmd.writeCoreRegister(13 /* SP */, sp);
for (i = 0; i < args.length; i++) {
cmd.writeCoreRegister(i, args[i]);
}
return [4 /*yield*/, cmd.go()];
case 1:
_a.sent();
if (!upload) return [3 /*break*/, 3];
return [4 /*yield*/, this.memory.writeBlock(address, code)];
case 2:
_a.sent();
_a.label = 3;
case 3:
// Run the program and wait for halt
return [4 /*yield*/, this.resume()];
case 4:
// Run the program and wait for halt
_a.sent();
return [4 /*yield*/, this.waitForHalt(constants_1.DEFAULT_RUNCODE_TIMEOUT)];
case 5:
_a.sent(); // timeout after 10s
return [4 /*yield*/, this.readCoreRegister(0 /* R0 */)];
case 6: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Spin until the chip has halted.
*/
CortexM.prototype.waitForHalt = function (timeout) {
if (timeout === void 0) { timeout = 0; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var running, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
running = true;
if (timeout > 0) {
setTimeout(function () {
if (running) {
reject("waitForHalt timed out.");
running = false;
}
}, timeout);
}
_b.label = 1;
case 1:
_a = running;
if (!_a) return [3 /*break*/, 3];
return [4 /*yield*/, this.isHalted()];
case 2:
_a = !(_b.sent());
_b.label = 3;
case 3:
if (!_a) return [3 /*break*/, 4];
return [3 /*break*/, 1];
case 4:
if (running) {
running = false;
resolve();
}
return [2 /*return*/];
}
});
}); })];
});
});
};
CortexM.prototype.softwareReset = function () {
return __awaiter(this, void 0, void 0, function () {
var dhcsr;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.memory.write32(3758157068 /* NVIC_AIRCR */, 100270080 /* NVIC_AIRCR_VECTKEY */ | 4 /* NVIC_AIRCR_SYSRESETREQ */)];
case 1:
_a.sent();
return [4 /*yield*/, this.memory.read32(3758157296 /* DHCSR */)];
case 2:
dhcsr = _a.sent();
_a.label = 3;
case 3:
if (!((dhcsr & 33554432 /* S_RESET_ST */) !== 0)) return [3 /*break*/, 5];
return [4 /*yield*/, this.memory.read32(3758157296 /* DHCSR */)];
case 4:
dhcsr = _a.sent();
return [3 /*break*/, 3];
case 5: return [2 /*return*/];
}
});
});
};
return CortexM;
}());
exports.CortexM = CortexM;
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_RUNCODE_TIMEOUT = 10000 /* ms */;
exports.CPUID_IMPLEMENTER_MASK = 0xff000000;
exports.CPUID_IMPLEMENTER_POS = 24;
exports.CPUID_VARIANT_MASK = 0x00f00000;
exports.CPUID_VARIANT_POS = 20;
exports.CPUID_ARCHITECTURE_MASK = 0x000f0000;
exports.CPUID_ARCHITECTURE_POS = 16;
exports.CPUID_PARTNO_MASK = 0x0000fff0;
exports.CPUID_PARTNO_POS = 4;
exports.CPUID_REVISION_MASK = 0x0000000f;
exports.CPUID_REVISION_POS = 0;
exports.ISANames = new Map();
exports.ISANames.set(12 /* ARMv6M */, "ARMv6M");
exports.ISANames.set(15 /* ARMv7M */, "ARMv7M");
exports.CoreNames = new Map();
exports.CoreNames.set(3104 /* CortexM0 */, "Cortex-M0");
exports.CoreNames.set(3105 /* CortexM1 */, "Cortex-M1");
exports.CoreNames.set(3107 /* CortexM3 */, "Cortex-M3");
exports.CoreNames.set(3108 /* CortexM4 */, "Cortex-M4");
exports.CoreNames.set(3168 /* CortexM0p */, "Cortex-M0+");
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var cortex_1 = __webpack_require__(2);
exports.CortexM = cortex_1.CortexM;
var constants_1 = __webpack_require__(3);
exports.CoreNames = constants_1.CoreNames;
exports.ISANames = constants_1.ISANames;
var dap_1 = __webpack_require__(9);
exports.DAP = dap_1.default;
var FlashTarget_1 = __webpack_require__(12);
exports.FlashTargets = FlashTarget_1.FlashTargets;
exports.FlashTarget = FlashTarget_1.FlashTarget;
var FlashProgram_1 = __webpack_require__(15);
exports.FlashProgram = FlashProgram_1.FlashProgram;
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var breakpoint_1 = __webpack_require__(6);
/**
* # Debug Interface
*
* Keeps track of breakpoints set on the target, as well as deciding whether to
* use a hardware breakpoint or a software breakpoint.
*
* ## Usage
*
* ```typescript
* const dbg = core.debug;
*
* await dbg.setBreakpoint(0x123456);
*
* // resume the core and wait for the breakpoint
* await core.resume();
* await core.waitForHalt();
*
* // step forward one instruction
* await dbg.step();
*
* // remove the breakpoint
* await dbg.deleteBreakpoint(0x123456);
* ```
*/
var Debug = (function () {
function Debug(core) {
this.core = core;
this.enabled = false;
this.availableHWBreakpoints = [];
this.breakpoints = new Map();
}
Debug.prototype.init = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.setupFpb()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Enable debugging on the target CPU
*/
Debug.prototype.enable = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.core.memory.write32(3758157296 /* DHCSR */, -1604386816 /* DBGKEY */ | 1 /* C_DEBUGEN */)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Set breakpoints at specified memory addresses.
*
* @param addrs An array of memory addresses at which to set breakpoints.
*/
Debug.prototype.setBreakpoint = function (addr) {
return __awaiter(this, void 0, void 0, function () {
var breakpoint, bkpt, regAddr;