-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpc8xx_debug.py
1329 lines (1028 loc) · 47.4 KB
/
mpc8xx_debug.py
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
import time
from itertools import islice
from collections.abc import Sequence
from struct import Struct
from traceback import print_stack
import subprocess
import signal
from contextlib import contextmanager
# from ch341 import CH341A
from pyftdi.spi import SpiController
from pyftdi.gpio import GpioAsyncController
DEBUG=False
# DEBUG=True
dev_url = "ftdi://::1/"
spi_freq = 0.5e6
last_cmd = 0xffffffff
load_addr = 0xFFF03000
ram_buf = 0xFFF02000
flash_base = 0x0
def cdiv(x, y):
return (x + y - 1) // y
def batched(iterable, n):
it = iter(iterable)
while True:
batch = tuple(islice(it, n))
if not batch:
break
yield batch
def print_hex(data):
for i, d in enumerate(data):
if i % 8 == 0:
if i != 0:
print("")
print(f"{i:02x}: ", end="")
print(f"{d:02x} ", end="")
print("")
def print_mem_buf(addr, data, print_abs=True):
for i, d in enumerate(data):
if print_abs:
a = f"{addr+i*4:08x}"
else:
a = f"{i:4d}"
print(f"{a}: {d:08x} ")
@contextmanager
def measure_time(count=1):
from datetime import timedelta
import time
start = time.perf_counter()
try:
yield
finally:
t = time.perf_counter()-start
print(f"Ready in {timedelta(seconds=t)}", end='')
if count > 1:
print(f", {count/t:.3f} tr/s")
else:
print()
class DelayedKeyboardInterrupt:
def __enter__(self):
self.signal_received = False
self.old_handler = signal.signal(signal.SIGINT, self.handler)
def handler(self, sig, frame):
self.signal_received = (sig, frame)
def __exit__(self, type, value, traceback):
signal.signal(signal.SIGINT, self.old_handler)
if self.signal_received:
self.old_handler(*self.signal_received)
PIN_SRESET = 1 << 4
PIN_HRESET = 1 << 5
PIN_FROZEN = 1 << 6
spi_ctrl = SpiController()
spi_ctrl.configure(
dev_url,
direction=PIN_SRESET|PIN_HRESET,
initial=PIN_SRESET|PIN_HRESET,
frequency=spi_freq,
cs_count=1,
turbo=True,
debug=True,
)
spi = spi_ctrl.get_port(0, mode=0)
gpio = spi_ctrl.get_gpio()
usb_dev = spi_ctrl.ftdi.usb_dev
print(f"[{usb_dev.idVendor:04x}:{usb_dev.idProduct:04x}] {usb_dev.manufacturer} {usb_dev.product} ({usb_dev.serial_number})")
print(f"Chip: {spi_ctrl.ftdi.ic_name}")
spi_ctrl.ftdi.reset()
def enter_debug(hard_reset=False, immediatly_stop=True):
def set_gpio(pin, val):
v = spi_ctrl.read_gpio(True) & spi_ctrl.direction
if val:
v |= pin
else:
v &= ~pin
spi_ctrl.write_gpio(v)
# CLK = immediatly_stop - set immediatly debug mode
# MOSI = 0 - set asynchronous clocked mode
cfg = spi_ctrl.SCK_BIT if immediatly_stop else 0
spi_ctrl.force_control(spi_ctrl.frequency, bytes([cfg])) # set soft reset state pins
set_gpio(PIN_SRESET, False) # assert SRESET
time.sleep(1.1)
if hard_reset:
set_gpio(PIN_HRESET, False) # assert HRESET
time.sleep(0.1)
set_gpio(PIN_HRESET, True) # deassert HRESET
time.sleep(0.1)
set_gpio(PIN_SRESET, True) # deassert SRESET
time.sleep(0.1)
spi_ctrl.force_control(spi_ctrl.frequency, bytes([0x00]))
def finish_debug():
cmd_nop()
spi_ctrl.close()
gpio = GpioAsyncController()
gpio.configure(dev_url, direction=0x00)
gpio.close(freeze=True)
def wait_ready():
while True:
with DelayedKeyboardInterrupt():
res = spi.exchange(bytes([0]), readlen=1, duplex=True, start=False, stop=False)
if res[0] == 0:
return
def wait_debug(timeout=None):
start = time.time()
try:
while True:
res = instr([nop(), nop()], silent=True)
status = res[1][0]
cmd_nop(silent=True)
if status != 1:
return True
if timeout is not None and time.time() - start > timeout:
print("wait_debug timeout")
return False
except KeyboardInterrupt:
cmd_nop(silent=True)
print("wait_debug interrupted")
return False
def do_debug_transfer(mode, ctrl, data, silent=False, synchronious=False):
global last_cmd
chunk_size = 5 if synchronious else 4096
if not isinstance(data, Sequence):
data = [data]
ctrl_byte = 0
if mode: ctrl_byte |= 0x2
if ctrl: ctrl_byte |= 0x1
wbuf = []
for d in data:
wbuf += [ctrl_byte | 0x4] # start bit
if mode:
d <<= 25
wbuf += Struct('>I').pack(d)
rbuf = []
for b in batched(wbuf, chunk_size):
b = bytes(b)
if synchronious:
with DelayedKeyboardInterrupt():
wait_ready()
with DelayedKeyboardInterrupt():
rbuf += spi.exchange(b, readlen=len(b), duplex=True, start=False, stop=False)
res = tuple(Struct('>BI').iter_unpack(bytes(rbuf)))
if not silent:
for i, (status, rdata) in enumerate(res):
if status not in [0, 3]:
if i>0:
err_ctrl = ctrl_byte
err_cmd = data[i-1]
else:
err_ctrl = last_cmd >> 32
err_cmd = last_cmd & 0xFFFFFFFF
if status & 0x80:
raise Exception(f"status overrun ({status:x}) in cmd {err_ctrl:x}.{err_cmd:08x}")
print(f"!!! error status: {status:x} in cmd {err_ctrl:x}.{err_cmd:08x}")
print_stack()
if DEBUG:
print(f"send: {ctrl_byte:x}.{data[i]:08x}; recv: {status:x}.{rdata:08x}")
last_cmd = (ctrl_byte<<32) | data[-1]
return res
def instr(data, *v, **kv):
return do_debug_transfer(0, 0, data, *v, **kv)
def op_immd(op, ra, rb, data):
ret = op
ret |= (ra & 0x1F) << 21
ret |= (rb & 0x1F) << 16
ret |= (data & 0xFFFF) << 0
return ret
def lis(dest_gpr, data):
return op_immd(0x3C000000, dest_gpr, 0, data)
def li(dest_gpr, data):
return op_immd(0x38000000, dest_gpr, 0, data)
def ori(dest_gpr, src_gpr, data):
return op_immd(0x60000000, src_gpr, dest_gpr, data)
def addi(dest_gpr, src_gpr, data):
return op_immd(0x38000000, dest_gpr, src_gpr, data)
def subi(dest_gpr, src_gpr, data):
addi(dest_gpr, src_gpr, -data)
def stw(src_gpr, ofs, dst_addr_gpr):
return op_immd(0x90000000, src_gpr, dst_addr_gpr, ofs)
def stwu(src_gpr, ofs, dst_addr_gpr):
return stw(src_gpr, ofs, dst_addr_gpr) | 0x04000000
def lwz(dst_gpr, ofs, src_addr_gpr):
return op_immd(0x80000000, dst_gpr, src_addr_gpr, ofs)
def lwzu(dst_gpr, ofs, src_addr_gpr):
return lwz(dst_gpr, ofs, src_addr_gpr) | 0x04000000
def mfmsr(dest_gpr):
return op_immd(0x7C0000A6, dest_gpr, 0, 0)
def mtmsr(gpr):
return op_immd(0x7C000124, gpr, 0, 0)
def mfspr(dest_gpr, spr):
spr_data = ((spr & 0x1F) << 16) | ((spr & 0x3E0) << 6)
return op_immd(0x7C0002A6, dest_gpr, 0, 0) | spr_data
def mtspr(spr, src_gpr):
spr_data = ((spr & 0x1F) << 16) | ((spr & 0x3E0) << 6)
return op_immd(0x7C0003A6, src_gpr, 0, 0) | spr_data
def trap():
return 0x7FE00008
def twi(op, gpr, val):
return op_immd(0x0c000000, op, gpr, val)
def rfi():
return 0x4C000064
def b(ofs):
ofs &= 0x3FFFFFC
return 0x48000000 | ofs
def bl(ofs):
return b(ofs) | 0x1
def blr(ofs):
return 0x4e800020
def nop():
return 0x60000000
@contextmanager
def borrow_gprs(*gpr_list):
gprs = {gpr: get_gpr(gpr) for gpr in gpr_list}
try:
yield
finally:
for gpr, val in gprs.items():
set_gpr(gpr, val)
def set_gpr(gpr, data):
instr([
lis(gpr, data >> 16),
ori(gpr, gpr, data),
])
def get_gpr(gpr):
res = instr([
mtspr(630, gpr),
nop(),
])
return res[-1][1]
def set_spr(spr, data):
gpr1 = 12
with borrow_gprs(gpr1):
instr([
lis(gpr1, data >> 16),
ori(gpr1, gpr1, data),
mtspr(spr, gpr1),
])
def get_spr(spr):
gpr1=12
with borrow_gprs(gpr1):
res = instr([
mfspr(gpr1, spr),
mtspr(630, gpr1),
nop(),
])
return res[-1][1]
def set_msr(data):
gpr1=12
with borrow_gprs(gpr1):
instr([
lis(gpr1, data >> 16),
ori(gpr1, gpr1, data),
mtmsr(gpr1),
])
def get_msr():
gpr1=12
with borrow_gprs(gpr1):
res = instr([
mfmsr(gpr1),
mtspr(630, gpr1),
nop(),
])
return res[-1][1]
def write_ram(addr, data):
gpr1, gpr2 = 12, 11
if not isinstance(data, Sequence):
data = [data]
addr -= 4 # due to stwu
wbuf = [
lis(gpr1, addr >> 16),
ori(gpr1, gpr1, addr),
]
for d in data:
wbuf += [
lis(gpr2, d >> 16),
ori(gpr2, gpr2, d),
stwu(gpr2, 4, gpr1),
]
with borrow_gprs(gpr1, gpr2):
instr(wbuf)
def write_ram_fast(addr, data):
addr -= 4 # due to fast download procedure
with borrow_gprs(30, 31):
set_gpr(30, addr)
cmd_set_download_mode(True)
cmd_data(data)
cmd_set_download_mode(False)
def read_ram(addr, count=1):
gpr1, gpr2 = 12, 11
addr -= 4 # due to lwzu
wbuf = [
lis(gpr1, addr >> 16),
ori(gpr1, gpr1, addr),
]
for _ in range(count):
wbuf += [
lwzu(gpr2, 4, gpr1),
mtspr(630, gpr2),
]
wbuf += [nop()]
with borrow_gprs(gpr1, gpr2):
ret = instr(wbuf)
# print(ret)
ret = tuple(x[1] for x in ret[4::2]) # each 2nd word started from 4th
return ret
def print_ram(addr, count=1, print_abs=True):
res = read_ram(addr, count)
print_mem_buf(addr, res, print_abs=print_abs)
def write_ram_inside(addr, data):
load_addr = 0xfff02000
if not isinstance(data, Sequence):
data = [data]
write_ram(load_addr, [
stwu(30, 4, 31),
trap(),
])
with borrow_gprs(30, 31):
set_gpr(31, addr - 4)
for d in data:
set_gpr(30, d)
go_to_addr(load_addr)
if not wait_stop(timeout=1):
raise Exception("write_ram_inside is hang")
def read_ram_inside(addr, cnt=1):
load_addr = 0xfff02000
write_ram(load_addr, [
lwzu(30, 4, 31),
trap(),
])
rdata = []
with borrow_gprs(30, 31):
set_gpr(31, addr - 4)
for _ in range(cnt):
go_to_addr(load_addr)
if not wait_stop(timeout=1):
raise Exception("write_ram_inside is hang")
rdata += [get_gpr(30)]
return rdata
def print_ram_inside(addr, cnt=1):
res = read_ram_inside(addr, cnt)
print_mem_buf(addr, res)
def cmd_nop(*v, **kv):
return do_debug_transfer(1,1,0x0, *v,**kv)
def cmd_hard_reset():
do_debug_transfer(1,1,0x1)
def cmd_soft_reset():
do_debug_transfer(1,1,0x1)
def cmd_data(data):
do_debug_transfer(0,1,data)
def cmd_set_download_mode(val):
if val:
do_debug_transfer(1,1,0x63)
else:
do_debug_transfer(1,1,0x43)
cmd_data([0x0])
def cmd_nmi_trap():
do_debug_transfer(1,1,0x1F)
do_debug_transfer(1,1,0x7F)
def bp_set_breakpoint(addr):
set_spr(144, addr) # CMPA
set_spr(158, (0x4<<29) | (0x2<<18) | (0x1<<11)) # ICTRL = CTA=Equal,IW0=Match A,SIW0EN
def bp_continue(next=True):
if next:
set_spr(26, get_spr(26)+4) # SRR0
get_spr(148) # reset ICR
instr( rfi() ) # go
def bp_go():
bp_continue(next=False)
def print_gpr(r):
print(f">> r{r} = {get_gpr(r):08x}")
def print_gprs():
for i in range(0, 31+1):
print_gpr(i)
def print_sprs():
print(f">> MSR = {get_msr():08x}")
print(f">> LR = {get_spr(8):08x}")
print(f">> DAR = {get_spr(19):08x}")
print(f">> BAR = {get_spr(159):08x}")
print(f">> DER = {get_spr(149):08x}")
print(f">> SRR0 = {get_spr(26):08x}")
print(f">> SRR1 = {get_spr(27):08x}")
def print_icr():
print(f">> ICR = {get_spr(148):08x}")
def check_icr():
icr = get_spr(148) # it resets automatically
# print(f">> ICR = {icr:08x}")
# ICR[0] - Development port interrupt bit
# ICR[23] - Program interrupt bit
if icr & ~((1<<23) | (1<<0)) or icr == 0:
print(f"Exception occured (ICR = {icr:08x})")
print_sprs()
raise Exception("ICR error")
if icr == 0:
print(f"ICR is 0. Maybe power is off?")
raise Exception("ICR error")
def load_firmware(file):
print("Load firmware...", end='', flush=True)
with open(file, 'rb') as f:
prog_bin = f.read()
prog = [d[0] for d in Struct(">I").iter_unpack(prog_bin)]
write_ram_fast(load_addr, prog)
print(" done")
def run_prog(ep, gprs={}):
set_gpr(31, ep) # select entry point
for r,v in gprs.items():
set_gpr(r, v)
set_spr(149, 0xFFFFFFFF) # set DER to ALL
# set_spr(149, 0x2180000F) # set DER to (CHSTPE | ALIE | PRIE | LBRKE | IBRKE | EBRKE | DPIE)
# set_spr(149, 0x2180200F) # set DER to (CHSTPE | ALIE | PRIE | ITLBMSE? | LBRKE | IBRKE | EBRKE | DPIE)
# set_spr(149, 0x2002000F) # set DER to (CHSTPE | TRE | LBRKE | IBRKE | EBRKE | DPIE)
set_spr(26, load_addr) # set SRR0 (Entry Point)
set_spr(27, 0) # set SRR1 (next MSR)
get_spr(148) # reset ICR
instr( rfi() ) # go
wait_debug() # wait for complete
check_icr() # check if it was fault
def run_prog_simple():
run_prog(0)
def run_prog_memcpy(dst, src, cnt=1):
run_prog(1, gprs={3: dst, 4: src, 5: cnt})
def save_mem_to_file(filename, src, size):
cnt = (size+3)//4 # rounded up
chunk = 0x1000//4
chunk_cnt = cdiv(cnt, chunk)
data = []
for i in range(chunk_cnt):
cur_cnt = min(cnt, chunk)
print(f"[{i+1}/{chunk_cnt}] Read {cur_cnt*4} bytes from address 0x{src:08x}")
data += read_ram_inside(src, cur_cnt)
cnt -= cur_cnt
src += cur_cnt*4
data = Struct(f">{len(data)}I").pack(*data)
data = data[:size]
# print(data)
# for d in data: print(f"{d:02x} ")
with open(filename, 'wb') as f:
f.write(data)
def run_prog_flash_write(addr, data):
print(f"flash write({addr:03x}, {data:02x})")
run_prog(2, gprs={3: addr, 4: data})
def run_prog_flash_read(addr):
run_prog(3, gprs={3: addr})
data = read_ram(ram_buf, 1)[0] & 0xFF
print(f"flash read ({addr:03x}) = {data:02x}")
return data
def run_prog_flash_erase(flash, addr, size):
run_prog(4, gprs={3: flash, 4: addr, 5: size})
# for x in read_ram(ram_buf, 10): print(f"{x:08x}")
return True if read_ram(ram_buf, 1)[0] == 0 else False
def run_prog_flash_program(flash, addr, data, size):
run_prog(5, gprs={3: flash, 4: addr, 5: data, 6: size})
return True if read_ram(ram_buf, 1)[0] == 0 else False
def flash_cmd(addr, cmd):
run_prog_flash_write(0xAAA, 0xAA)
run_prog_flash_write(0x555, 0x55)
run_prog_flash_write(addr, cmd)
def flash_erase_sector(addr, size=1):
print(f"Erase sector at address 0x{addr:08x}")
ret = run_prog_flash_erase(flash_base, addr, size)
if not ret:
raise Exception("Flash erasing failed")
def flash_write_buf(addr, wbuf):
max_chunk = 0x1000
size = len(wbuf)
chunk_cnt = cdiv(size, max_chunk)
flash_erase_sector(addr, size)
for i, buf in enumerate(batched(wbuf, max_chunk)):
cur_size = len(buf)
print(f"[{i+1}/{chunk_cnt}] Write {cur_size} bytes to address 0x{addr:08x}")
buf_aligned = bytes(buf).ljust(cdiv(cur_size, 4) * 4, b'\x00')
pkt_data = [d[0] for d in Struct(">I").iter_unpack(buf_aligned)]
write_ram_fast(ram_buf, pkt_data)
ret = run_prog_flash_program(flash_base, addr, ram_buf, cur_size)
if not ret:
raise Exception("Flash programming failed")
addr += cur_size
def flash_program_from_file(filename, dst, ofs=0, size=None):
with open(filename, 'rb') as f:
data = f.read()
if size is None:
size = len(data)
wbuf = data[ofs:ofs+size]
flash_write_buf(dst, wbuf)
def flash_program_array(dst, data):
if not isinstance(data, Sequence):
data = [data]
wbuf = Struct(f">{len(data)}I").pack(*data)
flash_write_buf(dst, wbuf)
def go_to_addr(addr = 0x100, msr=None):
# write_ram_fast(load_addr, [
# lis(2, addr>>16),
# ori(2, 2, addr),
# mtspr(8, 2),
# 0x4e800020, # blr
# ])
# set_spr(26, load_addr) # set SRR0 (Entry Point)
set_spr(26, addr) # set SRR0 (Entry Point)
if msr:
set_spr(27, msr) # set SRR1 (next MSR)
get_spr(148) # reset ICR
instr( rfi() ) # go
def try_stop(timeout=1):
print("Try NMI trap stop")
cmd_nmi_trap()
if not wait_debug(timeout):
print("Try SRESET stop")
enter_debug()
if not wait_debug(timeout):
print("Failed")
return False
return True
def wait_stop(timeout=None, exit_timeout=1):
if not wait_debug(timeout):
return try_stop(exit_timeout)
return True
def addr2line(dbgsym_file, addr, reloc_ofs=0):
if dbgsym_file is None:
return f"0x{addr:08x}\n"
addr = (addr - reloc_ofs) & 0xffffffff
return subprocess.check_output([
'addr2line', '-pifCa', '-e', dbgsym_file, f"0x{addr:08x}"
]).decode()
def print_target_stack(dbgsym_file, reloc_ofs=0):
# read_ram_f = read_ram_inside
read_ram_f = read_ram
print(f".:", addr2line(dbgsym_file, get_spr(26), reloc_ofs), end='') # SRR0
print(f"0:", addr2line(dbgsym_file, get_spr(8), reloc_ofs), end='') # LR
def get_stack(sp):
frame = read_ram_f(sp)[0]
while frame:
old_frame = frame
frame, lr = read_ram_f(frame, 2)
if old_frame == frame:
print("stack is looped!")
return
yield lr
for i, lr in enumerate(get_stack(get_gpr(1))):
print(f"{1+i}:", addr2line(dbgsym_file, lr-4, reloc_ofs), end='')
def dump_all_immr():
def print_ram1(addr, s):
addr |= 0xfff00000
a = addr
a &= 0xFFFFFFFC
d = read_ram(a, 1)[0]
d >>= 8*(addr&0x3)
d &= (1<<(8*(s+1)))-1
dstr = f"{d:0{2*s}x}".rjust(8)
print(f"{addr:08x}: {dstr}", end='')
print("General System Interface Unit")
print("SIUMCR = ", end=''); print_ram1(0x000, 4); print(" (SIU module configuration register)")
print("SYPCR = ", end=''); print_ram1(0x004, 4); print(" (System protection control register)")
print("SWSR = ", end=''); print_ram1(0x00E, 2); print(" (Software service register)")
print("SIPEND = ", end=''); print_ram1(0x010, 4); print(" (SIU interrupt pending register)")
print("SIMASK = ", end=''); print_ram1(0x014, 4); print(" (SIU interrupt mask register)")
print("SIEL = ", end=''); print_ram1(0x018, 4); print(" (SIU interrupt edge/level register)")
print("SIVEC = ", end=''); print_ram1(0x01C, 4); print(" (SIU interrupt vector register)")
print("TESR = ", end=''); print_ram1(0x020, 4); print(" (Transfer error status register)")
print("SDCR = ", end=''); print_ram1(0x030, 4); print(" (SDMA configuration register)")
print()
print("PCMCIA")
print("PBR0 = ", end=''); print_ram1(0x080, 4); print(" (PCMCIA interface base register 0)")
print("POR0 = ", end=''); print_ram1(0x084, 4); print(" (PCMCIA interface option register 0)")
print("PBR1 = ", end=''); print_ram1(0x088, 4); print(" (PCMCIA interface base register 1)")
print("POR1 = ", end=''); print_ram1(0x08C, 4); print(" (PCMCIA interface option register 1)")
print("PBR2 = ", end=''); print_ram1(0x090, 4); print(" (PCMCIA interface base register 2)")
print("POR2 = ", end=''); print_ram1(0x094, 4); print(" (PCMCIA interface option register 2)")
print("PBR3 = ", end=''); print_ram1(0x098, 4); print(" (PCMCIA interface base register 3)")
print("POR3 = ", end=''); print_ram1(0x09C, 4); print(" (PCMCIA interface option register 3)")
print("PBR4 = ", end=''); print_ram1(0x0A0, 4); print(" (PCMCIA interface base register 4)")
print("POR4 = ", end=''); print_ram1(0x0A4, 4); print(" (PCMCIA interface option register 4)")
print("PBR5 = ", end=''); print_ram1(0x0A8, 4); print(" (PCMCIA interface base register 5)")
print("POR5 = ", end=''); print_ram1(0x0AC, 4); print(" (PCMCIA interface option register 5)")
print("PBR6 = ", end=''); print_ram1(0x0B0, 4); print(" (PCMCIA interface base register 6)")
print("POR6 = ", end=''); print_ram1(0x0B4, 4); print(" (PCMCIA interface option register 6)")
print("PBR7 = ", end=''); print_ram1(0x0B8, 4); print(" (PCMCIA interface base register 7)")
print("POR7 = ", end=''); print_ram1(0x0BC, 4); print(" (PCMCIA interface option register 7)")
print("PGCRA = ", end=''); print_ram1(0x0E0, 4); print(" (PCMCIA interface general control register A)")
print("PGCRB = ", end=''); print_ram1(0x0E4, 4); print(" (PCMCIA interface general control register B)")
print("PSCR = ", end=''); print_ram1(0x0E8, 4); print(" (PCMCIA interface status changed register)")
print("PIPR = ", end=''); print_ram1(0x0F0, 4); print(" (PCMCIA interface input pins register)")
print("PER = ", end=''); print_ram1(0x0F8, 4); print(" (PCMCIA interface enable register)")
print()
print("Memory Controller")
print("BR0 = ", end=''); print_ram1(0x100, 4); print(" (Base register bank 0)")
print("OR0 = ", end=''); print_ram1(0x104, 4); print(" (Option register bank 0)")
print("BR1 = ", end=''); print_ram1(0x108, 4); print(" (Base register bank 1)")
print("OR1 = ", end=''); print_ram1(0x10C, 4); print(" (Option register bank 1)")
print("BR2 = ", end=''); print_ram1(0x110, 4); print(" (Base register bank 2)")
print("OR2 = ", end=''); print_ram1(0x114, 4); print(" (Option register bank 2)")
print("BR3 = ", end=''); print_ram1(0x118, 4); print(" (Base register bank 3)")
print("OR3 = ", end=''); print_ram1(0x11C, 4); print(" (Option register bank 3)")
print("BR4 = ", end=''); print_ram1(0x120, 4); print(" (Base register bank 4)")
print("OR4 = ", end=''); print_ram1(0x124, 4); print(" (Option register bank 4)")
print("BR5 = ", end=''); print_ram1(0x128, 4); print(" (Base register bank 5)")
print("OR5 = ", end=''); print_ram1(0x12C, 4); print(" (Option register bank 5)")
print("BR6 = ", end=''); print_ram1(0x130, 4); print(" (Base register bank 6)")
print("OR6 = ", end=''); print_ram1(0x134, 4); print(" (Option register bank 6)")
print("BR7 = ", end=''); print_ram1(0x138, 4); print(" (Base register bank 7)")
print("OR7 = ", end=''); print_ram1(0x13C, 4); print(" (Option register bank 7)")
print("MAR = ", end=''); print_ram1(0x164, 4); print(" (Memory address register)")
print("MCR = ", end=''); print_ram1(0x168, 4); print(" (Memory command register)")
print("MAMR = ", end=''); print_ram1(0x170, 4); print(" (Machine A mode register)")
print("MBMR = ", end=''); print_ram1(0x174, 4); print(" (Machine B mode register)")
print("MSTAT = ", end=''); print_ram1(0x178, 2); print(" (Memory status register)")
print("MPTPR = ", end=''); print_ram1(0x17A, 2); print(" (Memory periodic timer prescaler)")
print("MDR = ", end=''); print_ram1(0x17C, 4); print(" (Memory data register)")
print()
print("System Integration Timers")
print("TBSCR = ", end=''); print_ram1(0x200, 2); print(" (Timebase status and control register)")
print("TBREFA = ", end=''); print_ram1(0x204, 4); print(" (Timebase reference register A)")
print("TBREFB = ", end=''); print_ram1(0x208, 4); print(" (Timebase reference register B)")
print("PISCR = ", end=''); print_ram1(0x240, 2); print(" (Periodic interrupt status and control register)")
print("PITC = ", end=''); print_ram1(0x244, 4); print(" (Periodic interrupt count register)")
print("PITR = ", end=''); print_ram1(0x248, 4); print(" (Periodic interrupt timer register)")
print()
print("Clocks and Reset")
print("SCCR = ", end=''); print_ram1(0x280, 4); print(" (System clock reset control register)")
print("PLPRCR = ", end=''); print_ram1(0x284, 4); print(" (PLL and reset control register)")
print("RSR = ", end=''); print_ram1(0x288, 4); print(" (Reset status register)")
print()
print("System Integration Timers Keys")
print("TBSCRK = ", end=''); print_ram1(0x300, 4); print(" (Timebase status and control register key)")
print("TBREFAK = ", end=''); print_ram1(0x304, 4); print(" (Timebase reference register A key)")
print("TBREFBK = ", end=''); print_ram1(0x308, 4); print(" (Timebase reference register B key)")
print("TBK = ", end=''); print_ram1(0x30C, 4); print(" (Timebase/decrementer register key)")
print("PISCRK = ", end=''); print_ram1(0x340, 4); print(" (Periodic interrupt status and control register key)")
print("PITCK = ", end=''); print_ram1(0x344, 4); print(" (Periodic interrupt count register key)")
print()
print("Clocks and Reset Keys")
print("SCCRK = ", end=''); print_ram1(0x380, 4); print(" (System clock control key)")
print("PLPRCRK = ", end=''); print_ram1(0x384, 4); print(" (PLL and reset control register key)")
print("RSRK = ", end=''); print_ram1(0x388, 4); print(" (Reset status register key)")
print()
print("I2C Controller")
print("I2MOD = ", end=''); print_ram1(0x860, 1); print(" (I2C mode register)")
print("I2ADD = ", end=''); print_ram1(0x864, 1); print(" (I2C address register)")
print("I2BRG = ", end=''); print_ram1(0x868, 1); print(" (I2C BRG register)")
print("I2COM = ", end=''); print_ram1(0x86C, 1); print(" (I2C command register)")
print("I2CER = ", end=''); print_ram1(0x870, 1); print(" (I2C event register)")
print("I2CMR = ", end=''); print_ram1(0x874, 1); print(" (I2C mask register)")
print()
print("DMA")
print("SDAR = ", end=''); print_ram1(0x904, 4); print(" (SDMA address register)")
print("SDSR = ", end=''); print_ram1(0x908, 1); print(" (SDMA status register)")
print("SDMR = ", end=''); print_ram1(0x90C, 1); print(" (SDMA mask register)")
print("IDSR1 = ", end=''); print_ram1(0x910, 1); print(" (IDMA1 status register)")
print("IDMR1 = ", end=''); print_ram1(0x914, 1); print(" (IDMA1 mask register)")
print("IDSR2 = ", end=''); print_ram1(0x918, 1); print(" (IDMA2 status register)")
print("IDMR2 = ", end=''); print_ram1(0x91C, 1); print(" (IDMA2 mask register)")
print()
print("Communications Processor Module Interrupt Control")
print("CIVR = ", end=''); print_ram1(0x930, 2); print(" (CPM interrupt vector register)")
print("CICR = ", end=''); print_ram1(0x940, 4); print(" (CPM interrupt configuration register)")
print("CIPR = ", end=''); print_ram1(0x944, 4); print(" (CPM interrupt pending register)")
print("CIMR = ", end=''); print_ram1(0x948, 4); print(" (CPM interrupt mask register)")
print("CISR = ", end=''); print_ram1(0x94C, 4); print(" (CPM in-service register)")
print()
print("Input/Output Port")
print("PADIR = ", end=''); print_ram1(0x950, 2); print(" (Port A data direction register)")
print("PAPAR = ", end=''); print_ram1(0x952, 2); print(" (Port A pin assignment register)")
print("PAODR = ", end=''); print_ram1(0x954, 2); print(" (Port A open drain register)")
print("PADAT = ", end=''); print_ram1(0x956, 2); print(" (Port A data register)")
print("PCDIR = ", end=''); print_ram1(0x960, 2); print(" (Port C data direction register)")
print("PCPAR = ", end=''); print_ram1(0x962, 2); print(" (Port C pin assignment register)")
print("PCSO = ", end=''); print_ram1(0x964, 2); print(" (Port C special options register)")
print("PCDAT = ", end=''); print_ram1(0x966, 2); print(" (Port C data register)")
print("PCINT = ", end=''); print_ram1(0x968, 2); print(" (Port C interrupt control register)")
print("PDDIR = ", end=''); print_ram1(0x970, 2); print(" (Port D data direction register)")
print("PDPAR = ", end=''); print_ram1(0x972, 2); print(" (Port D pin assignment register)")
print("PDDAT = ", end=''); print_ram1(0x976, 2); print(" (Port D data register)")
print("UTMODE = ", end=''); print_ram1(0x978, 4); print(" (UTOPIA mode register 42.2/-1)")
print()
print("CPM General-Purpose Timers")
print("TGCR = ", end=''); print_ram1(0x980, 2); print(" (Timer global configuration register)")
print("TMR1 = ", end=''); print_ram1(0x990, 2); print(" (Timer 1 mode register)")
print("TMR2 = ", end=''); print_ram1(0x992, 2); print(" (Timer 2 mode register)")
print("TRR1 = ", end=''); print_ram1(0x994, 2); print(" (Timer 1 reference register)")
print("TRR2 = ", end=''); print_ram1(0x996, 2); print(" (Timer 2 reference register)")
print("TCR1 = ", end=''); print_ram1(0x998, 2); print(" (Timer 1 capture register)")
print("TCR2 = ", end=''); print_ram1(0x99A, 2); print(" (Timer 2 capture register)")
print("TCN1 = ", end=''); print_ram1(0x99C, 2); print(" (Timer 1 counter)")
print("TCN2 = ", end=''); print_ram1(0x99E, 2); print(" (Timer 2 counter)")
print("TMR3 = ", end=''); print_ram1(0x9A0, 2); print(" (Timer 3 mode register)")
print("TMR4 = ", end=''); print_ram1(0x9A2, 2); print(" (Timer 4 mode register)")
print("TRR3 = ", end=''); print_ram1(0x9A4, 2); print(" (Timer 3 reference register)")
print("TRR4 = ", end=''); print_ram1(0x9A6, 2); print(" (Timer 4 reference register)")
print("TCR3 = ", end=''); print_ram1(0x9A8, 2); print(" (Timer 3 capture register)")
print("TCR4 = ", end=''); print_ram1(0x9AA, 2); print(" (Timer 4 capture register)")
print("TCN3 = ", end=''); print_ram1(0x9AC, 2); print(" (Timer 3 counter)")
print("TCN4 = ", end=''); print_ram1(0x9AE, 2); print(" (Timer 4 counter)")
print("TER1 = ", end=''); print_ram1(0x9B0, 2); print(" (Timer 1 event register)")
print("TER2 = ", end=''); print_ram1(0x9B2, 2); print(" (Timer 2 event register)")
print("TER3 = ", end=''); print_ram1(0x9B4, 2); print(" (Timer 3 event register)")
print("TER4 = ", end=''); print_ram1(0x9B6, 2); print(" (Timer 4 event register)")
print()
print("Communications Processor (CP)")
print("CPCR = ", end=''); print_ram1(0x9C0, 2); print(" (CP command register)")
print("RCCR = ", end=''); print_ram1(0x9C4, 2); print(" (RISC controller configuration register)")
print("RMDS = ", end=''); print_ram1(0x9C7, 1); print(" (RISC microcode development support control register)")
print("RCTR1 = ", end=''); print_ram1(0x9CC, 2); print(" (RISC controller trap register 1)")
print("RCTR2 = ", end=''); print_ram1(0x9CE, 2); print(" (RISC controller trap register 2)")
print("RCTR3 = ", end=''); print_ram1(0x9D0, 2); print(" (RISC controller trap register 3)")
print("RCTR4 = ", end=''); print_ram1(0x9D2, 2); print(" (RISC controller trap register 4)")
print("RTER = ", end=''); print_ram1(0x9D6, 2); print(" (RISC timer event register)")
print("RTMR = ", end=''); print_ram1(0x9DA, 2); print(" (RISC timers mask register)")
print()
print("Baud Rate Generators")
print("BRGC1 = ", end=''); print_ram1(0x9F0, 4); print(" (BRG1 configuration register)")
print("BRGC2 = ", end=''); print_ram1(0x9F4, 4); print(" (BRG2 configuration register)")
print("BRGC3 = ", end=''); print_ram1(0x9F8, 4); print(" (BRG3 configuration register)")
print("BRGC4 = ", end=''); print_ram1(0x9FC, 4); print(" (BRG4 configuration register)")
print()
print("Serial Communications Controller 1 (SCC1)")
print("GSMR_L1 = ", end=''); print_ram1(0xA00, 4); print(" (SCC1 general mode register)")
print("GSMR_H1 = ", end=''); print_ram1(0xA04, 4); print(" (SCC1 general mode register)")
print("PSMR1 = ", end=''); print_ram1(0xA08, 2); print(" (SCC1 protocol specific mode register)")
print("TODR1 = ", end=''); print_ram1(0xA0C, 2); print(" (SCC1 transmit-on-demand register)")
print("DSR1 = ", end=''); print_ram1(0xA0E, 2); print(" (SCC1 data synchronization register)")
print("SCCE1 = ", end=''); print_ram1(0xA10, 2); print(" (SCC1 event register)")
print("SCCM1 = ", end=''); print_ram1(0xA14, 2); print(" (SCC1 mask register)")
print("SCCS1 = ", end=''); print_ram1(0xA17, 1); print(" (SCC1 status register)")
print()
print("Serial Communications Controller 2 (SCC2)")
print("GSMR_L2 = ", end=''); print_ram1(0xA20, 4); print(" (SCC2 general mode register)")
print("GSMR_H2 = ", end=''); print_ram1(0xA24, 4); print(" (SCC2 general mode register)")
print("PSMR2 = ", end=''); print_ram1(0xA28, 2); print(" (SCC2 protocol-specific mode register)")
print("TODR2 = ", end=''); print_ram1(0xA2C, 2); print(" (SCC2 transmit on demand register)")
print("DSR2 = ", end=''); print_ram1(0xA2E, 2); print(" (SCC2 data synchronization register)")
print("SCCE2 = ", end=''); print_ram1(0xA30, 2); print(" (SCC2 event register)")
print("SCCM2 = ", end=''); print_ram1(0xA34, 2); print(" (SCC2 mask register)")
print("SCCS2 = ", end=''); print_ram1(0xA37, 1); print(" (SCC2 status register)")
print("GSMR_L3 = ", end=''); print_ram1(0xA40, 4); print(" (SCC3 general mode register)")
print("GSMR_H3 = ", end=''); print_ram1(0xA44, 4); print(" (SCC3 general mode register)")
print("PSMR3 = ", end=''); print_ram1(0xA48, 2); print(" (SCC3 protocol specific mode register)")
print("TODR3 = ", end=''); print_ram1(0xA4C, 2); print(" (SCC3 transmit on demand register)")
print("DSR3 = ", end=''); print_ram1(0xA4E, 2); print(" (SCC3 data synchronization register)")
print("SCCE3 = ", end=''); print_ram1(0xA50, 2); print(" (SCC3 event register)")
print("SCCM3 = ", end=''); print_ram1(0xA54, 2); print(" (SCC3 mask register)")
print("SCCS3 = ", end=''); print_ram1(0xA57, 1); print(" (SCC3 status register)")
print("GSMR_L4 = ", end=''); print_ram1(0xA60, 4); print(" (SCC4 general mode register)")
print("GSMR_H4 = ", end=''); print_ram1(0xA64, 4); print(" (SCC4 general mode register)")
print("PSMR4 = ", end=''); print_ram1(0xA68, 2); print(" (SCC4 protocol specific mode register)")
print("TODR4 = ", end=''); print_ram1(0xA6C, 2); print(" (SCC4 transmit on demand register)")
print("DSR4 = ", end=''); print_ram1(0xA6E, 2); print(" (SCC4 data synchronization register)")
print("SCCE4 = ", end=''); print_ram1(0xA70, 2); print(" (SCC4 event register)")
print("SCCM4 = ", end=''); print_ram1(0xA74, 2); print(" (SCC4 mask register)")
print("SCCS4 = ", end=''); print_ram1(0xA77, 1); print(" (SCC4 status register)")
print()
print("Serial Management Controller 1 (SMC1)")
print("SMCMR1 = ", end=''); print_ram1(0xA82, 2); print(" (SMC1 mode register)")
print("SMCE1 = ", end=''); print_ram1(0xA86, 1); print(" (SMC1 event register)")
print("SMCM1 = ", end=''); print_ram1(0xA8A, 1); print(" (SMC1 mask register)")
print("SMCE2 = ", end=''); print_ram1(0xA96, 1); print(" (SMC2 event register)")
print("SMCM2 = ", end=''); print_ram1(0xA9A, 1); print(" (SMC2 mask register)")
print()
print("Serial Peripheral Interface (SPI)")
print("SPMODE = ", end=''); print_ram1(0xAA0, 2); print(" (SPI mode register)")
print("SPIE = ", end=''); print_ram1(0xAA6, 1); print(" (SPI event register)")
print("SPIM = ", end=''); print_ram1(0xAAA, 1); print(" (SPI mask register)")
print("SPCOM = ", end=''); print_ram1(0xAAD, 1); print(" (SPI command register)")
print()
print("Parallel Interface Port (PIP) and Port B")
print("PIPC = ", end=''); print_ram1(0xAB2, 2); print(" (PIP configuration register)")
print("PTPR = ", end=''); print_ram1(0xAB6, 2); print(" (PIP timing parameters register)")
print("PBDIR = ", end=''); print_ram1(0xAB8, 4); print(" (Port B data direction register)")
print("PBPAR = ", end=''); print_ram1(0xABC, 4); print(" (Port B pin assignment register)")
print("PBODR = ", end=''); print_ram1(0xAC0, 4); print(" (Port B open drain register)")
print("PBDAT = ", end=''); print_ram1(0xAC4, 4); print(" (Port B data register)")
print()
print("Serial Interface (SI)")
print("SIMODE = ", end=''); print_ram1(0xAE0, 4); print(" (SI mode register)")
print("SIGMR = ", end=''); print_ram1(0xAE4, 1); print(" (SI global mode register)")
print("SISTR = ", end=''); print_ram1(0xAE6, 1); print(" (SI status register)")
print("SICMR = ", end=''); print_ram1(0xAE7, 1); print(" (SI command register)")
print("SICR = ", end=''); print_ram1(0xAEC, 4); print(" (SI clock route register)")
print("SIRP = ", end=''); print_ram1(0xAF0, 4); print(" (Serial interface RAM pointer register)")
# print("SIRAM = ", end=''); print_ram1(0xC00, 512); print(" (SI routing RAM)")
print()
print("Fast Ethernet Controller (FEC)")
print("ADDR_LOW = ", end=''); print_ram1(0xE00, 4); print(" ()")
print("ADDR_HIGH = ", end=''); print_ram1(0xE04, 4); print(" ()")
print("HASH_TABLE_HIGH = ", end=''); print_ram1(0xE08, 4); print(" ()")
print("HASH_TABLE_LOW = ", end=''); print_ram1(0xE0C, 4); print(" ()")